omap_device.c 26.8 KB
Newer Older
1 2 3
/*
 * omap_device implementation
 *
4
 * Copyright (C) 2009-2010 Nokia Corporation
5
 * Paul Walmsley, Kevin Hilman
6 7
 *
 * Developed in collaboration with (alphabetical order): Benoit
8
 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
 * Woodruff
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This code provides a consistent interface for OMAP device drivers
 * to control power management and interconnect properties of their
 * devices.
 *
 * In the medium- to long-term, this code should either be
 * a) implemented via arch-specific pointers in platform_data
 * or
 * b) implemented as a proper omap_bus/omap_device in Linux, no more
 *    platform_data func pointers
 *
 *
 * Guidelines for usage by driver authors:
 *
 * 1. These functions are intended to be used by device drivers via
 * function pointers in struct platform_data.  As an example,
 * omap_device_enable() should be passed to the driver as
 *
 * struct foo_driver_platform_data {
 * ...
 *      int (*device_enable)(struct platform_device *pdev);
 * ...
 * }
 *
 * Note that the generic "device_enable" name is used, rather than
 * "omap_device_enable".  This is so other architectures can pass in their
 * own enable/disable functions here.
 *
 * This should be populated during device setup:
 *
 * ...
 * pdata->device_enable = omap_device_enable;
 * ...
 *
 * 2. Drivers should first check to ensure the function pointer is not null
 * before calling it, as in:
 *
 * if (pdata->device_enable)
 *     pdata->device_enable(pdev);
 *
 * This allows other architectures that don't use similar device_enable()/
 * device_shutdown() functions to execute normally.
 *
 * ...
 *
 * Suggested usage by device drivers:
 *
 * During device initialization:
 * device_enable()
 *
 * During device idle:
 * (save remaining device context if necessary)
 * device_idle();
 *
 * During device resume:
 * device_enable();
 * (restore context if necessary)
 *
 * During device shutdown:
 * device_shutdown()
 * (device must be reinitialized at this point to use it again)
 *
 */
#undef DEBUG

#include <linux/kernel.h>
#include <linux/platform_device.h>
82
#include <linux/slab.h>
83 84
#include <linux/err.h>
#include <linux/io.h>
85
#include <linux/clk.h>
86
#include <linux/clkdev.h>
87
#include <linux/pm_runtime.h>
88

89 90
#include <plat/omap_device.h>
#include <plat/omap_hwmod.h>
91
#include <plat/clock.h>
92 93 94 95 96

/* These parameters are passed to _omap_device_{de,}activate() */
#define USE_WAKEUP_LAT			0
#define IGNORE_WAKEUP_LAT		1

97 98
static int omap_device_register(struct platform_device *pdev);
static int omap_early_device_register(struct platform_device *pdev);
99

100 101 102 103 104 105 106 107
static struct omap_device_pm_latency omap_default_latency[] = {
	{
		.deactivate_func = omap_device_idle_hwmods,
		.activate_func   = omap_device_enable_hwmods,
		.flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
	}
};

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* Private functions */

/**
 * _omap_device_activate - increase device readiness
 * @od: struct omap_device *
 * @ignore_lat: increase to latency target (0) or full readiness (1)?
 *
 * Increase readiness of omap_device @od (thus decreasing device
 * wakeup latency, but consuming more power).  If @ignore_lat is
 * IGNORE_WAKEUP_LAT, make the omap_device fully active.  Otherwise,
 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
 * latency is greater than the requested maximum wakeup latency, step
 * backwards in the omap_device_pm_latency table to ensure the
 * device's maximum wakeup latency is less than or equal to the
 * requested maximum wakeup latency.  Returns 0.
 */
