spi-dw-mmio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Memory-mapped interface driver for DW SPI Core
  3. *
  4. * Copyright (c) 2010, Octasic semiconductor.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/module.h>
  18. #include <linux/of_gpio.h>
  19. #include "spi-dw.h"
  20. #define DRIVER_NAME "dw_spi_mmio"
  21. struct dw_spi_mmio {
  22. struct dw_spi dws;
  23. struct clk *clk;
  24. };
  25. static int dw_spi_mmio_probe(struct platform_device *pdev)
  26. {
  27. struct dw_spi_mmio *dwsmmio;
  28. struct dw_spi *dws;
  29. struct resource *mem;
  30. int ret;
  31. dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
  32. GFP_KERNEL);
  33. if (!dwsmmio)
  34. return -ENOMEM;
  35. dws = &dwsmmio->dws;
  36. /* Get basic io resource and map it */
  37. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38. if (!mem) {
  39. dev_err(&pdev->dev, "no mem resource?\n");
  40. return -EINVAL;
  41. }
  42. dws->regs = devm_ioremap_resource(&pdev->dev, mem);
  43. if (IS_ERR(dws->regs)) {
  44. dev_err(&pdev->dev, "SPI region map failed\n");
  45. return PTR_ERR(dws->regs);
  46. }
  47. dws->irq = platform_get_irq(pdev, 0);
  48. if (dws->irq < 0) {
  49. dev_err(&pdev->dev, "no irq resource?\n");
  50. return dws->irq; /* -ENXIO */
  51. }
  52. dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
  53. if (IS_ERR(dwsmmio->clk))
  54. return PTR_ERR(dwsmmio->clk);
  55. ret = clk_prepare_enable(dwsmmio->clk);
  56. if (ret)
  57. return ret;
  58. dws->bus_num = pdev->id;
  59. dws->num_cs = 4;
  60. dws->max_freq = clk_get_rate(dwsmmio->clk);
  61. if (pdev->dev.of_node) {
  62. int i;
  63. for (i = 0; i < dws->num_cs; i++) {
  64. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  65. "cs-gpios", i);
  66. if (cs_gpio == -EPROBE_DEFER) {
  67. ret = cs_gpio;
  68. goto out;
  69. }
  70. if (gpio_is_valid(cs_gpio)) {
  71. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  72. dev_name(&pdev->dev));
  73. if (ret)
  74. goto out;
  75. }
  76. }
  77. }
  78. ret = dw_spi_add_host(&pdev->dev, dws);
  79. if (ret)
  80. goto out;
  81. platform_set_drvdata(pdev, dwsmmio);
  82. return 0;
  83. out:
  84. clk_disable_unprepare(dwsmmio->clk);
  85. return ret;
  86. }
  87. static int dw_spi_mmio_remove(struct platform_device *pdev)
  88. {
  89. struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
  90. clk_disable_unprepare(dwsmmio->clk);
  91. dw_spi_remove_host(&dwsmmio->dws);
  92. return 0;
  93. }
  94. static struct platform_driver dw_spi_mmio_driver = {
  95. .probe = dw_spi_mmio_probe,
  96. .remove = dw_spi_mmio_remove,
  97. .driver = {
  98. .name = DRIVER_NAME,
  99. .owner = THIS_MODULE,
  100. },
  101. };
  102. module_platform_driver(dw_spi_mmio_driver);
  103. MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
  104. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
  105. MODULE_LICENSE("GPL v2");