diff options
Diffstat (limited to 'drivers/devfreq/devfreq.c')
| -rw-r--r-- | drivers/devfreq/devfreq.c | 442 |
1 files changed, 189 insertions, 253 deletions
diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c index b146d76f04cf..1aaf1aeb1f1d 100644 --- a/drivers/devfreq/devfreq.c +++ b/drivers/devfreq/devfreq.c | |||
| @@ -30,17 +30,11 @@ | |||
| 30 | struct class *devfreq_class; | 30 | struct class *devfreq_class; |
| 31 | 31 | ||
| 32 | /* | 32 | /* |
| 33 | * devfreq_work periodically monitors every registered device. | 33 | * devfreq core provides delayed work based load monitoring helper |
| 34 | * The minimum polling interval is one jiffy. The polling interval is | 34 | * functions. Governors can use these or can implement their own |
| 35 | * determined by the minimum polling period among all polling devfreq | 35 | * monitoring mechanism. |
| 36 | * devices. The resolution of polling interval is one jiffy. | ||
| 37 | */ | 36 | */ |
| 38 | static bool polling; | ||
| 39 | static struct workqueue_struct *devfreq_wq; | 37 | static struct workqueue_struct *devfreq_wq; |
| 40 | static struct delayed_work devfreq_work; | ||
| 41 | |||
| 42 | /* wait removing if this is to be removed */ | ||
| 43 | static struct devfreq *wait_remove_device; | ||
| 44 | 38 | ||
| 45 | /* The list of all device-devfreq */ | 39 | /* The list of all device-devfreq */ |
| 46 | static LIST_HEAD(devfreq_list); | 40 | static LIST_HEAD(devfreq_list); |
| @@ -72,6 +66,8 @@ static struct devfreq *find_device_devfreq(struct device *dev) | |||
| 72 | return ERR_PTR(-ENODEV); | 66 | return ERR_PTR(-ENODEV); |
| 73 | } | 67 | } |
| 74 | 68 | ||
| 69 | /* Load monitoring helper functions for governors use */ | ||
| 70 | |||
| 75 | /** | 71 | /** |
| 76 | * update_devfreq() - Reevaluate the device and configure frequency. | 72 | * update_devfreq() - Reevaluate the device and configure frequency. |
| 77 | * @devfreq: the devfreq instance. | 73 | * @devfreq: the devfreq instance. |
| @@ -121,6 +117,152 @@ int update_devfreq(struct devfreq *devfreq) | |||
| 121 | } | 117 | } |
| 122 | 118 | ||
| 123 | /** | 119 | /** |
| 120 | * devfreq_monitor() - Periodically poll devfreq objects. | ||
| 121 | * @work: the work struct used to run devfreq_monitor periodically. | ||
| 122 | * | ||
| 123 | */ | ||
| 124 | static void devfreq_monitor(struct work_struct *work) | ||
| 125 | { | ||
| 126 | int err; | ||
| 127 | struct devfreq *devfreq = container_of(work, | ||
| 128 | struct devfreq, work.work); | ||
| 129 | |||
| 130 | mutex_lock(&devfreq->lock); | ||
| 131 | err = update_devfreq(devfreq); | ||
| 132 | if (err) | ||
| 133 | dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err); | ||
| 134 | |||
| 135 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
| 136 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
| 137 | mutex_unlock(&devfreq->lock); | ||
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * devfreq_monitor_start() - Start load monitoring of devfreq instance | ||
| 142 | * @devfreq: the devfreq instance. | ||
| 143 | * | ||
| 144 | * Helper function for starting devfreq device load monitoing. By | ||
| 145 | * default delayed work based monitoring is supported. Function | ||
| 146 | * to be called from governor in response to DEVFREQ_GOV_START | ||
| 147 | * event when device is added to devfreq framework. | ||
| 148 | */ | ||
| 149 | void devfreq_monitor_start(struct devfreq *devfreq) | ||
| 150 | { | ||
| 151 | INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); | ||
| 152 | if (devfreq->profile->polling_ms) | ||
| 153 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
| 154 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
| 155 | } | ||
| 156 | |||
| 157 | /** | ||
| 158 | * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance | ||
| 159 | * @devfreq: the devfreq instance. | ||
| 160 | * | ||
| 161 | * Helper function to stop devfreq device load monitoing. Function | ||
| 162 | * to be called from governor in response to DEVFREQ_GOV_STOP | ||
| 163 | * event when device is removed from devfreq framework. | ||
| 164 | */ | ||
| 165 | void devfreq_monitor_stop(struct devfreq *devfreq) | ||
| 166 | { | ||
| 167 | cancel_delayed_work_sync(&devfreq->work); | ||
| 168 | } | ||
| 169 | |||
| 170 | /** | ||
| 171 | * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance | ||
| 172 | * @devfreq: the devfreq instance. | ||
| 173 | * | ||
| 174 | * Helper function to suspend devfreq device load monitoing. Function | ||
| 175 | * to be called from governor in response to DEVFREQ_GOV_SUSPEND | ||
| 176 | * event or when polling interval is set to zero. | ||
| 177 | * | ||
| 178 | * Note: Though this function is same as devfreq_monitor_stop(), | ||
| 179 | * intentionally kept separate to provide hooks for collecting | ||
| 180 | * transition statistics. | ||
| 181 | */ | ||
| 182 | void devfreq_monitor_suspend(struct devfreq *devfreq) | ||
| 183 | { | ||
| 184 | mutex_lock(&devfreq->lock); | ||
| 185 | if (devfreq->stop_polling) { | ||
| 186 | mutex_unlock(&devfreq->lock); | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | |||
| 190 | devfreq->stop_polling = true; | ||
| 191 | mutex_unlock(&devfreq->lock); | ||
| 192 | cancel_delayed_work_sync(&devfreq->work); | ||
| 193 | } | ||
| 194 | |||
| 195 | /** | ||
| 196 | * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance | ||
| 197 | * @devfreq: the devfreq instance. | ||
| 198 | * | ||
| 199 | * Helper function to resume devfreq device load monitoing. Function | ||
| 200 | * to be called from governor in response to DEVFREQ_GOV_RESUME | ||
| 201 | * event or when polling interval is set to non-zero. | ||
| 202 | */ | ||
| 203 | void devfreq_monitor_resume(struct devfreq *devfreq) | ||
| 204 | { | ||
| 205 | mutex_lock(&devfreq->lock); | ||
| 206 | if (!devfreq->stop_polling) | ||
| 207 | goto out; | ||
| 208 | |||
| 209 | if (!delayed_work_pending(&devfreq->work) && | ||
| 210 | devfreq->profile->polling_ms) | ||
| 211 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
| 212 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
| 213 | devfreq->stop_polling = false; | ||
| 214 | |||
| 215 | out: | ||
| 216 | mutex_unlock(&devfreq->lock); | ||
| 217 | } | ||
| 218 | |||
| 219 | /** | ||
| 220 | * devfreq_interval_update() - Update device devfreq monitoring interval | ||
| 221 | * @devfreq: the devfreq instance. | ||
| 222 | * @delay: new polling interval to be set. | ||
| 223 | * | ||
| 224 | * Helper function to set new load monitoring polling interval. Function | ||
| 225 | * to be called from governor in response to DEVFREQ_GOV_INTERVAL event. | ||
| 226 | */ | ||
| 227 | void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay) | ||
| 228 | { | ||
| 229 | unsigned int cur_delay = devfreq->profile->polling_ms; | ||
| 230 | unsigned int new_delay = *delay; | ||
| 231 | |||
| 232 | mutex_lock(&devfreq->lock); | ||
| 233 | devfreq->profile->polling_ms = new_delay; | ||
| 234 | |||
| 235 | if (devfreq->stop_polling) | ||
| 236 | goto out; | ||
| 237 | |||
| 238 | /* if new delay is zero, stop polling */ | ||
| 239 | if (!new_delay) { | ||
| 240 | mutex_unlock(&devfreq->lock); | ||
| 241 | cancel_delayed_work_sync(&devfreq->work); | ||
| 242 | return; | ||
| 243 | } | ||
| 244 | |||
| 245 | /* if current delay is zero, start polling with new delay */ | ||
| 246 | if (!cur_delay) { | ||
| 247 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
| 248 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
| 249 | goto out; | ||
| 250 | } | ||
| 251 | |||
| 252 | /* if current delay is greater than new delay, restart polling */ | ||
| 253 | if (cur_delay > new_delay) { | ||
| 254 | mutex_unlock(&devfreq->lock); | ||
| 255 | cancel_delayed_work_sync(&devfreq->work); | ||
| 256 | mutex_lock(&devfreq->lock); | ||
| 257 | if (!devfreq->stop_polling) | ||
| 258 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
| 259 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
| 260 | } | ||
| 261 | out: | ||
| 262 | mutex_unlock(&devfreq->lock); | ||
| 263 | } | ||
| 264 | |||
| 265 | /** | ||
| 124 | * devfreq_notifier_call() - Notify that the device frequency requirements | 266 | * devfreq_notifier_call() - Notify that the device frequency requirements |
| 125 | * has been changed out of devfreq framework. | 267 | * has been changed out of devfreq framework. |
| 126 | * @nb the notifier_block (supposed to be devfreq->nb) | 268 | * @nb the notifier_block (supposed to be devfreq->nb) |
| @@ -143,59 +285,32 @@ static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, | |||
| 143 | } | 285 | } |
| 144 | 286 | ||
| 145 | /** | 287 | /** |
| 146 | * _remove_devfreq() - Remove devfreq from the device. | 288 | * _remove_devfreq() - Remove devfreq from the list and release its resources. |
| 147 | * @devfreq: the devfreq struct | 289 | * @devfreq: the devfreq struct |
| 148 | * @skip: skip calling device_unregister(). | 290 | * @skip: skip calling device_unregister(). |
| 149 | * | ||
| 150 | * Note that the caller should lock devfreq->lock before calling | ||
| 151 | * this. _remove_devfreq() will unlock it and free devfreq | ||
| 152 | * internally. devfreq_list_lock should be locked by the caller | ||
| 153 | * as well (not relased at return) | ||
| 154 | * | ||
| 155 | * Lock usage: | ||
| 156 | * devfreq->lock: locked before call. | ||
| 157 | * unlocked at return (and freed) | ||
| 158 | * devfreq_list_lock: locked before call. | ||
| 159 | * kept locked at return. | ||
| 160 | * if devfreq is centrally polled. | ||
| 161 | * | ||
| 162 | * Freed memory: | ||
| 163 | * devfreq | ||
| 164 | */ | 291 | */ |
| 165 | static void _remove_devfreq(struct devfreq *devfreq, bool skip) | 292 | static void _remove_devfreq(struct devfreq *devfreq, bool skip) |
| 166 | { | 293 | { |
| 167 | if (!mutex_is_locked(&devfreq->lock)) { | 294 | mutex_lock(&devfreq_list_lock); |
| 168 | WARN(true, "devfreq->lock must be locked by the caller.\n"); | 295 | if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) { |
| 169 | return; | 296 | mutex_unlock(&devfreq_list_lock); |
| 170 | } | 297 | dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n"); |
| 171 | if (!devfreq->governor->no_central_polling && | ||
| 172 | !mutex_is_locked(&devfreq_list_lock)) { | ||
| 173 | WARN(true, "devfreq_list_lock must be locked by the caller.\n"); | ||
| 174 | return; | 298 | return; |
| 175 | } | 299 | } |
| 300 | list_del(&devfreq->node); | ||
| 301 | mutex_unlock(&devfreq_list_lock); | ||
| 176 | 302 | ||
| 177 | if (devfreq->being_removed) | 303 | devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_STOP, NULL); |
| 178 | return; | ||
| 179 | |||
| 180 | devfreq->being_removed = true; | ||
| 181 | 304 | ||
| 182 | if (devfreq->profile->exit) | 305 | if (devfreq->profile->exit) |
| 183 | devfreq->profile->exit(devfreq->dev.parent); | 306 | devfreq->profile->exit(devfreq->dev.parent); |
| 184 | 307 | ||
| 185 | if (devfreq->governor->exit) | ||
| 186 | devfreq->governor->exit(devfreq); | ||
| 187 | |||
| 188 | if (!skip && get_device(&devfreq->dev)) { | 308 | if (!skip && get_device(&devfreq->dev)) { |
| 189 | device_unregister(&devfreq->dev); | 309 | device_unregister(&devfreq->dev); |
| 190 | put_device(&devfreq->dev); | 310 | put_device(&devfreq->dev); |
| 191 | } | 311 | } |
| 192 | 312 | ||
| 193 | if (!devfreq->governor->no_central_polling) | ||
| 194 | list_del(&devfreq->node); | ||
| 195 | |||
| 196 | mutex_unlock(&devfreq->lock); | ||
| 197 | mutex_destroy(&devfreq->lock); | 313 | mutex_destroy(&devfreq->lock); |
| 198 | |||
| 199 | kfree(devfreq); | 314 | kfree(devfreq); |
| 200 | } | 315 | } |
| 201 | 316 | ||
| @@ -210,130 +325,8 @@ static void _remove_devfreq(struct devfreq *devfreq, bool skip) | |||
| 210 | static void devfreq_dev_release(struct device *dev) | 325 | static void devfreq_dev_release(struct device *dev) |
| 211 | { | 326 | { |
| 212 | struct devfreq *devfreq = to_devfreq(dev); | 327 | struct devfreq *devfreq = to_devfreq(dev); |
| 213 | bool central_polling = !devfreq->governor->no_central_polling; | ||
| 214 | |||
| 215 | /* | ||
| 216 | * If devfreq_dev_release() was called by device_unregister() of | ||
| 217 | * _remove_devfreq(), we cannot mutex_lock(&devfreq->lock) and | ||
| 218 | * being_removed is already set. This also partially checks the case | ||
| 219 | * where devfreq_dev_release() is called from a thread other than | ||
| 220 | * the one called _remove_devfreq(); however, this case is | ||
| 221 | * dealt completely with another following being_removed check. | ||
| 222 | * | ||
| 223 | * Because being_removed is never being | ||
| 224 | * unset, we do not need to worry about race conditions on | ||
| 225 | * being_removed. | ||
| 226 | */ | ||
| 227 | if (devfreq->being_removed) | ||
| 228 | return; | ||
| 229 | 328 | ||
| 230 | if (central_polling) | ||
| 231 | mutex_lock(&devfreq_list_lock); | ||
| 232 | |||
| 233 | mutex_lock(&devfreq->lock); | ||
| 234 | |||
| 235 | /* | ||
| 236 | * Check being_removed flag again for the case where | ||
| 237 | * devfreq_dev_release() was called in a thread other than the one | ||
| 238 | * possibly called _remove_devfreq(). | ||
| 239 | */ | ||
| 240 | if (devfreq->being_removed) { | ||
| 241 | mutex_unlock(&devfreq->lock); | ||
| 242 | goto out; | ||
| 243 | } | ||
| 244 | |||
| 245 | /* devfreq->lock is unlocked and removed in _removed_devfreq() */ | ||
| 246 | _remove_devfreq(devfreq, true); | 329 | _remove_devfreq(devfreq, true); |
| 247 | |||
| 248 | out: | ||
| 249 | if (central_polling) | ||
| 250 | mutex_unlock(&devfreq_list_lock); | ||
| 251 | } | ||
| 252 | |||
| 253 | /** | ||
| 254 | * devfreq_monitor() - Periodically poll devfreq objects. | ||
| 255 | * @work: the work struct used to run devfreq_monitor periodically. | ||
| 256 | * | ||
| 257 | */ | ||
| 258 | static void devfreq_monitor(struct work_struct *work) | ||
| 259 | { | ||
| 260 | static unsigned long last_polled_at; | ||
| 261 | struct devfreq *devfreq, *tmp; | ||
| 262 | int error; | ||
| 263 | unsigned long jiffies_passed; | ||
| 264 | unsigned long next_jiffies = ULONG_MAX, now = jiffies; | ||
| 265 | struct device *dev; | ||
| 266 | |||
| 267 | /* Initially last_polled_at = 0, polling every device at bootup */ | ||
| 268 | jiffies_passed = now - last_polled_at; | ||
| 269 | last_polled_at = now; | ||
| 270 | if (jiffies_passed == 0) | ||
| 271 | jiffies_passed = 1; | ||
| 272 | |||
| 273 | mutex_lock(&devfreq_list_lock); | ||
| 274 | list_for_each_entry_safe(devfreq, tmp, &devfreq_list, node) { | ||
| 275 | mutex_lock(&devfreq->lock); | ||
| 276 | dev = devfreq->dev.parent; | ||
| 277 | |||
| 278 | /* Do not remove tmp for a while */ | ||
| 279 | wait_remove_device = tmp; | ||
| 280 | |||
| 281 | if (devfreq->governor->no_central_polling || | ||
| 282 | devfreq->next_polling == 0) { | ||
| 283 | mutex_unlock(&devfreq->lock); | ||
| 284 | continue; | ||
| 285 | } | ||
| 286 | mutex_unlock(&devfreq_list_lock); | ||
| 287 | |||
| 288 | /* | ||
| 289 | * Reduce more next_polling if devfreq_wq took an extra | ||
| 290 | * delay. (i.e., CPU has been idled.) | ||
| 291 | */ | ||
| 292 | if (devfreq->next_polling <= jiffies_passed) { | ||
| 293 | error = update_devfreq(devfreq); | ||
| 294 | |||
| 295 | /* Remove a devfreq with an error. */ | ||
| 296 | if (error && error != -EAGAIN) { | ||
| 297 | |||
| 298 | dev_err(dev, "Due to update_devfreq error(%d), devfreq(%s) is removed from the device\n", | ||
| 299 | error, devfreq->governor->name); | ||
| 300 | |||
| 301 | /* | ||
| 302 | * Unlock devfreq before locking the list | ||
| 303 | * in order to avoid deadlock with | ||
| 304 | * find_device_devfreq or others | ||
| 305 | */ | ||
| 306 | mutex_unlock(&devfreq->lock); | ||
| 307 | mutex_lock(&devfreq_list_lock); | ||
| 308 | /* Check if devfreq is already removed */ | ||
| 309 | if (IS_ERR(find_device_devfreq(dev))) | ||
| 310 | continue; | ||
| 311 | mutex_lock(&devfreq->lock); | ||
| 312 | /* This unlocks devfreq->lock and free it */ | ||
| 313 | _remove_devfreq(devfreq, false); | ||
| 314 | continue; | ||
| 315 | } | ||
| 316 | devfreq->next_polling = devfreq->polling_jiffies; | ||
| 317 | } else { | ||
| 318 | devfreq->next_polling -= jiffies_passed; | ||
| 319 | } | ||
| 320 | |||
| 321 | if (devfreq->next_polling) | ||
| 322 | next_jiffies = (next_jiffies > devfreq->next_polling) ? | ||
| 323 | devfreq->next_polling : next_jiffies; | ||
| 324 | |||
| 325 | mutex_unlock(&devfreq->lock); | ||
| 326 | mutex_lock(&devfreq_list_lock); | ||
| 327 | } | ||
| 328 | wait_remove_device = NULL; | ||
| 329 | mutex_unlock(&devfreq_list_lock); | ||
| 330 | |||
| 331 | if (next_jiffies > 0 && next_jiffies < ULONG_MAX) { | ||
| 332 | polling = true; | ||
| 333 | queue_delayed_work(devfreq_wq, &devfreq_work, next_jiffies); | ||
| 334 | } else { | ||
| 335 | polling = false; | ||
| 336 | } | ||
| 337 | } | 330 | } |
| 338 | 331 | ||
| 339 | /** | 332 | /** |
| @@ -357,16 +350,13 @@ struct devfreq *devfreq_add_device(struct device *dev, | |||
| 357 | return ERR_PTR(-EINVAL); | 350 | return ERR_PTR(-EINVAL); |
| 358 | } | 351 | } |
| 359 | 352 | ||
| 360 | 353 | mutex_lock(&devfreq_list_lock); | |
| 361 | if (!governor->no_central_polling) { | 354 | devfreq = find_device_devfreq(dev); |
| 362 | mutex_lock(&devfreq_list_lock); | 355 | mutex_unlock(&devfreq_list_lock); |
| 363 | devfreq = find_device_devfreq(dev); | 356 | if (!IS_ERR(devfreq)) { |
| 364 | mutex_unlock(&devfreq_list_lock); | 357 | dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__); |
| 365 | if (!IS_ERR(devfreq)) { | 358 | err = -EINVAL; |
| 366 | dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__); | 359 | goto err_out; |
| 367 | err = -EINVAL; | ||
| 368 | goto err_out; | ||
| 369 | } | ||
| 370 | } | 360 | } |
| 371 | 361 | ||
| 372 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); | 362 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); |
| @@ -386,48 +376,41 @@ struct devfreq *devfreq_add_device(struct device *dev, | |||
| 386 | devfreq->governor = governor; | 376 | devfreq->governor = governor; |
| 387 | devfreq->previous_freq = profile->initial_freq; | 377 | devfreq->previous_freq = profile->initial_freq; |
| 388 | devfreq->data = data; | 378 | devfreq->data = data; |
| 389 | devfreq->next_polling = devfreq->polling_jiffies | ||
| 390 | = msecs_to_jiffies(devfreq->profile->polling_ms); | ||
| 391 | devfreq->nb.notifier_call = devfreq_notifier_call; | 379 | devfreq->nb.notifier_call = devfreq_notifier_call; |
| 392 | 380 | ||
| 393 | dev_set_name(&devfreq->dev, dev_name(dev)); | 381 | dev_set_name(&devfreq->dev, dev_name(dev)); |
| 394 | err = device_register(&devfreq->dev); | 382 | err = device_register(&devfreq->dev); |
| 395 | if (err) { | 383 | if (err) { |
| 396 | put_device(&devfreq->dev); | 384 | put_device(&devfreq->dev); |
| 385 | mutex_unlock(&devfreq->lock); | ||
| 397 | goto err_dev; | 386 | goto err_dev; |
| 398 | } | 387 | } |
| 399 | 388 | ||
| 400 | if (governor->init) | ||
| 401 | err = governor->init(devfreq); | ||
| 402 | if (err) | ||
| 403 | goto err_init; | ||
| 404 | |||
| 405 | mutex_unlock(&devfreq->lock); | 389 | mutex_unlock(&devfreq->lock); |
| 406 | 390 | ||
| 407 | if (governor->no_central_polling) | ||
| 408 | goto out; | ||
| 409 | |||
| 410 | mutex_lock(&devfreq_list_lock); | 391 | mutex_lock(&devfreq_list_lock); |
| 411 | |||
| 412 | list_add(&devfreq->node, &devfreq_list); | 392 | list_add(&devfreq->node, &devfreq_list); |
| 393 | mutex_unlock(&devfreq_list_lock); | ||
| 413 | 394 | ||
| 414 | if (devfreq_wq && devfreq->next_polling && !polling) { | 395 | err = devfreq->governor->event_handler(devfreq, |
| 415 | polling = true; | 396 | DEVFREQ_GOV_START, NULL); |
| 416 | queue_delayed_work(devfreq_wq, &devfreq_work, | 397 | if (err) { |
| 417 | devfreq->next_polling); | 398 | dev_err(dev, "%s: Unable to start governor for the device\n", |
| 399 | __func__); | ||
| 400 | goto err_init; | ||
| 418 | } | 401 | } |
| 419 | mutex_unlock(&devfreq_list_lock); | 402 | |
| 420 | out: | ||
| 421 | return devfreq; | 403 | return devfreq; |
| 422 | 404 | ||
| 423 | err_init: | 405 | err_init: |
| 406 | list_del(&devfreq->node); | ||
| 424 | device_unregister(&devfreq->dev); | 407 | device_unregister(&devfreq->dev); |
| 425 | err_dev: | 408 | err_dev: |
| 426 | mutex_unlock(&devfreq->lock); | ||
| 427 | kfree(devfreq); | 409 | kfree(devfreq); |
| 428 | err_out: | 410 | err_out: |
| 429 | return ERR_PTR(err); | 411 | return ERR_PTR(err); |
| 430 | } | 412 | } |
| 413 | EXPORT_SYMBOL(devfreq_add_device); | ||
| 431 | 414 | ||
| 432 | /** | 415 | /** |
| 433 | * devfreq_remove_device() - Remove devfreq feature from a device. | 416 | * devfreq_remove_device() - Remove devfreq feature from a device. |
| @@ -435,30 +418,14 @@ err_out: | |||
| 435 | */ | 418 | */ |
| 436 | int devfreq_remove_device(struct devfreq *devfreq) | 419 | int devfreq_remove_device(struct devfreq *devfreq) |
| 437 | { | 420 | { |
| 438 | bool central_polling; | ||
| 439 | |||
| 440 | if (!devfreq) | 421 | if (!devfreq) |
| 441 | return -EINVAL; | 422 | return -EINVAL; |
| 442 | 423 | ||
| 443 | central_polling = !devfreq->governor->no_central_polling; | 424 | _remove_devfreq(devfreq, false); |
| 444 | |||
| 445 | if (central_polling) { | ||
| 446 | mutex_lock(&devfreq_list_lock); | ||
| 447 | while (wait_remove_device == devfreq) { | ||
| 448 | mutex_unlock(&devfreq_list_lock); | ||
| 449 | schedule(); | ||
| 450 | mutex_lock(&devfreq_list_lock); | ||
| 451 | } | ||
| 452 | } | ||
| 453 | |||
| 454 | mutex_lock(&devfreq->lock); | ||
| 455 | _remove_devfreq(devfreq, false); /* it unlocks devfreq->lock */ | ||
| 456 | |||
| 457 | if (central_polling) | ||
| 458 | mutex_unlock(&devfreq_list_lock); | ||
| 459 | 425 | ||
| 460 | return 0; | 426 | return 0; |
| 461 | } | 427 | } |
| 428 | EXPORT_SYMBOL(devfreq_remove_device); | ||
| 462 | 429 | ||
| 463 | static ssize_t show_governor(struct device *dev, | 430 | static ssize_t show_governor(struct device *dev, |
| 464 | struct device_attribute *attr, char *buf) | 431 | struct device_attribute *attr, char *buf) |
| @@ -490,35 +457,13 @@ static ssize_t store_polling_interval(struct device *dev, | |||
| 490 | if (ret != 1) | 457 | if (ret != 1) |
| 491 | goto out; | 458 | goto out; |
| 492 | 459 | ||
| 493 | mutex_lock(&df->lock); | 460 | df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value); |
| 494 | df->profile->polling_ms = value; | ||
| 495 | df->next_polling = df->polling_jiffies | ||
| 496 | = msecs_to_jiffies(value); | ||
| 497 | mutex_unlock(&df->lock); | ||
| 498 | |||
| 499 | ret = count; | 461 | ret = count; |
| 500 | 462 | ||
| 501 | if (df->governor->no_central_polling) | ||
| 502 | goto out; | ||
| 503 | |||
| 504 | mutex_lock(&devfreq_list_lock); | ||
| 505 | if (df->next_polling > 0 && !polling) { | ||
| 506 | polling = true; | ||
| 507 | queue_delayed_work(devfreq_wq, &devfreq_work, | ||
| 508 | df->next_polling); | ||
| 509 | } | ||
| 510 | mutex_unlock(&devfreq_list_lock); | ||
| 511 | out: | 463 | out: |
| 512 | return ret; | 464 | return ret; |
| 513 | } | 465 | } |
| 514 | 466 | ||
| 515 | static ssize_t show_central_polling(struct device *dev, | ||
| 516 | struct device_attribute *attr, char *buf) | ||
| 517 | { | ||
| 518 | return sprintf(buf, "%d\n", | ||
| 519 | !to_devfreq(dev)->governor->no_central_polling); | ||
| 520 | } | ||
| 521 | |||
| 522 | static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, | 467 | static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, |
| 523 | const char *buf, size_t count) | 468 | const char *buf, size_t count) |
| 524 | { | 469 | { |
| @@ -590,7 +535,6 @@ static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr, | |||
| 590 | static struct device_attribute devfreq_attrs[] = { | 535 | static struct device_attribute devfreq_attrs[] = { |
| 591 | __ATTR(governor, S_IRUGO, show_governor, NULL), | 536 | __ATTR(governor, S_IRUGO, show_governor, NULL), |
| 592 | __ATTR(cur_freq, S_IRUGO, show_freq, NULL), | 537 | __ATTR(cur_freq, S_IRUGO, show_freq, NULL), |
| 593 | __ATTR(central_polling, S_IRUGO, show_central_polling, NULL), | ||
| 594 | __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, | 538 | __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, |
| 595 | store_polling_interval), | 539 | store_polling_interval), |
| 596 | __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq), | 540 | __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq), |
| @@ -598,23 +542,6 @@ static struct device_attribute devfreq_attrs[] = { | |||
| 598 | { }, | 542 | { }, |
| 599 | }; | 543 | }; |
| 600 | 544 | ||
| 601 | /** | ||
| 602 | * devfreq_start_polling() - Initialize data structure for devfreq framework and | ||
| 603 | * start polling registered devfreq devices. | ||
| 604 | */ | ||
| 605 | static int __init devfreq_start_polling(void) | ||
| 606 | { | ||
| 607 | mutex_lock(&devfreq_list_lock); | ||
| 608 | polling = false; | ||
| 609 | devfreq_wq = create_freezable_workqueue("devfreq_wq"); | ||
| 610 | INIT_DEFERRABLE_WORK(&devfreq_work, devfreq_monitor); | ||
| 611 | mutex_unlock(&devfreq_list_lock); | ||
| 612 | |||
| 613 | devfreq_monitor(&devfreq_work.work); | ||
| 614 | return 0; | ||
| 615 | } | ||
| 616 | late_initcall(devfreq_start_polling); | ||
| 617 | |||
| 618 | static int __init devfreq_init(void) | 545 | static int __init devfreq_init(void) |
| 619 | { | 546 | { |
| 620 | devfreq_class = class_create(THIS_MODULE, "devfreq"); | 547 | devfreq_class = class_create(THIS_MODULE, "devfreq"); |
| @@ -622,7 +549,15 @@ static int __init devfreq_init(void) | |||
| 622 | pr_err("%s: couldn't create class\n", __FILE__); | 549 | pr_err("%s: couldn't create class\n", __FILE__); |
| 623 | return PTR_ERR(devfreq_class); | 550 | return PTR_ERR(devfreq_class); |
| 624 | } | 551 | } |
| 552 | |||
| 553 | devfreq_wq = create_freezable_workqueue("devfreq_wq"); | ||
| 554 | if (IS_ERR(devfreq_wq)) { | ||
| 555 | class_destroy(devfreq_class); | ||
| 556 | pr_err("%s: couldn't create workqueue\n", __FILE__); | ||
| 557 | return PTR_ERR(devfreq_wq); | ||
| 558 | } | ||
| 625 | devfreq_class->dev_attrs = devfreq_attrs; | 559 | devfreq_class->dev_attrs = devfreq_attrs; |
| 560 | |||
| 626 | return 0; | 561 | return 0; |
| 627 | } | 562 | } |
| 628 | subsys_initcall(devfreq_init); | 563 | subsys_initcall(devfreq_init); |
| @@ -630,6 +565,7 @@ subsys_initcall(devfreq_init); | |||
| 630 | static void __exit devfreq_exit(void) | 565 | static void __exit devfreq_exit(void) |
| 631 | { | 566 | { |
| 632 | class_destroy(devfreq_class); | 567 | class_destroy(devfreq_class); |
| 568 | destroy_workqueue(devfreq_wq); | ||
| 633 | } | 569 | } |
| 634 | module_exit(devfreq_exit); | 570 | module_exit(devfreq_exit); |
| 635 | 571 | ||
