soc-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/gpio/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/of.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/slab.h>
  28. #include <sound/ac97_codec.h>
  29. #include <sound/soc.h>
  30. struct snd_ac97_reset_cfg {
  31. struct pinctrl *pctl;
  32. struct pinctrl_state *pstate_reset;
  33. struct pinctrl_state *pstate_warm_reset;
  34. struct pinctrl_state *pstate_run;
  35. int gpio_sdata;
  36. int gpio_sync;
  37. int gpio_reset;
  38. };
  39. struct snd_ac97_gpio_priv {
  40. #ifdef CONFIG_GPIOLIB
  41. struct gpio_chip gpio_chip;
  42. #endif
  43. unsigned int gpios_set;
  44. struct snd_soc_codec *codec;
  45. };
  46. static struct snd_ac97_bus soc_ac97_bus = {
  47. .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
  48. };
  49. static void soc_ac97_device_release(struct device *dev)
  50. {
  51. kfree(to_ac97_t(dev));
  52. }
  53. #ifdef CONFIG_GPIOLIB
  54. static inline struct snd_soc_codec *gpio_to_codec(struct gpio_chip *chip)
  55. {
  56. struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
  57. return gpio_priv->codec;
  58. }
  59. static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
  60. {
  61. if (offset >= AC97_NUM_GPIOS)
  62. return -EINVAL;
  63. return 0;
  64. }
  65. static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
  66. unsigned offset)
  67. {
  68. struct snd_soc_codec *codec = gpio_to_codec(chip);
  69. dev_dbg(codec->dev, "set gpio %d to output\n", offset);
  70. return snd_soc_update_bits(codec, AC97_GPIO_CFG,
  71. 1 << offset, 1 << offset);
  72. }
  73. static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
  74. {
  75. struct snd_soc_codec *codec = gpio_to_codec(chip);
  76. int ret;
  77. ret = snd_soc_read(codec, AC97_GPIO_STATUS);
  78. dev_dbg(codec->dev, "get gpio %d : %d\n", offset,
  79. ret < 0 ? ret : ret & (1 << offset));
  80. return ret < 0 ? ret : !!(ret & (1 << offset));
  81. }
  82. static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
  83. int value)
  84. {
  85. struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
  86. struct snd_soc_codec *codec = gpio_to_codec(chip);
  87. gpio_priv->gpios_set &= ~(1 << offset);
  88. gpio_priv->gpios_set |= (!!value) << offset;
  89. snd_soc_write(codec, AC97_GPIO_STATUS, gpio_priv->gpios_set);
  90. dev_dbg(codec->dev, "set gpio %d to %d\n", offset, !!value);
  91. }
  92. static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
  93. unsigned offset, int value)
  94. {
  95. struct snd_soc_codec *codec = gpio_to_codec(chip);
  96. dev_dbg(codec->dev, "set gpio %d to output\n", offset);
  97. snd_soc_ac97_gpio_set(chip, offset, value);
  98. return snd_soc_update_bits(codec, AC97_GPIO_CFG, 1 << offset, 0);
  99. }
  100. static const struct gpio_chip snd_soc_ac97_gpio_chip = {
  101. .label = "snd_soc_ac97",
  102. .owner = THIS_MODULE,
  103. .request = snd_soc_ac97_gpio_request,
  104. .direction_input = snd_soc_ac97_gpio_direction_in,
  105. .get = snd_soc_ac97_gpio_get,
  106. .direction_output = snd_soc_ac97_gpio_direction_out,
  107. .set = snd_soc_ac97_gpio_set,
  108. .can_sleep = 1,
  109. };
  110. static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
  111. struct snd_soc_codec *codec)
  112. {
  113. struct snd_ac97_gpio_priv *gpio_priv;
  114. int ret;
  115. gpio_priv = devm_kzalloc(codec->dev, sizeof(*gpio_priv), GFP_KERNEL);
  116. if (!gpio_priv)
  117. return -ENOMEM;
  118. ac97->gpio_priv = gpio_priv;
  119. gpio_priv->codec = codec;
  120. gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
  121. gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
  122. gpio_priv->gpio_chip.parent = codec->dev;
  123. gpio_priv->gpio_chip.base = -1;
  124. ret = gpiochip_add_data(&gpio_priv->gpio_chip, gpio_priv);
  125. if (ret != 0)
  126. dev_err(codec->dev, "Failed to add GPIOs: %d\n", ret);
  127. return ret;
  128. }
  129. static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
  130. {
  131. gpiochip_remove(&ac97->gpio_priv->gpio_chip);
  132. }
  133. #else
  134. static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
  135. struct snd_soc_codec *codec)
  136. {
  137. return 0;
  138. }
  139. static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
  140. {
  141. }
  142. #endif
  143. /**
  144. * snd_soc_alloc_ac97_codec() - Allocate new a AC'97 device
  145. * @codec: The CODEC for which to create the AC'97 device
  146. *
  147. * Allocated a new snd_ac97 device and intializes it, but does not yet register
  148. * it. The caller is responsible to either call device_add(&ac97->dev) to
  149. * register the device, or to call put_device(&ac97->dev) to free the device.
  150. *
  151. * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
  152. */
  153. struct snd_ac97 *snd_soc_alloc_ac97_codec(struct snd_soc_codec *codec)
  154. {
  155. struct snd_ac97 *ac97;
  156. ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  157. if (ac97 == NULL)
  158. return ERR_PTR(-ENOMEM);
  159. ac97->bus = &soc_ac97_bus;
  160. ac97->num = 0;
  161. ac97->dev.bus = &ac97_bus_type;
  162. ac97->dev.parent = codec->component.card->dev;
  163. ac97->dev.release = soc_ac97_device_release;
  164. dev_set_name(&ac97->dev, "%d-%d:%s",
  165. codec->component.card->snd_card->number, 0,
  166. codec->component.name);
  167. device_initialize(&ac97->dev);
  168. return ac97;
  169. }
  170. EXPORT_SYMBOL(snd_soc_alloc_ac97_codec);
  171. /**
  172. * snd_soc_new_ac97_codec - initailise AC97 device
  173. * @codec: audio codec
  174. * @id: The expected device ID
  175. * @id_mask: Mask that is applied to the device ID before comparing with @id
  176. *
  177. * Initialises AC97 codec resources for use by ad-hoc devices only.
  178. *
  179. * If @id is not 0 this function will reset the device, then read the ID from
  180. * the device and check if it matches the expected ID. If it doesn't match an
  181. * error will be returned and device will not be registered.
  182. *
  183. * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
  184. */
  185. struct snd_ac97 *snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  186. unsigned int id, unsigned int id_mask)
  187. {
  188. struct snd_ac97 *ac97;
  189. int ret;
  190. ac97 = snd_soc_alloc_ac97_codec(codec);
  191. if (IS_ERR(ac97))
  192. return ac97;
  193. if (id) {
  194. ret = snd_ac97_reset(ac97, false, id, id_mask);
  195. if (ret < 0) {
  196. dev_err(codec->dev, "Failed to reset AC97 device: %d\n",
  197. ret);
  198. goto err_put_device;
  199. }
  200. }
  201. ret = device_add(&ac97->dev);
  202. if (ret)
  203. goto err_put_device;
  204. ret = snd_soc_ac97_init_gpio(ac97, codec);
  205. if (ret)
  206. goto err_put_device;
  207. return ac97;
  208. err_put_device:
  209. put_device(&ac97->dev);
  210. return ERR_PTR(ret);
  211. }
  212. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  213. /**
  214. * snd_soc_free_ac97_codec - free AC97 codec device
  215. * @codec: audio codec
  216. *
  217. * Frees AC97 codec device resources.
  218. */
  219. void snd_soc_free_ac97_codec(struct snd_ac97 *ac97)
  220. {
  221. snd_soc_ac97_free_gpio(ac97);
  222. device_del(&ac97->dev);
  223. ac97->bus = NULL;
  224. put_device(&ac97->dev);
  225. }
  226. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  227. static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
  228. static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
  229. {
  230. struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
  231. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
  232. gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
  233. udelay(10);
  234. gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
  235. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
  236. msleep(2);
  237. }
  238. static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
  239. {
  240. struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
  241. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
  242. gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
  243. gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
  244. gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
  245. udelay(10);
  246. gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
  247. pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
  248. msleep(2);
  249. }
  250. static int snd_soc_ac97_parse_pinctl(struct device *dev,
  251. struct snd_ac97_reset_cfg *cfg)
  252. {
  253. struct pinctrl *p;
  254. struct pinctrl_state *state;
  255. int gpio;
  256. int ret;
  257. p = devm_pinctrl_get(dev);
  258. if (IS_ERR(p)) {
  259. dev_err(dev, "Failed to get pinctrl\n");
  260. return PTR_ERR(p);
  261. }
  262. cfg->pctl = p;
  263. state = pinctrl_lookup_state(p, "ac97-reset");
  264. if (IS_ERR(state)) {
  265. dev_err(dev, "Can't find pinctrl state ac97-reset\n");
  266. return PTR_ERR(state);
  267. }
  268. cfg->pstate_reset = state;
  269. state = pinctrl_lookup_state(p, "ac97-warm-reset");
  270. if (IS_ERR(state)) {
  271. dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
  272. return PTR_ERR(state);
  273. }
  274. cfg->pstate_warm_reset = state;
  275. state = pinctrl_lookup_state(p, "ac97-running");
  276. if (IS_ERR(state)) {
  277. dev_err(dev, "Can't find pinctrl state ac97-running\n");
  278. return PTR_ERR(state);
  279. }
  280. cfg->pstate_run = state;
  281. gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
  282. if (gpio < 0) {
  283. dev_err(dev, "Can't find ac97-sync gpio\n");
  284. return gpio;
  285. }
  286. ret = devm_gpio_request(dev, gpio, "AC97 link sync");
  287. if (ret) {
  288. dev_err(dev, "Failed requesting ac97-sync gpio\n");
  289. return ret;
  290. }
  291. cfg->gpio_sync = gpio;
  292. gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
  293. if (gpio < 0) {
  294. dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
  295. return gpio;
  296. }
  297. ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
  298. if (ret) {
  299. dev_err(dev, "Failed requesting ac97-sdata gpio\n");
  300. return ret;
  301. }
  302. cfg->gpio_sdata = gpio;
  303. gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
  304. if (gpio < 0) {
  305. dev_err(dev, "Can't find ac97-reset gpio\n");
  306. return gpio;
  307. }
  308. ret = devm_gpio_request(dev, gpio, "AC97 link reset");
  309. if (ret) {
  310. dev_err(dev, "Failed requesting ac97-reset gpio\n");
  311. return ret;
  312. }
  313. cfg->gpio_reset = gpio;
  314. return 0;
  315. }
  316. struct snd_ac97_bus_ops *soc_ac97_ops;
  317. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  318. int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
  319. {
  320. if (ops == soc_ac97_ops)
  321. return 0;
  322. if (soc_ac97_ops && ops)
  323. return -EBUSY;
  324. soc_ac97_ops = ops;
  325. soc_ac97_bus.ops = ops;
  326. return 0;
  327. }
  328. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
  329. /**
  330. * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
  331. *
  332. * This function sets the reset and warm_reset properties of ops and parses
  333. * the device node of pdev to get pinctrl states and gpio numbers to use.
  334. */
  335. int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
  336. struct platform_device *pdev)
  337. {
  338. struct device *dev = &pdev->dev;
  339. struct snd_ac97_reset_cfg cfg;
  340. int ret;
  341. ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
  342. if (ret)
  343. return ret;
  344. ret = snd_soc_set_ac97_ops(ops);
  345. if (ret)
  346. return ret;
  347. ops->warm_reset = snd_soc_ac97_warm_reset;
  348. ops->reset = snd_soc_ac97_reset;
  349. snd_ac97_rst_cfg = cfg;
  350. return 0;
  351. }
  352. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);