static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
{
126
	struct timespec a, b, c;
127

128
	dev_dbg(&od->pdev->dev, "omap_device: activating\n");
129 130 131

	while (od->pm_lat_level > 0) {
		struct omap_device_pm_latency *odpl;
132
		unsigned long long act_lat = 0;
133 134 135 136 137 138 139 140 141

		od->pm_lat_level--;

		odpl = od->pm_lats + od->pm_lat_level;

		if (!ignore_lat &&
		    (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
			break;

142
		read_persistent_clock(&a);
143 144 145 146

		/* XXX check return code */
		odpl->activate_func(od);

147
		read_persistent_clock(&b);
148

149
		c = timespec_sub(b, a);
150
		act_lat = timespec_to_ns(&c);
151

152
		dev_dbg(&od->pdev->dev,
153 154
			"omap_device: pm_lat %d: activate: elapsed time "
			"%llu nsec\n", od->pm_lat_level, act_lat);
155

156 157 158 159
		if (act_lat > odpl->activate_lat) {
			odpl->activate_lat_worst = act_lat;
			if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
				odpl->activate_lat = act_lat;
160
				dev_dbg(&od->pdev->dev,
161 162 163
					"new worst case activate latency "
					"%d: %llu\n",
					od->pm_lat_level, act_lat);
164
			} else
165
				dev_warn(&od->pdev->dev,
166 167 168 169
					 "activate latency %d "
					 "higher than exptected. (%llu > %d)\n",
					 od->pm_lat_level, act_lat,
					 odpl->activate_lat);
170
		}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

		od->dev_wakeup_lat -= odpl->activate_lat;
	}

	return 0;
}

/**
 * _omap_device_deactivate - decrease device readiness
 * @od: struct omap_device *
 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
 *
 * Decrease readiness of omap_device @od (thus increasing device
 * wakeup latency, but conserving power).  If @ignore_lat is
 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive.  Otherwise,
 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
 * latency is less than the requested maximum wakeup latency, step
 * forwards in the omap_device_pm_latency table to ensure the device's
 * maximum wakeup latency is less than or equal to the requested
 * maximum wakeup latency.  Returns 0.
 */
static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
{
194
	struct timespec a, b, c;
195

196
	dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
197 198 199

	while (od->pm_lat_level < od->pm_lats_cnt) {
		struct omap_device_pm_latency *odpl;
200
		unsigned long long deact_lat = 0;
201 202 203 204 205 206 207 208

		odpl = od->pm_lats + od->pm_lat_level;

		if (!ignore_lat &&
		    ((od->dev_wakeup_lat + odpl->activate_lat) >
		     od->_dev_wakeup_lat_limit))
			break;

209
		read_persistent_clock(&a);
210 211 212 213

		/* XXX check return code */
		odpl->deactivate_func(od);

214
		read_persistent_clock(&b);
215

216
		c = timespec_sub(b, a);
217
		deact_lat = timespec_to_ns(&c);
218

219
		dev_dbg(&od->pdev->dev,
220 221
			"omap_device: pm_lat %d: deactivate: elapsed time "
			"%llu nsec\n", od->pm_lat_level, deact_lat);
222

223 224 225 226
		if (deact_lat > odpl->deactivate_lat) {
			odpl->deactivate_lat_worst = deact_lat;
			if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
				odpl->deactivate_lat = deact_lat;
227
				dev_dbg(&od->pdev->dev,
228 229 230
					"new worst case deactivate latency "
					"%d: %llu\n",
					od->pm_lat_level, deact_lat);
231
			} else
232
				dev_warn(&od->pdev->dev,
233 234 235 236
					 "deactivate latency %d "
					 "higher than exptected. (%llu > %d)\n",
					 od->pm_lat_level, deact_lat,
					 odpl->deactivate_lat);
237 238
		}

239 240 241 242 243 244 245 246
		od->dev_wakeup_lat += odpl->activate_lat;

		od->pm_lat_level++;
	}

	return 0;
}

247 248 249 250 251 252 253 254 255
static void _add_clkdev(struct omap_device *od, const char *clk_alias,
		       const char *clk_name)
{
	struct clk *r;
	struct clk_lookup *l;

	if (!clk_alias || !clk_name)
		return;

256
	dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
257

258
	r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
259
	if (!IS_ERR(r)) {
260
		dev_warn(&od->pdev->dev,
261
			 "alias %s already exists\n", clk_alias);
262 263 264 265 266 267
		clk_put(r);
		return;
	}

	r = omap_clk_get_by_name(clk_name);
	if (IS_ERR(r)) {
268
		dev_err(&od->pdev->dev,
269
			"omap_clk_get_by_name for %s failed\n", clk_name);
270 271 272
		return;
	}

273
	l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
274
	if (!l) {
275
		dev_err(&od->pdev->dev,
276
			"clkdev_alloc for %s failed\n", clk_alias);
277 278 279 280 281 282
		return;
	}

	clkdev_add(l);
}

