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/hwmon-sysfs.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/platform_device.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>
27 struct pwm_device *pwm;
28 struct regulator *reg_en;
33 u8 pulses_per_revolution;
35 struct timer_list rpm_timer;
37 unsigned int pwm_value;
38 unsigned int pwm_fan_state;
39 unsigned int pwm_fan_max_state;
40 unsigned int *pwm_fan_cooling_levels;
41 struct thermal_cooling_device *cdev;
44 /* This handler assumes self resetting edge triggered interrupt. */
45 static irqreturn_t pulse_handler(int irq, void *dev_id)
47 struct pwm_fan_ctx *ctx = dev_id;
49 atomic_inc(&ctx->pulses);
54 static void sample_timer(struct timer_list *t)
56 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
57 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
61 pulses = atomic_read(&ctx->pulses);
62 atomic_sub(pulses, &ctx->pulses);
63 ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
64 (ctx->pulses_per_revolution * delta);
66 ctx->sample_start = ktime_get();
69 mod_timer(&ctx->rpm_timer, jiffies + HZ);
72 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
76 struct pwm_state state = { };
78 mutex_lock(&ctx->lock);
79 if (ctx->pwm_value == pwm)
80 goto exit_set_pwm_err;
82 pwm_init_state(ctx->pwm, &state);
83 period = ctx->pwm->args.period;
84 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
85 state.enabled = pwm ? true : false;
87 ret = pwm_apply_state(ctx->pwm, &state);
91 mutex_unlock(&ctx->lock);
95 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
99 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
100 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
103 ctx->pwm_fan_state = i;
106 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
107 const char *buf, size_t count)
109 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
113 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
116 ret = __set_pwm(ctx, pwm);
120 pwm_fan_update_state(ctx, pwm);
124 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
127 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
129 return sprintf(buf, "%u\n", ctx->pwm_value);
132 static ssize_t rpm_show(struct device *dev,
133 struct device_attribute *attr, char *buf)
135 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
137 return sprintf(buf, "%u\n", ctx->rpm);
140 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
141 static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
143 static struct attribute *pwm_fan_attrs[] = {
144 &sensor_dev_attr_pwm1.dev_attr.attr,
145 &sensor_dev_attr_fan1_input.dev_attr.attr,
149 static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
152 struct device *dev = container_of(kobj, struct device, kobj);
153 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
155 /* Hide fan_input in case no interrupt is available */
156 if (n == 1 && ctx->irq <= 0)
162 static const struct attribute_group pwm_fan_group = {
163 .attrs = pwm_fan_attrs,
164 .is_visible = pwm_fan_attrs_visible,
167 static const struct attribute_group *pwm_fan_groups[] = {
172 /* thermal cooling device callbacks */
173 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
174 unsigned long *state)
176 struct pwm_fan_ctx *ctx = cdev->devdata;
181 *state = ctx->pwm_fan_max_state;
186 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
187 unsigned long *state)
189 struct pwm_fan_ctx *ctx = cdev->devdata;
194 *state = ctx->pwm_fan_state;
200 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
202 struct pwm_fan_ctx *ctx = cdev->devdata;
205 if (!ctx || (state > ctx->pwm_fan_max_state))
208 if (state == ctx->pwm_fan_state)
211 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
213 dev_err(&cdev->device, "Cannot set pwm!\n");
217 ctx->pwm_fan_state = state;
222 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
223 .get_max_state = pwm_fan_get_max_state,
224 .get_cur_state = pwm_fan_get_cur_state,
225 .set_cur_state = pwm_fan_set_cur_state,
228 static int pwm_fan_of_get_cooling_data(struct device *dev,
229 struct pwm_fan_ctx *ctx)
231 struct device_node *np = dev->of_node;
234 if (!of_find_property(np, "cooling-levels", NULL))
237 ret = of_property_count_u32_elems(np, "cooling-levels");
239 dev_err(dev, "Wrong data!\n");
240 return ret ? : -EINVAL;
244 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
246 if (!ctx->pwm_fan_cooling_levels)
249 ret = of_property_read_u32_array(np, "cooling-levels",
250 ctx->pwm_fan_cooling_levels, num);
252 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
256 for (i = 0; i < num; i++) {
257 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
258 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
259 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
264 ctx->pwm_fan_max_state = num - 1;
269 static void pwm_fan_regulator_disable(void *data)
271 regulator_disable(data);
274 static void pwm_fan_pwm_disable(void *__ctx)
276 struct pwm_fan_ctx *ctx = __ctx;
277 pwm_disable(ctx->pwm);
278 del_timer_sync(&ctx->rpm_timer);
281 static int pwm_fan_probe(struct platform_device *pdev)
283 struct thermal_cooling_device *cdev;
284 struct device *dev = &pdev->dev;
285 struct pwm_fan_ctx *ctx;
286 struct device *hwmon;
288 struct pwm_state state = { };
291 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
295 mutex_init(&ctx->lock);
297 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
298 if (IS_ERR(ctx->pwm))
299 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
301 platform_set_drvdata(pdev, ctx);
303 ctx->irq = platform_get_irq_optional(pdev, 0);
304 if (ctx->irq == -EPROBE_DEFER)
307 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
308 if (IS_ERR(ctx->reg_en)) {
309 if (PTR_ERR(ctx->reg_en) != -ENODEV)
310 return PTR_ERR(ctx->reg_en);
314 ret = regulator_enable(ctx->reg_en);
316 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
319 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
325 ctx->pwm_value = MAX_PWM;
327 pwm_init_state(ctx->pwm, &state);
329 * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
330 * long. Check this here to prevent the fan running at a too low
333 if (state.period > ULONG_MAX / MAX_PWM + 1) {
334 dev_err(dev, "Configured period too big\n");
338 /* Set duty cycle to maximum allowed and enable PWM output */
339 state.duty_cycle = ctx->pwm->args.period - 1;
340 state.enabled = true;
342 ret = pwm_apply_state(ctx->pwm, &state);
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 of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr);
353 ctx->pulses_per_revolution = ppr;
354 if (!ctx->pulses_per_revolution) {
355 dev_err(dev, "pulses-per-revolution can't be zero.\n");
360 ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
363 dev_err(dev, "Failed to request interrupt: %d\n", ret);
366 ctx->sample_start = ktime_get();
367 mod_timer(&ctx->rpm_timer, jiffies + HZ);
370 hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
371 ctx, pwm_fan_groups);
373 dev_err(dev, "Failed to register hwmon device\n");
374 return PTR_ERR(hwmon);
377 ret = pwm_fan_of_get_cooling_data(dev, ctx);
381 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
382 if (IS_ENABLED(CONFIG_THERMAL)) {
383 cdev = devm_thermal_of_cooling_device_register(dev,
384 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
388 "Failed to register pwm-fan as cooling device: %d\n",
393 thermal_cdev_update(cdev);
399 static int pwm_fan_disable(struct device *dev)
401 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
402 struct pwm_args args;
405 pwm_get_args(ctx->pwm, &args);
407 if (ctx->pwm_value) {
408 ret = pwm_config(ctx->pwm, 0, args.period);
412 pwm_disable(ctx->pwm);
416 ret = regulator_disable(ctx->reg_en);
418 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
426 static void pwm_fan_shutdown(struct platform_device *pdev)
428 pwm_fan_disable(&pdev->dev);
431 #ifdef CONFIG_PM_SLEEP
432 static int pwm_fan_suspend(struct device *dev)
434 return pwm_fan_disable(dev);
437 static int pwm_fan_resume(struct device *dev)
439 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
440 struct pwm_args pargs;
445 ret = regulator_enable(ctx->reg_en);
447 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
452 if (ctx->pwm_value == 0)
455 pwm_get_args(ctx->pwm, &pargs);
456 duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
457 ret = pwm_config(ctx->pwm, duty, pargs.period);
460 return pwm_enable(ctx->pwm);
464 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
466 static const struct of_device_id of_pwm_fan_match[] = {
467 { .compatible = "pwm-fan", },
470 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
472 static struct platform_driver pwm_fan_driver = {
473 .probe = pwm_fan_probe,
474 .shutdown = pwm_fan_shutdown,
478 .of_match_table = of_pwm_fan_match,
482 module_platform_driver(pwm_fan_driver);
484 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
485 MODULE_ALIAS("platform:pwm-fan");
486 MODULE_DESCRIPTION("PWM FAN driver");
487 MODULE_LICENSE("GPL");