soc-ac97.c 11 KB

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