283
/**
284 285
 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
 * and main clock
286
 * @od: struct omap_device *od
287
 * @oh: struct omap_hwmod *oh
288
 *
289 290 291
 * For the main clock and every optional clock present per hwmod per
 * omap_device, this function adds an entry in the clkdev table of the
 * form <dev-id=dev_name, con-id=role> if it does not exist already.
292 293 294 295 296 297
 *
 * The function is called from inside omap_device_build_ss(), after
 * omap_device_register.
 *
 * This allows drivers to get a pointer to its optional clocks based on its role
 * by calling clk_get(<dev*>, <role>).
298
 * In the case of the main clock, a "fck" alias is used.
299 300 301
 *
 * No return value.
 */
302 303
static void _add_hwmod_clocks_clkdev(struct omap_device *od,
				     struct omap_hwmod *oh)
304 305 306
{
	int i;

307
	_add_clkdev(od, "fck", oh->main_clk);
308

309 310
	for (i = 0; i < oh->opt_clks_cnt; i++)
		_add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
311 312
}

313 314 315

/* Public functions for use by core code */

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
/**
 * omap_device_get_context_loss_count - get lost context count
 * @od: struct omap_device *
 *
 * Using the primary hwmod, query the context loss count for this
 * device.
 *
 * Callers should consider context for this device lost any time this
 * function returns a value different than the value the caller got
 * the last time it called this function.
 *
 * If any hwmods exist for the omap_device assoiated with @pdev,
 * return the context loss counter for that hwmod, otherwise return
 * zero.
 */
u32 omap_device_get_context_loss_count(struct platform_device *pdev)
{
	struct omap_device *od;
	u32 ret = 0;

336
	od = to_omap_device(pdev);
337 338 339 340 341 342 343

	if (od->hwmods_cnt)
		ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);

	return ret;
}

344 345 346 347 348 349 350 351 352
/**
 * omap_device_count_resources - count number of struct resource entries needed
 * @od: struct omap_device *
 *
 * Count the number of struct resource entries needed for this
 * omap_device @od.  Used by omap_device_build_ss() to determine how
 * much memory to allocate before calling
 * omap_device_fill_resources().  Returns the count.
 */
353
static int omap_device_count_resources(struct omap_device *od)
354 355 356 357
{
	int c = 0;
	int i;

358 359
	for (i = 0; i < od->hwmods_cnt; i++)
		c += omap_hwmod_count_resources(od->hwmods[i]);
360 361

	pr_debug("omap_device: %s: counted %d total resources across %d "
362
		 "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

	return c;
}

/**
 * omap_device_fill_resources - fill in array of struct resource
 * @od: struct omap_device *
 * @res: pointer to an array of struct resource to be filled in
 *
 * Populate one or more empty struct resource pointed to by @res with
 * the resource data for this omap_device @od.  Used by
 * omap_device_build_ss() after calling omap_device_count_resources().
 * Ideally this function would not be needed at all.  If omap_device
 * replaces platform_device, then we can specify our own
 * get_resource()/ get_irq()/etc functions that use the underlying
 * omap_hwmod information.  Or if platform_device is extended to use
 * subarchitecture-specific function pointers, the various
 * platform_device functions can simply call omap_device internal
 * functions to get device resources.  Hacking around the existing
 * platform_device code wastes memory.  Returns 0.
 */
384 385
static int omap_device_fill_resources(struct omap_device *od,
				      struct resource *res)
386 387 388 389
{
	int c = 0;
	int i, r;

390 391
	for (i = 0; i < od->hwmods_cnt; i++) {
		r = omap_hwmod_fill_resources(od->hwmods[i], res);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
		res += r;
		c += r;
	}

	return 0;
}

/**
 * omap_device_build - build and register an omap_device with one omap_hwmod
 * @pdev_name: name of the platform_device driver to use
 * @pdev_id: this platform_device's connection ID
 * @oh: ptr to the single omap_hwmod that backs this omap_device
 * @pdata: platform_data ptr to associate with the platform_device
 * @pdata_len: amount of memory pointed to by @pdata
 * @pm_lats: pointer to a omap_device_pm_latency array for this device
 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
408
 * @is_early_device: should the device be registered as an early device or not
409 410 411 412 413 414 415
 *
 * Convenience function for building and registering a single
 * omap_device record, which in turn builds and registers a
 * platform_device record.  See omap_device_build_ss() for more
 * information.  Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
 * passes along the return value of omap_device_build_ss().
 */
416
struct platform_device *omap_device_build(const char *pdev_name, int pdev_id,
417 418 419
				      struct omap_hwmod *oh, void *pdata,
				      int pdata_len,
				      struct omap_device_pm_latency *pm_lats,
420
				      int pm_lats_cnt, int is_early_device)
421 422 423 424 425 426 427
{
	struct omap_hwmod *ohs[] = { oh };

	if (!oh)
		return ERR_PTR(-EINVAL);

	return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
428 429
				    pdata_len, pm_lats, pm_lats_cnt,
				    is_early_device);
430 431 432 433 434 435 436 437 438 439 440
}

