blob: ec171f2b684a174e2da05bd3874d8a10352d2e42 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
10#include <linux/hwmon.h>
11#include <linux/hwmon-sysfs.h>
David Brazdil0f672f62019-12-10 10:32:29 +000012#include <linux/interrupt.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/of.h>
16#include <linux/platform_device.h>
17#include <linux/pwm.h>
David Brazdil0f672f62019-12-10 10:32:29 +000018#include <linux/regulator/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include <linux/sysfs.h>
20#include <linux/thermal.h>
David Brazdil0f672f62019-12-10 10:32:29 +000021#include <linux/timer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
23#define MAX_PWM 255
24
25struct pwm_fan_ctx {
26 struct mutex lock;
27 struct pwm_device *pwm;
David Brazdil0f672f62019-12-10 10:32:29 +000028 struct regulator *reg_en;
29
30 int irq;
31 atomic_t pulses;
32 unsigned int rpm;
33 u8 pulses_per_revolution;
34 ktime_t sample_start;
35 struct timer_list rpm_timer;
36
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 unsigned int pwm_value;
38 unsigned int pwm_fan_state;
39 unsigned int pwm_fan_max_state;
40 unsigned int *pwm_fan_cooling_levels;
41 struct thermal_cooling_device *cdev;
42};
43
David Brazdil0f672f62019-12-10 10:32:29 +000044/* This handler assumes self resetting edge triggered interrupt. */
45static irqreturn_t pulse_handler(int irq, void *dev_id)
46{
47 struct pwm_fan_ctx *ctx = dev_id;
48
49 atomic_inc(&ctx->pulses);
50
51 return IRQ_HANDLED;
52}
53
54static void sample_timer(struct timer_list *t)
55{
56 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
Olivier Deprez0e641232021-09-23 10:07:05 +020057 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
David Brazdil0f672f62019-12-10 10:32:29 +000058 int pulses;
David Brazdil0f672f62019-12-10 10:32:29 +000059
Olivier Deprez0e641232021-09-23 10:07:05 +020060 if (delta) {
61 pulses = atomic_read(&ctx->pulses);
62 atomic_sub(pulses, &ctx->pulses);
63 ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
64 (ctx->pulses_per_revolution * delta);
David Brazdil0f672f62019-12-10 10:32:29 +000065
Olivier Deprez0e641232021-09-23 10:07:05 +020066 ctx->sample_start = ktime_get();
67 }
68
David Brazdil0f672f62019-12-10 10:32:29 +000069 mod_timer(&ctx->rpm_timer, jiffies + HZ);
70}
71
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
73{
74 unsigned long period;
75 int ret = 0;
76 struct pwm_state state = { };
77
78 mutex_lock(&ctx->lock);
79 if (ctx->pwm_value == pwm)
80 goto exit_set_pwm_err;
81
82 pwm_init_state(ctx->pwm, &state);
83 period = ctx->pwm->args.period;
84 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
85 state.enabled = pwm ? true : false;
86
87 ret = pwm_apply_state(ctx->pwm, &state);
88 if (!ret)
89 ctx->pwm_value = pwm;
90exit_set_pwm_err:
91 mutex_unlock(&ctx->lock);
92 return ret;
93}
94
95static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
96{
97 int i;
98
99 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
100 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
101 break;
102
103 ctx->pwm_fan_state = i;
104}
105
David Brazdil0f672f62019-12-10 10:32:29 +0000106static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
107 const char *buf, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108{
109 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
110 unsigned long pwm;
111 int ret;
112
113 if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
114 return -EINVAL;
115
116 ret = __set_pwm(ctx, pwm);
117 if (ret)
118 return ret;
119
120 pwm_fan_update_state(ctx, pwm);
121 return count;
122}
123
David Brazdil0f672f62019-12-10 10:32:29 +0000124static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
125 char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126{
127 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
128
129 return sprintf(buf, "%u\n", ctx->pwm_value);
130}
131
David Brazdil0f672f62019-12-10 10:32:29 +0000132static ssize_t rpm_show(struct device *dev,
133 struct device_attribute *attr, char *buf)
134{
135 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136
David Brazdil0f672f62019-12-10 10:32:29 +0000137 return sprintf(buf, "%u\n", ctx->rpm);
138}
139
140static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
141static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142
143static struct attribute *pwm_fan_attrs[] = {
144 &sensor_dev_attr_pwm1.dev_attr.attr,
David Brazdil0f672f62019-12-10 10:32:29 +0000145 &sensor_dev_attr_fan1_input.dev_attr.attr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146 NULL,
147};
148
David Brazdil0f672f62019-12-10 10:32:29 +0000149static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
150 int n)
151{
152 struct device *dev = container_of(kobj, struct device, kobj);
153 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
154
155 /* Hide fan_input in case no interrupt is available */
156 if (n == 1 && ctx->irq <= 0)
157 return 0;
158
159 return a->mode;
160}
161
162static const struct attribute_group pwm_fan_group = {
163 .attrs = pwm_fan_attrs,
164 .is_visible = pwm_fan_attrs_visible,
165};
166
167static const struct attribute_group *pwm_fan_groups[] = {
168 &pwm_fan_group,
169 NULL,
170};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171
172/* thermal cooling device callbacks */
173static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
174 unsigned long *state)
175{
176 struct pwm_fan_ctx *ctx = cdev->devdata;
177
178 if (!ctx)
179 return -EINVAL;
180
181 *state = ctx->pwm_fan_max_state;
182
183 return 0;
184}
185
186static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
187 unsigned long *state)
188{
189 struct pwm_fan_ctx *ctx = cdev->devdata;
190
191 if (!ctx)
192 return -EINVAL;
193
194 *state = ctx->pwm_fan_state;
195
196 return 0;
197}
198
199static int
200pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
201{
202 struct pwm_fan_ctx *ctx = cdev->devdata;
203 int ret;
204
205 if (!ctx || (state > ctx->pwm_fan_max_state))
206 return -EINVAL;
207
208 if (state == ctx->pwm_fan_state)
209 return 0;
210
211 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
212 if (ret) {
213 dev_err(&cdev->device, "Cannot set pwm!\n");
214 return ret;
215 }
216
217 ctx->pwm_fan_state = state;
218
219 return ret;
220}
221
222static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
223 .get_max_state = pwm_fan_get_max_state,
224 .get_cur_state = pwm_fan_get_cur_state,
225 .set_cur_state = pwm_fan_set_cur_state,
226};
227
228static int pwm_fan_of_get_cooling_data(struct device *dev,
229 struct pwm_fan_ctx *ctx)
230{
231 struct device_node *np = dev->of_node;
232 int num, i, ret;
233
234 if (!of_find_property(np, "cooling-levels", NULL))
235 return 0;
236
237 ret = of_property_count_u32_elems(np, "cooling-levels");
238 if (ret <= 0) {
239 dev_err(dev, "Wrong data!\n");
240 return ret ? : -EINVAL;
241 }
242
243 num = ret;
244 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
245 GFP_KERNEL);
246 if (!ctx->pwm_fan_cooling_levels)
247 return -ENOMEM;
248
249 ret = of_property_read_u32_array(np, "cooling-levels",
250 ctx->pwm_fan_cooling_levels, num);
251 if (ret) {
252 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
253 return ret;
254 }
255
256 for (i = 0; i < num; i++) {
257 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
258 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
259 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
260 return -EINVAL;
261 }
262 }
263
264 ctx->pwm_fan_max_state = num - 1;
265
266 return 0;
267}
268
David Brazdil0f672f62019-12-10 10:32:29 +0000269static void pwm_fan_regulator_disable(void *data)
270{
271 regulator_disable(data);
272}
273
274static void pwm_fan_pwm_disable(void *__ctx)
275{
276 struct pwm_fan_ctx *ctx = __ctx;
277 pwm_disable(ctx->pwm);
278 del_timer_sync(&ctx->rpm_timer);
279}
280
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281static int pwm_fan_probe(struct platform_device *pdev)
282{
283 struct thermal_cooling_device *cdev;
David Brazdil0f672f62019-12-10 10:32:29 +0000284 struct device *dev = &pdev->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000285 struct pwm_fan_ctx *ctx;
286 struct device *hwmon;
287 int ret;
288 struct pwm_state state = { };
David Brazdil0f672f62019-12-10 10:32:29 +0000289 u32 ppr = 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000290
David Brazdil0f672f62019-12-10 10:32:29 +0000291 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292 if (!ctx)
293 return -ENOMEM;
294
295 mutex_init(&ctx->lock);
296
David Brazdil0f672f62019-12-10 10:32:29 +0000297 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200298 if (IS_ERR(ctx->pwm))
299 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000300
301 platform_set_drvdata(pdev, ctx);
302
David Brazdil0f672f62019-12-10 10:32:29 +0000303 ctx->irq = platform_get_irq_optional(pdev, 0);
304 if (ctx->irq == -EPROBE_DEFER)
305 return ctx->irq;
306
307 ctx->reg_en = devm_regulator_get_optional(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(dev, "Failed to enable fan supply: %d\n", ret);
317 return ret;
318 }
319 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
320 ctx->reg_en);
321 if (ret)
322 return ret;
323 }
324
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325 ctx->pwm_value = MAX_PWM;
326
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000327 pwm_init_state(ctx->pwm, &state);
Olivier Deprez0e641232021-09-23 10:07:05 +0200328 /*
329 * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
330 * long. Check this here to prevent the fan running at a too low
331 * frequency.
332 */
333 if (state.period > ULONG_MAX / MAX_PWM + 1) {
334 dev_err(dev, "Configured period too big\n");
335 return -EINVAL;
336 }
337
338 /* Set duty cycle to maximum allowed and enable PWM output */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 state.duty_cycle = ctx->pwm->args.period - 1;
340 state.enabled = true;
341
342 ret = pwm_apply_state(ctx->pwm, &state);
343 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +0000344 dev_err(dev, "Failed to configure PWM: %d\n", ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345 return ret;
346 }
David Brazdil0f672f62019-12-10 10:32:29 +0000347 timer_setup(&ctx->rpm_timer, sample_timer, 0);
348 ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
349 if (ret)
350 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000351
David Brazdil0f672f62019-12-10 10:32:29 +0000352 of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr);
353 ctx->pulses_per_revolution = ppr;
354 if (!ctx->pulses_per_revolution) {
355 dev_err(dev, "pulses-per-revolution can't be zero.\n");
356 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357 }
358
David Brazdil0f672f62019-12-10 10:32:29 +0000359 if (ctx->irq > 0) {
360 ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
361 pdev->name, ctx);
362 if (ret) {
363 dev_err(dev, "Failed to request interrupt: %d\n", ret);
364 return ret;
365 }
366 ctx->sample_start = ktime_get();
367 mod_timer(&ctx->rpm_timer, jiffies + HZ);
368 }
369
370 hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
371 ctx, pwm_fan_groups);
372 if (IS_ERR(hwmon)) {
373 dev_err(dev, "Failed to register hwmon device\n");
374 return PTR_ERR(hwmon);
375 }
376
377 ret = pwm_fan_of_get_cooling_data(dev, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000378 if (ret)
379 return ret;
380
381 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
382 if (IS_ENABLED(CONFIG_THERMAL)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000383 cdev = devm_thermal_of_cooling_device_register(dev,
384 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000385 if (IS_ERR(cdev)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386 ret = PTR_ERR(cdev);
David Brazdil0f672f62019-12-10 10:32:29 +0000387 dev_err(dev,
388 "Failed to register pwm-fan as cooling device: %d\n",
389 ret);
390 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000391 }
392 ctx->cdev = cdev;
393 thermal_cdev_update(cdev);
394 }
395
396 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000397}
398
Olivier Deprez157378f2022-04-04 15:47:50 +0200399static int pwm_fan_disable(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400{
401 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
402 struct pwm_args args;
403 int ret;
404
405 pwm_get_args(ctx->pwm, &args);
406
407 if (ctx->pwm_value) {
408 ret = pwm_config(ctx->pwm, 0, args.period);
409 if (ret < 0)
410 return ret;
411
412 pwm_disable(ctx->pwm);
413 }
414
David Brazdil0f672f62019-12-10 10:32:29 +0000415 if (ctx->reg_en) {
416 ret = regulator_disable(ctx->reg_en);
417 if (ret) {
418 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
419 return ret;
420 }
421 }
422
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423 return 0;
424}
425
Olivier Deprez157378f2022-04-04 15:47:50 +0200426static void pwm_fan_shutdown(struct platform_device *pdev)
427{
428 pwm_fan_disable(&pdev->dev);
429}
430
431#ifdef CONFIG_PM_SLEEP
432static int pwm_fan_suspend(struct device *dev)
433{
434 return pwm_fan_disable(dev);
435}
436
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000437static int pwm_fan_resume(struct device *dev)
438{
439 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
440 struct pwm_args pargs;
441 unsigned long duty;
442 int ret;
443
David Brazdil0f672f62019-12-10 10:32:29 +0000444 if (ctx->reg_en) {
445 ret = regulator_enable(ctx->reg_en);
446 if (ret) {
447 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
448 return ret;
449 }
450 }
451
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 if (ctx->pwm_value == 0)
453 return 0;
454
455 pwm_get_args(ctx->pwm, &pargs);
Olivier Deprez157378f2022-04-04 15:47:50 +0200456 duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000457 ret = pwm_config(ctx->pwm, duty, pargs.period);
458 if (ret)
459 return ret;
460 return pwm_enable(ctx->pwm);
461}
462#endif
463
464static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
465
466static const struct of_device_id of_pwm_fan_match[] = {
467 { .compatible = "pwm-fan", },
468 {},
469};
470MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
471
472static struct platform_driver pwm_fan_driver = {
473 .probe = pwm_fan_probe,
Olivier Deprez157378f2022-04-04 15:47:50 +0200474 .shutdown = pwm_fan_shutdown,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000475 .driver = {
476 .name = "pwm-fan",
477 .pm = &pwm_fan_pm,
478 .of_match_table = of_pwm_fan_match,
479 },
480};
481
482module_platform_driver(pwm_fan_driver);
483
484MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
485MODULE_ALIAS("platform:pwm-fan");
486MODULE_DESCRIPTION("PWM FAN driver");
487MODULE_LICENSE("GPL");