xref: /linux/drivers/soc/qcom/socinfo.c (revision de320c07da3d0e464d0e029c90ae05e810454afe)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2017-2019, Linaro Ltd.
5  */
6 
7 #include <linux/debugfs.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/random.h>
12 #include <linux/slab.h>
13 #include <linux/soc/qcom/smem.h>
14 #include <linux/string.h>
15 #include <linux/stringify.h>
16 #include <linux/sys_soc.h>
17 #include <linux/types.h>
18 
19 #include <asm/unaligned.h>
20 
21 #include <dt-bindings/arm/qcom,ids.h>
22 
23 /*
24  * SoC version type with major number in the upper 16 bits and minor
25  * number in the lower 16 bits.
26  */
27 #define SOCINFO_MAJOR(ver) (((ver) >> 16) & 0xffff)
28 #define SOCINFO_MINOR(ver) ((ver) & 0xffff)
29 #define SOCINFO_VERSION(maj, min)  ((((maj) & 0xffff) << 16)|((min) & 0xffff))
30 
31 /* Helper macros to create soc_id table */
32 #define qcom_board_id(id) QCOM_ID_ ## id, __stringify(id)
33 #define qcom_board_id_named(id, name) QCOM_ID_ ## id, (name)
34 
35 #define SMEM_SOCINFO_BUILD_ID_LENGTH           32
36 #define SMEM_SOCINFO_CHIP_ID_LENGTH            32
37 
38 /*
39  * SMEM item id, used to acquire handles to respective
40  * SMEM region.
41  */
42 #define SMEM_HW_SW_BUILD_ID            137
43 
44 #ifdef CONFIG_DEBUG_FS
45 #define SMEM_IMAGE_VERSION_BLOCKS_COUNT        32
46 #define SMEM_IMAGE_VERSION_SIZE                4096
47 #define SMEM_IMAGE_VERSION_NAME_SIZE           75
48 #define SMEM_IMAGE_VERSION_VARIANT_SIZE        20
49 #define SMEM_IMAGE_VERSION_OEM_SIZE            32
50 
51 /*
52  * SMEM Image table indices
53  */
54 #define SMEM_IMAGE_TABLE_BOOT_INDEX     0
55 #define SMEM_IMAGE_TABLE_TZ_INDEX       1
56 #define SMEM_IMAGE_TABLE_RPM_INDEX      3
57 #define SMEM_IMAGE_TABLE_APPS_INDEX     10
58 #define SMEM_IMAGE_TABLE_MPSS_INDEX     11
59 #define SMEM_IMAGE_TABLE_ADSP_INDEX     12
60 #define SMEM_IMAGE_TABLE_CNSS_INDEX     13
61 #define SMEM_IMAGE_TABLE_VIDEO_INDEX    14
62 #define SMEM_IMAGE_VERSION_TABLE       469
63 
64 /*
65  * SMEM Image table names
66  */
67 static const char *const socinfo_image_names[] = {
68 	[SMEM_IMAGE_TABLE_ADSP_INDEX] = "adsp",
69 	[SMEM_IMAGE_TABLE_APPS_INDEX] = "apps",
70 	[SMEM_IMAGE_TABLE_BOOT_INDEX] = "boot",
71 	[SMEM_IMAGE_TABLE_CNSS_INDEX] = "cnss",
72 	[SMEM_IMAGE_TABLE_MPSS_INDEX] = "mpss",
73 	[SMEM_IMAGE_TABLE_RPM_INDEX] = "rpm",
74 	[SMEM_IMAGE_TABLE_TZ_INDEX] = "tz",
75 	[SMEM_IMAGE_TABLE_VIDEO_INDEX] = "video",
76 };
77 
78 static const char *const pmic_models[] = {
79 	[0]  = "Unknown PMIC model",
80 	[1]  = "PM8941",
81 	[2]  = "PM8841",
82 	[3]  = "PM8019",
83 	[4]  = "PM8226",
84 	[5]  = "PM8110",
85 	[6]  = "PMA8084",
86 	[7]  = "PMI8962",
87 	[8]  = "PMD9635",
88 	[9]  = "PM8994",
89 	[10] = "PMI8994",
90 	[11] = "PM8916",
91 	[12] = "PM8004",
92 	[13] = "PM8909/PM8058",
93 	[14] = "PM8028",
94 	[15] = "PM8901",
95 	[16] = "PM8950/PM8027",
96 	[17] = "PMI8950/ISL9519",
97 	[18] = "PMK8001/PM8921",
98 	[19] = "PMI8996/PM8018",
99 	[20] = "PM8998/PM8015",
100 	[21] = "PMI8998/PM8014",
101 	[22] = "PM8821",
102 	[23] = "PM8038",
103 	[24] = "PM8005/PM8922",
104 	[25] = "PM8917",
105 	[26] = "PM660L",
106 	[27] = "PM660",
107 	[30] = "PM8150",
108 	[31] = "PM8150L",
109 	[32] = "PM8150B",
110 	[33] = "PMK8002",
111 	[36] = "PM8009",
112 	[38] = "PM8150C",
113 	[41] = "SMB2351",
114 	[45] = "PM6125",
115 	[47] = "PMK8350",
116 	[48] = "PM8350",
117 	[49] = "PM8350C",
118 	[50] = "PM8350B",
119 	[51] = "PMR735A",
120 	[52] = "PMR735B",
121 	[58] = "PM8450",
122 	[65] = "PM8010",
123 };
124 #endif /* CONFIG_DEBUG_FS */
125 
126 /* Socinfo SMEM item structure */
127 struct socinfo {
128 	__le32 fmt;
129 	__le32 id;
130 	__le32 ver;
131 	char build_id[SMEM_SOCINFO_BUILD_ID_LENGTH];
132 	/* Version 2 */
133 	__le32 raw_id;
134 	__le32 raw_ver;
135 	/* Version 3 */
136 	__le32 hw_plat;
137 	/* Version 4 */
138 	__le32 plat_ver;
139 	/* Version 5 */
140 	__le32 accessory_chip;
141 	/* Version 6 */
142 	__le32 hw_plat_subtype;
143 	/* Version 7 */
144 	__le32 pmic_model;
145 	__le32 pmic_die_rev;
146 	/* Version 8 */
147 	__le32 pmic_model_1;
148 	__le32 pmic_die_rev_1;
149 	__le32 pmic_model_2;
150 	__le32 pmic_die_rev_2;
151 	/* Version 9 */
152 	__le32 foundry_id;
153 	/* Version 10 */
154 	__le32 serial_num;
155 	/* Version 11 */
156 	__le32 num_pmics;
157 	__le32 pmic_array_offset;
158 	/* Version 12 */
159 	__le32 chip_family;
160 	__le32 raw_device_family;
161 	__le32 raw_device_num;
162 	/* Version 13 */
163 	__le32 nproduct_id;
164 	char chip_id[SMEM_SOCINFO_CHIP_ID_LENGTH];
165 	/* Version 14 */
166 	__le32 num_clusters;
167 	__le32 ncluster_array_offset;
168 	__le32 num_defective_parts;
169 	__le32 ndefective_parts_array_offset;
170 	/* Version 15 */
171 	__le32 nmodem_supported;
172 };
173 
174 #ifdef CONFIG_DEBUG_FS
175 struct socinfo_params {
176 	u32 raw_device_family;
177 	u32 hw_plat_subtype;
178 	u32 accessory_chip;
179 	u32 raw_device_num;
180 	u32 chip_family;
181 	u32 foundry_id;
182 	u32 plat_ver;
183 	u32 raw_ver;
184 	u32 hw_plat;
185 	u32 fmt;
186 	u32 nproduct_id;
187 	u32 num_clusters;
188 	u32 ncluster_array_offset;
189 	u32 num_defective_parts;
190 	u32 ndefective_parts_array_offset;
191 	u32 nmodem_supported;
192 };
193 
194 struct smem_image_version {
195 	char name[SMEM_IMAGE_VERSION_NAME_SIZE];
196 	char variant[SMEM_IMAGE_VERSION_VARIANT_SIZE];
197 	char pad;
198 	char oem[SMEM_IMAGE_VERSION_OEM_SIZE];
199 };
200 #endif /* CONFIG_DEBUG_FS */
201 
202 struct qcom_socinfo {
203 	struct soc_device *soc_dev;
204 	struct soc_device_attribute attr;
205 #ifdef CONFIG_DEBUG_FS
206 	struct dentry *dbg_root;
207 	struct socinfo_params info;
208 #endif /* CONFIG_DEBUG_FS */
209 };
210 
211 struct soc_id {
212 	unsigned int id;
213 	const char *name;
214 };
215 
216 static const struct soc_id soc_id[] = {
217 	{ qcom_board_id(MSM8960) },
218 	{ qcom_board_id(APQ8064) },
219 	{ qcom_board_id(MSM8660A) },
220 	{ qcom_board_id(MSM8260A) },
221 	{ qcom_board_id(APQ8060A) },
222 	{ qcom_board_id(MSM8974) },
223 	{ qcom_board_id(MPQ8064) },
224 	{ qcom_board_id(MSM8960AB) },
225 	{ qcom_board_id(APQ8060AB) },
226 	{ qcom_board_id(MSM8260AB) },
227 	{ qcom_board_id(MSM8660AB) },
228 	{ qcom_board_id(MSM8626) },
229 	{ qcom_board_id(MSM8610) },
230 	{ qcom_board_id(APQ8064AB) },
231 	{ qcom_board_id(MSM8226) },
232 	{ qcom_board_id(MSM8526) },
233 	{ qcom_board_id(MSM8110) },
234 	{ qcom_board_id(MSM8210) },
235 	{ qcom_board_id(MSM8810) },
236 	{ qcom_board_id(MSM8212) },
237 	{ qcom_board_id(MSM8612) },
238 	{ qcom_board_id(MSM8112) },
239 	{ qcom_board_id(MSM8225Q) },
240 	{ qcom_board_id(MSM8625Q) },
241 	{ qcom_board_id(MSM8125Q) },
242 	{ qcom_board_id(APQ8064AA) },
243 	{ qcom_board_id(APQ8084) },
244 	{ qcom_board_id(APQ8074) },
245 	{ qcom_board_id(MSM8274) },
246 	{ qcom_board_id(MSM8674) },
247 	{ qcom_board_id_named(MSM8974PRO_AC, "MSM8974PRO-AC") },
248 	{ qcom_board_id(MSM8126) },
249 	{ qcom_board_id(APQ8026) },
250 	{ qcom_board_id(MSM8926) },
251 	{ qcom_board_id(MSM8326) },
252 	{ qcom_board_id(MSM8916) },
253 	{ qcom_board_id(MSM8956) },
254 	{ qcom_board_id(MSM8976) },
255 	{ qcom_board_id(MSM8994) },
256 	{ qcom_board_id_named(APQ8074PRO_AA, "APQ8074PRO-AA") },
257 	{ qcom_board_id_named(APQ8074PRO_AB, "APQ8074PRO-AB") },
258 	{ qcom_board_id_named(APQ8074PRO_AC, "APQ8074PRO-AC") },
259 	{ qcom_board_id_named(MSM8274PRO_AA, "MSM8274PRO-AA") },
260 	{ qcom_board_id_named(MSM8274PRO_AB, "MSM8274PRO-AB") },
261 	{ qcom_board_id_named(MSM8274PRO_AC, "MSM8274PRO-AC") },
262 	{ qcom_board_id_named(MSM8674PRO_AA, "MSM8674PRO-AA") },
263 	{ qcom_board_id_named(MSM8674PRO_AB, "MSM8674PRO-AB") },
264 	{ qcom_board_id_named(MSM8674PRO_AC, "MSM8674PRO-AC") },
265 	{ qcom_board_id_named(MSM8974PRO_AA, "MSM8974PRO-AA") },
266 	{ qcom_board_id_named(MSM8974PRO_AB, "MSM8974PRO-AB") },
267 	{ qcom_board_id(APQ8028) },
268 	{ qcom_board_id(MSM8128) },
269 	{ qcom_board_id(MSM8228) },
270 	{ qcom_board_id(MSM8528) },
271 	{ qcom_board_id(MSM8628) },
272 	{ qcom_board_id(MSM8928) },
273 	{ qcom_board_id(MSM8510) },
274 	{ qcom_board_id(MSM8512) },
275 	{ qcom_board_id(MSM8936) },
276 	{ qcom_board_id(MSM8939) },
277 	{ qcom_board_id(APQ8036) },
278 	{ qcom_board_id(APQ8039) },
279 	{ qcom_board_id(MSM8996) },
280 	{ qcom_board_id(APQ8016) },
281 	{ qcom_board_id(MSM8216) },
282 	{ qcom_board_id(MSM8116) },
283 	{ qcom_board_id(MSM8616) },
284 	{ qcom_board_id(MSM8992) },
285 	{ qcom_board_id(APQ8094) },
286 	{ qcom_board_id(MDM9607) },
287 	{ qcom_board_id(APQ8096) },
288 	{ qcom_board_id(MSM8998) },
289 	{ qcom_board_id(MSM8953) },
290 	{ qcom_board_id(MDM8207) },
291 	{ qcom_board_id(MDM9207) },
292 	{ qcom_board_id(MDM9307) },
293 	{ qcom_board_id(MDM9628) },
294 	{ qcom_board_id(APQ8053) },
295 	{ qcom_board_id(MSM8996SG) },
296 	{ qcom_board_id(MSM8996AU) },
297 	{ qcom_board_id(APQ8096AU) },
298 	{ qcom_board_id(APQ8096SG) },
299 	{ qcom_board_id(SDM660) },
300 	{ qcom_board_id(SDM630) },
301 	{ qcom_board_id(APQ8098) },
302 	{ qcom_board_id(SDM845) },
303 	{ qcom_board_id(MDM9206) },
304 	{ qcom_board_id(IPQ8074) },
305 	{ qcom_board_id(SDA660) },
306 	{ qcom_board_id(SDM658) },
307 	{ qcom_board_id(SDA658) },
308 	{ qcom_board_id(SDA630) },
309 	{ qcom_board_id(SDM450) },
310 	{ qcom_board_id(SDA845) },
311 	{ qcom_board_id(IPQ8072) },
312 	{ qcom_board_id(IPQ8076) },
313 	{ qcom_board_id(IPQ8078) },
314 	{ qcom_board_id(SDM636) },
315 	{ qcom_board_id(SDA636) },
316 	{ qcom_board_id(SDM632) },
317 	{ qcom_board_id(SDA632) },
318 	{ qcom_board_id(SDA450) },
319 	{ qcom_board_id(SM8250) },
320 	{ qcom_board_id(IPQ8070) },
321 	{ qcom_board_id(IPQ8071) },
322 	{ qcom_board_id(IPQ8072A) },
323 	{ qcom_board_id(IPQ8074A) },
324 	{ qcom_board_id(IPQ8076A) },
325 	{ qcom_board_id(IPQ8078A) },
326 	{ qcom_board_id(SM6125) },
327 	{ qcom_board_id(IPQ8070A) },
328 	{ qcom_board_id(IPQ8071A) },
329 	{ qcom_board_id(IPQ6018) },
330 	{ qcom_board_id(IPQ6028) },
331 	{ qcom_board_id(IPQ6000) },
332 	{ qcom_board_id(IPQ6010) },
333 	{ qcom_board_id(SC7180) },
334 	{ qcom_board_id(SM6350) },
335 	{ qcom_board_id(SM8350) },
336 	{ qcom_board_id(SC8280XP) },
337 	{ qcom_board_id(IPQ6005) },
338 	{ qcom_board_id(QRB5165) },
339 	{ qcom_board_id(SM8450) },
340 	{ qcom_board_id(SM7225) },
341 	{ qcom_board_id(SA8295P) },
342 	{ qcom_board_id(SA8540P) },
343 	{ qcom_board_id_named(SM8450_2, "SM8450") },
344 	{ qcom_board_id_named(SM8450_3, "SM8450") },
345 	{ qcom_board_id(SC7280) },
346 	{ qcom_board_id(SC7180P) },
347 	{ qcom_board_id(SM6375) },
348 	{ qcom_board_id(QRU1000) },
349 	{ qcom_board_id(QDU1000) },
350 	{ qcom_board_id(QDU1010) },
351 	{ qcom_board_id(QRU1032) },
352 	{ qcom_board_id(QRU1052) },
353 	{ qcom_board_id(QRU1062) },
354 };
355 
356 static const char *socinfo_machine(struct device *dev, unsigned int id)
357 {
358 	int idx;
359 
360 	for (idx = 0; idx < ARRAY_SIZE(soc_id); idx++) {
361 		if (soc_id[idx].id == id)
362 			return soc_id[idx].name;
363 	}
364 
365 	return NULL;
366 }
367 
368 #ifdef CONFIG_DEBUG_FS
369 
370 #define QCOM_OPEN(name, _func)						\
371 static int qcom_open_##name(struct inode *inode, struct file *file)	\
372 {									\
373 	return single_open(file, _func, inode->i_private);		\
374 }									\
375 									\
376 static const struct file_operations qcom_ ##name## _ops = {		\
377 	.open = qcom_open_##name,					\
378 	.read = seq_read,						\
379 	.llseek = seq_lseek,						\
380 	.release = single_release,					\
381 }
382 
383 #define DEBUGFS_ADD(info, name)						\
384 	debugfs_create_file(__stringify(name), 0444,			\
385 			    qcom_socinfo->dbg_root,			\
386 			    info, &qcom_ ##name## _ops)
387 
388 
389 static int qcom_show_build_id(struct seq_file *seq, void *p)
390 {
391 	struct socinfo *socinfo = seq->private;
392 
393 	seq_printf(seq, "%s\n", socinfo->build_id);
394 
395 	return 0;
396 }
397 
398 static int qcom_show_pmic_model(struct seq_file *seq, void *p)
399 {
400 	struct socinfo *socinfo = seq->private;
401 	int model = SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_model));
402 
403 	if (model < 0)
404 		return -EINVAL;
405 
406 	if (model < ARRAY_SIZE(pmic_models) && pmic_models[model])
407 		seq_printf(seq, "%s\n", pmic_models[model]);
408 	else
409 		seq_printf(seq, "unknown (%d)\n", model);
410 
411 	return 0;
412 }
413 
414 static int qcom_show_pmic_model_array(struct seq_file *seq, void *p)
415 {
416 	struct socinfo *socinfo = seq->private;
417 	unsigned int num_pmics = le32_to_cpu(socinfo->num_pmics);
418 	unsigned int pmic_array_offset = le32_to_cpu(socinfo->pmic_array_offset);
419 	int i;
420 	void *ptr = socinfo;
421 
422 	ptr += pmic_array_offset;
423 
424 	/* No need for bounds checking, it happened at socinfo_debugfs_init */
425 	for (i = 0; i < num_pmics; i++) {
426 		unsigned int model = SOCINFO_MINOR(get_unaligned_le32(ptr + 2 * i * sizeof(u32)));
427 		unsigned int die_rev = get_unaligned_le32(ptr + (2 * i + 1) * sizeof(u32));
428 
429 		if (model < ARRAY_SIZE(pmic_models) && pmic_models[model])
430 			seq_printf(seq, "%s %u.%u\n", pmic_models[model],
431 				   SOCINFO_MAJOR(die_rev),
432 				   SOCINFO_MINOR(die_rev));
433 		else
434 			seq_printf(seq, "unknown (%d)\n", model);
435 	}
436 
437 	return 0;
438 }
439 
440 static int qcom_show_pmic_die_revision(struct seq_file *seq, void *p)
441 {
442 	struct socinfo *socinfo = seq->private;
443 
444 	seq_printf(seq, "%u.%u\n",
445 		   SOCINFO_MAJOR(le32_to_cpu(socinfo->pmic_die_rev)),
446 		   SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_die_rev)));
447 
448 	return 0;
449 }
450 
451 static int qcom_show_chip_id(struct seq_file *seq, void *p)
452 {
453 	struct socinfo *socinfo = seq->private;
454 
455 	seq_printf(seq, "%s\n", socinfo->chip_id);
456 
457 	return 0;
458 }
459 
460 QCOM_OPEN(build_id, qcom_show_build_id);
461 QCOM_OPEN(pmic_model, qcom_show_pmic_model);
462 QCOM_OPEN(pmic_model_array, qcom_show_pmic_model_array);
463 QCOM_OPEN(pmic_die_rev, qcom_show_pmic_die_revision);
464 QCOM_OPEN(chip_id, qcom_show_chip_id);
465 
466 #define DEFINE_IMAGE_OPS(type)					\
467 static int show_image_##type(struct seq_file *seq, void *p)		  \
468 {								  \
469 	struct smem_image_version *image_version = seq->private;  \
470 	if (image_version->type[0] != '\0')			  \
471 		seq_printf(seq, "%s\n", image_version->type);	  \
472 	return 0;						  \
473 }								  \
474 static int open_image_##type(struct inode *inode, struct file *file)	  \
475 {									  \
476 	return single_open(file, show_image_##type, inode->i_private); \
477 }									  \
478 									  \
479 static const struct file_operations qcom_image_##type##_ops = {	  \
480 	.open = open_image_##type,					  \
481 	.read = seq_read,						  \
482 	.llseek = seq_lseek,						  \
483 	.release = single_release,					  \
484 }
485 
486 DEFINE_IMAGE_OPS(name);
487 DEFINE_IMAGE_OPS(variant);
488 DEFINE_IMAGE_OPS(oem);
489 
490 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
491 				 struct socinfo *info, size_t info_size)
492 {
493 	struct smem_image_version *versions;
494 	struct dentry *dentry;
495 	size_t size;
496 	int i;
497 	unsigned int num_pmics;
498 	unsigned int pmic_array_offset;
499 
500 	qcom_socinfo->dbg_root = debugfs_create_dir("qcom_socinfo", NULL);
501 
502 	qcom_socinfo->info.fmt = __le32_to_cpu(info->fmt);
503 
504 	debugfs_create_x32("info_fmt", 0444, qcom_socinfo->dbg_root,
505 			   &qcom_socinfo->info.fmt);
506 
507 	switch (qcom_socinfo->info.fmt) {
508 	case SOCINFO_VERSION(0, 15):
509 		qcom_socinfo->info.nmodem_supported = __le32_to_cpu(info->nmodem_supported);
510 
511 		debugfs_create_u32("nmodem_supported", 0444, qcom_socinfo->dbg_root,
512 				   &qcom_socinfo->info.nmodem_supported);
513 		fallthrough;
514 	case SOCINFO_VERSION(0, 14):
515 		qcom_socinfo->info.num_clusters = __le32_to_cpu(info->num_clusters);
516 		qcom_socinfo->info.ncluster_array_offset = __le32_to_cpu(info->ncluster_array_offset);
517 		qcom_socinfo->info.num_defective_parts = __le32_to_cpu(info->num_defective_parts);
518 		qcom_socinfo->info.ndefective_parts_array_offset = __le32_to_cpu(info->ndefective_parts_array_offset);
519 
520 		debugfs_create_u32("num_clusters", 0444, qcom_socinfo->dbg_root,
521 				   &qcom_socinfo->info.num_clusters);
522 		debugfs_create_u32("ncluster_array_offset", 0444, qcom_socinfo->dbg_root,
523 				   &qcom_socinfo->info.ncluster_array_offset);
524 		debugfs_create_u32("num_defective_parts", 0444, qcom_socinfo->dbg_root,
525 				   &qcom_socinfo->info.num_defective_parts);
526 		debugfs_create_u32("ndefective_parts_array_offset", 0444, qcom_socinfo->dbg_root,
527 				   &qcom_socinfo->info.ndefective_parts_array_offset);
528 		fallthrough;
529 	case SOCINFO_VERSION(0, 13):
530 		qcom_socinfo->info.nproduct_id = __le32_to_cpu(info->nproduct_id);
531 
532 		debugfs_create_u32("nproduct_id", 0444, qcom_socinfo->dbg_root,
533 				   &qcom_socinfo->info.nproduct_id);
534 		DEBUGFS_ADD(info, chip_id);
535 		fallthrough;
536 	case SOCINFO_VERSION(0, 12):
537 		qcom_socinfo->info.chip_family =
538 			__le32_to_cpu(info->chip_family);
539 		qcom_socinfo->info.raw_device_family =
540 			__le32_to_cpu(info->raw_device_family);
541 		qcom_socinfo->info.raw_device_num =
542 			__le32_to_cpu(info->raw_device_num);
543 
544 		debugfs_create_x32("chip_family", 0444, qcom_socinfo->dbg_root,
545 				   &qcom_socinfo->info.chip_family);
546 		debugfs_create_x32("raw_device_family", 0444,
547 				   qcom_socinfo->dbg_root,
548 				   &qcom_socinfo->info.raw_device_family);
549 		debugfs_create_x32("raw_device_number", 0444,
550 				   qcom_socinfo->dbg_root,
551 				   &qcom_socinfo->info.raw_device_num);
552 		fallthrough;
553 	case SOCINFO_VERSION(0, 11):
554 		num_pmics = le32_to_cpu(info->num_pmics);
555 		pmic_array_offset = le32_to_cpu(info->pmic_array_offset);
556 		if (pmic_array_offset + 2 * num_pmics * sizeof(u32) <= info_size)
557 			DEBUGFS_ADD(info, pmic_model_array);
558 		fallthrough;
559 	case SOCINFO_VERSION(0, 10):
560 	case SOCINFO_VERSION(0, 9):
561 		qcom_socinfo->info.foundry_id = __le32_to_cpu(info->foundry_id);
562 
563 		debugfs_create_u32("foundry_id", 0444, qcom_socinfo->dbg_root,
564 				   &qcom_socinfo->info.foundry_id);
565 		fallthrough;
566 	case SOCINFO_VERSION(0, 8):
567 	case SOCINFO_VERSION(0, 7):
568 		DEBUGFS_ADD(info, pmic_model);
569 		DEBUGFS_ADD(info, pmic_die_rev);
570 		fallthrough;
571 	case SOCINFO_VERSION(0, 6):
572 		qcom_socinfo->info.hw_plat_subtype =
573 			__le32_to_cpu(info->hw_plat_subtype);
574 
575 		debugfs_create_u32("hardware_platform_subtype", 0444,
576 				   qcom_socinfo->dbg_root,
577 				   &qcom_socinfo->info.hw_plat_subtype);
578 		fallthrough;
579 	case SOCINFO_VERSION(0, 5):
580 		qcom_socinfo->info.accessory_chip =
581 			__le32_to_cpu(info->accessory_chip);
582 
583 		debugfs_create_u32("accessory_chip", 0444,
584 				   qcom_socinfo->dbg_root,
585 				   &qcom_socinfo->info.accessory_chip);
586 		fallthrough;
587 	case SOCINFO_VERSION(0, 4):
588 		qcom_socinfo->info.plat_ver = __le32_to_cpu(info->plat_ver);
589 
590 		debugfs_create_u32("platform_version", 0444,
591 				   qcom_socinfo->dbg_root,
592 				   &qcom_socinfo->info.plat_ver);
593 		fallthrough;
594 	case SOCINFO_VERSION(0, 3):
595 		qcom_socinfo->info.hw_plat = __le32_to_cpu(info->hw_plat);
596 
597 		debugfs_create_u32("hardware_platform", 0444,
598 				   qcom_socinfo->dbg_root,
599 				   &qcom_socinfo->info.hw_plat);
600 		fallthrough;
601 	case SOCINFO_VERSION(0, 2):
602 		qcom_socinfo->info.raw_ver  = __le32_to_cpu(info->raw_ver);
603 
604 		debugfs_create_u32("raw_version", 0444, qcom_socinfo->dbg_root,
605 				   &qcom_socinfo->info.raw_ver);
606 		fallthrough;
607 	case SOCINFO_VERSION(0, 1):
608 		DEBUGFS_ADD(info, build_id);
609 		break;
610 	}
611 
612 	versions = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_IMAGE_VERSION_TABLE,
613 				 &size);
614 
615 	for (i = 0; i < ARRAY_SIZE(socinfo_image_names); i++) {
616 		if (!socinfo_image_names[i])
617 			continue;
618 
619 		dentry = debugfs_create_dir(socinfo_image_names[i],
620 					    qcom_socinfo->dbg_root);
621 		debugfs_create_file("name", 0444, dentry, &versions[i],
622 				    &qcom_image_name_ops);
623 		debugfs_create_file("variant", 0444, dentry, &versions[i],
624 				    &qcom_image_variant_ops);
625 		debugfs_create_file("oem", 0444, dentry, &versions[i],
626 				    &qcom_image_oem_ops);
627 	}
628 }
629 
630 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo)
631 {
632 	debugfs_remove_recursive(qcom_socinfo->dbg_root);
633 }
634 #else
635 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
636 				 struct socinfo *info, size_t info_size)
637 {
638 }
639 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo) {  }
640 #endif /* CONFIG_DEBUG_FS */
641 
642 static int qcom_socinfo_probe(struct platform_device *pdev)
643 {
644 	struct qcom_socinfo *qs;
645 	struct socinfo *info;
646 	size_t item_size;
647 
648 	info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HW_SW_BUILD_ID,
649 			      &item_size);
650 	if (IS_ERR(info)) {
651 		dev_err(&pdev->dev, "Couldn't find socinfo\n");
652 		return PTR_ERR(info);
653 	}
654 
655 	qs = devm_kzalloc(&pdev->dev, sizeof(*qs), GFP_KERNEL);
656 	if (!qs)
657 		return -ENOMEM;
658 
659 	qs->attr.family = "Snapdragon";
660 	qs->attr.machine = socinfo_machine(&pdev->dev,
661 					   le32_to_cpu(info->id));
662 	qs->attr.soc_id = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u",
663 					 le32_to_cpu(info->id));
664 	qs->attr.revision = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u.%u",
665 					   SOCINFO_MAJOR(le32_to_cpu(info->ver)),
666 					   SOCINFO_MINOR(le32_to_cpu(info->ver)));
667 	if (offsetof(struct socinfo, serial_num) <= item_size)
668 		qs->attr.serial_number = devm_kasprintf(&pdev->dev, GFP_KERNEL,
669 							"%u",
670 							le32_to_cpu(info->serial_num));
671 
672 	qs->soc_dev = soc_device_register(&qs->attr);
673 	if (IS_ERR(qs->soc_dev))
674 		return PTR_ERR(qs->soc_dev);
675 
676 	socinfo_debugfs_init(qs, info, item_size);
677 
678 	/* Feed the soc specific unique data into entropy pool */
679 	add_device_randomness(info, item_size);
680 
681 	platform_set_drvdata(pdev, qs);
682 
683 	return 0;
684 }
685 
686 static int qcom_socinfo_remove(struct platform_device *pdev)
687 {
688 	struct qcom_socinfo *qs = platform_get_drvdata(pdev);
689 
690 	soc_device_unregister(qs->soc_dev);
691 
692 	socinfo_debugfs_exit(qs);
693 
694 	return 0;
695 }
696 
697 static struct platform_driver qcom_socinfo_driver = {
698 	.probe = qcom_socinfo_probe,
699 	.remove = qcom_socinfo_remove,
700 	.driver  = {
701 		.name = "qcom-socinfo",
702 	},
703 };
704 
705 module_platform_driver(qcom_socinfo_driver);
706 
707 MODULE_DESCRIPTION("Qualcomm SoCinfo driver");
708 MODULE_LICENSE("GPL v2");
709 MODULE_ALIAS("platform:qcom-socinfo");
710