dw_spi_mmio.c 3.4 KB

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