soc-ac97.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * soc-ac97.c -- ALSA SoC Audio Layer AC97 support
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. * Copyright (C) 2010 Slimlogic Ltd.
  7. * Copyright (C) 2010 Texas Instruments Inc.
  8. *
  9. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  10. * with code, comments and ideas from :-
  11. * Richard Purdie <richard@openedhand.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/ctype.h>
  19. #include <linux/delay.h>
  20. #include <linux/export.h>
  21. #include <linux/gpio.h>
  22. #include <linux/init.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/slab.h>
  27. #include <sound/ac97_codec.h>
  28. #include <sound/soc.h>
  29. struct snd_ac97_reset_cfg {
  30. struct pinctrl *pctl;
  31. struct pinctrl_state *pstate_reset;
  32. struct pinctrl_state *pstate_warm_reset;
  33. struct pinctrl_state *pstate_run;
  34. int gpio_sdata;
  35. int gpio_sync;
  36. int gpio_reset;
  37. };
  38. static struct snd_ac97_bus soc_ac97_bus = {
  39. .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
  40. };
  41. static void soc_ac97_device_release(struct device *dev)
  42. {
  43. kfree(to_ac97_t(dev));
  44. }
  45. /**
  46. * snd_soc_alloc_ac97_codec() - Allocate new a AC'97 device
  47. * @codec: The CODEC for which to create the AC'97 device
  48. *
  49. * Allocated a new snd_ac97 device and intializes it, but does not yet register
  50. * it. The caller is responsible to either call device_add(&ac97->dev) to
  51. * register the device, or to call put_device(&ac97->dev) to free the device.
  52. *
  53. * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
  54. */
  55. struct snd_ac97 *snd_soc_alloc_ac97_codec(struct snd_soc_codec *codec)
  56. {
  57. struct snd_ac97 *ac97;
  58. ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  59. if (ac97 == NULL)
  60. return ERR_PTR(-ENOMEM);
  61. ac97->bus = &soc_ac97_bus;
  62. ac97->num = 0;
  63. ac97->dev.bus = &ac97_bus_type;
  64. ac97->dev.parent = codec->component.card->dev;
  65. ac97->dev.release = soc_ac97_device_release;
  66. dev_set_name(&ac97->dev, "%d-%d:%s",
  67. codec->component.card->snd_card->number, 0,
  68. codec->component.name);
  69. device_initialize(&ac97->dev);
  70. return ac97;
  71. }
  72. EXPORT_SYMBOL(snd_soc_alloc_ac97_codec);
  73. /**
  74. * snd_soc_new_ac97_codec - initailise AC97 device
  75. * @codec: audio codec
  76. * @id: The expected device ID
  77. * @id_mask: Mask that is applied to the device ID before comparing with @id
  78. *
  79. * Initialises AC97 codec resources for use by ad-hoc devices only.
  80. *
  81. * If @id is not 0 this function will reset the device, then read the ID from
  82. * the device and check if it matches the expected ID. If it doesn't match an
  83. * error will be returned and device will not be registered.
  84. *
  85. * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
  86. */
  87. struct snd_ac97 *snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  88. unsigned int id, unsigned int id_mask)
  89. {
  90. struct snd_ac97 *ac97;
  91. int ret;
  92. ac97 = snd_soc_alloc_ac97_codec(codec);
  93. if (IS_ERR(ac97))
  94. return ac97;
  95. if (id) {
  96. ret = snd_ac97_reset(ac97, false, id, id_mask);
  97. if (ret < 0) {
  98. dev_err(codec->dev, "Failed to reset AC97 device: %d\n",
  99. ret);
  100. goto err_put_device;
  101. }
  102. }
  103. ret = device_add(&ac97->dev);
  104. if (ret)
  105. goto err_put_device;
  106. return ac97;
  107. err_put_device:
  108. put_device(&ac97->dev);
  109. return ERR_PTR(ret);
  110. }
  111. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  112. /**
  113. * snd_soc_free_ac97_codec - free AC97 codec device
  114. * @codec: audio codec
  115. *
  116. * Frees AC97 codec device resources.
  117. */
  118. void snd_soc_free_ac97_codec(struct snd_ac97 *ac97)
  119. {
  120. device_del(&ac97->dev);
  121. ac97->bus = NULL;
  122. put_device(&ac97->dev);
  123. }
  124. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  125. static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
  126. static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
  127. {
  128. struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
  129. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
  130. gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
  131. udelay(10);
  132. gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
  133. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
  134. msleep(2);
  135. }
  136. static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
  137. {
  138. struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
  139. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
  140. gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
  141. gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
  142. gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
  143. udelay(10);
  144. gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
  145. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
  146. msleep(2);
  147. }
  148. static int snd_soc_ac97_parse_pinctl(struct device *dev,
  149. struct snd_ac97_reset_cfg *cfg)
  150. {
  151. struct pinctrl *p;
  152. struct pinctrl_state *state;
  153. int gpio;
  154. int ret;
  155. p = devm_pinctrl_get(dev);
  156. if (IS_ERR(p)) {
  157. dev_err(dev, "Failed to get pinctrl\n");
  158. return PTR_ERR(p);
  159. }
  160. cfg->pctl = p;
  161. state = pinctrl_lookup_state(p, "ac97-reset");
  162. if (IS_ERR(state)) {
  163. dev_err(dev, "Can't find pinctrl state ac97-reset\n");
  164. return PTR_ERR(state);
  165. }
  166. cfg->pstate_reset = state;
  167. state = pinctrl_lookup_state(p, "ac97-warm-reset");
  168. if (IS_ERR(state)) {
  169. dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
  170. return PTR_ERR(state);
  171. }
  172. cfg->pstate_warm_reset = state;
  173. state = pinctrl_lookup_state(p, "ac97-running");
  174. if (IS_ERR(state)) {
  175. dev_err(dev, "Can't find pinctrl state ac97-running\n");
  176. return PTR_ERR(state);
  177. }
  178. cfg->pstate_run = state;
  179. gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
  180. if (gpio < 0) {
  181. dev_err(dev, "Can't find ac97-sync gpio\n");
  182. return gpio;
  183. }
  184. ret = devm_gpio_request(dev, gpio, "AC97 link sync");
  185. if (ret) {
  186. dev_err(dev, "Failed requesting ac97-sync gpio\n");
  187. return ret;
  188. }
  189. cfg->gpio_sync = gpio;
  190. gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
  191. if (gpio < 0) {
  192. dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
  193. return gpio;
  194. }
  195. ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
  196. if (ret) {
  197. dev_err(dev, "Failed requesting ac97-sdata gpio\n");
  198. return ret;
  199. }
  200. cfg->gpio_sdata = gpio;
  201. gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
  202. if (gpio < 0) {
  203. dev_err(dev, "Can't find ac97-reset gpio\n");
  204. return gpio;
  205. }
  206. ret = devm_gpio_request(dev, gpio, "AC97 link reset");
  207. if (ret) {
  208. dev_err(dev, "Failed requesting ac97-reset gpio\n");
  209. return ret;
  210. }
  211. cfg->gpio_reset = gpio;
  212. return 0;
  213. }
  214. struct snd_ac97_bus_ops *soc_ac97_ops;
  215. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  216. int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
  217. {
  218. if (ops == soc_ac97_ops)
  219. return 0;
  220. if (soc_ac97_ops && ops)
  221. return -EBUSY;
  222. soc_ac97_ops = ops;
  223. soc_ac97_bus.ops = ops;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
  227. /**
  228. * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
  229. *
  230. * This function sets the reset and warm_reset properties of ops and parses
  231. * the device node of pdev to get pinctrl states and gpio numbers to use.
  232. */
  233. int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
  234. struct platform_device *pdev)
  235. {
  236. struct device *dev = &pdev->dev;
  237. struct snd_ac97_reset_cfg cfg;
  238. int ret;
  239. ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
  240. if (ret)
  241. return ret;
  242. ret = snd_soc_set_ac97_ops(ops);
  243. if (ret)
  244. return ret;
  245. ops->warm_reset = snd_soc_ac97_warm_reset;
  246. ops->reset = snd_soc_ac97_reset;
  247. snd_ac97_rst_cfg = cfg;
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);