soc-ac97.c 11 KB

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