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/sysfs.h>
32 struct pwm_device
*pwm
;
33 unsigned char pwm_value
;
36 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
37 const char *buf
, size_t count
)
39 struct pwm_fan_ctx
*ctx
= dev_get_drvdata(dev
);
40 unsigned long pwm
, duty
;
43 if (kstrtoul(buf
, 10, &pwm
) || pwm
> MAX_PWM
)
46 mutex_lock(&ctx
->lock
);
48 if (ctx
->pwm_value
== pwm
)
49 goto exit_set_pwm_no_change
;
52 pwm_disable(ctx
->pwm
);
56 duty
= DIV_ROUND_UP(pwm
* (ctx
->pwm
->period
- 1), MAX_PWM
);
57 ret
= pwm_config(ctx
->pwm
, duty
, ctx
->pwm
->period
);
59 goto exit_set_pwm_err
;
61 if (ctx
->pwm_value
== 0) {
62 ret
= pwm_enable(ctx
->pwm
);
64 goto exit_set_pwm_err
;
69 exit_set_pwm_no_change
:
72 mutex_unlock(&ctx
->lock
);
76 static ssize_t
show_pwm(struct device
*dev
,
77 struct device_attribute
*attr
, char *buf
)
79 struct pwm_fan_ctx
*ctx
= dev_get_drvdata(dev
);
81 return sprintf(buf
, "%u\n", ctx
->pwm_value
);
85 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 0);
87 static struct attribute
*pwm_fan_attrs
[] = {
88 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
92 ATTRIBUTE_GROUPS(pwm_fan
);
94 static int pwm_fan_probe(struct platform_device
*pdev
)
97 struct pwm_fan_ctx
*ctx
;
101 ctx
= devm_kzalloc(&pdev
->dev
, sizeof(*ctx
), GFP_KERNEL
);
105 mutex_init(&ctx
->lock
);
107 ctx
->pwm
= devm_of_pwm_get(&pdev
->dev
, pdev
->dev
.of_node
, NULL
);
108 if (IS_ERR(ctx
->pwm
)) {
109 dev_err(&pdev
->dev
, "Could not get PWM\n");
110 return PTR_ERR(ctx
->pwm
);
113 platform_set_drvdata(pdev
, ctx
);
115 /* Set duty cycle to maximum allowed */
116 duty_cycle
= ctx
->pwm
->period
- 1;
117 ctx
->pwm_value
= MAX_PWM
;
119 ret
= pwm_config(ctx
->pwm
, duty_cycle
, ctx
->pwm
->period
);
121 dev_err(&pdev
->dev
, "Failed to configure PWM\n");
125 /* Enbale PWM output */
126 ret
= pwm_enable(ctx
->pwm
);
128 dev_err(&pdev
->dev
, "Failed to enable PWM\n");
132 hwmon
= devm_hwmon_device_register_with_groups(&pdev
->dev
, "pwmfan",
133 ctx
, pwm_fan_groups
);
135 dev_err(&pdev
->dev
, "Failed to register hwmon device\n");
136 pwm_disable(ctx
->pwm
);
137 return PTR_ERR(hwmon
);
142 static int pwm_fan_remove(struct platform_device
*pdev
)
144 struct pwm_fan_ctx
*ctx
= platform_get_drvdata(pdev
);
147 pwm_disable(ctx
->pwm
);
151 #ifdef CONFIG_PM_SLEEP
152 static int pwm_fan_suspend(struct device
*dev
)
154 struct pwm_fan_ctx
*ctx
= dev_get_drvdata(dev
);
157 pwm_disable(ctx
->pwm
);
161 static int pwm_fan_resume(struct device
*dev
)
163 struct pwm_fan_ctx
*ctx
= dev_get_drvdata(dev
);
167 if (ctx
->pwm_value
== 0)
170 duty
= DIV_ROUND_UP(ctx
->pwm_value
* (ctx
->pwm
->period
- 1), MAX_PWM
);
171 ret
= pwm_config(ctx
->pwm
, duty
, ctx
->pwm
->period
);
174 return pwm_enable(ctx
->pwm
);
178 static SIMPLE_DEV_PM_OPS(pwm_fan_pm
, pwm_fan_suspend
, pwm_fan_resume
);
180 static struct of_device_id of_pwm_fan_match
[] = {
181 { .compatible
= "pwm-fan", },
185 static struct platform_driver pwm_fan_driver
= {
186 .probe
= pwm_fan_probe
,
187 .remove
= pwm_fan_remove
,
191 .of_match_table
= of_pwm_fan_match
,
195 module_platform_driver(pwm_fan_driver
);
197 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
198 MODULE_ALIAS("platform:pwm-fan");
199 MODULE_DESCRIPTION("PWM FAN driver");
200 MODULE_LICENSE("GPL");