2 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Kamil Debski <k.debski@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
25 #include <linux/platform_device.h>
26 #include <linux/pwm.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/sysfs.h>
29 #include <linux/thermal.h>
30 #include <linux/timer.h>
36 struct pwm_device *pwm;
37 struct regulator *reg_en;
42 u8 pulses_per_revolution;
44 struct timer_list rpm_timer;
46 unsigned int pwm_value;
47 unsigned int pwm_fan_state;
48 unsigned int pwm_fan_max_state;
49 unsigned int *pwm_fan_cooling_levels;
50 struct thermal_cooling_device *cdev;
53 /* This handler assumes self resetting edge triggered interrupt. */
54 static irqreturn_t pulse_handler(int irq, void *dev_id)
56 struct pwm_fan_ctx *ctx = dev_id;
58 atomic_inc(&ctx->pulses);
63 static void sample_timer(struct timer_list *t)
65 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
69 pulses = atomic_read(&ctx->pulses);
70 atomic_sub(pulses, &ctx->pulses);
71 tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
72 do_div(tmp, ctx->pulses_per_revolution * 1000);
75 ctx->sample_start = ktime_get();
76 mod_timer(&ctx->rpm_timer, jiffies + HZ);
79 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
83 struct pwm_state state = { };
85 mutex_lock(&ctx->lock);
86 if (ctx->pwm_value == pwm)
87 goto exit_set_pwm_err;
89 pwm_init_state(ctx->pwm, &state);
90 period = ctx->pwm->args.period;
91 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
92 state.enabled = pwm ? true : false;
94 ret = pwm_apply_state(ctx->pwm, &state);
98 mutex_unlock(&ctx->lock);
102 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
106 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
107 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
110 ctx->pwm_fan_state = i;
113 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
114 const char *buf, size_t count)
116 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
120 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
123 ret = __set_pwm(ctx, pwm);
127 pwm_fan_update_state(ctx, pwm);
131 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
134 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
136 return sprintf(buf, "%u\n", ctx->pwm_value);
139 static ssize_t rpm_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
142 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
144 return sprintf(buf, "%u\n", ctx->rpm);
147 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
148 static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
150 static struct attribute *pwm_fan_attrs[] = {
151 &sensor_dev_attr_pwm1.dev_attr.attr,
152 &sensor_dev_attr_fan1_input.dev_attr.attr,
156 static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
159 struct device *dev = container_of(kobj, struct device, kobj);
160 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
162 /* Hide fan_input in case no interrupt is available */
163 if (n == 1 && ctx->irq <= 0)
169 static const struct attribute_group pwm_fan_group = {
170 .attrs = pwm_fan_attrs,
171 .is_visible = pwm_fan_attrs_visible,
174 static const struct attribute_group *pwm_fan_groups[] = {
179 /* thermal cooling device callbacks */
180 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
181 unsigned long *state)
183 struct pwm_fan_ctx *ctx = cdev->devdata;
188 *state = ctx->pwm_fan_max_state;
193 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
194 unsigned long *state)
196 struct pwm_fan_ctx *ctx = cdev->devdata;
201 *state = ctx->pwm_fan_state;
207 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
209 struct pwm_fan_ctx *ctx = cdev->devdata;
212 if (!ctx || (state > ctx->pwm_fan_max_state))
215 if (state == ctx->pwm_fan_state)
218 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
220 dev_err(&cdev->device, "Cannot set pwm!\n");
224 ctx->pwm_fan_state = state;
229 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
230 .get_max_state = pwm_fan_get_max_state,
231 .get_cur_state = pwm_fan_get_cur_state,
232 .set_cur_state = pwm_fan_set_cur_state,
235 static int pwm_fan_of_get_cooling_data(struct device *dev,
236 struct pwm_fan_ctx *ctx)
238 struct device_node *np = dev->of_node;
241 if (!of_find_property(np, "cooling-levels", NULL))
244 ret = of_property_count_u32_elems(np, "cooling-levels");
246 dev_err(dev, "Wrong data!\n");
247 return ret ? : -EINVAL;
251 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
253 if (!ctx->pwm_fan_cooling_levels)
256 ret = of_property_read_u32_array(np, "cooling-levels",
257 ctx->pwm_fan_cooling_levels, num);
259 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
263 for (i = 0; i < num; i++) {
264 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
265 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
266 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
271 ctx->pwm_fan_max_state = num - 1;
276 static int pwm_fan_probe(struct platform_device *pdev)
278 struct thermal_cooling_device *cdev;
279 struct pwm_fan_ctx *ctx;
280 struct device *hwmon;
282 struct pwm_state state = { };
285 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
289 mutex_init(&ctx->lock);
291 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
292 if (IS_ERR(ctx->pwm)) {
293 ret = PTR_ERR(ctx->pwm);
295 if (ret != -EPROBE_DEFER)
296 dev_err(&pdev->dev, "Could not get PWM: %d\n", ret);
301 platform_set_drvdata(pdev, ctx);
303 ctx->irq = platform_get_irq(pdev, 0);
304 if (ctx->irq == -EPROBE_DEFER)
307 ctx->reg_en = devm_regulator_get_optional(&pdev->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);
317 "Failed to enable fan supply: %d\n", ret);
322 ctx->pwm_value = MAX_PWM;
324 /* Set duty cycle to maximum allowed and enable PWM output */
325 pwm_init_state(ctx->pwm, &state);
326 state.duty_cycle = ctx->pwm->args.period - 1;
327 state.enabled = true;
329 ret = pwm_apply_state(ctx->pwm, &state);
331 dev_err(&pdev->dev, "Failed to configure PWM: %d\n", ret);
332 goto err_reg_disable;
335 timer_setup(&ctx->rpm_timer, sample_timer, 0);
337 of_property_read_u32(pdev->dev.of_node, "pulses-per-revolution", &ppr);
338 ctx->pulses_per_revolution = ppr;
339 if (!ctx->pulses_per_revolution) {
340 dev_err(&pdev->dev, "pulses-per-revolution can't be zero.\n");
342 goto err_pwm_disable;
346 ret = devm_request_irq(&pdev->dev, ctx->irq, pulse_handler, 0,
350 "Failed to request interrupt: %d\n", ret);
351 goto err_pwm_disable;
353 ctx->sample_start = ktime_get();
354 mod_timer(&ctx->rpm_timer, jiffies + HZ);
357 hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
358 ctx, pwm_fan_groups);
360 ret = PTR_ERR(hwmon);
362 "Failed to register hwmon device: %d\n", ret);
366 ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
370 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
371 if (IS_ENABLED(CONFIG_THERMAL)) {
372 cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
374 &pwm_fan_cooling_ops);
378 "Failed to register pwm-fan as cooling device: %d\n",
383 thermal_cdev_update(cdev);
389 del_timer_sync(&ctx->rpm_timer);
392 state.enabled = false;
393 pwm_apply_state(ctx->pwm, &state);
397 regulator_disable(ctx->reg_en);
402 static int pwm_fan_remove(struct platform_device *pdev)
404 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
406 thermal_cooling_device_unregister(ctx->cdev);
407 del_timer_sync(&ctx->rpm_timer);
410 pwm_disable(ctx->pwm);
413 regulator_disable(ctx->reg_en);
418 #ifdef CONFIG_PM_SLEEP
419 static int pwm_fan_suspend(struct device *dev)
421 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
422 struct pwm_args args;
425 pwm_get_args(ctx->pwm, &args);
427 if (ctx->pwm_value) {
428 ret = pwm_config(ctx->pwm, 0, args.period);
432 pwm_disable(ctx->pwm);
436 ret = regulator_disable(ctx->reg_en);
438 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
446 static int pwm_fan_resume(struct device *dev)
448 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
449 struct pwm_args pargs;
454 ret = regulator_enable(ctx->reg_en);
456 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
461 if (ctx->pwm_value == 0)
464 pwm_get_args(ctx->pwm, &pargs);
465 duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
466 ret = pwm_config(ctx->pwm, duty, pargs.period);
469 return pwm_enable(ctx->pwm);
473 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
475 static const struct of_device_id of_pwm_fan_match[] = {
476 { .compatible = "pwm-fan", },
479 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
481 static struct platform_driver pwm_fan_driver = {
482 .probe = pwm_fan_probe,
483 .remove = pwm_fan_remove,
487 .of_match_table = of_pwm_fan_match,
491 module_platform_driver(pwm_fan_driver);
493 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
494 MODULE_ALIAS("platform:pwm-fan");
495 MODULE_DESCRIPTION("PWM FAN driver");
496 MODULE_LICENSE("GPL");