blob: 863367ad89ce6aa1b173914f8c2541903d4f9846 [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
Liam Girdwood022658b2012-02-03 17:43:09 +000048static void corgi_ext_control(struct snd_soc_dapm_context *dapm)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020049{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020050 /* set up jack connection */
51 switch (corgi_jack_func) {
52 case CORGI_HP:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020053 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080054 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
55 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwoodce6120c2010-11-05 15:53:46 +020056 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");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020060 break;
61 case CORGI_MIC:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020062 /* reset = mute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080063 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
64 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +020065 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");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020069 break;
70 case CORGI_LINE:
Eric Miao6168cda2008-09-05 18:15:22 +080071 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
72 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwoodce6120c2010-11-05 15:53:46 +020073 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");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020077 break;
78 case CORGI_HEADSET:
Eric Miao6168cda2008-09-05 18:15:22 +080079 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
80 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwoodce6120c2010-11-05 15:53:46 +020081 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");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020085 break;
86 }
87
88 if (corgi_spk_func == CORGI_SPK_ON)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020089 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
Liam Girdwooda5302182008-07-07 13:35:17 +010090 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020091 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020092
93 /* signal a DAPM event */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020094 snd_soc_dapm_sync(dapm);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020095}
96
97static int corgi_startup(struct snd_pcm_substream *substream)
98{
99 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000100 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200101
Mark Brown71a29562010-11-05 13:50:48 -0400102 mutex_lock(&codec->mutex);
103
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200104 /* check the jack status at stream startup */
Liam Girdwood022658b2012-02-03 17:43:09 +0000105 corgi_ext_control(&codec->dapm);
Mark Brown71a29562010-11-05 13:50:48 -0400106
107 mutex_unlock(&codec->mutex);
108
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200109 return 0;
110}
111
112/* we need to unmute the HP at shutdown as the mute burns power on corgi */
Takashi Iwai5220ed62008-12-01 20:00:47 +0100113static void corgi_shutdown(struct snd_pcm_substream *substream)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200114{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200115 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +0800116 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
117 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200118}
119
Liam Girdwoodd928b252007-02-02 17:22:20 +0100120static int corgi_hw_params(struct snd_pcm_substream *substream,
121 struct snd_pcm_hw_params *params)
122{
123 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000124 struct snd_soc_dai *codec_dai = rtd->codec_dai;
125 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodd928b252007-02-02 17:22:20 +0100126 unsigned int clk = 0;
127 int ret = 0;
128
129 switch (params_rate(params)) {
130 case 8000:
131 case 16000:
132 case 48000:
133 case 96000:
134 clk = 12288000;
135 break;
136 case 11025:
137 case 22050:
138 case 44100:
139 clk = 11289600;
140 break;
141 }
142
Liam Girdwoodd928b252007-02-02 17:22:20 +0100143 /* set the codec system clock for DAC and ADC */
Mark Brown9745e822010-08-17 23:40:24 +0100144 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100145 SND_SOC_CLOCK_IN);
146 if (ret < 0)
147 return ret;
148
149 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100150 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100151 SND_SOC_CLOCK_IN);
152 if (ret < 0)
153 return ret;
154
155 return 0;
156}
157
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200158static struct snd_soc_ops corgi_ops = {
159 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100160 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200161 .shutdown = corgi_shutdown,
162};
163
164static int corgi_get_jack(struct snd_kcontrol *kcontrol,
165 struct snd_ctl_elem_value *ucontrol)
166{
167 ucontrol->value.integer.value[0] = corgi_jack_func;
168 return 0;
169}
170
171static int corgi_set_jack(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *ucontrol)
173{
Liam Girdwood022658b2012-02-03 17:43:09 +0000174 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200175
176 if (corgi_jack_func == ucontrol->value.integer.value[0])
177 return 0;
178
179 corgi_jack_func = ucontrol->value.integer.value[0];
Liam Girdwood022658b2012-02-03 17:43:09 +0000180 corgi_ext_control(&card->dapm);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200181 return 1;
182}
183
184static int corgi_get_spk(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
186{
187 ucontrol->value.integer.value[0] = corgi_spk_func;
188 return 0;
189}
190
191static int corgi_set_spk(struct snd_kcontrol *kcontrol,
192 struct snd_ctl_elem_value *ucontrol)
193{
Liam Girdwood022658b2012-02-03 17:43:09 +0000194 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200195
196 if (corgi_spk_func == ucontrol->value.integer.value[0])
197 return 0;
198
199 corgi_spk_func = ucontrol->value.integer.value[0];
Liam Girdwood022658b2012-02-03 17:43:09 +0000200 corgi_ext_control(&card->dapm);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200201 return 1;
202}
203
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100204static int corgi_amp_event(struct snd_soc_dapm_widget *w,
205 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200206{
Eric Miao6168cda2008-09-05 18:15:22 +0800207 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200208 return 0;
209}
210
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100211static int corgi_mic_event(struct snd_soc_dapm_widget *w,
212 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200213{
Eric Miao6168cda2008-09-05 18:15:22 +0800214 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200215 return 0;
216}
217
218/* corgi machine dapm widgets */
219static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
220SND_SOC_DAPM_HP("Headphone Jack", NULL),
221SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
222SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
223SND_SOC_DAPM_LINE("Line Jack", NULL),
224SND_SOC_DAPM_HP("Headset Jack", NULL),
225};
226
227/* Corgi machine audio map (connections to the codec pins) */
Axel Lin32696af2011-12-30 11:16:32 +0800228static const struct snd_soc_dapm_route corgi_audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200229
230 /* headset Jack - in = micin, out = LHPOUT*/
231 {"Headset Jack", NULL, "LHPOUT"},
232
233 /* headphone connected to LHPOUT1, RHPOUT1 */
234 {"Headphone Jack", NULL, "LHPOUT"},
235 {"Headphone Jack", NULL, "RHPOUT"},
236
237 /* speaker connected to LOUT, ROUT */
238 {"Ext Spk", NULL, "ROUT"},
239 {"Ext Spk", NULL, "LOUT"},
240
241 /* mic is connected to MICIN (via right channel of headphone jack) */
242 {"MICIN", NULL, "Mic Jack"},
243
244 /* Same as the above but no mic bias for line signals */
245 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200246};
247
248static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
249 "Off"};
250static const char *spk_function[] = {"On", "Off"};
251static const struct soc_enum corgi_enum[] = {
252 SOC_ENUM_SINGLE_EXT(5, jack_function),
253 SOC_ENUM_SINGLE_EXT(2, spk_function),
254};
255
256static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
257 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
258 corgi_set_jack),
259 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
260 corgi_set_spk),
261};
262
263/*
264 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
265 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000266static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200267{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000268 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200269 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200270
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271 snd_soc_dapm_nc_pin(dapm, "LLINEIN");
272 snd_soc_dapm_nc_pin(dapm, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200273
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200274 return 0;
275}
276
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200277/* corgi digital audio interface glue - connects codec <--> CPU */
278static struct snd_soc_dai_link corgi_dai = {
279 .name = "WM8731",
280 .stream_name = "WM8731",
Dmitry Eremin-Solenikova3adfa02011-01-21 22:14:17 +0300281 .cpu_dai_name = "pxa2xx-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000282 .codec_dai_name = "wm8731-hifi",
283 .platform_name = "pxa-pcm-audio",
Mark Brown99b59f32011-03-27 14:35:15 +0100284 .codec_name = "wm8731.0-001b",
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200285 .init = corgi_wm8731_init,
Axel Lin74f4dd52011-12-30 11:13:24 +0800286 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
287 SND_SOC_DAIFMT_CBS_CFS,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100288 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200289};
290
291/* corgi audio machine driver */
Axel Lin34be9242011-12-30 11:18:13 +0800292static struct snd_soc_card corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200293 .name = "Corgi",
Axel Lin561c6a12011-12-22 09:44:43 +0800294 .owner = THIS_MODULE,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200295 .dai_link = &corgi_dai,
296 .num_links = 1,
Axel Lin32696af2011-12-30 11:16:32 +0800297
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),
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200304};
305
Axel Lin34be9242011-12-30 11:18:13 +0800306static int __devinit corgi_probe(struct platform_device *pdev)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200307{
Axel Lin34be9242011-12-30 11:18:13 +0800308 struct snd_soc_card *card = &corgi;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200309 int ret;
310
Axel Lin34be9242011-12-30 11:18:13 +0800311 card->dev = &pdev->dev;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200312
Axel Lin34be9242011-12-30 11:18:13 +0800313 ret = snd_soc_register_card(card);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200314 if (ret)
Axel Lin34be9242011-12-30 11:18:13 +0800315 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
316 ret);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200317 return ret;
318}
319
Axel Lin34be9242011-12-30 11:18:13 +0800320static int __devexit corgi_remove(struct platform_device *pdev)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200321{
Axel Lin34be9242011-12-30 11:18:13 +0800322 struct snd_soc_card *card = platform_get_drvdata(pdev);
323
324 snd_soc_unregister_card(card);
325 return 0;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200326}
327
Axel Lin34be9242011-12-30 11:18:13 +0800328static struct platform_driver corgi_driver = {
329 .driver = {
330 .name = "corgi-audio",
331 .owner = THIS_MODULE,
332 },
333 .probe = corgi_probe,
334 .remove = __devexit_p(corgi_remove),
335};
336
337module_platform_driver(corgi_driver);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200338
339/* Module information */
340MODULE_AUTHOR("Richard Purdie");
341MODULE_DESCRIPTION("ALSA SoC Corgi");
342MODULE_LICENSE("GPL");
Axel Lin34be9242011-12-30 11:18:13 +0800343MODULE_ALIAS("platform:corgi-audio");