blob: fefe1a57f31adb329493bc482668a053afb65ea8 [file] [log] [blame]
Liam Girdwooda1eb4b32006-10-12 14:31:16 +02001/*
2 * corgi.c -- SoC audio for Corgi
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +02008 * 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.
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020014 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/timer.h>
Mark Brownfc996752009-02-18 12:34:53 +000019#include <linux/i2c.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020020#include <linux/interrupt.h>
21#include <linux/platform_device.h>
Eric Miao6168cda2008-09-05 18:15:22 +080022#include <linux/gpio.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/soc.h>
26#include <sound/soc-dapm.h>
27
28#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/corgi.h>
30#include <mach/audio.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020031
32#include "../codecs/wm8731.h"
33#include "pxa2xx-pcm.h"
Liam Girdwoodd928b252007-02-02 17:22:20 +010034#include "pxa2xx-i2s.h"
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020035
36#define CORGI_HP 0
37#define CORGI_MIC 1
38#define CORGI_LINE 2
39#define CORGI_HEADSET 3
40#define CORGI_HP_OFF 4
41#define CORGI_SPK_ON 0
42#define CORGI_SPK_OFF 1
43
44 /* audio clock in Hz - rounded from 12.235MHz */
45#define CORGI_AUDIO_CLOCK 12288000
46
47static int corgi_jack_func;
48static int corgi_spk_func;
49
50static void corgi_ext_control(struct snd_soc_codec *codec)
51{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020052 /* set up jack connection */
53 switch (corgi_jack_func) {
54 case CORGI_HP:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020055 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080056 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
57 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010058 snd_soc_dapm_disable_pin(codec, "Mic Jack");
59 snd_soc_dapm_disable_pin(codec, "Line Jack");
60 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
61 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020062 break;
63 case CORGI_MIC:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020064 /* reset = mute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080065 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
66 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010067 snd_soc_dapm_enable_pin(codec, "Mic Jack");
68 snd_soc_dapm_disable_pin(codec, "Line Jack");
69 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
70 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020071 break;
72 case CORGI_LINE:
Eric Miao6168cda2008-09-05 18:15:22 +080073 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
74 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010075 snd_soc_dapm_disable_pin(codec, "Mic Jack");
76 snd_soc_dapm_enable_pin(codec, "Line Jack");
77 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
78 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020079 break;
80 case CORGI_HEADSET:
Eric Miao6168cda2008-09-05 18:15:22 +080081 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
82 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010083 snd_soc_dapm_enable_pin(codec, "Mic Jack");
84 snd_soc_dapm_disable_pin(codec, "Line Jack");
85 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
86 snd_soc_dapm_enable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020087 break;
88 }
89
90 if (corgi_spk_func == CORGI_SPK_ON)
Liam Girdwooda5302182008-07-07 13:35:17 +010091 snd_soc_dapm_enable_pin(codec, "Ext Spk");
92 else
93 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020094
95 /* signal a DAPM event */
Liam Girdwooda5302182008-07-07 13:35:17 +010096 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020097}
98
99static int corgi_startup(struct snd_pcm_substream *substream)
100{
101 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwai9e30d772009-02-11 08:28:04 +0100102 struct snd_soc_codec *codec = rtd->socdev->card->codec;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200103
104 /* check the jack status at stream startup */
105 corgi_ext_control(codec);
106 return 0;
107}
108
109/* we need to unmute the HP at shutdown as the mute burns power on corgi */
Takashi Iwai5220ed62008-12-01 20:00:47 +0100110static void corgi_shutdown(struct snd_pcm_substream *substream)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200111{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200112 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +0800113 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
114 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200115}
116
Liam Girdwoodd928b252007-02-02 17:22:20 +0100117static int corgi_hw_params(struct snd_pcm_substream *substream,
118 struct snd_pcm_hw_params *params)
119{
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood917f93a2008-07-07 16:08:11 +0100121 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
122 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Liam Girdwoodd928b252007-02-02 17:22:20 +0100123 unsigned int clk = 0;
124 int ret = 0;
125
126 switch (params_rate(params)) {
127 case 8000:
128 case 16000:
129 case 48000:
130 case 96000:
131 clk = 12288000;
132 break;
133 case 11025:
134 case 22050:
135 case 44100:
136 clk = 11289600;
137 break;
138 }
139
140 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100141 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100142 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
143 if (ret < 0)
144 return ret;
145
146 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100147 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100148 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
149 if (ret < 0)
150 return ret;
151
152 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100153 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100154 SND_SOC_CLOCK_IN);
155 if (ret < 0)
156 return ret;
157
158 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100159 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100160 SND_SOC_CLOCK_IN);
161 if (ret < 0)
162 return ret;
163
164 return 0;
165}
166
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200167static struct snd_soc_ops corgi_ops = {
168 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100169 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200170 .shutdown = corgi_shutdown,
171};
172
173static int corgi_get_jack(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176 ucontrol->value.integer.value[0] = corgi_jack_func;
177 return 0;
178}
179
180static int corgi_set_jack(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *ucontrol)
182{
183 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
184
185 if (corgi_jack_func == ucontrol->value.integer.value[0])
186 return 0;
187
188 corgi_jack_func = ucontrol->value.integer.value[0];
189 corgi_ext_control(codec);
190 return 1;
191}
192
193static int corgi_get_spk(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
195{
196 ucontrol->value.integer.value[0] = corgi_spk_func;
197 return 0;
198}
199
200static int corgi_set_spk(struct snd_kcontrol *kcontrol,
201 struct snd_ctl_elem_value *ucontrol)
202{
203 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
204
205 if (corgi_spk_func == ucontrol->value.integer.value[0])
206 return 0;
207
208 corgi_spk_func = ucontrol->value.integer.value[0];
209 corgi_ext_control(codec);
210 return 1;
211}
212
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100213static int corgi_amp_event(struct snd_soc_dapm_widget *w,
214 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200215{
Eric Miao6168cda2008-09-05 18:15:22 +0800216 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200217 return 0;
218}
219
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100220static int corgi_mic_event(struct snd_soc_dapm_widget *w,
221 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200222{
Eric Miao6168cda2008-09-05 18:15:22 +0800223 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200224 return 0;
225}
226
227/* corgi machine dapm widgets */
228static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
229SND_SOC_DAPM_HP("Headphone Jack", NULL),
230SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
231SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
232SND_SOC_DAPM_LINE("Line Jack", NULL),
233SND_SOC_DAPM_HP("Headset Jack", NULL),
234};
235
236/* Corgi machine audio map (connections to the codec pins) */
Mark Brown25191c42008-05-13 14:55:48 +0200237static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200238
239 /* headset Jack - in = micin, out = LHPOUT*/
240 {"Headset Jack", NULL, "LHPOUT"},
241
242 /* headphone connected to LHPOUT1, RHPOUT1 */
243 {"Headphone Jack", NULL, "LHPOUT"},
244 {"Headphone Jack", NULL, "RHPOUT"},
245
246 /* speaker connected to LOUT, ROUT */
247 {"Ext Spk", NULL, "ROUT"},
248 {"Ext Spk", NULL, "LOUT"},
249
250 /* mic is connected to MICIN (via right channel of headphone jack) */
251 {"MICIN", NULL, "Mic Jack"},
252
253 /* Same as the above but no mic bias for line signals */
254 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200255};
256
257static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
258 "Off"};
259static const char *spk_function[] = {"On", "Off"};
260static const struct soc_enum corgi_enum[] = {
261 SOC_ENUM_SINGLE_EXT(5, jack_function),
262 SOC_ENUM_SINGLE_EXT(2, spk_function),
263};
264
265static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
266 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
267 corgi_set_jack),
268 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
269 corgi_set_spk),
270};
271
272/*
273 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
274 */
275static int corgi_wm8731_init(struct snd_soc_codec *codec)
276{
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100277 int err;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200278
Mark Brown35f5e542008-09-27 10:45:09 +0100279 snd_soc_dapm_nc_pin(codec, "LLINEIN");
280 snd_soc_dapm_nc_pin(codec, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200281
282 /* Add corgi specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100283 err = snd_soc_add_controls(codec, wm8731_corgi_controls,
284 ARRAY_SIZE(wm8731_corgi_controls));
285 if (err < 0)
286 return err;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200287
288 /* Add corgi specific widgets */
Mark Brown25191c42008-05-13 14:55:48 +0200289 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
290 ARRAY_SIZE(wm8731_dapm_widgets));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200291
292 /* Set up corgi specific audio path audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200293 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200294
Liam Girdwooda5302182008-07-07 13:35:17 +0100295 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200296 return 0;
297}
298
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200299/* corgi digital audio interface glue - connects codec <--> CPU */
300static struct snd_soc_dai_link corgi_dai = {
301 .name = "WM8731",
302 .stream_name = "WM8731",
303 .cpu_dai = &pxa_i2s_dai,
304 .codec_dai = &wm8731_dai,
305 .init = corgi_wm8731_init,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100306 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200307};
308
309/* corgi audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000310static struct snd_soc_card snd_soc_corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200311 .name = "Corgi",
Mark Brown87689d52008-12-02 16:01:14 +0000312 .platform = &pxa2xx_soc_platform,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200313 .dai_link = &corgi_dai,
314 .num_links = 1,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200315};
316
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200317/* corgi audio subsystem */
318static struct snd_soc_device corgi_snd_devdata = {
Mark Brown87506542008-11-18 20:50:34 +0000319 .card = &snd_soc_corgi,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200320 .codec_dev = &soc_codec_dev_wm8731,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200321};
322
323static struct platform_device *corgi_snd_device;
324
325static int __init corgi_init(void)
326{
327 int ret;
328
Mark Brown1bfcd362008-04-23 15:12:19 +0200329 if (!(machine_is_corgi() || machine_is_shepherd() ||
330 machine_is_husky()))
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200331 return -ENODEV;
332
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200333 corgi_snd_device = platform_device_alloc("soc-audio", -1);
334 if (!corgi_snd_device)
335 return -ENOMEM;
336
337 platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
338 corgi_snd_devdata.dev = &corgi_snd_device->dev;
339 ret = platform_device_add(corgi_snd_device);
340
341 if (ret)
342 platform_device_put(corgi_snd_device);
343
344 return ret;
345}
346
347static void __exit corgi_exit(void)
348{
349 platform_device_unregister(corgi_snd_device);
350}
351
352module_init(corgi_init);
353module_exit(corgi_exit);
354
355/* Module information */
356MODULE_AUTHOR("Richard Purdie");
357MODULE_DESCRIPTION("ALSA SoC Corgi");
358MODULE_LICENSE("GPL");