blob: 6f5c58096819de7913ea707daf45cc03c960dba2 [file] [log] [blame]
Kevin Hilman9cf793f2012-02-20 09:43:30 -08001
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03002/*
3 * omap_device implementation
4 *
Paul Walmsley887adea2010-07-26 16:34:33 -06005 * Copyright (C) 2009-2010 Nokia Corporation
Paul Walmsley4788da22010-05-18 20:24:05 -06006 * Paul Walmsley, Kevin Hilman
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03007 *
8 * Developed in collaboration with (alphabetical order): Benoit
Paul Walmsley4788da22010-05-18 20:24:05 -06009 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030010 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
11 * Woodruff
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This code provides a consistent interface for OMAP device drivers
18 * to control power management and interconnect properties of their
19 * devices.
20 *
21 * In the medium- to long-term, this code should either be
22 * a) implemented via arch-specific pointers in platform_data
23 * or
24 * b) implemented as a proper omap_bus/omap_device in Linux, no more
25 * platform_data func pointers
26 *
27 *
28 * Guidelines for usage by driver authors:
29 *
30 * 1. These functions are intended to be used by device drivers via
31 * function pointers in struct platform_data. As an example,
32 * omap_device_enable() should be passed to the driver as
33 *
34 * struct foo_driver_platform_data {
35 * ...
36 * int (*device_enable)(struct platform_device *pdev);
37 * ...
38 * }
39 *
40 * Note that the generic "device_enable" name is used, rather than
41 * "omap_device_enable". This is so other architectures can pass in their
42 * own enable/disable functions here.
43 *
44 * This should be populated during device setup:
45 *
46 * ...
47 * pdata->device_enable = omap_device_enable;
48 * ...
49 *
50 * 2. Drivers should first check to ensure the function pointer is not null
51 * before calling it, as in:
52 *
53 * if (pdata->device_enable)
54 * pdata->device_enable(pdev);
55 *
56 * This allows other architectures that don't use similar device_enable()/
57 * device_shutdown() functions to execute normally.
58 *
59 * ...
60 *
61 * Suggested usage by device drivers:
62 *
63 * During device initialization:
64 * device_enable()
65 *
66 * During device idle:
67 * (save remaining device context if necessary)
68 * device_idle();
69 *
70 * During device resume:
71 * device_enable();
72 * (restore context if necessary)
73 *
74 * During device shutdown:
75 * device_shutdown()
76 * (device must be reinitialized at this point to use it again)
77 *
78 */
79#undef DEBUG
80
81#include <linux/kernel.h>
Axel Lin55581412011-11-07 12:27:10 -080082#include <linux/export.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030083#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090084#include <linux/slab.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030085#include <linux/err.h>
86#include <linux/io.h>
Partha Basak4ef7aca2010-09-21 19:23:04 +020087#include <linux/clk.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070088#include <linux/clkdev.h>
Kevin Hilman345f79b2011-05-31 16:08:09 -070089#include <linux/pm_runtime.h>
Benoit Coussondc2d07e2011-08-10 13:32:08 +020090#include <linux/of.h>
91#include <linux/notifier.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030092
Tony Lindgrence491cf2009-10-20 09:40:47 -070093#include <plat/omap_device.h>
94#include <plat/omap_hwmod.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070095#include <plat/clock.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030096
97/* These parameters are passed to _omap_device_{de,}activate() */
98#define USE_WAKEUP_LAT 0
99#define IGNORE_WAKEUP_LAT 1
100
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700101static int omap_early_device_register(struct platform_device *pdev);
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700102
Benoit Coussonb7b5bc92011-08-09 16:54:19 +0200103static struct omap_device_pm_latency omap_default_latency[] = {
104 {
105 .deactivate_func = omap_device_idle_hwmods,
106 .activate_func = omap_device_enable_hwmods,
107 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
108 }
109};
110
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300111/* Private functions */
112
113/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300114 * _omap_device_activate - increase device readiness
115 * @od: struct omap_device *
116 * @ignore_lat: increase to latency target (0) or full readiness (1)?
117 *
118 * Increase readiness of omap_device @od (thus decreasing device
119 * wakeup latency, but consuming more power). If @ignore_lat is
120 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
121 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
122 * latency is greater than the requested maximum wakeup latency, step
123 * backwards in the omap_device_pm_latency table to ensure the
124 * device's maximum wakeup latency is less than or equal to the
125 * requested maximum wakeup latency. Returns 0.
126 */
127static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
128{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700129 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300130
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700131 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300132
133 while (od->pm_lat_level > 0) {
134 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700135 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300136
137 od->pm_lat_level--;
138
139 odpl = od->pm_lats + od->pm_lat_level;
140
141 if (!ignore_lat &&
142 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
143 break;
144
Kevin Hilmand2292662009-12-08 16:34:23 -0700145 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300146
147 /* XXX check return code */
148 odpl->activate_func(od);
149
Kevin Hilmand2292662009-12-08 16:34:23 -0700150 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300151
Tony Lindgrenf0594292009-10-19 15:25:24 -0700152 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700153 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300154
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700155 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700156 "omap_device: pm_lat %d: activate: elapsed time "
157 "%llu nsec\n", od->pm_lat_level, act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300158
Kevin Hilman9799aca2010-01-26 20:13:02 -0700159 if (act_lat > odpl->activate_lat) {
160 odpl->activate_lat_worst = act_lat;
161 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
162 odpl->activate_lat = act_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700163 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300164 "new worst case activate latency "
165 "%d: %llu\n",
166 od->pm_lat_level, act_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700167 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700168 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700169 "activate latency %d "
170 "higher than exptected. (%llu > %d)\n",
171 od->pm_lat_level, act_lat,
172 odpl->activate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700173 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300174
175 od->dev_wakeup_lat -= odpl->activate_lat;
176 }
177
178 return 0;
179}
180
181/**
182 * _omap_device_deactivate - decrease device readiness
183 * @od: struct omap_device *
184 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
185 *
186 * Decrease readiness of omap_device @od (thus increasing device
187 * wakeup latency, but conserving power). If @ignore_lat is
188 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
189 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
190 * latency is less than the requested maximum wakeup latency, step
191 * forwards in the omap_device_pm_latency table to ensure the device's
192 * maximum wakeup latency is less than or equal to the requested
193 * maximum wakeup latency. Returns 0.
194 */
195static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
196{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700197 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300198
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700199 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300200
201 while (od->pm_lat_level < od->pm_lats_cnt) {
202 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700203 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300204
205 odpl = od->pm_lats + od->pm_lat_level;
206
207 if (!ignore_lat &&
208 ((od->dev_wakeup_lat + odpl->activate_lat) >
209 od->_dev_wakeup_lat_limit))
210 break;
211
Kevin Hilmand2292662009-12-08 16:34:23 -0700212 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300213
214 /* XXX check return code */
215 odpl->deactivate_func(od);
216
Kevin Hilmand2292662009-12-08 16:34:23 -0700217 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300218
Tony Lindgrenf0594292009-10-19 15:25:24 -0700219 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700220 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300221
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700222 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700223 "omap_device: pm_lat %d: deactivate: elapsed time "
224 "%llu nsec\n", od->pm_lat_level, deact_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300225
Kevin Hilman9799aca2010-01-26 20:13:02 -0700226 if (deact_lat > odpl->deactivate_lat) {
227 odpl->deactivate_lat_worst = deact_lat;
228 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
229 odpl->deactivate_lat = deact_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700230 dev_dbg(&od->pdev->dev,
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300231 "new worst case deactivate latency "
232 "%d: %llu\n",
233 od->pm_lat_level, deact_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700234 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700235 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700236 "deactivate latency %d "
237 "higher than exptected. (%llu > %d)\n",
238 od->pm_lat_level, deact_lat,
239 odpl->deactivate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700240 }
241
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300242 od->dev_wakeup_lat += odpl->activate_lat;
243
244 od->pm_lat_level++;
245 }
246
247 return 0;
248}
249
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600250static void _add_clkdev(struct omap_device *od, const char *clk_alias,
251 const char *clk_name)
252{
253 struct clk *r;
254 struct clk_lookup *l;
255
256 if (!clk_alias || !clk_name)
257 return;
258
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700259 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600260
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700261 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600262 if (!IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700263 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700264 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600265 clk_put(r);
266 return;
267 }
268
269 r = omap_clk_get_by_name(clk_name);
270 if (IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700271 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700272 "omap_clk_get_by_name for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600273 return;
274 }
275
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700276 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600277 if (!l) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700278 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700279 "clkdev_alloc for %s failed\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600280 return;
281 }
282
283 clkdev_add(l);
284}
285
Partha Basak4ef7aca2010-09-21 19:23:04 +0200286/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600287 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
288 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +0200289 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600290 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +0200291 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600292 * For the main clock and every optional clock present per hwmod per
293 * omap_device, this function adds an entry in the clkdev table of the
294 * form <dev-id=dev_name, con-id=role> if it does not exist already.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200295 *
296 * The function is called from inside omap_device_build_ss(), after
297 * omap_device_register.
298 *
299 * This allows drivers to get a pointer to its optional clocks based on its role
300 * by calling clk_get(<dev*>, <role>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600301 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200302 *
303 * No return value.
304 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600305static void _add_hwmod_clocks_clkdev(struct omap_device *od,
306 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200307{
308 int i;
309
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600310 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200311
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600312 for (i = 0; i < oh->opt_clks_cnt; i++)
313 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200314}
315
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300316
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200317/**
318 * omap_device_build_from_dt - build an omap_device with multiple hwmods
319 * @pdev_name: name of the platform_device driver to use
320 * @pdev_id: this platform_device's connection ID
321 * @oh: ptr to the single omap_hwmod that backs this omap_device
322 * @pdata: platform_data ptr to associate with the platform_device
323 * @pdata_len: amount of memory pointed to by @pdata
324 * @pm_lats: pointer to a omap_device_pm_latency array for this device
325 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
326 * @is_early_device: should the device be registered as an early device or not
327 *
328 * Function for building an omap_device already registered from device-tree
329 *
330 * Returns 0 or PTR_ERR() on error.
331 */
332static int omap_device_build_from_dt(struct platform_device *pdev)
333{
334 struct omap_hwmod **hwmods;
335 struct omap_device *od;
336 struct omap_hwmod *oh;
337 struct device_node *node = pdev->dev.of_node;
338 const char *oh_name;
339 int oh_cnt, i, ret = 0;
340
341 oh_cnt = of_property_count_strings(node, "ti,hwmods");
342 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
Benoit Cousson5dc06b72012-01-20 18:14:00 +0100343 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200344 return -ENODEV;
345 }
346
347 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
348 if (!hwmods) {
349 ret = -ENOMEM;
350 goto odbfd_exit;
351 }
352
353 for (i = 0; i < oh_cnt; i++) {
354 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
355 oh = omap_hwmod_lookup(oh_name);
356 if (!oh) {
357 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
358 oh_name);
359 ret = -EINVAL;
360 goto odbfd_exit1;
361 }
362 hwmods[i] = oh;
363 }
364
365 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
366 if (!od) {
367 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
368 oh_name);
369 ret = PTR_ERR(od);
370 goto odbfd_exit1;
371 }
372
Peter Ujfalusi3956a1a2012-08-23 16:54:09 +0300373 /* Fix up missing resource names */
374 for (i = 0; i < pdev->num_resources; i++) {
375 struct resource *r = &pdev->resource[i];
376
377 if (r->name == NULL)
378 r->name = dev_name(&pdev->dev);
379 }
380
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200381 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
382 omap_device_disable_idle_on_suspend(pdev);
383
384 pdev->dev.pm_domain = &omap_device_pm_domain;
385
386odbfd_exit1:
387 kfree(hwmods);
388odbfd_exit:
389 return ret;
390}
391
392static int _omap_device_notifier_call(struct notifier_block *nb,
393 unsigned long event, void *dev)
394{
395 struct platform_device *pdev = to_platform_device(dev);
396
397 switch (event) {
398 case BUS_NOTIFY_ADD_DEVICE:
399 if (pdev->dev.of_node)
400 omap_device_build_from_dt(pdev);
401 break;
402
403 case BUS_NOTIFY_DEL_DEVICE:
404 if (pdev->archdata.od)
405 omap_device_delete(pdev->archdata.od);
406 break;
407 }
408
409 return NOTIFY_DONE;
410}
411
412
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300413/* Public functions for use by core code */
414
415/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700416 * omap_device_get_context_loss_count - get lost context count
417 * @od: struct omap_device *
418 *
419 * Using the primary hwmod, query the context loss count for this
420 * device.
421 *
422 * Callers should consider context for this device lost any time this
423 * function returns a value different than the value the caller got
424 * the last time it called this function.
425 *
426 * If any hwmods exist for the omap_device assoiated with @pdev,
427 * return the context loss counter for that hwmod, otherwise return
428 * zero.
429 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +0300430int omap_device_get_context_loss_count(struct platform_device *pdev)
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700431{
432 struct omap_device *od;
433 u32 ret = 0;
434
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600435 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700436
437 if (od->hwmods_cnt)
438 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
439
440 return ret;
441}
442
443/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300444 * omap_device_count_resources - count number of struct resource entries needed
445 * @od: struct omap_device *
446 *
447 * Count the number of struct resource entries needed for this
448 * omap_device @od. Used by omap_device_build_ss() to determine how
449 * much memory to allocate before calling
450 * omap_device_fill_resources(). Returns the count.
451 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700452static int omap_device_count_resources(struct omap_device *od)
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++)
458 c += omap_hwmod_count_resources(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300459
460 pr_debug("omap_device: %s: counted %d total resources across %d "
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700461 "hwmods\n", 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
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200562 res_count = omap_device_count_resources(od);
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530563 /*
564 * DT Boot:
565 * OF framework will construct the resource structure (currently
566 * does for MEM & IRQ resource) and we should respect/use these
567 * resources, killing hwmod dependency.
568 * If pdev->num_resources > 0, we assume that MEM & IRQ resources
569 * have been allocated by OF layer already (through DTB).
570 *
571 * Non-DT Boot:
572 * Here, pdev->num_resources = 0, and we should get all the
573 * resources from hwmod.
574 *
575 * TODO: Once DMA resource is available from OF layer, we should
576 * kill filling any resources from hwmod.
577 */
578 if (res_count > pdev->num_resources) {
579 /* Allocate resources memory to account for new resources */
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200580 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
581 if (!res)
582 goto oda_exit3;
583
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530584 /*
585 * If pdev->num_resources > 0, then assume that,
586 * MEM and IRQ resources will only come from DT and only
587 * fill DMA resource from hwmod layer.
588 */
589 if (pdev->num_resources && pdev->resource) {
590 dev_dbg(&pdev->dev, "%s(): resources already allocated %d\n",
591 __func__, res_count);
592 memcpy(res, pdev->resource,
593 sizeof(struct resource) * pdev->num_resources);
594 _od_fill_dma_resources(od, &res[pdev->num_resources]);
595 } else {
596 dev_dbg(&pdev->dev, "%s(): using resources from hwmod %d\n",
597 __func__, res_count);
598 omap_device_fill_resources(od, res);
599 }
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200600
601 ret = platform_device_add_resources(pdev, res, res_count);
602 kfree(res);
603
604 if (ret)
605 goto oda_exit3;
606 }
607
608 if (!pm_lats) {
609 pm_lats = omap_default_latency;
610 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
611 }
612
613 od->pm_lats_cnt = pm_lats_cnt;
614 od->pm_lats = kmemdup(pm_lats,
615 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
616 GFP_KERNEL);
617 if (!od->pm_lats)
618 goto oda_exit3;
619
620 pdev->archdata.od = od;
621
622 for (i = 0; i < oh_cnt; i++) {
623 hwmods[i]->od = od;
624 _add_hwmod_clocks_clkdev(od, hwmods[i]);
625 }
626
627 return od;
628
629oda_exit3:
630 kfree(hwmods);
631oda_exit2:
632 kfree(od);
633oda_exit1:
634 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
635
636 return ERR_PTR(ret);
637}
638
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800639void omap_device_delete(struct omap_device *od)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200640{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200641 if (!od)
642 return;
643
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200644 od->pdev->archdata.od = NULL;
645 kfree(od->pm_lats);
646 kfree(od->hwmods);
647 kfree(od);
648}
649
650/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300651 * omap_device_build - build and register an omap_device with one omap_hwmod
652 * @pdev_name: name of the platform_device driver to use
653 * @pdev_id: this platform_device's connection ID
654 * @oh: ptr to the single omap_hwmod that backs this omap_device
655 * @pdata: platform_data ptr to associate with the platform_device
656 * @pdata_len: amount of memory pointed to by @pdata
657 * @pm_lats: pointer to a omap_device_pm_latency array for this device
658 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700659 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300660 *
661 * Convenience function for building and registering a single
662 * omap_device record, which in turn builds and registers a
663 * platform_device record. See omap_device_build_ss() for more
664 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
665 * passes along the return value of omap_device_build_ss().
666 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800667struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300668 struct omap_hwmod *oh, void *pdata,
669 int pdata_len,
670 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700671 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300672{
673 struct omap_hwmod *ohs[] = { oh };
674
675 if (!oh)
676 return ERR_PTR(-EINVAL);
677
678 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700679 pdata_len, pm_lats, pm_lats_cnt,
680 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300681}
682
683/**
684 * omap_device_build_ss - build and register an omap_device with multiple hwmods
685 * @pdev_name: name of the platform_device driver to use
686 * @pdev_id: this platform_device's connection ID
687 * @oh: ptr to the single omap_hwmod that backs this omap_device
688 * @pdata: platform_data ptr to associate with the platform_device
689 * @pdata_len: amount of memory pointed to by @pdata
690 * @pm_lats: pointer to a omap_device_pm_latency array for this device
691 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700692 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300693 *
694 * Convenience function for building and registering an omap_device
695 * subsystem record. Subsystem records consist of multiple
696 * omap_hwmods. This function in turn builds and registers a
697 * platform_device record. Returns an ERR_PTR() on error, or passes
698 * along the return value of omap_device_register().
699 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800700struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300701 struct omap_hwmod **ohs, int oh_cnt,
702 void *pdata, int pdata_len,
703 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700704 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300705{
706 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700707 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300708 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300709
710 if (!ohs || oh_cnt == 0 || !pdev_name)
711 return ERR_PTR(-EINVAL);
712
713 if (!pdata && pdata_len > 0)
714 return ERR_PTR(-EINVAL);
715
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700716 pdev = platform_device_alloc(pdev_name, pdev_id);
717 if (!pdev) {
718 ret = -ENOMEM;
719 goto odbs_exit;
720 }
721
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200722 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
723 if (pdev->id != -1)
724 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
725 else
726 dev_set_name(&pdev->dev, "%s", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300727
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200728 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
729 if (!od)
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700730 goto odbs_exit1;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300731
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700732 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000733 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200734 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700735
736 if (is_early_device)
737 ret = omap_early_device_register(pdev);
738 else
739 ret = omap_device_register(pdev);
740 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200741 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600742
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700743 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300744
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700745odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200746 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700747odbs_exit1:
748 platform_device_put(pdev);
749odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300750
751 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
752
753 return ERR_PTR(ret);
754}
755
756/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700757 * omap_early_device_register - register an omap_device as an early platform
758 * device.
759 * @od: struct omap_device * to register
760 *
761 * Register the omap_device structure. This currently just calls
762 * platform_early_add_device() on the underlying platform_device.
763 * Returns 0 by default.
764 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800765static int __init omap_early_device_register(struct platform_device *pdev)
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700766{
767 struct platform_device *devices[1];
768
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700769 devices[0] = pdev;
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700770 early_platform_add_devices(devices, 1);
771 return 0;
772}
773
Kevin Hilman256a5432011-07-12 22:48:03 +0200774#ifdef CONFIG_PM_RUNTIME
Kevin Hilman638080c2011-04-29 00:36:42 +0200775static int _od_runtime_suspend(struct device *dev)
776{
777 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700778 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200779
Kevin Hilman345f79b2011-05-31 16:08:09 -0700780 ret = pm_generic_runtime_suspend(dev);
781
782 if (!ret)
783 omap_device_idle(pdev);
784
785 return ret;
786}
787
788static int _od_runtime_idle(struct device *dev)
789{
790 return pm_generic_runtime_idle(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200791}
792
793static int _od_runtime_resume(struct device *dev)
794{
795 struct platform_device *pdev = to_platform_device(dev);
796
Kevin Hilman345f79b2011-05-31 16:08:09 -0700797 omap_device_enable(pdev);
798
799 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200800}
Kevin Hilman256a5432011-07-12 22:48:03 +0200801#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200802
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200803#ifdef CONFIG_SUSPEND
804static int _od_suspend_noirq(struct device *dev)
805{
806 struct platform_device *pdev = to_platform_device(dev);
807 struct omap_device *od = to_omap_device(pdev);
808 int ret;
809
810 ret = pm_generic_suspend_noirq(dev);
811
812 if (!ret && !pm_runtime_status_suspended(dev)) {
813 if (pm_generic_runtime_suspend(dev) == 0) {
Paul Walmsleyb7c39a32012-03-03 13:15:33 -0700814 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
815 omap_device_idle(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200816 od->flags |= OMAP_DEVICE_SUSPENDED;
817 }
818 }
819
820 return ret;
821}
822
823static int _od_resume_noirq(struct device *dev)
824{
825 struct platform_device *pdev = to_platform_device(dev);
826 struct omap_device *od = to_omap_device(pdev);
827
828 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
829 !pm_runtime_status_suspended(dev)) {
830 od->flags &= ~OMAP_DEVICE_SUSPENDED;
Paul Walmsleyb7c39a32012-03-03 13:15:33 -0700831 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
832 omap_device_enable(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200833 pm_generic_runtime_resume(dev);
834 }
835
836 return pm_generic_resume_noirq(dev);
837}
Kevin Hilman126caf12011-09-01 10:59:36 -0700838#else
839#define _od_suspend_noirq NULL
840#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200841#endif
842
Kevin Hilman3ec2dec2012-02-15 11:47:45 -0800843struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200844 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200845 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
846 _od_runtime_idle)
Kevin Hilman638080c2011-04-29 00:36:42 +0200847 USE_PLATFORM_PM_SLEEP_OPS
Kevin Hilmanff353362011-08-25 15:31:14 +0200848 .suspend_noirq = _od_suspend_noirq,
849 .resume_noirq = _od_resume_noirq,
Kevin Hilman638080c2011-04-29 00:36:42 +0200850 }
851};
852
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700853/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300854 * omap_device_register - register an omap_device with one omap_hwmod
855 * @od: struct omap_device * to register
856 *
857 * Register the omap_device structure. This currently just calls
858 * platform_device_register() on the underlying platform_device.
859 * Returns the return value of platform_device_register().
860 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800861int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300862{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700863 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300864
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700865 pdev->dev.pm_domain = &omap_device_pm_domain;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700866 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300867}
868
869
870/* Public functions for use by device drivers through struct platform_data */
871
872/**
873 * omap_device_enable - fully activate an omap_device
874 * @od: struct omap_device * to activate
875 *
876 * Do whatever is necessary for the hwmods underlying omap_device @od
877 * to be accessible and ready to operate. This generally involves
878 * enabling clocks, setting SYSCONFIG registers; and in the future may
879 * involve remuxing pins. Device drivers should call this function
880 * (through platform_data function pointers) where they would normally
881 * enable clocks, etc. Returns -EINVAL if called when the omap_device
882 * is already enabled, or passes along the return value of
883 * _omap_device_activate().
884 */
885int omap_device_enable(struct platform_device *pdev)
886{
887 int ret;
888 struct omap_device *od;
889
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600890 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300891
892 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700893 dev_warn(&pdev->dev,
894 "omap_device: %s() called from invalid state %d\n",
895 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300896 return -EINVAL;
897 }
898
899 /* Enable everything if we're enabling this device from scratch */
900 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
901 od->pm_lat_level = od->pm_lats_cnt;
902
903 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
904
905 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700906 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300907 od->_state = OMAP_DEVICE_STATE_ENABLED;
908
909 return ret;
910}
911
912/**
913 * omap_device_idle - idle an omap_device
914 * @od: struct omap_device * to idle
915 *
916 * Idle omap_device @od by calling as many .deactivate_func() entries
917 * in the omap_device's pm_lats table as is possible without exceeding
918 * the device's maximum wakeup latency limit, pm_lat_limit. Device
919 * drivers should call this function (through platform_data function
920 * pointers) where they would normally disable clocks after operations
921 * complete, etc.. Returns -EINVAL if the omap_device is not
922 * currently enabled, or passes along the return value of
923 * _omap_device_deactivate().
924 */
925int omap_device_idle(struct platform_device *pdev)
926{
927 int ret;
928 struct omap_device *od;
929
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600930 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300931
932 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700933 dev_warn(&pdev->dev,
934 "omap_device: %s() called from invalid state %d\n",
935 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300936 return -EINVAL;
937 }
938
939 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
940
941 od->_state = OMAP_DEVICE_STATE_IDLE;
942
943 return ret;
944}
945
946/**
947 * omap_device_shutdown - shut down an omap_device
948 * @od: struct omap_device * to shut down
949 *
950 * Shut down omap_device @od by calling all .deactivate_func() entries
951 * in the omap_device's pm_lats table and then shutting down all of
952 * the underlying omap_hwmods. Used when a device is being "removed"
953 * or a device driver is being unloaded. Returns -EINVAL if the
954 * omap_device is not currently enabled or idle, or passes along the
955 * return value of _omap_device_deactivate().
956 */
957int omap_device_shutdown(struct platform_device *pdev)
958{
959 int ret, i;
960 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300961
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600962 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300963
964 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
965 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700966 dev_warn(&pdev->dev,
967 "omap_device: %s() called from invalid state %d\n",
968 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300969 return -EINVAL;
970 }
971
972 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
973
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600974 for (i = 0; i < od->hwmods_cnt; i++)
975 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300976
977 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
978
979 return ret;
980}
981
982/**
983 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
984 * @od: struct omap_device *
985 *
986 * When a device's maximum wakeup latency limit changes, call some of
987 * the .activate_func or .deactivate_func function pointers in the
988 * omap_device's pm_lats array to ensure that the device's maximum
989 * wakeup latency is less than or equal to the new latency limit.
990 * Intended to be called by OMAP PM code whenever a device's maximum
991 * wakeup latency limit changes (e.g., via
992 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
993 * done (e.g., if the omap_device is not currently idle, or if the
994 * wakeup latency is already current with the new limit) or passes
995 * along the return value of _omap_device_deactivate() or
996 * _omap_device_activate().
997 */
998int omap_device_align_pm_lat(struct platform_device *pdev,
999 u32 new_wakeup_lat_limit)
1000{
1001 int ret = -EINVAL;
1002 struct omap_device *od;
1003
Kevin Hilman8f0d69d2011-07-09 19:15:20 -06001004 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001005
1006 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
1007 return 0;
1008
1009 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
1010
1011 if (od->_state != OMAP_DEVICE_STATE_IDLE)
1012 return 0;
1013 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
1014 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
1015 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
1016 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
1017
1018 return ret;
1019}
1020
1021/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001022 * omap_device_get_pwrdm - return the powerdomain * associated with @od
1023 * @od: struct omap_device *
1024 *
1025 * Return the powerdomain associated with the first underlying
1026 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
1027 * code. Returns NULL on error or a struct powerdomain * upon
1028 * success.
1029 */
1030struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
1031{
1032 /*
1033 * XXX Assumes that all omap_hwmod powerdomains are identical.
1034 * This may not necessarily be true. There should be a sanity
1035 * check in here to WARN() if any difference appears.
1036 */
1037 if (!od->hwmods_cnt)
1038 return NULL;
1039
1040 return omap_hwmod_get_pwrdm(od->hwmods[0]);
1041}
1042
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06001043/**
1044 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
1045 * @od: struct omap_device *
1046 *
1047 * Return the MPU's virtual address for the base of the hwmod, from
1048 * the ioremap() that the hwmod code does. Only valid if there is one
1049 * hwmod associated with this device. Returns NULL if there are zero
1050 * or more than one hwmods associated with this omap_device;
1051 * otherwise, passes along the return value from
1052 * omap_hwmod_get_mpu_rt_va().
1053 */
1054void __iomem *omap_device_get_rt_va(struct omap_device *od)
1055{
1056 if (od->hwmods_cnt != 1)
1057 return NULL;
1058
1059 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1060}
1061
Nishanth Menon1f8a7d52011-07-27 15:02:32 -05001062/**
1063 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1064 * device pointer.
1065 * @oh_name: name of the hwmod device
1066 *
1067 * Returns back a struct device * pointer associated with a hwmod
1068 * device represented by a hwmod_name
1069 */
1070struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1071{
1072 struct omap_hwmod *oh;
1073
1074 if (!oh_name) {
1075 WARN(1, "%s: no hwmod name!\n", __func__);
1076 return ERR_PTR(-EINVAL);
1077 }
1078
1079 oh = omap_hwmod_lookup(oh_name);
1080 if (IS_ERR_OR_NULL(oh)) {
1081 WARN(1, "%s: no hwmod for %s\n", __func__,
1082 oh_name);
1083 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1084 }
1085 if (IS_ERR_OR_NULL(oh->od)) {
1086 WARN(1, "%s: no omap_device for %s\n", __func__,
1087 oh_name);
1088 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1089 }
1090
1091 if (IS_ERR_OR_NULL(oh->od->pdev))
1092 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1093
1094 return &oh->od->pdev->dev;
1095}
1096EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1097
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001098/*
1099 * Public functions intended for use in omap_device_pm_latency
1100 * .activate_func and .deactivate_func function pointers
1101 */
1102
1103/**
1104 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1105 * @od: struct omap_device *od
1106 *
1107 * Enable all underlying hwmods. Returns 0.
1108 */
1109int omap_device_enable_hwmods(struct omap_device *od)
1110{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001111 int i;
1112
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001113 for (i = 0; i < od->hwmods_cnt; i++)
1114 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001115
1116 /* XXX pass along return value here? */
1117 return 0;
1118}
1119
1120/**
1121 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1122 * @od: struct omap_device *od
1123 *
1124 * Idle all underlying hwmods. Returns 0.
1125 */
1126int omap_device_idle_hwmods(struct omap_device *od)
1127{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001128 int i;
1129
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001130 for (i = 0; i < od->hwmods_cnt; i++)
1131 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001132
1133 /* XXX pass along return value here? */
1134 return 0;
1135}
1136
1137/**
1138 * omap_device_disable_clocks - disable all main and interface clocks
1139 * @od: struct omap_device *od
1140 *
1141 * Disable the main functional clock and interface clock for all of the
1142 * omap_hwmods associated with the omap_device. Returns 0.
1143 */
1144int omap_device_disable_clocks(struct omap_device *od)
1145{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001146 int i;
1147
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001148 for (i = 0; i < od->hwmods_cnt; i++)
1149 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001150
1151 /* XXX pass along return value here? */
1152 return 0;
1153}
1154
1155/**
1156 * omap_device_enable_clocks - enable all main and interface clocks
1157 * @od: struct omap_device *od
1158 *
1159 * Enable the main functional clock and interface clock for all of the
1160 * omap_hwmods associated with the omap_device. Returns 0.
1161 */
1162int omap_device_enable_clocks(struct omap_device *od)
1163{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001164 int i;
1165
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001166 for (i = 0; i < od->hwmods_cnt; i++)
1167 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001168
1169 /* XXX pass along return value here? */
1170 return 0;
1171}
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001172
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001173static struct notifier_block platform_nb = {
1174 .notifier_call = _omap_device_notifier_call,
1175};
1176
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001177static int __init omap_device_init(void)
1178{
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001179 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman3ec2dec2012-02-15 11:47:45 -08001180 return 0;
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001181}
1182core_initcall(omap_device_init);