blob: dbfca7e7dddb572fd6167e86e0a92eb2b6ee0cf7 [file] [log] [blame]
Lars-Peter Clausen336b8422014-11-10 22:41:46 +01001/*
2 * soc-ac97.c -- ALSA SoC Audio Layer AC97 support
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
12 *
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.
17 */
18
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>
25#include <linux/of.h>
26#include <linux/pinctrl/consumer.h>
27#include <linux/slab.h>
28#include <sound/ac97_codec.h>
29#include <sound/soc.h>
30
31struct snd_ac97_reset_cfg {
32 struct pinctrl *pctl;
33 struct pinctrl_state *pstate_reset;
34 struct pinctrl_state *pstate_warm_reset;
35 struct pinctrl_state *pstate_run;
36 int gpio_sdata;
37 int gpio_sync;
38 int gpio_reset;
39};
40
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +010041static struct snd_ac97_bus soc_ac97_bus = {
42 .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
43};
44
Lars-Peter Clausen336b8422014-11-10 22:41:46 +010045/* unregister ac97 codec */
46static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
47{
48 if (codec->ac97->dev.bus)
49 device_del(&codec->ac97->dev);
50 return 0;
51}
52
53/* register ac97 codec to bus */
54static int soc_ac97_dev_register(struct snd_soc_codec *codec)
55{
56 int err;
57
58 codec->ac97->dev.bus = &ac97_bus_type;
59 codec->ac97->dev.parent = codec->component.card->dev;
60
61 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
62 codec->component.card->snd_card->number, 0,
63 codec->component.name);
64 err = device_add(&codec->ac97->dev);
65 if (err < 0) {
66 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
67 codec->ac97->dev.bus = NULL;
68 return err;
69 }
70 return 0;
71}
72
73static int soc_register_ac97_codec(struct snd_soc_codec *codec,
74 struct snd_soc_dai *codec_dai)
75{
76 int ret;
77
78 /* Only instantiate AC97 if not already done by the adaptor
79 * for the generic AC97 subsystem.
80 */
81 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
82 /*
83 * It is possible that the AC97 device is already registered to
84 * the device subsystem. This happens when the device is created
85 * via snd_ac97_mixer(). Currently only SoC codec that does so
86 * is the generic AC97 glue but others migh emerge.
87 *
88 * In those cases we don't try to register the device again.
89 */
90 if (!codec->ac97_created)
91 return 0;
92
93 ret = soc_ac97_dev_register(codec);
94 if (ret < 0) {
95 dev_err(codec->dev,
96 "ASoC: AC97 device register failed: %d\n", ret);
97 return ret;
98 }
99
100 codec->ac97_registered = 1;
101 }
102 return 0;
103}
104
105static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
106{
107 if (codec->ac97_registered) {
108 soc_ac97_dev_unregister(codec);
109 codec->ac97_registered = 0;
110 }
111}
112
113static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
114{
115 int i, ret;
116
117 for (i = 0; i < rtd->num_codecs; i++) {
118 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
119
120 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
121 if (ret) {
122 while (--i >= 0)
123 soc_unregister_ac97_codec(codec_dai->codec);
124 return ret;
125 }
126 }
127
128 return 0;
129}
130
131static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
132{
133 int i;
134
135 for (i = 0; i < rtd->num_codecs; i++)
136 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
137}
138
139static void soc_ac97_device_release(struct device *dev)
140{
141 kfree(to_ac97_t(dev));
142}
143
144/**
145 * snd_soc_new_ac97_codec - initailise AC97 device
146 * @codec: audio codec
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100147 *
148 * Initialises AC97 codec resources for use by ad-hoc devices only.
149 */
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +0100150int snd_soc_new_ac97_codec(struct snd_soc_codec *codec)
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100151{
152 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
153 if (codec->ac97 == NULL)
154 return -ENOMEM;
155
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +0100156 codec->ac97->bus = &soc_ac97_bus;
157 codec->ac97->num = 0;
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100158 codec->ac97->dev.release = soc_ac97_device_release;
159
160 /*
161 * Mark the AC97 device to be created by us. This way we ensure that the
162 * device will be registered with the device subsystem later on.
163 */
164 codec->ac97_created = 1;
165 device_initialize(&codec->ac97->dev);
166
167 return 0;
168}
169EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
170
171/**
172 * snd_soc_free_ac97_codec - free AC97 codec device
173 * @codec: audio codec
174 *
175 * Frees AC97 codec device resources.
176 */
177void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
178{
179 soc_unregister_ac97_codec(codec);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100180 codec->ac97->bus = NULL;
181 put_device(&codec->ac97->dev);
182 codec->ac97 = NULL;
183 codec->ac97_created = 0;
184}
185EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
186
187static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
188
189static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
190{
191 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
192
193 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
194
195 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
196
197 udelay(10);
198
199 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
200
201 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
202 msleep(2);
203}
204
205static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
206{
207 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
208
209 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
210
211 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
212 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
213 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
214
215 udelay(10);
216
217 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
218
219 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
220 msleep(2);
221}
222
223static int snd_soc_ac97_parse_pinctl(struct device *dev,
224 struct snd_ac97_reset_cfg *cfg)
225{
226 struct pinctrl *p;
227 struct pinctrl_state *state;
228 int gpio;
229 int ret;
230
231 p = devm_pinctrl_get(dev);
232 if (IS_ERR(p)) {
233 dev_err(dev, "Failed to get pinctrl\n");
234 return PTR_ERR(p);
235 }
236 cfg->pctl = p;
237
238 state = pinctrl_lookup_state(p, "ac97-reset");
239 if (IS_ERR(state)) {
240 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
241 return PTR_ERR(state);
242 }
243 cfg->pstate_reset = state;
244
245 state = pinctrl_lookup_state(p, "ac97-warm-reset");
246 if (IS_ERR(state)) {
247 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
248 return PTR_ERR(state);
249 }
250 cfg->pstate_warm_reset = state;
251
252 state = pinctrl_lookup_state(p, "ac97-running");
253 if (IS_ERR(state)) {
254 dev_err(dev, "Can't find pinctrl state ac97-running\n");
255 return PTR_ERR(state);
256 }
257 cfg->pstate_run = state;
258
259 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
260 if (gpio < 0) {
261 dev_err(dev, "Can't find ac97-sync gpio\n");
262 return gpio;
263 }
264 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
265 if (ret) {
266 dev_err(dev, "Failed requesting ac97-sync gpio\n");
267 return ret;
268 }
269 cfg->gpio_sync = gpio;
270
271 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
272 if (gpio < 0) {
273 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
274 return gpio;
275 }
276 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
277 if (ret) {
278 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
279 return ret;
280 }
281 cfg->gpio_sdata = gpio;
282
283 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
284 if (gpio < 0) {
285 dev_err(dev, "Can't find ac97-reset gpio\n");
286 return gpio;
287 }
288 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
289 if (ret) {
290 dev_err(dev, "Failed requesting ac97-reset gpio\n");
291 return ret;
292 }
293 cfg->gpio_reset = gpio;
294
295 return 0;
296}
297
298struct snd_ac97_bus_ops *soc_ac97_ops;
299EXPORT_SYMBOL_GPL(soc_ac97_ops);
300
301int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
302{
303 if (ops == soc_ac97_ops)
304 return 0;
305
306 if (soc_ac97_ops && ops)
307 return -EBUSY;
308
309 soc_ac97_ops = ops;
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +0100310 soc_ac97_bus.ops = ops;
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100311
312 return 0;
313}
314EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
315
316/**
317 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
318 *
319 * This function sets the reset and warm_reset properties of ops and parses
320 * the device node of pdev to get pinctrl states and gpio numbers to use.
321 */
322int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
323 struct platform_device *pdev)
324{
325 struct device *dev = &pdev->dev;
326 struct snd_ac97_reset_cfg cfg;
327 int ret;
328
329 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
330 if (ret)
331 return ret;
332
333 ret = snd_soc_set_ac97_ops(ops);
334 if (ret)
335 return ret;
336
337 ops->warm_reset = snd_soc_ac97_warm_reset;
338 ops->reset = snd_soc_ac97_reset;
339
340 snd_ac97_rst_cfg = cfg;
341 return 0;
342}
343EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
344
345int snd_soc_ac97_register_dai_links(struct snd_soc_card *card)
346{
347 int i;
348 int ret;
349
350 /* register any AC97 codecs */
351 for (i = 0; i < card->num_rtd; i++) {
352 ret = soc_register_ac97_dai_link(&card->rtd[i]);
353 if (ret < 0)
354 goto err;
355 }
356
357 return 0;
358err:
359 dev_err(card->dev,
360 "ASoC: failed to register AC97: %d\n", ret);
361 while (--i >= 0)
362 soc_unregister_ac97_dai_link(&card->rtd[i]);
363 return ret;
364}
365
366void snd_soc_ac97_add_pdata(struct snd_soc_pcm_runtime *rtd)
367{
368 unsigned int i;
369
370 /* add platform data for AC97 devices */
371 for (i = 0; i < rtd->num_codecs; i++) {
372 if (rtd->codec_dais[i]->driver->ac97_control)
373 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
374 rtd->cpu_dai->ac97_pdata);
375 }
376}