dm vdo: add sysfs support for setting parameters and fetching stats
[linux-2.6-block.git] / drivers / mfd / qcom-spmi-pmic.c
CommitLineData
97fb5e8d 1// SPDX-License-Identifier: GPL-2.0-only
c3a973a7
JC
2/*
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
c3a973a7
JC
4 */
5
e9c11c6e
CC
6#include <linux/device.h>
7#include <linux/errno.h>
8#include <linux/gfp.h>
c3a973a7
JC
9#include <linux/kernel.h>
10#include <linux/module.h>
830fafce
RH
11#include <linux/of.h>
12#include <linux/of_device.h>
13#include <linux/of_platform.h>
c3a973a7 14#include <linux/spmi.h>
e9c11c6e 15#include <linux/types.h>
c3a973a7 16#include <linux/regmap.h>
e9c11c6e 17#include <soc/qcom/qcom-spmi-pmic.h>
c3a973a7 18
dc716bbf
II
19#define PMIC_REV2 0x101
20#define PMIC_REV3 0x102
21#define PMIC_REV4 0x103
22#define PMIC_TYPE 0x104
23#define PMIC_SUBTYPE 0x105
d23c3c08 24#define PMIC_FAB_ID 0x1f2
dc716bbf
II
25
26#define PMIC_TYPE_VALUE 0x51
27
e9c11c6e
CC
28#define PMIC_REV4_V2 0x02
29
30struct qcom_spmi_dev {
31 int num_usids;
32 struct qcom_spmi_pmic pmic;
33};
34
7b439aaa
JH
35static DEFINE_MUTEX(pmic_spmi_revid_lock);
36
e9c11c6e 37#define N_USIDS(n) ((void *)n)
dc716bbf
II
38
39static const struct of_device_id pmic_spmi_id_table[] = {
e9c11c6e
CC
40 { .compatible = "qcom,pm660", .data = N_USIDS(2) },
41 { .compatible = "qcom,pm660l", .data = N_USIDS(2) },
42 { .compatible = "qcom,pm8004", .data = N_USIDS(2) },
43 { .compatible = "qcom,pm8005", .data = N_USIDS(2) },
44 { .compatible = "qcom,pm8019", .data = N_USIDS(2) },
45 { .compatible = "qcom,pm8028", .data = N_USIDS(2) },
46 { .compatible = "qcom,pm8110", .data = N_USIDS(2) },
47 { .compatible = "qcom,pm8150", .data = N_USIDS(2) },
48 { .compatible = "qcom,pm8150b", .data = N_USIDS(2) },
49 { .compatible = "qcom,pm8150c", .data = N_USIDS(2) },
50 { .compatible = "qcom,pm8150l", .data = N_USIDS(2) },
51 { .compatible = "qcom,pm8226", .data = N_USIDS(2) },
52 { .compatible = "qcom,pm8841", .data = N_USIDS(2) },
53 { .compatible = "qcom,pm8901", .data = N_USIDS(2) },
54 { .compatible = "qcom,pm8909", .data = N_USIDS(2) },
55 { .compatible = "qcom,pm8916", .data = N_USIDS(2) },
4773d2f1 56 { .compatible = "qcom,pm8937", .data = N_USIDS(2) },
e9c11c6e
CC
57 { .compatible = "qcom,pm8941", .data = N_USIDS(2) },
58 { .compatible = "qcom,pm8950", .data = N_USIDS(2) },
59 { .compatible = "qcom,pm8994", .data = N_USIDS(2) },
60 { .compatible = "qcom,pm8998", .data = N_USIDS(2) },
61 { .compatible = "qcom,pma8084", .data = N_USIDS(2) },
62 { .compatible = "qcom,pmd9635", .data = N_USIDS(2) },
63 { .compatible = "qcom,pmi8950", .data = N_USIDS(2) },
64 { .compatible = "qcom,pmi8962", .data = N_USIDS(2) },
65 { .compatible = "qcom,pmi8994", .data = N_USIDS(2) },
66 { .compatible = "qcom,pmi8998", .data = N_USIDS(2) },
67 { .compatible = "qcom,pmk8002", .data = N_USIDS(2) },
90d7c403 68 { .compatible = "qcom,pmp8074", .data = N_USIDS(2) },
e9c11c6e
CC
69 { .compatible = "qcom,smb2351", .data = N_USIDS(2) },
70 { .compatible = "qcom,spmi-pmic", .data = N_USIDS(1) },
dc716bbf
II
71 { }
72};
73
e9c11c6e
CC
74/*
75 * A PMIC can be represented by multiple SPMI devices, but
76 * only the base PMIC device will contain a reference to
77 * the revision information.
78 *
79 * This function takes a pointer to a pmic device and
80 * returns a pointer to the base PMIC device.
81 *
82 * This only supports PMICs with 1 or 2 USIDs.
83 */
7b439aaa 84static struct spmi_device *qcom_pmic_get_base_usid(struct spmi_device *sdev, struct qcom_spmi_dev *ctx)
dc716bbf 85{
e9c11c6e 86 struct device_node *spmi_bus;
a0fa44c2 87 struct device_node *child;
e9c11c6e
CC
88 int function_parent_usid, ret;
89 u32 pmic_addr;
dc716bbf 90
e9c11c6e
CC
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 */
7b439aaa
JH
95 if (sdev->usid % ctx->num_usids == 0) {
96 get_device(&sdev->dev);
e9c11c6e 97 return sdev;
7b439aaa 98 }
dc716bbf 99
e9c11c6e
CC
100 function_parent_usid = sdev->usid;
101
102 /*
103 * Walk through the list of PMICs until we find the sibling USID.
104 * The goal is to find the first USID which is less than the
105 * number of USIDs in the PMIC array, e.g. for a PMIC with 2 USIDs
106 * where the function device is under USID 3, we want to find the
107 * device for USID 2.
108 */
109 spmi_bus = of_get_parent(sdev->dev.of_node);
a0fa44c2
JH
110 sdev = ERR_PTR(-ENODATA);
111 for_each_child_of_node(spmi_bus, child) {
112 ret = of_property_read_u32_index(child, "reg", 0, &pmic_addr);
113 if (ret) {
114 of_node_put(child);
115 sdev = ERR_PTR(ret);
116 break;
117 }
e9c11c6e 118
e9c11c6e 119 if (pmic_addr == function_parent_usid - (ctx->num_usids - 1)) {
272f99ed 120 sdev = spmi_find_device_by_of_node(child);
a0fa44c2 121 if (!sdev) {
e9c11c6e 122 /*
7b439aaa
JH
123 * If the base USID for this PMIC hasn't been
124 * registered yet then we need to defer.
e9c11c6e 125 */
a0fa44c2
JH
126 sdev = ERR_PTR(-EPROBE_DEFER);
127 }
128 of_node_put(child);
129 break;
e9c11c6e 130 }
a0fa44c2
JH
131 }
132
133 of_node_put(spmi_bus);
e9c11c6e 134
a0fa44c2 135 return sdev;
e9c11c6e
CC
136}
137
7b439aaa
JH
138static int pmic_spmi_get_base_revid(struct spmi_device *sdev, struct qcom_spmi_dev *ctx)
139{
140 struct qcom_spmi_dev *base_ctx;
141 struct spmi_device *base;
142 int ret = 0;
143
144 base = qcom_pmic_get_base_usid(sdev, ctx);
145 if (IS_ERR(base))
146 return PTR_ERR(base);
147
148 /*
149 * Copy revid info from base device if it has probed and is still
150 * bound to its driver.
151 */
152 mutex_lock(&pmic_spmi_revid_lock);
153 base_ctx = spmi_device_get_drvdata(base);
154 if (!base_ctx) {
155 ret = -EPROBE_DEFER;
156 goto out_unlock;
157 }
158 memcpy(&ctx->pmic, &base_ctx->pmic, sizeof(ctx->pmic));
159out_unlock:
160 mutex_unlock(&pmic_spmi_revid_lock);
161
162 put_device(&base->dev);
163
164 return ret;
165}
166
e9c11c6e
CC
167static int pmic_spmi_load_revid(struct regmap *map, struct device *dev,
168 struct qcom_spmi_pmic *pmic)
169{
170 int ret;
dc716bbf 171
e9c11c6e 172 ret = regmap_read(map, PMIC_TYPE, &pmic->type);
dc716bbf 173 if (ret < 0)
e9c11c6e 174 return ret;
dc716bbf 175
e9c11c6e
CC
176 if (pmic->type != PMIC_TYPE_VALUE)
177 return ret;
178
179 ret = regmap_read(map, PMIC_SUBTYPE, &pmic->subtype);
180 if (ret < 0)
181 return ret;
dc716bbf 182
e9c11c6e 183 pmic->name = of_match_device(pmic_spmi_id_table, dev)->compatible;
dc716bbf 184
e9c11c6e 185 ret = regmap_read(map, PMIC_REV2, &pmic->rev2);
dc716bbf 186 if (ret < 0)
e9c11c6e 187 return ret;
dc716bbf 188
e9c11c6e 189 ret = regmap_read(map, PMIC_REV3, &pmic->minor);
dc716bbf 190 if (ret < 0)
e9c11c6e 191 return ret;
dc716bbf 192
e9c11c6e 193 ret = regmap_read(map, PMIC_REV4, &pmic->major);
dc716bbf 194 if (ret < 0)
e9c11c6e 195 return ret;
dc716bbf 196
d23c3c08
CC
197 if (pmic->subtype == PMI8998_SUBTYPE || pmic->subtype == PM660_SUBTYPE) {
198 ret = regmap_read(map, PMIC_FAB_ID, &pmic->fab_id);
199 if (ret < 0)
200 return ret;
201 }
202
dc716bbf
II
203 /*
204 * In early versions of PM8941 and PM8226, the major revision number
205 * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
206 * Increment the major revision number here if the chip is an early
207 * version of PM8941 or PM8226.
208 */
e9c11c6e
CC
209 if ((pmic->subtype == PM8941_SUBTYPE || pmic->subtype == PM8226_SUBTYPE) &&
210 pmic->major < PMIC_REV4_V2)
211 pmic->major++;
212
213 if (pmic->subtype == PM8110_SUBTYPE)
214 pmic->minor = pmic->rev2;
215
216 dev_dbg(dev, "%x: %s v%d.%d\n",
217 pmic->subtype, pmic->name, pmic->major, pmic->minor);
218
219 return 0;
220}
221
222/**
223 * qcom_pmic_get() - Get a pointer to the base PMIC device
224 *
225 * This function takes a struct device for a driver which is a child of a PMIC.
226 * And locates the PMIC revision information for it.
227 *
228 * @dev: the pmic function device
229 * @return: the struct qcom_spmi_pmic* pointer associated with the function device
230 */
231const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev)
232{
233 struct spmi_device *sdev;
234 struct qcom_spmi_dev *spmi;
235
236 /*
237 * Make sure the device is actually a child of a PMIC
238 */
239 if (!of_match_device(pmic_spmi_id_table, dev->parent))
240 return ERR_PTR(-EINVAL);
241
7b439aaa 242 sdev = to_spmi_device(dev->parent);
e9c11c6e
CC
243 spmi = dev_get_drvdata(&sdev->dev);
244
245 return &spmi->pmic;
dc716bbf 246}
10450565 247EXPORT_SYMBOL_GPL(qcom_pmic_get);
dc716bbf 248
c3a973a7
JC
249static const struct regmap_config spmi_regmap_config = {
250 .reg_bits = 16,
251 .val_bits = 8,
252 .max_register = 0xffff,
253 .fast_io = true,
254};
255
256static int pmic_spmi_probe(struct spmi_device *sdev)
257{
c3a973a7 258 struct regmap *regmap;
e9c11c6e
CC
259 struct qcom_spmi_dev *ctx;
260 int ret;
c3a973a7
JC
261
262 regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
263 if (IS_ERR(regmap))
264 return PTR_ERR(regmap);
265
e9c11c6e
CC
266 ctx = devm_kzalloc(&sdev->dev, sizeof(*ctx), GFP_KERNEL);
267 if (!ctx)
268 return -ENOMEM;
269
830fafce 270 ctx->num_usids = (uintptr_t)device_get_match_data(&sdev->dev);
e9c11c6e 271
742dcd11 272 /* Only the first slave id for a PMIC contains this information */
e9c11c6e
CC
273 if (sdev->usid % ctx->num_usids == 0) {
274 ret = pmic_spmi_load_revid(regmap, &sdev->dev, &ctx->pmic);
275 if (ret < 0)
276 return ret;
7b439aaa
JH
277 } else {
278 ret = pmic_spmi_get_base_revid(sdev, ctx);
279 if (ret)
280 return ret;
e9c11c6e 281 }
7b439aaa
JH
282
283 mutex_lock(&pmic_spmi_revid_lock);
e9c11c6e 284 spmi_device_set_drvdata(sdev, ctx);
7b439aaa 285 mutex_unlock(&pmic_spmi_revid_lock);
dc716bbf 286
6f00f8c8 287 return devm_of_platform_populate(&sdev->dev);
c3a973a7
JC
288}
289
7b439aaa
JH
290static void pmic_spmi_remove(struct spmi_device *sdev)
291{
292 mutex_lock(&pmic_spmi_revid_lock);
293 spmi_device_set_drvdata(sdev, NULL);
294 mutex_unlock(&pmic_spmi_revid_lock);
295}
296
c3a973a7
JC
297MODULE_DEVICE_TABLE(of, pmic_spmi_id_table);
298
299static struct spmi_driver pmic_spmi_driver = {
300 .probe = pmic_spmi_probe,
7b439aaa 301 .remove = pmic_spmi_remove,
c3a973a7
JC
302 .driver = {
303 .name = "pmic-spmi",
304 .of_match_table = pmic_spmi_id_table,
305 },
306};
307module_spmi_driver(pmic_spmi_driver);
308
309MODULE_DESCRIPTION("Qualcomm SPMI PMIC driver");
310MODULE_ALIAS("spmi:spmi-pmic");
311MODULE_LICENSE("GPL v2");
312MODULE_AUTHOR("Josh Cartwright <joshc@codeaurora.org>");
313MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");