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 "spi-dw.h"
  19. #define DRIVER_NAME "dw_spi_mmio"
  20. struct dw_spi_mmio {
  21. struct dw_spi dws;
  22. struct clk *clk;
  23. };
  24. static int __devinit dw_spi_mmio_probe(struct platform_device *pdev)
  25. {
  26. struct dw_spi_mmio *dwsmmio;
  27. struct dw_spi *dws;
  28. struct resource *mem, *ioarea;
  29. int ret;
  30. dwsmmio = kzalloc(sizeof(struct dw_spi_mmio), GFP_KERNEL);
  31. if (!dwsmmio) {
  32. ret = -ENOMEM;
  33. goto err_end;
  34. }
  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. ret = -EINVAL;
  41. goto err_kfree;
  42. }
  43. ioarea = request_mem_region(mem->start, resource_size(mem),
  44. pdev->name);
  45. if (!ioarea) {
  46. dev_err(&pdev->dev, "SPI region already claimed\n");
  47. ret = -EBUSY;
  48. goto err_kfree;
  49. }
  50. dws->regs = ioremap_nocache(mem->start, resource_size(mem));
  51. if (!dws->regs) {
  52. dev_err(&pdev->dev, "SPI region already mapped\n");
  53. ret = -ENOMEM;
  54. goto err_release_reg;
  55. }
  56. dws->irq = platform_get_irq(pdev, 0);
  57. if (dws->irq < 0) {
  58. dev_err(&pdev->dev, "no irq resource?\n");
  59. ret = dws->irq; /* -ENXIO */
  60. goto err_unmap;
  61. }
  62. dwsmmio->clk = clk_get(&pdev->dev, NULL);
  63. if (IS_ERR(dwsmmio->clk)) {
  64. ret = PTR_ERR(dwsmmio->clk);
  65. goto err_irq;
  66. }
  67. clk_enable(dwsmmio->clk);
  68. dws->parent_dev = &pdev->dev;
  69. dws->bus_num = 0;
  70. dws->num_cs = 4;
  71. dws->max_freq = clk_get_rate(dwsmmio->clk);
  72. ret = dw_spi_add_host(dws);
  73. if (ret)
  74. goto err_clk;
  75. platform_set_drvdata(pdev, dwsmmio);
  76. return 0;
  77. err_clk:
  78. clk_disable(dwsmmio->clk);
  79. clk_put(dwsmmio->clk);
  80. dwsmmio->clk = NULL;
  81. err_irq:
  82. free_irq(dws->irq, dws);
  83. err_unmap:
  84. iounmap(dws->regs);
  85. err_release_reg:
  86. release_mem_region(mem->start, resource_size(mem));
  87. err_kfree:
  88. kfree(dwsmmio);
  89. err_end:
  90. return ret;
  91. }
  92. static int __devexit dw_spi_mmio_remove(struct platform_device *pdev)
  93. {
  94. struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
  95. struct resource *mem;
  96. platform_set_drvdata(pdev, NULL);
  97. clk_disable(dwsmmio->clk);
  98. clk_put(dwsmmio->clk);
  99. dwsmmio->clk = NULL;
  100. free_irq(dwsmmio->dws.irq, &dwsmmio->dws);
  101. dw_spi_remove_host(&dwsmmio->dws);
  102. iounmap(dwsmmio->dws.regs);
  103. kfree(dwsmmio);
  104. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. release_mem_region(mem->start, resource_size(mem));
  106. return 0;
  107. }
  108. static struct platform_driver dw_spi_mmio_driver = {
  109. .probe = dw_spi_mmio_probe,
  110. .remove = __devexit_p(dw_spi_mmio_remove),
  111. .driver = {
  112. .name = DRIVER_NAME,
  113. .owner = THIS_MODULE,
  114. },
  115. };
  116. module_platform_driver(dw_spi_mmio_driver);
  117. MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
  118. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
  119. MODULE_LICENSE("GPL v2");