Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 *
7 * Author: Kamil Debski <k.debski@samsung.com>
8 */
9
10#include <linux/delay.h>
11#include <linux/hwmon.h>
12#include <linux/interrupt.h>
13#include <linux/mod_devicetable.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/platform_device.h>
17#include <linux/property.h>
18#include <linux/pwm.h>
19#include <linux/regulator/consumer.h>
20#include <linux/sysfs.h>
21#include <linux/thermal.h>
22#include <linux/timer.h>
23
24#define MAX_PWM 255
25
26struct pwm_fan_tach {
27 int irq;
28 atomic_t pulses;
29 unsigned int rpm;
30};
31
32enum pwm_fan_enable_mode {
33 pwm_off_reg_off,
34 pwm_disable_reg_enable,
35 pwm_enable_reg_enable,
36 pwm_disable_reg_disable,
37};
38
39struct pwm_fan_ctx {
40 struct device *dev;
41
42 struct mutex lock;
43 struct pwm_device *pwm;
44 struct pwm_state pwm_state;
45 struct regulator *reg_en;
46 enum pwm_fan_enable_mode enable_mode;
47 bool regulator_enabled;
48 bool enabled;
49
50 int tach_count;
51 struct pwm_fan_tach *tachs;
52 u32 *pulses_per_revolution;
53 ktime_t sample_start;
54 struct timer_list rpm_timer;
55
56 unsigned int pwm_value;
57 unsigned int pwm_fan_state;
58 unsigned int pwm_fan_max_state;
59 unsigned int *pwm_fan_cooling_levels;
60 struct thermal_cooling_device *cdev;
61
62 struct hwmon_chip_info info;
63 struct hwmon_channel_info fan_channel;
64
65 u64 pwm_duty_cycle_from_stopped;
66 u32 pwm_usec_from_stopped;
67 u8 pwm_shutdown;
68};
69
70/* This handler assumes self resetting edge triggered interrupt. */
71static irqreturn_t pulse_handler(int irq, void *dev_id)
72{
73 struct pwm_fan_tach *tach = dev_id;
74
75 atomic_inc(&tach->pulses);
76
77 return IRQ_HANDLED;
78}
79
80static void sample_timer(struct timer_list *t)
81{
82 struct pwm_fan_ctx *ctx = timer_container_of(ctx, t, rpm_timer);
83 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
84 int i;
85
86 if (delta) {
87 for (i = 0; i < ctx->tach_count; i++) {
88 struct pwm_fan_tach *tach = &ctx->tachs[i];
89 int pulses;
90
91 pulses = atomic_read(&tach->pulses);
92 atomic_sub(pulses, &tach->pulses);
93 tach->rpm = (unsigned int)(pulses * 1000 * 60) /
94 (ctx->pulses_per_revolution[i] * delta);
95 }
96
97 ctx->sample_start = ktime_get();
98 }
99
100 mod_timer(&ctx->rpm_timer, jiffies + HZ);
101}
102
103static void pwm_fan_enable_mode_2_state(int enable_mode,
104 struct pwm_state *state,
105 bool *enable_regulator)
106{
107 switch (enable_mode) {
108 case pwm_disable_reg_enable:
109 /* disable pwm, keep regulator enabled */
110 state->enabled = false;
111 *enable_regulator = true;
112 break;
113 case pwm_enable_reg_enable:
114 /* keep pwm and regulator enabled */
115 state->enabled = true;
116 *enable_regulator = true;
117 break;
118 case pwm_off_reg_off:
119 case pwm_disable_reg_disable:
120 /* disable pwm and regulator */
121 state->enabled = false;
122 *enable_regulator = false;
123 }
124}
125
126static int pwm_fan_switch_power(struct pwm_fan_ctx *ctx, bool on)
127{
128 int ret = 0;
129
130 if (!ctx->reg_en)
131 return ret;
132
133 if (!ctx->regulator_enabled && on) {
134 ret = regulator_enable(ctx->reg_en);
135 if (ret == 0)
136 ctx->regulator_enabled = true;
137 } else if (ctx->regulator_enabled && !on) {
138 ret = regulator_disable(ctx->reg_en);
139 if (ret == 0)
140 ctx->regulator_enabled = false;
141 }
142 return ret;
143}
144
145static int pwm_fan_power_on(struct pwm_fan_ctx *ctx)
146{
147 struct pwm_state *state = &ctx->pwm_state;
148 int ret;
149
150 if (ctx->enabled)
151 return 0;
152
153 ret = pwm_fan_switch_power(ctx, true);
154 if (ret < 0) {
155 dev_err(ctx->dev, "failed to enable power supply\n");
156 return ret;
157 }
158
159 state->enabled = true;
160 ret = pwm_apply_might_sleep(ctx->pwm, state);
161 if (ret) {
162 dev_err(ctx->dev, "failed to enable PWM\n");
163 goto disable_regulator;
164 }
165
166 ctx->enabled = true;
167
168 return 0;
169
170disable_regulator:
171 pwm_fan_switch_power(ctx, false);
172 return ret;
173}
174
175static int pwm_fan_power_off(struct pwm_fan_ctx *ctx, bool force_disable)
176{
177 struct pwm_state *state = &ctx->pwm_state;
178 bool enable_regulator = false;
179 int ret;
180
181 if (!ctx->enabled)
182 return 0;
183
184 pwm_fan_enable_mode_2_state(ctx->enable_mode,
185 state,
186 &enable_regulator);
187
188 if (force_disable)
189 state->enabled = false;
190 state->duty_cycle = 0;
191 ret = pwm_apply_might_sleep(ctx->pwm, state);
192 if (ret) {
193 dev_err(ctx->dev, "failed to disable PWM\n");
194 return ret;
195 }
196
197 pwm_fan_switch_power(ctx, enable_regulator);
198
199 ctx->enabled = false;
200
201 return 0;
202}
203
204static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
205{
206 struct pwm_state *state = &ctx->pwm_state;
207 unsigned long final_pwm = pwm;
208 unsigned long period;
209 bool update = false;
210 int ret = 0;
211
212 if (pwm > 0) {
213 if (ctx->enable_mode == pwm_off_reg_off)
214 /* pwm-fan hard disabled */
215 return 0;
216
217 period = state->period;
218 update = state->duty_cycle < ctx->pwm_duty_cycle_from_stopped;
219 if (update)
220 state->duty_cycle = ctx->pwm_duty_cycle_from_stopped;
221 else
222 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
223 ret = pwm_apply_might_sleep(ctx->pwm, state);
224 if (ret)
225 return ret;
226 ret = pwm_fan_power_on(ctx);
227 if (!ret && update) {
228 pwm = final_pwm;
229 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
230 usleep_range(ctx->pwm_usec_from_stopped,
231 ctx->pwm_usec_from_stopped * 2);
232 ret = pwm_apply_might_sleep(ctx->pwm, state);
233 }
234 } else {
235 ret = pwm_fan_power_off(ctx, false);
236 }
237 if (!ret)
238 ctx->pwm_value = pwm;
239
240 return ret;
241}
242
243static int set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
244{
245 int ret;
246
247 mutex_lock(&ctx->lock);
248 ret = __set_pwm(ctx, pwm);
249 mutex_unlock(&ctx->lock);
250
251 return ret;
252}
253
254static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
255{
256 int i;
257
258 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
259 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
260 break;
261
262 ctx->pwm_fan_state = i;
263}
264
265static int pwm_fan_update_enable(struct pwm_fan_ctx *ctx, long val)
266{
267 int ret = 0;
268 int old_val;
269
270 mutex_lock(&ctx->lock);
271
272 if (ctx->enable_mode == val)
273 goto out;
274
275 old_val = ctx->enable_mode;
276 ctx->enable_mode = val;
277
278 if (val == 0) {
279 /* Disable pwm-fan unconditionally */
280 if (ctx->enabled)
281 ret = __set_pwm(ctx, 0);
282 else
283 ret = pwm_fan_switch_power(ctx, false);
284 if (ret)
285 ctx->enable_mode = old_val;
286 pwm_fan_update_state(ctx, 0);
287 } else {
288 /*
289 * Change PWM and/or regulator state if currently disabled
290 * Nothing to do if currently enabled
291 */
292 if (!ctx->enabled) {
293 struct pwm_state *state = &ctx->pwm_state;
294 bool enable_regulator = false;
295
296 state->duty_cycle = 0;
297 pwm_fan_enable_mode_2_state(val,
298 state,
299 &enable_regulator);
300
301 pwm_apply_might_sleep(ctx->pwm, state);
302 pwm_fan_switch_power(ctx, enable_regulator);
303 pwm_fan_update_state(ctx, 0);
304 }
305 }
306out:
307 mutex_unlock(&ctx->lock);
308
309 return ret;
310}
311
312static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
313 u32 attr, int channel, long val)
314{
315 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
316 int ret;
317
318 switch (attr) {
319 case hwmon_pwm_input:
320 if (val < 0 || val > MAX_PWM)
321 return -EINVAL;
322 ret = set_pwm(ctx, val);
323 if (ret)
324 return ret;
325 pwm_fan_update_state(ctx, val);
326 break;
327 case hwmon_pwm_enable:
328 if (val < 0 || val > 3)
329 ret = -EINVAL;
330 else
331 ret = pwm_fan_update_enable(ctx, val);
332
333 return ret;
334 default:
335 return -EOPNOTSUPP;
336 }
337
338 return 0;
339}
340
341static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
342 u32 attr, int channel, long *val)
343{
344 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
345
346 switch (type) {
347 case hwmon_pwm:
348 switch (attr) {
349 case hwmon_pwm_input:
350 *val = ctx->pwm_value;
351 return 0;
352 case hwmon_pwm_enable:
353 *val = ctx->enable_mode;
354 return 0;
355 }
356 return -EOPNOTSUPP;
357 case hwmon_fan:
358 *val = ctx->tachs[channel].rpm;
359 return 0;
360
361 default:
362 return -ENOTSUPP;
363 }
364}
365
366static umode_t pwm_fan_is_visible(const void *data,
367 enum hwmon_sensor_types type,
368 u32 attr, int channel)
369{
370 switch (type) {
371 case hwmon_pwm:
372 return 0644;
373
374 case hwmon_fan:
375 return 0444;
376
377 default:
378 return 0;
379 }
380}
381
382static const struct hwmon_ops pwm_fan_hwmon_ops = {
383 .is_visible = pwm_fan_is_visible,
384 .read = pwm_fan_read,
385 .write = pwm_fan_write,
386};
387
388/* thermal cooling device callbacks */
389static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
390 unsigned long *state)
391{
392 struct pwm_fan_ctx *ctx = cdev->devdata;
393
394 if (!ctx)
395 return -EINVAL;
396
397 *state = ctx->pwm_fan_max_state;
398
399 return 0;
400}
401
402static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
403 unsigned long *state)
404{
405 struct pwm_fan_ctx *ctx = cdev->devdata;
406
407 if (!ctx)
408 return -EINVAL;
409
410 *state = ctx->pwm_fan_state;
411
412 return 0;
413}
414
415static int
416pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
417{
418 struct pwm_fan_ctx *ctx = cdev->devdata;
419 int ret;
420
421 if (!ctx || (state > ctx->pwm_fan_max_state))
422 return -EINVAL;
423
424 if (state == ctx->pwm_fan_state)
425 return 0;
426
427 ret = set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
428 if (ret) {
429 dev_err(&cdev->device, "Cannot set pwm!\n");
430 return ret;
431 }
432
433 ctx->pwm_fan_state = state;
434
435 return ret;
436}
437
438static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
439 .get_max_state = pwm_fan_get_max_state,
440 .get_cur_state = pwm_fan_get_cur_state,
441 .set_cur_state = pwm_fan_set_cur_state,
442};
443
444static int pwm_fan_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx)
445{
446 int num, i, ret;
447
448 if (!device_property_present(dev, "cooling-levels"))
449 return 0;
450
451 ret = device_property_count_u32(dev, "cooling-levels");
452 if (ret <= 0) {
453 dev_err(dev, "Wrong data!\n");
454 return ret ? : -EINVAL;
455 }
456
457 num = ret;
458 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
459 GFP_KERNEL);
460 if (!ctx->pwm_fan_cooling_levels)
461 return -ENOMEM;
462
463 ret = device_property_read_u32_array(dev, "cooling-levels",
464 ctx->pwm_fan_cooling_levels, num);
465 if (ret) {
466 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
467 return ret;
468 }
469
470 for (i = 0; i < num; i++) {
471 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
472 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
473 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
474 return -EINVAL;
475 }
476 }
477
478 ctx->pwm_fan_max_state = num - 1;
479
480 return 0;
481}
482
483static void pwm_fan_cleanup(void *__ctx)
484{
485 struct pwm_fan_ctx *ctx = __ctx;
486
487 timer_delete_sync(&ctx->rpm_timer);
488 if (ctx->pwm_shutdown) {
489 ctx->enable_mode = pwm_enable_reg_enable;
490 __set_pwm(ctx, ctx->pwm_shutdown);
491 } else {
492 /* Switch off everything */
493 ctx->enable_mode = pwm_disable_reg_disable;
494 pwm_fan_power_off(ctx, true);
495 }
496}
497
498static int pwm_fan_probe(struct platform_device *pdev)
499{
500 struct thermal_cooling_device *cdev;
501 struct device *dev = &pdev->dev;
502 struct pwm_fan_ctx *ctx;
503 struct device *hwmon;
504 int ret;
505 const struct hwmon_channel_info **channels;
506 u32 initial_pwm, pwm_min_from_stopped = 0;
507 u32 pwm_shutdown_percent = 0;
508 u32 *fan_channel_config;
509 int channel_count = 1; /* We always have a PWM channel. */
510 int i;
511
512 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
513 if (!ctx)
514 return -ENOMEM;
515
516 mutex_init(&ctx->lock);
517
518 ctx->dev = &pdev->dev;
519 ctx->pwm = devm_pwm_get(dev, NULL);
520 if (IS_ERR(ctx->pwm))
521 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
522
523 platform_set_drvdata(pdev, ctx);
524
525 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
526 if (IS_ERR(ctx->reg_en)) {
527 if (PTR_ERR(ctx->reg_en) != -ENODEV)
528 return PTR_ERR(ctx->reg_en);
529
530 ctx->reg_en = NULL;
531 }
532
533 pwm_init_state(ctx->pwm, &ctx->pwm_state);
534
535 /*
536 * PWM fans are controlled solely by the duty cycle of the PWM signal,
537 * they do not care about the exact timing. Thus set usage_power to true
538 * to allow less flexible hardware to work as a PWM source for fan
539 * control.
540 */
541 ctx->pwm_state.usage_power = true;
542
543 /*
544 * set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
545 * long. Check this here to prevent the fan running at a too low
546 * frequency.
547 */
548 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
549 dev_err(dev, "Configured period too big\n");
550 return -EINVAL;
551 }
552
553 ctx->enable_mode = pwm_disable_reg_enable;
554
555 ret = pwm_fan_get_cooling_data(dev, ctx);
556 if (ret)
557 return ret;
558
559 /* use maximum cooling level if provided */
560 if (ctx->pwm_fan_cooling_levels)
561 initial_pwm = ctx->pwm_fan_cooling_levels[ctx->pwm_fan_max_state];
562 else
563 initial_pwm = MAX_PWM;
564
565 /*
566 * Set duty cycle to maximum allowed and enable PWM output as well as
567 * the regulator. In case of error nothing is changed
568 */
569 ret = set_pwm(ctx, initial_pwm);
570 if (ret) {
571 dev_err(dev, "Failed to configure PWM: %d\n", ret);
572 return ret;
573 }
574 timer_setup(&ctx->rpm_timer, sample_timer, 0);
575 ret = devm_add_action_or_reset(dev, pwm_fan_cleanup, ctx);
576 if (ret)
577 return ret;
578
579 ctx->tach_count = platform_irq_count(pdev);
580 if (ctx->tach_count < 0)
581 return dev_err_probe(dev, ctx->tach_count,
582 "Could not get number of fan tachometer inputs\n");
583 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count);
584
585 if (ctx->tach_count) {
586 channel_count++; /* We also have a FAN channel. */
587
588 ctx->tachs = devm_kcalloc(dev, ctx->tach_count,
589 sizeof(struct pwm_fan_tach),
590 GFP_KERNEL);
591 if (!ctx->tachs)
592 return -ENOMEM;
593
594 ctx->fan_channel.type = hwmon_fan;
595 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1,
596 sizeof(u32), GFP_KERNEL);
597 if (!fan_channel_config)
598 return -ENOMEM;
599 ctx->fan_channel.config = fan_channel_config;
600
601 ctx->pulses_per_revolution = devm_kmalloc_array(dev,
602 ctx->tach_count,
603 sizeof(*ctx->pulses_per_revolution),
604 GFP_KERNEL);
605 if (!ctx->pulses_per_revolution)
606 return -ENOMEM;
607
608 /* Setup default pulses per revolution */
609 for (i = 0; i < ctx->tach_count; i++)
610 ctx->pulses_per_revolution[i] = 2;
611
612 device_property_read_u32_array(dev, "pulses-per-revolution",
613 ctx->pulses_per_revolution, ctx->tach_count);
614 }
615
616 channels = devm_kcalloc(dev, channel_count + 1,
617 sizeof(struct hwmon_channel_info *), GFP_KERNEL);
618 if (!channels)
619 return -ENOMEM;
620
621 channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE);
622
623 for (i = 0; i < ctx->tach_count; i++) {
624 struct pwm_fan_tach *tach = &ctx->tachs[i];
625
626 tach->irq = platform_get_irq(pdev, i);
627 if (tach->irq == -EPROBE_DEFER)
628 return tach->irq;
629 if (tach->irq > 0) {
630 ret = devm_request_irq(dev, tach->irq, pulse_handler,
631 IRQF_NO_THREAD, pdev->name, tach);
632 if (ret) {
633 dev_err(dev,
634 "Failed to request interrupt: %d\n",
635 ret);
636 return ret;
637 }
638 }
639
640 if (!ctx->pulses_per_revolution[i]) {
641 dev_err(dev, "pulses-per-revolution can't be zero.\n");
642 return -EINVAL;
643 }
644
645 fan_channel_config[i] = HWMON_F_INPUT;
646
647 dev_dbg(dev, "tach%d: irq=%d, pulses_per_revolution=%d\n",
648 i, tach->irq, ctx->pulses_per_revolution[i]);
649 }
650
651 if (ctx->tach_count > 0) {
652 ctx->sample_start = ktime_get();
653 mod_timer(&ctx->rpm_timer, jiffies + HZ);
654
655 channels[1] = &ctx->fan_channel;
656 }
657
658 ret = device_property_read_u32(dev, "fan-shutdown-percent",
659 &pwm_shutdown_percent);
660 if (!ret && pwm_shutdown_percent)
661 ctx->pwm_shutdown = (clamp(pwm_shutdown_percent, 0, 100) * 255) / 100;
662
663 ret = device_property_read_u32(dev, "fan-stop-to-start-percent",
664 &pwm_min_from_stopped);
665 if (!ret && pwm_min_from_stopped) {
666 ctx->pwm_duty_cycle_from_stopped =
667 DIV_ROUND_UP_ULL(pwm_min_from_stopped *
668 (ctx->pwm_state.period - 1),
669 100);
670 }
671 ret = device_property_read_u32(dev, "fan-stop-to-start-us",
672 &ctx->pwm_usec_from_stopped);
673 if (ret)
674 ctx->pwm_usec_from_stopped = 250000;
675
676 ctx->info.ops = &pwm_fan_hwmon_ops;
677 ctx->info.info = channels;
678
679 hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
680 ctx, &ctx->info, NULL);
681 if (IS_ERR(hwmon)) {
682 dev_err(dev, "Failed to register hwmon device\n");
683 return PTR_ERR(hwmon);
684 }
685
686 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
687 if (IS_ENABLED(CONFIG_THERMAL)) {
688 cdev = devm_thermal_of_cooling_device_register(dev,
689 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
690 if (IS_ERR(cdev)) {
691 ret = PTR_ERR(cdev);
692 dev_err(dev,
693 "Failed to register pwm-fan as cooling device: %d\n",
694 ret);
695 return ret;
696 }
697 ctx->cdev = cdev;
698 }
699
700 return 0;
701}
702
703static void pwm_fan_shutdown(struct platform_device *pdev)
704{
705 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
706
707 pwm_fan_cleanup(ctx);
708}
709
710static int pwm_fan_suspend(struct device *dev)
711{
712 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
713
714 return pwm_fan_power_off(ctx, true);
715}
716
717static int pwm_fan_resume(struct device *dev)
718{
719 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
720
721 return set_pwm(ctx, ctx->pwm_value);
722}
723
724static DEFINE_SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
725
726static const struct of_device_id of_pwm_fan_match[] = {
727 { .compatible = "pwm-fan", },
728 {},
729};
730MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
731
732static struct platform_driver pwm_fan_driver = {
733 .probe = pwm_fan_probe,
734 .shutdown = pwm_fan_shutdown,
735 .driver = {
736 .name = "pwm-fan",
737 .pm = pm_sleep_ptr(&pwm_fan_pm),
738 .of_match_table = of_pwm_fan_match,
739 },
740};
741
742module_platform_driver(pwm_fan_driver);
743
744MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
745MODULE_ALIAS("platform:pwm-fan");
746MODULE_DESCRIPTION("PWM FAN driver");
747MODULE_LICENSE("GPL");