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
6 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
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
15 * Add vendor to of_device_id compatible string.
16 * Rename m_data_s struct to fw_data_s
21 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
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
39 +++ b/Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
41 +Bindings for the Raspberry Pi PoE HAT fan
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
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>;
62 + cpu_thermal: cpu-thermal {
64 + threshold: trip-point@0 {
65 + temperature = <45000>;
66 + hysteresis = <5000>;
69 + target: trip-point@1 {
70 + temperature = <50000>;
71 + hysteresis = <2000>;
74 + cpu_hot: cpu_hot@0 {
75 + temperature = <55000>;
76 + hysteresis = <2000>;
82 + trip = <&threshold>;
83 + cooling-device = <&fan0 0 1>;
87 + cooling-device = <&fan0 1 2>;
91 + cooling-device = <&fan0 2 3>;
97 +++ b/Documentation/hwmon/rpi-poe-fan
99 +Kernel driver rpi-poe-fan
100 +=====================
102 +This driver enables the use of the Raspberry Pi PoE HAT fan.
104 +Author: Serge Schneider <serge@raspberrypi.org>
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) += \
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);
131 +Info: Raspberry Pi POE HAT
132 +Load: dtoverlay=rpi-poe
137 Info: Configures the RPi Proto audio card
138 Load: dtoverlay=rpi-proto
140 +++ b/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts
143 + * Overlay for the Raspberry Pi POE HAT.
149 + compatible = "brcm,bcm2708";
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>;
167 + target = <&cpu_thermal>;
170 + threshold: trip-point@0 {
171 + temperature = <45000>;
172 + hysteresis = <5000>;
175 + target: trip-point@1 {
176 + temperature = <50000>;
177 + hysteresis = <2000>;
180 + cpu_hot: cpu_hot@0 {
181 + temperature = <55000>;
182 + hysteresis = <2000>;
188 + trip = <&threshold>;
189 + cooling-device = <&fan0 0 1>;
193 + cooling-device = <&fan0 1 2>;
197 + cooling-device = <&fan0 2 3>;
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.
209 +config SENSORS_RPI_POE_FAN
210 + tristate "Raspberry Pi PoE HAT fan"
211 + depends on RASPBERRYPI_FIRMWARE
212 + depends on THERMAL || THERMAL=n
214 + If you say yes here you get support for Raspberry Pi PoE (Power over
217 + This driver can also be built as a module. If so, the module
218 + will be called rpi-poe-fan.
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
234 +++ b/drivers/hwmon/rpi-poe-fan.c
236 +// SPDX-License-Identifier: GPL-2.0
238 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi PoE HAT fan.
240 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
241 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
243 + * Author: Serge Schneider <serge@raspberrypi.org>
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>
260 +#define POE_CUR_PWM 0x0
261 +#define POE_DEF_PWM 0x1
263 +struct rpi_poe_fan_ctx {
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;
275 +struct fw_tag_data_s{
281 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
282 + struct fw_tag_data_s fw_tag_data = {
287 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
288 + &fw_tag_data, sizeof(fw_tag_data));
291 + } else if (fw_tag_data.ret) {
297 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
298 + struct fw_tag_data_s fw_tag_data = {
302 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
303 + &fw_tag_data, sizeof(fw_tag_data));
306 + } else if (fw_tag_data.ret) {
309 + *val = fw_tag_data.val;
313 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
316 + struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
319 + if (ctx->pwm_value != ctx->def_pwm_value)
320 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
322 + return NOTIFY_DONE;
325 +static int __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
329 + mutex_lock(&ctx->lock);
330 + if (ctx->pwm_value == pwm)
331 + goto exit_set_pwm_err;
333 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
335 + ctx->pwm_value = pwm;
337 + mutex_unlock(&ctx->lock);
341 +static int __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
344 + mutex_lock(&ctx->lock);
345 + if (ctx->def_pwm_value == def_pwm)
346 + goto exit_set_def_pwm_err;
348 + ret = write_reg(ctx->fw, POE_CUR_PWM, &def_pwm);
350 + ctx->def_pwm_value = def_pwm;
351 +exit_set_def_pwm_err:
352 + mutex_unlock(&ctx->lock);
356 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
361 + for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
362 + if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
365 + ctx->rpi_poe_fan_state = i;
368 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
369 + const char *buf, size_t count)
371 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
375 + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
378 + ret = __set_pwm(ctx, pwm);
382 + rpi_poe_fan_update_state(ctx, pwm);
386 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
387 + const char *buf, size_t count)
389 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
390 + unsigned long def_pwm;
393 + if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
396 + ret = __set_def_pwm(ctx, def_pwm);
402 +static ssize_t show_pwm(struct device *dev,
403 + struct device_attribute *attr, char *buf)
405 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
407 + return sprintf(buf, "%u\n", ctx->pwm_value);
410 +static ssize_t show_def_pwm(struct device *dev,
411 + struct device_attribute *attr, char *buf)
413 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
415 + return sprintf(buf, "%u\n", ctx->def_pwm_value);
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);
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,
428 +ATTRIBUTE_GROUPS(rpi_poe_fan);
430 +/* thermal cooling device callbacks */
431 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
432 + unsigned long *state)
434 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
439 + *state = ctx->rpi_poe_fan_max_state;
444 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
445 + unsigned long *state)
447 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
452 + *state = ctx->rpi_poe_fan_state;
457 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
458 + unsigned long state)
460 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
463 + if (!ctx || (state > ctx->rpi_poe_fan_max_state))
466 + if (state == ctx->rpi_poe_fan_state)
469 + ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
471 + dev_err(&cdev->device, "Cannot set pwm!\n");
475 + ctx->rpi_poe_fan_state = state;
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,
486 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
487 + struct rpi_poe_fan_ctx *ctx)
489 + struct device_node *np = dev->of_node;
492 + if (!of_find_property(np, "cooling-levels", NULL))
495 + ret = of_property_count_u32_elems(np, "cooling-levels");
497 + dev_err(dev, "cooling-levels property missing or invalid: %d\n",
499 + return ret ? : -EINVAL;
503 + ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
505 + if (!ctx->rpi_poe_fan_cooling_levels)
508 + ret = of_property_read_u32_array(np, "cooling-levels",
509 + ctx->rpi_poe_fan_cooling_levels, num);
511 + dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
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);
523 + ctx->rpi_poe_fan_max_state = num - 1;
528 +static int rpi_poe_fan_probe(struct platform_device *pdev)
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;
537 + fw_node = of_parse_phandle(np, "firmware", 0);
539 + dev_err(&pdev->dev, "Missing firmware node\n");
543 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
547 + mutex_init(&ctx->lock);
549 + ctx->fw = rpi_firmware_get(fw_node);
551 + return -EPROBE_DEFER;
553 + platform_set_drvdata(pdev, ctx);
555 + ctx->nb.notifier_call = rpi_poe_reboot;
556 + ret = register_reboot_notifier(&ctx->nb);
558 + dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
562 + ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
564 + dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
568 + ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
570 + dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
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);
583 + ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
587 + rpi_poe_fan_update_state(ctx, ctx->pwm_value);
588 + if (!IS_ENABLED(CONFIG_THERMAL))
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);
601 + thermal_cdev_update(cdev);
605 + unregister_reboot_notifier(&ctx->nb);
609 +static int rpi_poe_fan_remove(struct platform_device *pdev)
611 + struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
612 + u32 value = ctx->def_pwm_value;
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);
622 +#ifdef CONFIG_PM_SLEEP
623 +static int rpi_poe_fan_suspend(struct device *dev)
625 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
629 + if (ctx->pwm_value != value)
630 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
634 +static int rpi_poe_fan_resume(struct device *dev)
636 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
637 + u32 value = ctx->pwm_value;
641 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
647 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
648 + rpi_poe_fan_resume);
650 +static const struct of_device_id of_rpi_poe_fan_match[] = {
651 + { .compatible = "raspberrypi,rpi-poe-fan", },
654 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
656 +static struct platform_driver rpi_poe_fan_driver = {
657 + .probe = rpi_poe_fan_probe,
658 + .remove = rpi_poe_fan_remove,
660 + .name = "rpi-poe-fan",
661 + .pm = &rpi_poe_fan_pm,
662 + .of_match_table = of_rpi_poe_fan_match,
666 +module_platform_driver(rpi_poe_fan_driver);
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,