imx-weim.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 {
  15. void __iomem *base;
  16. struct clk *clk;
  17. };
  18. static const struct of_device_id weim_id_table[] = {
  19. { .compatible = "fsl,imx6q-weim", },
  20. {}
  21. };
  22. MODULE_DEVICE_TABLE(of, weim_id_table);
  23. #define CS_TIMING_LEN 6
  24. #define CS_REG_RANGE 0x18
  25. /* Parse and set the timing for this device. */
  26. static int
  27. weim_timing_setup(struct platform_device *pdev, struct device_node *np)
  28. {
  29. struct imx_weim *weim = platform_get_drvdata(pdev);
  30. u32 value[CS_TIMING_LEN];
  31. u32 cs_idx;
  32. int ret;
  33. int i;
  34. /* get the CS index from this child node's "reg" property. */
  35. ret = of_property_read_u32(np, "reg", &cs_idx);
  36. if (ret)
  37. return ret;
  38. /* The weim has four chip selects. */
  39. if (cs_idx > 3)
  40. return -EINVAL;
  41. ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
  42. value, CS_TIMING_LEN);
  43. if (ret)
  44. return ret;
  45. /* set the timing for WEIM */
  46. for (i = 0; i < CS_TIMING_LEN; i++)
  47. writel(value[i], weim->base + cs_idx * CS_REG_RANGE + i * 4);
  48. return 0;
  49. }
  50. static int weim_parse_dt(struct platform_device *pdev)
  51. {
  52. struct device_node *child;
  53. int ret;
  54. for_each_child_of_node(pdev->dev.of_node, child) {
  55. if (!child->name)
  56. continue;
  57. ret = weim_timing_setup(pdev, child);
  58. if (ret) {
  59. dev_err(&pdev->dev, "%s set timing failed.\n",
  60. child->full_name);
  61. return ret;
  62. }
  63. }
  64. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  65. if (ret)
  66. dev_err(&pdev->dev, "%s fail to create devices.\n",
  67. pdev->dev.of_node->full_name);
  68. return ret;
  69. }
  70. static int weim_probe(struct platform_device *pdev)
  71. {
  72. struct imx_weim *weim;
  73. struct resource *res;
  74. int ret = -EINVAL;
  75. weim = devm_kzalloc(&pdev->dev, sizeof(*weim), GFP_KERNEL);
  76. if (!weim) {
  77. ret = -ENOMEM;
  78. goto weim_err;
  79. }
  80. platform_set_drvdata(pdev, weim);
  81. /* get the resource */
  82. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. weim->base = devm_ioremap_resource(&pdev->dev, res);
  84. if (IS_ERR(weim->base)) {
  85. ret = PTR_ERR(weim->base);
  86. goto weim_err;
  87. }
  88. /* get the clock */
  89. weim->clk = devm_clk_get(&pdev->dev, NULL);
  90. if (IS_ERR(weim->clk))
  91. goto weim_err;
  92. ret = clk_prepare_enable(weim->clk);
  93. if (ret)
  94. goto weim_err;
  95. /* parse the device node */
  96. ret = weim_parse_dt(pdev);
  97. if (ret) {
  98. clk_disable_unprepare(weim->clk);
  99. goto weim_err;
  100. }
  101. dev_info(&pdev->dev, "WEIM driver registered.\n");
  102. return 0;
  103. weim_err:
  104. return ret;
  105. }
  106. static struct platform_driver weim_driver = {
  107. .driver = {
  108. .name = "imx-weim",
  109. .of_match_table = weim_id_table,
  110. },
  111. .probe = weim_probe,
  112. };
  113. module_platform_driver(weim_driver);
  114. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  115. MODULE_DESCRIPTION("i.MX EIM Controller Driver");
  116. MODULE_LICENSE("GPL");