.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * pwm-fan.c - Hwmon driver for fans connected to PWM lines. |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
---|
5 | 6 | * |
---|
6 | 7 | * Author: Kamil Debski <k.debski@samsung.com> |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or modify |
---|
9 | | - * it under the terms of the GNU General Public License as published by |
---|
10 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
11 | | - * (at your option) any later version. |
---|
12 | | - * |
---|
13 | | - * This program is distributed in the hope that it will be useful, |
---|
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | | - * GNU General Public License for more details. |
---|
17 | 8 | */ |
---|
18 | 9 | |
---|
19 | 10 | #include <linux/hwmon.h> |
---|
20 | 11 | #include <linux/hwmon-sysfs.h> |
---|
| 12 | +#include <linux/interrupt.h> |
---|
21 | 13 | #include <linux/module.h> |
---|
22 | 14 | #include <linux/mutex.h> |
---|
23 | 15 | #include <linux/of.h> |
---|
24 | 16 | #include <linux/platform_device.h> |
---|
25 | 17 | #include <linux/pwm.h> |
---|
| 18 | +#include <linux/regulator/consumer.h> |
---|
26 | 19 | #include <linux/sysfs.h> |
---|
27 | 20 | #include <linux/thermal.h> |
---|
| 21 | +#include <linux/timer.h> |
---|
| 22 | +#include <soc/rockchip/rockchip_system_monitor.h> |
---|
28 | 23 | |
---|
29 | 24 | #define MAX_PWM 255 |
---|
| 25 | + |
---|
| 26 | +struct thermal_trips { |
---|
| 27 | + int temp; |
---|
| 28 | + int state; |
---|
| 29 | +}; |
---|
30 | 30 | |
---|
31 | 31 | struct pwm_fan_ctx { |
---|
32 | 32 | struct mutex lock; |
---|
33 | 33 | struct pwm_device *pwm; |
---|
| 34 | + struct regulator *reg_en; |
---|
| 35 | + |
---|
| 36 | + int irq; |
---|
| 37 | + atomic_t pulses; |
---|
| 38 | + unsigned int rpm; |
---|
| 39 | + u8 pulses_per_revolution; |
---|
| 40 | + ktime_t sample_start; |
---|
| 41 | + struct timer_list rpm_timer; |
---|
| 42 | + |
---|
34 | 43 | unsigned int pwm_value; |
---|
35 | 44 | unsigned int pwm_fan_state; |
---|
36 | 45 | unsigned int pwm_fan_max_state; |
---|
37 | 46 | unsigned int *pwm_fan_cooling_levels; |
---|
38 | 47 | struct thermal_cooling_device *cdev; |
---|
| 48 | + struct notifier_block thermal_nb; |
---|
| 49 | + struct thermal_trips *thermal_trips; |
---|
| 50 | + bool thermal_notifier_is_ok; |
---|
39 | 51 | }; |
---|
| 52 | + |
---|
| 53 | +/* This handler assumes self resetting edge triggered interrupt. */ |
---|
| 54 | +static irqreturn_t pulse_handler(int irq, void *dev_id) |
---|
| 55 | +{ |
---|
| 56 | + struct pwm_fan_ctx *ctx = dev_id; |
---|
| 57 | + |
---|
| 58 | + atomic_inc(&ctx->pulses); |
---|
| 59 | + |
---|
| 60 | + return IRQ_HANDLED; |
---|
| 61 | +} |
---|
| 62 | + |
---|
| 63 | +static void sample_timer(struct timer_list *t) |
---|
| 64 | +{ |
---|
| 65 | + struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); |
---|
| 66 | + unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); |
---|
| 67 | + int pulses; |
---|
| 68 | + |
---|
| 69 | + if (delta) { |
---|
| 70 | + pulses = atomic_read(&ctx->pulses); |
---|
| 71 | + atomic_sub(pulses, &ctx->pulses); |
---|
| 72 | + ctx->rpm = (unsigned int)(pulses * 1000 * 60) / |
---|
| 73 | + (ctx->pulses_per_revolution * delta); |
---|
| 74 | + |
---|
| 75 | + ctx->sample_start = ktime_get(); |
---|
| 76 | + } |
---|
| 77 | + |
---|
| 78 | + mod_timer(&ctx->rpm_timer, jiffies + HZ); |
---|
| 79 | +} |
---|
40 | 80 | |
---|
41 | 81 | static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) |
---|
42 | 82 | { |
---|
.. | .. |
---|
72 | 112 | ctx->pwm_fan_state = i; |
---|
73 | 113 | } |
---|
74 | 114 | |
---|
75 | | -static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
---|
76 | | - const char *buf, size_t count) |
---|
| 115 | +static ssize_t pwm_store(struct device *dev, struct device_attribute *attr, |
---|
| 116 | + const char *buf, size_t count) |
---|
77 | 117 | { |
---|
78 | 118 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
---|
79 | 119 | unsigned long pwm; |
---|
.. | .. |
---|
90 | 130 | return count; |
---|
91 | 131 | } |
---|
92 | 132 | |
---|
93 | | -static ssize_t show_pwm(struct device *dev, |
---|
94 | | - struct device_attribute *attr, char *buf) |
---|
| 133 | +static ssize_t pwm_show(struct device *dev, struct device_attribute *attr, |
---|
| 134 | + char *buf) |
---|
95 | 135 | { |
---|
96 | 136 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
---|
97 | 137 | |
---|
98 | 138 | return sprintf(buf, "%u\n", ctx->pwm_value); |
---|
99 | 139 | } |
---|
100 | 140 | |
---|
| 141 | +static ssize_t rpm_show(struct device *dev, |
---|
| 142 | + struct device_attribute *attr, char *buf) |
---|
| 143 | +{ |
---|
| 144 | + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
---|
101 | 145 | |
---|
102 | | -static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); |
---|
| 146 | + return sprintf(buf, "%u\n", ctx->rpm); |
---|
| 147 | +} |
---|
| 148 | + |
---|
| 149 | +static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); |
---|
| 150 | +static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0); |
---|
103 | 151 | |
---|
104 | 152 | static struct attribute *pwm_fan_attrs[] = { |
---|
105 | 153 | &sensor_dev_attr_pwm1.dev_attr.attr, |
---|
| 154 | + &sensor_dev_attr_fan1_input.dev_attr.attr, |
---|
106 | 155 | NULL, |
---|
107 | 156 | }; |
---|
108 | 157 | |
---|
109 | | -ATTRIBUTE_GROUPS(pwm_fan); |
---|
| 158 | +static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a, |
---|
| 159 | + int n) |
---|
| 160 | +{ |
---|
| 161 | + struct device *dev = container_of(kobj, struct device, kobj); |
---|
| 162 | + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
---|
| 163 | + |
---|
| 164 | + /* Hide fan_input in case no interrupt is available */ |
---|
| 165 | + if (n == 1 && ctx->irq <= 0) |
---|
| 166 | + return 0; |
---|
| 167 | + |
---|
| 168 | + return a->mode; |
---|
| 169 | +} |
---|
| 170 | + |
---|
| 171 | +static const struct attribute_group pwm_fan_group = { |
---|
| 172 | + .attrs = pwm_fan_attrs, |
---|
| 173 | + .is_visible = pwm_fan_attrs_visible, |
---|
| 174 | +}; |
---|
| 175 | + |
---|
| 176 | +static const struct attribute_group *pwm_fan_groups[] = { |
---|
| 177 | + &pwm_fan_group, |
---|
| 178 | + NULL, |
---|
| 179 | +}; |
---|
110 | 180 | |
---|
111 | 181 | /* thermal cooling device callbacks */ |
---|
112 | 182 | static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev, |
---|
.. | .. |
---|
205 | 275 | return 0; |
---|
206 | 276 | } |
---|
207 | 277 | |
---|
| 278 | +static void pwm_fan_regulator_disable(void *data) |
---|
| 279 | +{ |
---|
| 280 | + regulator_disable(data); |
---|
| 281 | +} |
---|
| 282 | + |
---|
| 283 | +static void pwm_fan_pwm_disable(void *__ctx) |
---|
| 284 | +{ |
---|
| 285 | + struct pwm_fan_ctx *ctx = __ctx; |
---|
| 286 | + pwm_disable(ctx->pwm); |
---|
| 287 | + del_timer_sync(&ctx->rpm_timer); |
---|
| 288 | +} |
---|
| 289 | + |
---|
| 290 | +static int pwm_fan_get_thermal_trips(struct device *dev, char *porp_name, |
---|
| 291 | + struct thermal_trips **trips) |
---|
| 292 | +{ |
---|
| 293 | + struct device_node *np = dev->of_node; |
---|
| 294 | + struct thermal_trips *thermal_trips; |
---|
| 295 | + const struct property *prop; |
---|
| 296 | + int count, i; |
---|
| 297 | + |
---|
| 298 | + prop = of_find_property(np, porp_name, NULL); |
---|
| 299 | + if (!prop) |
---|
| 300 | + return -EINVAL; |
---|
| 301 | + if (!prop->value) |
---|
| 302 | + return -ENODATA; |
---|
| 303 | + count = of_property_count_u32_elems(np, porp_name); |
---|
| 304 | + if (count < 0) |
---|
| 305 | + return -EINVAL; |
---|
| 306 | + if (count % 2) |
---|
| 307 | + return -EINVAL; |
---|
| 308 | + thermal_trips = devm_kzalloc(dev, |
---|
| 309 | + sizeof(*thermal_trips) * (count / 2 + 1), |
---|
| 310 | + GFP_KERNEL); |
---|
| 311 | + if (!thermal_trips) |
---|
| 312 | + return -ENOMEM; |
---|
| 313 | + |
---|
| 314 | + for (i = 0; i < count / 2; i++) { |
---|
| 315 | + of_property_read_u32_index(np, porp_name, 2 * i, |
---|
| 316 | + &thermal_trips[i].temp); |
---|
| 317 | + of_property_read_u32_index(np, porp_name, 2 * i + 1, |
---|
| 318 | + &thermal_trips[i].state); |
---|
| 319 | + } |
---|
| 320 | + thermal_trips[i].temp = 0; |
---|
| 321 | + thermal_trips[i].state = INT_MAX; |
---|
| 322 | + |
---|
| 323 | + *trips = thermal_trips; |
---|
| 324 | + |
---|
| 325 | + return 0; |
---|
| 326 | +} |
---|
| 327 | + |
---|
| 328 | +static int pwm_fan_temp_to_state(struct pwm_fan_ctx *ctx, int temp) |
---|
| 329 | +{ |
---|
| 330 | + struct thermal_trips *trips = ctx->thermal_trips; |
---|
| 331 | + int i, state = 0; |
---|
| 332 | + |
---|
| 333 | + for (i = 0; trips[i].state != INT_MAX; i++) { |
---|
| 334 | + if (temp >= trips[i].temp) |
---|
| 335 | + state = trips[i].state; |
---|
| 336 | + } |
---|
| 337 | + |
---|
| 338 | + return state; |
---|
| 339 | +} |
---|
| 340 | + |
---|
| 341 | +static int pwm_fan_thermal_notifier_call(struct notifier_block *nb, |
---|
| 342 | + unsigned long event, void *data) |
---|
| 343 | +{ |
---|
| 344 | + struct pwm_fan_ctx *ctx = container_of(nb, struct pwm_fan_ctx, thermal_nb); |
---|
| 345 | + struct system_monitor_event_data *event_data = data; |
---|
| 346 | + int state, ret; |
---|
| 347 | + |
---|
| 348 | + if (event != SYSTEM_MONITOR_CHANGE_TEMP) |
---|
| 349 | + return NOTIFY_OK; |
---|
| 350 | + |
---|
| 351 | + state = pwm_fan_temp_to_state(ctx, event_data->temp); |
---|
| 352 | + if (state > ctx->pwm_fan_max_state) |
---|
| 353 | + return NOTIFY_BAD; |
---|
| 354 | + if (state == ctx->pwm_fan_state) |
---|
| 355 | + return NOTIFY_OK; |
---|
| 356 | + |
---|
| 357 | + ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); |
---|
| 358 | + if (ret) |
---|
| 359 | + return NOTIFY_BAD; |
---|
| 360 | + |
---|
| 361 | + ctx->pwm_fan_state = state; |
---|
| 362 | + |
---|
| 363 | + return NOTIFY_OK; |
---|
| 364 | +} |
---|
| 365 | + |
---|
| 366 | +static int pwm_fan_register_thermal_notifier(struct device *dev, |
---|
| 367 | + struct pwm_fan_ctx *ctx) |
---|
| 368 | +{ |
---|
| 369 | + if (pwm_fan_get_thermal_trips(dev, "rockchip,temp-trips", |
---|
| 370 | + &ctx->thermal_trips)) |
---|
| 371 | + return -EINVAL; |
---|
| 372 | + |
---|
| 373 | + ctx->thermal_nb.notifier_call = pwm_fan_thermal_notifier_call; |
---|
| 374 | + |
---|
| 375 | + return rockchip_system_monitor_register_notifier(&ctx->thermal_nb); |
---|
| 376 | +} |
---|
| 377 | + |
---|
208 | 378 | static int pwm_fan_probe(struct platform_device *pdev) |
---|
209 | 379 | { |
---|
210 | 380 | struct thermal_cooling_device *cdev; |
---|
| 381 | + struct device *dev = &pdev->dev; |
---|
211 | 382 | struct pwm_fan_ctx *ctx; |
---|
212 | 383 | struct device *hwmon; |
---|
213 | 384 | int ret; |
---|
214 | 385 | struct pwm_state state = { }; |
---|
| 386 | + u32 ppr = 2; |
---|
215 | 387 | |
---|
216 | | - ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); |
---|
| 388 | + ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); |
---|
217 | 389 | if (!ctx) |
---|
218 | 390 | return -ENOMEM; |
---|
219 | 391 | |
---|
220 | 392 | mutex_init(&ctx->lock); |
---|
221 | 393 | |
---|
222 | | - ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL); |
---|
223 | | - if (IS_ERR(ctx->pwm)) { |
---|
224 | | - ret = PTR_ERR(ctx->pwm); |
---|
225 | | - |
---|
226 | | - if (ret != -EPROBE_DEFER) |
---|
227 | | - dev_err(&pdev->dev, "Could not get PWM: %d\n", ret); |
---|
228 | | - |
---|
229 | | - return ret; |
---|
230 | | - } |
---|
| 394 | + ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); |
---|
| 395 | + if (IS_ERR(ctx->pwm)) |
---|
| 396 | + return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n"); |
---|
231 | 397 | |
---|
232 | 398 | platform_set_drvdata(pdev, ctx); |
---|
233 | 399 | |
---|
| 400 | + ctx->irq = platform_get_irq_optional(pdev, 0); |
---|
| 401 | + if (ctx->irq == -EPROBE_DEFER) |
---|
| 402 | + return ctx->irq; |
---|
| 403 | + |
---|
| 404 | + ctx->reg_en = devm_regulator_get_optional(dev, "fan"); |
---|
| 405 | + if (IS_ERR(ctx->reg_en)) { |
---|
| 406 | + if (PTR_ERR(ctx->reg_en) != -ENODEV) |
---|
| 407 | + return PTR_ERR(ctx->reg_en); |
---|
| 408 | + |
---|
| 409 | + ctx->reg_en = NULL; |
---|
| 410 | + } else { |
---|
| 411 | + ret = regulator_enable(ctx->reg_en); |
---|
| 412 | + if (ret) { |
---|
| 413 | + dev_err(dev, "Failed to enable fan supply: %d\n", ret); |
---|
| 414 | + return ret; |
---|
| 415 | + } |
---|
| 416 | + ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable, |
---|
| 417 | + ctx->reg_en); |
---|
| 418 | + if (ret) |
---|
| 419 | + return ret; |
---|
| 420 | + } |
---|
| 421 | + |
---|
234 | 422 | ctx->pwm_value = MAX_PWM; |
---|
235 | 423 | |
---|
236 | | - /* Set duty cycle to maximum allowed and enable PWM output */ |
---|
237 | 424 | pwm_init_state(ctx->pwm, &state); |
---|
| 425 | + /* |
---|
| 426 | + * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned |
---|
| 427 | + * long. Check this here to prevent the fan running at a too low |
---|
| 428 | + * frequency. |
---|
| 429 | + */ |
---|
| 430 | + if (state.period > ULONG_MAX / MAX_PWM + 1) { |
---|
| 431 | + dev_err(dev, "Configured period too big\n"); |
---|
| 432 | + return -EINVAL; |
---|
| 433 | + } |
---|
| 434 | + |
---|
| 435 | + /* Set duty cycle to maximum allowed and enable PWM output */ |
---|
238 | 436 | state.duty_cycle = ctx->pwm->args.period - 1; |
---|
239 | 437 | state.enabled = true; |
---|
240 | 438 | |
---|
241 | 439 | ret = pwm_apply_state(ctx->pwm, &state); |
---|
242 | 440 | if (ret) { |
---|
243 | | - dev_err(&pdev->dev, "Failed to configure PWM\n"); |
---|
| 441 | + dev_err(dev, "Failed to configure PWM: %d\n", ret); |
---|
244 | 442 | return ret; |
---|
245 | 443 | } |
---|
| 444 | + timer_setup(&ctx->rpm_timer, sample_timer, 0); |
---|
| 445 | + ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx); |
---|
| 446 | + if (ret) |
---|
| 447 | + return ret; |
---|
246 | 448 | |
---|
247 | | - hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan", |
---|
248 | | - ctx, pwm_fan_groups); |
---|
249 | | - if (IS_ERR(hwmon)) { |
---|
250 | | - dev_err(&pdev->dev, "Failed to register hwmon device\n"); |
---|
251 | | - ret = PTR_ERR(hwmon); |
---|
252 | | - goto err_pwm_disable; |
---|
| 449 | + of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr); |
---|
| 450 | + ctx->pulses_per_revolution = ppr; |
---|
| 451 | + if (!ctx->pulses_per_revolution) { |
---|
| 452 | + dev_err(dev, "pulses-per-revolution can't be zero.\n"); |
---|
| 453 | + return -EINVAL; |
---|
253 | 454 | } |
---|
254 | 455 | |
---|
255 | | - ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx); |
---|
| 456 | + if (ctx->irq > 0) { |
---|
| 457 | + ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0, |
---|
| 458 | + pdev->name, ctx); |
---|
| 459 | + if (ret) { |
---|
| 460 | + dev_err(dev, "Failed to request interrupt: %d\n", ret); |
---|
| 461 | + return ret; |
---|
| 462 | + } |
---|
| 463 | + ctx->sample_start = ktime_get(); |
---|
| 464 | + mod_timer(&ctx->rpm_timer, jiffies + HZ); |
---|
| 465 | + } |
---|
| 466 | + |
---|
| 467 | + hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan", |
---|
| 468 | + ctx, pwm_fan_groups); |
---|
| 469 | + if (IS_ERR(hwmon)) { |
---|
| 470 | + dev_err(dev, "Failed to register hwmon device\n"); |
---|
| 471 | + return PTR_ERR(hwmon); |
---|
| 472 | + } |
---|
| 473 | + |
---|
| 474 | + ret = pwm_fan_of_get_cooling_data(dev, ctx); |
---|
256 | 475 | if (ret) |
---|
257 | | - goto err_pwm_disable; |
---|
| 476 | + return ret; |
---|
258 | 477 | |
---|
259 | 478 | ctx->pwm_fan_state = ctx->pwm_fan_max_state; |
---|
| 479 | + if (IS_REACHABLE(CONFIG_ROCKCHIP_SYSTEM_MONITOR) && |
---|
| 480 | + of_find_property(dev->of_node, "rockchip,temp-trips", NULL)) { |
---|
| 481 | + ret = pwm_fan_register_thermal_notifier(dev, ctx); |
---|
| 482 | + if (ret) |
---|
| 483 | + dev_err(dev, "Failed to register thermal notifier: %d\n", ret); |
---|
| 484 | + else |
---|
| 485 | + ctx->thermal_notifier_is_ok = true; |
---|
| 486 | + return 0; |
---|
| 487 | + } |
---|
260 | 488 | if (IS_ENABLED(CONFIG_THERMAL)) { |
---|
261 | | - cdev = thermal_of_cooling_device_register(pdev->dev.of_node, |
---|
262 | | - "pwm-fan", ctx, |
---|
263 | | - &pwm_fan_cooling_ops); |
---|
| 489 | + cdev = devm_thermal_of_cooling_device_register(dev, |
---|
| 490 | + dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); |
---|
264 | 491 | if (IS_ERR(cdev)) { |
---|
265 | | - dev_err(&pdev->dev, |
---|
266 | | - "Failed to register pwm-fan as cooling device"); |
---|
267 | 492 | ret = PTR_ERR(cdev); |
---|
268 | | - goto err_pwm_disable; |
---|
| 493 | + dev_err(dev, |
---|
| 494 | + "Failed to register pwm-fan as cooling device: %d\n", |
---|
| 495 | + ret); |
---|
| 496 | + return ret; |
---|
269 | 497 | } |
---|
270 | 498 | ctx->cdev = cdev; |
---|
271 | 499 | thermal_cdev_update(cdev); |
---|
272 | 500 | } |
---|
273 | 501 | |
---|
274 | 502 | return 0; |
---|
275 | | - |
---|
276 | | -err_pwm_disable: |
---|
277 | | - state.enabled = false; |
---|
278 | | - pwm_apply_state(ctx->pwm, &state); |
---|
279 | | - |
---|
280 | | - return ret; |
---|
281 | 503 | } |
---|
282 | 504 | |
---|
283 | | -static int pwm_fan_remove(struct platform_device *pdev) |
---|
284 | | -{ |
---|
285 | | - struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); |
---|
286 | | - |
---|
287 | | - thermal_cooling_device_unregister(ctx->cdev); |
---|
288 | | - if (ctx->pwm_value) |
---|
289 | | - pwm_disable(ctx->pwm); |
---|
290 | | - return 0; |
---|
291 | | -} |
---|
292 | | - |
---|
293 | | -#ifdef CONFIG_PM_SLEEP |
---|
294 | | -static int pwm_fan_suspend(struct device *dev) |
---|
| 505 | +static int pwm_fan_disable(struct device *dev) |
---|
295 | 506 | { |
---|
296 | 507 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); |
---|
297 | 508 | struct pwm_args args; |
---|
.. | .. |
---|
299 | 510 | |
---|
300 | 511 | pwm_get_args(ctx->pwm, &args); |
---|
301 | 512 | |
---|
302 | | - if (ctx->pwm_value) { |
---|
| 513 | + if (ctx->pwm_value || ctx->thermal_notifier_is_ok) { |
---|
303 | 514 | ret = pwm_config(ctx->pwm, 0, args.period); |
---|
304 | 515 | if (ret < 0) |
---|
305 | 516 | return ret; |
---|
.. | .. |
---|
307 | 518 | pwm_disable(ctx->pwm); |
---|
308 | 519 | } |
---|
309 | 520 | |
---|
| 521 | + if (ctx->reg_en) { |
---|
| 522 | + ret = regulator_disable(ctx->reg_en); |
---|
| 523 | + if (ret) { |
---|
| 524 | + dev_err(dev, "Failed to disable fan supply: %d\n", ret); |
---|
| 525 | + return ret; |
---|
| 526 | + } |
---|
| 527 | + } |
---|
| 528 | + |
---|
310 | 529 | return 0; |
---|
| 530 | +} |
---|
| 531 | + |
---|
| 532 | +static void pwm_fan_shutdown(struct platform_device *pdev) |
---|
| 533 | +{ |
---|
| 534 | + pwm_fan_disable(&pdev->dev); |
---|
| 535 | +} |
---|
| 536 | + |
---|
| 537 | +#ifdef CONFIG_PM_SLEEP |
---|
| 538 | +static int pwm_fan_suspend(struct device *dev) |
---|
| 539 | +{ |
---|
| 540 | + return pwm_fan_disable(dev); |
---|
311 | 541 | } |
---|
312 | 542 | |
---|
313 | 543 | static int pwm_fan_resume(struct device *dev) |
---|
.. | .. |
---|
317 | 547 | unsigned long duty; |
---|
318 | 548 | int ret; |
---|
319 | 549 | |
---|
320 | | - if (ctx->pwm_value == 0) |
---|
| 550 | + if (ctx->reg_en) { |
---|
| 551 | + ret = regulator_enable(ctx->reg_en); |
---|
| 552 | + if (ret) { |
---|
| 553 | + dev_err(dev, "Failed to enable fan supply: %d\n", ret); |
---|
| 554 | + return ret; |
---|
| 555 | + } |
---|
| 556 | + } |
---|
| 557 | + |
---|
| 558 | + if (ctx->pwm_value == 0 && !ctx->thermal_notifier_is_ok) |
---|
321 | 559 | return 0; |
---|
322 | 560 | |
---|
323 | 561 | pwm_get_args(ctx->pwm, &pargs); |
---|
.. | .. |
---|
339 | 577 | |
---|
340 | 578 | static struct platform_driver pwm_fan_driver = { |
---|
341 | 579 | .probe = pwm_fan_probe, |
---|
342 | | - .remove = pwm_fan_remove, |
---|
| 580 | + .shutdown = pwm_fan_shutdown, |
---|
343 | 581 | .driver = { |
---|
344 | 582 | .name = "pwm-fan", |
---|
345 | 583 | .pm = &pwm_fan_pm, |
---|