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