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