mtk-socinfo.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #define MTK_SOCINFO_ENTRY(_soc_name, _segment_name, _marketing_name, _cell_data1, _cell_data2) {\
  20. .soc_name = _soc_name, \
  21. .segment_name = _segment_name, \
  22. .marketing_name = _marketing_name, \
  23. .cell_data = {_cell_data1, _cell_data2} \
  24. }
  25. #define CELL_NOT_USED (0xFFFFFFFF)
  26. #define MAX_CELLS (2)
  27. struct mtk_socinfo {
  28. struct device *dev;
  29. struct name_data *name_data;
  30. struct socinfo_data *socinfo_data;
  31. struct soc_device *soc_dev;
  32. };
  33. struct socinfo_data {
  34. char *soc_name;
  35. char *segment_name;
  36. char *marketing_name;
  37. u32 cell_data[MAX_CELLS];
  38. };
  39. static const char *cell_names[MAX_CELLS] = {"socinfo-data1", "socinfo-data2"};
  40. static struct socinfo_data socinfo_data_table[] = {
  41. MTK_SOCINFO_ENTRY("MT8173", "MT8173V/AC", "MT8173", 0x6CA20004, 0x10000000),
  42. MTK_SOCINFO_ENTRY("MT8183", "MT8183V/AZA", "Kompanio 500", 0x00010043, 0x00000840),
  43. MTK_SOCINFO_ENTRY("MT8183", "MT8183V/AZA", "Kompanio 500", 0x00010043, 0x00000940),
  44. MTK_SOCINFO_ENTRY("MT8186", "MT8186GV/AZA", "Kompanio 520", 0x81861001, CELL_NOT_USED),
  45. MTK_SOCINFO_ENTRY("MT8186T", "MT8186TV/AZA", "Kompanio 528", 0x81862001, CELL_NOT_USED),
  46. MTK_SOCINFO_ENTRY("MT8188", "MT8188GV/AZA", "Kompanio 838", 0x81880000, 0x00000010),
  47. MTK_SOCINFO_ENTRY("MT8188", "MT8188GV/HZA", "Kompanio 838", 0x81880000, 0x00000011),
  48. MTK_SOCINFO_ENTRY("MT8189", "MT8189GV/AZA", "Kompanio 540", 0x81890000, 0x00000020),
  49. MTK_SOCINFO_ENTRY("MT8189", "MT8189HV/AZA", "Kompanio 540", 0x81890000, 0x00000021),
  50. MTK_SOCINFO_ENTRY("MT8192", "MT8192V/AZA", "Kompanio 820", 0x00001100, 0x00040080),
  51. MTK_SOCINFO_ENTRY("MT8192T", "MT8192V/ATZA", "Kompanio 828", 0x00000100, 0x000400C0),
  52. MTK_SOCINFO_ENTRY("MT8195", "MT8195GV/EZA", "Kompanio 1200", 0x81950300, CELL_NOT_USED),
  53. MTK_SOCINFO_ENTRY("MT8195", "MT8195GV/EHZA", "Kompanio 1200", 0x81950304, CELL_NOT_USED),
  54. MTK_SOCINFO_ENTRY("MT8195", "MT8195TV/EZA", "Kompanio 1380", 0x81950400, CELL_NOT_USED),
  55. MTK_SOCINFO_ENTRY("MT8195", "MT8195TV/EHZA", "Kompanio 1380", 0x81950404, CELL_NOT_USED),
  56. MTK_SOCINFO_ENTRY("MT8370", "MT8370AV/AZA", "Genio 510", 0x83700000, 0x00000081),
  57. MTK_SOCINFO_ENTRY("MT8371", "MT8371AV/AZA", "Genio 520", 0x83710000, 0x00000081),
  58. MTK_SOCINFO_ENTRY("MT8390", "MT8390AV/AZA", "Genio 700", 0x83900000, 0x00000080),
  59. MTK_SOCINFO_ENTRY("MT8391", "MT8391AV/AZA", "Genio 720", 0x83910000, 0x00000080),
  60. MTK_SOCINFO_ENTRY("MT8395", "MT8395AV/ZA", "Genio 1200", 0x83950100, CELL_NOT_USED),
  61. MTK_SOCINFO_ENTRY("MT8395", "MT8395AV/ZA", "Genio 1200", 0x83950800, CELL_NOT_USED),
  62. };
  63. static int mtk_socinfo_create_socinfo_node(struct mtk_socinfo *mtk_socinfop)
  64. {
  65. struct soc_device_attribute *attrs;
  66. struct socinfo_data *data = mtk_socinfop->socinfo_data;
  67. static const char *soc_manufacturer = "MediaTek";
  68. attrs = devm_kzalloc(mtk_socinfop->dev, sizeof(*attrs), GFP_KERNEL);
  69. if (!attrs)
  70. return -ENOMEM;
  71. if (data->marketing_name != NULL && data->marketing_name[0] != '\0')
  72. attrs->family = devm_kasprintf(mtk_socinfop->dev, GFP_KERNEL, "MediaTek %s",
  73. data->marketing_name);
  74. else
  75. attrs->family = soc_manufacturer;
  76. attrs->soc_id = data->soc_name;
  77. /*
  78. * The "machine" field will be populated automatically with the model
  79. * name from board DTS (if available).
  80. **/
  81. mtk_socinfop->soc_dev = soc_device_register(attrs);
  82. if (IS_ERR(mtk_socinfop->soc_dev))
  83. return PTR_ERR(mtk_socinfop->soc_dev);
  84. dev_info(mtk_socinfop->dev, "%s (%s) SoC detected.\n", attrs->family, attrs->soc_id);
  85. return 0;
  86. }
  87. static u32 mtk_socinfo_read_cell(struct device *dev, const char *name)
  88. {
  89. struct nvmem_device *nvmemp;
  90. struct device_node *np, *nvmem_node = dev->parent->of_node;
  91. u32 offset;
  92. u32 cell_val = CELL_NOT_USED;
  93. /* should never fail since the nvmem driver registers this child */
  94. nvmemp = nvmem_device_find(nvmem_node, device_match_of_node);
  95. if (IS_ERR(nvmemp))
  96. goto out;
  97. np = of_get_child_by_name(nvmem_node, name);
  98. if (!np)
  99. goto put_device;
  100. if (of_property_read_u32_index(np, "reg", 0, &offset))
  101. goto put_node;
  102. nvmem_device_read(nvmemp, offset, sizeof(cell_val), &cell_val);
  103. put_node:
  104. of_node_put(np);
  105. put_device:
  106. nvmem_device_put(nvmemp);
  107. out:
  108. return cell_val;
  109. }
  110. static int mtk_socinfo_get_socinfo_data(struct mtk_socinfo *mtk_socinfop)
  111. {
  112. unsigned int i, j;
  113. unsigned int num_cell_data = 0;
  114. u32 cell_data[MAX_CELLS] = {0};
  115. bool match_socinfo;
  116. int match_socinfo_index = -1;
  117. for (i = 0; i < MAX_CELLS; i++) {
  118. cell_data[i] = mtk_socinfo_read_cell(mtk_socinfop->dev, cell_names[i]);
  119. if (cell_data[i] != CELL_NOT_USED)
  120. num_cell_data++;
  121. else
  122. break;
  123. }
  124. if (!num_cell_data)
  125. return -ENOENT;
  126. for (i = 0; i < ARRAY_SIZE(socinfo_data_table); i++) {
  127. match_socinfo = true;
  128. for (j = 0; j < num_cell_data; j++) {
  129. if (cell_data[j] != socinfo_data_table[i].cell_data[j]) {
  130. match_socinfo = false;
  131. break;
  132. }
  133. }
  134. if (match_socinfo) {
  135. mtk_socinfop->socinfo_data = &(socinfo_data_table[i]);
  136. match_socinfo_index = i;
  137. break;
  138. }
  139. }
  140. if (match_socinfo_index < 0) {
  141. dev_warn(mtk_socinfop->dev,
  142. "Unknown MediaTek SoC with ID 0x%08x 0x%08x\n",
  143. cell_data[0], cell_data[1]);
  144. return -ENOENT;
  145. }
  146. return match_socinfo_index;
  147. }
  148. static int mtk_socinfo_probe(struct platform_device *pdev)
  149. {
  150. struct mtk_socinfo *mtk_socinfop;
  151. int ret;
  152. mtk_socinfop = devm_kzalloc(&pdev->dev, sizeof(*mtk_socinfop), GFP_KERNEL);
  153. if (!mtk_socinfop)
  154. return -ENOMEM;
  155. mtk_socinfop->dev = &pdev->dev;
  156. ret = mtk_socinfo_get_socinfo_data(mtk_socinfop);
  157. if (ret < 0)
  158. return dev_err_probe(mtk_socinfop->dev, ret, "Failed to get socinfo data\n");
  159. ret = mtk_socinfo_create_socinfo_node(mtk_socinfop);
  160. if (ret)
  161. return dev_err_probe(mtk_socinfop->dev, ret, "Cannot create node\n");
  162. platform_set_drvdata(pdev, mtk_socinfop);
  163. return 0;
  164. }
  165. static void mtk_socinfo_remove(struct platform_device *pdev)
  166. {
  167. struct mtk_socinfo *mtk_socinfop = platform_get_drvdata(pdev);
  168. soc_device_unregister(mtk_socinfop->soc_dev);
  169. }
  170. static struct platform_driver mtk_socinfo = {
  171. .probe = mtk_socinfo_probe,
  172. .remove = mtk_socinfo_remove,
  173. .driver = {
  174. .name = "mtk-socinfo",
  175. },
  176. };
  177. module_platform_driver(mtk_socinfo);
  178. MODULE_AUTHOR("William-TW LIN <william-tw.lin@mediatek.com>");
  179. MODULE_DESCRIPTION("MediaTek socinfo driver");
  180. MODULE_LICENSE("GPL");