qfprom.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/nvmem-provider.h>
  17. #include <linux/platform_device.h>
  18. static int qfprom_reg_read(void *context,
  19. unsigned int reg, void *_val, size_t bytes)
  20. {
  21. void __iomem *base = context;
  22. u32 *val = _val;
  23. int i = 0, words = bytes / 4;
  24. while (words--)
  25. *val++ = readl(base + reg + (i++ * 4));
  26. return 0;
  27. }
  28. static int qfprom_reg_write(void *context,
  29. unsigned int reg, void *_val, size_t bytes)
  30. {
  31. void __iomem *base = context;
  32. u32 *val = _val;
  33. int i = 0, words = bytes / 4;
  34. while (words--)
  35. writel(*val++, base + reg + (i++ * 4));
  36. return 0;
  37. }
  38. static int qfprom_remove(struct platform_device *pdev)
  39. {
  40. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  41. return nvmem_unregister(nvmem);
  42. }
  43. static struct nvmem_config econfig = {
  44. .name = "qfprom",
  45. .owner = THIS_MODULE,
  46. .stride = 4,
  47. .word_size = 1,
  48. .reg_read = qfprom_reg_read,
  49. .reg_write = qfprom_reg_write,
  50. };
  51. static int qfprom_probe(struct platform_device *pdev)
  52. {
  53. struct device *dev = &pdev->dev;
  54. struct resource *res;
  55. struct nvmem_device *nvmem;
  56. void __iomem *base;
  57. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  58. base = devm_ioremap_resource(dev, res);
  59. if (IS_ERR(base))
  60. return PTR_ERR(base);
  61. econfig.size = resource_size(res);
  62. econfig.dev = dev;
  63. econfig.priv = base;
  64. nvmem = nvmem_register(&econfig);
  65. if (IS_ERR(nvmem))
  66. return PTR_ERR(nvmem);
  67. platform_set_drvdata(pdev, nvmem);
  68. return 0;
  69. }
  70. static const struct of_device_id qfprom_of_match[] = {
  71. { .compatible = "qcom,qfprom",},
  72. {/* sentinel */},
  73. };
  74. MODULE_DEVICE_TABLE(of, qfprom_of_match);
  75. static struct platform_driver qfprom_driver = {
  76. .probe = qfprom_probe,
  77. .remove = qfprom_remove,
  78. .driver = {
  79. .name = "qcom,qfprom",
  80. .of_match_table = qfprom_of_match,
  81. },
  82. };
  83. module_platform_driver(qfprom_driver);
  84. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
  85. MODULE_DESCRIPTION("Qualcomm QFPROM driver");
  86. MODULE_LICENSE("GPL v2");