blob: abe933cd8f0926c99b059d2a214bda900e5326eb [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>
Partha Basak4ef7aca2010-09-21 19:23:04 +020085#include <linux/clk.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030086
Tony Lindgrence491cf2009-10-20 09:40:47 -070087#include <plat/omap_device.h>
88#include <plat/omap_hwmod.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030089
90/* These parameters are passed to _omap_device_{de,}activate() */
91#define USE_WAKEUP_LAT 0
92#define IGNORE_WAKEUP_LAT 1
93
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030094/* Private functions */
95
96/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030097 * _omap_device_activate - increase device readiness
98 * @od: struct omap_device *
99 * @ignore_lat: increase to latency target (0) or full readiness (1)?
100 *
101 * Increase readiness of omap_device @od (thus decreasing device
102 * wakeup latency, but consuming more power). If @ignore_lat is
103 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
104 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
105 * latency is greater than the requested maximum wakeup latency, step
106 * backwards in the omap_device_pm_latency table to ensure the
107 * device's maximum wakeup latency is less than or equal to the
108 * requested maximum wakeup latency. Returns 0.
109 */
110static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
111{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700112 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300113
114 pr_debug("omap_device: %s: activating\n", od->pdev.name);
115
116 while (od->pm_lat_level > 0) {
117 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700118 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300119
120 od->pm_lat_level--;
121
122 odpl = od->pm_lats + od->pm_lat_level;
123
124 if (!ignore_lat &&
125 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
126 break;
127
Kevin Hilmand2292662009-12-08 16:34:23 -0700128 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300129
130 /* XXX check return code */
131 odpl->activate_func(od);
132
Kevin Hilmand2292662009-12-08 16:34:23 -0700133 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300134
Tony Lindgrenf0594292009-10-19 15:25:24 -0700135 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700136 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300137
138 pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700139 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
Tony Lindgrenf0594292009-10-19 15:25:24 -0700140 act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300141
Kevin Hilman9799aca2010-01-26 20:13:02 -0700142 if (act_lat > odpl->activate_lat) {
143 odpl->activate_lat_worst = act_lat;
144 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
145 odpl->activate_lat = act_lat;
146 pr_warning("omap_device: %s.%d: new worst case "
147 "activate latency %d: %llu\n",
148 od->pdev.name, od->pdev.id,
149 od->pm_lat_level, act_lat);
150 } else
151 pr_warning("omap_device: %s.%d: activate "
152 "latency %d higher than exptected. "
153 "(%llu > %d)\n",
154 od->pdev.name, od->pdev.id,
155 od->pm_lat_level, act_lat,
156 odpl->activate_lat);
157 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300158
159 od->dev_wakeup_lat -= odpl->activate_lat;
160 }
161
162 return 0;
163}
164
165/**
166 * _omap_device_deactivate - decrease device readiness
167 * @od: struct omap_device *
168 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
169 *
170 * Decrease readiness of omap_device @od (thus increasing device
171 * wakeup latency, but conserving power). If @ignore_lat is
172 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
173 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
174 * latency is less than the requested maximum wakeup latency, step
175 * forwards in the omap_device_pm_latency table to ensure the device's
176 * maximum wakeup latency is less than or equal to the requested
177 * maximum wakeup latency. Returns 0.
178 */
179static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
180{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700181 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300182
183 pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
184
185 while (od->pm_lat_level < od->pm_lats_cnt) {
186 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700187 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300188
189 odpl = od->pm_lats + od->pm_lat_level;
190
191 if (!ignore_lat &&
192 ((od->dev_wakeup_lat + odpl->activate_lat) >
193 od->_dev_wakeup_lat_limit))
194 break;
195
Kevin Hilmand2292662009-12-08 16:34:23 -0700196 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300197
198 /* XXX check return code */
199 odpl->deactivate_func(od);
200
Kevin Hilmand2292662009-12-08 16:34:23 -0700201 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300202
Tony Lindgrenf0594292009-10-19 15:25:24 -0700203 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700204 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300205
206 pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700207 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300208 deact_lat);
209
Kevin Hilman9799aca2010-01-26 20:13:02 -0700210 if (deact_lat > odpl->deactivate_lat) {
211 odpl->deactivate_lat_worst = deact_lat;
212 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
213 odpl->deactivate_lat = deact_lat;
214 pr_warning("omap_device: %s.%d: new worst case "
215 "deactivate latency %d: %llu\n",
216 od->pdev.name, od->pdev.id,
217 od->pm_lat_level, deact_lat);
218 } else
219 pr_warning("omap_device: %s.%d: deactivate "
220 "latency %d higher than exptected. "
221 "(%llu > %d)\n",
222 od->pdev.name, od->pdev.id,
223 od->pm_lat_level, deact_lat,
224 odpl->deactivate_lat);
225 }
226
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300227
228 od->dev_wakeup_lat += odpl->activate_lat;
229
230 od->pm_lat_level++;
231 }
232
233 return 0;
234}
235
236static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
237{
238 return container_of(pdev, struct omap_device, pdev);
239}
240
Partha Basak4ef7aca2010-09-21 19:23:04 +0200241/**
242 * _add_optional_clock_alias - Add clock alias for hwmod optional clocks
243 * @od: struct omap_device *od
244 *
245 * For every optional clock present per hwmod per omap_device, this function
246 * adds an entry in the clocks list of the form <dev-id=dev_name, con-id=role>
247 * if an entry is already present in it with the form <dev-id=NULL, con-id=role>
248 *
249 * The function is called from inside omap_device_build_ss(), after
250 * omap_device_register.
251 *
252 * This allows drivers to get a pointer to its optional clocks based on its role
253 * by calling clk_get(<dev*>, <role>).
254 *
255 * No return value.
256 */
257static void _add_optional_clock_alias(struct omap_device *od,
258 struct omap_hwmod *oh)
259{
260 int i;
261
262 for (i = 0; i < oh->opt_clks_cnt; i++) {
263 struct omap_hwmod_opt_clk *oc;
264 int r;
265
266 oc = &oh->opt_clks[i];
267
268 if (!oc->_clk)
269 continue;
270
271 r = clk_add_alias(oc->role, dev_name(&od->pdev.dev),
272 (char *)oc->clk, &od->pdev.dev);
273 if (r)
274 pr_err("omap_device: %s: clk_add_alias for %s failed\n",
275 dev_name(&od->pdev.dev), oc->role);
276 }
277}
278
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300279
280/* Public functions for use by core code */
281
282/**
283 * omap_device_count_resources - count number of struct resource entries needed
284 * @od: struct omap_device *
285 *
286 * Count the number of struct resource entries needed for this
287 * omap_device @od. Used by omap_device_build_ss() to determine how
288 * much memory to allocate before calling
289 * omap_device_fill_resources(). Returns the count.
290 */
291int omap_device_count_resources(struct omap_device *od)
292{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300293 int c = 0;
294 int i;
295
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600296 for (i = 0; i < od->hwmods_cnt; i++)
297 c += omap_hwmod_count_resources(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300298
299 pr_debug("omap_device: %s: counted %d total resources across %d "
300 "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
301
302 return c;
303}
304
305/**
306 * omap_device_fill_resources - fill in array of struct resource
307 * @od: struct omap_device *
308 * @res: pointer to an array of struct resource to be filled in
309 *
310 * Populate one or more empty struct resource pointed to by @res with
311 * the resource data for this omap_device @od. Used by
312 * omap_device_build_ss() after calling omap_device_count_resources().
313 * Ideally this function would not be needed at all. If omap_device
314 * replaces platform_device, then we can specify our own
315 * get_resource()/ get_irq()/etc functions that use the underlying
316 * omap_hwmod information. Or if platform_device is extended to use
317 * subarchitecture-specific function pointers, the various
318 * platform_device functions can simply call omap_device internal
319 * functions to get device resources. Hacking around the existing
320 * platform_device code wastes memory. Returns 0.
321 */
322int omap_device_fill_resources(struct omap_device *od, struct resource *res)
323{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300324 int c = 0;
325 int i, r;
326
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600327 for (i = 0; i < od->hwmods_cnt; i++) {
328 r = omap_hwmod_fill_resources(od->hwmods[i], res);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300329 res += r;
330 c += r;
331 }
332
333 return 0;
334}
335
336/**
337 * omap_device_build - build and register an omap_device with one omap_hwmod
338 * @pdev_name: name of the platform_device driver to use
339 * @pdev_id: this platform_device's connection ID
340 * @oh: ptr to the single omap_hwmod that backs this omap_device
341 * @pdata: platform_data ptr to associate with the platform_device
342 * @pdata_len: amount of memory pointed to by @pdata
343 * @pm_lats: pointer to a omap_device_pm_latency array for this device
344 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700345 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300346 *
347 * Convenience function for building and registering a single
348 * omap_device record, which in turn builds and registers a
349 * platform_device record. See omap_device_build_ss() for more
350 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
351 * passes along the return value of omap_device_build_ss().
352 */
353struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
354 struct omap_hwmod *oh, void *pdata,
355 int pdata_len,
356 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700357 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300358{
359 struct omap_hwmod *ohs[] = { oh };
360
361 if (!oh)
362 return ERR_PTR(-EINVAL);
363
364 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700365 pdata_len, pm_lats, pm_lats_cnt,
366 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300367}
368
369/**
370 * omap_device_build_ss - build and register an omap_device with multiple hwmods
371 * @pdev_name: name of the platform_device driver to use
372 * @pdev_id: this platform_device's connection ID
373 * @oh: ptr to the single omap_hwmod that backs this omap_device
374 * @pdata: platform_data ptr to associate with the platform_device
375 * @pdata_len: amount of memory pointed to by @pdata
376 * @pm_lats: pointer to a omap_device_pm_latency array for this device
377 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700378 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300379 *
380 * Convenience function for building and registering an omap_device
381 * subsystem record. Subsystem records consist of multiple
382 * omap_hwmods. This function in turn builds and registers a
383 * platform_device record. Returns an ERR_PTR() on error, or passes
384 * along the return value of omap_device_register().
385 */
386struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
387 struct omap_hwmod **ohs, int oh_cnt,
388 void *pdata, int pdata_len,
389 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700390 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300391{
392 int ret = -ENOMEM;
393 struct omap_device *od;
394 char *pdev_name2;
395 struct resource *res = NULL;
Kevin Hilman06563582010-07-26 16:34:30 -0600396 int i, res_count;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300397 struct omap_hwmod **hwmods;
398
399 if (!ohs || oh_cnt == 0 || !pdev_name)
400 return ERR_PTR(-EINVAL);
401
402 if (!pdata && pdata_len > 0)
403 return ERR_PTR(-EINVAL);
404
405 pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
406 oh_cnt);
407
408 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
409 if (!od)
410 return ERR_PTR(-ENOMEM);
411
412 od->hwmods_cnt = oh_cnt;
413
414 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
415 GFP_KERNEL);
416 if (!hwmods)
417 goto odbs_exit1;
418
419 memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
420 od->hwmods = hwmods;
421
422 pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
423 if (!pdev_name2)
424 goto odbs_exit2;
425 strcpy(pdev_name2, pdev_name);
426
427 od->pdev.name = pdev_name2;
428 od->pdev.id = pdev_id;
429
430 res_count = omap_device_count_resources(od);
431 if (res_count > 0) {
432 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
433 if (!res)
434 goto odbs_exit3;
435 }
436 omap_device_fill_resources(od, res);
437
438 od->pdev.num_resources = res_count;
439 od->pdev.resource = res;
440
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000441 ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
442 if (ret)
443 goto odbs_exit4;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300444
445 od->pm_lats = pm_lats;
446 od->pm_lats_cnt = pm_lats_cnt;
447
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700448 if (is_early_device)
449 ret = omap_early_device_register(od);
450 else
451 ret = omap_device_register(od);
452
Partha Basak4ef7aca2010-09-21 19:23:04 +0200453 for (i = 0; i < oh_cnt; i++) {
Kevin Hilman06563582010-07-26 16:34:30 -0600454 hwmods[i]->od = od;
Partha Basak4ef7aca2010-09-21 19:23:04 +0200455 _add_optional_clock_alias(od, hwmods[i]);
456 }
Kevin Hilman06563582010-07-26 16:34:30 -0600457
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300458 if (ret)
459 goto odbs_exit4;
460
461 return od;
462
463odbs_exit4:
464 kfree(res);
465odbs_exit3:
466 kfree(pdev_name2);
467odbs_exit2:
468 kfree(hwmods);
469odbs_exit1:
470 kfree(od);
471
472 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
473
474 return ERR_PTR(ret);
475}
476
477/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700478 * omap_early_device_register - register an omap_device as an early platform
479 * device.
480 * @od: struct omap_device * to register
481 *
482 * Register the omap_device structure. This currently just calls
483 * platform_early_add_device() on the underlying platform_device.
484 * Returns 0 by default.
485 */
486int omap_early_device_register(struct omap_device *od)
487{
488 struct platform_device *devices[1];
489
490 devices[0] = &(od->pdev);
491 early_platform_add_devices(devices, 1);
492 return 0;
493}
494
495/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300496 * omap_device_register - register an omap_device with one omap_hwmod
497 * @od: struct omap_device * to register
498 *
499 * Register the omap_device structure. This currently just calls
500 * platform_device_register() on the underlying platform_device.
501 * Returns the return value of platform_device_register().
502 */
503int omap_device_register(struct omap_device *od)
504{
505 pr_debug("omap_device: %s: registering\n", od->pdev.name);
506
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700507 od->pdev.dev.parent = &omap_device_parent;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300508 return platform_device_register(&od->pdev);
509}
510
511
512/* Public functions for use by device drivers through struct platform_data */
513
514/**
515 * omap_device_enable - fully activate an omap_device
516 * @od: struct omap_device * to activate
517 *
518 * Do whatever is necessary for the hwmods underlying omap_device @od
519 * to be accessible and ready to operate. This generally involves
520 * enabling clocks, setting SYSCONFIG registers; and in the future may
521 * involve remuxing pins. Device drivers should call this function
522 * (through platform_data function pointers) where they would normally
523 * enable clocks, etc. Returns -EINVAL if called when the omap_device
524 * is already enabled, or passes along the return value of
525 * _omap_device_activate().
526 */
527int omap_device_enable(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 /* Enable everything if we're enabling this device from scratch */
541 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
542 od->pm_lat_level = od->pm_lats_cnt;
543
544 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
545
546 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700547 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300548 od->_state = OMAP_DEVICE_STATE_ENABLED;
549
550 return ret;
551}
552
553/**
554 * omap_device_idle - idle an omap_device
555 * @od: struct omap_device * to idle
556 *
557 * Idle omap_device @od by calling as many .deactivate_func() entries
558 * in the omap_device's pm_lats table as is possible without exceeding
559 * the device's maximum wakeup latency limit, pm_lat_limit. Device
560 * drivers should call this function (through platform_data function
561 * pointers) where they would normally disable clocks after operations
562 * complete, etc.. Returns -EINVAL if the omap_device is not
563 * currently enabled, or passes along the return value of
564 * _omap_device_deactivate().
565 */
566int omap_device_idle(struct platform_device *pdev)
567{
568 int ret;
569 struct omap_device *od;
570
571 od = _find_by_pdev(pdev);
572
573 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700574 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
575 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300576 return -EINVAL;
577 }
578
579 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
580
581 od->_state = OMAP_DEVICE_STATE_IDLE;
582
583 return ret;
584}
585
586/**
587 * omap_device_shutdown - shut down an omap_device
588 * @od: struct omap_device * to shut down
589 *
590 * Shut down omap_device @od by calling all .deactivate_func() entries
591 * in the omap_device's pm_lats table and then shutting down all of
592 * the underlying omap_hwmods. Used when a device is being "removed"
593 * or a device driver is being unloaded. Returns -EINVAL if the
594 * omap_device is not currently enabled or idle, or passes along the
595 * return value of _omap_device_deactivate().
596 */
597int omap_device_shutdown(struct platform_device *pdev)
598{
599 int ret, i;
600 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300601
602 od = _find_by_pdev(pdev);
603
604 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
605 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700606 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
607 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300608 return -EINVAL;
609 }
610
611 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
612
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600613 for (i = 0; i < od->hwmods_cnt; i++)
614 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300615
616 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
617
618 return ret;
619}
620
621/**
622 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
623 * @od: struct omap_device *
624 *
625 * When a device's maximum wakeup latency limit changes, call some of
626 * the .activate_func or .deactivate_func function pointers in the
627 * omap_device's pm_lats array to ensure that the device's maximum
628 * wakeup latency is less than or equal to the new latency limit.
629 * Intended to be called by OMAP PM code whenever a device's maximum
630 * wakeup latency limit changes (e.g., via
631 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
632 * done (e.g., if the omap_device is not currently idle, or if the
633 * wakeup latency is already current with the new limit) or passes
634 * along the return value of _omap_device_deactivate() or
635 * _omap_device_activate().
636 */
637int omap_device_align_pm_lat(struct platform_device *pdev,
638 u32 new_wakeup_lat_limit)
639{
640 int ret = -EINVAL;
641 struct omap_device *od;
642
643 od = _find_by_pdev(pdev);
644
645 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
646 return 0;
647
648 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
649
650 if (od->_state != OMAP_DEVICE_STATE_IDLE)
651 return 0;
652 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
653 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
654 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
655 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
656
657 return ret;
658}
659
660/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300661 * omap_device_get_pwrdm - return the powerdomain * associated with @od
662 * @od: struct omap_device *
663 *
664 * Return the powerdomain associated with the first underlying
665 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
666 * code. Returns NULL on error or a struct powerdomain * upon
667 * success.
668 */
669struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
670{
671 /*
672 * XXX Assumes that all omap_hwmod powerdomains are identical.
673 * This may not necessarily be true. There should be a sanity
674 * check in here to WARN() if any difference appears.
675 */
676 if (!od->hwmods_cnt)
677 return NULL;
678
679 return omap_hwmod_get_pwrdm(od->hwmods[0]);
680}
681
Paul Walmsleydb2a60b2010-07-26 16:34:33 -0600682/**
683 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
684 * @od: struct omap_device *
685 *
686 * Return the MPU's virtual address for the base of the hwmod, from
687 * the ioremap() that the hwmod code does. Only valid if there is one
688 * hwmod associated with this device. Returns NULL if there are zero
689 * or more than one hwmods associated with this omap_device;
690 * otherwise, passes along the return value from
691 * omap_hwmod_get_mpu_rt_va().
692 */
693void __iomem *omap_device_get_rt_va(struct omap_device *od)
694{
695 if (od->hwmods_cnt != 1)
696 return NULL;
697
698 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
699}
700
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300701/*
702 * Public functions intended for use in omap_device_pm_latency
703 * .activate_func and .deactivate_func function pointers
704 */
705
706/**
707 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
708 * @od: struct omap_device *od
709 *
710 * Enable all underlying hwmods. Returns 0.
711 */
712int omap_device_enable_hwmods(struct omap_device *od)
713{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300714 int i;
715
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600716 for (i = 0; i < od->hwmods_cnt; i++)
717 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300718
719 /* XXX pass along return value here? */
720 return 0;
721}
722
723/**
724 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
725 * @od: struct omap_device *od
726 *
727 * Idle all underlying hwmods. Returns 0.
728 */
729int omap_device_idle_hwmods(struct omap_device *od)
730{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300731 int i;
732
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600733 for (i = 0; i < od->hwmods_cnt; i++)
734 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300735
736 /* XXX pass along return value here? */
737 return 0;
738}
739
740/**
741 * omap_device_disable_clocks - disable all main and interface clocks
742 * @od: struct omap_device *od
743 *
744 * Disable the main functional clock and interface clock for all of the
745 * omap_hwmods associated with the omap_device. Returns 0.
746 */
747int omap_device_disable_clocks(struct omap_device *od)
748{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300749 int i;
750
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600751 for (i = 0; i < od->hwmods_cnt; i++)
752 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300753
754 /* XXX pass along return value here? */
755 return 0;
756}
757
758/**
759 * omap_device_enable_clocks - enable all main and interface clocks
760 * @od: struct omap_device *od
761 *
762 * Enable the main functional clock and interface clock for all of the
763 * omap_hwmods associated with the omap_device. Returns 0.
764 */
765int omap_device_enable_clocks(struct omap_device *od)
766{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300767 int i;
768
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600769 for (i = 0; i < od->hwmods_cnt; i++)
770 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300771
772 /* XXX pass along return value here? */
773 return 0;
774}
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700775
776struct device omap_device_parent = {
777 .init_name = "omap",
778 .parent = &platform_bus,
779};
780
781static int __init omap_device_init(void)
782{
783 return device_register(&omap_device_parent);
784}
785core_initcall(omap_device_init);