blob: 7c83dc4c8dbd5040301797e009e7e9c765665038 [file] [log] [blame]
Kamil Debskid82d5772014-07-16 17:46:42 +02001/*
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>
Lukasz Majewskib6bddec2015-02-26 14:59:37 +010027#include <linux/thermal.h>
Kamil Debskid82d5772014-07-16 17:46:42 +020028
29#define MAX_PWM 255
30
31struct pwm_fan_ctx {
32 struct mutex lock;
33 struct pwm_device *pwm;
Lukasz Majewski2e5219c2015-02-26 14:59:36 +010034 unsigned int pwm_value;
35 unsigned int pwm_fan_state;
36 unsigned int pwm_fan_max_state;
37 unsigned int *pwm_fan_cooling_levels;
Lukasz Majewskib6bddec2015-02-26 14:59:37 +010038 struct thermal_cooling_device *cdev;
Kamil Debskid82d5772014-07-16 17:46:42 +020039};
40
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010041static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
Kamil Debskid82d5772014-07-16 17:46:42 +020042{
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010043 unsigned long duty;
44 int ret = 0;
Kamil Debskid82d5772014-07-16 17:46:42 +020045
46 mutex_lock(&ctx->lock);
Kamil Debskid82d5772014-07-16 17:46:42 +020047 if (ctx->pwm_value == pwm)
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010048 goto exit_set_pwm_err;
Kamil Debskid82d5772014-07-16 17:46:42 +020049
50 if (pwm == 0) {
51 pwm_disable(ctx->pwm);
52 goto exit_set_pwm;
53 }
54
55 duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM);
56 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period);
57 if (ret)
58 goto exit_set_pwm_err;
59
60 if (ctx->pwm_value == 0) {
61 ret = pwm_enable(ctx->pwm);
62 if (ret)
63 goto exit_set_pwm_err;
64 }
65
66exit_set_pwm:
67 ctx->pwm_value = pwm;
Kamil Debskid82d5772014-07-16 17:46:42 +020068exit_set_pwm_err:
69 mutex_unlock(&ctx->lock);
70 return ret;
71}
72
Lukasz Majewskib6bddec2015-02-26 14:59:37 +010073static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
74{
75 int i;
76
77 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
78 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
79 break;
80
81 ctx->pwm_fan_state = i;
82}
83
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010084static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
85 const char *buf, size_t count)
86{
87 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
88 unsigned long pwm;
89 int ret;
90
91 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
92 return -EINVAL;
93
94 ret = __set_pwm(ctx, pwm);
95 if (ret)
96 return ret;
97
Lukasz Majewskib6bddec2015-02-26 14:59:37 +010098 pwm_fan_update_state(ctx, pwm);
Lukasz Majewskicb85ca32015-02-26 14:59:35 +010099 return count;
100}
101
Kamil Debskid82d5772014-07-16 17:46:42 +0200102static ssize_t show_pwm(struct device *dev,
103 struct device_attribute *attr, char *buf)
104{
105 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
106
107 return sprintf(buf, "%u\n", ctx->pwm_value);
108}
109
110
111static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
112
113static struct attribute *pwm_fan_attrs[] = {
114 &sensor_dev_attr_pwm1.dev_attr.attr,
115 NULL,
116};
117
118ATTRIBUTE_GROUPS(pwm_fan);
119
Lukasz Majewskib6bddec2015-02-26 14:59:37 +0100120/* thermal cooling device callbacks */
121static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
122 unsigned long *state)
123{
124 struct pwm_fan_ctx *ctx = cdev->devdata;
125
126 if (!ctx)
127 return -EINVAL;
128
129 *state = ctx->pwm_fan_max_state;
130
131 return 0;
132}
133
134static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
135 unsigned long *state)
136{
137 struct pwm_fan_ctx *ctx = cdev->devdata;
138
139 if (!ctx)
140 return -EINVAL;
141
142 *state = ctx->pwm_fan_state;
143
144 return 0;
145}
146
147static int
148pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
149{
150 struct pwm_fan_ctx *ctx = cdev->devdata;
151 int ret;
152
153 if (!ctx || (state > ctx->pwm_fan_max_state))
154 return -EINVAL;
155
156 if (state == ctx->pwm_fan_state)
157 return 0;
158
159 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
160 if (ret) {
161 dev_err(&cdev->device, "Cannot set pwm!\n");
162 return ret;
163 }
164
165 ctx->pwm_fan_state = state;
166
167 return ret;
168}
169
170static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
171 .get_max_state = pwm_fan_get_max_state,
172 .get_cur_state = pwm_fan_get_cur_state,
173 .set_cur_state = pwm_fan_set_cur_state,
174};
175
Lukasz Majewski2e5219c2015-02-26 14:59:36 +0100176int pwm_fan_of_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx)
177{
178 struct device_node *np = dev->of_node;
179 int num, i, ret;
180
181 if (!of_find_property(np, "cooling-levels", NULL))
182 return 0;
183
184 ret = of_property_count_u32_elems(np, "cooling-levels");
185 if (ret <= 0) {
186 dev_err(dev, "Wrong data!\n");
187 return ret ? : -EINVAL;
188 }
189
190 num = ret;
191 ctx->pwm_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
192 GFP_KERNEL);
193 if (!ctx->pwm_fan_cooling_levels)
194 return -ENOMEM;
195
196 ret = of_property_read_u32_array(np, "cooling-levels",
197 ctx->pwm_fan_cooling_levels, num);
198 if (ret) {
199 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
200 return ret;
201 }
202
203 for (i = 0; i < num; i++) {
204 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
205 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
206 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
207 return -EINVAL;
208 }
209 }
210
211 ctx->pwm_fan_max_state = num - 1;
212
213 return 0;
214}
215
Kamil Debskid82d5772014-07-16 17:46:42 +0200216static int pwm_fan_probe(struct platform_device *pdev)
217{
Lukasz Majewskib6bddec2015-02-26 14:59:37 +0100218 struct thermal_cooling_device *cdev;
Kamil Debskid82d5772014-07-16 17:46:42 +0200219 struct pwm_fan_ctx *ctx;
Lukasz Majewskib6bddec2015-02-26 14:59:37 +0100220 struct device *hwmon;
Kamil Debskid82d5772014-07-16 17:46:42 +0200221 int duty_cycle;
222 int ret;
223
224 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
225 if (!ctx)
226 return -ENOMEM;
227
228 mutex_init(&ctx->lock);
229
230 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
231 if (IS_ERR(ctx->pwm)) {
232 dev_err(&pdev->dev, "Could not get PWM\n");
233 return PTR_ERR(ctx->pwm);
234 }
235
Kamil Debskid82d5772014-07-16 17:46:42 +0200236 platform_set_drvdata(pdev, ctx);
237
238 /* Set duty cycle to maximum allowed */
239 duty_cycle = ctx->pwm->period - 1;
240 ctx->pwm_value = MAX_PWM;
241
242 ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period);
243 if (ret) {
244 dev_err(&pdev->dev, "Failed to configure PWM\n");
245 return ret;
246 }
247
248 /* Enbale PWM output */
249 ret = pwm_enable(ctx->pwm);
250 if (ret) {
251 dev_err(&pdev->dev, "Failed to enable PWM\n");
252 return ret;
253 }
254
255 hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
256 ctx, pwm_fan_groups);
257 if (IS_ERR(hwmon)) {
258 dev_err(&pdev->dev, "Failed to register hwmon device\n");
259 pwm_disable(ctx->pwm);
260 return PTR_ERR(hwmon);
261 }
Lukasz Majewski2e5219c2015-02-26 14:59:36 +0100262
263 ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
264 if (ret)
265 return ret;
266
Lukasz Majewskib6bddec2015-02-26 14:59:37 +0100267 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
268 if (IS_ENABLED(CONFIG_THERMAL)) {
269 cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
270 "pwm-fan", ctx,
271 &pwm_fan_cooling_ops);
272 if (IS_ERR(cdev)) {
273 dev_err(&pdev->dev,
274 "Failed to register pwm-fan as cooling device");
275 pwm_disable(ctx->pwm);
276 return PTR_ERR(cdev);
277 }
278 ctx->cdev = cdev;
279 thermal_cdev_update(cdev);
280 }
281
Kamil Debskid82d5772014-07-16 17:46:42 +0200282 return 0;
283}
284
285static int pwm_fan_remove(struct platform_device *pdev)
286{
287 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
288
Lukasz Majewskib6bddec2015-02-26 14:59:37 +0100289 thermal_cooling_device_unregister(ctx->cdev);
Kamil Debskid82d5772014-07-16 17:46:42 +0200290 if (ctx->pwm_value)
291 pwm_disable(ctx->pwm);
292 return 0;
293}
294
295#ifdef CONFIG_PM_SLEEP
296static int pwm_fan_suspend(struct device *dev)
297{
298 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
299
300 if (ctx->pwm_value)
301 pwm_disable(ctx->pwm);
302 return 0;
303}
304
305static int pwm_fan_resume(struct device *dev)
306{
307 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
Kamil Debski48b9d5b2014-11-03 15:42:55 +0100308 unsigned long duty;
309 int ret;
Kamil Debskid82d5772014-07-16 17:46:42 +0200310
Kamil Debski48b9d5b2014-11-03 15:42:55 +0100311 if (ctx->pwm_value == 0)
312 return 0;
313
314 duty = DIV_ROUND_UP(ctx->pwm_value * (ctx->pwm->period - 1), MAX_PWM);
315 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period);
316 if (ret)
317 return ret;
318 return pwm_enable(ctx->pwm);
Kamil Debskid82d5772014-07-16 17:46:42 +0200319}
320#endif
321
322static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
323
324static struct of_device_id of_pwm_fan_match[] = {
325 { .compatible = "pwm-fan", },
326 {},
327};
328
329static struct platform_driver pwm_fan_driver = {
330 .probe = pwm_fan_probe,
331 .remove = pwm_fan_remove,
332 .driver = {
333 .name = "pwm-fan",
334 .pm = &pwm_fan_pm,
335 .of_match_table = of_pwm_fan_match,
336 },
337};
338
339module_platform_driver(pwm_fan_driver);
340
341MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
342MODULE_ALIAS("platform:pwm-fan");
343MODULE_DESCRIPTION("PWM FAN driver");
344MODULE_LICENSE("GPL");