Commit | Line | Data |
---|---|---|
b04b65ab PW |
1 | /* |
2 | * omap_device implementation | |
3 | * | |
887adeac | 4 | * Copyright (C) 2009-2010 Nokia Corporation |
4788da26 | 5 | * Paul Walmsley, Kevin Hilman |
b04b65ab PW |
6 | * |
7 | * Developed in collaboration with (alphabetical order): Benoit | |
4788da26 | 8 | * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram |
b04b65ab PW |
9 | * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard |
10 | * Woodruff | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | * | |
16 | * This code provides a consistent interface for OMAP device drivers | |
17 | * to control power management and interconnect properties of their | |
18 | * devices. | |
19 | * | |
20 | * In the medium- to long-term, this code should either be | |
21 | * a) implemented via arch-specific pointers in platform_data | |
22 | * or | |
23 | * b) implemented as a proper omap_bus/omap_device in Linux, no more | |
24 | * platform_data func pointers | |
25 | * | |
26 | * | |
27 | * Guidelines for usage by driver authors: | |
28 | * | |
29 | * 1. These functions are intended to be used by device drivers via | |
30 | * function pointers in struct platform_data. As an example, | |
31 | * omap_device_enable() should be passed to the driver as | |
32 | * | |
33 | * struct foo_driver_platform_data { | |
34 | * ... | |
35 | * int (*device_enable)(struct platform_device *pdev); | |
36 | * ... | |
37 | * } | |
38 | * | |
39 | * Note that the generic "device_enable" name is used, rather than | |
40 | * "omap_device_enable". This is so other architectures can pass in their | |
41 | * own enable/disable functions here. | |
42 | * | |
43 | * This should be populated during device setup: | |
44 | * | |
45 | * ... | |
46 | * pdata->device_enable = omap_device_enable; | |
47 | * ... | |
48 | * | |
49 | * 2. Drivers should first check to ensure the function pointer is not null | |
50 | * before calling it, as in: | |
51 | * | |
52 | * if (pdata->device_enable) | |
53 | * pdata->device_enable(pdev); | |
54 | * | |
55 | * This allows other architectures that don't use similar device_enable()/ | |
56 | * device_shutdown() functions to execute normally. | |
57 | * | |
58 | * ... | |
59 | * | |
60 | * Suggested usage by device drivers: | |
61 | * | |
62 | * During device initialization: | |
63 | * device_enable() | |
64 | * | |
65 | * During device idle: | |
66 | * (save remaining device context if necessary) | |
67 | * device_idle(); | |
68 | * | |
69 | * During device resume: | |
70 | * device_enable(); | |
71 | * (restore context if necessary) | |
72 | * | |
73 | * During device shutdown: | |
74 | * device_shutdown() | |
75 | * (device must be reinitialized at this point to use it again) | |
76 | * | |
77 | */ | |
78 | #undef DEBUG | |
79 | ||
80 | #include <linux/kernel.h> | |
81 | #include <linux/platform_device.h> | |
5a0e3ad6 | 82 | #include <linux/slab.h> |
b04b65ab PW |
83 | #include <linux/err.h> |
84 | #include <linux/io.h> | |
4ef7aca8 | 85 | #include <linux/clk.h> |
da0653fe | 86 | #include <linux/clkdev.h> |
345f79b3 | 87 | #include <linux/pm_runtime.h> |
b04b65ab | 88 | |
ce491cf8 TL |
89 | #include <plat/omap_device.h> |
90 | #include <plat/omap_hwmod.h> | |
da0653fe | 91 | #include <plat/clock.h> |
b04b65ab PW |
92 | |
93 | /* These parameters are passed to _omap_device_{de,}activate() */ | |
94 | #define USE_WAKEUP_LAT 0 | |
95 | #define IGNORE_WAKEUP_LAT 1 | |
96 | ||
bfae4f8f KH |
97 | static int omap_device_register(struct platform_device *pdev); |
98 | static int omap_early_device_register(struct platform_device *pdev); | |
a4f6cdb0 BC |
99 | static struct omap_device *omap_device_alloc(struct platform_device *pdev, |
100 | struct omap_hwmod **ohs, int oh_cnt, | |
101 | struct omap_device_pm_latency *pm_lats, | |
102 | int pm_lats_cnt); | |
103 | ||
a2a28ad9 | 104 | |
b7b5bc91 BC |
105 | static struct omap_device_pm_latency omap_default_latency[] = { |
106 | { | |
107 | .deactivate_func = omap_device_idle_hwmods, | |
108 | .activate_func = omap_device_enable_hwmods, | |
109 | .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST, | |
110 | } | |
111 | }; | |
112 | ||
b04b65ab PW |
113 | /* Private functions */ |
114 | ||
b04b65ab PW |
115 | /** |
116 | * _omap_device_activate - increase device readiness | |
117 | * @od: struct omap_device * | |
118 | * @ignore_lat: increase to latency target (0) or full readiness (1)? | |
119 | * | |
120 | * Increase readiness of omap_device @od (thus decreasing device | |
121 | * wakeup latency, but consuming more power). If @ignore_lat is | |
122 | * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise, | |
123 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup | |
124 | * latency is greater than the requested maximum wakeup latency, step | |
125 | * backwards in the omap_device_pm_latency table to ensure the | |
126 | * device's maximum wakeup latency is less than or equal to the | |
127 | * requested maximum wakeup latency. Returns 0. | |
128 | */ | |
129 | static int _omap_device_activate(struct omap_device *od, u8 ignore_lat) | |
130 | { | |
f059429e | 131 | struct timespec a, b, c; |
b04b65ab | 132 | |
d66b3fe4 | 133 | dev_dbg(&od->pdev->dev, "omap_device: activating\n"); |
b04b65ab PW |
134 | |
135 | while (od->pm_lat_level > 0) { | |
136 | struct omap_device_pm_latency *odpl; | |
f059429e | 137 | unsigned long long act_lat = 0; |
b04b65ab PW |
138 | |
139 | od->pm_lat_level--; | |
140 | ||
141 | odpl = od->pm_lats + od->pm_lat_level; | |
142 | ||
143 | if (!ignore_lat && | |
144 | (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit)) | |
145 | break; | |
146 | ||
d2292667 | 147 | read_persistent_clock(&a); |
b04b65ab PW |
148 | |
149 | /* XXX check return code */ | |
150 | odpl->activate_func(od); | |
151 | ||
d2292667 | 152 | read_persistent_clock(&b); |
b04b65ab | 153 | |
f059429e | 154 | c = timespec_sub(b, a); |
0d93d8bb | 155 | act_lat = timespec_to_ns(&c); |
b04b65ab | 156 | |
d66b3fe4 | 157 | dev_dbg(&od->pdev->dev, |
49882c99 KH |
158 | "omap_device: pm_lat %d: activate: elapsed time " |
159 | "%llu nsec\n", od->pm_lat_level, act_lat); | |
b04b65ab | 160 | |
9799aca2 KH |
161 | if (act_lat > odpl->activate_lat) { |
162 | odpl->activate_lat_worst = act_lat; | |
163 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { | |
164 | odpl->activate_lat = act_lat; | |
d66b3fe4 | 165 | dev_dbg(&od->pdev->dev, |
47c3e5d8 GI |
166 | "new worst case activate latency " |
167 | "%d: %llu\n", | |
168 | od->pm_lat_level, act_lat); | |
9799aca2 | 169 | } else |
d66b3fe4 | 170 | dev_warn(&od->pdev->dev, |
49882c99 KH |
171 | "activate latency %d " |
172 | "higher than exptected. (%llu > %d)\n", | |
173 | od->pm_lat_level, act_lat, | |
174 | odpl->activate_lat); | |
9799aca2 | 175 | } |
b04b65ab PW |
176 | |
177 | od->dev_wakeup_lat -= odpl->activate_lat; | |
178 | } | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
183 | /** | |
184 | * _omap_device_deactivate - decrease device readiness | |
185 | * @od: struct omap_device * | |
186 | * @ignore_lat: decrease to latency target (0) or full inactivity (1)? | |
187 | * | |
188 | * Decrease readiness of omap_device @od (thus increasing device | |
189 | * wakeup latency, but conserving power). If @ignore_lat is | |
190 | * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise, | |
191 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup | |
192 | * latency is less than the requested maximum wakeup latency, step | |
193 | * forwards in the omap_device_pm_latency table to ensure the device's | |
194 | * maximum wakeup latency is less than or equal to the requested | |
195 | * maximum wakeup latency. Returns 0. | |
196 | */ | |
197 | static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat) | |
198 | { | |
f059429e | 199 | struct timespec a, b, c; |
b04b65ab | 200 | |
d66b3fe4 | 201 | dev_dbg(&od->pdev->dev, "omap_device: deactivating\n"); |
b04b65ab PW |
202 | |
203 | while (od->pm_lat_level < od->pm_lats_cnt) { | |
204 | struct omap_device_pm_latency *odpl; | |
f059429e | 205 | unsigned long long deact_lat = 0; |
b04b65ab PW |
206 | |
207 | odpl = od->pm_lats + od->pm_lat_level; | |
208 | ||
209 | if (!ignore_lat && | |
210 | ((od->dev_wakeup_lat + odpl->activate_lat) > | |
211 | od->_dev_wakeup_lat_limit)) | |
212 | break; | |
213 | ||
d2292667 | 214 | read_persistent_clock(&a); |
b04b65ab PW |
215 | |
216 | /* XXX check return code */ | |
217 | odpl->deactivate_func(od); | |
218 | ||
d2292667 | 219 | read_persistent_clock(&b); |
b04b65ab | 220 | |
f059429e | 221 | c = timespec_sub(b, a); |
0d93d8bb | 222 | deact_lat = timespec_to_ns(&c); |
b04b65ab | 223 | |
d66b3fe4 | 224 | dev_dbg(&od->pdev->dev, |
49882c99 KH |
225 | "omap_device: pm_lat %d: deactivate: elapsed time " |
226 | "%llu nsec\n", od->pm_lat_level, deact_lat); | |
b04b65ab | 227 | |
9799aca2 KH |
228 | if (deact_lat > odpl->deactivate_lat) { |
229 | odpl->deactivate_lat_worst = deact_lat; | |
230 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { | |
231 | odpl->deactivate_lat = deact_lat; | |
d66b3fe4 | 232 | dev_dbg(&od->pdev->dev, |
47c3e5d8 GI |
233 | "new worst case deactivate latency " |
234 | "%d: %llu\n", | |
235 | od->pm_lat_level, deact_lat); | |
9799aca2 | 236 | } else |
d66b3fe4 | 237 | dev_warn(&od->pdev->dev, |
49882c99 KH |
238 | "deactivate latency %d " |
239 | "higher than exptected. (%llu > %d)\n", | |
240 | od->pm_lat_level, deact_lat, | |
241 | odpl->deactivate_lat); | |
9799aca2 KH |
242 | } |
243 | ||
b04b65ab PW |
244 | od->dev_wakeup_lat += odpl->activate_lat; |
245 | ||
246 | od->pm_lat_level++; | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
bf1e0776 BC |
252 | static void _add_clkdev(struct omap_device *od, const char *clk_alias, |
253 | const char *clk_name) | |
254 | { | |
255 | struct clk *r; | |
256 | struct clk_lookup *l; | |
257 | ||
258 | if (!clk_alias || !clk_name) | |
259 | return; | |
260 | ||
d66b3fe4 | 261 | dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name); |
bf1e0776 | 262 | |
d66b3fe4 | 263 | r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias); |
bf1e0776 | 264 | if (!IS_ERR(r)) { |
d66b3fe4 | 265 | dev_warn(&od->pdev->dev, |
49882c99 | 266 | "alias %s already exists\n", clk_alias); |
bf1e0776 BC |
267 | clk_put(r); |
268 | return; | |
269 | } | |
270 | ||
271 | r = omap_clk_get_by_name(clk_name); | |
272 | if (IS_ERR(r)) { | |
d66b3fe4 | 273 | dev_err(&od->pdev->dev, |
49882c99 | 274 | "omap_clk_get_by_name for %s failed\n", clk_name); |
bf1e0776 BC |
275 | return; |
276 | } | |
277 | ||
d66b3fe4 | 278 | l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev)); |
bf1e0776 | 279 | if (!l) { |
d66b3fe4 | 280 | dev_err(&od->pdev->dev, |
49882c99 | 281 | "clkdev_alloc for %s failed\n", clk_alias); |
bf1e0776 BC |
282 | return; |
283 | } | |
284 | ||
285 | clkdev_add(l); | |
286 | } | |
287 | ||
4ef7aca8 | 288 | /** |
bf1e0776 BC |
289 | * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks |
290 | * and main clock | |
4ef7aca8 | 291 | * @od: struct omap_device *od |
bf1e0776 | 292 | * @oh: struct omap_hwmod *oh |
4ef7aca8 | 293 | * |
bf1e0776 BC |
294 | * For the main clock and every optional clock present per hwmod per |
295 | * omap_device, this function adds an entry in the clkdev table of the | |
296 | * form <dev-id=dev_name, con-id=role> if it does not exist already. | |
4ef7aca8 PB |
297 | * |
298 | * The function is called from inside omap_device_build_ss(), after | |
299 | * omap_device_register. | |
300 | * | |
301 | * This allows drivers to get a pointer to its optional clocks based on its role | |
302 | * by calling clk_get(<dev*>, <role>). | |
bf1e0776 | 303 | * In the case of the main clock, a "fck" alias is used. |
4ef7aca8 PB |
304 | * |
305 | * No return value. | |
306 | */ | |
bf1e0776 BC |
307 | static void _add_hwmod_clocks_clkdev(struct omap_device *od, |
308 | struct omap_hwmod *oh) | |
4ef7aca8 PB |
309 | { |
310 | int i; | |
311 | ||
bf1e0776 | 312 | _add_clkdev(od, "fck", oh->main_clk); |
da0653fe | 313 | |
bf1e0776 BC |
314 | for (i = 0; i < oh->opt_clks_cnt; i++) |
315 | _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk); | |
4ef7aca8 PB |
316 | } |
317 | ||
b04b65ab PW |
318 | |
319 | /* Public functions for use by core code */ | |
320 | ||
c80705aa KH |
321 | /** |
322 | * omap_device_get_context_loss_count - get lost context count | |
323 | * @od: struct omap_device * | |
324 | * | |
325 | * Using the primary hwmod, query the context loss count for this | |
326 | * device. | |
327 | * | |
328 | * Callers should consider context for this device lost any time this | |
329 | * function returns a value different than the value the caller got | |
330 | * the last time it called this function. | |
331 | * | |
332 | * If any hwmods exist for the omap_device assoiated with @pdev, | |
333 | * return the context loss counter for that hwmod, otherwise return | |
334 | * zero. | |
335 | */ | |
336 | u32 omap_device_get_context_loss_count(struct platform_device *pdev) | |
337 | { | |
338 | struct omap_device *od; | |
339 | u32 ret = 0; | |
340 | ||
8f0d69de | 341 | od = to_omap_device(pdev); |
c80705aa KH |
342 | |
343 | if (od->hwmods_cnt) | |
344 | ret = omap_hwmod_get_context_loss_count(od->hwmods[0]); | |
345 | ||
346 | return ret; | |
347 | } | |
348 | ||
b04b65ab PW |
349 | /** |
350 | * omap_device_count_resources - count number of struct resource entries needed | |
351 | * @od: struct omap_device * | |
352 | * | |
353 | * Count the number of struct resource entries needed for this | |
354 | * omap_device @od. Used by omap_device_build_ss() to determine how | |
355 | * much memory to allocate before calling | |
356 | * omap_device_fill_resources(). Returns the count. | |
357 | */ | |
a2a28ad9 | 358 | static int omap_device_count_resources(struct omap_device *od) |
b04b65ab | 359 | { |
b04b65ab PW |
360 | int c = 0; |
361 | int i; | |
362 | ||
f39f4898 KVA |
363 | for (i = 0; i < od->hwmods_cnt; i++) |
364 | c += omap_hwmod_count_resources(od->hwmods[i]); | |
b04b65ab PW |
365 | |
366 | pr_debug("omap_device: %s: counted %d total resources across %d " | |
d66b3fe4 | 367 | "hwmods\n", od->pdev->name, c, od->hwmods_cnt); |
b04b65ab PW |
368 | |
369 | return c; | |
370 | } | |
371 | ||
372 | /** | |
373 | * omap_device_fill_resources - fill in array of struct resource | |
374 | * @od: struct omap_device * | |
375 | * @res: pointer to an array of struct resource to be filled in | |
376 | * | |
377 | * Populate one or more empty struct resource pointed to by @res with | |
378 | * the resource data for this omap_device @od. Used by | |
379 | * omap_device_build_ss() after calling omap_device_count_resources(). | |
380 | * Ideally this function would not be needed at all. If omap_device | |
381 | * replaces platform_device, then we can specify our own | |
382 | * get_resource()/ get_irq()/etc functions that use the underlying | |
383 | * omap_hwmod information. Or if platform_device is extended to use | |
384 | * subarchitecture-specific function pointers, the various | |
385 | * platform_device functions can simply call omap_device internal | |
386 | * functions to get device resources. Hacking around the existing | |
387 | * platform_device code wastes memory. Returns 0. | |
388 | */ | |
a2a28ad9 KH |
389 | static int omap_device_fill_resources(struct omap_device *od, |
390 | struct resource *res) | |
b04b65ab | 391 | { |
b04b65ab PW |
392 | int c = 0; |
393 | int i, r; | |
394 | ||
f39f4898 KVA |
395 | for (i = 0; i < od->hwmods_cnt; i++) { |
396 | r = omap_hwmod_fill_resources(od->hwmods[i], res); | |
b04b65ab PW |
397 | res += r; |
398 | c += r; | |
399 | } | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
a4f6cdb0 BC |
404 | /** |
405 | * omap_device_alloc - allocate an omap_device | |
406 | * @pdev: platform_device that will be included in this omap_device | |
407 | * @oh: ptr to the single omap_hwmod that backs this omap_device | |
408 | * @pdata: platform_data ptr to associate with the platform_device | |
409 | * @pdata_len: amount of memory pointed to by @pdata | |
410 | * @pm_lats: pointer to a omap_device_pm_latency array for this device | |
411 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats | |
412 | * | |
413 | * Convenience function for allocating an omap_device structure and filling | |
414 | * hwmods, resources and pm_latency attributes. | |
415 | * | |
416 | * Returns an struct omap_device pointer or ERR_PTR() on error; | |
417 | */ | |
418 | static struct omap_device *omap_device_alloc(struct platform_device *pdev, | |
419 | struct omap_hwmod **ohs, int oh_cnt, | |
420 | struct omap_device_pm_latency *pm_lats, | |
421 | int pm_lats_cnt) | |
422 | { | |
423 | int ret = -ENOMEM; | |
424 | struct omap_device *od; | |
425 | struct resource *res = NULL; | |
426 | int i, res_count; | |
427 | struct omap_hwmod **hwmods; | |
428 | ||
429 | od = kzalloc(sizeof(struct omap_device), GFP_KERNEL); | |
430 | if (!od) { | |
431 | ret = -ENOMEM; | |
432 | goto oda_exit1; | |
433 | } | |
434 | od->hwmods_cnt = oh_cnt; | |
435 | ||
436 | hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL); | |
437 | if (!hwmods) | |
438 | goto oda_exit2; | |
439 | ||
440 | od->hwmods = hwmods; | |
441 | od->pdev = pdev; | |
442 | ||
443 | /* | |
444 | * HACK: Ideally the resources from DT should match, and hwmod | |
445 | * should just add the missing ones. Since the name is not | |
446 | * properly populated by DT, stick to hwmod resources only. | |
447 | */ | |
448 | if (pdev->num_resources && pdev->resource) | |
449 | dev_warn(&pdev->dev, "%s(): resources already allocated %d\n", | |
450 | __func__, pdev->num_resources); | |
451 | ||
452 | res_count = omap_device_count_resources(od); | |
453 | if (res_count > 0) { | |
454 | dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n", | |
455 | __func__, res_count); | |
456 | res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL); | |
457 | if (!res) | |
458 | goto oda_exit3; | |
459 | ||
460 | omap_device_fill_resources(od, res); | |
461 | ||
462 | ret = platform_device_add_resources(pdev, res, res_count); | |
463 | kfree(res); | |
464 | ||
465 | if (ret) | |
466 | goto oda_exit3; | |
467 | } | |
468 | ||
469 | if (!pm_lats) { | |
470 | pm_lats = omap_default_latency; | |
471 | pm_lats_cnt = ARRAY_SIZE(omap_default_latency); | |
472 | } | |
473 | ||
474 | od->pm_lats_cnt = pm_lats_cnt; | |
475 | od->pm_lats = kmemdup(pm_lats, | |
476 | sizeof(struct omap_device_pm_latency) * pm_lats_cnt, | |
477 | GFP_KERNEL); | |
478 | if (!od->pm_lats) | |
479 | goto oda_exit3; | |
480 | ||
481 | pdev->archdata.od = od; | |
482 | ||
483 | for (i = 0; i < oh_cnt; i++) { | |
484 | hwmods[i]->od = od; | |
485 | _add_hwmod_clocks_clkdev(od, hwmods[i]); | |
486 | } | |
487 | ||
488 | return od; | |
489 | ||
490 | oda_exit3: | |
491 | kfree(hwmods); | |
492 | oda_exit2: | |
493 | kfree(od); | |
494 | oda_exit1: | |
495 | dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret); | |
496 | ||
497 | return ERR_PTR(ret); | |
498 | } | |
499 | ||
500 | static void omap_device_delete(struct omap_device *od) | |
501 | { | |
502 | od->pdev->archdata.od = NULL; | |
503 | kfree(od->pm_lats); | |
504 | kfree(od->hwmods); | |
505 | kfree(od); | |
506 | } | |
507 | ||
b04b65ab PW |
508 | /** |
509 | * omap_device_build - build and register an omap_device with one omap_hwmod | |
510 | * @pdev_name: name of the platform_device driver to use | |
511 | * @pdev_id: this platform_device's connection ID | |
512 | * @oh: ptr to the single omap_hwmod that backs this omap_device | |
513 | * @pdata: platform_data ptr to associate with the platform_device | |
514 | * @pdata_len: amount of memory pointed to by @pdata | |
515 | * @pm_lats: pointer to a omap_device_pm_latency array for this device | |
516 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats | |
c23a97d3 | 517 | * @is_early_device: should the device be registered as an early device or not |
b04b65ab PW |
518 | * |
519 | * Convenience function for building and registering a single | |
520 | * omap_device record, which in turn builds and registers a | |
521 | * platform_device record. See omap_device_build_ss() for more | |
522 | * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise, | |
523 | * passes along the return value of omap_device_build_ss(). | |
524 | */ | |
3528c58e | 525 | struct platform_device *omap_device_build(const char *pdev_name, int pdev_id, |
b04b65ab PW |
526 | struct omap_hwmod *oh, void *pdata, |
527 | int pdata_len, | |
528 | struct omap_device_pm_latency *pm_lats, | |
c23a97d3 | 529 | int pm_lats_cnt, int is_early_device) |
b04b65ab PW |
530 | { |
531 | struct omap_hwmod *ohs[] = { oh }; | |
532 | ||
533 | if (!oh) | |
534 | return ERR_PTR(-EINVAL); | |
535 | ||
536 | return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata, | |
c23a97d3 TG |
537 | pdata_len, pm_lats, pm_lats_cnt, |
538 | is_early_device); | |
b04b65ab PW |
539 | } |
540 | ||
541 | /** | |
542 | * omap_device_build_ss - build and register an omap_device with multiple hwmods | |
543 | * @pdev_name: name of the platform_device driver to use | |
544 | * @pdev_id: this platform_device's connection ID | |
545 | * @oh: ptr to the single omap_hwmod that backs this omap_device | |
546 | * @pdata: platform_data ptr to associate with the platform_device | |
547 | * @pdata_len: amount of memory pointed to by @pdata | |
548 | * @pm_lats: pointer to a omap_device_pm_latency array for this device | |
549 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats | |
c23a97d3 | 550 | * @is_early_device: should the device be registered as an early device or not |
b04b65ab PW |
551 | * |
552 | * Convenience function for building and registering an omap_device | |
553 | * subsystem record. Subsystem records consist of multiple | |
554 | * omap_hwmods. This function in turn builds and registers a | |
555 | * platform_device record. Returns an ERR_PTR() on error, or passes | |
556 | * along the return value of omap_device_register(). | |
557 | */ | |
3528c58e | 558 | struct platform_device *omap_device_build_ss(const char *pdev_name, int pdev_id, |
b04b65ab PW |
559 | struct omap_hwmod **ohs, int oh_cnt, |
560 | void *pdata, int pdata_len, | |
561 | struct omap_device_pm_latency *pm_lats, | |
c23a97d3 | 562 | int pm_lats_cnt, int is_early_device) |
b04b65ab PW |
563 | { |
564 | int ret = -ENOMEM; | |
d66b3fe4 | 565 | struct platform_device *pdev; |
b04b65ab | 566 | struct omap_device *od; |
b04b65ab PW |
567 | |
568 | if (!ohs || oh_cnt == 0 || !pdev_name) | |
569 | return ERR_PTR(-EINVAL); | |
570 | ||
571 | if (!pdata && pdata_len > 0) | |
572 | return ERR_PTR(-EINVAL); | |
573 | ||
d66b3fe4 KH |
574 | pdev = platform_device_alloc(pdev_name, pdev_id); |
575 | if (!pdev) { | |
576 | ret = -ENOMEM; | |
577 | goto odbs_exit; | |
578 | } | |
579 | ||
a4f6cdb0 BC |
580 | /* Set the dev_name early to allow dev_xxx in omap_device_alloc */ |
581 | if (pdev->id != -1) | |
582 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); | |
583 | else | |
584 | dev_set_name(&pdev->dev, "%s", pdev->name); | |
b04b65ab | 585 | |
a4f6cdb0 BC |
586 | od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt); |
587 | if (!od) | |
d66b3fe4 | 588 | goto odbs_exit1; |
d66b3fe4 KH |
589 | |
590 | ret = platform_device_add_data(pdev, pdata, pdata_len); | |
49b368a6 | 591 | if (ret) |
a4f6cdb0 | 592 | goto odbs_exit2; |
b04b65ab | 593 | |
c23a97d3 | 594 | if (is_early_device) |
d66b3fe4 | 595 | ret = omap_early_device_register(pdev); |
c23a97d3 | 596 | else |
d66b3fe4 KH |
597 | ret = omap_device_register(pdev); |
598 | if (ret) | |
a4f6cdb0 | 599 | goto odbs_exit2; |
06563581 | 600 | |
d66b3fe4 | 601 | return pdev; |
b04b65ab | 602 | |
d66b3fe4 | 603 | odbs_exit2: |
a4f6cdb0 | 604 | omap_device_delete(od); |
d66b3fe4 KH |
605 | odbs_exit1: |
606 | platform_device_put(pdev); | |
607 | odbs_exit: | |
b04b65ab PW |
608 | |
609 | pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret); | |
610 | ||
611 | return ERR_PTR(ret); | |
612 | } | |
613 | ||
c23a97d3 TG |
614 | /** |
615 | * omap_early_device_register - register an omap_device as an early platform | |
616 | * device. | |
617 | * @od: struct omap_device * to register | |
618 | * | |
619 | * Register the omap_device structure. This currently just calls | |
620 | * platform_early_add_device() on the underlying platform_device. | |
621 | * Returns 0 by default. | |
622 | */ | |
bfae4f8f | 623 | static int omap_early_device_register(struct platform_device *pdev) |
c23a97d3 TG |
624 | { |
625 | struct platform_device *devices[1]; | |
626 | ||
bfae4f8f | 627 | devices[0] = pdev; |
c23a97d3 TG |
628 | early_platform_add_devices(devices, 1); |
629 | return 0; | |
630 | } | |
631 | ||
256a5435 | 632 | #ifdef CONFIG_PM_RUNTIME |
638080c3 KH |
633 | static int _od_runtime_suspend(struct device *dev) |
634 | { | |
635 | struct platform_device *pdev = to_platform_device(dev); | |
345f79b3 | 636 | int ret; |
638080c3 | 637 | |
345f79b3 KH |
638 | ret = pm_generic_runtime_suspend(dev); |
639 | ||
640 | if (!ret) | |
641 | omap_device_idle(pdev); | |
642 | ||
643 | return ret; | |
644 | } | |
645 | ||
646 | static int _od_runtime_idle(struct device *dev) | |
647 | { | |
648 | return pm_generic_runtime_idle(dev); | |
638080c3 KH |
649 | } |
650 | ||
651 | static int _od_runtime_resume(struct device *dev) | |
652 | { | |
653 | struct platform_device *pdev = to_platform_device(dev); | |
654 | ||
345f79b3 KH |
655 | omap_device_enable(pdev); |
656 | ||
657 | return pm_generic_runtime_resume(dev); | |
638080c3 | 658 | } |
256a5435 | 659 | #endif |
638080c3 | 660 | |
c03f007a KH |
661 | #ifdef CONFIG_SUSPEND |
662 | static int _od_suspend_noirq(struct device *dev) | |
663 | { | |
664 | struct platform_device *pdev = to_platform_device(dev); | |
665 | struct omap_device *od = to_omap_device(pdev); | |
666 | int ret; | |
667 | ||
80c6d1e6 KH |
668 | if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND) |
669 | return pm_generic_suspend_noirq(dev); | |
670 | ||
c03f007a KH |
671 | ret = pm_generic_suspend_noirq(dev); |
672 | ||
673 | if (!ret && !pm_runtime_status_suspended(dev)) { | |
674 | if (pm_generic_runtime_suspend(dev) == 0) { | |
675 | omap_device_idle(pdev); | |
676 | od->flags |= OMAP_DEVICE_SUSPENDED; | |
677 | } | |
678 | } | |
679 | ||
680 | return ret; | |
681 | } | |
682 | ||
683 | static int _od_resume_noirq(struct device *dev) | |
684 | { | |
685 | struct platform_device *pdev = to_platform_device(dev); | |
686 | struct omap_device *od = to_omap_device(pdev); | |
687 | ||
80c6d1e6 KH |
688 | if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND) |
689 | return pm_generic_resume_noirq(dev); | |
690 | ||
c03f007a KH |
691 | if ((od->flags & OMAP_DEVICE_SUSPENDED) && |
692 | !pm_runtime_status_suspended(dev)) { | |
693 | od->flags &= ~OMAP_DEVICE_SUSPENDED; | |
694 | omap_device_enable(pdev); | |
695 | pm_generic_runtime_resume(dev); | |
696 | } | |
697 | ||
698 | return pm_generic_resume_noirq(dev); | |
699 | } | |
126caf13 KH |
700 | #else |
701 | #define _od_suspend_noirq NULL | |
702 | #define _od_resume_noirq NULL | |
c03f007a KH |
703 | #endif |
704 | ||
564b905a | 705 | static struct dev_pm_domain omap_device_pm_domain = { |
638080c3 | 706 | .ops = { |
256a5435 KH |
707 | SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume, |
708 | _od_runtime_idle) | |
638080c3 | 709 | USE_PLATFORM_PM_SLEEP_OPS |
ff35336d KH |
710 | .suspend_noirq = _od_suspend_noirq, |
711 | .resume_noirq = _od_resume_noirq, | |
638080c3 KH |
712 | } |
713 | }; | |
714 | ||
b04b65ab PW |
715 | /** |
716 | * omap_device_register - register an omap_device with one omap_hwmod | |
717 | * @od: struct omap_device * to register | |
718 | * | |
719 | * Register the omap_device structure. This currently just calls | |
720 | * platform_device_register() on the underlying platform_device. | |
721 | * Returns the return value of platform_device_register(). | |
722 | */ | |
bfae4f8f | 723 | static int omap_device_register(struct platform_device *pdev) |
b04b65ab | 724 | { |
bfae4f8f | 725 | pr_debug("omap_device: %s: registering\n", pdev->name); |
b04b65ab | 726 | |
bfae4f8f KH |
727 | pdev->dev.parent = &omap_device_parent; |
728 | pdev->dev.pm_domain = &omap_device_pm_domain; | |
d66b3fe4 | 729 | return platform_device_add(pdev); |
b04b65ab PW |
730 | } |
731 | ||
732 | ||
733 | /* Public functions for use by device drivers through struct platform_data */ | |
734 | ||
735 | /** | |
736 | * omap_device_enable - fully activate an omap_device | |
737 | * @od: struct omap_device * to activate | |
738 | * | |
739 | * Do whatever is necessary for the hwmods underlying omap_device @od | |
740 | * to be accessible and ready to operate. This generally involves | |
741 | * enabling clocks, setting SYSCONFIG registers; and in the future may | |
742 | * involve remuxing pins. Device drivers should call this function | |
743 | * (through platform_data function pointers) where they would normally | |
744 | * enable clocks, etc. Returns -EINVAL if called when the omap_device | |
745 | * is already enabled, or passes along the return value of | |
746 | * _omap_device_activate(). | |
747 | */ | |
748 | int omap_device_enable(struct platform_device *pdev) | |
749 | { | |
750 | int ret; | |
751 | struct omap_device *od; | |
752 | ||
8f0d69de | 753 | od = to_omap_device(pdev); |
b04b65ab PW |
754 | |
755 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { | |
49882c99 KH |
756 | dev_warn(&pdev->dev, |
757 | "omap_device: %s() called from invalid state %d\n", | |
758 | __func__, od->_state); | |
b04b65ab PW |
759 | return -EINVAL; |
760 | } | |
761 | ||
762 | /* Enable everything if we're enabling this device from scratch */ | |
763 | if (od->_state == OMAP_DEVICE_STATE_UNKNOWN) | |
764 | od->pm_lat_level = od->pm_lats_cnt; | |
765 | ||
766 | ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT); | |
767 | ||
768 | od->dev_wakeup_lat = 0; | |
5f1b6ef7 | 769 | od->_dev_wakeup_lat_limit = UINT_MAX; |
b04b65ab PW |
770 | od->_state = OMAP_DEVICE_STATE_ENABLED; |
771 | ||
772 | return ret; | |
773 | } | |
774 | ||
775 | /** | |
776 | * omap_device_idle - idle an omap_device | |
777 | * @od: struct omap_device * to idle | |
778 | * | |
779 | * Idle omap_device @od by calling as many .deactivate_func() entries | |
780 | * in the omap_device's pm_lats table as is possible without exceeding | |
781 | * the device's maximum wakeup latency limit, pm_lat_limit. Device | |
782 | * drivers should call this function (through platform_data function | |
783 | * pointers) where they would normally disable clocks after operations | |
784 | * complete, etc.. Returns -EINVAL if the omap_device is not | |
785 | * currently enabled, or passes along the return value of | |
786 | * _omap_device_deactivate(). | |
787 | */ | |
788 | int omap_device_idle(struct platform_device *pdev) | |
789 | { | |
790 | int ret; | |
791 | struct omap_device *od; | |
792 | ||
8f0d69de | 793 | od = to_omap_device(pdev); |
b04b65ab PW |
794 | |
795 | if (od->_state != OMAP_DEVICE_STATE_ENABLED) { | |
49882c99 KH |
796 | dev_warn(&pdev->dev, |
797 | "omap_device: %s() called from invalid state %d\n", | |
798 | __func__, od->_state); | |
b04b65ab PW |
799 | return -EINVAL; |
800 | } | |
801 | ||
802 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); | |
803 | ||
804 | od->_state = OMAP_DEVICE_STATE_IDLE; | |
805 | ||
806 | return ret; | |
807 | } | |
808 | ||
809 | /** | |
810 | * omap_device_shutdown - shut down an omap_device | |
811 | * @od: struct omap_device * to shut down | |
812 | * | |
813 | * Shut down omap_device @od by calling all .deactivate_func() entries | |
814 | * in the omap_device's pm_lats table and then shutting down all of | |
815 | * the underlying omap_hwmods. Used when a device is being "removed" | |
816 | * or a device driver is being unloaded. Returns -EINVAL if the | |
817 | * omap_device is not currently enabled or idle, or passes along the | |
818 | * return value of _omap_device_deactivate(). | |
819 | */ | |
820 | int omap_device_shutdown(struct platform_device *pdev) | |
821 | { | |
822 | int ret, i; | |
823 | struct omap_device *od; | |
b04b65ab | 824 | |
8f0d69de | 825 | od = to_omap_device(pdev); |
b04b65ab PW |
826 | |
827 | if (od->_state != OMAP_DEVICE_STATE_ENABLED && | |
828 | od->_state != OMAP_DEVICE_STATE_IDLE) { | |
49882c99 KH |
829 | dev_warn(&pdev->dev, |
830 | "omap_device: %s() called from invalid state %d\n", | |
831 | __func__, od->_state); | |
b04b65ab PW |
832 | return -EINVAL; |
833 | } | |
834 | ||
835 | ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT); | |
836 | ||
f39f4898 KVA |
837 | for (i = 0; i < od->hwmods_cnt; i++) |
838 | omap_hwmod_shutdown(od->hwmods[i]); | |
b04b65ab PW |
839 | |
840 | od->_state = OMAP_DEVICE_STATE_SHUTDOWN; | |
841 | ||
842 | return ret; | |
843 | } | |
844 | ||
845 | /** | |
846 | * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim | |
847 | * @od: struct omap_device * | |
848 | * | |
849 | * When a device's maximum wakeup latency limit changes, call some of | |
850 | * the .activate_func or .deactivate_func function pointers in the | |
851 | * omap_device's pm_lats array to ensure that the device's maximum | |
852 | * wakeup latency is less than or equal to the new latency limit. | |
853 | * Intended to be called by OMAP PM code whenever a device's maximum | |
854 | * wakeup latency limit changes (e.g., via | |
855 | * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be | |
856 | * done (e.g., if the omap_device is not currently idle, or if the | |
857 | * wakeup latency is already current with the new limit) or passes | |
858 | * along the return value of _omap_device_deactivate() or | |
859 | * _omap_device_activate(). | |
860 | */ | |
861 | int omap_device_align_pm_lat(struct platform_device *pdev, | |
862 | u32 new_wakeup_lat_limit) | |
863 | { | |
864 | int ret = -EINVAL; | |
865 | struct omap_device *od; | |
866 | ||
8f0d69de | 867 | od = to_omap_device(pdev); |
b04b65ab PW |
868 | |
869 | if (new_wakeup_lat_limit == od->dev_wakeup_lat) | |
870 | return 0; | |
871 | ||
872 | od->_dev_wakeup_lat_limit = new_wakeup_lat_limit; | |
873 | ||
874 | if (od->_state != OMAP_DEVICE_STATE_IDLE) | |
875 | return 0; | |
876 | else if (new_wakeup_lat_limit > od->dev_wakeup_lat) | |
877 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); | |
878 | else if (new_wakeup_lat_limit < od->dev_wakeup_lat) | |
879 | ret = _omap_device_activate(od, USE_WAKEUP_LAT); | |
880 | ||
881 | return ret; | |
882 | } | |
883 | ||
884 | /** | |
885 | * omap_device_get_pwrdm - return the powerdomain * associated with @od | |
886 | * @od: struct omap_device * | |
887 | * | |
888 | * Return the powerdomain associated with the first underlying | |
889 | * omap_hwmod for this omap_device. Intended for use by core OMAP PM | |
890 | * code. Returns NULL on error or a struct powerdomain * upon | |
891 | * success. | |
892 | */ | |
893 | struct powerdomain *omap_device_get_pwrdm(struct omap_device *od) | |
894 | { | |
895 | /* | |
896 | * XXX Assumes that all omap_hwmod powerdomains are identical. | |
897 | * This may not necessarily be true. There should be a sanity | |
898 | * check in here to WARN() if any difference appears. | |
899 | */ | |
900 | if (!od->hwmods_cnt) | |
901 | return NULL; | |
902 | ||
903 | return omap_hwmod_get_pwrdm(od->hwmods[0]); | |
904 | } | |
905 | ||
db2a60bf PW |
906 | /** |
907 | * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base | |
908 | * @od: struct omap_device * | |
909 | * | |
910 | * Return the MPU's virtual address for the base of the hwmod, from | |
911 | * the ioremap() that the hwmod code does. Only valid if there is one | |
912 | * hwmod associated with this device. Returns NULL if there are zero | |
913 | * or more than one hwmods associated with this omap_device; | |
914 | * otherwise, passes along the return value from | |
915 | * omap_hwmod_get_mpu_rt_va(). | |
916 | */ | |
917 | void __iomem *omap_device_get_rt_va(struct omap_device *od) | |
918 | { | |
919 | if (od->hwmods_cnt != 1) | |
920 | return NULL; | |
921 | ||
922 | return omap_hwmod_get_mpu_rt_va(od->hwmods[0]); | |
923 | } | |
924 | ||
1f8a7d52 NM |
925 | /** |
926 | * omap_device_get_by_hwmod_name() - convert a hwmod name to | |
927 | * device pointer. | |
928 | * @oh_name: name of the hwmod device | |
929 | * | |
930 | * Returns back a struct device * pointer associated with a hwmod | |
931 | * device represented by a hwmod_name | |
932 | */ | |
933 | struct device *omap_device_get_by_hwmod_name(const char *oh_name) | |
934 | { | |
935 | struct omap_hwmod *oh; | |
936 | ||
937 | if (!oh_name) { | |
938 | WARN(1, "%s: no hwmod name!\n", __func__); | |
939 | return ERR_PTR(-EINVAL); | |
940 | } | |
941 | ||
942 | oh = omap_hwmod_lookup(oh_name); | |
943 | if (IS_ERR_OR_NULL(oh)) { | |
944 | WARN(1, "%s: no hwmod for %s\n", __func__, | |
945 | oh_name); | |
946 | return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV); | |
947 | } | |
948 | if (IS_ERR_OR_NULL(oh->od)) { | |
949 | WARN(1, "%s: no omap_device for %s\n", __func__, | |
950 | oh_name); | |
951 | return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV); | |
952 | } | |
953 | ||
954 | if (IS_ERR_OR_NULL(oh->od->pdev)) | |
955 | return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV); | |
956 | ||
957 | return &oh->od->pdev->dev; | |
958 | } | |
959 | EXPORT_SYMBOL(omap_device_get_by_hwmod_name); | |
960 | ||
b04b65ab PW |
961 | /* |
962 | * Public functions intended for use in omap_device_pm_latency | |
963 | * .activate_func and .deactivate_func function pointers | |
964 | */ | |
965 | ||
966 | /** | |
967 | * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods | |
968 | * @od: struct omap_device *od | |
969 | * | |
970 | * Enable all underlying hwmods. Returns 0. | |
971 | */ | |
972 | int omap_device_enable_hwmods(struct omap_device *od) | |
973 | { | |
b04b65ab PW |
974 | int i; |
975 | ||
f39f4898 KVA |
976 | for (i = 0; i < od->hwmods_cnt; i++) |
977 | omap_hwmod_enable(od->hwmods[i]); | |
b04b65ab PW |
978 | |
979 | /* XXX pass along return value here? */ | |
980 | return 0; | |
981 | } | |
982 | ||
983 | /** | |
984 | * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods | |
985 | * @od: struct omap_device *od | |
986 | * | |
987 | * Idle all underlying hwmods. Returns 0. | |
988 | */ | |
989 | int omap_device_idle_hwmods(struct omap_device *od) | |
990 | { | |
b04b65ab PW |
991 | int i; |
992 | ||
f39f4898 KVA |
993 | for (i = 0; i < od->hwmods_cnt; i++) |
994 | omap_hwmod_idle(od->hwmods[i]); | |
b04b65ab PW |
995 | |
996 | /* XXX pass along return value here? */ | |
997 | return 0; | |
998 | } | |
999 | ||
1000 | /** | |
1001 | * omap_device_disable_clocks - disable all main and interface clocks | |
1002 | * @od: struct omap_device *od | |
1003 | * | |
1004 | * Disable the main functional clock and interface clock for all of the | |
1005 | * omap_hwmods associated with the omap_device. Returns 0. | |
1006 | */ | |
1007 | int omap_device_disable_clocks(struct omap_device *od) | |
1008 | { | |
b04b65ab PW |
1009 | int i; |
1010 | ||
f39f4898 KVA |
1011 | for (i = 0; i < od->hwmods_cnt; i++) |
1012 | omap_hwmod_disable_clocks(od->hwmods[i]); | |
b04b65ab PW |
1013 | |
1014 | /* XXX pass along return value here? */ | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | /** | |
1019 | * omap_device_enable_clocks - enable all main and interface clocks | |
1020 | * @od: struct omap_device *od | |
1021 | * | |
1022 | * Enable the main functional clock and interface clock for all of the | |
1023 | * omap_hwmods associated with the omap_device. Returns 0. | |
1024 | */ | |
1025 | int omap_device_enable_clocks(struct omap_device *od) | |
1026 | { | |
b04b65ab PW |
1027 | int i; |
1028 | ||
f39f4898 KVA |
1029 | for (i = 0; i < od->hwmods_cnt; i++) |
1030 | omap_hwmod_enable_clocks(od->hwmods[i]); | |
b04b65ab PW |
1031 | |
1032 | /* XXX pass along return value here? */ | |
1033 | return 0; | |
1034 | } | |
0d5e8252 KH |
1035 | |
1036 | struct device omap_device_parent = { | |
1037 | .init_name = "omap", | |
1038 | .parent = &platform_bus, | |
1039 | }; | |
1040 | ||
1041 | static int __init omap_device_init(void) | |
1042 | { | |
1043 | return device_register(&omap_device_parent); | |
1044 | } | |
1045 | core_initcall(omap_device_init); |