blob: bc21944851c4c69f274fc4df418dd499e45a0bdd [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>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020026
27#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/corgi.h>
29#include <mach/audio.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020030
31#include "../codecs/wm8731.h"
Liam Girdwoodd928b252007-02-02 17:22:20 +010032#include "pxa2xx-i2s.h"
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020033
34#define CORGI_HP 0
35#define CORGI_MIC 1
36#define CORGI_LINE 2
37#define CORGI_HEADSET 3
38#define CORGI_HP_OFF 4
39#define CORGI_SPK_ON 0
40#define CORGI_SPK_OFF 1
41
42 /* audio clock in Hz - rounded from 12.235MHz */
43#define CORGI_AUDIO_CLOCK 12288000
44
45static int corgi_jack_func;
46static int corgi_spk_func;
47
48static void corgi_ext_control(struct snd_soc_codec *codec)
49{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020050 struct snd_soc_dapm_context *dapm = &codec->dapm;
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 Girdwoodce6120c2010-11-05 15:53:46 +020058 snd_soc_dapm_disable_pin(dapm, "Mic Jack");
59 snd_soc_dapm_disable_pin(dapm, "Line Jack");
60 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
61 snd_soc_dapm_disable_pin(dapm, "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 Girdwoodce6120c2010-11-05 15:53:46 +020067 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
68 snd_soc_dapm_disable_pin(dapm, "Line Jack");
69 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
70 snd_soc_dapm_disable_pin(dapm, "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 Girdwoodce6120c2010-11-05 15:53:46 +020075 snd_soc_dapm_disable_pin(dapm, "Mic Jack");
76 snd_soc_dapm_enable_pin(dapm, "Line Jack");
77 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
78 snd_soc_dapm_disable_pin(dapm, "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 Girdwoodce6120c2010-11-05 15:53:46 +020083 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
84 snd_soc_dapm_disable_pin(dapm, "Line Jack");
85 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
86 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020087 break;
88 }
89
90 if (corgi_spk_func == CORGI_SPK_ON)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020091 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
Liam Girdwooda5302182008-07-07 13:35:17 +010092 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020093 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020094
95 /* signal a DAPM event */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020096 snd_soc_dapm_sync(dapm);
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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000102 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200103
Mark Brown71a29562010-11-05 13:50:48 -0400104 mutex_lock(&codec->mutex);
105
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200106 /* check the jack status at stream startup */
107 corgi_ext_control(codec);
Mark Brown71a29562010-11-05 13:50:48 -0400108
109 mutex_unlock(&codec->mutex);
110
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200111 return 0;
112}
113
114/* we need to unmute the HP at shutdown as the mute burns power on corgi */
Takashi Iwai5220ed62008-12-01 20:00:47 +0100115static void corgi_shutdown(struct snd_pcm_substream *substream)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200116{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200117 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +0800118 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
119 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200120}
121
Liam Girdwoodd928b252007-02-02 17:22:20 +0100122static int corgi_hw_params(struct snd_pcm_substream *substream,
123 struct snd_pcm_hw_params *params)
124{
125 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000126 struct snd_soc_dai *codec_dai = rtd->codec_dai;
127 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodd928b252007-02-02 17:22:20 +0100128 unsigned int clk = 0;
129 int ret = 0;
130
131 switch (params_rate(params)) {
132 case 8000:
133 case 16000:
134 case 48000:
135 case 96000:
136 clk = 12288000;
137 break;
138 case 11025:
139 case 22050:
140 case 44100:
141 clk = 11289600;
142 break;
143 }
144
Liam Girdwoodd928b252007-02-02 17:22:20 +0100145 /* set the codec system clock for DAC and ADC */
Mark Brown9745e822010-08-17 23:40:24 +0100146 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100147 SND_SOC_CLOCK_IN);
148 if (ret < 0)
149 return ret;
150
151 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100152 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100153 SND_SOC_CLOCK_IN);
154 if (ret < 0)
155 return ret;
156
157 return 0;
158}
159
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200160static struct snd_soc_ops corgi_ops = {
161 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100162 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200163 .shutdown = corgi_shutdown,
164};
165
166static int corgi_get_jack(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
168{
169 ucontrol->value.integer.value[0] = corgi_jack_func;
170 return 0;
171}
172
173static int corgi_set_jack(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
177
178 if (corgi_jack_func == ucontrol->value.integer.value[0])
179 return 0;
180
181 corgi_jack_func = ucontrol->value.integer.value[0];
182 corgi_ext_control(codec);
183 return 1;
184}
185
186static int corgi_get_spk(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
189 ucontrol->value.integer.value[0] = corgi_spk_func;
190 return 0;
191}
192
193static int corgi_set_spk(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
195{
196 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
197
198 if (corgi_spk_func == ucontrol->value.integer.value[0])
199 return 0;
200
201 corgi_spk_func = ucontrol->value.integer.value[0];
202 corgi_ext_control(codec);
203 return 1;
204}
205
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100206static int corgi_amp_event(struct snd_soc_dapm_widget *w,
207 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200208{
Eric Miao6168cda2008-09-05 18:15:22 +0800209 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200210 return 0;
211}
212
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100213static int corgi_mic_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_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200217 return 0;
218}
219
220/* corgi machine dapm widgets */
221static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
222SND_SOC_DAPM_HP("Headphone Jack", NULL),
223SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
224SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
225SND_SOC_DAPM_LINE("Line Jack", NULL),
226SND_SOC_DAPM_HP("Headset Jack", NULL),
227};
228
229/* Corgi machine audio map (connections to the codec pins) */
Axel Lin32696af2011-12-30 11:16:32 +0800230static const struct snd_soc_dapm_route corgi_audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200231
232 /* headset Jack - in = micin, out = LHPOUT*/
233 {"Headset Jack", NULL, "LHPOUT"},
234
235 /* headphone connected to LHPOUT1, RHPOUT1 */
236 {"Headphone Jack", NULL, "LHPOUT"},
237 {"Headphone Jack", NULL, "RHPOUT"},
238
239 /* speaker connected to LOUT, ROUT */
240 {"Ext Spk", NULL, "ROUT"},
241 {"Ext Spk", NULL, "LOUT"},
242
243 /* mic is connected to MICIN (via right channel of headphone jack) */
244 {"MICIN", NULL, "Mic Jack"},
245
246 /* Same as the above but no mic bias for line signals */
247 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200248};
249
250static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
251 "Off"};
252static const char *spk_function[] = {"On", "Off"};
253static const struct soc_enum corgi_enum[] = {
254 SOC_ENUM_SINGLE_EXT(5, jack_function),
255 SOC_ENUM_SINGLE_EXT(2, spk_function),
256};
257
258static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
259 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
260 corgi_set_jack),
261 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
262 corgi_set_spk),
263};
264
265/*
266 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
267 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000268static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200269{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000270 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200272
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200273 snd_soc_dapm_nc_pin(dapm, "LLINEIN");
274 snd_soc_dapm_nc_pin(dapm, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200275
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200276 return 0;
277}
278
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200279/* corgi digital audio interface glue - connects codec <--> CPU */
280static struct snd_soc_dai_link corgi_dai = {
281 .name = "WM8731",
282 .stream_name = "WM8731",
Dmitry Eremin-Solenikova3adfa02011-01-21 22:14:17 +0300283 .cpu_dai_name = "pxa2xx-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284 .codec_dai_name = "wm8731-hifi",
285 .platform_name = "pxa-pcm-audio",
Mark Brown99b59f32011-03-27 14:35:15 +0100286 .codec_name = "wm8731.0-001b",
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200287 .init = corgi_wm8731_init,
Axel Lin74f4dd52011-12-30 11:13:24 +0800288 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
289 SND_SOC_DAIFMT_CBS_CFS,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100290 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200291};
292
293/* corgi audio machine driver */
Axel Lin34be9242011-12-30 11:18:13 +0800294static struct snd_soc_card corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200295 .name = "Corgi",
Axel Lin561c6a12011-12-22 09:44:43 +0800296 .owner = THIS_MODULE,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200297 .dai_link = &corgi_dai,
298 .num_links = 1,
Axel Lin32696af2011-12-30 11:16:32 +0800299
300 .controls = wm8731_corgi_controls,
301 .num_controls = ARRAY_SIZE(wm8731_corgi_controls),
302 .dapm_widgets = wm8731_dapm_widgets,
303 .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
304 .dapm_routes = corgi_audio_map,
305 .num_dapm_routes = ARRAY_SIZE(corgi_audio_map),
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200306};
307
Axel Lin34be9242011-12-30 11:18:13 +0800308static int __devinit corgi_probe(struct platform_device *pdev)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200309{
Axel Lin34be9242011-12-30 11:18:13 +0800310 struct snd_soc_card *card = &corgi;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200311 int ret;
312
Axel Lin34be9242011-12-30 11:18:13 +0800313 card->dev = &pdev->dev;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200314
Axel Lin34be9242011-12-30 11:18:13 +0800315 ret = snd_soc_register_card(card);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200316 if (ret)
Axel Lin34be9242011-12-30 11:18:13 +0800317 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
318 ret);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200319 return ret;
320}
321
Axel Lin34be9242011-12-30 11:18:13 +0800322static int __devexit corgi_remove(struct platform_device *pdev)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200323{
Axel Lin34be9242011-12-30 11:18:13 +0800324 struct snd_soc_card *card = platform_get_drvdata(pdev);
325
326 snd_soc_unregister_card(card);
327 return 0;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200328}
329
Axel Lin34be9242011-12-30 11:18:13 +0800330static struct platform_driver corgi_driver = {
331 .driver = {
332 .name = "corgi-audio",
333 .owner = THIS_MODULE,
334 },
335 .probe = corgi_probe,
336 .remove = __devexit_p(corgi_remove),
337};
338
339module_platform_driver(corgi_driver);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200340
341/* Module information */
342MODULE_AUTHOR("Richard Purdie");
343MODULE_DESCRIPTION("ALSA SoC Corgi");
344MODULE_LICENSE("GPL");
Axel Lin34be9242011-12-30 11:18:13 +0800345MODULE_ALIAS("platform:corgi-audio");