1 From ae762f7cc705c20c8320613425a7179c3e6f47f3 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/703] 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 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
41 +++ b/Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
43 +Bindings for the Raspberry Pi PoE HAT fan
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
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>;
64 + cpu_thermal: cpu-thermal {
66 + threshold: trip-point@0 {
67 + temperature = <45000>;
68 + hysteresis = <5000>;
71 + target: trip-point@1 {
72 + temperature = <50000>;
73 + hysteresis = <2000>;
76 + cpu_hot: cpu_hot@0 {
77 + temperature = <55000>;
78 + hysteresis = <2000>;
84 + trip = <&threshold>;
85 + cooling-device = <&fan0 0 1>;
89 + cooling-device = <&fan0 1 2>;
93 + cooling-device = <&fan0 2 3>;
99 +++ b/Documentation/hwmon/rpi-poe-fan
101 +Kernel driver rpi-poe-fan
102 +=====================
104 +This driver enables the use of the Raspberry Pi PoE HAT fan.
106 +Author: Serge Schneider <serge@raspberrypi.org>
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) += \
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);
133 +Info: Raspberry Pi POE HAT
134 +Load: dtoverlay=rpi-poe
139 Info: Configures the RPi Proto audio card
140 Load: dtoverlay=rpi-proto
142 +++ b/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts
145 + * Overlay for the Raspberry Pi POE HAT.
151 + compatible = "brcm,bcm2708";
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>;
169 + target = <&cpu_thermal>;
172 + threshold: trip-point@0 {
173 + temperature = <45000>;
174 + hysteresis = <5000>;
177 + target: trip-point@1 {
178 + temperature = <50000>;
179 + hysteresis = <2000>;
182 + cpu_hot: cpu_hot@0 {
183 + temperature = <55000>;
184 + hysteresis = <2000>;
190 + trip = <&threshold>;
191 + cooling-device = <&fan0 0 1>;
195 + cooling-device = <&fan0 1 2>;
199 + cooling-device = <&fan0 2 3>;
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.
231 +config SENSORS_RPI_POE_FAN
232 + tristate "Raspberry Pi PoE HAT fan"
233 + depends on RASPBERRYPI_FIRMWARE
234 + depends on THERMAL || THERMAL=n
236 + If you say yes here you get support for Raspberry Pi PoE (Power over
239 + This driver can also be built as a module. If so, the module
240 + will be called rpi-poe-fan.
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
256 +++ b/drivers/hwmon/rpi-poe-fan.c
258 +// SPDX-License-Identifier: GPL-2.0
260 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi PoE HAT fan.
262 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
263 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
265 + * Author: Serge Schneider <serge@raspberrypi.org>
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>
282 +#define POE_CUR_PWM 0x0
283 +#define POE_DEF_PWM 0x1
285 +struct rpi_poe_fan_ctx {
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;
297 +struct fw_tag_data_s{
303 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
304 + struct fw_tag_data_s fw_tag_data = {
309 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
310 + &fw_tag_data, sizeof(fw_tag_data));
313 + } else if (fw_tag_data.ret) {
319 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
320 + struct fw_tag_data_s fw_tag_data = {
324 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
325 + &fw_tag_data, sizeof(fw_tag_data));
328 + } else if (fw_tag_data.ret) {
331 + *val = fw_tag_data.val;
335 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
338 + struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
341 + if (ctx->pwm_value != ctx->def_pwm_value)
342 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
344 + return NOTIFY_DONE;
347 +static int __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
351 + mutex_lock(&ctx->lock);
352 + if (ctx->pwm_value == pwm)
353 + goto exit_set_pwm_err;
355 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
357 + ctx->pwm_value = pwm;
359 + mutex_unlock(&ctx->lock);
363 +static int __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
366 + mutex_lock(&ctx->lock);
367 + if (ctx->def_pwm_value == def_pwm)
368 + goto exit_set_def_pwm_err;
370 + ret = write_reg(ctx->fw, POE_CUR_PWM, &def_pwm);
372 + ctx->def_pwm_value = def_pwm;
373 +exit_set_def_pwm_err:
374 + mutex_unlock(&ctx->lock);
378 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
383 + for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
384 + if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
387 + ctx->rpi_poe_fan_state = i;
390 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
391 + const char *buf, size_t count)
393 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
397 + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
400 + ret = __set_pwm(ctx, pwm);
404 + rpi_poe_fan_update_state(ctx, pwm);
408 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
409 + const char *buf, size_t count)
411 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
412 + unsigned long def_pwm;
415 + if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
418 + ret = __set_def_pwm(ctx, def_pwm);
424 +static ssize_t show_pwm(struct device *dev,
425 + struct device_attribute *attr, char *buf)
427 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
429 + return sprintf(buf, "%u\n", ctx->pwm_value);
432 +static ssize_t show_def_pwm(struct device *dev,
433 + struct device_attribute *attr, char *buf)
435 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
437 + return sprintf(buf, "%u\n", ctx->def_pwm_value);
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);
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,
450 +ATTRIBUTE_GROUPS(rpi_poe_fan);
452 +/* thermal cooling device callbacks */
453 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
454 + unsigned long *state)
456 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
461 + *state = ctx->rpi_poe_fan_max_state;
466 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
467 + unsigned long *state)
469 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
474 + *state = ctx->rpi_poe_fan_state;
479 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
480 + unsigned long state)
482 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
485 + if (!ctx || (state > ctx->rpi_poe_fan_max_state))
488 + if (state == ctx->rpi_poe_fan_state)
491 + ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
493 + dev_err(&cdev->device, "Cannot set pwm!\n");
497 + ctx->rpi_poe_fan_state = state;
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,
508 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
509 + struct rpi_poe_fan_ctx *ctx)
511 + struct device_node *np = dev->of_node;
514 + if (!of_find_property(np, "cooling-levels", NULL))
517 + ret = of_property_count_u32_elems(np, "cooling-levels");
519 + dev_err(dev, "cooling-levels property missing or invalid: %d\n",
521 + return ret ? : -EINVAL;
525 + ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
527 + if (!ctx->rpi_poe_fan_cooling_levels)
530 + ret = of_property_read_u32_array(np, "cooling-levels",
531 + ctx->rpi_poe_fan_cooling_levels, num);
533 + dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
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);
545 + ctx->rpi_poe_fan_max_state = num - 1;
550 +static int rpi_poe_fan_probe(struct platform_device *pdev)
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;
559 + fw_node = of_parse_phandle(np, "firmware", 0);
561 + dev_err(&pdev->dev, "Missing firmware node\n");
565 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
569 + mutex_init(&ctx->lock);
571 + ctx->fw = rpi_firmware_get(fw_node);
573 + return -EPROBE_DEFER;
575 + platform_set_drvdata(pdev, ctx);
577 + ctx->nb.notifier_call = rpi_poe_reboot;
578 + ret = register_reboot_notifier(&ctx->nb);
580 + dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
584 + ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
586 + dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
590 + ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
592 + dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
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);
605 + ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
609 + rpi_poe_fan_update_state(ctx, ctx->pwm_value);
610 + if (!IS_ENABLED(CONFIG_THERMAL))
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);
623 + thermal_cdev_update(cdev);
627 + unregister_reboot_notifier(&ctx->nb);
631 +static int rpi_poe_fan_remove(struct platform_device *pdev)
633 + struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
634 + u32 value = ctx->def_pwm_value;
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);
644 +#ifdef CONFIG_PM_SLEEP
645 +static int rpi_poe_fan_suspend(struct device *dev)
647 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
651 + if (ctx->pwm_value != value)
652 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
656 +static int rpi_poe_fan_resume(struct device *dev)
658 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
659 + u32 value = ctx->pwm_value;
663 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
669 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
670 + rpi_poe_fan_resume);
672 +static const struct of_device_id of_rpi_poe_fan_match[] = {
673 + { .compatible = "raspberrypi,rpi-poe-fan", },
676 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
678 +static struct platform_driver rpi_poe_fan_driver = {
679 + .probe = rpi_poe_fan_probe,
680 + .remove = rpi_poe_fan_remove,
682 + .name = "rpi-poe-fan",
683 + .pm = &rpi_poe_fan_pm,
684 + .of_match_table = of_rpi_poe_fan_match,
688 +module_platform_driver(rpi_poe_fan_driver);
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,