Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hwmon / pwm-fan.c
blob777439f48c1471a1cbd02d5b32208367ddf44b2d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Author: Kamil Debski <k.debski@samsung.com>
8 */
10 #include <linux/hwmon.h>
11 #include <linux/interrupt.h>
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>
17 #include <linux/regulator/consumer.h>
18 #include <linux/sysfs.h>
19 #include <linux/thermal.h>
20 #include <linux/timer.h>
22 #define MAX_PWM 255
24 struct pwm_fan_ctx {
25 struct mutex lock;
26 struct pwm_device *pwm;
27 struct regulator *reg_en;
29 int irq;
30 atomic_t pulses;
31 unsigned int rpm;
32 u8 pulses_per_revolution;
33 ktime_t sample_start;
34 struct timer_list rpm_timer;
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;
42 struct hwmon_chip_info info;
45 static const u32 pwm_fan_channel_config_pwm[] = {
46 HWMON_PWM_INPUT,
50 static const struct hwmon_channel_info pwm_fan_channel_pwm = {
51 .type = hwmon_pwm,
52 .config = pwm_fan_channel_config_pwm,
55 static const u32 pwm_fan_channel_config_fan[] = {
56 HWMON_F_INPUT,
60 static const struct hwmon_channel_info pwm_fan_channel_fan = {
61 .type = hwmon_fan,
62 .config = pwm_fan_channel_config_fan,
65 /* This handler assumes self resetting edge triggered interrupt. */
66 static irqreturn_t pulse_handler(int irq, void *dev_id)
68 struct pwm_fan_ctx *ctx = dev_id;
70 atomic_inc(&ctx->pulses);
72 return IRQ_HANDLED;
75 static void sample_timer(struct timer_list *t)
77 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
78 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
79 int pulses;
81 if (delta) {
82 pulses = atomic_read(&ctx->pulses);
83 atomic_sub(pulses, &ctx->pulses);
84 ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
85 (ctx->pulses_per_revolution * delta);
87 ctx->sample_start = ktime_get();
90 mod_timer(&ctx->rpm_timer, jiffies + HZ);
93 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
95 unsigned long period;
96 int ret = 0;
97 struct pwm_state state = { };
99 mutex_lock(&ctx->lock);
100 if (ctx->pwm_value == pwm)
101 goto exit_set_pwm_err;
103 pwm_init_state(ctx->pwm, &state);
104 period = ctx->pwm->args.period;
105 state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
106 state.enabled = pwm ? true : false;
108 ret = pwm_apply_state(ctx->pwm, &state);
109 if (!ret)
110 ctx->pwm_value = pwm;
111 exit_set_pwm_err:
112 mutex_unlock(&ctx->lock);
113 return ret;
116 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm)
118 int i;
120 for (i = 0; i < ctx->pwm_fan_max_state; ++i)
121 if (pwm < ctx->pwm_fan_cooling_levels[i + 1])
122 break;
124 ctx->pwm_fan_state = i;
127 static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
128 u32 attr, int channel, long val)
130 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
131 int ret;
133 if (val < 0 || val > MAX_PWM)
134 return -EINVAL;
136 ret = __set_pwm(ctx, val);
137 if (ret)
138 return ret;
140 pwm_fan_update_state(ctx, val);
141 return 0;
144 static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
145 u32 attr, int channel, long *val)
147 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
149 switch (type) {
150 case hwmon_pwm:
151 *val = ctx->pwm_value;
152 return 0;
154 case hwmon_fan:
155 *val = ctx->rpm;
156 return 0;
158 default:
159 return -ENOTSUPP;
163 static umode_t pwm_fan_is_visible(const void *data,
164 enum hwmon_sensor_types type,
165 u32 attr, int channel)
167 switch (type) {
168 case hwmon_pwm:
169 return 0644;
171 case hwmon_fan:
172 return 0444;
174 default:
175 return 0;
179 static const struct hwmon_ops pwm_fan_hwmon_ops = {
180 .is_visible = pwm_fan_is_visible,
181 .read = pwm_fan_read,
182 .write = pwm_fan_write,
185 /* thermal cooling device callbacks */
186 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
187 unsigned long *state)
189 struct pwm_fan_ctx *ctx = cdev->devdata;
191 if (!ctx)
192 return -EINVAL;
194 *state = ctx->pwm_fan_max_state;
196 return 0;
199 static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev,
200 unsigned long *state)
202 struct pwm_fan_ctx *ctx = cdev->devdata;
204 if (!ctx)
205 return -EINVAL;
207 *state = ctx->pwm_fan_state;
209 return 0;
212 static int
213 pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
215 struct pwm_fan_ctx *ctx = cdev->devdata;
216 int ret;
218 if (!ctx || (state > ctx->pwm_fan_max_state))
219 return -EINVAL;
221 if (state == ctx->pwm_fan_state)
222 return 0;
224 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
225 if (ret) {
226 dev_err(&cdev->device, "Cannot set pwm!\n");
227 return ret;
230 ctx->pwm_fan_state = state;
232 return ret;
235 static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = {
236 .get_max_state = pwm_fan_get_max_state,
237 .get_cur_state = pwm_fan_get_cur_state,
238 .set_cur_state = pwm_fan_set_cur_state,
241 static int pwm_fan_of_get_cooling_data(struct device *dev,
242 struct pwm_fan_ctx *ctx)
244 struct device_node *np = dev->of_node;
245 int num, i, ret;
247 if (!of_find_property(np, "cooling-levels", NULL))
248 return 0;
250 ret = of_property_count_u32_elems(np, "cooling-levels");
251 if (ret <= 0) {
252 dev_err(dev, "Wrong data!\n");
253 return ret ? : -EINVAL;
256 num = ret;
257 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
258 GFP_KERNEL);
259 if (!ctx->pwm_fan_cooling_levels)
260 return -ENOMEM;
262 ret = of_property_read_u32_array(np, "cooling-levels",
263 ctx->pwm_fan_cooling_levels, num);
264 if (ret) {
265 dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
266 return ret;
269 for (i = 0; i < num; i++) {
270 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
271 dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
272 ctx->pwm_fan_cooling_levels[i], MAX_PWM);
273 return -EINVAL;
277 ctx->pwm_fan_max_state = num - 1;
279 return 0;
282 static void pwm_fan_regulator_disable(void *data)
284 regulator_disable(data);
287 static void pwm_fan_pwm_disable(void *__ctx)
289 struct pwm_fan_ctx *ctx = __ctx;
290 pwm_disable(ctx->pwm);
291 del_timer_sync(&ctx->rpm_timer);
294 static int pwm_fan_probe(struct platform_device *pdev)
296 struct thermal_cooling_device *cdev;
297 struct device *dev = &pdev->dev;
298 struct pwm_fan_ctx *ctx;
299 struct device *hwmon;
300 int ret;
301 struct pwm_state state = { };
302 int tach_count;
303 const struct hwmon_channel_info **channels;
305 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
306 if (!ctx)
307 return -ENOMEM;
309 mutex_init(&ctx->lock);
311 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
312 if (IS_ERR(ctx->pwm))
313 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
315 platform_set_drvdata(pdev, ctx);
317 ctx->reg_en = devm_regulator_get_optional(dev, "fan");
318 if (IS_ERR(ctx->reg_en)) {
319 if (PTR_ERR(ctx->reg_en) != -ENODEV)
320 return PTR_ERR(ctx->reg_en);
322 ctx->reg_en = NULL;
323 } else {
324 ret = regulator_enable(ctx->reg_en);
325 if (ret) {
326 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
327 return ret;
329 ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
330 ctx->reg_en);
331 if (ret)
332 return ret;
335 ctx->pwm_value = MAX_PWM;
337 /* Set duty cycle to maximum allowed and enable PWM output */
338 pwm_init_state(ctx->pwm, &state);
339 state.duty_cycle = ctx->pwm->args.period - 1;
340 state.enabled = true;
342 ret = pwm_apply_state(ctx->pwm, &state);
343 if (ret) {
344 dev_err(dev, "Failed to configure PWM: %d\n", ret);
345 return ret;
347 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;
352 tach_count = platform_irq_count(pdev);
353 if (tach_count < 0)
354 return dev_err_probe(dev, tach_count,
355 "Could not get number of fan tachometer inputs\n");
357 channels = devm_kcalloc(dev, tach_count + 2,
358 sizeof(struct hwmon_channel_info *), GFP_KERNEL);
359 if (!channels)
360 return -ENOMEM;
362 channels[0] = &pwm_fan_channel_pwm;
364 if (tach_count > 0) {
365 u32 ppr = 2;
367 ctx->irq = platform_get_irq(pdev, 0);
368 if (ctx->irq == -EPROBE_DEFER)
369 return ctx->irq;
370 if (ctx->irq > 0) {
371 ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
372 pdev->name, ctx);
373 if (ret) {
374 dev_err(dev,
375 "Failed to request interrupt: %d\n",
376 ret);
377 return ret;
381 of_property_read_u32(dev->of_node,
382 "pulses-per-revolution",
383 &ppr);
384 ctx->pulses_per_revolution = ppr;
385 if (!ctx->pulses_per_revolution) {
386 dev_err(dev, "pulses-per-revolution can't be zero.\n");
387 return -EINVAL;
390 dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n",
391 ctx->irq, ctx->pulses_per_revolution);
393 ctx->sample_start = ktime_get();
394 mod_timer(&ctx->rpm_timer, jiffies + HZ);
396 channels[1] = &pwm_fan_channel_fan;
399 ctx->info.ops = &pwm_fan_hwmon_ops;
400 ctx->info.info = channels;
402 hwmon = devm_hwmon_device_register_with_info(dev, "pwmfan",
403 ctx, &ctx->info, NULL);
404 if (IS_ERR(hwmon)) {
405 dev_err(dev, "Failed to register hwmon device\n");
406 return PTR_ERR(hwmon);
409 ret = pwm_fan_of_get_cooling_data(dev, ctx);
410 if (ret)
411 return ret;
413 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
414 if (IS_ENABLED(CONFIG_THERMAL)) {
415 cdev = devm_thermal_of_cooling_device_register(dev,
416 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
417 if (IS_ERR(cdev)) {
418 ret = PTR_ERR(cdev);
419 dev_err(dev,
420 "Failed to register pwm-fan as cooling device: %d\n",
421 ret);
422 return ret;
424 ctx->cdev = cdev;
425 thermal_cdev_update(cdev);
428 return 0;
431 static int pwm_fan_disable(struct device *dev)
433 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
434 struct pwm_args args;
435 int ret;
437 pwm_get_args(ctx->pwm, &args);
439 if (ctx->pwm_value) {
440 ret = pwm_config(ctx->pwm, 0, args.period);
441 if (ret < 0)
442 return ret;
444 pwm_disable(ctx->pwm);
447 if (ctx->reg_en) {
448 ret = regulator_disable(ctx->reg_en);
449 if (ret) {
450 dev_err(dev, "Failed to disable fan supply: %d\n", ret);
451 return ret;
455 return 0;
458 static void pwm_fan_shutdown(struct platform_device *pdev)
460 pwm_fan_disable(&pdev->dev);
463 #ifdef CONFIG_PM_SLEEP
464 static int pwm_fan_suspend(struct device *dev)
466 return pwm_fan_disable(dev);
469 static int pwm_fan_resume(struct device *dev)
471 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
472 struct pwm_args pargs;
473 unsigned long duty;
474 int ret;
476 if (ctx->reg_en) {
477 ret = regulator_enable(ctx->reg_en);
478 if (ret) {
479 dev_err(dev, "Failed to enable fan supply: %d\n", ret);
480 return ret;
484 if (ctx->pwm_value == 0)
485 return 0;
487 pwm_get_args(ctx->pwm, &pargs);
488 duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
489 ret = pwm_config(ctx->pwm, duty, pargs.period);
490 if (ret)
491 return ret;
492 return pwm_enable(ctx->pwm);
494 #endif
496 static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume);
498 static const struct of_device_id of_pwm_fan_match[] = {
499 { .compatible = "pwm-fan", },
502 MODULE_DEVICE_TABLE(of, of_pwm_fan_match);
504 static struct platform_driver pwm_fan_driver = {
505 .probe = pwm_fan_probe,
506 .shutdown = pwm_fan_shutdown,
507 .driver = {
508 .name = "pwm-fan",
509 .pm = &pwm_fan_pm,
510 .of_match_table = of_pwm_fan_match,
514 module_platform_driver(pwm_fan_driver);
516 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
517 MODULE_ALIAS("platform:pwm-fan");
518 MODULE_DESCRIPTION("PWM FAN driver");
519 MODULE_LICENSE("GPL");