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