qfprom.c 2.4 KB

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