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