imx-iim.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * i.MX IIM driver
  3. *
  4. * Copyright (c) 2017 Pengutronix, Michael Grzeschik <m.grzeschik@pengutronix.de>
  5. *
  6. * Based on the barebox iim driver,
  7. * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
  8. * Orex Computed Radiography
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * http://www.opensource.org/licenses/gpl-license.html
  15. * http://www.gnu.org/copyleft/gpl.html
  16. */
  17. #include <linux/device.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/nvmem-provider.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/clk.h>
  26. #define IIM_BANK_BASE(n) (0x800 + 0x400 * (n))
  27. struct imx_iim_drvdata {
  28. unsigned int nregs;
  29. };
  30. struct iim_priv {
  31. void __iomem *base;
  32. struct clk *clk;
  33. };
  34. static int imx_iim_read(void *context, unsigned int offset,
  35. void *buf, size_t bytes)
  36. {
  37. struct iim_priv *iim = context;
  38. int i, ret;
  39. u8 *buf8 = buf;
  40. ret = clk_prepare_enable(iim->clk);
  41. if (ret)
  42. return ret;
  43. for (i = offset; i < offset + bytes; i++) {
  44. int bank = i >> 5;
  45. int reg = i & 0x1f;
  46. *buf8++ = readl(iim->base + IIM_BANK_BASE(bank) + reg * 4);
  47. }
  48. clk_disable_unprepare(iim->clk);
  49. return 0;
  50. }
  51. static struct imx_iim_drvdata imx27_drvdata = {
  52. .nregs = 2 * 32,
  53. };
  54. static struct imx_iim_drvdata imx25_imx31_imx35_drvdata = {
  55. .nregs = 3 * 32,
  56. };
  57. static struct imx_iim_drvdata imx51_drvdata = {
  58. .nregs = 4 * 32,
  59. };
  60. static struct imx_iim_drvdata imx53_drvdata = {
  61. .nregs = 4 * 32 + 16,
  62. };
  63. static const struct of_device_id imx_iim_dt_ids[] = {
  64. {
  65. .compatible = "fsl,imx25-iim",
  66. .data = &imx25_imx31_imx35_drvdata,
  67. }, {
  68. .compatible = "fsl,imx27-iim",
  69. .data = &imx27_drvdata,
  70. }, {
  71. .compatible = "fsl,imx31-iim",
  72. .data = &imx25_imx31_imx35_drvdata,
  73. }, {
  74. .compatible = "fsl,imx35-iim",
  75. .data = &imx25_imx31_imx35_drvdata,
  76. }, {
  77. .compatible = "fsl,imx51-iim",
  78. .data = &imx51_drvdata,
  79. }, {
  80. .compatible = "fsl,imx53-iim",
  81. .data = &imx53_drvdata,
  82. }, {
  83. /* sentinel */
  84. },
  85. };
  86. MODULE_DEVICE_TABLE(of, imx_iim_dt_ids);
  87. static int imx_iim_probe(struct platform_device *pdev)
  88. {
  89. const struct of_device_id *of_id;
  90. struct device *dev = &pdev->dev;
  91. struct resource *res;
  92. struct iim_priv *iim;
  93. struct nvmem_device *nvmem;
  94. struct nvmem_config cfg = {};
  95. const struct imx_iim_drvdata *drvdata = NULL;
  96. iim = devm_kzalloc(dev, sizeof(*iim), GFP_KERNEL);
  97. if (!iim)
  98. return -ENOMEM;
  99. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. iim->base = devm_ioremap_resource(dev, res);
  101. if (IS_ERR(iim->base))
  102. return PTR_ERR(iim->base);
  103. of_id = of_match_device(imx_iim_dt_ids, dev);
  104. if (!of_id)
  105. return -ENODEV;
  106. drvdata = of_id->data;
  107. iim->clk = devm_clk_get(dev, NULL);
  108. if (IS_ERR(iim->clk))
  109. return PTR_ERR(iim->clk);
  110. cfg.name = "imx-iim",
  111. cfg.read_only = true,
  112. cfg.word_size = 1,
  113. cfg.stride = 1,
  114. cfg.reg_read = imx_iim_read,
  115. cfg.dev = dev;
  116. cfg.size = drvdata->nregs;
  117. cfg.priv = iim;
  118. nvmem = devm_nvmem_register(dev, &cfg);
  119. return PTR_ERR_OR_ZERO(nvmem);
  120. }
  121. static struct platform_driver imx_iim_driver = {
  122. .probe = imx_iim_probe,
  123. .driver = {
  124. .name = "imx-iim",
  125. .of_match_table = imx_iim_dt_ids,
  126. },
  127. };
  128. module_platform_driver(imx_iim_driver);
  129. MODULE_AUTHOR("Michael Grzeschik <m.grzeschik@pengutronix.de>");
  130. MODULE_DESCRIPTION("i.MX IIM driver");
  131. MODULE_LICENSE("GPL v2");