PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/spi/dw_spi_mmio.c

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