1 From 2150bf813a64c18cdc56ae6b84036377c242cebd Mon Sep 17 00:00:00 2001
2 From: Serge Schneider <serge@raspberrypi.com>
3 Date: Mon, 2 Dec 2019 14:48:05 +0000
4 Subject: [PATCH] Add Raspberry Pi PoE+ HAT support
6 Signed-off-by: Serge Schneider <serge@raspberrypi.com>
8 drivers/hwmon/rpi-poe-fan.c | 35 +++-
9 drivers/power/supply/Kconfig | 6 +
10 drivers/power/supply/Makefile | 1 +
11 drivers/power/supply/rpi_poe_power.c | 227 +++++++++++++++++++++
12 include/soc/bcm2835/raspberrypi-firmware.h | 3 +-
13 5 files changed, 261 insertions(+), 11 deletions(-)
14 create mode 100644 drivers/power/supply/rpi_poe_power.c
16 --- a/drivers/hwmon/rpi-poe-fan.c
17 +++ b/drivers/hwmon/rpi-poe-fan.c
19 struct rpi_poe_fan_ctx {
21 struct rpi_firmware *fw;
23 unsigned int pwm_value;
24 unsigned int def_pwm_value;
25 unsigned int rpi_poe_fan_state;
26 @@ -43,13 +44,15 @@ struct fw_tag_data_s{
30 -static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
31 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val, u32 set_tag)
33 struct fw_tag_data_s fw_tag_data = {
38 - ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
40 + ret = rpi_firmware_property(fw, set_tag,
41 &fw_tag_data, sizeof(fw_tag_data));
44 @@ -82,7 +85,7 @@ static int rpi_poe_reboot(struct notifie
47 if (ctx->pwm_value != ctx->def_pwm_value)
48 - write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
49 + write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value, ctx->set_tag);
53 @@ -95,7 +98,7 @@ static int __set_pwm(struct rpi_poe_fan
54 if (ctx->pwm_value == pwm)
55 goto exit_set_pwm_err;
57 - ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
58 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm, ctx->set_tag);
62 @@ -110,7 +113,7 @@ static int __set_def_pwm(struct rpi_poe
63 if (ctx->def_pwm_value == def_pwm)
64 goto exit_set_def_pwm_err;
66 - ret = write_reg(ctx->fw, POE_DEF_PWM, &def_pwm);
67 + ret = write_reg(ctx->fw, POE_DEF_PWM, &def_pwm, ctx->set_tag);
69 ctx->def_pwm_value = def_pwm;
71 @@ -297,6 +300,7 @@ static int rpi_poe_fan_probe(struct plat
73 struct device_node *np = pdev->dev.of_node;
74 struct device_node *fw_node;
78 fw_node = of_parse_phandle(np, "firmware", 0);
79 @@ -314,6 +318,17 @@ static int rpi_poe_fan_probe(struct plat
80 ctx->fw = rpi_firmware_get(fw_node);
83 + ret = rpi_firmware_property(ctx->fw,
84 + RPI_FIRMWARE_GET_FIRMWARE_REVISION,
85 + &revision, sizeof(revision));
87 + dev_err(&pdev->dev, "Failed to get firmware revision: %i\n", ret);
90 + if (revision < 0x60af72e8)
91 + ctx->set_tag = RPI_FIRMWARE_SET_POE_HAT_VAL_OLD;
93 + ctx->set_tag = RPI_FIRMWARE_SET_POE_HAT_VAL;
95 platform_set_drvdata(pdev, ctx);
97 @@ -378,9 +393,9 @@ static int rpi_poe_fan_remove(struct pla
99 unregister_reboot_notifier(&ctx->nb);
100 thermal_cooling_device_unregister(ctx->cdev);
101 - if (ctx->pwm_value != value) {
102 - write_reg(ctx->fw, POE_CUR_PWM, &value);
104 + if (ctx->pwm_value != value)
105 + write_reg(ctx->fw, POE_CUR_PWM, &value, ctx->set_tag);
110 @@ -392,7 +407,7 @@ static int rpi_poe_fan_suspend(struct de
113 if (ctx->pwm_value != value)
114 - ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
115 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value, ctx->set_tag);
119 @@ -403,7 +418,7 @@ static int rpi_poe_fan_resume(struct dev
123 - ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
124 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value, ctx->set_tag);
128 --- a/drivers/power/supply/Kconfig
129 +++ b/drivers/power/supply/Kconfig
130 @@ -28,6 +28,12 @@ config POWER_SUPPLY_HWMON
131 Say 'Y' here if you want power supplies to
132 have hwmon sysfs interface too.
134 +config RPI_POE_POWER
135 + tristate "Raspberry Pi PoE+ HAT power supply driver"
136 + depends on RASPBERRYPI_FIRMWARE
138 + Say Y here to enable support for Raspberry Pi PoE+ (Power over Ethernet
139 + Plus) HAT current measurement.
142 tristate "Generic PDA/phone power driver"
143 --- a/drivers/power/supply/Makefile
144 +++ b/drivers/power/supply/Makefile
145 @@ -9,6 +9,7 @@ obj-$(CONFIG_POWER_SUPPLY) += power_supp
146 obj-$(CONFIG_POWER_SUPPLY_HWMON) += power_supply_hwmon.o
147 obj-$(CONFIG_GENERIC_ADC_BATTERY) += generic-adc-battery.o
149 +obj-$(CONFIG_RPI_POE_POWER) += rpi_poe_power.o
150 obj-$(CONFIG_PDA_POWER) += pda_power.o
151 obj-$(CONFIG_APM_POWER) += apm_power.o
152 obj-$(CONFIG_AXP20X_POWER) += axp20x_usb_power.o
154 +++ b/drivers/power/supply/rpi_poe_power.c
156 +// SPDX-License-Identifier: GPL-2.0
158 + * rpi-poe-power.c - Raspberry Pi PoE+ HAT power supply driver.
160 + * Copyright (C) 2019 Raspberry Pi (Trading) Ltd.
161 + * Based on axp20x_ac_power.c by Quentin Schulz <quentin.schulz@free-electrons.com>
163 + * Author: Serge Schneider <serge@raspberrypi.org>
166 +#include <linux/module.h>
167 +#include <linux/of.h>
168 +#include <linux/platform_device.h>
169 +#include <linux/power_supply.h>
170 +#include <soc/bcm2835/raspberrypi-firmware.h>
172 +#define RPI_POE_ADC_REG 0x2
173 +#define RPI_POE_FLAG_REG 0x4
175 +#define RPI_POE_FLAG_AT BIT(0)
176 +#define RPI_POE_FLAG_OC BIT(1)
178 +#define RPI_POE_CURRENT_AF_MAX (2500 * 1000)
179 +#define RPI_POE_CURRENT_AT_MAX (5000 * 1000)
181 +#define DRVNAME "rpi-poe-power-supply"
183 +struct rpi_poe_power_supply_ctx {
184 + struct power_supply *supply;
185 + struct rpi_firmware *fw;
188 +struct fw_tag_data_s {
194 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val)
196 + struct fw_tag_data_s fw_tag_data = {
202 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
203 + &fw_tag_data, sizeof(fw_tag_data));
206 + else if (fw_tag_data.ret)
211 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val)
213 + struct fw_tag_data_s fw_tag_data = {
219 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
220 + &fw_tag_data, sizeof(fw_tag_data));
223 + else if (fw_tag_data.ret)
226 + *val = fw_tag_data.val;
230 +static int rpi_poe_power_supply_get_property(struct power_supply *psy,
231 + enum power_supply_property psp,
232 + union power_supply_propval *r_val)
234 + struct rpi_poe_power_supply_ctx *ctx = power_supply_get_drvdata(psy);
236 + unsigned int val = 0;
239 + case POWER_SUPPLY_PROP_HEALTH:
240 + ret = read_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
244 + if (val & RPI_POE_FLAG_OC) {
245 + r_val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
246 + val = RPI_POE_FLAG_OC;
247 + ret = write_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
253 + r_val->intval = POWER_SUPPLY_HEALTH_GOOD;
256 + case POWER_SUPPLY_PROP_ONLINE:
257 + ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
261 + r_val->intval = (val > 5);
264 + case POWER_SUPPLY_PROP_CURRENT_AVG:
266 + ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
269 + val = (val * 3300)/9821;
270 + r_val->intval = val * 1000;
273 + case POWER_SUPPLY_PROP_CURRENT_NOW:
274 + ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
277 + val = (val * 3300)/9821;
278 + r_val->intval = val * 1000;
281 + case POWER_SUPPLY_PROP_CURRENT_MAX:
282 + ret = read_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
286 + if (val & RPI_POE_FLAG_AT) {
287 + r_val->intval = RPI_POE_CURRENT_AT_MAX;
290 + r_val->intval = RPI_POE_CURRENT_AF_MAX;
300 +static enum power_supply_property rpi_poe_power_supply_properties[] = {
301 + POWER_SUPPLY_PROP_HEALTH,
302 + POWER_SUPPLY_PROP_ONLINE,
303 + POWER_SUPPLY_PROP_CURRENT_AVG,
304 + POWER_SUPPLY_PROP_CURRENT_NOW,
305 + POWER_SUPPLY_PROP_CURRENT_MAX,
308 +static const struct power_supply_desc rpi_poe_power_supply_desc = {
310 + .type = POWER_SUPPLY_TYPE_MAINS,
311 + .properties = rpi_poe_power_supply_properties,
312 + .num_properties = ARRAY_SIZE(rpi_poe_power_supply_properties),
313 + .get_property = rpi_poe_power_supply_get_property,
316 +static int rpi_poe_power_supply_probe(struct platform_device *pdev)
318 + struct power_supply_config psy_cfg = {};
319 + struct rpi_poe_power_supply_ctx *ctx;
320 + struct device_node *fw_node;
323 + if (!of_device_is_available(pdev->dev.of_node))
326 + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
328 + dev_err(&pdev->dev, "Missing firmware node\n");
332 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
336 + ctx->fw = rpi_firmware_get(fw_node);
338 + return -EPROBE_DEFER;
339 + if (rpi_firmware_property(ctx->fw,
340 + RPI_FIRMWARE_GET_FIRMWARE_REVISION,
341 + &revision, sizeof(revision))) {
342 + dev_err(&pdev->dev, "Failed to get firmware revision\n");
345 + if (revision < 0x60af72e8) {
346 + dev_err(&pdev->dev, "Unsupported firmware\n");
349 + platform_set_drvdata(pdev, ctx);
351 + psy_cfg.of_node = pdev->dev.of_node;
352 + psy_cfg.drv_data = ctx;
354 + ctx->supply = devm_power_supply_register(&pdev->dev,
355 + &rpi_poe_power_supply_desc,
357 + if (IS_ERR(ctx->supply))
358 + return PTR_ERR(ctx->supply);
363 +static const struct of_device_id of_rpi_poe_power_supply_match[] = {
364 + { .compatible = "raspberrypi,rpi-poe-power-supply", },
367 +MODULE_DEVICE_TABLE(of, of_rpi_poe_power_supply_match);
369 +static struct platform_driver rpi_poe_power_supply_driver = {
370 + .probe = rpi_poe_power_supply_probe,
373 + .of_match_table = of_rpi_poe_power_supply_match
377 +module_platform_driver(rpi_poe_power_supply_driver);
379 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
380 +MODULE_ALIAS("platform:" DRVNAME);
381 +MODULE_DESCRIPTION("Raspberry Pi PoE+ HAT power supply driver");
382 +MODULE_LICENSE("GPL");
383 --- a/include/soc/bcm2835/raspberrypi-firmware.h
384 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
385 @@ -94,7 +94,8 @@ enum rpi_firmware_property_tag {
386 RPI_FIRMWARE_GET_PERIPH_REG = 0x00030045,
387 RPI_FIRMWARE_SET_PERIPH_REG = 0x00038045,
388 RPI_FIRMWARE_GET_POE_HAT_VAL = 0x00030049,
389 - RPI_FIRMWARE_SET_POE_HAT_VAL = 0x00030050,
390 + RPI_FIRMWARE_SET_POE_HAT_VAL = 0x00038049,
391 + RPI_FIRMWARE_SET_POE_HAT_VAL_OLD = 0x00030050,
392 RPI_FIRMWARE_NOTIFY_XHCI_RESET = 0x00030058,
393 RPI_FIRMWARE_GET_REBOOT_FLAGS = 0x00030064,
394 RPI_FIRMWARE_SET_REBOOT_FLAGS = 0x00038064,