ASoC: Move AC'97 support to its own file
[linux-2.6-block.git] / sound / soc / soc-ac97.c
1 /*
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
31 struct 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
41 /* unregister ac97 codec */
42 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
43 {
44         if (codec->ac97->dev.bus)
45                 device_del(&codec->ac97->dev);
46         return 0;
47 }
48
49 /* register ac97 codec to bus */
50 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
51 {
52         int err;
53
54         codec->ac97->dev.bus = &ac97_bus_type;
55         codec->ac97->dev.parent = codec->component.card->dev;
56
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);
61         if (err < 0) {
62                 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
63                 codec->ac97->dev.bus = NULL;
64                 return err;
65         }
66         return 0;
67 }
68
69 static int soc_register_ac97_codec(struct snd_soc_codec *codec,
70                                    struct snd_soc_dai *codec_dai)
71 {
72         int ret;
73
74         /* Only instantiate AC97 if not already done by the adaptor
75          * for the generic AC97 subsystem.
76          */
77         if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
78                 /*
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.
83                  *
84                  * In those cases we don't try to register the device again.
85                  */
86                 if (!codec->ac97_created)
87                         return 0;
88
89                 ret = soc_ac97_dev_register(codec);
90                 if (ret < 0) {
91                         dev_err(codec->dev,
92                                 "ASoC: AC97 device register failed: %d\n", ret);
93                         return ret;
94                 }
95
96                 codec->ac97_registered = 1;
97         }
98         return 0;
99 }
100
101 static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
102 {
103         if (codec->ac97_registered) {
104                 soc_ac97_dev_unregister(codec);
105                 codec->ac97_registered = 0;
106         }
107 }
108
109 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
110 {
111         int i, ret;
112
113         for (i = 0; i < rtd->num_codecs; i++) {
114                 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
115
116                 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
117                 if (ret) {
118                         while (--i >= 0)
119                                 soc_unregister_ac97_codec(codec_dai->codec);
120                         return ret;
121                 }
122         }
123
124         return 0;
125 }
126
127 static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
128 {
129         int i;
130
131         for (i = 0; i < rtd->num_codecs; i++)
132                 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
133 }
134
135 static void soc_ac97_device_release(struct device *dev)
136 {
137         kfree(to_ac97_t(dev));
138 }
139
140 /**
141  * snd_soc_new_ac97_codec - initailise AC97 device
142  * @codec: audio codec
143  * @ops: AC97 bus operations
144  * @num: AC97 codec number
145  *
146  * Initialises AC97 codec resources for use by ad-hoc devices only.
147  */
148 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
149         struct snd_ac97_bus_ops *ops, int num)
150 {
151         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
152         if (codec->ac97 == NULL)
153                 return -ENOMEM;
154
155         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
156         if (codec->ac97->bus == NULL) {
157                 kfree(codec->ac97);
158                 codec->ac97 = NULL;
159                 return -ENOMEM;
160         }
161
162         codec->ac97->bus->ops = ops;
163         codec->ac97->num = num;
164         codec->ac97->dev.release = soc_ac97_device_release;
165
166         /*
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.
169          */
170         codec->ac97_created = 1;
171         device_initialize(&codec->ac97->dev);
172
173         return 0;
174 }
175 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
176
177 /**
178  * snd_soc_free_ac97_codec - free AC97 codec device
179  * @codec: audio codec
180  *
181  * Frees AC97 codec device resources.
182  */
183 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
184 {
185         soc_unregister_ac97_codec(codec);
186         kfree(codec->ac97->bus);
187         codec->ac97->bus = NULL;
188         put_device(&codec->ac97->dev);
189         codec->ac97 = NULL;
190         codec->ac97_created = 0;
191 }
192 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
193
194 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
195
196 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
197 {
198         struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
199
200         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
201
202         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
203
204         udelay(10);
205
206         gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
207
208         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
209         msleep(2);
210 }
211
212 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
213 {
214         struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
215
216         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
217
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);
221
222         udelay(10);
223
224         gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
225
226         pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
227         msleep(2);
228 }
229
230 static int snd_soc_ac97_parse_pinctl(struct device *dev,
231                 struct snd_ac97_reset_cfg *cfg)
232 {
233         struct pinctrl *p;
234         struct pinctrl_state *state;
235         int gpio;
236         int ret;
237
238         p = devm_pinctrl_get(dev);
239         if (IS_ERR(p)) {
240                 dev_err(dev, "Failed to get pinctrl\n");
241                 return PTR_ERR(p);
242         }
243         cfg->pctl = p;
244
245         state = pinctrl_lookup_state(p, "ac97-reset");
246         if (IS_ERR(state)) {
247                 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
248                 return PTR_ERR(state);
249         }
250         cfg->pstate_reset = state;
251
252         state = pinctrl_lookup_state(p, "ac97-warm-reset");
253         if (IS_ERR(state)) {
254                 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
255                 return PTR_ERR(state);
256         }
257         cfg->pstate_warm_reset = state;
258
259         state = pinctrl_lookup_state(p, "ac97-running");
260         if (IS_ERR(state)) {
261                 dev_err(dev, "Can't find pinctrl state ac97-running\n");
262                 return PTR_ERR(state);
263         }
264         cfg->pstate_run = state;
265
266         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
267         if (gpio < 0) {
268                 dev_err(dev, "Can't find ac97-sync gpio\n");
269                 return gpio;
270         }
271         ret = devm_gpio_request(dev, gpio, "AC97 link sync");
272         if (ret) {
273                 dev_err(dev, "Failed requesting ac97-sync gpio\n");
274                 return ret;
275         }
276         cfg->gpio_sync = gpio;
277
278         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
279         if (gpio < 0) {
280                 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
281                 return gpio;
282         }
283         ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
284         if (ret) {
285                 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
286                 return ret;
287         }
288         cfg->gpio_sdata = gpio;
289
290         gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
291         if (gpio < 0) {
292                 dev_err(dev, "Can't find ac97-reset gpio\n");
293                 return gpio;
294         }
295         ret = devm_gpio_request(dev, gpio, "AC97 link reset");
296         if (ret) {
297                 dev_err(dev, "Failed requesting ac97-reset gpio\n");
298                 return ret;
299         }
300         cfg->gpio_reset = gpio;
301
302         return 0;
303 }
304
305 struct snd_ac97_bus_ops *soc_ac97_ops;
306 EXPORT_SYMBOL_GPL(soc_ac97_ops);
307
308 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
309 {
310         if (ops == soc_ac97_ops)
311                 return 0;
312
313         if (soc_ac97_ops && ops)
314                 return -EBUSY;
315
316         soc_ac97_ops = ops;
317
318         return 0;
319 }
320 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
321
322 /**
323  * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
324  *
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.
327  */
328 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
329                 struct platform_device *pdev)
330 {
331         struct device *dev = &pdev->dev;
332         struct snd_ac97_reset_cfg cfg;
333         int ret;
334
335         ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
336         if (ret)
337                 return ret;
338
339         ret = snd_soc_set_ac97_ops(ops);
340         if (ret)
341                 return ret;
342
343         ops->warm_reset = snd_soc_ac97_warm_reset;
344         ops->reset = snd_soc_ac97_reset;
345
346         snd_ac97_rst_cfg = cfg;
347         return 0;
348 }
349 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
350
351 int snd_soc_ac97_register_dai_links(struct snd_soc_card *card)
352 {
353         int i;
354         int ret;
355
356         /* register any AC97 codecs */
357         for (i = 0; i < card->num_rtd; i++) {
358                 ret = soc_register_ac97_dai_link(&card->rtd[i]);
359                 if (ret < 0)
360                         goto err;
361         }
362
363         return 0;
364 err:
365         dev_err(card->dev,
366                 "ASoC: failed to register AC97: %d\n", ret);
367         while (--i >= 0)
368                 soc_unregister_ac97_dai_link(&card->rtd[i]);
369         return ret;
370 }
371
372 void snd_soc_ac97_add_pdata(struct snd_soc_pcm_runtime *rtd)
373 {
374         unsigned int i;
375
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);
381         }
382 }