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 | * |
| 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 Lin | 5558141 | 2011-11-07 12:27:10 -0800 | [diff] [blame] | 81 | #include <linux/export.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 82 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 83 | #include <linux/slab.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 84 | #include <linux/err.h> |
| 85 | #include <linux/io.h> |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 86 | #include <linux/clk.h> |
Rajendra Nayak | da0653f | 2011-02-25 15:40:21 -0700 | [diff] [blame] | 87 | #include <linux/clkdev.h> |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 88 | #include <linux/pm_runtime.h> |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 89 | #include <linux/of.h> |
| 90 | #include <linux/notifier.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 91 | |
Tony Lindgren | 25c7d49 | 2012-10-02 17:25:48 -0700 | [diff] [blame] | 92 | #include "omap_device.h" |
Tony Lindgren | 2a296c8 | 2012-10-02 17:41:35 -0700 | [diff] [blame] | 93 | #include "omap_hwmod.h" |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 94 | |
| 95 | /* These parameters are passed to _omap_device_{de,}activate() */ |
| 96 | #define USE_WAKEUP_LAT 0 |
| 97 | #define IGNORE_WAKEUP_LAT 1 |
| 98 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 99 | static int omap_early_device_register(struct platform_device *pdev); |
Kevin Hilman | a2a28ad | 2011-07-21 14:14:35 -0700 | [diff] [blame] | 100 | |
Benoit Cousson | b7b5bc9 | 2011-08-09 16:54:19 +0200 | [diff] [blame] | 101 | static struct omap_device_pm_latency omap_default_latency[] = { |
| 102 | { |
| 103 | .deactivate_func = omap_device_idle_hwmods, |
| 104 | .activate_func = omap_device_enable_hwmods, |
| 105 | .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST, |
| 106 | } |
| 107 | }; |
| 108 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 109 | /* Private functions */ |
| 110 | |
| 111 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 112 | * _omap_device_activate - increase device readiness |
| 113 | * @od: struct omap_device * |
| 114 | * @ignore_lat: increase to latency target (0) or full readiness (1)? |
| 115 | * |
| 116 | * Increase readiness of omap_device @od (thus decreasing device |
| 117 | * wakeup latency, but consuming more power). If @ignore_lat is |
| 118 | * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise, |
| 119 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup |
| 120 | * latency is greater than the requested maximum wakeup latency, step |
| 121 | * backwards in the omap_device_pm_latency table to ensure the |
| 122 | * device's maximum wakeup latency is less than or equal to the |
| 123 | * requested maximum wakeup latency. Returns 0. |
| 124 | */ |
| 125 | static int _omap_device_activate(struct omap_device *od, u8 ignore_lat) |
| 126 | { |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 127 | struct timespec a, b, c; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 128 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 129 | dev_dbg(&od->pdev->dev, "omap_device: activating\n"); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 130 | |
| 131 | while (od->pm_lat_level > 0) { |
| 132 | struct omap_device_pm_latency *odpl; |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 133 | unsigned long long act_lat = 0; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 134 | |
| 135 | od->pm_lat_level--; |
| 136 | |
| 137 | odpl = od->pm_lats + od->pm_lat_level; |
| 138 | |
| 139 | if (!ignore_lat && |
| 140 | (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit)) |
| 141 | break; |
| 142 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 143 | read_persistent_clock(&a); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 144 | |
| 145 | /* XXX check return code */ |
| 146 | odpl->activate_func(od); |
| 147 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 148 | read_persistent_clock(&b); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 149 | |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 150 | c = timespec_sub(b, a); |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 151 | act_lat = timespec_to_ns(&c); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 152 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 153 | dev_dbg(&od->pdev->dev, |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 154 | "omap_device: pm_lat %d: activate: elapsed time %llu nsec\n", |
| 155 | od->pm_lat_level, act_lat); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 156 | |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 157 | if (act_lat > odpl->activate_lat) { |
| 158 | odpl->activate_lat_worst = act_lat; |
| 159 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { |
| 160 | odpl->activate_lat = act_lat; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 161 | dev_dbg(&od->pdev->dev, |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 162 | "new worst case activate latency %d: %llu\n", |
Grazvydas Ignotas | 47c3e5d | 2011-07-25 16:18:24 +0300 | [diff] [blame] | 163 | od->pm_lat_level, act_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 164 | } else |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 165 | dev_warn(&od->pdev->dev, |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 166 | "activate latency %d higher than expected. (%llu > %d)\n", |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 167 | od->pm_lat_level, act_lat, |
| 168 | odpl->activate_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 169 | } |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 170 | |
| 171 | od->dev_wakeup_lat -= odpl->activate_lat; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * _omap_device_deactivate - decrease device readiness |
| 179 | * @od: struct omap_device * |
| 180 | * @ignore_lat: decrease to latency target (0) or full inactivity (1)? |
| 181 | * |
| 182 | * Decrease readiness of omap_device @od (thus increasing device |
| 183 | * wakeup latency, but conserving power). If @ignore_lat is |
| 184 | * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise, |
| 185 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup |
| 186 | * latency is less than the requested maximum wakeup latency, step |
| 187 | * forwards in the omap_device_pm_latency table to ensure the device's |
| 188 | * maximum wakeup latency is less than or equal to the requested |
| 189 | * maximum wakeup latency. Returns 0. |
| 190 | */ |
| 191 | static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat) |
| 192 | { |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 193 | struct timespec a, b, c; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 194 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 195 | dev_dbg(&od->pdev->dev, "omap_device: deactivating\n"); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 196 | |
| 197 | while (od->pm_lat_level < od->pm_lats_cnt) { |
| 198 | struct omap_device_pm_latency *odpl; |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 199 | unsigned long long deact_lat = 0; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 200 | |
| 201 | odpl = od->pm_lats + od->pm_lat_level; |
| 202 | |
| 203 | if (!ignore_lat && |
| 204 | ((od->dev_wakeup_lat + odpl->activate_lat) > |
| 205 | od->_dev_wakeup_lat_limit)) |
| 206 | break; |
| 207 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 208 | read_persistent_clock(&a); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 209 | |
| 210 | /* XXX check return code */ |
| 211 | odpl->deactivate_func(od); |
| 212 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 213 | read_persistent_clock(&b); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 214 | |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 215 | c = timespec_sub(b, a); |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 216 | deact_lat = timespec_to_ns(&c); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 217 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 218 | dev_dbg(&od->pdev->dev, |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 219 | "omap_device: pm_lat %d: deactivate: elapsed time %llu nsec\n", |
| 220 | od->pm_lat_level, deact_lat); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 221 | |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 222 | if (deact_lat > odpl->deactivate_lat) { |
| 223 | odpl->deactivate_lat_worst = deact_lat; |
| 224 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { |
| 225 | odpl->deactivate_lat = deact_lat; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 226 | dev_dbg(&od->pdev->dev, |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 227 | "new worst case deactivate latency %d: %llu\n", |
Grazvydas Ignotas | 47c3e5d | 2011-07-25 16:18:24 +0300 | [diff] [blame] | 228 | od->pm_lat_level, deact_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 229 | } else |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 230 | dev_warn(&od->pdev->dev, |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 231 | "deactivate latency %d higher than expected. (%llu > %d)\n", |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 232 | od->pm_lat_level, deact_lat, |
| 233 | odpl->deactivate_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 236 | od->dev_wakeup_lat += odpl->activate_lat; |
| 237 | |
| 238 | od->pm_lat_level++; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 244 | static void _add_clkdev(struct omap_device *od, const char *clk_alias, |
| 245 | const char *clk_name) |
| 246 | { |
| 247 | struct clk *r; |
| 248 | struct clk_lookup *l; |
| 249 | |
| 250 | if (!clk_alias || !clk_name) |
| 251 | return; |
| 252 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 253 | 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] | 254 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 255 | r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 256 | if (!IS_ERR(r)) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 257 | dev_warn(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 258 | "alias %s already exists\n", clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 259 | clk_put(r); |
| 260 | return; |
| 261 | } |
| 262 | |
Rajendra Nayak | 6ea74cb | 2012-09-22 02:24:16 -0600 | [diff] [blame] | 263 | r = clk_get(NULL, clk_name); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 264 | if (IS_ERR(r)) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 265 | dev_err(&od->pdev->dev, |
Rajendra Nayak | 6ea74cb | 2012-09-22 02:24:16 -0600 | [diff] [blame] | 266 | "clk_get for %s failed\n", clk_name); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 270 | l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev)); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 271 | if (!l) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 272 | dev_err(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 273 | "clkdev_alloc for %s failed\n", clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
| 277 | clkdev_add(l); |
| 278 | } |
| 279 | |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 280 | /** |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 281 | * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks |
| 282 | * and main clock |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 283 | * @od: struct omap_device *od |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 284 | * @oh: struct omap_hwmod *oh |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 285 | * |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 286 | * For the main clock and every optional clock present per hwmod per |
| 287 | * omap_device, this function adds an entry in the clkdev table of the |
| 288 | * form <dev-id=dev_name, con-id=role> if it does not exist already. |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 289 | * |
| 290 | * The function is called from inside omap_device_build_ss(), after |
| 291 | * omap_device_register. |
| 292 | * |
| 293 | * This allows drivers to get a pointer to its optional clocks based on its role |
| 294 | * by calling clk_get(<dev*>, <role>). |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 295 | * In the case of the main clock, a "fck" alias is used. |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 296 | * |
| 297 | * No return value. |
| 298 | */ |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 299 | static void _add_hwmod_clocks_clkdev(struct omap_device *od, |
| 300 | struct omap_hwmod *oh) |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 301 | { |
| 302 | int i; |
| 303 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 304 | _add_clkdev(od, "fck", oh->main_clk); |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 305 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 306 | for (i = 0; i < oh->opt_clks_cnt; i++) |
| 307 | _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] | 308 | } |
| 309 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 310 | |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 311 | /** |
| 312 | * omap_device_build_from_dt - build an omap_device with multiple hwmods |
| 313 | * @pdev_name: name of the platform_device driver to use |
| 314 | * @pdev_id: this platform_device's connection ID |
| 315 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 316 | * @pdata: platform_data ptr to associate with the platform_device |
| 317 | * @pdata_len: amount of memory pointed to by @pdata |
| 318 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 319 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
| 320 | * @is_early_device: should the device be registered as an early device or not |
| 321 | * |
| 322 | * Function for building an omap_device already registered from device-tree |
| 323 | * |
| 324 | * Returns 0 or PTR_ERR() on error. |
| 325 | */ |
| 326 | static int omap_device_build_from_dt(struct platform_device *pdev) |
| 327 | { |
| 328 | struct omap_hwmod **hwmods; |
| 329 | struct omap_device *od; |
| 330 | struct omap_hwmod *oh; |
| 331 | struct device_node *node = pdev->dev.of_node; |
| 332 | const char *oh_name; |
| 333 | int oh_cnt, i, ret = 0; |
| 334 | |
| 335 | oh_cnt = of_property_count_strings(node, "ti,hwmods"); |
| 336 | if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) { |
Benoit Cousson | 5dc06b7 | 2012-01-20 18:14:00 +0100 | [diff] [blame] | 337 | dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n"); |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 338 | return -ENODEV; |
| 339 | } |
| 340 | |
| 341 | hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL); |
| 342 | if (!hwmods) { |
| 343 | ret = -ENOMEM; |
| 344 | goto odbfd_exit; |
| 345 | } |
| 346 | |
| 347 | for (i = 0; i < oh_cnt; i++) { |
| 348 | of_property_read_string_index(node, "ti,hwmods", i, &oh_name); |
| 349 | oh = omap_hwmod_lookup(oh_name); |
| 350 | if (!oh) { |
| 351 | dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n", |
| 352 | oh_name); |
| 353 | ret = -EINVAL; |
| 354 | goto odbfd_exit1; |
| 355 | } |
| 356 | hwmods[i] = oh; |
| 357 | } |
| 358 | |
| 359 | od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0); |
| 360 | if (!od) { |
| 361 | dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n", |
| 362 | oh_name); |
| 363 | ret = PTR_ERR(od); |
| 364 | goto odbfd_exit1; |
| 365 | } |
| 366 | |
Peter Ujfalusi | 3956a1a | 2012-08-23 16:54:09 +0300 | [diff] [blame] | 367 | /* Fix up missing resource names */ |
| 368 | for (i = 0; i < pdev->num_resources; i++) { |
| 369 | struct resource *r = &pdev->resource[i]; |
| 370 | |
| 371 | if (r->name == NULL) |
| 372 | r->name = dev_name(&pdev->dev); |
| 373 | } |
| 374 | |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 375 | if (of_get_property(node, "ti,no_idle_on_suspend", NULL)) |
| 376 | omap_device_disable_idle_on_suspend(pdev); |
| 377 | |
| 378 | pdev->dev.pm_domain = &omap_device_pm_domain; |
| 379 | |
| 380 | odbfd_exit1: |
| 381 | kfree(hwmods); |
| 382 | odbfd_exit: |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static int _omap_device_notifier_call(struct notifier_block *nb, |
| 387 | unsigned long event, void *dev) |
| 388 | { |
| 389 | struct platform_device *pdev = to_platform_device(dev); |
Kevin Hilman | e753345 | 2012-07-10 11:13:16 -0700 | [diff] [blame] | 390 | struct omap_device *od; |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 391 | |
| 392 | switch (event) { |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 393 | case BUS_NOTIFY_DEL_DEVICE: |
| 394 | if (pdev->archdata.od) |
| 395 | omap_device_delete(pdev->archdata.od); |
| 396 | break; |
Kevin Hilman | e753345 | 2012-07-10 11:13:16 -0700 | [diff] [blame] | 397 | case BUS_NOTIFY_ADD_DEVICE: |
| 398 | if (pdev->dev.of_node) |
| 399 | omap_device_build_from_dt(pdev); |
| 400 | /* fall through */ |
| 401 | default: |
| 402 | od = to_omap_device(pdev); |
| 403 | if (od) |
| 404 | od->_driver_status = event; |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return NOTIFY_DONE; |
| 408 | } |
| 409 | |
| 410 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 411 | /* Public functions for use by core code */ |
| 412 | |
| 413 | /** |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 414 | * omap_device_get_context_loss_count - get lost context count |
| 415 | * @od: struct omap_device * |
| 416 | * |
| 417 | * Using the primary hwmod, query the context loss count for this |
| 418 | * device. |
| 419 | * |
| 420 | * Callers should consider context for this device lost any time this |
| 421 | * function returns a value different than the value the caller got |
| 422 | * the last time it called this function. |
| 423 | * |
| 424 | * If any hwmods exist for the omap_device assoiated with @pdev, |
| 425 | * return the context loss counter for that hwmod, otherwise return |
| 426 | * zero. |
| 427 | */ |
Tomi Valkeinen | fc01387 | 2011-06-09 16:56:23 +0300 | [diff] [blame] | 428 | int omap_device_get_context_loss_count(struct platform_device *pdev) |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 429 | { |
| 430 | struct omap_device *od; |
| 431 | u32 ret = 0; |
| 432 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 433 | od = to_omap_device(pdev); |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 434 | |
| 435 | if (od->hwmods_cnt) |
| 436 | ret = omap_hwmod_get_context_loss_count(od->hwmods[0]); |
| 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 442 | * omap_device_count_resources - count number of struct resource entries needed |
| 443 | * @od: struct omap_device * |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame^] | 444 | * @flags: Type of resources to include when counting (IRQ/DMA/MEM) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 445 | * |
| 446 | * Count the number of struct resource entries needed for this |
| 447 | * omap_device @od. Used by omap_device_build_ss() to determine how |
| 448 | * much memory to allocate before calling |
| 449 | * omap_device_fill_resources(). Returns the count. |
| 450 | */ |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame^] | 451 | static int omap_device_count_resources(struct omap_device *od, |
| 452 | unsigned long flags) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 453 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 454 | int c = 0; |
| 455 | int i; |
| 456 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 457 | for (i = 0; i < od->hwmods_cnt; i++) |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame^] | 458 | c += omap_hwmod_count_resources(od->hwmods[i], flags); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 459 | |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 460 | pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n", |
| 461 | od->pdev->name, c, od->hwmods_cnt); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 462 | |
| 463 | return c; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * omap_device_fill_resources - fill in array of struct resource |
| 468 | * @od: struct omap_device * |
| 469 | * @res: pointer to an array of struct resource to be filled in |
| 470 | * |
| 471 | * Populate one or more empty struct resource pointed to by @res with |
| 472 | * the resource data for this omap_device @od. Used by |
| 473 | * omap_device_build_ss() after calling omap_device_count_resources(). |
| 474 | * Ideally this function would not be needed at all. If omap_device |
| 475 | * replaces platform_device, then we can specify our own |
| 476 | * get_resource()/ get_irq()/etc functions that use the underlying |
| 477 | * omap_hwmod information. Or if platform_device is extended to use |
| 478 | * subarchitecture-specific function pointers, the various |
| 479 | * platform_device functions can simply call omap_device internal |
| 480 | * functions to get device resources. Hacking around the existing |
| 481 | * platform_device code wastes memory. Returns 0. |
| 482 | */ |
Kevin Hilman | a2a28ad | 2011-07-21 14:14:35 -0700 | [diff] [blame] | 483 | static int omap_device_fill_resources(struct omap_device *od, |
| 484 | struct resource *res) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 485 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 486 | int i, r; |
| 487 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 488 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 489 | r = omap_hwmod_fill_resources(od->hwmods[i], res); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 490 | res += r; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /** |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 497 | * _od_fill_dma_resources - fill in array of struct resource with dma resources |
| 498 | * @od: struct omap_device * |
| 499 | * @res: pointer to an array of struct resource to be filled in |
| 500 | * |
| 501 | * Populate one or more empty struct resource pointed to by @res with |
| 502 | * the dma resource data for this omap_device @od. Used by |
| 503 | * omap_device_alloc() after calling omap_device_count_resources(). |
| 504 | * |
| 505 | * Ideally this function would not be needed at all. If we have |
| 506 | * mechanism to get dma resources from DT. |
| 507 | * |
| 508 | * Returns 0. |
| 509 | */ |
| 510 | static int _od_fill_dma_resources(struct omap_device *od, |
| 511 | struct resource *res) |
| 512 | { |
| 513 | int i, r; |
| 514 | |
| 515 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 516 | r = omap_hwmod_fill_dma_resources(od->hwmods[i], res); |
| 517 | res += r; |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /** |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 524 | * omap_device_alloc - allocate an omap_device |
| 525 | * @pdev: platform_device that will be included in this omap_device |
| 526 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 527 | * @pdata: platform_data ptr to associate with the platform_device |
| 528 | * @pdata_len: amount of memory pointed to by @pdata |
| 529 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 530 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
| 531 | * |
| 532 | * Convenience function for allocating an omap_device structure and filling |
| 533 | * hwmods, resources and pm_latency attributes. |
| 534 | * |
| 535 | * Returns an struct omap_device pointer or ERR_PTR() on error; |
| 536 | */ |
Ohad Ben-Cohen | 993e4fb | 2012-02-20 09:43:29 -0800 | [diff] [blame] | 537 | struct omap_device *omap_device_alloc(struct platform_device *pdev, |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 538 | struct omap_hwmod **ohs, int oh_cnt, |
| 539 | struct omap_device_pm_latency *pm_lats, |
| 540 | int pm_lats_cnt) |
| 541 | { |
| 542 | int ret = -ENOMEM; |
| 543 | struct omap_device *od; |
| 544 | struct resource *res = NULL; |
| 545 | int i, res_count; |
| 546 | struct omap_hwmod **hwmods; |
| 547 | |
| 548 | od = kzalloc(sizeof(struct omap_device), GFP_KERNEL); |
| 549 | if (!od) { |
| 550 | ret = -ENOMEM; |
| 551 | goto oda_exit1; |
| 552 | } |
| 553 | od->hwmods_cnt = oh_cnt; |
| 554 | |
| 555 | hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL); |
| 556 | if (!hwmods) |
| 557 | goto oda_exit2; |
| 558 | |
| 559 | od->hwmods = hwmods; |
| 560 | od->pdev = pdev; |
| 561 | |
Peter Ujfalusi | dad4191 | 2012-11-21 16:15:17 -0700 | [diff] [blame^] | 562 | /* Count all resources for the device */ |
| 563 | res_count = omap_device_count_resources(od, IORESOURCE_IRQ | |
| 564 | IORESOURCE_DMA | |
| 565 | IORESOURCE_MEM); |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 566 | /* |
| 567 | * DT Boot: |
| 568 | * OF framework will construct the resource structure (currently |
| 569 | * does for MEM & IRQ resource) and we should respect/use these |
| 570 | * resources, killing hwmod dependency. |
| 571 | * If pdev->num_resources > 0, we assume that MEM & IRQ resources |
| 572 | * have been allocated by OF layer already (through DTB). |
| 573 | * |
| 574 | * Non-DT Boot: |
| 575 | * Here, pdev->num_resources = 0, and we should get all the |
| 576 | * resources from hwmod. |
| 577 | * |
| 578 | * TODO: Once DMA resource is available from OF layer, we should |
| 579 | * kill filling any resources from hwmod. |
| 580 | */ |
| 581 | if (res_count > pdev->num_resources) { |
| 582 | /* Allocate resources memory to account for new resources */ |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 583 | res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL); |
| 584 | if (!res) |
| 585 | goto oda_exit3; |
| 586 | |
Vaibhav Hiremath | b82b04e | 2012-08-29 15:18:11 +0530 | [diff] [blame] | 587 | /* |
| 588 | * If pdev->num_resources > 0, then assume that, |
| 589 | * MEM and IRQ resources will only come from DT and only |
| 590 | * fill DMA resource from hwmod layer. |
| 591 | */ |
| 592 | if (pdev->num_resources && pdev->resource) { |
| 593 | dev_dbg(&pdev->dev, "%s(): resources already allocated %d\n", |
| 594 | __func__, res_count); |
| 595 | memcpy(res, pdev->resource, |
| 596 | sizeof(struct resource) * pdev->num_resources); |
| 597 | _od_fill_dma_resources(od, &res[pdev->num_resources]); |
| 598 | } else { |
| 599 | dev_dbg(&pdev->dev, "%s(): using resources from hwmod %d\n", |
| 600 | __func__, res_count); |
| 601 | omap_device_fill_resources(od, res); |
| 602 | } |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 603 | |
| 604 | ret = platform_device_add_resources(pdev, res, res_count); |
| 605 | kfree(res); |
| 606 | |
| 607 | if (ret) |
| 608 | goto oda_exit3; |
| 609 | } |
| 610 | |
| 611 | if (!pm_lats) { |
| 612 | pm_lats = omap_default_latency; |
| 613 | pm_lats_cnt = ARRAY_SIZE(omap_default_latency); |
| 614 | } |
| 615 | |
| 616 | od->pm_lats_cnt = pm_lats_cnt; |
| 617 | od->pm_lats = kmemdup(pm_lats, |
| 618 | sizeof(struct omap_device_pm_latency) * pm_lats_cnt, |
| 619 | GFP_KERNEL); |
| 620 | if (!od->pm_lats) |
| 621 | goto oda_exit3; |
| 622 | |
| 623 | pdev->archdata.od = od; |
| 624 | |
| 625 | for (i = 0; i < oh_cnt; i++) { |
| 626 | hwmods[i]->od = od; |
| 627 | _add_hwmod_clocks_clkdev(od, hwmods[i]); |
| 628 | } |
| 629 | |
| 630 | return od; |
| 631 | |
| 632 | oda_exit3: |
| 633 | kfree(hwmods); |
| 634 | oda_exit2: |
| 635 | kfree(od); |
| 636 | oda_exit1: |
| 637 | dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret); |
| 638 | |
| 639 | return ERR_PTR(ret); |
| 640 | } |
| 641 | |
Ohad Ben-Cohen | 993e4fb | 2012-02-20 09:43:29 -0800 | [diff] [blame] | 642 | void omap_device_delete(struct omap_device *od) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 643 | { |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 644 | if (!od) |
| 645 | return; |
| 646 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 647 | od->pdev->archdata.od = NULL; |
| 648 | kfree(od->pm_lats); |
| 649 | kfree(od->hwmods); |
| 650 | kfree(od); |
| 651 | } |
| 652 | |
| 653 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 654 | * omap_device_build - build and register an omap_device with one omap_hwmod |
| 655 | * @pdev_name: name of the platform_device driver to use |
| 656 | * @pdev_id: this platform_device's connection ID |
| 657 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 658 | * @pdata: platform_data ptr to associate with the platform_device |
| 659 | * @pdata_len: amount of memory pointed to by @pdata |
| 660 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 661 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 662 | * @is_early_device: should the device be registered as an early device or not |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 663 | * |
| 664 | * Convenience function for building and registering a single |
| 665 | * omap_device record, which in turn builds and registers a |
| 666 | * platform_device record. See omap_device_build_ss() for more |
| 667 | * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise, |
| 668 | * passes along the return value of omap_device_build_ss(). |
| 669 | */ |
Kevin Hilman | 9cf793f | 2012-02-20 09:43:30 -0800 | [diff] [blame] | 670 | struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id, |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 671 | struct omap_hwmod *oh, void *pdata, |
| 672 | int pdata_len, |
| 673 | struct omap_device_pm_latency *pm_lats, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 674 | int pm_lats_cnt, int is_early_device) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 675 | { |
| 676 | struct omap_hwmod *ohs[] = { oh }; |
| 677 | |
| 678 | if (!oh) |
| 679 | return ERR_PTR(-EINVAL); |
| 680 | |
| 681 | return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 682 | pdata_len, pm_lats, pm_lats_cnt, |
| 683 | is_early_device); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
| 687 | * omap_device_build_ss - build and register an omap_device with multiple hwmods |
| 688 | * @pdev_name: name of the platform_device driver to use |
| 689 | * @pdev_id: this platform_device's connection ID |
| 690 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 691 | * @pdata: platform_data ptr to associate with the platform_device |
| 692 | * @pdata_len: amount of memory pointed to by @pdata |
| 693 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 694 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 695 | * @is_early_device: should the device be registered as an early device or not |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 696 | * |
| 697 | * Convenience function for building and registering an omap_device |
| 698 | * subsystem record. Subsystem records consist of multiple |
| 699 | * omap_hwmods. This function in turn builds and registers a |
| 700 | * platform_device record. Returns an ERR_PTR() on error, or passes |
| 701 | * along the return value of omap_device_register(). |
| 702 | */ |
Kevin Hilman | 9cf793f | 2012-02-20 09:43:30 -0800 | [diff] [blame] | 703 | struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id, |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 704 | struct omap_hwmod **ohs, int oh_cnt, |
| 705 | void *pdata, int pdata_len, |
| 706 | struct omap_device_pm_latency *pm_lats, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 707 | int pm_lats_cnt, int is_early_device) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 708 | { |
| 709 | int ret = -ENOMEM; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 710 | struct platform_device *pdev; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 711 | struct omap_device *od; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 712 | |
| 713 | if (!ohs || oh_cnt == 0 || !pdev_name) |
| 714 | return ERR_PTR(-EINVAL); |
| 715 | |
| 716 | if (!pdata && pdata_len > 0) |
| 717 | return ERR_PTR(-EINVAL); |
| 718 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 719 | pdev = platform_device_alloc(pdev_name, pdev_id); |
| 720 | if (!pdev) { |
| 721 | ret = -ENOMEM; |
| 722 | goto odbs_exit; |
| 723 | } |
| 724 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 725 | /* Set the dev_name early to allow dev_xxx in omap_device_alloc */ |
| 726 | if (pdev->id != -1) |
| 727 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
| 728 | else |
| 729 | dev_set_name(&pdev->dev, "%s", pdev->name); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 730 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 731 | od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt); |
Wei Yongjun | a87e662 | 2012-09-21 14:32:04 +0800 | [diff] [blame] | 732 | if (IS_ERR(od)) |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 733 | goto odbs_exit1; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 734 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 735 | ret = platform_device_add_data(pdev, pdata, pdata_len); |
Artem Bityutskiy | 49b368a | 2010-07-12 13:38:07 +0000 | [diff] [blame] | 736 | if (ret) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 737 | goto odbs_exit2; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 738 | |
| 739 | if (is_early_device) |
| 740 | ret = omap_early_device_register(pdev); |
| 741 | else |
| 742 | ret = omap_device_register(pdev); |
| 743 | if (ret) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 744 | goto odbs_exit2; |
Kevin Hilman | 0656358 | 2010-07-26 16:34:30 -0600 | [diff] [blame] | 745 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 746 | return pdev; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 747 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 748 | odbs_exit2: |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame] | 749 | omap_device_delete(od); |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 750 | odbs_exit1: |
| 751 | platform_device_put(pdev); |
| 752 | odbs_exit: |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 753 | |
| 754 | pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret); |
| 755 | |
| 756 | return ERR_PTR(ret); |
| 757 | } |
| 758 | |
| 759 | /** |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 760 | * omap_early_device_register - register an omap_device as an early platform |
| 761 | * device. |
| 762 | * @od: struct omap_device * to register |
| 763 | * |
| 764 | * Register the omap_device structure. This currently just calls |
| 765 | * platform_early_add_device() on the underlying platform_device. |
| 766 | * Returns 0 by default. |
| 767 | */ |
Kevin Hilman | 9cf793f | 2012-02-20 09:43:30 -0800 | [diff] [blame] | 768 | static int __init omap_early_device_register(struct platform_device *pdev) |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 769 | { |
| 770 | struct platform_device *devices[1]; |
| 771 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 772 | devices[0] = pdev; |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 773 | early_platform_add_devices(devices, 1); |
| 774 | return 0; |
| 775 | } |
| 776 | |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 777 | #ifdef CONFIG_PM_RUNTIME |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 778 | static int _od_runtime_suspend(struct device *dev) |
| 779 | { |
| 780 | struct platform_device *pdev = to_platform_device(dev); |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 781 | int ret; |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 782 | |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 783 | ret = pm_generic_runtime_suspend(dev); |
| 784 | |
| 785 | if (!ret) |
| 786 | omap_device_idle(pdev); |
| 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | static int _od_runtime_idle(struct device *dev) |
| 792 | { |
| 793 | return pm_generic_runtime_idle(dev); |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static int _od_runtime_resume(struct device *dev) |
| 797 | { |
| 798 | struct platform_device *pdev = to_platform_device(dev); |
| 799 | |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 800 | omap_device_enable(pdev); |
| 801 | |
| 802 | return pm_generic_runtime_resume(dev); |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 803 | } |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 804 | #endif |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 805 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 806 | #ifdef CONFIG_SUSPEND |
| 807 | static int _od_suspend_noirq(struct device *dev) |
| 808 | { |
| 809 | struct platform_device *pdev = to_platform_device(dev); |
| 810 | struct omap_device *od = to_omap_device(pdev); |
| 811 | int ret; |
| 812 | |
Kevin Hilman | 72bb6f9 | 2012-07-10 15:29:04 -0700 | [diff] [blame] | 813 | /* Don't attempt late suspend on a driver that is not bound */ |
| 814 | if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) |
| 815 | return 0; |
| 816 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 817 | ret = pm_generic_suspend_noirq(dev); |
| 818 | |
| 819 | if (!ret && !pm_runtime_status_suspended(dev)) { |
| 820 | if (pm_generic_runtime_suspend(dev) == 0) { |
Paul Walmsley | b7c39a3 | 2012-03-03 13:15:33 -0700 | [diff] [blame] | 821 | if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)) |
| 822 | omap_device_idle(pdev); |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 823 | od->flags |= OMAP_DEVICE_SUSPENDED; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | static int _od_resume_noirq(struct device *dev) |
| 831 | { |
| 832 | struct platform_device *pdev = to_platform_device(dev); |
| 833 | struct omap_device *od = to_omap_device(pdev); |
| 834 | |
| 835 | if ((od->flags & OMAP_DEVICE_SUSPENDED) && |
| 836 | !pm_runtime_status_suspended(dev)) { |
| 837 | od->flags &= ~OMAP_DEVICE_SUSPENDED; |
Paul Walmsley | b7c39a3 | 2012-03-03 13:15:33 -0700 | [diff] [blame] | 838 | if (!(od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND)) |
| 839 | omap_device_enable(pdev); |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 840 | pm_generic_runtime_resume(dev); |
| 841 | } |
| 842 | |
| 843 | return pm_generic_resume_noirq(dev); |
| 844 | } |
Kevin Hilman | 126caf1 | 2011-09-01 10:59:36 -0700 | [diff] [blame] | 845 | #else |
| 846 | #define _od_suspend_noirq NULL |
| 847 | #define _od_resume_noirq NULL |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 848 | #endif |
| 849 | |
Kevin Hilman | 3ec2dec | 2012-02-15 11:47:45 -0800 | [diff] [blame] | 850 | struct dev_pm_domain omap_device_pm_domain = { |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 851 | .ops = { |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 852 | SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume, |
| 853 | _od_runtime_idle) |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 854 | USE_PLATFORM_PM_SLEEP_OPS |
Kevin Hilman | ff35336 | 2011-08-25 15:31:14 +0200 | [diff] [blame] | 855 | .suspend_noirq = _od_suspend_noirq, |
| 856 | .resume_noirq = _od_resume_noirq, |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 857 | } |
| 858 | }; |
| 859 | |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 860 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 861 | * omap_device_register - register an omap_device with one omap_hwmod |
| 862 | * @od: struct omap_device * to register |
| 863 | * |
| 864 | * Register the omap_device structure. This currently just calls |
| 865 | * platform_device_register() on the underlying platform_device. |
| 866 | * Returns the return value of platform_device_register(). |
| 867 | */ |
Ohad Ben-Cohen | 993e4fb | 2012-02-20 09:43:29 -0800 | [diff] [blame] | 868 | int omap_device_register(struct platform_device *pdev) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 869 | { |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 870 | pr_debug("omap_device: %s: registering\n", pdev->name); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 871 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 872 | pdev->dev.pm_domain = &omap_device_pm_domain; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 873 | return platform_device_add(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
| 877 | /* Public functions for use by device drivers through struct platform_data */ |
| 878 | |
| 879 | /** |
| 880 | * omap_device_enable - fully activate an omap_device |
| 881 | * @od: struct omap_device * to activate |
| 882 | * |
| 883 | * Do whatever is necessary for the hwmods underlying omap_device @od |
| 884 | * to be accessible and ready to operate. This generally involves |
| 885 | * enabling clocks, setting SYSCONFIG registers; and in the future may |
| 886 | * involve remuxing pins. Device drivers should call this function |
| 887 | * (through platform_data function pointers) where they would normally |
| 888 | * enable clocks, etc. Returns -EINVAL if called when the omap_device |
| 889 | * is already enabled, or passes along the return value of |
| 890 | * _omap_device_activate(). |
| 891 | */ |
| 892 | int omap_device_enable(struct platform_device *pdev) |
| 893 | { |
| 894 | int ret; |
| 895 | struct omap_device *od; |
| 896 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 897 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 898 | |
| 899 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 900 | dev_warn(&pdev->dev, |
| 901 | "omap_device: %s() called from invalid state %d\n", |
| 902 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 903 | return -EINVAL; |
| 904 | } |
| 905 | |
| 906 | /* Enable everything if we're enabling this device from scratch */ |
| 907 | if (od->_state == OMAP_DEVICE_STATE_UNKNOWN) |
| 908 | od->pm_lat_level = od->pm_lats_cnt; |
| 909 | |
| 910 | ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT); |
| 911 | |
| 912 | od->dev_wakeup_lat = 0; |
Kevin Hilman | 5f1b6ef | 2009-12-08 16:34:22 -0700 | [diff] [blame] | 913 | od->_dev_wakeup_lat_limit = UINT_MAX; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 914 | od->_state = OMAP_DEVICE_STATE_ENABLED; |
| 915 | |
| 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * omap_device_idle - idle an omap_device |
| 921 | * @od: struct omap_device * to idle |
| 922 | * |
| 923 | * Idle omap_device @od by calling as many .deactivate_func() entries |
| 924 | * in the omap_device's pm_lats table as is possible without exceeding |
| 925 | * the device's maximum wakeup latency limit, pm_lat_limit. Device |
| 926 | * drivers should call this function (through platform_data function |
| 927 | * pointers) where they would normally disable clocks after operations |
| 928 | * complete, etc.. Returns -EINVAL if the omap_device is not |
| 929 | * currently enabled, or passes along the return value of |
| 930 | * _omap_device_deactivate(). |
| 931 | */ |
| 932 | int omap_device_idle(struct platform_device *pdev) |
| 933 | { |
| 934 | int ret; |
| 935 | struct omap_device *od; |
| 936 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 937 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 938 | |
| 939 | if (od->_state != OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 940 | dev_warn(&pdev->dev, |
| 941 | "omap_device: %s() called from invalid state %d\n", |
| 942 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 943 | return -EINVAL; |
| 944 | } |
| 945 | |
| 946 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); |
| 947 | |
| 948 | od->_state = OMAP_DEVICE_STATE_IDLE; |
| 949 | |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * omap_device_shutdown - shut down an omap_device |
| 955 | * @od: struct omap_device * to shut down |
| 956 | * |
| 957 | * Shut down omap_device @od by calling all .deactivate_func() entries |
| 958 | * in the omap_device's pm_lats table and then shutting down all of |
| 959 | * the underlying omap_hwmods. Used when a device is being "removed" |
| 960 | * or a device driver is being unloaded. Returns -EINVAL if the |
| 961 | * omap_device is not currently enabled or idle, or passes along the |
| 962 | * return value of _omap_device_deactivate(). |
| 963 | */ |
| 964 | int omap_device_shutdown(struct platform_device *pdev) |
| 965 | { |
| 966 | int ret, i; |
| 967 | struct omap_device *od; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 968 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 969 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 970 | |
| 971 | if (od->_state != OMAP_DEVICE_STATE_ENABLED && |
| 972 | od->_state != OMAP_DEVICE_STATE_IDLE) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 973 | dev_warn(&pdev->dev, |
| 974 | "omap_device: %s() called from invalid state %d\n", |
| 975 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 976 | return -EINVAL; |
| 977 | } |
| 978 | |
| 979 | ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT); |
| 980 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 981 | for (i = 0; i < od->hwmods_cnt; i++) |
| 982 | omap_hwmod_shutdown(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 983 | |
| 984 | od->_state = OMAP_DEVICE_STATE_SHUTDOWN; |
| 985 | |
| 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | /** |
Omar Ramirez Luna | 8bb9fde | 2012-09-23 17:28:18 -0600 | [diff] [blame] | 990 | * omap_device_assert_hardreset - set a device's hardreset line |
| 991 | * @pdev: struct platform_device * to reset |
| 992 | * @name: const char * name of the reset line |
| 993 | * |
| 994 | * Set the hardreset line identified by @name on the IP blocks |
| 995 | * associated with the hwmods backing the platform_device @pdev. All |
| 996 | * of the hwmods associated with @pdev must have the same hardreset |
| 997 | * line linked to them for this to work. Passes along the return value |
| 998 | * of omap_hwmod_assert_hardreset() in the event of any failure, or |
| 999 | * returns 0 upon success. |
| 1000 | */ |
| 1001 | int omap_device_assert_hardreset(struct platform_device *pdev, const char *name) |
| 1002 | { |
| 1003 | struct omap_device *od = to_omap_device(pdev); |
| 1004 | int ret = 0; |
| 1005 | int i; |
| 1006 | |
| 1007 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 1008 | ret = omap_hwmod_assert_hardreset(od->hwmods[i], name); |
| 1009 | if (ret) |
| 1010 | break; |
| 1011 | } |
| 1012 | |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * omap_device_deassert_hardreset - release a device's hardreset line |
| 1018 | * @pdev: struct platform_device * to reset |
| 1019 | * @name: const char * name of the reset line |
| 1020 | * |
| 1021 | * Release the hardreset line identified by @name on the IP blocks |
| 1022 | * associated with the hwmods backing the platform_device @pdev. All |
| 1023 | * of the hwmods associated with @pdev must have the same hardreset |
| 1024 | * line linked to them for this to work. Passes along the return |
| 1025 | * value of omap_hwmod_deassert_hardreset() in the event of any |
| 1026 | * failure, or returns 0 upon success. |
| 1027 | */ |
| 1028 | int omap_device_deassert_hardreset(struct platform_device *pdev, |
| 1029 | const char *name) |
| 1030 | { |
| 1031 | struct omap_device *od = to_omap_device(pdev); |
| 1032 | int ret = 0; |
| 1033 | int i; |
| 1034 | |
| 1035 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 1036 | ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name); |
| 1037 | if (ret) |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1045 | * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim |
| 1046 | * @od: struct omap_device * |
| 1047 | * |
| 1048 | * When a device's maximum wakeup latency limit changes, call some of |
| 1049 | * the .activate_func or .deactivate_func function pointers in the |
| 1050 | * omap_device's pm_lats array to ensure that the device's maximum |
| 1051 | * wakeup latency is less than or equal to the new latency limit. |
| 1052 | * Intended to be called by OMAP PM code whenever a device's maximum |
| 1053 | * wakeup latency limit changes (e.g., via |
| 1054 | * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be |
| 1055 | * done (e.g., if the omap_device is not currently idle, or if the |
| 1056 | * wakeup latency is already current with the new limit) or passes |
| 1057 | * along the return value of _omap_device_deactivate() or |
| 1058 | * _omap_device_activate(). |
| 1059 | */ |
| 1060 | int omap_device_align_pm_lat(struct platform_device *pdev, |
| 1061 | u32 new_wakeup_lat_limit) |
| 1062 | { |
| 1063 | int ret = -EINVAL; |
| 1064 | struct omap_device *od; |
| 1065 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 1066 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1067 | |
| 1068 | if (new_wakeup_lat_limit == od->dev_wakeup_lat) |
| 1069 | return 0; |
| 1070 | |
| 1071 | od->_dev_wakeup_lat_limit = new_wakeup_lat_limit; |
| 1072 | |
| 1073 | if (od->_state != OMAP_DEVICE_STATE_IDLE) |
| 1074 | return 0; |
| 1075 | else if (new_wakeup_lat_limit > od->dev_wakeup_lat) |
| 1076 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); |
| 1077 | else if (new_wakeup_lat_limit < od->dev_wakeup_lat) |
| 1078 | ret = _omap_device_activate(od, USE_WAKEUP_LAT); |
| 1079 | |
| 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1084 | * omap_device_get_pwrdm - return the powerdomain * associated with @od |
| 1085 | * @od: struct omap_device * |
| 1086 | * |
| 1087 | * Return the powerdomain associated with the first underlying |
| 1088 | * omap_hwmod for this omap_device. Intended for use by core OMAP PM |
| 1089 | * code. Returns NULL on error or a struct powerdomain * upon |
| 1090 | * success. |
| 1091 | */ |
| 1092 | struct powerdomain *omap_device_get_pwrdm(struct omap_device *od) |
| 1093 | { |
| 1094 | /* |
| 1095 | * XXX Assumes that all omap_hwmod powerdomains are identical. |
| 1096 | * This may not necessarily be true. There should be a sanity |
| 1097 | * check in here to WARN() if any difference appears. |
| 1098 | */ |
| 1099 | if (!od->hwmods_cnt) |
| 1100 | return NULL; |
| 1101 | |
| 1102 | return omap_hwmod_get_pwrdm(od->hwmods[0]); |
| 1103 | } |
| 1104 | |
Paul Walmsley | db2a60b | 2010-07-26 16:34:33 -0600 | [diff] [blame] | 1105 | /** |
| 1106 | * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base |
| 1107 | * @od: struct omap_device * |
| 1108 | * |
| 1109 | * Return the MPU's virtual address for the base of the hwmod, from |
| 1110 | * the ioremap() that the hwmod code does. Only valid if there is one |
| 1111 | * hwmod associated with this device. Returns NULL if there are zero |
| 1112 | * or more than one hwmods associated with this omap_device; |
| 1113 | * otherwise, passes along the return value from |
| 1114 | * omap_hwmod_get_mpu_rt_va(). |
| 1115 | */ |
| 1116 | void __iomem *omap_device_get_rt_va(struct omap_device *od) |
| 1117 | { |
| 1118 | if (od->hwmods_cnt != 1) |
| 1119 | return NULL; |
| 1120 | |
| 1121 | return omap_hwmod_get_mpu_rt_va(od->hwmods[0]); |
| 1122 | } |
| 1123 | |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 1124 | /** |
| 1125 | * omap_device_get_by_hwmod_name() - convert a hwmod name to |
| 1126 | * device pointer. |
| 1127 | * @oh_name: name of the hwmod device |
| 1128 | * |
| 1129 | * Returns back a struct device * pointer associated with a hwmod |
| 1130 | * device represented by a hwmod_name |
| 1131 | */ |
| 1132 | struct device *omap_device_get_by_hwmod_name(const char *oh_name) |
| 1133 | { |
| 1134 | struct omap_hwmod *oh; |
| 1135 | |
| 1136 | if (!oh_name) { |
| 1137 | WARN(1, "%s: no hwmod name!\n", __func__); |
| 1138 | return ERR_PTR(-EINVAL); |
| 1139 | } |
| 1140 | |
| 1141 | oh = omap_hwmod_lookup(oh_name); |
| 1142 | if (IS_ERR_OR_NULL(oh)) { |
| 1143 | WARN(1, "%s: no hwmod for %s\n", __func__, |
| 1144 | oh_name); |
| 1145 | return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV); |
| 1146 | } |
| 1147 | if (IS_ERR_OR_NULL(oh->od)) { |
| 1148 | WARN(1, "%s: no omap_device for %s\n", __func__, |
| 1149 | oh_name); |
| 1150 | return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV); |
| 1151 | } |
| 1152 | |
| 1153 | if (IS_ERR_OR_NULL(oh->od->pdev)) |
| 1154 | return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV); |
| 1155 | |
| 1156 | return &oh->od->pdev->dev; |
| 1157 | } |
| 1158 | EXPORT_SYMBOL(omap_device_get_by_hwmod_name); |
| 1159 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1160 | /* |
| 1161 | * Public functions intended for use in omap_device_pm_latency |
| 1162 | * .activate_func and .deactivate_func function pointers |
| 1163 | */ |
| 1164 | |
| 1165 | /** |
| 1166 | * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods |
| 1167 | * @od: struct omap_device *od |
| 1168 | * |
| 1169 | * Enable all underlying hwmods. Returns 0. |
| 1170 | */ |
| 1171 | int omap_device_enable_hwmods(struct omap_device *od) |
| 1172 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1173 | int i; |
| 1174 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 1175 | for (i = 0; i < od->hwmods_cnt; i++) |
| 1176 | omap_hwmod_enable(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1177 | |
| 1178 | /* XXX pass along return value here? */ |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods |
| 1184 | * @od: struct omap_device *od |
| 1185 | * |
| 1186 | * Idle all underlying hwmods. Returns 0. |
| 1187 | */ |
| 1188 | int omap_device_idle_hwmods(struct omap_device *od) |
| 1189 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1190 | int i; |
| 1191 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 1192 | for (i = 0; i < od->hwmods_cnt; i++) |
| 1193 | omap_hwmod_idle(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1194 | |
| 1195 | /* XXX pass along return value here? */ |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * omap_device_disable_clocks - disable all main and interface clocks |
| 1201 | * @od: struct omap_device *od |
| 1202 | * |
| 1203 | * Disable the main functional clock and interface clock for all of the |
| 1204 | * omap_hwmods associated with the omap_device. Returns 0. |
| 1205 | */ |
| 1206 | int omap_device_disable_clocks(struct omap_device *od) |
| 1207 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1208 | int i; |
| 1209 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 1210 | for (i = 0; i < od->hwmods_cnt; i++) |
| 1211 | omap_hwmod_disable_clocks(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1212 | |
| 1213 | /* XXX pass along return value here? */ |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * omap_device_enable_clocks - enable all main and interface clocks |
| 1219 | * @od: struct omap_device *od |
| 1220 | * |
| 1221 | * Enable the main functional clock and interface clock for all of the |
| 1222 | * omap_hwmods associated with the omap_device. Returns 0. |
| 1223 | */ |
| 1224 | int omap_device_enable_clocks(struct omap_device *od) |
| 1225 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1226 | int i; |
| 1227 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 1228 | for (i = 0; i < od->hwmods_cnt; i++) |
| 1229 | omap_hwmod_enable_clocks(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1230 | |
| 1231 | /* XXX pass along return value here? */ |
| 1232 | return 0; |
| 1233 | } |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 1234 | |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 1235 | static struct notifier_block platform_nb = { |
| 1236 | .notifier_call = _omap_device_notifier_call, |
| 1237 | }; |
| 1238 | |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 1239 | static int __init omap_device_init(void) |
| 1240 | { |
Benoit Cousson | dc2d07e | 2011-08-10 13:32:08 +0200 | [diff] [blame] | 1241 | bus_register_notifier(&platform_bus_type, &platform_nb); |
Kevin Hilman | 3ec2dec | 2012-02-15 11:47:45 -0800 | [diff] [blame] | 1242 | return 0; |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 1243 | } |
| 1244 | core_initcall(omap_device_init); |
Kevin Hilman | 9634c8d | 2012-07-10 15:06:11 -0700 | [diff] [blame] | 1245 | |
| 1246 | /** |
| 1247 | * omap_device_late_idle - idle devices without drivers |
| 1248 | * @dev: struct device * associated with omap_device |
| 1249 | * @data: unused |
| 1250 | * |
| 1251 | * Check the driver bound status of this device, and idle it |
| 1252 | * if there is no driver attached. |
| 1253 | */ |
| 1254 | static int __init omap_device_late_idle(struct device *dev, void *data) |
| 1255 | { |
| 1256 | struct platform_device *pdev = to_platform_device(dev); |
| 1257 | struct omap_device *od = to_omap_device(pdev); |
| 1258 | |
| 1259 | if (!od) |
| 1260 | return 0; |
| 1261 | |
| 1262 | /* |
| 1263 | * If omap_device state is enabled, but has no driver bound, |
| 1264 | * idle it. |
| 1265 | */ |
| 1266 | if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) { |
| 1267 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { |
| 1268 | dev_warn(dev, "%s: enabled but no driver. Idling\n", |
| 1269 | __func__); |
| 1270 | omap_device_idle(pdev); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int __init omap_device_late_init(void) |
| 1278 | { |
| 1279 | bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | late_initcall(omap_device_late_init); |