blob: 684aec81eb0c0298b5f821b64dd26d8142c50551 [file] [log] [blame]
David Collins8885f792017-01-26 14:36:34 -08001/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
2 *
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",
64};
65
66struct revid_chip {
67 struct list_head link;
68 struct device_node *dev_node;
69 struct pmic_revid_data data;
70};
71
72static LIST_HEAD(revid_chips);
73static DEFINE_MUTEX(revid_chips_lock);
74
75static const struct of_device_id qpnp_revid_match_table[] = {
76 { .compatible = QPNP_REVID_DEV_NAME },
77 {}
78};
79
80static u8 qpnp_read_byte(struct regmap *regmap, u16 addr)
81{
82 int rc;
83 int val;
84
85 rc = regmap_read(regmap, addr, &val);
86 if (rc) {
87 pr_err("read failed rc=%d\n", rc);
88 return 0;
89 }
90 return (u8)val;
91}
92
93/**
94 * get_revid_data - Return the revision information of PMIC
95 * @dev_node: Pointer to the revid peripheral of the PMIC for which
96 * revision information is seeked
97 *
98 * CONTEXT: Should be called in non atomic context
99 *
100 * RETURNS: pointer to struct pmic_revid_data filled with the information
101 * about the PMIC revision
102 */
103struct pmic_revid_data *get_revid_data(struct device_node *dev_node)
104{
105 struct revid_chip *revid_chip;
106
107 if (!dev_node)
108 return ERR_PTR(-EINVAL);
109
110 mutex_lock(&revid_chips_lock);
111 list_for_each_entry(revid_chip, &revid_chips, link) {
112 if (dev_node == revid_chip->dev_node) {
113 mutex_unlock(&revid_chips_lock);
114 return &revid_chip->data;
115 }
116 }
117 mutex_unlock(&revid_chips_lock);
118 return ERR_PTR(-EINVAL);
119}
120EXPORT_SYMBOL(get_revid_data);
121
122#define PM8941_PERIPHERAL_SUBTYPE 0x01
123#define PM8226_PERIPHERAL_SUBTYPE 0x04
124#define PMD9655_PERIPHERAL_SUBTYPE 0x0F
125#define PMI8950_PERIPHERAL_SUBTYPE 0x11
126#define PMI8937_PERIPHERAL_SUBTYPE 0x37
127static size_t build_pmic_string(char *buf, size_t n, int sid,
128 u8 subtype, u8 rev1, u8 rev2, u8 rev3, u8 rev4)
129{
130 size_t pos = 0;
131 /*
132 * In early versions of PM8941 and PM8226, the major revision number
133 * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
134 * Increment the major revision number here if the chip is an early
135 * version of PM8941 or PM8226.
136 */
137 if (((int)subtype == PM8941_PERIPHERAL_SUBTYPE
138 || (int)subtype == PM8226_PERIPHERAL_SUBTYPE)
139 && rev4 < 0x02)
140 rev4++;
141
142 pos += snprintf(buf + pos, n - pos, "PMIC@SID%d", sid);
143 if (subtype >= ARRAY_SIZE(pmic_names) || subtype == 0)
144 pos += snprintf(buf + pos, n - pos, ": %s (subtype: 0x%02X)",
145 pmic_names[0], subtype);
146 else
147 pos += snprintf(buf + pos, n - pos, ": %s",
148 pmic_names[subtype]);
149 pos += snprintf(buf + pos, n - pos, " v%d.%d", rev4, rev3);
150 if (rev2 || rev1)
151 pos += snprintf(buf + pos, n - pos, ".%d", rev2);
152 if (rev1)
153 pos += snprintf(buf + pos, n - pos, ".%d", rev1);
154 return pos;
155}
156
157#define PMIC_PERIPHERAL_TYPE 0x51
158#define PMIC_STRING_MAXLENGTH 80
159static int qpnp_revid_probe(struct platform_device *pdev)
160{
161 u8 rev1, rev2, rev3, rev4, pmic_type, pmic_subtype, pmic_status;
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530162 u8 option1, option2, option3, option4, spare0;
David Collins8885f792017-01-26 14:36:34 -0800163 unsigned int base;
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530164 int rc, fab_id, tp_rev;
David Collins8885f792017-01-26 14:36:34 -0800165 char pmic_string[PMIC_STRING_MAXLENGTH] = {'\0'};
166 struct revid_chip *revid_chip;
167 struct regmap *regmap;
168
169 regmap = dev_get_regmap(pdev->dev.parent, NULL);
170 if (!regmap) {
171 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
172 return -EINVAL;
173 }
174
175 rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
176 if (rc < 0) {
177 dev_err(&pdev->dev,
178 "Couldn't find reg in node = %s rc = %d\n",
179 pdev->dev.of_node->full_name, rc);
180 return rc;
181 }
182 pmic_type = qpnp_read_byte(regmap, base + REVID_TYPE);
183 if (pmic_type != PMIC_PERIPHERAL_TYPE) {
184 pr_err("Invalid REVID peripheral type: %02X\n", pmic_type);
185 return -EINVAL;
186 }
187
188 rev1 = qpnp_read_byte(regmap, base + REVID_REVISION1);
189 rev2 = qpnp_read_byte(regmap, base + REVID_REVISION2);
190 rev3 = qpnp_read_byte(regmap, base + REVID_REVISION3);
191 rev4 = qpnp_read_byte(regmap, base + REVID_REVISION4);
192
193 pmic_subtype = qpnp_read_byte(regmap, base + REVID_SUBTYPE);
194 if (pmic_subtype != PMD9655_PERIPHERAL_SUBTYPE)
195 pmic_status = qpnp_read_byte(regmap, base + REVID_STATUS1);
196 else
197 pmic_status = 0;
198
199 /* special case for PMI8937 */
200 if (pmic_subtype == PMI8950_PERIPHERAL_SUBTYPE) {
201 /* read spare register */
202 spare0 = qpnp_read_byte(regmap, base + REVID_SPARE_0);
203 if (spare0)
204 pmic_subtype = PMI8937_PERIPHERAL_SUBTYPE;
205 }
206
207 if (of_property_read_bool(pdev->dev.of_node, "qcom,fab-id-valid"))
208 fab_id = qpnp_read_byte(regmap, base + REVID_FAB_ID);
209 else
210 fab_id = -EINVAL;
211
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530212 if (of_property_read_bool(pdev->dev.of_node, "qcom,tp-rev-valid"))
213 tp_rev = qpnp_read_byte(regmap, base + REVID_TP_REV);
214 else
215 tp_rev = -EINVAL;
216
David Collins8885f792017-01-26 14:36:34 -0800217 revid_chip = devm_kzalloc(&pdev->dev, sizeof(struct revid_chip),
218 GFP_KERNEL);
219 if (!revid_chip)
220 return -ENOMEM;
221
222 revid_chip->dev_node = pdev->dev.of_node;
223 revid_chip->data.rev1 = rev1;
224 revid_chip->data.rev2 = rev2;
225 revid_chip->data.rev3 = rev3;
226 revid_chip->data.rev4 = rev4;
227 revid_chip->data.pmic_subtype = pmic_subtype;
228 revid_chip->data.pmic_type = pmic_type;
229 revid_chip->data.fab_id = fab_id;
Rama Krishna Phani A379c70e2017-07-30 19:04:31 +0530230 revid_chip->data.tp_rev = tp_rev;
David Collins8885f792017-01-26 14:36:34 -0800231
232 if (pmic_subtype < ARRAY_SIZE(pmic_names))
233 revid_chip->data.pmic_name = pmic_names[pmic_subtype];
234 else
235 revid_chip->data.pmic_name = pmic_names[0];
236
237 mutex_lock(&revid_chips_lock);
238 list_add(&revid_chip->link, &revid_chips);
239 mutex_unlock(&revid_chips_lock);
240
241 option1 = pmic_status & 0x3;
242 option2 = (pmic_status >> 2) & 0x3;
243 option3 = (pmic_status >> 4) & 0x3;
244 option4 = (pmic_status >> 6) & 0x3;
245
246 build_pmic_string(pmic_string, PMIC_STRING_MAXLENGTH,
247 to_spmi_device(pdev->dev.parent)->usid,
248 pmic_subtype, rev1, rev2, rev3, rev4);
249 pr_info("%s options: %d, %d, %d, %d\n",
250 pmic_string, option1, option2, option3, option4);
251 return 0;
252}
253
254static struct platform_driver qpnp_revid_driver = {
255 .probe = qpnp_revid_probe,
256 .driver = {
257 .name = QPNP_REVID_DEV_NAME,
258 .owner = THIS_MODULE,
259 .of_match_table = qpnp_revid_match_table,
260 },
261};
262
263static int __init qpnp_revid_init(void)
264{
265 return platform_driver_register(&qpnp_revid_driver);
266}
267
268static void __exit qpnp_revid_exit(void)
269{
270 return platform_driver_unregister(&qpnp_revid_driver);
271}
272
273subsys_initcall(qpnp_revid_init);
274module_exit(qpnp_revid_exit);
275
276MODULE_DESCRIPTION("QPNP REVID DRIVER");
277MODULE_LICENSE("GPL v2");
278MODULE_ALIAS("platform:" QPNP_REVID_DEV_NAME);