blob: 1ba25a559524296217824f188ac8f4de081a72ec [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>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
Eric Miao6168cda2008-09-05 18:15:22 +080021#include <linux/gpio.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26
27#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/pxa-regs.h>
29#include <mach/hardware.h>
30#include <mach/corgi.h>
31#include <mach/audio.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020032
33#include "../codecs/wm8731.h"
34#include "pxa2xx-pcm.h"
Liam Girdwoodd928b252007-02-02 17:22:20 +010035#include "pxa2xx-i2s.h"
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020036
37#define CORGI_HP 0
38#define CORGI_MIC 1
39#define CORGI_LINE 2
40#define CORGI_HEADSET 3
41#define CORGI_HP_OFF 4
42#define CORGI_SPK_ON 0
43#define CORGI_SPK_OFF 1
44
45 /* audio clock in Hz - rounded from 12.235MHz */
46#define CORGI_AUDIO_CLOCK 12288000
47
48static int corgi_jack_func;
49static int corgi_spk_func;
50
51static void corgi_ext_control(struct snd_soc_codec *codec)
52{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020053 /* set up jack connection */
54 switch (corgi_jack_func) {
55 case CORGI_HP:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020056 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080057 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
58 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010059 snd_soc_dapm_disable_pin(codec, "Mic Jack");
60 snd_soc_dapm_disable_pin(codec, "Line Jack");
61 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
62 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020063 break;
64 case CORGI_MIC:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020065 /* reset = mute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080066 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
67 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010068 snd_soc_dapm_enable_pin(codec, "Mic Jack");
69 snd_soc_dapm_disable_pin(codec, "Line Jack");
70 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
71 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020072 break;
73 case CORGI_LINE:
Eric Miao6168cda2008-09-05 18:15:22 +080074 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
75 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010076 snd_soc_dapm_disable_pin(codec, "Mic Jack");
77 snd_soc_dapm_enable_pin(codec, "Line Jack");
78 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
79 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020080 break;
81 case CORGI_HEADSET:
Eric Miao6168cda2008-09-05 18:15:22 +080082 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
83 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010084 snd_soc_dapm_enable_pin(codec, "Mic Jack");
85 snd_soc_dapm_disable_pin(codec, "Line Jack");
86 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
87 snd_soc_dapm_enable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020088 break;
89 }
90
91 if (corgi_spk_func == CORGI_SPK_ON)
Liam Girdwooda5302182008-07-07 13:35:17 +010092 snd_soc_dapm_enable_pin(codec, "Ext Spk");
93 else
94 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020095
96 /* signal a DAPM event */
Liam Girdwooda5302182008-07-07 13:35:17 +010097 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020098}
99
100static int corgi_startup(struct snd_pcm_substream *substream)
101{
102 struct snd_soc_pcm_runtime *rtd = substream->private_data;
103 struct snd_soc_codec *codec = rtd->socdev->codec;
104
105 /* check the jack status at stream startup */
106 corgi_ext_control(codec);
107 return 0;
108}
109
110/* we need to unmute the HP at shutdown as the mute burns power on corgi */
Takashi Iwai5220ed62008-12-01 20:00:47 +0100111static void corgi_shutdown(struct snd_pcm_substream *substream)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200112{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200113 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +0800114 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
115 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200116}
117
Liam Girdwoodd928b252007-02-02 17:22:20 +0100118static int corgi_hw_params(struct snd_pcm_substream *substream,
119 struct snd_pcm_hw_params *params)
120{
121 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood917f93a2008-07-07 16:08:11 +0100122 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
123 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Liam Girdwoodd928b252007-02-02 17:22:20 +0100124 unsigned int clk = 0;
125 int ret = 0;
126
127 switch (params_rate(params)) {
128 case 8000:
129 case 16000:
130 case 48000:
131 case 96000:
132 clk = 12288000;
133 break;
134 case 11025:
135 case 22050:
136 case 44100:
137 clk = 11289600;
138 break;
139 }
140
141 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100142 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100143 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
144 if (ret < 0)
145 return ret;
146
147 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100148 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100149 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
150 if (ret < 0)
151 return ret;
152
153 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100154 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100155 SND_SOC_CLOCK_IN);
156 if (ret < 0)
157 return ret;
158
159 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100160 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100161 SND_SOC_CLOCK_IN);
162 if (ret < 0)
163 return ret;
164
165 return 0;
166}
167
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200168static struct snd_soc_ops corgi_ops = {
169 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100170 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200171 .shutdown = corgi_shutdown,
172};
173
174static int corgi_get_jack(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
176{
177 ucontrol->value.integer.value[0] = corgi_jack_func;
178 return 0;
179}
180
181static int corgi_set_jack(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
183{
184 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
185
186 if (corgi_jack_func == ucontrol->value.integer.value[0])
187 return 0;
188
189 corgi_jack_func = ucontrol->value.integer.value[0];
190 corgi_ext_control(codec);
191 return 1;
192}
193
194static int corgi_get_spk(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
196{
197 ucontrol->value.integer.value[0] = corgi_spk_func;
198 return 0;
199}
200
201static int corgi_set_spk(struct snd_kcontrol *kcontrol,
202 struct snd_ctl_elem_value *ucontrol)
203{
204 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
205
206 if (corgi_spk_func == ucontrol->value.integer.value[0])
207 return 0;
208
209 corgi_spk_func = ucontrol->value.integer.value[0];
210 corgi_ext_control(codec);
211 return 1;
212}
213
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100214static int corgi_amp_event(struct snd_soc_dapm_widget *w,
215 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200216{
Eric Miao6168cda2008-09-05 18:15:22 +0800217 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200218 return 0;
219}
220
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100221static int corgi_mic_event(struct snd_soc_dapm_widget *w,
222 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200223{
Eric Miao6168cda2008-09-05 18:15:22 +0800224 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200225 return 0;
226}
227
228/* corgi machine dapm widgets */
229static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
230SND_SOC_DAPM_HP("Headphone Jack", NULL),
231SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
232SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
233SND_SOC_DAPM_LINE("Line Jack", NULL),
234SND_SOC_DAPM_HP("Headset Jack", NULL),
235};
236
237/* Corgi machine audio map (connections to the codec pins) */
Mark Brown25191c42008-05-13 14:55:48 +0200238static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200239
240 /* headset Jack - in = micin, out = LHPOUT*/
241 {"Headset Jack", NULL, "LHPOUT"},
242
243 /* headphone connected to LHPOUT1, RHPOUT1 */
244 {"Headphone Jack", NULL, "LHPOUT"},
245 {"Headphone Jack", NULL, "RHPOUT"},
246
247 /* speaker connected to LOUT, ROUT */
248 {"Ext Spk", NULL, "ROUT"},
249 {"Ext Spk", NULL, "LOUT"},
250
251 /* mic is connected to MICIN (via right channel of headphone jack) */
252 {"MICIN", NULL, "Mic Jack"},
253
254 /* Same as the above but no mic bias for line signals */
255 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200256};
257
258static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
259 "Off"};
260static const char *spk_function[] = {"On", "Off"};
261static const struct soc_enum corgi_enum[] = {
262 SOC_ENUM_SINGLE_EXT(5, jack_function),
263 SOC_ENUM_SINGLE_EXT(2, spk_function),
264};
265
266static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
267 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
268 corgi_set_jack),
269 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
270 corgi_set_spk),
271};
272
273/*
274 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
275 */
276static int corgi_wm8731_init(struct snd_soc_codec *codec)
277{
278 int i, err;
279
Mark Brown35f5e542008-09-27 10:45:09 +0100280 snd_soc_dapm_nc_pin(codec, "LLINEIN");
281 snd_soc_dapm_nc_pin(codec, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200282
283 /* Add corgi specific controls */
284 for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
285 err = snd_ctl_add(codec->card,
Mark Brown1bfcd362008-04-23 15:12:19 +0200286 snd_soc_cnew(&wm8731_corgi_controls[i], codec, NULL));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200287 if (err < 0)
288 return err;
289 }
290
291 /* Add corgi specific widgets */
Mark Brown25191c42008-05-13 14:55:48 +0200292 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
293 ARRAY_SIZE(wm8731_dapm_widgets));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200294
295 /* Set up corgi specific audio path audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200296 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200297
Liam Girdwooda5302182008-07-07 13:35:17 +0100298 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200299 return 0;
300}
301
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200302/* corgi digital audio interface glue - connects codec <--> CPU */
303static struct snd_soc_dai_link corgi_dai = {
304 .name = "WM8731",
305 .stream_name = "WM8731",
306 .cpu_dai = &pxa_i2s_dai,
307 .codec_dai = &wm8731_dai,
308 .init = corgi_wm8731_init,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100309 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200310};
311
312/* corgi audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000313static struct snd_soc_card snd_soc_corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200314 .name = "Corgi",
Mark Brown87689d52008-12-02 16:01:14 +0000315 .platform = &pxa2xx_soc_platform,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200316 .dai_link = &corgi_dai,
317 .num_links = 1,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200318};
319
320/* corgi audio private data */
321static struct wm8731_setup_data corgi_wm8731_setup = {
Jean Delvare81297c82008-09-01 18:47:01 +0100322 .i2c_bus = 0,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200323 .i2c_address = 0x1b,
324};
325
326/* corgi audio subsystem */
327static struct snd_soc_device corgi_snd_devdata = {
Mark Brown87506542008-11-18 20:50:34 +0000328 .card = &snd_soc_corgi,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200329 .codec_dev = &soc_codec_dev_wm8731,
330 .codec_data = &corgi_wm8731_setup,
331};
332
333static struct platform_device *corgi_snd_device;
334
335static int __init corgi_init(void)
336{
337 int ret;
338
Mark Brown1bfcd362008-04-23 15:12:19 +0200339 if (!(machine_is_corgi() || machine_is_shepherd() ||
340 machine_is_husky()))
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200341 return -ENODEV;
342
343 corgi_snd_device = platform_device_alloc("soc-audio", -1);
344 if (!corgi_snd_device)
345 return -ENOMEM;
346
347 platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
348 corgi_snd_devdata.dev = &corgi_snd_device->dev;
349 ret = platform_device_add(corgi_snd_device);
350
351 if (ret)
352 platform_device_put(corgi_snd_device);
353
354 return ret;
355}
356
357static void __exit corgi_exit(void)
358{
359 platform_device_unregister(corgi_snd_device);
360}
361
362module_init(corgi_init);
363module_exit(corgi_exit);
364
365/* Module information */
366MODULE_AUTHOR("Richard Purdie");
367MODULE_DESCRIPTION("ALSA SoC Corgi");
368MODULE_LICENSE("GPL");