hwmon: (lm90) Add support and detection of Philips/NXP NE1618
[linux-2.6-block.git] / drivers / hwmon / pwm-fan.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
d82d5776
KD
2/*
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 *
7 * Author: Kamil Debski <k.debski@samsung.com>
d82d5776
KD
8 */
9
10#include <linux/hwmon.h>
6b1ec478 11#include <linux/interrupt.h>
d82d5776
KD
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16#include <linux/pwm.h>
b57e1d42 17#include <linux/regulator/consumer.h>
d82d5776 18#include <linux/sysfs.h>
b6bddec0 19#include <linux/thermal.h>
6b1ec478 20#include <linux/timer.h>
d82d5776
KD
21
22#define MAX_PWM 255
23
01695410
PB
24struct pwm_fan_tach {
25 int irq;
26 atomic_t pulses;
27 unsigned int rpm;
28 u8 pulses_per_revolution;
29};
30
d82d5776
KD
31struct pwm_fan_ctx {
32 struct mutex lock;
33 struct pwm_device *pwm;
86585c61 34 struct pwm_state pwm_state;
b57e1d42 35 struct regulator *reg_en;
6b1ec478 36
f0dc7cb6
PB
37 int tach_count;
38 struct pwm_fan_tach *tachs;
6b1ec478
SW
39 ktime_t sample_start;
40 struct timer_list rpm_timer;
41
2e5219c7
LM
42 unsigned int pwm_value;
43 unsigned int pwm_fan_state;
44 unsigned int pwm_fan_max_state;
45 unsigned int *pwm_fan_cooling_levels;
b6bddec0 46 struct thermal_cooling_device *cdev;
1aa03655
PB
47
48 struct hwmon_chip_info info;
f0dc7cb6 49 struct hwmon_channel_info fan_channel;
1aa03655
PB
50};
51
6b1ec478
SW
52/* This handler assumes self resetting edge triggered interrupt. */
53static irqreturn_t pulse_handler(int irq, void *dev_id)
54{
01695410 55 struct pwm_fan_tach *tach = dev_id;
6b1ec478 56
01695410 57 atomic_inc(&tach->pulses);
6b1ec478
SW
58
59 return IRQ_HANDLED;
60}
61
62static void sample_timer(struct timer_list *t)
63{
64 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
fd8feec6 65 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
f0dc7cb6 66 int i;
6b1ec478 67
fd8feec6 68 if (delta) {
f0dc7cb6
PB
69 for (i = 0; i < ctx->tach_count; i++) {
70 struct pwm_fan_tach *tach = &ctx->tachs[i];
71 int pulses;
72
73 pulses = atomic_read(&tach->pulses);
74 atomic_sub(pulses, &tach->pulses);
75 tach->rpm = (unsigned int)(pulses * 1000 * 60) /
76 (tach->pulses_per_revolution * delta);
77 }
fd8feec6
PB
78
79 ctx->sample_start = ktime_get();
80 }
6b1ec478 81
6b1ec478
SW
82 mod_timer(&ctx->rpm_timer, jiffies + HZ);
83}
84
cb85ca33 85static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
d82d5776 86{
677252a1 87 unsigned long period;
cb85ca33 88 int ret = 0;
86585c61 89 struct pwm_state *state = &ctx->pwm_state;
2289711c 90
d82d5776 91 mutex_lock(&ctx->lock);
d82d5776 92 if (ctx->pwm_value == pwm)
cb85ca33 93 goto exit_set_pwm_err;
d82d5776 94
86585c61
UKK
95 period = state->period;
96 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
97 state->enabled = pwm ? true : false;
f354169e 98
86585c61 99 ret = pwm_apply_state(ctx->pwm, state);
677252a1
BZ
100 if (!ret)
101 ctx->pwm_value = pwm;
d82d5776
KD
102exit_set_pwm_err:
103 mutex_unlock(&ctx->lock);
104 return ret;
105}
106
b6bddec0
LM
107static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
108{
109 int i;
110
111 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
112 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
113 break;
114
115 ctx->pwm_fan_state = i;
116}
117
1aa03655
PB
118static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
119 u32 attr, int channel, long val)
cb85ca33
LM
120{
121 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
cb85ca33
LM
122 int ret;
123
1aa03655 124 if (val < 0 || val > MAX_PWM)
cb85ca33
LM
125 return -EINVAL;
126
1aa03655 127 ret = __set_pwm(ctx, val);
cb85ca33
LM
128 if (ret)
129 return ret;
130
1aa03655
PB
131 pwm_fan_update_state(ctx, val);
132 return 0;
cb85ca33
LM
133}
134
1aa03655
PB
135static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
136 u32 attr, int channel, long *val)
d82d5776
KD
137{
138 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
139
1aa03655
PB
140 switch (type) {
141 case hwmon_pwm:
142 *val = ctx->pwm_value;
143 return 0;
d82d5776 144
1aa03655 145 case hwmon_fan:
f0dc7cb6 146 *val = ctx->tachs[channel].rpm;
1aa03655 147 return 0;
6b1ec478 148
1aa03655
PB
149 default:
150 return -ENOTSUPP;
151 }
6b1ec478 152}
d82d5776 153
1aa03655
PB
154static umode_t pwm_fan_is_visible(const void *data,
155 enum hwmon_sensor_types type,
156 u32 attr, int channel)
6b1ec478 157{
1aa03655
PB
158 switch (type) {
159 case hwmon_pwm:
160 return 0644;
6b1ec478 161
1aa03655
PB
162 case hwmon_fan:
163 return 0444;
6b1ec478 164
1aa03655
PB
165 default:
166 return 0;
167 }
6b1ec478
SW
168}
169
1aa03655
PB
170static const struct hwmon_ops pwm_fan_hwmon_ops = {
171 .is_visible = pwm_fan_is_visible,
172 .read = pwm_fan_read,
173 .write = pwm_fan_write,
6b1ec478 174};
d82d5776 175
b6bddec0
LM
176/* thermal cooling device callbacks */
177static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
178 unsigned long *state)
179{
180 struct pwm_fan_ctx *ctx = cdev->devdata;
181
182 if (!ctx)
183 return -EINVAL;
184
185 *state = ctx->pwm_fan_max_state;
186
187 return 0;
188}
189
190static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
191 unsigned long *state)
192{
193 struct pwm_fan_ctx *ctx = cdev->devdata;
194
195 if (!ctx)
196 return -EINVAL;
197
198 *state = ctx->pwm_fan_state;
199
200 return 0;
201}
202
203static int
204pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
205{
206 struct pwm_fan_ctx *ctx = cdev->devdata;
207 int ret;
208
209 if (!ctx || (state > ctx->pwm_fan_max_state))
210 return -EINVAL;
211
212 if (state == ctx->pwm_fan_state)
213 return 0;
214
215 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
216 if (ret) {
217 dev_err(&cdev->device, "Cannot set pwm!\n");
218 return ret;
219 }
220
221 ctx->pwm_fan_state = state;
222
223 return ret;
224}
225
226static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
227 .get_max_state = pwm_fan_get_max_state,
228 .get_cur_state = pwm_fan_get_cur_state,
229 .set_cur_state = pwm_fan_set_cur_state,
230};
231
de52b049
GR
232static int pwm_fan_of_get_cooling_data(struct device *dev,
233 struct pwm_fan_ctx *ctx)
2e5219c7
LM
234{
235 struct device_node *np = dev->of_node;
236 int num, i, ret;
237
238 if (!of_find_property(np, "cooling-levels", NULL))
239 return 0;
240
241 ret = of_property_count_u32_elems(np, "cooling-levels");
242 if (ret <= 0) {
243 dev_err(dev, "Wrong data!\n");
244 return ret ? : -EINVAL;
245 }
246
247 num = ret;
a86854d0 248 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
2e5219c7
LM
249 GFP_KERNEL);
250 if (!ctx->pwm_fan_cooling_levels)
251 return -ENOMEM;
252
253 ret = of_property_read_u32_array(np, "cooling-levels",
254 ctx->pwm_fan_cooling_levels, num);
255 if (ret) {
256 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
257 return ret;
258 }
259
260 for (i = 0; i < num; i++) {
261 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
262 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
263 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
264 return -EINVAL;
265 }
266 }
267
268 ctx->pwm_fan_max_state = num - 1;
269
270 return 0;
271}
272
37bcec5d
GR
273static void pwm_fan_regulator_disable(void *data)
274{
275 regulator_disable(data);
276}
277
a455eda3 278static void pwm_fan_pwm_disable(void *__ctx)
37bcec5d 279{
a455eda3 280 struct pwm_fan_ctx *ctx = __ctx;
86585c61
UKK
281
282 ctx->pwm_state.enabled = false;
283 pwm_apply_state(ctx->pwm, &ctx->pwm_state);
a455eda3 284 del_timer_sync(&ctx->rpm_timer);
37bcec5d
GR
285}
286
d82d5776
KD
287static int pwm_fan_probe(struct platform_device *pdev)
288{
b6bddec0 289 struct thermal_cooling_device *cdev;
37bcec5d 290 struct device *dev = &pdev->dev;
d82d5776 291 struct pwm_fan_ctx *ctx;
b6bddec0 292 struct device *hwmon;
d82d5776 293 int ret;
1aa03655 294 const struct hwmon_channel_info **channels;
f0dc7cb6
PB
295 u32 *fan_channel_config;
296 int channel_count = 1; /* We always have a PWM channel. */
297 int i;
d82d5776 298
37bcec5d 299 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
d82d5776
KD
300 if (!ctx)
301 return -ENOMEM;
302
303 mutex_init(&ctx->lock);
304
37bcec5d 305 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
65b2aad0
AH
306 if (IS_ERR(ctx->pwm))
307 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
d82d5776 308
d82d5776
KD
309 platform_set_drvdata(pdev, ctx);
310
37bcec5d 311 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
b57e1d42
SW
312 if (IS_ERR(ctx->reg_en)) {
313 if (PTR_ERR(ctx->reg_en) != -ENODEV)
314 return PTR_ERR(ctx->reg_en);
315
316 ctx->reg_en = NULL;
317 } else {
318 ret = regulator_enable(ctx->reg_en);
319 if (ret) {
37bcec5d 320 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
b57e1d42
SW
321 return ret;
322 }
5696e4aa
GR
323 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
324 ctx->reg_en);
325 if (ret)
326 return ret;
b57e1d42
SW
327 }
328
86585c61
UKK
329 pwm_init_state(ctx->pwm, &ctx->pwm_state);
330
1eda5233
UKK
331 /*
332 * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
333 * long. Check this here to prevent the fan running at a too low
334 * frequency.
335 */
86585c61 336 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
1eda5233
UKK
337 dev_err(dev, "Configured period too big\n");
338 return -EINVAL;
339 }
340
341 /* Set duty cycle to maximum allowed and enable PWM output */
86585c61 342 ret = __set_pwm(ctx, MAX_PWM);
d82d5776 343 if (ret) {
a455eda3 344 dev_err(dev, "Failed to configure PWM: %d\n", ret);
37bcec5d 345 return ret;
d82d5776 346 }
6b1ec478 347 timer_setup(&ctx->rpm_timer, sample_timer, 0);
5696e4aa
GR
348 ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
349 if (ret)
350 return ret;
6b1ec478 351
f0dc7cb6
PB
352 ctx->tach_count = platform_irq_count(pdev);
353 if (ctx->tach_count < 0)
354 return dev_err_probe(dev, ctx->tach_count,
b5fcb8a4 355 "Could not get number of fan tachometer inputs\n");
f0dc7cb6 356 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count);
b5fcb8a4 357
f0dc7cb6
PB
358 if (ctx->tach_count) {
359 channel_count++; /* We also have a FAN channel. */
360
361 ctx->tachs = devm_kcalloc(dev, ctx->tach_count,
362 sizeof(struct pwm_fan_tach),
363 GFP_KERNEL);
364 if (!ctx->tachs)
365 return -ENOMEM;
366
367 ctx->fan_channel.type = hwmon_fan;
368 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1,
369 sizeof(u32), GFP_KERNEL);
370 if (!fan_channel_config)
371 return -ENOMEM;
372 ctx->fan_channel.config = fan_channel_config;
373 }
374
375 channels = devm_kcalloc(dev, channel_count + 1,
1aa03655
PB
376 sizeof(struct hwmon_channel_info *), GFP_KERNEL);
377 if (!channels)
378 return -ENOMEM;
379
7282d2ae 380 channels[0] = HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT);
1aa03655 381
f0dc7cb6
PB
382 for (i = 0; i < ctx->tach_count; i++) {
383 struct pwm_fan_tach *tach = &ctx->tachs[i];
b5fcb8a4
PB
384 u32 ppr = 2;
385
f0dc7cb6 386 tach->irq = platform_get_irq(pdev, i);
01695410
PB
387 if (tach->irq == -EPROBE_DEFER)
388 return tach->irq;
389 if (tach->irq > 0) {
390 ret = devm_request_irq(dev, tach->irq, pulse_handler, 0,
391 pdev->name, tach);
b5fcb8a4
PB
392 if (ret) {
393 dev_err(dev,
394 "Failed to request interrupt: %d\n",
395 ret);
396 return ret;
397 }
398 }
6b1ec478 399
f0dc7cb6
PB
400 of_property_read_u32_index(dev->of_node,
401 "pulses-per-revolution",
402 i,
403 &ppr);
01695410
PB
404 tach->pulses_per_revolution = ppr;
405 if (!tach->pulses_per_revolution) {
b5fcb8a4
PB
406 dev_err(dev, "pulses-per-revolution can't be zero.\n");
407 return -EINVAL;
6b1ec478 408 }
b5fcb8a4 409
f0dc7cb6
PB
410 fan_channel_config[i] = HWMON_F_INPUT;
411
412 dev_dbg(dev, "tach%d: irq=%d, pulses_per_revolution=%d\n",
413 i, tach->irq, tach->pulses_per_revolution);
414 }
b5fcb8a4 415
f0dc7cb6 416 if (ctx->tach_count > 0) {
6b1ec478
SW
417 ctx->sample_start = ktime_get();
418 mod_timer(&ctx->rpm_timer, jiffies + HZ);
1aa03655 419
f0dc7cb6 420 channels[1] = &ctx->fan_channel;
6b1ec478
SW
421 }
422
1aa03655
PB
423 ctx->info.ops = &pwm_fan_hwmon_ops;
424 ctx->info.info = channels;
425
426 hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
427 ctx, &ctx->info, NULL);
d82d5776 428 if (IS_ERR(hwmon)) {
37bcec5d
GR
429 dev_err(dev, "Failed to register hwmon device\n");
430 return PTR_ERR(hwmon);
d82d5776 431 }
2e5219c7 432
37bcec5d 433 ret = pwm_fan_of_get_cooling_data(dev, ctx);
2e5219c7
LM
434 if (ret)
435 return ret;
436
b6bddec0
LM
437 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
438 if (IS_ENABLED(CONFIG_THERMAL)) {
37bcec5d
GR
439 cdev = devm_thermal_of_cooling_device_register(dev,
440 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
b6bddec0 441 if (IS_ERR(cdev)) {
677252a1 442 ret = PTR_ERR(cdev);
37bcec5d 443 dev_err(dev,
841cf676
RM
444 "Failed to register pwm-fan as cooling device: %d\n",
445 ret);
a455eda3 446 return ret;
b6bddec0
LM
447 }
448 ctx->cdev = cdev;
b6bddec0
LM
449 }
450
d82d5776 451 return 0;
d82d5776
KD
452}
453
7992db7c 454static int pwm_fan_disable(struct device *dev)
d82d5776
KD
455{
456 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
95dcd64b
TR
457 int ret;
458
95dcd64b 459 if (ctx->pwm_value) {
86585c61
UKK
460 /* keep ctx->pwm_state unmodified for pwm_fan_resume() */
461 struct pwm_state state = ctx->pwm_state;
462
463 state.duty_cycle = 0;
464 state.enabled = false;
465 ret = pwm_apply_state(ctx->pwm, &state);
95dcd64b
TR
466 if (ret < 0)
467 return ret;
95dcd64b
TR
468 }
469
b57e1d42
SW
470 if (ctx->reg_en) {
471 ret = regulator_disable(ctx->reg_en);
472 if (ret) {
473 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
474 return ret;
475 }
476 }
477
d82d5776
KD
478 return 0;
479}
480
7992db7c
AM
481static void pwm_fan_shutdown(struct platform_device *pdev)
482{
483 pwm_fan_disable(&pdev->dev);
484}
485
486#ifdef CONFIG_PM_SLEEP
487static int pwm_fan_suspend(struct device *dev)
488{
489 return pwm_fan_disable(dev);
490}
491
d82d5776
KD
492static int pwm_fan_resume(struct device *dev)
493{
494 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
48b9d5b4 495 int ret;
d82d5776 496
b57e1d42
SW
497 if (ctx->reg_en) {
498 ret = regulator_enable(ctx->reg_en);
499 if (ret) {
500 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
501 return ret;
502 }
503 }
504
48b9d5b4
KD
505 if (ctx->pwm_value == 0)
506 return 0;
507
86585c61 508 return pwm_apply_state(ctx->pwm, &ctx->pwm_state);
d82d5776
KD
509}
510#endif
511
512static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
513
d720acac 514static const struct of_device_id of_pwm_fan_match[] = {
d82d5776
KD
515 { .compatible = "pwm-fan", },
516 {},
517};
f491e70c 518MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
d82d5776
KD
519
520static struct platform_driver pwm_fan_driver = {
521 .probe = pwm_fan_probe,
7992db7c 522 .shutdown = pwm_fan_shutdown,
d82d5776
KD
523 .driver = {
524 .name = "pwm-fan",
525 .pm = &pwm_fan_pm,
526 .of_match_table = of_pwm_fan_match,
527 },
528};
529
530module_platform_driver(pwm_fan_driver);
531
532MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
533MODULE_ALIAS("platform:pwm-fan");
534MODULE_DESCRIPTION("PWM FAN driver");
535MODULE_LICENSE("GPL");