diff options
Diffstat (limited to 'drivers/hwmon/pwm-fan.c')
-rw-r--r-- | drivers/hwmon/pwm-fan.c | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c new file mode 100644 index 000000000000..af30dd63de6a --- /dev/null +++ b/drivers/hwmon/pwm-fan.c | |||
@@ -0,0 +1,194 @@ | |||
1 | /* | ||
2 | * pwm-fan.c - Hwmon driver for fans connected to PWM lines. | ||
3 | * | ||
4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. | ||
5 | * | ||
6 | * Author: Kamil Debski <k.debski@samsung.com> | ||
7 | * | ||
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. | ||
12 | * | ||
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. | ||
17 | */ | ||
18 | |||
19 | #include <linux/hwmon.h> | ||
20 | #include <linux/hwmon-sysfs.h> | ||
21 | #include <linux/module.h> | ||
22 | #include <linux/mutex.h> | ||
23 | #include <linux/of.h> | ||
24 | #include <linux/platform_device.h> | ||
25 | #include <linux/pwm.h> | ||
26 | #include <linux/sysfs.h> | ||
27 | |||
28 | #define MAX_PWM 255 | ||
29 | |||
30 | struct pwm_fan_ctx { | ||
31 | struct mutex lock; | ||
32 | struct pwm_device *pwm; | ||
33 | unsigned char pwm_value; | ||
34 | }; | ||
35 | |||
36 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, | ||
37 | const char *buf, size_t count) | ||
38 | { | ||
39 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | ||
40 | unsigned long pwm, duty; | ||
41 | ssize_t ret; | ||
42 | |||
43 | if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM) | ||
44 | return -EINVAL; | ||
45 | |||
46 | mutex_lock(&ctx->lock); | ||
47 | |||
48 | if (ctx->pwm_value == pwm) | ||
49 | goto exit_set_pwm_no_change; | ||
50 | |||
51 | if (pwm == 0) { | ||
52 | pwm_disable(ctx->pwm); | ||
53 | goto exit_set_pwm; | ||
54 | } | ||
55 | |||
56 | duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM); | ||
57 | ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); | ||
58 | if (ret) | ||
59 | goto exit_set_pwm_err; | ||
60 | |||
61 | if (ctx->pwm_value == 0) { | ||
62 | ret = pwm_enable(ctx->pwm); | ||
63 | if (ret) | ||
64 | goto exit_set_pwm_err; | ||
65 | } | ||
66 | |||
67 | exit_set_pwm: | ||
68 | ctx->pwm_value = pwm; | ||
69 | exit_set_pwm_no_change: | ||
70 | ret = count; | ||
71 | exit_set_pwm_err: | ||
72 | mutex_unlock(&ctx->lock); | ||
73 | return ret; | ||
74 | } | ||
75 | |||
76 | static ssize_t show_pwm(struct device *dev, | ||
77 | struct device_attribute *attr, char *buf) | ||
78 | { | ||
79 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | ||
80 | |||
81 | return sprintf(buf, "%u\n", ctx->pwm_value); | ||
82 | } | ||
83 | |||
84 | |||
85 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); | ||
86 | |||
87 | static struct attribute *pwm_fan_attrs[] = { | ||
88 | &sensor_dev_attr_pwm1.dev_attr.attr, | ||
89 | NULL, | ||
90 | }; | ||
91 | |||
92 | ATTRIBUTE_GROUPS(pwm_fan); | ||
93 | |||
94 | static int pwm_fan_probe(struct platform_device *pdev) | ||
95 | { | ||
96 | struct device *hwmon; | ||
97 | struct pwm_fan_ctx *ctx; | ||
98 | int duty_cycle; | ||
99 | int ret; | ||
100 | |||
101 | ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); | ||
102 | if (!ctx) | ||
103 | return -ENOMEM; | ||
104 | |||
105 | mutex_init(&ctx->lock); | ||
106 | |||
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); | ||
111 | } | ||
112 | |||
113 | dev_set_drvdata(&pdev->dev, ctx); | ||
114 | platform_set_drvdata(pdev, ctx); | ||
115 | |||
116 | /* Set duty cycle to maximum allowed */ | ||
117 | duty_cycle = ctx->pwm->period - 1; | ||
118 | ctx->pwm_value = MAX_PWM; | ||
119 | |||
120 | ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period); | ||
121 | if (ret) { | ||
122 | dev_err(&pdev->dev, "Failed to configure PWM\n"); | ||
123 | return ret; | ||
124 | } | ||
125 | |||
126 | /* Enbale PWM output */ | ||
127 | ret = pwm_enable(ctx->pwm); | ||
128 | if (ret) { | ||
129 | dev_err(&pdev->dev, "Failed to enable PWM\n"); | ||
130 | return ret; | ||
131 | } | ||
132 | |||
133 | hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan", | ||
134 | ctx, pwm_fan_groups); | ||
135 | if (IS_ERR(hwmon)) { | ||
136 | dev_err(&pdev->dev, "Failed to register hwmon device\n"); | ||
137 | pwm_disable(ctx->pwm); | ||
138 | return PTR_ERR(hwmon); | ||
139 | } | ||
140 | return 0; | ||
141 | } | ||
142 | |||
143 | static int pwm_fan_remove(struct platform_device *pdev) | ||
144 | { | ||
145 | struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); | ||
146 | |||
147 | if (ctx->pwm_value) | ||
148 | pwm_disable(ctx->pwm); | ||
149 | return 0; | ||
150 | } | ||
151 | |||
152 | #ifdef CONFIG_PM_SLEEP | ||
153 | static int pwm_fan_suspend(struct device *dev) | ||
154 | { | ||
155 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | ||
156 | |||
157 | if (ctx->pwm_value) | ||
158 | pwm_disable(ctx->pwm); | ||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | static int pwm_fan_resume(struct device *dev) | ||
163 | { | ||
164 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | ||
165 | |||
166 | if (ctx->pwm_value) | ||
167 | return pwm_enable(ctx->pwm); | ||
168 | return 0; | ||
169 | } | ||
170 | #endif | ||
171 | |||
172 | static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume); | ||
173 | |||
174 | static struct of_device_id of_pwm_fan_match[] = { | ||
175 | { .compatible = "pwm-fan", }, | ||
176 | {}, | ||
177 | }; | ||
178 | |||
179 | static struct platform_driver pwm_fan_driver = { | ||
180 | .probe = pwm_fan_probe, | ||
181 | .remove = pwm_fan_remove, | ||
182 | .driver = { | ||
183 | .name = "pwm-fan", | ||
184 | .pm = &pwm_fan_pm, | ||
185 | .of_match_table = of_pwm_fan_match, | ||
186 | }, | ||
187 | }; | ||
188 | |||
189 | module_platform_driver(pwm_fan_driver); | ||
190 | |||
191 | MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); | ||
192 | MODULE_ALIAS("platform:pwm-fan"); | ||
193 | MODULE_DESCRIPTION("PWM FAN driver"); | ||
194 | MODULE_LICENSE("GPL"); | ||