ssp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <asm/irq.h>
  35. #include <mach/hardware.h>
  36. static DEFINE_MUTEX(ssp_lock);
  37. static LIST_HEAD(ssp_list);
  38. struct ssp_device *pxa_ssp_request(int port, const char *label)
  39. {
  40. struct ssp_device *ssp = NULL;
  41. mutex_lock(&ssp_lock);
  42. list_for_each_entry(ssp, &ssp_list, node) {
  43. if (ssp->port_id == port && ssp->use_count == 0) {
  44. ssp->use_count++;
  45. ssp->label = label;
  46. break;
  47. }
  48. }
  49. mutex_unlock(&ssp_lock);
  50. if (&ssp->node == &ssp_list)
  51. return NULL;
  52. return ssp;
  53. }
  54. EXPORT_SYMBOL(pxa_ssp_request);
  55. void pxa_ssp_free(struct ssp_device *ssp)
  56. {
  57. mutex_lock(&ssp_lock);
  58. if (ssp->use_count) {
  59. ssp->use_count--;
  60. ssp->label = NULL;
  61. } else
  62. dev_err(&ssp->pdev->dev, "device already free\n");
  63. mutex_unlock(&ssp_lock);
  64. }
  65. EXPORT_SYMBOL(pxa_ssp_free);
  66. #ifdef CONFIG_OF
  67. static const struct of_device_id pxa_ssp_of_ids[] = {
  68. { .compatible = "mrvl,pxa25x-ssp", .data = (void *) PXA25x_SSP },
  69. { .compatible = "mvrl,pxa25x-nssp", .data = (void *) PXA25x_NSSP },
  70. { .compatible = "mrvl,pxa27x-ssp", .data = (void *) PXA27x_SSP },
  71. { .compatible = "mrvl,pxa3xx-ssp", .data = (void *) PXA3xx_SSP },
  72. { .compatible = "mvrl,pxa168-ssp", .data = (void *) PXA168_SSP },
  73. { .compatible = "mrvl,pxa910-ssp", .data = (void *) PXA910_SSP },
  74. { .compatible = "mrvl,ce4100-ssp", .data = (void *) CE4100_SSP },
  75. { .compatible = "mrvl,lpss-ssp", .data = (void *) LPSS_SSP },
  76. { },
  77. };
  78. MODULE_DEVICE_TABLE(of, pxa_ssp_of_ids);
  79. #endif
  80. static int pxa_ssp_probe(struct platform_device *pdev)
  81. {
  82. struct resource *res;
  83. struct ssp_device *ssp;
  84. struct device *dev = &pdev->dev;
  85. int ret = 0;
  86. ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
  87. if (ssp == NULL)
  88. return -ENOMEM;
  89. ssp->pdev = pdev;
  90. ssp->clk = clk_get(dev, NULL);
  91. if (IS_ERR(ssp->clk)) {
  92. ret = PTR_ERR(ssp->clk);
  93. goto err_free;
  94. }
  95. if (dev->of_node) {
  96. struct of_phandle_args dma_spec;
  97. struct device_node *np = dev->of_node;
  98. /*
  99. * FIXME: we should allocate the DMA channel from this
  100. * context and pass the channel down to the ssp users.
  101. * For now, we lookup the rx and tx indices manually
  102. */
  103. /* rx */
  104. of_parse_phandle_with_args(np, "dmas", "#dma-cells",
  105. 0, &dma_spec);
  106. ssp->drcmr_rx = dma_spec.args[0];
  107. of_node_put(dma_spec.np);
  108. /* tx */
  109. of_parse_phandle_with_args(np, "dmas", "#dma-cells",
  110. 1, &dma_spec);
  111. ssp->drcmr_tx = dma_spec.args[0];
  112. of_node_put(dma_spec.np);
  113. } else {
  114. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  115. if (res == NULL) {
  116. dev_err(dev, "no SSP RX DRCMR defined\n");
  117. return -ENODEV;
  118. }
  119. ssp->drcmr_rx = res->start;
  120. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  121. if (res == NULL) {
  122. dev_err(dev, "no SSP TX DRCMR defined\n");
  123. return -ENODEV;
  124. }
  125. ssp->drcmr_tx = res->start;
  126. }
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. if (res == NULL) {
  129. dev_err(dev, "no memory resource defined\n");
  130. ret = -ENODEV;
  131. goto err_free_clk;
  132. }
  133. res = request_mem_region(res->start, resource_size(res),
  134. pdev->name);
  135. if (res == NULL) {
  136. dev_err(dev, "failed to request memory resource\n");
  137. ret = -EBUSY;
  138. goto err_free_clk;
  139. }
  140. ssp->phys_base = res->start;
  141. ssp->mmio_base = ioremap(res->start, resource_size(res));
  142. if (ssp->mmio_base == NULL) {
  143. dev_err(dev, "failed to ioremap() registers\n");
  144. ret = -ENODEV;
  145. goto err_free_mem;
  146. }
  147. ssp->irq = platform_get_irq(pdev, 0);
  148. if (ssp->irq < 0) {
  149. dev_err(dev, "no IRQ resource defined\n");
  150. ret = -ENODEV;
  151. goto err_free_io;
  152. }
  153. if (dev->of_node) {
  154. const struct of_device_id *id =
  155. of_match_device(of_match_ptr(pxa_ssp_of_ids), dev);
  156. ssp->type = (int) id->data;
  157. } else {
  158. const struct platform_device_id *id =
  159. platform_get_device_id(pdev);
  160. ssp->type = (int) id->driver_data;
  161. /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
  162. * starts from 0, do a translation here
  163. */
  164. ssp->port_id = pdev->id + 1;
  165. }
  166. ssp->use_count = 0;
  167. mutex_lock(&ssp_lock);
  168. list_add(&ssp->node, &ssp_list);
  169. mutex_unlock(&ssp_lock);
  170. platform_set_drvdata(pdev, ssp);
  171. return 0;
  172. err_free_io:
  173. iounmap(ssp->mmio_base);
  174. err_free_mem:
  175. release_mem_region(res->start, resource_size(res));
  176. err_free_clk:
  177. clk_put(ssp->clk);
  178. err_free:
  179. kfree(ssp);
  180. return ret;
  181. }
  182. static int pxa_ssp_remove(struct platform_device *pdev)
  183. {
  184. struct resource *res;
  185. struct ssp_device *ssp;
  186. ssp = platform_get_drvdata(pdev);
  187. if (ssp == NULL)
  188. return -ENODEV;
  189. iounmap(ssp->mmio_base);
  190. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. release_mem_region(res->start, resource_size(res));
  192. clk_put(ssp->clk);
  193. mutex_lock(&ssp_lock);
  194. list_del(&ssp->node);
  195. mutex_unlock(&ssp_lock);
  196. kfree(ssp);
  197. return 0;
  198. }
  199. static const struct platform_device_id ssp_id_table[] = {
  200. { "pxa25x-ssp", PXA25x_SSP },
  201. { "pxa25x-nssp", PXA25x_NSSP },
  202. { "pxa27x-ssp", PXA27x_SSP },
  203. { "pxa168-ssp", PXA168_SSP },
  204. { "pxa910-ssp", PXA910_SSP },
  205. { },
  206. };
  207. static struct platform_driver pxa_ssp_driver = {
  208. .probe = pxa_ssp_probe,
  209. .remove = pxa_ssp_remove,
  210. .driver = {
  211. .owner = THIS_MODULE,
  212. .name = "pxa2xx-ssp",
  213. .of_match_table = of_match_ptr(pxa_ssp_of_ids),
  214. },
  215. .id_table = ssp_id_table,
  216. };
  217. static int __init pxa_ssp_init(void)
  218. {
  219. return platform_driver_register(&pxa_ssp_driver);
  220. }
  221. static void __exit pxa_ssp_exit(void)
  222. {
  223. platform_driver_unregister(&pxa_ssp_driver);
  224. }
  225. arch_initcall(pxa_ssp_init);
  226. module_exit(pxa_ssp_exit);
  227. MODULE_DESCRIPTION("PXA SSP driver");
  228. MODULE_AUTHOR("Liam Girdwood");
  229. MODULE_LICENSE("GPL");