spi-dw-mmio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if (!mem) {
  43. dev_err(&pdev->dev, "no mem resource?\n");
  44. return -EINVAL;
  45. }
  46. dws->regs = devm_ioremap_resource(&pdev->dev, mem);
  47. if (IS_ERR(dws->regs)) {
  48. dev_err(&pdev->dev, "SPI region map failed\n");
  49. return PTR_ERR(dws->regs);
  50. }
  51. dws->irq = platform_get_irq(pdev, 0);
  52. if (dws->irq < 0) {
  53. dev_err(&pdev->dev, "no irq resource?\n");
  54. return dws->irq; /* -ENXIO */
  55. }
  56. dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
  57. if (IS_ERR(dwsmmio->clk))
  58. return PTR_ERR(dwsmmio->clk);
  59. ret = clk_prepare_enable(dwsmmio->clk);
  60. if (ret)
  61. return ret;
  62. dws->bus_num = pdev->id;
  63. dws->max_freq = clk_get_rate(dwsmmio->clk);
  64. device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
  65. num_cs = 4;
  66. device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
  67. dws->num_cs = num_cs;
  68. if (pdev->dev.of_node) {
  69. int i;
  70. for (i = 0; i < dws->num_cs; i++) {
  71. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  72. "cs-gpios", i);
  73. if (cs_gpio == -EPROBE_DEFER) {
  74. ret = cs_gpio;
  75. goto out;
  76. }
  77. if (gpio_is_valid(cs_gpio)) {
  78. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  79. dev_name(&pdev->dev));
  80. if (ret)
  81. goto out;
  82. }
  83. }
  84. }
  85. ret = dw_spi_add_host(&pdev->dev, dws);
  86. if (ret)
  87. goto out;
  88. platform_set_drvdata(pdev, dwsmmio);
  89. return 0;
  90. out:
  91. clk_disable_unprepare(dwsmmio->clk);
  92. return ret;
  93. }
  94. static int dw_spi_mmio_remove(struct platform_device *pdev)
  95. {
  96. struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
  97. dw_spi_remove_host(&dwsmmio->dws);
  98. clk_disable_unprepare(dwsmmio->clk);
  99. return 0;
  100. }
  101. static const struct of_device_id dw_spi_mmio_of_match[] = {
  102. { .compatible = "snps,dw-apb-ssi", },
  103. { /* end of table */}
  104. };
  105. MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
  106. static struct platform_driver dw_spi_mmio_driver = {
  107. .probe = dw_spi_mmio_probe,
  108. .remove = dw_spi_mmio_remove,
  109. .driver = {
  110. .name = DRIVER_NAME,
  111. .of_match_table = dw_spi_mmio_of_match,
  112. },
  113. };
  114. module_platform_driver(dw_spi_mmio_driver);
  115. MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
  116. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
  117. MODULE_LICENSE("GPL v2");