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