ssp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * linux/arch/arm/mach-pxa/ssp.c
  3. *
  4. * based on linux/arch/arm/mach-sa1100/ssp.c by Russell King
  5. *
  6. * Copyright (C) 2003 Russell King.
  7. * Copyright (C) 2003 Wolfson Microelectronics PLC
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * PXA2xx SSP driver. This provides the generic core for simple
  14. * IO-based SSP applications and allows easy port setup for DMA access.
  15. *
  16. * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioport.h>
  25. #include <linux/init.h>
  26. #include <linux/mutex.h>
  27. #include <linux/clk.h>
  28. #include <linux/err.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/spi/pxa2xx_spi.h>
  31. #include <linux/io.h>
  32. #include <asm/irq.h>
  33. #include <mach/hardware.h>
  34. static DEFINE_MUTEX(ssp_lock);
  35. static LIST_HEAD(ssp_list);
  36. struct ssp_device *pxa_ssp_request(int port, const char *label)
  37. {
  38. struct ssp_device *ssp = NULL;
  39. mutex_lock(&ssp_lock);
  40. list_for_each_entry(ssp, &ssp_list, node) {
  41. if (ssp->port_id == port && ssp->use_count == 0) {
  42. ssp->use_count++;
  43. ssp->label = label;
  44. break;
  45. }
  46. }
  47. mutex_unlock(&ssp_lock);
  48. if (&ssp->node == &ssp_list)
  49. return NULL;
  50. return ssp;
  51. }
  52. EXPORT_SYMBOL(pxa_ssp_request);
  53. void pxa_ssp_free(struct ssp_device *ssp)
  54. {
  55. mutex_lock(&ssp_lock);
  56. if (ssp->use_count) {
  57. ssp->use_count--;
  58. ssp->label = NULL;
  59. } else
  60. dev_err(&ssp->pdev->dev, "device already free\n");
  61. mutex_unlock(&ssp_lock);
  62. }
  63. EXPORT_SYMBOL(pxa_ssp_free);
  64. static int __devinit pxa_ssp_probe(struct platform_device *pdev)
  65. {
  66. const struct platform_device_id *id = platform_get_device_id(pdev);
  67. struct resource *res;
  68. struct ssp_device *ssp;
  69. int ret = 0;
  70. ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
  71. if (ssp == NULL) {
  72. dev_err(&pdev->dev, "failed to allocate memory");
  73. return -ENOMEM;
  74. }
  75. ssp->pdev = pdev;
  76. ssp->clk = clk_get(&pdev->dev, NULL);
  77. if (IS_ERR(ssp->clk)) {
  78. ret = PTR_ERR(ssp->clk);
  79. goto err_free;
  80. }
  81. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  82. if (res == NULL) {
  83. dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
  84. ret = -ENODEV;
  85. goto err_free_clk;
  86. }
  87. ssp->drcmr_rx = res->start;
  88. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  89. if (res == NULL) {
  90. dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
  91. ret = -ENODEV;
  92. goto err_free_clk;
  93. }
  94. ssp->drcmr_tx = res->start;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. if (res == NULL) {
  97. dev_err(&pdev->dev, "no memory resource defined\n");
  98. ret = -ENODEV;
  99. goto err_free_clk;
  100. }
  101. res = request_mem_region(res->start, resource_size(res),
  102. pdev->name);
  103. if (res == NULL) {
  104. dev_err(&pdev->dev, "failed to request memory resource\n");
  105. ret = -EBUSY;
  106. goto err_free_clk;
  107. }
  108. ssp->phys_base = res->start;
  109. ssp->mmio_base = ioremap(res->start, resource_size(res));
  110. if (ssp->mmio_base == NULL) {
  111. dev_err(&pdev->dev, "failed to ioremap() registers\n");
  112. ret = -ENODEV;
  113. goto err_free_mem;
  114. }
  115. ssp->irq = platform_get_irq(pdev, 0);
  116. if (ssp->irq < 0) {
  117. dev_err(&pdev->dev, "no IRQ resource defined\n");
  118. ret = -ENODEV;
  119. goto err_free_io;
  120. }
  121. /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
  122. * starts from 0, do a translation here
  123. */
  124. ssp->port_id = pdev->id + 1;
  125. ssp->use_count = 0;
  126. ssp->type = (int)id->driver_data;
  127. mutex_lock(&ssp_lock);
  128. list_add(&ssp->node, &ssp_list);
  129. mutex_unlock(&ssp_lock);
  130. platform_set_drvdata(pdev, ssp);
  131. return 0;
  132. err_free_io:
  133. iounmap(ssp->mmio_base);
  134. err_free_mem:
  135. release_mem_region(res->start, resource_size(res));
  136. err_free_clk:
  137. clk_put(ssp->clk);
  138. err_free:
  139. kfree(ssp);
  140. return ret;
  141. }
  142. static int __devexit pxa_ssp_remove(struct platform_device *pdev)
  143. {
  144. struct resource *res;
  145. struct ssp_device *ssp;
  146. ssp = platform_get_drvdata(pdev);
  147. if (ssp == NULL)
  148. return -ENODEV;
  149. iounmap(ssp->mmio_base);
  150. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  151. release_mem_region(res->start, resource_size(res));
  152. clk_put(ssp->clk);
  153. mutex_lock(&ssp_lock);
  154. list_del(&ssp->node);
  155. mutex_unlock(&ssp_lock);
  156. kfree(ssp);
  157. return 0;
  158. }
  159. static const struct platform_device_id ssp_id_table[] = {
  160. { "pxa25x-ssp", PXA25x_SSP },
  161. { "pxa25x-nssp", PXA25x_NSSP },
  162. { "pxa27x-ssp", PXA27x_SSP },
  163. { "pxa168-ssp", PXA168_SSP },
  164. { },
  165. };
  166. static struct platform_driver pxa_ssp_driver = {
  167. .probe = pxa_ssp_probe,
  168. .remove = __devexit_p(pxa_ssp_remove),
  169. .driver = {
  170. .owner = THIS_MODULE,
  171. .name = "pxa2xx-ssp",
  172. },
  173. .id_table = ssp_id_table,
  174. };
  175. static int __init pxa_ssp_init(void)
  176. {
  177. return platform_driver_register(&pxa_ssp_driver);
  178. }
  179. static void __exit pxa_ssp_exit(void)
  180. {
  181. platform_driver_unregister(&pxa_ssp_driver);
  182. }
  183. arch_initcall(pxa_ssp_init);
  184. module_exit(pxa_ssp_exit);
  185. MODULE_DESCRIPTION("PXA SSP driver");
  186. MODULE_AUTHOR("Liam Girdwood");
  187. MODULE_LICENSE("GPL");