blob: e065daa537c07bd8c8946d394d9ab60c0b120c3b [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 Lindgren25c7d492012-10-02 17:25:48 -070092#include "omap_device.h"
Tony Lindgren2a296c82012-10-02 17:41:35 -070093#include "omap_hwmod.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_early_device_register(struct platform_device *pdev);
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700100
Benoit Coussonb7b5bc92011-08-09 16:54:19 +0200101static struct omap_device_pm_latency omap_default_latency[] = {
102 {
103 .deactivate_func = omap_device_idle_hwmods,
104 .activate_func = omap_device_enable_hwmods,
105 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
106 }
107};
108
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300109/* Private functions */
110
111/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300112 * _omap_device_activate - increase device readiness
113 * @od: struct omap_device *
114 * @ignore_lat: increase to latency target (0) or full readiness (1)?
115 *
116 * Increase readiness of omap_device @od (thus decreasing device
117 * wakeup latency, but consuming more power). If @ignore_lat is
118 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
119 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
120 * latency is greater than the requested maximum wakeup latency, step
121 * backwards in the omap_device_pm_latency table to ensure the
122 * device's maximum wakeup latency is less than or equal to the
123 * requested maximum wakeup latency. Returns 0.
124 */
125static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
126{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700127 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300128
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700129 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300130
131 while (od->pm_lat_level > 0) {
132 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700133 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300134
135 od->pm_lat_level--;
136
137 odpl = od->pm_lats + od->pm_lat_level;
138
139 if (!ignore_lat &&
140 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
141 break;
142
Kevin Hilmand2292662009-12-08 16:34:23 -0700143 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300144
145 /* XXX check return code */
146 odpl->activate_func(od);
147
Kevin Hilmand2292662009-12-08 16:34:23 -0700148 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300149
Tony Lindgrenf0594292009-10-19 15:25:24 -0700150 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700151 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300152
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700153 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600154 "omap_device: pm_lat %d: activate: elapsed time %llu nsec\n",
155 od->pm_lat_level, act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300156
Kevin Hilman9799aca2010-01-26 20:13:02 -0700157 if (act_lat > odpl->activate_lat) {
158 odpl->activate_lat_worst = act_lat;
159 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
160 odpl->activate_lat = act_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700161 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600162 "new worst case activate latency %d: %llu\n",
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300163 od->pm_lat_level, act_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700164 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700165 dev_warn(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600166 "activate latency %d higher than expected. (%llu > %d)\n",
Kevin Hilman49882c92011-07-21 09:58:36 -0700167 od->pm_lat_level, act_lat,
168 odpl->activate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700169 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300170
171 od->dev_wakeup_lat -= odpl->activate_lat;
172 }
173
174 return 0;
175}
176
177/**
178 * _omap_device_deactivate - decrease device readiness
179 * @od: struct omap_device *
180 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
181 *
182 * Decrease readiness of omap_device @od (thus increasing device
183 * wakeup latency, but conserving power). If @ignore_lat is
184 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
185 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
186 * latency is less than the requested maximum wakeup latency, step
187 * forwards in the omap_device_pm_latency table to ensure the device's
188 * maximum wakeup latency is less than or equal to the requested
189 * maximum wakeup latency. Returns 0.
190 */
191static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
192{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700193 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300194
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700195 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300196
197 while (od->pm_lat_level < od->pm_lats_cnt) {
198 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700199 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300200
201 odpl = od->pm_lats + od->pm_lat_level;
202
203 if (!ignore_lat &&
204 ((od->dev_wakeup_lat + odpl->activate_lat) >
205 od->_dev_wakeup_lat_limit))
206 break;
207
Kevin Hilmand2292662009-12-08 16:34:23 -0700208 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300209
210 /* XXX check return code */
211 odpl->deactivate_func(od);
212
Kevin Hilmand2292662009-12-08 16:34:23 -0700213 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300214
Tony Lindgrenf0594292009-10-19 15:25:24 -0700215 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700216 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300217
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700218 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600219 "omap_device: pm_lat %d: deactivate: elapsed time %llu nsec\n",
220 od->pm_lat_level, deact_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300221
Kevin Hilman9799aca2010-01-26 20:13:02 -0700222 if (deact_lat > odpl->deactivate_lat) {
223 odpl->deactivate_lat_worst = deact_lat;
224 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
225 odpl->deactivate_lat = deact_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700226 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600227 "new worst case deactivate latency %d: %llu\n",
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300228 od->pm_lat_level, deact_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700229 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700230 dev_warn(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600231 "deactivate latency %d higher than expected. (%llu > %d)\n",
Kevin Hilman49882c92011-07-21 09:58:36 -0700232 od->pm_lat_level, deact_lat,
233 odpl->deactivate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700234 }
235
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300236 od->dev_wakeup_lat += odpl->activate_lat;
237
238 od->pm_lat_level++;
239 }
240
241 return 0;
242}
243
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600244static void _add_clkdev(struct omap_device *od, const char *clk_alias,
245 const char *clk_name)
246{
247 struct clk *r;
248 struct clk_lookup *l;
249
250 if (!clk_alias || !clk_name)
251 return;
252
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700253 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600254
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700255 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600256 if (!IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700257 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700258 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600259 clk_put(r);
260 return;
261 }
262
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600263 r = clk_get(NULL, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600264 if (IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700265 dev_err(&od->pdev->dev,
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600266 "clk_get for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600267 return;
268 }
269
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700270 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600271 if (!l) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700272 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700273 "clkdev_alloc for %s failed\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600274 return;
275 }
276
277 clkdev_add(l);
278}
279
Partha Basak4ef7aca2010-09-21 19:23:04 +0200280/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600281 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
282 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +0200283 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600284 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +0200285 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600286 * For the main clock and every optional clock present per hwmod per
287 * omap_device, this function adds an entry in the clkdev table of the
288 * form <dev-id=dev_name, con-id=role> if it does not exist already.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200289 *
290 * The function is called from inside omap_device_build_ss(), after
291 * omap_device_register.
292 *
293 * This allows drivers to get a pointer to its optional clocks based on its role
294 * by calling clk_get(<dev*>, <role>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600295 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200296 *
297 * No return value.
298 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600299static void _add_hwmod_clocks_clkdev(struct omap_device *od,
300 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200301{
302 int i;
303
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600304 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200305
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600306 for (i = 0; i < oh->opt_clks_cnt; i++)
307 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200308}
309
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300310
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200311/**
312 * omap_device_build_from_dt - build an omap_device with multiple hwmods
313 * @pdev_name: name of the platform_device driver to use
314 * @pdev_id: this platform_device's connection ID
315 * @oh: ptr to the single omap_hwmod that backs this omap_device
316 * @pdata: platform_data ptr to associate with the platform_device
317 * @pdata_len: amount of memory pointed to by @pdata
318 * @pm_lats: pointer to a omap_device_pm_latency array for this device
319 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
320 * @is_early_device: should the device be registered as an early device or not
321 *
322 * Function for building an omap_device already registered from device-tree
323 *
324 * Returns 0 or PTR_ERR() on error.
325 */
326static int omap_device_build_from_dt(struct platform_device *pdev)
327{
328 struct omap_hwmod **hwmods;
329 struct omap_device *od;
330 struct omap_hwmod *oh;
331 struct device_node *node = pdev->dev.of_node;
332 const char *oh_name;
333 int oh_cnt, i, ret = 0;
334
335 oh_cnt = of_property_count_strings(node, "ti,hwmods");
336 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
Benoit Cousson5dc06b72012-01-20 18:14:00 +0100337 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200338 return -ENODEV;
339 }
340
341 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
342 if (!hwmods) {
343 ret = -ENOMEM;
344 goto odbfd_exit;
345 }
346
347 for (i = 0; i < oh_cnt; i++) {
348 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
349 oh = omap_hwmod_lookup(oh_name);
350 if (!oh) {
351 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
352 oh_name);
353 ret = -EINVAL;
354 goto odbfd_exit1;
355 }
356 hwmods[i] = oh;
357 }
358
359 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
360 if (!od) {
361 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
362 oh_name);
363 ret = PTR_ERR(od);
364 goto odbfd_exit1;
365 }
366
Peter Ujfalusi3956a1a2012-08-23 16:54:09 +0300367 /* Fix up missing resource names */
368 for (i = 0; i < pdev->num_resources; i++) {
369 struct resource *r = &pdev->resource[i];
370
371 if (r->name == NULL)
372 r->name = dev_name(&pdev->dev);
373 }
374
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200375 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
376 omap_device_disable_idle_on_suspend(pdev);
377
378 pdev->dev.pm_domain = &omap_device_pm_domain;
379
380odbfd_exit1:
381 kfree(hwmods);
382odbfd_exit:
383 return ret;
384}
385
386static int _omap_device_notifier_call(struct notifier_block *nb,
387 unsigned long event, void *dev)
388{
389 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilmane7533452012-07-10 11:13:16 -0700390 struct omap_device *od;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200391
392 switch (event) {
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200393 case BUS_NOTIFY_DEL_DEVICE:
394 if (pdev->archdata.od)
395 omap_device_delete(pdev->archdata.od);
396 break;
Kevin Hilmane7533452012-07-10 11:13:16 -0700397 case BUS_NOTIFY_ADD_DEVICE:
398 if (pdev->dev.of_node)
399 omap_device_build_from_dt(pdev);
400 /* fall through */
401 default:
402 od = to_omap_device(pdev);
403 if (od)
404 od->_driver_status = event;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200405 }
406
407 return NOTIFY_DONE;
408}
409
410
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300411/* Public functions for use by core code */
412
413/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700414 * omap_device_get_context_loss_count - get lost context count
415 * @od: struct omap_device *
416 *
417 * Using the primary hwmod, query the context loss count for this
418 * device.
419 *
420 * Callers should consider context for this device lost any time this
421 * function returns a value different than the value the caller got
422 * the last time it called this function.
423 *
424 * If any hwmods exist for the omap_device assoiated with @pdev,
425 * return the context loss counter for that hwmod, otherwise return
426 * zero.
427 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +0300428int omap_device_get_context_loss_count(struct platform_device *pdev)
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700429{
430 struct omap_device *od;
431 u32 ret = 0;
432
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600433 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700434
435 if (od->hwmods_cnt)
436 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
437
438 return ret;
439}
440
441/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300442 * omap_device_count_resources - count number of struct resource entries needed
443 * @od: struct omap_device *
Peter Ujfalusidad41912012-11-21 16:15:17 -0700444 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300445 *
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 */
Peter Ujfalusidad41912012-11-21 16:15:17 -0700451static int omap_device_count_resources(struct omap_device *od,
452 unsigned long flags)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300453{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300454 int c = 0;
455 int i;
456
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600457 for (i = 0; i < od->hwmods_cnt; i++)
Peter Ujfalusidad41912012-11-21 16:15:17 -0700458 c += omap_hwmod_count_resources(od->hwmods[i], flags);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300459
Paul Walmsley7852ec02012-07-26 00:54:26 -0600460 pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
461 od->pdev->name, c, od->hwmods_cnt);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300462
463 return c;
464}
465
466/**
467 * omap_device_fill_resources - fill in array of struct resource
468 * @od: struct omap_device *
469 * @res: pointer to an array of struct resource to be filled in
470 *
471 * Populate one or more empty struct resource pointed to by @res with
472 * the resource data for this omap_device @od. Used by
473 * omap_device_build_ss() after calling omap_device_count_resources().
474 * Ideally this function would not be needed at all. If omap_device
475 * replaces platform_device, then we can specify our own
476 * get_resource()/ get_irq()/etc functions that use the underlying
477 * omap_hwmod information. Or if platform_device is extended to use
478 * subarchitecture-specific function pointers, the various
479 * platform_device functions can simply call omap_device internal
480 * functions to get device resources. Hacking around the existing
481 * platform_device code wastes memory. Returns 0.
482 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700483static int omap_device_fill_resources(struct omap_device *od,
484 struct resource *res)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300485{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300486 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;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300491 }
492
493 return 0;
494}
495
496/**
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530497 * _od_fill_dma_resources - fill in array of struct resource with dma resources
498 * @od: struct omap_device *
499 * @res: pointer to an array of struct resource to be filled in
500 *
501 * Populate one or more empty struct resource pointed to by @res with
502 * the dma resource data for this omap_device @od. Used by
503 * omap_device_alloc() after calling omap_device_count_resources().
504 *
505 * Ideally this function would not be needed at all. If we have
506 * mechanism to get dma resources from DT.
507 *
508 * Returns 0.
509 */
510static int _od_fill_dma_resources(struct omap_device *od,
511 struct resource *res)
512{
513 int i, r;
514
515 for (i = 0; i < od->hwmods_cnt; i++) {
516 r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
517 res += r;
518 }
519
520 return 0;
521}
522
523/**
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200524 * omap_device_alloc - allocate an omap_device
525 * @pdev: platform_device that will be included in this omap_device
526 * @oh: ptr to the single omap_hwmod that backs this omap_device
527 * @pdata: platform_data ptr to associate with the platform_device
528 * @pdata_len: amount of memory pointed to by @pdata
529 * @pm_lats: pointer to a omap_device_pm_latency array for this device
530 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
531 *
532 * Convenience function for allocating an omap_device structure and filling
533 * hwmods, resources and pm_latency attributes.
534 *
535 * Returns an struct omap_device pointer or ERR_PTR() on error;
536 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800537struct omap_device *omap_device_alloc(struct platform_device *pdev,
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200538 struct omap_hwmod **ohs, int oh_cnt,
539 struct omap_device_pm_latency *pm_lats,
540 int pm_lats_cnt)
541{
542 int ret = -ENOMEM;
543 struct omap_device *od;
544 struct resource *res = NULL;
545 int i, res_count;
546 struct omap_hwmod **hwmods;
547
548 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
549 if (!od) {
550 ret = -ENOMEM;
551 goto oda_exit1;
552 }
553 od->hwmods_cnt = oh_cnt;
554
555 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
556 if (!hwmods)
557 goto oda_exit2;
558
559 od->hwmods = hwmods;
560 od->pdev = pdev;
561
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530562 /*
Peter Ujfalusic567b052012-11-21 16:15:18 -0700563 * Non-DT Boot:
564 * Here, pdev->num_resources = 0, and we should get all the
565 * resources from hwmod.
566 *
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530567 * DT Boot:
568 * OF framework will construct the resource structure (currently
569 * does for MEM & IRQ resource) and we should respect/use these
570 * resources, killing hwmod dependency.
571 * If pdev->num_resources > 0, we assume that MEM & IRQ resources
572 * have been allocated by OF layer already (through DTB).
Peter Ujfalusic567b052012-11-21 16:15:18 -0700573 * As preparation for the future we examine the OF provided resources
574 * to see if we have DMA resources provided already. In this case
575 * there is no need to update the resources for the device, we use the
576 * OF provided ones.
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530577 *
578 * TODO: Once DMA resource is available from OF layer, we should
579 * kill filling any resources from hwmod.
580 */
Peter Ujfalusic567b052012-11-21 16:15:18 -0700581 if (!pdev->num_resources) {
582 /* Count all resources for the device */
583 res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
584 IORESOURCE_DMA |
585 IORESOURCE_MEM);
586 } else {
587 /* Take a look if we already have DMA resource via DT */
588 for (i = 0; i < pdev->num_resources; i++) {
589 struct resource *r = &pdev->resource[i];
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200590
Peter Ujfalusic567b052012-11-21 16:15:18 -0700591 /* We have it, no need to touch the resources */
592 if (r->flags == IORESOURCE_DMA)
593 goto have_everything;
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530594 }
Peter Ujfalusic567b052012-11-21 16:15:18 -0700595 /* Count only DMA resources for the device */
596 res_count = omap_device_count_resources(od, IORESOURCE_DMA);
597 /* The device has no DMA resource, no need for update */
598 if (!res_count)
599 goto have_everything;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200600
Peter Ujfalusic567b052012-11-21 16:15:18 -0700601 res_count += pdev->num_resources;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200602 }
603
Peter Ujfalusic567b052012-11-21 16:15:18 -0700604 /* Allocate resources memory to account for new resources */
605 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
606 if (!res)
607 goto oda_exit3;
608
609 if (!pdev->num_resources) {
610 dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
611 __func__, res_count);
612 omap_device_fill_resources(od, res);
613 } else {
614 dev_dbg(&pdev->dev,
615 "%s: appending %d DMA resources from hwmod\n",
616 __func__, res_count - pdev->num_resources);
617 memcpy(res, pdev->resource,
618 sizeof(struct resource) * pdev->num_resources);
619 _od_fill_dma_resources(od, &res[pdev->num_resources]);
620 }
621
622 ret = platform_device_add_resources(pdev, res, res_count);
623 kfree(res);
624
625 if (ret)
626 goto oda_exit3;
627
628have_everything:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200629 if (!pm_lats) {
630 pm_lats = omap_default_latency;
631 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
632 }
633
634 od->pm_lats_cnt = pm_lats_cnt;
635 od->pm_lats = kmemdup(pm_lats,
636 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
637 GFP_KERNEL);
638 if (!od->pm_lats)
639 goto oda_exit3;
640
641 pdev->archdata.od = od;
642
643 for (i = 0; i < oh_cnt; i++) {
644 hwmods[i]->od = od;
645 _add_hwmod_clocks_clkdev(od, hwmods[i]);
646 }
647
648 return od;
649
650oda_exit3:
651 kfree(hwmods);
652oda_exit2:
653 kfree(od);
654oda_exit1:
655 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
656
657 return ERR_PTR(ret);
658}
659
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800660void omap_device_delete(struct omap_device *od)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200661{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200662 if (!od)
663 return;
664
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200665 od->pdev->archdata.od = NULL;
666 kfree(od->pm_lats);
667 kfree(od->hwmods);
668 kfree(od);
669}
670
671/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300672 * omap_device_build - build and register an omap_device with one omap_hwmod
673 * @pdev_name: name of the platform_device driver to use
674 * @pdev_id: this platform_device's connection ID
675 * @oh: ptr to the single omap_hwmod that backs this omap_device
676 * @pdata: platform_data ptr to associate with the platform_device
677 * @pdata_len: amount of memory pointed to by @pdata
678 * @pm_lats: pointer to a omap_device_pm_latency array for this device
679 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700680 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300681 *
682 * Convenience function for building and registering a single
683 * omap_device record, which in turn builds and registers a
684 * platform_device record. See omap_device_build_ss() for more
685 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
686 * passes along the return value of omap_device_build_ss().
687 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800688struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300689 struct omap_hwmod *oh, void *pdata,
690 int pdata_len,
691 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700692 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300693{
694 struct omap_hwmod *ohs[] = { oh };
695
696 if (!oh)
697 return ERR_PTR(-EINVAL);
698
699 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700700 pdata_len, pm_lats, pm_lats_cnt,
701 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300702}
703
704/**
705 * omap_device_build_ss - build and register an omap_device with multiple hwmods
706 * @pdev_name: name of the platform_device driver to use
707 * @pdev_id: this platform_device's connection ID
708 * @oh: ptr to the single omap_hwmod that backs this omap_device
709 * @pdata: platform_data ptr to associate with the platform_device
710 * @pdata_len: amount of memory pointed to by @pdata
711 * @pm_lats: pointer to a omap_device_pm_latency array for this device
712 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700713 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300714 *
715 * Convenience function for building and registering an omap_device
716 * subsystem record. Subsystem records consist of multiple
717 * omap_hwmods. This function in turn builds and registers a
718 * platform_device record. Returns an ERR_PTR() on error, or passes
719 * along the return value of omap_device_register().
720 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800721struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300722 struct omap_hwmod **ohs, int oh_cnt,
723 void *pdata, int pdata_len,
724 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700725 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300726{
727 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700728 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300729 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300730
731 if (!ohs || oh_cnt == 0 || !pdev_name)
732 return ERR_PTR(-EINVAL);
733
734 if (!pdata && pdata_len > 0)
735 return ERR_PTR(-EINVAL);
736
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700737 pdev = platform_device_alloc(pdev_name, pdev_id);
738 if (!pdev) {
739 ret = -ENOMEM;
740 goto odbs_exit;
741 }
742
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200743 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
744 if (pdev->id != -1)
745 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
746 else
747 dev_set_name(&pdev->dev, "%s", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300748
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200749 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
Wei Yongjuna87e6622012-09-21 14:32:04 +0800750 if (IS_ERR(od))
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700751 goto odbs_exit1;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300752
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700753 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000754 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200755 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700756
757 if (is_early_device)
758 ret = omap_early_device_register(pdev);
759 else
760 ret = omap_device_register(pdev);
761 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200762 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600763
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700764 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300765
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700766odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200767 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700768odbs_exit1:
769 platform_device_put(pdev);
770odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300771
772 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
773
774 return ERR_PTR(ret);
775}
776
777/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700778 * omap_early_device_register - register an omap_device as an early platform
779 * device.
780 * @od: struct omap_device * to register
781 *
782 * Register the omap_device structure. This currently just calls
783 * platform_early_add_device() on the underlying platform_device.
784 * Returns 0 by default.
785 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800786static int __init omap_early_device_register(struct platform_device *pdev)
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700787{
788 struct platform_device *devices[1];
789
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700790 devices[0] = pdev;
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700791 early_platform_add_devices(devices, 1);
792 return 0;
793}
794
Kevin Hilman256a5432011-07-12 22:48:03 +0200795#ifdef CONFIG_PM_RUNTIME
Kevin Hilman638080c2011-04-29 00:36:42 +0200796static int _od_runtime_suspend(struct device *dev)
797{
798 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700799 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200800
Kevin Hilman345f79b2011-05-31 16:08:09 -0700801 ret = pm_generic_runtime_suspend(dev);
802
803 if (!ret)
804 omap_device_idle(pdev);
805
806 return ret;
807}
808
809static int _od_runtime_idle(struct device *dev)
810{
811 return pm_generic_runtime_idle(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200812}
813
814static int _od_runtime_resume(struct device *dev)
815{
816 struct platform_device *pdev = to_platform_device(dev);
817
Kevin Hilman345f79b2011-05-31 16:08:09 -0700818 omap_device_enable(pdev);
819
820 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200821}
Kevin Hilman256a5432011-07-12 22:48:03 +0200822#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200823
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200824#ifdef CONFIG_SUSPEND
825static int _od_suspend_noirq(struct device *dev)
826{
827 struct platform_device *pdev = to_platform_device(dev);
828 struct omap_device *od = to_omap_device(pdev);
829 int ret;
830
Kevin Hilman72bb6f92012-07-10 15:29:04 -0700831 /* Don't attempt late suspend on a driver that is not bound */
832 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
833 return 0;
834
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200835 ret = pm_generic_suspend_noirq(dev);
836
837 if (!ret && !pm_runtime_status_suspended(dev)) {
838 if (pm_generic_runtime_suspend(dev) == 0) {
Paul Walmsleyb7c39a32012-03-03 13:15:33 -0700839 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
840 omap_device_idle(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200841 od->flags |= OMAP_DEVICE_SUSPENDED;
842 }
843 }
844
845 return ret;
846}
847
848static int _od_resume_noirq(struct device *dev)
849{
850 struct platform_device *pdev = to_platform_device(dev);
851 struct omap_device *od = to_omap_device(pdev);
852
853 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
854 !pm_runtime_status_suspended(dev)) {
855 od->flags &= ~OMAP_DEVICE_SUSPENDED;
Paul Walmsleyb7c39a32012-03-03 13:15:33 -0700856 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
857 omap_device_enable(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200858 pm_generic_runtime_resume(dev);
859 }
860
861 return pm_generic_resume_noirq(dev);
862}
Kevin Hilman126caf12011-09-01 10:59:36 -0700863#else
864#define _od_suspend_noirq NULL
865#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200866#endif
867
Kevin Hilman3ec2dec2012-02-15 11:47:45 -0800868struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200869 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200870 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
871 _od_runtime_idle)
Kevin Hilman638080c2011-04-29 00:36:42 +0200872 USE_PLATFORM_PM_SLEEP_OPS
Kevin Hilmanff353362011-08-25 15:31:14 +0200873 .suspend_noirq = _od_suspend_noirq,
874 .resume_noirq = _od_resume_noirq,
Kevin Hilman638080c2011-04-29 00:36:42 +0200875 }
876};
877
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700878/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300879 * omap_device_register - register an omap_device with one omap_hwmod
880 * @od: struct omap_device * to register
881 *
882 * Register the omap_device structure. This currently just calls
883 * platform_device_register() on the underlying platform_device.
884 * Returns the return value of platform_device_register().
885 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800886int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300887{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700888 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300889
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700890 pdev->dev.pm_domain = &omap_device_pm_domain;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700891 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300892}
893
894
895/* Public functions for use by device drivers through struct platform_data */
896
897/**
898 * omap_device_enable - fully activate an omap_device
899 * @od: struct omap_device * to activate
900 *
901 * Do whatever is necessary for the hwmods underlying omap_device @od
902 * to be accessible and ready to operate. This generally involves
903 * enabling clocks, setting SYSCONFIG registers; and in the future may
904 * involve remuxing pins. Device drivers should call this function
905 * (through platform_data function pointers) where they would normally
906 * enable clocks, etc. Returns -EINVAL if called when the omap_device
907 * is already enabled, or passes along the return value of
908 * _omap_device_activate().
909 */
910int omap_device_enable(struct platform_device *pdev)
911{
912 int ret;
913 struct omap_device *od;
914
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) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700918 dev_warn(&pdev->dev,
919 "omap_device: %s() called from invalid state %d\n",
920 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300921 return -EINVAL;
922 }
923
924 /* Enable everything if we're enabling this device from scratch */
925 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
926 od->pm_lat_level = od->pm_lats_cnt;
927
928 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
929
930 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700931 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300932 od->_state = OMAP_DEVICE_STATE_ENABLED;
933
934 return ret;
935}
936
937/**
938 * omap_device_idle - idle an omap_device
939 * @od: struct omap_device * to idle
940 *
941 * Idle omap_device @od by calling as many .deactivate_func() entries
942 * in the omap_device's pm_lats table as is possible without exceeding
943 * the device's maximum wakeup latency limit, pm_lat_limit. Device
944 * drivers should call this function (through platform_data function
945 * pointers) where they would normally disable clocks after operations
946 * complete, etc.. Returns -EINVAL if the omap_device is not
947 * currently enabled, or passes along the return value of
948 * _omap_device_deactivate().
949 */
950int omap_device_idle(struct platform_device *pdev)
951{
952 int ret;
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 (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700958 dev_warn(&pdev->dev,
959 "omap_device: %s() called from invalid state %d\n",
960 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300961 return -EINVAL;
962 }
963
964 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
965
966 od->_state = OMAP_DEVICE_STATE_IDLE;
967
968 return ret;
969}
970
971/**
972 * omap_device_shutdown - shut down an omap_device
973 * @od: struct omap_device * to shut down
974 *
975 * Shut down omap_device @od by calling all .deactivate_func() entries
976 * in the omap_device's pm_lats table and then shutting down all of
977 * the underlying omap_hwmods. Used when a device is being "removed"
978 * or a device driver is being unloaded. Returns -EINVAL if the
979 * omap_device is not currently enabled or idle, or passes along the
980 * return value of _omap_device_deactivate().
981 */
982int omap_device_shutdown(struct platform_device *pdev)
983{
984 int ret, i;
985 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300986
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600987 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300988
989 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
990 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700991 dev_warn(&pdev->dev,
992 "omap_device: %s() called from invalid state %d\n",
993 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300994 return -EINVAL;
995 }
996
997 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
998
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600999 for (i = 0; i < od->hwmods_cnt; i++)
1000 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001001
1002 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
1003
1004 return ret;
1005}
1006
1007/**
Omar Ramirez Luna8bb9fde2012-09-23 17:28:18 -06001008 * omap_device_assert_hardreset - set a device's hardreset line
1009 * @pdev: struct platform_device * to reset
1010 * @name: const char * name of the reset line
1011 *
1012 * Set the hardreset line identified by @name on the IP blocks
1013 * associated with the hwmods backing the platform_device @pdev. All
1014 * of the hwmods associated with @pdev must have the same hardreset
1015 * line linked to them for this to work. Passes along the return value
1016 * of omap_hwmod_assert_hardreset() in the event of any failure, or
1017 * returns 0 upon success.
1018 */
1019int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
1020{
1021 struct omap_device *od = to_omap_device(pdev);
1022 int ret = 0;
1023 int i;
1024
1025 for (i = 0; i < od->hwmods_cnt; i++) {
1026 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
1027 if (ret)
1028 break;
1029 }
1030
1031 return ret;
1032}
1033
1034/**
1035 * omap_device_deassert_hardreset - release a device's hardreset line
1036 * @pdev: struct platform_device * to reset
1037 * @name: const char * name of the reset line
1038 *
1039 * Release the hardreset line identified by @name on the IP blocks
1040 * associated with the hwmods backing the platform_device @pdev. All
1041 * of the hwmods associated with @pdev must have the same hardreset
1042 * line linked to them for this to work. Passes along the return
1043 * value of omap_hwmod_deassert_hardreset() in the event of any
1044 * failure, or returns 0 upon success.
1045 */
1046int omap_device_deassert_hardreset(struct platform_device *pdev,
1047 const char *name)
1048{
1049 struct omap_device *od = to_omap_device(pdev);
1050 int ret = 0;
1051 int i;
1052
1053 for (i = 0; i < od->hwmods_cnt; i++) {
1054 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
1055 if (ret)
1056 break;
1057 }
1058
1059 return ret;
1060}
1061
1062/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001063 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
1064 * @od: struct omap_device *
1065 *
1066 * When a device's maximum wakeup latency limit changes, call some of
1067 * the .activate_func or .deactivate_func function pointers in the
1068 * omap_device's pm_lats array to ensure that the device's maximum
1069 * wakeup latency is less than or equal to the new latency limit.
1070 * Intended to be called by OMAP PM code whenever a device's maximum
1071 * wakeup latency limit changes (e.g., via
1072 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
1073 * done (e.g., if the omap_device is not currently idle, or if the
1074 * wakeup latency is already current with the new limit) or passes
1075 * along the return value of _omap_device_deactivate() or
1076 * _omap_device_activate().
1077 */
1078int omap_device_align_pm_lat(struct platform_device *pdev,
1079 u32 new_wakeup_lat_limit)
1080{
1081 int ret = -EINVAL;
1082 struct omap_device *od;
1083
Kevin Hilman8f0d69d2011-07-09 19:15:20 -06001084 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001085
1086 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
1087 return 0;
1088
1089 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
1090
1091 if (od->_state != OMAP_DEVICE_STATE_IDLE)
1092 return 0;
1093 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
1094 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
1095 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
1096 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
1097
1098 return ret;
1099}
1100
1101/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001102 * omap_device_get_pwrdm - return the powerdomain * associated with @od
1103 * @od: struct omap_device *
1104 *
1105 * Return the powerdomain associated with the first underlying
1106 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
1107 * code. Returns NULL on error or a struct powerdomain * upon
1108 * success.
1109 */
1110struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
1111{
1112 /*
1113 * XXX Assumes that all omap_hwmod powerdomains are identical.
1114 * This may not necessarily be true. There should be a sanity
1115 * check in here to WARN() if any difference appears.
1116 */
1117 if (!od->hwmods_cnt)
1118 return NULL;
1119
1120 return omap_hwmod_get_pwrdm(od->hwmods[0]);
1121}
1122
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06001123/**
1124 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
1125 * @od: struct omap_device *
1126 *
1127 * Return the MPU's virtual address for the base of the hwmod, from
1128 * the ioremap() that the hwmod code does. Only valid if there is one
1129 * hwmod associated with this device. Returns NULL if there are zero
1130 * or more than one hwmods associated with this omap_device;
1131 * otherwise, passes along the return value from
1132 * omap_hwmod_get_mpu_rt_va().
1133 */
1134void __iomem *omap_device_get_rt_va(struct omap_device *od)
1135{
1136 if (od->hwmods_cnt != 1)
1137 return NULL;
1138
1139 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1140}
1141
Nishanth Menon1f8a7d52011-07-27 15:02:32 -05001142/**
1143 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1144 * device pointer.
1145 * @oh_name: name of the hwmod device
1146 *
1147 * Returns back a struct device * pointer associated with a hwmod
1148 * device represented by a hwmod_name
1149 */
1150struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1151{
1152 struct omap_hwmod *oh;
1153
1154 if (!oh_name) {
1155 WARN(1, "%s: no hwmod name!\n", __func__);
1156 return ERR_PTR(-EINVAL);
1157 }
1158
1159 oh = omap_hwmod_lookup(oh_name);
1160 if (IS_ERR_OR_NULL(oh)) {
1161 WARN(1, "%s: no hwmod for %s\n", __func__,
1162 oh_name);
1163 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1164 }
1165 if (IS_ERR_OR_NULL(oh->od)) {
1166 WARN(1, "%s: no omap_device for %s\n", __func__,
1167 oh_name);
1168 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1169 }
1170
1171 if (IS_ERR_OR_NULL(oh->od->pdev))
1172 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1173
1174 return &oh->od->pdev->dev;
1175}
1176EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1177
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001178/*
1179 * Public functions intended for use in omap_device_pm_latency
1180 * .activate_func and .deactivate_func function pointers
1181 */
1182
1183/**
1184 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1185 * @od: struct omap_device *od
1186 *
1187 * Enable all underlying hwmods. Returns 0.
1188 */
1189int omap_device_enable_hwmods(struct omap_device *od)
1190{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001191 int i;
1192
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001193 for (i = 0; i < od->hwmods_cnt; i++)
1194 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001195
1196 /* XXX pass along return value here? */
1197 return 0;
1198}
1199
1200/**
1201 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1202 * @od: struct omap_device *od
1203 *
1204 * Idle all underlying hwmods. Returns 0.
1205 */
1206int omap_device_idle_hwmods(struct omap_device *od)
1207{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001208 int i;
1209
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001210 for (i = 0; i < od->hwmods_cnt; i++)
1211 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001212
1213 /* XXX pass along return value here? */
1214 return 0;
1215}
1216
1217/**
1218 * omap_device_disable_clocks - disable all main and interface clocks
1219 * @od: struct omap_device *od
1220 *
1221 * Disable the main functional clock and interface clock for all of the
1222 * omap_hwmods associated with the omap_device. Returns 0.
1223 */
1224int omap_device_disable_clocks(struct omap_device *od)
1225{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001226 int i;
1227
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001228 for (i = 0; i < od->hwmods_cnt; i++)
1229 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001230
1231 /* XXX pass along return value here? */
1232 return 0;
1233}
1234
1235/**
1236 * omap_device_enable_clocks - enable all main and interface clocks
1237 * @od: struct omap_device *od
1238 *
1239 * Enable the main functional clock and interface clock for all of the
1240 * omap_hwmods associated with the omap_device. Returns 0.
1241 */
1242int omap_device_enable_clocks(struct omap_device *od)
1243{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001244 int i;
1245
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001246 for (i = 0; i < od->hwmods_cnt; i++)
1247 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001248
1249 /* XXX pass along return value here? */
1250 return 0;
1251}
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001252
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001253static struct notifier_block platform_nb = {
1254 .notifier_call = _omap_device_notifier_call,
1255};
1256
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001257static int __init omap_device_init(void)
1258{
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001259 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman3ec2dec2012-02-15 11:47:45 -08001260 return 0;
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001261}
1262core_initcall(omap_device_init);
Kevin Hilman9634c8d2012-07-10 15:06:11 -07001263
1264/**
1265 * omap_device_late_idle - idle devices without drivers
1266 * @dev: struct device * associated with omap_device
1267 * @data: unused
1268 *
1269 * Check the driver bound status of this device, and idle it
1270 * if there is no driver attached.
1271 */
1272static int __init omap_device_late_idle(struct device *dev, void *data)
1273{
1274 struct platform_device *pdev = to_platform_device(dev);
1275 struct omap_device *od = to_omap_device(pdev);
1276
1277 if (!od)
1278 return 0;
1279
1280 /*
1281 * If omap_device state is enabled, but has no driver bound,
1282 * idle it.
1283 */
1284 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) {
1285 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
1286 dev_warn(dev, "%s: enabled but no driver. Idling\n",
1287 __func__);
1288 omap_device_idle(pdev);
1289 }
1290 }
1291
1292 return 0;
1293}
1294
1295static int __init omap_device_late_init(void)
1296{
1297 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
1298 return 0;
1299}
1300late_initcall(omap_device_late_init);