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 be implemented as a
21 * proper omap_bus/omap_device in Linux, no more platform_data func
28 #include <linux/kernel.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
31 #include <linux/err.h>
33 #include <linux/clk.h>
34 #include <linux/clkdev.h>
35 #include <linux/pm_domain.h>
36 #include <linux/pm_runtime.h>
38 #include <linux/of_address.h>
39 #include <linux/of_irq.h>
40 #include <linux/notifier.h>
44 #include "omap_device.h"
45 #include "omap_hwmod.h"
47 /* Private functions */
49 static void _add_clkdev(struct omap_device *od, const char *clk_alias,
55 if (!clk_alias || !clk_name)
58 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
60 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
62 dev_dbg(&od->pdev->dev,
63 "alias %s already exists\n", clk_alias);
68 r = clk_get_sys(NULL, clk_name);
71 struct of_phandle_args clkspec;
73 clkspec.np = of_find_node_by_name(NULL, clk_name);
75 r = of_clk_get_from_provider(&clkspec);
77 rc = clk_register_clkdev(r, clk_alias,
78 dev_name(&od->pdev->dev));
80 rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev),
85 if (rc == -ENODEV || rc == -ENOMEM)
86 dev_err(&od->pdev->dev,
87 "clkdev_alloc for %s failed\n", clk_alias);
89 dev_err(&od->pdev->dev,
90 "clk_get for %s failed\n", clk_name);
95 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
97 * @od: struct omap_device *od
98 * @oh: struct omap_hwmod *oh
100 * For the main clock and every optional clock present per hwmod per
101 * omap_device, this function adds an entry in the clkdev table of the
102 * form <dev-id=dev_name, con-id=role> if it does not exist already.
104 * The function is called from inside omap_device_build_ss(), after
105 * omap_device_register.
107 * This allows drivers to get a pointer to its optional clocks based on its role
108 * by calling clk_get(<dev*>, <role>).
109 * In the case of the main clock, a "fck" alias is used.
113 static void _add_hwmod_clocks_clkdev(struct omap_device *od,
114 struct omap_hwmod *oh)
118 _add_clkdev(od, "fck", oh->main_clk);
120 for (i = 0; i < oh->opt_clks_cnt; i++)
121 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
126 * omap_device_build_from_dt - build an omap_device with multiple hwmods
127 * @pdev_name: name of the platform_device driver to use
128 * @pdev_id: this platform_device's connection ID
129 * @oh: ptr to the single omap_hwmod that backs this omap_device
130 * @pdata: platform_data ptr to associate with the platform_device
131 * @pdata_len: amount of memory pointed to by @pdata
133 * Function for building an omap_device already registered from device-tree
135 * Returns 0 or PTR_ERR() on error.
137 static int omap_device_build_from_dt(struct platform_device *pdev)
139 struct omap_hwmod **hwmods;
140 struct omap_device *od;
141 struct omap_hwmod *oh;
142 struct device_node *node = pdev->dev.of_node;
145 int oh_cnt, i, ret = 0;
146 bool device_active = false, skip_pm_domain = false;
148 oh_cnt = of_property_count_strings(node, "ti,hwmods");
150 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
154 /* SDMA still needs special handling for omap_device_build() */
155 ret = of_property_read_string_index(node, "ti,hwmods", 0, &oh_name);
156 if (!ret && (!strncmp("dma_system", oh_name, 10) ||
157 !strncmp("dma", oh_name, 3)))
158 skip_pm_domain = true;
160 /* Use ti-sysc driver instead of omap_device? */
161 if (!skip_pm_domain &&
162 !omap_hwmod_parse_module_range(NULL, node, &res))
165 hwmods = kcalloc(oh_cnt, sizeof(struct omap_hwmod *), GFP_KERNEL);
171 for (i = 0; i < oh_cnt; i++) {
172 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
173 oh = omap_hwmod_lookup(oh_name);
175 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
181 if (oh->flags & HWMOD_INIT_NO_IDLE)
182 device_active = true;
185 od = omap_device_alloc(pdev, hwmods, oh_cnt);
187 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
193 /* Fix up missing resource names */
194 for (i = 0; i < pdev->num_resources; i++) {
195 struct resource *r = &pdev->resource[i];
198 r->name = dev_name(&pdev->dev);
201 if (!skip_pm_domain) {
202 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
204 omap_device_enable(pdev);
205 pm_runtime_set_active(&pdev->dev);
212 /* if data/we are at fault.. load up a fail handler */
214 dev_pm_domain_set(&pdev->dev, &omap_device_fail_pm_domain);
219 static int _omap_device_notifier_call(struct notifier_block *nb,
220 unsigned long event, void *dev)
222 struct platform_device *pdev = to_platform_device(dev);
223 struct omap_device *od;
227 case BUS_NOTIFY_REMOVED_DEVICE:
228 if (pdev->archdata.od)
229 omap_device_delete(pdev->archdata.od);
231 case BUS_NOTIFY_UNBOUND_DRIVER:
232 od = to_omap_device(pdev);
233 if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED)) {
234 dev_info(dev, "enabled after unload, idling\n");
235 err = omap_device_idle(pdev);
237 dev_err(dev, "failed to idle\n");
240 case BUS_NOTIFY_BIND_DRIVER:
241 od = to_omap_device(pdev);
242 if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED) &&
243 pm_runtime_status_suspended(dev)) {
244 od->_driver_status = BUS_NOTIFY_BIND_DRIVER;
245 pm_runtime_set_active(dev);
248 case BUS_NOTIFY_ADD_DEVICE:
249 if (pdev->dev.of_node)
250 omap_device_build_from_dt(pdev);
251 omap_auxdata_legacy_init(dev);
254 od = to_omap_device(pdev);
256 od->_driver_status = event;
263 * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
264 * @od: struct omap_device *od
266 * Enable all underlying hwmods. Returns 0.
268 static int _omap_device_enable_hwmods(struct omap_device *od)
273 for (i = 0; i < od->hwmods_cnt; i++)
274 ret |= omap_hwmod_enable(od->hwmods[i]);
280 * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
281 * @od: struct omap_device *od
283 * Idle all underlying hwmods. Returns 0.
285 static int _omap_device_idle_hwmods(struct omap_device *od)
290 for (i = 0; i < od->hwmods_cnt; i++)
291 ret |= omap_hwmod_idle(od->hwmods[i]);
296 /* Public functions for use by core code */
299 * omap_device_get_context_loss_count - get lost context count
300 * @od: struct omap_device *
302 * Using the primary hwmod, query the context loss count for this
305 * Callers should consider context for this device lost any time this
306 * function returns a value different than the value the caller got
307 * the last time it called this function.
309 * If any hwmods exist for the omap_device associated with @pdev,
310 * return the context loss counter for that hwmod, otherwise return
313 int omap_device_get_context_loss_count(struct platform_device *pdev)
315 struct omap_device *od;
318 od = to_omap_device(pdev);
321 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
327 * omap_device_alloc - allocate an omap_device
328 * @pdev: platform_device that will be included in this omap_device
329 * @oh: ptr to the single omap_hwmod that backs this omap_device
330 * @pdata: platform_data ptr to associate with the platform_device
331 * @pdata_len: amount of memory pointed to by @pdata
333 * Convenience function for allocating an omap_device structure and filling
334 * hwmods, and resources.
336 * Returns an struct omap_device pointer or ERR_PTR() on error;
338 struct omap_device *omap_device_alloc(struct platform_device *pdev,
339 struct omap_hwmod **ohs, int oh_cnt)
342 struct omap_device *od;
344 struct omap_hwmod **hwmods;
346 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
351 od->hwmods_cnt = oh_cnt;
353 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
359 pdev->archdata.od = od;
361 for (i = 0; i < oh_cnt; i++) {
363 _add_hwmod_clocks_clkdev(od, hwmods[i]);
371 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
376 void omap_device_delete(struct omap_device *od)
381 od->pdev->archdata.od = NULL;
387 * omap_device_copy_resources - Add legacy IO and IRQ resources
388 * @oh: interconnect target module
389 * @pdev: platform device to copy resources to
391 * We still have legacy DMA and smartreflex needing resources.
392 * Let's populate what they need until we can eventually just
393 * remove this function. Note that there should be no need to
394 * call this from omap_device_build_from_dt(), nor should there
395 * be any need to call it for other devices.
398 omap_device_copy_resources(struct omap_hwmod *oh,
399 struct platform_device *pdev)
401 struct device_node *np, *child;
402 struct property *prop;
403 struct resource *res;
407 if (!oh || !oh->od || !oh->od->pdev)
410 np = oh->od->pdev->dev.of_node;
416 res = kcalloc(2, sizeof(*res), GFP_KERNEL);
420 /* Do we have a dts range for the interconnect target module? */
421 error = omap_hwmod_parse_module_range(oh, np, res);
423 /* No ranges, rely on device reg entry */
425 error = of_address_to_resource(np, 0, res);
429 /* SmartReflex needs first IO resource name to be "mpu" */
433 * We may have a configured "ti,sysc" interconnect target with a
434 * dts child with the interrupt. If so use the first child's
435 * first interrupt for "ti-hwmods" legacy support.
437 of_property_for_each_string(np, "compatible", prop, name)
438 if (!strncmp("ti,sysc-", name, 8))
441 child = of_get_next_available_child(np, NULL);
444 irq = irq_of_parse_and_map(child, 0);
446 irq = irq_of_parse_and_map(np, 0);
452 /* Legacy DMA code needs interrupt name to be "0" */
455 res[1].flags = IORESOURCE_IRQ;
458 error = platform_device_add_resources(pdev, res, 2);
464 WARN(error, "%s: %s device %s failed: %i\n",
465 __func__, oh->name, dev_name(&pdev->dev),
472 * omap_device_build - build and register an omap_device with one omap_hwmod
473 * @pdev_name: name of the platform_device driver to use
474 * @pdev_id: this platform_device's connection ID
475 * @oh: ptr to the single omap_hwmod that backs this omap_device
476 * @pdata: platform_data ptr to associate with the platform_device
477 * @pdata_len: amount of memory pointed to by @pdata
479 * Convenience function for building and registering a single
480 * omap_device record, which in turn builds and registers a
481 * platform_device record. See omap_device_build_ss() for more
482 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
483 * passes along the return value of omap_device_build_ss().
485 struct platform_device __init *omap_device_build(const char *pdev_name,
487 struct omap_hwmod *oh,
488 void *pdata, int pdata_len)
491 struct platform_device *pdev;
492 struct omap_device *od;
494 if (!oh || !pdev_name)
495 return ERR_PTR(-EINVAL);
497 if (!pdata && pdata_len > 0)
498 return ERR_PTR(-EINVAL);
500 if (strncmp(oh->name, "smartreflex", 11) &&
501 strncmp(oh->name, "dma", 3)) {
502 pr_warn("%s need to update %s to probe with dt\na",
503 __func__, pdev_name);
508 pdev = platform_device_alloc(pdev_name, pdev_id);
514 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
516 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
518 dev_set_name(&pdev->dev, "%s", pdev->name);
521 * Must be called before omap_device_alloc() as oh->od
522 * only contains the currently registered omap_device
523 * and will get overwritten by omap_device_alloc().
525 ret = omap_device_copy_resources(oh, pdev);
529 od = omap_device_alloc(pdev, &oh, 1);
535 ret = platform_device_add_data(pdev, pdata, pdata_len);
539 ret = omap_device_register(pdev);
546 omap_device_delete(od);
548 platform_device_put(pdev);
551 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
557 static int _od_runtime_suspend(struct device *dev)
559 struct platform_device *pdev = to_platform_device(dev);
562 ret = pm_generic_runtime_suspend(dev);
566 return omap_device_idle(pdev);
569 static int _od_runtime_resume(struct device *dev)
571 struct platform_device *pdev = to_platform_device(dev);
574 ret = omap_device_enable(pdev);
576 dev_err(dev, "use pm_runtime_put_sync_suspend() in driver?\n");
580 return pm_generic_runtime_resume(dev);
583 static int _od_fail_runtime_suspend(struct device *dev)
585 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
589 static int _od_fail_runtime_resume(struct device *dev)
591 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
597 #ifdef CONFIG_SUSPEND
598 static int _od_suspend_noirq(struct device *dev)
600 struct platform_device *pdev = to_platform_device(dev);
601 struct omap_device *od = to_omap_device(pdev);
604 /* Don't attempt late suspend on a driver that is not bound */
605 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
608 ret = pm_generic_suspend_noirq(dev);
610 if (!ret && !pm_runtime_status_suspended(dev)) {
611 if (pm_generic_runtime_suspend(dev) == 0) {
612 omap_device_idle(pdev);
613 od->flags |= OMAP_DEVICE_SUSPENDED;
620 static int _od_resume_noirq(struct device *dev)
622 struct platform_device *pdev = to_platform_device(dev);
623 struct omap_device *od = to_omap_device(pdev);
625 if (od->flags & OMAP_DEVICE_SUSPENDED) {
626 od->flags &= ~OMAP_DEVICE_SUSPENDED;
627 omap_device_enable(pdev);
628 pm_generic_runtime_resume(dev);
631 return pm_generic_resume_noirq(dev);
634 #define _od_suspend_noirq NULL
635 #define _od_resume_noirq NULL
638 struct dev_pm_domain omap_device_fail_pm_domain = {
640 SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
641 _od_fail_runtime_resume, NULL)
645 struct dev_pm_domain omap_device_pm_domain = {
647 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
649 USE_PLATFORM_PM_SLEEP_OPS
650 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
656 * omap_device_register - register an omap_device with one omap_hwmod
657 * @od: struct omap_device * to register
659 * Register the omap_device structure. This currently just calls
660 * platform_device_register() on the underlying platform_device.
661 * Returns the return value of platform_device_register().
663 int omap_device_register(struct platform_device *pdev)
665 pr_debug("omap_device: %s: registering\n", pdev->name);
667 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
668 return platform_device_add(pdev);
672 /* Public functions for use by device drivers through struct platform_data */
675 * omap_device_enable - fully activate an omap_device
676 * @od: struct omap_device * to activate
678 * Do whatever is necessary for the hwmods underlying omap_device @od
679 * to be accessible and ready to operate. This generally involves
680 * enabling clocks, setting SYSCONFIG registers; and in the future may
681 * involve remuxing pins. Device drivers should call this function
682 * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
683 * the omap_device is already enabled, or passes along the return
684 * value of _omap_device_enable_hwmods().
686 int omap_device_enable(struct platform_device *pdev)
689 struct omap_device *od;
691 od = to_omap_device(pdev);
693 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
695 "omap_device: %s() called from invalid state %d\n",
696 __func__, od->_state);
700 ret = _omap_device_enable_hwmods(od);
703 od->_state = OMAP_DEVICE_STATE_ENABLED;
709 * omap_device_idle - idle an omap_device
710 * @od: struct omap_device * to idle
712 * Idle omap_device @od. Device drivers call this function indirectly
713 * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not
714 * currently enabled, or passes along the return value of
715 * _omap_device_idle_hwmods().
717 int omap_device_idle(struct platform_device *pdev)
720 struct omap_device *od;
722 od = to_omap_device(pdev);
724 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
726 "omap_device: %s() called from invalid state %d\n",
727 __func__, od->_state);
731 ret = _omap_device_idle_hwmods(od);
734 od->_state = OMAP_DEVICE_STATE_IDLE;
740 * omap_device_assert_hardreset - set a device's hardreset line
741 * @pdev: struct platform_device * to reset
742 * @name: const char * name of the reset line
744 * Set the hardreset line identified by @name on the IP blocks
745 * associated with the hwmods backing the platform_device @pdev. All
746 * of the hwmods associated with @pdev must have the same hardreset
747 * line linked to them for this to work. Passes along the return value
748 * of omap_hwmod_assert_hardreset() in the event of any failure, or
749 * returns 0 upon success.
751 int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
753 struct omap_device *od = to_omap_device(pdev);
757 for (i = 0; i < od->hwmods_cnt; i++) {
758 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
767 * omap_device_deassert_hardreset - release a device's hardreset line
768 * @pdev: struct platform_device * to reset
769 * @name: const char * name of the reset line
771 * Release the hardreset line identified by @name on the IP blocks
772 * associated with the hwmods backing the platform_device @pdev. All
773 * of the hwmods associated with @pdev must have the same hardreset
774 * line linked to them for this to work. Passes along the return
775 * value of omap_hwmod_deassert_hardreset() in the event of any
776 * failure, or returns 0 upon success.
778 int omap_device_deassert_hardreset(struct platform_device *pdev,
781 struct omap_device *od = to_omap_device(pdev);
785 for (i = 0; i < od->hwmods_cnt; i++) {
786 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
795 * omap_device_get_by_hwmod_name() - convert a hwmod name to
797 * @oh_name: name of the hwmod device
799 * Returns back a struct device * pointer associated with a hwmod
800 * device represented by a hwmod_name
802 struct device *omap_device_get_by_hwmod_name(const char *oh_name)
804 struct omap_hwmod *oh;
807 WARN(1, "%s: no hwmod name!\n", __func__);
808 return ERR_PTR(-EINVAL);
811 oh = omap_hwmod_lookup(oh_name);
813 WARN(1, "%s: no hwmod for %s\n", __func__,
815 return ERR_PTR(-ENODEV);
818 WARN(1, "%s: no omap_device for %s\n", __func__,
820 return ERR_PTR(-ENODEV);
823 return &oh->od->pdev->dev;
826 static struct notifier_block platform_nb = {
827 .notifier_call = _omap_device_notifier_call,
830 static int __init omap_device_init(void)
832 bus_register_notifier(&platform_bus_type, &platform_nb);
835 omap_postcore_initcall(omap_device_init);
838 * omap_device_late_idle - idle devices without drivers
839 * @dev: struct device * associated with omap_device
842 * Check the driver bound status of this device, and idle it
843 * if there is no driver attached.
845 static int __init omap_device_late_idle(struct device *dev, void *data)
847 struct platform_device *pdev = to_platform_device(dev);
848 struct omap_device *od = to_omap_device(pdev);
855 * If omap_device state is enabled, but has no driver bound,
860 * Some devices (like memory controllers) are always kept
861 * enabled, and should not be idled even with no drivers.
863 for (i = 0; i < od->hwmods_cnt; i++)
864 if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
867 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
868 od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
869 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
870 dev_warn(dev, "%s: enabled but no driver. Idling\n",
872 omap_device_idle(pdev);
879 static int __init omap_device_late_init(void)
881 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
885 omap_late_initcall_sync(omap_device_late_init);