blob: 533ad13d0d9fbbf93752c002abf9d82fcbe2e2a7 [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 Walmsley887adea2010-07-26 16:34:33 -060094/*
95 * OMAP_DEVICE_MAGIC: used to determine whether a struct omap_device
96 * obtained via container_of() is in fact a struct omap_device
97 */
98#define OMAP_DEVICE_MAGIC 0xf00dcafe
Kevin Hilman00071222010-02-24 12:05:45 -070099
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300100/* Private functions */
101
102/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300103 * _omap_device_activate - increase device readiness
104 * @od: struct omap_device *
105 * @ignore_lat: increase to latency target (0) or full readiness (1)?
106 *
107 * Increase readiness of omap_device @od (thus decreasing device
108 * wakeup latency, but consuming more power). If @ignore_lat is
109 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
110 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
111 * latency is greater than the requested maximum wakeup latency, step
112 * backwards in the omap_device_pm_latency table to ensure the
113 * device's maximum wakeup latency is less than or equal to the
114 * requested maximum wakeup latency. Returns 0.
115 */
116static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
117{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700118 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300119
120 pr_debug("omap_device: %s: activating\n", od->pdev.name);
121
122 while (od->pm_lat_level > 0) {
123 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700124 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300125
126 od->pm_lat_level--;
127
128 odpl = od->pm_lats + od->pm_lat_level;
129
130 if (!ignore_lat &&
131 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
132 break;
133
Kevin Hilmand2292662009-12-08 16:34:23 -0700134 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300135
136 /* XXX check return code */
137 odpl->activate_func(od);
138
Kevin Hilmand2292662009-12-08 16:34:23 -0700139 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300140
Tony Lindgrenf0594292009-10-19 15:25:24 -0700141 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700142 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300143
144 pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time "
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700145 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
Tony Lindgrenf0594292009-10-19 15:25:24 -0700146 act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300147
Kevin Hilman9799aca2010-01-26 20:13:02 -0700148 if (act_lat > odpl->activate_lat) {
149 odpl->activate_lat_worst = act_lat;
150 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
151 odpl->activate_lat = act_lat;
152 pr_warning("omap_device: %s.%d: new worst case "
153 "activate latency %d: %llu\n",
154 od->pdev.name, od->pdev.id,
155 od->pm_lat_level, act_lat);
156 } else
157 pr_warning("omap_device: %s.%d: activate "
158 "latency %d higher than exptected. "
159 "(%llu > %d)\n",
160 od->pdev.name, od->pdev.id,
161 od->pm_lat_level, act_lat,
162 odpl->activate_lat);
163 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300164
165 od->dev_wakeup_lat -= odpl->activate_lat;
166 }
167
168 return 0;
169}
170
171/**
172 * _omap_device_deactivate - decrease device readiness
173 * @od: struct omap_device *
174 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
175 *
176 * Decrease readiness of omap_device @od (thus increasing device
177 * wakeup latency, but conserving power). If @ignore_lat is
178 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
179 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
180 * latency is less than the requested maximum wakeup latency, step
181 * forwards in the omap_device_pm_latency table to ensure the device's
182 * maximum wakeup latency is less than or equal to the requested
183 * maximum wakeup latency. Returns 0.
184 */
185static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
186{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700187 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300188
189 pr_debug("omap_device: %s: deactivating\n", od->pdev.name);
190
191 while (od->pm_lat_level < od->pm_lats_cnt) {
192 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700193 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300194
195 odpl = od->pm_lats + od->pm_lat_level;
196
197 if (!ignore_lat &&
198 ((od->dev_wakeup_lat + odpl->activate_lat) >
199 od->_dev_wakeup_lat_limit))
200 break;
201
Kevin Hilmand2292662009-12-08 16:34:23 -0700202 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300203
204 /* XXX check return code */
205 odpl->deactivate_func(od);
206
Kevin Hilmand2292662009-12-08 16:34:23 -0700207 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300208
Tony Lindgrenf0594292009-10-19 15:25:24 -0700209 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700210 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300211
212 pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time "
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700213 "%llu nsec\n", od->pdev.name, od->pm_lat_level,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300214 deact_lat);
215
Kevin Hilman9799aca2010-01-26 20:13:02 -0700216 if (deact_lat > odpl->deactivate_lat) {
217 odpl->deactivate_lat_worst = deact_lat;
218 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
219 odpl->deactivate_lat = deact_lat;
220 pr_warning("omap_device: %s.%d: new worst case "
221 "deactivate latency %d: %llu\n",
222 od->pdev.name, od->pdev.id,
223 od->pm_lat_level, deact_lat);
224 } else
225 pr_warning("omap_device: %s.%d: deactivate "
226 "latency %d higher than exptected. "
227 "(%llu > %d)\n",
228 od->pdev.name, od->pdev.id,
229 od->pm_lat_level, deact_lat,
230 odpl->deactivate_lat);
231 }
232
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300233
234 od->dev_wakeup_lat += odpl->activate_lat;
235
236 od->pm_lat_level++;
237 }
238
239 return 0;
240}
241
242static inline struct omap_device *_find_by_pdev(struct platform_device *pdev)
243{
244 return container_of(pdev, struct omap_device, pdev);
245}
246
Partha Basak4ef7aca2010-09-21 19:23:04 +0200247/**
248 * _add_optional_clock_alias - Add clock alias for hwmod optional clocks
249 * @od: struct omap_device *od
250 *
251 * For every optional clock present per hwmod per omap_device, this function
252 * adds an entry in the clocks list of the form <dev-id=dev_name, con-id=role>
253 * if an entry is already present in it with the form <dev-id=NULL, con-id=role>
254 *
255 * The function is called from inside omap_device_build_ss(), after
256 * omap_device_register.
257 *
258 * This allows drivers to get a pointer to its optional clocks based on its role
259 * by calling clk_get(<dev*>, <role>).
260 *
261 * No return value.
262 */
263static void _add_optional_clock_alias(struct omap_device *od,
264 struct omap_hwmod *oh)
265{
266 int i;
267
268 for (i = 0; i < oh->opt_clks_cnt; i++) {
269 struct omap_hwmod_opt_clk *oc;
270 int r;
271
272 oc = &oh->opt_clks[i];
273
274 if (!oc->_clk)
275 continue;
276
277 r = clk_add_alias(oc->role, dev_name(&od->pdev.dev),
278 (char *)oc->clk, &od->pdev.dev);
279 if (r)
280 pr_err("omap_device: %s: clk_add_alias for %s failed\n",
281 dev_name(&od->pdev.dev), oc->role);
282 }
283}
284
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300285
286/* Public functions for use by core code */
287
288/**
289 * omap_device_count_resources - count number of struct resource entries needed
290 * @od: struct omap_device *
291 *
292 * Count the number of struct resource entries needed for this
293 * omap_device @od. Used by omap_device_build_ss() to determine how
294 * much memory to allocate before calling
295 * omap_device_fill_resources(). Returns the count.
296 */
297int omap_device_count_resources(struct omap_device *od)
298{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300299 int c = 0;
300 int i;
301
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600302 for (i = 0; i < od->hwmods_cnt; i++)
303 c += omap_hwmod_count_resources(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300304
305 pr_debug("omap_device: %s: counted %d total resources across %d "
306 "hwmods\n", od->pdev.name, c, od->hwmods_cnt);
307
308 return c;
309}
310
311/**
312 * omap_device_fill_resources - fill in array of struct resource
313 * @od: struct omap_device *
314 * @res: pointer to an array of struct resource to be filled in
315 *
316 * Populate one or more empty struct resource pointed to by @res with
317 * the resource data for this omap_device @od. Used by
318 * omap_device_build_ss() after calling omap_device_count_resources().
319 * Ideally this function would not be needed at all. If omap_device
320 * replaces platform_device, then we can specify our own
321 * get_resource()/ get_irq()/etc functions that use the underlying
322 * omap_hwmod information. Or if platform_device is extended to use
323 * subarchitecture-specific function pointers, the various
324 * platform_device functions can simply call omap_device internal
325 * functions to get device resources. Hacking around the existing
326 * platform_device code wastes memory. Returns 0.
327 */
328int omap_device_fill_resources(struct omap_device *od, struct resource *res)
329{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300330 int c = 0;
331 int i, r;
332
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600333 for (i = 0; i < od->hwmods_cnt; i++) {
334 r = omap_hwmod_fill_resources(od->hwmods[i], res);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300335 res += r;
336 c += r;
337 }
338
339 return 0;
340}
341
342/**
343 * omap_device_build - build and register an omap_device with one omap_hwmod
344 * @pdev_name: name of the platform_device driver to use
345 * @pdev_id: this platform_device's connection ID
346 * @oh: ptr to the single omap_hwmod that backs this omap_device
347 * @pdata: platform_data ptr to associate with the platform_device
348 * @pdata_len: amount of memory pointed to by @pdata
349 * @pm_lats: pointer to a omap_device_pm_latency array for this device
350 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700351 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300352 *
353 * Convenience function for building and registering a single
354 * omap_device record, which in turn builds and registers a
355 * platform_device record. See omap_device_build_ss() for more
356 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
357 * passes along the return value of omap_device_build_ss().
358 */
359struct omap_device *omap_device_build(const char *pdev_name, int pdev_id,
360 struct omap_hwmod *oh, void *pdata,
361 int pdata_len,
362 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700363 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300364{
365 struct omap_hwmod *ohs[] = { oh };
366
367 if (!oh)
368 return ERR_PTR(-EINVAL);
369
370 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700371 pdata_len, pm_lats, pm_lats_cnt,
372 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300373}
374
375/**
376 * omap_device_build_ss - build and register an omap_device with multiple hwmods
377 * @pdev_name: name of the platform_device driver to use
378 * @pdev_id: this platform_device's connection ID
379 * @oh: ptr to the single omap_hwmod that backs this omap_device
380 * @pdata: platform_data ptr to associate with the platform_device
381 * @pdata_len: amount of memory pointed to by @pdata
382 * @pm_lats: pointer to a omap_device_pm_latency array for this device
383 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700384 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300385 *
386 * Convenience function for building and registering an omap_device
387 * subsystem record. Subsystem records consist of multiple
388 * omap_hwmods. This function in turn builds and registers a
389 * platform_device record. Returns an ERR_PTR() on error, or passes
390 * along the return value of omap_device_register().
391 */
392struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
393 struct omap_hwmod **ohs, int oh_cnt,
394 void *pdata, int pdata_len,
395 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700396 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300397{
398 int ret = -ENOMEM;
399 struct omap_device *od;
400 char *pdev_name2;
401 struct resource *res = NULL;
Kevin Hilman06563582010-07-26 16:34:30 -0600402 int i, res_count;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300403 struct omap_hwmod **hwmods;
404
405 if (!ohs || oh_cnt == 0 || !pdev_name)
406 return ERR_PTR(-EINVAL);
407
408 if (!pdata && pdata_len > 0)
409 return ERR_PTR(-EINVAL);
410
411 pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name,
412 oh_cnt);
413
414 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
415 if (!od)
416 return ERR_PTR(-ENOMEM);
417
418 od->hwmods_cnt = oh_cnt;
419
420 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt,
421 GFP_KERNEL);
422 if (!hwmods)
423 goto odbs_exit1;
424
425 memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt);
426 od->hwmods = hwmods;
427
428 pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL);
429 if (!pdev_name2)
430 goto odbs_exit2;
431 strcpy(pdev_name2, pdev_name);
432
433 od->pdev.name = pdev_name2;
434 od->pdev.id = pdev_id;
435
436 res_count = omap_device_count_resources(od);
437 if (res_count > 0) {
438 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
439 if (!res)
440 goto odbs_exit3;
441 }
442 omap_device_fill_resources(od, res);
443
444 od->pdev.num_resources = res_count;
445 od->pdev.resource = res;
446
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000447 ret = platform_device_add_data(&od->pdev, pdata, pdata_len);
448 if (ret)
449 goto odbs_exit4;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300450
451 od->pm_lats = pm_lats;
452 od->pm_lats_cnt = pm_lats_cnt;
453
Kevin Hilman00071222010-02-24 12:05:45 -0700454 od->magic = OMAP_DEVICE_MAGIC;
455
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700456 if (is_early_device)
457 ret = omap_early_device_register(od);
458 else
459 ret = omap_device_register(od);
460
Partha Basak4ef7aca2010-09-21 19:23:04 +0200461 for (i = 0; i < oh_cnt; i++) {
Kevin Hilman06563582010-07-26 16:34:30 -0600462 hwmods[i]->od = od;
Partha Basak4ef7aca2010-09-21 19:23:04 +0200463 _add_optional_clock_alias(od, hwmods[i]);
464 }
Kevin Hilman06563582010-07-26 16:34:30 -0600465
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300466 if (ret)
467 goto odbs_exit4;
468
469 return od;
470
471odbs_exit4:
472 kfree(res);
473odbs_exit3:
474 kfree(pdev_name2);
475odbs_exit2:
476 kfree(hwmods);
477odbs_exit1:
478 kfree(od);
479
480 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
481
482 return ERR_PTR(ret);
483}
484
485/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700486 * omap_early_device_register - register an omap_device as an early platform
487 * device.
488 * @od: struct omap_device * to register
489 *
490 * Register the omap_device structure. This currently just calls
491 * platform_early_add_device() on the underlying platform_device.
492 * Returns 0 by default.
493 */
494int omap_early_device_register(struct omap_device *od)
495{
496 struct platform_device *devices[1];
497
498 devices[0] = &(od->pdev);
499 early_platform_add_devices(devices, 1);
500 return 0;
501}
502
503/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300504 * omap_device_register - register an omap_device with one omap_hwmod
505 * @od: struct omap_device * to register
506 *
507 * Register the omap_device structure. This currently just calls
508 * platform_device_register() on the underlying platform_device.
509 * Returns the return value of platform_device_register().
510 */
511int omap_device_register(struct omap_device *od)
512{
513 pr_debug("omap_device: %s: registering\n", od->pdev.name);
514
515 return platform_device_register(&od->pdev);
516}
517
518
519/* Public functions for use by device drivers through struct platform_data */
520
521/**
522 * omap_device_enable - fully activate an omap_device
523 * @od: struct omap_device * to activate
524 *
525 * Do whatever is necessary for the hwmods underlying omap_device @od
526 * to be accessible and ready to operate. This generally involves
527 * enabling clocks, setting SYSCONFIG registers; and in the future may
528 * involve remuxing pins. Device drivers should call this function
529 * (through platform_data function pointers) where they would normally
530 * enable clocks, etc. Returns -EINVAL if called when the omap_device
531 * is already enabled, or passes along the return value of
532 * _omap_device_activate().
533 */
534int omap_device_enable(struct platform_device *pdev)
535{
536 int ret;
537 struct omap_device *od;
538
539 od = _find_by_pdev(pdev);
540
541 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700542 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
543 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300544 return -EINVAL;
545 }
546
547 /* Enable everything if we're enabling this device from scratch */
548 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
549 od->pm_lat_level = od->pm_lats_cnt;
550
551 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
552
553 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700554 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300555 od->_state = OMAP_DEVICE_STATE_ENABLED;
556
557 return ret;
558}
559
560/**
561 * omap_device_idle - idle an omap_device
562 * @od: struct omap_device * to idle
563 *
564 * Idle omap_device @od by calling as many .deactivate_func() entries
565 * in the omap_device's pm_lats table as is possible without exceeding
566 * the device's maximum wakeup latency limit, pm_lat_limit. Device
567 * drivers should call this function (through platform_data function
568 * pointers) where they would normally disable clocks after operations
569 * complete, etc.. Returns -EINVAL if the omap_device is not
570 * currently enabled, or passes along the return value of
571 * _omap_device_deactivate().
572 */
573int omap_device_idle(struct platform_device *pdev)
574{
575 int ret;
576 struct omap_device *od;
577
578 od = _find_by_pdev(pdev);
579
580 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700581 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
582 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300583 return -EINVAL;
584 }
585
586 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
587
588 od->_state = OMAP_DEVICE_STATE_IDLE;
589
590 return ret;
591}
592
593/**
594 * omap_device_shutdown - shut down an omap_device
595 * @od: struct omap_device * to shut down
596 *
597 * Shut down omap_device @od by calling all .deactivate_func() entries
598 * in the omap_device's pm_lats table and then shutting down all of
599 * the underlying omap_hwmods. Used when a device is being "removed"
600 * or a device driver is being unloaded. Returns -EINVAL if the
601 * omap_device is not currently enabled or idle, or passes along the
602 * return value of _omap_device_deactivate().
603 */
604int omap_device_shutdown(struct platform_device *pdev)
605{
606 int ret, i;
607 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300608
609 od = _find_by_pdev(pdev);
610
611 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
612 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman24d82e32010-02-24 12:05:45 -0700613 WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n",
614 od->pdev.name, od->pdev.id, __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300615 return -EINVAL;
616 }
617
618 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
619
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600620 for (i = 0; i < od->hwmods_cnt; i++)
621 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300622
623 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
624
625 return ret;
626}
627
628/**
629 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
630 * @od: struct omap_device *
631 *
632 * When a device's maximum wakeup latency limit changes, call some of
633 * the .activate_func or .deactivate_func function pointers in the
634 * omap_device's pm_lats array to ensure that the device's maximum
635 * wakeup latency is less than or equal to the new latency limit.
636 * Intended to be called by OMAP PM code whenever a device's maximum
637 * wakeup latency limit changes (e.g., via
638 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
639 * done (e.g., if the omap_device is not currently idle, or if the
640 * wakeup latency is already current with the new limit) or passes
641 * along the return value of _omap_device_deactivate() or
642 * _omap_device_activate().
643 */
644int omap_device_align_pm_lat(struct platform_device *pdev,
645 u32 new_wakeup_lat_limit)
646{
647 int ret = -EINVAL;
648 struct omap_device *od;
649
650 od = _find_by_pdev(pdev);
651
652 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
653 return 0;
654
655 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
656
657 if (od->_state != OMAP_DEVICE_STATE_IDLE)
658 return 0;
659 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
660 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
661 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
662 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
663
664 return ret;
665}
666
667/**
Kevin Hilman00071222010-02-24 12:05:45 -0700668 * omap_device_is_valid - Check if pointer is a valid omap_device
669 * @od: struct omap_device *
670 *
671 * Return whether struct omap_device pointer @od points to a valid
672 * omap_device.
673 */
674bool omap_device_is_valid(struct omap_device *od)
675{
676 return (od && od->magic == OMAP_DEVICE_MAGIC);
677}
678
679/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300680 * omap_device_get_pwrdm - return the powerdomain * associated with @od
681 * @od: struct omap_device *
682 *
683 * Return the powerdomain associated with the first underlying
684 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
685 * code. Returns NULL on error or a struct powerdomain * upon
686 * success.
687 */
688struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
689{
690 /*
691 * XXX Assumes that all omap_hwmod powerdomains are identical.
692 * This may not necessarily be true. There should be a sanity
693 * check in here to WARN() if any difference appears.
694 */
695 if (!od->hwmods_cnt)
696 return NULL;
697
698 return omap_hwmod_get_pwrdm(od->hwmods[0]);
699}
700
Paul Walmsleydb2a60b2010-07-26 16:34:33 -0600701/**
702 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
703 * @od: struct omap_device *
704 *
705 * Return the MPU's virtual address for the base of the hwmod, from
706 * the ioremap() that the hwmod code does. Only valid if there is one
707 * hwmod associated with this device. Returns NULL if there are zero
708 * or more than one hwmods associated with this omap_device;
709 * otherwise, passes along the return value from
710 * omap_hwmod_get_mpu_rt_va().
711 */
712void __iomem *omap_device_get_rt_va(struct omap_device *od)
713{
714 if (od->hwmods_cnt != 1)
715 return NULL;
716
717 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
718}
719
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300720/*
721 * Public functions intended for use in omap_device_pm_latency
722 * .activate_func and .deactivate_func function pointers
723 */
724
725/**
726 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
727 * @od: struct omap_device *od
728 *
729 * Enable all underlying hwmods. Returns 0.
730 */
731int omap_device_enable_hwmods(struct omap_device *od)
732{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300733 int i;
734
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600735 for (i = 0; i < od->hwmods_cnt; i++)
736 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300737
738 /* XXX pass along return value here? */
739 return 0;
740}
741
742/**
743 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
744 * @od: struct omap_device *od
745 *
746 * Idle all underlying hwmods. Returns 0.
747 */
748int omap_device_idle_hwmods(struct omap_device *od)
749{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300750 int i;
751
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600752 for (i = 0; i < od->hwmods_cnt; i++)
753 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300754
755 /* XXX pass along return value here? */
756 return 0;
757}
758
759/**
760 * omap_device_disable_clocks - disable all main and interface clocks
761 * @od: struct omap_device *od
762 *
763 * Disable the main functional clock and interface clock for all of the
764 * omap_hwmods associated with the omap_device. Returns 0.
765 */
766int omap_device_disable_clocks(struct omap_device *od)
767{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300768 int i;
769
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600770 for (i = 0; i < od->hwmods_cnt; i++)
771 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300772
773 /* XXX pass along return value here? */
774 return 0;
775}
776
777/**
778 * omap_device_enable_clocks - enable all main and interface clocks
779 * @od: struct omap_device *od
780 *
781 * Enable the main functional clock and interface clock for all of the
782 * omap_hwmods associated with the omap_device. Returns 0.
783 */
784int omap_device_enable_clocks(struct omap_device *od)
785{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300786 int i;
787
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600788 for (i = 0; i < od->hwmods_cnt; i++)
789 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300790
791 /* XXX pass along return value here? */
792 return 0;
793}