xref: /linux/drivers/soc/mediatek/mtk-socinfo.c (revision 8b40a46966d294bc64bad0feb13d3304fde738f2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023 MediaTek Inc.
4  */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/of.h>
8 #include <linux/of_platform.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/device.h>
12 #include <linux/device/bus.h>
13 #include <linux/debugfs.h>
14 #include <linux/seq_file.h>
15 #include <linux/string.h>
16 #include <linux/sys_soc.h>
17 #include <linux/slab.h>
18 #include <linux/platform_device.h>
19 
20 #define MTK_SOCINFO_ENTRY(_soc_name, _segment_name, _marketing_name, _cell_data1, _cell_data2) {\
21 	.soc_name = _soc_name,									\
22 	.segment_name = _segment_name,								\
23 	.marketing_name = _marketing_name,							\
24 	.cell_data = {_cell_data1, _cell_data2}							\
25 }
26 #define CELL_NOT_USED (0xFFFFFFFF)
27 #define MAX_CELLS (2)
28 
29 struct mtk_socinfo {
30 	struct device *dev;
31 	struct name_data *name_data;
32 	struct socinfo_data *socinfo_data;
33 	struct soc_device *soc_dev;
34 };
35 
36 struct socinfo_data {
37 	char *soc_name;
38 	char *segment_name;
39 	char *marketing_name;
40 	u32 cell_data[MAX_CELLS];
41 };
42 
43 static const char *cell_names[MAX_CELLS] = {"socinfo-data1", "socinfo-data2"};
44 
45 static struct socinfo_data socinfo_data_table[] = {
46 	MTK_SOCINFO_ENTRY("MT8173", "MT8173V/AC", "MT8173", 0x6CA20004, 0x10000000),
47 	MTK_SOCINFO_ENTRY("MT8183", "MT8183V/AZA", "Kompanio 500", 0x00010043, 0x00000840),
48 	MTK_SOCINFO_ENTRY("MT8183", "MT8183V/AZA", "Kompanio 500", 0x00010043, 0x00000940),
49 	MTK_SOCINFO_ENTRY("MT8186", "MT8186GV/AZA", "Kompanio 520", 0x81861001, CELL_NOT_USED),
50 	MTK_SOCINFO_ENTRY("MT8186T", "MT8186TV/AZA", "Kompanio 528", 0x81862001, CELL_NOT_USED),
51 	MTK_SOCINFO_ENTRY("MT8188", "MT8188GV/AZA", "Kompanio 830", 0x81880000, 0x00000010),
52 	MTK_SOCINFO_ENTRY("MT8188", "MT8188GV/HZA", "Kompanio 830", 0x81880000, 0x00000011),
53 	MTK_SOCINFO_ENTRY("MT8192", "MT8192V/AZA", "Kompanio 820", 0x00001100, 0x00040080),
54 	MTK_SOCINFO_ENTRY("MT8192T", "MT8192V/ATZA", "Kompanio 828", 0x00000100, 0x000400C0),
55 	MTK_SOCINFO_ENTRY("MT8195", "MT8195GV/EZA", "Kompanio 1200", 0x81950300, CELL_NOT_USED),
56 	MTK_SOCINFO_ENTRY("MT8195", "MT8195GV/EHZA", "Kompanio 1200", 0x81950304, CELL_NOT_USED),
57 	MTK_SOCINFO_ENTRY("MT8195", "MT8195TV/EZA", "Kompanio 1380", 0x81950400, CELL_NOT_USED),
58 	MTK_SOCINFO_ENTRY("MT8195", "MT8195TV/EHZA", "Kompanio 1380", 0x81950404, CELL_NOT_USED),
59 };
60 
61 static int mtk_socinfo_create_socinfo_node(struct mtk_socinfo *mtk_socinfop)
62 {
63 	struct soc_device_attribute *attrs;
64 	static char machine[30] = {0};
65 	static const char *soc_manufacturer = "MediaTek";
66 
67 	attrs = devm_kzalloc(mtk_socinfop->dev, sizeof(*attrs), GFP_KERNEL);
68 	if (!attrs)
69 		return -ENOMEM;
70 
71 	snprintf(machine, sizeof(machine), "%s (%s)", mtk_socinfop->socinfo_data->marketing_name,
72 		mtk_socinfop->socinfo_data->soc_name);
73 	attrs->family = soc_manufacturer;
74 	attrs->machine = machine;
75 
76 	mtk_socinfop->soc_dev = soc_device_register(attrs);
77 	if (IS_ERR(mtk_socinfop->soc_dev))
78 		return PTR_ERR(mtk_socinfop->soc_dev);
79 
80 	dev_info(mtk_socinfop->dev, "%s %s SoC detected.\n", soc_manufacturer, attrs->machine);
81 	return 0;
82 }
83 
84 static u32 mtk_socinfo_read_cell(struct device *dev, const char *name)
85 {
86 	struct nvmem_device *nvmemp;
87 	struct device_node *np, *nvmem_node = dev->parent->of_node;
88 	u32 offset;
89 	u32 cell_val = CELL_NOT_USED;
90 
91 	/* should never fail since the nvmem driver registers this child */
92 	nvmemp = nvmem_device_find(nvmem_node, device_match_of_node);
93 	if (IS_ERR(nvmemp))
94 		goto out;
95 
96 	np = of_get_child_by_name(nvmem_node, name);
97 	if (!np)
98 		goto put_device;
99 
100 	if (of_property_read_u32_index(np, "reg", 0, &offset))
101 		goto put_node;
102 
103 	nvmem_device_read(nvmemp, offset, sizeof(cell_val), &cell_val);
104 
105 put_node:
106 	of_node_put(np);
107 put_device:
108 	nvmem_device_put(nvmemp);
109 out:
110 	return cell_val;
111 }
112 
113 static int mtk_socinfo_get_socinfo_data(struct mtk_socinfo *mtk_socinfop)
114 {
115 	unsigned int i, j;
116 	unsigned int num_cell_data = 0;
117 	u32 cell_data[MAX_CELLS] = {0};
118 	bool match_socinfo;
119 	int match_socinfo_index = -1;
120 
121 	for (i = 0; i < MAX_CELLS; i++) {
122 		cell_data[i] = mtk_socinfo_read_cell(mtk_socinfop->dev, cell_names[i]);
123 		if (cell_data[i] != CELL_NOT_USED)
124 			num_cell_data++;
125 		else
126 			break;
127 	}
128 
129 	if (!num_cell_data)
130 		return -ENOENT;
131 
132 	for (i = 0; i < ARRAY_SIZE(socinfo_data_table); i++) {
133 		match_socinfo = true;
134 		for (j = 0; j < num_cell_data; j++) {
135 			if (cell_data[j] != socinfo_data_table[i].cell_data[j]) {
136 				match_socinfo = false;
137 				break;
138 			}
139 		}
140 		if (match_socinfo) {
141 			mtk_socinfop->socinfo_data = &(socinfo_data_table[i]);
142 			match_socinfo_index = i;
143 			break;
144 		}
145 	}
146 
147 	return match_socinfo_index >= 0 ? match_socinfo_index : -ENOENT;
148 }
149 
150 static int mtk_socinfo_probe(struct platform_device *pdev)
151 {
152 	struct mtk_socinfo *mtk_socinfop;
153 	int ret;
154 
155 	mtk_socinfop = devm_kzalloc(&pdev->dev, sizeof(*mtk_socinfop), GFP_KERNEL);
156 	if (!mtk_socinfop)
157 		return -ENOMEM;
158 
159 	mtk_socinfop->dev = &pdev->dev;
160 
161 	ret = mtk_socinfo_get_socinfo_data(mtk_socinfop);
162 	if (ret < 0)
163 		return dev_err_probe(mtk_socinfop->dev, ret, "Failed to get socinfo data\n");
164 
165 	ret = mtk_socinfo_create_socinfo_node(mtk_socinfop);
166 	if (ret)
167 		return dev_err_probe(mtk_socinfop->dev, ret, "Cannot create node\n");
168 
169 	platform_set_drvdata(pdev, mtk_socinfop);
170 	return 0;
171 }
172 
173 static void mtk_socinfo_remove(struct platform_device *pdev)
174 {
175 	struct mtk_socinfo *mtk_socinfop = platform_get_drvdata(pdev);
176 
177 	soc_device_unregister(mtk_socinfop->soc_dev);
178 }
179 
180 static struct platform_driver mtk_socinfo = {
181 	.probe = mtk_socinfo_probe,
182 	.remove_new = mtk_socinfo_remove,
183 	.driver = {
184 		.name = "mtk-socinfo",
185 	},
186 };
187 module_platform_driver(mtk_socinfo);
188 
189 MODULE_AUTHOR("William-TW LIN <william-tw.lin@mediatek.com>");
190 MODULE_DESCRIPTION("MediaTek socinfo driver");
191 MODULE_LICENSE("GPL");
192