aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/bcm27xx/patches-5.15/950-0660-power-rpi-poe-Add-option-of-being-created-by-MFD-or-.patch
blob: e402f0d639e176c9a2aafb11b908676223119ad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
From 2b9f5f00b22766743607b70d361875a9a1b0e1ed Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Thu, 20 Jan 2022 15:50:27 +0000
Subject: [PATCH] power: rpi-poe: Add option of being created by MFD or
 FW

The firmware can only use I2C0 if the kernel isn't, therefore
with libcamera and DRM using it the PoE HAT fan control needs
to move to the kernel.

Add the option for the driver to be created by the PoE HAT core
MFD driver, and use the I2C regmap that provides to control fan
functions.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 drivers/power/supply/rpi_poe_power.c | 124 ++++++++++++++++-----------
 1 file changed, 75 insertions(+), 49 deletions(-)

--- a/drivers/power/supply/rpi_poe_power.c
+++ b/drivers/power/supply/rpi_poe_power.c
@@ -12,10 +12,13 @@
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
+#include <linux/regmap.h>
 #include <soc/bcm2835/raspberrypi-firmware.h>
 
-#define RPI_POE_ADC_REG			0x2
-#define RPI_POE_FLAG_REG		0x4
+#define RPI_POE_FW_BASE_REG		0x2
+
+#define RPI_POE_ADC_REG			0x0
+#define RPI_POE_FLAG_REG		0x2
 
 #define RPI_POE_FLAG_AT			BIT(0)
 #define RPI_POE_FLAG_OC			BIT(1)
@@ -26,8 +29,12 @@
 #define DRVNAME "rpi-poe-power-supply"
 
 struct rpi_poe_power_supply_ctx {
-	struct power_supply *supply;
 	struct rpi_firmware *fw;
+
+	struct regmap *regmap;
+	u32 offset;
+
+	struct power_supply *supply;
 };
 
 struct fw_tag_data_s {
@@ -36,40 +43,51 @@ struct fw_tag_data_s {
 	u32 ret;
 };
 
-static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val)
+static int write_reg(struct rpi_poe_power_supply_ctx *ctx, u32 reg, u32 *val)
 {
 	struct fw_tag_data_s fw_tag_data = {
-		.reg = reg,
+		.reg = reg + RPI_POE_FW_BASE_REG,
 		.val = *val
 	};
 	int ret;
 
-	ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
-				    &fw_tag_data, sizeof(fw_tag_data));
-	if (ret)
-		return ret;
-	else if (fw_tag_data.ret)
-		return -EIO;
-	return 0;
+	if (ctx->fw) {
+		ret = rpi_firmware_property(ctx->fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
+					    &fw_tag_data, sizeof(fw_tag_data));
+		if (!ret && fw_tag_data.ret)
+			ret = -EIO;
+	} else {
+		ret = regmap_write(ctx->regmap, ctx->offset + reg, *val);
+	}
+
+	return ret;
 }
 
-static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val)
+static int read_reg(struct rpi_poe_power_supply_ctx *ctx, u32 reg, u32 *val)
 {
 	struct fw_tag_data_s fw_tag_data = {
-		.reg = reg,
+		.reg = reg + RPI_POE_FW_BASE_REG,
 		.val = *val
 	};
+	u32 value;
 	int ret;
 
-	ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
-				    &fw_tag_data, sizeof(fw_tag_data));
-	if (ret)
-		return ret;
-	else if (fw_tag_data.ret)
-		return -EIO;
+	if (ctx->fw) {
+		ret = rpi_firmware_property(ctx->fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
+					    &fw_tag_data, sizeof(fw_tag_data));
+		if (!ret && fw_tag_data.ret)
+			ret = -EIO;
+		*val = fw_tag_data.val;
+	} else {
+		ret = regmap_read(ctx->regmap, ctx->offset + reg, &value);
+		if (!ret) {
+			*val = value;
+			ret = regmap_read(ctx->regmap, ctx->offset + reg + 1, &value);
+			*val |= value << 8;
+		}
+	}
 
