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>
26 #include <sound/soc-dapm.h>
28 #include <asm/mach-types.h>
29 #include <mach/pxa-regs.h>
30 #include <mach/hardware.h>
31 #include <mach/corgi.h>
32 #include <mach/audio.h>
34 #include "../codecs/wm8731.h"
35 #include "pxa2xx-pcm.h"
36 #include "pxa2xx-i2s.h"
41 #define CORGI_HEADSET 3
42 #define CORGI_HP_OFF 4
43 #define CORGI_SPK_ON 0
44 #define CORGI_SPK_OFF 1
46 /* audio clock in Hz - rounded from 12.235MHz */
47 #define CORGI_AUDIO_CLOCK 12288000
49 static int corgi_jack_func
;
50 static int corgi_spk_func
;
52 static void corgi_ext_control(struct snd_soc_codec
*codec
)
54 /* set up jack connection */
55 switch (corgi_jack_func
) {
57 /* set = unmute headphone */
58 gpio_set_value(CORGI_GPIO_MUTE_L
, 1);
59 gpio_set_value(CORGI_GPIO_MUTE_R
, 1);
60 snd_soc_dapm_disable_pin(codec
, "Mic Jack");
61 snd_soc_dapm_disable_pin(codec
, "Line Jack");
62 snd_soc_dapm_enable_pin(codec
, "Headphone Jack");
63 snd_soc_dapm_disable_pin(codec
, "Headset Jack");
66 /* reset = mute headphone */
67 gpio_set_value(CORGI_GPIO_MUTE_L
, 0);
68 gpio_set_value(CORGI_GPIO_MUTE_R
, 0);
69 snd_soc_dapm_enable_pin(codec
, "Mic Jack");
70 snd_soc_dapm_disable_pin(codec
, "Line Jack");
71 snd_soc_dapm_disable_pin(codec
, "Headphone Jack");
72 snd_soc_dapm_disable_pin(codec
, "Headset Jack");
75 gpio_set_value(CORGI_GPIO_MUTE_L
, 0);
76 gpio_set_value(CORGI_GPIO_MUTE_R
, 0);
77 snd_soc_dapm_disable_pin(codec
, "Mic Jack");
78 snd_soc_dapm_enable_pin(codec
, "Line Jack");
79 snd_soc_dapm_disable_pin(codec
, "Headphone Jack");
80 snd_soc_dapm_disable_pin(codec
, "Headset Jack");
83 gpio_set_value(CORGI_GPIO_MUTE_L
, 0);
84 gpio_set_value(CORGI_GPIO_MUTE_R
, 1);
85 snd_soc_dapm_enable_pin(codec
, "Mic Jack");
86 snd_soc_dapm_disable_pin(codec
, "Line Jack");
87 snd_soc_dapm_disable_pin(codec
, "Headphone Jack");
88 snd_soc_dapm_enable_pin(codec
, "Headset Jack");
92 if (corgi_spk_func
== CORGI_SPK_ON
)
93 snd_soc_dapm_enable_pin(codec
, "Ext Spk");
95 snd_soc_dapm_disable_pin(codec
, "Ext Spk");
97 /* signal a DAPM event */
98 snd_soc_dapm_sync(codec
);
101 static int corgi_startup(struct snd_pcm_substream
*substream
)
103 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
104 struct snd_soc_codec
*codec
= rtd
->socdev
->card
->codec
;
106 /* check the jack status at stream startup */
107 corgi_ext_control(codec
);
111 /* we need to unmute the HP at shutdown as the mute burns power on corgi */
112 static void corgi_shutdown(struct snd_pcm_substream
*substream
)
114 /* set = unmute headphone */
115 gpio_set_value(CORGI_GPIO_MUTE_L
, 1);
116 gpio_set_value(CORGI_GPIO_MUTE_R
, 1);
119 static int corgi_hw_params(struct snd_pcm_substream
*substream
,
120 struct snd_pcm_hw_params
*params
)
122 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
123 struct snd_soc_dai
*codec_dai
= rtd
->dai
->codec_dai
;
124 struct snd_soc_dai
*cpu_dai
= rtd
->dai
->cpu_dai
;
125 unsigned int clk
= 0;
128 switch (params_rate(params
)) {
142 /* set codec DAI configuration */
143 ret
= snd_soc_dai_set_fmt(codec_dai
, SND_SOC_DAIFMT_I2S
|
144 SND_SOC_DAIFMT_NB_NF
| SND_SOC_DAIFMT_CBS_CFS
);
148 /* set cpu DAI configuration */
149 ret
= snd_soc_dai_set_fmt(cpu_dai
, SND_SOC_DAIFMT_I2S
|
150 SND_SOC_DAIFMT_NB_NF
| SND_SOC_DAIFMT_CBS_CFS
);
154 /* set the codec system clock for DAC and ADC */
155 ret
= snd_soc_dai_set_sysclk(codec_dai
, WM8731_SYSCLK
, clk
,
160 /* set the I2S system clock as input (unused) */
161 ret
= snd_soc_dai_set_sysclk(cpu_dai
, PXA2XX_I2S_SYSCLK
, 0,
169 static struct snd_soc_ops corgi_ops
= {
170 .startup
= corgi_startup
,
171 .hw_params
= corgi_hw_params
,
172 .shutdown
= corgi_shutdown
,
175 static int corgi_get_jack(struct snd_kcontrol
*kcontrol
,
176 struct snd_ctl_elem_value
*ucontrol
)
178 ucontrol
->value
.integer
.value
[0] = corgi_jack_func
;
182 static int corgi_set_jack(struct snd_kcontrol
*kcontrol
,
183 struct snd_ctl_elem_value
*ucontrol
)
185 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
187 if (corgi_jack_func
== ucontrol
->value
.integer
.value
[0])
190 corgi_jack_func
= ucontrol
->value
.integer
.value
[0];
191 corgi_ext_control(codec
);
195 static int corgi_get_spk(struct snd_kcontrol
*kcontrol
,
196 struct snd_ctl_elem_value
*ucontrol
)
198 ucontrol
->value
.integer
.value
[0] = corgi_spk_func
;
202 static int corgi_set_spk(struct snd_kcontrol
*kcontrol
,
203 struct snd_ctl_elem_value
*ucontrol
)
205 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
207 if (corgi_spk_func
== ucontrol
->value
.integer
.value
[0])
210 corgi_spk_func
= ucontrol
->value
.integer
.value
[0];
211 corgi_ext_control(codec
);
215 static int corgi_amp_event(struct snd_soc_dapm_widget
*w
,
216 struct snd_kcontrol
*k
, int event
)
218 gpio_set_value(CORGI_GPIO_APM_ON
, SND_SOC_DAPM_EVENT_ON(event
));
222 static int corgi_mic_event(struct snd_soc_dapm_widget
*w
,
223 struct snd_kcontrol
*k
, int event
)
225 gpio_set_value(CORGI_GPIO_MIC_BIAS
, SND_SOC_DAPM_EVENT_ON(event
));
229 /* corgi machine dapm widgets */
230 static const struct snd_soc_dapm_widget wm8731_dapm_widgets
[] = {
231 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
232 SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event
),
233 SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event
),
234 SND_SOC_DAPM_LINE("Line Jack", NULL
),
235 SND_SOC_DAPM_HP("Headset Jack", NULL
),
238 /* Corgi machine audio map (connections to the codec pins) */
239 static const struct snd_soc_dapm_route audio_map
[] = {
241 /* headset Jack - in = micin, out = LHPOUT*/
242 {"Headset Jack", NULL
, "LHPOUT"},
244 /* headphone connected to LHPOUT1, RHPOUT1 */
245 {"Headphone Jack", NULL
, "LHPOUT"},
246 {"Headphone Jack", NULL
, "RHPOUT"},
248 /* speaker connected to LOUT, ROUT */
249 {"Ext Spk", NULL
, "ROUT"},
250 {"Ext Spk", NULL
, "LOUT"},
252 /* mic is connected to MICIN (via right channel of headphone jack) */
253 {"MICIN", NULL
, "Mic Jack"},
255 /* Same as the above but no mic bias for line signals */
256 {"MICIN", NULL
, "Line Jack"},
259 static const char *jack_function
[] = {"Headphone", "Mic", "Line", "Headset",
261 static const char *spk_function
[] = {"On", "Off"};
262 static const struct soc_enum corgi_enum
[] = {
263 SOC_ENUM_SINGLE_EXT(5, jack_function
),
264 SOC_ENUM_SINGLE_EXT(2, spk_function
),
267 static const struct snd_kcontrol_new wm8731_corgi_controls
[] = {
268 SOC_ENUM_EXT("Jack Function", corgi_enum
[0], corgi_get_jack
,
270 SOC_ENUM_EXT("Speaker Function", corgi_enum
[1], corgi_get_spk
,
275 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
277 static int corgi_wm8731_init(struct snd_soc_codec
*codec
)
281 snd_soc_dapm_nc_pin(codec
, "LLINEIN");
282 snd_soc_dapm_nc_pin(codec
, "RLINEIN");
284 /* Add corgi specific controls */
285 err
= snd_soc_add_controls(codec
, wm8731_corgi_controls
,
286 ARRAY_SIZE(wm8731_corgi_controls
));
290 /* Add corgi specific widgets */
291 snd_soc_dapm_new_controls(codec
, wm8731_dapm_widgets
,
292 ARRAY_SIZE(wm8731_dapm_widgets
));
294 /* Set up corgi specific audio path audio_map */
295 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
297 snd_soc_dapm_sync(codec
);
301 /* corgi digital audio interface glue - connects codec <--> CPU */
302 static struct snd_soc_dai_link corgi_dai
= {
304 .stream_name
= "WM8731",
305 .cpu_dai
= &pxa_i2s_dai
,
306 .codec_dai
= &wm8731_dai
,
307 .init
= corgi_wm8731_init
,
311 /* corgi audio machine driver */
312 static struct snd_soc_card snd_soc_corgi
= {
314 .platform
= &pxa2xx_soc_platform
,
315 .dai_link
= &corgi_dai
,
319 /* corgi audio subsystem */
320 static struct snd_soc_device corgi_snd_devdata
= {
321 .card
= &snd_soc_corgi
,
322 .codec_dev
= &soc_codec_dev_wm8731
,
326 * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
327 * New drivers should register the wm8731 I2C device in the machine
328 * setup code (under arch/arm for ARM systems).
330 static int wm8731_i2c_register(void)
332 struct i2c_board_info info
;
333 struct i2c_adapter
*adapter
;
334 struct i2c_client
*client
;
336 memset(&info
, 0, sizeof(struct i2c_board_info
));
338 strlcpy(info
.type
, "wm8731", I2C_NAME_SIZE
);
340 adapter
= i2c_get_adapter(0);
342 printk(KERN_ERR
"can't get i2c adapter 0\n");
346 client
= i2c_new_device(adapter
, &info
);
347 i2c_put_adapter(adapter
);
349 printk(KERN_ERR
"can't add i2c device at 0x%x\n",
350 (unsigned int)info
.addr
);
357 static struct platform_device
*corgi_snd_device
;
359 static int __init
corgi_init(void)
363 if (!(machine_is_corgi() || machine_is_shepherd() ||
367 ret
= wm8731_i2c_register();
371 corgi_snd_device
= platform_device_alloc("soc-audio", -1);
372 if (!corgi_snd_device
)
375 platform_set_drvdata(corgi_snd_device
, &corgi_snd_devdata
);
376 corgi_snd_devdata
.dev
= &corgi_snd_device
->dev
;
377 ret
= platform_device_add(corgi_snd_device
);
380 platform_device_put(corgi_snd_device
);
385 static void __exit
corgi_exit(void)
387 platform_device_unregister(corgi_snd_device
);
390 module_init(corgi_init
);
391 module_exit(corgi_exit
);
393 /* Module information */
394 MODULE_AUTHOR("Richard Purdie");
395 MODULE_DESCRIPTION("ALSA SoC Corgi");
396 MODULE_LICENSE("GPL");