ssp.c 6.6 KB

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