ipq807x: add Qualcomm Atheros IPQ807x target
[openwrt/staging/nbd.git] / target / linux / ipq807x / patches-5.15 / 0058-v6.0-mfd-qcom-spmi-pmic-expose-the-PMIC-revid-information.patch
1 From 231f6a9f24a5e9b6e7af801ca2377970474cdf59 Mon Sep 17 00:00:00 2001
2 From: Caleb Connolly <caleb.connolly@linaro.org>
3 Date: Fri, 29 Apr 2022 23:08:57 +0100
4 Subject: [PATCH] mfd: qcom-spmi-pmic: expose the PMIC revid information to
5 clients
6
7 Some PMIC functions such as the RRADC need to be aware of the PMIC
8 chip revision information to implement errata or otherwise adjust
9 behaviour, export the PMIC information to enable this.
10
11 This is specifically required to enable the RRADC to adjust
12 coefficients based on which chip fab the PMIC was produced in,
13 this can vary per unique device and therefore has to be read at
14 runtime.
15
16 Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
17 Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
18 Tested-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
19 Acked-by: Lee Jones <lee.jones@linaro.org>
20 Link: https://lore.kernel.org/r/20220429220904.137297-3-caleb.connolly@linaro.org
21 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
22 ---
23 drivers/mfd/qcom-spmi-pmic.c | 265 ++++++++++++++++++++----------
24 include/soc/qcom/qcom-spmi-pmic.h | 60 +++++++
25 2 files changed, 235 insertions(+), 90 deletions(-)
26 create mode 100644 include/soc/qcom/qcom-spmi-pmic.h
27
28 --- a/drivers/mfd/qcom-spmi-pmic.c
29 +++ b/drivers/mfd/qcom-spmi-pmic.c
30 @@ -3,11 +3,16 @@
31 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
32 */
33
34 +#include <linux/device.h>
35 +#include <linux/errno.h>
36 +#include <linux/gfp.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/spmi.h>
40 +#include <linux/types.h>
41 #include <linux/regmap.h>
42 #include <linux/of_platform.h>
43 +#include <soc/qcom/qcom-spmi-pmic.h>
44
45 #define PMIC_REV2 0x101
46 #define PMIC_REV3 0x102
47 @@ -17,106 +22,140 @@
48
49 #define PMIC_TYPE_VALUE 0x51
50
51 -#define COMMON_SUBTYPE 0x00
52 -#define PM8941_SUBTYPE 0x01
53 -#define PM8841_SUBTYPE 0x02
54 -#define PM8019_SUBTYPE 0x03
55 -#define PM8226_SUBTYPE 0x04
56 -#define PM8110_SUBTYPE 0x05
57 -#define PMA8084_SUBTYPE 0x06
58 -#define PMI8962_SUBTYPE 0x07
59 -#define PMD9635_SUBTYPE 0x08
60 -#define PM8994_SUBTYPE 0x09
61 -#define PMI8994_SUBTYPE 0x0a
62 -#define PM8916_SUBTYPE 0x0b
63 -#define PM8004_SUBTYPE 0x0c
64 -#define PM8909_SUBTYPE 0x0d
65 -#define PM8028_SUBTYPE 0x0e
66 -#define PM8901_SUBTYPE 0x0f
67 -#define PM8950_SUBTYPE 0x10
68 -#define PMI8950_SUBTYPE 0x11
69 -#define PM8998_SUBTYPE 0x14
70 -#define PMI8998_SUBTYPE 0x15
71 -#define PM8005_SUBTYPE 0x18
72 -#define PM660L_SUBTYPE 0x1A
73 -#define PM660_SUBTYPE 0x1B
74 -#define PM8150_SUBTYPE 0x1E
75 -#define PM8150L_SUBTYPE 0x1f
76 -#define PM8150B_SUBTYPE 0x20
77 -#define PMK8002_SUBTYPE 0x21
78 -#define PM8009_SUBTYPE 0x24
79 -#define PM8150C_SUBTYPE 0x26
80 -#define SMB2351_SUBTYPE 0x29
81 +#define PMIC_REV4_V2 0x02
82 +
83 +struct qcom_spmi_dev {
84 + int num_usids;
85 + struct qcom_spmi_pmic pmic;
86 +};
87 +
88 +#define N_USIDS(n) ((void *)n)
89
90 static const struct of_device_id pmic_spmi_id_table[] = {
91 - { .compatible = "qcom,pm660", .data = (void *)PM660_SUBTYPE },
92 - { .compatible = "qcom,pm660l", .data = (void *)PM660L_SUBTYPE },
93 - { .compatible = "qcom,pm8004", .data = (void *)PM8004_SUBTYPE },
94 - { .compatible = "qcom,pm8005", .data = (void *)PM8005_SUBTYPE },
95 - { .compatible = "qcom,pm8019", .data = (void *)PM8019_SUBTYPE },
96 - { .compatible = "qcom,pm8028", .data = (void *)PM8028_SUBTYPE },
97 - { .compatible = "qcom,pm8110", .data = (void *)PM8110_SUBTYPE },
98 - { .compatible = "qcom,pm8150", .data = (void *)PM8150_SUBTYPE },
99 - { .compatible = "qcom,pm8150b", .data = (void *)PM8150B_SUBTYPE },
100 - { .compatible = "qcom,pm8150c", .data = (void *)PM8150C_SUBTYPE },
101 - { .compatible = "qcom,pm8150l", .data = (void *)PM8150L_SUBTYPE },
102 - { .compatible = "qcom,pm8226", .data = (void *)PM8226_SUBTYPE },
103 - { .compatible = "qcom,pm8841", .data = (void *)PM8841_SUBTYPE },
104 - { .compatible = "qcom,pm8901", .data = (void *)PM8901_SUBTYPE },
105 - { .compatible = "qcom,pm8909", .data = (void *)PM8909_SUBTYPE },
106 - { .compatible = "qcom,pm8916", .data = (void *)PM8916_SUBTYPE },
107 - { .compatible = "qcom,pm8941", .data = (void *)PM8941_SUBTYPE },
108 - { .compatible = "qcom,pm8950", .data = (void *)PM8950_SUBTYPE },
109 - { .compatible = "qcom,pm8994", .data = (void *)PM8994_SUBTYPE },
110 - { .compatible = "qcom,pm8998", .data = (void *)PM8998_SUBTYPE },
111 - { .compatible = "qcom,pma8084", .data = (void *)PMA8084_SUBTYPE },
112 - { .compatible = "qcom,pmd9635", .data = (void *)PMD9635_SUBTYPE },
113 - { .compatible = "qcom,pmi8950", .data = (void *)PMI8950_SUBTYPE },
114 - { .compatible = "qcom,pmi8962", .data = (void *)PMI8962_SUBTYPE },
115 - { .compatible = "qcom,pmi8994", .data = (void *)PMI8994_SUBTYPE },
116 - { .compatible = "qcom,pmi8998", .data = (void *)PMI8998_SUBTYPE },
117 - { .compatible = "qcom,pmk8002", .data = (void *)PMK8002_SUBTYPE },
118 - { .compatible = "qcom,smb2351", .data = (void *)SMB2351_SUBTYPE },
119 - { .compatible = "qcom,spmi-pmic", .data = (void *)COMMON_SUBTYPE },
120 + { .compatible = "qcom,pm660", .data = N_USIDS(2) },
121 + { .compatible = "qcom,pm660l", .data = N_USIDS(2) },
122 + { .compatible = "qcom,pm8004", .data = N_USIDS(2) },
123 + { .compatible = "qcom,pm8005", .data = N_USIDS(2) },
124 + { .compatible = "qcom,pm8019", .data = N_USIDS(2) },
125 + { .compatible = "qcom,pm8028", .data = N_USIDS(2) },
126 + { .compatible = "qcom,pm8110", .data = N_USIDS(2) },
127 + { .compatible = "qcom,pm8150", .data = N_USIDS(2) },
128 + { .compatible = "qcom,pm8150b", .data = N_USIDS(2) },
129 + { .compatible = "qcom,pm8150c", .data = N_USIDS(2) },
130 + { .compatible = "qcom,pm8150l", .data = N_USIDS(2) },
131 + { .compatible = "qcom,pm8226", .data = N_USIDS(2) },
132 + { .compatible = "qcom,pm8841", .data = N_USIDS(2) },
133 + { .compatible = "qcom,pm8901", .data = N_USIDS(2) },
134 + { .compatible = "qcom,pm8909", .data = N_USIDS(2) },
135 + { .compatible = "qcom,pm8916", .data = N_USIDS(2) },
136 + { .compatible = "qcom,pm8941", .data = N_USIDS(2) },
137 + { .compatible = "qcom,pm8950", .data = N_USIDS(2) },
138 + { .compatible = "qcom,pm8994", .data = N_USIDS(2) },
139 + { .compatible = "qcom,pm8998", .data = N_USIDS(2) },
140 + { .compatible = "qcom,pma8084", .data = N_USIDS(2) },
141 + { .compatible = "qcom,pmd9635", .data = N_USIDS(2) },
142 + { .compatible = "qcom,pmi8950", .data = N_USIDS(2) },
143 + { .compatible = "qcom,pmi8962", .data = N_USIDS(2) },
144 + { .compatible = "qcom,pmi8994", .data = N_USIDS(2) },
145 + { .compatible = "qcom,pmi8998", .data = N_USIDS(2) },
146 + { .compatible = "qcom,pmk8002", .data = N_USIDS(2) },
147 + { .compatible = "qcom,smb2351", .data = N_USIDS(2) },
148 + { .compatible = "qcom,spmi-pmic", .data = N_USIDS(1) },
149 { }
150 };
151
152 -static void pmic_spmi_show_revid(struct regmap *map, struct device *dev)
153 +/*
154 + * A PMIC can be represented by multiple SPMI devices, but
155 + * only the base PMIC device will contain a reference to
156 + * the revision information.
157 + *
158 + * This function takes a pointer to a pmic device and
159 + * returns a pointer to the base PMIC device.
160 + *
161 + * This only supports PMICs with 1 or 2 USIDs.
162 + */
163 +static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev)
164 {
165 - unsigned int rev2, minor, major, type, subtype;
166 - const char *name = "unknown";
167 - int ret, i;
168 + struct spmi_device *sdev;
169 + struct qcom_spmi_dev *ctx;
170 + struct device_node *spmi_bus;
171 + struct device_node *other_usid = NULL;
172 + int function_parent_usid, ret;
173 + u32 pmic_addr;
174
175 - ret = regmap_read(map, PMIC_TYPE, &type);
176 - if (ret < 0)
177 - return;
178 + sdev = to_spmi_device(dev);
179 + ctx = dev_get_drvdata(&sdev->dev);
180
181 - if (type != PMIC_TYPE_VALUE)
182 - return;
183 + /*
184 + * Quick return if the function device is already in the base
185 + * USID. This will always be hit for PMICs with only 1 USID.
186 + */
187 + if (sdev->usid % ctx->num_usids == 0)
188 + return sdev;
189
190 - ret = regmap_read(map, PMIC_SUBTYPE, &subtype);
191 + function_parent_usid = sdev->usid;
192 +
193 + /*
194 + * Walk through the list of PMICs until we find the sibling USID.
195 + * The goal is to find the first USID which is less than the
196 + * number of USIDs in the PMIC array, e.g. for a PMIC with 2 USIDs
197 + * where the function device is under USID 3, we want to find the
198 + * device for USID 2.
199 + */
200 + spmi_bus = of_get_parent(sdev->dev.of_node);
201 + do {
202 + other_usid = of_get_next_child(spmi_bus, other_usid);
203 +
204 + ret = of_property_read_u32_index(other_usid, "reg", 0, &pmic_addr);
205 + if (ret)
206 + return ERR_PTR(ret);
207 +
208 + sdev = spmi_device_from_of(other_usid);
209 + if (pmic_addr == function_parent_usid - (ctx->num_usids - 1)) {
210 + if (!sdev)
211 + /*
212 + * If the base USID for this PMIC hasn't probed yet
213 + * but the secondary USID has, then we need to defer
214 + * the function driver so that it will attempt to
215 + * probe again when the base USID is ready.
216 + */
217 + return ERR_PTR(-EPROBE_DEFER);
218 + return sdev;
219 + }
220 + } while (other_usid->sibling);
221 +
222 + return ERR_PTR(-ENODATA);
223 +}
224 +
225 +static int pmic_spmi_load_revid(struct regmap *map, struct device *dev,
226 + struct qcom_spmi_pmic *pmic)
227 +{
228 + int ret;
229 +
230 + ret = regmap_read(map, PMIC_TYPE, &pmic->type);
231 if (ret < 0)
232 - return;
233 + return ret;
234
235 - for (i = 0; i < ARRAY_SIZE(pmic_spmi_id_table); i++) {
236 - if (subtype == (unsigned long)pmic_spmi_id_table[i].data)
237 - break;
238 - }
239 + if (pmic->type != PMIC_TYPE_VALUE)
240 + return ret;
241
242 - if (i != ARRAY_SIZE(pmic_spmi_id_table))
243 - name = pmic_spmi_id_table[i].compatible;
244 + ret = regmap_read(map, PMIC_SUBTYPE, &pmic->subtype);
245 + if (ret < 0)
246 + return ret;
247
248 - ret = regmap_read(map, PMIC_REV2, &rev2);
249 + pmic->name = of_match_device(pmic_spmi_id_table, dev)->compatible;
250 +
251 + ret = regmap_read(map, PMIC_REV2, &pmic->rev2);
252 if (ret < 0)
253 - return;
254 + return ret;
255
256 - ret = regmap_read(map, PMIC_REV3, &minor);
257 + ret = regmap_read(map, PMIC_REV3, &pmic->minor);
258 if (ret < 0)
259 - return;
260 + return ret;
261
262 - ret = regmap_read(map, PMIC_REV4, &major);
263 + ret = regmap_read(map, PMIC_REV4, &pmic->major);
264 if (ret < 0)
265 - return;
266 + return ret;
267
268 /*
269 * In early versions of PM8941 and PM8226, the major revision number
270 @@ -124,15 +163,49 @@ static void pmic_spmi_show_revid(struct
271 * Increment the major revision number here if the chip is an early
272 * version of PM8941 or PM8226.
273 */
274 - if ((subtype == PM8941_SUBTYPE || subtype == PM8226_SUBTYPE) &&
275 - major < 0x02)
276 - major++;
277 + if ((pmic->subtype == PM8941_SUBTYPE || pmic->subtype == PM8226_SUBTYPE) &&
278 + pmic->major < PMIC_REV4_V2)
279 + pmic->major++;
280 +
281 + if (pmic->subtype == PM8110_SUBTYPE)
282 + pmic->minor = pmic->rev2;
283 +
284 + dev_dbg(dev, "%x: %s v%d.%d\n",
285 + pmic->subtype, pmic->name, pmic->major, pmic->minor);
286 +
287 + return 0;
288 +}
289 +
290 +/**
291 + * qcom_pmic_get() - Get a pointer to the base PMIC device
292 + *
293 + * This function takes a struct device for a driver which is a child of a PMIC.
294 + * And locates the PMIC revision information for it.
295 + *
296 + * @dev: the pmic function device
297 + * @return: the struct qcom_spmi_pmic* pointer associated with the function device
298 + */
299 +const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev)
300 +{
301 + struct spmi_device *sdev;
302 + struct qcom_spmi_dev *spmi;
303 +
304 + /*
305 + * Make sure the device is actually a child of a PMIC
306 + */
307 + if (!of_match_device(pmic_spmi_id_table, dev->parent))
308 + return ERR_PTR(-EINVAL);
309 +
310 + sdev = qcom_pmic_get_base_usid(dev->parent);
311
312 - if (subtype == PM8110_SUBTYPE)
313 - minor = rev2;
314 + if (IS_ERR(sdev))
315 + return ERR_CAST(sdev);
316
317 - dev_dbg(dev, "%x: %s v%d.%d\n", subtype, name, major, minor);
318 + spmi = dev_get_drvdata(&sdev->dev);
319 +
320 + return &spmi->pmic;
321 }
322 +EXPORT_SYMBOL(qcom_pmic_get);
323
324 static const struct regmap_config spmi_regmap_config = {
325 .reg_bits = 16,
326 @@ -144,14 +217,26 @@ static const struct regmap_config spmi_r
327 static int pmic_spmi_probe(struct spmi_device *sdev)
328 {
329 struct regmap *regmap;
330 + struct qcom_spmi_dev *ctx;
331 + int ret;
332
333 regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
334 if (IS_ERR(regmap))
335 return PTR_ERR(regmap);
336
337 + ctx = devm_kzalloc(&sdev->dev, sizeof(*ctx), GFP_KERNEL);
338 + if (!ctx)
339 + return -ENOMEM;
340 +
341 + ctx->num_usids = (uintptr_t)of_device_get_match_data(&sdev->dev);
342 +
343 /* Only the first slave id for a PMIC contains this information */
344 - if (sdev->usid % 2 == 0)
345 - pmic_spmi_show_revid(regmap, &sdev->dev);
346 + if (sdev->usid % ctx->num_usids == 0) {
347 + ret = pmic_spmi_load_revid(regmap, &sdev->dev, &ctx->pmic);
348 + if (ret < 0)
349 + return ret;
350 + }
351 + spmi_device_set_drvdata(sdev, ctx);
352
353 return devm_of_platform_populate(&sdev->dev);
354 }
355 --- /dev/null
356 +++ b/include/soc/qcom/qcom-spmi-pmic.h
357 @@ -0,0 +1,60 @@
358 +/* SPDX-License-Identifier: GPL-2.0-only */
359 +/* Copyright (c) 2022 Linaro. All rights reserved.
360 + * Author: Caleb Connolly <caleb.connolly@linaro.org>
361 + */
362 +
363 +#ifndef __QCOM_SPMI_PMIC_H__
364 +#define __QCOM_SPMI_PMIC_H__
365 +
366 +#include <linux/device.h>
367 +
368 +#define COMMON_SUBTYPE 0x00
369 +#define PM8941_SUBTYPE 0x01
370 +#define PM8841_SUBTYPE 0x02
371 +#define PM8019_SUBTYPE 0x03
372 +#define PM8226_SUBTYPE 0x04
373 +#define PM8110_SUBTYPE 0x05
374 +#define PMA8084_SUBTYPE 0x06
375 +#define PMI8962_SUBTYPE 0x07
376 +#define PMD9635_SUBTYPE 0x08
377 +#define PM8994_SUBTYPE 0x09
378 +#define PMI8994_SUBTYPE 0x0a
379 +#define PM8916_SUBTYPE 0x0b
380 +#define PM8004_SUBTYPE 0x0c
381 +#define PM8909_SUBTYPE 0x0d
382 +#define PM8028_SUBTYPE 0x0e
383 +#define PM8901_SUBTYPE 0x0f
384 +#define PM8950_SUBTYPE 0x10
385 +#define PMI8950_SUBTYPE 0x11
386 +#define PM8998_SUBTYPE 0x14
387 +#define PMI8998_SUBTYPE 0x15
388 +#define PM8005_SUBTYPE 0x18
389 +#define PM660L_SUBTYPE 0x1A
390 +#define PM660_SUBTYPE 0x1B
391 +#define PM8150_SUBTYPE 0x1E
392 +#define PM8150L_SUBTYPE 0x1f
393 +#define PM8150B_SUBTYPE 0x20
394 +#define PMK8002_SUBTYPE 0x21
395 +#define PM8009_SUBTYPE 0x24
396 +#define PM8150C_SUBTYPE 0x26
397 +#define SMB2351_SUBTYPE 0x29
398 +
399 +#define PMI8998_FAB_ID_SMIC 0x11
400 +#define PMI8998_FAB_ID_GF 0x30
401 +
402 +#define PM660_FAB_ID_GF 0x0
403 +#define PM660_FAB_ID_TSMC 0x2
404 +#define PM660_FAB_ID_MX 0x3
405 +
406 +struct qcom_spmi_pmic {
407 + unsigned int type;
408 + unsigned int subtype;
409 + unsigned int major;
410 + unsigned int minor;
411 + unsigned int rev2;
412 + const char *name;
413 +};
414 +
415 +const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev);
416 +
417 +#endif /* __QCOM_SPMI_PMIC_H__ */