soc-ac97.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 int 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. return 0;
  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. int ret;
  91. dev_dbg(component->dev, "set gpio %d to output\n", offset);
  92. ret = snd_soc_ac97_gpio_set(chip, offset, value);
  93. if (ret)
  94. return ret;
  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 an ERR_PTR 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_obj(struct snd_ac97);
  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: An ERR_PTR 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. gpiod_direction_output_raw(snd_ac97_rst_cfg.sync_gpio, 1);
  231. udelay(10);
  232. gpiod_direction_output_raw(snd_ac97_rst_cfg.sync_gpio, 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. gpiod_direction_output_raw(snd_ac97_rst_cfg.sync_gpio, 0);
  241. gpiod_direction_output_raw(snd_ac97_rst_cfg.sdata_gpio, 0);
  242. gpiod_direction_output_raw(snd_ac97_rst_cfg.reset_gpio, 0);
  243. udelay(10);
  244. gpiod_direction_output_raw(snd_ac97_rst_cfg.reset_gpio, 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. p = devm_pinctrl_get(dev);
  254. if (IS_ERR(p)) {
  255. dev_err(dev, "Failed to get pinctrl\n");
  256. return PTR_ERR(p);
  257. }
  258. cfg->pctl = p;
  259. state = pinctrl_lookup_state(p, "ac97-reset");
  260. if (IS_ERR(state)) {
  261. dev_err(dev, "Can't find pinctrl state ac97-reset\n");
  262. return PTR_ERR(state);
  263. }
  264. cfg->pstate_reset = state;
  265. state = pinctrl_lookup_state(p, "ac97-warm-reset");
  266. if (IS_ERR(state)) {
  267. dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
  268. return PTR_ERR(state);
  269. }
  270. cfg->pstate_warm_reset = state;
  271. state = pinctrl_lookup_state(p, "ac97-running");
  272. if (IS_ERR(state)) {
  273. dev_err(dev, "Can't find pinctrl state ac97-running\n");
  274. return PTR_ERR(state);
  275. }
  276. cfg->pstate_run = state;
  277. cfg->sync_gpio = devm_gpiod_get_index(dev, "ac97", 0, GPIOD_ASIS);
  278. if (IS_ERR(cfg->sync_gpio))
  279. return dev_err_probe(dev, PTR_ERR(cfg->sync_gpio), "Can't find ac97-sync gpio\n");
  280. gpiod_set_consumer_name(cfg->sync_gpio, "AC97 link sync");
  281. cfg->sdata_gpio = devm_gpiod_get_index(dev, "ac97", 1, GPIOD_ASIS);
  282. if (IS_ERR(cfg->sdata_gpio))
  283. return dev_err_probe(dev, PTR_ERR(cfg->sdata_gpio), "Can't find ac97-sdata gpio\n");
  284. gpiod_set_consumer_name(cfg->sdata_gpio, "AC97 link sdata");
  285. cfg->reset_gpio = devm_gpiod_get_index(dev, "ac97", 2, GPIOD_ASIS);
  286. if (IS_ERR(cfg->reset_gpio))
  287. return dev_err_probe(dev, PTR_ERR(cfg->reset_gpio), "Can't find ac97-reset gpio\n");
  288. gpiod_set_consumer_name(cfg->reset_gpio, "AC97 link reset");
  289. return 0;
  290. }
  291. struct snd_ac97_bus_ops *soc_ac97_ops;
  292. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  293. int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
  294. {
  295. if (ops == soc_ac97_ops)
  296. return 0;
  297. if (soc_ac97_ops && ops)
  298. return -EBUSY;
  299. soc_ac97_ops = ops;
  300. soc_ac97_bus.ops = ops;
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
  304. /**
  305. * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
  306. * @ops: bus ops
  307. * @pdev: platform device
  308. *
  309. * This function sets the reset and warm_reset properties of ops and parses
  310. * the device node of pdev to get pinctrl states and gpio numbers to use.
  311. */
  312. int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
  313. struct platform_device *pdev)
  314. {
  315. struct device *dev = &pdev->dev;
  316. struct snd_ac97_reset_cfg cfg;
  317. int ret;
  318. ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
  319. if (ret)
  320. return ret;
  321. ret = snd_soc_set_ac97_ops(ops);
  322. if (ret)
  323. return ret;
  324. ops->warm_reset = snd_soc_ac97_warm_reset;
  325. ops->reset = snd_soc_ac97_reset;
  326. snd_ac97_rst_cfg = cfg;
  327. return 0;
  328. }
  329. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);