2 * soc-ac97.c -- ALSA SoC Audio Layer AC97 support
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 #include <linux/ctype.h>
20 #include <linux/delay.h>
21 #include <linux/export.h>
22 #include <linux/gpio.h>
23 #include <linux/init.h>
24 #include <linux/of_gpio.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/slab.h>
28 #include <sound/ac97_codec.h>
29 #include <sound/soc.h>
31 struct snd_ac97_reset_cfg {
33 struct pinctrl_state *pstate_reset;
34 struct pinctrl_state *pstate_warm_reset;
35 struct pinctrl_state *pstate_run;
41 static struct snd_ac97_bus soc_ac97_bus = {
42 .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
45 /* register ac97 codec to bus */
46 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
47 struct snd_soc_dai *codec_dai)
51 /* Only instantiate AC97 if not already done by the adaptor
52 * for the generic AC97 subsystem.
54 if (!codec_dai->driver->ac97_control || codec->ac97_registered)
58 * It is possible that the AC97 device is already registered to
59 * the device subsystem. This happens when the device is created
60 * via snd_ac97_mixer(). Currently only SoC codec that does so
61 * is the generic AC97 glue but others migh emerge.
63 * In those cases we don't try to register the device again.
65 if (!codec->ac97_created)
68 codec->ac97->dev.bus = &ac97_bus_type;
69 codec->ac97->dev.parent = codec->component.card->dev;
71 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
72 codec->component.card->snd_card->number, 0,
73 codec->component.name);
74 ret = device_add(&codec->ac97->dev);
76 dev_err(codec->dev, "ASoC: AC97 device register failed: %d\n",
80 codec->ac97_registered = 1;
85 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
87 if (!codec->ac97_registered)
89 device_del(&codec->ac97->dev);
90 codec->ac97_registered = 0;
93 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
97 for (i = 0; i < rtd->num_codecs; i++) {
98 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
100 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
103 soc_unregister_ac97_codec(codec_dai->codec);
111 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
115 for (i = 0; i < rtd->num_codecs; i++)
116 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
119 static void soc_ac97_device_release(struct device *dev)
121 kfree(to_ac97_t(dev));
125 * snd_soc_new_ac97_codec - initailise AC97 device
126 * @codec: audio codec
128 * Initialises AC97 codec resources for use by ad-hoc devices only.
130 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec)
132 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
133 if (codec->ac97 == NULL)
136 codec->ac97->bus = &soc_ac97_bus;
137 codec->ac97->num = 0;
138 codec->ac97->dev.release = soc_ac97_device_release;
141 * Mark the AC97 device to be created by us. This way we ensure that the
142 * device will be registered with the device subsystem later on.
144 codec->ac97_created = 1;
145 device_initialize(&codec->ac97->dev);
149 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
152 * snd_soc_free_ac97_codec - free AC97 codec device
153 * @codec: audio codec
155 * Frees AC97 codec device resources.
157 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
159 soc_unregister_ac97_codec(codec);
160 codec->ac97->bus = NULL;
161 put_device(&codec->ac97->dev);
163 codec->ac97_created = 0;
165 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
167 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
169 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
171 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
173 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
175 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
179 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
181 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
185 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
187 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
189 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
191 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
192 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
193 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
197 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
199 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
203 static int snd_soc_ac97_parse_pinctl(struct device *dev,
204 struct snd_ac97_reset_cfg *cfg)
207 struct pinctrl_state *state;
211 p = devm_pinctrl_get(dev);
213 dev_err(dev, "Failed to get pinctrl\n");
218 state = pinctrl_lookup_state(p, "ac97-reset");
220 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
221 return PTR_ERR(state);
223 cfg->pstate_reset = state;
225 state = pinctrl_lookup_state(p, "ac97-warm-reset");
227 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
228 return PTR_ERR(state);
230 cfg->pstate_warm_reset = state;
232 state = pinctrl_lookup_state(p, "ac97-running");
234 dev_err(dev, "Can't find pinctrl state ac97-running\n");
235 return PTR_ERR(state);
237 cfg->pstate_run = state;
239 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
241 dev_err(dev, "Can't find ac97-sync gpio\n");
244 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
246 dev_err(dev, "Failed requesting ac97-sync gpio\n");
249 cfg->gpio_sync = gpio;
251 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
253 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
256 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
258 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
261 cfg->gpio_sdata = gpio;
263 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
265 dev_err(dev, "Can't find ac97-reset gpio\n");
268 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
270 dev_err(dev, "Failed requesting ac97-reset gpio\n");
273 cfg->gpio_reset = gpio;
278 struct snd_ac97_bus_ops *soc_ac97_ops;
279 EXPORT_SYMBOL_GPL(soc_ac97_ops);
281 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
283 if (ops == soc_ac97_ops)
286 if (soc_ac97_ops && ops)
290 soc_ac97_bus.ops = ops;
294 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
297 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
299 * This function sets the reset and warm_reset properties of ops and parses
300 * the device node of pdev to get pinctrl states and gpio numbers to use.
302 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
303 struct platform_device *pdev)
305 struct device *dev = &pdev->dev;
306 struct snd_ac97_reset_cfg cfg;
309 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
313 ret = snd_soc_set_ac97_ops(ops);
317 ops->warm_reset = snd_soc_ac97_warm_reset;
318 ops->reset = snd_soc_ac97_reset;
320 snd_ac97_rst_cfg = cfg;
323 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
325 int snd_soc_ac97_register_dai_links(struct snd_soc_card *card)
330 /* register any AC97 codecs */
331 for (i = 0; i < card->num_rtd; i++) {
332 ret = soc_register_ac97_dai_link(&card->rtd[i]);
340 "ASoC: failed to register AC97: %d\n", ret);
342 soc_unregister_ac97_dai_link(&card->rtd[i]);
346 void snd_soc_ac97_add_pdata(struct snd_soc_pcm_runtime *rtd)
350 /* add platform data for AC97 devices */
351 for (i = 0; i < rtd->num_codecs; i++) {
352 if (rtd->codec_dais[i]->driver->ac97_control)
353 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
354 rtd->cpu_dai->ac97_pdata);