a93529e4ba8684dbeec8366ed50f24450c3c49ae
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.10 / 950-0084-Add-rpi-poe-fan-driver.patch
1 From 079bce6210d997fda44d6143b4b394173360bef1 Mon Sep 17 00:00:00 2001
2 From: Serge Schneider <serge@raspberrypi.org>
3 Date: Mon, 9 Jul 2018 12:54:25 +0100
4 Subject: [PATCH] Add rpi-poe-fan driver
5
6 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
7
8 PoE HAT driver cleanup
9
10 * Fix undeclared variable in rpi_poe_fan_suspend
11 * Add SPDX-License-Identifier
12 * Expand PoE acronym in Kconfig help
13 * Give clearer error message on of_property_count_u32_elems fail
14 * Add documentation
15 * Add vendor to of_device_id compatible string.
16 * Rename m_data_s struct to fw_data_s
17 * Fix typos
18
19 Fixes: #2665
20
21 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
22
23 rpi-poe-fan: fix def_pwm1 writes
24
25 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
26 ---
27 .../devicetree/bindings/hwmon/rpi-poe-fan.txt | 55 +++
28 Documentation/hwmon/rpi-poe-fan | 15 +
29 drivers/hwmon/Kconfig | 11 +
30 drivers/hwmon/Makefile | 1 +
31 drivers/hwmon/rpi-poe-fan.c | 436 ++++++++++++++++++
32 5 files changed, 518 insertions(+)
33 create mode 100644 Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
34 create mode 100644 Documentation/hwmon/rpi-poe-fan
35 create mode 100644 drivers/hwmon/rpi-poe-fan.c
36
37 --- /dev/null
38 +++ b/Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
39 @@ -0,0 +1,55 @@
40 +Bindings for the Raspberry Pi PoE HAT fan
41 +
42 +Required properties:
43 +- compatible : "raspberrypi,rpi-poe-fan"
44 +- firmware : Reference to the RPi firmware device node
45 +- pwms : the PWM that is used to control the PWM fan
46 +- cooling-levels : PWM duty cycle values in a range from 0 to 255
47 + which correspond to thermal cooling states
48 +
49 +Example:
50 + fan0: rpi-poe-fan@0 {
51 + compatible = "raspberrypi,rpi-poe-fan";
52 + firmware = <&firmware>;
53 + cooling-min-state = <0>;
54 + cooling-max-state = <3>;
55 + #cooling-cells = <2>;
56 + cooling-levels = <0 50 150 255>;
57 + status = "okay";
58 + };
59 +
60 + thermal-zones {
61 + cpu_thermal: cpu-thermal {
62 + trips {
63 + threshold: trip-point@0 {
64 + temperature = <45000>;
65 + hysteresis = <5000>;
66 + type = "active";
67 + };
68 + target: trip-point@1 {
69 + temperature = <50000>;
70 + hysteresis = <2000>;
71 + type = "active";
72 + };
73 + cpu_hot: cpu_hot@0 {
74 + temperature = <55000>;
75 + hysteresis = <2000>;
76 + type = "active";
77 + };
78 + };
79 + cooling-maps {
80 + map0 {
81 + trip = <&threshold>;
82 + cooling-device = <&fan0 0 1>;
83 + };
84 + map1 {
85 + trip = <&target>;
86 + cooling-device = <&fan0 1 2>;
87 + };
88 + map2 {
89 + trip = <&cpu_hot>;
90 + cooling-device = <&fan0 2 3>;
91 + };
92 + };
93 + };
94 + };
95 --- /dev/null
96 +++ b/Documentation/hwmon/rpi-poe-fan
97 @@ -0,0 +1,15 @@
98 +Kernel driver rpi-poe-fan
99 +=====================
100 +
101 +This driver enables the use of the Raspberry Pi PoE HAT fan.
102 +
103 +Author: Serge Schneider <serge@raspberrypi.org>
104 +
105 +Description
106 +-----------
107 +
108 +The driver implements a simple interface for driving the Raspberry Pi PoE
109 +(Power over Ethernet) HAT fan. The driver passes commands to the Raspberry Pi
110 +firmware through the mailbox property interface. The firmware then forwards
111 +the commands to the board over I2C on the ID_EEPROM pins. The driver exposes
112 +the fan to the user space through the hwmon sysfs interface.
113 --- a/drivers/hwmon/Kconfig
114 +++ b/drivers/hwmon/Kconfig
115 @@ -1489,6 +1489,17 @@ config SENSORS_RASPBERRYPI_HWMON
116 This driver can also be built as a module. If so, the module
117 will be called raspberrypi-hwmon.
118
119 +config SENSORS_RPI_POE_FAN
120 + tristate "Raspberry Pi PoE HAT fan"
121 + depends on RASPBERRYPI_FIRMWARE
122 + depends on THERMAL || THERMAL=n
123 + help
124 + If you say yes here you get support for Raspberry Pi PoE (Power over
125 + Ethernet) HAT fan.
126 +
127 + This driver can also be built as a module. If so, the module
128 + will be called rpi-poe-fan.
129 +
130 config SENSORS_SL28CPLD
131 tristate "Kontron sl28cpld hardware monitoring driver"
132 depends on MFD_SL28CPLD || COMPILE_TEST
133 --- a/drivers/hwmon/Makefile
134 +++ b/drivers/hwmon/Makefile
135 @@ -157,6 +157,7 @@ obj-$(CONFIG_SENSORS_PCF8591) += pcf8591
136 obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o
137 obj-$(CONFIG_SENSORS_PWM_FAN) += pwm-fan.o
138 obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON) += raspberrypi-hwmon.o
139 +obj-$(CONFIG_SENSORS_RPI_POE_FAN) += rpi-poe-fan.o
140 obj-$(CONFIG_SENSORS_S3C) += s3c-hwmon.o
141 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
142 obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o
143 --- /dev/null
144 +++ b/drivers/hwmon/rpi-poe-fan.c
145 @@ -0,0 +1,436 @@
146 +// SPDX-License-Identifier: GPL-2.0
147 +/*
148 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi PoE HAT fan.
149 + *
150 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
151 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
152 + *
153 + * Author: Serge Schneider <serge@raspberrypi.org>
154 + */
155 +
156 +#include <linux/hwmon.h>
157 +#include <linux/hwmon-sysfs.h>
158 +#include <linux/module.h>
159 +#include <linux/mutex.h>
160 +#include <linux/notifier.h>
161 +#include <linux/of.h>
162 +#include <linux/platform_device.h>
163 +#include <linux/reboot.h>
164 +#include <linux/sysfs.h>
165 +#include <linux/thermal.h>
166 +#include <soc/bcm2835/raspberrypi-firmware.h>
167 +
168 +#define MAX_PWM 255
169 +
170 +#define POE_CUR_PWM 0x0
171 +#define POE_DEF_PWM 0x1
172 +
173 +struct rpi_poe_fan_ctx {
174 + struct mutex lock;
175 + struct rpi_firmware *fw;
176 + unsigned int pwm_value;
177 + unsigned int def_pwm_value;
178 + unsigned int rpi_poe_fan_state;
179 + unsigned int rpi_poe_fan_max_state;
180 + unsigned int *rpi_poe_fan_cooling_levels;
181 + struct thermal_cooling_device *cdev;
182 + struct notifier_block nb;
183 +};
184 +
185 +struct fw_tag_data_s{
186 + u32 reg;
187 + u32 val;
188 + u32 ret;
189 +};
190 +
191 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
192 + struct fw_tag_data_s fw_tag_data = {
193 + .reg = reg,
194 + .val = *val
195 + };
196 + int ret;
197 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
198 + &fw_tag_data, sizeof(fw_tag_data));
199 + if (ret) {
200 + return ret;
201 + } else if (fw_tag_data.ret) {
202 + return -EIO;
203 + }
204 + return 0;
205 +}
206 +
207 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
208 + struct fw_tag_data_s fw_tag_data = {
209 + .reg = reg,
210 + };
211 + int ret;
212 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
213 + &fw_tag_data, sizeof(fw_tag_data));
214 + if (ret) {
215 + return ret;
216 + } else if (fw_tag_data.ret) {
217 + return -EIO;
218 + }
219 + *val = fw_tag_data.val;
220 + return 0;
221 +}
222 +
223 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
224 + void *unused)
225 +{
226 + struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
227 + nb);
228 +
229 + if (ctx->pwm_value != ctx->def_pwm_value)
230 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
231 +
232 + return NOTIFY_DONE;
233 +}
234 +
235 +static int __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
236 +{
237 + int ret = 0;
238 +
239 + mutex_lock(&ctx->lock);
240 + if (ctx->pwm_value == pwm)
241 + goto exit_set_pwm_err;
242 +
243 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
244 + if (!ret)
245 + ctx->pwm_value = pwm;
246 +exit_set_pwm_err:
247 + mutex_unlock(&ctx->lock);
248 + return ret;
249 +}
250 +
251 +static int __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
252 +{
253 + int ret = 0;
254 + mutex_lock(&ctx->lock);
255 + if (ctx->def_pwm_value == def_pwm)
256 + goto exit_set_def_pwm_err;
257 +
258 + ret = write_reg(ctx->fw, POE_DEF_PWM, &def_pwm);
259 + if (!ret)
260 + ctx->def_pwm_value = def_pwm;
261 +exit_set_def_pwm_err:
262 + mutex_unlock(&ctx->lock);
263 + return ret;
264 +}
265 +
266 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
267 + unsigned long pwm)
268 +{
269 + int i;
270 +
271 + for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
272 + if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
273 + break;
274 +
275 + ctx->rpi_poe_fan_state = i;
276 +}
277 +
278 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
279 + const char *buf, size_t count)
280 +{
281 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
282 + unsigned long pwm;
283 + int ret;
284 +
285 + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
286 + return -EINVAL;
287 +
288 + ret = __set_pwm(ctx, pwm);
289 + if (ret)
290 + return ret;
291 +
292 + rpi_poe_fan_update_state(ctx, pwm);
293 + return count;
294 +}
295 +
296 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
297 + const char *buf, size_t count)
298 +{
299 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
300 + unsigned long def_pwm;
301 + int ret;
302 +
303 + if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
304 + return -EINVAL;
305 +
306 + ret = __set_def_pwm(ctx, def_pwm);
307 + if (ret)
308 + return ret;
309 + return count;
310 +}
311 +
312 +static ssize_t show_pwm(struct device *dev,
313 + struct device_attribute *attr, char *buf)
314 +{
315 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
316 +
317 + return sprintf(buf, "%u\n", ctx->pwm_value);
318 +}
319 +
320 +static ssize_t show_def_pwm(struct device *dev,
321 + struct device_attribute *attr, char *buf)
322 +{
323 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
324 +
325 + return sprintf(buf, "%u\n", ctx->def_pwm_value);
326 +}
327 +
328 +
329 +static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
330 +static SENSOR_DEVICE_ATTR(def_pwm1, 0644, show_def_pwm, set_def_pwm, 1);
331 +
332 +static struct attribute *rpi_poe_fan_attrs[] = {
333 + &sensor_dev_attr_pwm1.dev_attr.attr,
334 + &sensor_dev_attr_def_pwm1.dev_attr.attr,
335 + NULL,
336 +};
337 +
338 +ATTRIBUTE_GROUPS(rpi_poe_fan);
339 +
340 +/* thermal cooling device callbacks */
341 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
342 + unsigned long *state)
343 +{
344 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
345 +
346 + if (!ctx)
347 + return -EINVAL;
348 +
349 + *state = ctx->rpi_poe_fan_max_state;
350 +
351 + return 0;
352 +}
353 +
354 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
355 + unsigned long *state)
356 +{
357 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
358 +
359 + if (!ctx)
360 + return -EINVAL;
361 +
362 + *state = ctx->rpi_poe_fan_state;
363 +
364 + return 0;
365 +}
366 +
367 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
368 + unsigned long state)
369 +{
370 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
371 + int ret;
372 +
373 + if (!ctx || (state > ctx->rpi_poe_fan_max_state))
374 + return -EINVAL;
375 +
376 + if (state == ctx->rpi_poe_fan_state)
377 + return 0;
378 +
379 + ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
380 + if (ret) {
381 + dev_err(&cdev->device, "Cannot set pwm!\n");
382 + return ret;
383 + }
384 +
385 + ctx->rpi_poe_fan_state = state;
386 +
387 + return ret;
388 +}
389 +
390 +static const struct thermal_cooling_device_ops rpi_poe_fan_cooling_ops = {
391 + .get_max_state = rpi_poe_fan_get_max_state,
392 + .get_cur_state = rpi_poe_fan_get_cur_state,
393 + .set_cur_state = rpi_poe_fan_set_cur_state,
394 +};
395 +
396 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
397 + struct rpi_poe_fan_ctx *ctx)
398 +{
399 + struct device_node *np = dev->of_node;
400 + int num, i, ret;
401 +
402 + if (!of_find_property(np, "cooling-levels", NULL))
403 + return 0;
404 +
405 + ret = of_property_count_u32_elems(np, "cooling-levels");
406 + if (ret <= 0) {
407 + dev_err(dev, "cooling-levels property missing or invalid: %d\n",
408 + ret);
409 + return ret ? : -EINVAL;
410 + }
411 +
412 + num = ret;
413 + ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
414 + GFP_KERNEL);
415 + if (!ctx->rpi_poe_fan_cooling_levels)
416 + return -ENOMEM;
417 +
418 + ret = of_property_read_u32_array(np, "cooling-levels",
419 + ctx->rpi_poe_fan_cooling_levels, num);
420 + if (ret) {
421 + dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
422 + return ret;
423 + }
424 +
425 + for (i = 0; i < num; i++) {
426 + if (ctx->rpi_poe_fan_cooling_levels[i] > MAX_PWM) {
427 + dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
428 + ctx->rpi_poe_fan_cooling_levels[i], MAX_PWM);
429 + return -EINVAL;
430 + }
431 + }
432 +
433 + ctx->rpi_poe_fan_max_state = num - 1;
434 +
435 + return 0;
436 +}
437 +
438 +static int rpi_poe_fan_probe(struct platform_device *pdev)
439 +{
440 + struct thermal_cooling_device *cdev;
441 + struct rpi_poe_fan_ctx *ctx;
442 + struct device *hwmon;
443 + struct device_node *np = pdev->dev.of_node;
444 + struct device_node *fw_node;
445 + int ret;
446 +
447 + fw_node = of_parse_phandle(np, "firmware", 0);
448 + if (!fw_node) {
449 + dev_err(&pdev->dev, "Missing firmware node\n");
450 + return -ENOENT;
451 + }
452 +
453 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
454 + if (!ctx)
455 + return -ENOMEM;
456 +
457 + mutex_init(&ctx->lock);
458 +
459 + ctx->fw = rpi_firmware_get(fw_node);
460 + if (!ctx->fw)
461 + return -EPROBE_DEFER;
462 +
463 + platform_set_drvdata(pdev, ctx);
464 +
465 + ctx->nb.notifier_call = rpi_poe_reboot;
466 + ret = register_reboot_notifier(&ctx->nb);
467 + if (ret) {
468 + dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
469 + ret);
470 + return ret;
471 + }
472 + ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
473 + if (ret) {
474 + dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
475 + ret);
476 + goto err;
477 + }
478 + ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
479 + if (ret) {
480 + dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
481 + ret);
482 + goto err;
483 + }
484 +
485 + hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "rpipoefan",
486 + ctx, rpi_poe_fan_groups);
487 + if (IS_ERR(hwmon)) {
488 + dev_err(&pdev->dev, "Failed to register hwmon device\n");
489 + ret = PTR_ERR(hwmon);
490 + goto err;
491 + }
492 +
493 + ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
494 + if (ret)
495 + return ret;
496 +
497 + rpi_poe_fan_update_state(ctx, ctx->pwm_value);
498 + if (!IS_ENABLED(CONFIG_THERMAL))
499 + return 0;
500 +
501 + cdev = thermal_of_cooling_device_register(np,
502 + "rpi-poe-fan", ctx,
503 + &rpi_poe_fan_cooling_ops);
504 + if (IS_ERR(cdev)) {
505 + dev_err(&pdev->dev,
506 + "Failed to register rpi-poe-fan as cooling device");
507 + ret = PTR_ERR(cdev);
508 + goto err;
509 + }
510 + ctx->cdev = cdev;
511 + thermal_cdev_update(cdev);
512 +
513 + return 0;
514 +err:
515 + unregister_reboot_notifier(&ctx->nb);
516 + return ret;
517 +}
518 +
519 +static int rpi_poe_fan_remove(struct platform_device *pdev)
520 +{
521 + struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
522 + u32 value = ctx->def_pwm_value;
523 +
524 + unregister_reboot_notifier(&ctx->nb);
525 + thermal_cooling_device_unregister(ctx->cdev);
526 + if (ctx->pwm_value != value) {
527 + write_reg(ctx->fw, POE_CUR_PWM, &value);
528 + }
529 + return 0;
530 +}
531 +
532 +#ifdef CONFIG_PM_SLEEP
533 +static int rpi_poe_fan_suspend(struct device *dev)
534 +{
535 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
536 + u32 value = 0;
537 + int ret = 0;
538 +
539 + if (ctx->pwm_value != value)
540 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
541 + return ret;
542 +}
543 +
544 +static int rpi_poe_fan_resume(struct device *dev)
545 +{
546 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
547 + u32 value = ctx->pwm_value;
548 + int ret = 0;
549 +
550 + if (value != 0)
551 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
552 +
553 + return ret;
554 +}
555 +#endif
556 +
557 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
558 + rpi_poe_fan_resume);
559 +
560 +static const struct of_device_id of_rpi_poe_fan_match[] = {
561 + { .compatible = "raspberrypi,rpi-poe-fan", },
562 + {},
563 +};
564 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
565 +
566 +static struct platform_driver rpi_poe_fan_driver = {
567 + .probe = rpi_poe_fan_probe,
568 + .remove = rpi_poe_fan_remove,
569 + .driver = {
570 + .name = "rpi-poe-fan",
571 + .pm = &rpi_poe_fan_pm,
572 + .of_match_table = of_rpi_poe_fan_match,
573 + },
574 +};
575 +
576 +module_platform_driver(rpi_poe_fan_driver);
577 +
578 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
579 +MODULE_ALIAS("platform:rpi-poe-fan");
580 +MODULE_DESCRIPTION("Raspberry Pi PoE HAT fan driver");
581 +MODULE_LICENSE("GPL");