/**
 * omap_device_build_ss - build and register an omap_device with multiple hwmods
 * @pdev_name: name of the platform_device driver to use
 * @pdev_id: this platform_device's connection ID
 * @oh: ptr to the single omap_hwmod that backs this omap_device
 * @pdata: platform_data ptr to associate with the platform_device
 * @pdata_len: amount of memory pointed to by @pdata
 * @pm_lats: pointer to a omap_device_pm_latency array for this device
 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
441
 * @is_early_device: should the device be registered as an early device or not
442 443 444 445 446 447 448
 *
 * Convenience function for building and registering an omap_device
 * subsystem record.  Subsystem records consist of multiple
 * omap_hwmods.  This function in turn builds and registers a
 * platform_device record.  Returns an ERR_PTR() on error, or passes
 * along the return value of omap_device_register().
 */
449
struct platform_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
450 451 452
					 struct omap_hwmod **ohs, int oh_cnt,
					 void *pdata, int pdata_len,
					 struct omap_device_pm_latency *pm_lats,
453
					 int pm_lats_cnt, int is_early_device)
454 455
{
	int ret = -ENOMEM;
456
	struct platform_device *pdev;
457 458
	struct omap_device *od;
	struct resource *res = NULL;
459
	int i, res_count;
460 461 462 463 464 465 466 467
	struct omap_hwmod **hwmods;

	if (!ohs || oh_cnt == 0 || !pdev_name)
		return ERR_PTR(-EINVAL);

	if (!pdata && pdata_len > 0)
		return ERR_PTR(-EINVAL);

468 469 470 471 472 473
	pdev = platform_device_alloc(pdev_name, pdev_id);
	if (!pdev) {
		ret = -ENOMEM;
		goto odbs_exit;
	}

474 475 476 477
	pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
		 oh_cnt);

	od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
478 479 480 481
	if (!od) {
		ret = -ENOMEM;
		goto odbs_exit1;
	}
482 483 484 485 486
	od->hwmods_cnt = oh_cnt;

	hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
			 GFP_KERNEL);
	if (!hwmods)
487
		goto odbs_exit2;
488 489 490

	memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
	od->hwmods = hwmods;
491
	od->pdev = pdev;
492 493 494 495 496 497 498

	res_count = omap_device_count_resources(od);
	if (res_count > 0) {
		res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
		if (!res)
			goto odbs_exit3;

499
		omap_device_fill_resources(od, res);
500

501 502 503 504 505 506 507 508
		ret = platform_device_add_resources(pdev, res, res_count);
		kfree(res);

		if (ret)
			goto odbs_exit3;
	}

	ret = platform_device_add_data(pdev, pdata, pdata_len);
509
	if (ret)
510
		goto odbs_exit3;
511

512
	pdev->archdata.od = od;
513

514
	if (is_early_device)
515
		ret = omap_early_device_register(pdev);
516
	else
517 518 519 520
		ret = omap_device_register(pdev);
	if (ret)
		goto odbs_exit3;

521 522 523 524 525
	if (!pm_lats) {
		pm_lats = omap_default_latency;
		pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
	}

526
	od->pm_lats_cnt = pm_lats_cnt;
527 528 529 530 531
	od->pm_lats = kmemdup(pm_lats,
			sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
			GFP_KERNEL);
	if (!od->pm_lats)
		goto odbs_exit3;
532

533
	for (i = 0; i < oh_cnt; i++) {
534
		hwmods[i]->od = od;
535
		_add_hwmod_clocks_clkdev(od, hwmods[i]);
536
	}
537

538
	return pdev;
539 540 541

