2 * corgi.c -- SoC audio for Corgi
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
8 * Richard Purdie <richard@openedhand.com>
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.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/timer.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/soc.h>
27 #include <asm/mach-types.h>
28 #include <mach/corgi.h>
29 #include <mach/audio.h>
31 #include "../codecs/wm8731.h"
32 #include "pxa2xx-i2s.h"
37 #define CORGI_HEADSET 3
38 #define CORGI_HP_OFF 4
39 #define CORGI_SPK_ON 0
40 #define CORGI_SPK_OFF 1
42 /* audio clock in Hz - rounded from 12.235MHz */
43 #define CORGI_AUDIO_CLOCK 12288000
45 static int corgi_jack_func
;
46 static int corgi_spk_func
;
48 static void corgi_ext_control(struct snd_soc_dapm_context
*dapm
)
50 /* set up jack connection */
51 switch (corgi_jack_func
) {
53 /* set = unmute headphone */
54 gpio_set_value(CORGI_GPIO_MUTE_L
, 1);
55 gpio_set_value(CORGI_GPIO_MUTE_R
, 1);
56 snd_soc_dapm_disable_pin(dapm
, "Mic Jack");
57 snd_soc_dapm_disable_pin(dapm
, "Line Jack");
58 snd_soc_dapm_enable_pin(dapm
, "Headphone Jack");
59 snd_soc_dapm_disable_pin(dapm
, "Headset Jack");
62 /* reset = mute headphone */
63 gpio_set_value(CORGI_GPIO_MUTE_L
, 0);
64 gpio_set_value(CORGI_GPIO_MUTE_R
, 0);
65 snd_soc_dapm_enable_pin(dapm
, "Mic Jack");
66 snd_soc_dapm_disable_pin(dapm
, "Line Jack");
67 snd_soc_dapm_disable_pin(dapm
, "Headphone Jack");
68 snd_soc_dapm_disable_pin(dapm
, "Headset Jack");
71 gpio_set_value(CORGI_GPIO_MUTE_L
, 0);
72 gpio_set_value(CORGI_GPIO_MUTE_R
, 0);
73 snd_soc_dapm_disable_pin(dapm
, "Mic Jack");
74 snd_soc_dapm_enable_pin(dapm
, "Line Jack");
75 snd_soc_dapm_disable_pin(dapm
, "Headphone Jack");
76 snd_soc_dapm_disable_pin(dapm
, "Headset Jack");
79 gpio_set_value(CORGI_GPIO_MUTE_L
, 0);
80 gpio_set_value(CORGI_GPIO_MUTE_R
, 1);
81 snd_soc_dapm_enable_pin(dapm
, "Mic Jack");
82 snd_soc_dapm_disable_pin(dapm
, "Line Jack");
83 snd_soc_dapm_disable_pin(dapm
, "Headphone Jack");
84 snd_soc_dapm_enable_pin(dapm
, "Headset Jack");
88 if (corgi_spk_func
== CORGI_SPK_ON
)
89 snd_soc_dapm_enable_pin(dapm
, "Ext Spk");
91 snd_soc_dapm_disable_pin(dapm
, "Ext Spk");
93 /* signal a DAPM event */
94 snd_soc_dapm_sync(dapm
);
97 static int corgi_startup(struct snd_pcm_substream
*substream
)
99 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
100 struct snd_soc_codec
*codec
= rtd
->codec
;
102 mutex_lock(&codec
->mutex
);
104 /* check the jack status at stream startup */
105 corgi_ext_control(&codec
->dapm
);
107 mutex_unlock(&codec
->mutex
);
112 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
113 static void corgi_shutdown(struct snd_pcm_substream
*substream
)
115 /* set = unmute headphone */
116 gpio_set_value(CORGI_GPIO_MUTE_L
, 1);
117 gpio_set_value(CORGI_GPIO_MUTE_R
, 1);
120 static int corgi_hw_params(struct snd_pcm_substream
*substream
,
121 struct snd_pcm_hw_params
*params
)
123 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
124 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
125 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
126 unsigned int clk
= 0;
129 switch (params_rate(params
)) {
143 /* set the codec system clock for DAC and ADC */
144 ret
= snd_soc_dai_set_sysclk(codec_dai
, WM8731_SYSCLK_XTAL
, clk
,
149 /* set the I2S system clock as input (unused) */
150 ret
= snd_soc_dai_set_sysclk(cpu_dai
, PXA2XX_I2S_SYSCLK
, 0,
158 static struct snd_soc_ops corgi_ops
= {
159 .startup
= corgi_startup
,
160 .hw_params
= corgi_hw_params
,
161 .shutdown
= corgi_shutdown
,
164 static int corgi_get_jack(struct snd_kcontrol
*kcontrol
,
165 struct snd_ctl_elem_value
*ucontrol
)
167 ucontrol
->value
.integer
.value
[0] = corgi_jack_func
;
171 static int corgi_set_jack(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
176 if (corgi_jack_func
== ucontrol
->value
.integer
.value
[0])
179 corgi_jack_func
= ucontrol
->value
.integer
.value
[0];
180 corgi_ext_control(&card
->dapm
);
184 static int corgi_get_spk(struct snd_kcontrol
*kcontrol
,
185 struct snd_ctl_elem_value
*ucontrol
)
187 ucontrol
->value
.integer
.value
[0] = corgi_spk_func
;
191 static int corgi_set_spk(struct snd_kcontrol
*kcontrol
,
192 struct snd_ctl_elem_value
*ucontrol
)
194 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
196 if (corgi_spk_func
== ucontrol
->value
.integer
.value
[0])
199 corgi_spk_func
= ucontrol
->value
.integer
.value
[0];
200 corgi_ext_control(&card
->dapm
);
204 static int corgi_amp_event(struct snd_soc_dapm_widget
*w
,
205 struct snd_kcontrol
*k
, int event
)
207 gpio_set_value(CORGI_GPIO_APM_ON
, SND_SOC_DAPM_EVENT_ON(event
));
211 static int corgi_mic_event(struct snd_soc_dapm_widget
*w
,
212 struct snd_kcontrol
*k
, int event
)
214 gpio_set_value(CORGI_GPIO_MIC_BIAS
, SND_SOC_DAPM_EVENT_ON(event
));
218 /* corgi machine dapm widgets */
219 static const struct snd_soc_dapm_widget wm8731_dapm_widgets
[] = {
220 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
221 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event
),
222 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event
),
223 SND_SOC_DAPM_LINE("Line Jack", NULL
),
224 SND_SOC_DAPM_HP("Headset Jack", NULL
),
227 /* Corgi machine audio map (connections to the codec pins) */
228 static const struct snd_soc_dapm_route corgi_audio_map
[] = {
230 /* headset Jack - in = micin, out = LHPOUT*/
231 {"Headset Jack", NULL
, "LHPOUT"},
233 /* headphone connected to LHPOUT1, RHPOUT1 */
234 {"Headphone Jack", NULL
, "LHPOUT"},
235 {"Headphone Jack", NULL
, "RHPOUT"},
237 /* speaker connected to LOUT, ROUT */
238 {"Ext Spk", NULL
, "ROUT"},
239 {"Ext Spk", NULL
, "LOUT"},
241 /* mic is connected to MICIN (via right channel of headphone jack) */
242 {"MICIN", NULL
, "Mic Jack"},
244 /* Same as the above but no mic bias for line signals */
245 {"MICIN", NULL
, "Line Jack"},
248 static const char *jack_function
[] = {"Headphone", "Mic", "Line", "Headset",
250 static const char *spk_function
[] = {"On", "Off"};
251 static const struct soc_enum corgi_enum
[] = {
252 SOC_ENUM_SINGLE_EXT(5, jack_function
),
253 SOC_ENUM_SINGLE_EXT(2, spk_function
),
256 static const struct snd_kcontrol_new wm8731_corgi_controls
[] = {
257 SOC_ENUM_EXT("Jack Function", corgi_enum
[0], corgi_get_jack
,
259 SOC_ENUM_EXT("Speaker Function", corgi_enum
[1], corgi_get_spk
,
264 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
266 static int corgi_wm8731_init(struct snd_soc_pcm_runtime
*rtd
)
268 struct snd_soc_codec
*codec
= rtd
->codec
;
269 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
271 snd_soc_dapm_nc_pin(dapm
, "LLINEIN");
272 snd_soc_dapm_nc_pin(dapm
, "RLINEIN");
277 /* corgi digital audio interface glue - connects codec <--> CPU */
278 static struct snd_soc_dai_link corgi_dai
= {
280 .stream_name
= "WM8731",
281 .cpu_dai_name
= "pxa2xx-i2s",
282 .codec_dai_name
= "wm8731-hifi",
283 .platform_name
= "pxa-pcm-audio",
284 .codec_name
= "wm8731.0-001b",
285 .init
= corgi_wm8731_init
,
286 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
287 SND_SOC_DAIFMT_CBS_CFS
,
291 /* corgi audio machine driver */
292 static struct snd_soc_card corgi
= {
294 .owner
= THIS_MODULE
,
295 .dai_link
= &corgi_dai
,
298 .controls
= wm8731_corgi_controls
,
299 .num_controls
= ARRAY_SIZE(wm8731_corgi_controls
),
300 .dapm_widgets
= wm8731_dapm_widgets
,
301 .num_dapm_widgets
= ARRAY_SIZE(wm8731_dapm_widgets
),
302 .dapm_routes
= corgi_audio_map
,
303 .num_dapm_routes
= ARRAY_SIZE(corgi_audio_map
),
306 static int __devinit
corgi_probe(struct platform_device
*pdev
)
308 struct snd_soc_card
*card
= &corgi
;
311 card
->dev
= &pdev
->dev
;
313 ret
= snd_soc_register_card(card
);
315 dev_err(&pdev
->dev
, "snd_soc_register_card() failed: %d\n",
320 static int __devexit
corgi_remove(struct platform_device
*pdev
)
322 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
324 snd_soc_unregister_card(card
);
328 static struct platform_driver corgi_driver
= {
330 .name
= "corgi-audio",
331 .owner
= THIS_MODULE
,
333 .probe
= corgi_probe
,
334 .remove
= __devexit_p(corgi_remove
),
337 module_platform_driver(corgi_driver
);
339 /* Module information */
340 MODULE_AUTHOR("Richard Purdie");
341 MODULE_DESCRIPTION("ALSA SoC Corgi");
342 MODULE_LICENSE("GPL");
343 MODULE_ALIAS("platform:corgi-audio");