1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#undef DEBUG
27
28#include <linux/kernel.h>
29#include <linux/platform_device.h>
30#include <linux/slab.h>
31#include <linux/err.h>
32#include <linux/io.h>
33#include <linux/clk.h>
34#include <linux/clkdev.h>
35#include <linux/pm_runtime.h>
36#include <linux/of.h>
37#include <linux/notifier.h>
38
39#include "common.h"
40#include "soc.h"
41#include "omap_device.h"
42#include "omap_hwmod.h"
43
44
45
46static void _add_clkdev(struct omap_device *od, const char *clk_alias,
47 const char *clk_name)
48{
49 struct clk *r;
50 int rc;
51
52 if (!clk_alias || !clk_name)
53 return;
54
55 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
56
57 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
58 if (!IS_ERR(r)) {
59 dev_dbg(&od->pdev->dev,
60 "alias %s already exists\n", clk_alias);
61 clk_put(r);
62 return;
63 }
64
65 rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev), clk_name, NULL);
66 if (rc) {
67 if (rc == -ENODEV || rc == -ENOMEM)
68 dev_err(&od->pdev->dev,
69 "clkdev_alloc for %s failed\n", clk_alias);
70 else
71 dev_err(&od->pdev->dev,
72 "clk_get for %s failed\n", clk_name);
73 }
74}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95static void _add_hwmod_clocks_clkdev(struct omap_device *od,
96 struct omap_hwmod *oh)
97{
98 int i;
99
100 _add_clkdev(od, "fck", oh->main_clk);
101
102 for (i = 0; i < oh->opt_clks_cnt; i++)
103 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
104}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119static int omap_device_build_from_dt(struct platform_device *pdev)
120{
121 struct omap_hwmod **hwmods;
122 struct omap_device *od;
123 struct omap_hwmod *oh;
124 struct device_node *node = pdev->dev.of_node;
125 const char *oh_name;
126 int oh_cnt, i, ret = 0;
127 bool device_active = false;
128
129 oh_cnt = of_property_count_strings(node, "ti,hwmods");
130 if (oh_cnt <= 0) {
131 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
132 return -ENODEV;
133 }
134
135 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
136 if (!hwmods) {
137 ret = -ENOMEM;
138 goto odbfd_exit;
139 }
140
141 for (i = 0; i < oh_cnt; i++) {
142 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
143 oh = omap_hwmod_lookup(oh_name);
144 if (!oh) {
145 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
146 oh_name);
147 ret = -EINVAL;
148 goto odbfd_exit1;
149 }
150 hwmods[i] = oh;
151 if (oh->flags & HWMOD_INIT_NO_IDLE)
152 device_active = true;
153 }
154
155 od = omap_device_alloc(pdev, hwmods, oh_cnt);
156 if (IS_ERR(od)) {
157 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
158 oh_name);
159 ret = PTR_ERR(od);
160 goto odbfd_exit1;
161 }
162
163
164 for (i = 0; i < pdev->num_resources; i++) {
165 struct resource *r = &pdev->resource[i];
166
167 if (r->name == NULL)
168 r->name = dev_name(&pdev->dev);
169 }
170
171 pdev->dev.pm_domain = &omap_device_pm_domain;
172
173 if (device_active) {
174 omap_device_enable(pdev);
175 pm_runtime_set_active(&pdev->dev);
176 }
177
178odbfd_exit1:
179 kfree(hwmods);
180odbfd_exit:
181
182 if (ret)
183 pdev->dev.pm_domain = &omap_device_fail_pm_domain;
184
185 return ret;
186}
187
188static int _omap_device_notifier_call(struct notifier_block *nb,
189 unsigned long event, void *dev)
190{
191 struct platform_device *pdev = to_platform_device(dev);
192 struct omap_device *od;
193
194 switch (event) {
195 case BUS_NOTIFY_DEL_DEVICE:
196 if (pdev->archdata.od)
197 omap_device_delete(pdev->archdata.od);
198 break;
199 case BUS_NOTIFY_ADD_DEVICE:
200 if (pdev->dev.of_node)
201 omap_device_build_from_dt(pdev);
202 omap_auxdata_legacy_init(dev);
203
204 default:
205 od = to_omap_device(pdev);
206 if (od)
207 od->_driver_status = event;
208 }
209
210 return NOTIFY_DONE;
211}
212
213
214
215
216
217
218
219static int _omap_device_enable_hwmods(struct omap_device *od)
220{
221 int ret = 0;
222 int i;
223
224 for (i = 0; i < od->hwmods_cnt; i++)
225 ret |= omap_hwmod_enable(od->hwmods[i]);
226
227 return ret;
228}
229
230
231
232
233
234
235
236static int _omap_device_idle_hwmods(struct omap_device *od)
237{
238 int ret = 0;
239 int i;
240
241 for (i = 0; i < od->hwmods_cnt; i++)
242 ret |= omap_hwmod_idle(od->hwmods[i]);
243
244 return ret;
245}
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264int omap_device_get_context_loss_count(struct platform_device *pdev)
265{
266 struct omap_device *od;
267 u32 ret = 0;
268
269 od = to_omap_device(pdev);
270
271 if (od->hwmods_cnt)
272 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
273
274 return ret;
275}
276
277
278
279
280
281
282
283
284
285
286
287static int omap_device_count_resources(struct omap_device *od,
288 unsigned long flags)
289{
290 int c = 0;
291 int i;
292
293 for (i = 0; i < od->hwmods_cnt; i++)
294 c += omap_hwmod_count_resources(od->hwmods[i], flags);
295
296 pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
297 od->pdev->name, c, od->hwmods_cnt);
298
299 return c;
300}
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319static int omap_device_fill_resources(struct omap_device *od,
320 struct resource *res)
321{
322 int i, r;
323
324 for (i = 0; i < od->hwmods_cnt; i++) {
325 r = omap_hwmod_fill_resources(od->hwmods[i], res);
326 res += r;
327 }
328
329 return 0;
330}
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346static int _od_fill_dma_resources(struct omap_device *od,
347 struct resource *res)
348{
349 int i, r;
350
351 for (i = 0; i < od->hwmods_cnt; i++) {
352 r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
353 res += r;
354 }
355
356 return 0;
357}
358
359
360
361
362
363
364
365
366
367
368
369
370
371struct omap_device *omap_device_alloc(struct platform_device *pdev,
372 struct omap_hwmod **ohs, int oh_cnt)
373{
374 int ret = -ENOMEM;
375 struct omap_device *od;
376 struct resource *res = NULL;
377 int i, res_count;
378 struct omap_hwmod **hwmods;
379
380 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
381 if (!od) {
382 ret = -ENOMEM;
383 goto oda_exit1;
384 }
385 od->hwmods_cnt = oh_cnt;
386
387 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
388 if (!hwmods)
389 goto oda_exit2;
390
391 od->hwmods = hwmods;
392 od->pdev = pdev;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 if (!pdev->num_resources) {
414
415 res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
416 IORESOURCE_DMA |
417 IORESOURCE_MEM);
418 } else {
419
420 for (i = 0; i < pdev->num_resources; i++) {
421 struct resource *r = &pdev->resource[i];
422
423
424 if (r->flags == IORESOURCE_DMA)
425 goto have_everything;
426 }
427
428 res_count = omap_device_count_resources(od, IORESOURCE_DMA);
429
430 if (!res_count)
431 goto have_everything;
432
433 res_count += pdev->num_resources;
434 }
435
436
437 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
438 if (!res)
439 goto oda_exit3;
440
441 if (!pdev->num_resources) {
442 dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
443 __func__, res_count);
444 omap_device_fill_resources(od, res);
445 } else {
446 dev_dbg(&pdev->dev,
447 "%s: appending %d DMA resources from hwmod\n",
448 __func__, res_count - pdev->num_resources);
449 memcpy(res, pdev->resource,
450 sizeof(struct resource) * pdev->num_resources);
451 _od_fill_dma_resources(od, &res[pdev->num_resources]);
452 }
453
454 ret = platform_device_add_resources(pdev, res, res_count);
455 kfree(res);
456
457 if (ret)
458 goto oda_exit3;
459
460have_everything:
461 pdev->archdata.od = od;
462
463 for (i = 0; i < oh_cnt; i++) {
464 hwmods[i]->od = od;
465 _add_hwmod_clocks_clkdev(od, hwmods[i]);
466 }
467
468 return od;
469
470oda_exit3:
471 kfree(hwmods);
472oda_exit2:
473 kfree(od);
474oda_exit1:
475 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
476
477 return ERR_PTR(ret);
478}
479
480void omap_device_delete(struct omap_device *od)
481{
482 if (!od)
483 return;
484
485 od->pdev->archdata.od = NULL;
486 kfree(od->hwmods);
487 kfree(od);
488}
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504struct platform_device __init *omap_device_build(const char *pdev_name,
505 int pdev_id,
506 struct omap_hwmod *oh,
507 void *pdata, int pdata_len)
508{
509 struct omap_hwmod *ohs[] = { oh };
510
511 if (!oh)
512 return ERR_PTR(-EINVAL);
513
514 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
515 pdata_len);
516}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532struct platform_device __init *omap_device_build_ss(const char *pdev_name,
533 int pdev_id,
534 struct omap_hwmod **ohs,
535 int oh_cnt, void *pdata,
536 int pdata_len)
537{
538 int ret = -ENOMEM;
539 struct platform_device *pdev;
540 struct omap_device *od;
541
542 if (!ohs || oh_cnt == 0 || !pdev_name)
543 return ERR_PTR(-EINVAL);
544
545 if (!pdata && pdata_len > 0)
546 return ERR_PTR(-EINVAL);
547
548 pdev = platform_device_alloc(pdev_name, pdev_id);
549 if (!pdev) {
550 ret = -ENOMEM;
551 goto odbs_exit;
552 }
553
554
555 if (pdev->id != -1)
556 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
557 else
558 dev_set_name(&pdev->dev, "%s", pdev->name);
559
560 od = omap_device_alloc(pdev, ohs, oh_cnt);
561 if (IS_ERR(od))
562 goto odbs_exit1;
563
564 ret = platform_device_add_data(pdev, pdata, pdata_len);
565 if (ret)
566 goto odbs_exit2;
567
568 ret = omap_device_register(pdev);
569 if (ret)
570 goto odbs_exit2;
571
572 return pdev;
573
574odbs_exit2:
575 omap_device_delete(od);
576odbs_exit1:
577 platform_device_put(pdev);
578odbs_exit:
579
580 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
581
582 return ERR_PTR(ret);
583}
584
585#ifdef CONFIG_PM
586static int _od_runtime_suspend(struct device *dev)
587{
588 struct platform_device *pdev = to_platform_device(dev);
589 int ret;
590
591 ret = pm_generic_runtime_suspend(dev);
592 if (ret)
593 return ret;
594
595 return omap_device_idle(pdev);
596}
597
598static int _od_runtime_resume(struct device *dev)
599{
600 struct platform_device *pdev = to_platform_device(dev);
601 int ret;
602
603 ret = omap_device_enable(pdev);
604 if (ret)
605 return ret;
606
607 return pm_generic_runtime_resume(dev);
608}
609
610static int _od_fail_runtime_suspend(struct device *dev)
611{
612 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
613 return -ENODEV;
614}
615
616static int _od_fail_runtime_resume(struct device *dev)
617{
618 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
619 return -ENODEV;
620}
621
622#endif
623
624#ifdef CONFIG_SUSPEND
625static int _od_suspend_noirq(struct device *dev)
626{
627 struct platform_device *pdev = to_platform_device(dev);
628 struct omap_device *od = to_omap_device(pdev);
629 int ret;
630
631
632 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
633 return 0;
634
635 ret = pm_generic_suspend_noirq(dev);
636
637 if (!ret && !pm_runtime_status_suspended(dev)) {
638 if (pm_generic_runtime_suspend(dev) == 0) {
639 pm_runtime_set_suspended(dev);
640 omap_device_idle(pdev);
641 od->flags |= OMAP_DEVICE_SUSPENDED;
642 }
643 }
644
645 return ret;
646}
647
648static int _od_resume_noirq(struct device *dev)
649{
650 struct platform_device *pdev = to_platform_device(dev);
651 struct omap_device *od = to_omap_device(pdev);
652
653 if (od->flags & OMAP_DEVICE_SUSPENDED) {
654 od->flags &= ~OMAP_DEVICE_SUSPENDED;
655 omap_device_enable(pdev);
656
657
658
659
660
661
662
663 WARN(pm_runtime_set_active(dev),
664 "Could not set %s runtime state active\n", dev_name(dev));
665 pm_generic_runtime_resume(dev);
666 }
667
668 return pm_generic_resume_noirq(dev);
669}
670#else
671#define _od_suspend_noirq NULL
672#define _od_resume_noirq NULL
673#endif
674
675struct dev_pm_domain omap_device_fail_pm_domain = {
676 .ops = {
677 SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
678 _od_fail_runtime_resume, NULL)
679 }
680};
681
682struct dev_pm_domain omap_device_pm_domain = {
683 .ops = {
684 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
685 NULL)
686 USE_PLATFORM_PM_SLEEP_OPS
687 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
688 _od_resume_noirq)
689 }
690};
691
692
693
694
695
696
697
698
699
700int omap_device_register(struct platform_device *pdev)
701{
702 pr_debug("omap_device: %s: registering\n", pdev->name);
703
704 pdev->dev.pm_domain = &omap_device_pm_domain;
705 return platform_device_add(pdev);
706}
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723int omap_device_enable(struct platform_device *pdev)
724{
725 int ret;
726 struct omap_device *od;
727
728 od = to_omap_device(pdev);
729
730 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
731 dev_warn(&pdev->dev,
732 "omap_device: %s() called from invalid state %d\n",
733 __func__, od->_state);
734 return -EINVAL;
735 }
736
737 ret = _omap_device_enable_hwmods(od);
738
739 if (ret == 0)
740 od->_state = OMAP_DEVICE_STATE_ENABLED;
741
742 return ret;
743}
744
745
746
747
748
749
750
751
752
753
754int omap_device_idle(struct platform_device *pdev)
755{
756 int ret;
757 struct omap_device *od;
758
759 od = to_omap_device(pdev);
760
761 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
762 dev_warn(&pdev->dev,
763 "omap_device: %s() called from invalid state %d\n",
764 __func__, od->_state);
765 return -EINVAL;
766 }
767
768 ret = _omap_device_idle_hwmods(od);
769
770 if (ret == 0)
771 od->_state = OMAP_DEVICE_STATE_IDLE;
772
773 return ret;
774}
775
776
777
778
779
780
781
782
783
784
785
786
787
788int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
789{
790 struct omap_device *od = to_omap_device(pdev);
791 int ret = 0;
792 int i;
793
794 for (i = 0; i < od->hwmods_cnt; i++) {
795 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
796 if (ret)
797 break;
798 }
799
800 return ret;
801}
802
803
804
805
806
807
808
809
810
811
812
813
814
815int omap_device_deassert_hardreset(struct platform_device *pdev,
816 const char *name)
817{
818 struct omap_device *od = to_omap_device(pdev);
819 int ret = 0;
820 int i;
821
822 for (i = 0; i < od->hwmods_cnt; i++) {
823 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
824 if (ret)
825 break;
826 }
827
828 return ret;
829}
830
831
832
833
834
835
836
837
838
839struct device *omap_device_get_by_hwmod_name(const char *oh_name)
840{
841 struct omap_hwmod *oh;
842
843 if (!oh_name) {
844 WARN(1, "%s: no hwmod name!\n", __func__);
845 return ERR_PTR(-EINVAL);
846 }
847
848 oh = omap_hwmod_lookup(oh_name);
849 if (!oh) {
850 WARN(1, "%s: no hwmod for %s\n", __func__,
851 oh_name);
852 return ERR_PTR(-ENODEV);
853 }
854 if (!oh->od) {
855 WARN(1, "%s: no omap_device for %s\n", __func__,
856 oh_name);
857 return ERR_PTR(-ENODEV);
858 }
859
860 return &oh->od->pdev->dev;
861}
862
863static struct notifier_block platform_nb = {
864 .notifier_call = _omap_device_notifier_call,
865};
866
867static int __init omap_device_init(void)
868{
869 bus_register_notifier(&platform_bus_type, &platform_nb);
870 return 0;
871}
872omap_core_initcall(omap_device_init);
873
874
875
876
877
878
879
880
881
882static int __init omap_device_late_idle(struct device *dev, void *data)
883{
884 struct platform_device *pdev = to_platform_device(dev);
885 struct omap_device *od = to_omap_device(pdev);
886 int i;
887
888 if (!od)
889 return 0;
890
891
892
893
894
895
896
897
898
899
900 for (i = 0; i < od->hwmods_cnt; i++)
901 if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
902 return 0;
903
904 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
905 od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
906 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
907 dev_warn(dev, "%s: enabled but no driver. Idling\n",
908 __func__);
909 omap_device_idle(pdev);
910 }
911 }
912
913 return 0;
914}
915
916static int __init omap_device_late_init(void)
917{
918 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
919
920 WARN(!of_have_populated_dt(),
921 "legacy booting deprecated, please update to boot with .dts\n");
922
923 return 0;
924}
925omap_late_initcall_sync(omap_device_late_init);
926