odbs_exit3:
	kfree(hwmods);
542
odbs_exit2:
543
	kfree(od);
544 545 546
odbs_exit1:
	platform_device_put(pdev);
odbs_exit:
547 548 549 550 551 552

	pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);

	return ERR_PTR(ret);
}

553 554 555 556 557 558 559 560 561
/**
 * omap_early_device_register - register an omap_device as an early platform
 * device.
 * @od: struct omap_device * to register
 *
 * Register the omap_device structure.  This currently just calls
 * platform_early_add_device() on the underlying platform_device.
 * Returns 0 by default.
 */
562
static int omap_early_device_register(struct platform_device *pdev)
563 564 565
{
	struct platform_device *devices[1];

566
	devices[0] = pdev;
567 568 569 570
	early_platform_add_devices(devices, 1);
	return 0;
}

571
#ifdef CONFIG_PM_RUNTIME
572 573 574
static int _od_runtime_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
575
	int ret;
576

577 578 579 580 581 582 583 584 585 586 587
	ret = pm_generic_runtime_suspend(dev);

	if (!ret)
		omap_device_idle(pdev);

	return ret;
}

static int _od_runtime_idle(struct device *dev)
{
	return pm_generic_runtime_idle(dev);
588 589 590 591 592 593
}

static int _od_runtime_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);

594 595 596
	omap_device_enable(pdev);

	return pm_generic_runtime_resume(dev);
597
}
598
#endif
599

600 601 602 603 604 605 606
#ifdef CONFIG_SUSPEND
static int _od_suspend_noirq(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct omap_device *od = to_omap_device(pdev);
	int ret;

607 608 609
	if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
		return pm_generic_suspend_noirq(dev);

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	ret = pm_generic_suspend_noirq(dev);

	if (!ret && !pm_runtime_status_suspended(dev)) {
		if (pm_generic_runtime_suspend(dev) == 0) {
			omap_device_idle(pdev);
			od->flags |= OMAP_DEVICE_SUSPENDED;
		}
	}

	return ret;
}

static int _od_resume_noirq(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct omap_device *od = to_omap_device(pdev);

627 628 629
	if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
		return pm_generic_resume_noirq(dev);

630 631 632 633 634 635 636 637 638
	if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
	    !pm_runtime_status_suspended(dev)) {
		od->flags &= ~OMAP_DEVICE_SUSPENDED;
		omap_device_enable(pdev);
		pm_generic_runtime_resume(dev);
	}

	return pm_generic_resume_noirq(dev);
}
639 640 641
#else
#define _od_suspend_noirq NULL
#define _od_resume_noirq NULL
642 643
#endif

644
static struct dev_pm_domain omap_device_pm_domain = {
645
	.ops = {
646 647
		SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
				   _od_runtime_idle)
648
		USE_PLATFORM_PM_SLEEP_OPS
649 650
		.suspend_noirq = _od_suspend_noirq,
		.resume_noirq = _od_resume_noirq,
651 652 653
	}
};

654 655 656 657 658 659 660 661
/**
 * omap_device_register - register an omap_device with one omap_hwmod
 * @od: struct omap_device * to register
 *
 * Register the omap_device structure.  This currently just calls
 * platform_device_register() on the underlying platform_device.
 * Returns the return value of platform_device_register().
 */
662
static int omap_device_register(struct platform_device *pdev)
663
{
664
	pr_debug("omap_device: %s: registering\n", pdev->name);
665

666 667
	pdev->dev.parent = &omap_device_parent;
	pdev->dev.pm_domain = &omap_device_pm_domain;
668
	return platform_device_add(pdev);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
}


/* Public functions for use by device drivers through struct platform_data */

/**
 * omap_device_enable - fully activate an omap_device
 * @od: struct omap_device * to activate
 *
 * Do whatever is necessary for the hwmods underlying omap_device @od
 * to be accessible and ready to operate.  This generally involves
 * enabling clocks, setting SYSCONFIG registers; and in the future may
 * involve remuxing pins.  Device drivers should call this function
 * (through platform_data function pointers) where they would normally
 * enable clocks, etc.  Returns -EINVAL if called when the omap_device
 * is already enabled, or passes along the return value of
 * _omap_device_activate().
 */
