imx-iim.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. struct nvmem_config nvmem;
  34. };
  35. static int imx_iim_read(void *context, unsigned int offset,
  36. void *buf, size_t bytes)
  37. {
  38. struct iim_priv *iim = context;
  39. int i, ret;
  40. u8 *buf8 = buf;
  41. ret = clk_prepare_enable(iim->clk);
  42. if (ret)
  43. return ret;
  44. for (i = offset; i < offset + bytes; i++) {
  45. int bank = i >> 5;
  46. int reg = i & 0x1f;
  47. *buf8++ = readl(iim->base + IIM_BANK_BASE(bank) + reg * 4);
  48. }
  49. clk_disable_unprepare(iim->clk);
  50. return 0;
  51. }
  52. static struct imx_iim_drvdata imx27_drvdata = {
  53. .nregs = 2 * 32,
  54. };
  55. static struct imx_iim_drvdata imx25_imx31_imx35_drvdata = {
  56. .nregs = 3 * 32,
  57. };
  58. static struct imx_iim_drvdata imx51_drvdata = {
  59. .nregs = 4 * 32,
  60. };
  61. static struct imx_iim_drvdata imx53_drvdata = {
  62. .nregs = 4 * 32 + 16,
  63. };
  64. static const struct of_device_id imx_iim_dt_ids[] = {
  65. {
  66. .compatible = "fsl,imx25-iim",
  67. .data = &imx25_imx31_imx35_drvdata,
  68. }, {
  69. .compatible = "fsl,imx27-iim",
  70. .data = &imx27_drvdata,
  71. }, {
  72. .compatible = "fsl,imx31-iim",
  73. .data = &imx25_imx31_imx35_drvdata,
  74. }, {
  75. .compatible = "fsl,imx35-iim",
  76. .data = &imx25_imx31_imx35_drvdata,
  77. }, {
  78. .compatible = "fsl,imx51-iim",
  79. .data = &imx51_drvdata,
  80. }, {
  81. .compatible = "fsl,imx53-iim",
  82. .data = &imx53_drvdata,
  83. }, {
  84. /* sentinel */
  85. },
  86. };
  87. MODULE_DEVICE_TABLE(of, imx_iim_dt_ids);
  88. static int imx_iim_probe(struct platform_device *pdev)
  89. {
  90. const struct of_device_id *of_id;
  91. struct device *dev = &pdev->dev;
  92. struct resource *res;
  93. struct iim_priv *iim;
  94. struct nvmem_device *nvmem;
  95. struct nvmem_config *cfg;
  96. const struct imx_iim_drvdata *drvdata = NULL;
  97. iim = devm_kzalloc(dev, sizeof(*iim), GFP_KERNEL);
  98. if (!iim)
  99. return -ENOMEM;
  100. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  101. iim->base = devm_ioremap_resource(dev, res);
  102. if (IS_ERR(iim->base))
  103. return PTR_ERR(iim->base);
  104. of_id = of_match_device(imx_iim_dt_ids, dev);
  105. if (!of_id)
  106. return -ENODEV;
  107. drvdata = of_id->data;
  108. iim->clk = devm_clk_get(&pdev->dev, NULL);
  109. if (IS_ERR(iim->clk))
  110. return PTR_ERR(iim->clk);
  111. cfg = &iim->nvmem;
  112. cfg->name = "imx-iim",
  113. cfg->read_only = true,
  114. cfg->word_size = 1,
  115. cfg->stride = 1,
  116. cfg->owner = THIS_MODULE,
  117. cfg->reg_read = imx_iim_read,
  118. cfg->dev = dev;
  119. cfg->size = drvdata->nregs;
  120. cfg->priv = iim;
  121. nvmem = nvmem_register(cfg);
  122. if (IS_ERR(nvmem))
  123. return PTR_ERR(nvmem);
  124. platform_set_drvdata(pdev, nvmem);
  125. return 0;
  126. }
  127. static int imx_iim_remove(struct platform_device *pdev)
  128. {
  129. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  130. return nvmem_unregister(nvmem);
  131. }
  132. static struct platform_driver imx_iim_driver = {
  133. .probe = imx_iim_probe,
  134. .remove = imx_iim_remove,
  135. .driver = {
  136. .name = "imx-iim",
  137. .of_match_table = imx_iim_dt_ids,
  138. },
  139. };
  140. module_platform_driver(imx_iim_driver);
  141. MODULE_AUTHOR("Michael Grzeschik <m.grzeschik@pengutronix.de>");
  142. MODULE_DESCRIPTION("i.MX IIM driver");
  143. MODULE_LICENSE("GPL v2");