blob: 146973ae0974d31ed46fd52d4174b93909196277 [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/pxa-regs.h>
30#include <mach/hardware.h>
31#include <mach/corgi.h>
32#include <mach/audio.h>
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020033
34#include "../codecs/wm8731.h"
35#include "pxa2xx-pcm.h"
Liam Girdwoodd928b252007-02-02 17:22:20 +010036#include "pxa2xx-i2s.h"
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020037
38#define CORGI_HP 0
39#define CORGI_MIC 1
40#define CORGI_LINE 2
41#define CORGI_HEADSET 3
42#define CORGI_HP_OFF 4
43#define CORGI_SPK_ON 0
44#define CORGI_SPK_OFF 1
45
46 /* audio clock in Hz - rounded from 12.235MHz */
47#define CORGI_AUDIO_CLOCK 12288000
48
49static int corgi_jack_func;
50static int corgi_spk_func;
51
52static void corgi_ext_control(struct snd_soc_codec *codec)
53{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020054 /* set up jack connection */
55 switch (corgi_jack_func) {
56 case CORGI_HP:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020057 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080058 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
59 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010060 snd_soc_dapm_disable_pin(codec, "Mic Jack");
61 snd_soc_dapm_disable_pin(codec, "Line Jack");
62 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
63 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020064 break;
65 case CORGI_MIC:
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020066 /* reset = mute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +080067 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
68 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010069 snd_soc_dapm_enable_pin(codec, "Mic Jack");
70 snd_soc_dapm_disable_pin(codec, "Line Jack");
71 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
72 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020073 break;
74 case CORGI_LINE:
Eric Miao6168cda2008-09-05 18:15:22 +080075 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
76 gpio_set_value(CORGI_GPIO_MUTE_R, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +010077 snd_soc_dapm_disable_pin(codec, "Mic Jack");
78 snd_soc_dapm_enable_pin(codec, "Line Jack");
79 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
80 snd_soc_dapm_disable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020081 break;
82 case CORGI_HEADSET:
Eric Miao6168cda2008-09-05 18:15:22 +080083 gpio_set_value(CORGI_GPIO_MUTE_L, 0);
84 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda5302182008-07-07 13:35:17 +010085 snd_soc_dapm_enable_pin(codec, "Mic Jack");
86 snd_soc_dapm_disable_pin(codec, "Line Jack");
87 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
88 snd_soc_dapm_enable_pin(codec, "Headset Jack");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020089 break;
90 }
91
92 if (corgi_spk_func == CORGI_SPK_ON)
Liam Girdwooda5302182008-07-07 13:35:17 +010093 snd_soc_dapm_enable_pin(codec, "Ext Spk");
94 else
95 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020096
97 /* signal a DAPM event */
Liam Girdwooda5302182008-07-07 13:35:17 +010098 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +020099}
100
101static int corgi_startup(struct snd_pcm_substream *substream)
102{
103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwai9e30d772009-02-11 08:28:04 +0100104 struct snd_soc_codec *codec = rtd->socdev->card->codec;
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200105
106 /* check the jack status at stream startup */
107 corgi_ext_control(codec);
108 return 0;
109}
110
111/* we need to unmute the HP at shutdown as the mute burns power on corgi */
Takashi Iwai5220ed62008-12-01 20:00:47 +0100112static void corgi_shutdown(struct snd_pcm_substream *substream)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200113{
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200114 /* set = unmute headphone */
Eric Miao6168cda2008-09-05 18:15:22 +0800115 gpio_set_value(CORGI_GPIO_MUTE_L, 1);
116 gpio_set_value(CORGI_GPIO_MUTE_R, 1);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200117}
118
Liam Girdwoodd928b252007-02-02 17:22:20 +0100119static int corgi_hw_params(struct snd_pcm_substream *substream,
120 struct snd_pcm_hw_params *params)
121{
122 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood917f93a2008-07-07 16:08:11 +0100123 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
124 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Liam Girdwoodd928b252007-02-02 17:22:20 +0100125 unsigned int clk = 0;
126 int ret = 0;
127
128 switch (params_rate(params)) {
129 case 8000:
130 case 16000:
131 case 48000:
132 case 96000:
133 clk = 12288000;
134 break;
135 case 11025:
136 case 22050:
137 case 44100:
138 clk = 11289600;
139 break;
140 }
141
142 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100143 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100144 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
145 if (ret < 0)
146 return ret;
147
148 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100149 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwoodd928b252007-02-02 17:22:20 +0100150 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
151 if (ret < 0)
152 return ret;
153
154 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100155 ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK, clk,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100156 SND_SOC_CLOCK_IN);
157 if (ret < 0)
158 return ret;
159
160 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100161 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100162 SND_SOC_CLOCK_IN);
163 if (ret < 0)
164 return ret;
165
166 return 0;
167}
168
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200169static struct snd_soc_ops corgi_ops = {
170 .startup = corgi_startup,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100171 .hw_params = corgi_hw_params,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200172 .shutdown = corgi_shutdown,
173};
174
175static int corgi_get_jack(struct snd_kcontrol *kcontrol,
176 struct snd_ctl_elem_value *ucontrol)
177{
178 ucontrol->value.integer.value[0] = corgi_jack_func;
179 return 0;
180}
181
182static int corgi_set_jack(struct snd_kcontrol *kcontrol,
183 struct snd_ctl_elem_value *ucontrol)
184{
185 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
186
187 if (corgi_jack_func == ucontrol->value.integer.value[0])
188 return 0;
189
190 corgi_jack_func = ucontrol->value.integer.value[0];
191 corgi_ext_control(codec);
192 return 1;
193}
194
195static int corgi_get_spk(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
197{
198 ucontrol->value.integer.value[0] = corgi_spk_func;
199 return 0;
200}
201
202static int corgi_set_spk(struct snd_kcontrol *kcontrol,
203 struct snd_ctl_elem_value *ucontrol)
204{
205 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
206
207 if (corgi_spk_func == ucontrol->value.integer.value[0])
208 return 0;
209
210 corgi_spk_func = ucontrol->value.integer.value[0];
211 corgi_ext_control(codec);
212 return 1;
213}
214
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100215static int corgi_amp_event(struct snd_soc_dapm_widget *w,
216 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200217{
Eric Miao6168cda2008-09-05 18:15:22 +0800218 gpio_set_value(CORGI_GPIO_APM_ON, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200219 return 0;
220}
221
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100222static int corgi_mic_event(struct snd_soc_dapm_widget *w,
223 struct snd_kcontrol *k, int event)
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200224{
Eric Miao6168cda2008-09-05 18:15:22 +0800225 gpio_set_value(CORGI_GPIO_MIC_BIAS, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200226 return 0;
227}
228
229/* corgi machine dapm widgets */
230static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
231SND_SOC_DAPM_HP("Headphone Jack", NULL),
232SND_SOC_DAPM_MIC("Mic Jack", corgi_mic_event),
233SND_SOC_DAPM_SPK("Ext Spk", corgi_amp_event),
234SND_SOC_DAPM_LINE("Line Jack", NULL),
235SND_SOC_DAPM_HP("Headset Jack", NULL),
236};
237
238/* Corgi machine audio map (connections to the codec pins) */
Mark Brown25191c42008-05-13 14:55:48 +0200239static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200240
241 /* headset Jack - in = micin, out = LHPOUT*/
242 {"Headset Jack", NULL, "LHPOUT"},
243
244 /* headphone connected to LHPOUT1, RHPOUT1 */
245 {"Headphone Jack", NULL, "LHPOUT"},
246 {"Headphone Jack", NULL, "RHPOUT"},
247
248 /* speaker connected to LOUT, ROUT */
249 {"Ext Spk", NULL, "ROUT"},
250 {"Ext Spk", NULL, "LOUT"},
251
252 /* mic is connected to MICIN (via right channel of headphone jack) */
253 {"MICIN", NULL, "Mic Jack"},
254
255 /* Same as the above but no mic bias for line signals */
256 {"MICIN", NULL, "Line Jack"},
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200257};
258
259static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
260 "Off"};
261static const char *spk_function[] = {"On", "Off"};
262static const struct soc_enum corgi_enum[] = {
263 SOC_ENUM_SINGLE_EXT(5, jack_function),
264 SOC_ENUM_SINGLE_EXT(2, spk_function),
265};
266
267static const struct snd_kcontrol_new wm8731_corgi_controls[] = {
268 SOC_ENUM_EXT("Jack Function", corgi_enum[0], corgi_get_jack,
269 corgi_set_jack),
270 SOC_ENUM_EXT("Speaker Function", corgi_enum[1], corgi_get_spk,
271 corgi_set_spk),
272};
273
274/*
275 * Logic for a wm8731 as connected on a Sharp SL-C7x0 Device
276 */
277static int corgi_wm8731_init(struct snd_soc_codec *codec)
278{
279 int i, err;
280
Mark Brown35f5e542008-09-27 10:45:09 +0100281 snd_soc_dapm_nc_pin(codec, "LLINEIN");
282 snd_soc_dapm_nc_pin(codec, "RLINEIN");
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200283
284 /* Add corgi specific controls */
285 for (i = 0; i < ARRAY_SIZE(wm8731_corgi_controls); i++) {
286 err = snd_ctl_add(codec->card,
Mark Brown1bfcd362008-04-23 15:12:19 +0200287 snd_soc_cnew(&wm8731_corgi_controls[i], codec, NULL));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200288 if (err < 0)
289 return err;
290 }
291
292 /* Add corgi specific widgets */
Mark Brown25191c42008-05-13 14:55:48 +0200293 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
294 ARRAY_SIZE(wm8731_dapm_widgets));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200295
296 /* Set up corgi specific audio path audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200297 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200298
Liam Girdwooda5302182008-07-07 13:35:17 +0100299 snd_soc_dapm_sync(codec);
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200300 return 0;
301}
302
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200303/* corgi digital audio interface glue - connects codec <--> CPU */
304static struct snd_soc_dai_link corgi_dai = {
305 .name = "WM8731",
306 .stream_name = "WM8731",
307 .cpu_dai = &pxa_i2s_dai,
308 .codec_dai = &wm8731_dai,
309 .init = corgi_wm8731_init,
Liam Girdwoodd928b252007-02-02 17:22:20 +0100310 .ops = &corgi_ops,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200311};
312
313/* corgi audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000314static struct snd_soc_card snd_soc_corgi = {
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200315 .name = "Corgi",
Mark Brown87689d52008-12-02 16:01:14 +0000316 .platform = &pxa2xx_soc_platform,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200317 .dai_link = &corgi_dai,
318 .num_links = 1,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200319};
320
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200321/* corgi audio subsystem */
322static struct snd_soc_device corgi_snd_devdata = {
Mark Brown87506542008-11-18 20:50:34 +0000323 .card = &snd_soc_corgi,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200324 .codec_dev = &soc_codec_dev_wm8731,
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200325};
326
Mark Brown59981022009-02-16 20:49:16 +0000327/*
328 * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
329 * New drivers should register the wm8731 I2C device in the machine
330 * setup code (under arch/arm for ARM systems).
331 */
332static int wm8731_i2c_register(void)
333{
334 struct i2c_board_info info;
335 struct i2c_adapter *adapter;
336 struct i2c_client *client;
337
338 memset(&info, 0, sizeof(struct i2c_board_info));
339 info.addr = 0x1b;
340 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
341
342 adapter = i2c_get_adapter(0);
343 if (!adapter) {
344 printk(KERN_ERR "can't get i2c adapter 0\n");
345 return -ENODEV;
346 }
347
348 client = i2c_new_device(adapter, &info);
349 i2c_put_adapter(adapter);
350 if (!client) {
351 printk(KERN_ERR "can't add i2c device at 0x%x\n",
352 (unsigned int)info.addr);
353 return -ENODEV;
354 }
355
356 return 0;
357}
358
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200359static struct platform_device *corgi_snd_device;
360
361static int __init corgi_init(void)
362{
363 int ret;
364
Mark Brown1bfcd362008-04-23 15:12:19 +0200365 if (!(machine_is_corgi() || machine_is_shepherd() ||
366 machine_is_husky()))
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200367 return -ENODEV;
368
Mark Brownfc996752009-02-18 12:34:53 +0000369 ret = wm8731_i2c_register();
Mark Brown59981022009-02-16 20:49:16 +0000370 if (ret != 0)
371 return ret;
372
Liam Girdwooda1eb4b32006-10-12 14:31:16 +0200373 corgi_snd_device = platform_device_alloc("soc-audio", -1);
374 if (!corgi_snd_device)
375 return -ENOMEM;
376
377 platform_set_drvdata(corgi_snd_device, &corgi_snd_devdata);
378 corgi_snd_devdata.dev = &corgi_snd_device->dev;
379 ret = platform_device_add(corgi_snd_device);
380
381 if (ret)
382 platform_device_put(corgi_snd_device);
383
384 return ret;
385}
386
387static void __exit corgi_exit(void)
388{
389 platform_device_unregister(corgi_snd_device);
390}
391
392module_init(corgi_init);
393module_exit(corgi_exit);
394
395/* Module information */
396MODULE_AUTHOR("Richard Purdie");
397MODULE_DESCRIPTION("ALSA SoC Corgi");
398MODULE_LICENSE("GPL");