Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/spmi.h>
9#include <linux/regmap.h>
10#include <linux/of_platform.h>
11
12#define PMIC_REV2 0x101
13#define PMIC_REV3 0x102
14#define PMIC_REV4 0x103
15#define PMIC_TYPE 0x104
16#define PMIC_SUBTYPE 0x105
17
18#define PMIC_TYPE_VALUE 0x51
19
20#define COMMON_SUBTYPE 0x00
21#define PM8941_SUBTYPE 0x01
22#define PM8841_SUBTYPE 0x02
23#define PM8019_SUBTYPE 0x03
24#define PM8226_SUBTYPE 0x04
25#define PM8110_SUBTYPE 0x05
26#define PMA8084_SUBTYPE 0x06
27#define PMI8962_SUBTYPE 0x07
28#define PMD9635_SUBTYPE 0x08
29#define PM8994_SUBTYPE 0x09
30#define PMI8994_SUBTYPE 0x0a
31#define PM8916_SUBTYPE 0x0b
32#define PM8004_SUBTYPE 0x0c
33#define PM8909_SUBTYPE 0x0d
34#define PM8950_SUBTYPE 0x10
35#define PMI8950_SUBTYPE 0x11
36#define PM8998_SUBTYPE 0x14
37#define PMI8998_SUBTYPE 0x15
38#define PM8005_SUBTYPE 0x18
39
40static const struct of_device_id pmic_spmi_id_table[] = {
41 { .compatible = "qcom,spmi-pmic", .data = (void *)COMMON_SUBTYPE },
42 { .compatible = "qcom,pm8941", .data = (void *)PM8941_SUBTYPE },
43 { .compatible = "qcom,pm8841", .data = (void *)PM8841_SUBTYPE },
44 { .compatible = "qcom,pm8019", .data = (void *)PM8019_SUBTYPE },
45 { .compatible = "qcom,pm8226", .data = (void *)PM8226_SUBTYPE },
46 { .compatible = "qcom,pm8110", .data = (void *)PM8110_SUBTYPE },
47 { .compatible = "qcom,pma8084", .data = (void *)PMA8084_SUBTYPE },
48 { .compatible = "qcom,pmi8962", .data = (void *)PMI8962_SUBTYPE },
49 { .compatible = "qcom,pmd9635", .data = (void *)PMD9635_SUBTYPE },
50 { .compatible = "qcom,pm8994", .data = (void *)PM8994_SUBTYPE },
51 { .compatible = "qcom,pmi8994", .data = (void *)PMI8994_SUBTYPE },
52 { .compatible = "qcom,pm8916", .data = (void *)PM8916_SUBTYPE },
53 { .compatible = "qcom,pm8004", .data = (void *)PM8004_SUBTYPE },
54 { .compatible = "qcom,pm8909", .data = (void *)PM8909_SUBTYPE },
55 { .compatible = "qcom,pm8950", .data = (void *)PM8950_SUBTYPE },
56 { .compatible = "qcom,pmi8950", .data = (void *)PMI8950_SUBTYPE },
57 { .compatible = "qcom,pm8998", .data = (void *)PM8998_SUBTYPE },
58 { .compatible = "qcom,pmi8998", .data = (void *)PMI8998_SUBTYPE },
59 { .compatible = "qcom,pm8005", .data = (void *)PM8005_SUBTYPE },
60 { }
61};
62
63static void pmic_spmi_show_revid(struct regmap *map, struct device *dev)
64{
65 unsigned int rev2, minor, major, type, subtype;
66 const char *name = "unknown";
67 int ret, i;
68
69 ret = regmap_read(map, PMIC_TYPE, &type);
70 if (ret < 0)
71 return;
72
73 if (type != PMIC_TYPE_VALUE)
74 return;
75
76 ret = regmap_read(map, PMIC_SUBTYPE, &subtype);
77 if (ret < 0)
78 return;
79
80 for (i = 0; i < ARRAY_SIZE(pmic_spmi_id_table); i++) {
81 if (subtype == (unsigned long)pmic_spmi_id_table[i].data)
82 break;
83 }
84
85 if (i != ARRAY_SIZE(pmic_spmi_id_table))
86 name = pmic_spmi_id_table[i].compatible;
87
88 ret = regmap_read(map, PMIC_REV2, &rev2);
89 if (ret < 0)
90 return;
91
92 ret = regmap_read(map, PMIC_REV3, &minor);
93 if (ret < 0)
94 return;
95
96 ret = regmap_read(map, PMIC_REV4, &major);
97 if (ret < 0)
98 return;
99
100 /*
101 * In early versions of PM8941 and PM8226, the major revision number
102 * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
103 * Increment the major revision number here if the chip is an early
104 * version of PM8941 or PM8226.
105 */
106 if ((subtype == PM8941_SUBTYPE || subtype == PM8226_SUBTYPE) &&
107 major < 0x02)
108 major++;
109
110 if (subtype == PM8110_SUBTYPE)
111 minor = rev2;
112
113 dev_dbg(dev, "%x: %s v%d.%d\n", subtype, name, major, minor);
114}
115
116static const struct regmap_config spmi_regmap_config = {
117 .reg_bits = 16,
118 .val_bits = 8,
119 .max_register = 0xffff,
120 .fast_io = true,
121};
122
123static int pmic_spmi_probe(struct spmi_device *sdev)
124{
125 struct regmap *regmap;
126
127 regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
128 if (IS_ERR(regmap))
129 return PTR_ERR(regmap);
130
131 /* Only the first slave id for a PMIC contains this information */
132 if (sdev->usid % 2 == 0)
133 pmic_spmi_show_revid(regmap, &sdev->dev);
134
135 return devm_of_platform_populate(&sdev->dev);
136}
137
138MODULE_DEVICE_TABLE(of, pmic_spmi_id_table);
139
140static struct spmi_driver pmic_spmi_driver = {
141 .probe = pmic_spmi_probe,
142 .driver = {
143 .name = "pmic-spmi",
144 .of_match_table = pmic_spmi_id_table,
145 },
146};
147module_spmi_driver(pmic_spmi_driver);
148
149MODULE_DESCRIPTION("Qualcomm SPMI PMIC driver");
150MODULE_ALIAS("spmi:spmi-pmic");
151MODULE_LICENSE("GPL v2");
152MODULE_AUTHOR("Josh Cartwright <joshc@codeaurora.org>");
153MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/device.h>
7#include <linux/errno.h>
8#include <linux/gfp.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/spmi.h>
12#include <linux/types.h>
13#include <linux/regmap.h>
14#include <linux/of_platform.h>
15#include <soc/qcom/qcom-spmi-pmic.h>
16
17#define PMIC_REV2 0x101
18#define PMIC_REV3 0x102
19#define PMIC_REV4 0x103
20#define PMIC_TYPE 0x104
21#define PMIC_SUBTYPE 0x105
22#define PMIC_FAB_ID 0x1f2
23
24#define PMIC_TYPE_VALUE 0x51
25
26#define PMIC_REV4_V2 0x02
27
28struct qcom_spmi_dev {
29 int num_usids;
30 struct qcom_spmi_pmic pmic;
31};
32
33#define N_USIDS(n) ((void *)n)
34
35static const struct of_device_id pmic_spmi_id_table[] = {
36 { .compatible = "qcom,pm660", .data = N_USIDS(2) },
37 { .compatible = "qcom,pm660l", .data = N_USIDS(2) },
38 { .compatible = "qcom,pm8004", .data = N_USIDS(2) },
39 { .compatible = "qcom,pm8005", .data = N_USIDS(2) },
40 { .compatible = "qcom,pm8019", .data = N_USIDS(2) },
41 { .compatible = "qcom,pm8028", .data = N_USIDS(2) },
42 { .compatible = "qcom,pm8110", .data = N_USIDS(2) },
43 { .compatible = "qcom,pm8150", .data = N_USIDS(2) },
44 { .compatible = "qcom,pm8150b", .data = N_USIDS(2) },
45 { .compatible = "qcom,pm8150c", .data = N_USIDS(2) },
46 { .compatible = "qcom,pm8150l", .data = N_USIDS(2) },
47 { .compatible = "qcom,pm8226", .data = N_USIDS(2) },
48 { .compatible = "qcom,pm8841", .data = N_USIDS(2) },
49 { .compatible = "qcom,pm8901", .data = N_USIDS(2) },
50 { .compatible = "qcom,pm8909", .data = N_USIDS(2) },
51 { .compatible = "qcom,pm8916", .data = N_USIDS(2) },
52 { .compatible = "qcom,pm8941", .data = N_USIDS(2) },
53 { .compatible = "qcom,pm8950", .data = N_USIDS(2) },
54 { .compatible = "qcom,pm8994", .data = N_USIDS(2) },
55 { .compatible = "qcom,pm8998", .data = N_USIDS(2) },
56 { .compatible = "qcom,pma8084", .data = N_USIDS(2) },
57 { .compatible = "qcom,pmd9635", .data = N_USIDS(2) },
58 { .compatible = "qcom,pmi8950", .data = N_USIDS(2) },
59 { .compatible = "qcom,pmi8962", .data = N_USIDS(2) },
60 { .compatible = "qcom,pmi8994", .data = N_USIDS(2) },
61 { .compatible = "qcom,pmi8998", .data = N_USIDS(2) },
62 { .compatible = "qcom,pmk8002", .data = N_USIDS(2) },
63 { .compatible = "qcom,pmp8074", .data = N_USIDS(2) },
64 { .compatible = "qcom,smb2351", .data = N_USIDS(2) },
65 { .compatible = "qcom,spmi-pmic", .data = N_USIDS(1) },
66 { }
67};
68
69/*
70 * A PMIC can be represented by multiple SPMI devices, but
71 * only the base PMIC device will contain a reference to
72 * the revision information.
73 *
74 * This function takes a pointer to a pmic device and
75 * returns a pointer to the base PMIC device.
76 *
77 * This only supports PMICs with 1 or 2 USIDs.
78 */
79static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev)
80{
81 struct spmi_device *sdev;
82 struct qcom_spmi_dev *ctx;
83 struct device_node *spmi_bus;
84 struct device_node *other_usid = NULL;
85 int function_parent_usid, ret;
86 u32 pmic_addr;
87
88 sdev = to_spmi_device(dev);
89 ctx = dev_get_drvdata(&sdev->dev);
90
91 /*
92 * Quick return if the function device is already in the base
93 * USID. This will always be hit for PMICs with only 1 USID.
94 */
95 if (sdev->usid % ctx->num_usids == 0)
96 return sdev;
97
98 function_parent_usid = sdev->usid;
99
100 /*
101 * Walk through the list of PMICs until we find the sibling USID.
102 * The goal is to find the first USID which is less than the
103 * number of USIDs in the PMIC array, e.g. for a PMIC with 2 USIDs
104 * where the function device is under USID 3, we want to find the
105 * device for USID 2.
106 */
107 spmi_bus = of_get_parent(sdev->dev.of_node);
108 do {
109 other_usid = of_get_next_child(spmi_bus, other_usid);
110
111 ret = of_property_read_u32_index(other_usid, "reg", 0, &pmic_addr);
112 if (ret)
113 return ERR_PTR(ret);
114
115 sdev = spmi_device_from_of(other_usid);
116 if (pmic_addr == function_parent_usid - (ctx->num_usids - 1)) {
117 if (!sdev)
118 /*
119 * If the base USID for this PMIC hasn't probed yet
120 * but the secondary USID has, then we need to defer
121 * the function driver so that it will attempt to
122 * probe again when the base USID is ready.
123 */
124 return ERR_PTR(-EPROBE_DEFER);
125 return sdev;
126 }
127 } while (other_usid->sibling);
128
129 return ERR_PTR(-ENODATA);
130}
131
132static int pmic_spmi_load_revid(struct regmap *map, struct device *dev,
133 struct qcom_spmi_pmic *pmic)
134{
135 int ret;
136
137 ret = regmap_read(map, PMIC_TYPE, &pmic->type);
138 if (ret < 0)
139 return ret;
140
141 if (pmic->type != PMIC_TYPE_VALUE)
142 return ret;
143
144 ret = regmap_read(map, PMIC_SUBTYPE, &pmic->subtype);
145 if (ret < 0)
146 return ret;
147
148 pmic->name = of_match_device(pmic_spmi_id_table, dev)->compatible;
149
150 ret = regmap_read(map, PMIC_REV2, &pmic->rev2);
151 if (ret < 0)
152 return ret;
153
154 ret = regmap_read(map, PMIC_REV3, &pmic->minor);
155 if (ret < 0)
156 return ret;
157
158 ret = regmap_read(map, PMIC_REV4, &pmic->major);
159 if (ret < 0)
160 return ret;
161
162 if (pmic->subtype == PMI8998_SUBTYPE || pmic->subtype == PM660_SUBTYPE) {
163 ret = regmap_read(map, PMIC_FAB_ID, &pmic->fab_id);
164 if (ret < 0)
165 return ret;
166 }
167
168 /*
169 * In early versions of PM8941 and PM8226, the major revision number
170 * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
171 * Increment the major revision number here if the chip is an early
172 * version of PM8941 or PM8226.
173 */
174 if ((pmic->subtype == PM8941_SUBTYPE || pmic->subtype == PM8226_SUBTYPE) &&
175 pmic->major < PMIC_REV4_V2)
176 pmic->major++;
177
178 if (pmic->subtype == PM8110_SUBTYPE)
179 pmic->minor = pmic->rev2;
180
181 dev_dbg(dev, "%x: %s v%d.%d\n",
182 pmic->subtype, pmic->name, pmic->major, pmic->minor);
183
184 return 0;
185}
186
187/**
188 * qcom_pmic_get() - Get a pointer to the base PMIC device
189 *
190 * This function takes a struct device for a driver which is a child of a PMIC.
191 * And locates the PMIC revision information for it.
192 *
193 * @dev: the pmic function device
194 * @return: the struct qcom_spmi_pmic* pointer associated with the function device
195 */
196const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev)
197{
198 struct spmi_device *sdev;
199 struct qcom_spmi_dev *spmi;
200
201 /*
202 * Make sure the device is actually a child of a PMIC
203 */
204 if (!of_match_device(pmic_spmi_id_table, dev->parent))
205 return ERR_PTR(-EINVAL);
206
207 sdev = qcom_pmic_get_base_usid(dev->parent);
208
209 if (IS_ERR(sdev))
210 return ERR_CAST(sdev);
211
212 spmi = dev_get_drvdata(&sdev->dev);
213
214 return &spmi->pmic;
215}
216EXPORT_SYMBOL(qcom_pmic_get);
217
218static const struct regmap_config spmi_regmap_config = {
219 .reg_bits = 16,
220 .val_bits = 8,
221 .max_register = 0xffff,
222 .fast_io = true,
223};
224
225static int pmic_spmi_probe(struct spmi_device *sdev)
226{
227 struct regmap *regmap;
228 struct qcom_spmi_dev *ctx;
229 int ret;
230
231 regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
232 if (IS_ERR(regmap))
233 return PTR_ERR(regmap);
234
235 ctx = devm_kzalloc(&sdev->dev, sizeof(*ctx), GFP_KERNEL);
236 if (!ctx)
237 return -ENOMEM;
238
239 ctx->num_usids = (uintptr_t)of_device_get_match_data(&sdev->dev);
240
241 /* Only the first slave id for a PMIC contains this information */
242 if (sdev->usid % ctx->num_usids == 0) {
243 ret = pmic_spmi_load_revid(regmap, &sdev->dev, &ctx->pmic);
244 if (ret < 0)
245 return ret;
246 }
247 spmi_device_set_drvdata(sdev, ctx);
248
249 return devm_of_platform_populate(&sdev->dev);
250}
251
252MODULE_DEVICE_TABLE(of, pmic_spmi_id_table);
253
254static struct spmi_driver pmic_spmi_driver = {
255 .probe = pmic_spmi_probe,
256 .driver = {
257 .name = "pmic-spmi",
258 .of_match_table = pmic_spmi_id_table,
259 },
260};
261module_spmi_driver(pmic_spmi_driver);
262
263MODULE_DESCRIPTION("Qualcomm SPMI PMIC driver");
264MODULE_ALIAS("spmi:spmi-pmic");
265MODULE_LICENSE("GPL v2");
266MODULE_AUTHOR("Josh Cartwright <joshc@codeaurora.org>");
267MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");