int omap_device_enable(struct platform_device *pdev)
{
	int ret;
	struct omap_device *od;

692
	od = to_omap_device(pdev);
693 694

	if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
695 696 697
		dev_warn(&pdev->dev,
			 "omap_device: %s() called from invalid state %d\n",
			 __func__, od->_state);
698 699 700 701 702 703 704 705 706 707
		return -EINVAL;
	}

	/* Enable everything if we're enabling this device from scratch */
	if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
		od->pm_lat_level = od->pm_lats_cnt;

	ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);

	od->dev_wakeup_lat = 0;
708
	od->_dev_wakeup_lat_limit = UINT_MAX;
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	od->_state = OMAP_DEVICE_STATE_ENABLED;

	return ret;
}

/**
 * omap_device_idle - idle an omap_device
 * @od: struct omap_device * to idle
 *
 * Idle omap_device @od by calling as many .deactivate_func() entries
 * in the omap_device's pm_lats table as is possible without exceeding
 * the device's maximum wakeup latency limit, pm_lat_limit.  Device
 * drivers should call this function (through platform_data function
 * pointers) where they would normally disable clocks after operations
 * complete, etc..  Returns -EINVAL if the omap_device is not
 * currently enabled, or passes along the return value of
 * _omap_device_deactivate().
 */
int omap_device_idle(struct platform_device *pdev)
{
	int ret;
	struct omap_device *od;

732
	od = to_omap_device(pdev);
733 734

	if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
735 736 737
		dev_warn(&pdev->dev,
			 "omap_device: %s() called from invalid state %d\n",
			 __func__, od->_state);
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
		return -EINVAL;
	}

	ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);

	od->_state = OMAP_DEVICE_STATE_IDLE;

	return ret;
}

/**
 * omap_device_shutdown - shut down an omap_device
 * @od: struct omap_device * to shut down
 *
 * Shut down omap_device @od by calling all .deactivate_func() entries
 * in the omap_device's pm_lats table and then shutting down all of
 * the underlying omap_hwmods.  Used when a device is being "removed"
 * or a device driver is being unloaded.  Returns -EINVAL if the
 * omap_device is not currently enabled or idle, or passes along the
 * return value of _omap_device_deactivate().
 */
int omap_device_shutdown(struct platform_device *pdev)
{
	int ret, i;
	struct omap_device *od;

764
	od = to_omap_device(pdev);
765 766 767

	if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
	    od->_state != OMAP_DEVICE_STATE_IDLE) {
768 769 770
		dev_warn(&pdev->dev,
			 "omap_device: %s() called from invalid state %d\n",
			 __func__, od->_state);
771 772 773 774 775
		return -EINVAL;
	}

	ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);

776 777
	for (i = 0; i < od->hwmods_cnt; i++)
		omap_hwmod_shutdown(od->hwmods[i]);
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805

	od->_state = OMAP_DEVICE_STATE_SHUTDOWN;

	return ret;
}

/**
 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
 * @od: struct omap_device *
 *
 * When a device's maximum wakeup latency limit changes, call some of
 * the .activate_func or .deactivate_func function pointers in the
 * omap_device's pm_lats array to ensure that the device's maximum
 * wakeup latency is less than or equal to the new latency limit.
 * Intended to be called by OMAP PM code whenever a device's maximum
 * wakeup latency limit changes (e.g., via
 * omap_pm_set_dev_wakeup_lat()).  Returns 0 if nothing needs to be
 * done (e.g., if the omap_device is not currently idle, or if the
 * wakeup latency is already current with the new limit) or passes
 * along the return value of _omap_device_deactivate() or
 * _omap_device_activate().
 */
int omap_device_align_pm_lat(struct platform_device *pdev,
			     u32 new_wakeup_lat_limit)
{
	int ret = -EINVAL;
	struct omap_device *od;

806
	od = to_omap_device(pdev);
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

	if (new_wakeup_lat_limit == od->dev_wakeup_lat)
		return 0;

	od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;

	if (od->_state != OMAP_DEVICE_STATE_IDLE)
		return 0;
	else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
		ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
	else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
		ret = _omap_device_activate(od, USE_WAKEUP_LAT);

	return ret;
}

/**
 * omap_device_get_pwrdm - return the powerdomain * associated with @od
 * @od: struct omap_device *
 *
 * Return the powerdomain associated with the first underlying
 * omap_hwmod for this omap_device.  Intended for use by core OMAP PM
 * code.  Returns NULL on error or a struct powerdomain * upon
 * success.
 */
struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
{
	/*
	 * XXX Assumes that all omap_hwmod powerdomains are identical.
	 * This may not necessarily be true.  There should be a sanity
	 * check in here to WARN() if any difference appears.
	 */
	if (!od->hwmods_cnt)
		return NULL;

	return omap_hwmod_get_pwrdm(od->hwmods[0]);
}

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
/**
 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
 * @od: struct omap_device *
 *
 * Return the MPU's virtual address for the base of the hwmod, from
 * the ioremap() that the hwmod code does.  Only valid if there is one
 * hwmod associated with this device.  Returns NULL if there are zero
 * or more than one hwmods associated with this omap_device;
 * otherwise, passes along the return value from
 * omap_hwmod_get_mpu_rt_va().
 */
void __iomem *omap_device_get_rt_va(struct omap_device *od)
{
	if (od->hwmods_cnt != 1)
		return NULL;

	return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
}

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
/**
 * omap_device_get_by_hwmod_name() - convert a hwmod name to
 * device pointer.
 * @oh_name: name of the hwmod device
 *
 * Returns back a struct device * pointer associated with a hwmod
 * device represented by a hwmod_name
 */
struct device *omap_device_get_by_hwmod_name(const char *oh_name)
{
	struct omap_hwmod *oh;

	if (!oh_name) {
		WARN(1, "%s: no hwmod name!\n", __func__);
		return ERR_PTR(-EINVAL);
	}

	oh = omap_hwmod_lookup(oh_name);
	if (IS_ERR_OR_NULL(oh)) {
		WARN(1, "%s: no hwmod for %s\n", __func__,
			oh_name);
		return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
	}
	if (IS_ERR_OR_NULL(oh->od)) {
		WARN(1, "%s: no omap_device for %s\n", __func__,
			oh_name);
		return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
	}

	if (IS_ERR_OR_NULL(oh->od->pdev))
		return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);

	return &oh->od->pdev->dev;
}
EXPORT_SYMBOL(omap_device_get_by_hwmod_name);

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
/*
 * Public functions intended for use in omap_device_pm_latency
 * .activate_func and .deactivate_func function pointers
 */

/**
 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
 * @od: struct omap_device *od
 *
 * Enable all underlying hwmods.  Returns 0.
 */
int omap_device_enable_hwmods(struct omap_device *od)
{
	int i;

915 916
	for (i = 0; i < od->hwmods_cnt; i++)
		omap_hwmod_enable(od->hwmods[i]);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

	/* XXX pass along return value here? */
	return 0;
}

/**
 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
 * @od: struct omap_device *od
 *
 * Idle all underlying hwmods.  Returns 0.
 */
int omap_device_idle_hwmods(struct omap_device *od)
{
	int i;

932 933
	for (i = 0; i < od->hwmods_cnt; i++)
		omap_hwmod_idle(od->hwmods[i]);
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

	/* XXX pass along return value here? */
	return 0;
}

/**
 * omap_device_disable_clocks - disable all main and interface clocks
 * @od: struct omap_device *od
 *
 * Disable the main functional clock and interface clock for all of the
 * omap_hwmods associated with the omap_device.  Returns 0.
 */
int omap_device_disable_clocks(struct omap_device *od)
{
	int i;

950 951
	for (i = 0; i < od->hwmods_cnt; i++)
		omap_hwmod_disable_clocks(od->hwmods[i]);
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

	/* XXX pass along return value here? */
	return 0;
}

/**
 * omap_device_enable_clocks - enable all main and interface clocks
 * @od: struct omap_device *od
 *
 * Enable the main functional clock and interface clock for all of the
 * omap_hwmods associated with the omap_device.  Returns 0.
 */
int omap_device_enable_clocks(struct omap_device *od)
{
	int i;

968 969
	for (i = 0; i < od->hwmods_cnt; i++)
		omap_hwmod_enable_clocks(od->hwmods[i]);
970 971 972 973

	/* XXX pass along return value here? */
	return 0;
}
974 975 976 977 978 979 980 981 982 983 984

struct device omap_device_parent = {
	.init_name	= "omap",
	.parent         = &platform_bus,
};

static int __init omap_device_init(void)
{
	return device_register(&omap_device_parent);
}
core_initcall(omap_device_init);