Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / arch / arm / plat-omap / omap_device.c
1 /*
2  * omap_device implementation
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Paul Walmsley
6  *
7  * Developed in collaboration with (alphabetical order): Benoit
8  * Cousson, Kevin Hilman, 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/platform_device.h>
82 #include <linux/err.h>
83 #include <linux/io.h>
84
85 #include <plat/omap_device.h>
86 #include <plat/omap_hwmod.h>
87
88 /* These parameters are passed to _omap_device_{de,}activate() */
89 #define USE_WAKEUP_LAT                  0
90 #define IGNORE_WAKEUP_LAT               1
91
92
93 /* Private functions */
94
95 /**
96  * _omap_device_activate - increase device readiness
97  * @od: struct omap_device *
98  * @ignore_lat: increase to latency target (0) or full readiness (1)?
99  *
100  * Increase readiness of omap_device @od (thus decreasing device
101  * wakeup latency, but consuming more power).  If @ignore_lat is
102  * IGNORE_WAKEUP_LAT, make the omap_device fully active.  Otherwise,
103  * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
104  * latency is greater than the requested maximum wakeup latency, step
105  * backwards in the omap_device_pm_latency table to ensure the
106  * device's maximum wakeup latency is less than or equal to the
107  * requested maximum wakeup latency.  Returns 0.
108  */
109 static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
110 {
111         struct timespec a, b, c;
112
113         pr_debug("omap_device: %s: activating\n", od->pdev.name);
114
115         while (od->pm_lat_level > 0) {
116                 struct omap_device_pm_latency *odpl;
117                 unsigned long long act_lat = 0;
118
119                 od->pm_lat_level--;
120
121                 odpl = od->pm_lats + od->pm_lat_level;
122
123                 if (!ignore_lat &&
124                     (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
125                         break;
126
127                 read_persistent_clock(&a);
128
129                 /* XXX check return code */
130                 odpl->activate_func(od);
131
132                 read_persistent_clock(&b);
133
134                 c = timespec_sub(b, a);
135                 act_lat = timespec_to_ns(&c);
136
137                 pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
138                          "%llu nsec\n", od->pdev.name, od->pm_lat_level,
139                          act_lat);
140
141                 WARN(act_lat > odpl->activate_lat, "omap_device: %s.%d: "
142                      "activate step %d took longer than expected (%llu > %d)\n",
143                      od->pdev.name, od->pdev.id, od->pm_lat_level,
144                      act_lat, odpl->activate_lat);
145
146                 od->dev_wakeup_lat -= odpl->activate_lat;
147         }
148
149         return 0;
150 }
151
152 /**
153  * _omap_device_deactivate - decrease device readiness
154  * @od: struct omap_device *
155  * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
156  *
157  * Decrease readiness of omap_device @od (thus increasing device
158  * wakeup latency, but conserving power).  If @ignore_lat is
159  * IGNORE_WAKEUP_LAT, make the omap_device fully inactive.  Otherwise,
160  * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
161  * latency is less than the requested maximum wakeup latency, step
162  * forwards in the omap_device_pm_latency table to ensure the device's
163  * maximum wakeup latency is less than or equal to the requested
164  * maximum wakeup latency.  Returns 0.
165  */
166 static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
167 {
168         struct timespec a, b, c;
169
170         pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
171
172         while (od->pm_lat_level < od->pm_lats_cnt) {
173                 struct omap_device_pm_latency *odpl;
174                 unsigned long long deact_lat = 0;
175
176                 odpl = od->pm_lats + od->pm_lat_level;
177
178                 if (!ignore_lat &&
179                     ((od->dev_wakeup_lat + odpl->activate_lat) >
180                      od->_dev_wakeup_lat_limit))
181                         break;
182
183                 read_persistent_clock(&a);
184
185                 /* XXX check return code */
186                 odpl->deactivate_func(od);
187
188                 read_persistent_clock(&b);
189
190                 c = timespec_sub(b, a);
191                 deact_lat = timespec_to_ns(&c);
192
193                 pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
194                          "%llu nsec\n", od->pdev.name, od->pm_lat_level,
195                          deact_lat);
196
197                 WARN(deact_lat > odpl->deactivate_lat, "omap_device: %s.%d: "
198                      "deactivate step %d took longer than expected "
199                      "(%llu > %d)\n", od->pdev.name, od->pdev.id,
200                      od->pm_lat_level, deact_lat, odpl->deactivate_lat);
201
202                 od->dev_wakeup_lat += odpl->activate_lat;
203
204                 od->pm_lat_level++;
205         }
206
207         return 0;
208 }
209
210 static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
211 {
212         return container_of(pdev, struct omap_device, pdev);
213 }
214
215
216 /* Public functions for use by core code */
217
218 /**
219  * omap_device_count_resources - count number of struct resource entries needed
220  * @od: struct omap_device *
221  *
222  * Count the number of struct resource entries needed for this
223  * omap_device @od.  Used by omap_device_build_ss() to determine how
224  * much memory to allocate before calling
225  * omap_device_fill_resources().  Returns the count.
226  */
227 int omap_device_count_resources(struct omap_device *od)
228 {
229         struct omap_hwmod *oh;
230         int c = 0;
231         int i;
232
233         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
234                 c += omap_hwmod_count_resources(oh);
235
236         pr_debug("omap_device: %s: counted %d total resources across %d "
237                  "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
238
239         return c;
240 }
241
242 /**
243  * omap_device_fill_resources - fill in array of struct resource
244  * @od: struct omap_device *
245  * @res: pointer to an array of struct resource to be filled in
246  *
247  * Populate one or more empty struct resource pointed to by @res with
248  * the resource data for this omap_device @od.  Used by
249  * omap_device_build_ss() after calling omap_device_count_resources().
250  * Ideally this function would not be needed at all.  If omap_device
251  * replaces platform_device, then we can specify our own
252  * get_resource()/ get_irq()/etc functions that use the underlying
253  * omap_hwmod information.  Or if platform_device is extended to use
254  * subarchitecture-specific function pointers, the various
255  * platform_device functions can simply call omap_device internal
256  * functions to get device resources.  Hacking around the existing
257  * platform_device code wastes memory.  Returns 0.
258  */
259 int omap_device_fill_resources(struct omap_device *od, struct resource *res)
260 {
261         struct omap_hwmod *oh;
262         int c = 0;
263         int i, r;
264
265         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++) {
266                 r = omap_hwmod_fill_resources(oh, res);
267                 res += r;
268                 c += r;
269         }
270
271         return 0;
272 }
273
274 /**
275  * omap_device_build - build and register an omap_device with one omap_hwmod
276  * @pdev_name: name of the platform_device driver to use
277  * @pdev_id: this platform_device's connection ID
278  * @oh: ptr to the single omap_hwmod that backs this omap_device
279  * @pdata: platform_data ptr to associate with the platform_device
280  * @pdata_len: amount of memory pointed to by @pdata
281  * @pm_lats: pointer to a omap_device_pm_latency array for this device
282  * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
283  *
284  * Convenience function for building and registering a single
285  * omap_device record, which in turn builds and registers a
286  * platform_device record.  See omap_device_build_ss() for more
287  * information.  Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
288  * passes along the return value of omap_device_build_ss().
289  */
290 struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
291                                       struct omap_hwmod *oh, void *pdata,
292                                       int pdata_len,
293                                       struct omap_device_pm_latency *pm_lats,
294                                       int pm_lats_cnt)
295 {
296         struct omap_hwmod *ohs[] = { oh };
297
298         if (!oh)
299                 return ERR_PTR(-EINVAL);
300
301         return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
302                                     pdata_len, pm_lats, pm_lats_cnt);
303 }
304
305 /**
306  * omap_device_build_ss - build and register an omap_device with multiple hwmods
307  * @pdev_name: name of the platform_device driver to use
308  * @pdev_id: this platform_device's connection ID
309  * @oh: ptr to the single omap_hwmod that backs this omap_device
310  * @pdata: platform_data ptr to associate with the platform_device
311  * @pdata_len: amount of memory pointed to by @pdata
312  * @pm_lats: pointer to a omap_device_pm_latency array for this device
313  * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
314  *
315  * Convenience function for building and registering an omap_device
316  * subsystem record.  Subsystem records consist of multiple
317  * omap_hwmods.  This function in turn builds and registers a
318  * platform_device record.  Returns an ERR_PTR() on error, or passes
319  * along the return value of omap_device_register().
320  */
321 struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
322                                          struct omap_hwmod **ohs, int oh_cnt,
323                                          void *pdata, int pdata_len,
324                                          struct omap_device_pm_latency *pm_lats,
325                                          int pm_lats_cnt)
326 {
327         int ret = -ENOMEM;
328         struct omap_device *od;
329         char *pdev_name2;
330         struct resource *res = NULL;
331         int res_count;
332         struct omap_hwmod **hwmods;
333
334         if (!ohs || oh_cnt == 0 || !pdev_name)
335                 return ERR_PTR(-EINVAL);
336
337         if (!pdata && pdata_len > 0)
338                 return ERR_PTR(-EINVAL);
339
340         pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
341                  oh_cnt);
342
343         od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
344         if (!od)
345                 return ERR_PTR(-ENOMEM);
346
347         od->hwmods_cnt = oh_cnt;
348
349         hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
350                          GFP_KERNEL);
351         if (!hwmods)
352                 goto odbs_exit1;
353
354         memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
355         od->hwmods = hwmods;
356
357         pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
358         if (!pdev_name2)
359                 goto odbs_exit2;
360         strcpy(pdev_name2, pdev_name);
361
362         od->pdev.name = pdev_name2;
363         od->pdev.id = pdev_id;
364
365         res_count = omap_device_count_resources(od);
366         if (res_count > 0) {
367                 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
368                 if (!res)
369                         goto odbs_exit3;
370         }
371         omap_device_fill_resources(od, res);
372
373         od->pdev.num_resources = res_count;
374         od->pdev.resource = res;
375
376         platform_device_add_data(&od->pdev, pdata, pdata_len);
377
378         od->pm_lats = pm_lats;
379         od->pm_lats_cnt = pm_lats_cnt;
380
381         ret = omap_device_register(od);
382         if (ret)
383                 goto odbs_exit4;
384
385         return od;
386
387 odbs_exit4:
388         kfree(res);
389 odbs_exit3:
390         kfree(pdev_name2);
391 odbs_exit2:
392         kfree(hwmods);
393 odbs_exit1:
394         kfree(od);
395
396         pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
397
398         return ERR_PTR(ret);
399 }
400
401 /**
402  * omap_device_register - register an omap_device with one omap_hwmod
403  * @od: struct omap_device * to register
404  *
405  * Register the omap_device structure.  This currently just calls
406  * platform_device_register() on the underlying platform_device.
407  * Returns the return value of platform_device_register().
408  */
409 int omap_device_register(struct omap_device *od)
410 {
411         pr_debug("omap_device: %s: registering\n", od->pdev.name);
412
413         return platform_device_register(&od->pdev);
414 }
415
416
417 /* Public functions for use by device drivers through struct platform_data */
418
419 /**
420  * omap_device_enable - fully activate an omap_device
421  * @od: struct omap_device * to activate
422  *
423  * Do whatever is necessary for the hwmods underlying omap_device @od
424  * to be accessible and ready to operate.  This generally involves
425  * enabling clocks, setting SYSCONFIG registers; and in the future may
426  * involve remuxing pins.  Device drivers should call this function
427  * (through platform_data function pointers) where they would normally
428  * enable clocks, etc.  Returns -EINVAL if called when the omap_device
429  * is already enabled, or passes along the return value of
430  * _omap_device_activate().
431  */
432 int omap_device_enable(struct platform_device *pdev)
433 {
434         int ret;
435         struct omap_device *od;
436
437         od = _find_by_pdev(pdev);
438
439         if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
440                 WARN(1, "omap_device: %s.%d: omap_device_enable() called from "
441                      "invalid state\n", od->pdev.name, od->pdev.id);
442                 return -EINVAL;
443         }
444
445         /* Enable everything if we're enabling this device from scratch */
446         if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
447                 od->pm_lat_level = od->pm_lats_cnt;
448
449         ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
450
451         od->dev_wakeup_lat = 0;
452         od->_dev_wakeup_lat_limit = UINT_MAX;
453         od->_state = OMAP_DEVICE_STATE_ENABLED;
454
455         return ret;
456 }
457
458 /**
459  * omap_device_idle - idle an omap_device
460  * @od: struct omap_device * to idle
461  *
462  * Idle omap_device @od by calling as many .deactivate_func() entries
463  * in the omap_device's pm_lats table as is possible without exceeding
464  * the device's maximum wakeup latency limit, pm_lat_limit.  Device
465  * drivers should call this function (through platform_data function
466  * pointers) where they would normally disable clocks after operations
467  * complete, etc..  Returns -EINVAL if the omap_device is not
468  * currently enabled, or passes along the return value of
469  * _omap_device_deactivate().
470  */
471 int omap_device_idle(struct platform_device *pdev)
472 {
473         int ret;
474         struct omap_device *od;
475
476         od = _find_by_pdev(pdev);
477
478         if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
479                 WARN(1, "omap_device: %s.%d: omap_device_idle() called from "
480                      "invalid state\n", od->pdev.name, od->pdev.id);
481                 return -EINVAL;
482         }
483
484         ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
485
486         od->_state = OMAP_DEVICE_STATE_IDLE;
487
488         return ret;
489 }
490
491 /**
492  * omap_device_shutdown - shut down an omap_device
493  * @od: struct omap_device * to shut down
494  *
495  * Shut down omap_device @od by calling all .deactivate_func() entries
496  * in the omap_device's pm_lats table and then shutting down all of
497  * the underlying omap_hwmods.  Used when a device is being "removed"
498  * or a device driver is being unloaded.  Returns -EINVAL if the
499  * omap_device is not currently enabled or idle, or passes along the
500  * return value of _omap_device_deactivate().
501  */
502 int omap_device_shutdown(struct platform_device *pdev)
503 {
504         int ret, i;
505         struct omap_device *od;
506         struct omap_hwmod *oh;
507
508         od = _find_by_pdev(pdev);
509
510         if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
511             od->_state != OMAP_DEVICE_STATE_IDLE) {
512                 WARN(1, "omap_device: %s.%d: omap_device_shutdown() called "
513                      "from invalid state\n", od->pdev.name, od->pdev.id);
514                 return -EINVAL;
515         }
516
517         ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
518
519         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
520                 omap_hwmod_shutdown(oh);
521
522         od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
523
524         return ret;
525 }
526
527 /**
528  * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
529  * @od: struct omap_device *
530  *
531  * When a device's maximum wakeup latency limit changes, call some of
532  * the .activate_func or .deactivate_func function pointers in the
533  * omap_device's pm_lats array to ensure that the device's maximum
534  * wakeup latency is less than or equal to the new latency limit.
535  * Intended to be called by OMAP PM code whenever a device's maximum
536  * wakeup latency limit changes (e.g., via
537  * omap_pm_set_dev_wakeup_lat()).  Returns 0 if nothing needs to be
538  * done (e.g., if the omap_device is not currently idle, or if the
539  * wakeup latency is already current with the new limit) or passes
540  * along the return value of _omap_device_deactivate() or
541  * _omap_device_activate().
542  */
543 int omap_device_align_pm_lat(struct platform_device *pdev,
544                              u32 new_wakeup_lat_limit)
545 {
546         int ret = -EINVAL;
547         struct omap_device *od;
548
549         od = _find_by_pdev(pdev);
550
551         if (new_wakeup_lat_limit == od->dev_wakeup_lat)
552                 return 0;
553
554         od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
555
556         if (od->_state != OMAP_DEVICE_STATE_IDLE)
557                 return 0;
558         else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
559                 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
560         else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
561                 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
562
563         return ret;
564 }
565
566 /**
567  * omap_device_get_pwrdm - return the powerdomain * associated with @od
568  * @od: struct omap_device *
569  *
570  * Return the powerdomain associated with the first underlying
571  * omap_hwmod for this omap_device.  Intended for use by core OMAP PM
572  * code.  Returns NULL on error or a struct powerdomain * upon
573  * success.
574  */
575 struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
576 {
577         /*
578          * XXX Assumes that all omap_hwmod powerdomains are identical.
579          * This may not necessarily be true.  There should be a sanity
580          * check in here to WARN() if any difference appears.
581          */
582         if (!od->hwmods_cnt)
583                 return NULL;
584
585         return omap_hwmod_get_pwrdm(od->hwmods[0]);
586 }
587
588 /*
589  * Public functions intended for use in omap_device_pm_latency
590  * .activate_func and .deactivate_func function pointers
591  */
592
593 /**
594  * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
595  * @od: struct omap_device *od
596  *
597  * Enable all underlying hwmods.  Returns 0.
598  */
599 int omap_device_enable_hwmods(struct omap_device *od)
600 {
601         struct omap_hwmod *oh;
602         int i;
603
604         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
605                 omap_hwmod_enable(oh);
606
607         /* XXX pass along return value here? */
608         return 0;
609 }
610
611 /**
612  * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
613  * @od: struct omap_device *od
614  *
615  * Idle all underlying hwmods.  Returns 0.
616  */
617 int omap_device_idle_hwmods(struct omap_device *od)
618 {
619         struct omap_hwmod *oh;
620         int i;
621
622         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
623                 omap_hwmod_idle(oh);
624
625         /* XXX pass along return value here? */
626         return 0;
627 }
628
629 /**
630  * omap_device_disable_clocks - disable all main and interface clocks
631  * @od: struct omap_device *od
632  *
633  * Disable the main functional clock and interface clock for all of the
634  * omap_hwmods associated with the omap_device.  Returns 0.
635  */
636 int omap_device_disable_clocks(struct omap_device *od)
637 {
638         struct omap_hwmod *oh;
639         int i;
640
641         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
642                 omap_hwmod_disable_clocks(oh);
643
644         /* XXX pass along return value here? */
645         return 0;
646 }
647
648 /**
649  * omap_device_enable_clocks - enable all main and interface clocks
650  * @od: struct omap_device *od
651  *
652  * Enable the main functional clock and interface clock for all of the
653  * omap_hwmods associated with the omap_device.  Returns 0.
654  */
655 int omap_device_enable_clocks(struct omap_device *od)
656 {
657         struct omap_hwmod *oh;
658         int i;
659
660         for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
661                 omap_hwmod_enable_clocks(oh);
662
663         /* XXX pass along return value here? */
664         return 0;
665 }