hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/hwmon/pwm-fan.c
....@@ -1,42 +1,82 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
34 *
45 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
56 *
67 * 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.
178 */
189
1910 #include <linux/hwmon.h>
2011 #include <linux/hwmon-sysfs.h>
12
+#include <linux/interrupt.h>
2113 #include <linux/module.h>
2214 #include <linux/mutex.h>
2315 #include <linux/of.h>
2416 #include <linux/platform_device.h>
2517 #include <linux/pwm.h>
18
+#include <linux/regulator/consumer.h>
2619 #include <linux/sysfs.h>
2720 #include <linux/thermal.h>
21
+#include <linux/timer.h>
22
+#include <soc/rockchip/rockchip_system_monitor.h>
2823
2924 #define MAX_PWM 255
25
+
26
+struct thermal_trips {
27
+ int temp;
28
+ int state;
29
+};
3030
3131 struct pwm_fan_ctx {
3232 struct mutex lock;
3333 struct pwm_device *pwm;
34
+ struct regulator *reg_en;
35
+
36
+ int irq;
37
+ atomic_t pulses;
38
+ unsigned int rpm;
39
+ u8 pulses_per_revolution;
40
+ ktime_t sample_start;
41
+ struct timer_list rpm_timer;
42
+
3443 unsigned int pwm_value;
3544 unsigned int pwm_fan_state;
3645 unsigned int pwm_fan_max_state;
3746 unsigned int *pwm_fan_cooling_levels;
3847 struct thermal_cooling_device *cdev;
48
+ struct notifier_block thermal_nb;
49
+ struct thermal_trips *thermal_trips;
50
+ bool thermal_notifier_is_ok;
3951 };
52
+
53
+/* This handler assumes self resetting edge triggered interrupt. */
54
+static 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
+
63
+static void sample_timer(struct timer_list *t)
64
+{
65
+ struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
66
+ unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
67
+ int pulses;
68
+
69
+ if (delta) {
70
+ pulses = atomic_read(&ctx->pulses);
71
+ atomic_sub(pulses, &ctx->pulses);
72
+ ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
73
+ (ctx->pulses_per_revolution * delta);
74
+
75
+ ctx->sample_start = ktime_get();
76
+ }
77
+
78
+ mod_timer(&ctx->rpm_timer, jiffies + HZ);
79
+}
4080
4181 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
4282 {
....@@ -72,8 +112,8 @@
72112 ctx->pwm_fan_state = i;
73113 }
74114
75
-static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
76
- const char *buf, size_t count)
115
+static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
116
+ const char *buf, size_t count)
77117 {
78118 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
79119 unsigned long pwm;
....@@ -90,23 +130,53 @@
90130 return count;
91131 }
92132
93
-static ssize_t show_pwm(struct device *dev,
94
- struct device_attribute *attr, char *buf)
133
+static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
134
+ char *buf)
95135 {
96136 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
97137
98138 return sprintf(buf, "%u\n", ctx->pwm_value);
99139 }
100140
141
+static ssize_t rpm_show(struct device *dev,
142
+ struct device_attribute *attr, char *buf)
143
+{
144
+ struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
101145
102
-static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
146
+ return sprintf(buf, "%u\n", ctx->rpm);
147
+}
148
+
149
+static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
150
+static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0);
103151
104152 static struct attribute *pwm_fan_attrs[] = {
105153 &sensor_dev_attr_pwm1.dev_attr.attr,
154
+ &sensor_dev_attr_fan1_input.dev_attr.attr,
106155 NULL,
107156 };
108157
109
-ATTRIBUTE_GROUPS(pwm_fan);
158
+static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a,
159
+ int n)
160
+{
161
+ struct device *dev = container_of(kobj, struct device, kobj);
162
+ struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
163
+
164
+ /* Hide fan_input in case no interrupt is available */
165
+ if (n == 1 && ctx->irq <= 0)
166
+ return 0;
167
+
168
+ return a->mode;
169
+}
170
+
171
+static const struct attribute_group pwm_fan_group = {
172
+ .attrs = pwm_fan_attrs,
173
+ .is_visible = pwm_fan_attrs_visible,
174
+};
175
+
176
+static const struct attribute_group *pwm_fan_groups[] = {
177
+ &pwm_fan_group,
178
+ NULL,
179
+};
110180
111181 /* thermal cooling device callbacks */
112182 static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev,
....@@ -205,93 +275,234 @@
205275 return 0;
206276 }
207277
278
+static void pwm_fan_regulator_disable(void *data)
279
+{
280
+ regulator_disable(data);
281
+}
282
+
283
+static void pwm_fan_pwm_disable(void *__ctx)
284
+{
285
+ struct pwm_fan_ctx *ctx = __ctx;
286
+ pwm_disable(ctx->pwm);
287
+ del_timer_sync(&ctx->rpm_timer);
288
+}
289
+
290
+static int pwm_fan_get_thermal_trips(struct device *dev, char *porp_name,
291
+ struct thermal_trips **trips)
292
+{
293
+ struct device_node *np = dev->of_node;
294
+ struct thermal_trips *thermal_trips;
295
+ const struct property *prop;
296
+ int count, i;
297
+
298
+ prop = of_find_property(np, porp_name, NULL);
299
+ if (!prop)
300
+ return -EINVAL;
301
+ if (!prop->value)
302
+ return -ENODATA;
303
+ count = of_property_count_u32_elems(np, porp_name);
304
+ if (count < 0)
305
+ return -EINVAL;
306
+ if (count % 2)
307
+ return -EINVAL;
308
+ thermal_trips = devm_kzalloc(dev,
309
+ sizeof(*thermal_trips) * (count / 2 + 1),
310
+ GFP_KERNEL);
311
+ if (!thermal_trips)
312
+ return -ENOMEM;
313
+
314
+ for (i = 0; i < count / 2; i++) {
315
+ of_property_read_u32_index(np, porp_name, 2 * i,
316
+ &thermal_trips[i].temp);
317
+ of_property_read_u32_index(np, porp_name, 2 * i + 1,
318
+ &thermal_trips[i].state);
319
+ }
320
+ thermal_trips[i].temp = 0;
321
+ thermal_trips[i].state = INT_MAX;
322
+
323
+ *trips = thermal_trips;
324
+
325
+ return 0;
326
+}
327
+
328
+static int pwm_fan_temp_to_state(struct pwm_fan_ctx *ctx, int temp)
329
+{
330
+ struct thermal_trips *trips = ctx->thermal_trips;
331
+ int i, state = 0;
332
+
333
+ for (i = 0; trips[i].state != INT_MAX; i++) {
334
+ if (temp >= trips[i].temp)
335
+ state = trips[i].state;
336
+ }
337
+
338
+ return state;
339
+}
340
+
341
+static int pwm_fan_thermal_notifier_call(struct notifier_block *nb,
342
+ unsigned long event, void *data)
343
+{
344
+ struct pwm_fan_ctx *ctx = container_of(nb, struct pwm_fan_ctx, thermal_nb);
345
+ struct system_monitor_event_data *event_data = data;
346
+ int state, ret;
347
+
348
+ if (event != SYSTEM_MONITOR_CHANGE_TEMP)
349
+ return NOTIFY_OK;
350
+
351
+ state = pwm_fan_temp_to_state(ctx, event_data->temp);
352
+ if (state > ctx->pwm_fan_max_state)
353
+ return NOTIFY_BAD;
354
+ if (state == ctx->pwm_fan_state)
355
+ return NOTIFY_OK;
356
+
357
+ ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]);
358
+ if (ret)
359
+ return NOTIFY_BAD;
360
+
361
+ ctx->pwm_fan_state = state;
362
+
363
+ return NOTIFY_OK;
364
+}
365
+
366
+static int pwm_fan_register_thermal_notifier(struct device *dev,
367
+ struct pwm_fan_ctx *ctx)
368
+{
369
+ if (pwm_fan_get_thermal_trips(dev, "rockchip,temp-trips",
370
+ &ctx->thermal_trips))
371
+ return -EINVAL;
372
+
373
+ ctx->thermal_nb.notifier_call = pwm_fan_thermal_notifier_call;
374
+
375
+ return rockchip_system_monitor_register_notifier(&ctx->thermal_nb);
376
+}
377
+
208378 static int pwm_fan_probe(struct platform_device *pdev)
209379 {
210380 struct thermal_cooling_device *cdev;
381
+ struct device *dev = &pdev->dev;
211382 struct pwm_fan_ctx *ctx;
212383 struct device *hwmon;
213384 int ret;
214385 struct pwm_state state = { };
386
+ u32 ppr = 2;
215387
216
- ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
388
+ ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
217389 if (!ctx)
218390 return -ENOMEM;
219391
220392 mutex_init(&ctx->lock);
221393
222
- ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL);
223
- if (IS_ERR(ctx->pwm)) {
224
- ret = PTR_ERR(ctx->pwm);
225
-
226
- if (ret != -EPROBE_DEFER)
227
- dev_err(&pdev->dev, "Could not get PWM: %d\n", ret);
228
-
229
- return ret;
230
- }
394
+ ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL);
395
+ if (IS_ERR(ctx->pwm))
396
+ return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
231397
232398 platform_set_drvdata(pdev, ctx);
233399
400
+ ctx->irq = platform_get_irq_optional(pdev, 0);
401
+ if (ctx->irq == -EPROBE_DEFER)
402
+ return ctx->irq;
403
+
404
+ ctx->reg_en = devm_regulator_get_optional(dev, "fan");
405
+ if (IS_ERR(ctx->reg_en)) {
406
+ if (PTR_ERR(ctx->reg_en) != -ENODEV)
407
+ return PTR_ERR(ctx->reg_en);
408
+
409
+ ctx->reg_en = NULL;
410
+ } else {
411
+ ret = regulator_enable(ctx->reg_en);
412
+ if (ret) {
413
+ dev_err(dev, "Failed to enable fan supply: %d\n", ret);
414
+ return ret;
415
+ }
416
+ ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
417
+ ctx->reg_en);
418
+ if (ret)
419
+ return ret;
420
+ }
421
+
234422 ctx->pwm_value = MAX_PWM;
235423
236
- /* Set duty cycle to maximum allowed and enable PWM output */
237424 pwm_init_state(ctx->pwm, &state);
425
+ /*
426
+ * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
427
+ * long. Check this here to prevent the fan running at a too low
428
+ * frequency.
429
+ */
430
+ if (state.period > ULONG_MAX / MAX_PWM + 1) {
431
+ dev_err(dev, "Configured period too big\n");
432
+ return -EINVAL;
433
+ }
434
+
435
+ /* Set duty cycle to maximum allowed and enable PWM output */
238436 state.duty_cycle = ctx->pwm->args.period - 1;
239437 state.enabled = true;
240438
241439 ret = pwm_apply_state(ctx->pwm, &state);
242440 if (ret) {
243
- dev_err(&pdev->dev, "Failed to configure PWM\n");
441
+ dev_err(dev, "Failed to configure PWM: %d\n", ret);
244442 return ret;
245443 }
444
+ timer_setup(&ctx->rpm_timer, sample_timer, 0);
445
+ ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx);
446
+ if (ret)
447
+ return ret;
246448
247
- hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
248
- ctx, pwm_fan_groups);
249
- if (IS_ERR(hwmon)) {
250
- dev_err(&pdev->dev, "Failed to register hwmon device\n");
251
- ret = PTR_ERR(hwmon);
252
- goto err_pwm_disable;
449
+ of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr);
450
+ ctx->pulses_per_revolution = ppr;
451
+ if (!ctx->pulses_per_revolution) {
452
+ dev_err(dev, "pulses-per-revolution can't be zero.\n");
453
+ return -EINVAL;
253454 }
254455
255
- ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
456
+ if (ctx->irq > 0) {
457
+ ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
458
+ pdev->name, ctx);
459
+ if (ret) {
460
+ dev_err(dev, "Failed to request interrupt: %d\n", ret);
461
+ return ret;
462
+ }
463
+ ctx->sample_start = ktime_get();
464
+ mod_timer(&ctx->rpm_timer, jiffies + HZ);
465
+ }
466
+
467
+ hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan",
468
+ ctx, pwm_fan_groups);
469
+ if (IS_ERR(hwmon)) {
470
+ dev_err(dev, "Failed to register hwmon device\n");
471
+ return PTR_ERR(hwmon);
472
+ }
473
+
474
+ ret = pwm_fan_of_get_cooling_data(dev, ctx);
256475 if (ret)
257
- goto err_pwm_disable;
476
+ return ret;
258477
259478 ctx->pwm_fan_state = ctx->pwm_fan_max_state;
479
+ if (IS_REACHABLE(CONFIG_ROCKCHIP_SYSTEM_MONITOR) &&
480
+ of_find_property(dev->of_node, "rockchip,temp-trips", NULL)) {
481
+ ret = pwm_fan_register_thermal_notifier(dev, ctx);
482
+ if (ret)
483
+ dev_err(dev, "Failed to register thermal notifier: %d\n", ret);
484
+ else
485
+ ctx->thermal_notifier_is_ok = true;
486
+ return 0;
487
+ }
260488 if (IS_ENABLED(CONFIG_THERMAL)) {
261
- cdev = thermal_of_cooling_device_register(pdev->dev.of_node,
262
- "pwm-fan", ctx,
263
- &pwm_fan_cooling_ops);
489
+ cdev = devm_thermal_of_cooling_device_register(dev,
490
+ dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops);
264491 if (IS_ERR(cdev)) {
265
- dev_err(&pdev->dev,
266
- "Failed to register pwm-fan as cooling device");
267492 ret = PTR_ERR(cdev);
268
- goto err_pwm_disable;
493
+ dev_err(dev,
494
+ "Failed to register pwm-fan as cooling device: %d\n",
495
+ ret);
496
+ return ret;
269497 }
270498 ctx->cdev = cdev;
271499 thermal_cdev_update(cdev);
272500 }
273501
274502 return 0;
275
-
276
-err_pwm_disable:
277
- state.enabled = false;
278
- pwm_apply_state(ctx->pwm, &state);
279
-
280
- return ret;
281503 }
282504
283
-static int pwm_fan_remove(struct platform_device *pdev)
284
-{
285
- struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev);
286
-
287
- thermal_cooling_device_unregister(ctx->cdev);
288
- if (ctx->pwm_value)
289
- pwm_disable(ctx->pwm);
290
- return 0;
291
-}
292
-
293
-#ifdef CONFIG_PM_SLEEP
294
-static int pwm_fan_suspend(struct device *dev)
505
+static int pwm_fan_disable(struct device *dev)
295506 {
296507 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
297508 struct pwm_args args;
....@@ -299,7 +510,7 @@
299510
300511 pwm_get_args(ctx->pwm, &args);
301512
302
- if (ctx->pwm_value) {
513
+ if (ctx->pwm_value || ctx->thermal_notifier_is_ok) {
303514 ret = pwm_config(ctx->pwm, 0, args.period);
304515 if (ret < 0)
305516 return ret;
....@@ -307,7 +518,26 @@
307518 pwm_disable(ctx->pwm);
308519 }
309520
521
+ if (ctx->reg_en) {
522
+ ret = regulator_disable(ctx->reg_en);
523
+ if (ret) {
524
+ dev_err(dev, "Failed to disable fan supply: %d\n", ret);
525
+ return ret;
526
+ }
527
+ }
528
+
310529 return 0;
530
+}
531
+
532
+static void pwm_fan_shutdown(struct platform_device *pdev)
533
+{
534
+ pwm_fan_disable(&pdev->dev);
535
+}
536
+
537
+#ifdef CONFIG_PM_SLEEP
538
+static int pwm_fan_suspend(struct device *dev)
539
+{
540
+ return pwm_fan_disable(dev);
311541 }
312542
313543 static int pwm_fan_resume(struct device *dev)
....@@ -317,7 +547,15 @@
317547 unsigned long duty;
318548 int ret;
319549
320
- if (ctx->pwm_value == 0)
550
+ if (ctx->reg_en) {
551
+ ret = regulator_enable(ctx->reg_en);
552
+ if (ret) {
553
+ dev_err(dev, "Failed to enable fan supply: %d\n", ret);
554
+ return ret;
555
+ }
556
+ }
557
+
558
+ if (ctx->pwm_value == 0 && !ctx->thermal_notifier_is_ok)
321559 return 0;
322560
323561 pwm_get_args(ctx->pwm, &pargs);
....@@ -339,7 +577,7 @@
339577
340578 static struct platform_driver pwm_fan_driver = {
341579 .probe = pwm_fan_probe,
342
- .remove = pwm_fan_remove,
580
+ .shutdown = pwm_fan_shutdown,
343581 .driver = {
344582 .name = "pwm-fan",
345583 .pm = &pwm_fan_pm,