blob: 41c7b905980a9e7a37a512cf26c06a428b4e9ca6 [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 *
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -070020 * In the medium- to long-term, this code should be implemented as a
21 * proper omap_bus/omap_device in Linux, no more platform_data func
22 * pointers
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030023 *
24 *
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030025 */
26#undef DEBUG
27
28#include <linux/kernel.h>
29#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030031#include <linux/err.h>
32#include <linux/io.h>
Partha Basak4ef7aca2010-09-21 19:23:04 +020033#include <linux/clk.h>
Rajendra Nayakda0653f2011-02-25 15:40:21 -070034#include <linux/clkdev.h>
Tomeu Vizoso989561d2016-01-07 16:46:13 +010035#include <linux/pm_domain.h>
Kevin Hilman345f79b2011-05-31 16:08:09 -070036#include <linux/pm_runtime.h>
Benoit Coussondc2d07e2011-08-10 13:32:08 +020037#include <linux/of.h>
Tony Lindgrend85a2d62017-10-10 14:23:35 -070038#include <linux/of_address.h>
39#include <linux/of_irq.h>
Benoit Coussondc2d07e2011-08-10 13:32:08 +020040#include <linux/notifier.h>
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030041
Tony Lindgrendad12d12013-12-06 10:52:58 -080042#include "common.h"
Tony Lindgrenb76c8b192013-01-11 11:24:18 -080043#include "soc.h"
Tony Lindgren25c7d492012-10-02 17:25:48 -070044#include "omap_device.h"
Tony Lindgren2a296c82012-10-02 17:41:35 -070045#include "omap_hwmod.h"
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030046
Paul Walmsleyb04b65a2009-09-03 20:14:05 +030047/* Private functions */
48
Benoit Coussonbf1e0772011-07-10 05:54:12 -060049static void _add_clkdev(struct omap_device *od, const char *clk_alias,
50 const char *clk_name)
51{
52 struct clk *r;
Russell King1ca90bd2015-03-02 15:46:03 +000053 int rc;
Benoit Coussonbf1e0772011-07-10 05:54:12 -060054
55 if (!clk_alias || !clk_name)
56 return;
57
Kevin Hilmand66b3fe2011-07-21 13:58:51 -070058 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -060059
Kevin Hilmand66b3fe2011-07-21 13:58:51 -070060 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -060061 if (!IS_ERR(r)) {
Markus Pargmann9a02ae42014-08-25 16:15:34 -070062 dev_dbg(&od->pdev->dev,
Kevin Hilman49882c92011-07-21 09:58:36 -070063 "alias %s already exists\n", clk_alias);
Benoit Coussonbf1e0772011-07-10 05:54:12 -060064 clk_put(r);
65 return;
66 }
67
Tero Kristo59dcfc42016-06-30 16:14:59 +030068 r = clk_get_sys(NULL, clk_name);
69
Tony Lindgren1aa8f0c2017-05-31 15:51:37 -070070 if (IS_ERR(r)) {
Tero Kristo59dcfc42016-06-30 16:14:59 +030071 struct of_phandle_args clkspec;
72
73 clkspec.np = of_find_node_by_name(NULL, clk_name);
74
75 r = of_clk_get_from_provider(&clkspec);
76
77 rc = clk_register_clkdev(r, clk_alias,
78 dev_name(&od->pdev->dev));
79 } else {
80 rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev),
81 clk_name, NULL);
82 }
83
Russell King1ca90bd2015-03-02 15:46:03 +000084 if (rc) {
85 if (rc == -ENODEV || rc == -ENOMEM)
86 dev_err(&od->pdev->dev,
87 "clkdev_alloc for %s failed\n", clk_alias);
88 else
89 dev_err(&od->pdev->dev,
90 "clk_get for %s failed\n", clk_name);
Benoit Coussonbf1e0772011-07-10 05:54:12 -060091 }
Benoit Coussonbf1e0772011-07-10 05:54:12 -060092}
93
Partha Basak4ef7aca2010-09-21 19:23:04 +020094/**
Benoit Coussonbf1e0772011-07-10 05:54:12 -060095 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
96 * and main clock
Partha Basak4ef7aca2010-09-21 19:23:04 +020097 * @od: struct omap_device *od
Benoit Coussonbf1e0772011-07-10 05:54:12 -060098 * @oh: struct omap_hwmod *oh
Partha Basak4ef7aca2010-09-21 19:23:04 +020099 *
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600100 * For the main clock and every optional clock present per hwmod per
101 * omap_device, this function adds an entry in the clkdev table of the
102 * form <dev-id=dev_name, con-id=role> if it does not exist already.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200103 *
104 * The function is called from inside omap_device_build_ss(), after
105 * omap_device_register.
106 *
107 * This allows drivers to get a pointer to its optional clocks based on its role
108 * by calling clk_get(<dev*>, <role>).
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600109 * In the case of the main clock, a "fck" alias is used.
Partha Basak4ef7aca2010-09-21 19:23:04 +0200110 *
111 * No return value.
112 */
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600113static void _add_hwmod_clocks_clkdev(struct omap_device *od,
114 struct omap_hwmod *oh)
Partha Basak4ef7aca2010-09-21 19:23:04 +0200115{
116 int i;
117
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600118 _add_clkdev(od, "fck", oh->main_clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200119
Benoit Coussonbf1e0772011-07-10 05:54:12 -0600120 for (i = 0; i < oh->opt_clks_cnt; i++)
121 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
Partha Basak4ef7aca2010-09-21 19:23:04 +0200122}
123
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300124
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200125/**
126 * omap_device_build_from_dt - build an omap_device with multiple hwmods
127 * @pdev_name: name of the platform_device driver to use
128 * @pdev_id: this platform_device's connection ID
129 * @oh: ptr to the single omap_hwmod that backs this omap_device
130 * @pdata: platform_data ptr to associate with the platform_device
131 * @pdata_len: amount of memory pointed to by @pdata
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200132 *
133 * Function for building an omap_device already registered from device-tree
134 *
135 * Returns 0 or PTR_ERR() on error.
136 */
137static int omap_device_build_from_dt(struct platform_device *pdev)
138{
139 struct omap_hwmod **hwmods;
140 struct omap_device *od;
141 struct omap_hwmod *oh;
142 struct device_node *node = pdev->dev.of_node;
Tony Lindgren695eea32018-02-22 14:05:31 -0800143 struct resource res;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200144 const char *oh_name;
145 int oh_cnt, i, ret = 0;
Tony Lindgren71941002018-04-16 10:24:14 -0700146 bool device_active = false, skip_pm_domain = false;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200147
148 oh_cnt = of_property_count_strings(node, "ti,hwmods");
Russell Kingc48cd652013-03-13 20:44:21 +0000149 if (oh_cnt <= 0) {
Benoit Cousson5dc06b72012-01-20 18:14:00 +0100150 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200151 return -ENODEV;
152 }
153
Tony Lindgren71941002018-04-16 10:24:14 -0700154 /* SDMA still needs special handling for omap_device_build() */
155 ret = of_property_read_string_index(node, "ti,hwmods", 0, &oh_name);
156 if (!ret && (!strncmp("dma_system", oh_name, 10) ||
157 !strncmp("dma", oh_name, 3)))
158 skip_pm_domain = true;
159
Tony Lindgren695eea32018-02-22 14:05:31 -0800160 /* Use ti-sysc driver instead of omap_device? */
Tony Lindgren71941002018-04-16 10:24:14 -0700161 if (!skip_pm_domain &&
162 !omap_hwmod_parse_module_range(NULL, node, &res))
Tony Lindgren695eea32018-02-22 14:05:31 -0800163 return -ENODEV;
164
Kees Cook6396bb22018-06-12 14:03:40 -0700165 hwmods = kcalloc(oh_cnt, sizeof(struct omap_hwmod *), GFP_KERNEL);
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200166 if (!hwmods) {
167 ret = -ENOMEM;
168 goto odbfd_exit;
169 }
170
171 for (i = 0; i < oh_cnt; i++) {
172 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
173 oh = omap_hwmod_lookup(oh_name);
174 if (!oh) {
175 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
176 oh_name);
177 ret = -EINVAL;
178 goto odbfd_exit1;
179 }
180 hwmods[i] = oh;
Rajendra Nayak72680322013-07-28 23:01:51 -0600181 if (oh->flags & HWMOD_INIT_NO_IDLE)
182 device_active = true;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200183 }
184
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700185 od = omap_device_alloc(pdev, hwmods, oh_cnt);
Wei Yongjun4cf9cf82013-09-18 12:01:58 -0700186 if (IS_ERR(od)) {
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200187 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
188 oh_name);
189 ret = PTR_ERR(od);
190 goto odbfd_exit1;
191 }
192
Peter Ujfalusi3956a1a2012-08-23 16:54:09 +0300193 /* Fix up missing resource names */
194 for (i = 0; i < pdev->num_resources; i++) {
195 struct resource *r = &pdev->resource[i];
196
197 if (r->name == NULL)
198 r->name = dev_name(&pdev->dev);
199 }
200
Tony Lindgren71941002018-04-16 10:24:14 -0700201 if (!skip_pm_domain) {
202 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
203 if (device_active) {
204 omap_device_enable(pdev);
205 pm_runtime_set_active(&pdev->dev);
206 }
Rajendra Nayak72680322013-07-28 23:01:51 -0600207 }
208
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200209odbfd_exit1:
210 kfree(hwmods);
211odbfd_exit:
Nishanth Menonf5c33b02013-12-03 19:39:13 -0600212 /* if data/we are at fault.. load up a fail handler */
213 if (ret)
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100214 dev_pm_domain_set(&pdev->dev, &omap_device_fail_pm_domain);
Nishanth Menonf5c33b02013-12-03 19:39:13 -0600215
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200216 return ret;
217}
218
219static int _omap_device_notifier_call(struct notifier_block *nb,
220 unsigned long event, void *dev)
221{
222 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilmane7533452012-07-10 11:13:16 -0700223 struct omap_device *od;
Tony Lindgrencf26f112016-02-12 08:56:52 -0800224 int err;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200225
226 switch (event) {
Grygorii Strashko213fa102016-07-28 20:50:37 +0300227 case BUS_NOTIFY_REMOVED_DEVICE:
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200228 if (pdev->archdata.od)
229 omap_device_delete(pdev->archdata.od);
230 break;
Tony Lindgrencf26f112016-02-12 08:56:52 -0800231 case BUS_NOTIFY_UNBOUND_DRIVER:
232 od = to_omap_device(pdev);
233 if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED)) {
234 dev_info(dev, "enabled after unload, idling\n");
235 err = omap_device_idle(pdev);
236 if (err)
237 dev_err(dev, "failed to idle\n");
238 }
239 break;
Dave Gerlach04abaf02017-03-30 14:58:18 -0500240 case BUS_NOTIFY_BIND_DRIVER:
241 od = to_omap_device(pdev);
242 if (od && (od->_state == OMAP_DEVICE_STATE_ENABLED) &&
243 pm_runtime_status_suspended(dev)) {
244 od->_driver_status = BUS_NOTIFY_BIND_DRIVER;
245 pm_runtime_set_active(dev);
246 }
247 break;
Kevin Hilmane7533452012-07-10 11:13:16 -0700248 case BUS_NOTIFY_ADD_DEVICE:
249 if (pdev->dev.of_node)
250 omap_device_build_from_dt(pdev);
Tony Lindgrendad12d12013-12-06 10:52:58 -0800251 omap_auxdata_legacy_init(dev);
Kevin Hilmane7533452012-07-10 11:13:16 -0700252 /* fall through */
253 default:
254 od = to_omap_device(pdev);
255 if (od)
256 od->_driver_status = event;
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200257 }
258
259 return NOTIFY_DONE;
260}
261
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700262/**
263 * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
264 * @od: struct omap_device *od
265 *
266 * Enable all underlying hwmods. Returns 0.
267 */
268static int _omap_device_enable_hwmods(struct omap_device *od)
269{
Pali Rohár6da23352015-02-26 14:49:51 +0100270 int ret = 0;
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700271 int i;
272
273 for (i = 0; i < od->hwmods_cnt; i++)
Pali Rohár6da23352015-02-26 14:49:51 +0100274 ret |= omap_hwmod_enable(od->hwmods[i]);
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700275
Pali Rohár6da23352015-02-26 14:49:51 +0100276 return ret;
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700277}
278
279/**
280 * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
281 * @od: struct omap_device *od
282 *
283 * Idle all underlying hwmods. Returns 0.
284 */
285static int _omap_device_idle_hwmods(struct omap_device *od)
286{
Pali Rohár6da23352015-02-26 14:49:51 +0100287 int ret = 0;
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700288 int i;
289
290 for (i = 0; i < od->hwmods_cnt; i++)
Pali Rohár6da23352015-02-26 14:49:51 +0100291 ret |= omap_hwmod_idle(od->hwmods[i]);
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700292
Pali Rohár6da23352015-02-26 14:49:51 +0100293 return ret;
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700294}
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200295
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300296/* Public functions for use by core code */
297
298/**
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700299 * omap_device_get_context_loss_count - get lost context count
300 * @od: struct omap_device *
301 *
302 * Using the primary hwmod, query the context loss count for this
303 * device.
304 *
305 * Callers should consider context for this device lost any time this
306 * function returns a value different than the value the caller got
307 * the last time it called this function.
308 *
Andrea Gelminif733e7c2016-05-21 13:50:25 +0200309 * If any hwmods exist for the omap_device associated with @pdev,
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700310 * return the context loss counter for that hwmod, otherwise return
311 * zero.
312 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +0300313int omap_device_get_context_loss_count(struct platform_device *pdev)
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700314{
315 struct omap_device *od;
316 u32 ret = 0;
317
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600318 od = to_omap_device(pdev);
Kevin Hilmanc80705a2010-12-21 21:31:55 -0700319
320 if (od->hwmods_cnt)
321 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
322
323 return ret;
324}
325
326/**
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200327 * omap_device_alloc - allocate an omap_device
328 * @pdev: platform_device that will be included in this omap_device
329 * @oh: ptr to the single omap_hwmod that backs this omap_device
330 * @pdata: platform_data ptr to associate with the platform_device
331 * @pdata_len: amount of memory pointed to by @pdata
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200332 *
333 * Convenience function for allocating an omap_device structure and filling
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700334 * hwmods, and resources.
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200335 *
336 * Returns an struct omap_device pointer or ERR_PTR() on error;
337 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800338struct omap_device *omap_device_alloc(struct platform_device *pdev,
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700339 struct omap_hwmod **ohs, int oh_cnt)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200340{
341 int ret = -ENOMEM;
342 struct omap_device *od;
Tony Lindgrenc2b84a92017-10-10 14:27:26 -0700343 int i;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200344 struct omap_hwmod **hwmods;
345
346 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
347 if (!od) {
348 ret = -ENOMEM;
349 goto oda_exit1;
350 }
351 od->hwmods_cnt = oh_cnt;
352
353 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
354 if (!hwmods)
355 goto oda_exit2;
356
357 od->hwmods = hwmods;
358 od->pdev = pdev;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200359 pdev->archdata.od = od;
360
361 for (i = 0; i < oh_cnt; i++) {
362 hwmods[i]->od = od;
363 _add_hwmod_clocks_clkdev(od, hwmods[i]);
364 }
365
366 return od;
367
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200368oda_exit2:
369 kfree(od);
370oda_exit1:
371 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
372
373 return ERR_PTR(ret);
374}
375
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800376void omap_device_delete(struct omap_device *od)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200377{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200378 if (!od)
379 return;
380
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200381 od->pdev->archdata.od = NULL;
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200382 kfree(od->hwmods);
383 kfree(od);
384}
385
386/**
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700387 * omap_device_copy_resources - Add legacy IO and IRQ resources
388 * @oh: interconnect target module
389 * @pdev: platform device to copy resources to
390 *
391 * We still have legacy DMA and smartreflex needing resources.
392 * Let's populate what they need until we can eventually just
393 * remove this function. Note that there should be no need to
394 * call this from omap_device_build_from_dt(), nor should there
395 * be any need to call it for other devices.
396 */
397static int
398omap_device_copy_resources(struct omap_hwmod *oh,
399 struct platform_device *pdev)
400{
401 struct device_node *np, *child;
402 struct property *prop;
403 struct resource *res;
404 const char *name;
405 int error, irq = 0;
406
Tony Lindgrenf0c96c62017-10-30 10:01:39 -0700407 if (!oh || !oh->od || !oh->od->pdev)
408 return -EINVAL;
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700409
410 np = oh->od->pdev->dev.of_node;
411 if (!np) {
412 error = -ENODEV;
413 goto error;
414 }
415
Kees Cook6396bb22018-06-12 14:03:40 -0700416 res = kcalloc(2, sizeof(*res), GFP_KERNEL);
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700417 if (!res)
418 return -ENOMEM;
419
420 /* Do we have a dts range for the interconnect target module? */
421 error = omap_hwmod_parse_module_range(oh, np, res);
422
423 /* No ranges, rely on device reg entry */
424 if (error)
425 error = of_address_to_resource(np, 0, res);
426 if (error)
427 goto free;
428
429 /* SmartReflex needs first IO resource name to be "mpu" */
430 res[0].name = "mpu";
431
432 /*
433 * We may have a configured "ti,sysc" interconnect target with a
434 * dts child with the interrupt. If so use the first child's
435 * first interrupt for "ti-hwmods" legacy support.
436 */
437 of_property_for_each_string(np, "compatible", prop, name)
438 if (!strncmp("ti,sysc-", name, 8))
439 break;
440
441 child = of_get_next_available_child(np, NULL);
442
443 if (name)
444 irq = irq_of_parse_and_map(child, 0);
445 if (!irq)
446 irq = irq_of_parse_and_map(np, 0);
Wei Yongjun552ee302017-10-13 09:29:25 +0000447 if (!irq) {
448 error = -EINVAL;
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700449 goto free;
Wei Yongjun552ee302017-10-13 09:29:25 +0000450 }
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700451
452 /* Legacy DMA code needs interrupt name to be "0" */
453 res[1].start = irq;
454 res[1].end = irq;
455 res[1].flags = IORESOURCE_IRQ;
456 res[1].name = "0";
457
458 error = platform_device_add_resources(pdev, res, 2);
459
460free:
461 kfree(res);
462
463error:
464 WARN(error, "%s: %s device %s failed: %i\n",
465 __func__, oh->name, dev_name(&pdev->dev),
466 error);
467
468 return error;
469}
470
471/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300472 * omap_device_build - build and register an omap_device with one omap_hwmod
473 * @pdev_name: name of the platform_device driver to use
474 * @pdev_id: this platform_device's connection ID
475 * @oh: ptr to the single omap_hwmod that backs this omap_device
476 * @pdata: platform_data ptr to associate with the platform_device
477 * @pdata_len: amount of memory pointed to by @pdata
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300478 *
479 * Convenience function for building and registering a single
480 * omap_device record, which in turn builds and registers a
481 * platform_device record. See omap_device_build_ss() for more
482 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
483 * passes along the return value of omap_device_build_ss().
484 */
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700485struct platform_device __init *omap_device_build(const char *pdev_name,
486 int pdev_id,
487 struct omap_hwmod *oh,
488 void *pdata, int pdata_len)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300489{
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300490 int ret = -ENOMEM;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700491 struct platform_device *pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300492 struct omap_device *od;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300493
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700494 if (!oh || !pdev_name)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300495 return ERR_PTR(-EINVAL);
496
497 if (!pdata && pdata_len > 0)
498 return ERR_PTR(-EINVAL);
499
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700500 if (strncmp(oh->name, "smartreflex", 11) &&
501 strncmp(oh->name, "dma", 3)) {
502 pr_warn("%s need to update %s to probe with dt\na",
503 __func__, pdev_name);
504 ret = -ENODEV;
505 goto odbs_exit;
506 }
507
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700508 pdev = platform_device_alloc(pdev_name, pdev_id);
509 if (!pdev) {
510 ret = -ENOMEM;
511 goto odbs_exit;
512 }
513
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200514 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
515 if (pdev->id != -1)
516 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
517 else
518 dev_set_name(&pdev->dev, "%s", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300519
Tony Lindgrend85a2d62017-10-10 14:23:35 -0700520 /*
521 * Must be called before omap_device_alloc() as oh->od
522 * only contains the currently registered omap_device
523 * and will get overwritten by omap_device_alloc().
524 */
525 ret = omap_device_copy_resources(oh, pdev);
526 if (ret)
527 goto odbs_exit1;
528
529 od = omap_device_alloc(pdev, &oh, 1);
Dan Carpentere9a9bb42017-11-14 09:06:48 +0300530 if (IS_ERR(od)) {
531 ret = PTR_ERR(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700532 goto odbs_exit1;
Dan Carpentere9a9bb42017-11-14 09:06:48 +0300533 }
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300534
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700535 ret = platform_device_add_data(pdev, pdata, pdata_len);
Artem Bityutskiy49b368a2010-07-12 13:38:07 +0000536 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200537 goto odbs_exit2;
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700538
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700539 ret = omap_device_register(pdev);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700540 if (ret)
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200541 goto odbs_exit2;
Kevin Hilman06563582010-07-26 16:34:30 -0600542
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700543 return pdev;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300544
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700545odbs_exit2:
Benoit Coussona4f6cdb2011-08-09 16:47:01 +0200546 omap_device_delete(od);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700547odbs_exit1:
548 platform_device_put(pdev);
549odbs_exit:
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300550
551 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
552
553 return ERR_PTR(ret);
554}
555
Rafael J. Wysockibf7c5442014-12-13 00:42:49 +0100556#ifdef CONFIG_PM
Kevin Hilman638080c2011-04-29 00:36:42 +0200557static int _od_runtime_suspend(struct device *dev)
558{
559 struct platform_device *pdev = to_platform_device(dev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700560 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200561
Kevin Hilman345f79b2011-05-31 16:08:09 -0700562 ret = pm_generic_runtime_suspend(dev);
Pali Rohár6da23352015-02-26 14:49:51 +0100563 if (ret)
564 return ret;
Kevin Hilman345f79b2011-05-31 16:08:09 -0700565
Pali Rohár6da23352015-02-26 14:49:51 +0100566 return omap_device_idle(pdev);
Kevin Hilman345f79b2011-05-31 16:08:09 -0700567}
568
Kevin Hilman638080c2011-04-29 00:36:42 +0200569static int _od_runtime_resume(struct device *dev)
570{
571 struct platform_device *pdev = to_platform_device(dev);
Pali Rohár6da23352015-02-26 14:49:51 +0100572 int ret;
Kevin Hilman638080c2011-04-29 00:36:42 +0200573
Pali Rohár6da23352015-02-26 14:49:51 +0100574 ret = omap_device_enable(pdev);
Tony Lindgren08c78e92016-02-12 08:56:52 -0800575 if (ret) {
576 dev_err(dev, "use pm_runtime_put_sync_suspend() in driver?\n");
Pali Rohár6da23352015-02-26 14:49:51 +0100577 return ret;
Tony Lindgren08c78e92016-02-12 08:56:52 -0800578 }
Kevin Hilman345f79b2011-05-31 16:08:09 -0700579
580 return pm_generic_runtime_resume(dev);
Kevin Hilman638080c2011-04-29 00:36:42 +0200581}
Nishanth Menonf5c33b02013-12-03 19:39:13 -0600582
583static int _od_fail_runtime_suspend(struct device *dev)
584{
585 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
586 return -ENODEV;
587}
588
589static int _od_fail_runtime_resume(struct device *dev)
590{
591 dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
592 return -ENODEV;
593}
594
Kevin Hilman256a5432011-07-12 22:48:03 +0200595#endif
Kevin Hilman638080c2011-04-29 00:36:42 +0200596
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200597#ifdef CONFIG_SUSPEND
598static int _od_suspend_noirq(struct device *dev)
599{
600 struct platform_device *pdev = to_platform_device(dev);
601 struct omap_device *od = to_omap_device(pdev);
602 int ret;
603
Kevin Hilman72bb6f92012-07-10 15:29:04 -0700604 /* Don't attempt late suspend on a driver that is not bound */
605 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
606 return 0;
607
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200608 ret = pm_generic_suspend_noirq(dev);
609
610 if (!ret && !pm_runtime_status_suspended(dev)) {
611 if (pm_generic_runtime_suspend(dev) == 0) {
Sourav Poddar4b7ec5a2013-04-27 01:55:34 +0530612 omap_device_idle(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200613 od->flags |= OMAP_DEVICE_SUSPENDED;
614 }
615 }
616
617 return ret;
618}
619
620static int _od_resume_noirq(struct device *dev)
621{
622 struct platform_device *pdev = to_platform_device(dev);
623 struct omap_device *od = to_omap_device(pdev);
624
Nishanth Menon3522bf72013-11-14 11:05:16 -0600625 if (od->flags & OMAP_DEVICE_SUSPENDED) {
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200626 od->flags &= ~OMAP_DEVICE_SUSPENDED;
Sourav Poddar4b7ec5a2013-04-27 01:55:34 +0530627 omap_device_enable(pdev);
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200628 pm_generic_runtime_resume(dev);
629 }
630
631 return pm_generic_resume_noirq(dev);
632}
Kevin Hilman126caf12011-09-01 10:59:36 -0700633#else
634#define _od_suspend_noirq NULL
635#define _od_resume_noirq NULL
Kevin Hilmanc03f0072011-07-12 22:48:19 +0200636#endif
637
Nishanth Menonf5c33b02013-12-03 19:39:13 -0600638struct dev_pm_domain omap_device_fail_pm_domain = {
639 .ops = {
640 SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
641 _od_fail_runtime_resume, NULL)
642 }
643};
644
Kevin Hilman3ec2dec2012-02-15 11:47:45 -0800645struct dev_pm_domain omap_device_pm_domain = {
Kevin Hilman638080c2011-04-29 00:36:42 +0200646 .ops = {
Kevin Hilman256a5432011-07-12 22:48:03 +0200647 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200648 NULL)
Kevin Hilman638080c2011-04-29 00:36:42 +0200649 USE_PLATFORM_PM_SLEEP_OPS
Grygorii Strashkoc381f222015-04-27 21:24:32 +0300650 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
651 _od_resume_noirq)
Kevin Hilman638080c2011-04-29 00:36:42 +0200652 }
653};
654
Thara Gopinathc23a97d2010-02-24 12:05:58 -0700655/**
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300656 * omap_device_register - register an omap_device with one omap_hwmod
657 * @od: struct omap_device * to register
658 *
659 * Register the omap_device structure. This currently just calls
660 * platform_device_register() on the underlying platform_device.
661 * Returns the return value of platform_device_register().
662 */
Ohad Ben-Cohen993e4fb2012-02-20 09:43:29 -0800663int omap_device_register(struct platform_device *pdev)
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300664{
Kevin Hilmanbfae4f82011-07-21 14:47:53 -0700665 pr_debug("omap_device: %s: registering\n", pdev->name);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300666
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100667 dev_pm_domain_set(&pdev->dev, &omap_device_pm_domain);
Kevin Hilmand66b3fe2011-07-21 13:58:51 -0700668 return platform_device_add(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300669}
670
671
672/* Public functions for use by device drivers through struct platform_data */
673
674/**
675 * omap_device_enable - fully activate an omap_device
676 * @od: struct omap_device * to activate
677 *
678 * Do whatever is necessary for the hwmods underlying omap_device @od
679 * to be accessible and ready to operate. This generally involves
680 * enabling clocks, setting SYSCONFIG registers; and in the future may
681 * involve remuxing pins. Device drivers should call this function
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700682 * indirectly via pm_runtime_get*(). Returns -EINVAL if called when
683 * the omap_device is already enabled, or passes along the return
684 * value of _omap_device_enable_hwmods().
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300685 */
686int omap_device_enable(struct platform_device *pdev)
687{
688 int ret;
689 struct omap_device *od;
690
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600691 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300692
693 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700694 dev_warn(&pdev->dev,
695 "omap_device: %s() called from invalid state %d\n",
696 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300697 return -EINVAL;
698 }
699
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700700 ret = _omap_device_enable_hwmods(od);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300701
Pali Rohár6da23352015-02-26 14:49:51 +0100702 if (ret == 0)
703 od->_state = OMAP_DEVICE_STATE_ENABLED;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300704
705 return ret;
706}
707
708/**
709 * omap_device_idle - idle an omap_device
710 * @od: struct omap_device * to idle
711 *
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700712 * Idle omap_device @od. Device drivers call this function indirectly
713 * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300714 * currently enabled, or passes along the return value of
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700715 * _omap_device_idle_hwmods().
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300716 */
717int omap_device_idle(struct platform_device *pdev)
718{
719 int ret;
720 struct omap_device *od;
721
Kevin Hilman8f0d69d2011-07-09 19:15:20 -0600722 od = to_omap_device(pdev);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300723
724 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
Kevin Hilman49882c92011-07-21 09:58:36 -0700725 dev_warn(&pdev->dev,
726 "omap_device: %s() called from invalid state %d\n",
727 __func__, od->_state);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300728 return -EINVAL;
729 }
730
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -0700731 ret = _omap_device_idle_hwmods(od);
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300732
Pali Rohár6da23352015-02-26 14:49:51 +0100733 if (ret == 0)
734 od->_state = OMAP_DEVICE_STATE_IDLE;
Paul Walmsleyb04b65a2009-09-03 20:14:05 +0300735
736 return ret;
737}
738
739/**
Omar Ramirez Luna8bb9fde2012-09-23 17:28:18 -0600740 * omap_device_assert_hardreset - set a device's hardreset line
741 * @pdev: struct platform_device * to reset
742 * @name: const char * name of the reset line
743 *
744 * Set the hardreset line identified by @name on the IP blocks
745 * associated with the hwmods backing the platform_device @pdev. All
746 * of the hwmods associated with @pdev must have the same hardreset
747 * line linked to them for this to work. Passes along the return value
748 * of omap_hwmod_assert_hardreset() in the event of any failure, or
749 * returns 0 upon success.
750 */
751int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
752{
753 struct omap_device *od = to_omap_device(pdev);
754 int ret = 0;
755 int i;
756
757 for (i = 0; i < od->hwmods_cnt; i++) {
758 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
759 if (ret)
760 break;
761 }
762
763 return ret;
764}
765
766/**
767 * omap_device_deassert_hardreset - release a device's hardreset line
768 * @pdev: struct platform_device * to reset
769 * @name: const char * name of the reset line
770 *
771 * Release the hardreset line identified by @name on the IP blocks
772 * associated with the hwmods backing the platform_device @pdev. All
773 * of the hwmods associated with @pdev must have the same hardreset
774 * line linked to them for this to work. Passes along the return
775 * value of omap_hwmod_deassert_hardreset() in the event of any
776 * failure, or returns 0 upon success.
777 */
778int omap_device_deassert_hardreset(struct platform_device *pdev,
779 const char *name)
780{
781 struct omap_device *od = to_omap_device(pdev);
782 int ret = 0;
783 int i;
784
785 for (i = 0; i < od->hwmods_cnt; i++) {
786 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
787 if (ret)
788 break;
789 }
790
791 return ret;
792}
793
794/**
Nishanth Menon1f8a7d52011-07-27 15:02:32 -0500795 * omap_device_get_by_hwmod_name() - convert a hwmod name to
796 * device pointer.
797 * @oh_name: name of the hwmod device
798 *
799 * Returns back a struct device * pointer associated with a hwmod
800 * device represented by a hwmod_name
801 */
802struct device *omap_device_get_by_hwmod_name(const char *oh_name)
803{
804 struct omap_hwmod *oh;
805
806 if (!oh_name) {
807 WARN(1, "%s: no hwmod name!\n", __func__);
808 return ERR_PTR(-EINVAL);
809 }
810
811 oh = omap_hwmod_lookup(oh_name);
Russell King857835c2013-02-24 10:56:59 +0000812 if (!oh) {
Nishanth Menon1f8a7d52011-07-27 15:02:32 -0500813 WARN(1, "%s: no hwmod for %s\n", __func__,
814 oh_name);
Russell King857835c2013-02-24 10:56:59 +0000815 return ERR_PTR(-ENODEV);
Nishanth Menon1f8a7d52011-07-27 15:02:32 -0500816 }
Russell King857835c2013-02-24 10:56:59 +0000817 if (!oh->od) {
Nishanth Menon1f8a7d52011-07-27 15:02:32 -0500818 WARN(1, "%s: no omap_device for %s\n", __func__,
819 oh_name);
Russell King857835c2013-02-24 10:56:59 +0000820 return ERR_PTR(-ENODEV);
Nishanth Menon1f8a7d52011-07-27 15:02:32 -0500821 }
822
Nishanth Menon1f8a7d52011-07-27 15:02:32 -0500823 return &oh->od->pdev->dev;
824}
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700825
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200826static struct notifier_block platform_nb = {
827 .notifier_call = _omap_device_notifier_call,
828};
829
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700830static int __init omap_device_init(void)
831{
Benoit Coussondc2d07e2011-08-10 13:32:08 +0200832 bus_register_notifier(&platform_bus_type, &platform_nb);
Kevin Hilman3ec2dec2012-02-15 11:47:45 -0800833 return 0;
Kevin Hilman0d5e8252010-08-23 08:10:55 -0700834}
Tony Lindgren8dd5ea72015-12-03 11:38:09 -0800835omap_postcore_initcall(omap_device_init);
Kevin Hilman9634c8d2012-07-10 15:06:11 -0700836
837/**
838 * omap_device_late_idle - idle devices without drivers
839 * @dev: struct device * associated with omap_device
840 * @data: unused
841 *
842 * Check the driver bound status of this device, and idle it
843 * if there is no driver attached.
844 */
845static int __init omap_device_late_idle(struct device *dev, void *data)
846{
847 struct platform_device *pdev = to_platform_device(dev);
848 struct omap_device *od = to_omap_device(pdev);
Rajendra Nayakf66e3292013-07-28 23:01:50 -0600849 int i;
Kevin Hilman9634c8d2012-07-10 15:06:11 -0700850
851 if (!od)
852 return 0;
853
854 /*
855 * If omap_device state is enabled, but has no driver bound,
856 * idle it.
857 */
Rajendra Nayakf66e3292013-07-28 23:01:50 -0600858
859 /*
860 * Some devices (like memory controllers) are always kept
861 * enabled, and should not be idled even with no drivers.
862 */
863 for (i = 0; i < od->hwmods_cnt; i++)
864 if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
865 return 0;
866
Grygorii Strashkofe8291e2015-09-01 13:59:24 -0700867 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
868 od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
Kevin Hilman9634c8d2012-07-10 15:06:11 -0700869 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
870 dev_warn(dev, "%s: enabled but no driver. Idling\n",
871 __func__);
872 omap_device_idle(pdev);
873 }
874 }
875
876 return 0;
877}
878
879static int __init omap_device_late_init(void)
880{
881 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
Tony Lindgren4b91f7f2014-10-27 13:05:54 -0700882
Kevin Hilman9634c8d2012-07-10 15:06:11 -0700883 return 0;
884}
Kevin Hilmane7e17c52013-05-08 16:48:01 -0700885omap_late_initcall_sync(omap_device_late_init);