blob: 05e8172cd3b7f7df6964d35d881b8b818450bd14 [file] [log] [blame]
Subbaraman Narayanamurthyc3d22c42018-01-22 18:39:02 -08001/* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
David Collins8885f792017-01-26 14:36:34 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/spmi.h>
16#include <linux/platform_device.h>
17#include <linux/regmap.h>
18#include <linux/err.h>
19#include <linux/qpnp/qpnp-revid.h>
20#include <linux/of.h>
21
22#define REVID_REVISION1 0x0
23#define REVID_REVISION2 0x1
24#define REVID_REVISION3 0x2
25#define REVID_REVISION4 0x3
26#define REVID_TYPE 0x4
27#define REVID_SUBTYPE 0x5
28#define REVID_STATUS1 0x8
29#define REVID_SPARE_0 0x60
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +053030#define REVID_TP_REV 0xf1
David Collins8885f792017-01-26 14:36:34 -080031#define REVID_FAB_ID 0xf2
32
33#define QPNP_REVID_DEV_NAME "qcom,qpnp-revid"
34
35static const char *const pmic_names[] = {
36 [0] = "Unknown PMIC",
37 [PM8941_SUBTYPE] = "PM8941",
38 [PM8841_SUBTYPE] = "PM8841",
39 [PM8019_SUBTYPE] = "PM8019",
40 [PM8226_SUBTYPE] = "PM8226",
41 [PM8110_SUBTYPE] = "PM8110",
42 [PMA8084_SUBTYPE] = "PMA8084",
43 [PMI8962_SUBTYPE] = "PMI8962",
44 [PMD9635_SUBTYPE] = "PMD9635",
45 [PM8994_SUBTYPE] = "PM8994",
46 [PMI8994_SUBTYPE] = "PMI8994",
47 [PM8916_SUBTYPE] = "PM8916",
48 [PM8004_SUBTYPE] = "PM8004",
49 [PM8909_SUBTYPE] = "PM8909",
50 [PM2433_SUBTYPE] = "PM2433",
51 [PMD9655_SUBTYPE] = "PMD9655",
52 [PM8950_SUBTYPE] = "PM8950",
53 [PMI8950_SUBTYPE] = "PMI8950",
54 [PMK8001_SUBTYPE] = "PMK8001",
55 [PMI8996_SUBTYPE] = "PMI8996",
56 [PM8998_SUBTYPE] = "PM8998",
57 [PMI8998_SUBTYPE] = "PMI8998",
58 [PM8005_SUBTYPE] = "PM8005",
59 [PM8937_SUBTYPE] = "PM8937",
60 [PM660L_SUBTYPE] = "PM660L",
61 [PM660_SUBTYPE] = "PM660",
Kiran Gunda8c383ac2017-12-07 11:16:12 +053062 [PMI632_SUBTYPE] = "PMI632",
David Collins8885f792017-01-26 14:36:34 -080063 [PMI8937_SUBTYPE] = "PMI8937",
Subbaraman Narayanamurthyc3d22c42018-01-22 18:39:02 -080064 [PM855_SUBTYPE] = "PM855",
65 [PM855B_SUBTYPE] = "PM855B",
66 [PM855L_SUBTYPE] = "PM855L",
David Collins8885f792017-01-26 14:36:34 -080067};
68
69struct revid_chip {
70 struct list_head link;
71 struct device_node *dev_node;
72 struct pmic_revid_data data;
73};
74
75static LIST_HEAD(revid_chips);
76static DEFINE_MUTEX(revid_chips_lock);
77
78static const struct of_device_id qpnp_revid_match_table[] = {
79 { .compatible = QPNP_REVID_DEV_NAME },
80 {}
81};
82
83static u8 qpnp_read_byte(struct regmap *regmap, u16 addr)
84{
85 int rc;
86 int val;
87
88 rc = regmap_read(regmap, addr, &val);
89 if (rc) {
90 pr_err("read failed rc=%d\n", rc);
91 return 0;
92 }
93 return (u8)val;
94}
95
96/**
97 * get_revid_data - Return the revision information of PMIC
98 * @dev_node: Pointer to the revid peripheral of the PMIC for which
99 * revision information is seeked
100 *
101 * CONTEXT: Should be called in non atomic context
102 *
103 * RETURNS: pointer to struct pmic_revid_data filled with the information
104 * about the PMIC revision
105 */
106struct pmic_revid_data *get_revid_data(struct device_node *dev_node)
107{
108 struct revid_chip *revid_chip;
109
110 if (!dev_node)
111 return ERR_PTR(-EINVAL);
112
113 mutex_lock(&revid_chips_lock);
114 list_for_each_entry(revid_chip, &revid_chips, link) {
115 if (dev_node == revid_chip->dev_node) {
116 mutex_unlock(&revid_chips_lock);
117 return &revid_chip->data;
118 }
119 }
120 mutex_unlock(&revid_chips_lock);
121 return ERR_PTR(-EINVAL);
122}
123EXPORT_SYMBOL(get_revid_data);
124
125#define PM8941_PERIPHERAL_SUBTYPE 0x01
126#define PM8226_PERIPHERAL_SUBTYPE 0x04
127#define PMD9655_PERIPHERAL_SUBTYPE 0x0F
128#define PMI8950_PERIPHERAL_SUBTYPE 0x11
129#define PMI8937_PERIPHERAL_SUBTYPE 0x37
130static size_t build_pmic_string(char *buf, size_t n, int sid,
131 u8 subtype, u8 rev1, u8 rev2, u8 rev3, u8 rev4)
132{
133 size_t pos = 0;
134 /*
135 * In early versions of PM8941 and PM8226, the major revision number
136 * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
137 * Increment the major revision number here if the chip is an early
138 * version of PM8941 or PM8226.
139 */
140 if (((int)subtype == PM8941_PERIPHERAL_SUBTYPE
141 || (int)subtype == PM8226_PERIPHERAL_SUBTYPE)
142 && rev4 < 0x02)
143 rev4++;
144
145 pos += snprintf(buf + pos, n - pos, "PMIC@SID%d", sid);
146 if (subtype >= ARRAY_SIZE(pmic_names) || subtype == 0)
147 pos += snprintf(buf + pos, n - pos, ": %s (subtype: 0x%02X)",
148 pmic_names[0], subtype);
149 else
150 pos += snprintf(buf + pos, n - pos, ": %s",
151 pmic_names[subtype]);
152 pos += snprintf(buf + pos, n - pos, " v%d.%d", rev4, rev3);
153 if (rev2 || rev1)
154 pos += snprintf(buf + pos, n - pos, ".%d", rev2);
155 if (rev1)
156 pos += snprintf(buf + pos, n - pos, ".%d", rev1);
157 return pos;
158}
159
160#define PMIC_PERIPHERAL_TYPE 0x51
161#define PMIC_STRING_MAXLENGTH 80
162static int qpnp_revid_probe(struct platform_device *pdev)
163{
164 u8 rev1, rev2, rev3, rev4, pmic_type, pmic_subtype, pmic_status;
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530165 u8 option1, option2, option3, option4, spare0;
David Collins8885f792017-01-26 14:36:34 -0800166 unsigned int base;
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530167 int rc, fab_id, tp_rev;
David Collins8885f792017-01-26 14:36:34 -0800168 char pmic_string[PMIC_STRING_MAXLENGTH] = {'\0'};
169 struct revid_chip *revid_chip;
170 struct regmap *regmap;
171
172 regmap = dev_get_regmap(pdev->dev.parent, NULL);
173 if (!regmap) {
174 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
175 return -EINVAL;
176 }
177
178 rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
179 if (rc < 0) {
180 dev_err(&pdev->dev,
181 "Couldn't find reg in node = %s rc = %d\n",
182 pdev->dev.of_node->full_name, rc);
183 return rc;
184 }
185 pmic_type = qpnp_read_byte(regmap, base + REVID_TYPE);
186 if (pmic_type != PMIC_PERIPHERAL_TYPE) {
187 pr_err("Invalid REVID peripheral type: %02X\n", pmic_type);
188 return -EINVAL;
189 }
190
191 rev1 = qpnp_read_byte(regmap, base + REVID_REVISION1);
192 rev2 = qpnp_read_byte(regmap, base + REVID_REVISION2);
193 rev3 = qpnp_read_byte(regmap, base + REVID_REVISION3);
194 rev4 = qpnp_read_byte(regmap, base + REVID_REVISION4);
195
196 pmic_subtype = qpnp_read_byte(regmap, base + REVID_SUBTYPE);
197 if (pmic_subtype != PMD9655_PERIPHERAL_SUBTYPE)
198 pmic_status = qpnp_read_byte(regmap, base + REVID_STATUS1);
199 else
200 pmic_status = 0;
201
202 /* special case for PMI8937 */
203 if (pmic_subtype == PMI8950_PERIPHERAL_SUBTYPE) {
204 /* read spare register */
205 spare0 = qpnp_read_byte(regmap, base + REVID_SPARE_0);
206 if (spare0)
207 pmic_subtype = PMI8937_PERIPHERAL_SUBTYPE;
208 }
209
210 if (of_property_read_bool(pdev->dev.of_node, "qcom,fab-id-valid"))
211 fab_id = qpnp_read_byte(regmap, base + REVID_FAB_ID);
212 else
213 fab_id = -EINVAL;
214
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530215 if (of_property_read_bool(pdev->dev.of_node, "qcom,tp-rev-valid"))
216 tp_rev = qpnp_read_byte(regmap, base + REVID_TP_REV);
217 else
218 tp_rev = -EINVAL;
219
David Collins8885f792017-01-26 14:36:34 -0800220 revid_chip = devm_kzalloc(&pdev->dev, sizeof(struct revid_chip),
221 GFP_KERNEL);
222 if (!revid_chip)
223 return -ENOMEM;
224
225 revid_chip->dev_node = pdev->dev.of_node;
226 revid_chip->data.rev1 = rev1;
227 revid_chip->data.rev2 = rev2;
228 revid_chip->data.rev3 = rev3;
229 revid_chip->data.rev4 = rev4;
230 revid_chip->data.pmic_subtype = pmic_subtype;
231 revid_chip->data.pmic_type = pmic_type;
232 revid_chip->data.fab_id = fab_id;
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530233 revid_chip->data.tp_rev = tp_rev;
David Collins8885f792017-01-26 14:36:34 -0800234
235 if (pmic_subtype < ARRAY_SIZE(pmic_names))
236 revid_chip->data.pmic_name = pmic_names[pmic_subtype];
237 else
238 revid_chip->data.pmic_name = pmic_names[0];
239
240 mutex_lock(&revid_chips_lock);
241 list_add(&revid_chip->link, &revid_chips);
242 mutex_unlock(&revid_chips_lock);
243
244 option1 = pmic_status & 0x3;
245 option2 = (pmic_status >> 2) & 0x3;
246 option3 = (pmic_status >> 4) & 0x3;
247 option4 = (pmic_status >> 6) & 0x3;
248
249 build_pmic_string(pmic_string, PMIC_STRING_MAXLENGTH,
250 to_spmi_device(pdev->dev.parent)->usid,
251 pmic_subtype, rev1, rev2, rev3, rev4);
252 pr_info("%s options: %d, %d, %d, %d\n",
253 pmic_string, option1, option2, option3, option4);
254 return 0;
255}
256
257static struct platform_driver qpnp_revid_driver = {
258 .probe = qpnp_revid_probe,
259 .driver = {
260 .name = QPNP_REVID_DEV_NAME,
261 .owner = THIS_MODULE,
262 .of_match_table = qpnp_revid_match_table,
263 },
264};
265
266static int __init qpnp_revid_init(void)
267{
268 return platform_driver_register(&qpnp_revid_driver);
269}
270
271static void __exit qpnp_revid_exit(void)
272{
273 return platform_driver_unregister(&qpnp_revid_driver);
274}
275
276subsys_initcall(qpnp_revid_init);
277module_exit(qpnp_revid_exit);
278
279MODULE_DESCRIPTION("QPNP REVID DRIVER");
280MODULE_LICENSE("GPL v2");
281MODULE_ALIAS("platform:" QPNP_REVID_DEV_NAME);