mtk-efuse.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/io.h>
  17. #include <linux/nvmem-provider.h>
  18. #include <linux/platform_device.h>
  19. struct mtk_efuse_priv {
  20. void __iomem *base;
  21. };
  22. static int mtk_reg_read(void *context,
  23. unsigned int reg, void *_val, size_t bytes)
  24. {
  25. struct mtk_efuse_priv *priv = context;
  26. u32 *val = _val;
  27. int i = 0, words = bytes / 4;
  28. while (words--)
  29. *val++ = readl(priv->base + reg + (i++ * 4));
  30. return 0;
  31. }
  32. static int mtk_reg_write(void *context,
  33. unsigned int reg, void *_val, size_t bytes)
  34. {
  35. struct mtk_efuse_priv *priv = context;
  36. u32 *val = _val;
  37. int i = 0, words = bytes / 4;
  38. while (words--)
  39. writel(*val++, priv->base + reg + (i++ * 4));
  40. return 0;
  41. }
  42. static int mtk_efuse_probe(struct platform_device *pdev)
  43. {
  44. struct device *dev = &pdev->dev;
  45. struct resource *res;
  46. struct nvmem_device *nvmem;
  47. struct nvmem_config econfig = {};
  48. struct mtk_efuse_priv *priv;
  49. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  50. if (!priv)
  51. return -ENOMEM;
  52. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  53. priv->base = devm_ioremap_resource(dev, res);
  54. if (IS_ERR(priv->base))
  55. return PTR_ERR(priv->base);
  56. econfig.stride = 4;
  57. econfig.word_size = 4;
  58. econfig.reg_read = mtk_reg_read;
  59. econfig.reg_write = mtk_reg_write;
  60. econfig.size = resource_size(res);
  61. econfig.priv = priv;
  62. econfig.dev = dev;
  63. nvmem = devm_nvmem_register(dev, &econfig);
  64. return PTR_ERR_OR_ZERO(nvmem);
  65. }
  66. static const struct of_device_id mtk_efuse_of_match[] = {
  67. { .compatible = "mediatek,mt8173-efuse",},
  68. { .compatible = "mediatek,efuse",},
  69. {/* sentinel */},
  70. };
  71. MODULE_DEVICE_TABLE(of, mtk_efuse_of_match);
  72. static struct platform_driver mtk_efuse_driver = {
  73. .probe = mtk_efuse_probe,
  74. .driver = {
  75. .name = "mediatek,efuse",
  76. .of_match_table = mtk_efuse_of_match,
  77. },
  78. };
  79. static int __init mtk_efuse_init(void)
  80. {
  81. int ret;
  82. ret = platform_driver_register(&mtk_efuse_driver);
  83. if (ret) {
  84. pr_err("Failed to register efuse driver\n");
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. static void __exit mtk_efuse_exit(void)
  90. {
  91. return platform_driver_unregister(&mtk_efuse_driver);
  92. }
  93. subsys_initcall(mtk_efuse_init);
  94. module_exit(mtk_efuse_exit);
  95. MODULE_AUTHOR("Andrew-CT Chen <andrew-ct.chen@mediatek.com>");
  96. MODULE_DESCRIPTION("Mediatek EFUSE driver");
  97. MODULE_LICENSE("GPL v2");