spi-dw-mmio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/property.h>
  22. #include "spi-dw.h"
  23. #define DRIVER_NAME "dw_spi_mmio"
  24. struct dw_spi_mmio {
  25. struct dw_spi dws;
  26. struct clk *clk;
  27. };
  28. static int dw_spi_mmio_probe(struct platform_device *pdev)
  29. {
  30. struct dw_spi_mmio *dwsmmio;
  31. struct dw_spi *dws;
  32. struct resource *mem;
  33. int ret;
  34. int num_cs;
  35. dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
  36. GFP_KERNEL);
  37. if (!dwsmmio)
  38. return -ENOMEM;
  39. dws = &dwsmmio->dws;
  40. /* Get basic io resource and map it */
  41. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  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->max_freq = clk_get_rate(dwsmmio->clk);
  60. device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
  61. num_cs = 4;
  62. device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
  63. dws->num_cs = num_cs;
  64. if (pdev->dev.of_node) {
  65. int i;
  66. for (i = 0; i < dws->num_cs; i++) {
  67. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  68. "cs-gpios", i);
  69. if (cs_gpio == -EPROBE_DEFER) {
  70. ret = cs_gpio;
  71. goto out;
  72. }
  73. if (gpio_is_valid(cs_gpio)) {
  74. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  75. dev_name(&pdev->dev));
  76. if (ret)
  77. goto out;
  78. }
  79. }
  80. }
  81. ret = dw_spi_add_host(&pdev->dev, dws);
  82. if (ret)
  83. goto out;
  84. platform_set_drvdata(pdev, dwsmmio);
  85. return 0;
  86. out:
  87. clk_disable_unprepare(dwsmmio->clk);
  88. return ret;
  89. }
  90. static int dw_spi_mmio_remove(struct platform_device *pdev)
  91. {
  92. struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
  93. dw_spi_remove_host(&dwsmmio->dws);
  94. clk_disable_unprepare(dwsmmio->clk);
  95. return 0;
  96. }
  97. static const struct of_device_id dw_spi_mmio_of_match[] = {
  98. { .compatible = "snps,dw-apb-ssi", },
  99. { /* end of table */}
  100. };
  101. MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
  102. static struct platform_driver dw_spi_mmio_driver = {
  103. .probe = dw_spi_mmio_probe,
  104. .remove = dw_spi_mmio_remove,
  105. .driver = {
  106. .name = DRIVER_NAME,
  107. .of_match_table = dw_spi_mmio_of_match,
  108. },
  109. };
  110. module_platform_driver(dw_spi_mmio_driver);
  111. MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
  112. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
  113. MODULE_LICENSE("GPL v2");