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> |
| 81 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 82 | #include <linux/slab.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 83 | #include <linux/err.h> |
| 84 | #include <linux/io.h> |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 85 | #include <linux/clk.h> |
Rajendra Nayak | da0653f | 2011-02-25 15:40:21 -0700 | [diff] [blame] | 86 | #include <linux/clkdev.h> |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 87 | #include <linux/pm_runtime.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 88 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 89 | #include <plat/omap_device.h> |
| 90 | #include <plat/omap_hwmod.h> |
Rajendra Nayak | da0653f | 2011-02-25 15:40:21 -0700 | [diff] [blame] | 91 | #include <plat/clock.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 92 | |
| 93 | /* These parameters are passed to _omap_device_{de,}activate() */ |
| 94 | #define USE_WAKEUP_LAT 0 |
| 95 | #define IGNORE_WAKEUP_LAT 1 |
| 96 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 97 | static int omap_device_register(struct platform_device *pdev); |
| 98 | static int omap_early_device_register(struct platform_device *pdev); |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 99 | static struct omap_device *omap_device_alloc(struct platform_device *pdev, |
| 100 | struct omap_hwmod **ohs, int oh_cnt, |
| 101 | struct omap_device_pm_latency *pm_lats, |
| 102 | int pm_lats_cnt); |
| 103 | |
Kevin Hilman | a2a28ad | 2011-07-21 14:14:35 -0700 | [diff] [blame] | 104 | |
Benoit Cousson | b7b5bc9 | 2011-08-09 16:54:19 +0200 | [diff] [blame] | 105 | static struct omap_device_pm_latency omap_default_latency[] = { |
| 106 | { |
| 107 | .deactivate_func = omap_device_idle_hwmods, |
| 108 | .activate_func = omap_device_enable_hwmods, |
| 109 | .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST, |
| 110 | } |
| 111 | }; |
| 112 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 113 | /* Private functions */ |
| 114 | |
| 115 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 116 | * _omap_device_activate - increase device readiness |
| 117 | * @od: struct omap_device * |
| 118 | * @ignore_lat: increase to latency target (0) or full readiness (1)? |
| 119 | * |
| 120 | * Increase readiness of omap_device @od (thus decreasing device |
| 121 | * wakeup latency, but consuming more power). If @ignore_lat is |
| 122 | * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise, |
| 123 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup |
| 124 | * latency is greater than the requested maximum wakeup latency, step |
| 125 | * backwards in the omap_device_pm_latency table to ensure the |
| 126 | * device's maximum wakeup latency is less than or equal to the |
| 127 | * requested maximum wakeup latency. Returns 0. |
| 128 | */ |
| 129 | static int _omap_device_activate(struct omap_device *od, u8 ignore_lat) |
| 130 | { |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 131 | struct timespec a, b, c; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 132 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 133 | dev_dbg(&od->pdev->dev, "omap_device: activating\n"); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 134 | |
| 135 | while (od->pm_lat_level > 0) { |
| 136 | struct omap_device_pm_latency *odpl; |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 137 | unsigned long long act_lat = 0; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 138 | |
| 139 | od->pm_lat_level--; |
| 140 | |
| 141 | odpl = od->pm_lats + od->pm_lat_level; |
| 142 | |
| 143 | if (!ignore_lat && |
| 144 | (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit)) |
| 145 | break; |
| 146 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 147 | read_persistent_clock(&a); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 148 | |
| 149 | /* XXX check return code */ |
| 150 | odpl->activate_func(od); |
| 151 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 152 | read_persistent_clock(&b); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 153 | |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 154 | c = timespec_sub(b, a); |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 155 | act_lat = timespec_to_ns(&c); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 156 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 157 | dev_dbg(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 158 | "omap_device: pm_lat %d: activate: elapsed time " |
| 159 | "%llu nsec\n", od->pm_lat_level, act_lat); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 160 | |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 161 | if (act_lat > odpl->activate_lat) { |
| 162 | odpl->activate_lat_worst = act_lat; |
| 163 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { |
| 164 | odpl->activate_lat = act_lat; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 165 | dev_dbg(&od->pdev->dev, |
Grazvydas Ignotas | 47c3e5d | 2011-07-25 16:18:24 +0300 | [diff] [blame] | 166 | "new worst case activate latency " |
| 167 | "%d: %llu\n", |
| 168 | od->pm_lat_level, act_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 169 | } else |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 170 | dev_warn(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 171 | "activate latency %d " |
| 172 | "higher than exptected. (%llu > %d)\n", |
| 173 | od->pm_lat_level, act_lat, |
| 174 | odpl->activate_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 175 | } |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 176 | |
| 177 | od->dev_wakeup_lat -= odpl->activate_lat; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * _omap_device_deactivate - decrease device readiness |
| 185 | * @od: struct omap_device * |
| 186 | * @ignore_lat: decrease to latency target (0) or full inactivity (1)? |
| 187 | * |
| 188 | * Decrease readiness of omap_device @od (thus increasing device |
| 189 | * wakeup latency, but conserving power). If @ignore_lat is |
| 190 | * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise, |
| 191 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup |
| 192 | * latency is less than the requested maximum wakeup latency, step |
| 193 | * forwards in the omap_device_pm_latency table to ensure the device's |
| 194 | * maximum wakeup latency is less than or equal to the requested |
| 195 | * maximum wakeup latency. Returns 0. |
| 196 | */ |
| 197 | static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat) |
| 198 | { |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 199 | struct timespec a, b, c; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 200 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 201 | dev_dbg(&od->pdev->dev, "omap_device: deactivating\n"); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 202 | |
| 203 | while (od->pm_lat_level < od->pm_lats_cnt) { |
| 204 | struct omap_device_pm_latency *odpl; |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 205 | unsigned long long deact_lat = 0; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 206 | |
| 207 | odpl = od->pm_lats + od->pm_lat_level; |
| 208 | |
| 209 | if (!ignore_lat && |
| 210 | ((od->dev_wakeup_lat + odpl->activate_lat) > |
| 211 | od->_dev_wakeup_lat_limit)) |
| 212 | break; |
| 213 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 214 | read_persistent_clock(&a); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 215 | |
| 216 | /* XXX check return code */ |
| 217 | odpl->deactivate_func(od); |
| 218 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 219 | read_persistent_clock(&b); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 220 | |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 221 | c = timespec_sub(b, a); |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 222 | deact_lat = timespec_to_ns(&c); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 223 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 224 | dev_dbg(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 225 | "omap_device: pm_lat %d: deactivate: elapsed time " |
| 226 | "%llu nsec\n", od->pm_lat_level, deact_lat); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 227 | |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 228 | if (deact_lat > odpl->deactivate_lat) { |
| 229 | odpl->deactivate_lat_worst = deact_lat; |
| 230 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { |
| 231 | odpl->deactivate_lat = deact_lat; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 232 | dev_dbg(&od->pdev->dev, |
Grazvydas Ignotas | 47c3e5d | 2011-07-25 16:18:24 +0300 | [diff] [blame] | 233 | "new worst case deactivate latency " |
| 234 | "%d: %llu\n", |
| 235 | od->pm_lat_level, deact_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 236 | } else |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 237 | dev_warn(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 238 | "deactivate latency %d " |
| 239 | "higher than exptected. (%llu > %d)\n", |
| 240 | od->pm_lat_level, deact_lat, |
| 241 | odpl->deactivate_lat); |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 244 | od->dev_wakeup_lat += odpl->activate_lat; |
| 245 | |
| 246 | od->pm_lat_level++; |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 252 | static void _add_clkdev(struct omap_device *od, const char *clk_alias, |
| 253 | const char *clk_name) |
| 254 | { |
| 255 | struct clk *r; |
| 256 | struct clk_lookup *l; |
| 257 | |
| 258 | if (!clk_alias || !clk_name) |
| 259 | return; |
| 260 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 261 | 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] | 262 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 263 | r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias); |
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_warn(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 266 | "alias %s already exists\n", clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 267 | clk_put(r); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | r = omap_clk_get_by_name(clk_name); |
| 272 | if (IS_ERR(r)) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 273 | dev_err(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 274 | "omap_clk_get_by_name for %s failed\n", clk_name); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 278 | l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev)); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 279 | if (!l) { |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 280 | dev_err(&od->pdev->dev, |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 281 | "clkdev_alloc for %s failed\n", clk_alias); |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
| 285 | clkdev_add(l); |
| 286 | } |
| 287 | |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 288 | /** |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 289 | * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks |
| 290 | * and main clock |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 291 | * @od: struct omap_device *od |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 292 | * @oh: struct omap_hwmod *oh |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 293 | * |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 294 | * For the main clock and every optional clock present per hwmod per |
| 295 | * omap_device, this function adds an entry in the clkdev table of the |
| 296 | * 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] | 297 | * |
| 298 | * The function is called from inside omap_device_build_ss(), after |
| 299 | * omap_device_register. |
| 300 | * |
| 301 | * This allows drivers to get a pointer to its optional clocks based on its role |
| 302 | * by calling clk_get(<dev*>, <role>). |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 303 | * In the case of the main clock, a "fck" alias is used. |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 304 | * |
| 305 | * No return value. |
| 306 | */ |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 307 | static void _add_hwmod_clocks_clkdev(struct omap_device *od, |
| 308 | struct omap_hwmod *oh) |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 309 | { |
| 310 | int i; |
| 311 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 312 | _add_clkdev(od, "fck", oh->main_clk); |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 313 | |
Benoit Cousson | bf1e077 | 2011-07-10 05:54:12 -0600 | [diff] [blame] | 314 | for (i = 0; i < oh->opt_clks_cnt; i++) |
| 315 | _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] | 316 | } |
| 317 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 318 | |
| 319 | /* Public functions for use by core code */ |
| 320 | |
| 321 | /** |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 322 | * omap_device_get_context_loss_count - get lost context count |
| 323 | * @od: struct omap_device * |
| 324 | * |
| 325 | * Using the primary hwmod, query the context loss count for this |
| 326 | * device. |
| 327 | * |
| 328 | * Callers should consider context for this device lost any time this |
| 329 | * function returns a value different than the value the caller got |
| 330 | * the last time it called this function. |
| 331 | * |
| 332 | * If any hwmods exist for the omap_device assoiated with @pdev, |
| 333 | * return the context loss counter for that hwmod, otherwise return |
| 334 | * zero. |
| 335 | */ |
| 336 | u32 omap_device_get_context_loss_count(struct platform_device *pdev) |
| 337 | { |
| 338 | struct omap_device *od; |
| 339 | u32 ret = 0; |
| 340 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 341 | od = to_omap_device(pdev); |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 342 | |
| 343 | if (od->hwmods_cnt) |
| 344 | ret = omap_hwmod_get_context_loss_count(od->hwmods[0]); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 350 | * omap_device_count_resources - count number of struct resource entries needed |
| 351 | * @od: struct omap_device * |
| 352 | * |
| 353 | * Count the number of struct resource entries needed for this |
| 354 | * omap_device @od. Used by omap_device_build_ss() to determine how |
| 355 | * much memory to allocate before calling |
| 356 | * omap_device_fill_resources(). Returns the count. |
| 357 | */ |
Kevin Hilman | a2a28ad | 2011-07-21 14:14:35 -0700 | [diff] [blame] | 358 | static int omap_device_count_resources(struct omap_device *od) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 359 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 360 | int c = 0; |
| 361 | int i; |
| 362 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 363 | for (i = 0; i < od->hwmods_cnt; i++) |
| 364 | c += omap_hwmod_count_resources(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 365 | |
| 366 | pr_debug("omap_device: %s: counted %d total resources across %d " |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 367 | "hwmods\n", od->pdev->name, c, od->hwmods_cnt); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 368 | |
| 369 | return c; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * omap_device_fill_resources - fill in array of struct resource |
| 374 | * @od: struct omap_device * |
| 375 | * @res: pointer to an array of struct resource to be filled in |
| 376 | * |
| 377 | * Populate one or more empty struct resource pointed to by @res with |
| 378 | * the resource data for this omap_device @od. Used by |
| 379 | * omap_device_build_ss() after calling omap_device_count_resources(). |
| 380 | * Ideally this function would not be needed at all. If omap_device |
| 381 | * replaces platform_device, then we can specify our own |
| 382 | * get_resource()/ get_irq()/etc functions that use the underlying |
| 383 | * omap_hwmod information. Or if platform_device is extended to use |
| 384 | * subarchitecture-specific function pointers, the various |
| 385 | * platform_device functions can simply call omap_device internal |
| 386 | * functions to get device resources. Hacking around the existing |
| 387 | * platform_device code wastes memory. Returns 0. |
| 388 | */ |
Kevin Hilman | a2a28ad | 2011-07-21 14:14:35 -0700 | [diff] [blame] | 389 | static int omap_device_fill_resources(struct omap_device *od, |
| 390 | struct resource *res) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 391 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 392 | int c = 0; |
| 393 | int i, r; |
| 394 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 395 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 396 | r = omap_hwmod_fill_resources(od->hwmods[i], res); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 397 | res += r; |
| 398 | c += r; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | /** |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 405 | * omap_device_alloc - allocate an omap_device |
| 406 | * @pdev: platform_device that will be included in this omap_device |
| 407 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 408 | * @pdata: platform_data ptr to associate with the platform_device |
| 409 | * @pdata_len: amount of memory pointed to by @pdata |
| 410 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 411 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
| 412 | * |
| 413 | * Convenience function for allocating an omap_device structure and filling |
| 414 | * hwmods, resources and pm_latency attributes. |
| 415 | * |
| 416 | * Returns an struct omap_device pointer or ERR_PTR() on error; |
| 417 | */ |
| 418 | static struct omap_device *omap_device_alloc(struct platform_device *pdev, |
| 419 | struct omap_hwmod **ohs, int oh_cnt, |
| 420 | struct omap_device_pm_latency *pm_lats, |
| 421 | int pm_lats_cnt) |
| 422 | { |
| 423 | int ret = -ENOMEM; |
| 424 | struct omap_device *od; |
| 425 | struct resource *res = NULL; |
| 426 | int i, res_count; |
| 427 | struct omap_hwmod **hwmods; |
| 428 | |
| 429 | od = kzalloc(sizeof(struct omap_device), GFP_KERNEL); |
| 430 | if (!od) { |
| 431 | ret = -ENOMEM; |
| 432 | goto oda_exit1; |
| 433 | } |
| 434 | od->hwmods_cnt = oh_cnt; |
| 435 | |
| 436 | hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL); |
| 437 | if (!hwmods) |
| 438 | goto oda_exit2; |
| 439 | |
| 440 | od->hwmods = hwmods; |
| 441 | od->pdev = pdev; |
| 442 | |
| 443 | /* |
| 444 | * HACK: Ideally the resources from DT should match, and hwmod |
| 445 | * should just add the missing ones. Since the name is not |
| 446 | * properly populated by DT, stick to hwmod resources only. |
| 447 | */ |
| 448 | if (pdev->num_resources && pdev->resource) |
| 449 | dev_warn(&pdev->dev, "%s(): resources already allocated %d\n", |
| 450 | __func__, pdev->num_resources); |
| 451 | |
| 452 | res_count = omap_device_count_resources(od); |
| 453 | if (res_count > 0) { |
| 454 | dev_dbg(&pdev->dev, "%s(): resources allocated from hwmod %d\n", |
| 455 | __func__, res_count); |
| 456 | res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL); |
| 457 | if (!res) |
| 458 | goto oda_exit3; |
| 459 | |
| 460 | omap_device_fill_resources(od, res); |
| 461 | |
| 462 | ret = platform_device_add_resources(pdev, res, res_count); |
| 463 | kfree(res); |
| 464 | |
| 465 | if (ret) |
| 466 | goto oda_exit3; |
| 467 | } |
| 468 | |
| 469 | if (!pm_lats) { |
| 470 | pm_lats = omap_default_latency; |
| 471 | pm_lats_cnt = ARRAY_SIZE(omap_default_latency); |
| 472 | } |
| 473 | |
| 474 | od->pm_lats_cnt = pm_lats_cnt; |
| 475 | od->pm_lats = kmemdup(pm_lats, |
| 476 | sizeof(struct omap_device_pm_latency) * pm_lats_cnt, |
| 477 | GFP_KERNEL); |
| 478 | if (!od->pm_lats) |
| 479 | goto oda_exit3; |
| 480 | |
| 481 | pdev->archdata.od = od; |
| 482 | |
| 483 | for (i = 0; i < oh_cnt; i++) { |
| 484 | hwmods[i]->od = od; |
| 485 | _add_hwmod_clocks_clkdev(od, hwmods[i]); |
| 486 | } |
| 487 | |
| 488 | return od; |
| 489 | |
| 490 | oda_exit3: |
| 491 | kfree(hwmods); |
| 492 | oda_exit2: |
| 493 | kfree(od); |
| 494 | oda_exit1: |
| 495 | dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret); |
| 496 | |
| 497 | return ERR_PTR(ret); |
| 498 | } |
| 499 | |
| 500 | static void omap_device_delete(struct omap_device *od) |
| 501 | { |
| 502 | od->pdev->archdata.od = NULL; |
| 503 | kfree(od->pm_lats); |
| 504 | kfree(od->hwmods); |
| 505 | kfree(od); |
| 506 | } |
| 507 | |
| 508 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 509 | * omap_device_build - build and register an omap_device with one omap_hwmod |
| 510 | * @pdev_name: name of the platform_device driver to use |
| 511 | * @pdev_id: this platform_device's connection ID |
| 512 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 513 | * @pdata: platform_data ptr to associate with the platform_device |
| 514 | * @pdata_len: amount of memory pointed to by @pdata |
| 515 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 516 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 517 | * @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] | 518 | * |
| 519 | * Convenience function for building and registering a single |
| 520 | * omap_device record, which in turn builds and registers a |
| 521 | * platform_device record. See omap_device_build_ss() for more |
| 522 | * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise, |
| 523 | * passes along the return value of omap_device_build_ss(). |
| 524 | */ |
Kevin Hilman | 3528c58 | 2011-07-21 13:48:45 -0700 | [diff] [blame] | 525 | struct platform_device *omap_device_build(const char *pdev_name, int pdev_id, |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 526 | struct omap_hwmod *oh, void *pdata, |
| 527 | int pdata_len, |
| 528 | struct omap_device_pm_latency *pm_lats, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 529 | int pm_lats_cnt, int is_early_device) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 530 | { |
| 531 | struct omap_hwmod *ohs[] = { oh }; |
| 532 | |
| 533 | if (!oh) |
| 534 | return ERR_PTR(-EINVAL); |
| 535 | |
| 536 | return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 537 | pdata_len, pm_lats, pm_lats_cnt, |
| 538 | is_early_device); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
| 542 | * omap_device_build_ss - build and register an omap_device with multiple hwmods |
| 543 | * @pdev_name: name of the platform_device driver to use |
| 544 | * @pdev_id: this platform_device's connection ID |
| 545 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 546 | * @pdata: platform_data ptr to associate with the platform_device |
| 547 | * @pdata_len: amount of memory pointed to by @pdata |
| 548 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 549 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 550 | * @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] | 551 | * |
| 552 | * Convenience function for building and registering an omap_device |
| 553 | * subsystem record. Subsystem records consist of multiple |
| 554 | * omap_hwmods. This function in turn builds and registers a |
| 555 | * platform_device record. Returns an ERR_PTR() on error, or passes |
| 556 | * along the return value of omap_device_register(). |
| 557 | */ |
Kevin Hilman | 3528c58 | 2011-07-21 13:48:45 -0700 | [diff] [blame] | 558 | struct platform_device *omap_device_build_ss(const char *pdev_name, int pdev_id, |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 559 | struct omap_hwmod **ohs, int oh_cnt, |
| 560 | void *pdata, int pdata_len, |
| 561 | struct omap_device_pm_latency *pm_lats, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 562 | int pm_lats_cnt, int is_early_device) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 563 | { |
| 564 | int ret = -ENOMEM; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 565 | struct platform_device *pdev; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 566 | struct omap_device *od; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 567 | |
| 568 | if (!ohs || oh_cnt == 0 || !pdev_name) |
| 569 | return ERR_PTR(-EINVAL); |
| 570 | |
| 571 | if (!pdata && pdata_len > 0) |
| 572 | return ERR_PTR(-EINVAL); |
| 573 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 574 | pdev = platform_device_alloc(pdev_name, pdev_id); |
| 575 | if (!pdev) { |
| 576 | ret = -ENOMEM; |
| 577 | goto odbs_exit; |
| 578 | } |
| 579 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 580 | /* Set the dev_name early to allow dev_xxx in omap_device_alloc */ |
| 581 | if (pdev->id != -1) |
| 582 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
| 583 | else |
| 584 | dev_set_name(&pdev->dev, "%s", pdev->name); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 585 | |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 586 | od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt); |
| 587 | if (!od) |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 588 | goto odbs_exit1; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 589 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 590 | ret = platform_device_add_data(pdev, pdata, pdata_len); |
Artem Bityutskiy | 49b368a | 2010-07-12 13:38:07 +0000 | [diff] [blame] | 591 | if (ret) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 592 | goto odbs_exit2; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 593 | |
| 594 | if (is_early_device) |
| 595 | ret = omap_early_device_register(pdev); |
| 596 | else |
| 597 | ret = omap_device_register(pdev); |
| 598 | if (ret) |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 599 | goto odbs_exit2; |
Kevin Hilman | 0656358 | 2010-07-26 16:34:30 -0600 | [diff] [blame] | 600 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 601 | return pdev; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 602 | |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 603 | odbs_exit2: |
Benoit Cousson | a4f6cdb | 2011-08-09 16:47:01 +0200 | [diff] [blame^] | 604 | omap_device_delete(od); |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 605 | odbs_exit1: |
| 606 | platform_device_put(pdev); |
| 607 | odbs_exit: |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 608 | |
| 609 | pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret); |
| 610 | |
| 611 | return ERR_PTR(ret); |
| 612 | } |
| 613 | |
| 614 | /** |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 615 | * omap_early_device_register - register an omap_device as an early platform |
| 616 | * device. |
| 617 | * @od: struct omap_device * to register |
| 618 | * |
| 619 | * Register the omap_device structure. This currently just calls |
| 620 | * platform_early_add_device() on the underlying platform_device. |
| 621 | * Returns 0 by default. |
| 622 | */ |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 623 | static int omap_early_device_register(struct platform_device *pdev) |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 624 | { |
| 625 | struct platform_device *devices[1]; |
| 626 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 627 | devices[0] = pdev; |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 628 | early_platform_add_devices(devices, 1); |
| 629 | return 0; |
| 630 | } |
| 631 | |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 632 | #ifdef CONFIG_PM_RUNTIME |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 633 | static int _od_runtime_suspend(struct device *dev) |
| 634 | { |
| 635 | struct platform_device *pdev = to_platform_device(dev); |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 636 | int ret; |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 637 | |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 638 | ret = pm_generic_runtime_suspend(dev); |
| 639 | |
| 640 | if (!ret) |
| 641 | omap_device_idle(pdev); |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static int _od_runtime_idle(struct device *dev) |
| 647 | { |
| 648 | return pm_generic_runtime_idle(dev); |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static int _od_runtime_resume(struct device *dev) |
| 652 | { |
| 653 | struct platform_device *pdev = to_platform_device(dev); |
| 654 | |
Kevin Hilman | 345f79b | 2011-05-31 16:08:09 -0700 | [diff] [blame] | 655 | omap_device_enable(pdev); |
| 656 | |
| 657 | return pm_generic_runtime_resume(dev); |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 658 | } |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 659 | #endif |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 660 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 661 | #ifdef CONFIG_SUSPEND |
| 662 | static int _od_suspend_noirq(struct device *dev) |
| 663 | { |
| 664 | struct platform_device *pdev = to_platform_device(dev); |
| 665 | struct omap_device *od = to_omap_device(pdev); |
| 666 | int ret; |
| 667 | |
Kevin Hilman | 80c6d1e | 2011-07-12 22:48:29 +0200 | [diff] [blame] | 668 | if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND) |
| 669 | return pm_generic_suspend_noirq(dev); |
| 670 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 671 | ret = pm_generic_suspend_noirq(dev); |
| 672 | |
| 673 | if (!ret && !pm_runtime_status_suspended(dev)) { |
| 674 | if (pm_generic_runtime_suspend(dev) == 0) { |
| 675 | omap_device_idle(pdev); |
| 676 | od->flags |= OMAP_DEVICE_SUSPENDED; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static int _od_resume_noirq(struct device *dev) |
| 684 | { |
| 685 | struct platform_device *pdev = to_platform_device(dev); |
| 686 | struct omap_device *od = to_omap_device(pdev); |
| 687 | |
Kevin Hilman | 80c6d1e | 2011-07-12 22:48:29 +0200 | [diff] [blame] | 688 | if (od->flags & OMAP_DEVICE_NO_IDLE_ON_SUSPEND) |
| 689 | return pm_generic_resume_noirq(dev); |
| 690 | |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 691 | if ((od->flags & OMAP_DEVICE_SUSPENDED) && |
| 692 | !pm_runtime_status_suspended(dev)) { |
| 693 | od->flags &= ~OMAP_DEVICE_SUSPENDED; |
| 694 | omap_device_enable(pdev); |
| 695 | pm_generic_runtime_resume(dev); |
| 696 | } |
| 697 | |
| 698 | return pm_generic_resume_noirq(dev); |
| 699 | } |
Kevin Hilman | 126caf1 | 2011-09-01 10:59:36 -0700 | [diff] [blame] | 700 | #else |
| 701 | #define _od_suspend_noirq NULL |
| 702 | #define _od_resume_noirq NULL |
Kevin Hilman | c03f007 | 2011-07-12 22:48:19 +0200 | [diff] [blame] | 703 | #endif |
| 704 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 705 | static struct dev_pm_domain omap_device_pm_domain = { |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 706 | .ops = { |
Kevin Hilman | 256a543 | 2011-07-12 22:48:03 +0200 | [diff] [blame] | 707 | SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume, |
| 708 | _od_runtime_idle) |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 709 | USE_PLATFORM_PM_SLEEP_OPS |
Kevin Hilman | ff35336 | 2011-08-25 15:31:14 +0200 | [diff] [blame] | 710 | .suspend_noirq = _od_suspend_noirq, |
| 711 | .resume_noirq = _od_resume_noirq, |
Kevin Hilman | 638080c | 2011-04-29 00:36:42 +0200 | [diff] [blame] | 712 | } |
| 713 | }; |
| 714 | |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 715 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 716 | * omap_device_register - register an omap_device with one omap_hwmod |
| 717 | * @od: struct omap_device * to register |
| 718 | * |
| 719 | * Register the omap_device structure. This currently just calls |
| 720 | * platform_device_register() on the underlying platform_device. |
| 721 | * Returns the return value of platform_device_register(). |
| 722 | */ |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 723 | static int omap_device_register(struct platform_device *pdev) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 724 | { |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 725 | pr_debug("omap_device: %s: registering\n", pdev->name); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 726 | |
Kevin Hilman | bfae4f8 | 2011-07-21 14:47:53 -0700 | [diff] [blame] | 727 | pdev->dev.parent = &omap_device_parent; |
| 728 | pdev->dev.pm_domain = &omap_device_pm_domain; |
Kevin Hilman | d66b3fe | 2011-07-21 13:58:51 -0700 | [diff] [blame] | 729 | return platform_device_add(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | |
| 733 | /* Public functions for use by device drivers through struct platform_data */ |
| 734 | |
| 735 | /** |
| 736 | * omap_device_enable - fully activate an omap_device |
| 737 | * @od: struct omap_device * to activate |
| 738 | * |
| 739 | * Do whatever is necessary for the hwmods underlying omap_device @od |
| 740 | * to be accessible and ready to operate. This generally involves |
| 741 | * enabling clocks, setting SYSCONFIG registers; and in the future may |
| 742 | * involve remuxing pins. Device drivers should call this function |
| 743 | * (through platform_data function pointers) where they would normally |
| 744 | * enable clocks, etc. Returns -EINVAL if called when the omap_device |
| 745 | * is already enabled, or passes along the return value of |
| 746 | * _omap_device_activate(). |
| 747 | */ |
| 748 | int omap_device_enable(struct platform_device *pdev) |
| 749 | { |
| 750 | int ret; |
| 751 | struct omap_device *od; |
| 752 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 753 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 754 | |
| 755 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 756 | dev_warn(&pdev->dev, |
| 757 | "omap_device: %s() called from invalid state %d\n", |
| 758 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 759 | return -EINVAL; |
| 760 | } |
| 761 | |
| 762 | /* Enable everything if we're enabling this device from scratch */ |
| 763 | if (od->_state == OMAP_DEVICE_STATE_UNKNOWN) |
| 764 | od->pm_lat_level = od->pm_lats_cnt; |
| 765 | |
| 766 | ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT); |
| 767 | |
| 768 | od->dev_wakeup_lat = 0; |
Kevin Hilman | 5f1b6ef | 2009-12-08 16:34:22 -0700 | [diff] [blame] | 769 | od->_dev_wakeup_lat_limit = UINT_MAX; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 770 | od->_state = OMAP_DEVICE_STATE_ENABLED; |
| 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * omap_device_idle - idle an omap_device |
| 777 | * @od: struct omap_device * to idle |
| 778 | * |
| 779 | * Idle omap_device @od by calling as many .deactivate_func() entries |
| 780 | * in the omap_device's pm_lats table as is possible without exceeding |
| 781 | * the device's maximum wakeup latency limit, pm_lat_limit. Device |
| 782 | * drivers should call this function (through platform_data function |
| 783 | * pointers) where they would normally disable clocks after operations |
| 784 | * complete, etc.. Returns -EINVAL if the omap_device is not |
| 785 | * currently enabled, or passes along the return value of |
| 786 | * _omap_device_deactivate(). |
| 787 | */ |
| 788 | int omap_device_idle(struct platform_device *pdev) |
| 789 | { |
| 790 | int ret; |
| 791 | struct omap_device *od; |
| 792 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 793 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 794 | |
| 795 | if (od->_state != OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 796 | dev_warn(&pdev->dev, |
| 797 | "omap_device: %s() called from invalid state %d\n", |
| 798 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 799 | return -EINVAL; |
| 800 | } |
| 801 | |
| 802 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); |
| 803 | |
| 804 | od->_state = OMAP_DEVICE_STATE_IDLE; |
| 805 | |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * omap_device_shutdown - shut down an omap_device |
| 811 | * @od: struct omap_device * to shut down |
| 812 | * |
| 813 | * Shut down omap_device @od by calling all .deactivate_func() entries |
| 814 | * in the omap_device's pm_lats table and then shutting down all of |
| 815 | * the underlying omap_hwmods. Used when a device is being "removed" |
| 816 | * or a device driver is being unloaded. Returns -EINVAL if the |
| 817 | * omap_device is not currently enabled or idle, or passes along the |
| 818 | * return value of _omap_device_deactivate(). |
| 819 | */ |
| 820 | int omap_device_shutdown(struct platform_device *pdev) |
| 821 | { |
| 822 | int ret, i; |
| 823 | struct omap_device *od; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 824 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 825 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 826 | |
| 827 | if (od->_state != OMAP_DEVICE_STATE_ENABLED && |
| 828 | od->_state != OMAP_DEVICE_STATE_IDLE) { |
Kevin Hilman | 49882c9 | 2011-07-21 09:58:36 -0700 | [diff] [blame] | 829 | dev_warn(&pdev->dev, |
| 830 | "omap_device: %s() called from invalid state %d\n", |
| 831 | __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 832 | return -EINVAL; |
| 833 | } |
| 834 | |
| 835 | ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT); |
| 836 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 837 | for (i = 0; i < od->hwmods_cnt; i++) |
| 838 | omap_hwmod_shutdown(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 839 | |
| 840 | od->_state = OMAP_DEVICE_STATE_SHUTDOWN; |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim |
| 847 | * @od: struct omap_device * |
| 848 | * |
| 849 | * When a device's maximum wakeup latency limit changes, call some of |
| 850 | * the .activate_func or .deactivate_func function pointers in the |
| 851 | * omap_device's pm_lats array to ensure that the device's maximum |
| 852 | * wakeup latency is less than or equal to the new latency limit. |
| 853 | * Intended to be called by OMAP PM code whenever a device's maximum |
| 854 | * wakeup latency limit changes (e.g., via |
| 855 | * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be |
| 856 | * done (e.g., if the omap_device is not currently idle, or if the |
| 857 | * wakeup latency is already current with the new limit) or passes |
| 858 | * along the return value of _omap_device_deactivate() or |
| 859 | * _omap_device_activate(). |
| 860 | */ |
| 861 | int omap_device_align_pm_lat(struct platform_device *pdev, |
| 862 | u32 new_wakeup_lat_limit) |
| 863 | { |
| 864 | int ret = -EINVAL; |
| 865 | struct omap_device *od; |
| 866 | |
Kevin Hilman | 8f0d69d | 2011-07-09 19:15:20 -0600 | [diff] [blame] | 867 | od = to_omap_device(pdev); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 868 | |
| 869 | if (new_wakeup_lat_limit == od->dev_wakeup_lat) |
| 870 | return 0; |
| 871 | |
| 872 | od->_dev_wakeup_lat_limit = new_wakeup_lat_limit; |
| 873 | |
| 874 | if (od->_state != OMAP_DEVICE_STATE_IDLE) |
| 875 | return 0; |
| 876 | else if (new_wakeup_lat_limit > od->dev_wakeup_lat) |
| 877 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); |
| 878 | else if (new_wakeup_lat_limit < od->dev_wakeup_lat) |
| 879 | ret = _omap_device_activate(od, USE_WAKEUP_LAT); |
| 880 | |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 885 | * omap_device_get_pwrdm - return the powerdomain * associated with @od |
| 886 | * @od: struct omap_device * |
| 887 | * |
| 888 | * Return the powerdomain associated with the first underlying |
| 889 | * omap_hwmod for this omap_device. Intended for use by core OMAP PM |
| 890 | * code. Returns NULL on error or a struct powerdomain * upon |
| 891 | * success. |
| 892 | */ |
| 893 | struct powerdomain *omap_device_get_pwrdm(struct omap_device *od) |
| 894 | { |
| 895 | /* |
| 896 | * XXX Assumes that all omap_hwmod powerdomains are identical. |
| 897 | * This may not necessarily be true. There should be a sanity |
| 898 | * check in here to WARN() if any difference appears. |
| 899 | */ |
| 900 | if (!od->hwmods_cnt) |
| 901 | return NULL; |
| 902 | |
| 903 | return omap_hwmod_get_pwrdm(od->hwmods[0]); |
| 904 | } |
| 905 | |
Paul Walmsley | db2a60b | 2010-07-26 16:34:33 -0600 | [diff] [blame] | 906 | /** |
| 907 | * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base |
| 908 | * @od: struct omap_device * |
| 909 | * |
| 910 | * Return the MPU's virtual address for the base of the hwmod, from |
| 911 | * the ioremap() that the hwmod code does. Only valid if there is one |
| 912 | * hwmod associated with this device. Returns NULL if there are zero |
| 913 | * or more than one hwmods associated with this omap_device; |
| 914 | * otherwise, passes along the return value from |
| 915 | * omap_hwmod_get_mpu_rt_va(). |
| 916 | */ |
| 917 | void __iomem *omap_device_get_rt_va(struct omap_device *od) |
| 918 | { |
| 919 | if (od->hwmods_cnt != 1) |
| 920 | return NULL; |
| 921 | |
| 922 | return omap_hwmod_get_mpu_rt_va(od->hwmods[0]); |
| 923 | } |
| 924 | |
Nishanth Menon | 1f8a7d5 | 2011-07-27 15:02:32 -0500 | [diff] [blame] | 925 | /** |
| 926 | * omap_device_get_by_hwmod_name() - convert a hwmod name to |
| 927 | * device pointer. |
| 928 | * @oh_name: name of the hwmod device |
| 929 | * |
| 930 | * Returns back a struct device * pointer associated with a hwmod |
| 931 | * device represented by a hwmod_name |
| 932 | */ |
| 933 | struct device *omap_device_get_by_hwmod_name(const char *oh_name) |
| 934 | { |
| 935 | struct omap_hwmod *oh; |
| 936 | |
| 937 | if (!oh_name) { |
| 938 | WARN(1, "%s: no hwmod name!\n", __func__); |
| 939 | return ERR_PTR(-EINVAL); |
| 940 | } |
| 941 | |
| 942 | oh = omap_hwmod_lookup(oh_name); |
| 943 | if (IS_ERR_OR_NULL(oh)) { |
| 944 | WARN(1, "%s: no hwmod for %s\n", __func__, |
| 945 | oh_name); |
| 946 | return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV); |
| 947 | } |
| 948 | if (IS_ERR_OR_NULL(oh->od)) { |
| 949 | WARN(1, "%s: no omap_device for %s\n", __func__, |
| 950 | oh_name); |
| 951 | return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV); |
| 952 | } |
| 953 | |
| 954 | if (IS_ERR_OR_NULL(oh->od->pdev)) |
| 955 | return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV); |
| 956 | |
| 957 | return &oh->od->pdev->dev; |
| 958 | } |
| 959 | EXPORT_SYMBOL(omap_device_get_by_hwmod_name); |
| 960 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 961 | /* |
| 962 | * Public functions intended for use in omap_device_pm_latency |
| 963 | * .activate_func and .deactivate_func function pointers |
| 964 | */ |
| 965 | |
| 966 | /** |
| 967 | * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods |
| 968 | * @od: struct omap_device *od |
| 969 | * |
| 970 | * Enable all underlying hwmods. Returns 0. |
| 971 | */ |
| 972 | int omap_device_enable_hwmods(struct omap_device *od) |
| 973 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 974 | int i; |
| 975 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 976 | for (i = 0; i < od->hwmods_cnt; i++) |
| 977 | omap_hwmod_enable(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 978 | |
| 979 | /* XXX pass along return value here? */ |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods |
| 985 | * @od: struct omap_device *od |
| 986 | * |
| 987 | * Idle all underlying hwmods. Returns 0. |
| 988 | */ |
| 989 | int omap_device_idle_hwmods(struct omap_device *od) |
| 990 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 991 | int i; |
| 992 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 993 | for (i = 0; i < od->hwmods_cnt; i++) |
| 994 | omap_hwmod_idle(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 995 | |
| 996 | /* XXX pass along return value here? */ |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * omap_device_disable_clocks - disable all main and interface clocks |
| 1002 | * @od: struct omap_device *od |
| 1003 | * |
| 1004 | * Disable the main functional clock and interface clock for all of the |
| 1005 | * omap_hwmods associated with the omap_device. Returns 0. |
| 1006 | */ |
| 1007 | int omap_device_disable_clocks(struct omap_device *od) |
| 1008 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1009 | int i; |
| 1010 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 1011 | for (i = 0; i < od->hwmods_cnt; i++) |
| 1012 | omap_hwmod_disable_clocks(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1013 | |
| 1014 | /* XXX pass along return value here? */ |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * omap_device_enable_clocks - enable all main and interface clocks |
| 1020 | * @od: struct omap_device *od |
| 1021 | * |
| 1022 | * Enable the main functional clock and interface clock for all of the |
| 1023 | * omap_hwmods associated with the omap_device. Returns 0. |
| 1024 | */ |
| 1025 | int omap_device_enable_clocks(struct omap_device *od) |
| 1026 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1027 | int i; |
| 1028 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 1029 | for (i = 0; i < od->hwmods_cnt; i++) |
| 1030 | omap_hwmod_enable_clocks(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 1031 | |
| 1032 | /* XXX pass along return value here? */ |
| 1033 | return 0; |
| 1034 | } |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 1035 | |
| 1036 | struct device omap_device_parent = { |
| 1037 | .init_name = "omap", |
| 1038 | .parent = &platform_bus, |
| 1039 | }; |
| 1040 | |
| 1041 | static int __init omap_device_init(void) |
| 1042 | { |
| 1043 | return device_register(&omap_device_parent); |
| 1044 | } |
| 1045 | core_initcall(omap_device_init); |