mtk-efuse.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/io.h>
  18. #include <linux/nvmem-provider.h>
  19. #include <linux/platform_device.h>
  20. struct mtk_efuse_priv {
  21. void __iomem *base;
  22. };
  23. static int mtk_reg_read(void *context,
  24. unsigned int reg, void *_val, size_t bytes)
  25. {
  26. struct mtk_efuse_priv *priv = context;
  27. u32 *val = _val;
  28. int i = 0, words = bytes / 4;
  29. while (words--)
  30. *val++ = readl(priv->base + reg + (i++ * 4));
  31. return 0;
  32. }
  33. static int mtk_reg_write(void *context,
  34. unsigned int reg, void *_val, size_t bytes)
  35. {
  36. struct mtk_efuse_priv *priv = context;
  37. u32 *val = _val;
  38. int i = 0, words = bytes / 4;
  39. while (words--)
  40. writel(*val++, priv->base + reg + (i++ * 4));
  41. return 0;
  42. }
  43. static int mtk_efuse_probe(struct platform_device *pdev)
  44. {
  45. struct device *dev = &pdev->dev;
  46. struct resource *res;
  47. struct nvmem_device *nvmem;
  48. struct nvmem_config econfig = {};
  49. struct mtk_efuse_priv *priv;
  50. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  51. if (!priv)
  52. return -ENOMEM;
  53. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  54. priv->base = devm_ioremap_resource(dev, res);
  55. if (IS_ERR(priv->base))
  56. return PTR_ERR(priv->base);
  57. econfig.stride = 4;
  58. econfig.word_size = 4;
  59. econfig.reg_read = mtk_reg_read;
  60. econfig.reg_write = mtk_reg_write;
  61. econfig.size = resource_size(res);
  62. econfig.priv = priv;
  63. econfig.dev = dev;
  64. nvmem = devm_nvmem_register(dev, &econfig);
  65. return PTR_ERR_OR_ZERO(nvmem);
  66. }
  67. static const struct of_device_id mtk_efuse_of_match[] = {
  68. { .compatible = "mediatek,mt8173-efuse",},
  69. { .compatible = "mediatek,efuse",},
  70. {/* sentinel */},
  71. };
  72. MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
  73. static struct platform_driver mtk_efuse_driver = {
  74. .probe = mtk_efuse_probe,
  75. .driver = {
  76. .name = "mediatek,efuse",
  77. .of_match_table = mtk_efuse_of_match,
  78. },
  79. };
  80. static int __init mtk_efuse_init(void)
  81. {
  82. int ret;
  83. ret = platform_driver_register(&mtk_efuse_driver);
  84. if (ret) {
  85. pr_err("Failed to register efuse driver\n");
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. static void __exit mtk_efuse_exit(void)
  91. {
  92. return platform_driver_unregister(&mtk_efuse_driver);
  93. }
  94. subsys_initcall(mtk_efuse_init);
  95. module_exit(mtk_efuse_exit);
  96. MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
  97. MODULE_DESCRIPTION("Mediatek EFUSE driver");
  98. MODULE_LICENSE("GPL v2");