bcm27xx: add kernel 5.10 support
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.10 / 950-0622-Add-Raspberry-Pi-PoE-HAT-support.patch
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
5
6 Signed-off-by: Serge Schneider <serge@raspberrypi.com>
7 ---
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
15
16 --- a/drivers/hwmon/rpi-poe-fan.c
17 +++ b/drivers/hwmon/rpi-poe-fan.c
18 @@ -28,6 +28,7 @@
19 struct rpi_poe_fan_ctx {
20 struct mutex lock;
21 struct rpi_firmware *fw;
22 + u32 set_tag;
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{
27 u32 ret;
28 };
29
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)
32 +{
33 struct fw_tag_data_s fw_tag_data = {
34 .reg = reg,
35 .val = *val
36 };
37 int ret;
38 - ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
39 +
40 + ret = rpi_firmware_property(fw, set_tag,
41 &fw_tag_data, sizeof(fw_tag_data));
42 if (ret) {
43 return ret;
44 @@ -82,7 +85,7 @@ static int rpi_poe_reboot(struct notifie
45 nb);
46
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);
50
51 return NOTIFY_DONE;
52 }
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;
56
57 - ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
58 + ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm, ctx->set_tag);
59 if (!ret)
60 ctx->pwm_value = pwm;
61 exit_set_pwm_err:
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;
65
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);
68 if (!ret)
69 ctx->def_pwm_value = def_pwm;
70 exit_set_def_pwm_err:
71 @@ -297,6 +300,7 @@ static int rpi_poe_fan_probe(struct plat
72 struct device *hwmon;
73 struct device_node *np = pdev->dev.of_node;
74 struct device_node *fw_node;
75 + u32 revision;
76 int ret;
77
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);
81 if (!ctx->fw)
82 return -EPROBE_DEFER;
83 + ret = rpi_firmware_property(ctx->fw,
84 + RPI_FIRMWARE_GET_FIRMWARE_REVISION,
85 + &revision, sizeof(revision));
86 + if (ret) {
87 + dev_err(&pdev->dev, "Failed to get firmware revision: %i\n", ret);
88 + return ret;
89 + }
90 + if (revision < 0x60af72e8)
91 + ctx->set_tag = RPI_FIRMWARE_SET_POE_HAT_VAL_OLD;
92 + else
93 + ctx->set_tag = RPI_FIRMWARE_SET_POE_HAT_VAL;
94
95 platform_set_drvdata(pdev, ctx);
96
97 @@ -378,9 +393,9 @@ static int rpi_poe_fan_remove(struct pla
98
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);
103 - }
104 + if (ctx->pwm_value != value)
105 + write_reg(ctx->fw, POE_CUR_PWM, &value, ctx->set_tag);
106 +
107 return 0;
108 }
109
110 @@ -392,7 +407,7 @@ static int rpi_poe_fan_suspend(struct de
111 int ret = 0;
112
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);
116 return ret;
117 }
118
119 @@ -403,7 +418,7 @@ static int rpi_poe_fan_resume(struct dev
120 int ret = 0;
121
122 if (value != 0)
123 - ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
124 + ret = write_reg(ctx->fw, POE_CUR_PWM, &value, ctx->set_tag);
125
126 return ret;
127 }
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.
133
134 +config RPI_POE_POWER
135 + tristate "Raspberry Pi PoE+ HAT power supply driver"
136 + depends on RASPBERRYPI_FIRMWARE
137 + help
138 + Say Y here to enable support for Raspberry Pi PoE+ (Power over Ethernet
139 + Plus) HAT current measurement.
140
141 config PDA_POWER
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
148
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
153 --- /dev/null
154 +++ b/drivers/power/supply/rpi_poe_power.c
155 @@ -0,0 +1,227 @@
156 +// SPDX-License-Identifier: GPL-2.0
157 +/*
158 + * rpi-poe-power.c - Raspberry Pi PoE+ HAT power supply driver.
159 + *
160 + * Copyright (C) 2019 Raspberry Pi (Trading) Ltd.
161 + * Based on axp20x_ac_power.c by Quentin Schulz <quentin.schulz@free-electrons.com>
162 + *
163 + * Author: Serge Schneider <serge@raspberrypi.org>
164 + */
165 +
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>
171 +
172 +#define RPI_POE_ADC_REG 0x2
173 +#define RPI_POE_FLAG_REG 0x4
174 +
175 +#define RPI_POE_FLAG_AT BIT(0)
176 +#define RPI_POE_FLAG_OC BIT(1)
177 +
178 +#define RPI_POE_CURRENT_AF_MAX (2500 * 1000)
179 +#define RPI_POE_CURRENT_AT_MAX (5000 * 1000)
180 +
181 +#define DRVNAME "rpi-poe-power-supply"
182 +
183 +struct rpi_poe_power_supply_ctx {
184 + struct power_supply *supply;
185 + struct rpi_firmware *fw;
186 +};
187 +
188 +struct fw_tag_data_s {
189 + u32 reg;
190 + u32 val;
191 + u32 ret;
192 +};
193 +
194 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val)
195 +{
196 + struct fw_tag_data_s fw_tag_data = {
197 + .reg = reg,
198 + .val = *val
199 + };
200 + int ret;
201 +
202 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
203 + &fw_tag_data, sizeof(fw_tag_data));
204 + if (ret)
205 + return ret;
206 + else if (fw_tag_data.ret)
207 + return -EIO;
208 + return 0;
209 +}
210 +
211 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val)
212 +{
213 + struct fw_tag_data_s fw_tag_data = {
214 + .reg = reg,
215 + .val = *val
216 + };
217 + int ret;
218 +
219 + ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
220 + &fw_tag_data, sizeof(fw_tag_data));
221 + if (ret)
222 + return ret;
223 + else if (fw_tag_data.ret)
224 + return -EIO;
225 +
226 + *val = fw_tag_data.val;
227 + return 0;
228 +}
229 +
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)
233 +{
234 + struct rpi_poe_power_supply_ctx *ctx = power_supply_get_drvdata(psy);
235 + int ret;
236 + unsigned int val = 0;
237 +
238 + switch (psp) {
239 + case POWER_SUPPLY_PROP_HEALTH:
240 + ret = read_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
241 + if (ret)
242 + return ret;
243 +
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);
248 + if (ret)
249 + return ret;
250 + return 0;
251 + }
252 +
253 + r_val->intval = POWER_SUPPLY_HEALTH_GOOD;
254 + return 0;
255 +
256 + case POWER_SUPPLY_PROP_ONLINE:
257 + ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
258 + if (ret)
259 + return ret;
260 +
261 + r_val->intval = (val > 5);
262 + return 0;
263 +
264 + case POWER_SUPPLY_PROP_CURRENT_AVG:
265 + val = 50;
266 + ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
267 + if (ret)
268 + return ret;
269 + val = (val * 3300)/9821;
270 + r_val->intval = val * 1000;
271 + return 0;
272 +
273 + case POWER_SUPPLY_PROP_CURRENT_NOW:
274 + ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
275 + if (ret)
276 + return ret;
277 + val = (val * 3300)/9821;
278 + r_val->intval = val * 1000;
279 + return 0;
280 +
281 + case POWER_SUPPLY_PROP_CURRENT_MAX:
282 + ret = read_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
283 + if (ret)
284 + return ret;
285 +
286 + if (val & RPI_POE_FLAG_AT) {
287 + r_val->intval = RPI_POE_CURRENT_AT_MAX;
288 + return 0;
289 + }
290 + r_val->intval = RPI_POE_CURRENT_AF_MAX;
291 + return 0;
292 +
293 + default:
294 + return -EINVAL;
295 + }
296 +
297 + return -EINVAL;
298 +}
299 +
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,
306 +};
307 +
308 +static const struct power_supply_desc rpi_poe_power_supply_desc = {
309 + .name = "rpi-poe",
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,
314 +};
315 +
316 +static int rpi_poe_power_supply_probe(struct platform_device *pdev)
317 +{
318 + struct power_supply_config psy_cfg = {};
319 + struct rpi_poe_power_supply_ctx *ctx;
320 + struct device_node *fw_node;
321 + u32 revision;
322 +
323 + if (!of_device_is_available(pdev->dev.of_node))
324 + return -ENODEV;
325 +
326 + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
327 + if (!fw_node) {
328 + dev_err(&pdev->dev, "Missing firmware node\n");
329 + return -ENOENT;
330 + }
331 +
332 + ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
333 + if (!ctx)
334 + return -ENOMEM;
335 +
336 + ctx->fw = rpi_firmware_get(fw_node);
337 + if (!ctx->fw)
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");
343 + return -ENOENT;
344 + }
345 + if (revision < 0x60af72e8) {
346 + dev_err(&pdev->dev, "Unsupported firmware\n");
347 + return -ENOENT;
348 + }
349 + platform_set_drvdata(pdev, ctx);
350 +
351 + psy_cfg.of_node = pdev->dev.of_node;
352 + psy_cfg.drv_data = ctx;
353 +
354 + ctx->supply = devm_power_supply_register(&pdev->dev,
355 + &rpi_poe_power_supply_desc,
356 + &psy_cfg);
357 + if (IS_ERR(ctx->supply))
358 + return PTR_ERR(ctx->supply);
359 +
360 + return 0;
361 +}
362 +
363 +static const struct of_device_id of_rpi_poe_power_supply_match[] = {
364 + { .compatible = "raspberrypi,rpi-poe-power-supply", },
365 + { /* sentinel */ }
366 +};
367 +MODULE_DEVICE_TABLE(of, of_rpi_poe_power_supply_match);
368 +
369 +static struct platform_driver rpi_poe_power_supply_driver = {
370 + .probe = rpi_poe_power_supply_probe,
371 + .driver = {
372 + .name = DRVNAME,
373 + .of_match_table = of_rpi_poe_power_supply_match
374 + },
375 +};
376 +
377 +module_platform_driver(rpi_poe_power_supply_driver);
378 +
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,