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