1 From 1f8ba449fabb0eda1d835138d2324f8bc251e8c5 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 365/454] Add rpi-poe-fan driver
6 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
8 arch/arm/boot/dts/overlays/Makefile | 1 +
9 arch/arm/boot/dts/overlays/README | 6 +
10 .../arm/boot/dts/overlays/rpi-poe-overlay.dts | 61 +++
11 arch/arm/configs/bcm2709_defconfig | 1 +
12 arch/arm/configs/bcmrpi_defconfig | 1 +
13 drivers/hwmon/Kconfig | 10 +
14 drivers/hwmon/Makefile | 1 +
15 drivers/hwmon/rpi-poe-fan.c | 443 ++++++++++++++++++
16 include/soc/bcm2835/raspberrypi-firmware.h | 2 +
17 9 files changed, 526 insertions(+)
18 create mode 100644 arch/arm/boot/dts/overlays/rpi-poe-overlay.dts
19 create mode 100644 drivers/hwmon/rpi-poe-fan.c
21 --- a/arch/arm/boot/dts/overlays/Makefile
22 +++ b/arch/arm/boot/dts/overlays/Makefile
23 @@ -103,6 +103,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
31 --- a/arch/arm/boot/dts/overlays/README
32 +++ b/arch/arm/boot/dts/overlays/README
33 @@ -1576,6 +1576,12 @@ Params: touchscreen-size-x Touchscr
34 touchscreen-swapped-x-y Swap X and Y cordinates (default 0);
38 +Info: Raspberry Pi POE HAT
39 +Load: dtoverlay=rpi-poe
44 Info: Configures the RPi Proto audio card
45 Load: dtoverlay=rpi-proto
47 +++ b/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts
50 + * Overlay for the Raspberry Pi POE HAT.
56 + compatible = "brcm,bcm2708";
61 + fan0: rpi-poe-fan@0 {
62 + compatible = "rpi-poe-fan";
63 + firmware = <&firmware>;
64 + cooling-min-state = <0>;
65 + cooling-max-state = <3>;
66 + #cooling-cells = <2>;
67 + cooling-levels = <0 50 150 255>;
74 + target = <&cpu_thermal>;
77 + threshold: trip-point@0 {
78 + temperature = <45000>;
79 + hysteresis = <5000>;
82 + target: trip-point@1 {
83 + temperature = <50000>;
84 + hysteresis = <2000>;
87 + cpu_hot: cpu_hot@0 {
88 + temperature = <55000>;
89 + hysteresis = <2000>;
95 + trip = <&threshold>;
96 + cooling-device = <&fan0 0 1>;
100 + cooling-device = <&fan0 1 2>;
104 + cooling-device = <&fan0 2 3>;
110 --- a/arch/arm/configs/bcm2709_defconfig
111 +++ b/arch/arm/configs/bcm2709_defconfig
112 @@ -658,6 +658,7 @@ CONFIG_HWMON=m
113 CONFIG_SENSORS_DS1621=m
114 CONFIG_SENSORS_JC42=m
115 CONFIG_SENSORS_LM75=m
116 +CONFIG_SENSORS_RPI_POE_FAN=m
117 CONFIG_SENSORS_SHT21=m
118 CONFIG_SENSORS_SHT3x=m
119 CONFIG_SENSORS_SHTC1=m
120 --- a/arch/arm/configs/bcmrpi_defconfig
121 +++ b/arch/arm/configs/bcmrpi_defconfig
122 @@ -651,6 +651,7 @@ CONFIG_HWMON=m
123 CONFIG_SENSORS_DS1621=m
124 CONFIG_SENSORS_JC42=m
125 CONFIG_SENSORS_LM75=m
126 +CONFIG_SENSORS_RPI_POE_FAN=m
127 CONFIG_SENSORS_SHT21=m
128 CONFIG_SENSORS_SHT3x=m
129 CONFIG_SENSORS_SHTC1=m
130 --- a/drivers/hwmon/Kconfig
131 +++ b/drivers/hwmon/Kconfig
132 @@ -1286,6 +1286,16 @@ config SENSORS_PWM_FAN
133 This driver can also be built as a module. If so, the module
134 will be called pwm-fan.
136 +config SENSORS_RPI_POE_FAN
137 + tristate "Raspberry Pi POE HAT fan"
138 + depends on RASPBERRYPI_FIRMWARE
139 + depends on THERMAL || THERMAL=n
141 + If you say yes here you get support for Raspberry Pi POE HAT fan.
143 + This driver can also be built as a module. If so, the module
144 + will be called rpi-poe-fan.
147 tristate "Sensiron humidity and temperature sensors. SHT15 and compat."
148 depends on GPIOLIB || COMPILE_TEST
149 --- a/drivers/hwmon/Makefile
150 +++ b/drivers/hwmon/Makefile
151 @@ -138,6 +138,7 @@ obj-$(CONFIG_SENSORS_PC87427) += pc87427
152 obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o
153 obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o
154 obj-$(CONFIG_SENSORS_PWM_FAN) += pwm-fan.o
155 +obj-$(CONFIG_SENSORS_RPI_POE_FAN) += rpi-poe-fan.o
156 obj-$(CONFIG_SENSORS_S3C) += s3c-hwmon.o
157 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
158 obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o
160 +++ b/drivers/hwmon/rpi-poe-fan.c
163 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi POE HAT fan.
165 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
166 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
168 + * Author: Serge Schneider <serge@raspberrypi.org>
170 + * This program is free software; you can redistribute it and/or modify
171 + * it under the terms of the GNU General Public License as published by
172 + * the Free Software Foundation; either version 2 of the License, or
173 + * (at your option) any later version.
175 + * This program is distributed in the hope that it will be useful,
176 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
177 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
178 + * GNU General Public License for more details.
181 +#include <linux/hwmon.h>
182 +#include <linux/hwmon-sysfs.h>
183 +#include <linux/module.h>
184 +#include <linux/mutex.h>
185 +#include <linux/notifier.h>
186 +#include <linux/of.h>
187 +#include <linux/platform_device.h>
188 +#include <linux/reboot.h>
189 +#include <linux/sysfs.h>
190 +#include <linux/thermal.h>
191 +#include <soc/bcm2835/raspberrypi-firmware.h>
195 +#define POE_CUR_PWM 0x0
196 +#define POE_DEF_PWM 0x1
198 +struct rpi_poe_fan_ctx {
200 + struct rpi_firmware *fw;
201 + unsigned int pwm_value;
202 + unsigned int def_pwm_value;
203 + unsigned int rpi_poe_fan_state;
204 + unsigned int rpi_poe_fan_max_state;
205 + unsigned int *rpi_poe_fan_cooling_levels;
206 + struct thermal_cooling_device *cdev;
207 + struct notifier_block nb;
216 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
217 + struct m_data_s m_data = {
222 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
223 + &m_data, sizeof(m_data));
226 + } else if (m_data.ret) {
232 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
233 + struct m_data_s m_data = {
237 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
238 + &m_data, sizeof(m_data));
241 + } else if (m_data.ret) {
248 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
251 + struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
254 + if (ctx->pwm_value != ctx->def_pwm_value)
255 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
257 + return NOTIFY_DONE;
260 +static int __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
264 + mutex_lock(&ctx->lock);
265 + if (ctx->pwm_value == pwm)
266 + goto exit_set_pwm_err;
268 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
270 + ctx->pwm_value = pwm;
272 + mutex_unlock(&ctx->lock);
276 +static int __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
279 + mutex_lock(&ctx->lock);
280 + if (ctx->def_pwm_value == def_pwm)
281 + goto exit_set_def_pwm_err;
283 + ret = write_reg(ctx->fw, POE_CUR_PWM, &def_pwm);
285 + ctx->def_pwm_value = def_pwm;
286 +exit_set_def_pwm_err:
287 + mutex_unlock(&ctx->lock);
291 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
296 + for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
297 + if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
300 + ctx->rpi_poe_fan_state = i;
303 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
304 + const char *buf, size_t count)
306 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
310 + if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
313 + ret = __set_pwm(ctx, pwm);
317 + rpi_poe_fan_update_state(ctx, pwm);
321 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
322 + const char *buf, size_t count)
324 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
325 + unsigned long def_pwm;
328 + if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
331 + ret = __set_def_pwm(ctx, def_pwm);
337 +static ssize_t show_pwm(struct device *dev,
338 + struct device_attribute *attr, char *buf)
340 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
342 + return sprintf(buf, "%u\n", ctx->pwm_value);
345 +static ssize_t show_def_pwm(struct device *dev,
346 + struct device_attribute *attr, char *buf)
348 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
350 + return sprintf(buf, "%u\n", ctx->def_pwm_value);
354 +static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
355 +static SENSOR_DEVICE_ATTR(def_pwm1, 0644, show_def_pwm, set_def_pwm, 1);
357 +static struct attribute *rpi_poe_fan_attrs[] = {
358 + &sensor_dev_attr_pwm1.dev_attr.attr,
359 + &sensor_dev_attr_def_pwm1.dev_attr.attr,
363 +ATTRIBUTE_GROUPS(rpi_poe_fan);
365 +/* thermal cooling device callbacks */
366 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
367 + unsigned long *state)
369 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
374 + *state = ctx->rpi_poe_fan_max_state;
379 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
380 + unsigned long *state)
382 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
387 + *state = ctx->rpi_poe_fan_state;
392 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
393 + unsigned long state)
395 + struct rpi_poe_fan_ctx *ctx = cdev->devdata;
398 + if (!ctx || (state > ctx->rpi_poe_fan_max_state))
401 + if (state == ctx->rpi_poe_fan_state)
404 + ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
406 + dev_err(&cdev->device, "Cannot set pwm!\n");
410 + ctx->rpi_poe_fan_state = state;
415 +static const struct thermal_cooling_device_ops rpi_poe_fan_cooling_ops = {
416 + .get_max_state = rpi_poe_fan_get_max_state,
417 + .get_cur_state = rpi_poe_fan_get_cur_state,
418 + .set_cur_state = rpi_poe_fan_set_cur_state,
421 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
422 + struct rpi_poe_fan_ctx *ctx)
424 + struct device_node *np = dev->of_node;
427 + if (!of_find_property(np, "cooling-levels", NULL))
430 + ret = of_property_count_u32_elems(np, "cooling-levels");
432 + dev_err(dev, "Wrong data!\n");
433 + return ret ? : -EINVAL;
437 + ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
439 + if (!ctx->rpi_poe_fan_cooling_levels)
442 + ret = of_property_read_u32_array(np, "cooling-levels",
443 + ctx->rpi_poe_fan_cooling_levels, num);
445 + dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
449 + for (i = 0; i < num; i++) {
450 + if (ctx->rpi_poe_fan_cooling_levels[i] > MAX_PWM) {
451 + dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
452 + ctx->rpi_poe_fan_cooling_levels[i], MAX_PWM);
457 + ctx->rpi_poe_fan_max_state = num - 1;
462 +static int rpi_poe_fan_probe(struct platform_device *pdev)
464 + struct thermal_cooling_device *cdev;
465 + struct rpi_poe_fan_ctx *ctx;
466 + struct device *hwmon;
467 + struct device_node *np = pdev->dev.of_node;
468 + struct device_node *fw_node;
471 + fw_node = of_parse_phandle(np, "firmware", 0);
473 + dev_err(&pdev->dev, "Missing firmware node\n");
477 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
481 + mutex_init(&ctx->lock);
483 + ctx->fw = rpi_firmware_get(fw_node);
485 + return -EPROBE_DEFER;
487 + platform_set_drvdata(pdev, ctx);
489 + ctx->nb.notifier_call = rpi_poe_reboot;
490 + ret = register_reboot_notifier(&ctx->nb);
492 + dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
496 + ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
498 + dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
502 + ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
504 + dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
509 + hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "rpipoefan",
510 + ctx, rpi_poe_fan_groups);
511 + if (IS_ERR(hwmon)) {
512 + dev_err(&pdev->dev, "Failed to register hwmon device\n");
513 + ret = PTR_ERR(hwmon);
517 + ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
521 + rpi_poe_fan_update_state(ctx, ctx->pwm_value);
522 + if (!IS_ENABLED(CONFIG_THERMAL))
525 + cdev = thermal_of_cooling_device_register(np,
526 + "rpi-poe-fan", ctx,
527 + &rpi_poe_fan_cooling_ops);
528 + if (IS_ERR(cdev)) {
529 + dev_err(&pdev->dev,
530 + "Failed to register rpi-poe-fan as cooling device");
531 + ret = PTR_ERR(cdev);
535 + thermal_cdev_update(cdev);
539 + unregister_reboot_notifier(&ctx->nb);
543 +static int rpi_poe_fan_remove(struct platform_device *pdev)
545 + struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
546 + u32 value = ctx->def_pwm_value;
548 + unregister_reboot_notifier(&ctx->nb);
549 + thermal_cooling_device_unregister(ctx->cdev);
550 + if (ctx->pwm_value != value) {
551 + write_reg(ctx->fw, POE_CUR_PWM, &value);
556 +#ifdef CONFIG_PM_SLEEP
557 +static int rpi_poe_fan_suspend(struct device *dev)
559 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
562 + if (ctx->pwm_value != value)
563 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
567 +static int rpi_poe_fan_resume(struct device *dev)
569 + struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
570 + u32 value = ctx->pwm_value;
574 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
580 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
581 + rpi_poe_fan_resume);
583 +static const struct of_device_id of_rpi_poe_fan_match[] = {
584 + { .compatible = "rpi-poe-fan", },
587 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
589 +static struct platform_driver rpi_poe_fan_driver = {
590 + .probe = rpi_poe_fan_probe,
591 + .remove = rpi_poe_fan_remove,
593 + .name = "rpi-poe-fan",
594 + .pm = &rpi_poe_fan_pm,
595 + .of_match_table = of_rpi_poe_fan_match,
599 +module_platform_driver(rpi_poe_fan_driver);
601 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
602 +MODULE_ALIAS("platform:rpi-poe-fan");
603 +MODULE_DESCRIPTION("Raspberry Pi POE HAT fan driver");
604 +MODULE_LICENSE("GPL");
605 --- a/include/soc/bcm2835/raspberrypi-firmware.h
606 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
607 @@ -93,6 +93,8 @@ enum rpi_firmware_property_tag {
608 RPI_FIRMWARE_SET_GPIO_CONFIG = 0x00038043,
609 RPI_FIRMWARE_GET_PERIPH_REG = 0x00030045,
610 RPI_FIRMWARE_SET_PERIPH_REG = 0x00038045,
611 + RPI_FIRMWARE_GET_POE_HAT_VAL = 0x00030049,
612 + RPI_FIRMWARE_SET_POE_HAT_VAL = 0x00030050,