blob: 1ae9d6f653a962153ef09075d13186b864512019 [file] [log] [blame]
Kevin Hilman9cf793f2012-02-20 09:43:30 -08001
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03002/*
3 * omap_device implementation
4 *
Paul Walmsley887adea2010-07-26 16:34:33 -06005 * Copyright (C) 2009-2010 Nokia Corporation
Paul Walmsley4788da22010-05-18 20:24:05 -06006 * Paul Walmsley, Kevin Hilman
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03007 *
8 * Developed in collaboration with (alphabetical order): Benoit
Paul Walmsley4788da22010-05-18 20:24:05 -06009 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030010 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
11 * Woodruff
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This code provides a consistent interface for OMAP device drivers
18 * to control power management and interconnect properties of their
19 * devices.
20 *
21 * In the medium- to long-term, this code should either be
22 * a) implemented via arch-specific pointers in platform_data
23 * or
24 * b) implemented as a proper omap_bus/omap_device in Linux, no more
25 * platform_data func pointers
26 *
27 *
28 * Guidelines for usage by driver authors:
29 *
30 * 1. These functions are intended to be used by device drivers via
31 * function pointers in struct platform_data. As an example,
32 * omap_device_enable() should be passed to the driver as
33 *
34 * struct foo_driver_platform_data {
35 * ...
36 * int (*device_enable)(struct platform_device *pdev);
37 * ...
38 * }
39 *
40 * Note that the generic "device_enable" name is used, rather than
41 * "omap_device_enable". This is so other architectures can pass in their
42 * own enable/disable functions here.
43 *
44 * This should be populated during device setup:
45 *
46 * ...
47 * pdata->device_enable = omap_device_enable;
48 * ...
49 *
50 * 2. Drivers should first check to ensure the function pointer is not null
51 * before calling it, as in:
52 *
53 * if (pdata->device_enable)
54 * pdata->device_enable(pdev);
55 *
56 * This allows other architectures that don't use similar device_enable()/
57 * device_shutdown() functions to execute normally.
58 *
59 * ...
60 *
61 * Suggested usage by device drivers:
62 *
63 * During device initialization:
64 * device_enable()
65 *
66 * During device idle:
67 * (save remaining device context if necessary)
68 * device_idle();
69 *
70 * During device resume:
71 * device_enable();
72 * (restore context if necessary)
73 *
74 * During device shutdown:
75 * device_shutdown()
76 * (device must be reinitialized at this point to use it again)
77 *
78 */
79#undef DEBUG
80
81#include <linux/kernel.h>
Axel Lin55581412011-11-07 12:27:10 -080082#include <linux/export.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030083#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090084#include <linux/slab.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030085#include <linux/err.h>
86#include <linux/io.h>
Partha Basak4ef7aca2010-09-21 19:23:04 +020087#include <linux/clk.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070088#include <linux/clkdev.h>
Kevin Hilman345f79b2011-05-31 16:08:09 -070089#include <linux/pm_runtime.h>
Benoit Coussondc2d07e2011-08-10 13:32:08 +020090#include <linux/of.h>
91#include <linux/notifier.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030092
Tony Lindgrence491cf2009-10-20 09:40:47 -070093#include <plat/omap_device.h>
94#include <plat/omap_hwmod.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070095#include <plat/clock.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030096
97/* These parameters are passed to _omap_device_{de,}activate() */
98#define USE_WAKEUP_LAT 0
99#define IGNORE_WAKEUP_LAT 1
100
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700101static int omap_early_device_register(struct platform_device *pdev);
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700102
Benoit Coussonb7b5bc92011-08-09 16:54:19 +0200103static struct omap_device_pm_latency omap_default_latency[] = {
104 {
105 .deactivate_func = omap_device_idle_hwmods,
106 .activate_func = omap_device_enable_hwmods,
107 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
108 }
109};
110
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300111/* Private functions */
112
113/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300114 * _omap_device_activate - increase device readiness
115 * @od: struct omap_device *
116 * @ignore_lat: increase to latency target (0) or full readiness (1)?
117 *
118 * Increase readiness of omap_device @od (thus decreasing device
119 * wakeup latency, but consuming more power). If @ignore_lat is
120 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
121 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
122 * latency is greater than the requested maximum wakeup latency, step
123 * backwards in the omap_device_pm_latency table to ensure the
124 * device's maximum wakeup latency is less than or equal to the
125 * requested maximum wakeup latency. Returns 0.
126 */
127static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
128{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700129 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300130
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700131 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300132
133 while (od->pm_lat_level > 0) {
134 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700135 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300136
137 od->pm_lat_level--;
138
139 odpl = od->pm_lats + od->pm_lat_level;
140
141 if (!ignore_lat &&
142 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
143 break;
144
Kevin Hilmand2292662009-12-08 16:34:23 -0700145 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300146
147 /* XXX check return code */
148 odpl->activate_func(od);
149
Kevin Hilmand2292662009-12-08 16:34:23 -0700150 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300151
Tony Lindgrenf0594292009-10-19 15:25:24 -0700152 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700153 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300154
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700155 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700156 "omap_device: pm_lat %d: activate: elapsed time "
157 "%llu nsec\n", od->pm_lat_level, act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300158
Kevin Hilman9799aca2010-01-26 20:13:02 -0700159 if (act_lat > odpl->activate_lat) {
160 odpl->activate_lat_worst = act_lat;
161 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
162 odpl->activate_lat = act_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700163 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300164 "new worst case activate latency "
165 "%d: %llu\n",
166 od->pm_lat_level, act_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700167 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700168 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700169 "activate latency %d "
170 "higher than exptected. (%llu > %d)\n",
171 od->pm_lat_level, act_lat,
172 odpl->activate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700173 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300174
175 od->dev_wakeup_lat -= odpl->activate_lat;
176 }
177
178 return 0;
179}
180
181/**
182 * _omap_device_deactivate - decrease device readiness
183 * @od: struct omap_device *
184 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
185 *
186 * Decrease readiness of omap_device @od (thus increasing device
187 * wakeup latency, but conserving power). If @ignore_lat is
188 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
189 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
190 * latency is less than the requested maximum wakeup latency, step
191 * forwards in the omap_device_pm_latency table to ensure the device's
192 * maximum wakeup latency is less than or equal to the requested
193 * maximum wakeup latency. Returns 0.
194 */
195static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
196{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700197 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300198
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700199 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300200
201 while (od->pm_lat_level < od->pm_lats_cnt) {
202 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700203 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300204
205 odpl = od->pm_lats + od->pm_lat_level;
206
207 if (!ignore_lat &&
208 ((od->dev_wakeup_lat + odpl->activate_lat) >
209 od->_dev_wakeup_lat_limit))
210 break;
211
Kevin Hilmand2292662009-12-08 16:34:23 -0700212 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300213
214 /* XXX check return code */
215 odpl->deactivate_func(od);
216
Kevin Hilmand2292662009-12-08 16:34:23 -0700217 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300218
Tony Lindgrenf0594292009-10-19 15:25:24 -0700219 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700220 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300221
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700222 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700223 "omap_device: pm_lat %d: deactivate: elapsed time "
224 "%llu nsec\n", od->pm_lat_level, deact_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300225
Kevin Hilman9799aca2010-01-26 20:13:02 -0700226 if (deact_lat > odpl->deactivate_lat) {
227 odpl->deactivate_lat_worst = deact_lat;
228 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
229 odpl->deactivate_lat = deact_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700230 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300231 "new worst case deactivate latency "
232 "%d: %llu\n",
233 od->pm_lat_level, deact_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700234 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700235 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700236 "deactivate latency %d "
237 "higher than exptected. (%llu > %d)\n",
238 od->pm_lat_level, deact_lat,
239 odpl->deactivate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700240 }
241
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300242 od->dev_wakeup_lat += odpl->activate_lat;
243
244 od->pm_lat_level++;
245 }
246
247 return 0;
248}
249
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600250static void _add_clkdev(struct omap_device *od, const char *clk_alias,
251 const char *clk_name)
252{
253 struct clk *r;
254 struct clk_lookup *l;
255
256 if (!clk_alias || !clk_name)
257 return;
258
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700259 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600260
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700261 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600262 if (!IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700263 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700264 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600265 clk_put(r);
266 return;
267 }
268
269 r = omap_clk_get_by_name(clk_name);
270 if (IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700271 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700272 "omap_clk_get_by_name for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600273 return;
274 }
275
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700276 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600277 if (!l) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700278 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700279 "clkdev_alloc for %s failed\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600280 return;
281 }
282
283 clkdev_add(l);
284}
285
Partha Basak4ef7aca2010-09-21 19:23:04 +0200286/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600287 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
288 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +0200289 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600290 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +0200291 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600292 * For the main clock and every optional clock present per hwmod per
293 * omap_device, this function adds an entry in the clkdev table of the
294 * form <dev-id=dev_name, con-id=role> if it does not exist already.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200295 *
296 * The function is called from inside omap_device_build_ss(), after
297 * omap_device_register.
298 *
299 * This allows drivers to get a pointer to its optional clocks based on its role
300 * by calling clk_get(<dev*>, <role>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600301 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200302 *
303 * No return value.
304 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600305static void _add_hwmod_clocks_clkdev(struct omap_device *od,
306 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200307{
308 int i;
309
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600310 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200311
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600312 for (i = 0; i < oh->opt_clks_cnt; i++)
313 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200314}
315
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300316
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200317/**
318 * omap_device_build_from_dt - build an omap_device with multiple hwmods
319 * @pdev_name: name of the platform_device driver to use
320 * @pdev_id: this platform_device's connection ID
321 * @oh: ptr to the single omap_hwmod that backs this omap_device
322 * @pdata: platform_data ptr to associate with the platform_device
323 * @pdata_len: amount of memory pointed to by @pdata
324 * @pm_lats: pointer to a omap_device_pm_latency array for this device
325 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
326 * @is_early_device: should the device be registered as an early device or not
327 *
328 * Function for building an omap_device already registered from device-tree
329 *
330 * Returns 0 or PTR_ERR() on error.
331 */
332static int omap_device_build_from_dt(struct platform_device *pdev)
333{
334 struct omap_hwmod **hwmods;
335 struct omap_device *od;
336 struct omap_hwmod *oh;
337 struct device_node *node = pdev->dev.of_node;
338 const char *oh_name;
339 int oh_cnt, i, ret = 0;
340
341 oh_cnt = of_property_count_strings(node, "ti,hwmods");
342 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
343 dev_warn(&pdev->dev, "No 'hwmods' to build omap_device\n");
344 return -ENODEV;
345 }
346
347 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
348 if (!hwmods) {
349 ret = -ENOMEM;
350 goto odbfd_exit;
351 }
352
353 for (i = 0; i < oh_cnt; i++) {
354 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
355 oh = omap_hwmod_lookup(oh_name);
356 if (!oh) {
357 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
358 oh_name);
359 ret = -EINVAL;
360 goto odbfd_exit1;
361 }
362 hwmods[i] = oh;
363 }
364
365 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
366 if (!od) {
367 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
368 oh_name);
369 ret = PTR_ERR(od);
370 goto odbfd_exit1;
371 }
372
373 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
374 omap_device_disable_idle_on_suspend(pdev);
375
376 pdev->dev.pm_domain = &omap_device_pm_domain;
377
378odbfd_exit1:
379 kfree(hwmods);
380odbfd_exit:
381 return ret;
382}
383
384static int _omap_device_notifier_call(struct notifier_block *nb,
385 unsigned long event, void *dev)
386{
387 struct platform_device *pdev = to_platform_device(dev);
388
389 switch (event) {
390 case BUS_NOTIFY_ADD_DEVICE:
391 if (pdev->dev.of_node)
392 omap_device_build_from_dt(pdev);
393 break;
394
395 case BUS_NOTIFY_DEL_DEVICE:
396 if (pdev->archdata.od)
397 omap_device_delete(pdev->archdata.od);
398 break;
399 }
400
401 return NOTIFY_DONE;
402}
403
404
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300405/* Public functions for use by core code */
406
407/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700408 * omap_device_get_context_loss_count - get lost context count
409 * @od: struct omap_device *
410 *
411 * Using the primary hwmod, query the context loss count for this
412 * device.
413 *
414 * Callers should consider context for this device lost any time this
415 * function returns a value different than the value the caller got
416 * the last time it called this function.
417 *
418 * If any hwmods exist for the omap_device assoiated with @pdev,
419 * return the context loss counter for that hwmod, otherwise return
420 * zero.
421 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +0300422int omap_device_get_context_loss_count(struct platform_device *pdev)
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700423{
424 struct omap_device *od;
425 u32 ret = 0;
426
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600427 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700428
429 if (od->hwmods_cnt)
430 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
431
432 return ret;
433}
434
435/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300436 * omap_device_count_resources - count number of struct resource entries needed
437 * @od: struct omap_device *
438 *
439 * Count the number of struct resource entries needed for this
440 * omap_device @od. Used by omap_device_build_ss() to determine how
441 * much memory to allocate before calling
442 * omap_device_fill_resources(). Returns the count.
443 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700444static int omap_device_count_resources(struct omap_device *od)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300445{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300446 int c = 0;
447 int i;
448
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600449 for (i = 0; i < od->hwmods_cnt; i++)
450 c += omap_hwmod_count_resources(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300451
452 pr_debug("omap_device: %s: counted %d total resources across %d "
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700453 "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300454
455 return c;
456}
457
458/**
459 * omap_device_fill_resources - fill in array of struct resource
460 * @od: struct omap_device *
461 * @res: pointer to an array of struct resource to be filled in
462 *
463 * Populate one or more empty struct resource pointed to by @res with
464 * the resource data for this omap_device @od. Used by
465 * omap_device_build_ss() after calling omap_device_count_resources().
466 * Ideally this function would not be needed at all. If omap_device
467 * replaces platform_device, then we can specify our own
468 * get_resource()/ get_irq()/etc functions that use the underlying
469 * omap_hwmod information. Or if platform_device is extended to use
470 * subarchitecture-specific function pointers, the various
471 * platform_device functions can simply call omap_device internal
472 * functions to get device resources. Hacking around the existing
473 * platform_device code wastes memory. Returns 0.
474 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700475static int omap_device_fill_resources(struct omap_device *od,
476 struct resource *res)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300477{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300478 int c = 0;
479 int i, r;
480
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600481 for (i = 0; i < od->hwmods_cnt; i++) {
482 r = omap_hwmod_fill_resources(od->hwmods[i], res);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300483 res += r;
484 c += r;
485 }
486
487 return 0;
488}
489
490/**
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200491 * omap_device_alloc - allocate an omap_device
492 * @pdev: platform_device that will be included in this omap_device
493 * @oh: ptr to the single omap_hwmod that backs this omap_device
494 * @pdata: platform_data ptr to associate with the platform_device
495 * @pdata_len: amount of memory pointed to by @pdata
496 * @pm_lats: pointer to a omap_device_pm_latency array for this device
497 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
498 *
499 * Convenience function for allocating an omap_device structure and filling
500 * hwmods, resources and pm_latency attributes.
501 *
502 * Returns an struct omap_device pointer or ERR_PTR() on error;
503 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800504struct omap_device *omap_device_alloc(struct platform_device *pdev,
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200505 struct omap_hwmod **ohs, int oh_cnt,
506 struct omap_device_pm_latency *pm_lats,
507 int pm_lats_cnt)
508{
509 int ret = -ENOMEM;
510 struct omap_device *od;
511 struct resource *res = NULL;
512 int i, res_count;
513 struct omap_hwmod **hwmods;
514
515 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
516 if (!od) {
517 ret = -ENOMEM;
518 goto oda_exit1;
519 }
520 od->hwmods_cnt = oh_cnt;
521
522 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
523 if (!hwmods)
524 goto oda_exit2;
525
526 od->hwmods = hwmods;
527 od->pdev = pdev;
528
529 /*
530 * HACK: Ideally the resources from DT should match, and hwmod
531 * should just add the missing ones. Since the name is not
532 * properly populated by DT, stick to hwmod resources only.
533 */
534 if (pdev->num_resources && pdev->resource)
535 dev_warn(&pdev->dev, "%s(): resources already allocated %d\n",
536 __func__, pdev->num_resources);
537
538 res_count = omap_device_count_resources(od);
539 if (res_count > 0) {
540 dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n",
541 __func__, res_count);
542 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
543 if (!res)
544 goto oda_exit3;
545
546 omap_device_fill_resources(od, res);
547
548 ret = platform_device_add_resources(pdev, res, res_count);
549 kfree(res);
550
551 if (ret)
552 goto oda_exit3;
553 }
554
555 if (!pm_lats) {
556 pm_lats = omap_default_latency;
557 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
558 }
559
560 od->pm_lats_cnt = pm_lats_cnt;
561 od->pm_lats = kmemdup(pm_lats,
562 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
563 GFP_KERNEL);
564 if (!od->pm_lats)
565 goto oda_exit3;
566
567 pdev->archdata.od = od;
568
569 for (i = 0; i < oh_cnt; i++) {
570 hwmods[i]->od = od;
571 _add_hwmod_clocks_clkdev(od, hwmods[i]);
572 }
573
574 return od;
575
576oda_exit3:
577 kfree(hwmods);
578oda_exit2:
579 kfree(od);
580oda_exit1:
581 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
582
583 return ERR_PTR(ret);
584}
585
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800586void omap_device_delete(struct omap_device *od)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200587{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200588 if (!od)
589 return;
590
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200591 od->pdev->archdata.od = NULL;
592 kfree(od->pm_lats);
593 kfree(od->hwmods);
594 kfree(od);
595}
596
597/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300598 * omap_device_build - build and register an omap_device with one omap_hwmod
599 * @pdev_name: name of the platform_device driver to use
600 * @pdev_id: this platform_device's connection ID
601 * @oh: ptr to the single omap_hwmod that backs this omap_device
602 * @pdata: platform_data ptr to associate with the platform_device
603 * @pdata_len: amount of memory pointed to by @pdata
604 * @pm_lats: pointer to a omap_device_pm_latency array for this device
605 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700606 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300607 *
608 * Convenience function for building and registering a single
609 * omap_device record, which in turn builds and registers a
610 * platform_device record. See omap_device_build_ss() for more
611 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
612 * passes along the return value of omap_device_build_ss().
613 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800614struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300615 struct omap_hwmod *oh, void *pdata,
616 int pdata_len,
617 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700618 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300619{
620 struct omap_hwmod *ohs[] = { oh };
621
622 if (!oh)
623 return ERR_PTR(-EINVAL);
624
625 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700626 pdata_len, pm_lats, pm_lats_cnt,
627 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300628}
629
630/**
631 * omap_device_build_ss - build and register an omap_device with multiple hwmods
632 * @pdev_name: name of the platform_device driver to use
633 * @pdev_id: this platform_device's connection ID
634 * @oh: ptr to the single omap_hwmod that backs this omap_device
635 * @pdata: platform_data ptr to associate with the platform_device
636 * @pdata_len: amount of memory pointed to by @pdata
637 * @pm_lats: pointer to a omap_device_pm_latency array for this device
638 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700639 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300640 *
641 * Convenience function for building and registering an omap_device
642 * subsystem record. Subsystem records consist of multiple
643 * omap_hwmods. This function in turn builds and registers a
644 * platform_device record. Returns an ERR_PTR() on error, or passes
645 * along the return value of omap_device_register().
646 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800647struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300648 struct omap_hwmod **ohs, int oh_cnt,
649 void *pdata, int pdata_len,
650 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700651 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300652{
653 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700654 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300655 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300656
657 if (!ohs || oh_cnt == 0 || !pdev_name)
658 return ERR_PTR(-EINVAL);
659
660 if (!pdata && pdata_len > 0)
661 return ERR_PTR(-EINVAL);
662
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700663 pdev = platform_device_alloc(pdev_name, pdev_id);
664 if (!pdev) {
665 ret = -ENOMEM;
666 goto odbs_exit;
667 }
668
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200669 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
670 if (pdev->id != -1)
671 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
672 else
673 dev_set_name(&pdev->dev, "%s", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300674
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200675 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
676 if (!od)
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700677 goto odbs_exit1;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300678
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700679 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000680 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200681 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700682
683 if (is_early_device)
684 ret = omap_early_device_register(pdev);
685 else
686 ret = omap_device_register(pdev);
687 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200688 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600689
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700690 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300691
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700692odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200693 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700694odbs_exit1:
695 platform_device_put(pdev);
696odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300697
698 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
699
700 return ERR_PTR(ret);
701}
702
703/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700704 * omap_early_device_register - register an omap_device as an early platform
705 * device.
706 * @od: struct omap_device * to register
707 *
708 * Register the omap_device structure. This currently just calls
709 * platform_early_add_device() on the underlying platform_device.
710 * Returns 0 by default.
711 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800712static int __init omap_early_device_register(struct platform_device *pdev)
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700713{
714 struct platform_device *devices[1];
715
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700716 devices[0] = pdev;
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700717 early_platform_add_devices(devices, 1);
718 return 0;
719}
720
Kevin Hilman256a5432011-07-12 22:48:03 +0200721#ifdef CONFIG_PM_RUNTIME
Kevin Hilman638080c2011-04-29 00:36:42 +0200722static int _od_runtime_suspend(struct device *dev)
723{
724 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700725 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200726
Kevin Hilman345f79b2011-05-31 16:08:09 -0700727 ret = pm_generic_runtime_suspend(dev);
728
729 if (!ret)
730 omap_device_idle(pdev);
731
732 return ret;
733}
734
735static int _od_runtime_idle(struct device *dev)
736{
737 return pm_generic_runtime_idle(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200738}
739
740static int _od_runtime_resume(struct device *dev)
741{
742 struct platform_device *pdev = to_platform_device(dev);
743
Kevin Hilman345f79b2011-05-31 16:08:09 -0700744 omap_device_enable(pdev);
745
746 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200747}
Kevin Hilman256a5432011-07-12 22:48:03 +0200748#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200749
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200750#ifdef CONFIG_SUSPEND
751static int _od_suspend_noirq(struct device *dev)
752{
753 struct platform_device *pdev = to_platform_device(dev);
754 struct omap_device *od = to_omap_device(pdev);
755 int ret;
756
Kevin Hilman80c6d1e2011-07-12 22:48:29 +0200757 if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
758 return pm_generic_suspend_noirq(dev);
759
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200760 ret = pm_generic_suspend_noirq(dev);
761
762 if (!ret && !pm_runtime_status_suspended(dev)) {
763 if (pm_generic_runtime_suspend(dev) == 0) {
764 omap_device_idle(pdev);
765 od->flags |= OMAP_DEVICE_SUSPENDED;
766 }
767 }
768
769 return ret;
770}
771
772static int _od_resume_noirq(struct device *dev)
773{
774 struct platform_device *pdev = to_platform_device(dev);
775 struct omap_device *od = to_omap_device(pdev);
776
Kevin Hilman80c6d1e2011-07-12 22:48:29 +0200777 if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
778 return pm_generic_resume_noirq(dev);
779
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200780 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
781 !pm_runtime_status_suspended(dev)) {
782 od->flags &= ~OMAP_DEVICE_SUSPENDED;
783 omap_device_enable(pdev);
784 pm_generic_runtime_resume(dev);
785 }
786
787 return pm_generic_resume_noirq(dev);
788}
Kevin Hilman126caf12011-09-01 10:59:36 -0700789#else
790#define _od_suspend_noirq NULL
791#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200792#endif
793
Kevin Hilman3ec2dec2012-02-15 11:47:45 -0800794struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200795 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200796 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
797 _od_runtime_idle)
Kevin Hilman638080c2011-04-29 00:36:42 +0200798 USE_PLATFORM_PM_SLEEP_OPS
Kevin Hilmanff353362011-08-25 15:31:14 +0200799 .suspend_noirq = _od_suspend_noirq,
800 .resume_noirq = _od_resume_noirq,
Kevin Hilman638080c2011-04-29 00:36:42 +0200801 }
802};
803
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700804/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300805 * omap_device_register - register an omap_device with one omap_hwmod
806 * @od: struct omap_device * to register
807 *
808 * Register the omap_device structure. This currently just calls
809 * platform_device_register() on the underlying platform_device.
810 * Returns the return value of platform_device_register().
811 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800812int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300813{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700814 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300815
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700816 pdev->dev.pm_domain = &omap_device_pm_domain;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700817 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300818}
819
820
821/* Public functions for use by device drivers through struct platform_data */
822
823/**
824 * omap_device_enable - fully activate an omap_device
825 * @od: struct omap_device * to activate
826 *
827 * Do whatever is necessary for the hwmods underlying omap_device @od
828 * to be accessible and ready to operate. This generally involves
829 * enabling clocks, setting SYSCONFIG registers; and in the future may
830 * involve remuxing pins. Device drivers should call this function
831 * (through platform_data function pointers) where they would normally
832 * enable clocks, etc. Returns -EINVAL if called when the omap_device
833 * is already enabled, or passes along the return value of
834 * _omap_device_activate().
835 */
836int omap_device_enable(struct platform_device *pdev)
837{
838 int ret;
839 struct omap_device *od;
840
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600841 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300842
843 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700844 dev_warn(&pdev->dev,
845 "omap_device: %s() called from invalid state %d\n",
846 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300847 return -EINVAL;
848 }
849
850 /* Enable everything if we're enabling this device from scratch */
851 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
852 od->pm_lat_level = od->pm_lats_cnt;
853
854 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
855
856 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700857 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300858 od->_state = OMAP_DEVICE_STATE_ENABLED;
859
860 return ret;
861}
862
863/**
864 * omap_device_idle - idle an omap_device
865 * @od: struct omap_device * to idle
866 *
867 * Idle omap_device @od by calling as many .deactivate_func() entries
868 * in the omap_device's pm_lats table as is possible without exceeding
869 * the device's maximum wakeup latency limit, pm_lat_limit. Device
870 * drivers should call this function (through platform_data function
871 * pointers) where they would normally disable clocks after operations
872 * complete, etc.. Returns -EINVAL if the omap_device is not
873 * currently enabled, or passes along the return value of
874 * _omap_device_deactivate().
875 */
876int omap_device_idle(struct platform_device *pdev)
877{
878 int ret;
879 struct omap_device *od;
880
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600881 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300882
883 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700884 dev_warn(&pdev->dev,
885 "omap_device: %s() called from invalid state %d\n",
886 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300887 return -EINVAL;
888 }
889
890 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
891
892 od->_state = OMAP_DEVICE_STATE_IDLE;
893
894 return ret;
895}
896
897/**
898 * omap_device_shutdown - shut down an omap_device
899 * @od: struct omap_device * to shut down
900 *
901 * Shut down omap_device @od by calling all .deactivate_func() entries
902 * in the omap_device's pm_lats table and then shutting down all of
903 * the underlying omap_hwmods. Used when a device is being "removed"
904 * or a device driver is being unloaded. Returns -EINVAL if the
905 * omap_device is not currently enabled or idle, or passes along the
906 * return value of _omap_device_deactivate().
907 */
908int omap_device_shutdown(struct platform_device *pdev)
909{
910 int ret, i;
911 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300912
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600913 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300914
915 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
916 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700917 dev_warn(&pdev->dev,
918 "omap_device: %s() called from invalid state %d\n",
919 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300920 return -EINVAL;
921 }
922
923 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
924
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600925 for (i = 0; i < od->hwmods_cnt; i++)
926 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300927
928 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
929
930 return ret;
931}
932
933/**
934 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
935 * @od: struct omap_device *
936 *
937 * When a device's maximum wakeup latency limit changes, call some of
938 * the .activate_func or .deactivate_func function pointers in the
939 * omap_device's pm_lats array to ensure that the device's maximum
940 * wakeup latency is less than or equal to the new latency limit.
941 * Intended to be called by OMAP PM code whenever a device's maximum
942 * wakeup latency limit changes (e.g., via
943 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
944 * done (e.g., if the omap_device is not currently idle, or if the
945 * wakeup latency is already current with the new limit) or passes
946 * along the return value of _omap_device_deactivate() or
947 * _omap_device_activate().
948 */
949int omap_device_align_pm_lat(struct platform_device *pdev,
950 u32 new_wakeup_lat_limit)
951{
952 int ret = -EINVAL;
953 struct omap_device *od;
954
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600955 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300956
957 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
958 return 0;
959
960 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
961
962 if (od->_state != OMAP_DEVICE_STATE_IDLE)
963 return 0;
964 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
965 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
966 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
967 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
968
969 return ret;
970}
971
972/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300973 * omap_device_get_pwrdm - return the powerdomain * associated with @od
974 * @od: struct omap_device *
975 *
976 * Return the powerdomain associated with the first underlying
977 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
978 * code. Returns NULL on error or a struct powerdomain * upon
979 * success.
980 */
981struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
982{
983 /*
984 * XXX Assumes that all omap_hwmod powerdomains are identical.
985 * This may not necessarily be true. There should be a sanity
986 * check in here to WARN() if any difference appears.
987 */
988 if (!od->hwmods_cnt)
989 return NULL;
990
991 return omap_hwmod_get_pwrdm(od->hwmods[0]);
992}
993
Paul Walmsleydb2a60b2010-07-26 16:34:33 -0600994/**
995 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
996 * @od: struct omap_device *
997 *
998 * Return the MPU's virtual address for the base of the hwmod, from
999 * the ioremap() that the hwmod code does. Only valid if there is one
1000 * hwmod associated with this device. Returns NULL if there are zero
1001 * or more than one hwmods associated with this omap_device;
1002 * otherwise, passes along the return value from
1003 * omap_hwmod_get_mpu_rt_va().
1004 */
1005void __iomem *omap_device_get_rt_va(struct omap_device *od)
1006{
1007 if (od->hwmods_cnt != 1)
1008 return NULL;
1009
1010 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1011}
1012
Nishanth Menon1f8a7d52011-07-27 15:02:32 -05001013/**
1014 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1015 * device pointer.
1016 * @oh_name: name of the hwmod device
1017 *
1018 * Returns back a struct device * pointer associated with a hwmod
1019 * device represented by a hwmod_name
1020 */
1021struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1022{
1023 struct omap_hwmod *oh;
1024
1025 if (!oh_name) {
1026 WARN(1, "%s: no hwmod name!\n", __func__);
1027 return ERR_PTR(-EINVAL);
1028 }
1029
1030 oh = omap_hwmod_lookup(oh_name);
1031 if (IS_ERR_OR_NULL(oh)) {
1032 WARN(1, "%s: no hwmod for %s\n", __func__,
1033 oh_name);
1034 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1035 }
1036 if (IS_ERR_OR_NULL(oh->od)) {
1037 WARN(1, "%s: no omap_device for %s\n", __func__,
1038 oh_name);
1039 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1040 }
1041
1042 if (IS_ERR_OR_NULL(oh->od->pdev))
1043 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1044
1045 return &oh->od->pdev->dev;
1046}
1047EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1048
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001049/*
1050 * Public functions intended for use in omap_device_pm_latency
1051 * .activate_func and .deactivate_func function pointers
1052 */
1053
1054/**
1055 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1056 * @od: struct omap_device *od
1057 *
1058 * Enable all underlying hwmods. Returns 0.
1059 */
1060int omap_device_enable_hwmods(struct omap_device *od)
1061{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001062 int i;
1063
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001064 for (i = 0; i < od->hwmods_cnt; i++)
1065 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001066
1067 /* XXX pass along return value here? */
1068 return 0;
1069}
1070
1071/**
1072 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1073 * @od: struct omap_device *od
1074 *
1075 * Idle all underlying hwmods. Returns 0.
1076 */
1077int omap_device_idle_hwmods(struct omap_device *od)
1078{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001079 int i;
1080
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001081 for (i = 0; i < od->hwmods_cnt; i++)
1082 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001083
1084 /* XXX pass along return value here? */
1085 return 0;
1086}
1087
1088/**
1089 * omap_device_disable_clocks - disable all main and interface clocks
1090 * @od: struct omap_device *od
1091 *
1092 * Disable the main functional clock and interface clock for all of the
1093 * omap_hwmods associated with the omap_device. Returns 0.
1094 */
1095int omap_device_disable_clocks(struct omap_device *od)
1096{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001097 int i;
1098
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001099 for (i = 0; i < od->hwmods_cnt; i++)
1100 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001101
1102 /* XXX pass along return value here? */
1103 return 0;
1104}
1105
1106/**
1107 * omap_device_enable_clocks - enable all main and interface clocks
1108 * @od: struct omap_device *od
1109 *
1110 * Enable the main functional clock and interface clock for all of the
1111 * omap_hwmods associated with the omap_device. Returns 0.
1112 */
1113int omap_device_enable_clocks(struct omap_device *od)
1114{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001115 int i;
1116
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001117 for (i = 0; i < od->hwmods_cnt; i++)
1118 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001119
1120 /* XXX pass along return value here? */
1121 return 0;
1122}
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001123
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001124static struct notifier_block platform_nb = {
1125 .notifier_call = _omap_device_notifier_call,
1126};
1127
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001128static int __init omap_device_init(void)
1129{
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001130 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman3ec2dec2012-02-15 11:47:45 -08001131 return 0;
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001132}
1133core_initcall(omap_device_init);