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 /* unregister ac97 codec */
42 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
44 if (codec->ac97->dev.bus)
45 device_del(&codec->ac97->dev);
49 /* register ac97 codec to bus */
50 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
54 codec->ac97->dev.bus = &ac97_bus_type;
55 codec->ac97->dev.parent = codec->component.card->dev;
57 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
58 codec->component.card->snd_card->number, 0,
59 codec->component.name);
60 err = device_add(&codec->ac97->dev);
62 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
63 codec->ac97->dev.bus = NULL;
69 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
70 struct snd_soc_dai *codec_dai)
74 /* Only instantiate AC97 if not already done by the adaptor
75 * for the generic AC97 subsystem.
77 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
79 * It is possible that the AC97 device is already registered to
80 * the device subsystem. This happens when the device is created
81 * via snd_ac97_mixer(). Currently only SoC codec that does so
82 * is the generic AC97 glue but others migh emerge.
84 * In those cases we don't try to register the device again.
86 if (!codec->ac97_created)
89 ret = soc_ac97_dev_register(codec);
92 "ASoC: AC97 device register failed: %d\n", ret);
96 codec->ac97_registered = 1;
101 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
103 if (codec->ac97_registered) {
104 soc_ac97_dev_unregister(codec);
105 codec->ac97_registered = 0;
109 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
113 for (i = 0; i < rtd->num_codecs; i++) {
114 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
116 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
119 soc_unregister_ac97_codec(codec_dai->codec);
127 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
131 for (i = 0; i < rtd->num_codecs; i++)
132 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
135 static void soc_ac97_device_release(struct device *dev)
137 kfree(to_ac97_t(dev));
141 * snd_soc_new_ac97_codec - initailise AC97 device
142 * @codec: audio codec
143 * @ops: AC97 bus operations
144 * @num: AC97 codec number
146 * Initialises AC97 codec resources for use by ad-hoc devices only.
148 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
149 struct snd_ac97_bus_ops *ops, int num)
151 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
152 if (codec->ac97 == NULL)
155 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
156 if (codec->ac97->bus == NULL) {
162 codec->ac97->bus->ops = ops;
163 codec->ac97->num = num;
164 codec->ac97->dev.release = soc_ac97_device_release;
167 * Mark the AC97 device to be created by us. This way we ensure that the
168 * device will be registered with the device subsystem later on.
170 codec->ac97_created = 1;
171 device_initialize(&codec->ac97->dev);
175 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
178 * snd_soc_free_ac97_codec - free AC97 codec device
179 * @codec: audio codec
181 * Frees AC97 codec device resources.
183 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
185 soc_unregister_ac97_codec(codec);
186 kfree(codec->ac97->bus);
187 codec->ac97->bus = NULL;
188 put_device(&codec->ac97->dev);
190 codec->ac97_created = 0;
192 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
194 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
196 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
198 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
200 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
202 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
206 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
208 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
212 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
214 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
216 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
218 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
219 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
220 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
224 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
226 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
230 static int snd_soc_ac97_parse_pinctl(struct device *dev,
231 struct snd_ac97_reset_cfg *cfg)
234 struct pinctrl_state *state;
238 p = devm_pinctrl_get(dev);
240 dev_err(dev, "Failed to get pinctrl\n");
245 state = pinctrl_lookup_state(p, "ac97-reset");
247 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
248 return PTR_ERR(state);
250 cfg->pstate_reset = state;
252 state = pinctrl_lookup_state(p, "ac97-warm-reset");
254 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
255 return PTR_ERR(state);
257 cfg->pstate_warm_reset = state;
259 state = pinctrl_lookup_state(p, "ac97-running");
261 dev_err(dev, "Can't find pinctrl state ac97-running\n");
262 return PTR_ERR(state);
264 cfg->pstate_run = state;
266 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
268 dev_err(dev, "Can't find ac97-sync gpio\n");
271 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
273 dev_err(dev, "Failed requesting ac97-sync gpio\n");
276 cfg->gpio_sync = gpio;
278 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
280 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
283 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
285 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
288 cfg->gpio_sdata = gpio;
290 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
292 dev_err(dev, "Can't find ac97-reset gpio\n");
295 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
297 dev_err(dev, "Failed requesting ac97-reset gpio\n");
300 cfg->gpio_reset = gpio;
305 struct snd_ac97_bus_ops *soc_ac97_ops;
306 EXPORT_SYMBOL_GPL(soc_ac97_ops);
308 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
310 if (ops == soc_ac97_ops)
313 if (soc_ac97_ops && ops)
320 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
323 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
325 * This function sets the reset and warm_reset properties of ops and parses
326 * the device node of pdev to get pinctrl states and gpio numbers to use.
328 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
329 struct platform_device *pdev)
331 struct device *dev = &pdev->dev;
332 struct snd_ac97_reset_cfg cfg;
335 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
339 ret = snd_soc_set_ac97_ops(ops);
343 ops->warm_reset = snd_soc_ac97_warm_reset;
344 ops->reset = snd_soc_ac97_reset;
346 snd_ac97_rst_cfg = cfg;
349 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
351 int snd_soc_ac97_register_dai_links(struct snd_soc_card *card)
356 /* register any AC97 codecs */
357 for (i = 0; i < card->num_rtd; i++) {
358 ret = soc_register_ac97_dai_link(&card->rtd[i]);
366 "ASoC: failed to register AC97: %d\n", ret);
368 soc_unregister_ac97_dai_link(&card->rtd[i]);
372 void snd_soc_ac97_add_pdata(struct snd_soc_pcm_runtime *rtd)
376 /* add platform data for AC97 devices */
377 for (i = 0; i < rtd->num_codecs; i++) {
378 if (rtd->codec_dais[i]->driver->ac97_control)
379 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
380 rtd->cpu_dai->ac97_pdata);