1 // SPDX-License-Identifier: GPL-2.0-only
3 * omap_device implementation
5 * Copyright (C) 2009-2010 Nokia Corporation
6 * Paul Walmsley, Kevin Hilman
8 * Developed in collaboration with (alphabetical order): Benoit
9 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
10 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
13 * This code provides a consistent interface for OMAP device drivers
14 * to control power management and interconnect properties of their
17 * In the medium- to long-term, this code should be implemented as a
18 * proper omap_bus/omap_device in Linux, no more platform_data func
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/err.h>
28 #include <linux/clk.h>
29 #include <linux/clkdev.h>
30 #include <linux/pm_domain.h>
31 #include <linux/pm_runtime.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/notifier.h>
39 #include "omap_device.h"
40 #include "omap_hwmod.h"
42 /* Private functions */
44 static void _add_clkdev(struct omap_device *od, const char *clk_alias,
50 if (!clk_alias || !clk_name)
53 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
55 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
57 dev_dbg(&od->pdev->dev,
58 "alias %s already exists\n", clk_alias);
63 r = clk_get_sys(NULL, clk_name);
66 struct of_phandle_args clkspec;
68 clkspec.np = of_find_node_by_name(NULL, clk_name);
70 r = of_clk_get_from_provider(&clkspec);
72 rc = clk_register_clkdev(r, clk_alias,
73 dev_name(&od->pdev->dev));
75 rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev),
80 if (rc == -ENODEV || rc == -ENOMEM)
81 dev_err(&od->pdev->dev,
82 "clkdev_alloc for %s failed\n", clk_alias);
84 dev_err(&od->pdev->dev,
85 "clk_get for %s failed\n", clk_name);
90 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
92 * @od: struct omap_device *od
93 * @oh: struct omap_hwmod *oh
95 * For the main clock and every optional clock present per hwmod per
96 * omap_device, this function adds an entry in the clkdev table of the
97 * form <dev-id=dev_name, con-id=role> if it does not exist already.
99 * The function is called from inside omap_device_build_ss(), after
100 * omap_device_register.
102 * This allows drivers to get a pointer to its optional clocks based on its role
103 * by calling clk_get(<dev*>, <role>).
104 * In the case of the main clock, a "fck" alias is used.
108 static void _add_hwmod_clocks_clkdev(struct omap_device *od,
109 struct omap_hwmod *oh)
113 _add_clkdev(od, "fck", oh->main_clk);
115 for (i = 0; i < oh->opt_clks_cnt; i++)
116 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
121 * omap_device_build_from_dt - build an omap_device with multiple hwmods
122 * @pdev_name: name of the platform_device driver to use
123 * @pdev_id: this platform_device's connection ID
124 * @oh: ptr to the single omap_hwmod that backs this omap_device
125 * @pdata: platform_data ptr to associate with the platform_device
126 * @pdata_len: amount of memory pointed to by @pdata
128 * Function for building an omap_device already registered from device-tree
130 * Returns 0 or PTR_ERR() on error.
132 static int omap_device_build_from_dt(struct platform_device *pdev)
134 struct omap_hwmod **hwmods;
135 struct omap_device *od;
136 struct omap_hwmod *oh;
137 struct device_node *node = pdev->dev.of_node;
140 int oh_cnt, i, ret = 0;
141 bool device_active = false, skip_pm_domain = false;
143 oh_cnt = of_property_count_strings(node, "ti,hwmods");
145 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
149 /* SDMA still needs special handling for omap_device_build() */
150 ret = of_property_read_string_index(node, "ti,hwmods", 0, &oh_name);
151 if (!ret && (!strncmp("dma_system", oh_name, 10) ||
152 !strncmp("dma", oh_name, 3)))
153 skip_pm_domain = true;
155 /* Use ti-sysc driver instead of omap_device? */
156 if (!skip_pm_domain &&
157 !omap_hwmod_parse_module_range(NULL, node, &res))
160 hwmods = kcalloc(oh_cnt, sizeof(struct omap_hwmod *), GFP_KERNEL);
166 for (i = 0; i < oh_cnt; i++) {
167 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
168 oh = omap_hwmod_lookup(oh_name);
170 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
176 if (oh->flags & HWMOD_INIT_NO_IDLE)
177 device_active = true;
180 od = omap_device_alloc(pdev, hwmods, oh_cnt);
182 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
188 /* Fix up missing resource names */
189 for (i = 0; i < pdev->num_resources; i++) {
190 struct resource *r = &pdev->resource[i];
193 r->name = dev_name(&pdev->dev);
196 if (!skip_pm_domain) {
197 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
199 omap_device_enable(pdev);
200 pm_runtime_set_active(&pdev->dev);
207 /* if data/we are at fault.. load up a fail handler */
209 dev_pm_domain_set(&pdev->dev, &omap_device_fail_pm_domain);
214 static int _omap_device_notifier_call(struct notifier_block *nb,
215 unsigned long event, void *dev)
217 struct platform_device *pdev = to_platform_device(dev);
218 struct omap_device *od;
222 case BUS_NOTIFY_REMOVED_DEVICE:
223 if (pdev->archdata.od)
224 omap_device_delete(pdev->archdata.od);
226 case BUS_NOTIFY_UNBOUND_DRIVER:
227 od = to_omap_device(pdev);
228 if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED)) {
229 dev_info(dev, "enabled after unload, idling\n");
230 err = omap_device_idle(pdev);
232 dev_err(dev, "failed to idle\n");
235 case BUS_NOTIFY_BIND_DRIVER:
236 od = to_omap_device(pdev);
238 od->_driver_status = BUS_NOTIFY_BIND_DRIVER;
239 if (od->_state == OMAP_DEVICE_STATE_ENABLED &&
240 pm_runtime_status_suspended(dev)) {
241 pm_runtime_set_active(dev);
245 case BUS_NOTIFY_ADD_DEVICE:
246 if (pdev->dev.of_node)
247 omap_device_build_from_dt(pdev);
248 omap_auxdata_legacy_init(dev);
251 od = to_omap_device(pdev);
253 od->_driver_status = event;
260 * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
261 * @od: struct omap_device *od
263 * Enable all underlying hwmods. Returns 0.
265 static int _omap_device_enable_hwmods(struct omap_device *od)
270 for (i = 0; i < od->hwmods_cnt; i++)
271 ret |= omap_hwmod_enable(od->hwmods[i]);
277 * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
278 * @od: struct omap_device *od
280 * Idle all underlying hwmods. Returns 0.
282 static int _omap_device_idle_hwmods(struct omap_device *od)
287 for (i = 0; i < od->hwmods_cnt; i++)
288 ret |= omap_hwmod_idle(od->hwmods[i]);
293 /* Public functions for use by core code */
296 * omap_device_get_context_loss_count - get lost context count
297 * @od: struct omap_device *
299 * Using the primary hwmod, query the context loss count for this
302 * Callers should consider context for this device lost any time this
303 * function returns a value different than the value the caller got
304 * the last time it called this function.
306 * If any hwmods exist for the omap_device associated with @pdev,
307 * return the context loss counter for that hwmod, otherwise return
310 int omap_device_get_context_loss_count(struct platform_device *pdev)
312 struct omap_device *od;
315 od = to_omap_device(pdev);
318 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
324 * omap_device_alloc - allocate an omap_device
325 * @pdev: platform_device that will be included in this omap_device
326 * @oh: ptr to the single omap_hwmod that backs this omap_device
327 * @pdata: platform_data ptr to associate with the platform_device
328 * @pdata_len: amount of memory pointed to by @pdata
330 * Convenience function for allocating an omap_device structure and filling
331 * hwmods, and resources.
333 * Returns an struct omap_device pointer or ERR_PTR() on error;
335 struct omap_device *omap_device_alloc(struct platform_device *pdev,
336 struct omap_hwmod **ohs, int oh_cnt)
339 struct omap_device *od;
341 struct omap_hwmod **hwmods;
343 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
348 od->hwmods_cnt = oh_cnt;
350 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
356 pdev->archdata.od = od;
358 for (i = 0; i < oh_cnt; i++) {
360 _add_hwmod_clocks_clkdev(od, hwmods[i]);
368 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
373 void omap_device_delete(struct omap_device *od)
378 od->pdev->archdata.od = NULL;
384 * omap_device_copy_resources - Add legacy IO and IRQ resources
385 * @oh: interconnect target module
386 * @pdev: platform device to copy resources to
388 * We still have legacy DMA and smartreflex needing resources.
389 * Let's populate what they need until we can eventually just
390 * remove this function. Note that there should be no need to
391 * call this from omap_device_build_from_dt(), nor should there
392 * be any need to call it for other devices.
395 omap_device_copy_resources(struct omap_hwmod *oh,
396 struct platform_device *pdev)
398 struct device_node *np, *child;
399 struct property *prop;
400 struct resource *res;
404 if (!oh || !oh->od || !oh->od->pdev)
407 np = oh->od->pdev->dev.of_node;
413 res = kcalloc(2, sizeof(*res), GFP_KERNEL);
417 /* Do we have a dts range for the interconnect target module? */
418 error = omap_hwmod_parse_module_range(oh, np, res);
420 /* No ranges, rely on device reg entry */
422 error = of_address_to_resource(np, 0, res);
426 /* SmartReflex needs first IO resource name to be "mpu" */
430 * We may have a configured "ti,sysc" interconnect target with a
431 * dts child with the interrupt. If so use the first child's
432 * first interrupt for "ti-hwmods" legacy support.
434 of_property_for_each_string(np, "compatible", prop, name)
435 if (!strncmp("ti,sysc-", name, 8))
438 child = of_get_next_available_child(np, NULL);
441 irq = irq_of_parse_and_map(child, 0);
443 irq = irq_of_parse_and_map(np, 0);
449 /* Legacy DMA code needs interrupt name to be "0" */
452 res[1].flags = IORESOURCE_IRQ;
455 error = platform_device_add_resources(pdev, res, 2);
461 WARN(error, "%s: %s device %s failed: %i\n",
462 __func__, oh->name, dev_name(&pdev->dev),
469 * omap_device_build - build and register an omap_device with one omap_hwmod
470 * @pdev_name: name of the platform_device driver to use
471 * @pdev_id: this platform_device's connection ID
472 * @oh: ptr to the single omap_hwmod that backs this omap_device
473 * @pdata: platform_data ptr to associate with the platform_device
474 * @pdata_len: amount of memory pointed to by @pdata
476 * Convenience function for building and registering a single
477 * omap_device record, which in turn builds and registers a
478 * platform_device record. See omap_device_build_ss() for more
479 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
480 * passes along the return value of omap_device_build_ss().
482 struct platform_device __init *omap_device_build(const char *pdev_name,
484 struct omap_hwmod *oh,
485 void *pdata, int pdata_len)
488 struct platform_device *pdev;
489 struct omap_device *od;
491 if (!oh || !pdev_name)
492 return ERR_PTR(-EINVAL);
494 if (!pdata && pdata_len > 0)
495 return ERR_PTR(-EINVAL);
497 if (strncmp(oh->name, "smartreflex", 11) &&
498 strncmp(oh->name, "dma", 3)) {
499 pr_warn("%s need to update %s to probe with dt\na",
500 __func__, pdev_name);
505 pdev = platform_device_alloc(pdev_name, pdev_id);
511 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
513 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
515 dev_set_name(&pdev->dev, "%s", pdev->name);
518 * Must be called before omap_device_alloc() as oh->od
519 * only contains the currently registered omap_device
520 * and will get overwritten by omap_device_alloc().
522 ret = omap_device_copy_resources(oh, pdev);
526 od = omap_device_alloc(pdev, &oh, 1);
532 ret = platform_device_add_data(pdev, pdata, pdata_len);
536 ret = omap_device_register(pdev);
543 omap_device_delete(od);
545 platform_device_put(pdev);
548 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
554 static int _od_runtime_suspend(struct device *dev)
556 struct platform_device *pdev = to_platform_device(dev);
559 ret = pm_generic_runtime_suspend(dev);
563 return omap_device_idle(pdev);
566 static int _od_runtime_resume(struct device *dev)
568 struct platform_device *pdev = to_platform_device(dev);
571 ret = omap_device_enable(pdev);
573 dev_err(dev, "use pm_runtime_put_sync_suspend() in driver?\n");
577 return pm_generic_runtime_resume(dev);
580 static int _od_fail_runtime_suspend(struct device *dev)
582 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
586 static int _od_fail_runtime_resume(struct device *dev)
588 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
594 #ifdef CONFIG_SUSPEND
595 static int _od_suspend_noirq(struct device *dev)
597 struct platform_device *pdev = to_platform_device(dev);
598 struct omap_device *od = to_omap_device(pdev);
601 /* Don't attempt late suspend on a driver that is not bound */
602 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
605 ret = pm_generic_suspend_noirq(dev);
607 if (!ret && !pm_runtime_status_suspended(dev)) {
608 if (pm_generic_runtime_suspend(dev) == 0) {
609 omap_device_idle(pdev);
610 od->flags |= OMAP_DEVICE_SUSPENDED;
617 static int _od_resume_noirq(struct device *dev)
619 struct platform_device *pdev = to_platform_device(dev);
620 struct omap_device *od = to_omap_device(pdev);
622 if (od->flags & OMAP_DEVICE_SUSPENDED) {
623 od->flags &= ~OMAP_DEVICE_SUSPENDED;
624 omap_device_enable(pdev);
625 pm_generic_runtime_resume(dev);
628 return pm_generic_resume_noirq(dev);
631 #define _od_suspend_noirq NULL
632 #define _od_resume_noirq NULL
635 struct dev_pm_domain omap_device_fail_pm_domain = {
637 SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
638 _od_fail_runtime_resume, NULL)
642 struct dev_pm_domain omap_device_pm_domain = {
644 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
646 USE_PLATFORM_PM_SLEEP_OPS
647 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
653 * omap_device_register - register an omap_device with one omap_hwmod
654 * @od: struct omap_device * to register
656 * Register the omap_device structure. This currently just calls
657 * platform_device_register() on the underlying platform_device.
658 * Returns the return value of platform_device_register().
660 int omap_device_register(struct platform_device *pdev)
662 pr_debug("omap_device: %s: registering\n", pdev->name);
664 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
665 return platform_device_add(pdev);
669 /* Public functions for use by device drivers through struct platform_data */
672 * omap_device_enable - fully activate an omap_device
673 * @od: struct omap_device * to activate
675 * Do whatever is necessary for the hwmods underlying omap_device @od
676 * to be accessible and ready to operate. This generally involves
677 * enabling clocks, setting SYSCONFIG registers; and in the future may
678 * involve remuxing pins. Device drivers should call this function
679 * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
680 * the omap_device is already enabled, or passes along the return
681 * value of _omap_device_enable_hwmods().
683 int omap_device_enable(struct platform_device *pdev)
686 struct omap_device *od;
688 od = to_omap_device(pdev);
690 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
692 "omap_device: %s() called from invalid state %d\n",
693 __func__, od->_state);
697 ret = _omap_device_enable_hwmods(od);
700 od->_state = OMAP_DEVICE_STATE_ENABLED;
706 * omap_device_idle - idle an omap_device
707 * @od: struct omap_device * to idle
709 * Idle omap_device @od. Device drivers call this function indirectly
710 * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not
711 * currently enabled, or passes along the return value of
712 * _omap_device_idle_hwmods().
714 int omap_device_idle(struct platform_device *pdev)
717 struct omap_device *od;
719 od = to_omap_device(pdev);
721 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
723 "omap_device: %s() called from invalid state %d\n",
724 __func__, od->_state);
728 ret = _omap_device_idle_hwmods(od);
731 od->_state = OMAP_DEVICE_STATE_IDLE;
737 * omap_device_assert_hardreset - set a device's hardreset line
738 * @pdev: struct platform_device * to reset
739 * @name: const char * name of the reset line
741 * Set the hardreset line identified by @name on the IP blocks
742 * associated with the hwmods backing the platform_device @pdev. All
743 * of the hwmods associated with @pdev must have the same hardreset
744 * line linked to them for this to work. Passes along the return value
745 * of omap_hwmod_assert_hardreset() in the event of any failure, or
746 * returns 0 upon success.
748 int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
750 struct omap_device *od = to_omap_device(pdev);
754 for (i = 0; i < od->hwmods_cnt; i++) {
755 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
764 * omap_device_deassert_hardreset - release a device's hardreset line
765 * @pdev: struct platform_device * to reset
766 * @name: const char * name of the reset line
768 * Release the hardreset line identified by @name on the IP blocks
769 * associated with the hwmods backing the platform_device @pdev. All
770 * of the hwmods associated with @pdev must have the same hardreset
771 * line linked to them for this to work. Passes along the return
772 * value of omap_hwmod_deassert_hardreset() in the event of any
773 * failure, or returns 0 upon success.
775 int omap_device_deassert_hardreset(struct platform_device *pdev,
778 struct omap_device *od = to_omap_device(pdev);
782 for (i = 0; i < od->hwmods_cnt; i++) {
783 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
792 * omap_device_get_by_hwmod_name() - convert a hwmod name to
794 * @oh_name: name of the hwmod device
796 * Returns back a struct device * pointer associated with a hwmod
797 * device represented by a hwmod_name
799 struct device *omap_device_get_by_hwmod_name(const char *oh_name)
801 struct omap_hwmod *oh;
804 WARN(1, "%s: no hwmod name!\n", __func__);
805 return ERR_PTR(-EINVAL);
808 oh = omap_hwmod_lookup(oh_name);
810 WARN(1, "%s: no hwmod for %s\n", __func__,
812 return ERR_PTR(-ENODEV);
815 WARN(1, "%s: no omap_device for %s\n", __func__,
817 return ERR_PTR(-ENODEV);
820 return &oh->od->pdev->dev;
823 static struct notifier_block platform_nb = {
824 .notifier_call = _omap_device_notifier_call,
827 static int __init omap_device_init(void)
829 bus_register_notifier(&platform_bus_type, &platform_nb);
832 omap_postcore_initcall(omap_device_init);
835 * omap_device_late_idle - idle devices without drivers
836 * @dev: struct device * associated with omap_device
839 * Check the driver bound status of this device, and idle it
840 * if there is no driver attached.
842 static int __init omap_device_late_idle(struct device *dev, void *data)
844 struct platform_device *pdev = to_platform_device(dev);
845 struct omap_device *od = to_omap_device(pdev);
852 * If omap_device state is enabled, but has no driver bound,
857 * Some devices (like memory controllers) are always kept
858 * enabled, and should not be idled even with no drivers.
860 for (i = 0; i < od->hwmods_cnt; i++)
861 if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
864 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
865 od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
866 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
867 dev_warn(dev, "%s: enabled but no driver. Idling\n",
869 omap_device_idle(pdev);
876 static int __init omap_device_late_init(void)
878 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
882 omap_late_initcall_sync(omap_device_late_init);