blob: f72fafc9e9f41fea1df35459abecf30c720e3bd6 [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>
Axel Lin55581412011-11-07 12:27:10 -080081#include <linux/export.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030082#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090083#include <linux/slab.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030084#include <linux/err.h>
85#include <linux/io.h>
Partha Basak4ef7aca2010-09-21 19:23:04 +020086#include <linux/clk.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070087#include <linux/clkdev.h>
Kevin Hilman345f79b2011-05-31 16:08:09 -070088#include <linux/pm_runtime.h>
Benoit Coussondc2d07e2011-08-10 13:32:08 +020089#include <linux/of.h>
90#include <linux/notifier.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030091
Tony Lindgrence491cf2009-10-20 09:40:47 -070092#include <plat/omap_device.h>
93#include <plat/omap_hwmod.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070094#include <plat/clock.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030095
96/* These parameters are passed to _omap_device_{de,}activate() */
97#define USE_WAKEUP_LAT 0
98#define IGNORE_WAKEUP_LAT 1
99
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700100static int omap_early_device_register(struct platform_device *pdev);
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700101
Benoit Coussonb7b5bc92011-08-09 16:54:19 +0200102static 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
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300110/* Private functions */
111
112/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300113 * _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{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700128 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300129
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700130 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300131
132 while (od->pm_lat_level > 0) {
133 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700134 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300135
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
Kevin Hilmand2292662009-12-08 16:34:23 -0700144 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300145
146 /* XXX check return code */
147 odpl->activate_func(od);
148
Kevin Hilmand2292662009-12-08 16:34:23 -0700149 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300150
Tony Lindgrenf0594292009-10-19 15:25:24 -0700151 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700152 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300153
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700154 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700155 "omap_device: pm_lat %d: activate: elapsed time "
156 "%llu nsec\n", od->pm_lat_level, act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300157
Kevin Hilman9799aca2010-01-26 20:13:02 -0700158 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;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700162 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300163 "new worst case activate latency "
164 "%d: %llu\n",
165 od->pm_lat_level, act_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700166 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700167 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700168 "activate latency %d "
169 "higher than exptected. (%llu > %d)\n",
170 od->pm_lat_level, act_lat,
171 odpl->activate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700172 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300173
174 od->dev_wakeup_lat -= odpl->activate_lat;
175 }
176
177 return 0;
178}
179
180/**
181 * _omap_device_deactivate - decrease device readiness
182 * @od: struct omap_device *
183 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
184 *
185 * Decrease readiness of omap_device @od (thus increasing device
186 * wakeup latency, but conserving power). If @ignore_lat is
187 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
188 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
189 * latency is less than the requested maximum wakeup latency, step
190 * forwards in the omap_device_pm_latency table to ensure the device's
191 * maximum wakeup latency is less than or equal to the requested
192 * maximum wakeup latency. Returns 0.
193 */
194static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
195{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700196 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300197
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700198 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300199
200 while (od->pm_lat_level < od->pm_lats_cnt) {
201 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700202 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300203
204 odpl = od->pm_lats + od->pm_lat_level;
205
206 if (!ignore_lat &&
207 ((od->dev_wakeup_lat + odpl->activate_lat) >
208 od->_dev_wakeup_lat_limit))
209 break;
210
Kevin Hilmand2292662009-12-08 16:34:23 -0700211 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300212
213 /* XXX check return code */
214 odpl->deactivate_func(od);
215
Kevin Hilmand2292662009-12-08 16:34:23 -0700216 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300217
Tony Lindgrenf0594292009-10-19 15:25:24 -0700218 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700219 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300220
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700221 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700222 "omap_device: pm_lat %d: deactivate: elapsed time "
223 "%llu nsec\n", od->pm_lat_level, deact_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300224
Kevin Hilman9799aca2010-01-26 20:13:02 -0700225 if (deact_lat > odpl->deactivate_lat) {
226 odpl->deactivate_lat_worst = deact_lat;
227 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
228 odpl->deactivate_lat = deact_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700229 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300230 "new worst case deactivate latency "
231 "%d: %llu\n",
232 od->pm_lat_level, deact_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700233 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700234 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700235 "deactivate latency %d "
236 "higher than exptected. (%llu > %d)\n",
237 od->pm_lat_level, deact_lat,
238 odpl->deactivate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700239 }
240
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300241 od->dev_wakeup_lat += odpl->activate_lat;
242
243 od->pm_lat_level++;
244 }
245
246 return 0;
247}
248
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600249static void _add_clkdev(struct omap_device *od, const char *clk_alias,
250 const char *clk_name)
251{
252 struct clk *r;
253 struct clk_lookup *l;
254
255 if (!clk_alias || !clk_name)
256 return;
257
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700258 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600259
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700260 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600261 if (!IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700262 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700263 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600264 clk_put(r);
265 return;
266 }
267
268 r = omap_clk_get_by_name(clk_name);
269 if (IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700270 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700271 "omap_clk_get_by_name for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600272 return;
273 }
274
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700275 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600276 if (!l) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700277 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700278 "clkdev_alloc for %s failed\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600279 return;
280 }
281
282 clkdev_add(l);
283}
284
Partha Basak4ef7aca2010-09-21 19:23:04 +0200285/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600286 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
287 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +0200288 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600289 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +0200290 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600291 * For the main clock and every optional clock present per hwmod per
292 * omap_device, this function adds an entry in the clkdev table of the
293 * form <dev-id=dev_name, con-id=role> if it does not exist already.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200294 *
295 * The function is called from inside omap_device_build_ss(), after
296 * omap_device_register.
297 *
298 * This allows drivers to get a pointer to its optional clocks based on its role
299 * by calling clk_get(<dev*>, <role>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600300 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200301 *
302 * No return value.
303 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600304static void _add_hwmod_clocks_clkdev(struct omap_device *od,
305 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200306{
307 int i;
308
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600309 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200310
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600311 for (i = 0; i < oh->opt_clks_cnt; i++)
312 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200313}
314
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300315
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200316static struct dev_pm_domain omap_device_pm_domain;
317
318/**
319 * omap_device_build_from_dt - build an omap_device with multiple hwmods
320 * @pdev_name: name of the platform_device driver to use
321 * @pdev_id: this platform_device's connection ID
322 * @oh: ptr to the single omap_hwmod that backs this omap_device
323 * @pdata: platform_data ptr to associate with the platform_device
324 * @pdata_len: amount of memory pointed to by @pdata
325 * @pm_lats: pointer to a omap_device_pm_latency array for this device
326 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
327 * @is_early_device: should the device be registered as an early device or not
328 *
329 * Function for building an omap_device already registered from device-tree
330 *
331 * Returns 0 or PTR_ERR() on error.
332 */
333static int omap_device_build_from_dt(struct platform_device *pdev)
334{
335 struct omap_hwmod **hwmods;
336 struct omap_device *od;
337 struct omap_hwmod *oh;
338 struct device_node *node = pdev->dev.of_node;
339 const char *oh_name;
340 int oh_cnt, i, ret = 0;
341
342 oh_cnt = of_property_count_strings(node, "ti,hwmods");
343 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
344 dev_warn(&pdev->dev, "No 'hwmods' to build omap_device\n");
345 return -ENODEV;
346 }
347
348 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
349 if (!hwmods) {
350 ret = -ENOMEM;
351 goto odbfd_exit;
352 }
353
354 for (i = 0; i < oh_cnt; i++) {
355 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
356 oh = omap_hwmod_lookup(oh_name);
357 if (!oh) {
358 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
359 oh_name);
360 ret = -EINVAL;
361 goto odbfd_exit1;
362 }
363 hwmods[i] = oh;
364 }
365
366 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
367 if (!od) {
368 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
369 oh_name);
370 ret = PTR_ERR(od);
371 goto odbfd_exit1;
372 }
373
374 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
375 omap_device_disable_idle_on_suspend(pdev);
376
377 pdev->dev.pm_domain = &omap_device_pm_domain;
378
379odbfd_exit1:
380 kfree(hwmods);
381odbfd_exit:
382 return ret;
383}
384
385static int _omap_device_notifier_call(struct notifier_block *nb,
386 unsigned long event, void *dev)
387{
388 struct platform_device *pdev = to_platform_device(dev);
389
390 switch (event) {
391 case BUS_NOTIFY_ADD_DEVICE:
392 if (pdev->dev.of_node)
393 omap_device_build_from_dt(pdev);
394 break;
395
396 case BUS_NOTIFY_DEL_DEVICE:
397 if (pdev->archdata.od)
398 omap_device_delete(pdev->archdata.od);
399 break;
400 }
401
402 return NOTIFY_DONE;
403}
404
405
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300406/* Public functions for use by core code */
407
408/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700409 * omap_device_get_context_loss_count - get lost context count
410 * @od: struct omap_device *
411 *
412 * Using the primary hwmod, query the context loss count for this
413 * device.
414 *
415 * Callers should consider context for this device lost any time this
416 * function returns a value different than the value the caller got
417 * the last time it called this function.
418 *
419 * If any hwmods exist for the omap_device assoiated with @pdev,
420 * return the context loss counter for that hwmod, otherwise return
421 * zero.
422 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +0300423int omap_device_get_context_loss_count(struct platform_device *pdev)
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700424{
425 struct omap_device *od;
426 u32 ret = 0;
427
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600428 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700429
430 if (od->hwmods_cnt)
431 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
432
433 return ret;
434}
435
436/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300437 * omap_device_count_resources - count number of struct resource entries needed
438 * @od: struct omap_device *
439 *
440 * Count the number of struct resource entries needed for this
441 * omap_device @od. Used by omap_device_build_ss() to determine how
442 * much memory to allocate before calling
443 * omap_device_fill_resources(). Returns the count.
444 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700445static int omap_device_count_resources(struct omap_device *od)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300446{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300447 int c = 0;
448 int i;
449
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600450 for (i = 0; i < od->hwmods_cnt; i++)
451 c += omap_hwmod_count_resources(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300452
453 pr_debug("omap_device: %s: counted %d total resources across %d "
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700454 "hwmods\n", od->pdev->name, c, od->hwmods_cnt);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300455
456 return c;
457}
458
459/**
460 * omap_device_fill_resources - fill in array of struct resource
461 * @od: struct omap_device *
462 * @res: pointer to an array of struct resource to be filled in
463 *
464 * Populate one or more empty struct resource pointed to by @res with
465 * the resource data for this omap_device @od. Used by
466 * omap_device_build_ss() after calling omap_device_count_resources().
467 * Ideally this function would not be needed at all. If omap_device
468 * replaces platform_device, then we can specify our own
469 * get_resource()/ get_irq()/etc functions that use the underlying
470 * omap_hwmod information. Or if platform_device is extended to use
471 * subarchitecture-specific function pointers, the various
472 * platform_device functions can simply call omap_device internal
473 * functions to get device resources. Hacking around the existing
474 * platform_device code wastes memory. Returns 0.
475 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700476static int omap_device_fill_resources(struct omap_device *od,
477 struct resource *res)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300478{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300479 int c = 0;
480 int i, r;
481
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600482 for (i = 0; i < od->hwmods_cnt; i++) {
483 r = omap_hwmod_fill_resources(od->hwmods[i], res);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300484 res += r;
485 c += r;
486 }
487
488 return 0;
489}
490
491/**
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200492 * omap_device_alloc - allocate an omap_device
493 * @pdev: platform_device that will be included in this omap_device
494 * @oh: ptr to the single omap_hwmod that backs this omap_device
495 * @pdata: platform_data ptr to associate with the platform_device
496 * @pdata_len: amount of memory pointed to by @pdata
497 * @pm_lats: pointer to a omap_device_pm_latency array for this device
498 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
499 *
500 * Convenience function for allocating an omap_device structure and filling
501 * hwmods, resources and pm_latency attributes.
502 *
503 * Returns an struct omap_device pointer or ERR_PTR() on error;
504 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800505struct omap_device *omap_device_alloc(struct platform_device *pdev,
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200506 struct omap_hwmod **ohs, int oh_cnt,
507 struct omap_device_pm_latency *pm_lats,
508 int pm_lats_cnt)
509{
510 int ret = -ENOMEM;
511 struct omap_device *od;
512 struct resource *res = NULL;
513 int i, res_count;
514 struct omap_hwmod **hwmods;
515
516 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
517 if (!od) {
518 ret = -ENOMEM;
519 goto oda_exit1;
520 }
521 od->hwmods_cnt = oh_cnt;
522
523 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
524 if (!hwmods)
525 goto oda_exit2;
526
527 od->hwmods = hwmods;
528 od->pdev = pdev;
529
530 /*
531 * HACK: Ideally the resources from DT should match, and hwmod
532 * should just add the missing ones. Since the name is not
533 * properly populated by DT, stick to hwmod resources only.
534 */
535 if (pdev->num_resources && pdev->resource)
536 dev_warn(&pdev->dev, "%s(): resources already allocated %d\n",
537 __func__, pdev->num_resources);
538
539 res_count = omap_device_count_resources(od);
540 if (res_count > 0) {
541 dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n",
542 __func__, res_count);
543 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
544 if (!res)
545 goto oda_exit3;
546
547 omap_device_fill_resources(od, res);
548
549 ret = platform_device_add_resources(pdev, res, res_count);
550 kfree(res);
551
552 if (ret)
553 goto oda_exit3;
554 }
555
556 if (!pm_lats) {
557 pm_lats = omap_default_latency;
558 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
559 }
560
561 od->pm_lats_cnt = pm_lats_cnt;
562 od->pm_lats = kmemdup(pm_lats,
563 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
564 GFP_KERNEL);
565 if (!od->pm_lats)
566 goto oda_exit3;
567
568 pdev->archdata.od = od;
569
570 for (i = 0; i < oh_cnt; i++) {
571 hwmods[i]->od = od;
572 _add_hwmod_clocks_clkdev(od, hwmods[i]);
573 }
574
575 return od;
576
577oda_exit3:
578 kfree(hwmods);
579oda_exit2:
580 kfree(od);
581oda_exit1:
582 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
583
584 return ERR_PTR(ret);
585}
586
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800587void omap_device_delete(struct omap_device *od)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200588{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200589 if (!od)
590 return;
591
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200592 od->pdev->archdata.od = NULL;
593 kfree(od->pm_lats);
594 kfree(od->hwmods);
595 kfree(od);
596}
597
598/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300599 * omap_device_build - build and register an omap_device with one omap_hwmod
600 * @pdev_name: name of the platform_device driver to use
601 * @pdev_id: this platform_device's connection ID
602 * @oh: ptr to the single omap_hwmod that backs this omap_device
603 * @pdata: platform_data ptr to associate with the platform_device
604 * @pdata_len: amount of memory pointed to by @pdata
605 * @pm_lats: pointer to a omap_device_pm_latency array for this device
606 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700607 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300608 *
609 * Convenience function for building and registering a single
610 * omap_device record, which in turn builds and registers a
611 * platform_device record. See omap_device_build_ss() for more
612 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
613 * passes along the return value of omap_device_build_ss().
614 */
Kevin Hilman3528c582011-07-21 13:48:45 -0700615struct platform_device *omap_device_build(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300616 struct omap_hwmod *oh, void *pdata,
617 int pdata_len,
618 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700619 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300620{
621 struct omap_hwmod *ohs[] = { oh };
622
623 if (!oh)
624 return ERR_PTR(-EINVAL);
625
626 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700627 pdata_len, pm_lats, pm_lats_cnt,
628 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300629}
630
631/**
632 * omap_device_build_ss - build and register an omap_device with multiple hwmods
633 * @pdev_name: name of the platform_device driver to use
634 * @pdev_id: this platform_device's connection ID
635 * @oh: ptr to the single omap_hwmod that backs this omap_device
636 * @pdata: platform_data ptr to associate with the platform_device
637 * @pdata_len: amount of memory pointed to by @pdata
638 * @pm_lats: pointer to a omap_device_pm_latency array for this device
639 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700640 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300641 *
642 * Convenience function for building and registering an omap_device
643 * subsystem record. Subsystem records consist of multiple
644 * omap_hwmods. This function in turn builds and registers a
645 * platform_device record. Returns an ERR_PTR() on error, or passes
646 * along the return value of omap_device_register().
647 */
Kevin Hilman3528c582011-07-21 13:48:45 -0700648struct platform_device *omap_device_build_ss(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300649 struct omap_hwmod **ohs, int oh_cnt,
650 void *pdata, int pdata_len,
651 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700652 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300653{
654 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700655 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300656 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300657
658 if (!ohs || oh_cnt == 0 || !pdev_name)
659 return ERR_PTR(-EINVAL);
660
661 if (!pdata && pdata_len > 0)
662 return ERR_PTR(-EINVAL);
663
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700664 pdev = platform_device_alloc(pdev_name, pdev_id);
665 if (!pdev) {
666 ret = -ENOMEM;
667 goto odbs_exit;
668 }
669
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200670 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
671 if (pdev->id != -1)
672 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
673 else
674 dev_set_name(&pdev->dev, "%s", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300675
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200676 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
677 if (!od)
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700678 goto odbs_exit1;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300679
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700680 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000681 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200682 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700683
684 if (is_early_device)
685 ret = omap_early_device_register(pdev);
686 else
687 ret = omap_device_register(pdev);
688 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200689 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600690
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700691 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300692
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700693odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200694 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700695odbs_exit1:
696 platform_device_put(pdev);
697odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300698
699 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
700
701 return ERR_PTR(ret);
702}
703
704/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700705 * omap_early_device_register - register an omap_device as an early platform
706 * device.
707 * @od: struct omap_device * to register
708 *
709 * Register the omap_device structure. This currently just calls
710 * platform_early_add_device() on the underlying platform_device.
711 * Returns 0 by default.
712 */
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700713static int omap_early_device_register(struct platform_device *pdev)
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700714{
715 struct platform_device *devices[1];
716
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700717 devices[0] = pdev;
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700718 early_platform_add_devices(devices, 1);
719 return 0;
720}
721
Kevin Hilman256a5432011-07-12 22:48:03 +0200722#ifdef CONFIG_PM_RUNTIME
Kevin Hilman638080c2011-04-29 00:36:42 +0200723static int _od_runtime_suspend(struct device *dev)
724{
725 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700726 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200727
Kevin Hilman345f79b2011-05-31 16:08:09 -0700728 ret = pm_generic_runtime_suspend(dev);
729
730 if (!ret)
731 omap_device_idle(pdev);
732
733 return ret;
734}
735
736static int _od_runtime_idle(struct device *dev)
737{
738 return pm_generic_runtime_idle(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200739}
740
741static int _od_runtime_resume(struct device *dev)
742{
743 struct platform_device *pdev = to_platform_device(dev);
744
Kevin Hilman345f79b2011-05-31 16:08:09 -0700745 omap_device_enable(pdev);
746
747 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200748}
Kevin Hilman256a5432011-07-12 22:48:03 +0200749#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200750
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200751#ifdef CONFIG_SUSPEND
752static int _od_suspend_noirq(struct device *dev)
753{
754 struct platform_device *pdev = to_platform_device(dev);
755 struct omap_device *od = to_omap_device(pdev);
756 int ret;
757
Kevin Hilman80c6d1e2011-07-12 22:48:29 +0200758 if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
759 return pm_generic_suspend_noirq(dev);
760
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200761 ret = pm_generic_suspend_noirq(dev);
762
763 if (!ret && !pm_runtime_status_suspended(dev)) {
764 if (pm_generic_runtime_suspend(dev) == 0) {
765 omap_device_idle(pdev);
766 od->flags |= OMAP_DEVICE_SUSPENDED;
767 }
768 }
769
770 return ret;
771}
772
773static int _od_resume_noirq(struct device *dev)
774{
775 struct platform_device *pdev = to_platform_device(dev);
776 struct omap_device *od = to_omap_device(pdev);
777
Kevin Hilman80c6d1e2011-07-12 22:48:29 +0200778 if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)
779 return pm_generic_resume_noirq(dev);
780
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200781 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
782 !pm_runtime_status_suspended(dev)) {
783 od->flags &= ~OMAP_DEVICE_SUSPENDED;
784 omap_device_enable(pdev);
785 pm_generic_runtime_resume(dev);
786 }
787
788 return pm_generic_resume_noirq(dev);
789}
Kevin Hilman126caf12011-09-01 10:59:36 -0700790#else
791#define _od_suspend_noirq NULL
792#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200793#endif
794
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200795static struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200796 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200797 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
798 _od_runtime_idle)
Kevin Hilman638080c2011-04-29 00:36:42 +0200799 USE_PLATFORM_PM_SLEEP_OPS
Kevin Hilmanff353362011-08-25 15:31:14 +0200800 .suspend_noirq = _od_suspend_noirq,
801 .resume_noirq = _od_resume_noirq,
Kevin Hilman638080c2011-04-29 00:36:42 +0200802 }
803};
804
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700805/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300806 * omap_device_register - register an omap_device with one omap_hwmod
807 * @od: struct omap_device * to register
808 *
809 * Register the omap_device structure. This currently just calls
810 * platform_device_register() on the underlying platform_device.
811 * Returns the return value of platform_device_register().
812 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800813int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300814{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700815 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300816
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700817 pdev->dev.parent = &omap_device_parent;
818 pdev->dev.pm_domain = &omap_device_pm_domain;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700819 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300820}
821
822
823/* Public functions for use by device drivers through struct platform_data */
824
825/**
826 * omap_device_enable - fully activate an omap_device
827 * @od: struct omap_device * to activate
828 *
829 * Do whatever is necessary for the hwmods underlying omap_device @od
830 * to be accessible and ready to operate. This generally involves
831 * enabling clocks, setting SYSCONFIG registers; and in the future may
832 * involve remuxing pins. Device drivers should call this function
833 * (through platform_data function pointers) where they would normally
834 * enable clocks, etc. Returns -EINVAL if called when the omap_device
835 * is already enabled, or passes along the return value of
836 * _omap_device_activate().
837 */
838int omap_device_enable(struct platform_device *pdev)
839{
840 int ret;
841 struct omap_device *od;
842
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600843 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300844
845 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700846 dev_warn(&pdev->dev,
847 "omap_device: %s() called from invalid state %d\n",
848 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300849 return -EINVAL;
850 }
851
852 /* Enable everything if we're enabling this device from scratch */
853 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
854 od->pm_lat_level = od->pm_lats_cnt;
855
856 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
857
858 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700859 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300860 od->_state = OMAP_DEVICE_STATE_ENABLED;
861
862 return ret;
863}
864
865/**
866 * omap_device_idle - idle an omap_device
867 * @od: struct omap_device * to idle
868 *
869 * Idle omap_device @od by calling as many .deactivate_func() entries
870 * in the omap_device's pm_lats table as is possible without exceeding
871 * the device's maximum wakeup latency limit, pm_lat_limit. Device
872 * drivers should call this function (through platform_data function
873 * pointers) where they would normally disable clocks after operations
874 * complete, etc.. Returns -EINVAL if the omap_device is not
875 * currently enabled, or passes along the return value of
876 * _omap_device_deactivate().
877 */
878int omap_device_idle(struct platform_device *pdev)
879{
880 int ret;
881 struct omap_device *od;
882
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600883 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300884
885 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700886 dev_warn(&pdev->dev,
887 "omap_device: %s() called from invalid state %d\n",
888 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300889 return -EINVAL;
890 }
891
892 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
893
894 od->_state = OMAP_DEVICE_STATE_IDLE;
895
896 return ret;
897}
898
899/**
900 * omap_device_shutdown - shut down an omap_device
901 * @od: struct omap_device * to shut down
902 *
903 * Shut down omap_device @od by calling all .deactivate_func() entries
904 * in the omap_device's pm_lats table and then shutting down all of
905 * the underlying omap_hwmods. Used when a device is being "removed"
906 * or a device driver is being unloaded. Returns -EINVAL if the
907 * omap_device is not currently enabled or idle, or passes along the
908 * return value of _omap_device_deactivate().
909 */
910int omap_device_shutdown(struct platform_device *pdev)
911{
912 int ret, i;
913 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300914
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600915 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300916
917 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
918 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700919 dev_warn(&pdev->dev,
920 "omap_device: %s() called from invalid state %d\n",
921 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300922 return -EINVAL;
923 }
924
925 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
926
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600927 for (i = 0; i < od->hwmods_cnt; i++)
928 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300929
930 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
931
932 return ret;
933}
934
935/**
936 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
937 * @od: struct omap_device *
938 *
939 * When a device's maximum wakeup latency limit changes, call some of
940 * the .activate_func or .deactivate_func function pointers in the
941 * omap_device's pm_lats array to ensure that the device's maximum
942 * wakeup latency is less than or equal to the new latency limit.
943 * Intended to be called by OMAP PM code whenever a device's maximum
944 * wakeup latency limit changes (e.g., via
945 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
946 * done (e.g., if the omap_device is not currently idle, or if the
947 * wakeup latency is already current with the new limit) or passes
948 * along the return value of _omap_device_deactivate() or
949 * _omap_device_activate().
950 */
951int omap_device_align_pm_lat(struct platform_device *pdev,
952 u32 new_wakeup_lat_limit)
953{
954 int ret = -EINVAL;
955 struct omap_device *od;
956
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600957 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300958
959 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
960 return 0;
961
962 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
963
964 if (od->_state != OMAP_DEVICE_STATE_IDLE)
965 return 0;
966 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
967 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
968 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
969 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
970
971 return ret;
972}
973
974/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300975 * omap_device_get_pwrdm - return the powerdomain * associated with @od
976 * @od: struct omap_device *
977 *
978 * Return the powerdomain associated with the first underlying
979 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
980 * code. Returns NULL on error or a struct powerdomain * upon
981 * success.
982 */
983struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
984{
985 /*
986 * XXX Assumes that all omap_hwmod powerdomains are identical.
987 * This may not necessarily be true. There should be a sanity
988 * check in here to WARN() if any difference appears.
989 */
990 if (!od->hwmods_cnt)
991 return NULL;
992
993 return omap_hwmod_get_pwrdm(od->hwmods[0]);
994}
995
Paul Walmsleydb2a60b2010-07-26 16:34:33 -0600996/**
997 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
998 * @od: struct omap_device *
999 *
1000 * Return the MPU's virtual address for the base of the hwmod, from
1001 * the ioremap() that the hwmod code does. Only valid if there is one
1002 * hwmod associated with this device. Returns NULL if there are zero
1003 * or more than one hwmods associated with this omap_device;
1004 * otherwise, passes along the return value from
1005 * omap_hwmod_get_mpu_rt_va().
1006 */
1007void __iomem *omap_device_get_rt_va(struct omap_device *od)
1008{
1009 if (od->hwmods_cnt != 1)
1010 return NULL;
1011
1012 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1013}
1014
Nishanth Menon1f8a7d52011-07-27 15:02:32 -05001015/**
1016 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1017 * device pointer.
1018 * @oh_name: name of the hwmod device
1019 *
1020 * Returns back a struct device * pointer associated with a hwmod
1021 * device represented by a hwmod_name
1022 */
1023struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1024{
1025 struct omap_hwmod *oh;
1026
1027 if (!oh_name) {
1028 WARN(1, "%s: no hwmod name!\n", __func__);
1029 return ERR_PTR(-EINVAL);
1030 }
1031
1032 oh = omap_hwmod_lookup(oh_name);
1033 if (IS_ERR_OR_NULL(oh)) {
1034 WARN(1, "%s: no hwmod for %s\n", __func__,
1035 oh_name);
1036 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1037 }
1038 if (IS_ERR_OR_NULL(oh->od)) {
1039 WARN(1, "%s: no omap_device for %s\n", __func__,
1040 oh_name);
1041 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1042 }
1043
1044 if (IS_ERR_OR_NULL(oh->od->pdev))
1045 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1046
1047 return &oh->od->pdev->dev;
1048}
1049EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1050
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001051/*
1052 * Public functions intended for use in omap_device_pm_latency
1053 * .activate_func and .deactivate_func function pointers
1054 */
1055
1056/**
1057 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1058 * @od: struct omap_device *od
1059 *
1060 * Enable all underlying hwmods. Returns 0.
1061 */
1062int omap_device_enable_hwmods(struct omap_device *od)
1063{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001064 int i;
1065
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001066 for (i = 0; i < od->hwmods_cnt; i++)
1067 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001068
1069 /* XXX pass along return value here? */
1070 return 0;
1071}
1072
1073/**
1074 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1075 * @od: struct omap_device *od
1076 *
1077 * Idle all underlying hwmods. Returns 0.
1078 */
1079int omap_device_idle_hwmods(struct omap_device *od)
1080{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001081 int i;
1082
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001083 for (i = 0; i < od->hwmods_cnt; i++)
1084 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001085
1086 /* XXX pass along return value here? */
1087 return 0;
1088}
1089
1090/**
1091 * omap_device_disable_clocks - disable all main and interface clocks
1092 * @od: struct omap_device *od
1093 *
1094 * Disable the main functional clock and interface clock for all of the
1095 * omap_hwmods associated with the omap_device. Returns 0.
1096 */
1097int omap_device_disable_clocks(struct omap_device *od)
1098{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001099 int i;
1100
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001101 for (i = 0; i < od->hwmods_cnt; i++)
1102 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001103
1104 /* XXX pass along return value here? */
1105 return 0;
1106}
1107
1108/**
1109 * omap_device_enable_clocks - enable all main and interface clocks
1110 * @od: struct omap_device *od
1111 *
1112 * Enable the main functional clock and interface clock for all of the
1113 * omap_hwmods associated with the omap_device. Returns 0.
1114 */
1115int omap_device_enable_clocks(struct omap_device *od)
1116{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001117 int i;
1118
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001119 for (i = 0; i < od->hwmods_cnt; i++)
1120 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001121
1122 /* XXX pass along return value here? */
1123 return 0;
1124}
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001125
1126struct device omap_device_parent = {
1127 .init_name = "omap",
1128 .parent = &platform_bus,
1129};
1130
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001131static struct notifier_block platform_nb = {
1132 .notifier_call = _omap_device_notifier_call,
1133};
1134
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001135static int __init omap_device_init(void)
1136{
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001137 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001138 return device_register(&omap_device_parent);
1139}
1140core_initcall(omap_device_init);