mtk-efuse.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
  5. */
  6. #include <linux/device.h>
  7. #include <linux/module.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/io.h>
  10. #include <linux/nvmem-provider.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/property.h>
  13. struct mtk_efuse_pdata {
  14. bool uses_post_processing;
  15. };
  16. struct mtk_efuse_priv {
  17. void __iomem *base;
  18. };
  19. static int mtk_reg_read(void *context,
  20. unsigned int reg, void *_val, size_t bytes)
  21. {
  22. struct mtk_efuse_priv *priv = context;
  23. void __iomem *addr = priv->base + reg;
  24. u8 *val = _val;
  25. int i;
  26. for (i = 0; i < bytes; i++, val++)
  27. *val = readb(addr + i);
  28. return 0;
  29. }
  30. static int mtk_efuse_gpu_speedbin_pp(void *context, const char *id, int index,
  31. unsigned int offset, void *data, size_t bytes)
  32. {
  33. u8 *val = data;
  34. if (val[0] < 8)
  35. val[0] = BIT(val[0]);
  36. return 0;
  37. }
  38. static void mtk_efuse_fixup_dt_cell_info(struct nvmem_device *nvmem,
  39. struct nvmem_cell_info *cell)
  40. {
  41. size_t sz = strlen(cell->name);
  42. /*
  43. * On some SoCs, the GPU speedbin is not read as bitmask but as
  44. * a number with range [0-7] (max 3 bits): post process to use
  45. * it in OPP tables to describe supported-hw.
  46. */
  47. if (cell->nbits <= 3 &&
  48. strncmp(cell->name, "gpu-speedbin", min(sz, strlen("gpu-speedbin"))) == 0)
  49. cell->read_post_process = mtk_efuse_gpu_speedbin_pp;
  50. }
  51. static int mtk_efuse_probe(struct platform_device *pdev)
  52. {
  53. struct device *dev = &pdev->dev;
  54. struct resource *res;
  55. struct nvmem_device *nvmem;
  56. struct nvmem_config econfig = {};
  57. struct mtk_efuse_priv *priv;
  58. const struct mtk_efuse_pdata *pdata;
  59. struct platform_device *socinfo;
  60. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  61. if (!priv)
  62. return -ENOMEM;
  63. priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  64. if (IS_ERR(priv->base))
  65. return PTR_ERR(priv->base);
  66. pdata = device_get_match_data(dev);
  67. econfig.add_legacy_fixed_of_cells = true;
  68. econfig.stride = 1;
  69. econfig.word_size = 1;
  70. econfig.reg_read = mtk_reg_read;
  71. econfig.size = resource_size(res);
  72. econfig.priv = priv;
  73. econfig.dev = dev;
  74. if (pdata->uses_post_processing)
  75. econfig.fixup_dt_cell_info = &mtk_efuse_fixup_dt_cell_info;
  76. nvmem = devm_nvmem_register(dev, &econfig);
  77. if (IS_ERR(nvmem))
  78. return PTR_ERR(nvmem);
  79. socinfo = platform_device_register_data(&pdev->dev, "mtk-socinfo",
  80. PLATFORM_DEVID_AUTO, NULL, 0);
  81. if (IS_ERR(socinfo))
  82. dev_info(dev, "MediaTek SoC Information will be unavailable\n");
  83. platform_set_drvdata(pdev, socinfo);
  84. return 0;
  85. }
  86. static const struct mtk_efuse_pdata mtk_mt8186_efuse_pdata = {
  87. .uses_post_processing = true,
  88. };
  89. static const struct mtk_efuse_pdata mtk_efuse_pdata = {
  90. .uses_post_processing = false,
  91. };
  92. static const struct of_device_id mtk_efuse_of_match[] = {
  93. { .compatible = "mediatek,mt8173-efuse", .data = &mtk_efuse_pdata },
  94. { .compatible = "mediatek,mt8186-efuse", .data = &mtk_mt8186_efuse_pdata },
  95. { .compatible = "mediatek,efuse", .data = &mtk_efuse_pdata },
  96. {/* sentinel */},
  97. };
  98. MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
  99. static void mtk_efuse_remove(struct platform_device *pdev)
  100. {
  101. struct platform_device *socinfo = platform_get_drvdata(pdev);
  102. if (!IS_ERR_OR_NULL(socinfo))
  103. platform_device_unregister(socinfo);
  104. }
  105. static struct platform_driver mtk_efuse_driver = {
  106. .probe = mtk_efuse_probe,
  107. .remove_new = mtk_efuse_remove,
  108. .driver = {
  109. .name = "mediatek,efuse",
  110. .of_match_table = mtk_efuse_of_match,
  111. },
  112. };
  113. static int __init mtk_efuse_init(void)
  114. {
  115. int ret;
  116. ret = platform_driver_register(&mtk_efuse_driver);
  117. if (ret) {
  118. pr_err("Failed to register efuse driver\n");
  119. return ret;
  120. }
  121. return 0;
  122. }
  123. static void __exit mtk_efuse_exit(void)
  124. {
  125. return platform_driver_unregister(&mtk_efuse_driver);
  126. }
  127. subsys_initcall(mtk_efuse_init);
  128. module_exit(mtk_efuse_exit);
  129. MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
  130. MODULE_DESCRIPTION("Mediatek EFUSE driver");
  131. MODULE_LICENSE("GPL v2");