mtk-efuse.c 2.9 KB

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