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