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