qfprom.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. struct qfprom_priv {
  19. void __iomem *base;
  20. };
  21. static int qfprom_reg_read(void *context,
  22. unsigned int reg, void *_val, size_t bytes)
  23. {
  24. struct qfprom_priv *priv = context;
  25. u8 *val = _val;
  26. int i = 0, words = bytes;
  27. while (words--)
  28. *val++ = readb(priv->base + reg + i++);
  29. return 0;
  30. }
  31. static int qfprom_reg_write(void *context,
  32. unsigned int reg, void *_val, size_t bytes)
  33. {
  34. struct qfprom_priv *priv = context;
  35. u8 *val = _val;
  36. int i = 0, words = bytes;
  37. while (words--)
  38. writeb(*val++, priv->base + reg + i++);
  39. return 0;
  40. }
  41. static struct nvmem_config econfig = {
  42. .name = "qfprom",
  43. .stride = 1,
  44. .word_size = 1,
  45. .reg_read = qfprom_reg_read,
  46. .reg_write = qfprom_reg_write,
  47. };
  48. static int qfprom_probe(struct platform_device *pdev)
  49. {
  50. struct device *dev = &pdev->dev;
  51. struct resource *res;
  52. struct nvmem_device *nvmem;
  53. struct qfprom_priv *priv;
  54. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  55. if (!priv)
  56. return -ENOMEM;
  57. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  58. priv->base = devm_ioremap_resource(dev, res);
  59. if (IS_ERR(priv->base))
  60. return PTR_ERR(priv->base);
  61. econfig.size = resource_size(res);
  62. econfig.dev = dev;
  63. econfig.priv = priv;
  64. nvmem = devm_nvmem_register(dev, &econfig);
  65. return PTR_ERR_OR_ZERO(nvmem);
  66. }
  67. static const struct of_device_id qfprom_of_match[] = {
  68. { .compatible = "qcom,qfprom",},
  69. {/* sentinel */},
  70. };
  71. MODULE_DEVICE_TABLE(of, qfprom_of_match);
  72. static struct platform_driver qfprom_driver = {
  73. .probe = qfprom_probe,
  74. .driver = {
  75. .name = "qcom,qfprom",
  76. .of_match_table = qfprom_of_match,
  77. },
  78. };
  79. module_platform_driver(qfprom_driver);
  80. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org>");
  81. MODULE_DESCRIPTION("Qualcomm QFPROM driver");
  82. MODULE_LICENSE("GPL v2");