1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Author: Kamil Debski <k.debski@samsung.com>
10 #include <linux/hwmon.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mutex.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>
28 u8 pulses_per_revolution;
33 struct pwm_device *pwm;
34 struct pwm_state pwm_state;
35 struct regulator *reg_en;
38 struct pwm_fan_tach *tachs;
40 struct timer_list rpm_timer;
42 unsigned int pwm_value;
43 unsigned int pwm_fan_state;
44 unsigned int pwm_fan_max_state;
45 unsigned int *pwm_fan_cooling_levels;
46 struct thermal_cooling_device *cdev;
48 struct hwmon_chip_info info;
49 struct hwmon_channel_info fan_channel;
52 /* This handler assumes self resetting edge triggered interrupt. */
53 static irqreturn_t pulse_handler(int irq, void *dev_id)
55 struct pwm_fan_tach *tach = dev_id;
57 atomic_inc(&tach->pulses);
62 static void sample_timer(struct timer_list *t)
64 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
65 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
69 for (i = 0; i < ctx->tach_count; i++) {
70 struct pwm_fan_tach *tach = &ctx->tachs[i];
73 pulses = atomic_read(&tach->pulses);
74 atomic_sub(pulses, &tach->pulses);
75 tach->rpm = (unsigned int)(pulses * 1000 * 60) /
76 (tach->pulses_per_revolution * delta);
79 ctx->sample_start = ktime_get();
82 mod_timer(&ctx->rpm_timer, jiffies + HZ);
85 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
89 struct pwm_state *state = &ctx->pwm_state;
91 mutex_lock(&ctx->lock);
92 if (ctx->pwm_value == pwm)
93 goto exit_set_pwm_err;
95 period = state->period;
96 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
97 state->enabled = pwm ? true : false;
99 ret = pwm_apply_state(ctx->pwm, state);
101 ctx->pwm_value = pwm;
103 mutex_unlock(&ctx->lock);
107 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
111 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
112 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
115 ctx->pwm_fan_state = i;
118 static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
119 u32 attr, int channel, long val)
121 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
124 if (val < 0 || val > MAX_PWM)
127 ret = __set_pwm(ctx, val);
131 pwm_fan_update_state(ctx, val);
135 static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
136 u32 attr, int channel, long *val)
138 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
142 *val = ctx->pwm_value;
146 *val = ctx->tachs[channel].rpm;
154 static umode_t pwm_fan_is_visible(const void *data,
155 enum hwmon_sensor_types type,
156 u32 attr, int channel)
170 static const struct hwmon_ops pwm_fan_hwmon_ops = {
171 .is_visible = pwm_fan_is_visible,
172 .read = pwm_fan_read,
173 .write = pwm_fan_write,
176 /* thermal cooling device callbacks */
177 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
178 unsigned long *state)
180 struct pwm_fan_ctx *ctx = cdev->devdata;
185 *state = ctx->pwm_fan_max_state;
190 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
191 unsigned long *state)
193 struct pwm_fan_ctx *ctx = cdev->devdata;
198 *state = ctx->pwm_fan_state;
204 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
206 struct pwm_fan_ctx *ctx = cdev->devdata;
209 if (!ctx || (state > ctx->pwm_fan_max_state))
212 if (state == ctx->pwm_fan_state)
215 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
217 dev_err(&cdev->device, "Cannot set pwm!\n");
221 ctx->pwm_fan_state = state;
226 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
227 .get_max_state = pwm_fan_get_max_state,
228 .get_cur_state = pwm_fan_get_cur_state,
229 .set_cur_state = pwm_fan_set_cur_state,
232 static int pwm_fan_of_get_cooling_data(struct device *dev,
233 struct pwm_fan_ctx *ctx)
235 struct device_node *np = dev->of_node;
238 if (!of_find_property(np, "cooling-levels", NULL))
241 ret = of_property_count_u32_elems(np, "cooling-levels");
243 dev_err(dev, "Wrong data!\n");
244 return ret ? : -EINVAL;
248 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
250 if (!ctx->pwm_fan_cooling_levels)
253 ret = of_property_read_u32_array(np, "cooling-levels",
254 ctx->pwm_fan_cooling_levels, num);
256 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
260 for (i = 0; i < num; i++) {
261 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
262 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
263 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
268 ctx->pwm_fan_max_state = num - 1;
273 static void pwm_fan_regulator_disable(void *data)
275 regulator_disable(data);
278 static void pwm_fan_pwm_disable(void *__ctx)
280 struct pwm_fan_ctx *ctx = __ctx;
282 ctx->pwm_state.enabled = false;
283 pwm_apply_state(ctx->pwm, &ctx->pwm_state);
284 del_timer_sync(&ctx->rpm_timer);
287 static int pwm_fan_probe(struct platform_device *pdev)
289 struct thermal_cooling_device *cdev;
290 struct device *dev = &pdev->dev;
291 struct pwm_fan_ctx *ctx;
292 struct device *hwmon;
294 const struct hwmon_channel_info **channels;
295 u32 *fan_channel_config;
296 int channel_count = 1; /* We always have a PWM channel. */
299 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
303 mutex_init(&ctx->lock);
305 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
306 if (IS_ERR(ctx->pwm))
307 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
309 platform_set_drvdata(pdev, ctx);
311 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
312 if (IS_ERR(ctx->reg_en)) {
313 if (PTR_ERR(ctx->reg_en) != -ENODEV)
314 return PTR_ERR(ctx->reg_en);
318 ret = regulator_enable(ctx->reg_en);
320 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
323 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
329 pwm_init_state(ctx->pwm, &ctx->pwm_state);
332 * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
333 * long. Check this here to prevent the fan running at a too low
336 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
337 dev_err(dev, "Configured period too big\n");
341 /* Set duty cycle to maximum allowed and enable PWM output */
342 ret = __set_pwm(ctx, MAX_PWM);
344 dev_err(dev, "Failed to configure PWM: %d\n", ret);
347 timer_setup(&ctx->rpm_timer, sample_timer, 0);
348 ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
352 ctx->tach_count = platform_irq_count(pdev);
353 if (ctx->tach_count < 0)
354 return dev_err_probe(dev, ctx->tach_count,
355 "Could not get number of fan tachometer inputs\n");
356 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count);
358 if (ctx->tach_count) {
359 channel_count++; /* We also have a FAN channel. */
361 ctx->tachs = devm_kcalloc(dev, ctx->tach_count,
362 sizeof(struct pwm_fan_tach),
367 ctx->fan_channel.type = hwmon_fan;
368 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1,
369 sizeof(u32), GFP_KERNEL);
370 if (!fan_channel_config)
372 ctx->fan_channel.config = fan_channel_config;
375 channels = devm_kcalloc(dev, channel_count + 1,
376 sizeof(struct hwmon_channel_info *), GFP_KERNEL);
380 channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT);
382 for (i = 0; i < ctx->tach_count; i++) {
383 struct pwm_fan_tach *tach = &ctx->tachs[i];
386 tach->irq = platform_get_irq(pdev, i);
387 if (tach->irq == -EPROBE_DEFER)
390 ret = devm_request_irq(dev, tach->irq, pulse_handler, 0,
394 "Failed to request interrupt: %d\n",
400 of_property_read_u32_index(dev->of_node,
401 "pulses-per-revolution",
404 tach->pulses_per_revolution = ppr;
405 if (!tach->pulses_per_revolution) {
406 dev_err(dev, "pulses-per-revolution can't be zero.\n");
410 fan_channel_config[i] = HWMON_F_INPUT;
412 dev_dbg(dev, "tach%d: irq=%d, pulses_per_revolution=%d\n",
413 i, tach->irq, tach->pulses_per_revolution);
416 if (ctx->tach_count > 0) {
417 ctx->sample_start = ktime_get();
418 mod_timer(&ctx->rpm_timer, jiffies + HZ);
420 channels[1] = &ctx->fan_channel;
423 ctx->info.ops = &pwm_fan_hwmon_ops;
424 ctx->info.info = channels;
426 hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
427 ctx, &ctx->info, NULL);
429 dev_err(dev, "Failed to register hwmon device\n");
430 return PTR_ERR(hwmon);
433 ret = pwm_fan_of_get_cooling_data(dev, ctx);
437 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
438 if (IS_ENABLED(CONFIG_THERMAL)) {
439 cdev = devm_thermal_of_cooling_device_register(dev,
440 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
444 "Failed to register pwm-fan as cooling device: %d\n",
454 static int pwm_fan_disable(struct device *dev)
456 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
459 if (ctx->pwm_value) {
460 /* keep ctx->pwm_state unmodified for pwm_fan_resume() */
461 struct pwm_state state = ctx->pwm_state;
463 state.duty_cycle = 0;
464 state.enabled = false;
465 ret = pwm_apply_state(ctx->pwm, &state);
471 ret = regulator_disable(ctx->reg_en);
473 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
481 static void pwm_fan_shutdown(struct platform_device *pdev)
483 pwm_fan_disable(&pdev->dev);
486 #ifdef CONFIG_PM_SLEEP
487 static int pwm_fan_suspend(struct device *dev)
489 return pwm_fan_disable(dev);
492 static int pwm_fan_resume(struct device *dev)
494 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
498 ret = regulator_enable(ctx->reg_en);
500 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
505 if (ctx->pwm_value == 0)
508 return pwm_apply_state(ctx->pwm, &ctx->pwm_state);
512 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
514 static const struct of_device_id of_pwm_fan_match[] = {
515 { .compatible = "pwm-fan", },
518 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
520 static struct platform_driver pwm_fan_driver = {
521 .probe = pwm_fan_probe,
522 .shutdown = pwm_fan_shutdown,
526 .of_match_table = of_pwm_fan_match,
530 module_platform_driver(pwm_fan_driver);
532 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
533 MODULE_ALIAS("platform:pwm-fan");
534 MODULE_DESCRIPTION("PWM FAN driver");
535 MODULE_LICENSE("GPL");