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/export.h>
82 #include <linux/platform_device.h>
83 #include <linux/slab.h>
84 #include <linux/err.h>
86 #include <linux/clk.h>
87 #include <linux/clkdev.h>
88 #include <linux/pm_runtime.h>
90 #include <linux/notifier.h>
92 #include <plat/omap_device.h>
93 #include <plat/omap_hwmod.h>
94 #include <plat/clock.h>
96 /* These parameters are passed to _omap_device_{de,}activate() */
97 #define USE_WAKEUP_LAT 0
98 #define IGNORE_WAKEUP_LAT 1
100 static int omap_early_device_register(struct platform_device *pdev);
102 static struct omap_device_pm_latency omap_default_latency[] = {
104 .deactivate_func = omap_device_idle_hwmods,
105 .activate_func = omap_device_enable_hwmods,
106 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
110 /* Private functions */
113 * _omap_device_activate - increase device readiness
114 * @od: struct omap_device *
115 * @ignore_lat: increase to latency target (0) or full readiness (1)?
117 * Increase readiness of omap_device @od (thus decreasing device
118 * wakeup latency, but consuming more power). If @ignore_lat is
119 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
120 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
121 * latency is greater than the requested maximum wakeup latency, step
122 * backwards in the omap_device_pm_latency table to ensure the
123 * device's maximum wakeup latency is less than or equal to the
124 * requested maximum wakeup latency. Returns 0.
126 static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
128 struct timespec a, b, c;
130 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
132 while (od->pm_lat_level > 0) {
133 struct omap_device_pm_latency *odpl;
134 unsigned long long act_lat = 0;
138 odpl = od->pm_lats + od->pm_lat_level;
141 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
144 read_persistent_clock(&a);
146 /* XXX check return code */
147 odpl->activate_func(od);
149 read_persistent_clock(&b);
151 c = timespec_sub(b, a);
152 act_lat = timespec_to_ns(&c);
154 dev_dbg(&od->pdev->dev,
155 "omap_device: pm_lat %d: activate: elapsed time %llu nsec\n",
156 od->pm_lat_level, act_lat);
158 if (act_lat > odpl->activate_lat) {
159 odpl->activate_lat_worst = act_lat;
160 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
161 odpl->activate_lat = act_lat;
162 dev_dbg(&od->pdev->dev,
163 "new worst case activate latency %d: %llu\n",
164 od->pm_lat_level, act_lat);
166 dev_warn(&od->pdev->dev,
167 "activate latency %d higher than expected. (%llu > %d)\n",
168 od->pm_lat_level, act_lat,
172 od->dev_wakeup_lat -= odpl->activate_lat;
179 * _omap_device_deactivate - decrease device readiness
180 * @od: struct omap_device *
181 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
183 * Decrease readiness of omap_device @od (thus increasing device
184 * wakeup latency, but conserving power). If @ignore_lat is
185 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
186 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
187 * latency is less than the requested maximum wakeup latency, step
188 * forwards in the omap_device_pm_latency table to ensure the device's
189 * maximum wakeup latency is less than or equal to the requested
190 * maximum wakeup latency. Returns 0.
192 static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
194 struct timespec a, b, c;
196 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
198 while (od->pm_lat_level < od->pm_lats_cnt) {
199 struct omap_device_pm_latency *odpl;
200 unsigned long long deact_lat = 0;
202 odpl = od->pm_lats + od->pm_lat_level;
205 ((od->dev_wakeup_lat + odpl->activate_lat) >
206 od->_dev_wakeup_lat_limit))
209 read_persistent_clock(&a);
211 /* XXX check return code */
212 odpl->deactivate_func(od);
214 read_persistent_clock(&b);
216 c = timespec_sub(b, a);
217 deact_lat = timespec_to_ns(&c);
219 dev_dbg(&od->pdev->dev,
220 "omap_device: pm_lat %d: deactivate: elapsed time %llu nsec\n",
221 od->pm_lat_level, deact_lat);
223 if (deact_lat > odpl->deactivate_lat) {
224 odpl->deactivate_lat_worst = deact_lat;
225 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
226 odpl->deactivate_lat = deact_lat;
227 dev_dbg(&od->pdev->dev,
228 "new worst case deactivate latency %d: %llu\n",
229 od->pm_lat_level, deact_lat);
231 dev_warn(&od->pdev->dev,
232 "deactivate latency %d higher than expected. (%llu > %d)\n",
233 od->pm_lat_level, deact_lat,
234 odpl->deactivate_lat);
237 od->dev_wakeup_lat += odpl->activate_lat;
245 static void _add_clkdev(struct omap_device *od, const char *clk_alias,
246 const char *clk_name)
249 struct clk_lookup *l;
251 if (!clk_alias || !clk_name)
254 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
256 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
258 dev_warn(&od->pdev->dev,
259 "alias %s already exists\n", clk_alias);
264 r = omap_clk_get_by_name(clk_name);
266 dev_err(&od->pdev->dev,
267 "omap_clk_get_by_name for %s failed\n", clk_name);
271 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
273 dev_err(&od->pdev->dev,
274 "clkdev_alloc for %s failed\n", clk_alias);
282 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
284 * @od: struct omap_device *od
285 * @oh: struct omap_hwmod *oh
287 * For the main clock and every optional clock present per hwmod per
288 * omap_device, this function adds an entry in the clkdev table of the
289 * form <dev-id=dev_name, con-id=role> if it does not exist already.
291 * The function is called from inside omap_device_build_ss(), after
292 * omap_device_register.
294 * This allows drivers to get a pointer to its optional clocks based on its role
295 * by calling clk_get(<dev*>, <role>).
296 * In the case of the main clock, a "fck" alias is used.
300 static void _add_hwmod_clocks_clkdev(struct omap_device *od,
301 struct omap_hwmod *oh)
305 _add_clkdev(od, "fck", oh->main_clk);
307 for (i = 0; i < oh->opt_clks_cnt; i++)
308 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
313 * omap_device_build_from_dt - build an omap_device with multiple hwmods
314 * @pdev_name: name of the platform_device driver to use
315 * @pdev_id: this platform_device's connection ID
316 * @oh: ptr to the single omap_hwmod that backs this omap_device
317 * @pdata: platform_data ptr to associate with the platform_device
318 * @pdata_len: amount of memory pointed to by @pdata
319 * @pm_lats: pointer to a omap_device_pm_latency array for this device
320 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
321 * @is_early_device: should the device be registered as an early device or not
323 * Function for building an omap_device already registered from device-tree
325 * Returns 0 or PTR_ERR() on error.
327 static int omap_device_build_from_dt(struct platform_device *pdev)
329 struct omap_hwmod **hwmods;
330 struct omap_device *od;
331 struct omap_hwmod *oh;
332 struct device_node *node = pdev->dev.of_node;
334 int oh_cnt, i, ret = 0;
336 oh_cnt = of_property_count_strings(node, "ti,hwmods");
337 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
338 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
342 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
348 for (i = 0; i < oh_cnt; i++) {
349 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
350 oh = omap_hwmod_lookup(oh_name);
352 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
360 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
362 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
368 /* Fix up missing resource names */
369 for (i = 0; i < pdev->num_resources; i++) {
370 struct resource *r = &pdev->resource[i];
373 r->name = dev_name(&pdev->dev);
376 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
377 omap_device_disable_idle_on_suspend(pdev);
379 pdev->dev.pm_domain = &omap_device_pm_domain;
387 static int _omap_device_notifier_call(struct notifier_block *nb,
388 unsigned long event, void *dev)
390 struct platform_device *pdev = to_platform_device(dev);
391 struct omap_device *od;
394 case BUS_NOTIFY_DEL_DEVICE:
395 if (pdev->archdata.od)
396 omap_device_delete(pdev->archdata.od);
398 case BUS_NOTIFY_ADD_DEVICE:
399 if (pdev->dev.of_node)
400 omap_device_build_from_dt(pdev);
403 od = to_omap_device(pdev);
405 od->_driver_status = event;
412 /* Public functions for use by core code */
415 * omap_device_get_context_loss_count - get lost context count
416 * @od: struct omap_device *
418 * Using the primary hwmod, query the context loss count for this
421 * Callers should consider context for this device lost any time this
422 * function returns a value different than the value the caller got
423 * the last time it called this function.
425 * If any hwmods exist for the omap_device assoiated with @pdev,
426 * return the context loss counter for that hwmod, otherwise return
429 int omap_device_get_context_loss_count(struct platform_device *pdev)
431 struct omap_device *od;
434 od = to_omap_device(pdev);
437 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
443 * omap_device_count_resources - count number of struct resource entries needed
444 * @od: struct omap_device *
446 * Count the number of struct resource entries needed for this
447 * omap_device @od. Used by omap_device_build_ss() to determine how
448 * much memory to allocate before calling
449 * omap_device_fill_resources(). Returns the count.
451 static int omap_device_count_resources(struct omap_device *od)
456 for (i = 0; i < od->hwmods_cnt; i++)
457 c += omap_hwmod_count_resources(od->hwmods[i]);
459 pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
460 od->pdev->name, c, od->hwmods_cnt);
466 * omap_device_fill_resources - fill in array of struct resource
467 * @od: struct omap_device *
468 * @res: pointer to an array of struct resource to be filled in
470 * Populate one or more empty struct resource pointed to by @res with
471 * the resource data for this omap_device @od. Used by
472 * omap_device_build_ss() after calling omap_device_count_resources().
473 * Ideally this function would not be needed at all. If omap_device
474 * replaces platform_device, then we can specify our own
475 * get_resource()/ get_irq()/etc functions that use the underlying
476 * omap_hwmod information. Or if platform_device is extended to use
477 * subarchitecture-specific function pointers, the various
478 * platform_device functions can simply call omap_device internal
479 * functions to get device resources. Hacking around the existing
480 * platform_device code wastes memory. Returns 0.
482 static int omap_device_fill_resources(struct omap_device *od,
483 struct resource *res)
487 for (i = 0; i < od->hwmods_cnt; i++) {
488 r = omap_hwmod_fill_resources(od->hwmods[i], res);
496 * _od_fill_dma_resources - fill in array of struct resource with dma resources
497 * @od: struct omap_device *
498 * @res: pointer to an array of struct resource to be filled in
500 * Populate one or more empty struct resource pointed to by @res with
501 * the dma resource data for this omap_device @od. Used by
502 * omap_device_alloc() after calling omap_device_count_resources().
504 * Ideally this function would not be needed at all. If we have
505 * mechanism to get dma resources from DT.
509 static int _od_fill_dma_resources(struct omap_device *od,
510 struct resource *res)
514 for (i = 0; i < od->hwmods_cnt; i++) {
515 r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
523 * omap_device_alloc - allocate an omap_device
524 * @pdev: platform_device that will be included in this omap_device
525 * @oh: ptr to the single omap_hwmod that backs this omap_device
526 * @pdata: platform_data ptr to associate with the platform_device
527 * @pdata_len: amount of memory pointed to by @pdata
528 * @pm_lats: pointer to a omap_device_pm_latency array for this device
529 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
531 * Convenience function for allocating an omap_device structure and filling
532 * hwmods, resources and pm_latency attributes.
534 * Returns an struct omap_device pointer or ERR_PTR() on error;
536 struct omap_device *omap_device_alloc(struct platform_device *pdev,
537 struct omap_hwmod **ohs, int oh_cnt,
538 struct omap_device_pm_latency *pm_lats,
542 struct omap_device *od;
543 struct resource *res = NULL;
545 struct omap_hwmod **hwmods;
547 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
552 od->hwmods_cnt = oh_cnt;
554 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
561 res_count = omap_device_count_resources(od);
564 * OF framework will construct the resource structure (currently
565 * does for MEM & IRQ resource) and we should respect/use these
566 * resources, killing hwmod dependency.
567 * If pdev->num_resources > 0, we assume that MEM & IRQ resources
568 * have been allocated by OF layer already (through DTB).
571 * Here, pdev->num_resources = 0, and we should get all the
572 * resources from hwmod.
574 * TODO: Once DMA resource is available from OF layer, we should
575 * kill filling any resources from hwmod.
577 if (res_count > pdev->num_resources) {
578 /* Allocate resources memory to account for new resources */
579 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
584 * If pdev->num_resources > 0, then assume that,
585 * MEM and IRQ resources will only come from DT and only
586 * fill DMA resource from hwmod layer.
588 if (pdev->num_resources && pdev->resource) {
589 dev_dbg(&pdev->dev, "%s(): resources already allocated %d\n",
590 __func__, res_count);
591 memcpy(res, pdev->resource,
592 sizeof(struct resource) * pdev->num_resources);
593 _od_fill_dma_resources(od, &res[pdev->num_resources]);
595 dev_dbg(&pdev->dev, "%s(): using resources from hwmod %d\n",
596 __func__, res_count);
597 omap_device_fill_resources(od, res);
600 ret = platform_device_add_resources(pdev, res, res_count);
608 pm_lats = omap_default_latency;
609 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
612 od->pm_lats_cnt = pm_lats_cnt;
613 od->pm_lats = kmemdup(pm_lats,
614 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
619 pdev->archdata.od = od;
621 for (i = 0; i < oh_cnt; i++) {
623 _add_hwmod_clocks_clkdev(od, hwmods[i]);
633 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
638 void omap_device_delete(struct omap_device *od)
643 od->pdev->archdata.od = NULL;
650 * omap_device_build - build and register an omap_device with one omap_hwmod
651 * @pdev_name: name of the platform_device driver to use
652 * @pdev_id: this platform_device's connection ID
653 * @oh: ptr to the single omap_hwmod that backs this omap_device
654 * @pdata: platform_data ptr to associate with the platform_device
655 * @pdata_len: amount of memory pointed to by @pdata
656 * @pm_lats: pointer to a omap_device_pm_latency array for this device
657 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
658 * @is_early_device: should the device be registered as an early device or not
660 * Convenience function for building and registering a single
661 * omap_device record, which in turn builds and registers a
662 * platform_device record. See omap_device_build_ss() for more
663 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
664 * passes along the return value of omap_device_build_ss().
666 struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
667 struct omap_hwmod *oh, void *pdata,
669 struct omap_device_pm_latency *pm_lats,
670 int pm_lats_cnt, int is_early_device)
672 struct omap_hwmod *ohs[] = { oh };
675 return ERR_PTR(-EINVAL);
677 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
678 pdata_len, pm_lats, pm_lats_cnt,
683 * omap_device_build_ss - build and register an omap_device with multiple hwmods
684 * @pdev_name: name of the platform_device driver to use
685 * @pdev_id: this platform_device's connection ID
686 * @oh: ptr to the single omap_hwmod that backs this omap_device
687 * @pdata: platform_data ptr to associate with the platform_device
688 * @pdata_len: amount of memory pointed to by @pdata
689 * @pm_lats: pointer to a omap_device_pm_latency array for this device
690 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
691 * @is_early_device: should the device be registered as an early device or not
693 * Convenience function for building and registering an omap_device
694 * subsystem record. Subsystem records consist of multiple
695 * omap_hwmods. This function in turn builds and registers a
696 * platform_device record. Returns an ERR_PTR() on error, or passes
697 * along the return value of omap_device_register().
699 struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
700 struct omap_hwmod **ohs, int oh_cnt,
701 void *pdata, int pdata_len,
702 struct omap_device_pm_latency *pm_lats,
703 int pm_lats_cnt, int is_early_device)
706 struct platform_device *pdev;
707 struct omap_device *od;
709 if (!ohs || oh_cnt == 0 || !pdev_name)
710 return ERR_PTR(-EINVAL);
712 if (!pdata && pdata_len > 0)
713 return ERR_PTR(-EINVAL);
715 pdev = platform_device_alloc(pdev_name, pdev_id);
721 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
723 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
725 dev_set_name(&pdev->dev, "%s", pdev->name);
727 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
731 ret = platform_device_add_data(pdev, pdata, pdata_len);
736 ret = omap_early_device_register(pdev);
738 ret = omap_device_register(pdev);
745 omap_device_delete(od);
747 platform_device_put(pdev);
750 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
756 * omap_early_device_register - register an omap_device as an early platform
758 * @od: struct omap_device * to register
760 * Register the omap_device structure. This currently just calls
761 * platform_early_add_device() on the underlying platform_device.
762 * Returns 0 by default.
764 static int __init omap_early_device_register(struct platform_device *pdev)
766 struct platform_device *devices[1];
769 early_platform_add_devices(devices, 1);
773 #ifdef CONFIG_PM_RUNTIME
774 static int _od_runtime_suspend(struct device *dev)
776 struct platform_device *pdev = to_platform_device(dev);
779 ret = pm_generic_runtime_suspend(dev);
782 omap_device_idle(pdev);
787 static int _od_runtime_idle(struct device *dev)
789 return pm_generic_runtime_idle(dev);
792 static int _od_runtime_resume(struct device *dev)
794 struct platform_device *pdev = to_platform_device(dev);
796 omap_device_enable(pdev);
798 return pm_generic_runtime_resume(dev);
802 #ifdef CONFIG_SUSPEND
803 static int _od_suspend_noirq(struct device *dev)
805 struct platform_device *pdev = to_platform_device(dev);
806 struct omap_device *od = to_omap_device(pdev);
809 /* Don't attempt late suspend on a driver that is not bound */
810 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
813 ret = pm_generic_suspend_noirq(dev);
815 if (!ret && !pm_runtime_status_suspended(dev)) {
816 if (pm_generic_runtime_suspend(dev) == 0) {
817 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
818 omap_device_idle(pdev);
819 od->flags |= OMAP_DEVICE_SUSPENDED;
826 static int _od_resume_noirq(struct device *dev)
828 struct platform_device *pdev = to_platform_device(dev);
829 struct omap_device *od = to_omap_device(pdev);
831 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
832 !pm_runtime_status_suspended(dev)) {
833 od->flags &= ~OMAP_DEVICE_SUSPENDED;
834 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
835 omap_device_enable(pdev);
836 pm_generic_runtime_resume(dev);
839 return pm_generic_resume_noirq(dev);
842 #define _od_suspend_noirq NULL
843 #define _od_resume_noirq NULL
846 struct dev_pm_domain omap_device_pm_domain = {
848 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
850 USE_PLATFORM_PM_SLEEP_OPS
851 .suspend_noirq = _od_suspend_noirq,
852 .resume_noirq = _od_resume_noirq,
857 * omap_device_register - register an omap_device with one omap_hwmod
858 * @od: struct omap_device * to register
860 * Register the omap_device structure. This currently just calls
861 * platform_device_register() on the underlying platform_device.
862 * Returns the return value of platform_device_register().
864 int omap_device_register(struct platform_device *pdev)
866 pr_debug("omap_device: %s: registering\n", pdev->name);
868 pdev->dev.pm_domain = &omap_device_pm_domain;
869 return platform_device_add(pdev);
873 /* Public functions for use by device drivers through struct platform_data */
876 * omap_device_enable - fully activate an omap_device
877 * @od: struct omap_device * to activate
879 * Do whatever is necessary for the hwmods underlying omap_device @od
880 * to be accessible and ready to operate. This generally involves
881 * enabling clocks, setting SYSCONFIG registers; and in the future may
882 * involve remuxing pins. Device drivers should call this function
883 * (through platform_data function pointers) where they would normally
884 * enable clocks, etc. Returns -EINVAL if called when the omap_device
885 * is already enabled, or passes along the return value of
886 * _omap_device_activate().
888 int omap_device_enable(struct platform_device *pdev)
891 struct omap_device *od;
893 od = to_omap_device(pdev);
895 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
897 "omap_device: %s() called from invalid state %d\n",
898 __func__, od->_state);
902 /* Enable everything if we're enabling this device from scratch */
903 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
904 od->pm_lat_level = od->pm_lats_cnt;
906 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
908 od->dev_wakeup_lat = 0;
909 od->_dev_wakeup_lat_limit = UINT_MAX;
910 od->_state = OMAP_DEVICE_STATE_ENABLED;
916 * omap_device_idle - idle an omap_device
917 * @od: struct omap_device * to idle
919 * Idle omap_device @od by calling as many .deactivate_func() entries
920 * in the omap_device's pm_lats table as is possible without exceeding
921 * the device's maximum wakeup latency limit, pm_lat_limit. Device
922 * drivers should call this function (through platform_data function
923 * pointers) where they would normally disable clocks after operations
924 * complete, etc.. Returns -EINVAL if the omap_device is not
925 * currently enabled, or passes along the return value of
926 * _omap_device_deactivate().
928 int omap_device_idle(struct platform_device *pdev)
931 struct omap_device *od;
933 od = to_omap_device(pdev);
935 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
937 "omap_device: %s() called from invalid state %d\n",
938 __func__, od->_state);
942 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
944 od->_state = OMAP_DEVICE_STATE_IDLE;
950 * omap_device_shutdown - shut down an omap_device
951 * @od: struct omap_device * to shut down
953 * Shut down omap_device @od by calling all .deactivate_func() entries
954 * in the omap_device's pm_lats table and then shutting down all of
955 * the underlying omap_hwmods. Used when a device is being "removed"
956 * or a device driver is being unloaded. Returns -EINVAL if the
957 * omap_device is not currently enabled or idle, or passes along the
958 * return value of _omap_device_deactivate().
960 int omap_device_shutdown(struct platform_device *pdev)
963 struct omap_device *od;
965 od = to_omap_device(pdev);
967 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
968 od->_state != OMAP_DEVICE_STATE_IDLE) {
970 "omap_device: %s() called from invalid state %d\n",
971 __func__, od->_state);
975 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
977 for (i = 0; i < od->hwmods_cnt; i++)
978 omap_hwmod_shutdown(od->hwmods[i]);
980 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
986 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
987 * @od: struct omap_device *
989 * When a device's maximum wakeup latency limit changes, call some of
990 * the .activate_func or .deactivate_func function pointers in the
991 * omap_device's pm_lats array to ensure that the device's maximum
992 * wakeup latency is less than or equal to the new latency limit.
993 * Intended to be called by OMAP PM code whenever a device's maximum
994 * wakeup latency limit changes (e.g., via
995 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
996 * done (e.g., if the omap_device is not currently idle, or if the
997 * wakeup latency is already current with the new limit) or passes
998 * along the return value of _omap_device_deactivate() or
999 * _omap_device_activate().
1001 int omap_device_align_pm_lat(struct platform_device *pdev,
1002 u32 new_wakeup_lat_limit)
1005 struct omap_device *od;
1007 od = to_omap_device(pdev);
1009 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
1012 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
1014 if (od->_state != OMAP_DEVICE_STATE_IDLE)
1016 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
1017 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
1018 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
1019 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
1025 * omap_device_get_pwrdm - return the powerdomain * associated with @od
1026 * @od: struct omap_device *
1028 * Return the powerdomain associated with the first underlying
1029 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
1030 * code. Returns NULL on error or a struct powerdomain * upon
1033 struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
1036 * XXX Assumes that all omap_hwmod powerdomains are identical.
1037 * This may not necessarily be true. There should be a sanity
1038 * check in here to WARN() if any difference appears.
1040 if (!od->hwmods_cnt)
1043 return omap_hwmod_get_pwrdm(od->hwmods[0]);
1047 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
1048 * @od: struct omap_device *
1050 * Return the MPU's virtual address for the base of the hwmod, from
1051 * the ioremap() that the hwmod code does. Only valid if there is one
1052 * hwmod associated with this device. Returns NULL if there are zero
1053 * or more than one hwmods associated with this omap_device;
1054 * otherwise, passes along the return value from
1055 * omap_hwmod_get_mpu_rt_va().
1057 void __iomem *omap_device_get_rt_va(struct omap_device *od)
1059 if (od->hwmods_cnt != 1)
1062 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1066 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1068 * @oh_name: name of the hwmod device
1070 * Returns back a struct device * pointer associated with a hwmod
1071 * device represented by a hwmod_name
1073 struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1075 struct omap_hwmod *oh;
1078 WARN(1, "%s: no hwmod name!\n", __func__);
1079 return ERR_PTR(-EINVAL);
1082 oh = omap_hwmod_lookup(oh_name);
1083 if (IS_ERR_OR_NULL(oh)) {
1084 WARN(1, "%s: no hwmod for %s\n", __func__,
1086 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1088 if (IS_ERR_OR_NULL(oh->od)) {
1089 WARN(1, "%s: no omap_device for %s\n", __func__,
1091 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1094 if (IS_ERR_OR_NULL(oh->od->pdev))
1095 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1097 return &oh->od->pdev->dev;
1099 EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1102 * Public functions intended for use in omap_device_pm_latency
1103 * .activate_func and .deactivate_func function pointers
1107 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1108 * @od: struct omap_device *od
1110 * Enable all underlying hwmods. Returns 0.
1112 int omap_device_enable_hwmods(struct omap_device *od)
1116 for (i = 0; i < od->hwmods_cnt; i++)
1117 omap_hwmod_enable(od->hwmods[i]);
1119 /* XXX pass along return value here? */
1124 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1125 * @od: struct omap_device *od
1127 * Idle all underlying hwmods. Returns 0.
1129 int omap_device_idle_hwmods(struct omap_device *od)
1133 for (i = 0; i < od->hwmods_cnt; i++)
1134 omap_hwmod_idle(od->hwmods[i]);
1136 /* XXX pass along return value here? */
1141 * omap_device_disable_clocks - disable all main and interface clocks
1142 * @od: struct omap_device *od
1144 * Disable the main functional clock and interface clock for all of the
1145 * omap_hwmods associated with the omap_device. Returns 0.
1147 int omap_device_disable_clocks(struct omap_device *od)
1151 for (i = 0; i < od->hwmods_cnt; i++)
1152 omap_hwmod_disable_clocks(od->hwmods[i]);
1154 /* XXX pass along return value here? */
1159 * omap_device_enable_clocks - enable all main and interface clocks
1160 * @od: struct omap_device *od
1162 * Enable the main functional clock and interface clock for all of the
1163 * omap_hwmods associated with the omap_device. Returns 0.
1165 int omap_device_enable_clocks(struct omap_device *od)
1169 for (i = 0; i < od->hwmods_cnt; i++)
1170 omap_hwmod_enable_clocks(od->hwmods[i]);
1172 /* XXX pass along return value here? */
1176 static struct notifier_block platform_nb = {
1177 .notifier_call = _omap_device_notifier_call,
1180 static int __init omap_device_init(void)
1182 bus_register_notifier(&platform_bus_type, &platform_nb);
1185 core_initcall(omap_device_init);
1188 * omap_device_late_idle - idle devices without drivers
1189 * @dev: struct device * associated with omap_device
1192 * Check the driver bound status of this device, and idle it
1193 * if there is no driver attached.
1195 static int __init omap_device_late_idle(struct device *dev, void *data)
1197 struct platform_device *pdev = to_platform_device(dev);
1198 struct omap_device *od = to_omap_device(pdev);
1204 * If omap_device state is enabled, but has no driver bound,
1207 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) {
1208 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
1209 dev_warn(dev, "%s: enabled but no driver. Idling\n",
1211 omap_device_idle(pdev);
1218 static int __init omap_device_late_init(void)
1220 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
1223 late_initcall(omap_device_late_init);