soc-ac97.c 11 KB

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