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