blob: d109f06785da28c93933f953c8e40cdf035771e3 [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 Lindgrenb76c8b12013-01-11 11:24:18 -080092#include "soc.h"
Tony Lindgren25c7d492012-10-02 17:25:48 -070093#include "omap_device.h"
Tony Lindgren2a296c82012-10-02 17:41:35 -070094#include "omap_hwmod.h"
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030095
96/* These parameters are passed to _omap_device_{de,}activate() */
97#define USE_WAKEUP_LAT 0
98#define IGNORE_WAKEUP_LAT 1
99
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700100static int omap_early_device_register(struct platform_device *pdev);
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700101
Benoit Coussonb7b5bc92011-08-09 16:54:19 +0200102static struct omap_device_pm_latency omap_default_latency[] = {
103 {
104 .deactivate_func = omap_device_idle_hwmods,
105 .activate_func = omap_device_enable_hwmods,
106 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
107 }
108};
109
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300110/* Private functions */
111
112/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300113 * _omap_device_activate - increase device readiness
114 * @od: struct omap_device *
115 * @ignore_lat: increase to latency target (0) or full readiness (1)?
116 *
117 * Increase readiness of omap_device @od (thus decreasing device
118 * wakeup latency, but consuming more power). If @ignore_lat is
119 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
120 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
121 * latency is greater than the requested maximum wakeup latency, step
122 * backwards in the omap_device_pm_latency table to ensure the
123 * device's maximum wakeup latency is less than or equal to the
124 * requested maximum wakeup latency. Returns 0.
125 */
126static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
127{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700128 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300129
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700130 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300131
132 while (od->pm_lat_level > 0) {
133 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700134 unsigned long long act_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300135
136 od->pm_lat_level--;
137
138 odpl = od->pm_lats + od->pm_lat_level;
139
140 if (!ignore_lat &&
141 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
142 break;
143
Kevin Hilmand2292662009-12-08 16:34:23 -0700144 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300145
146 /* XXX check return code */
147 odpl->activate_func(od);
148
Kevin Hilmand2292662009-12-08 16:34:23 -0700149 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300150
Tony Lindgrenf0594292009-10-19 15:25:24 -0700151 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700152 act_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300153
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700154 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600155 "omap_device: pm_lat %d: activate: elapsed time %llu nsec\n",
156 od->pm_lat_level, act_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300157
Kevin Hilman9799aca2010-01-26 20:13:02 -0700158 if (act_lat > odpl->activate_lat) {
159 odpl->activate_lat_worst = act_lat;
160 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
161 odpl->activate_lat = act_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700162 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600163 "new worst case activate latency %d: %llu\n",
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300164 od->pm_lat_level, act_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700165 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700166 dev_warn(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600167 "activate latency %d higher than expected. (%llu > %d)\n",
Kevin Hilman49882c92011-07-21 09:58:36 -0700168 od->pm_lat_level, act_lat,
169 odpl->activate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700170 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300171
172 od->dev_wakeup_lat -= odpl->activate_lat;
173 }
174
175 return 0;
176}
177
178/**
179 * _omap_device_deactivate - decrease device readiness
180 * @od: struct omap_device *
181 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
182 *
183 * Decrease readiness of omap_device @od (thus increasing device
184 * wakeup latency, but conserving power). If @ignore_lat is
185 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
186 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
187 * latency is less than the requested maximum wakeup latency, step
188 * forwards in the omap_device_pm_latency table to ensure the device's
189 * maximum wakeup latency is less than or equal to the requested
190 * maximum wakeup latency. Returns 0.
191 */
192static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
193{
Tony Lindgrenf0594292009-10-19 15:25:24 -0700194 struct timespec a, b, c;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300195
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700196 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300197
198 while (od->pm_lat_level < od->pm_lats_cnt) {
199 struct omap_device_pm_latency *odpl;
Tony Lindgrenf0594292009-10-19 15:25:24 -0700200 unsigned long long deact_lat = 0;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300201
202 odpl = od->pm_lats + od->pm_lat_level;
203
204 if (!ignore_lat &&
205 ((od->dev_wakeup_lat + odpl->activate_lat) >
206 od->_dev_wakeup_lat_limit))
207 break;
208
Kevin Hilmand2292662009-12-08 16:34:23 -0700209 read_persistent_clock(&a);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300210
211 /* XXX check return code */
212 odpl->deactivate_func(od);
213
Kevin Hilmand2292662009-12-08 16:34:23 -0700214 read_persistent_clock(&b);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300215
Tony Lindgrenf0594292009-10-19 15:25:24 -0700216 c = timespec_sub(b, a);
Kevin Hilman0d93d8b2009-12-08 16:34:26 -0700217 deact_lat = timespec_to_ns(&c);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300218
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700219 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600220 "omap_device: pm_lat %d: deactivate: elapsed time %llu nsec\n",
221 od->pm_lat_level, deact_lat);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300222
Kevin Hilman9799aca2010-01-26 20:13:02 -0700223 if (deact_lat > odpl->deactivate_lat) {
224 odpl->deactivate_lat_worst = deact_lat;
225 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
226 odpl->deactivate_lat = deact_lat;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700227 dev_dbg(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600228 "new worst case deactivate latency %d: %llu\n",
Grazvydas Ignotas47c3e5d2011-07-25 16:18:24 +0300229 od->pm_lat_level, deact_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700230 } else
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700231 dev_warn(&od->pdev->dev,
Paul Walmsley7852ec02012-07-26 00:54:26 -0600232 "deactivate latency %d higher than expected. (%llu > %d)\n",
Kevin Hilman49882c92011-07-21 09:58:36 -0700233 od->pm_lat_level, deact_lat,
234 odpl->deactivate_lat);
Kevin Hilman9799aca2010-01-26 20:13:02 -0700235 }
236
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300237 od->dev_wakeup_lat += odpl->activate_lat;
238
239 od->pm_lat_level++;
240 }
241
242 return 0;
243}
244
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600245static void _add_clkdev(struct omap_device *od, const char *clk_alias,
246 const char *clk_name)
247{
248 struct clk *r;
249 struct clk_lookup *l;
250
251 if (!clk_alias || !clk_name)
252 return;
253
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700254 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600255
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700256 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600257 if (!IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700258 dev_warn(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700259 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600260 clk_put(r);
261 return;
262 }
263
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600264 r = clk_get(NULL, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600265 if (IS_ERR(r)) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700266 dev_err(&od->pdev->dev,
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600267 "clk_get for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600268 return;
269 }
270
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700271 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600272 if (!l) {
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700273 dev_err(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -0700274 "clkdev_alloc for %s failed\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600275 return;
276 }
277
278 clkdev_add(l);
279}
280
Partha Basak4ef7aca2010-09-21 19:23:04 +0200281/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600282 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
283 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +0200284 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600285 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +0200286 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600287 * For the main clock and every optional clock present per hwmod per
288 * omap_device, this function adds an entry in the clkdev table of the
289 * form <dev-id=dev_name, con-id=role> if it does not exist already.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200290 *
291 * The function is called from inside omap_device_build_ss(), after
292 * omap_device_register.
293 *
294 * This allows drivers to get a pointer to its optional clocks based on its role
295 * by calling clk_get(<dev*>, <role>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600296 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200297 *
298 * No return value.
299 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600300static void _add_hwmod_clocks_clkdev(struct omap_device *od,
301 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200302{
303 int i;
304
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600305 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200306
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600307 for (i = 0; i < oh->opt_clks_cnt; i++)
308 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200309}
310
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300311
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200312/**
313 * omap_device_build_from_dt - build an omap_device with multiple hwmods
314 * @pdev_name: name of the platform_device driver to use
315 * @pdev_id: this platform_device's connection ID
316 * @oh: ptr to the single omap_hwmod that backs this omap_device
317 * @pdata: platform_data ptr to associate with the platform_device
318 * @pdata_len: amount of memory pointed to by @pdata
319 * @pm_lats: pointer to a omap_device_pm_latency array for this device
320 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
321 * @is_early_device: should the device be registered as an early device or not
322 *
323 * Function for building an omap_device already registered from device-tree
324 *
325 * Returns 0 or PTR_ERR() on error.
326 */
327static int omap_device_build_from_dt(struct platform_device *pdev)
328{
329 struct omap_hwmod **hwmods;
330 struct omap_device *od;
331 struct omap_hwmod *oh;
332 struct device_node *node = pdev->dev.of_node;
333 const char *oh_name;
334 int oh_cnt, i, ret = 0;
335
336 oh_cnt = of_property_count_strings(node, "ti,hwmods");
337 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
Benoit Cousson5dc06b72012-01-20 18:14:00 +0100338 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200339 return -ENODEV;
340 }
341
342 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
343 if (!hwmods) {
344 ret = -ENOMEM;
345 goto odbfd_exit;
346 }
347
348 for (i = 0; i < oh_cnt; i++) {
349 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
350 oh = omap_hwmod_lookup(oh_name);
351 if (!oh) {
352 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
353 oh_name);
354 ret = -EINVAL;
355 goto odbfd_exit1;
356 }
357 hwmods[i] = oh;
358 }
359
360 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
361 if (!od) {
362 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
363 oh_name);
364 ret = PTR_ERR(od);
365 goto odbfd_exit1;
366 }
367
Peter Ujfalusi3956a1a2012-08-23 16:54:09 +0300368 /* Fix up missing resource names */
369 for (i = 0; i < pdev->num_resources; i++) {
370 struct resource *r = &pdev->resource[i];
371
372 if (r->name == NULL)
373 r->name = dev_name(&pdev->dev);
374 }
375
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200376 if (of_get_property(node, "ti,no_idle_on_suspend", NULL))
377 omap_device_disable_idle_on_suspend(pdev);
378
379 pdev->dev.pm_domain = &omap_device_pm_domain;
380
381odbfd_exit1:
382 kfree(hwmods);
383odbfd_exit:
384 return ret;
385}
386
387static int _omap_device_notifier_call(struct notifier_block *nb,
388 unsigned long event, void *dev)
389{
390 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilmane7533452012-07-10 11:13:16 -0700391 struct omap_device *od;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200392
393 switch (event) {
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200394 case BUS_NOTIFY_DEL_DEVICE:
395 if (pdev->archdata.od)
396 omap_device_delete(pdev->archdata.od);
397 break;
Kevin Hilmane7533452012-07-10 11:13:16 -0700398 case BUS_NOTIFY_ADD_DEVICE:
399 if (pdev->dev.of_node)
400 omap_device_build_from_dt(pdev);
401 /* fall through */
402 default:
403 od = to_omap_device(pdev);
404 if (od)
405 od->_driver_status = event;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200406 }
407
408 return NOTIFY_DONE;
409}
410
411
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300412/* Public functions for use by core code */
413
414/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700415 * omap_device_get_context_loss_count - get lost context count
416 * @od: struct omap_device *
417 *
418 * Using the primary hwmod, query the context loss count for this
419 * device.
420 *
421 * Callers should consider context for this device lost any time this
422 * function returns a value different than the value the caller got
423 * the last time it called this function.
424 *
425 * If any hwmods exist for the omap_device assoiated with @pdev,
426 * return the context loss counter for that hwmod, otherwise return
427 * zero.
428 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +0300429int omap_device_get_context_loss_count(struct platform_device *pdev)
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700430{
431 struct omap_device *od;
432 u32 ret = 0;
433
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600434 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700435
436 if (od->hwmods_cnt)
437 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
438
439 return ret;
440}
441
442/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300443 * omap_device_count_resources - count number of struct resource entries needed
444 * @od: struct omap_device *
Peter Ujfalusidad41912012-11-21 16:15:17 -0700445 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300446 *
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 */
Peter Ujfalusidad41912012-11-21 16:15:17 -0700452static int omap_device_count_resources(struct omap_device *od,
453 unsigned long flags)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300454{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300455 int c = 0;
456 int i;
457
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600458 for (i = 0; i < od->hwmods_cnt; i++)
Peter Ujfalusidad41912012-11-21 16:15:17 -0700459 c += omap_hwmod_count_resources(od->hwmods[i], flags);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300460
Paul Walmsley7852ec02012-07-26 00:54:26 -0600461 pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
462 od->pdev->name, c, od->hwmods_cnt);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300463
464 return c;
465}
466
467/**
468 * omap_device_fill_resources - fill in array of struct resource
469 * @od: struct omap_device *
470 * @res: pointer to an array of struct resource to be filled in
471 *
472 * Populate one or more empty struct resource pointed to by @res with
473 * the resource data for this omap_device @od. Used by
474 * omap_device_build_ss() after calling omap_device_count_resources().
475 * Ideally this function would not be needed at all. If omap_device
476 * replaces platform_device, then we can specify our own
477 * get_resource()/ get_irq()/etc functions that use the underlying
478 * omap_hwmod information. Or if platform_device is extended to use
479 * subarchitecture-specific function pointers, the various
480 * platform_device functions can simply call omap_device internal
481 * functions to get device resources. Hacking around the existing
482 * platform_device code wastes memory. Returns 0.
483 */
Kevin Hilmana2a28ad2011-07-21 14:14:35 -0700484static int omap_device_fill_resources(struct omap_device *od,
485 struct resource *res)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300486{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300487 int i, r;
488
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -0600489 for (i = 0; i < od->hwmods_cnt; i++) {
490 r = omap_hwmod_fill_resources(od->hwmods[i], res);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300491 res += r;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300492 }
493
494 return 0;
495}
496
497/**
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530498 * _od_fill_dma_resources - fill in array of struct resource with dma resources
499 * @od: struct omap_device *
500 * @res: pointer to an array of struct resource to be filled in
501 *
502 * Populate one or more empty struct resource pointed to by @res with
503 * the dma resource data for this omap_device @od. Used by
504 * omap_device_alloc() after calling omap_device_count_resources().
505 *
506 * Ideally this function would not be needed at all. If we have
507 * mechanism to get dma resources from DT.
508 *
509 * Returns 0.
510 */
511static int _od_fill_dma_resources(struct omap_device *od,
512 struct resource *res)
513{
514 int i, r;
515
516 for (i = 0; i < od->hwmods_cnt; i++) {
517 r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
518 res += r;
519 }
520
521 return 0;
522}
523
524/**
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200525 * omap_device_alloc - allocate an omap_device
526 * @pdev: platform_device that will be included in this omap_device
527 * @oh: ptr to the single omap_hwmod that backs this omap_device
528 * @pdata: platform_data ptr to associate with the platform_device
529 * @pdata_len: amount of memory pointed to by @pdata
530 * @pm_lats: pointer to a omap_device_pm_latency array for this device
531 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
532 *
533 * Convenience function for allocating an omap_device structure and filling
534 * hwmods, resources and pm_latency attributes.
535 *
536 * Returns an struct omap_device pointer or ERR_PTR() on error;
537 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800538struct omap_device *omap_device_alloc(struct platform_device *pdev,
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200539 struct omap_hwmod **ohs, int oh_cnt,
540 struct omap_device_pm_latency *pm_lats,
541 int pm_lats_cnt)
542{
543 int ret = -ENOMEM;
544 struct omap_device *od;
545 struct resource *res = NULL;
546 int i, res_count;
547 struct omap_hwmod **hwmods;
548
549 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
550 if (!od) {
551 ret = -ENOMEM;
552 goto oda_exit1;
553 }
554 od->hwmods_cnt = oh_cnt;
555
556 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
557 if (!hwmods)
558 goto oda_exit2;
559
560 od->hwmods = hwmods;
561 od->pdev = pdev;
562
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530563 /*
Peter Ujfalusic567b052012-11-21 16:15:18 -0700564 * Non-DT Boot:
565 * Here, pdev->num_resources = 0, and we should get all the
566 * resources from hwmod.
567 *
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530568 * DT Boot:
569 * OF framework will construct the resource structure (currently
570 * does for MEM & IRQ resource) and we should respect/use these
571 * resources, killing hwmod dependency.
572 * If pdev->num_resources > 0, we assume that MEM & IRQ resources
573 * have been allocated by OF layer already (through DTB).
Peter Ujfalusic567b052012-11-21 16:15:18 -0700574 * As preparation for the future we examine the OF provided resources
575 * to see if we have DMA resources provided already. In this case
576 * there is no need to update the resources for the device, we use the
577 * OF provided ones.
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530578 *
579 * TODO: Once DMA resource is available from OF layer, we should
580 * kill filling any resources from hwmod.
581 */
Peter Ujfalusic567b052012-11-21 16:15:18 -0700582 if (!pdev->num_resources) {
583 /* Count all resources for the device */
584 res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
585 IORESOURCE_DMA |
586 IORESOURCE_MEM);
587 } else {
588 /* Take a look if we already have DMA resource via DT */
589 for (i = 0; i < pdev->num_resources; i++) {
590 struct resource *r = &pdev->resource[i];
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200591
Peter Ujfalusic567b052012-11-21 16:15:18 -0700592 /* We have it, no need to touch the resources */
593 if (r->flags == IORESOURCE_DMA)
594 goto have_everything;
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +0530595 }
Peter Ujfalusic567b052012-11-21 16:15:18 -0700596 /* Count only DMA resources for the device */
597 res_count = omap_device_count_resources(od, IORESOURCE_DMA);
598 /* The device has no DMA resource, no need for update */
599 if (!res_count)
600 goto have_everything;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200601
Peter Ujfalusic567b052012-11-21 16:15:18 -0700602 res_count += pdev->num_resources;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200603 }
604
Peter Ujfalusic567b052012-11-21 16:15:18 -0700605 /* Allocate resources memory to account for new resources */
606 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
607 if (!res)
608 goto oda_exit3;
609
610 if (!pdev->num_resources) {
611 dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
612 __func__, res_count);
613 omap_device_fill_resources(od, res);
614 } else {
615 dev_dbg(&pdev->dev,
616 "%s: appending %d DMA resources from hwmod\n",
617 __func__, res_count - pdev->num_resources);
618 memcpy(res, pdev->resource,
619 sizeof(struct resource) * pdev->num_resources);
620 _od_fill_dma_resources(od, &res[pdev->num_resources]);
621 }
622
623 ret = platform_device_add_resources(pdev, res, res_count);
624 kfree(res);
625
626 if (ret)
627 goto oda_exit3;
628
629have_everything:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200630 if (!pm_lats) {
631 pm_lats = omap_default_latency;
632 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
633 }
634
635 od->pm_lats_cnt = pm_lats_cnt;
636 od->pm_lats = kmemdup(pm_lats,
637 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
638 GFP_KERNEL);
639 if (!od->pm_lats)
640 goto oda_exit3;
641
642 pdev->archdata.od = od;
643
644 for (i = 0; i < oh_cnt; i++) {
645 hwmods[i]->od = od;
646 _add_hwmod_clocks_clkdev(od, hwmods[i]);
647 }
648
649 return od;
650
651oda_exit3:
652 kfree(hwmods);
653oda_exit2:
654 kfree(od);
655oda_exit1:
656 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
657
658 return ERR_PTR(ret);
659}
660
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800661void omap_device_delete(struct omap_device *od)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200662{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200663 if (!od)
664 return;
665
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200666 od->pdev->archdata.od = NULL;
667 kfree(od->pm_lats);
668 kfree(od->hwmods);
669 kfree(od);
670}
671
672/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300673 * omap_device_build - build and register an omap_device with one omap_hwmod
674 * @pdev_name: name of the platform_device driver to use
675 * @pdev_id: this platform_device's connection ID
676 * @oh: ptr to the single omap_hwmod that backs this omap_device
677 * @pdata: platform_data ptr to associate with the platform_device
678 * @pdata_len: amount of memory pointed to by @pdata
679 * @pm_lats: pointer to a omap_device_pm_latency array for this device
680 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700681 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300682 *
683 * Convenience function for building and registering a single
684 * omap_device record, which in turn builds and registers a
685 * platform_device record. See omap_device_build_ss() for more
686 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
687 * passes along the return value of omap_device_build_ss().
688 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800689struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300690 struct omap_hwmod *oh, void *pdata,
691 int pdata_len,
692 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700693 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300694{
695 struct omap_hwmod *ohs[] = { oh };
696
697 if (!oh)
698 return ERR_PTR(-EINVAL);
699
700 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700701 pdata_len, pm_lats, pm_lats_cnt,
702 is_early_device);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300703}
704
705/**
706 * omap_device_build_ss - build and register an omap_device with multiple hwmods
707 * @pdev_name: name of the platform_device driver to use
708 * @pdev_id: this platform_device's connection ID
709 * @oh: ptr to the single omap_hwmod that backs this omap_device
710 * @pdata: platform_data ptr to associate with the platform_device
711 * @pdata_len: amount of memory pointed to by @pdata
712 * @pm_lats: pointer to a omap_device_pm_latency array for this device
713 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700714 * @is_early_device: should the device be registered as an early device or not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300715 *
716 * Convenience function for building and registering an omap_device
717 * subsystem record. Subsystem records consist of multiple
718 * omap_hwmods. This function in turn builds and registers a
719 * platform_device record. Returns an ERR_PTR() on error, or passes
720 * along the return value of omap_device_register().
721 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800722struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300723 struct omap_hwmod **ohs, int oh_cnt,
724 void *pdata, int pdata_len,
725 struct omap_device_pm_latency *pm_lats,
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700726 int pm_lats_cnt, int is_early_device)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300727{
728 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700729 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300730 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300731
732 if (!ohs || oh_cnt == 0 || !pdev_name)
733 return ERR_PTR(-EINVAL);
734
735 if (!pdata && pdata_len > 0)
736 return ERR_PTR(-EINVAL);
737
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700738 pdev = platform_device_alloc(pdev_name, pdev_id);
739 if (!pdev) {
740 ret = -ENOMEM;
741 goto odbs_exit;
742 }
743
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200744 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
745 if (pdev->id != -1)
746 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
747 else
748 dev_set_name(&pdev->dev, "%s", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300749
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200750 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
Wei Yongjuna87e6622012-09-21 14:32:04 +0800751 if (IS_ERR(od))
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700752 goto odbs_exit1;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300753
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700754 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000755 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200756 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700757
758 if (is_early_device)
759 ret = omap_early_device_register(pdev);
760 else
761 ret = omap_device_register(pdev);
762 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200763 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600764
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700765 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300766
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700767odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200768 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700769odbs_exit1:
770 platform_device_put(pdev);
771odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300772
773 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
774
775 return ERR_PTR(ret);
776}
777
778/**
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700779 * omap_early_device_register - register an omap_device as an early platform
780 * device.
781 * @od: struct omap_device * to register
782 *
783 * Register the omap_device structure. This currently just calls
784 * platform_early_add_device() on the underlying platform_device.
785 * Returns 0 by default.
786 */
Kevin Hilman9cf793f2012-02-20 09:43:30 -0800787static int __init omap_early_device_register(struct platform_device *pdev)
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700788{
789 struct platform_device *devices[1];
790
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700791 devices[0] = pdev;
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700792 early_platform_add_devices(devices, 1);
793 return 0;
794}
795
Kevin Hilman256a5432011-07-12 22:48:03 +0200796#ifdef CONFIG_PM_RUNTIME
Kevin Hilman638080c2011-04-29 00:36:42 +0200797static int _od_runtime_suspend(struct device *dev)
798{
799 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700800 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200801
Kevin Hilman345f79b2011-05-31 16:08:09 -0700802 ret = pm_generic_runtime_suspend(dev);
803
804 if (!ret)
805 omap_device_idle(pdev);
806
807 return ret;
808}
809
810static int _od_runtime_idle(struct device *dev)
811{
812 return pm_generic_runtime_idle(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200813}
814
815static int _od_runtime_resume(struct device *dev)
816{
817 struct platform_device *pdev = to_platform_device(dev);
818
Kevin Hilman345f79b2011-05-31 16:08:09 -0700819 omap_device_enable(pdev);
820
821 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200822}
Kevin Hilman256a5432011-07-12 22:48:03 +0200823#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200824
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200825#ifdef CONFIG_SUSPEND
826static int _od_suspend_noirq(struct device *dev)
827{
828 struct platform_device *pdev = to_platform_device(dev);
829 struct omap_device *od = to_omap_device(pdev);
830 int ret;
831
Kevin Hilman72bb6f92012-07-10 15:29:04 -0700832 /* Don't attempt late suspend on a driver that is not bound */
833 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
834 return 0;
835
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200836 ret = pm_generic_suspend_noirq(dev);
837
838 if (!ret && !pm_runtime_status_suspended(dev)) {
839 if (pm_generic_runtime_suspend(dev) == 0) {
Paul Walmsleyb7c39a32012-03-03 13:15:33 -0700840 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
841 omap_device_idle(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200842 od->flags |= OMAP_DEVICE_SUSPENDED;
843 }
844 }
845
846 return ret;
847}
848
849static int _od_resume_noirq(struct device *dev)
850{
851 struct platform_device *pdev = to_platform_device(dev);
852 struct omap_device *od = to_omap_device(pdev);
853
854 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
855 !pm_runtime_status_suspended(dev)) {
856 od->flags &= ~OMAP_DEVICE_SUSPENDED;
Paul Walmsleyb7c39a32012-03-03 13:15:33 -0700857 if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND))
858 omap_device_enable(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200859 pm_generic_runtime_resume(dev);
860 }
861
862 return pm_generic_resume_noirq(dev);
863}
Kevin Hilman126caf12011-09-01 10:59:36 -0700864#else
865#define _od_suspend_noirq NULL
866#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200867#endif
868
Kevin Hilman3ec2dec2012-02-15 11:47:45 -0800869struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200870 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200871 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
872 _od_runtime_idle)
Kevin Hilman638080c2011-04-29 00:36:42 +0200873 USE_PLATFORM_PM_SLEEP_OPS
Kevin Hilmanff353362011-08-25 15:31:14 +0200874 .suspend_noirq = _od_suspend_noirq,
875 .resume_noirq = _od_resume_noirq,
Kevin Hilman638080c2011-04-29 00:36:42 +0200876 }
877};
878
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700879/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300880 * omap_device_register - register an omap_device with one omap_hwmod
881 * @od: struct omap_device * to register
882 *
883 * Register the omap_device structure. This currently just calls
884 * platform_device_register() on the underlying platform_device.
885 * Returns the return value of platform_device_register().
886 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800887int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300888{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700889 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300890
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700891 pdev->dev.pm_domain = &omap_device_pm_domain;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700892 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300893}
894
895
896/* Public functions for use by device drivers through struct platform_data */
897
898/**
899 * omap_device_enable - fully activate an omap_device
900 * @od: struct omap_device * to activate
901 *
902 * Do whatever is necessary for the hwmods underlying omap_device @od
903 * to be accessible and ready to operate. This generally involves
904 * enabling clocks, setting SYSCONFIG registers; and in the future may
905 * involve remuxing pins. Device drivers should call this function
906 * (through platform_data function pointers) where they would normally
907 * enable clocks, etc. Returns -EINVAL if called when the omap_device
908 * is already enabled, or passes along the return value of
909 * _omap_device_activate().
910 */
911int omap_device_enable(struct platform_device *pdev)
912{
913 int ret;
914 struct omap_device *od;
915
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600916 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300917
918 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700919 dev_warn(&pdev->dev,
920 "omap_device: %s() called from invalid state %d\n",
921 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300922 return -EINVAL;
923 }
924
925 /* Enable everything if we're enabling this device from scratch */
926 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
927 od->pm_lat_level = od->pm_lats_cnt;
928
929 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
930
931 od->dev_wakeup_lat = 0;
Kevin Hilman5f1b6ef2009-12-08 16:34:22 -0700932 od->_dev_wakeup_lat_limit = UINT_MAX;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300933 od->_state = OMAP_DEVICE_STATE_ENABLED;
934
935 return ret;
936}
937
938/**
939 * omap_device_idle - idle an omap_device
940 * @od: struct omap_device * to idle
941 *
942 * Idle omap_device @od by calling as many .deactivate_func() entries
943 * in the omap_device's pm_lats table as is possible without exceeding
944 * the device's maximum wakeup latency limit, pm_lat_limit. Device
945 * drivers should call this function (through platform_data function
946 * pointers) where they would normally disable clocks after operations
947 * complete, etc.. Returns -EINVAL if the omap_device is not
948 * currently enabled, or passes along the return value of
949 * _omap_device_deactivate().
950 */
951int omap_device_idle(struct platform_device *pdev)
952{
953 int ret;
954 struct omap_device *od;
955
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600956 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300957
958 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700959 dev_warn(&pdev->dev,
960 "omap_device: %s() called from invalid state %d\n",
961 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300962 return -EINVAL;
963 }
964
965 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
966
967 od->_state = OMAP_DEVICE_STATE_IDLE;
968
969 return ret;
970}
971
972/**
973 * omap_device_shutdown - shut down an omap_device
974 * @od: struct omap_device * to shut down
975 *
976 * Shut down omap_device @od by calling all .deactivate_func() entries
977 * in the omap_device's pm_lats table and then shutting down all of
978 * the underlying omap_hwmods. Used when a device is being "removed"
979 * or a device driver is being unloaded. Returns -EINVAL if the
980 * omap_device is not currently enabled or idle, or passes along the
981 * return value of _omap_device_deactivate().
982 */
983int omap_device_shutdown(struct platform_device *pdev)
984{
985 int ret, i;
986 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300987
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600988 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300989
990 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
991 od->_state != OMAP_DEVICE_STATE_IDLE) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700992 dev_warn(&pdev->dev,
993 "omap_device: %s() called from invalid state %d\n",
994 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300995 return -EINVAL;
996 }
997
998 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
999
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001000 for (i = 0; i < od->hwmods_cnt; i++)
1001 omap_hwmod_shutdown(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001002
1003 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
1004
1005 return ret;
1006}
1007
1008/**
Omar Ramirez Luna8bb9fde2012-09-23 17:28:18 -06001009 * omap_device_assert_hardreset - set a device's hardreset line
1010 * @pdev: struct platform_device * to reset
1011 * @name: const char * name of the reset line
1012 *
1013 * Set the hardreset line identified by @name on the IP blocks
1014 * associated with the hwmods backing the platform_device @pdev. All
1015 * of the hwmods associated with @pdev must have the same hardreset
1016 * line linked to them for this to work. Passes along the return value
1017 * of omap_hwmod_assert_hardreset() in the event of any failure, or
1018 * returns 0 upon success.
1019 */
1020int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
1021{
1022 struct omap_device *od = to_omap_device(pdev);
1023 int ret = 0;
1024 int i;
1025
1026 for (i = 0; i < od->hwmods_cnt; i++) {
1027 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
1028 if (ret)
1029 break;
1030 }
1031
1032 return ret;
1033}
1034
1035/**
1036 * omap_device_deassert_hardreset - release a device's hardreset line
1037 * @pdev: struct platform_device * to reset
1038 * @name: const char * name of the reset line
1039 *
1040 * Release the hardreset line identified by @name on the IP blocks
1041 * associated with the hwmods backing the platform_device @pdev. All
1042 * of the hwmods associated with @pdev must have the same hardreset
1043 * line linked to them for this to work. Passes along the return
1044 * value of omap_hwmod_deassert_hardreset() in the event of any
1045 * failure, or returns 0 upon success.
1046 */
1047int omap_device_deassert_hardreset(struct platform_device *pdev,
1048 const char *name)
1049{
1050 struct omap_device *od = to_omap_device(pdev);
1051 int ret = 0;
1052 int i;
1053
1054 for (i = 0; i < od->hwmods_cnt; i++) {
1055 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
1056 if (ret)
1057 break;
1058 }
1059
1060 return ret;
1061}
1062
1063/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001064 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
1065 * @od: struct omap_device *
1066 *
1067 * When a device's maximum wakeup latency limit changes, call some of
1068 * the .activate_func or .deactivate_func function pointers in the
1069 * omap_device's pm_lats array to ensure that the device's maximum
1070 * wakeup latency is less than or equal to the new latency limit.
1071 * Intended to be called by OMAP PM code whenever a device's maximum
1072 * wakeup latency limit changes (e.g., via
1073 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
1074 * done (e.g., if the omap_device is not currently idle, or if the
1075 * wakeup latency is already current with the new limit) or passes
1076 * along the return value of _omap_device_deactivate() or
1077 * _omap_device_activate().
1078 */
1079int omap_device_align_pm_lat(struct platform_device *pdev,
1080 u32 new_wakeup_lat_limit)
1081{
1082 int ret = -EINVAL;
1083 struct omap_device *od;
1084
Kevin Hilman8f0d69d2011-07-09 19:15:20 -06001085 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001086
1087 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
1088 return 0;
1089
1090 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
1091
1092 if (od->_state != OMAP_DEVICE_STATE_IDLE)
1093 return 0;
1094 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
1095 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
1096 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
1097 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
1098
1099 return ret;
1100}
1101
1102/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001103 * omap_device_get_pwrdm - return the powerdomain * associated with @od
1104 * @od: struct omap_device *
1105 *
1106 * Return the powerdomain associated with the first underlying
1107 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
1108 * code. Returns NULL on error or a struct powerdomain * upon
1109 * success.
1110 */
1111struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
1112{
1113 /*
1114 * XXX Assumes that all omap_hwmod powerdomains are identical.
1115 * This may not necessarily be true. There should be a sanity
1116 * check in here to WARN() if any difference appears.
1117 */
1118 if (!od->hwmods_cnt)
1119 return NULL;
1120
1121 return omap_hwmod_get_pwrdm(od->hwmods[0]);
1122}
1123
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06001124/**
1125 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
1126 * @od: struct omap_device *
1127 *
1128 * Return the MPU's virtual address for the base of the hwmod, from
1129 * the ioremap() that the hwmod code does. Only valid if there is one
1130 * hwmod associated with this device. Returns NULL if there are zero
1131 * or more than one hwmods associated with this omap_device;
1132 * otherwise, passes along the return value from
1133 * omap_hwmod_get_mpu_rt_va().
1134 */
1135void __iomem *omap_device_get_rt_va(struct omap_device *od)
1136{
1137 if (od->hwmods_cnt != 1)
1138 return NULL;
1139
1140 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1141}
1142
Nishanth Menon1f8a7d52011-07-27 15:02:32 -05001143/**
1144 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1145 * device pointer.
1146 * @oh_name: name of the hwmod device
1147 *
1148 * Returns back a struct device * pointer associated with a hwmod
1149 * device represented by a hwmod_name
1150 */
1151struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1152{
1153 struct omap_hwmod *oh;
1154
1155 if (!oh_name) {
1156 WARN(1, "%s: no hwmod name!\n", __func__);
1157 return ERR_PTR(-EINVAL);
1158 }
1159
1160 oh = omap_hwmod_lookup(oh_name);
1161 if (IS_ERR_OR_NULL(oh)) {
1162 WARN(1, "%s: no hwmod for %s\n", __func__,
1163 oh_name);
1164 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1165 }
1166 if (IS_ERR_OR_NULL(oh->od)) {
1167 WARN(1, "%s: no omap_device for %s\n", __func__,
1168 oh_name);
1169 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1170 }
1171
1172 if (IS_ERR_OR_NULL(oh->od->pdev))
1173 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1174
1175 return &oh->od->pdev->dev;
1176}
1177EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1178
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001179/*
1180 * Public functions intended for use in omap_device_pm_latency
1181 * .activate_func and .deactivate_func function pointers
1182 */
1183
1184/**
1185 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1186 * @od: struct omap_device *od
1187 *
1188 * Enable all underlying hwmods. Returns 0.
1189 */
1190int omap_device_enable_hwmods(struct omap_device *od)
1191{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001192 int i;
1193
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001194 for (i = 0; i < od->hwmods_cnt; i++)
1195 omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001196
1197 /* XXX pass along return value here? */
1198 return 0;
1199}
1200
1201/**
1202 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1203 * @od: struct omap_device *od
1204 *
1205 * Idle all underlying hwmods. Returns 0.
1206 */
1207int omap_device_idle_hwmods(struct omap_device *od)
1208{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001209 int i;
1210
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001211 for (i = 0; i < od->hwmods_cnt; i++)
1212 omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001213
1214 /* XXX pass along return value here? */
1215 return 0;
1216}
1217
1218/**
1219 * omap_device_disable_clocks - disable all main and interface clocks
1220 * @od: struct omap_device *od
1221 *
1222 * Disable the main functional clock and interface clock for all of the
1223 * omap_hwmods associated with the omap_device. Returns 0.
1224 */
1225int omap_device_disable_clocks(struct omap_device *od)
1226{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001227 int i;
1228
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001229 for (i = 0; i < od->hwmods_cnt; i++)
1230 omap_hwmod_disable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001231
1232 /* XXX pass along return value here? */
1233 return 0;
1234}
1235
1236/**
1237 * omap_device_enable_clocks - enable all main and interface clocks
1238 * @od: struct omap_device *od
1239 *
1240 * Enable the main functional clock and interface clock for all of the
1241 * omap_hwmods associated with the omap_device. Returns 0.
1242 */
1243int omap_device_enable_clocks(struct omap_device *od)
1244{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001245 int i;
1246
Kishon Vijay Abraham If39f4892010-09-24 10:23:18 -06001247 for (i = 0; i < od->hwmods_cnt; i++)
1248 omap_hwmod_enable_clocks(od->hwmods[i]);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +03001249
1250 /* XXX pass along return value here? */
1251 return 0;
1252}
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001253
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001254static struct notifier_block platform_nb = {
1255 .notifier_call = _omap_device_notifier_call,
1256};
1257
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001258static int __init omap_device_init(void)
1259{
Benoit Coussondc2d07e2011-08-10 13:32:08 +02001260 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman3ec2dec2012-02-15 11:47:45 -08001261 return 0;
Kevin Hilman0d5e8252010-08-23 08:10:55 -07001262}
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001263omap_core_initcall(omap_device_init);
Kevin Hilman9634c8d2012-07-10 15:06:11 -07001264
1265/**
1266 * omap_device_late_idle - idle devices without drivers
1267 * @dev: struct device * associated with omap_device
1268 * @data: unused
1269 *
1270 * Check the driver bound status of this device, and idle it
1271 * if there is no driver attached.
1272 */
1273static int __init omap_device_late_idle(struct device *dev, void *data)
1274{
1275 struct platform_device *pdev = to_platform_device(dev);
1276 struct omap_device *od = to_omap_device(pdev);
1277
1278 if (!od)
1279 return 0;
1280
1281 /*
1282 * If omap_device state is enabled, but has no driver bound,
1283 * idle it.
1284 */
1285 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) {
1286 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
1287 dev_warn(dev, "%s: enabled but no driver. Idling\n",
1288 __func__);
1289 omap_device_idle(pdev);
1290 }
1291 }
1292
1293 return 0;
1294}
1295
1296static int __init omap_device_late_init(void)
1297{
1298 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
1299 return 0;
1300}
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001301omap_late_initcall(omap_device_late_init);