]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/hwmon/pwm-fan.c
hwmon: (pwm-fan) Use devm_thermal_of_cooling_device_register
[linux.git] / drivers / hwmon / pwm-fan.c
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/regulator/consumer.h>
27 #include <linux/sysfs.h>
28 #include <linux/thermal.h>
29
30 #define MAX_PWM 255
31
32 struct pwm_fan_ctx {
33         struct mutex lock;
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;
41 };
42
43 static int  __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
44 {
45         unsigned long period;
46         int ret = 0;
47         struct pwm_state state = { };
48
49         mutex_lock(&ctx->lock);
50         if (ctx->pwm_value == pwm)
51                 goto exit_set_pwm_err;
52
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;
57
58         ret = pwm_apply_state(ctx->pwm, &state);
59         if (!ret)
60                 ctx->pwm_value = pwm;
61 exit_set_pwm_err:
62         mutex_unlock(&ctx->lock);
63         return ret;
64 }
65
66 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
67 {
68         int i;
69
70         for (i = 0; i < ctx->pwm_fan_max_state; ++i)
71                 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
72                         break;
73
74         ctx->pwm_fan_state = i;
75 }
76
77 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
78                          const char *buf, size_t count)
79 {
80         struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
81         unsigned long pwm;
82         int ret;
83
84         if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
85                 return -EINVAL;
86
87         ret = __set_pwm(ctx, pwm);
88         if (ret)
89                 return ret;
90
91         pwm_fan_update_state(ctx, pwm);
92         return count;
93 }
94
95 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
96                         char *buf)
97 {
98         struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
99
100         return sprintf(buf, "%u\n", ctx->pwm_value);
101 }
102
103
104 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
105
106 static struct attribute *pwm_fan_attrs[] = {
107         &sensor_dev_attr_pwm1.dev_attr.attr,
108         NULL,
109 };
110
111 ATTRIBUTE_GROUPS(pwm_fan);
112
113 /* thermal cooling device callbacks */
114 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
115                                  unsigned long *state)
116 {
117         struct pwm_fan_ctx *ctx = cdev->devdata;
118
119         if (!ctx)
120                 return -EINVAL;
121
122         *state = ctx->pwm_fan_max_state;
123
124         return 0;
125 }
126
127 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
128                                  unsigned long *state)
129 {
130         struct pwm_fan_ctx *ctx = cdev->devdata;
131
132         if (!ctx)
133                 return -EINVAL;
134
135         *state = ctx->pwm_fan_state;
136
137         return 0;
138 }
139
140 static int
141 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
142 {
143         struct pwm_fan_ctx *ctx = cdev->devdata;
144         int ret;
145
146         if (!ctx || (state > ctx->pwm_fan_max_state))
147                 return -EINVAL;
148
149         if (state == ctx->pwm_fan_state)
150                 return 0;
151
152         ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
153         if (ret) {
154                 dev_err(&cdev->device, "Cannot set pwm!\n");
155                 return ret;
156         }
157
158         ctx->pwm_fan_state = state;
159
160         return ret;
161 }
162
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,
167 };
168
169 static int pwm_fan_of_get_cooling_data(struct device *dev,
170                                        struct pwm_fan_ctx *ctx)
171 {
172         struct device_node *np = dev->of_node;
173         int num, i, ret;
174
175         if (!of_find_property(np, "cooling-levels", NULL))
176                 return 0;
177
178         ret = of_property_count_u32_elems(np, "cooling-levels");
179         if (ret <= 0) {
180                 dev_err(dev, "Wrong data!\n");
181                 return ret ? : -EINVAL;
182         }
183
184         num = ret;
185         ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
186                                                    GFP_KERNEL);
187         if (!ctx->pwm_fan_cooling_levels)
188                 return -ENOMEM;
189
190         ret = of_property_read_u32_array(np, "cooling-levels",
191                                          ctx->pwm_fan_cooling_levels, num);
192         if (ret) {
193                 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
194                 return ret;
195         }
196
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);
201                         return -EINVAL;
202                 }
203         }
204
205         ctx->pwm_fan_max_state = num - 1;
206
207         return 0;
208 }
209
210 static void pwm_fan_regulator_disable(void *data)
211 {
212         regulator_disable(data);
213 }
214
215 static void pwm_fan_pwm_disable(void *data)
216 {
217         pwm_disable(data);
218 }
219
220 static int pwm_fan_probe(struct platform_device *pdev)
221 {
222         struct thermal_cooling_device *cdev;
223         struct device *dev = &pdev->dev;
224         struct pwm_fan_ctx *ctx;
225         struct device *hwmon;
226         int ret;
227         struct pwm_state state = { };
228
229         ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
230         if (!ctx)
231                 return -ENOMEM;
232
233         mutex_init(&ctx->lock);
234
235         ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
236         if (IS_ERR(ctx->pwm)) {
237                 ret = PTR_ERR(ctx->pwm);
238
239                 if (ret != -EPROBE_DEFER)
240                         dev_err(dev, "Could not get PWM: %d\n", ret);
241
242                 return ret;
243         }
244
245         platform_set_drvdata(pdev, ctx);
246
247         ctx->reg_en = devm_regulator_get_optional(dev, "fan");
248         if (IS_ERR(ctx->reg_en)) {
249                 if (PTR_ERR(ctx->reg_en) != -ENODEV)
250                         return PTR_ERR(ctx->reg_en);
251
252                 ctx->reg_en = NULL;
253         } else {
254                 ret = regulator_enable(ctx->reg_en);
255                 if (ret) {
256                         dev_err(dev, "Failed to enable fan supply: %d\n", ret);
257                         return ret;
258                 }
259                 devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
260                                          ctx->reg_en);
261         }
262
263         ctx->pwm_value = MAX_PWM;
264
265         /* Set duty cycle to maximum allowed and enable PWM output */
266         pwm_init_state(ctx->pwm, &state);
267         state.duty_cycle = ctx->pwm->args.period - 1;
268         state.enabled = true;
269
270         ret = pwm_apply_state(ctx->pwm, &state);
271         if (ret) {
272                 dev_err(dev, "Failed to configure PWM\n");
273                 return ret;
274         }
275         devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx->pwm);
276
277         hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
278                                                        ctx, pwm_fan_groups);
279         if (IS_ERR(hwmon)) {
280                 dev_err(dev, "Failed to register hwmon device\n");
281                 return PTR_ERR(hwmon);
282         }
283
284         ret = pwm_fan_of_get_cooling_data(dev, ctx);
285         if (ret)
286                 return ret;
287
288         ctx->pwm_fan_state = ctx->pwm_fan_max_state;
289         if (IS_ENABLED(CONFIG_THERMAL)) {
290                 cdev = devm_thermal_of_cooling_device_register(dev,
291                         dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
292                 if (IS_ERR(cdev)) {
293                         dev_err(dev,
294                                 "Failed to register pwm-fan as cooling device");
295                         return PTR_ERR(cdev);
296                 }
297                 ctx->cdev = cdev;
298                 thermal_cdev_update(cdev);
299         }
300
301         return 0;
302 }
303
304 #ifdef CONFIG_PM_SLEEP
305 static int pwm_fan_suspend(struct device *dev)
306 {
307         struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
308         struct pwm_args args;
309         int ret;
310
311         pwm_get_args(ctx->pwm, &args);
312
313         if (ctx->pwm_value) {
314                 ret = pwm_config(ctx->pwm, 0, args.period);
315                 if (ret < 0)
316                         return ret;
317
318                 pwm_disable(ctx->pwm);
319         }
320
321         if (ctx->reg_en) {
322                 ret = regulator_disable(ctx->reg_en);
323                 if (ret) {
324                         dev_err(dev, "Failed to disable fan supply: %d\n", ret);
325                         return ret;
326                 }
327         }
328
329         return 0;
330 }
331
332 static int pwm_fan_resume(struct device *dev)
333 {
334         struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
335         struct pwm_args pargs;
336         unsigned long duty;
337         int ret;
338
339         if (ctx->reg_en) {
340                 ret = regulator_enable(ctx->reg_en);
341                 if (ret) {
342                         dev_err(dev, "Failed to enable fan supply: %d\n", ret);
343                         return ret;
344                 }
345         }
346
347         if (ctx->pwm_value == 0)
348                 return 0;
349
350         pwm_get_args(ctx->pwm, &pargs);
351         duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
352         ret = pwm_config(ctx->pwm, duty, pargs.period);
353         if (ret)
354                 return ret;
355         return pwm_enable(ctx->pwm);
356 }
357 #endif
358
359 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
360
361 static const struct of_device_id of_pwm_fan_match[] = {
362         { .compatible = "pwm-fan", },
363         {},
364 };
365 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
366
367 static struct platform_driver pwm_fan_driver = {
368         .probe          = pwm_fan_probe,
369         .driver = {
370                 .name           = "pwm-fan",
371                 .pm             = &pwm_fan_pm,
372                 .of_match_table = of_pwm_fan_match,
373         },
374 };
375
376 module_platform_driver(pwm_fan_driver);
377
378 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
379 MODULE_ALIAS("platform:pwm-fan");
380 MODULE_DESCRIPTION("PWM FAN driver");
381 MODULE_LICENSE("GPL");