-	*val = fw_tag_data.val;
-	return 0;
+	return ret;
 }
 
 static int rpi_poe_power_supply_get_property(struct power_supply *psy,
@@ -82,14 +100,14 @@ static int rpi_poe_power_supply_get_prop
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_HEALTH:
-		ret = read_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
+		ret = read_reg(ctx, RPI_POE_FLAG_REG, &val);
 		if (ret)
 			return ret;
 
 		if (val & RPI_POE_FLAG_OC) {
 			r_val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 			val = RPI_POE_FLAG_OC;
-			ret = write_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
+			ret = write_reg(ctx, RPI_POE_FLAG_REG, &val);
 			if (ret)
 				return ret;
 			return 0;
@@ -99,7 +117,7 @@ static int rpi_poe_power_supply_get_prop
 		return 0;
 
 	case POWER_SUPPLY_PROP_ONLINE:
-		ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
+		ret = read_reg(ctx, RPI_POE_ADC_REG, &val);
 		if (ret)
 			return ret;
 
@@ -107,7 +125,7 @@ static int rpi_poe_power_supply_get_prop
 		return 0;
 
 	case POWER_SUPPLY_PROP_CURRENT_NOW:
-		ret = read_reg(ctx->fw, RPI_POE_ADC_REG, &val);
+		ret = read_reg(ctx, RPI_POE_ADC_REG, &val);
 		if (ret)
 			return ret;
 		val = (val * 3300)/9821;
@@ -115,15 +133,14 @@ static int rpi_poe_power_supply_get_prop
 		return 0;
 
 	case POWER_SUPPLY_PROP_CURRENT_MAX:
-		ret = read_reg(ctx->fw, RPI_POE_FLAG_REG, &val);
+		ret = read_reg(ctx, RPI_POE_FLAG_REG, &val);
 		if (ret)
 			return ret;
 
-		if (val & RPI_POE_FLAG_AT) {
+		if (val & RPI_POE_FLAG_AT)
 			r_val->intval = RPI_POE_CURRENT_AT_MAX;
-			return 0;
-		}
-		r_val->intval = RPI_POE_CURRENT_AF_MAX;
+		else
+			r_val->intval = RPI_POE_CURRENT_AF_MAX;
 		return 0;
 
 	default:
@@ -158,29 +175,38 @@ static int rpi_poe_power_supply_probe(st
 	if (!of_device_is_available(pdev->dev.of_node))
 		return -ENODEV;
 
-	fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
-	if (!fw_node) {
-		dev_err(&pdev->dev, "Missing firmware node\n");
-		return -ENOENT;
-	}
-
 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 
-	ctx->fw = rpi_firmware_get(fw_node);
-	if (!ctx->fw)
-		return -EPROBE_DEFER;
-	if (rpi_firmware_property(ctx->fw,
-			RPI_FIRMWARE_GET_FIRMWARE_REVISION,
-			&revision, sizeof(revision))) {
-		dev_err(&pdev->dev, "Failed to get firmware revision\n");
-		return -ENOENT;
-	}
-	if (revision < 0x60af72e8) {
-		dev_err(&pdev->dev, "Unsupported firmware\n");
-		return -ENOENT;
+	if (pdev->dev.parent)
+		ctx->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+
+	if (ctx->regmap) {
+		if (device_property_read_u32(&pdev->dev, "reg", &ctx->offset))
+			return -EINVAL;
+	} else {
+		fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
+		if (!fw_node) {
+			dev_err(&pdev->dev, "Missing firmware node\n");
+			return -ENOENT;
+		}
+
+		ctx->fw = rpi_firmware_get(fw_node);
+		if (!ctx->fw)
+			return -EPROBE_DEFER;
+		if (rpi_firmware_property(ctx->fw,
+					  RPI_FIRMWARE_GET_FIRMWARE_REVISION,
+					  &revision, sizeof(revision))) {
+			dev_err(&pdev->dev, "Failed to get firmware revision\n");
+			return -ENOENT;
+		}
+		if (revision < 0x60af72e8) {
+			dev_err(&pdev->dev, "Unsupported firmware\n");
+			return -ENOENT;
+		}
 	}
+
 	platform_set_drvdata(pdev, ctx);
 
 	psy_cfg.of_node = pdev->dev.of_node;