]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm/plat-omap/omap_device.c
Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[karo-tx-linux.git] / arch / arm / plat-omap / omap_device.c
1
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 program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  *
17  * This code provides a consistent interface for OMAP device drivers
18  * to control power management and interconnect properties of their
19  * devices.
20  *
21  * In the medium- to long-term, this code should either be
22  * a) implemented via arch-specific pointers in platform_data
23  * or
24  * b) implemented as a proper omap_bus/omap_device in Linux, no more
25  *    platform_data func pointers
26  *
27  *
28  * Guidelines for usage by driver authors:
29  *
30  * 1. These functions are intended to be used by device drivers via
31  * function pointers in struct platform_data.  As an example,
32  * omap_device_enable() should be passed to the driver as
33  *
34  * struct foo_driver_platform_data {
35  * ...
36  *      int (*device_enable)(struct platform_device *pdev);
37  * ...
38  * }
39  *
40  * Note that the generic "device_enable" name is used, rather than
41  * "omap_device_enable".  This is so other architectures can pass in their
42  * own enable/disable functions here.
43  *
44  * This should be populated during device setup:
45  *
46  * ...
47  * pdata->device_enable = omap_device_enable;
48  * ...
49  *
50  * 2. Drivers should first check to ensure the function pointer is not null
51  * before calling it, as in:
52  *
53  * if (pdata->device_enable)
54  *     pdata->device_enable(pdev);
55  *
56  * This allows other architectures that don't use similar device_enable()/
57  * device_shutdown() functions to execute normally.
58  *
59  * ...
60  *
61  * Suggested usage by device drivers:
62  *
63  * During device initialization:
64  * device_enable()
65  *
66  * During device idle:
67  * (save remaining device context if necessary)
68  * device_idle();
69  *
70  * During device resume:
71  * device_enable();
72  * (restore context if necessary)
73  *
74  * During device shutdown:
75  * device_shutdown()
76  * (device must be reinitialized at this point to use it again)
77  *
78  */
79 #undef DEBUG
80
81 #include <linux/kernel.h>
82 #include <linux/export.h>
83 #include <linux/platform_device.h>
84 #include <linux/slab.h>
85 #include <linux/err.h>
86 #include <linux/io.h>
87 #include <linux/clk.h>
88 #include <linux/clkdev.h>
89 #include <linux/pm_runtime.h>
90 #include <linux/of.h>
91 #include <linux/notifier.h>
92
93 #include <plat/omap_device.h>
94 #include <plat/omap_hwmod.h>
95 #include <plat/clock.h>
96
97 /* These parameters are passed to _omap_device_{de,}activate() */
98 #define USE_WAKEUP_LAT                  0
99 #define IGNORE_WAKEUP_LAT               1
100
101 static int omap_early_device_register(struct platform_device *pdev);
102
103 static struct omap_device_pm_latency omap_default_latency[] = {
104         {
105                 .deactivate_func = omap_device_idle_hwmods,
106                 .activate_func   = omap_device_enable_hwmods,
107                 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
108         }
109 };
110
111 /* Private functions */
112
113 /**
114  * _omap_device_activate - increase device readiness
115  * @od: struct omap_device *
116  * @ignore_lat: increase to latency target (0) or full readiness (1)?
117  *
118  * Increase readiness of omap_device @od (thus decreasing device
119  * wakeup latency, but consuming more power).  If @ignore_lat is
120  * IGNORE_WAKEUP_LAT, make the omap_device fully active.  Otherwise,
121  * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
122  * latency is greater than the requested maximum wakeup latency, step
123  * backwards in the omap_device_pm_latency table to ensure the
124  * device's maximum wakeup latency is less than or equal to the
125  * requested maximum wakeup latency.  Returns 0.
126  */
127 static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
128 {
129         struct timespec a, b, c;
130
131         dev_dbg(&od->pdev->dev, "omap_device: activating\n");
132
133         while (od->pm_lat_level > 0) {
134                 struct omap_device_pm_latency *odpl;
135                 unsigned long long act_lat = 0;
136
137                 od->pm_lat_level--;
138
139                 odpl = od->pm_lats + od->pm_lat_level;
140
141                 if (!ignore_lat &&
142                     (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
143                         break;
144
145                 read_persistent_clock(&a);
146
147                 /* XXX check return code */
148                 odpl->activate_func(od);
149
150                 read_persistent_clock(&b);
151
152                 c = timespec_sub(b, a);
153                 act_lat = timespec_to_ns(&c);
154
155                 dev_dbg(&od->pdev->dev,
156                         "omap_device: pm_lat %d: activate: elapsed time "
157                         "%llu nsec\n", od->pm_lat_level, act_lat);
158
159                 if (act_lat > odpl->activate_lat) {
160                         odpl->activate_lat_worst = act_lat;
161                         if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
162                                 odpl->activate_lat = act_lat;
163                                 dev_dbg(&od->pdev->dev,
164                                         "new worst case activate latency "
165                                         "%d: %llu\n",
166                                         od->pm_lat_level, act_lat);
167                         } else
168                                 dev_warn(&od->pdev->dev,
169                                          "activate latency %d "
170                                          "higher than exptected. (%llu > %d)\n",
171                                          od->pm_lat_level, act_lat,
172                                          odpl->activate_lat);
173                 }
174
175                 od->dev_wakeup_lat -= odpl->activate_lat;
176         }
177
178         return 0;
179 }
180
181 /**
182  * _omap_device_deactivate - decrease device readiness
183  * @od: struct omap_device *
184  * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
185  *
186  * Decrease readiness of omap_device @od (thus increasing device
187  * wakeup latency, but conserving power).  If @ignore_lat is
188  * IGNORE_WAKEUP_LAT, make the omap_device fully inactive.  Otherwise,
189  * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
190  * latency is less than the requested maximum wakeup latency, step
191  * forwards in the omap_device_pm_latency table to ensure the device's
192  * maximum wakeup latency is less than or equal to the requested
193  * maximum wakeup latency.  Returns 0.
194  */
195 static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
196 {
197         struct timespec a, b, c;
198
199         dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
200
201         while (od->pm_lat_level < od->pm_lats_cnt) {
202                 struct omap_device_pm_latency *odpl;
203                 unsigned long long deact_lat = 0;
204
205                 odpl = od->pm_lats + od->pm_lat_level;
206
207                 if (!ignore_lat &&
208                     ((od->dev_wakeup_lat + odpl->activate_lat) >
209                      od->_dev_wakeup_lat_limit))
210                         break;
211
212                 read_persistent_clock(&a);
213
214                 /* XXX check return code */
215                 odpl->deactivate_func(od);
216
217                 read_persistent_clock(&b);
218
219                 c = timespec_sub(b, a);
220                 deact_lat = timespec_to_ns(&c);
221
222                 dev_dbg(&od->pdev->dev,
223                         "omap_device: pm_lat %d: deactivate: elapsed time "
224                         "%llu nsec\n", od->pm_lat_level, deact_lat);
225
226                 if (deact_lat > odpl->deactivate_lat) {
227                         odpl->deactivate_lat_worst = deact_lat;
228                         if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
229                                 odpl->deactivate_lat = deact_lat;
230                                 dev_dbg(&od->pdev->dev,
231                                         "new worst case deactivate latency "
232                                         "%d: %llu\n",
233                                         od->pm_lat_level, deact_lat);
234                         } else
235                                 dev_warn(&od->pdev->dev,
236                                          "deactivate latency %d "
237                                          "higher than exptected. (%llu > %d)\n",
238                                          od->pm_lat_level, deact_lat,
239                                          odpl->deactivate_lat);
240                 }
241
242                 od->dev_wakeup_lat += odpl->activate_lat;
243
244                 od->pm_lat_level++;
245         }
246
247         return 0;
248 }
249
250 static void _add_clkdev(struct omap_device *od, const char *clk_alias,
251                        const char *clk_name)
252 {
253         struct clk *r;
254         struct clk_lookup *l;
255
256         if (!clk_alias || !clk_name)
257                 return;
258
259         dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
260
261         r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
262         if (!IS_ERR(r)) {
263                 dev_warn(&od->pdev->dev,
264                          "alias %s already exists\n", clk_alias);
265                 clk_put(r);
266                 return;
267         }
268
269         r = omap_clk_get_by_name(clk_name);
270         if (IS_ERR(r)) {
271                 dev_err(&od->pdev->dev,
272                         "omap_clk_get_by_name for %s failed\n", clk_name);
273                 return;
274         }
275
276         l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
277         if (!l) {
278                 dev_err(&od->pdev->dev,
279                         "clkdev_alloc for %s failed\n", clk_alias);
280                 return;
281         }
282
283         clkdev_add(l);
284 }
285
286 /**
287  * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
288  * and main clock
289  * @od: struct omap_device *od
290  * @oh: struct omap_hwmod *oh
291  *
292  * For the main clock and every optional clock present per hwmod per
293  * omap_device, this function adds an entry in the clkdev table of the
294  * form <dev-id=dev_name, con-id=role> if it does not exist already.
295  *
296  * The function is called from inside omap_device_build_ss(), after
297  * omap_device_register.
298  *
299  * This allows drivers to get a pointer to its optional clocks based on its role
300  * by calling clk_get(<dev*>, <role>).
301  * In the case of the main clock, a "fck" alias is used.
302  *
303  * No return value.
304  */
305 static void _add_hwmod_clocks_clkdev(struct omap_device *od,
306                                      struct omap_hwmod *oh)
307 {
308         int i;
309
310         _add_clkdev(od, "fck", oh->main_clk);
311
312         for (i = 0; i < oh->opt_clks_cnt; i++)
313                 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
314 }
315
316
317 /**
318  * omap_device_build_from_dt - build an omap_device with multiple hwmods
319  * @pdev_name: name of the platform_device driver to use
320  * @pdev_id: this platform_device's connection ID
321  * @oh: ptr to the single omap_hwmod that backs this omap_device
322  * @pdata: platform_data ptr to associate with the platform_device
323  * @pdata_len: amount of memory pointed to by @pdata
324  * @pm_lats: pointer to a omap_device_pm_latency array for this device
325  * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
326  * @is_early_device: should the device be registered as an early device or not
327  *
328  * Function for building an omap_device already registered from device-tree
329  *
330  * Returns 0 or PTR_ERR() on error.
331  */
332 static int omap_device_build_from_dt(struct platform_device *pdev)
333 {
334         struct omap_hwmod **hwmods;
335         struct omap_device *od;
336         struct omap_hwmod *oh;
337         struct device_node *node = pdev->dev.of_node;
338         const char *oh_name;
339         int oh_cnt, i, ret = 0;
340
341         oh_cnt = of_property_count_strings(node, "ti,hwmods");
342         if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
343                 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
344                 return -ENODEV;
345         }
346
347         hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
348         if (!hwmods) {
349                 ret = -ENOMEM;
350                 goto odbfd_exit;
351         }
352
353         for (i = 0; i < oh_cnt; i++) {
354                 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
355                 oh = omap_hwmod_lookup(oh_name);
356                 if (!oh) {
357                         dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
358                                 oh_name);
359                         ret = -EINVAL;
360                         goto odbfd_exit1;
361                 }
362                 hwmods[i] = oh;
363         }
364
365         od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
366         if (!od) {
367                 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
368                         oh_name);
369                 ret = PTR_ERR(od);
370                 goto odbfd_exit1;
371         }
372
373         if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
374                 omap_device_disable_idle_on_suspend(pdev);
375
376         pdev->dev.pm_domain = &omap_device_pm_domain;
377
378 odbfd_exit1:
379         kfree(hwmods);
380 odbfd_exit:
381         return ret;
382 }
383
384 static int _omap_device_notifier_call(struct notifier_block *nb,
385                                       unsigned long event, void *dev)
386 {
387         struct platform_device *pdev = to_platform_device(dev);
388
389         switch (event) {
390         case BUS_NOTIFY_ADD_DEVICE:
391                 if (pdev->dev.of_node)
392                         omap_device_build_from_dt(pdev);
393                 break;
394
395         case BUS_NOTIFY_DEL_DEVICE:
396                 if (pdev->archdata.od)
397                         omap_device_delete(pdev->archdata.od);
398                 break;
399         }
400
401         return NOTIFY_DONE;
402 }
403
404
405 /* Public functions for use by core code */
406
407 /**
408  * omap_device_get_context_loss_count - get lost context count
409  * @od: struct omap_device *
410  *
411  * Using the primary hwmod, query the context loss count for this
412  * device.
413  *
414  * Callers should consider context for this device lost any time this
415  * function returns a value different than the value the caller got
416  * the last time it called this function.
417  *
418  * If any hwmods exist for the omap_device assoiated with @pdev,
419  * return the context loss counter for that hwmod, otherwise return
420  * zero.
421  */
422 int omap_device_get_context_loss_count(struct platform_device *pdev)
423 {
424         struct omap_device *od;
425         u32 ret = 0;
426
427         od = to_omap_device(pdev);
428
429         if (od->hwmods_cnt)
430                 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
431
432         return ret;
433 }
434
435 /**
436  * omap_device_count_resources - count number of struct resource entries needed
437  * @od: struct omap_device *
438  *
439  * Count the number of struct resource entries needed for this
440  * omap_device @od.  Used by omap_device_build_ss() to determine how
441  * much memory to allocate before calling
442  * omap_device_fill_resources().  Returns the count.
443  */
444 static int omap_device_count_resources(struct omap_device *od)
445 {
446         int c = 0;
447         int i;
448
449         for (i = 0; i < od->hwmods_cnt; i++)
450                 c += omap_hwmod_count_resources(od->hwmods[i]);
451
452         pr_debug("omap_device: %s: counted %d total resources across %d "
453                  "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
454
455         return c;
456 }
457
458 /**
459  * omap_device_fill_resources - fill in array of struct resource
460  * @od: struct omap_device *
461  * @res: pointer to an array of struct resource to be filled in
462  *
463  * Populate one or more empty struct resource pointed to by @res with
464  * the resource data for this omap_device @od.  Used by
465  * omap_device_build_ss() after calling omap_device_count_resources().
466  * Ideally this function would not be needed at all.  If omap_device
467  * replaces platform_device, then we can specify our own
468  * get_resource()/ get_irq()/etc functions that use the underlying
469  * omap_hwmod information.  Or if platform_device is extended to use
470  * subarchitecture-specific function pointers, the various
471  * platform_device functions can simply call omap_device internal
472  * functions to get device resources.  Hacking around the existing
473  * platform_device code wastes memory.  Returns 0.
474  */
475 static int omap_device_fill_resources(struct omap_device *od,
476                                       struct resource *res)
477 {
478         int i, r;
479
480         for (i = 0; i < od->hwmods_cnt; i++) {
481                 r = omap_hwmod_fill_resources(od->hwmods[i], res);
482                 res += r;
483         }
484
485         return 0;
486 }
487
488 /**
489  * omap_device_alloc - allocate an omap_device
490  * @pdev: platform_device that will be included in this omap_device
491  * @oh: ptr to the single omap_hwmod that backs this omap_device
492  * @pdata: platform_data ptr to associate with the platform_device
493  * @pdata_len: amount of memory pointed to by @pdata
494  * @pm_lats: pointer to a omap_device_pm_latency array for this device
495  * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
496  *
497  * Convenience function for allocating an omap_device structure and filling
498  * hwmods, resources and pm_latency attributes.
499  *
500  * Returns an struct omap_device pointer or ERR_PTR() on error;
501  */
502 struct omap_device *omap_device_alloc(struct platform_device *pdev,
503                                         struct omap_hwmod **ohs, int oh_cnt,
504                                         struct omap_device_pm_latency *pm_lats,
505                                         int pm_lats_cnt)
506 {
507         int ret = -ENOMEM;
508         struct omap_device *od;
509         struct resource *res = NULL;
510         int i, res_count;
511         struct omap_hwmod **hwmods;
512
513         od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
514         if (!od) {
515                 ret = -ENOMEM;
516                 goto oda_exit1;
517         }
518         od->hwmods_cnt = oh_cnt;
519
520         hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
521         if (!hwmods)
522                 goto oda_exit2;
523
524         od->hwmods = hwmods;
525         od->pdev = pdev;
526
527         /*
528          * HACK: Ideally the resources from DT should match, and hwmod
529          * should just add the missing ones. Since the name is not
530          * properly populated by DT, stick to hwmod resources only.
531          */
532         if (pdev->num_resources && pdev->resource)
533                 dev_warn(&pdev->dev, "%s(): resources already allocated %d\n",
534                         __func__, pdev->num_resources);
535
536         res_count = omap_device_count_resources(od);
537         if (res_count > 0) {
538                 dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n",
539                         __func__, res_count);
540                 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
541                 if (!res)
542                         goto oda_exit3;
543
544                 omap_device_fill_resources(od, res);
545
546                 ret = platform_device_add_resources(pdev, res, res_count);
547                 kfree(res);
548
549                 if (ret)
550                         goto oda_exit3;
551         }
552
553         if (!pm_lats) {
554                 pm_lats = omap_default_latency;
555                 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
556         }
557
558         od->pm_lats_cnt = pm_lats_cnt;
559         od->pm_lats = kmemdup(pm_lats,
560                         sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
561                         GFP_KERNEL);
562         if (!od->pm_lats)
563                 goto oda_exit3;
564
565         pdev->archdata.od = od;
566
567         for (i = 0; i < oh_cnt; i++) {
568                 hwmods[i]->od = od;
569                 _add_hwmod_clocks_clkdev(od, hwmods[i]);
570         }
571
572         return od;
573
574 oda_exit3:
575         kfree(hwmods);
576 oda_exit2:
577         kfree(od);
578 oda_exit1:
579         dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
580
581         return ERR_PTR(ret);
582 }
583
584 void omap_device_delete(struct omap_device *od)
585 {
586         if (!od)
587                 return;
588
589         od->pdev->archdata.od = NULL;
590         kfree(od->pm_lats);
591         kfree(od->hwmods);
592         kfree(od);
593 }
594
595 /**
596  * omap_device_build - build and register an omap_device with one omap_hwmod
597  * @pdev_name: name of the platform_device driver to use
598  * @pdev_id: this platform_device's connection ID
599  * @oh: ptr to the single omap_hwmod that backs this omap_device
600  * @pdata: platform_data ptr to associate with the platform_device
601  * @pdata_len: amount of memory pointed to by @pdata
602  * @pm_lats: pointer to a omap_device_pm_latency array for this device
603  * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
604  * @is_early_device: should the device be registered as an early device or not
605  *
606  * Convenience function for building and registering a single
607  * omap_device record, which in turn builds and registers a
608  * platform_device record.  See omap_device_build_ss() for more
609  * information.  Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
610  * passes along the return value of omap_device_build_ss().
611  */
612 struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
613                                       struct omap_hwmod *oh, void *pdata,
614                                       int pdata_len,
615                                       struct omap_device_pm_latency *pm_lats,
616                                       int pm_lats_cnt, int is_early_device)
617 {
618         struct omap_hwmod *ohs[] = { oh };
619
620         if (!oh)
621                 return ERR_PTR(-EINVAL);
622
623         return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
624                                     pdata_len, pm_lats, pm_lats_cnt,
625                                     is_early_device);
626 }
627
628 /**
629  * omap_device_build_ss - build and register an omap_device with multiple hwmods
630  * @pdev_name: name of the platform_device driver to use
631  * @pdev_id: this platform_device's connection ID
632  * @oh: ptr to the single omap_hwmod that backs this omap_device
633  * @pdata: platform_data ptr to associate with the platform_device
634  * @pdata_len: amount of memory pointed to by @pdata
635  * @pm_lats: pointer to a omap_device_pm_latency array for this device
636  * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
637  * @is_early_device: should the device be registered as an early device or not
638  *
639  * Convenience function for building and registering an omap_device
640  * subsystem record.  Subsystem records consist of multiple
641  * omap_hwmods.  This function in turn builds and registers a
642  * platform_device record.  Returns an ERR_PTR() on error, or passes
643  * along the return value of omap_device_register().
644  */
645 struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
646                                          struct omap_hwmod **ohs, int oh_cnt,
647                                          void *pdata, int pdata_len,
648                                          struct omap_device_pm_latency *pm_lats,
649                                          int pm_lats_cnt, int is_early_device)
650 {
651         int ret = -ENOMEM;
652         struct platform_device *pdev;
653         struct omap_device *od;
654
655         if (!ohs || oh_cnt == 0 || !pdev_name)
656                 return ERR_PTR(-EINVAL);
657
658         if (!pdata && pdata_len > 0)
659                 return ERR_PTR(-EINVAL);
660
661         pdev = platform_device_alloc(pdev_name, pdev_id);
662         if (!pdev) {
663                 ret = -ENOMEM;
664                 goto odbs_exit;
665         }
666
667         /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
668         if (pdev->id != -1)
669                 dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
670         else
671                 dev_set_name(&pdev->dev, "%s", pdev->name);
672
673         od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
674         if (!od)
675                 goto odbs_exit1;
676
677         ret = platform_device_add_data(pdev, pdata, pdata_len);
678         if (ret)
679                 goto odbs_exit2;
680
681         if (is_early_device)
682                 ret = omap_early_device_register(pdev);
683         else
684                 ret = omap_device_register(pdev);
685         if (ret)
686                 goto odbs_exit2;
687
688         return pdev;
689
690 odbs_exit2:
691         omap_device_delete(od);
692 odbs_exit1:
693         platform_device_put(pdev);
694 odbs_exit:
695
696         pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
697
698         return ERR_PTR(ret);
699 }
700
701 /**
702  * omap_early_device_register - register an omap_device as an early platform
703  * device.
704  * @od: struct omap_device * to register
705  *
706  * Register the omap_device structure.  This currently just calls
707  * platform_early_add_device() on the underlying platform_device.
708  * Returns 0 by default.
709  */
710 static int __init omap_early_device_register(struct platform_device *pdev)
711 {
712         struct platform_device *devices[1];
713
714         devices[0] = pdev;
715         early_platform_add_devices(devices, 1);
716         return 0;
717 }
718
719 #ifdef CONFIG_PM_RUNTIME
720 static int _od_runtime_suspend(struct device *dev)
721 {
722         struct platform_device *pdev = to_platform_device(dev);
723         int ret;
724
725         ret = pm_generic_runtime_suspend(dev);
726
727         if (!ret)
728                 omap_device_idle(pdev);
729
730         return ret;
731 }
732
733 static int _od_runtime_idle(struct device *dev)
734 {
735         return pm_generic_runtime_idle(dev);
736 }
737
738 static int _od_runtime_resume(struct device *dev)
739 {
740         struct platform_device *pdev = to_platform_device(dev);
741
742         omap_device_enable(pdev);
743
744         return pm_generic_runtime_resume(dev);
745 }
746 #endif
747
748 #ifdef CONFIG_SUSPEND
749 static int _od_suspend_noirq(struct device *dev)
750 {
751         struct platform_device *pdev = to_platform_device(dev);
752         struct omap_device *od = to_omap_device(pdev);
753         int ret;
754
755         ret = pm_generic_suspend_noirq(dev);
756
757         if (!ret && !pm_runtime_status_suspended(dev)) {
758                 if (pm_generic_runtime_suspend(dev) == 0) {
759                         if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
760                                 omap_device_idle(pdev);
761                         od->flags |= OMAP_DEVICE_SUSPENDED;
762                 }
763         }
764
765         return ret;
766 }
767
768 static int _od_resume_noirq(struct device *dev)
769 {
770         struct platform_device *pdev = to_platform_device(dev);
771         struct omap_device *od = to_omap_device(pdev);
772
773         if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
774             !pm_runtime_status_suspended(dev)) {
775                 od->flags &= ~OMAP_DEVICE_SUSPENDED;
776                 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
777                         omap_device_enable(pdev);
778                 pm_generic_runtime_resume(dev);
779         }
780
781         return pm_generic_resume_noirq(dev);
782 }
783 #else
784 #define _od_suspend_noirq NULL
785 #define _od_resume_noirq NULL
786 #endif
787
788 struct dev_pm_domain omap_device_pm_domain = {
789         .ops = {
790                 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
791                                    _od_runtime_idle)
792                 USE_PLATFORM_PM_SLEEP_OPS
793                 .suspend_noirq = _od_suspend_noirq,
794                 .resume_noirq = _od_resume_noirq,
795         }
796 };
797
798 /**
799  * omap_device_register - register an omap_device with one omap_hwmod
800  * @od: struct omap_device * to register
801  *
802  * Register the omap_device structure.  This currently just calls
803  * platform_device_register() on the underlying platform_device.
804  * Returns the return value of platform_device_register().
805  */
806 int omap_device_register(struct platform_device *pdev)
807 {
808         pr_debug("omap_device: %s: registering\n", pdev->name);
809
810         pdev->dev.pm_domain = &omap_device_pm_domain;
811         return platform_device_add(pdev);
812 }
813
814
815 /* Public functions for use by device drivers through struct platform_data */
816
817 /**
818  * omap_device_enable - fully activate an omap_device
819  * @od: struct omap_device * to activate
820  *
821  * Do whatever is necessary for the hwmods underlying omap_device @od
822  * to be accessible and ready to operate.  This generally involves
823  * enabling clocks, setting SYSCONFIG registers; and in the future may
824  * involve remuxing pins.  Device drivers should call this function
825  * (through platform_data function pointers) where they would normally
826  * enable clocks, etc.  Returns -EINVAL if called when the omap_device
827  * is already enabled, or passes along the return value of
828  * _omap_device_activate().
829  */
830 int omap_device_enable(struct platform_device *pdev)
831 {
832         int ret;
833         struct omap_device *od;
834
835         od = to_omap_device(pdev);
836
837         if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
838                 dev_warn(&pdev->dev,
839                          "omap_device: %s() called from invalid state %d\n",
840                          __func__, od->_state);
841                 return -EINVAL;
842         }
843
844         /* Enable everything if we're enabling this device from scratch */
845         if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
846                 od->pm_lat_level = od->pm_lats_cnt;
847
848         ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
849
850         od->dev_wakeup_lat = 0;
851         od->_dev_wakeup_lat_limit = UINT_MAX;
852         od->_state = OMAP_DEVICE_STATE_ENABLED;
853
854         return ret;
855 }
856
857 /**
858  * omap_device_idle - idle an omap_device
859  * @od: struct omap_device * to idle
860  *
861  * Idle omap_device @od by calling as many .deactivate_func() entries
862  * in the omap_device's pm_lats table as is possible without exceeding
863  * the device's maximum wakeup latency limit, pm_lat_limit.  Device
864  * drivers should call this function (through platform_data function
865  * pointers) where they would normally disable clocks after operations
866  * complete, etc..  Returns -EINVAL if the omap_device is not
867  * currently enabled, or passes along the return value of
868  * _omap_device_deactivate().
869  */
870 int omap_device_idle(struct platform_device *pdev)
871 {
872         int ret;
873         struct omap_device *od;
874
875         od = to_omap_device(pdev);
876
877         if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
878                 dev_warn(&pdev->dev,
879                          "omap_device: %s() called from invalid state %d\n",
880                          __func__, od->_state);
881                 return -EINVAL;
882         }
883
884         ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
885
886         od->_state = OMAP_DEVICE_STATE_IDLE;
887
888         return ret;
889 }
890
891 /**
892  * omap_device_shutdown - shut down an omap_device
893  * @od: struct omap_device * to shut down
894  *
895  * Shut down omap_device @od by calling all .deactivate_func() entries
896  * in the omap_device's pm_lats table and then shutting down all of
897  * the underlying omap_hwmods.  Used when a device is being "removed"
898  * or a device driver is being unloaded.  Returns -EINVAL if the
899  * omap_device is not currently enabled or idle, or passes along the
900  * return value of _omap_device_deactivate().
901  */
902 int omap_device_shutdown(struct platform_device *pdev)
903 {
904         int ret, i;
905         struct omap_device *od;
906
907         od = to_omap_device(pdev);
908
909         if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
910             od->_state != OMAP_DEVICE_STATE_IDLE) {
911                 dev_warn(&pdev->dev,
912                          "omap_device: %s() called from invalid state %d\n",
913                          __func__, od->_state);
914                 return -EINVAL;
915         }
916
917         ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
918
919         for (i = 0; i < od->hwmods_cnt; i++)
920                 omap_hwmod_shutdown(od->hwmods[i]);
921
922         od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
923
924         return ret;
925 }
926
927 /**
928  * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
929  * @od: struct omap_device *
930  *
931  * When a device's maximum wakeup latency limit changes, call some of
932  * the .activate_func or .deactivate_func function pointers in the
933  * omap_device's pm_lats array to ensure that the device's maximum
934  * wakeup latency is less than or equal to the new latency limit.
935  * Intended to be called by OMAP PM code whenever a device's maximum
936  * wakeup latency limit changes (e.g., via
937  * omap_pm_set_dev_wakeup_lat()).  Returns 0 if nothing needs to be
938  * done (e.g., if the omap_device is not currently idle, or if the
939  * wakeup latency is already current with the new limit) or passes
940  * along the return value of _omap_device_deactivate() or
941  * _omap_device_activate().
942  */
943 int omap_device_align_pm_lat(struct platform_device *pdev,
944                              u32 new_wakeup_lat_limit)
945 {
946         int ret = -EINVAL;
947         struct omap_device *od;
948
949         od = to_omap_device(pdev);
950
951         if (new_wakeup_lat_limit == od->dev_wakeup_lat)
952                 return 0;
953
954         od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
955
956         if (od->_state != OMAP_DEVICE_STATE_IDLE)
957                 return 0;
958         else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
959                 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
960         else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
961                 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
962
963         return ret;
964 }
965
966 /**
967  * omap_device_get_pwrdm - return the powerdomain * associated with @od
968  * @od: struct omap_device *
969  *
970  * Return the powerdomain associated with the first underlying
971  * omap_hwmod for this omap_device.  Intended for use by core OMAP PM
972  * code.  Returns NULL on error or a struct powerdomain * upon
973  * success.
974  */
975 struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
976 {
977         /*
978          * XXX Assumes that all omap_hwmod powerdomains are identical.
979          * This may not necessarily be true.  There should be a sanity
980          * check in here to WARN() if any difference appears.
981          */
982         if (!od->hwmods_cnt)
983                 return NULL;
984
985         return omap_hwmod_get_pwrdm(od->hwmods[0]);
986 }
987
988 /**
989  * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
990  * @od: struct omap_device *
991  *
992  * Return the MPU's virtual address for the base of the hwmod, from
993  * the ioremap() that the hwmod code does.  Only valid if there is one
994  * hwmod associated with this device.  Returns NULL if there are zero
995  * or more than one hwmods associated with this omap_device;
996  * otherwise, passes along the return value from
997  * omap_hwmod_get_mpu_rt_va().
998  */
999 void __iomem *omap_device_get_rt_va(struct omap_device *od)
1000 {
1001         if (od->hwmods_cnt != 1)
1002                 return NULL;
1003
1004         return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1005 }
1006
1007 /**
1008  * omap_device_get_by_hwmod_name() - convert a hwmod name to
1009  * device pointer.
1010  * @oh_name: name of the hwmod device
1011  *
1012  * Returns back a struct device * pointer associated with a hwmod
1013  * device represented by a hwmod_name
1014  */
1015 struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1016 {
1017         struct omap_hwmod *oh;
1018
1019         if (!oh_name) {
1020                 WARN(1, "%s: no hwmod name!\n", __func__);
1021                 return ERR_PTR(-EINVAL);
1022         }
1023
1024         oh = omap_hwmod_lookup(oh_name);
1025         if (IS_ERR_OR_NULL(oh)) {
1026                 WARN(1, "%s: no hwmod for %s\n", __func__,
1027                         oh_name);
1028                 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1029         }
1030         if (IS_ERR_OR_NULL(oh->od)) {
1031                 WARN(1, "%s: no omap_device for %s\n", __func__,
1032                         oh_name);
1033                 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1034         }
1035
1036         if (IS_ERR_OR_NULL(oh->od->pdev))
1037                 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1038
1039         return &oh->od->pdev->dev;
1040 }
1041 EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1042
1043 /*
1044  * Public functions intended for use in omap_device_pm_latency
1045  * .activate_func and .deactivate_func function pointers
1046  */
1047
1048 /**
1049  * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1050  * @od: struct omap_device *od
1051  *
1052  * Enable all underlying hwmods.  Returns 0.
1053  */
1054 int omap_device_enable_hwmods(struct omap_device *od)
1055 {
1056         int i;
1057
1058         for (i = 0; i < od->hwmods_cnt; i++)
1059                 omap_hwmod_enable(od->hwmods[i]);
1060
1061         /* XXX pass along return value here? */
1062         return 0;
1063 }
1064
1065 /**
1066  * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1067  * @od: struct omap_device *od
1068  *
1069  * Idle all underlying hwmods.  Returns 0.
1070  */
1071 int omap_device_idle_hwmods(struct omap_device *od)
1072 {
1073         int i;
1074
1075         for (i = 0; i < od->hwmods_cnt; i++)
1076                 omap_hwmod_idle(od->hwmods[i]);
1077
1078         /* XXX pass along return value here? */
1079         return 0;
1080 }
1081
1082 /**
1083  * omap_device_disable_clocks - disable all main and interface clocks
1084  * @od: struct omap_device *od
1085  *
1086  * Disable the main functional clock and interface clock for all of the
1087  * omap_hwmods associated with the omap_device.  Returns 0.
1088  */
1089 int omap_device_disable_clocks(struct omap_device *od)
1090 {
1091         int i;
1092
1093         for (i = 0; i < od->hwmods_cnt; i++)
1094                 omap_hwmod_disable_clocks(od->hwmods[i]);
1095
1096         /* XXX pass along return value here? */
1097         return 0;
1098 }
1099
1100 /**
1101  * omap_device_enable_clocks - enable all main and interface clocks
1102  * @od: struct omap_device *od
1103  *
1104  * Enable the main functional clock and interface clock for all of the
1105  * omap_hwmods associated with the omap_device.  Returns 0.
1106  */
1107 int omap_device_enable_clocks(struct omap_device *od)
1108 {
1109         int i;
1110
1111         for (i = 0; i < od->hwmods_cnt; i++)
1112                 omap_hwmod_enable_clocks(od->hwmods[i]);
1113
1114         /* XXX pass along return value here? */
1115         return 0;
1116 }
1117
1118 static struct notifier_block platform_nb = {
1119         .notifier_call = _omap_device_notifier_call,
1120 };
1121
1122 static int __init omap_device_init(void)
1123 {
1124         bus_register_notifier(&platform_bus_type, &platform_nb);
1125         return 0;
1126 }
1127 core_initcall(omap_device_init);