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