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/module.h>
22 #include <linux/mutex.h>
24 #include <linux/platform_device.h>
25 #include <linux/pwm.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/sysfs.h>
28 #include <linux/thermal.h>
34 struct pwm_device *pwm;
35 struct regulator *reg_en;
36 unsigned int pwm_value;
37 unsigned int pwm_fan_state;
38 unsigned int pwm_fan_max_state;
39 unsigned int *pwm_fan_cooling_levels;
40 struct thermal_cooling_device *cdev;
43 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
47 struct pwm_state state = { };
49 mutex_lock(&ctx->lock);
50 if (ctx->pwm_value == pwm)
51 goto exit_set_pwm_err;
53 pwm_init_state(ctx->pwm, &state);
54 period = ctx->pwm->args.period;
55 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
56 state.enabled = pwm ? true : false;
58 ret = pwm_apply_state(ctx->pwm, &state);
62 mutex_unlock(&ctx->lock);
66 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
70 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
71 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
74 ctx->pwm_fan_state = i;
77 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
78 const char *buf, size_t count)
80 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
84 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
87 ret = __set_pwm(ctx, pwm);
91 pwm_fan_update_state(ctx, pwm);
95 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
98 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
100 return sprintf(buf, "%u\n", ctx->pwm_value);
104 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
106 static struct attribute *pwm_fan_attrs[] = {
107 &sensor_dev_attr_pwm1.dev_attr.attr,
111 ATTRIBUTE_GROUPS(pwm_fan);
113 /* thermal cooling device callbacks */
114 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
115 unsigned long *state)
117 struct pwm_fan_ctx *ctx = cdev->devdata;
122 *state = ctx->pwm_fan_max_state;
127 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
128 unsigned long *state)
130 struct pwm_fan_ctx *ctx = cdev->devdata;
135 *state = ctx->pwm_fan_state;
141 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
143 struct pwm_fan_ctx *ctx = cdev->devdata;
146 if (!ctx || (state > ctx->pwm_fan_max_state))
149 if (state == ctx->pwm_fan_state)
152 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
154 dev_err(&cdev->device, "Cannot set pwm!\n");
158 ctx->pwm_fan_state = state;
163 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
164 .get_max_state = pwm_fan_get_max_state,
165 .get_cur_state = pwm_fan_get_cur_state,
166 .set_cur_state = pwm_fan_set_cur_state,
169 static int pwm_fan_of_get_cooling_data(struct device *dev,
170 struct pwm_fan_ctx *ctx)
172 struct device_node *np = dev->of_node;
175 if (!of_find_property(np, "cooling-levels", NULL))
178 ret = of_property_count_u32_elems(np, "cooling-levels");
180 dev_err(dev, "Wrong data!\n");
181 return ret ? : -EINVAL;
185 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
187 if (!ctx->pwm_fan_cooling_levels)
190 ret = of_property_read_u32_array(np, "cooling-levels",
191 ctx->pwm_fan_cooling_levels, num);
193 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
197 for (i = 0; i < num; i++) {
198 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
199 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
200 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
205 ctx->pwm_fan_max_state = num - 1;
210 static int pwm_fan_probe(struct platform_device *pdev)
212 struct thermal_cooling_device *cdev;
213 struct pwm_fan_ctx *ctx;
214 struct device *hwmon;
216 struct pwm_state state = { };
218 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
222 mutex_init(&ctx->lock);
224 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
225 if (IS_ERR(ctx->pwm)) {
226 ret = PTR_ERR(ctx->pwm);
228 if (ret != -EPROBE_DEFER)
229 dev_err(&pdev->dev, "Could not get PWM: %d\n", ret);
234 platform_set_drvdata(pdev, ctx);
236 ctx->reg_en = devm_regulator_get_optional(&pdev->dev, "fan");
237 if (IS_ERR(ctx->reg_en)) {
238 if (PTR_ERR(ctx->reg_en) != -ENODEV)
239 return PTR_ERR(ctx->reg_en);
243 ret = regulator_enable(ctx->reg_en);
246 "Failed to enable fan supply: %d\n", ret);
251 ctx->pwm_value = MAX_PWM;
253 /* Set duty cycle to maximum allowed and enable PWM output */
254 pwm_init_state(ctx->pwm, &state);
255 state.duty_cycle = ctx->pwm->args.period - 1;
256 state.enabled = true;
258 ret = pwm_apply_state(ctx->pwm, &state);
260 dev_err(&pdev->dev, "Failed to configure PWM\n");
261 goto err_reg_disable;
264 hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
265 ctx, pwm_fan_groups);
267 dev_err(&pdev->dev, "Failed to register hwmon device\n");
268 ret = PTR_ERR(hwmon);
269 goto err_pwm_disable;
272 ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
276 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
277 if (IS_ENABLED(CONFIG_THERMAL)) {
278 cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
280 &pwm_fan_cooling_ops);
283 "Failed to register pwm-fan as cooling device");
285 goto err_pwm_disable;
288 thermal_cdev_update(cdev);
294 state.enabled = false;
295 pwm_apply_state(ctx->pwm, &state);
299 regulator_disable(ctx->reg_en);
304 static int pwm_fan_remove(struct platform_device *pdev)
306 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
308 thermal_cooling_device_unregister(ctx->cdev);
310 pwm_disable(ctx->pwm);
313 regulator_disable(ctx->reg_en);
318 #ifdef CONFIG_PM_SLEEP
319 static int pwm_fan_suspend(struct device *dev)
321 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
322 struct pwm_args args;
325 pwm_get_args(ctx->pwm, &args);
327 if (ctx->pwm_value) {
328 ret = pwm_config(ctx->pwm, 0, args.period);
332 pwm_disable(ctx->pwm);
336 ret = regulator_disable(ctx->reg_en);
338 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
346 static int pwm_fan_resume(struct device *dev)
348 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
349 struct pwm_args pargs;
354 ret = regulator_enable(ctx->reg_en);
356 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
361 if (ctx->pwm_value == 0)
364 pwm_get_args(ctx->pwm, &pargs);
365 duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
366 ret = pwm_config(ctx->pwm, duty, pargs.period);
369 return pwm_enable(ctx->pwm);
373 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
375 static const struct of_device_id of_pwm_fan_match[] = {
376 { .compatible = "pwm-fan", },
379 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
381 static struct platform_driver pwm_fan_driver = {
382 .probe = pwm_fan_probe,
383 .remove = pwm_fan_remove,
387 .of_match_table = of_pwm_fan_match,
391 module_platform_driver(pwm_fan_driver);
393 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
394 MODULE_ALIAS("platform:pwm-fan");
395 MODULE_DESCRIPTION("PWM FAN driver");
396 MODULE_LICENSE("GPL");