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