pktgen: do not sleep with the thread lock held.
[sfrench/cifs-2.6.git] / sound / soc / pxa / magician.c
1 /*
2  * SoC audio for HTC Magician
3  *
4  * Copyright (c) 2006 Philipp Zabel <philipp.zabel@gmail.com>
5  *
6  * based on spitz.c,
7  * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
8  *          Richard Purdie <richard@openedhand.com>
9  *
10  *  This program is free software; you can redistribute  it and/or modify it
11  *  under  the terms of  the GNU General  Public License as published by the
12  *  Free Software Foundation;  either version 2 of the  License, or (at your
13  *  option) any later version.
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/timer.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/delay.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/uda1380.h>
30
31 #include <mach/magician.h>
32 #include <asm/mach-types.h>
33 #include "../codecs/uda1380.h"
34 #include "pxa2xx-i2s.h"
35 #include "pxa-ssp.h"
36
37 #define MAGICIAN_MIC       0
38 #define MAGICIAN_MIC_EXT   1
39
40 static int magician_hp_switch;
41 static int magician_spk_switch = 1;
42 static int magician_in_sel = MAGICIAN_MIC;
43
44 static void magician_ext_control(struct snd_soc_dapm_context *dapm)
45 {
46
47         snd_soc_dapm_mutex_lock(dapm);
48
49         if (magician_spk_switch)
50                 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
51         else
52                 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
53         if (magician_hp_switch)
54                 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
55         else
56                 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
57
58         switch (magician_in_sel) {
59         case MAGICIAN_MIC:
60                 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Mic");
61                 snd_soc_dapm_enable_pin_unlocked(dapm, "Call Mic");
62                 break;
63         case MAGICIAN_MIC_EXT:
64                 snd_soc_dapm_disable_pin_unlocked(dapm, "Call Mic");
65                 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Mic");
66                 break;
67         }
68
69         snd_soc_dapm_sync_unlocked(dapm);
70
71         snd_soc_dapm_mutex_unlock(dapm);
72 }
73
74 static int magician_startup(struct snd_pcm_substream *substream)
75 {
76         struct snd_soc_pcm_runtime *rtd = substream->private_data;
77
78         /* check the jack status at stream startup */
79         magician_ext_control(&rtd->card->dapm);
80
81         return 0;
82 }
83
84 /*
85  * Magician uses SSP port for playback.
86  */
87 static int magician_playback_hw_params(struct snd_pcm_substream *substream,
88                                        struct snd_pcm_hw_params *params)
89 {
90         struct snd_soc_pcm_runtime *rtd = substream->private_data;
91         struct snd_soc_dai *codec_dai = rtd->codec_dai;
92         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
93         unsigned int width;
94         int ret = 0;
95
96         /* set codec DAI configuration */
97         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
98                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
99         if (ret < 0)
100                 return ret;
101
102         /* set cpu DAI configuration */
103         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
104                         SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_CBS_CFS);
105         if (ret < 0)
106                 return ret;
107
108         width = snd_pcm_format_physical_width(params_format(params));
109         ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
110         if (ret < 0)
111                 return ret;
112
113         /* set audio clock as clock source */
114         ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
115                         SND_SOC_CLOCK_OUT);
116         if (ret < 0)
117                 return ret;
118
119         return 0;
120 }
121
122 /*
123  * Magician uses I2S for capture.
124  */
125 static int magician_capture_hw_params(struct snd_pcm_substream *substream,
126                                       struct snd_pcm_hw_params *params)
127 {
128         struct snd_soc_pcm_runtime *rtd = substream->private_data;
129         struct snd_soc_dai *codec_dai = rtd->codec_dai;
130         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
131         int ret = 0;
132
133         /* set codec DAI configuration */
134         ret = snd_soc_dai_set_fmt(codec_dai,
135                         SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
136                         SND_SOC_DAIFMT_CBS_CFS);
137         if (ret < 0)
138                 return ret;
139
140         /* set cpu DAI configuration */
141         ret = snd_soc_dai_set_fmt(cpu_dai,
142                         SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
143                         SND_SOC_DAIFMT_CBS_CFS);
144         if (ret < 0)
145                 return ret;
146
147         /* set the I2S system clock as output */
148         ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
149                         SND_SOC_CLOCK_OUT);
150         if (ret < 0)
151                 return ret;
152
153         return 0;
154 }
155
156 static const struct snd_soc_ops magician_capture_ops = {
157         .startup = magician_startup,
158         .hw_params = magician_capture_hw_params,
159 };
160
161 static const struct snd_soc_ops magician_playback_ops = {
162         .startup = magician_startup,
163         .hw_params = magician_playback_hw_params,
164 };
165
166 static int magician_get_hp(struct snd_kcontrol *kcontrol,
167                              struct snd_ctl_elem_value *ucontrol)
168 {
169         ucontrol->value.integer.value[0] = magician_hp_switch;
170         return 0;
171 }
172
173 static int magician_set_hp(struct snd_kcontrol *kcontrol,
174                              struct snd_ctl_elem_value *ucontrol)
175 {
176         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
177
178         if (magician_hp_switch == ucontrol->value.integer.value[0])
179                 return 0;
180
181         magician_hp_switch = ucontrol->value.integer.value[0];
182         magician_ext_control(&card->dapm);
183         return 1;
184 }
185
186 static int magician_get_spk(struct snd_kcontrol *kcontrol,
187                             struct snd_ctl_elem_value *ucontrol)
188 {
189         ucontrol->value.integer.value[0] = magician_spk_switch;
190         return 0;
191 }
192
193 static int magician_set_spk(struct snd_kcontrol *kcontrol,
194                             struct snd_ctl_elem_value *ucontrol)
195 {
196         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
197
198         if (magician_spk_switch == ucontrol->value.integer.value[0])
199                 return 0;
200
201         magician_spk_switch = ucontrol->value.integer.value[0];
202         magician_ext_control(&card->dapm);
203         return 1;
204 }
205
206 static int magician_get_input(struct snd_kcontrol *kcontrol,
207                               struct snd_ctl_elem_value *ucontrol)
208 {
209         ucontrol->value.enumerated.item[0] = magician_in_sel;
210         return 0;
211 }
212
213 static int magician_set_input(struct snd_kcontrol *kcontrol,
214                               struct snd_ctl_elem_value *ucontrol)
215 {
216         if (magician_in_sel == ucontrol->value.enumerated.item[0])
217                 return 0;
218
219         magician_in_sel = ucontrol->value.enumerated.item[0];
220
221         switch (magician_in_sel) {
222         case MAGICIAN_MIC:
223                 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
224                 break;
225         case MAGICIAN_MIC_EXT:
226                 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
227         }
228
229         return 1;
230 }
231
232 static int magician_spk_power(struct snd_soc_dapm_widget *w,
233                                 struct snd_kcontrol *k, int event)
234 {
235         gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
236         return 0;
237 }
238
239 static int magician_hp_power(struct snd_soc_dapm_widget *w,
240                                 struct snd_kcontrol *k, int event)
241 {
242         gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
243         return 0;
244 }
245
246 static int magician_mic_bias(struct snd_soc_dapm_widget *w,
247                                 struct snd_kcontrol *k, int event)
248 {
249         gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
250         return 0;
251 }
252
253 /* magician machine dapm widgets */
254 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
255         SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
256         SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
257         SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
258         SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
259 };
260
261 /* magician machine audio_map */
262 static const struct snd_soc_dapm_route audio_map[] = {
263
264         /* Headphone connected to VOUTL, VOUTR */
265         {"Headphone Jack", NULL, "VOUTL"},
266         {"Headphone Jack", NULL, "VOUTR"},
267
268         /* Speaker connected to VOUTL, VOUTR */
269         {"Speaker", NULL, "VOUTL"},
270         {"Speaker", NULL, "VOUTR"},
271
272         /* Mics are connected to VINM */
273         {"VINM", NULL, "Headset Mic"},
274         {"VINM", NULL, "Call Mic"},
275 };
276
277 static const char * const input_select[] = {"Call Mic", "Headset Mic"};
278 static const struct soc_enum magician_in_sel_enum =
279         SOC_ENUM_SINGLE_EXT(2, input_select);
280
281 static const struct snd_kcontrol_new uda1380_magician_controls[] = {
282         SOC_SINGLE_BOOL_EXT("Headphone Switch",
283                         (unsigned long)&magician_hp_switch,
284                         magician_get_hp, magician_set_hp),
285         SOC_SINGLE_BOOL_EXT("Speaker Switch",
286                         (unsigned long)&magician_spk_switch,
287                         magician_get_spk, magician_set_spk),
288         SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
289                         magician_get_input, magician_set_input),
290 };
291
292 /* magician digital audio interface glue - connects codec <--> CPU */
293 static struct snd_soc_dai_link magician_dai[] = {
294 {
295         .name = "uda1380",
296         .stream_name = "UDA1380 Playback",
297         .cpu_dai_name = "pxa-ssp-dai.0",
298         .codec_dai_name = "uda1380-hifi-playback",
299         .platform_name = "pxa-pcm-audio",
300         .codec_name = "uda1380-codec.0-0018",
301         .ops = &magician_playback_ops,
302 },
303 {
304         .name = "uda1380",
305         .stream_name = "UDA1380 Capture",
306         .cpu_dai_name = "pxa2xx-i2s",
307         .codec_dai_name = "uda1380-hifi-capture",
308         .platform_name = "pxa-pcm-audio",
309         .codec_name = "uda1380-codec.0-0018",
310         .ops = &magician_capture_ops,
311 }
312 };
313
314 /* magician audio machine driver */
315 static struct snd_soc_card snd_soc_card_magician = {
316         .name = "Magician",
317         .owner = THIS_MODULE,
318         .dai_link = magician_dai,
319         .num_links = ARRAY_SIZE(magician_dai),
320
321         .controls = uda1380_magician_controls,
322         .num_controls = ARRAY_SIZE(uda1380_magician_controls),
323         .dapm_widgets = uda1380_dapm_widgets,
324         .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
325         .dapm_routes = audio_map,
326         .num_dapm_routes = ARRAY_SIZE(audio_map),
327         .fully_routed = true,
328 };
329
330 static struct platform_device *magician_snd_device;
331
332 /*
333  * FIXME: move into magician board file once merged into the pxa tree
334  */
335 static struct uda1380_platform_data uda1380_info = {
336         .gpio_power = EGPIO_MAGICIAN_CODEC_POWER,
337         .gpio_reset = EGPIO_MAGICIAN_CODEC_RESET,
338         .dac_clk    = UDA1380_DAC_CLK_WSPLL,
339 };
340
341 static struct i2c_board_info i2c_board_info[] = {
342         {
343                 I2C_BOARD_INFO("uda1380", 0x18),
344                 .platform_data = &uda1380_info,
345         },
346 };
347
348 static int __init magician_init(void)
349 {
350         int ret;
351         struct i2c_adapter *adapter;
352         struct i2c_client *client;
353
354         if (!machine_is_magician())
355                 return -ENODEV;
356
357         adapter = i2c_get_adapter(0);
358         if (!adapter)
359                 return -ENODEV;
360         client = i2c_new_device(adapter, i2c_board_info);
361         i2c_put_adapter(adapter);
362         if (!client)
363                 return -ENODEV;
364
365         ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
366         if (ret)
367                 goto err_request_spk;
368         ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
369         if (ret)
370                 goto err_request_ep;
371         ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
372         if (ret)
373                 goto err_request_mic;
374         ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
375         if (ret)
376                 goto err_request_in_sel0;
377         ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
378         if (ret)
379                 goto err_request_in_sel1;
380
381         gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);
382
383         magician_snd_device = platform_device_alloc("soc-audio", -1);
384         if (!magician_snd_device) {
385                 ret = -ENOMEM;
386                 goto err_pdev;
387         }
388
389         platform_set_drvdata(magician_snd_device, &snd_soc_card_magician);
390         ret = platform_device_add(magician_snd_device);
391         if (ret) {
392                 platform_device_put(magician_snd_device);
393                 goto err_pdev;
394         }
395
396         return 0;
397
398 err_pdev:
399         gpio_free(EGPIO_MAGICIAN_IN_SEL1);
400 err_request_in_sel1:
401         gpio_free(EGPIO_MAGICIAN_IN_SEL0);
402 err_request_in_sel0:
403         gpio_free(EGPIO_MAGICIAN_MIC_POWER);
404 err_request_mic:
405         gpio_free(EGPIO_MAGICIAN_EP_POWER);
406 err_request_ep:
407         gpio_free(EGPIO_MAGICIAN_SPK_POWER);
408 err_request_spk:
409         return ret;
410 }
411
412 static void __exit magician_exit(void)
413 {
414         platform_device_unregister(magician_snd_device);
415
416         gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
417         gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
418         gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);
419
420         gpio_free(EGPIO_MAGICIAN_IN_SEL1);
421         gpio_free(EGPIO_MAGICIAN_IN_SEL0);
422         gpio_free(EGPIO_MAGICIAN_MIC_POWER);
423         gpio_free(EGPIO_MAGICIAN_EP_POWER);
424         gpio_free(EGPIO_MAGICIAN_SPK_POWER);
425 }
426
427 module_init(magician_init);
428 module_exit(magician_exit);
429
430 MODULE_AUTHOR("Philipp Zabel");
431 MODULE_DESCRIPTION("ALSA SoC Magician");
432 MODULE_LICENSE("GPL");