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
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 rpi-poe-fan: fix def_pwm1 writes
25 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
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
38 +++ b/Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
40 +Bindings for the Raspberry Pi PoE HAT fan
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
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>;
61 + cpu_thermal: cpu-thermal {
63 + threshold: trip-point@0 {
64 + temperature = <45000>;
65 + hysteresis = <5000>;
68 + target: trip-point@1 {
69 + temperature = <50000>;
70 + hysteresis = <2000>;
73 + cpu_hot: cpu_hot@0 {
74 + temperature = <55000>;
75 + hysteresis = <2000>;
81 + trip = <&threshold>;
82 + cooling-device = <&fan0 0 1>;
86 + cooling-device = <&fan0 1 2>;
90 + cooling-device = <&fan0 2 3>;
96 +++ b/Documentation/hwmon/rpi-poe-fan
98 +Kernel driver rpi-poe-fan
99 +=====================
101 +This driver enables the use of the Raspberry Pi PoE HAT fan.
103 +Author: Serge Schneider <serge@raspberrypi.org>
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.
119 +config SENSORS_RPI_POE_FAN
120 + tristate "Raspberry Pi PoE HAT fan"
121 + depends on RASPBERRYPI_FIRMWARE
122 + depends on THERMAL || THERMAL=n
124 + If you say yes here you get support for Raspberry Pi PoE (Power over
127 + This driver can also be built as a module. If so, the module
128 + will be called rpi-poe-fan.
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
144 +++ b/drivers/hwmon/rpi-poe-fan.c
146 +// SPDX-License-Identifier: GPL-2.0
148 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi PoE HAT fan.
150 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
151 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
153 + * Author: Serge Schneider <serge@raspberrypi.org>
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>
170 +#define POE_CUR_PWM 0x0
171 +#define POE_DEF_PWM 0x1
173 +struct rpi_poe_fan_ctx {
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;
185 +struct fw_tag_data_s{
191 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
192 + struct fw_tag_data_s fw_tag_data = {
197 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
198 + &fw_tag_data, sizeof(fw_tag_data));
201 + } else if (fw_tag_data.ret) {
207 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
208 + struct fw_tag_data_s fw_tag_data = {
212 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
213 + &fw_tag_data, sizeof(fw_tag_data));
216 + } else if (fw_tag_data.ret) {
219 + *val = fw_tag_data.val;
223 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
226 + struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
229 + if (ctx->pwm_value != ctx->def_pwm_value)
230 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
232 + return NOTIFY_DONE;
235 +static int __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
239 + mutex_lock(&ctx->lock);
240 + if (ctx->pwm_value == pwm)
241 + goto exit_set_pwm_err;
243 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
245 + ctx->pwm_value = pwm;
247 + mutex_unlock(&ctx->lock);
251 +static int __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
254 + mutex_lock(&ctx->lock);
255 + if (ctx->def_pwm_value == def_pwm)
256 + goto exit_set_def_pwm_err;
258 + ret = write_reg(ctx->fw, POE_DEF_PWM, &def_pwm);
260 + ctx->def_pwm_value = def_pwm;
261 +exit_set_def_pwm_err:
262 + mutex_unlock(&ctx->lock);
266 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
271 + for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
272 + if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
275 + ctx->rpi_poe_fan_state = i;
278 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
279 + const char *buf, size_t count)
281 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
285 + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
288 + ret = __set_pwm(ctx, pwm);
292 + rpi_poe_fan_update_state(ctx, pwm);
296 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
297 + const char *buf, size_t count)
299 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
300 + unsigned long def_pwm;
303 + if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
306 + ret = __set_def_pwm(ctx, def_pwm);
312 +static ssize_t show_pwm(struct device *dev,
313 + struct device_attribute *attr, char *buf)
315 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
317 + return sprintf(buf, "%u\n", ctx->pwm_value);
320 +static ssize_t show_def_pwm(struct device *dev,
321 + struct device_attribute *attr, char *buf)
323 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
325 + return sprintf(buf, "%u\n", ctx->def_pwm_value);
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);
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,
338 +ATTRIBUTE_GROUPS(rpi_poe_fan);
340 +/* thermal cooling device callbacks */
341 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
342 + unsigned long *state)
344 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
349 + *state = ctx->rpi_poe_fan_max_state;
354 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
355 + unsigned long *state)
357 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
362 + *state = ctx->rpi_poe_fan_state;
367 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
368 + unsigned long state)
370 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
373 + if (!ctx || (state > ctx->rpi_poe_fan_max_state))
376 + if (state == ctx->rpi_poe_fan_state)
379 + ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
381 + dev_err(&cdev->device, "Cannot set pwm!\n");
385 + ctx->rpi_poe_fan_state = state;
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,
396 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
397 + struct rpi_poe_fan_ctx *ctx)
399 + struct device_node *np = dev->of_node;
402 + if (!of_find_property(np, "cooling-levels", NULL))
405 + ret = of_property_count_u32_elems(np, "cooling-levels");
407 + dev_err(dev, "cooling-levels property missing or invalid: %d\n",
409 + return ret ? : -EINVAL;
413 + ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
415 + if (!ctx->rpi_poe_fan_cooling_levels)
418 + ret = of_property_read_u32_array(np, "cooling-levels",
419 + ctx->rpi_poe_fan_cooling_levels, num);
421 + dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
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);
433 + ctx->rpi_poe_fan_max_state = num - 1;
438 +static int rpi_poe_fan_probe(struct platform_device *pdev)
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;
447 + fw_node = of_parse_phandle(np, "firmware", 0);
449 + dev_err(&pdev->dev, "Missing firmware node\n");
453 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
457 + mutex_init(&ctx->lock);
459 + ctx->fw = rpi_firmware_get(fw_node);
461 + return -EPROBE_DEFER;
463 + platform_set_drvdata(pdev, ctx);
465 + ctx->nb.notifier_call = rpi_poe_reboot;
466 + ret = register_reboot_notifier(&ctx->nb);
468 + dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
472 + ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
474 + dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
478 + ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
480 + dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
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);
493 + ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
497 + rpi_poe_fan_update_state(ctx, ctx->pwm_value);
498 + if (!IS_ENABLED(CONFIG_THERMAL))
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);
511 + thermal_cdev_update(cdev);
515 + unregister_reboot_notifier(&ctx->nb);
519 +static int rpi_poe_fan_remove(struct platform_device *pdev)
521 + struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
522 + u32 value = ctx->def_pwm_value;
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);
532 +#ifdef CONFIG_PM_SLEEP
533 +static int rpi_poe_fan_suspend(struct device *dev)
535 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
539 + if (ctx->pwm_value != value)
540 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
544 +static int rpi_poe_fan_resume(struct device *dev)
546 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
547 + u32 value = ctx->pwm_value;
551 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
557 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
558 + rpi_poe_fan_resume);
560 +static const struct of_device_id of_rpi_poe_fan_match[] = {
561 + { .compatible = "raspberrypi,rpi-poe-fan", },
564 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
566 +static struct platform_driver rpi_poe_fan_driver = {
567 + .probe = rpi_poe_fan_probe,
568 + .remove = rpi_poe_fan_remove,
570 + .name = "rpi-poe-fan",
571 + .pm = &rpi_poe_fan_pm,
572 + .of_match_table = of_rpi_poe_fan_match,
576 +module_platform_driver(rpi_poe_fan_driver);
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");