soc-ac97.c 10 KB

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