atmel-ssc.c 5.9 KB

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