Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1 | /* |
| 2 | * omap_device implementation |
| 3 | * |
Paul Walmsley | 887adea | 2010-07-26 16:34:33 -0600 | [diff] [blame] | 4 | * Copyright (C) 2009-2010 Nokia Corporation |
Paul Walmsley | 4788da2 | 2010-05-18 20:24:05 -0600 | [diff] [blame] | 5 | * Paul Walmsley, Kevin Hilman |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 6 | * |
| 7 | * Developed in collaboration with (alphabetical order): Benoit |
Paul Walmsley | 4788da2 | 2010-05-18 20:24:05 -0600 | [diff] [blame] | 8 | * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 9 | * 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 Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 20 | * 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 Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 23 | * |
| 24 | * |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 25 | */ |
| 26 | #undef DEBUG |
| 27 | |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 31 | #include <linux/err.h> |
| 32 | #include <linux/io.h> |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 33 | #include <linux/clk.h> |
Rajendra Nayak | da0653f | 2011-02-25 15:40:21 -0700 | [diff] [blame] | 34 | #include <linux/clkdev.h> |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 35 | #include <linux/pm_runtime.h> |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 36 | #include <linux/of.h> |
| 37 | #include <linux/notifier.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 38 | |
Tony Lindgren | b76c8b1 | 2013-01-11 11:24:18 -0800 | [diff] [blame] | 39 | #include "soc.h" |
Tony Lindgren | 25c7d49 | 2012-10-02 17:25:48 -0700 | [diff] [blame] | 40 | #include "omap_device.h" |
Tony Lindgren | 2a296c8 | 2012-10-02 17:41:35 -0700 | [diff] [blame] | 41 | #include "omap_hwmod.h" |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 42 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 43 | /* Private functions */ |
| 44 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 45 | static void _add_clkdev(struct omap_device *od, const char *clk_alias, |
| 46 | const char *clk_name) |
| 47 | { |
| 48 | struct clk *r; |
| 49 | struct clk_lookup *l; |
| 50 | |
| 51 | if (!clk_alias || !clk_name) |
| 52 | return; |
| 53 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 54 | dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 55 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 56 | r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 57 | if (!IS_ERR(r)) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 58 | dev_warn(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 59 | "alias %s already exists\n", clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 60 | clk_put(r); |
| 61 | return; |
| 62 | } |
| 63 | |
Rajendra Nayak | 6ea74cb | 2012-09-22 02:24:16 -0600 | [diff] [blame] | 64 | r = clk_get(NULL, clk_name); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 65 | if (IS_ERR(r)) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 66 | dev_err(&od->pdev->dev, |
Rajendra Nayak | 6ea74cb | 2012-09-22 02:24:16 -0600 | [diff] [blame] | 67 | "clk_get for %s failed\n", clk_name); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 71 | l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev)); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 72 | if (!l) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 73 | dev_err(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 74 | "clkdev_alloc for %s failed\n", clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | clkdev_add(l); |
| 79 | } |
| 80 | |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 81 | /** |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 82 | * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks |
| 83 | * and main clock |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 84 | * @od: struct omap_device *od |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 85 | * @oh: struct omap_hwmod *oh |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 86 | * |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 87 | * For the main clock and every optional clock present per hwmod per |
| 88 | * omap_device, this function adds an entry in the clkdev table of the |
| 89 | * form <dev-id=dev_name, con-id=role> if it does not exist already. |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 90 | * |
| 91 | * The function is called from inside omap_device_build_ss(), after |
| 92 | * omap_device_register. |
| 93 | * |
| 94 | * This allows drivers to get a pointer to its optional clocks based on its role |
| 95 | * by calling clk_get(<dev*>, <role>). |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 96 | * In the case of the main clock, a "fck" alias is used. |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 97 | * |
| 98 | * No return value. |
| 99 | */ |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 100 | static void _add_hwmod_clocks_clkdev(struct omap_device *od, |
| 101 | struct omap_hwmod *oh) |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 102 | { |
| 103 | int i; |
| 104 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 105 | _add_clkdev(od, "fck", oh->main_clk); |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 106 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 107 | for (i = 0; i < oh->opt_clks_cnt; i++) |
| 108 | _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk); |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 111 | |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 112 | /** |
| 113 | * omap_device_build_from_dt - build an omap_device with multiple hwmods |
| 114 | * @pdev_name: name of the platform_device driver to use |
| 115 | * @pdev_id: this platform_device's connection ID |
| 116 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 117 | * @pdata: platform_data ptr to associate with the platform_device |
| 118 | * @pdata_len: amount of memory pointed to by @pdata |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 119 | * |
| 120 | * Function for building an omap_device already registered from device-tree |
| 121 | * |
| 122 | * Returns 0 or PTR_ERR() on error. |
| 123 | */ |
| 124 | static int omap_device_build_from_dt(struct platform_device *pdev) |
| 125 | { |
| 126 | struct omap_hwmod **hwmods; |
| 127 | struct omap_device *od; |
| 128 | struct omap_hwmod *oh; |
| 129 | struct device_node *node = pdev->dev.of_node; |
| 130 | const char *oh_name; |
| 131 | int oh_cnt, i, ret = 0; |
| 132 | |
| 133 | oh_cnt = of_property_count_strings(node, "ti,hwmods"); |
Russell King | c48cd65 | 2013-03-13 20:44:21 +0000 | [diff] [blame] | 134 | if (oh_cnt <= 0) { |
Benoit Cousson | 5dc06b7 | 2012-01-20 18:14:00 +0100 | [diff] [blame] | 135 | dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n"); |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 136 | return -ENODEV; |
| 137 | } |
| 138 | |
| 139 | hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL); |
| 140 | if (!hwmods) { |
| 141 | ret = -ENOMEM; |
| 142 | goto odbfd_exit; |
| 143 | } |
| 144 | |
| 145 | for (i = 0; i < oh_cnt; i++) { |
| 146 | of_property_read_string_index(node, "ti,hwmods", i, &oh_name); |
| 147 | oh = omap_hwmod_lookup(oh_name); |
| 148 | if (!oh) { |
| 149 | dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n", |
| 150 | oh_name); |
| 151 | ret = -EINVAL; |
| 152 | goto odbfd_exit1; |
| 153 | } |
| 154 | hwmods[i] = oh; |
| 155 | } |
| 156 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 157 | od = omap_device_alloc(pdev, hwmods, oh_cnt); |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 158 | if (!od) { |
| 159 | dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n", |
| 160 | oh_name); |
| 161 | ret = PTR_ERR(od); |
| 162 | goto odbfd_exit1; |
| 163 | } |
| 164 | |
Peter Ujfalusi | 3956a1a | 2012-08-23 16:54:09 +0300 | [diff] [blame] | 165 | /* Fix up missing resource names */ |
| 166 | for (i = 0; i < pdev->num_resources; i++) { |
| 167 | struct resource *r = &pdev->resource[i]; |
| 168 | |
| 169 | if (r->name == NULL) |
| 170 | r->name = dev_name(&pdev->dev); |
| 171 | } |
| 172 | |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 173 | if (of_get_property(node, "ti,no_idle_on_suspend", NULL)) |
| 174 | omap_device_disable_idle_on_suspend(pdev); |
| 175 | |
| 176 | pdev->dev.pm_domain = &omap_device_pm_domain; |
| 177 | |
| 178 | odbfd_exit1: |
| 179 | kfree(hwmods); |
| 180 | odbfd_exit: |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int _omap_device_notifier_call(struct notifier_block *nb, |
| 185 | unsigned long event, void *dev) |
| 186 | { |
| 187 | struct platform_device *pdev = to_platform_device(dev); |
Kevin Hilman | e753345 | 2012-07-10 11:13:16 -0700 | [diff] [blame] | 188 | struct omap_device *od; |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 189 | |
| 190 | switch (event) { |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 191 | case BUS_NOTIFY_DEL_DEVICE: |
| 192 | if (pdev->archdata.od) |
| 193 | omap_device_delete(pdev->archdata.od); |
| 194 | break; |
Kevin Hilman | e753345 | 2012-07-10 11:13:16 -0700 | [diff] [blame] | 195 | case BUS_NOTIFY_ADD_DEVICE: |
| 196 | if (pdev->dev.of_node) |
| 197 | omap_device_build_from_dt(pdev); |
| 198 | /* fall through */ |
| 199 | default: |
| 200 | od = to_omap_device(pdev); |
| 201 | if (od) |
| 202 | od->_driver_status = event; |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return NOTIFY_DONE; |
| 206 | } |
| 207 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 208 | /** |
| 209 | * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods |
| 210 | * @od: struct omap_device *od |
| 211 | * |
| 212 | * Enable all underlying hwmods. Returns 0. |
| 213 | */ |
| 214 | static int _omap_device_enable_hwmods(struct omap_device *od) |
| 215 | { |
| 216 | int i; |
| 217 | |
| 218 | for (i = 0; i < od->hwmods_cnt; i++) |
| 219 | omap_hwmod_enable(od->hwmods[i]); |
| 220 | |
| 221 | /* XXX pass along return value here? */ |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods |
| 227 | * @od: struct omap_device *od |
| 228 | * |
| 229 | * Idle all underlying hwmods. Returns 0. |
| 230 | */ |
| 231 | static int _omap_device_idle_hwmods(struct omap_device *od) |
| 232 | { |
| 233 | int i; |
| 234 | |
| 235 | for (i = 0; i < od->hwmods_cnt; i++) |
| 236 | omap_hwmod_idle(od->hwmods[i]); |
| 237 | |
| 238 | /* XXX pass along return value here? */ |
| 239 | return 0; |
| 240 | } |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 241 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 242 | /* Public functions for use by core code */ |
| 243 | |
| 244 | /** |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 245 | * omap_device_get_context_loss_count - get lost context count |
| 246 | * @od: struct omap_device * |
| 247 | * |
| 248 | * Using the primary hwmod, query the context loss count for this |
| 249 | * device. |
| 250 | * |
| 251 | * Callers should consider context for this device lost any time this |
| 252 | * function returns a value different than the value the caller got |
| 253 | * the last time it called this function. |
| 254 | * |
| 255 | * If any hwmods exist for the omap_device assoiated with @pdev, |
| 256 | * return the context loss counter for that hwmod, otherwise return |
| 257 | * zero. |
| 258 | */ |
Tomi Valkeinen | fc01387 | 2011-06-09 16:56:23 +0300 | [diff] [blame] | 259 | int omap_device_get_context_loss_count(struct platform_device *pdev) |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 260 | { |
| 261 | struct omap_device *od; |
| 262 | u32 ret = 0; |
| 263 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 264 | od = to_omap_device(pdev); |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 265 | |
| 266 | if (od->hwmods_cnt) |
| 267 | ret = omap_hwmod_get_context_loss_count(od->hwmods[0]); |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 273 | * omap_device_count_resources - count number of struct resource entries needed |
| 274 | * @od: struct omap_device * |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame] | 275 | * @flags: Type of resources to include when counting (IRQ/DMA/MEM) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 276 | * |
| 277 | * Count the number of struct resource entries needed for this |
| 278 | * omap_device @od. Used by omap_device_build_ss() to determine how |
| 279 | * much memory to allocate before calling |
| 280 | * omap_device_fill_resources(). Returns the count. |
| 281 | */ |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame] | 282 | static int omap_device_count_resources(struct omap_device *od, |
| 283 | unsigned long flags) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 284 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 285 | int c = 0; |
| 286 | int i; |
| 287 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 288 | for (i = 0; i < od->hwmods_cnt; i++) |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame] | 289 | c += omap_hwmod_count_resources(od->hwmods[i], flags); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 290 | |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 291 | pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n", |
| 292 | od->pdev->name, c, od->hwmods_cnt); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 293 | |
| 294 | return c; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * omap_device_fill_resources - fill in array of struct resource |
| 299 | * @od: struct omap_device * |
| 300 | * @res: pointer to an array of struct resource to be filled in |
| 301 | * |
| 302 | * Populate one or more empty struct resource pointed to by @res with |
| 303 | * the resource data for this omap_device @od. Used by |
| 304 | * omap_device_build_ss() after calling omap_device_count_resources(). |
| 305 | * Ideally this function would not be needed at all. If omap_device |
| 306 | * replaces platform_device, then we can specify our own |
| 307 | * get_resource()/ get_irq()/etc functions that use the underlying |
| 308 | * omap_hwmod information. Or if platform_device is extended to use |
| 309 | * subarchitecture-specific function pointers, the various |
| 310 | * platform_device functions can simply call omap_device internal |
| 311 | * functions to get device resources. Hacking around the existing |
| 312 | * platform_device code wastes memory. Returns 0. |
| 313 | */ |
Kevin Hilman | a2a28ad | 2011-07-21 14:14:35 -0700 | [diff] [blame] | 314 | static int omap_device_fill_resources(struct omap_device *od, |
| 315 | struct resource *res) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 316 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 317 | int i, r; |
| 318 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 319 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 320 | r = omap_hwmod_fill_resources(od->hwmods[i], res); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 321 | res += r; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /** |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 328 | * _od_fill_dma_resources - fill in array of struct resource with dma resources |
| 329 | * @od: struct omap_device * |
| 330 | * @res: pointer to an array of struct resource to be filled in |
| 331 | * |
| 332 | * Populate one or more empty struct resource pointed to by @res with |
| 333 | * the dma resource data for this omap_device @od. Used by |
| 334 | * omap_device_alloc() after calling omap_device_count_resources(). |
| 335 | * |
| 336 | * Ideally this function would not be needed at all. If we have |
| 337 | * mechanism to get dma resources from DT. |
| 338 | * |
| 339 | * Returns 0. |
| 340 | */ |
| 341 | static int _od_fill_dma_resources(struct omap_device *od, |
| 342 | struct resource *res) |
| 343 | { |
| 344 | int i, r; |
| 345 | |
| 346 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 347 | r = omap_hwmod_fill_dma_resources(od->hwmods[i], res); |
| 348 | res += r; |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /** |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 355 | * omap_device_alloc - allocate an omap_device |
| 356 | * @pdev: platform_device that will be included in this omap_device |
| 357 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 358 | * @pdata: platform_data ptr to associate with the platform_device |
| 359 | * @pdata_len: amount of memory pointed to by @pdata |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 360 | * |
| 361 | * Convenience function for allocating an omap_device structure and filling |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 362 | * hwmods, and resources. |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 363 | * |
| 364 | * Returns an struct omap_device pointer or ERR_PTR() on error; |
| 365 | */ |
Ohad Ben-Cohen | 993e4fb | 2012-02-20 09:43:29 -0800 | [diff] [blame] | 366 | struct omap_device *omap_device_alloc(struct platform_device *pdev, |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 367 | struct omap_hwmod **ohs, int oh_cnt) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 368 | { |
| 369 | int ret = -ENOMEM; |
| 370 | struct omap_device *od; |
| 371 | struct resource *res = NULL; |
| 372 | int i, res_count; |
| 373 | struct omap_hwmod **hwmods; |
| 374 | |
| 375 | od = kzalloc(sizeof(struct omap_device), GFP_KERNEL); |
| 376 | if (!od) { |
| 377 | ret = -ENOMEM; |
| 378 | goto oda_exit1; |
| 379 | } |
| 380 | od->hwmods_cnt = oh_cnt; |
| 381 | |
| 382 | hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL); |
| 383 | if (!hwmods) |
| 384 | goto oda_exit2; |
| 385 | |
| 386 | od->hwmods = hwmods; |
| 387 | od->pdev = pdev; |
| 388 | |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 389 | /* |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 390 | * Non-DT Boot: |
| 391 | * Here, pdev->num_resources = 0, and we should get all the |
| 392 | * resources from hwmod. |
| 393 | * |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 394 | * DT Boot: |
| 395 | * OF framework will construct the resource structure (currently |
| 396 | * does for MEM & IRQ resource) and we should respect/use these |
| 397 | * resources, killing hwmod dependency. |
| 398 | * If pdev->num_resources > 0, we assume that MEM & IRQ resources |
| 399 | * have been allocated by OF layer already (through DTB). |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 400 | * As preparation for the future we examine the OF provided resources |
| 401 | * to see if we have DMA resources provided already. In this case |
| 402 | * there is no need to update the resources for the device, we use the |
| 403 | * OF provided ones. |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 404 | * |
| 405 | * TODO: Once DMA resource is available from OF layer, we should |
| 406 | * kill filling any resources from hwmod. |
| 407 | */ |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 408 | if (!pdev->num_resources) { |
| 409 | /* Count all resources for the device */ |
| 410 | res_count = omap_device_count_resources(od, IORESOURCE_IRQ | |
| 411 | IORESOURCE_DMA | |
| 412 | IORESOURCE_MEM); |
| 413 | } else { |
| 414 | /* Take a look if we already have DMA resource via DT */ |
| 415 | for (i = 0; i < pdev->num_resources; i++) { |
| 416 | struct resource *r = &pdev->resource[i]; |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 417 | |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 418 | /* We have it, no need to touch the resources */ |
| 419 | if (r->flags == IORESOURCE_DMA) |
| 420 | goto have_everything; |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 421 | } |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 422 | /* Count only DMA resources for the device */ |
| 423 | res_count = omap_device_count_resources(od, IORESOURCE_DMA); |
| 424 | /* The device has no DMA resource, no need for update */ |
| 425 | if (!res_count) |
| 426 | goto have_everything; |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 427 | |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 428 | res_count += pdev->num_resources; |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Peter Ujfalusi | c567b05 | 2012-11-21 16:15:18 -0700 | [diff] [blame] | 431 | /* Allocate resources memory to account for new resources */ |
| 432 | res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL); |
| 433 | if (!res) |
| 434 | goto oda_exit3; |
| 435 | |
| 436 | if (!pdev->num_resources) { |
| 437 | dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n", |
| 438 | __func__, res_count); |
| 439 | omap_device_fill_resources(od, res); |
| 440 | } else { |
| 441 | dev_dbg(&pdev->dev, |
| 442 | "%s: appending %d DMA resources from hwmod\n", |
| 443 | __func__, res_count - pdev->num_resources); |
| 444 | memcpy(res, pdev->resource, |
| 445 | sizeof(struct resource) * pdev->num_resources); |
| 446 | _od_fill_dma_resources(od, &res[pdev->num_resources]); |
| 447 | } |
| 448 | |
| 449 | ret = platform_device_add_resources(pdev, res, res_count); |
| 450 | kfree(res); |
| 451 | |
| 452 | if (ret) |
| 453 | goto oda_exit3; |
| 454 | |
| 455 | have_everything: |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 456 | pdev->archdata.od = od; |
| 457 | |
| 458 | for (i = 0; i < oh_cnt; i++) { |
| 459 | hwmods[i]->od = od; |
| 460 | _add_hwmod_clocks_clkdev(od, hwmods[i]); |
| 461 | } |
| 462 | |
| 463 | return od; |
| 464 | |
| 465 | oda_exit3: |
| 466 | kfree(hwmods); |
| 467 | oda_exit2: |
| 468 | kfree(od); |
| 469 | oda_exit1: |
| 470 | dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret); |
| 471 | |
| 472 | return ERR_PTR(ret); |
| 473 | } |
| 474 | |
Ohad Ben-Cohen | 993e4fb | 2012-02-20 09:43:29 -0800 | [diff] [blame] | 475 | void omap_device_delete(struct omap_device *od) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 476 | { |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 477 | if (!od) |
| 478 | return; |
| 479 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 480 | od->pdev->archdata.od = NULL; |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 481 | kfree(od->hwmods); |
| 482 | kfree(od); |
| 483 | } |
| 484 | |
| 485 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 486 | * omap_device_build - build and register an omap_device with one omap_hwmod |
| 487 | * @pdev_name: name of the platform_device driver to use |
| 488 | * @pdev_id: this platform_device's connection ID |
| 489 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 490 | * @pdata: platform_data ptr to associate with the platform_device |
| 491 | * @pdata_len: amount of memory pointed to by @pdata |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 492 | * |
| 493 | * Convenience function for building and registering a single |
| 494 | * omap_device record, which in turn builds and registers a |
| 495 | * platform_device record. See omap_device_build_ss() for more |
| 496 | * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise, |
| 497 | * passes along the return value of omap_device_build_ss(). |
| 498 | */ |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 499 | struct platform_device __init *omap_device_build(const char *pdev_name, |
| 500 | int pdev_id, |
| 501 | struct omap_hwmod *oh, |
| 502 | void *pdata, int pdata_len) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 503 | { |
| 504 | struct omap_hwmod *ohs[] = { oh }; |
| 505 | |
| 506 | if (!oh) |
| 507 | return ERR_PTR(-EINVAL); |
| 508 | |
| 509 | return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata, |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 510 | pdata_len); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * omap_device_build_ss - build and register an omap_device with multiple hwmods |
| 515 | * @pdev_name: name of the platform_device driver to use |
| 516 | * @pdev_id: this platform_device's connection ID |
| 517 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 518 | * @pdata: platform_data ptr to associate with the platform_device |
| 519 | * @pdata_len: amount of memory pointed to by @pdata |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 520 | * |
| 521 | * Convenience function for building and registering an omap_device |
| 522 | * subsystem record. Subsystem records consist of multiple |
| 523 | * omap_hwmods. This function in turn builds and registers a |
| 524 | * platform_device record. Returns an ERR_PTR() on error, or passes |
| 525 | * along the return value of omap_device_register(). |
| 526 | */ |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 527 | struct platform_device __init *omap_device_build_ss(const char *pdev_name, |
| 528 | int pdev_id, |
| 529 | struct omap_hwmod **ohs, |
| 530 | int oh_cnt, void *pdata, |
| 531 | int pdata_len) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 532 | { |
| 533 | int ret = -ENOMEM; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 534 | struct platform_device *pdev; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 535 | struct omap_device *od; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 536 | |
| 537 | if (!ohs || oh_cnt == 0 || !pdev_name) |
| 538 | return ERR_PTR(-EINVAL); |
| 539 | |
| 540 | if (!pdata && pdata_len > 0) |
| 541 | return ERR_PTR(-EINVAL); |
| 542 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 543 | pdev = platform_device_alloc(pdev_name, pdev_id); |
| 544 | if (!pdev) { |
| 545 | ret = -ENOMEM; |
| 546 | goto odbs_exit; |
| 547 | } |
| 548 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 549 | /* Set the dev_name early to allow dev_xxx in omap_device_alloc */ |
| 550 | if (pdev->id != -1) |
| 551 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
| 552 | else |
| 553 | dev_set_name(&pdev->dev, "%s", pdev->name); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 554 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 555 | od = omap_device_alloc(pdev, ohs, oh_cnt); |
Wei Yongjun | a87e662 | 2012-09-21 14:32:04 +0800 | [diff] [blame] | 556 | if (IS_ERR(od)) |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 557 | goto odbs_exit1; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 558 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 559 | ret = platform_device_add_data(pdev, pdata, pdata_len); |
Artem Bityutskiy | 49b368a | 2010-07-12 13:38:07 +0000 | [diff] [blame] | 560 | if (ret) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 561 | goto odbs_exit2; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 562 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 563 | ret = omap_device_register(pdev); |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 564 | if (ret) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 565 | goto odbs_exit2; |
Kevin Hilman | 0656358 | 2010-07-26 16:34:30 -0600 | [diff] [blame] | 566 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 567 | return pdev; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 568 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 569 | odbs_exit2: |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 570 | omap_device_delete(od); |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 571 | odbs_exit1: |
| 572 | platform_device_put(pdev); |
| 573 | odbs_exit: |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 574 | |
| 575 | pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret); |
| 576 | |
| 577 | return ERR_PTR(ret); |
| 578 | } |
| 579 | |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 580 | #ifdef CONFIG_PM_RUNTIME |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 581 | static int _od_runtime_suspend(struct device *dev) |
| 582 | { |
| 583 | struct platform_device *pdev = to_platform_device(dev); |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 584 | int ret; |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 585 | |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 586 | ret = pm_generic_runtime_suspend(dev); |
| 587 | |
| 588 | if (!ret) |
| 589 | omap_device_idle(pdev); |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 594 | static int _od_runtime_resume(struct device *dev) |
| 595 | { |
| 596 | struct platform_device *pdev = to_platform_device(dev); |
| 597 | |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 598 | omap_device_enable(pdev); |
| 599 | |
| 600 | return pm_generic_runtime_resume(dev); |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 601 | } |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 602 | #endif |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 603 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 604 | #ifdef CONFIG_SUSPEND |
| 605 | static int _od_suspend_noirq(struct device *dev) |
| 606 | { |
| 607 | struct platform_device *pdev = to_platform_device(dev); |
| 608 | struct omap_device *od = to_omap_device(pdev); |
| 609 | int ret; |
| 610 | |
Kevin Hilman | 72bb6f9 | 2012-07-10 15:29:04 -0700 | [diff] [blame] | 611 | /* Don't attempt late suspend on a driver that is not bound */ |
| 612 | if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) |
| 613 | return 0; |
| 614 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 615 | ret = pm_generic_suspend_noirq(dev); |
| 616 | |
| 617 | if (!ret && !pm_runtime_status_suspended(dev)) { |
| 618 | if (pm_generic_runtime_suspend(dev) == 0) { |
Paul Walmsley | b7c39a3 | 2012-03-03 13:15:33 -0700 | [diff] [blame] | 619 | if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)) |
| 620 | omap_device_idle(pdev); |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 621 | od->flags |= OMAP_DEVICE_SUSPENDED; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | static int _od_resume_noirq(struct device *dev) |
| 629 | { |
| 630 | struct platform_device *pdev = to_platform_device(dev); |
| 631 | struct omap_device *od = to_omap_device(pdev); |
| 632 | |
| 633 | if ((od->flags & OMAP_DEVICE_SUSPENDED) && |
| 634 | !pm_runtime_status_suspended(dev)) { |
| 635 | od->flags &= ~OMAP_DEVICE_SUSPENDED; |
Paul Walmsley | b7c39a3 | 2012-03-03 13:15:33 -0700 | [diff] [blame] | 636 | if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)) |
| 637 | omap_device_enable(pdev); |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 638 | pm_generic_runtime_resume(dev); |
| 639 | } |
| 640 | |
| 641 | return pm_generic_resume_noirq(dev); |
| 642 | } |
Kevin Hilman | 126caf1 | 2011-09-01 10:59:36 -0700 | [diff] [blame] | 643 | #else |
| 644 | #define _od_suspend_noirq NULL |
| 645 | #define _od_resume_noirq NULL |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 646 | #endif |
| 647 | |
Kevin Hilman | 3ec2dec | 2012-02-15 11:47:45 -0800 | [diff] [blame] | 648 | struct dev_pm_domain omap_device_pm_domain = { |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 649 | .ops = { |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 650 | SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume, |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame^] | 651 | NULL) |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 652 | USE_PLATFORM_PM_SLEEP_OPS |
Kevin Hilman | ff35336 | 2011-08-25 15:31:14 +0200 | [diff] [blame] | 653 | .suspend_noirq = _od_suspend_noirq, |
| 654 | .resume_noirq = _od_resume_noirq, |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 655 | } |
| 656 | }; |
| 657 | |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 658 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 659 | * omap_device_register - register an omap_device with one omap_hwmod |
| 660 | * @od: struct omap_device * to register |
| 661 | * |
| 662 | * Register the omap_device structure. This currently just calls |
| 663 | * platform_device_register() on the underlying platform_device. |
| 664 | * Returns the return value of platform_device_register(). |
| 665 | */ |
Ohad Ben-Cohen | 993e4fb | 2012-02-20 09:43:29 -0800 | [diff] [blame] | 666 | int omap_device_register(struct platform_device *pdev) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 667 | { |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 668 | pr_debug("omap_device: %s: registering\n", pdev->name); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 669 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 670 | pdev->dev.pm_domain = &omap_device_pm_domain; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 671 | return platform_device_add(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
| 675 | /* Public functions for use by device drivers through struct platform_data */ |
| 676 | |
| 677 | /** |
| 678 | * omap_device_enable - fully activate an omap_device |
| 679 | * @od: struct omap_device * to activate |
| 680 | * |
| 681 | * Do whatever is necessary for the hwmods underlying omap_device @od |
| 682 | * to be accessible and ready to operate. This generally involves |
| 683 | * enabling clocks, setting SYSCONFIG registers; and in the future may |
| 684 | * involve remuxing pins. Device drivers should call this function |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 685 | * indirectly via pm_runtime_get*(). Returns -EINVAL if called when |
| 686 | * the omap_device is already enabled, or passes along the return |
| 687 | * value of _omap_device_enable_hwmods(). |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 688 | */ |
| 689 | int omap_device_enable(struct platform_device *pdev) |
| 690 | { |
| 691 | int ret; |
| 692 | struct omap_device *od; |
| 693 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 694 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 695 | |
| 696 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 697 | dev_warn(&pdev->dev, |
| 698 | "omap_device: %s() called from invalid state %d\n", |
| 699 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 700 | return -EINVAL; |
| 701 | } |
| 702 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 703 | ret = _omap_device_enable_hwmods(od); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 704 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 705 | od->_state = OMAP_DEVICE_STATE_ENABLED; |
| 706 | |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * omap_device_idle - idle an omap_device |
| 712 | * @od: struct omap_device * to idle |
| 713 | * |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 714 | * Idle omap_device @od. Device drivers call this function indirectly |
| 715 | * via pm_runtime_put*(). Returns -EINVAL if the omap_device is not |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 716 | * currently enabled, or passes along the return value of |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 717 | * _omap_device_idle_hwmods(). |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 718 | */ |
| 719 | int omap_device_idle(struct platform_device *pdev) |
| 720 | { |
| 721 | int ret; |
| 722 | struct omap_device *od; |
| 723 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 724 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 725 | |
| 726 | if (od->_state != OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 727 | dev_warn(&pdev->dev, |
| 728 | "omap_device: %s() called from invalid state %d\n", |
| 729 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 730 | return -EINVAL; |
| 731 | } |
| 732 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 733 | ret = _omap_device_idle_hwmods(od); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 734 | |
| 735 | od->_state = OMAP_DEVICE_STATE_IDLE; |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | /** |
Omar Ramirez Luna | 8bb9fde | 2012-09-23 17:28:18 -0600 | [diff] [blame] | 741 | * omap_device_assert_hardreset - set a device's hardreset line |
| 742 | * @pdev: struct platform_device * to reset |
| 743 | * @name: const char * name of the reset line |
| 744 | * |
| 745 | * Set the hardreset line identified by @name on the IP blocks |
| 746 | * associated with the hwmods backing the platform_device @pdev. All |
| 747 | * of the hwmods associated with @pdev must have the same hardreset |
| 748 | * line linked to them for this to work. Passes along the return value |
| 749 | * of omap_hwmod_assert_hardreset() in the event of any failure, or |
| 750 | * returns 0 upon success. |
| 751 | */ |
| 752 | int omap_device_assert_hardreset(struct platform_device *pdev, const char *name) |
| 753 | { |
| 754 | struct omap_device *od = to_omap_device(pdev); |
| 755 | int ret = 0; |
| 756 | int i; |
| 757 | |
| 758 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 759 | ret = omap_hwmod_assert_hardreset(od->hwmods[i], name); |
| 760 | if (ret) |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * omap_device_deassert_hardreset - release a device's hardreset line |
| 769 | * @pdev: struct platform_device * to reset |
| 770 | * @name: const char * name of the reset line |
| 771 | * |
| 772 | * Release the hardreset line identified by @name on the IP blocks |
| 773 | * associated with the hwmods backing the platform_device @pdev. All |
| 774 | * of the hwmods associated with @pdev must have the same hardreset |
| 775 | * line linked to them for this to work. Passes along the return |
| 776 | * value of omap_hwmod_deassert_hardreset() in the event of any |
| 777 | * failure, or returns 0 upon success. |
| 778 | */ |
| 779 | int omap_device_deassert_hardreset(struct platform_device *pdev, |
| 780 | const char *name) |
| 781 | { |
| 782 | struct omap_device *od = to_omap_device(pdev); |
| 783 | int ret = 0; |
| 784 | int i; |
| 785 | |
| 786 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 787 | ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name); |
| 788 | if (ret) |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | /** |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 796 | * omap_device_get_by_hwmod_name() - convert a hwmod name to |
| 797 | * device pointer. |
| 798 | * @oh_name: name of the hwmod device |
| 799 | * |
| 800 | * Returns back a struct device * pointer associated with a hwmod |
| 801 | * device represented by a hwmod_name |
| 802 | */ |
| 803 | struct device *omap_device_get_by_hwmod_name(const char *oh_name) |
| 804 | { |
| 805 | struct omap_hwmod *oh; |
| 806 | |
| 807 | if (!oh_name) { |
| 808 | WARN(1, "%s: no hwmod name!\n", __func__); |
| 809 | return ERR_PTR(-EINVAL); |
| 810 | } |
| 811 | |
| 812 | oh = omap_hwmod_lookup(oh_name); |
Russell King | 857835c | 2013-02-24 10:56:59 +0000 | [diff] [blame] | 813 | if (!oh) { |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 814 | WARN(1, "%s: no hwmod for %s\n", __func__, |
| 815 | oh_name); |
Russell King | 857835c | 2013-02-24 10:56:59 +0000 | [diff] [blame] | 816 | return ERR_PTR(-ENODEV); |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 817 | } |
Russell King | 857835c | 2013-02-24 10:56:59 +0000 | [diff] [blame] | 818 | if (!oh->od) { |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 819 | WARN(1, "%s: no omap_device for %s\n", __func__, |
| 820 | oh_name); |
Russell King | 857835c | 2013-02-24 10:56:59 +0000 | [diff] [blame] | 821 | return ERR_PTR(-ENODEV); |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 822 | } |
| 823 | |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 824 | return &oh->od->pdev->dev; |
| 825 | } |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 826 | |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 827 | static struct notifier_block platform_nb = { |
| 828 | .notifier_call = _omap_device_notifier_call, |
| 829 | }; |
| 830 | |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 831 | static int __init omap_device_init(void) |
| 832 | { |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 833 | bus_register_notifier(&platform_bus_type, &platform_nb); |
Kevin Hilman | 3ec2dec | 2012-02-15 11:47:45 -0800 | [diff] [blame] | 834 | return 0; |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 835 | } |
Tony Lindgren | b76c8b1 | 2013-01-11 11:24:18 -0800 | [diff] [blame] | 836 | omap_core_initcall(omap_device_init); |
Kevin Hilman | 9634c8d | 2012-07-10 15:06:11 -0700 | [diff] [blame] | 837 | |
| 838 | /** |
| 839 | * omap_device_late_idle - idle devices without drivers |
| 840 | * @dev: struct device * associated with omap_device |
| 841 | * @data: unused |
| 842 | * |
| 843 | * Check the driver bound status of this device, and idle it |
| 844 | * if there is no driver attached. |
| 845 | */ |
| 846 | static int __init omap_device_late_idle(struct device *dev, void *data) |
| 847 | { |
| 848 | struct platform_device *pdev = to_platform_device(dev); |
| 849 | struct omap_device *od = to_omap_device(pdev); |
| 850 | |
| 851 | if (!od) |
| 852 | return 0; |
| 853 | |
| 854 | /* |
| 855 | * If omap_device state is enabled, but has no driver bound, |
| 856 | * idle it. |
| 857 | */ |
| 858 | if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) { |
| 859 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { |
| 860 | dev_warn(dev, "%s: enabled but no driver. Idling\n", |
| 861 | __func__); |
| 862 | omap_device_idle(pdev); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static int __init omap_device_late_init(void) |
| 870 | { |
| 871 | bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle); |
| 872 | return 0; |
| 873 | } |
Kevin Hilman | e7e17c5 | 2013-05-08 16:48:01 -0700 | [diff] [blame] | 874 | omap_late_initcall_sync(omap_device_late_init); |