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