1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * omap_device implementation
4 *
5 * Copyright (C) 2009-2010 Nokia Corporation
6 * Paul Walmsley, Kevin Hilman
7 *
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
11 * Woodruff
12 *
13 * This code provides a consistent interface for OMAP device drivers
14 * to control power management and interconnect properties of their
15 * devices.
16 *
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
19 * pointers
20 */
21 #undef DEBUG
22
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/clk.h>
29 #include <linux/clkdev.h>
30 #include <linux/pm_domain.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/of.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/notifier.h>
36
37 #include "common.h"
38 #include "soc.h"
39 #include "omap_device.h"
40 #include "omap_hwmod.h"
41
42 /* Private functions */
43
_add_clkdev(struct omap_device * od,const char * clk_alias,const char * clk_name)44 static void _add_clkdev(struct omap_device *od, const char *clk_alias,
45 const char *clk_name)
46 {
47 struct clk *r;
48 int rc;
49
50 if (!clk_alias || !clk_name)
51 return;
52
53 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
54
55 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
56 if (!IS_ERR(r)) {
57 dev_dbg(&od->pdev->dev,
58 "alias %s already exists\n", clk_alias);
59 clk_put(r);
60 return;
61 }
62
63 r = clk_get_sys(NULL, clk_name);
64
65 if (IS_ERR(r)) {
66 struct of_phandle_args clkspec;
67
68 clkspec.np = of_find_node_by_name(NULL, clk_name);
69
70 r = of_clk_get_from_provider(&clkspec);
71
72 rc = clk_register_clkdev(r, clk_alias,
73 dev_name(&od->pdev->dev));
74 } else {
75 rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev),
76 clk_name, NULL);
77 }
78
79 if (rc) {
80 if (rc == -ENODEV || rc == -ENOMEM)
81 dev_err(&od->pdev->dev,
82 "clkdev_alloc for %s failed\n", clk_alias);
83 else
84 dev_err(&od->pdev->dev,
85 "clk_get for %s failed\n", clk_name);
86 }
87 }
88
89 /**
90 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
91 * and main clock
92 * @od: struct omap_device *od
93 * @oh: struct omap_hwmod *oh
94 *
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.
98 *
99 * The function is called from inside omap_device_build_ss(), after
100 * omap_device_register.
101 *
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.
105 *
106 * No return value.
107 */
_add_hwmod_clocks_clkdev(struct omap_device * od,struct omap_hwmod * oh)108 static void _add_hwmod_clocks_clkdev(struct omap_device *od,
109 struct omap_hwmod *oh)
110 {
111 int i;
112
113 _add_clkdev(od, "fck", oh->main_clk);
114
115 for (i = 0; i < oh->opt_clks_cnt; i++)
116 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
117 }
118
119
120 /**
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
127 *
128 * Function for building an omap_device already registered from device-tree
129 *
130 * Returns 0 or PTR_ERR() on error.
131 */
omap_device_build_from_dt(struct platform_device * pdev)132 static int omap_device_build_from_dt(struct platform_device *pdev)
133 {
134 struct omap_hwmod **hwmods;
135 struct omap_device *od;
136 struct omap_hwmod *oh;
137 struct device_node *node = pdev->dev.of_node;
138 struct resource res;
139 const char *oh_name;
140 int oh_cnt, i, ret = 0;
141 bool device_active = false, skip_pm_domain = false;
142
143 oh_cnt = of_property_count_strings(node, "ti,hwmods");
144 if (oh_cnt <= 0) {
145 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
146 return -ENODEV;
147 }
148
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;
154
155 /* Use ti-sysc driver instead of omap_device? */
156 if (!skip_pm_domain &&
157 !omap_hwmod_parse_module_range(NULL, node, &res))
158 return -ENODEV;
159
160 hwmods = kcalloc(oh_cnt, sizeof(struct omap_hwmod *), GFP_KERNEL);
161 if (!hwmods) {
162 ret = -ENOMEM;
163 goto odbfd_exit;
164 }
165
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);
169 if (!oh) {
170 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
171 oh_name);
172 ret = -EINVAL;
173 goto odbfd_exit1;
174 }
175 hwmods[i] = oh;
176 if (oh->flags & HWMOD_INIT_NO_IDLE)
177 device_active = true;
178 }
179
180 od = omap_device_alloc(pdev, hwmods, oh_cnt);
181 if (IS_ERR(od)) {
182 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
183 oh_name);
184 ret = PTR_ERR(od);
185 goto odbfd_exit1;
186 }
187
188 /* Fix up missing resource names */
189 for (i = 0; i < pdev->num_resources; i++) {
190 struct resource *r = &pdev->resource[i];
191
192 if (r->name == NULL)
193 r->name = dev_name(&pdev->dev);
194 }
195
196 if (!skip_pm_domain) {
197 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
198 if (device_active) {
199 omap_device_enable(pdev);
200 pm_runtime_set_active(&pdev->dev);
201 }
202 }
203
204 odbfd_exit1:
205 kfree(hwmods);
206 odbfd_exit:
207 /* if data/we are at fault.. load up a fail handler */
208 if (ret)
209 dev_pm_domain_set(&pdev->dev, &omap_device_fail_pm_domain);
210
211 return ret;
212 }
213
_omap_device_notifier_call(struct notifier_block * nb,unsigned long event,void * dev)214 static int _omap_device_notifier_call(struct notifier_block *nb,
215 unsigned long event, void *dev)
216 {
217 struct platform_device *pdev = to_platform_device(dev);
218 struct omap_device *od;
219 int err;
220
221 switch (event) {
222 case BUS_NOTIFY_REMOVED_DEVICE:
223 if (pdev->archdata.od)
224 omap_device_delete(pdev->archdata.od);
225 break;
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);
231 if (err)
232 dev_err(dev, "failed to idle\n");
233 }
234 break;
235 case BUS_NOTIFY_BIND_DRIVER:
236 od = to_omap_device(pdev);
237 if (od) {
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);
242 }
243 }
244 break;
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);
249 /* fall through */
250 default:
251 od = to_omap_device(pdev);
252 if (od)
253 od->_driver_status = event;
254 }
255
256 return NOTIFY_DONE;
257 }
258
259 /**
260 * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
261 * @od: struct omap_device *od
262 *
263 * Enable all underlying hwmods. Returns 0.
264 */
_omap_device_enable_hwmods(struct omap_device * od)265 static int _omap_device_enable_hwmods(struct omap_device *od)
266 {
267 int ret = 0;
268 int i;
269
270 for (i = 0; i < od->hwmods_cnt; i++)
271 ret |= omap_hwmod_enable(od->hwmods[i]);
272
273 return ret;
274 }
275
276 /**
277 * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
278 * @od: struct omap_device *od
279 *
280 * Idle all underlying hwmods. Returns 0.
281 */
_omap_device_idle_hwmods(struct omap_device * od)282 static int _omap_device_idle_hwmods(struct omap_device *od)
283 {
284 int ret = 0;
285 int i;
286
287 for (i = 0; i < od->hwmods_cnt; i++)
288 ret |= omap_hwmod_idle(od->hwmods[i]);
289
290 return ret;
291 }
292
293 /* Public functions for use by core code */
294
295 /**
296 * omap_device_get_context_loss_count - get lost context count
297 * @od: struct omap_device *
298 *
299 * Using the primary hwmod, query the context loss count for this
300 * device.
301 *
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.
305 *
306 * If any hwmods exist for the omap_device associated with @pdev,
307 * return the context loss counter for that hwmod, otherwise return
308 * zero.
309 */
omap_device_get_context_loss_count(struct platform_device * pdev)310 int omap_device_get_context_loss_count(struct platform_device *pdev)
311 {
312 struct omap_device *od;
313 u32 ret = 0;
314
315 od = to_omap_device(pdev);
316
317 if (od->hwmods_cnt)
318 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
319
320 return ret;
321 }
322
323 /**
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
329 *
330 * Convenience function for allocating an omap_device structure and filling
331 * hwmods, and resources.
332 *
333 * Returns an struct omap_device pointer or ERR_PTR() on error;
334 */
omap_device_alloc(struct platform_device * pdev,struct omap_hwmod ** ohs,int oh_cnt)335 struct omap_device *omap_device_alloc(struct platform_device *pdev,
336 struct omap_hwmod **ohs, int oh_cnt)
337 {
338 int ret = -ENOMEM;
339 struct omap_device *od;
340 int i;
341 struct omap_hwmod **hwmods;
342
343 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
344 if (!od) {
345 ret = -ENOMEM;
346 goto oda_exit1;
347 }
348 od->hwmods_cnt = oh_cnt;
349
350 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
351 if (!hwmods)
352 goto oda_exit2;
353
354 od->hwmods = hwmods;
355 od->pdev = pdev;
356 pdev->archdata.od = od;
357
358 for (i = 0; i < oh_cnt; i++) {
359 hwmods[i]->od = od;
360 _add_hwmod_clocks_clkdev(od, hwmods[i]);
361 }
362
363 return od;
364
365 oda_exit2:
366 kfree(od);
367 oda_exit1:
368 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
369
370 return ERR_PTR(ret);
371 }
372
omap_device_delete(struct omap_device * od)373 void omap_device_delete(struct omap_device *od)
374 {
375 if (!od)
376 return;
377
378 od->pdev->archdata.od = NULL;
379 kfree(od->hwmods);
380 kfree(od);
381 }
382
383 /**
384 * omap_device_copy_resources - Add legacy IO and IRQ resources
385 * @oh: interconnect target module
386 * @pdev: platform device to copy resources to
387 *
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.
393 */
394 static int
omap_device_copy_resources(struct omap_hwmod * oh,struct platform_device * pdev)395 omap_device_copy_resources(struct omap_hwmod *oh,
396 struct platform_device *pdev)
397 {
398 struct device_node *np, *child;
399 struct property *prop;
400 struct resource *res;
401 const char *name;
402 int error, irq = 0;
403
404 if (!oh || !oh->od || !oh->od->pdev)
405 return -EINVAL;
406
407 np = oh->od->pdev->dev.of_node;
408 if (!np) {
409 error = -ENODEV;
410 goto error;
411 }
412
413 res = kcalloc(2, sizeof(*res), GFP_KERNEL);
414 if (!res)
415 return -ENOMEM;
416
417 /* Do we have a dts range for the interconnect target module? */
418 error = omap_hwmod_parse_module_range(oh, np, res);
419
420 /* No ranges, rely on device reg entry */
421 if (error)
422 error = of_address_to_resource(np, 0, res);
423 if (error)
424 goto free;
425
426 /* SmartReflex needs first IO resource name to be "mpu" */
427 res[0].name = "mpu";
428
429 /*
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.
433 */
434 of_property_for_each_string(np, "compatible", prop, name)
435 if (!strncmp("ti,sysc-", name, 8))
436 break;
437
438 child = of_get_next_available_child(np, NULL);
439
440 if (name)
441 irq = irq_of_parse_and_map(child, 0);
442 if (!irq)
443 irq = irq_of_parse_and_map(np, 0);
444 if (!irq) {
445 error = -EINVAL;
446 goto free;
447 }
448
449 /* Legacy DMA code needs interrupt name to be "0" */
450 res[1].start = irq;
451 res[1].end = irq;
452 res[1].flags = IORESOURCE_IRQ;
453 res[1].name = "0";
454
455 error = platform_device_add_resources(pdev, res, 2);
456
457 free:
458 kfree(res);
459
460 error:
461 WARN(error, "%s: %s device %s failed: %i\n",
462 __func__, oh->name, dev_name(&pdev->dev),
463 error);
464
465 return error;
466 }
467
468 /**
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
475 *
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().
481 */
omap_device_build(const char * pdev_name,int pdev_id,struct omap_hwmod * oh,void * pdata,int pdata_len)482 struct platform_device __init *omap_device_build(const char *pdev_name,
483 int pdev_id,
484 struct omap_hwmod *oh,
485 void *pdata, int pdata_len)
486 {
487 int ret = -ENOMEM;
488 struct platform_device *pdev;
489 struct omap_device *od;
490
491 if (!oh || !pdev_name)
492 return ERR_PTR(-EINVAL);
493
494 if (!pdata && pdata_len > 0)
495 return ERR_PTR(-EINVAL);
496
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);
501 ret = -ENODEV;
502 goto odbs_exit;
503 }
504
505 pdev = platform_device_alloc(pdev_name, pdev_id);
506 if (!pdev) {
507 ret = -ENOMEM;
508 goto odbs_exit;
509 }
510
511 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
512 if (pdev->id != -1)
513 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
514 else
515 dev_set_name(&pdev->dev, "%s", pdev->name);
516
517 /*
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().
521 */
522 ret = omap_device_copy_resources(oh, pdev);
523 if (ret)
524 goto odbs_exit1;
525
526 od = omap_device_alloc(pdev, &oh, 1);
527 if (IS_ERR(od)) {
528 ret = PTR_ERR(od);
529 goto odbs_exit1;
530 }
531
532 ret = platform_device_add_data(pdev, pdata, pdata_len);
533 if (ret)
534 goto odbs_exit2;
535
536 ret = omap_device_register(pdev);
537 if (ret)
538 goto odbs_exit2;
539
540 return pdev;
541
542 odbs_exit2:
543 omap_device_delete(od);
544 odbs_exit1:
545 platform_device_put(pdev);
546 odbs_exit:
547
548 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
549
550 return ERR_PTR(ret);
551 }
552
553 #ifdef CONFIG_PM
_od_runtime_suspend(struct device * dev)554 static int _od_runtime_suspend(struct device *dev)
555 {
556 struct platform_device *pdev = to_platform_device(dev);
557 int ret;
558
559 ret = pm_generic_runtime_suspend(dev);
560 if (ret)
561 return ret;
562
563 return omap_device_idle(pdev);
564 }
565
_od_runtime_resume(struct device * dev)566 static int _od_runtime_resume(struct device *dev)
567 {
568 struct platform_device *pdev = to_platform_device(dev);
569 int ret;
570
571 ret = omap_device_enable(pdev);
572 if (ret) {
573 dev_err(dev, "use pm_runtime_put_sync_suspend() in driver?\n");
574 return ret;
575 }
576
577 return pm_generic_runtime_resume(dev);
578 }
579
_od_fail_runtime_suspend(struct device * dev)580 static int _od_fail_runtime_suspend(struct device *dev)
581 {
582 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
583 return -ENODEV;
584 }
585
_od_fail_runtime_resume(struct device * dev)586 static int _od_fail_runtime_resume(struct device *dev)
587 {
588 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
589 return -ENODEV;
590 }
591
592 #endif
593
594 #ifdef CONFIG_SUSPEND
_od_suspend_noirq(struct device * dev)595 static int _od_suspend_noirq(struct device *dev)
596 {
597 struct platform_device *pdev = to_platform_device(dev);
598 struct omap_device *od = to_omap_device(pdev);
599 int ret;
600
601 /* Don't attempt late suspend on a driver that is not bound */
602 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
603 return 0;
604
605 ret = pm_generic_suspend_noirq(dev);
606
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;
611 }
612 }
613
614 return ret;
615 }
616
_od_resume_noirq(struct device * dev)617 static int _od_resume_noirq(struct device *dev)
618 {
619 struct platform_device *pdev = to_platform_device(dev);
620 struct omap_device *od = to_omap_device(pdev);
621
622 if (od->flags & OMAP_DEVICE_SUSPENDED) {
623 od->flags &= ~OMAP_DEVICE_SUSPENDED;
624 omap_device_enable(pdev);
625 pm_generic_runtime_resume(dev);
626 }
627
628 return pm_generic_resume_noirq(dev);
629 }
630 #else
631 #define _od_suspend_noirq NULL
632 #define _od_resume_noirq NULL
633 #endif
634
635 struct dev_pm_domain omap_device_fail_pm_domain = {
636 .ops = {
637 SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
638 _od_fail_runtime_resume, NULL)
639 }
640 };
641
642 struct dev_pm_domain omap_device_pm_domain = {
643 .ops = {
644 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
645 NULL)
646 USE_PLATFORM_PM_SLEEP_OPS
647 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
648 _od_resume_noirq)
649 }
650 };
651
652 /**
653 * omap_device_register - register an omap_device with one omap_hwmod
654 * @od: struct omap_device * to register
655 *
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().
659 */
omap_device_register(struct platform_device * pdev)660 int omap_device_register(struct platform_device *pdev)
661 {
662 pr_debug("omap_device: %s: registering\n", pdev->name);
663
664 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
665 return platform_device_add(pdev);
666 }
667
668
669 /* Public functions for use by device drivers through struct platform_data */
670
671 /**
672 * omap_device_enable - fully activate an omap_device
673 * @od: struct omap_device * to activate
674 *
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().
682 */
omap_device_enable(struct platform_device * pdev)683 int omap_device_enable(struct platform_device *pdev)
684 {
685 int ret;
686 struct omap_device *od;
687
688 od = to_omap_device(pdev);
689
690 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
691 dev_warn(&pdev->dev,
692 "omap_device: %s() called from invalid state %d\n",
693 __func__, od->_state);
694 return -EINVAL;
695 }
696
697 ret = _omap_device_enable_hwmods(od);
698
699 if (ret == 0)
700 od->_state = OMAP_DEVICE_STATE_ENABLED;
701
702 return ret;
703 }
704
705 /**
706 * omap_device_idle - idle an omap_device
707 * @od: struct omap_device * to idle
708 *
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().
713 */
omap_device_idle(struct platform_device * pdev)714 int omap_device_idle(struct platform_device *pdev)
715 {
716 int ret;
717 struct omap_device *od;
718
719 od = to_omap_device(pdev);
720
721 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
722 dev_warn(&pdev->dev,
723 "omap_device: %s() called from invalid state %d\n",
724 __func__, od->_state);
725 return -EINVAL;
726 }
727
728 ret = _omap_device_idle_hwmods(od);
729
730 if (ret == 0)
731 od->_state = OMAP_DEVICE_STATE_IDLE;
732
733 return ret;
734 }
735
736 /**
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
740 *
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.
747 */
omap_device_assert_hardreset(struct platform_device * pdev,const char * name)748 int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
749 {
750 struct omap_device *od = to_omap_device(pdev);
751 int ret = 0;
752 int i;
753
754 for (i = 0; i < od->hwmods_cnt; i++) {
755 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
756 if (ret)
757 break;
758 }
759
760 return ret;
761 }
762
763 /**
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
767 *
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.
774 */
omap_device_deassert_hardreset(struct platform_device * pdev,const char * name)775 int omap_device_deassert_hardreset(struct platform_device *pdev,
776 const char *name)
777 {
778 struct omap_device *od = to_omap_device(pdev);
779 int ret = 0;
780 int i;
781
782 for (i = 0; i < od->hwmods_cnt; i++) {
783 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
784 if (ret)
785 break;
786 }
787
788 return ret;
789 }
790
791 /**
792 * omap_device_get_by_hwmod_name() - convert a hwmod name to
793 * device pointer.
794 * @oh_name: name of the hwmod device
795 *
796 * Returns back a struct device * pointer associated with a hwmod
797 * device represented by a hwmod_name
798 */
omap_device_get_by_hwmod_name(const char * oh_name)799 struct device *omap_device_get_by_hwmod_name(const char *oh_name)
800 {
801 struct omap_hwmod *oh;
802
803 if (!oh_name) {
804 WARN(1, "%s: no hwmod name!\n", __func__);
805 return ERR_PTR(-EINVAL);
806 }
807
808 oh = omap_hwmod_lookup(oh_name);
809 if (!oh) {
810 WARN(1, "%s: no hwmod for %s\n", __func__,
811 oh_name);
812 return ERR_PTR(-ENODEV);
813 }
814 if (!oh->od) {
815 WARN(1, "%s: no omap_device for %s\n", __func__,
816 oh_name);
817 return ERR_PTR(-ENODEV);
818 }
819
820 return &oh->od->pdev->dev;
821 }
822
823 static struct notifier_block platform_nb = {
824 .notifier_call = _omap_device_notifier_call,
825 };
826
omap_device_init(void)827 static int __init omap_device_init(void)
828 {
829 bus_register_notifier(&platform_bus_type, &platform_nb);
830 return 0;
831 }
832 omap_postcore_initcall(omap_device_init);
833
834 /**
835 * omap_device_late_idle - idle devices without drivers
836 * @dev: struct device * associated with omap_device
837 * @data: unused
838 *
839 * Check the driver bound status of this device, and idle it
840 * if there is no driver attached.
841 */
omap_device_late_idle(struct device * dev,void * data)842 static int __init omap_device_late_idle(struct device *dev, void *data)
843 {
844 struct platform_device *pdev = to_platform_device(dev);
845 struct omap_device *od = to_omap_device(pdev);
846 int i;
847
848 if (!od)
849 return 0;
850
851 /*
852 * If omap_device state is enabled, but has no driver bound,
853 * idle it.
854 */
855
856 /*
857 * Some devices (like memory controllers) are always kept
858 * enabled, and should not be idled even with no drivers.
859 */
860 for (i = 0; i < od->hwmods_cnt; i++)
861 if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
862 return 0;
863
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",
868 __func__);
869 omap_device_idle(pdev);
870 }
871 }
872
873 return 0;
874 }
875
omap_device_late_init(void)876 static int __init omap_device_late_init(void)
877 {
878 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
879
880 return 0;
881 }
882 omap_late_initcall_sync(omap_device_late_init);
883