blob: cd90bedd9306fb6389d5f9eef0d6061db1c02076 [file] [log] [blame]
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001/*
2 * omap_device implementation
3 *
Paul Walmsley887adea2010-07-26 16:34:33 -06004 * Copyright (C) 2009-2010 Nokia Corporation
Paul Walmsley4788da22010-05-18 20:24:05 -06005 * Paul Walmsley, Kevin Hilman
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03006 *
7 * Developed in collaboration with (alphabetical order): Benoit
Paul Walmsley4788da22010-05-18 20:24:05 -06008 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03009 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090082#include <linux/slab.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030083#include <linux/err.h>
84#include <linux/io.h>
Partha Basak4ef7aca2010-09-21 19:23:04 +020085#include <linux/clk.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070086#include <linux/clkdev.h>
Kevin Hilman345f79b2011-05-31 16:08:09 -070087#include <linux/pm_runtime.h>
Benoit Coussondc2d07e2011-08-10 13:32:08 +020088#include <linux/of.h>
89#include <linux/notifier.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030090
Tony Lindgrence491cf2009-10-20 09:40:47 -070091#include <plat/omap_device.h>
92#include <plat/omap_hwmod.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070093#include <plat/clock.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030094
95/* These parameters are passed to _omap_device_{de,}activate() */
96#define USE_WAKEUP_LAT 0
97#define IGNORE_WAKEUP_LAT 1
98
Kevin Hilmanbfae4f82011-07-21 14:47:53 -070099static int omap_device_register(struct platform_device *pdev);
100static int omap_early_device_register(struct platform_device *pdev);
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200101static 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);
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200105static void omap_device_delete(struct omap_device *od);
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200106
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700107
Benoit Coussonb7b5bc92011-08-09 16:54:19 +0200108static 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
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300116/* Private functions */
117
118/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300119 * _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{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700134 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300135
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700136 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300137
138 while (od->pm_lat_level > 0) {
139 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700140 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300141
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
Kevin Hilmand2292662009-12-08 16:34:23 -0700150 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300151
152 /* XXX check return code */
153 odpl->activate_func(od);
154
Kevin Hilmand2292662009-12-08 16:34:23 -0700155 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300156
Tony Lindgrenf0594292009-10-19 15:25:24 -0700157 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700158 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300159
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700160 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700161 "omap_device: pm_lat %d: activate: elapsed time "
162 "%llu nsec\n", od->pm_lat_level, act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300163
Kevin Hilman9799aca2010-01-26 20:13:02 -0700164 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;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700168 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300169 "new worst case activate latency "
170 "%d: %llu\n",
171 od->pm_lat_level, act_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700172 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700173 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700174 "activate latency %d "
175 "higher than exptected. (%llu > %d)\n",
176 od->pm_lat_level, act_lat,
177 odpl->activate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700178 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300179
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{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700202 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300203
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700204 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300205
206 while (od->pm_lat_level < od->pm_lats_cnt) {
207 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700208 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300209
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
Kevin Hilmand2292662009-12-08 16:34:23 -0700217 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300218
219 /* XXX check return code */
220 odpl->deactivate_func(od);
221
Kevin Hilmand2292662009-12-08 16:34:23 -0700222 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300223
Tony Lindgrenf0594292009-10-19 15:25:24 -0700224 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700225 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300226
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700227 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700228 "omap_device: pm_lat %d: deactivate: elapsed time "
229 "%llu nsec\n", od->pm_lat_level, deact_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300230
Kevin Hilman9799aca2010-01-26 20:13:02 -0700231 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;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700235 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300236 "new worst case deactivate latency "
237 "%d: %llu\n",
238 od->pm_lat_level, deact_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700239 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700240 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700241 "deactivate latency %d "
242 "higher than exptected. (%llu > %d)\n",
243 od->pm_lat_level, deact_lat,
244 odpl->deactivate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700245 }
246
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300247 od->dev_wakeup_lat += odpl->activate_lat;
248
249 od->pm_lat_level++;
250 }
251
252 return 0;
253}
254
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600255static 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
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700264 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600265
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700266 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600267 if (!IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700268 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700269 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600270 clk_put(r);
271 return;
272 }
273
274 r = omap_clk_get_by_name(clk_name);
275 if (IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700276 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700277 "omap_clk_get_by_name for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600278 return;
279 }
280
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700281 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600282 if (!l) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700283 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700284 "clkdev_alloc for %s failed\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600285 return;
286 }
287
288 clkdev_add(l);
289}
290
Partha Basak4ef7aca2010-09-21 19:23:04 +0200291/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600292 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
293 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +0200294 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600295 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +0200296 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600297 * 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.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200300 *
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>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600306 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200307 *
308 * No return value.
309 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600310static void _add_hwmod_clocks_clkdev(struct omap_device *od,
311 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200312{
313 int i;
314
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600315 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200316
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600317 for (i = 0; i < oh->opt_clks_cnt; i++)
318 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200319}
320
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300321
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200322static 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
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300412/* Public functions for use by core code */
413
414/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700415 * 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
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600434 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700435
436 if (od->hwmods_cnt)
437 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
438
439 return ret;
440}
441
442/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300443 * 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 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700451static int omap_device_count_resources(struct omap_device *od)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300452{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300453 int c = 0;
454 int i;
455
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600456 for (i = 0; i < od->hwmods_cnt; i++)
457 c += omap_hwmod_count_resources(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300458
459 pr_debug("omap_device: %s: counted %d total resources across %d "
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700460 "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300461
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 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700482static int omap_device_fill_resources(struct omap_device *od,
483 struct resource *res)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300484{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300485 int c = 0;
486 int i, r;
487
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600488 for (i = 0; i < od->hwmods_cnt; i++) {
489 r = omap_hwmod_fill_resources(od->hwmods[i], res);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300490 res += r;
491 c += r;
492 }
493
494 return 0;
495}
496
497/**
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200498 * 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{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200595 if (!od)
596 return;
597
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200598 od->pdev->archdata.od = NULL;
599 kfree(od->pm_lats);
600 kfree(od->hwmods);
601 kfree(od);
602}
603
604/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300605 * 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
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700613 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300614 *
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 */
Kevin Hilman3528c582011-07-21 13:48:45 -0700621struct platform_device *omap_device_build(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300622 struct omap_hwmod *oh, void *pdata,
623 int pdata_len,
624 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700625 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300626{
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,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700633 pdata_len, pm_lats, pm_lats_cnt,
634 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300635}
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
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700646 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300647 *
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 */
Kevin Hilman3528c582011-07-21 13:48:45 -0700654struct platform_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300655 struct omap_hwmod **ohs, int oh_cnt,
656 void *pdata, int pdata_len,
657 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700658 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300659{
660 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700661 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300662 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300663
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
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700670 pdev = platform_device_alloc(pdev_name, pdev_id);
671 if (!pdev) {
672 ret = -ENOMEM;
673 goto odbs_exit;
674 }
675
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200676 /* 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);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300681
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200682 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
683 if (!od)
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700684 goto odbs_exit1;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300685
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700686 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000687 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200688 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700689
690 if (is_early_device)
691 ret = omap_early_device_register(pdev);
692 else
693 ret = omap_device_register(pdev);
694 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200695 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600696
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700697 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300698
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700699odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200700 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700701odbs_exit1:
702 platform_device_put(pdev);
703odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300704
705 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
706
707 return ERR_PTR(ret);
708}
709
710/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700711 * 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 */
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700719static int omap_early_device_register(struct platform_device *pdev)
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700720{
721 struct platform_device *devices[1];
722
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700723 devices[0] = pdev;
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700724 early_platform_add_devices(devices, 1);
725 return 0;
726}
727
Kevin Hilman256a5432011-07-12 22:48:03 +0200728#ifdef CONFIG_PM_RUNTIME
Kevin Hilman638080c2011-04-29 00:36:42 +0200729static int _od_runtime_suspend(struct device *dev)
730{
731 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700732 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200733
Kevin Hilman345f79b2011-05-31 16:08:09 -0700734 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);
Kevin Hilman638080c2011-04-29 00:36:42 +0200745}
746
747static int _od_runtime_resume(struct device *dev)
748{
749 struct platform_device *pdev = to_platform_device(dev);
750
Kevin Hilman345f79b2011-05-31 16:08:09 -0700751 omap_device_enable(pdev);
752
753 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200754}
Kevin Hilman256a5432011-07-12 22:48:03 +0200755#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200756
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200757#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
Kevin Hilman80c6d1e2011-07-12 22:48:29 +0200764 if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
765 return pm_generic_suspend_noirq(dev);
766
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200767 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
Kevin Hilman80c6d1e2011-07-12 22:48:29 +0200784 if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
785 return pm_generic_resume_noirq(dev);
786
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200787 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}
Kevin Hilman126caf12011-09-01 10:59:36 -0700796#else
797#define _od_suspend_noirq NULL
798#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200799#endif
800
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200801static struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200802 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200803 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
804 _od_runtime_idle)
Kevin Hilman638080c2011-04-29 00:36:42 +0200805 USE_PLATFORM_PM_SLEEP_OPS
Kevin Hilmanff353362011-08-25 15:31:14 +0200806 .suspend_noirq = _od_suspend_noirq,
807 .resume_noirq = _od_resume_noirq,
Kevin Hilman638080c2011-04-29 00:36:42 +0200808 }
809};
810
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700811/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300812 * 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 */
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700819static int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300820{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700821 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300822
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700823 pdev->dev.parent = &omap_device_parent;
824 pdev->dev.pm_domain = &omap_device_pm_domain;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700825 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300826}
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
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600849 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300850
851 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700852 dev_warn(&pdev->dev,
853 "omap_device: %s() called from invalid state %d\n",
854 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300855 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;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700865 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300866 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
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600889 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300890
891 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700892 dev_warn(&pdev->dev,
893 "omap_device: %s() called from invalid state %d\n",
894 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300895 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;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300920
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600921 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300922
923 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
924 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700925 dev_warn(&pdev->dev,
926 "omap_device: %s() called from invalid state %d\n",
927 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300928 return -EINVAL;
929 }
930
931 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
932
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600933 for (i = 0; i < od->hwmods_cnt; i++)
934 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300935
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
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600963 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300964
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/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300981 * 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
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06001002/**
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
Nishanth Menon1f8a7d52011-07-27 15:02:32 -05001021/**
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
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001057/*
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{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001070 int i;
1071
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001072 for (i = 0; i < od->hwmods_cnt; i++)
1073 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001074
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{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001087 int i;
1088
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001089 for (i = 0; i < od->hwmods_cnt; i++)
1090 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001091
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{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001105 int i;
1106
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001107 for (i = 0; i < od->hwmods_cnt; i++)
1108 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001109
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{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001123 int i;
1124
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001125 for (i = 0; i < od->hwmods_cnt; i++)
1126 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001127
1128 /* XXX pass along return value here? */
1129 return 0;
1130}
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001131
1132struct device omap_device_parent = {
1133 .init_name = "omap",
1134 .parent = &platform_bus,
1135};
1136
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001137static struct notifier_block platform_nb = {
1138 .notifier_call = _omap_device_notifier_call,
1139};
1140
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001141static int __init omap_device_init(void)
1142{
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001143 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001144 return device_register(&omap_device_parent);
1145}
1146core_initcall(omap_device_init);