blob: 5e5004a840738b2d696d7ab62662f5ba904a2355 [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
145 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100146 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100147 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
148 if (ret < 0)
149 return ret;
150
151 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100152 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100153 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
154 if (ret < 0)
155 return ret;
156
157 /* set the codec system clock for DAC and ADC */
Mark Brown9745e822010-08-17 23:40:24 +0100158 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100159 SND_SOC_CLOCK_IN);
160 if (ret < 0)
161 return ret;
162
163 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100164 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100165 SND_SOC_CLOCK_IN);
166 if (ret < 0)
167 return ret;
168
169 return 0;
170}
171
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200172static struct snd_soc_ops corgi_ops = {
173 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100174 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200175 .shutdown = corgi_shutdown,
176};
177
178static int corgi_get_jack(struct snd_kcontrol *kcontrol,
179 struct snd_ctl_elem_value *ucontrol)
180{
181 ucontrol->value.integer.value[0] = corgi_jack_func;
182 return 0;
183}
184
185static int corgi_set_jack(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
187{
188 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
189
190 if (corgi_jack_func == ucontrol->value.integer.value[0])
191 return 0;
192
193 corgi_jack_func = ucontrol->value.integer.value[0];
194 corgi_ext_control(codec);
195 return 1;
196}
197
198static int corgi_get_spk(struct snd_kcontrol *kcontrol,
199 struct snd_ctl_elem_value *ucontrol)
200{
201 ucontrol->value.integer.value[0] = corgi_spk_func;
202 return 0;
203}
204
205static int corgi_set_spk(struct snd_kcontrol *kcontrol,
206 struct snd_ctl_elem_value *ucontrol)
207{
208 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
209
210 if (corgi_spk_func == ucontrol->value.integer.value[0])
211 return 0;
212
213 corgi_spk_func = ucontrol->value.integer.value[0];
214 corgi_ext_control(codec);
215 return 1;
216}
217
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100218static int corgi_amp_event(struct snd_soc_dapm_widget *w,
219 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200220{
Eric Miao6168cda2008-09-05 18:15:22 +0800221 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200222 return 0;
223}
224
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100225static int corgi_mic_event(struct snd_soc_dapm_widget *w,
226 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200227{
Eric Miao6168cda2008-09-05 18:15:22 +0800228 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200229 return 0;
230}
231
232/* corgi machine dapm widgets */
233static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
234SND_SOC_DAPM_HP("Headphone Jack", NULL),
235SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
236SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
237SND_SOC_DAPM_LINE("Line Jack", NULL),
238SND_SOC_DAPM_HP("Headset Jack", NULL),
239};
240
241/* Corgi machine audio map (connections to the codec pins) */
Mark Brown25191c42008-05-13 14:55:48 +0200242static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200243
244 /* headset Jack - in = micin, out = LHPOUT*/
245 {"Headset Jack", NULL, "LHPOUT"},
246
247 /* headphone connected to LHPOUT1, RHPOUT1 */
248 {"Headphone Jack", NULL, "LHPOUT"},
249 {"Headphone Jack", NULL, "RHPOUT"},
250
251 /* speaker connected to LOUT, ROUT */
252 {"Ext Spk", NULL, "ROUT"},
253 {"Ext Spk", NULL, "LOUT"},
254
255 /* mic is connected to MICIN (via right channel of headphone jack) */
256 {"MICIN", NULL, "Mic Jack"},
257
258 /* Same as the above but no mic bias for line signals */
259 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200260};
261
262static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
263 "Off"};
264static const char *spk_function[] = {"On", "Off"};
265static const struct soc_enum corgi_enum[] = {
266 SOC_ENUM_SINGLE_EXT(5, jack_function),
267 SOC_ENUM_SINGLE_EXT(2, spk_function),
268};
269
270static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
271 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
272 corgi_set_jack),
273 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
274 corgi_set_spk),
275};
276
277/*
278 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
279 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200281{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000282 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200283 struct snd_soc_dapm_context *dapm = &codec->dapm;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100284 int err;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200285
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200286 snd_soc_dapm_nc_pin(dapm, "LLINEIN");
287 snd_soc_dapm_nc_pin(dapm, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200288
289 /* Add corgi specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100290 err = snd_soc_add_controls(codec, wm8731_corgi_controls,
291 ARRAY_SIZE(wm8731_corgi_controls));
292 if (err < 0)
293 return err;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200294
295 /* Add corgi specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200296 snd_soc_dapm_new_controls(dapm, wm8731_dapm_widgets,
Mark Brown25191c42008-05-13 14:55:48 +0200297 ARRAY_SIZE(wm8731_dapm_widgets));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200298
299 /* Set up corgi specific audio path audio_map */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200300 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200301
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200302 return 0;
303}
304
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200305/* corgi digital audio interface glue - connects codec <--> CPU */
306static struct snd_soc_dai_link corgi_dai = {
307 .name = "WM8731",
308 .stream_name = "WM8731",
Dmitry Eremin-Solenikova3adfa02011-01-21 22:14:17 +0300309 .cpu_dai_name = "pxa2xx-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000310 .codec_dai_name = "wm8731-hifi",
311 .platform_name = "pxa-pcm-audio",
Mark Brown99b59f32011-03-27 14:35:15 +0100312 .codec_name = "wm8731.0-001b",
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200313 .init = corgi_wm8731_init,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100314 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200315};
316
317/* corgi audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000318static struct snd_soc_card snd_soc_corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200319 .name = "Corgi",
Axel Lin561c6a12011-12-22 09:44:43 +0800320 .owner = THIS_MODULE,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200321 .dai_link = &corgi_dai,
322 .num_links = 1,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200323};
324
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200325static struct platform_device *corgi_snd_device;
326
327static int __init corgi_init(void)
328{
329 int ret;
330
Mark Brown1bfcd362008-04-23 15:12:19 +0200331 if (!(machine_is_corgi() || machine_is_shepherd() ||
332 machine_is_husky()))
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200333 return -ENODEV;
334
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200335 corgi_snd_device = platform_device_alloc("soc-audio", -1);
336 if (!corgi_snd_device)
337 return -ENOMEM;
338
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000339 platform_set_drvdata(corgi_snd_device, &snd_soc_corgi);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200340 ret = platform_device_add(corgi_snd_device);
341
342 if (ret)
343 platform_device_put(corgi_snd_device);
344
345 return ret;
346}
347
348static void __exit corgi_exit(void)
349{
350 platform_device_unregister(corgi_snd_device);
351}
352
353module_init(corgi_init);
354module_exit(corgi_exit);
355
356/* Module information */
357MODULE_AUTHOR("Richard Purdie");
358MODULE_DESCRIPTION("ALSA SoC Corgi");
359MODULE_LICENSE("GPL");