Add KZM-ARM11-01 support
[linux-2.6/libata-dev.git] / arch / arm / plat-omap / omap_device.c
blob2c409fc6dd21d93817de1f6c5321cb9d0aff5048
1 /*
2 * omap_device implementation
4 * Copyright (C) 2009 Nokia Corporation
5 * Paul Walmsley
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
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.
16 * This code provides a consistent interface for OMAP device drivers
17 * to control power management and interconnect properties of their
18 * devices.
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
27 * Guidelines for usage by driver authors:
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
33 * struct foo_driver_platform_data {
34 * ...
35 * int (*device_enable)(struct platform_device *pdev);
36 * ...
37 * }
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.
43 * This should be populated during device setup:
45 * ...
46 * pdata->device_enable = omap_device_enable;
47 * ...
49 * 2. Drivers should first check to ensure the function pointer is not null
50 * before calling it, as in:
52 * if (pdata->device_enable)
53 * pdata->device_enable(pdev);
55 * This allows other architectures that don't use similar device_enable()/
56 * device_shutdown() functions to execute normally.
58 * ...
60 * Suggested usage by device drivers:
62 * During device initialization:
63 * device_enable()
65 * During device idle:
66 * (save remaining device context if necessary)
67 * device_idle();
69 * During device resume:
70 * device_enable();
71 * (restore context if necessary)
73 * During device shutdown:
74 * device_shutdown()
75 * (device must be reinitialized at this point to use it again)
78 #undef DEBUG
80 #include <linux/kernel.h>
81 #include <linux/platform_device.h>
82 #include <linux/err.h>
83 #include <linux/io.h>
85 #include <mach/omap_device.h>
86 #include <mach/omap_hwmod.h>
88 /* These parameters are passed to _omap_device_{de,}activate() */
89 #define USE_WAKEUP_LAT 0
90 #define IGNORE_WAKEUP_LAT 1
92 /* XXX this should be moved into a separate file */
93 #if defined(CONFIG_ARCH_OMAP2420)
94 # define OMAP_32KSYNCT_BASE 0x48004000
95 #elif defined(CONFIG_ARCH_OMAP2430)
96 # define OMAP_32KSYNCT_BASE 0x49020000
97 #elif defined(CONFIG_ARCH_OMAP3430)
98 # define OMAP_32KSYNCT_BASE 0x48320000
99 #else
100 # error Unknown OMAP device
101 #endif
103 /* Private functions */
106 * _read_32ksynct - read the OMAP 32K sync timer
108 * Returns the current value of the 32KiHz synchronization counter.
109 * XXX this should be generalized to simply read the system clocksource.
110 * XXX this should be moved to a separate synctimer32k.c file
112 static u32 _read_32ksynct(void)
114 if (!cpu_class_is_omap2())
115 BUG();
117 return __raw_readl(OMAP2_IO_ADDRESS(OMAP_32KSYNCT_BASE + 0x010));
121 * _omap_device_activate - increase device readiness
122 * @od: struct omap_device *
123 * @ignore_lat: increase to latency target (0) or full readiness (1)?
125 * Increase readiness of omap_device @od (thus decreasing device
126 * wakeup latency, but consuming more power). If @ignore_lat is
127 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
128 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
129 * latency is greater than the requested maximum wakeup latency, step
130 * backwards in the omap_device_pm_latency table to ensure the
131 * device's maximum wakeup latency is less than or equal to the
132 * requested maximum wakeup latency. Returns 0.
134 static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
136 u32 a, b;
138 pr_debug("omap_device: %s: activating\n", od->pdev.name);
140 while (od->pm_lat_level > 0) {
141 struct omap_device_pm_latency *odpl;
142 int act_lat = 0;
144 od->pm_lat_level--;
146 odpl = od->pm_lats + od->pm_lat_level;
148 if (!ignore_lat &&
149 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
150 break;
152 a = _read_32ksynct();
154 /* XXX check return code */
155 odpl->activate_func(od);
157 b = _read_32ksynct();
159 act_lat = (b - a) >> 15; /* 32KiHz cycles to microseconds */
161 pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
162 "%d usec\n", od->pdev.name, od->pm_lat_level, act_lat);
164 WARN(act_lat > odpl->activate_lat, "omap_device: %s.%d: "
165 "activate step %d took longer than expected (%d > %d)\n",
166 od->pdev.name, od->pdev.id, od->pm_lat_level,
167 act_lat, odpl->activate_lat);
169 od->dev_wakeup_lat -= odpl->activate_lat;
172 return 0;
176 * _omap_device_deactivate - decrease device readiness
177 * @od: struct omap_device *
178 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
180 * Decrease readiness of omap_device @od (thus increasing device
181 * wakeup latency, but conserving power). If @ignore_lat is
182 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
183 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
184 * latency is less than the requested maximum wakeup latency, step
185 * forwards in the omap_device_pm_latency table to ensure the device's
186 * maximum wakeup latency is less than or equal to the requested
187 * maximum wakeup latency. Returns 0.
189 static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
191 u32 a, b;
193 pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
195 while (od->pm_lat_level < od->pm_lats_cnt) {
196 struct omap_device_pm_latency *odpl;
197 int deact_lat = 0;
199 odpl = od->pm_lats + od->pm_lat_level;
201 if (!ignore_lat &&
202 ((od->dev_wakeup_lat + odpl->activate_lat) >
203 od->_dev_wakeup_lat_limit))
204 break;
206 a = _read_32ksynct();
208 /* XXX check return code */
209 odpl->deactivate_func(od);
211 b = _read_32ksynct();
213 deact_lat = (b - a) >> 15; /* 32KiHz cycles to microseconds */
215 pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
216 "%d usec\n", od->pdev.name, od->pm_lat_level,
217 deact_lat);
219 WARN(deact_lat > odpl->deactivate_lat, "omap_device: %s.%d: "
220 "deactivate step %d took longer than expected (%d > %d)\n",
221 od->pdev.name, od->pdev.id, od->pm_lat_level,
222 deact_lat, odpl->deactivate_lat);
224 od->dev_wakeup_lat += odpl->activate_lat;
226 od->pm_lat_level++;
229 return 0;
232 static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
234 return container_of(pdev, struct omap_device, pdev);
238 /* Public functions for use by core code */
241 * omap_device_count_resources - count number of struct resource entries needed
242 * @od: struct omap_device *
244 * Count the number of struct resource entries needed for this
245 * omap_device @od. Used by omap_device_build_ss() to determine how
246 * much memory to allocate before calling
247 * omap_device_fill_resources(). Returns the count.
249 int omap_device_count_resources(struct omap_device *od)
251 struct omap_hwmod *oh;
252 int c = 0;
253 int i;
255 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
256 c += omap_hwmod_count_resources(oh);
258 pr_debug("omap_device: %s: counted %d total resources across %d "
259 "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
261 return c;
265 * omap_device_fill_resources - fill in array of struct resource
266 * @od: struct omap_device *
267 * @res: pointer to an array of struct resource to be filled in
269 * Populate one or more empty struct resource pointed to by @res with
270 * the resource data for this omap_device @od. Used by
271 * omap_device_build_ss() after calling omap_device_count_resources().
272 * Ideally this function would not be needed at all. If omap_device
273 * replaces platform_device, then we can specify our own
274 * get_resource()/ get_irq()/etc functions that use the underlying
275 * omap_hwmod information. Or if platform_device is extended to use
276 * subarchitecture-specific function pointers, the various
277 * platform_device functions can simply call omap_device internal
278 * functions to get device resources. Hacking around the existing
279 * platform_device code wastes memory. Returns 0.
281 int omap_device_fill_resources(struct omap_device *od, struct resource *res)
283 struct omap_hwmod *oh;
284 int c = 0;
285 int i, r;
287 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++) {
288 r = omap_hwmod_fill_resources(oh, res);
289 res += r;
290 c += r;
293 return 0;
297 * omap_device_build - build and register an omap_device with one omap_hwmod
298 * @pdev_name: name of the platform_device driver to use
299 * @pdev_id: this platform_device's connection ID
300 * @oh: ptr to the single omap_hwmod that backs this omap_device
301 * @pdata: platform_data ptr to associate with the platform_device
302 * @pdata_len: amount of memory pointed to by @pdata
303 * @pm_lats: pointer to a omap_device_pm_latency array for this device
304 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
306 * Convenience function for building and registering a single
307 * omap_device record, which in turn builds and registers a
308 * platform_device record. See omap_device_build_ss() for more
309 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
310 * passes along the return value of omap_device_build_ss().
312 struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
313 struct omap_hwmod *oh, void *pdata,
314 int pdata_len,
315 struct omap_device_pm_latency *pm_lats,
316 int pm_lats_cnt)
318 struct omap_hwmod *ohs[] = { oh };
320 if (!oh)
321 return ERR_PTR(-EINVAL);
323 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
324 pdata_len, pm_lats, pm_lats_cnt);
328 * omap_device_build_ss - build and register an omap_device with multiple hwmods
329 * @pdev_name: name of the platform_device driver to use
330 * @pdev_id: this platform_device's connection ID
331 * @oh: ptr to the single omap_hwmod that backs this omap_device
332 * @pdata: platform_data ptr to associate with the platform_device
333 * @pdata_len: amount of memory pointed to by @pdata
334 * @pm_lats: pointer to a omap_device_pm_latency array for this device
335 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
337 * Convenience function for building and registering an omap_device
338 * subsystem record. Subsystem records consist of multiple
339 * omap_hwmods. This function in turn builds and registers a
340 * platform_device record. Returns an ERR_PTR() on error, or passes
341 * along the return value of omap_device_register().
343 struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
344 struct omap_hwmod **ohs, int oh_cnt,
345 void *pdata, int pdata_len,
346 struct omap_device_pm_latency *pm_lats,
347 int pm_lats_cnt)
349 int ret = -ENOMEM;
350 struct omap_device *od;
351 char *pdev_name2;
352 struct resource *res = NULL;
353 int res_count;
354 struct omap_hwmod **hwmods;
356 if (!ohs || oh_cnt == 0 || !pdev_name)
357 return ERR_PTR(-EINVAL);
359 if (!pdata && pdata_len > 0)
360 return ERR_PTR(-EINVAL);
362 pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
363 oh_cnt);
365 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
366 if (!od)
367 return ERR_PTR(-ENOMEM);
369 od->hwmods_cnt = oh_cnt;
371 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
372 GFP_KERNEL);
373 if (!hwmods)
374 goto odbs_exit1;
376 memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
377 od->hwmods = hwmods;
379 pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
380 if (!pdev_name2)
381 goto odbs_exit2;
382 strcpy(pdev_name2, pdev_name);
384 od->pdev.name = pdev_name2;
385 od->pdev.id = pdev_id;
387 res_count = omap_device_count_resources(od);
388 if (res_count > 0) {
389 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
390 if (!res)
391 goto odbs_exit3;
393 omap_device_fill_resources(od, res);
395 od->pdev.num_resources = res_count;
396 od->pdev.resource = res;
398 platform_device_add_data(&od->pdev, pdata, pdata_len);
400 od->pm_lats = pm_lats;
401 od->pm_lats_cnt = pm_lats_cnt;
403 ret = omap_device_register(od);
404 if (ret)
405 goto odbs_exit4;
407 return od;
409 odbs_exit4:
410 kfree(res);
411 odbs_exit3:
412 kfree(pdev_name2);
413 odbs_exit2:
414 kfree(hwmods);
415 odbs_exit1:
416 kfree(od);
418 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
420 return ERR_PTR(ret);
424 * omap_device_register - register an omap_device with one omap_hwmod
425 * @od: struct omap_device * to register
427 * Register the omap_device structure. This currently just calls
428 * platform_device_register() on the underlying platform_device.
429 * Returns the return value of platform_device_register().
431 int omap_device_register(struct omap_device *od)
433 pr_debug("omap_device: %s: registering\n", od->pdev.name);
435 return platform_device_register(&od->pdev);
439 /* Public functions for use by device drivers through struct platform_data */
442 * omap_device_enable - fully activate an omap_device
443 * @od: struct omap_device * to activate
445 * Do whatever is necessary for the hwmods underlying omap_device @od
446 * to be accessible and ready to operate. This generally involves
447 * enabling clocks, setting SYSCONFIG registers; and in the future may
448 * involve remuxing pins. Device drivers should call this function
449 * (through platform_data function pointers) where they would normally
450 * enable clocks, etc. Returns -EINVAL if called when the omap_device
451 * is already enabled, or passes along the return value of
452 * _omap_device_activate().
454 int omap_device_enable(struct platform_device *pdev)
456 int ret;
457 struct omap_device *od;
459 od = _find_by_pdev(pdev);
461 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
462 WARN(1, "omap_device: %s.%d: omap_device_enable() called from "
463 "invalid state\n", od->pdev.name, od->pdev.id);
464 return -EINVAL;
467 /* Enable everything if we're enabling this device from scratch */
468 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
469 od->pm_lat_level = od->pm_lats_cnt;
471 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
473 od->dev_wakeup_lat = 0;
474 od->_dev_wakeup_lat_limit = INT_MAX;
475 od->_state = OMAP_DEVICE_STATE_ENABLED;
477 return ret;
481 * omap_device_idle - idle an omap_device
482 * @od: struct omap_device * to idle
484 * Idle omap_device @od by calling as many .deactivate_func() entries
485 * in the omap_device's pm_lats table as is possible without exceeding
486 * the device's maximum wakeup latency limit, pm_lat_limit. Device
487 * drivers should call this function (through platform_data function
488 * pointers) where they would normally disable clocks after operations
489 * complete, etc.. Returns -EINVAL if the omap_device is not
490 * currently enabled, or passes along the return value of
491 * _omap_device_deactivate().
493 int omap_device_idle(struct platform_device *pdev)
495 int ret;
496 struct omap_device *od;
498 od = _find_by_pdev(pdev);
500 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
501 WARN(1, "omap_device: %s.%d: omap_device_idle() called from "
502 "invalid state\n", od->pdev.name, od->pdev.id);
503 return -EINVAL;
506 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
508 od->_state = OMAP_DEVICE_STATE_IDLE;
510 return ret;
514 * omap_device_shutdown - shut down an omap_device
515 * @od: struct omap_device * to shut down
517 * Shut down omap_device @od by calling all .deactivate_func() entries
518 * in the omap_device's pm_lats table and then shutting down all of
519 * the underlying omap_hwmods. Used when a device is being "removed"
520 * or a device driver is being unloaded. Returns -EINVAL if the
521 * omap_device is not currently enabled or idle, or passes along the
522 * return value of _omap_device_deactivate().
524 int omap_device_shutdown(struct platform_device *pdev)
526 int ret, i;
527 struct omap_device *od;
528 struct omap_hwmod *oh;
530 od = _find_by_pdev(pdev);
532 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
533 od->_state != OMAP_DEVICE_STATE_IDLE) {
534 WARN(1, "omap_device: %s.%d: omap_device_shutdown() called "
535 "from invalid state\n", od->pdev.name, od->pdev.id);
536 return -EINVAL;
539 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
541 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
542 omap_hwmod_shutdown(oh);
544 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
546 return ret;
550 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
551 * @od: struct omap_device *
553 * When a device's maximum wakeup latency limit changes, call some of
554 * the .activate_func or .deactivate_func function pointers in the
555 * omap_device's pm_lats array to ensure that the device's maximum
556 * wakeup latency is less than or equal to the new latency limit.
557 * Intended to be called by OMAP PM code whenever a device's maximum
558 * wakeup latency limit changes (e.g., via
559 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
560 * done (e.g., if the omap_device is not currently idle, or if the
561 * wakeup latency is already current with the new limit) or passes
562 * along the return value of _omap_device_deactivate() or
563 * _omap_device_activate().
565 int omap_device_align_pm_lat(struct platform_device *pdev,
566 u32 new_wakeup_lat_limit)
568 int ret = -EINVAL;
569 struct omap_device *od;
571 od = _find_by_pdev(pdev);
573 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
574 return 0;
576 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
578 if (od->_state != OMAP_DEVICE_STATE_IDLE)
579 return 0;
580 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
581 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
582 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
583 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
585 return ret;
589 * omap_device_get_pwrdm - return the powerdomain * associated with @od
590 * @od: struct omap_device *
592 * Return the powerdomain associated with the first underlying
593 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
594 * code. Returns NULL on error or a struct powerdomain * upon
595 * success.
597 struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
600 * XXX Assumes that all omap_hwmod powerdomains are identical.
601 * This may not necessarily be true. There should be a sanity
602 * check in here to WARN() if any difference appears.
604 if (!od->hwmods_cnt)
605 return NULL;
607 return omap_hwmod_get_pwrdm(od->hwmods[0]);
611 * Public functions intended for use in omap_device_pm_latency
612 * .activate_func and .deactivate_func function pointers
616 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
617 * @od: struct omap_device *od
619 * Enable all underlying hwmods. Returns 0.
621 int omap_device_enable_hwmods(struct omap_device *od)
623 struct omap_hwmod *oh;
624 int i;
626 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
627 omap_hwmod_enable(oh);
629 /* XXX pass along return value here? */
630 return 0;
634 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
635 * @od: struct omap_device *od
637 * Idle all underlying hwmods. Returns 0.
639 int omap_device_idle_hwmods(struct omap_device *od)
641 struct omap_hwmod *oh;
642 int i;
644 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
645 omap_hwmod_idle(oh);
647 /* XXX pass along return value here? */
648 return 0;
652 * omap_device_disable_clocks - disable all main and interface clocks
653 * @od: struct omap_device *od
655 * Disable the main functional clock and interface clock for all of the
656 * omap_hwmods associated with the omap_device. Returns 0.
658 int omap_device_disable_clocks(struct omap_device *od)
660 struct omap_hwmod *oh;
661 int i;
663 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
664 omap_hwmod_disable_clocks(oh);
666 /* XXX pass along return value here? */
667 return 0;
671 * omap_device_enable_clocks - enable all main and interface clocks
672 * @od: struct omap_device *od
674 * Enable the main functional clock and interface clock for all of the
675 * omap_hwmods associated with the omap_device. Returns 0.
677 int omap_device_enable_clocks(struct omap_device *od)
679 struct omap_hwmod *oh;
680 int i;
682 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
683 omap_hwmod_enable_clocks(oh);
685 /* XXX pass along return value here? */
686 return 0;