Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[powerpc.git] / sound / soc / pxa / corgi.c
1 /*
2  * corgi.c  --  SoC audio for Corgi
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Copyright 2005 Openedhand Ltd.
6  *
7  * Authors: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
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  *  Revision history
16  *    30th Nov 2005   Initial version.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30
31 #include <asm/mach-types.h>
32 #include <asm/hardware/scoop.h>
33 #include <asm/arch/pxa-regs.h>
34 #include <asm/arch/hardware.h>
35 #include <asm/arch/corgi.h>
36 #include <asm/arch/audio.h>
37
38 #include "../codecs/wm8731.h"
39 #include "pxa2xx-pcm.h"
40 #include "pxa2xx-i2s.h"
41
42 #define CORGI_HP        0
43 #define CORGI_MIC       1
44 #define CORGI_LINE      2
45 #define CORGI_HEADSET   3
46 #define CORGI_HP_OFF    4
47 #define CORGI_SPK_ON    0
48 #define CORGI_SPK_OFF   1
49
50  /* audio clock in Hz - rounded from 12.235MHz */
51 #define CORGI_AUDIO_CLOCK 12288000
52
53 static int corgi_jack_func;
54 static int corgi_spk_func;
55
56 static void corgi_ext_control(struct snd_soc_codec *codec)
57 {
58         int spk = 0, mic = 0, line = 0, hp = 0, hs = 0;
59
60         /* set up jack connection */
61         switch (corgi_jack_func) {
62         case CORGI_HP:
63                 hp = 1;
64                 /* set = unmute headphone */
65                 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
66                 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
67                 break;
68         case CORGI_MIC:
69                 mic = 1;
70                 /* reset = mute headphone */
71                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
72                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
73                 break;
74         case CORGI_LINE:
75                 line = 1;
76                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
77                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
78                 break;
79         case CORGI_HEADSET:
80                 hs = 1;
81                 mic = 1;
82                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
83                 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
84                 break;
85         }
86
87         if (corgi_spk_func == CORGI_SPK_ON)
88                 spk = 1;
89
90         /* set the enpoints to their new connetion states */
91         snd_soc_dapm_set_endpoint(codec, "Ext Spk", spk);
92         snd_soc_dapm_set_endpoint(codec, "Mic Jack", mic);
93         snd_soc_dapm_set_endpoint(codec, "Line Jack", line);
94         snd_soc_dapm_set_endpoint(codec, "Headphone Jack", hp);
95         snd_soc_dapm_set_endpoint(codec, "Headset Jack", hs);
96
97         /* signal a DAPM event */
98         snd_soc_dapm_sync_endpoints(codec);
99 }
100
101 static int corgi_startup(struct snd_pcm_substream *substream)
102 {
103         struct snd_soc_pcm_runtime *rtd = substream->private_data;
104         struct snd_soc_codec *codec = rtd->socdev->codec;
105
106         /* check the jack status at stream startup */
107         corgi_ext_control(codec);
108         return 0;
109 }
110
111 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
112 static int corgi_shutdown(struct snd_pcm_substream *substream)
113 {
114         struct snd_soc_pcm_runtime *rtd = substream->private_data;
115         struct snd_soc_codec *codec = rtd->socdev->codec;
116
117         /* set = unmute headphone */
118         set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_L);
119         set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MUTE_R);
120         return 0;
121 }
122
123 static int corgi_hw_params(struct snd_pcm_substream *substream,
124         struct snd_pcm_hw_params *params)
125 {
126         struct snd_soc_pcm_runtime *rtd = substream->private_data;
127         struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
128         struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
129         unsigned int clk = 0;
130         int ret = 0;
131
132         switch (params_rate(params)) {
133         case 8000:
134         case 16000:
135         case 48000:
136         case 96000:
137                 clk = 12288000;
138                 break;
139         case 11025:
140         case 22050:
141         case 44100:
142                 clk = 11289600;
143                 break;
144         }
145
146         /* set codec DAI configuration */
147         ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
148                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
149         if (ret < 0)
150                 return ret;
151
152         /* set cpu DAI configuration */
153         ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
154                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
155         if (ret < 0)
156                 return ret;
157
158         /* set the codec system clock for DAC and ADC */
159         ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8731_SYSCLK, clk,
160                 SND_SOC_CLOCK_IN);
161         if (ret < 0)
162                 return ret;
163
164         /* set the I2S system clock as input (unused) */
165         ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
166                 SND_SOC_CLOCK_IN);
167         if (ret < 0)
168                 return ret;
169
170         return 0;
171 }
172
173 static struct snd_soc_ops corgi_ops = {
174         .startup = corgi_startup,
175         .hw_params = corgi_hw_params,
176         .shutdown = corgi_shutdown,
177 };
178
179 static int corgi_get_jack(struct snd_kcontrol *kcontrol,
180         struct snd_ctl_elem_value *ucontrol)
181 {
182         ucontrol->value.integer.value[0] = corgi_jack_func;
183         return 0;
184 }
185
186 static int corgi_set_jack(struct snd_kcontrol *kcontrol,
187         struct snd_ctl_elem_value *ucontrol)
188 {
189         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
190
191         if (corgi_jack_func == ucontrol->value.integer.value[0])
192                 return 0;
193
194         corgi_jack_func = ucontrol->value.integer.value[0];
195         corgi_ext_control(codec);
196         return 1;
197 }
198
199 static int corgi_get_spk(struct snd_kcontrol *kcontrol,
200         struct snd_ctl_elem_value *ucontrol)
201 {
202         ucontrol->value.integer.value[0] = corgi_spk_func;
203         return 0;
204 }
205
206 static int corgi_set_spk(struct snd_kcontrol *kcontrol,
207         struct snd_ctl_elem_value *ucontrol)
208 {
209         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
210
211         if (corgi_spk_func == ucontrol->value.integer.value[0])
212                 return 0;
213
214         corgi_spk_func = ucontrol->value.integer.value[0];
215         corgi_ext_control(codec);
216         return 1;
217 }
218
219 static int corgi_amp_event(struct snd_soc_dapm_widget *w, int event)
220 {
221         if (SND_SOC_DAPM_EVENT_ON(event))
222                 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
223         else
224                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_APM_ON);
225
226         return 0;
227 }
228
229 static int corgi_mic_event(struct snd_soc_dapm_widget *w, int event)
230 {
231         if (SND_SOC_DAPM_EVENT_ON(event))
232                 set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
233         else
234                 reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_MIC_BIAS);
235
236         return 0;
237 }
238
239 /* corgi machine dapm widgets */
240 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
241 SND_SOC_DAPM_HP("Headphone Jack", NULL),
242 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
243 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
244 SND_SOC_DAPM_LINE("Line Jack", NULL),
245 SND_SOC_DAPM_HP("Headset Jack", NULL),
246 };
247
248 /* Corgi machine audio map (connections to the codec pins) */
249 static const char *audio_map[][3] = {
250
251         /* headset Jack  - in = micin, out = LHPOUT*/
252         {"Headset Jack", NULL, "LHPOUT"},
253
254         /* headphone connected to LHPOUT1, RHPOUT1 */
255         {"Headphone Jack", NULL, "LHPOUT"},
256         {"Headphone Jack", NULL, "RHPOUT"},
257
258         /* speaker connected to LOUT, ROUT */
259         {"Ext Spk", NULL, "ROUT"},
260         {"Ext Spk", NULL, "LOUT"},
261
262         /* mic is connected to MICIN (via right channel of headphone jack) */
263         {"MICIN", NULL, "Mic Jack"},
264
265         /* Same as the above but no mic bias for line signals */
266         {"MICIN", NULL, "Line Jack"},
267
268         {NULL, NULL, NULL},
269 };
270
271 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
272         "Off"};
273 static const char *spk_function[] = {"On", "Off"};
274 static const struct soc_enum corgi_enum[] = {
275         SOC_ENUM_SINGLE_EXT(5, jack_function),
276         SOC_ENUM_SINGLE_EXT(2, spk_function),
277 };
278
279 static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
280         SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
281                 corgi_set_jack),
282         SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
283                 corgi_set_spk),
284 };
285
286 /*
287  * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
288  */
289 static int corgi_wm8731_init(struct snd_soc_codec *codec)
290 {
291         int i, err;
292
293         snd_soc_dapm_set_endpoint(codec, "LLINEIN", 0);
294         snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
295
296         /* Add corgi specific controls */
297         for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
298                 err = snd_ctl_add(codec->card,
299                         snd_soc_cnew(&wm8731_corgi_controls[i],codec, NULL));
300                 if (err < 0)
301                         return err;
302         }
303
304         /* Add corgi specific widgets */
305         for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
306                 snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
307         }
308
309         /* Set up corgi specific audio path audio_map */
310         for(i = 0; audio_map[i][0] != NULL; i++) {
311                 snd_soc_dapm_connect_input(codec, audio_map[i][0],
312                         audio_map[i][1], audio_map[i][2]);
313         }
314
315         snd_soc_dapm_sync_endpoints(codec);
316         return 0;
317 }
318
319 /* corgi digital audio interface glue - connects codec <--> CPU */
320 static struct snd_soc_dai_link corgi_dai = {
321         .name = "WM8731",
322         .stream_name = "WM8731",
323         .cpu_dai = &pxa_i2s_dai,
324         .codec_dai = &wm8731_dai,
325         .init = corgi_wm8731_init,
326         .ops = &corgi_ops,
327 };
328
329 /* corgi audio machine driver */
330 static struct snd_soc_machine snd_soc_machine_corgi = {
331         .name = "Corgi",
332         .dai_link = &corgi_dai,
333         .num_links = 1,
334 };
335
336 /* corgi audio private data */
337 static struct wm8731_setup_data corgi_wm8731_setup = {
338         .i2c_address = 0x1b,
339 };
340
341 /* corgi audio subsystem */
342 static struct snd_soc_device corgi_snd_devdata = {
343         .machine = &snd_soc_machine_corgi,
344         .platform = &pxa2xx_soc_platform,
345         .codec_dev = &soc_codec_dev_wm8731,
346         .codec_data = &corgi_wm8731_setup,
347 };
348
349 static struct platform_device *corgi_snd_device;
350
351 static int __init corgi_init(void)
352 {
353         int ret;
354
355         if (!(machine_is_corgi() || machine_is_shepherd() || machine_is_husky()))
356                 return -ENODEV;
357
358         corgi_snd_device = platform_device_alloc("soc-audio", -1);
359         if (!corgi_snd_device)
360                 return -ENOMEM;
361
362         platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
363         corgi_snd_devdata.dev = &corgi_snd_device->dev;
364         ret = platform_device_add(corgi_snd_device);
365
366         if (ret)
367                 platform_device_put(corgi_snd_device);
368
369         return ret;
370 }
371
372 static void __exit corgi_exit(void)
373 {
374         platform_device_unregister(corgi_snd_device);
375 }
376
377 module_init(corgi_init);
378 module_exit(corgi_exit);
379
380 /* Module information */
381 MODULE_AUTHOR("Richard Purdie");
382 MODULE_DESCRIPTION("ALSA SoC Corgi");
383 MODULE_LICENSE("GPL");