brcm2708: update to latest patches from the RPi foundation
[openwrt/staging/wigyori.git] / target / linux / brcm2708 / patches-4.19 / 950-0138-Add-rpi-poe-fan-driver.patch
1 From 1c1f26355604567bc11084cb3c4db204d7420941 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 138/725] 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 .../devicetree/bindings/hwmon/rpi-poe-fan.txt | 55 +++
24 Documentation/hwmon/rpi-poe-fan | 15 +
25 arch/arm/boot/dts/overlays/Makefile | 1 +
26 arch/arm/boot/dts/overlays/README | 6 +
27 .../arm/boot/dts/overlays/rpi-poe-overlay.dts | 61 +++
28 arch/arm/configs/bcm2709_defconfig | 1 +
29 arch/arm/configs/bcmrpi_defconfig | 1 +
30 drivers/hwmon/Kconfig | 11 +
31 drivers/hwmon/Makefile | 1 +
32 drivers/hwmon/rpi-poe-fan.c | 436 ++++++++++++++++++
33 include/soc/bcm2835/raspberrypi-firmware.h | 2 +
34 11 files changed, 590 insertions(+)
35 create mode 100644 Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
36 create mode 100644 Documentation/hwmon/rpi-poe-fan
37 create mode 100644 arch/arm/boot/dts/overlays/rpi-poe-overlay.dts
38 create mode 100644 drivers/hwmon/rpi-poe-fan.c
39
40 --- /dev/null
41 +++ b/Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
42 @@ -0,0 +1,55 @@
43 +Bindings for the Raspberry Pi PoE HAT fan
44 +
45 +Required properties:
46 +- compatible : "raspberrypi,rpi-poe-fan"
47 +- firmware : Reference to the RPi firmware device node
48 +- pwms : the PWM that is used to control the PWM fan
49 +- cooling-levels : PWM duty cycle values in a range from 0 to 255
50 + which correspond to thermal cooling states
51 +
52 +Example:
53 + fan0: rpi-poe-fan@0 {
54 + compatible = "raspberrypi,rpi-poe-fan";
55 + firmware = <&firmware>;
56 + cooling-min-state = <0>;
57 + cooling-max-state = <3>;
58 + #cooling-cells = <2>;
59 + cooling-levels = <0 50 150 255>;
60 + status = "okay";
61 + };
62 +
63 + thermal-zones {
64 + cpu_thermal: cpu-thermal {
65 + trips {
66 + threshold: trip-point@0 {
67 + temperature = <45000>;
68 + hysteresis = <5000>;
69 + type = "active";
70 + };
71 + target: trip-point@1 {
72 + temperature = <50000>;
73 + hysteresis = <2000>;
74 + type = "active";
75 + };
76 + cpu_hot: cpu_hot@0 {
77 + temperature = <55000>;
78 + hysteresis = <2000>;
79 + type = "active";
80 + };
81 + };
82 + cooling-maps {
83 + map0 {
84 + trip = <&threshold>;
85 + cooling-device = <&fan0 0 1>;
86 + };
87 + map1 {
88 + trip = <&target>;
89 + cooling-device = <&fan0 1 2>;
90 + };
91 + map2 {
92 + trip = <&cpu_hot>;
93 + cooling-device = <&fan0 2 3>;
94 + };
95 + };
96 + };
97 + };
98 --- /dev/null
99 +++ b/Documentation/hwmon/rpi-poe-fan
100 @@ -0,0 +1,15 @@
101 +Kernel driver rpi-poe-fan
102 +=====================
103 +
104 +This driver enables the use of the Raspberry Pi PoE HAT fan.
105 +
106 +Author: Serge Schneider <serge@raspberrypi.org>
107 +
108 +Description
109 +-----------
110 +
111 +The driver implements a simple interface for driving the Raspberry Pi PoE
112 +(Power over Ethernet) HAT fan. The driver passes commands to the Raspberry Pi
113 +firmware through the mailbox property interface. The firmware then forwards
114 +the commands to the board over I2C on the ID_EEPROM pins. The driver exposes
115 +the fan to the user space through the hwmon sysfs interface.
116 --- a/arch/arm/boot/dts/overlays/Makefile
117 +++ b/arch/arm/boot/dts/overlays/Makefile
118 @@ -100,6 +100,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
119 rpi-dac.dtbo \
120 rpi-display.dtbo \
121 rpi-ft5406.dtbo \
122 + rpi-poe.dtbo \
123 rpi-proto.dtbo \
124 rpi-sense.dtbo \
125 rpi-tv.dtbo \
126 --- a/arch/arm/boot/dts/overlays/README
127 +++ b/arch/arm/boot/dts/overlays/README
128 @@ -1515,6 +1515,12 @@ Params: touchscreen-size-x Touchscr
129 touchscreen-swapped-x-y Swap X and Y cordinates (default 0);
130
131
132 +Name: rpi-poe
133 +Info: Raspberry Pi POE HAT
134 +Load: dtoverlay=rpi-poe
135 +Params: <None>
136 +
137 +
138 Name: rpi-proto
139 Info: Configures the RPi Proto audio card
140 Load: dtoverlay=rpi-proto
141 --- /dev/null
142 +++ b/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts
143 @@ -0,0 +1,61 @@
144 +/*
145 + * Overlay for the Raspberry Pi POE HAT.
146 + */
147 +/dts-v1/;
148 +/plugin/;
149 +
150 +/ {
151 + compatible = "brcm,bcm2708";
152 +
153 + fragment@0 {
154 + target-path = "/";
155 + __overlay__ {
156 + fan0: rpi-poe-fan@0 {
157 + compatible = "raspberrypi,rpi-poe-fan";
158 + firmware = <&firmware>;
159 + cooling-min-state = <0>;
160 + cooling-max-state = <3>;
161 + #cooling-cells = <2>;
162 + cooling-levels = <0 50 150 255>;
163 + status = "okay";
164 + };
165 + };
166 + };
167 +
168 + fragment@1 {
169 + target = <&cpu_thermal>;
170 + __overlay__ {
171 + trips {
172 + threshold: trip-point@0 {
173 + temperature = <45000>;
174 + hysteresis = <5000>;
175 + type = "active";
176 + };
177 + target: trip-point@1 {
178 + temperature = <50000>;
179 + hysteresis = <2000>;
180 + type = "active";
181 + };
182 + cpu_hot: cpu_hot@0 {
183 + temperature = <55000>;
184 + hysteresis = <2000>;
185 + type = "active";
186 + };
187 + };
188 + cooling-maps {
189 + map0 {
190 + trip = <&threshold>;
191 + cooling-device = <&fan0 0 1>;
192 + };
193 + map1 {
194 + trip = <&target>;
195 + cooling-device = <&fan0 1 2>;
196 + };
197 + map2 {
198 + trip = <&cpu_hot>;
199 + cooling-device = <&fan0 2 3>;
200 + };
201 + };
202 + };
203 + };
204 +};
205 --- a/arch/arm/configs/bcm2709_defconfig
206 +++ b/arch/arm/configs/bcm2709_defconfig
207 @@ -659,6 +659,7 @@ CONFIG_HWMON=m
208 CONFIG_SENSORS_DS1621=m
209 CONFIG_SENSORS_JC42=m
210 CONFIG_SENSORS_LM75=m
211 +CONFIG_SENSORS_RPI_POE_FAN=m
212 CONFIG_SENSORS_SHT21=m
213 CONFIG_SENSORS_SHT3x=m
214 CONFIG_SENSORS_SHTC1=m
215 --- a/arch/arm/configs/bcmrpi_defconfig
216 +++ b/arch/arm/configs/bcmrpi_defconfig
217 @@ -652,6 +652,7 @@ CONFIG_HWMON=m
218 CONFIG_SENSORS_DS1621=m
219 CONFIG_SENSORS_JC42=m
220 CONFIG_SENSORS_LM75=m
221 +CONFIG_SENSORS_RPI_POE_FAN=m
222 CONFIG_SENSORS_SHT21=m
223 CONFIG_SENSORS_SHT3x=m
224 CONFIG_SENSORS_SHTC1=m
225 --- a/drivers/hwmon/Kconfig
226 +++ b/drivers/hwmon/Kconfig
227 @@ -1330,6 +1330,17 @@ config SENSORS_RASPBERRYPI_HWMON
228 This driver can also be built as a module. If so, the module
229 will be called raspberrypi-hwmon.
230
231 +config SENSORS_RPI_POE_FAN
232 + tristate "Raspberry Pi PoE HAT fan"
233 + depends on RASPBERRYPI_FIRMWARE
234 + depends on THERMAL || THERMAL=n
235 + help
236 + If you say yes here you get support for Raspberry Pi PoE (Power over
237 + Ethernet) HAT fan.
238 +
239 + This driver can also be built as a module. If so, the module
240 + will be called rpi-poe-fan.
241 +
242 config SENSORS_SHT15
243 tristate "Sensiron humidity and temperature sensors. SHT15 and compat."
244 depends on GPIOLIB || COMPILE_TEST
245 --- a/drivers/hwmon/Makefile
246 +++ b/drivers/hwmon/Makefile
247 @@ -144,6 +144,7 @@ obj-$(CONFIG_SENSORS_PCF8591) += pcf8591
248 obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o
249 obj-$(CONFIG_SENSORS_PWM_FAN) += pwm-fan.o
250 obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON) += raspberrypi-hwmon.o
251 +obj-$(CONFIG_SENSORS_RPI_POE_FAN) += rpi-poe-fan.o
252 obj-$(CONFIG_SENSORS_S3C) += s3c-hwmon.o
253 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
254 obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o
255 --- /dev/null
256 +++ b/drivers/hwmon/rpi-poe-fan.c
257 @@ -0,0 +1,436 @@
258 +// SPDX-License-Identifier: GPL-2.0
259 +/*
260 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi PoE HAT fan.
261 + *
262 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
263 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
264 + *
265 + * Author: Serge Schneider <serge@raspberrypi.org>
266 + */
267 +
268 +#include <linux/hwmon.h>
269 +#include <linux/hwmon-sysfs.h>
270 +#include <linux/module.h>
271 +#include <linux/mutex.h>
272 +#include <linux/notifier.h>
273 +#include <linux/of.h>
274 +#include <linux/platform_device.h>
275 +#include <linux/reboot.h>
276 +#include <linux/sysfs.h>
277 +#include <linux/thermal.h>
278 +#include <soc/bcm2835/raspberrypi-firmware.h>
279 +
280 +#define MAX_PWM 255
281 +
282 +#define POE_CUR_PWM 0x0
283 +#define POE_DEF_PWM 0x1
284 +
285 +struct rpi_poe_fan_ctx {
286 + struct mutex lock;
287 + struct rpi_firmware *fw;
288 + unsigned int pwm_value;
289 + unsigned int def_pwm_value;
290 + unsigned int rpi_poe_fan_state;
291 + unsigned int rpi_poe_fan_max_state;
292 + unsigned int *rpi_poe_fan_cooling_levels;
293 + struct thermal_cooling_device *cdev;
294 + struct notifier_block nb;
295 +};
296 +
297 +struct fw_tag_data_s{
298 + u32 reg;
299 + u32 val;
300 + u32 ret;
301 +};
302 +
303 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
304 + struct fw_tag_data_s fw_tag_data = {
305 + .reg = reg,
306 + .val = *val
307 + };
308 + int ret;
309 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
310 + &fw_tag_data, sizeof(fw_tag_data));
311 + if (ret) {
312 + return ret;
313 + } else if (fw_tag_data.ret) {
314 + return -EIO;
315 + }
316 + return 0;
317 +}
318 +
319 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
320 + struct fw_tag_data_s fw_tag_data = {
321 + .reg = reg,
322 + };
323 + int ret;
324 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
325 + &fw_tag_data, sizeof(fw_tag_data));
326 + if (ret) {
327 + return ret;
328 + } else if (fw_tag_data.ret) {
329 + return -EIO;
330 + }
331 + *val = fw_tag_data.val;
332 + return 0;
333 +}
334 +
335 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
336 + void *unused)
337 +{
338 + struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
339 + nb);
340 +
341 + if (ctx->pwm_value != ctx->def_pwm_value)
342 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
343 +
344 + return NOTIFY_DONE;
345 +}
346 +
347 +static int __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
348 +{
349 + int ret = 0;
350 +
351 + mutex_lock(&ctx->lock);
352 + if (ctx->pwm_value == pwm)
353 + goto exit_set_pwm_err;
354 +
355 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
356 + if (!ret)
357 + ctx->pwm_value = pwm;
358 +exit_set_pwm_err:
359 + mutex_unlock(&ctx->lock);
360 + return ret;
361 +}
362 +
363 +static int __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
364 +{
365 + int ret = 0;
366 + mutex_lock(&ctx->lock);
367 + if (ctx->def_pwm_value == def_pwm)
368 + goto exit_set_def_pwm_err;
369 +
370 + ret = write_reg(ctx->fw, POE_CUR_PWM, &def_pwm);
371 + if (!ret)
372 + ctx->def_pwm_value = def_pwm;
373 +exit_set_def_pwm_err:
374 + mutex_unlock(&ctx->lock);
375 + return ret;
376 +}
377 +
378 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
379 + unsigned long pwm)
380 +{
381 + int i;
382 +
383 + for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
384 + if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
385 + break;
386 +
387 + ctx->rpi_poe_fan_state = i;
388 +}
389 +
390 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
391 + const char *buf, size_t count)
392 +{
393 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
394 + unsigned long pwm;
395 + int ret;
396 +
397 + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
398 + return -EINVAL;
399 +
400 + ret = __set_pwm(ctx, pwm);
401 + if (ret)
402 + return ret;
403 +
404 + rpi_poe_fan_update_state(ctx, pwm);
405 + return count;
406 +}
407 +
408 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
409 + const char *buf, size_t count)
410 +{
411 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
412 + unsigned long def_pwm;
413 + int ret;
414 +
415 + if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
416 + return -EINVAL;
417 +
418 + ret = __set_def_pwm(ctx, def_pwm);
419 + if (ret)
420 + return ret;
421 + return count;
422 +}
423 +
424 +static ssize_t show_pwm(struct device *dev,
425 + struct device_attribute *attr, char *buf)
426 +{
427 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
428 +
429 + return sprintf(buf, "%u\n", ctx->pwm_value);
430 +}
431 +
432 +static ssize_t show_def_pwm(struct device *dev,
433 + struct device_attribute *attr, char *buf)
434 +{
435 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
436 +
437 + return sprintf(buf, "%u\n", ctx->def_pwm_value);
438 +}
439 +
440 +
441 +static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
442 +static SENSOR_DEVICE_ATTR(def_pwm1, 0644, show_def_pwm, set_def_pwm, 1);
443 +
444 +static struct attribute *rpi_poe_fan_attrs[] = {
445 + &sensor_dev_attr_pwm1.dev_attr.attr,
446 + &sensor_dev_attr_def_pwm1.dev_attr.attr,
447 + NULL,
448 +};
449 +
450 +ATTRIBUTE_GROUPS(rpi_poe_fan);
451 +
452 +/* thermal cooling device callbacks */
453 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
454 + unsigned long *state)
455 +{
456 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
457 +
458 + if (!ctx)
459 + return -EINVAL;
460 +
461 + *state = ctx->rpi_poe_fan_max_state;
462 +
463 + return 0;
464 +}
465 +
466 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
467 + unsigned long *state)
468 +{
469 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
470 +
471 + if (!ctx)
472 + return -EINVAL;
473 +
474 + *state = ctx->rpi_poe_fan_state;
475 +
476 + return 0;
477 +}
478 +
479 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
480 + unsigned long state)
481 +{
482 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
483 + int ret;
484 +
485 + if (!ctx || (state > ctx->rpi_poe_fan_max_state))
486 + return -EINVAL;
487 +
488 + if (state == ctx->rpi_poe_fan_state)
489 + return 0;
490 +
491 + ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
492 + if (ret) {
493 + dev_err(&cdev->device, "Cannot set pwm!\n");
494 + return ret;
495 + }
496 +
497 + ctx->rpi_poe_fan_state = state;
498 +
499 + return ret;
500 +}
501 +
502 +static const struct thermal_cooling_device_ops rpi_poe_fan_cooling_ops = {
503 + .get_max_state = rpi_poe_fan_get_max_state,
504 + .get_cur_state = rpi_poe_fan_get_cur_state,
505 + .set_cur_state = rpi_poe_fan_set_cur_state,
506 +};
507 +
508 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
509 + struct rpi_poe_fan_ctx *ctx)
510 +{
511 + struct device_node *np = dev->of_node;
512 + int num, i, ret;
513 +
514 + if (!of_find_property(np, "cooling-levels", NULL))
515 + return 0;
516 +
517 + ret = of_property_count_u32_elems(np, "cooling-levels");
518 + if (ret <= 0) {
519 + dev_err(dev, "cooling-levels property missing or invalid: %d\n",
520 + ret);
521 + return ret ? : -EINVAL;
522 + }
523 +
524 + num = ret;
525 + ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
526 + GFP_KERNEL);
527 + if (!ctx->rpi_poe_fan_cooling_levels)
528 + return -ENOMEM;
529 +
530 + ret = of_property_read_u32_array(np, "cooling-levels",
531 + ctx->rpi_poe_fan_cooling_levels, num);
532 + if (ret) {
533 + dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
534 + return ret;
535 + }
536 +
537 + for (i = 0; i < num; i++) {
538 + if (ctx->rpi_poe_fan_cooling_levels[i] > MAX_PWM) {
539 + dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
540 + ctx->rpi_poe_fan_cooling_levels[i], MAX_PWM);
541 + return -EINVAL;
542 + }
543 + }
544 +
545 + ctx->rpi_poe_fan_max_state = num - 1;
546 +
547 + return 0;
548 +}
549 +
550 +static int rpi_poe_fan_probe(struct platform_device *pdev)
551 +{
552 + struct thermal_cooling_device *cdev;
553 + struct rpi_poe_fan_ctx *ctx;
554 + struct device *hwmon;
555 + struct device_node *np = pdev->dev.of_node;
556 + struct device_node *fw_node;
557 + int ret;
558 +
559 + fw_node = of_parse_phandle(np, "firmware", 0);
560 + if (!fw_node) {
561 + dev_err(&pdev->dev, "Missing firmware node\n");
562 + return -ENOENT;
563 + }
564 +
565 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
566 + if (!ctx)
567 + return -ENOMEM;
568 +
569 + mutex_init(&ctx->lock);
570 +
571 + ctx->fw = rpi_firmware_get(fw_node);
572 + if (!ctx->fw)
573 + return -EPROBE_DEFER;
574 +
575 + platform_set_drvdata(pdev, ctx);
576 +
577 + ctx->nb.notifier_call = rpi_poe_reboot;
578 + ret = register_reboot_notifier(&ctx->nb);
579 + if (ret) {
580 + dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
581 + ret);
582 + return ret;
583 + }
584 + ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
585 + if (ret) {
586 + dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
587 + ret);
588 + goto err;
589 + }
590 + ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
591 + if (ret) {
592 + dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
593 + ret);
594 + goto err;
595 + }
596 +
597 + hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "rpipoefan",
598 + ctx, rpi_poe_fan_groups);
599 + if (IS_ERR(hwmon)) {
600 + dev_err(&pdev->dev, "Failed to register hwmon device\n");
601 + ret = PTR_ERR(hwmon);
602 + goto err;
603 + }
604 +
605 + ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
606 + if (ret)
607 + return ret;
608 +
609 + rpi_poe_fan_update_state(ctx, ctx->pwm_value);
610 + if (!IS_ENABLED(CONFIG_THERMAL))
611 + return 0;
612 +
613 + cdev = thermal_of_cooling_device_register(np,
614 + "rpi-poe-fan", ctx,
615 + &rpi_poe_fan_cooling_ops);
616 + if (IS_ERR(cdev)) {
617 + dev_err(&pdev->dev,
618 + "Failed to register rpi-poe-fan as cooling device");
619 + ret = PTR_ERR(cdev);
620 + goto err;
621 + }
622 + ctx->cdev = cdev;
623 + thermal_cdev_update(cdev);
624 +
625 + return 0;
626 +err:
627 + unregister_reboot_notifier(&ctx->nb);
628 + return ret;
629 +}
630 +
631 +static int rpi_poe_fan_remove(struct platform_device *pdev)
632 +{
633 + struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
634 + u32 value = ctx->def_pwm_value;
635 +
636 + unregister_reboot_notifier(&ctx->nb);
637 + thermal_cooling_device_unregister(ctx->cdev);
638 + if (ctx->pwm_value != value) {
639 + write_reg(ctx->fw, POE_CUR_PWM, &value);
640 + }
641 + return 0;
642 +}
643 +
644 +#ifdef CONFIG_PM_SLEEP
645 +static int rpi_poe_fan_suspend(struct device *dev)
646 +{
647 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
648 + u32 value = 0;
649 + int ret = 0;
650 +
651 + if (ctx->pwm_value != value)
652 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
653 + return ret;
654 +}
655 +
656 +static int rpi_poe_fan_resume(struct device *dev)
657 +{
658 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
659 + u32 value = ctx->pwm_value;
660 + int ret = 0;
661 +
662 + if (value != 0)
663 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
664 +
665 + return ret;
666 +}
667 +#endif
668 +
669 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
670 + rpi_poe_fan_resume);
671 +
672 +static const struct of_device_id of_rpi_poe_fan_match[] = {
673 + { .compatible = "raspberrypi,rpi-poe-fan", },
674 + {},
675 +};
676 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
677 +
678 +static struct platform_driver rpi_poe_fan_driver = {
679 + .probe = rpi_poe_fan_probe,
680 + .remove = rpi_poe_fan_remove,
681 + .driver = {
682 + .name = "rpi-poe-fan",
683 + .pm = &rpi_poe_fan_pm,
684 + .of_match_table = of_rpi_poe_fan_match,
685 + },
686 +};
687 +
688 +module_platform_driver(rpi_poe_fan_driver);
689 +
690 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
691 +MODULE_ALIAS("platform:rpi-poe-fan");
692 +MODULE_DESCRIPTION("Raspberry Pi PoE HAT fan driver");
693 +MODULE_LICENSE("GPL");
694 --- a/include/soc/bcm2835/raspberrypi-firmware.h
695 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
696 @@ -93,6 +93,8 @@ enum rpi_firmware_property_tag {
697 RPI_FIRMWARE_SET_GPIO_CONFIG = 0x00038043,
698 RPI_FIRMWARE_GET_PERIPH_REG = 0x00030045,
699 RPI_FIRMWARE_SET_PERIPH_REG = 0x00038045,
700 + RPI_FIRMWARE_GET_POE_HAT_VAL = 0x00030049,
701 + RPI_FIRMWARE_SET_POE_HAT_VAL = 0x00030050,
702
703
704 /* Dispmanx TAGS */