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 838", 0x81880000, 0x00000010),
52 MTK_SOCINFO_ENTRY("MT8188", "MT8188GV/HZA", "Kompanio 838", 0x81880000, 0x00000011),
53 MTK_SOCINFO_ENTRY("MT8189", "MT8189GV/AZA", "Kompanio 540", 0x81890000, 0x00000020),
54 MTK_SOCINFO_ENTRY("MT8189", "MT8189HV/AZA", "Kompanio 540", 0x81890000, 0x00000021),
55 MTK_SOCINFO_ENTRY("MT8192", "MT8192V/AZA", "Kompanio 820", 0x00001100, 0x00040080),
56 MTK_SOCINFO_ENTRY("MT8192T", "MT8192V/ATZA", "Kompanio 828", 0x00000100, 0x000400C0),
57 MTK_SOCINFO_ENTRY("MT8195", "MT8195GV/EZA", "Kompanio 1200", 0x81950300, CELL_NOT_USED),
58 MTK_SOCINFO_ENTRY("MT8195", "MT8195GV/EHZA", "Kompanio 1200", 0x81950304, CELL_NOT_USED),
59 MTK_SOCINFO_ENTRY("MT8195", "MT8195TV/EZA", "Kompanio 1380", 0x81950400, CELL_NOT_USED),
60 MTK_SOCINFO_ENTRY("MT8195", "MT8195TV/EHZA", "Kompanio 1380", 0x81950404, CELL_NOT_USED),
61 MTK_SOCINFO_ENTRY("MT8370", "MT8370AV/AZA", "Genio 510", 0x83700000, 0x00000081),
62 MTK_SOCINFO_ENTRY("MT8390", "MT8390AV/AZA", "Genio 700", 0x83900000, 0x00000080),
63 MTK_SOCINFO_ENTRY("MT8391", "MT8391AV/AZA", "Genio 720", 0x83910000, 0x00000080),
64 MTK_SOCINFO_ENTRY("MT8395", "MT8395AV/ZA", "Genio 1200", 0x83950100, CELL_NOT_USED),
65 MTK_SOCINFO_ENTRY("MT8395", "MT8395AV/ZA", "Genio 1200", 0x83950800, CELL_NOT_USED),
66 };
67
mtk_socinfo_create_socinfo_node(struct mtk_socinfo * mtk_socinfop)68 static int mtk_socinfo_create_socinfo_node(struct mtk_socinfo *mtk_socinfop)
69 {
70 struct soc_device_attribute *attrs;
71 struct socinfo_data *data = mtk_socinfop->socinfo_data;
72 static const char *soc_manufacturer = "MediaTek";
73
74 attrs = devm_kzalloc(mtk_socinfop->dev, sizeof(*attrs), GFP_KERNEL);
75 if (!attrs)
76 return -ENOMEM;
77
78 if (data->marketing_name != NULL && data->marketing_name[0] != '\0')
79 attrs->family = devm_kasprintf(mtk_socinfop->dev, GFP_KERNEL, "MediaTek %s",
80 data->marketing_name);
81 else
82 attrs->family = soc_manufacturer;
83
84 attrs->soc_id = data->soc_name;
85 /*
86 * The "machine" field will be populated automatically with the model
87 * name from board DTS (if available).
88 **/
89
90 mtk_socinfop->soc_dev = soc_device_register(attrs);
91 if (IS_ERR(mtk_socinfop->soc_dev))
92 return PTR_ERR(mtk_socinfop->soc_dev);
93
94 dev_info(mtk_socinfop->dev, "%s (%s) SoC detected.\n", attrs->family, attrs->soc_id);
95 return 0;
96 }
97
mtk_socinfo_read_cell(struct device * dev,const char * name)98 static u32 mtk_socinfo_read_cell(struct device *dev, const char *name)
99 {
100 struct nvmem_device *nvmemp;
101 struct device_node *np, *nvmem_node = dev->parent->of_node;
102 u32 offset;
103 u32 cell_val = CELL_NOT_USED;
104
105 /* should never fail since the nvmem driver registers this child */
106 nvmemp = nvmem_device_find(nvmem_node, device_match_of_node);
107 if (IS_ERR(nvmemp))
108 goto out;
109
110 np = of_get_child_by_name(nvmem_node, name);
111 if (!np)
112 goto put_device;
113
114 if (of_property_read_u32_index(np, "reg", 0, &offset))
115 goto put_node;
116
117 nvmem_device_read(nvmemp, offset, sizeof(cell_val), &cell_val);
118
119 put_node:
120 of_node_put(np);
121 put_device:
122 nvmem_device_put(nvmemp);
123 out:
124 return cell_val;
125 }
126
mtk_socinfo_get_socinfo_data(struct mtk_socinfo * mtk_socinfop)127 static int mtk_socinfo_get_socinfo_data(struct mtk_socinfo *mtk_socinfop)
128 {
129 unsigned int i, j;
130 unsigned int num_cell_data = 0;
131 u32 cell_data[MAX_CELLS] = {0};
132 bool match_socinfo;
133 int match_socinfo_index = -1;
134
135 for (i = 0; i < MAX_CELLS; i++) {
136 cell_data[i] = mtk_socinfo_read_cell(mtk_socinfop->dev, cell_names[i]);
137 if (cell_data[i] != CELL_NOT_USED)
138 num_cell_data++;
139 else
140 break;
141 }
142
143 if (!num_cell_data)
144 return -ENOENT;
145
146 for (i = 0; i < ARRAY_SIZE(socinfo_data_table); i++) {
147 match_socinfo = true;
148 for (j = 0; j < num_cell_data; j++) {
149 if (cell_data[j] != socinfo_data_table[i].cell_data[j]) {
150 match_socinfo = false;
151 break;
152 }
153 }
154 if (match_socinfo) {
155 mtk_socinfop->socinfo_data = &(socinfo_data_table[i]);
156 match_socinfo_index = i;
157 break;
158 }
159 }
160
161 if (match_socinfo_index < 0) {
162 dev_warn(mtk_socinfop->dev,
163 "Unknown MediaTek SoC with ID 0x%08x 0x%08x\n",
164 cell_data[0], cell_data[1]);
165 return -ENOENT;
166 }
167
168 return match_socinfo_index;
169 }
170
mtk_socinfo_probe(struct platform_device * pdev)171 static int mtk_socinfo_probe(struct platform_device *pdev)
172 {
173 struct mtk_socinfo *mtk_socinfop;
174 int ret;
175
176 mtk_socinfop = devm_kzalloc(&pdev->dev, sizeof(*mtk_socinfop), GFP_KERNEL);
177 if (!mtk_socinfop)
178 return -ENOMEM;
179
180 mtk_socinfop->dev = &pdev->dev;
181
182 ret = mtk_socinfo_get_socinfo_data(mtk_socinfop);
183 if (ret < 0)
184 return dev_err_probe(mtk_socinfop->dev, ret, "Failed to get socinfo data\n");
185
186 ret = mtk_socinfo_create_socinfo_node(mtk_socinfop);
187 if (ret)
188 return dev_err_probe(mtk_socinfop->dev, ret, "Cannot create node\n");
189
190 platform_set_drvdata(pdev, mtk_socinfop);
191 return 0;
192 }
193
mtk_socinfo_remove(struct platform_device * pdev)194 static void mtk_socinfo_remove(struct platform_device *pdev)
195 {
196 struct mtk_socinfo *mtk_socinfop = platform_get_drvdata(pdev);
197
198 soc_device_unregister(mtk_socinfop->soc_dev);
199 }
200
201 static struct platform_driver mtk_socinfo = {
202 .probe = mtk_socinfo_probe,
203 .remove = mtk_socinfo_remove,
204 .driver = {
205 .name = "mtk-socinfo",
206 },
207 };
208 module_platform_driver(mtk_socinfo);
209
210 MODULE_AUTHOR("William-TW LIN <william-tw.lin@mediatek.com>");
211 MODULE_DESCRIPTION("MediaTek socinfo driver");
212 MODULE_LICENSE("GPL");
213