Merge tag 'ovl-update-5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs
[linux-2.6-block.git] / drivers / hwmon / pwm-fan.c
CommitLineData
d82d5776
KD
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>
6b1ec478 21#include <linux/interrupt.h>
d82d5776
KD
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
26#include <linux/pwm.h>
b57e1d42 27#include <linux/regulator/consumer.h>
d82d5776 28#include <linux/sysfs.h>
b6bddec0 29#include <linux/thermal.h>
6b1ec478 30#include <linux/timer.h>
d82d5776
KD
31
32#define MAX_PWM 255
33
34struct pwm_fan_ctx {
35 struct mutex lock;
36 struct pwm_device *pwm;
b57e1d42 37 struct regulator *reg_en;
6b1ec478
SW
38
39 int irq;
40 atomic_t pulses;
41 unsigned int rpm;
42 u8 pulses_per_revolution;
43 ktime_t sample_start;
44 struct timer_list rpm_timer;
45
2e5219c7
LM
46 unsigned int pwm_value;
47 unsigned int pwm_fan_state;
48 unsigned int pwm_fan_max_state;
49 unsigned int *pwm_fan_cooling_levels;
b6bddec0 50 struct thermal_cooling_device *cdev;
d82d5776
KD
51};
52
6b1ec478
SW
53/* This handler assumes self resetting edge triggered interrupt. */
54static irqreturn_t pulse_handler(int irq, void *dev_id)
55{
56 struct pwm_fan_ctx *ctx = dev_id;
57
58 atomic_inc(&ctx->pulses);
59
60 return IRQ_HANDLED;
61}
62
63static void sample_timer(struct timer_list *t)
64{
65 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
66 int pulses;
67 u64 tmp;
68
69 pulses = atomic_read(&ctx->pulses);
70 atomic_sub(pulses, &ctx->pulses);
71 tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
72 do_div(tmp, ctx->pulses_per_revolution * 1000);
73 ctx->rpm = tmp;
74
75 ctx->sample_start = ktime_get();
76 mod_timer(&ctx->rpm_timer, jiffies + HZ);
77}
78
cb85ca33 79static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
d82d5776 80{
677252a1 81 unsigned long period;
cb85ca33 82 int ret = 0;
677252a1 83 struct pwm_state state = { };
2289711c 84
d82d5776 85 mutex_lock(&ctx->lock);
d82d5776 86 if (ctx->pwm_value == pwm)
cb85ca33 87 goto exit_set_pwm_err;
d82d5776 88
677252a1
BZ
89 pwm_init_state(ctx->pwm, &state);
90 period = ctx->pwm->args.period;
91 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
92 state.enabled = pwm ? true : false;
f354169e 93
677252a1
BZ
94 ret = pwm_apply_state(ctx->pwm, &state);
95 if (!ret)
96 ctx->pwm_value = pwm;
d82d5776
KD
97exit_set_pwm_err:
98 mutex_unlock(&ctx->lock);
99 return ret;
100}
101
b6bddec0
LM
102static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
103{
104 int i;
105
106 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
107 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
108 break;
109
110 ctx->pwm_fan_state = i;
111}
112
cb1d8534
GR
113static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
114 const char *buf, size_t count)
cb85ca33
LM
115{
116 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
117 unsigned long pwm;
118 int ret;
119
120 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
121 return -EINVAL;
122
123 ret = __set_pwm(ctx, pwm);
124 if (ret)
125 return ret;
126
b6bddec0 127 pwm_fan_update_state(ctx, pwm);
cb85ca33
LM
128 return count;
129}
130
cb1d8534
GR
131static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
132 char *buf)
d82d5776
KD
133{
134 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
135
136 return sprintf(buf, "%u\n", ctx->pwm_value);
137}
138
6b1ec478
SW
139static ssize_t rpm_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
141{
142 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
143
144 return sprintf(buf, "%u\n", ctx->rpm);
145}
d82d5776 146
cb1d8534 147static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
6b1ec478 148static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
d82d5776
KD
149
150static struct attribute *pwm_fan_attrs[] = {
151 &sensor_dev_attr_pwm1.dev_attr.attr,
6b1ec478 152 &sensor_dev_attr_fan1_input.dev_attr.attr,
d82d5776
KD
153 NULL,
154};
155
6b1ec478
SW
156static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
157 int n)
158{
159 struct device *dev = container_of(kobj, struct device, kobj);
160 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
161
162 /* Hide fan_input in case no interrupt is available */
163 if (n == 1 && ctx->irq <= 0)
164 return 0;
165
166 return a->mode;
167}
168
169static const struct attribute_group pwm_fan_group = {
170 .attrs = pwm_fan_attrs,
171 .is_visible = pwm_fan_attrs_visible,
172};
173
174static const struct attribute_group *pwm_fan_groups[] = {
175 &pwm_fan_group,
176 NULL,
177};
d82d5776 178
b6bddec0
LM
179/* thermal cooling device callbacks */
180static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
181 unsigned long *state)
182{
183 struct pwm_fan_ctx *ctx = cdev->devdata;
184
185 if (!ctx)
186 return -EINVAL;
187
188 *state = ctx->pwm_fan_max_state;
189
190 return 0;
191}
192
193static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
194 unsigned long *state)
195{
196 struct pwm_fan_ctx *ctx = cdev->devdata;
197
198 if (!ctx)
199 return -EINVAL;
200
201 *state = ctx->pwm_fan_state;
202
203 return 0;
204}
205
206static int
207pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
208{
209 struct pwm_fan_ctx *ctx = cdev->devdata;
210 int ret;
211
212 if (!ctx || (state > ctx->pwm_fan_max_state))
213 return -EINVAL;
214
215 if (state == ctx->pwm_fan_state)
216 return 0;
217
218 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
219 if (ret) {
220 dev_err(&cdev->device, "Cannot set pwm!\n");
221 return ret;
222 }
223
224 ctx->pwm_fan_state = state;
225
226 return ret;
227}
228
229static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
230 .get_max_state = pwm_fan_get_max_state,
231 .get_cur_state = pwm_fan_get_cur_state,
232 .set_cur_state = pwm_fan_set_cur_state,
233};
234
de52b049
GR
235static int pwm_fan_of_get_cooling_data(struct device *dev,
236 struct pwm_fan_ctx *ctx)
2e5219c7
LM
237{
238 struct device_node *np = dev->of_node;
239 int num, i, ret;
240
241 if (!of_find_property(np, "cooling-levels", NULL))
242 return 0;
243
244 ret = of_property_count_u32_elems(np, "cooling-levels");
245 if (ret <= 0) {
246 dev_err(dev, "Wrong data!\n");
247 return ret ? : -EINVAL;
248 }
249
250 num = ret;
a86854d0 251 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
2e5219c7
LM
252 GFP_KERNEL);
253 if (!ctx->pwm_fan_cooling_levels)
254 return -ENOMEM;
255
256 ret = of_property_read_u32_array(np, "cooling-levels",
257 ctx->pwm_fan_cooling_levels, num);
258 if (ret) {
259 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
260 return ret;
261 }
262
263 for (i = 0; i < num; i++) {
264 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
265 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
266 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
267 return -EINVAL;
268 }
269 }
270
271 ctx->pwm_fan_max_state = num - 1;
272
273 return 0;
274}
275
d82d5776
KD
276static int pwm_fan_probe(struct platform_device *pdev)
277{
b6bddec0 278 struct thermal_cooling_device *cdev;
d82d5776 279 struct pwm_fan_ctx *ctx;
b6bddec0 280 struct device *hwmon;
d82d5776 281 int ret;
677252a1 282 struct pwm_state state = { };
6b1ec478 283 u32 ppr = 2;
d82d5776
KD
284
285 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
286 if (!ctx)
287 return -ENOMEM;
288
289 mutex_init(&ctx->lock);
290
291 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
292 if (IS_ERR(ctx->pwm)) {
9f67f758
TR
293 ret = PTR_ERR(ctx->pwm);
294
295 if (ret != -EPROBE_DEFER)
296 dev_err(&pdev->dev, "Could not get PWM: %d\n", ret);
297
298 return ret;
d82d5776
KD
299 }
300
d82d5776
KD
301 platform_set_drvdata(pdev, ctx);
302
6b1ec478
SW
303 ctx->irq = platform_get_irq(pdev, 0);
304 if (ctx->irq == -EPROBE_DEFER)
305 return ctx->irq;
306
b57e1d42
SW
307 ctx->reg_en = devm_regulator_get_optional(&pdev->dev, "fan");
308 if (IS_ERR(ctx->reg_en)) {
309 if (PTR_ERR(ctx->reg_en) != -ENODEV)
310 return PTR_ERR(ctx->reg_en);
311
312 ctx->reg_en = NULL;
313 } else {
314 ret = regulator_enable(ctx->reg_en);
315 if (ret) {
316 dev_err(&pdev->dev,
317 "Failed to enable fan supply: %d\n", ret);
318 return ret;
319 }
320 }
321
d82d5776
KD
322 ctx->pwm_value = MAX_PWM;
323
677252a1
BZ
324 /* Set duty cycle to maximum allowed and enable PWM output */
325 pwm_init_state(ctx->pwm, &state);
326 state.duty_cycle = ctx->pwm->args.period - 1;
327 state.enabled = true;
d82d5776 328
677252a1 329 ret = pwm_apply_state(ctx->pwm, &state);
d82d5776 330 if (ret) {
841cf676 331 dev_err(&pdev->dev, "Failed to configure PWM: %d\n", ret);
b57e1d42 332 goto err_reg_disable;
d82d5776
KD
333 }
334
6b1ec478
SW
335 timer_setup(&ctx->rpm_timer, sample_timer, 0);
336
337 of_property_read_u32(pdev->dev.of_node, "pulses-per-revolution", &ppr);
338 ctx->pulses_per_revolution = ppr;
339 if (!ctx->pulses_per_revolution) {
340 dev_err(&pdev->dev, "pulses-per-revolution can't be zero.\n");
341 ret = -EINVAL;
342 goto err_pwm_disable;
343 }
344
345 if (ctx->irq > 0) {
346 ret = devm_request_irq(&pdev->dev, ctx->irq, pulse_handler, 0,
347 pdev->name, ctx);
348 if (ret) {
841cf676
RM
349 dev_err(&pdev->dev,
350 "Failed to request interrupt: %d\n", ret);
6b1ec478
SW
351 goto err_pwm_disable;
352 }
353 ctx->sample_start = ktime_get();
354 mod_timer(&ctx->rpm_timer, jiffies + HZ);
355 }
356
d82d5776
KD
357 hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
358 ctx, pwm_fan_groups);
359 if (IS_ERR(hwmon)) {
677252a1 360 ret = PTR_ERR(hwmon);
841cf676
RM
361 dev_err(&pdev->dev,
362 "Failed to register hwmon device: %d\n", ret);
6b1ec478 363 goto err_del_timer;
d82d5776 364 }
2e5219c7
LM
365
366 ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
367 if (ret)
6b1ec478 368 goto err_del_timer;
2e5219c7 369
b6bddec0
LM
370 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
371 if (IS_ENABLED(CONFIG_THERMAL)) {
372 cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
373 "pwm-fan", ctx,
374 &pwm_fan_cooling_ops);
375 if (IS_ERR(cdev)) {
677252a1 376 ret = PTR_ERR(cdev);
841cf676
RM
377 dev_err(&pdev->dev,
378 "Failed to register pwm-fan as cooling device: %d\n",
379 ret);
6b1ec478 380 goto err_del_timer;
b6bddec0
LM
381 }
382 ctx->cdev = cdev;
383 thermal_cdev_update(cdev);
384 }
385
d82d5776 386 return 0;
677252a1 387
6b1ec478
SW
388err_del_timer:
389 del_timer_sync(&ctx->rpm_timer);
390
677252a1
BZ
391err_pwm_disable:
392 state.enabled = false;
393 pwm_apply_state(ctx->pwm, &state);
394
b57e1d42
SW
395err_reg_disable:
396 if (ctx->reg_en)
397 regulator_disable(ctx->reg_en);
398
677252a1 399 return ret;
d82d5776
KD
400}
401
402static int pwm_fan_remove(struct platform_device *pdev)
403{
404 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
405
b6bddec0 406 thermal_cooling_device_unregister(ctx->cdev);
6b1ec478
SW
407 del_timer_sync(&ctx->rpm_timer);
408
d82d5776
KD
409 if (ctx->pwm_value)
410 pwm_disable(ctx->pwm);
b57e1d42
SW
411
412 if (ctx->reg_en)
413 regulator_disable(ctx->reg_en);
414
d82d5776
KD
415 return 0;
416}
417
418#ifdef CONFIG_PM_SLEEP
419static int pwm_fan_suspend(struct device *dev)
420{
421 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
95dcd64b
TR
422 struct pwm_args args;
423 int ret;
424
425 pwm_get_args(ctx->pwm, &args);
426
427 if (ctx->pwm_value) {
428 ret = pwm_config(ctx->pwm, 0, args.period);
429 if (ret < 0)
430 return ret;
d82d5776 431
d82d5776 432 pwm_disable(ctx->pwm);
95dcd64b
TR
433 }
434
b57e1d42
SW
435 if (ctx->reg_en) {
436 ret = regulator_disable(ctx->reg_en);
437 if (ret) {
438 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
439 return ret;
440 }
441 }
442
d82d5776
KD
443 return 0;
444}
445
446static int pwm_fan_resume(struct device *dev)
447{
448 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
2289711c 449 struct pwm_args pargs;
48b9d5b4
KD
450 unsigned long duty;
451 int ret;
d82d5776 452
b57e1d42
SW
453 if (ctx->reg_en) {
454 ret = regulator_enable(ctx->reg_en);
455 if (ret) {
456 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
457 return ret;
458 }
459 }
460
48b9d5b4
KD
461 if (ctx->pwm_value == 0)
462 return 0;
463
2289711c
BB
464 pwm_get_args(ctx->pwm, &pargs);
465 duty = DIV_ROUND_UP(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
466 ret = pwm_config(ctx->pwm, duty, pargs.period);
48b9d5b4
KD
467 if (ret)
468 return ret;
469 return pwm_enable(ctx->pwm);
d82d5776
KD
470}
471#endif
472
473static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
474
d720acac 475static const struct of_device_id of_pwm_fan_match[] = {
d82d5776
KD
476 { .compatible = "pwm-fan", },
477 {},
478};
f491e70c 479MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
d82d5776
KD
480
481static struct platform_driver pwm_fan_driver = {
482 .probe = pwm_fan_probe,
483 .remove = pwm_fan_remove,
484 .driver = {
485 .name = "pwm-fan",
486 .pm = &pwm_fan_pm,
487 .of_match_table = of_pwm_fan_match,
488 },
489};
490
491module_platform_driver(pwm_fan_driver);
492
493MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
494MODULE_ALIAS("platform:pwm-fan");
495MODULE_DESCRIPTION("PWM FAN driver");
496MODULE_LICENSE("GPL");