ARM: OMAP: omap_device: Fix up resource names when booted with devicetree
[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
3956a1a0
PU
373 /* Fix up missing resource names */
374 for (i = 0; i < pdev->num_resources; i++) {
375 struct resource *r = &pdev->resource[i];
376
377 if (r->name == NULL)
378 r->name = dev_name(&pdev->dev);
379 }
380
dc2d07eb
BC
381 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
382 omap_device_disable_idle_on_suspend(pdev);
383
384 pdev->dev.pm_domain = &omap_device_pm_domain;
385
386odbfd_exit1:
387 kfree(hwmods);
388odbfd_exit:
389 return ret;
390}
391
392static int _omap_device_notifier_call(struct notifier_block *nb,
393 unsigned long event, void *dev)
394{
395 struct platform_device *pdev = to_platform_device(dev);
396
397 switch (event) {
398 case BUS_NOTIFY_ADD_DEVICE:
399 if (pdev->dev.of_node)
400 omap_device_build_from_dt(pdev);
401 break;
402
403 case BUS_NOTIFY_DEL_DEVICE:
404 if (pdev->archdata.od)
405 omap_device_delete(pdev->archdata.od);
406 break;
407 }
408
409 return NOTIFY_DONE;
410}
411
412
b04b65ab
PW
413/* Public functions for use by core code */
414
c80705aa
KH
415/**
416 * omap_device_get_context_loss_count - get lost context count
417 * @od: struct omap_device *
418 *
419 * Using the primary hwmod, query the context loss count for this
420 * device.
421 *
422 * Callers should consider context for this device lost any time this
423 * function returns a value different than the value the caller got
424 * the last time it called this function.
425 *
426 * If any hwmods exist for the omap_device assoiated with @pdev,
427 * return the context loss counter for that hwmod, otherwise return
428 * zero.
429 */
fc013873 430int omap_device_get_context_loss_count(struct platform_device *pdev)
c80705aa
KH
431{
432 struct omap_device *od;
433 u32 ret = 0;
434
8f0d69de 435 od = to_omap_device(pdev);
c80705aa
KH
436
437 if (od->hwmods_cnt)
438 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
439
440 return ret;
441}
442
b04b65ab
PW
443/**
444 * omap_device_count_resources - count number of struct resource entries needed
445 * @od: struct omap_device *
446 *
447 * Count the number of struct resource entries needed for this
448 * omap_device @od. Used by omap_device_build_ss() to determine how
449 * much memory to allocate before calling
450 * omap_device_fill_resources(). Returns the count.
451 */
a2a28ad9 452static int omap_device_count_resources(struct omap_device *od)
b04b65ab 453{
b04b65ab
PW
454 int c = 0;
455 int i;
456
f39f4898
KVA
457 for (i = 0; i < od->hwmods_cnt; i++)
458 c += omap_hwmod_count_resources(od->hwmods[i]);
b04b65ab
PW
459
460 pr_debug("omap_device: %s: counted %d total resources across %d "
d66b3fe4 461 "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
b04b65ab
PW
462
463 return c;
464}
465
466/**
467 * omap_device_fill_resources - fill in array of struct resource
468 * @od: struct omap_device *
469 * @res: pointer to an array of struct resource to be filled in
470 *
471 * Populate one or more empty struct resource pointed to by @res with
472 * the resource data for this omap_device @od. Used by
473 * omap_device_build_ss() after calling omap_device_count_resources().
474 * Ideally this function would not be needed at all. If omap_device
475 * replaces platform_device, then we can specify our own
476 * get_resource()/ get_irq()/etc functions that use the underlying
477 * omap_hwmod information. Or if platform_device is extended to use
478 * subarchitecture-specific function pointers, the various
479 * platform_device functions can simply call omap_device internal
480 * functions to get device resources. Hacking around the existing
481 * platform_device code wastes memory. Returns 0.
482 */
a2a28ad9
KH
483static int omap_device_fill_resources(struct omap_device *od,
484 struct resource *res)
b04b65ab 485{
b04b65ab
PW
486 int i, r;
487
f39f4898
KVA
488 for (i = 0; i < od->hwmods_cnt; i++) {
489 r = omap_hwmod_fill_resources(od->hwmods[i], res);
b04b65ab 490 res += r;
b04b65ab
PW
491 }
492
493 return 0;
494}
495
a4f6cdb0
BC
496/**
497 * omap_device_alloc - allocate an omap_device
498 * @pdev: platform_device that will be included in this omap_device
499 * @oh: ptr to the single omap_hwmod that backs this omap_device
500 * @pdata: platform_data ptr to associate with the platform_device
501 * @pdata_len: amount of memory pointed to by @pdata
502 * @pm_lats: pointer to a omap_device_pm_latency array for this device
503 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
504 *
505 * Convenience function for allocating an omap_device structure and filling
506 * hwmods, resources and pm_latency attributes.
507 *
508 * Returns an struct omap_device pointer or ERR_PTR() on error;
509 */
993e4fbd 510struct omap_device *omap_device_alloc(struct platform_device *pdev,
a4f6cdb0
BC
511 struct omap_hwmod **ohs, int oh_cnt,
512 struct omap_device_pm_latency *pm_lats,
513 int pm_lats_cnt)
514{
515 int ret = -ENOMEM;
516 struct omap_device *od;
517 struct resource *res = NULL;
518 int i, res_count;
519 struct omap_hwmod **hwmods;
520
521 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
522 if (!od) {
523 ret = -ENOMEM;
524 goto oda_exit1;
525 }
526 od->hwmods_cnt = oh_cnt;
527
528 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
529 if (!hwmods)
530 goto oda_exit2;
531
532 od->hwmods = hwmods;
533 od->pdev = pdev;
534
535 /*
536 * HACK: Ideally the resources from DT should match, and hwmod
537 * should just add the missing ones. Since the name is not
538 * properly populated by DT, stick to hwmod resources only.
539 */
540 if (pdev->num_resources && pdev->resource)
541 dev_warn(&pdev->dev, "%s(): resources already allocated %d\n",
542 __func__, pdev->num_resources);
543
544 res_count = omap_device_count_resources(od);
545 if (res_count > 0) {
546 dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n",
547 __func__, res_count);
548 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
549 if (!res)
550 goto oda_exit3;
551
552 omap_device_fill_resources(od, res);
553
554 ret = platform_device_add_resources(pdev, res, res_count);
555 kfree(res);
556
557 if (ret)
558 goto oda_exit3;
559 }
560
561 if (!pm_lats) {
562 pm_lats = omap_default_latency;
563 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
564 }
565
566 od->pm_lats_cnt = pm_lats_cnt;
567 od->pm_lats = kmemdup(pm_lats,
568 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
569 GFP_KERNEL);
570 if (!od->pm_lats)
571 goto oda_exit3;
572
573 pdev->archdata.od = od;
574
575 for (i = 0; i < oh_cnt; i++) {
576 hwmods[i]->od = od;
577 _add_hwmod_clocks_clkdev(od, hwmods[i]);
578 }
579
580 return od;
581
582oda_exit3:
583 kfree(hwmods);
584oda_exit2:
585 kfree(od);
586oda_exit1:
587 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
588
589 return ERR_PTR(ret);
590}
591
993e4fbd 592void omap_device_delete(struct omap_device *od)
a4f6cdb0 593{
dc2d07eb
BC
594 if (!od)
595 return;
596
a4f6cdb0
BC
597 od->pdev->archdata.od = NULL;
598 kfree(od->pm_lats);
599 kfree(od->hwmods);
600 kfree(od);
601}
602
b04b65ab
PW
603/**
604 * omap_device_build - build and register an omap_device with one omap_hwmod
605 * @pdev_name: name of the platform_device driver to use
606 * @pdev_id: this platform_device's connection ID
607 * @oh: ptr to the single omap_hwmod that backs this omap_device
608 * @pdata: platform_data ptr to associate with the platform_device
609 * @pdata_len: amount of memory pointed to by @pdata
610 * @pm_lats: pointer to a omap_device_pm_latency array for this device
611 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
c23a97d3 612 * @is_early_device: should the device be registered as an early device or not
b04b65ab
PW
613 *
614 * Convenience function for building and registering a single
615 * omap_device record, which in turn builds and registers a
616 * platform_device record. See omap_device_build_ss() for more
617 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
618 * passes along the return value of omap_device_build_ss().
619 */
9cf793f9 620struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
b04b65ab
PW
621 struct omap_hwmod *oh, void *pdata,
622 int pdata_len,
623 struct omap_device_pm_latency *pm_lats,
c23a97d3 624 int pm_lats_cnt, int is_early_device)
b04b65ab
PW
625{
626 struct omap_hwmod *ohs[] = { oh };
627
628 if (!oh)
629 return ERR_PTR(-EINVAL);
630
631 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
c23a97d3
TG
632 pdata_len, pm_lats, pm_lats_cnt,
633 is_early_device);
b04b65ab
PW
634}
635
636/**
637 * omap_device_build_ss - build and register an omap_device with multiple hwmods
638 * @pdev_name: name of the platform_device driver to use
639 * @pdev_id: this platform_device's connection ID
640 * @oh: ptr to the single omap_hwmod that backs this omap_device
641 * @pdata: platform_data ptr to associate with the platform_device
642 * @pdata_len: amount of memory pointed to by @pdata
643 * @pm_lats: pointer to a omap_device_pm_latency array for this device
644 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
c23a97d3 645 * @is_early_device: should the device be registered as an early device or not
b04b65ab
PW
646 *
647 * Convenience function for building and registering an omap_device
648 * subsystem record. Subsystem records consist of multiple
649 * omap_hwmods. This function in turn builds and registers a
650 * platform_device record. Returns an ERR_PTR() on error, or passes
651 * along the return value of omap_device_register().
652 */
9cf793f9 653struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
b04b65ab
PW
654 struct omap_hwmod **ohs, int oh_cnt,
655 void *pdata, int pdata_len,
656 struct omap_device_pm_latency *pm_lats,
c23a97d3 657 int pm_lats_cnt, int is_early_device)
b04b65ab
PW
658{
659 int ret = -ENOMEM;
d66b3fe4 660 struct platform_device *pdev;
b04b65ab 661 struct omap_device *od;
b04b65ab
PW
662
663 if (!ohs || oh_cnt == 0 || !pdev_name)
664 return ERR_PTR(-EINVAL);
665
666 if (!pdata && pdata_len > 0)
667 return ERR_PTR(-EINVAL);
668
d66b3fe4
KH
669 pdev = platform_device_alloc(pdev_name, pdev_id);
670 if (!pdev) {
671 ret = -ENOMEM;
672 goto odbs_exit;
673 }
674
a4f6cdb0
BC
675 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
676 if (pdev->id != -1)
677 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
678 else
679 dev_set_name(&pdev->dev, "%s", pdev->name);
b04b65ab 680
a4f6cdb0
BC
681 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
682 if (!od)
d66b3fe4 683 goto odbs_exit1;
d66b3fe4
KH
684
685 ret = platform_device_add_data(pdev, pdata, pdata_len);
49b368a6 686 if (ret)
a4f6cdb0 687 goto odbs_exit2;
b04b65ab 688
c23a97d3 689 if (is_early_device)
d66b3fe4 690 ret = omap_early_device_register(pdev);
c23a97d3 691 else
d66b3fe4
KH
692 ret = omap_device_register(pdev);
693 if (ret)
a4f6cdb0 694 goto odbs_exit2;
06563581 695
d66b3fe4 696 return pdev;
b04b65ab 697
d66b3fe4 698odbs_exit2:
a4f6cdb0 699 omap_device_delete(od);
d66b3fe4
KH
700odbs_exit1:
701 platform_device_put(pdev);
702odbs_exit:
b04b65ab
PW
703
704 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
705
706 return ERR_PTR(ret);
707}
708
c23a97d3
TG
709/**
710 * omap_early_device_register - register an omap_device as an early platform
711 * device.
712 * @od: struct omap_device * to register
713 *
714 * Register the omap_device structure. This currently just calls
715 * platform_early_add_device() on the underlying platform_device.
716 * Returns 0 by default.
717 */
9cf793f9 718static int __init omap_early_device_register(struct platform_device *pdev)
c23a97d3
TG
719{
720 struct platform_device *devices[1];
721
bfae4f8f 722 devices[0] = pdev;
c23a97d3
TG
723 early_platform_add_devices(devices, 1);
724 return 0;
725}
726
256a5435 727#ifdef CONFIG_PM_RUNTIME
638080c3
KH
728static int _od_runtime_suspend(struct device *dev)
729{
730 struct platform_device *pdev = to_platform_device(dev);
345f79b3 731 int ret;
638080c3 732
345f79b3
KH
733 ret = pm_generic_runtime_suspend(dev);
734
735 if (!ret)
736 omap_device_idle(pdev);
737
738 return ret;
739}
740
741static int _od_runtime_idle(struct device *dev)
742{
743 return pm_generic_runtime_idle(dev);
638080c3
KH
744}
745
746static int _od_runtime_resume(struct device *dev)
747{
748 struct platform_device *pdev = to_platform_device(dev);
749
345f79b3
KH
750 omap_device_enable(pdev);
751
752 return pm_generic_runtime_resume(dev);
638080c3 753}
256a5435 754#endif
638080c3 755
c03f007a
KH
756#ifdef CONFIG_SUSPEND
757static int _od_suspend_noirq(struct device *dev)
758{
759 struct platform_device *pdev = to_platform_device(dev);
760 struct omap_device *od = to_omap_device(pdev);
761 int ret;
762
763 ret = pm_generic_suspend_noirq(dev);
764
765 if (!ret && !pm_runtime_status_suspended(dev)) {
766 if (pm_generic_runtime_suspend(dev) == 0) {
b7c39a3f
PW
767 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
768 omap_device_idle(pdev);
c03f007a
KH
769 od->flags |= OMAP_DEVICE_SUSPENDED;
770 }
771 }
772
773 return ret;
774}
775
776static int _od_resume_noirq(struct device *dev)
777{
778 struct platform_device *pdev = to_platform_device(dev);
779 struct omap_device *od = to_omap_device(pdev);
780
781 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
782 !pm_runtime_status_suspended(dev)) {
783 od->flags &= ~OMAP_DEVICE_SUSPENDED;
b7c39a3f
PW
784 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
785 omap_device_enable(pdev);
c03f007a
KH
786 pm_generic_runtime_resume(dev);
787 }
788
789 return pm_generic_resume_noirq(dev);
790}
126caf13
KH
791#else
792#define _od_suspend_noirq NULL
793#define _od_resume_noirq NULL
c03f007a
KH
794#endif
795
3ec2decb 796struct dev_pm_domain omap_device_pm_domain = {
638080c3 797 .ops = {
256a5435
KH
798 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
799 _od_runtime_idle)
638080c3 800 USE_PLATFORM_PM_SLEEP_OPS
ff35336d
KH
801 .suspend_noirq = _od_suspend_noirq,
802 .resume_noirq = _od_resume_noirq,
638080c3
KH
803 }
804};
805
b04b65ab
PW
806/**
807 * omap_device_register - register an omap_device with one omap_hwmod
808 * @od: struct omap_device * to register
809 *
810 * Register the omap_device structure. This currently just calls
811 * platform_device_register() on the underlying platform_device.
812 * Returns the return value of platform_device_register().
813 */
993e4fbd 814int omap_device_register(struct platform_device *pdev)
b04b65ab 815{
bfae4f8f 816 pr_debug("omap_device: %s: registering\n", pdev->name);
b04b65ab 817
bfae4f8f 818 pdev->dev.pm_domain = &omap_device_pm_domain;
d66b3fe4 819 return platform_device_add(pdev);
b04b65ab
PW
820}
821
822
823/* Public functions for use by device drivers through struct platform_data */
824
825/**
826 * omap_device_enable - fully activate an omap_device
827 * @od: struct omap_device * to activate
828 *
829 * Do whatever is necessary for the hwmods underlying omap_device @od
830 * to be accessible and ready to operate. This generally involves
831 * enabling clocks, setting SYSCONFIG registers; and in the future may
832 * involve remuxing pins. Device drivers should call this function
833 * (through platform_data function pointers) where they would normally
834 * enable clocks, etc. Returns -EINVAL if called when the omap_device
835 * is already enabled, or passes along the return value of
836 * _omap_device_activate().
837 */
838int omap_device_enable(struct platform_device *pdev)
839{
840 int ret;
841 struct omap_device *od;
842
8f0d69de 843 od = to_omap_device(pdev);
b04b65ab
PW
844
845 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
49882c99
KH
846 dev_warn(&pdev->dev,
847 "omap_device: %s() called from invalid state %d\n",
848 __func__, od->_state);
b04b65ab
PW
849 return -EINVAL;
850 }
851
852 /* Enable everything if we're enabling this device from scratch */
853 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
854 od->pm_lat_level = od->pm_lats_cnt;
855
856 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
857
858 od->dev_wakeup_lat = 0;
5f1b6ef7 859 od->_dev_wakeup_lat_limit = UINT_MAX;
b04b65ab
PW
860 od->_state = OMAP_DEVICE_STATE_ENABLED;
861
862 return ret;
863}
864
865/**
866 * omap_device_idle - idle an omap_device
867 * @od: struct omap_device * to idle
868 *
869 * Idle omap_device @od by calling as many .deactivate_func() entries
870 * in the omap_device's pm_lats table as is possible without exceeding
871 * the device's maximum wakeup latency limit, pm_lat_limit. Device
872 * drivers should call this function (through platform_data function
873 * pointers) where they would normally disable clocks after operations
874 * complete, etc.. Returns -EINVAL if the omap_device is not
875 * currently enabled, or passes along the return value of
876 * _omap_device_deactivate().
877 */
878int omap_device_idle(struct platform_device *pdev)
879{
880 int ret;
881 struct omap_device *od;
882
8f0d69de 883 od = to_omap_device(pdev);
b04b65ab
PW
884
885 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
49882c99
KH
886 dev_warn(&pdev->dev,
887 "omap_device: %s() called from invalid state %d\n",
888 __func__, od->_state);
b04b65ab
PW
889 return -EINVAL;
890 }
891
892 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
893
894 od->_state = OMAP_DEVICE_STATE_IDLE;
895
896 return ret;
897}
898
899/**
900 * omap_device_shutdown - shut down an omap_device
901 * @od: struct omap_device * to shut down
902 *
903 * Shut down omap_device @od by calling all .deactivate_func() entries
904 * in the omap_device's pm_lats table and then shutting down all of
905 * the underlying omap_hwmods. Used when a device is being "removed"
906 * or a device driver is being unloaded. Returns -EINVAL if the
907 * omap_device is not currently enabled or idle, or passes along the
908 * return value of _omap_device_deactivate().
909 */
910int omap_device_shutdown(struct platform_device *pdev)
911{
912 int ret, i;
913 struct omap_device *od;
b04b65ab 914
8f0d69de 915 od = to_omap_device(pdev);
b04b65ab
PW
916
917 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
918 od->_state != OMAP_DEVICE_STATE_IDLE) {
49882c99
KH
919 dev_warn(&pdev->dev,
920 "omap_device: %s() called from invalid state %d\n",
921 __func__, od->_state);
b04b65ab
PW
922 return -EINVAL;
923 }
924
925 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
926
f39f4898
KVA
927 for (i = 0; i < od->hwmods_cnt; i++)
928 omap_hwmod_shutdown(od->hwmods[i]);
b04b65ab
PW
929
930 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
931
932 return ret;
933}
934
935/**
936 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
937 * @od: struct omap_device *
938 *
939 * When a device's maximum wakeup latency limit changes, call some of
940 * the .activate_func or .deactivate_func function pointers in the
941 * omap_device's pm_lats array to ensure that the device's maximum
942 * wakeup latency is less than or equal to the new latency limit.
943 * Intended to be called by OMAP PM code whenever a device's maximum
944 * wakeup latency limit changes (e.g., via
945 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
946 * done (e.g., if the omap_device is not currently idle, or if the
947 * wakeup latency is already current with the new limit) or passes
948 * along the return value of _omap_device_deactivate() or
949 * _omap_device_activate().
950 */
951int omap_device_align_pm_lat(struct platform_device *pdev,
952 u32 new_wakeup_lat_limit)
953{
954 int ret = -EINVAL;
955 struct omap_device *od;
956
8f0d69de 957 od = to_omap_device(pdev);
b04b65ab
PW
958
959 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
960 return 0;
961
962 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
963
964 if (od->_state != OMAP_DEVICE_STATE_IDLE)
965 return 0;
966 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
967 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
968 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
969 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
970
971 return ret;
972}
973
974/**
975 * omap_device_get_pwrdm - return the powerdomain * associated with @od
976 * @od: struct omap_device *
977 *
978 * Return the powerdomain associated with the first underlying
979 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
980 * code. Returns NULL on error or a struct powerdomain * upon
981 * success.
982 */
983struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
984{
985 /*
986 * XXX Assumes that all omap_hwmod powerdomains are identical.
987 * This may not necessarily be true. There should be a sanity
988 * check in here to WARN() if any difference appears.
989 */
990 if (!od->hwmods_cnt)
991 return NULL;
992
993 return omap_hwmod_get_pwrdm(od->hwmods[0]);
994}
995
db2a60bf
PW
996/**
997 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
998 * @od: struct omap_device *
999 *
1000 * Return the MPU's virtual address for the base of the hwmod, from
1001 * the ioremap() that the hwmod code does. Only valid if there is one
1002 * hwmod associated with this device. Returns NULL if there are zero
1003 * or more than one hwmods associated with this omap_device;
1004 * otherwise, passes along the return value from
1005 * omap_hwmod_get_mpu_rt_va().
1006 */
1007void __iomem *omap_device_get_rt_va(struct omap_device *od)
1008{
1009 if (od->hwmods_cnt != 1)
1010 return NULL;
1011
1012 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1013}
1014
1f8a7d52
NM
1015/**
1016 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1017 * device pointer.
1018 * @oh_name: name of the hwmod device
1019 *
1020 * Returns back a struct device * pointer associated with a hwmod
1021 * device represented by a hwmod_name
1022 */
1023struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1024{
1025 struct omap_hwmod *oh;
1026
1027 if (!oh_name) {
1028 WARN(1, "%s: no hwmod name!\n", __func__);
1029 return ERR_PTR(-EINVAL);
1030 }
1031
1032 oh = omap_hwmod_lookup(oh_name);
1033 if (IS_ERR_OR_NULL(oh)) {
1034 WARN(1, "%s: no hwmod for %s\n", __func__,
1035 oh_name);
1036 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1037 }
1038 if (IS_ERR_OR_NULL(oh->od)) {
1039 WARN(1, "%s: no omap_device for %s\n", __func__,
1040 oh_name);
1041 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1042 }
1043
1044 if (IS_ERR_OR_NULL(oh->od->pdev))
1045 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1046
1047 return &oh->od->pdev->dev;
1048}
1049EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1050
b04b65ab
PW
1051/*
1052 * Public functions intended for use in omap_device_pm_latency
1053 * .activate_func and .deactivate_func function pointers
1054 */
1055
1056/**
1057 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1058 * @od: struct omap_device *od
1059 *
1060 * Enable all underlying hwmods. Returns 0.
1061 */
1062int omap_device_enable_hwmods(struct omap_device *od)
1063{
b04b65ab
PW
1064 int i;
1065
f39f4898
KVA
1066 for (i = 0; i < od->hwmods_cnt; i++)
1067 omap_hwmod_enable(od->hwmods[i]);
b04b65ab
PW
1068
1069 /* XXX pass along return value here? */
1070 return 0;
1071}
1072
1073/**
1074 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1075 * @od: struct omap_device *od
1076 *
1077 * Idle all underlying hwmods. Returns 0.
1078 */
1079int omap_device_idle_hwmods(struct omap_device *od)
1080{
b04b65ab
PW
1081 int i;
1082
f39f4898
KVA
1083 for (i = 0; i < od->hwmods_cnt; i++)
1084 omap_hwmod_idle(od->hwmods[i]);
b04b65ab
PW
1085
1086 /* XXX pass along return value here? */
1087 return 0;
1088}
1089
1090/**
1091 * omap_device_disable_clocks - disable all main and interface clocks
1092 * @od: struct omap_device *od
1093 *
1094 * Disable the main functional clock and interface clock for all of the
1095 * omap_hwmods associated with the omap_device. Returns 0.
1096 */
1097int omap_device_disable_clocks(struct omap_device *od)
1098{
b04b65ab
PW
1099 int i;
1100
f39f4898
KVA
1101 for (i = 0; i < od->hwmods_cnt; i++)
1102 omap_hwmod_disable_clocks(od->hwmods[i]);
b04b65ab
PW
1103
1104 /* XXX pass along return value here? */
1105 return 0;
1106}
1107
1108/**
1109 * omap_device_enable_clocks - enable all main and interface clocks
1110 * @od: struct omap_device *od
1111 *
1112 * Enable the main functional clock and interface clock for all of the
1113 * omap_hwmods associated with the omap_device. Returns 0.
1114 */
1115int omap_device_enable_clocks(struct omap_device *od)
1116{
b04b65ab
PW
1117 int i;
1118
f39f4898
KVA
1119 for (i = 0; i < od->hwmods_cnt; i++)
1120 omap_hwmod_enable_clocks(od->hwmods[i]);
b04b65ab
PW
1121
1122 /* XXX pass along return value here? */
1123 return 0;
1124}
0d5e8252 1125
dc2d07eb
BC
1126static struct notifier_block platform_nb = {
1127 .notifier_call = _omap_device_notifier_call,
1128};
1129
0d5e8252
KH
1130static int __init omap_device_init(void)
1131{
dc2d07eb 1132 bus_register_notifier(&platform_bus_type, &platform_nb);
3ec2decb 1133 return 0;
0d5e8252
KH
1134}
1135core_initcall(omap_device_init);
This page took 0.228833 seconds and 5 git commands to generate.