2 * omap_device implementation
4 * Copyright (C) 2009-2010 Nokia Corporation
5 * Paul Walmsley, Kevin Hilman
7 * Developed in collaboration with (alphabetical order): Benoit
8 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
9 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * This code provides a consistent interface for OMAP device drivers
17 * to control power management and interconnect properties of their
20 * In the medium- to long-term, this code should either be
21 * a) implemented via arch-specific pointers in platform_data
23 * b) implemented as a proper omap_bus/omap_device in Linux, no more
24 * platform_data func pointers
27 * Guidelines for usage by driver authors:
29 * 1. These functions are intended to be used by device drivers via
30 * function pointers in struct platform_data. As an example,
31 * omap_device_enable() should be passed to the driver as
33 * struct foo_driver_platform_data {
35 * int (*device_enable)(struct platform_device *pdev);
39 * Note that the generic "device_enable" name is used, rather than
40 * "omap_device_enable". This is so other architectures can pass in their
41 * own enable/disable functions here.
43 * This should be populated during device setup:
46 * pdata->device_enable = omap_device_enable;
49 * 2. Drivers should first check to ensure the function pointer is not null
50 * before calling it, as in:
52 * if (pdata->device_enable)
53 * pdata->device_enable(pdev);
55 * This allows other architectures that don't use similar device_enable()/
56 * device_shutdown() functions to execute normally.
60 * Suggested usage by device drivers:
62 * During device initialization:
66 * (save remaining device context if necessary)
69 * During device resume:
71 * (restore context if necessary)
73 * During device shutdown:
75 * (device must be reinitialized at this point to use it again)
80 #include <linux/kernel.h>
81 #include <linux/platform_device.h>
82 #include <linux/slab.h>
83 #include <linux/err.h>
86 #include <plat/omap_device.h>
87 #include <plat/omap_hwmod.h>
89 /* These parameters are passed to _omap_device_{de,}activate() */
90 #define USE_WAKEUP_LAT 0
91 #define IGNORE_WAKEUP_LAT 1
93 /* Private functions */
96 * _omap_device_activate - increase device readiness
97 * @od: struct omap_device *
98 * @ignore_lat: increase to latency target (0) or full readiness (1)?
100 * Increase readiness of omap_device @od (thus decreasing device
101 * wakeup latency, but consuming more power). If @ignore_lat is
102 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
103 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
104 * latency is greater than the requested maximum wakeup latency, step
105 * backwards in the omap_device_pm_latency table to ensure the
106 * device's maximum wakeup latency is less than or equal to the
107 * requested maximum wakeup latency. Returns 0.
109 static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
111 struct timespec a, b, c;
113 pr_debug("omap_device: %s: activating\n", od->pdev.name);
115 while (od->pm_lat_level > 0) {
116 struct omap_device_pm_latency *odpl;
117 unsigned long long act_lat = 0;
121 odpl = od->pm_lats + od->pm_lat_level;
124 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
127 read_persistent_clock(&a);
129 /* XXX check return code */
130 odpl->activate_func(od);
132 read_persistent_clock(&b);
134 c = timespec_sub(b, a);
135 act_lat = timespec_to_ns(&c);
137 pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
138 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
141 if (act_lat > odpl->activate_lat) {
142 odpl->activate_lat_worst = act_lat;
143 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
144 odpl->activate_lat = act_lat;
145 pr_warning("omap_device: %s.%d: new worst case "
146 "activate latency %d: %llu\n",
147 od->pdev.name, od->pdev.id,
148 od->pm_lat_level, act_lat);
150 pr_warning("omap_device: %s.%d: activate "
151 "latency %d higher than exptected. "
153 od->pdev.name, od->pdev.id,
154 od->pm_lat_level, act_lat,
158 od->dev_wakeup_lat -= odpl->activate_lat;
165 * _omap_device_deactivate - decrease device readiness
166 * @od: struct omap_device *
167 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
169 * Decrease readiness of omap_device @od (thus increasing device
170 * wakeup latency, but conserving power). If @ignore_lat is
171 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
172 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
173 * latency is less than the requested maximum wakeup latency, step
174 * forwards in the omap_device_pm_latency table to ensure the device's
175 * maximum wakeup latency is less than or equal to the requested
176 * maximum wakeup latency. Returns 0.
178 static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
180 struct timespec a, b, c;
182 pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
184 while (od->pm_lat_level < od->pm_lats_cnt) {
185 struct omap_device_pm_latency *odpl;
186 unsigned long long deact_lat = 0;
188 odpl = od->pm_lats + od->pm_lat_level;
191 ((od->dev_wakeup_lat + odpl->activate_lat) >
192 od->_dev_wakeup_lat_limit))
195 read_persistent_clock(&a);
197 /* XXX check return code */
198 odpl->deactivate_func(od);
200 read_persistent_clock(&b);
202 c = timespec_sub(b, a);
203 deact_lat = timespec_to_ns(&c);
205 pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
206 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
209 if (deact_lat > odpl->deactivate_lat) {
210 odpl->deactivate_lat_worst = deact_lat;
211 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
212 odpl->deactivate_lat = deact_lat;
213 pr_warning("omap_device: %s.%d: new worst case "
214 "deactivate latency %d: %llu\n",
215 od->pdev.name, od->pdev.id,
216 od->pm_lat_level, deact_lat);
218 pr_warning("omap_device: %s.%d: deactivate "
219 "latency %d higher than exptected. "
221 od->pdev.name, od->pdev.id,
222 od->pm_lat_level, deact_lat,
223 odpl->deactivate_lat);
227 od->dev_wakeup_lat += odpl->activate_lat;
235 static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
237 return container_of(pdev, struct omap_device, pdev);
241 /* Public functions for use by core code */
244 * omap_device_count_resources - count number of struct resource entries needed
245 * @od: struct omap_device *
247 * Count the number of struct resource entries needed for this
248 * omap_device @od. Used by omap_device_build_ss() to determine how
249 * much memory to allocate before calling
250 * omap_device_fill_resources(). Returns the count.
252 int omap_device_count_resources(struct omap_device *od)
254 struct omap_hwmod *oh;
258 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
259 c += omap_hwmod_count_resources(oh);
261 pr_debug("omap_device: %s: counted %d total resources across %d "
262 "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
268 * omap_device_fill_resources - fill in array of struct resource
269 * @od: struct omap_device *
270 * @res: pointer to an array of struct resource to be filled in
272 * Populate one or more empty struct resource pointed to by @res with
273 * the resource data for this omap_device @od. Used by
274 * omap_device_build_ss() after calling omap_device_count_resources().
275 * Ideally this function would not be needed at all. If omap_device
276 * replaces platform_device, then we can specify our own
277 * get_resource()/ get_irq()/etc functions that use the underlying
278 * omap_hwmod information. Or if platform_device is extended to use
279 * subarchitecture-specific function pointers, the various
280 * platform_device functions can simply call omap_device internal
281 * functions to get device resources. Hacking around the existing
282 * platform_device code wastes memory. Returns 0.
284 int omap_device_fill_resources(struct omap_device *od, struct resource *res)
286 struct omap_hwmod *oh;
290 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++) {
291 r = omap_hwmod_fill_resources(oh, res);
300 * omap_device_build - build and register an omap_device with one omap_hwmod
301 * @pdev_name: name of the platform_device driver to use
302 * @pdev_id: this platform_device's connection ID
303 * @oh: ptr to the single omap_hwmod that backs this omap_device
304 * @pdata: platform_data ptr to associate with the platform_device
305 * @pdata_len: amount of memory pointed to by @pdata
306 * @pm_lats: pointer to a omap_device_pm_latency array for this device
307 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
308 * @is_early_device: should the device be registered as an early device or not
310 * Convenience function for building and registering a single
311 * omap_device record, which in turn builds and registers a
312 * platform_device record. See omap_device_build_ss() for more
313 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
314 * passes along the return value of omap_device_build_ss().
316 struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
317 struct omap_hwmod *oh, void *pdata,
319 struct omap_device_pm_latency *pm_lats,
320 int pm_lats_cnt, int is_early_device)
322 struct omap_hwmod *ohs[] = { oh };
325 return ERR_PTR(-EINVAL);
327 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
328 pdata_len, pm_lats, pm_lats_cnt,
333 * omap_device_build_ss - build and register an omap_device with multiple hwmods
334 * @pdev_name: name of the platform_device driver to use
335 * @pdev_id: this platform_device's connection ID
336 * @oh: ptr to the single omap_hwmod that backs this omap_device
337 * @pdata: platform_data ptr to associate with the platform_device
338 * @pdata_len: amount of memory pointed to by @pdata
339 * @pm_lats: pointer to a omap_device_pm_latency array for this device
340 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
341 * @is_early_device: should the device be registered as an early device or not
343 * Convenience function for building and registering an omap_device
344 * subsystem record. Subsystem records consist of multiple
345 * omap_hwmods. This function in turn builds and registers a
346 * platform_device record. Returns an ERR_PTR() on error, or passes
347 * along the return value of omap_device_register().
349 struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
350 struct omap_hwmod **ohs, int oh_cnt,
351 void *pdata, int pdata_len,
352 struct omap_device_pm_latency *pm_lats,
353 int pm_lats_cnt, int is_early_device)
356 struct omap_device *od;
358 struct resource *res = NULL;
360 struct omap_hwmod **hwmods;
362 if (!ohs || oh_cnt == 0 || !pdev_name)
363 return ERR_PTR(-EINVAL);
365 if (!pdata && pdata_len > 0)
366 return ERR_PTR(-EINVAL);
368 pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
371 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
373 return ERR_PTR(-ENOMEM);
375 od->hwmods_cnt = oh_cnt;
377 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
382 memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
385 pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
388 strcpy(pdev_name2, pdev_name);
390 od->pdev.name = pdev_name2;
391 od->pdev.id = pdev_id;
393 res_count = omap_device_count_resources(od);
395 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
399 omap_device_fill_resources(od, res);
401 od->pdev.num_resources = res_count;
402 od->pdev.resource = res;
404 ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
408 od->pm_lats = pm_lats;
409 od->pm_lats_cnt = pm_lats_cnt;
412 ret = omap_early_device_register(od);
414 ret = omap_device_register(od);
416 for (i = 0; i < oh_cnt; i++)
433 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
439 * omap_early_device_register - register an omap_device as an early platform
441 * @od: struct omap_device * to register
443 * Register the omap_device structure. This currently just calls
444 * platform_early_add_device() on the underlying platform_device.
445 * Returns 0 by default.
447 int omap_early_device_register(struct omap_device *od)
449 struct platform_device *devices[1];
451 devices[0] = &(od->pdev);
452 early_platform_add_devices(devices, 1);
457 * omap_device_register - register an omap_device with one omap_hwmod
458 * @od: struct omap_device * to register
460 * Register the omap_device structure. This currently just calls
461 * platform_device_register() on the underlying platform_device.
462 * Returns the return value of platform_device_register().
464 int omap_device_register(struct omap_device *od)
466 pr_debug("omap_device: %s: registering\n", od->pdev.name);
468 return platform_device_register(&od->pdev);
472 /* Public functions for use by device drivers through struct platform_data */
475 * omap_device_enable - fully activate an omap_device
476 * @od: struct omap_device * to activate
478 * Do whatever is necessary for the hwmods underlying omap_device @od
479 * to be accessible and ready to operate. This generally involves
480 * enabling clocks, setting SYSCONFIG registers; and in the future may
481 * involve remuxing pins. Device drivers should call this function
482 * (through platform_data function pointers) where they would normally
483 * enable clocks, etc. Returns -EINVAL if called when the omap_device
484 * is already enabled, or passes along the return value of
485 * _omap_device_activate().
487 int omap_device_enable(struct platform_device *pdev)
490 struct omap_device *od;
492 od = _find_by_pdev(pdev);
494 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
495 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
496 od->pdev.name, od->pdev.id, __func__, od->_state);
500 /* Enable everything if we're enabling this device from scratch */
501 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
502 od->pm_lat_level = od->pm_lats_cnt;
504 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
506 od->dev_wakeup_lat = 0;
507 od->_dev_wakeup_lat_limit = UINT_MAX;
508 od->_state = OMAP_DEVICE_STATE_ENABLED;
514 * omap_device_idle - idle an omap_device
515 * @od: struct omap_device * to idle
517 * Idle omap_device @od by calling as many .deactivate_func() entries
518 * in the omap_device's pm_lats table as is possible without exceeding
519 * the device's maximum wakeup latency limit, pm_lat_limit. Device
520 * drivers should call this function (through platform_data function
521 * pointers) where they would normally disable clocks after operations
522 * complete, etc.. Returns -EINVAL if the omap_device is not
523 * currently enabled, or passes along the return value of
524 * _omap_device_deactivate().
526 int omap_device_idle(struct platform_device *pdev)
529 struct omap_device *od;
531 od = _find_by_pdev(pdev);
533 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
534 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
535 od->pdev.name, od->pdev.id, __func__, od->_state);
539 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
541 od->_state = OMAP_DEVICE_STATE_IDLE;
547 * omap_device_shutdown - shut down an omap_device
548 * @od: struct omap_device * to shut down
550 * Shut down omap_device @od by calling all .deactivate_func() entries
551 * in the omap_device's pm_lats table and then shutting down all of
552 * the underlying omap_hwmods. Used when a device is being "removed"
553 * or a device driver is being unloaded. Returns -EINVAL if the
554 * omap_device is not currently enabled or idle, or passes along the
555 * return value of _omap_device_deactivate().
557 int omap_device_shutdown(struct platform_device *pdev)
560 struct omap_device *od;
561 struct omap_hwmod *oh;
563 od = _find_by_pdev(pdev);
565 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
566 od->_state != OMAP_DEVICE_STATE_IDLE) {
567 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
568 od->pdev.name, od->pdev.id, __func__, od->_state);
572 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
574 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
575 omap_hwmod_shutdown(oh);
577 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
583 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
584 * @od: struct omap_device *
586 * When a device's maximum wakeup latency limit changes, call some of
587 * the .activate_func or .deactivate_func function pointers in the
588 * omap_device's pm_lats array to ensure that the device's maximum
589 * wakeup latency is less than or equal to the new latency limit.
590 * Intended to be called by OMAP PM code whenever a device's maximum
591 * wakeup latency limit changes (e.g., via
592 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
593 * done (e.g., if the omap_device is not currently idle, or if the
594 * wakeup latency is already current with the new limit) or passes
595 * along the return value of _omap_device_deactivate() or
596 * _omap_device_activate().
598 int omap_device_align_pm_lat(struct platform_device *pdev,
599 u32 new_wakeup_lat_limit)
602 struct omap_device *od;
604 od = _find_by_pdev(pdev);
606 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
609 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
611 if (od->_state != OMAP_DEVICE_STATE_IDLE)
613 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
614 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
615 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
616 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
622 * omap_device_get_pwrdm - return the powerdomain * associated with @od
623 * @od: struct omap_device *
625 * Return the powerdomain associated with the first underlying
626 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
627 * code. Returns NULL on error or a struct powerdomain * upon
630 struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
633 * XXX Assumes that all omap_hwmod powerdomains are identical.
634 * This may not necessarily be true. There should be a sanity
635 * check in here to WARN() if any difference appears.
640 return omap_hwmod_get_pwrdm(od->hwmods[0]);
644 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
645 * @od: struct omap_device *
647 * Return the MPU's virtual address for the base of the hwmod, from
648 * the ioremap() that the hwmod code does. Only valid if there is one
649 * hwmod associated with this device. Returns NULL if there are zero
650 * or more than one hwmods associated with this omap_device;
651 * otherwise, passes along the return value from
652 * omap_hwmod_get_mpu_rt_va().
654 void __iomem *omap_device_get_rt_va(struct omap_device *od)
656 if (od->hwmods_cnt != 1)
659 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
663 * Public functions intended for use in omap_device_pm_latency
664 * .activate_func and .deactivate_func function pointers
668 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
669 * @od: struct omap_device *od
671 * Enable all underlying hwmods. Returns 0.
673 int omap_device_enable_hwmods(struct omap_device *od)
675 struct omap_hwmod *oh;
678 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
679 omap_hwmod_enable(oh);
681 /* XXX pass along return value here? */
686 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
687 * @od: struct omap_device *od
689 * Idle all underlying hwmods. Returns 0.
691 int omap_device_idle_hwmods(struct omap_device *od)
693 struct omap_hwmod *oh;
696 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
699 /* XXX pass along return value here? */
704 * omap_device_disable_clocks - disable all main and interface clocks
705 * @od: struct omap_device *od
707 * Disable the main functional clock and interface clock for all of the
708 * omap_hwmods associated with the omap_device. Returns 0.
710 int omap_device_disable_clocks(struct omap_device *od)
712 struct omap_hwmod *oh;
715 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
716 omap_hwmod_disable_clocks(oh);
718 /* XXX pass along return value here? */
723 * omap_device_enable_clocks - enable all main and interface clocks
724 * @od: struct omap_device *od
726 * Enable the main functional clock and interface clock for all of the
727 * omap_hwmods associated with the omap_device. Returns 0.
729 int omap_device_enable_clocks(struct omap_device *od)
731 struct omap_hwmod *oh;
734 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
735 omap_hwmod_enable_clocks(oh);
737 /* XXX pass along return value here? */