atmel-ssc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Atmel SSC driver
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/list.h>
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/mutex.h>
  16. #include <linux/atmel-ssc.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include "../../sound/soc/atmel/atmel_ssc_dai.h"
  21. /* Serialize access to ssc_list and user count */
  22. static DEFINE_MUTEX(user_lock);
  23. static LIST_HEAD(ssc_list);
  24. struct ssc_device *ssc_request(unsigned int ssc_num)
  25. {
  26. int ssc_valid = 0;
  27. struct ssc_device *ssc;
  28. mutex_lock(&user_lock);
  29. list_for_each_entry(ssc, &ssc_list, list) {
  30. if (ssc->pdev->dev.of_node) {
  31. if (of_alias_get_id(ssc->pdev->dev.of_node, "ssc")
  32. == ssc_num) {
  33. ssc->pdev->id = ssc_num;
  34. ssc_valid = 1;
  35. break;
  36. }
  37. } else if (ssc->pdev->id == ssc_num) {
  38. ssc_valid = 1;
  39. break;
  40. }
  41. }
  42. if (!ssc_valid) {
  43. mutex_unlock(&user_lock);
  44. pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
  45. return ERR_PTR(-ENODEV);
  46. }
  47. if (ssc->user) {
  48. mutex_unlock(&user_lock);
  49. dev_dbg(&ssc->pdev->dev, "module busy\n");
  50. return ERR_PTR(-EBUSY);
  51. }
  52. ssc->user++;
  53. mutex_unlock(&user_lock);
  54. clk_prepare(ssc->clk);
  55. return ssc;
  56. }
  57. EXPORT_SYMBOL(ssc_request);
  58. void ssc_free(struct ssc_device *ssc)
  59. {
  60. bool disable_clk = true;
  61. mutex_lock(&user_lock);
  62. if (ssc->user)
  63. ssc->user--;
  64. else {
  65. disable_clk = false;
  66. dev_dbg(&ssc->pdev->dev, "device already free\n");
  67. }
  68. mutex_unlock(&user_lock);
  69. if (disable_clk)
  70. clk_unprepare(ssc->clk);
  71. }
  72. EXPORT_SYMBOL(ssc_free);
  73. static struct atmel_ssc_platform_data at91rm9200_config = {
  74. .use_dma = 0,
  75. .has_fslen_ext = 0,
  76. };
  77. static struct atmel_ssc_platform_data at91sam9rl_config = {
  78. .use_dma = 0,
  79. .has_fslen_ext = 1,
  80. };
  81. static struct atmel_ssc_platform_data at91sam9g45_config = {
  82. .use_dma = 1,
  83. .has_fslen_ext = 1,
  84. };
  85. static const struct platform_device_id atmel_ssc_devtypes[] = {
  86. {
  87. .name = "at91rm9200_ssc",
  88. .driver_data = (unsigned long) &at91rm9200_config,
  89. }, {
  90. .name = "at91sam9rl_ssc",
  91. .driver_data = (unsigned long) &at91sam9rl_config,
  92. }, {
  93. .name = "at91sam9g45_ssc",
  94. .driver_data = (unsigned long) &at91sam9g45_config,
  95. }, {
  96. /* sentinel */
  97. }
  98. };
  99. #ifdef CONFIG_OF
  100. static const struct of_device_id atmel_ssc_dt_ids[] = {
  101. {
  102. .compatible = "atmel,at91rm9200-ssc",
  103. .data = &at91rm9200_config,
  104. }, {
  105. .compatible = "atmel,at91sam9rl-ssc",
  106. .data = &at91sam9rl_config,
  107. }, {
  108. .compatible = "atmel,at91sam9g45-ssc",
  109. .data = &at91sam9g45_config,
  110. }, {
  111. /* sentinel */
  112. }
  113. };
  114. MODULE_DEVICE_TABLE(of, atmel_ssc_dt_ids);
  115. #endif
  116. static inline const struct atmel_ssc_platform_data *
  117. atmel_ssc_get_driver_data(struct platform_device *pdev)
  118. {
  119. if (pdev->dev.of_node) {
  120. const struct of_device_id *match;
  121. match = of_match_node(atmel_ssc_dt_ids, pdev->dev.of_node);
  122. if (match == NULL)
  123. return NULL;
  124. return match->data;
  125. }
  126. return (struct atmel_ssc_platform_data *)
  127. platform_get_device_id(pdev)->driver_data;
  128. }
  129. #ifdef CONFIG_SND_ATMEL_SOC_SSC
  130. static int ssc_sound_dai_probe(struct ssc_device *ssc)
  131. {
  132. struct device_node *np = ssc->pdev->dev.of_node;
  133. int ret;
  134. int id;
  135. ssc->sound_dai = false;
  136. if (!of_property_read_bool(np, "#sound-dai-cells"))
  137. return 0;
  138. id = of_alias_get_id(np, "ssc");
  139. if (id < 0)
  140. return id;
  141. ret = atmel_ssc_set_audio(id);
  142. ssc->sound_dai = !ret;
  143. return ret;
  144. }
  145. static void ssc_sound_dai_remove(struct ssc_device *ssc)
  146. {
  147. if (!ssc->sound_dai)
  148. return;
  149. atmel_ssc_put_audio(of_alias_get_id(ssc->pdev->dev.of_node, "ssc"));
  150. }
  151. #else
  152. static inline int ssc_sound_dai_probe(struct ssc_device *ssc)
  153. {
  154. if (of_property_read_bool(ssc->pdev->dev.of_node, "#sound-dai-cells"))
  155. return -ENOTSUPP;
  156. return 0;
  157. }
  158. static inline void ssc_sound_dai_remove(struct ssc_device *ssc)
  159. {
  160. }
  161. #endif
  162. static int ssc_probe(struct platform_device *pdev)
  163. {
  164. struct resource *regs;
  165. struct ssc_device *ssc;
  166. const struct atmel_ssc_platform_data *plat_dat;
  167. ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
  168. if (!ssc) {
  169. dev_dbg(&pdev->dev, "out of memory\n");
  170. return -ENOMEM;
  171. }
  172. ssc->pdev = pdev;
  173. plat_dat = atmel_ssc_get_driver_data(pdev);
  174. if (!plat_dat)
  175. return -ENODEV;
  176. ssc->pdata = (struct atmel_ssc_platform_data *)plat_dat;
  177. if (pdev->dev.of_node) {
  178. struct device_node *np = pdev->dev.of_node;
  179. ssc->clk_from_rk_pin =
  180. of_property_read_bool(np, "atmel,clk-from-rk-pin");
  181. }
  182. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  183. ssc->regs = devm_ioremap_resource(&pdev->dev, regs);
  184. if (IS_ERR(ssc->regs))
  185. return PTR_ERR(ssc->regs);
  186. ssc->phybase = regs->start;
  187. ssc->clk = devm_clk_get(&pdev->dev, "pclk");
  188. if (IS_ERR(ssc->clk)) {
  189. dev_dbg(&pdev->dev, "no pclk clock defined\n");
  190. return -ENXIO;
  191. }
  192. /* disable all interrupts */
  193. clk_prepare_enable(ssc->clk);
  194. ssc_writel(ssc->regs, IDR, -1);
  195. ssc_readl(ssc->regs, SR);
  196. clk_disable_unprepare(ssc->clk);
  197. ssc->irq = platform_get_irq(pdev, 0);
  198. if (!ssc->irq) {
  199. dev_dbg(&pdev->dev, "could not get irq\n");
  200. return -ENXIO;
  201. }
  202. mutex_lock(&user_lock);
  203. list_add_tail(&ssc->list, &ssc_list);
  204. mutex_unlock(&user_lock);
  205. platform_set_drvdata(pdev, ssc);
  206. dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
  207. ssc->regs, ssc->irq);
  208. if (ssc_sound_dai_probe(ssc))
  209. dev_err(&pdev->dev, "failed to auto-setup ssc for audio\n");
  210. return 0;
  211. }
  212. static int ssc_remove(struct platform_device *pdev)
  213. {
  214. struct ssc_device *ssc = platform_get_drvdata(pdev);
  215. ssc_sound_dai_remove(ssc);
  216. mutex_lock(&user_lock);
  217. list_del(&ssc->list);
  218. mutex_unlock(&user_lock);
  219. return 0;
  220. }
  221. static struct platform_driver ssc_driver = {
  222. .driver = {
  223. .name = "ssc",
  224. .of_match_table = of_match_ptr(atmel_ssc_dt_ids),
  225. },
  226. .id_table = atmel_ssc_devtypes,
  227. .probe = ssc_probe,
  228. .remove = ssc_remove,
  229. };
  230. module_platform_driver(ssc_driver);
  231. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  232. MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
  233. MODULE_LICENSE("GPL");
  234. MODULE_ALIAS("platform:ssc");