imx-weim.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * EIM driver for Freescale's i.MX chips
  3. *
  4. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/clk.h>
  12. #include <linux/io.h>
  13. #include <linux/of_device.h>
  14. struct imx_weim_devtype {
  15. unsigned int cs_count;
  16. unsigned int cs_regs_count;
  17. unsigned int cs_stride;
  18. };
  19. static const struct imx_weim_devtype imx1_weim_devtype = {
  20. .cs_count = 6,
  21. .cs_regs_count = 2,
  22. .cs_stride = 0x08,
  23. };
  24. static const struct imx_weim_devtype imx27_weim_devtype = {
  25. .cs_count = 6,
  26. .cs_regs_count = 3,
  27. .cs_stride = 0x10,
  28. };
  29. static const struct imx_weim_devtype imx50_weim_devtype = {
  30. .cs_count = 4,
  31. .cs_regs_count = 6,
  32. .cs_stride = 0x18,
  33. };
  34. static const struct imx_weim_devtype imx51_weim_devtype = {
  35. .cs_count = 6,
  36. .cs_regs_count = 6,
  37. .cs_stride = 0x18,
  38. };
  39. static const struct of_device_id weim_id_table[] = {
  40. /* i.MX1/21 */
  41. { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
  42. /* i.MX25/27/31/35 */
  43. { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
  44. /* i.MX50/53/6Q */
  45. { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
  46. { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
  47. /* i.MX51 */
  48. { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
  49. { }
  50. };
  51. MODULE_DEVICE_TABLE(of, weim_id_table);
  52. /* Parse and set the timing for this device. */
  53. static int __init weim_timing_setup(struct device_node *np, void __iomem *base,
  54. const struct imx_weim_devtype *devtype)
  55. {
  56. u32 cs_idx, value[devtype->cs_regs_count];
  57. int i, ret;
  58. /* get the CS index from this child node's "reg" property. */
  59. ret = of_property_read_u32(np, "reg", &cs_idx);
  60. if (ret)
  61. return ret;
  62. if (cs_idx >= devtype->cs_count)
  63. return -EINVAL;
  64. ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
  65. value, devtype->cs_regs_count);
  66. if (ret)
  67. return ret;
  68. /* set the timing for WEIM */
  69. for (i = 0; i < devtype->cs_regs_count; i++)
  70. writel(value[i], base + cs_idx * devtype->cs_stride + i * 4);
  71. return 0;
  72. }
  73. static int __init weim_parse_dt(struct platform_device *pdev,
  74. void __iomem *base)
  75. {
  76. const struct of_device_id *of_id = of_match_device(weim_id_table,
  77. &pdev->dev);
  78. const struct imx_weim_devtype *devtype = of_id->data;
  79. struct device_node *child;
  80. int ret;
  81. for_each_child_of_node(pdev->dev.of_node, child) {
  82. if (!child->name)
  83. continue;
  84. ret = weim_timing_setup(child, base, devtype);
  85. if (ret) {
  86. dev_err(&pdev->dev, "%s set timing failed.\n",
  87. child->full_name);
  88. return ret;
  89. }
  90. }
  91. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  92. if (ret)
  93. dev_err(&pdev->dev, "%s fail to create devices.\n",
  94. pdev->dev.of_node->full_name);
  95. return ret;
  96. }
  97. static int __init weim_probe(struct platform_device *pdev)
  98. {
  99. struct resource *res;
  100. struct clk *clk;
  101. void __iomem *base;
  102. int ret;
  103. /* get the resource */
  104. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. base = devm_ioremap_resource(&pdev->dev, res);
  106. if (IS_ERR(base))
  107. return PTR_ERR(base);
  108. /* get the clock */
  109. clk = devm_clk_get(&pdev->dev, NULL);
  110. if (IS_ERR(clk))
  111. return PTR_ERR(clk);
  112. ret = clk_prepare_enable(clk);
  113. if (ret)
  114. return ret;
  115. /* parse the device node */
  116. ret = weim_parse_dt(pdev, base);
  117. if (ret)
  118. clk_disable_unprepare(clk);
  119. else
  120. dev_info(&pdev->dev, "Driver registered.\n");
  121. return ret;
  122. }
  123. static struct platform_driver weim_driver = {
  124. .driver = {
  125. .name = "imx-weim",
  126. .owner = THIS_MODULE,
  127. .of_match_table = weim_id_table,
  128. },
  129. };
  130. module_platform_driver_probe(weim_driver, weim_probe);
  131. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  132. MODULE_DESCRIPTION("i.MX EIM Controller Driver");
  133. MODULE_LICENSE("GPL");