blob: f451acd4935b81db8d04d67da8e99c4e650f4a5d [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"
Liam Girdwoodd928b252007-02-02 17:22:20 +010033#include "pxa2xx-i2s.h"
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020034
35#define CORGI_HP 0
36#define CORGI_MIC 1
37#define CORGI_LINE 2
38#define CORGI_HEADSET 3
39#define CORGI_HP_OFF 4
40#define CORGI_SPK_ON 0
41#define CORGI_SPK_OFF 1
42
43 /* audio clock in Hz - rounded from 12.235MHz */
44#define CORGI_AUDIO_CLOCK 12288000
45
46static int corgi_jack_func;
47static int corgi_spk_func;
48
49static void corgi_ext_control(struct snd_soc_codec *codec)
50{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020051 /* set up jack connection */
52 switch (corgi_jack_func) {
53 case CORGI_HP:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020054 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080055 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
56 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010057 snd_soc_dapm_disable_pin(codec, "Mic Jack");
58 snd_soc_dapm_disable_pin(codec, "Line Jack");
59 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
60 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020061 break;
62 case CORGI_MIC:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020063 /* reset = mute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080064 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
65 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010066 snd_soc_dapm_enable_pin(codec, "Mic Jack");
67 snd_soc_dapm_disable_pin(codec, "Line Jack");
68 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
69 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020070 break;
71 case CORGI_LINE:
Eric Miao6168cda2008-09-05 18:15:22 +080072 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
73 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010074 snd_soc_dapm_disable_pin(codec, "Mic Jack");
75 snd_soc_dapm_enable_pin(codec, "Line Jack");
76 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
77 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020078 break;
79 case CORGI_HEADSET:
Eric Miao6168cda2008-09-05 18:15:22 +080080 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
81 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010082 snd_soc_dapm_enable_pin(codec, "Mic Jack");
83 snd_soc_dapm_disable_pin(codec, "Line Jack");
84 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
85 snd_soc_dapm_enable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020086 break;
87 }
88
89 if (corgi_spk_func == CORGI_SPK_ON)
Liam Girdwooda5302182008-07-07 13:35:17 +010090 snd_soc_dapm_enable_pin(codec, "Ext Spk");
91 else
92 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020093
94 /* signal a DAPM event */
Liam Girdwooda5302182008-07-07 13:35:17 +010095 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020096}
97
98static int corgi_startup(struct snd_pcm_substream *substream)
99{
100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000101 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200102
Mark Brown71a29562010-11-05 13:50:48 -0400103 mutex_lock(&codec->mutex);
104
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200105 /* check the jack status at stream startup */
106 corgi_ext_control(codec);
Mark Brown71a29562010-11-05 13:50:48 -0400107
108 mutex_unlock(&codec->mutex);
109
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200110 return 0;
111}
112
113/* we need to unmute the HP at shutdown as the mute burns power on corgi */
Takashi Iwai5220ed62008-12-01 20:00:47 +0100114static void corgi_shutdown(struct snd_pcm_substream *substream)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200115{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200116 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +0800117 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
118 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200119}
120
Liam Girdwoodd928b252007-02-02 17:22:20 +0100121static int corgi_hw_params(struct snd_pcm_substream *substream,
122 struct snd_pcm_hw_params *params)
123{
124 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000125 struct snd_soc_dai *codec_dai = rtd->codec_dai;
126 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodd928b252007-02-02 17:22:20 +0100127 unsigned int clk = 0;
128 int ret = 0;
129
130 switch (params_rate(params)) {
131 case 8000:
132 case 16000:
133 case 48000:
134 case 96000:
135 clk = 12288000;
136 break;
137 case 11025:
138 case 22050:
139 case 44100:
140 clk = 11289600;
141 break;
142 }
143
144 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100145 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100146 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
147 if (ret < 0)
148 return ret;
149
150 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100151 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100152 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
153 if (ret < 0)
154 return ret;
155
156 /* set the codec system clock for DAC and ADC */
Mark Brown9745e822010-08-17 23:40:24 +0100157 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100158 SND_SOC_CLOCK_IN);
159 if (ret < 0)
160 return ret;
161
162 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100163 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100164 SND_SOC_CLOCK_IN);
165 if (ret < 0)
166 return ret;
167
168 return 0;
169}
170
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200171static struct snd_soc_ops corgi_ops = {
172 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100173 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200174 .shutdown = corgi_shutdown,
175};
176
177static int corgi_get_jack(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_value *ucontrol)
179{
180 ucontrol->value.integer.value[0] = corgi_jack_func;
181 return 0;
182}
183
184static int corgi_set_jack(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
186{
187 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
188
189 if (corgi_jack_func == ucontrol->value.integer.value[0])
190 return 0;
191
192 corgi_jack_func = ucontrol->value.integer.value[0];
193 corgi_ext_control(codec);
194 return 1;
195}
196
197static int corgi_get_spk(struct snd_kcontrol *kcontrol,
198 struct snd_ctl_elem_value *ucontrol)
199{
200 ucontrol->value.integer.value[0] = corgi_spk_func;
201 return 0;
202}
203
204static int corgi_set_spk(struct snd_kcontrol *kcontrol,
205 struct snd_ctl_elem_value *ucontrol)
206{
207 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
208
209 if (corgi_spk_func == ucontrol->value.integer.value[0])
210 return 0;
211
212 corgi_spk_func = ucontrol->value.integer.value[0];
213 corgi_ext_control(codec);
214 return 1;
215}
216
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100217static int corgi_amp_event(struct snd_soc_dapm_widget *w,
218 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200219{
Eric Miao6168cda2008-09-05 18:15:22 +0800220 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200221 return 0;
222}
223
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100224static int corgi_mic_event(struct snd_soc_dapm_widget *w,
225 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200226{
Eric Miao6168cda2008-09-05 18:15:22 +0800227 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200228 return 0;
229}
230
231/* corgi machine dapm widgets */
232static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
233SND_SOC_DAPM_HP("Headphone Jack", NULL),
234SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
235SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
236SND_SOC_DAPM_LINE("Line Jack", NULL),
237SND_SOC_DAPM_HP("Headset Jack", NULL),
238};
239
240/* Corgi machine audio map (connections to the codec pins) */
Mark Brown25191c42008-05-13 14:55:48 +0200241static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200242
243 /* headset Jack - in = micin, out = LHPOUT*/
244 {"Headset Jack", NULL, "LHPOUT"},
245
246 /* headphone connected to LHPOUT1, RHPOUT1 */
247 {"Headphone Jack", NULL, "LHPOUT"},
248 {"Headphone Jack", NULL, "RHPOUT"},
249
250 /* speaker connected to LOUT, ROUT */
251 {"Ext Spk", NULL, "ROUT"},
252 {"Ext Spk", NULL, "LOUT"},
253
254 /* mic is connected to MICIN (via right channel of headphone jack) */
255 {"MICIN", NULL, "Mic Jack"},
256
257 /* Same as the above but no mic bias for line signals */
258 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200259};
260
261static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
262 "Off"};
263static const char *spk_function[] = {"On", "Off"};
264static const struct soc_enum corgi_enum[] = {
265 SOC_ENUM_SINGLE_EXT(5, jack_function),
266 SOC_ENUM_SINGLE_EXT(2, spk_function),
267};
268
269static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
270 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
271 corgi_set_jack),
272 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
273 corgi_set_spk),
274};
275
276/*
277 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
278 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000279static int corgi_wm8731_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200280{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 struct snd_soc_codec *codec = rtd->codec;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100282 int err;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200283
Mark Brown35f5e542008-09-27 10:45:09 +0100284 snd_soc_dapm_nc_pin(codec, "LLINEIN");
285 snd_soc_dapm_nc_pin(codec, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200286
287 /* Add corgi specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100288 err = snd_soc_add_controls(codec, wm8731_corgi_controls,
289 ARRAY_SIZE(wm8731_corgi_controls));
290 if (err < 0)
291 return err;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200292
293 /* Add corgi specific widgets */
Mark Brown25191c42008-05-13 14:55:48 +0200294 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
295 ARRAY_SIZE(wm8731_dapm_widgets));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200296
297 /* Set up corgi specific audio path audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200298 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200299
Liam Girdwooda5302182008-07-07 13:35:17 +0100300 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200301 return 0;
302}
303
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200304/* corgi digital audio interface glue - connects codec <--> CPU */
305static struct snd_soc_dai_link corgi_dai = {
306 .name = "WM8731",
307 .stream_name = "WM8731",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000308 .cpu_dai_name = "pxa-is2-dai",
309 .codec_dai_name = "wm8731-hifi",
310 .platform_name = "pxa-pcm-audio",
311 .codec_name = "wm8731-codec-0.001a",
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200312 .init = corgi_wm8731_init,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100313 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200314};
315
316/* corgi audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000317static struct snd_soc_card snd_soc_corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200318 .name = "Corgi",
319 .dai_link = &corgi_dai,
320 .num_links = 1,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200321};
322
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200323static 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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000337 platform_set_drvdata(corgi_snd_device, &snd_soc_corgi);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200338 ret = platform_device_add(corgi_snd_device);
339
340 if (ret)
341 platform_device_put(corgi_snd_device);
342
343 return ret;
344}
345
346static void __exit corgi_exit(void)
347{
348 platform_device_unregister(corgi_snd_device);
349}
350
351module_init(corgi_init);
352module_exit(corgi_exit);
353
354/* Module information */
355MODULE_AUTHOR("Richard Purdie");
356MODULE_DESCRIPTION("ALSA SoC Corgi");
357MODULE_LICENSE("GPL");