blob: 8215b1bf40f81522a641b68d07e3b5ea7568375c [file] [log] [blame]
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001/*
2 * omap_device implementation
3 *
Paul Walmsley887adea2010-07-26 16:34:33 -06004 * Copyright (C) 2009-2010 Nokia Corporation
Paul Walmsley4788da22010-05-18 20:24:05 -06005 * Paul Walmsley, Kevin Hilman
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03006 *
7 * Developed in collaboration with (alphabetical order): Benoit
Paul Walmsley4788da22010-05-18 20:24:05 -06008 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03009 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090082#include <linux/slab.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030083#include <linux/err.h>
84#include <linux/io.h>
85
Tony Lindgrence491cf2009-10-20 09:40:47 -070086#include <plat/omap_device.h>
87#include <plat/omap_hwmod.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030088
89/* These parameters are passed to _omap_device_{de,}activate() */
90#define USE_WAKEUP_LAT 0
91#define IGNORE_WAKEUP_LAT 1
92
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030093/* Private functions */
94
95/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030096 * _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 */
109static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
110{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700111 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300112
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;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700117 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300118
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
Kevin Hilmand2292662009-12-08 16:34:23 -0700127 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300128
129 /* XXX check return code */
130 odpl->activate_func(od);
131
Kevin Hilmand2292662009-12-08 16:34:23 -0700132 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300133
Tony Lindgrenf0594292009-10-19 15:25:24 -0700134 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700135 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300136
137 pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700138 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
Tony Lindgrenf0594292009-10-19 15:25:24 -0700139 act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300140
Kevin Hilman9799aca2010-01-26 20:13:02 -0700141 if (act_lat > odpl->activate_lat) {
142 odpl->activate_lat_worst = act_lat;
143 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
144 odpl->activate_lat = act_lat;
145 pr_warning("omap_device: %s.%d: new worst case "
146 "activate latency %d: %llu\n",
147 od->pdev.name, od->pdev.id,
148 od->pm_lat_level, act_lat);
149 } else
150 pr_warning("omap_device: %s.%d: activate "
151 "latency %d higher than exptected. "
152 "(%llu > %d)\n",
153 od->pdev.name, od->pdev.id,
154 od->pm_lat_level, act_lat,
155 odpl->activate_lat);
156 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300157
158 od->dev_wakeup_lat -= odpl->activate_lat;
159 }
160
161 return 0;
162}
163
164/**
165 * _omap_device_deactivate - decrease device readiness
166 * @od: struct omap_device *
167 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
168 *
169 * Decrease readiness of omap_device @od (thus increasing device
170 * wakeup latency, but conserving power). If @ignore_lat is
171 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
172 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
173 * latency is less than the requested maximum wakeup latency, step
174 * forwards in the omap_device_pm_latency table to ensure the device's
175 * maximum wakeup latency is less than or equal to the requested
176 * maximum wakeup latency. Returns 0.
177 */
178static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
179{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700180 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300181
182 pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
183
184 while (od->pm_lat_level < od->pm_lats_cnt) {
185 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700186 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300187
188 odpl = od->pm_lats + od->pm_lat_level;
189
190 if (!ignore_lat &&
191 ((od->dev_wakeup_lat + odpl->activate_lat) >
192 od->_dev_wakeup_lat_limit))
193 break;
194
Kevin Hilmand2292662009-12-08 16:34:23 -0700195 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300196
197 /* XXX check return code */
198 odpl->deactivate_func(od);
199
Kevin Hilmand2292662009-12-08 16:34:23 -0700200 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300201
Tony Lindgrenf0594292009-10-19 15:25:24 -0700202 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700203 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300204
205 pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700206 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300207 deact_lat);
208
Kevin Hilman9799aca2010-01-26 20:13:02 -0700209 if (deact_lat > odpl->deactivate_lat) {
210 odpl->deactivate_lat_worst = deact_lat;
211 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
212 odpl->deactivate_lat = deact_lat;
213 pr_warning("omap_device: %s.%d: new worst case "
214 "deactivate latency %d: %llu\n",
215 od->pdev.name, od->pdev.id,
216 od->pm_lat_level, deact_lat);
217 } else
218 pr_warning("omap_device: %s.%d: deactivate "
219 "latency %d higher than exptected. "
220 "(%llu > %d)\n",
221 od->pdev.name, od->pdev.id,
222 od->pm_lat_level, deact_lat,
223 odpl->deactivate_lat);
224 }
225
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300226
227 od->dev_wakeup_lat += odpl->activate_lat;
228
229 od->pm_lat_level++;
230 }
231
232 return 0;
233}
234
235static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
236{
237 return container_of(pdev, struct omap_device, pdev);
238}
239
240
241/* Public functions for use by core code */
242
243/**
244 * omap_device_count_resources - count number of struct resource entries needed
245 * @od: struct omap_device *
246 *
247 * Count the number of struct resource entries needed for this
248 * omap_device @od. Used by omap_device_build_ss() to determine how
249 * much memory to allocate before calling
250 * omap_device_fill_resources(). Returns the count.
251 */
252int omap_device_count_resources(struct omap_device *od)
253{
254 struct omap_hwmod *oh;
255 int c = 0;
256 int i;
257
258 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
259 c += omap_hwmod_count_resources(oh);
260
261 pr_debug("omap_device: %s: counted %d total resources across %d "
262 "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
263
264 return c;
265}
266
267/**
268 * omap_device_fill_resources - fill in array of struct resource
269 * @od: struct omap_device *
270 * @res: pointer to an array of struct resource to be filled in
271 *
272 * Populate one or more empty struct resource pointed to by @res with
273 * the resource data for this omap_device @od. Used by
274 * omap_device_build_ss() after calling omap_device_count_resources().
275 * Ideally this function would not be needed at all. If omap_device
276 * replaces platform_device, then we can specify our own
277 * get_resource()/ get_irq()/etc functions that use the underlying
278 * omap_hwmod information. Or if platform_device is extended to use
279 * subarchitecture-specific function pointers, the various
280 * platform_device functions can simply call omap_device internal
281 * functions to get device resources. Hacking around the existing
282 * platform_device code wastes memory. Returns 0.
283 */
284int omap_device_fill_resources(struct omap_device *od, struct resource *res)
285{
286 struct omap_hwmod *oh;
287 int c = 0;
288 int i, r;
289
290 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++) {
291 r = omap_hwmod_fill_resources(oh, res);
292 res += r;
293 c += r;
294 }
295
296 return 0;
297}
298
299/**
300 * omap_device_build - build and register an omap_device with one omap_hwmod
301 * @pdev_name: name of the platform_device driver to use
302 * @pdev_id: this platform_device's connection ID
303 * @oh: ptr to the single omap_hwmod that backs this omap_device
304 * @pdata: platform_data ptr to associate with the platform_device
305 * @pdata_len: amount of memory pointed to by @pdata
306 * @pm_lats: pointer to a omap_device_pm_latency array for this device
307 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700308 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300309 *
310 * Convenience function for building and registering a single
311 * omap_device record, which in turn builds and registers a
312 * platform_device record. See omap_device_build_ss() for more
313 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
314 * passes along the return value of omap_device_build_ss().
315 */
316struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
317 struct omap_hwmod *oh, void *pdata,
318 int pdata_len,
319 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700320 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300321{
322 struct omap_hwmod *ohs[] = { oh };
323
324 if (!oh)
325 return ERR_PTR(-EINVAL);
326
327 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700328 pdata_len, pm_lats, pm_lats_cnt,
329 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300330}
331
332/**
333 * omap_device_build_ss - build and register an omap_device with multiple hwmods
334 * @pdev_name: name of the platform_device driver to use
335 * @pdev_id: this platform_device's connection ID
336 * @oh: ptr to the single omap_hwmod that backs this omap_device
337 * @pdata: platform_data ptr to associate with the platform_device
338 * @pdata_len: amount of memory pointed to by @pdata
339 * @pm_lats: pointer to a omap_device_pm_latency array for this device
340 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700341 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300342 *
343 * Convenience function for building and registering an omap_device
344 * subsystem record. Subsystem records consist of multiple
345 * omap_hwmods. This function in turn builds and registers a
346 * platform_device record. Returns an ERR_PTR() on error, or passes
347 * along the return value of omap_device_register().
348 */
349struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
350 struct omap_hwmod **ohs, int oh_cnt,
351 void *pdata, int pdata_len,
352 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700353 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300354{
355 int ret = -ENOMEM;
356 struct omap_device *od;
357 char *pdev_name2;
358 struct resource *res = NULL;
Kevin Hilman06563582010-07-26 16:34:30 -0600359 int i, res_count;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300360 struct omap_hwmod **hwmods;
361
362 if (!ohs || oh_cnt == 0 || !pdev_name)
363 return ERR_PTR(-EINVAL);
364
365 if (!pdata && pdata_len > 0)
366 return ERR_PTR(-EINVAL);
367
368 pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
369 oh_cnt);
370
371 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
372 if (!od)
373 return ERR_PTR(-ENOMEM);
374
375 od->hwmods_cnt = oh_cnt;
376
377 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
378 GFP_KERNEL);
379 if (!hwmods)
380 goto odbs_exit1;
381
382 memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
383 od->hwmods = hwmods;
384
385 pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
386 if (!pdev_name2)
387 goto odbs_exit2;
388 strcpy(pdev_name2, pdev_name);
389
390 od->pdev.name = pdev_name2;
391 od->pdev.id = pdev_id;
392
393 res_count = omap_device_count_resources(od);
394 if (res_count > 0) {
395 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
396 if (!res)
397 goto odbs_exit3;
398 }
399 omap_device_fill_resources(od, res);
400
401 od->pdev.num_resources = res_count;
402 od->pdev.resource = res;
403
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000404 ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
405 if (ret)
406 goto odbs_exit4;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300407
408 od->pm_lats = pm_lats;
409 od->pm_lats_cnt = pm_lats_cnt;
410
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700411 if (is_early_device)
412 ret = omap_early_device_register(od);
413 else
414 ret = omap_device_register(od);
415
Kevin Hilman06563582010-07-26 16:34:30 -0600416 for (i = 0; i < oh_cnt; i++)
417 hwmods[i]->od = od;
418
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300419 if (ret)
420 goto odbs_exit4;
421
422 return od;
423
424odbs_exit4:
425 kfree(res);
426odbs_exit3:
427 kfree(pdev_name2);
428odbs_exit2:
429 kfree(hwmods);
430odbs_exit1:
431 kfree(od);
432
433 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
434
435 return ERR_PTR(ret);
436}
437
438/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700439 * omap_early_device_register - register an omap_device as an early platform
440 * device.
441 * @od: struct omap_device * to register
442 *
443 * Register the omap_device structure. This currently just calls
444 * platform_early_add_device() on the underlying platform_device.
445 * Returns 0 by default.
446 */
447int omap_early_device_register(struct omap_device *od)
448{
449 struct platform_device *devices[1];
450
451 devices[0] = &(od->pdev);
452 early_platform_add_devices(devices, 1);
453 return 0;
454}
455
456/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300457 * omap_device_register - register an omap_device with one omap_hwmod
458 * @od: struct omap_device * to register
459 *
460 * Register the omap_device structure. This currently just calls
461 * platform_device_register() on the underlying platform_device.
462 * Returns the return value of platform_device_register().
463 */
464int omap_device_register(struct omap_device *od)
465{
466 pr_debug("omap_device: %s: registering\n", od->pdev.name);
467
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700468 od->pdev.dev.parent = &omap_device_parent;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300469 return platform_device_register(&od->pdev);
470}
471
472
473/* Public functions for use by device drivers through struct platform_data */
474
475/**
476 * omap_device_enable - fully activate an omap_device
477 * @od: struct omap_device * to activate
478 *
479 * Do whatever is necessary for the hwmods underlying omap_device @od
480 * to be accessible and ready to operate. This generally involves
481 * enabling clocks, setting SYSCONFIG registers; and in the future may
482 * involve remuxing pins. Device drivers should call this function
483 * (through platform_data function pointers) where they would normally
484 * enable clocks, etc. Returns -EINVAL if called when the omap_device
485 * is already enabled, or passes along the return value of
486 * _omap_device_activate().
487 */
488int omap_device_enable(struct platform_device *pdev)
489{
490 int ret;
491 struct omap_device *od;
492
493 od = _find_by_pdev(pdev);
494
495 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700496 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
497 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300498 return -EINVAL;
499 }
500
501 /* Enable everything if we're enabling this device from scratch */
502 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
503 od->pm_lat_level = od->pm_lats_cnt;
504
505 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
506
507 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700508 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300509 od->_state = OMAP_DEVICE_STATE_ENABLED;
510
511 return ret;
512}
513
514/**
515 * omap_device_idle - idle an omap_device
516 * @od: struct omap_device * to idle
517 *
518 * Idle omap_device @od by calling as many .deactivate_func() entries
519 * in the omap_device's pm_lats table as is possible without exceeding
520 * the device's maximum wakeup latency limit, pm_lat_limit. Device
521 * drivers should call this function (through platform_data function
522 * pointers) where they would normally disable clocks after operations
523 * complete, etc.. Returns -EINVAL if the omap_device is not
524 * currently enabled, or passes along the return value of
525 * _omap_device_deactivate().
526 */
527int omap_device_idle(struct platform_device *pdev)
528{
529 int ret;
530 struct omap_device *od;
531
532 od = _find_by_pdev(pdev);
533
534 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700535 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
536 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300537 return -EINVAL;
538 }
539
540 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
541
542 od->_state = OMAP_DEVICE_STATE_IDLE;
543
544 return ret;
545}
546
547/**
548 * omap_device_shutdown - shut down an omap_device
549 * @od: struct omap_device * to shut down
550 *
551 * Shut down omap_device @od by calling all .deactivate_func() entries
552 * in the omap_device's pm_lats table and then shutting down all of
553 * the underlying omap_hwmods. Used when a device is being "removed"
554 * or a device driver is being unloaded. Returns -EINVAL if the
555 * omap_device is not currently enabled or idle, or passes along the
556 * return value of _omap_device_deactivate().
557 */
558int omap_device_shutdown(struct platform_device *pdev)
559{
560 int ret, i;
561 struct omap_device *od;
562 struct omap_hwmod *oh;
563
564 od = _find_by_pdev(pdev);
565
566 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
567 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700568 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
569 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300570 return -EINVAL;
571 }
572
573 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
574
575 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
576 omap_hwmod_shutdown(oh);
577
578 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
579
580 return ret;
581}
582
583/**
584 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
585 * @od: struct omap_device *
586 *
587 * When a device's maximum wakeup latency limit changes, call some of
588 * the .activate_func or .deactivate_func function pointers in the
589 * omap_device's pm_lats array to ensure that the device's maximum
590 * wakeup latency is less than or equal to the new latency limit.
591 * Intended to be called by OMAP PM code whenever a device's maximum
592 * wakeup latency limit changes (e.g., via
593 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
594 * done (e.g., if the omap_device is not currently idle, or if the
595 * wakeup latency is already current with the new limit) or passes
596 * along the return value of _omap_device_deactivate() or
597 * _omap_device_activate().
598 */
599int omap_device_align_pm_lat(struct platform_device *pdev,
600 u32 new_wakeup_lat_limit)
601{
602 int ret = -EINVAL;
603 struct omap_device *od;
604
605 od = _find_by_pdev(pdev);
606
607 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
608 return 0;
609
610 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
611
612 if (od->_state != OMAP_DEVICE_STATE_IDLE)
613 return 0;
614 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
615 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
616 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
617 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
618
619 return ret;
620}
621
622/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300623 * omap_device_get_pwrdm - return the powerdomain * associated with @od
624 * @od: struct omap_device *
625 *
626 * Return the powerdomain associated with the first underlying
627 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
628 * code. Returns NULL on error or a struct powerdomain * upon
629 * success.
630 */
631struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
632{
633 /*
634 * XXX Assumes that all omap_hwmod powerdomains are identical.
635 * This may not necessarily be true. There should be a sanity
636 * check in here to WARN() if any difference appears.
637 */
638 if (!od->hwmods_cnt)
639 return NULL;
640
641 return omap_hwmod_get_pwrdm(od->hwmods[0]);
642}
643
Paul Walmsleydb2a60b2010-07-26 16:34:33 -0600644/**
645 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
646 * @od: struct omap_device *
647 *
648 * Return the MPU's virtual address for the base of the hwmod, from
649 * the ioremap() that the hwmod code does. Only valid if there is one
650 * hwmod associated with this device. Returns NULL if there are zero
651 * or more than one hwmods associated with this omap_device;
652 * otherwise, passes along the return value from
653 * omap_hwmod_get_mpu_rt_va().
654 */
655void __iomem *omap_device_get_rt_va(struct omap_device *od)
656{
657 if (od->hwmods_cnt != 1)
658 return NULL;
659
660 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
661}
662
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300663/*
664 * Public functions intended for use in omap_device_pm_latency
665 * .activate_func and .deactivate_func function pointers
666 */
667
668/**
669 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
670 * @od: struct omap_device *od
671 *
672 * Enable all underlying hwmods. Returns 0.
673 */
674int omap_device_enable_hwmods(struct omap_device *od)
675{
676 struct omap_hwmod *oh;
677 int i;
678
679 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
680 omap_hwmod_enable(oh);
681
682 /* XXX pass along return value here? */
683 return 0;
684}
685
686/**
687 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
688 * @od: struct omap_device *od
689 *
690 * Idle all underlying hwmods. Returns 0.
691 */
692int omap_device_idle_hwmods(struct omap_device *od)
693{
694 struct omap_hwmod *oh;
695 int i;
696
697 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
698 omap_hwmod_idle(oh);
699
700 /* XXX pass along return value here? */
701 return 0;
702}
703
704/**
705 * omap_device_disable_clocks - disable all main and interface clocks
706 * @od: struct omap_device *od
707 *
708 * Disable the main functional clock and interface clock for all of the
709 * omap_hwmods associated with the omap_device. Returns 0.
710 */
711int omap_device_disable_clocks(struct omap_device *od)
712{
713 struct omap_hwmod *oh;
714 int i;
715
716 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
717 omap_hwmod_disable_clocks(oh);
718
719 /* XXX pass along return value here? */
720 return 0;
721}
722
723/**
724 * omap_device_enable_clocks - enable all main and interface clocks
725 * @od: struct omap_device *od
726 *
727 * Enable the main functional clock and interface clock for all of the
728 * omap_hwmods associated with the omap_device. Returns 0.
729 */
730int omap_device_enable_clocks(struct omap_device *od)
731{
732 struct omap_hwmod *oh;
733 int i;
734
735 for (i = 0, oh = *od->hwmods; i < od->hwmods_cnt; i++, oh++)
736 omap_hwmod_enable_clocks(oh);
737
738 /* XXX pass along return value here? */
739 return 0;
740}
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700741
742struct device omap_device_parent = {
743 .init_name = "omap",
744 .parent = &platform_bus,
745};
746
747static int __init omap_device_init(void)
748{
749 return device_register(&omap_device_parent);
750}
751core_initcall(omap_device_init);