ssp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/io.h>
  31. #include <asm/irq.h>
  32. #include <mach/hardware.h>
  33. #include <mach/ssp.h>
  34. #include <mach/regs-ssp.h>
  35. static DEFINE_MUTEX(ssp_lock);
  36. static LIST_HEAD(ssp_list);
  37. struct ssp_device *ssp_request(int port, const char *label)
  38. {
  39. struct ssp_device *ssp = NULL;
  40. mutex_lock(&ssp_lock);
  41. list_for_each_entry(ssp, &ssp_list, node) {
  42. if (ssp->port_id == port && ssp->use_count == 0) {
  43. ssp->use_count++;
  44. ssp->label = label;
  45. break;
  46. }
  47. }
  48. mutex_unlock(&ssp_lock);
  49. if (&ssp->node == &ssp_list)
  50. return NULL;
  51. return ssp;
  52. }
  53. EXPORT_SYMBOL(ssp_request);
  54. void ssp_free(struct ssp_device *ssp)
  55. {
  56. mutex_lock(&ssp_lock);
  57. if (ssp->use_count) {
  58. ssp->use_count--;
  59. ssp->label = NULL;
  60. } else
  61. dev_err(&ssp->pdev->dev, "device already free\n");
  62. mutex_unlock(&ssp_lock);
  63. }
  64. EXPORT_SYMBOL(ssp_free);
  65. static int __devinit ssp_probe(struct platform_device *pdev)
  66. {
  67. const struct platform_device_id *id = platform_get_device_id(pdev);
  68. struct resource *res;
  69. struct ssp_device *ssp;
  70. int ret = 0;
  71. ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
  72. if (ssp == NULL) {
  73. dev_err(&pdev->dev, "failed to allocate memory");
  74. return -ENOMEM;
  75. }
  76. ssp->pdev = pdev;
  77. ssp->clk = clk_get(&pdev->dev, NULL);
  78. if (IS_ERR(ssp->clk)) {
  79. ret = PTR_ERR(ssp->clk);
  80. goto err_free;
  81. }
  82. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. if (res == NULL) {
  84. dev_err(&pdev->dev, "no memory resource defined\n");
  85. ret = -ENODEV;
  86. goto err_free_clk;
  87. }
  88. res = request_mem_region(res->start, res->end - res->start + 1,
  89. pdev->name);
  90. if (res == NULL) {
  91. dev_err(&pdev->dev, "failed to request memory resource\n");
  92. ret = -EBUSY;
  93. goto err_free_clk;
  94. }
  95. ssp->phys_base = res->start;
  96. ssp->mmio_base = ioremap(res->start, res->end - res->start + 1);
  97. if (ssp->mmio_base == NULL) {
  98. dev_err(&pdev->dev, "failed to ioremap() registers\n");
  99. ret = -ENODEV;
  100. goto err_free_mem;
  101. }
  102. ssp->irq = platform_get_irq(pdev, 0);
  103. if (ssp->irq < 0) {
  104. dev_err(&pdev->dev, "no IRQ resource defined\n");
  105. ret = -ENODEV;
  106. goto err_free_io;
  107. }
  108. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  109. if (res == NULL) {
  110. dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
  111. ret = -ENODEV;
  112. goto err_free_io;
  113. }
  114. ssp->drcmr_rx = res->start;
  115. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  116. if (res == NULL) {
  117. dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
  118. ret = -ENODEV;
  119. goto err_free_io;
  120. }
  121. ssp->drcmr_tx = res->start;
  122. /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
  123. * starts from 0, do a translation here
  124. */
  125. ssp->port_id = pdev->id + 1;
  126. ssp->use_count = 0;
  127. ssp->type = (int)id->driver_data;
  128. mutex_lock(&ssp_lock);
  129. list_add(&ssp->node, &ssp_list);
  130. mutex_unlock(&ssp_lock);
  131. platform_set_drvdata(pdev, ssp);
  132. return 0;
  133. err_free_io:
  134. iounmap(ssp->mmio_base);
  135. err_free_mem:
  136. release_mem_region(res->start, res->end - res->start + 1);
  137. err_free_clk:
  138. clk_put(ssp->clk);
  139. err_free:
  140. kfree(ssp);
  141. return ret;
  142. }
  143. static int __devexit ssp_remove(struct platform_device *pdev)
  144. {
  145. struct resource *res;
  146. struct ssp_device *ssp;
  147. ssp = platform_get_drvdata(pdev);
  148. if (ssp == NULL)
  149. return -ENODEV;
  150. iounmap(ssp->mmio_base);
  151. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. release_mem_region(res->start, res->end - res->start + 1);
  153. clk_put(ssp->clk);
  154. mutex_lock(&ssp_lock);
  155. list_del(&ssp->node);
  156. mutex_unlock(&ssp_lock);
  157. kfree(ssp);
  158. return 0;
  159. }
  160. static const struct platform_device_id ssp_id_table[] = {
  161. { "pxa25x-ssp", PXA25x_SSP },
  162. { "pxa25x-nssp", PXA25x_NSSP },
  163. { "pxa27x-ssp", PXA27x_SSP },
  164. { },
  165. };
  166. static struct platform_driver ssp_driver = {
  167. .probe = ssp_probe,
  168. .remove = __devexit_p(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(&ssp_driver);
  178. }
  179. static void __exit pxa_ssp_exit(void)
  180. {
  181. platform_driver_unregister(&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");