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> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 86 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 87 | #include <plat/omap_device.h> |
| 88 | #include <plat/omap_hwmod.h> |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 89 | |
| 90 | /* These parameters are passed to _omap_device_{de,}activate() */ |
| 91 | #define USE_WAKEUP_LAT 0 |
| 92 | #define IGNORE_WAKEUP_LAT 1 |
| 93 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 94 | /* Private functions */ |
| 95 | |
| 96 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 97 | * _omap_device_activate - increase device readiness |
| 98 | * @od: struct omap_device * |
| 99 | * @ignore_lat: increase to latency target (0) or full readiness (1)? |
| 100 | * |
| 101 | * Increase readiness of omap_device @od (thus decreasing device |
| 102 | * wakeup latency, but consuming more power). If @ignore_lat is |
| 103 | * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise, |
| 104 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup |
| 105 | * latency is greater than the requested maximum wakeup latency, step |
| 106 | * backwards in the omap_device_pm_latency table to ensure the |
| 107 | * device's maximum wakeup latency is less than or equal to the |
| 108 | * requested maximum wakeup latency. Returns 0. |
| 109 | */ |
| 110 | static int _omap_device_activate(struct omap_device *od, u8 ignore_lat) |
| 111 | { |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 112 | struct timespec a, b, c; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 113 | |
| 114 | pr_debug("omap_device: %s: activating\n", od->pdev.name); |
| 115 | |
| 116 | while (od->pm_lat_level > 0) { |
| 117 | struct omap_device_pm_latency *odpl; |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 118 | unsigned long long act_lat = 0; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 119 | |
| 120 | od->pm_lat_level--; |
| 121 | |
| 122 | odpl = od->pm_lats + od->pm_lat_level; |
| 123 | |
| 124 | if (!ignore_lat && |
| 125 | (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit)) |
| 126 | break; |
| 127 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 128 | read_persistent_clock(&a); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 129 | |
| 130 | /* XXX check return code */ |
| 131 | odpl->activate_func(od); |
| 132 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 133 | read_persistent_clock(&b); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 134 | |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 135 | c = timespec_sub(b, a); |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 136 | act_lat = timespec_to_ns(&c); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 137 | |
| 138 | pr_debug("omap_device: %s: pm_lat %d: activate: elapsed time " |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 139 | "%llu nsec\n", od->pdev.name, od->pm_lat_level, |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 140 | act_lat); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 141 | |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 142 | if (act_lat > odpl->activate_lat) { |
| 143 | odpl->activate_lat_worst = act_lat; |
| 144 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { |
| 145 | odpl->activate_lat = act_lat; |
| 146 | pr_warning("omap_device: %s.%d: new worst case " |
| 147 | "activate latency %d: %llu\n", |
| 148 | od->pdev.name, od->pdev.id, |
| 149 | od->pm_lat_level, act_lat); |
| 150 | } else |
| 151 | pr_warning("omap_device: %s.%d: activate " |
| 152 | "latency %d higher than exptected. " |
| 153 | "(%llu > %d)\n", |
| 154 | od->pdev.name, od->pdev.id, |
| 155 | od->pm_lat_level, act_lat, |
| 156 | odpl->activate_lat); |
| 157 | } |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 158 | |
| 159 | od->dev_wakeup_lat -= odpl->activate_lat; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * _omap_device_deactivate - decrease device readiness |
| 167 | * @od: struct omap_device * |
| 168 | * @ignore_lat: decrease to latency target (0) or full inactivity (1)? |
| 169 | * |
| 170 | * Decrease readiness of omap_device @od (thus increasing device |
| 171 | * wakeup latency, but conserving power). If @ignore_lat is |
| 172 | * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise, |
| 173 | * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup |
| 174 | * latency is less than the requested maximum wakeup latency, step |
| 175 | * forwards in the omap_device_pm_latency table to ensure the device's |
| 176 | * maximum wakeup latency is less than or equal to the requested |
| 177 | * maximum wakeup latency. Returns 0. |
| 178 | */ |
| 179 | static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat) |
| 180 | { |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 181 | struct timespec a, b, c; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 182 | |
| 183 | pr_debug("omap_device: %s: deactivating\n", od->pdev.name); |
| 184 | |
| 185 | while (od->pm_lat_level < od->pm_lats_cnt) { |
| 186 | struct omap_device_pm_latency *odpl; |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 187 | unsigned long long deact_lat = 0; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 188 | |
| 189 | odpl = od->pm_lats + od->pm_lat_level; |
| 190 | |
| 191 | if (!ignore_lat && |
| 192 | ((od->dev_wakeup_lat + odpl->activate_lat) > |
| 193 | od->_dev_wakeup_lat_limit)) |
| 194 | break; |
| 195 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 196 | read_persistent_clock(&a); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 197 | |
| 198 | /* XXX check return code */ |
| 199 | odpl->deactivate_func(od); |
| 200 | |
Kevin Hilman | d229266 | 2009-12-08 16:34:23 -0700 | [diff] [blame] | 201 | read_persistent_clock(&b); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 202 | |
Tony Lindgren | f059429 | 2009-10-19 15:25:24 -0700 | [diff] [blame] | 203 | c = timespec_sub(b, a); |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 204 | deact_lat = timespec_to_ns(&c); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 205 | |
| 206 | pr_debug("omap_device: %s: pm_lat %d: deactivate: elapsed time " |
Kevin Hilman | 0d93d8b | 2009-12-08 16:34:26 -0700 | [diff] [blame] | 207 | "%llu nsec\n", od->pdev.name, od->pm_lat_level, |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 208 | deact_lat); |
| 209 | |
Kevin Hilman | 9799aca | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 210 | if (deact_lat > odpl->deactivate_lat) { |
| 211 | odpl->deactivate_lat_worst = deact_lat; |
| 212 | if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) { |
| 213 | odpl->deactivate_lat = deact_lat; |
| 214 | pr_warning("omap_device: %s.%d: new worst case " |
| 215 | "deactivate latency %d: %llu\n", |
| 216 | od->pdev.name, od->pdev.id, |
| 217 | od->pm_lat_level, deact_lat); |
| 218 | } else |
| 219 | pr_warning("omap_device: %s.%d: deactivate " |
| 220 | "latency %d higher than exptected. " |
| 221 | "(%llu > %d)\n", |
| 222 | od->pdev.name, od->pdev.id, |
| 223 | od->pm_lat_level, deact_lat, |
| 224 | odpl->deactivate_lat); |
| 225 | } |
| 226 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 227 | |
| 228 | od->dev_wakeup_lat += odpl->activate_lat; |
| 229 | |
| 230 | od->pm_lat_level++; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static inline struct omap_device *_find_by_pdev(struct platform_device *pdev) |
| 237 | { |
| 238 | return container_of(pdev, struct omap_device, pdev); |
| 239 | } |
| 240 | |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 241 | /** |
| 242 | * _add_optional_clock_alias - Add clock alias for hwmod optional clocks |
| 243 | * @od: struct omap_device *od |
| 244 | * |
| 245 | * For every optional clock present per hwmod per omap_device, this function |
| 246 | * adds an entry in the clocks list of the form <dev-id=dev_name, con-id=role> |
| 247 | * if an entry is already present in it with the form <dev-id=NULL, con-id=role> |
| 248 | * |
| 249 | * The function is called from inside omap_device_build_ss(), after |
| 250 | * omap_device_register. |
| 251 | * |
| 252 | * This allows drivers to get a pointer to its optional clocks based on its role |
| 253 | * by calling clk_get(<dev*>, <role>). |
| 254 | * |
| 255 | * No return value. |
| 256 | */ |
| 257 | static void _add_optional_clock_alias(struct omap_device *od, |
| 258 | struct omap_hwmod *oh) |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | for (i = 0; i < oh->opt_clks_cnt; i++) { |
| 263 | struct omap_hwmod_opt_clk *oc; |
| 264 | int r; |
| 265 | |
| 266 | oc = &oh->opt_clks[i]; |
| 267 | |
| 268 | if (!oc->_clk) |
| 269 | continue; |
| 270 | |
| 271 | r = clk_add_alias(oc->role, dev_name(&od->pdev.dev), |
| 272 | (char *)oc->clk, &od->pdev.dev); |
| 273 | if (r) |
| 274 | pr_err("omap_device: %s: clk_add_alias for %s failed\n", |
| 275 | dev_name(&od->pdev.dev), oc->role); |
| 276 | } |
| 277 | } |
| 278 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 279 | |
| 280 | /* Public functions for use by core code */ |
| 281 | |
| 282 | /** |
Kevin Hilman | c80705a | 2010-12-21 21:31:55 -0700 | [diff] [blame] | 283 | * omap_device_get_context_loss_count - get lost context count |
| 284 | * @od: struct omap_device * |
| 285 | * |
| 286 | * Using the primary hwmod, query the context loss count for this |
| 287 | * device. |
| 288 | * |
| 289 | * Callers should consider context for this device lost any time this |
| 290 | * function returns a value different than the value the caller got |
| 291 | * the last time it called this function. |
| 292 | * |
| 293 | * If any hwmods exist for the omap_device assoiated with @pdev, |
| 294 | * return the context loss counter for that hwmod, otherwise return |
| 295 | * zero. |
| 296 | */ |
| 297 | u32 omap_device_get_context_loss_count(struct platform_device *pdev) |
| 298 | { |
| 299 | struct omap_device *od; |
| 300 | u32 ret = 0; |
| 301 | |
| 302 | od = _find_by_pdev(pdev); |
| 303 | |
| 304 | if (od->hwmods_cnt) |
| 305 | ret = omap_hwmod_get_context_loss_count(od->hwmods[0]); |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 311 | * omap_device_count_resources - count number of struct resource entries needed |
| 312 | * @od: struct omap_device * |
| 313 | * |
| 314 | * Count the number of struct resource entries needed for this |
| 315 | * omap_device @od. Used by omap_device_build_ss() to determine how |
| 316 | * much memory to allocate before calling |
| 317 | * omap_device_fill_resources(). Returns the count. |
| 318 | */ |
| 319 | int omap_device_count_resources(struct omap_device *od) |
| 320 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 321 | int c = 0; |
| 322 | int i; |
| 323 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 324 | for (i = 0; i < od->hwmods_cnt; i++) |
| 325 | c += omap_hwmod_count_resources(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 326 | |
| 327 | pr_debug("omap_device: %s: counted %d total resources across %d " |
| 328 | "hwmods\n", od->pdev.name, c, od->hwmods_cnt); |
| 329 | |
| 330 | return c; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * omap_device_fill_resources - fill in array of struct resource |
| 335 | * @od: struct omap_device * |
| 336 | * @res: pointer to an array of struct resource to be filled in |
| 337 | * |
| 338 | * Populate one or more empty struct resource pointed to by @res with |
| 339 | * the resource data for this omap_device @od. Used by |
| 340 | * omap_device_build_ss() after calling omap_device_count_resources(). |
| 341 | * Ideally this function would not be needed at all. If omap_device |
| 342 | * replaces platform_device, then we can specify our own |
| 343 | * get_resource()/ get_irq()/etc functions that use the underlying |
| 344 | * omap_hwmod information. Or if platform_device is extended to use |
| 345 | * subarchitecture-specific function pointers, the various |
| 346 | * platform_device functions can simply call omap_device internal |
| 347 | * functions to get device resources. Hacking around the existing |
| 348 | * platform_device code wastes memory. Returns 0. |
| 349 | */ |
| 350 | int omap_device_fill_resources(struct omap_device *od, struct resource *res) |
| 351 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 352 | int c = 0; |
| 353 | int i, r; |
| 354 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 355 | for (i = 0; i < od->hwmods_cnt; i++) { |
| 356 | r = omap_hwmod_fill_resources(od->hwmods[i], res); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 357 | res += r; |
| 358 | c += r; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * omap_device_build - build and register an omap_device with one omap_hwmod |
| 366 | * @pdev_name: name of the platform_device driver to use |
| 367 | * @pdev_id: this platform_device's connection ID |
| 368 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 369 | * @pdata: platform_data ptr to associate with the platform_device |
| 370 | * @pdata_len: amount of memory pointed to by @pdata |
| 371 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 372 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 373 | * @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] | 374 | * |
| 375 | * Convenience function for building and registering a single |
| 376 | * omap_device record, which in turn builds and registers a |
| 377 | * platform_device record. See omap_device_build_ss() for more |
| 378 | * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise, |
| 379 | * passes along the return value of omap_device_build_ss(). |
| 380 | */ |
| 381 | struct omap_device *omap_device_build(const char *pdev_name, int pdev_id, |
| 382 | struct omap_hwmod *oh, void *pdata, |
| 383 | int pdata_len, |
| 384 | struct omap_device_pm_latency *pm_lats, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 385 | int pm_lats_cnt, int is_early_device) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 386 | { |
| 387 | struct omap_hwmod *ohs[] = { oh }; |
| 388 | |
| 389 | if (!oh) |
| 390 | return ERR_PTR(-EINVAL); |
| 391 | |
| 392 | return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 393 | pdata_len, pm_lats, pm_lats_cnt, |
| 394 | is_early_device); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * omap_device_build_ss - build and register an omap_device with multiple hwmods |
| 399 | * @pdev_name: name of the platform_device driver to use |
| 400 | * @pdev_id: this platform_device's connection ID |
| 401 | * @oh: ptr to the single omap_hwmod that backs this omap_device |
| 402 | * @pdata: platform_data ptr to associate with the platform_device |
| 403 | * @pdata_len: amount of memory pointed to by @pdata |
| 404 | * @pm_lats: pointer to a omap_device_pm_latency array for this device |
| 405 | * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 406 | * @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] | 407 | * |
| 408 | * Convenience function for building and registering an omap_device |
| 409 | * subsystem record. Subsystem records consist of multiple |
| 410 | * omap_hwmods. This function in turn builds and registers a |
| 411 | * platform_device record. Returns an ERR_PTR() on error, or passes |
| 412 | * along the return value of omap_device_register(). |
| 413 | */ |
| 414 | struct omap_device *omap_device_build_ss(const char *pdev_name, int pdev_id, |
| 415 | struct omap_hwmod **ohs, int oh_cnt, |
| 416 | void *pdata, int pdata_len, |
| 417 | struct omap_device_pm_latency *pm_lats, |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 418 | int pm_lats_cnt, int is_early_device) |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 419 | { |
| 420 | int ret = -ENOMEM; |
| 421 | struct omap_device *od; |
| 422 | char *pdev_name2; |
| 423 | struct resource *res = NULL; |
Kevin Hilman | 0656358 | 2010-07-26 16:34:30 -0600 | [diff] [blame] | 424 | int i, res_count; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 425 | struct omap_hwmod **hwmods; |
| 426 | |
| 427 | if (!ohs || oh_cnt == 0 || !pdev_name) |
| 428 | return ERR_PTR(-EINVAL); |
| 429 | |
| 430 | if (!pdata && pdata_len > 0) |
| 431 | return ERR_PTR(-EINVAL); |
| 432 | |
| 433 | pr_debug("omap_device: %s: building with %d hwmods\n", pdev_name, |
| 434 | oh_cnt); |
| 435 | |
| 436 | od = kzalloc(sizeof(struct omap_device), GFP_KERNEL); |
| 437 | if (!od) |
| 438 | return ERR_PTR(-ENOMEM); |
| 439 | |
| 440 | od->hwmods_cnt = oh_cnt; |
| 441 | |
| 442 | hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, |
| 443 | GFP_KERNEL); |
| 444 | if (!hwmods) |
| 445 | goto odbs_exit1; |
| 446 | |
| 447 | memcpy(hwmods, ohs, sizeof(struct omap_hwmod *) * oh_cnt); |
| 448 | od->hwmods = hwmods; |
| 449 | |
| 450 | pdev_name2 = kzalloc(strlen(pdev_name) + 1, GFP_KERNEL); |
| 451 | if (!pdev_name2) |
| 452 | goto odbs_exit2; |
| 453 | strcpy(pdev_name2, pdev_name); |
| 454 | |
| 455 | od->pdev.name = pdev_name2; |
| 456 | od->pdev.id = pdev_id; |
| 457 | |
| 458 | res_count = omap_device_count_resources(od); |
| 459 | if (res_count > 0) { |
| 460 | res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL); |
| 461 | if (!res) |
| 462 | goto odbs_exit3; |
| 463 | } |
| 464 | omap_device_fill_resources(od, res); |
| 465 | |
| 466 | od->pdev.num_resources = res_count; |
| 467 | od->pdev.resource = res; |
| 468 | |
Artem Bityutskiy | 49b368a | 2010-07-12 13:38:07 +0000 | [diff] [blame] | 469 | ret = platform_device_add_data(&od->pdev, pdata, pdata_len); |
| 470 | if (ret) |
| 471 | goto odbs_exit4; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 472 | |
| 473 | od->pm_lats = pm_lats; |
| 474 | od->pm_lats_cnt = pm_lats_cnt; |
| 475 | |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 476 | if (is_early_device) |
| 477 | ret = omap_early_device_register(od); |
| 478 | else |
| 479 | ret = omap_device_register(od); |
| 480 | |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 481 | for (i = 0; i < oh_cnt; i++) { |
Kevin Hilman | 0656358 | 2010-07-26 16:34:30 -0600 | [diff] [blame] | 482 | hwmods[i]->od = od; |
Partha Basak | 4ef7aca | 2010-09-21 19:23:04 +0200 | [diff] [blame] | 483 | _add_optional_clock_alias(od, hwmods[i]); |
| 484 | } |
Kevin Hilman | 0656358 | 2010-07-26 16:34:30 -0600 | [diff] [blame] | 485 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 486 | if (ret) |
| 487 | goto odbs_exit4; |
| 488 | |
| 489 | return od; |
| 490 | |
| 491 | odbs_exit4: |
| 492 | kfree(res); |
| 493 | odbs_exit3: |
| 494 | kfree(pdev_name2); |
| 495 | odbs_exit2: |
| 496 | kfree(hwmods); |
| 497 | odbs_exit1: |
| 498 | kfree(od); |
| 499 | |
| 500 | pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret); |
| 501 | |
| 502 | return ERR_PTR(ret); |
| 503 | } |
| 504 | |
| 505 | /** |
Thara Gopinath | c23a97d | 2010-02-24 12:05:58 -0700 | [diff] [blame] | 506 | * omap_early_device_register - register an omap_device as an early platform |
| 507 | * device. |
| 508 | * @od: struct omap_device * to register |
| 509 | * |
| 510 | * Register the omap_device structure. This currently just calls |
| 511 | * platform_early_add_device() on the underlying platform_device. |
| 512 | * Returns 0 by default. |
| 513 | */ |
| 514 | int omap_early_device_register(struct omap_device *od) |
| 515 | { |
| 516 | struct platform_device *devices[1]; |
| 517 | |
| 518 | devices[0] = &(od->pdev); |
| 519 | early_platform_add_devices(devices, 1); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 524 | * omap_device_register - register an omap_device with one omap_hwmod |
| 525 | * @od: struct omap_device * to register |
| 526 | * |
| 527 | * Register the omap_device structure. This currently just calls |
| 528 | * platform_device_register() on the underlying platform_device. |
| 529 | * Returns the return value of platform_device_register(). |
| 530 | */ |
| 531 | int omap_device_register(struct omap_device *od) |
| 532 | { |
| 533 | pr_debug("omap_device: %s: registering\n", od->pdev.name); |
| 534 | |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 535 | od->pdev.dev.parent = &omap_device_parent; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 536 | return platform_device_register(&od->pdev); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* Public functions for use by device drivers through struct platform_data */ |
| 541 | |
| 542 | /** |
| 543 | * omap_device_enable - fully activate an omap_device |
| 544 | * @od: struct omap_device * to activate |
| 545 | * |
| 546 | * Do whatever is necessary for the hwmods underlying omap_device @od |
| 547 | * to be accessible and ready to operate. This generally involves |
| 548 | * enabling clocks, setting SYSCONFIG registers; and in the future may |
| 549 | * involve remuxing pins. Device drivers should call this function |
| 550 | * (through platform_data function pointers) where they would normally |
| 551 | * enable clocks, etc. Returns -EINVAL if called when the omap_device |
| 552 | * is already enabled, or passes along the return value of |
| 553 | * _omap_device_activate(). |
| 554 | */ |
| 555 | int omap_device_enable(struct platform_device *pdev) |
| 556 | { |
| 557 | int ret; |
| 558 | struct omap_device *od; |
| 559 | |
| 560 | od = _find_by_pdev(pdev); |
| 561 | |
| 562 | if (od->_state == OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 24d82e3 | 2010-02-24 12:05:45 -0700 | [diff] [blame] | 563 | WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n", |
| 564 | od->pdev.name, od->pdev.id, __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 565 | return -EINVAL; |
| 566 | } |
| 567 | |
| 568 | /* Enable everything if we're enabling this device from scratch */ |
| 569 | if (od->_state == OMAP_DEVICE_STATE_UNKNOWN) |
| 570 | od->pm_lat_level = od->pm_lats_cnt; |
| 571 | |
| 572 | ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT); |
| 573 | |
| 574 | od->dev_wakeup_lat = 0; |
Kevin Hilman | 5f1b6ef | 2009-12-08 16:34:22 -0700 | [diff] [blame] | 575 | od->_dev_wakeup_lat_limit = UINT_MAX; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 576 | od->_state = OMAP_DEVICE_STATE_ENABLED; |
| 577 | |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * omap_device_idle - idle an omap_device |
| 583 | * @od: struct omap_device * to idle |
| 584 | * |
| 585 | * Idle omap_device @od by calling as many .deactivate_func() entries |
| 586 | * in the omap_device's pm_lats table as is possible without exceeding |
| 587 | * the device's maximum wakeup latency limit, pm_lat_limit. Device |
| 588 | * drivers should call this function (through platform_data function |
| 589 | * pointers) where they would normally disable clocks after operations |
| 590 | * complete, etc.. Returns -EINVAL if the omap_device is not |
| 591 | * currently enabled, or passes along the return value of |
| 592 | * _omap_device_deactivate(). |
| 593 | */ |
| 594 | int omap_device_idle(struct platform_device *pdev) |
| 595 | { |
| 596 | int ret; |
| 597 | struct omap_device *od; |
| 598 | |
| 599 | od = _find_by_pdev(pdev); |
| 600 | |
| 601 | if (od->_state != OMAP_DEVICE_STATE_ENABLED) { |
Kevin Hilman | 24d82e3 | 2010-02-24 12:05:45 -0700 | [diff] [blame] | 602 | WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n", |
| 603 | od->pdev.name, od->pdev.id, __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 604 | return -EINVAL; |
| 605 | } |
| 606 | |
| 607 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); |
| 608 | |
| 609 | od->_state = OMAP_DEVICE_STATE_IDLE; |
| 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * omap_device_shutdown - shut down an omap_device |
| 616 | * @od: struct omap_device * to shut down |
| 617 | * |
| 618 | * Shut down omap_device @od by calling all .deactivate_func() entries |
| 619 | * in the omap_device's pm_lats table and then shutting down all of |
| 620 | * the underlying omap_hwmods. Used when a device is being "removed" |
| 621 | * or a device driver is being unloaded. Returns -EINVAL if the |
| 622 | * omap_device is not currently enabled or idle, or passes along the |
| 623 | * return value of _omap_device_deactivate(). |
| 624 | */ |
| 625 | int omap_device_shutdown(struct platform_device *pdev) |
| 626 | { |
| 627 | int ret, i; |
| 628 | struct omap_device *od; |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 629 | |
| 630 | od = _find_by_pdev(pdev); |
| 631 | |
| 632 | if (od->_state != OMAP_DEVICE_STATE_ENABLED && |
| 633 | od->_state != OMAP_DEVICE_STATE_IDLE) { |
Kevin Hilman | 24d82e3 | 2010-02-24 12:05:45 -0700 | [diff] [blame] | 634 | WARN(1, "omap_device: %s.%d: %s() called from invalid state %d\n", |
| 635 | od->pdev.name, od->pdev.id, __func__, od->_state); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT); |
| 640 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 641 | for (i = 0; i < od->hwmods_cnt; i++) |
| 642 | omap_hwmod_shutdown(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 643 | |
| 644 | od->_state = OMAP_DEVICE_STATE_SHUTDOWN; |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim |
| 651 | * @od: struct omap_device * |
| 652 | * |
| 653 | * When a device's maximum wakeup latency limit changes, call some of |
| 654 | * the .activate_func or .deactivate_func function pointers in the |
| 655 | * omap_device's pm_lats array to ensure that the device's maximum |
| 656 | * wakeup latency is less than or equal to the new latency limit. |
| 657 | * Intended to be called by OMAP PM code whenever a device's maximum |
| 658 | * wakeup latency limit changes (e.g., via |
| 659 | * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be |
| 660 | * done (e.g., if the omap_device is not currently idle, or if the |
| 661 | * wakeup latency is already current with the new limit) or passes |
| 662 | * along the return value of _omap_device_deactivate() or |
| 663 | * _omap_device_activate(). |
| 664 | */ |
| 665 | int omap_device_align_pm_lat(struct platform_device *pdev, |
| 666 | u32 new_wakeup_lat_limit) |
| 667 | { |
| 668 | int ret = -EINVAL; |
| 669 | struct omap_device *od; |
| 670 | |
| 671 | od = _find_by_pdev(pdev); |
| 672 | |
| 673 | if (new_wakeup_lat_limit == od->dev_wakeup_lat) |
| 674 | return 0; |
| 675 | |
| 676 | od->_dev_wakeup_lat_limit = new_wakeup_lat_limit; |
| 677 | |
| 678 | if (od->_state != OMAP_DEVICE_STATE_IDLE) |
| 679 | return 0; |
| 680 | else if (new_wakeup_lat_limit > od->dev_wakeup_lat) |
| 681 | ret = _omap_device_deactivate(od, USE_WAKEUP_LAT); |
| 682 | else if (new_wakeup_lat_limit < od->dev_wakeup_lat) |
| 683 | ret = _omap_device_activate(od, USE_WAKEUP_LAT); |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | /** |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 689 | * omap_device_get_pwrdm - return the powerdomain * associated with @od |
| 690 | * @od: struct omap_device * |
| 691 | * |
| 692 | * Return the powerdomain associated with the first underlying |
| 693 | * omap_hwmod for this omap_device. Intended for use by core OMAP PM |
| 694 | * code. Returns NULL on error or a struct powerdomain * upon |
| 695 | * success. |
| 696 | */ |
| 697 | struct powerdomain *omap_device_get_pwrdm(struct omap_device *od) |
| 698 | { |
| 699 | /* |
| 700 | * XXX Assumes that all omap_hwmod powerdomains are identical. |
| 701 | * This may not necessarily be true. There should be a sanity |
| 702 | * check in here to WARN() if any difference appears. |
| 703 | */ |
| 704 | if (!od->hwmods_cnt) |
| 705 | return NULL; |
| 706 | |
| 707 | return omap_hwmod_get_pwrdm(od->hwmods[0]); |
| 708 | } |
| 709 | |
Paul Walmsley | db2a60b | 2010-07-26 16:34:33 -0600 | [diff] [blame] | 710 | /** |
| 711 | * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base |
| 712 | * @od: struct omap_device * |
| 713 | * |
| 714 | * Return the MPU's virtual address for the base of the hwmod, from |
| 715 | * the ioremap() that the hwmod code does. Only valid if there is one |
| 716 | * hwmod associated with this device. Returns NULL if there are zero |
| 717 | * or more than one hwmods associated with this omap_device; |
| 718 | * otherwise, passes along the return value from |
| 719 | * omap_hwmod_get_mpu_rt_va(). |
| 720 | */ |
| 721 | void __iomem *omap_device_get_rt_va(struct omap_device *od) |
| 722 | { |
| 723 | if (od->hwmods_cnt != 1) |
| 724 | return NULL; |
| 725 | |
| 726 | return omap_hwmod_get_mpu_rt_va(od->hwmods[0]); |
| 727 | } |
| 728 | |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 729 | /* |
| 730 | * Public functions intended for use in omap_device_pm_latency |
| 731 | * .activate_func and .deactivate_func function pointers |
| 732 | */ |
| 733 | |
| 734 | /** |
| 735 | * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods |
| 736 | * @od: struct omap_device *od |
| 737 | * |
| 738 | * Enable all underlying hwmods. Returns 0. |
| 739 | */ |
| 740 | int omap_device_enable_hwmods(struct omap_device *od) |
| 741 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 742 | int i; |
| 743 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 744 | for (i = 0; i < od->hwmods_cnt; i++) |
| 745 | omap_hwmod_enable(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 746 | |
| 747 | /* XXX pass along return value here? */ |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods |
| 753 | * @od: struct omap_device *od |
| 754 | * |
| 755 | * Idle all underlying hwmods. Returns 0. |
| 756 | */ |
| 757 | int omap_device_idle_hwmods(struct omap_device *od) |
| 758 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 759 | int i; |
| 760 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 761 | for (i = 0; i < od->hwmods_cnt; i++) |
| 762 | omap_hwmod_idle(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 763 | |
| 764 | /* XXX pass along return value here? */ |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * omap_device_disable_clocks - disable all main and interface clocks |
| 770 | * @od: struct omap_device *od |
| 771 | * |
| 772 | * Disable the main functional clock and interface clock for all of the |
| 773 | * omap_hwmods associated with the omap_device. Returns 0. |
| 774 | */ |
| 775 | int omap_device_disable_clocks(struct omap_device *od) |
| 776 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 777 | int i; |
| 778 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 779 | for (i = 0; i < od->hwmods_cnt; i++) |
| 780 | omap_hwmod_disable_clocks(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 781 | |
| 782 | /* XXX pass along return value here? */ |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * omap_device_enable_clocks - enable all main and interface clocks |
| 788 | * @od: struct omap_device *od |
| 789 | * |
| 790 | * Enable the main functional clock and interface clock for all of the |
| 791 | * omap_hwmods associated with the omap_device. Returns 0. |
| 792 | */ |
| 793 | int omap_device_enable_clocks(struct omap_device *od) |
| 794 | { |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 795 | int i; |
| 796 | |
Kishon Vijay Abraham I | f39f489 | 2010-09-24 10:23:18 -0600 | [diff] [blame] | 797 | for (i = 0; i < od->hwmods_cnt; i++) |
| 798 | omap_hwmod_enable_clocks(od->hwmods[i]); |
Paul Walmsley | b04b65a | 2009-09-03 20:14:05 +0300 | [diff] [blame] | 799 | |
| 800 | /* XXX pass along return value here? */ |
| 801 | return 0; |
| 802 | } |
Kevin Hilman | 0d5e825 | 2010-08-23 08:10:55 -0700 | [diff] [blame] | 803 | |
| 804 | struct device omap_device_parent = { |
| 805 | .init_name = "omap", |
| 806 | .parent = &platform_bus, |
| 807 | }; |
| 808 | |
| 809 | static int __init omap_device_init(void) |
| 810 | { |
| 811 | return device_register(&omap_device_parent); |
| 812 | } |
| 813 | core_initcall(omap_device_init); |