blob: d307b6757e9550646b565a4cffaac5abc0a02007 [file] [log] [blame]
Liam Girdwood7fb290d2006-10-12 14:32:13 +02001/*
2 * spitz.c -- SoC audio for Sharp SL-Cxx00 models Spitz, Borzoi and Akita
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 Girdwood7fb290d2006-10-12 14:32:13 +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.
14 *
Liam Girdwood7fb290d2006-10-12 14:32:13 +020015 */
16
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/timer.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
Eric Miaofff14722008-09-05 22:15:23 +080022#include <linux/gpio.h>
Liam Girdwood7fb290d2006-10-12 14:32:13 +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>
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/spitz.h>
Liam Girdwood7fb290d2006-10-12 14:32:13 +020032#include "../codecs/wm8750.h"
33#include "pxa2xx-pcm.h"
Liam Girdwood97952f62007-02-02 17:22:46 +010034#include "pxa2xx-i2s.h"
Liam Girdwood7fb290d2006-10-12 14:32:13 +020035
36#define SPITZ_HP 0
37#define SPITZ_MIC 1
38#define SPITZ_LINE 2
39#define SPITZ_HEADSET 3
40#define SPITZ_HP_OFF 4
41#define SPITZ_SPK_ON 0
42#define SPITZ_SPK_OFF 1
43
44 /* audio clock in Hz - rounded from 12.235MHz */
45#define SPITZ_AUDIO_CLOCK 12288000
46
47static int spitz_jack_func;
48static int spitz_spk_func;
49
50static void spitz_ext_control(struct snd_soc_codec *codec)
51{
52 if (spitz_spk_func == SPITZ_SPK_ON)
Liam Girdwooda5302182008-07-07 13:35:17 +010053 snd_soc_dapm_enable_pin(codec, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020054 else
Liam Girdwooda5302182008-07-07 13:35:17 +010055 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020056
57 /* set up jack connection */
58 switch (spitz_jack_func) {
59 case SPITZ_HP:
60 /* enable and unmute hp jack, disable mic bias */
Liam Girdwooda5302182008-07-07 13:35:17 +010061 snd_soc_dapm_disable_pin(codec, "Headset Jack");
62 snd_soc_dapm_disable_pin(codec, "Mic Jack");
63 snd_soc_dapm_disable_pin(codec, "Line Jack");
64 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
Eric Miaofff14722008-09-05 22:15:23 +080065 gpio_set_value(SPITZ_GPIO_MUTE_L, 1);
66 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020067 break;
68 case SPITZ_MIC:
69 /* enable mic jack and bias, mute hp */
Liam Girdwooda5302182008-07-07 13:35:17 +010070 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
71 snd_soc_dapm_disable_pin(codec, "Headset Jack");
72 snd_soc_dapm_disable_pin(codec, "Line Jack");
73 snd_soc_dapm_enable_pin(codec, "Mic Jack");
Eric Miaofff14722008-09-05 22:15:23 +080074 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
75 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020076 break;
77 case SPITZ_LINE:
78 /* enable line jack, disable mic bias and mute hp */
Liam Girdwooda5302182008-07-07 13:35:17 +010079 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
80 snd_soc_dapm_disable_pin(codec, "Headset Jack");
81 snd_soc_dapm_disable_pin(codec, "Mic Jack");
82 snd_soc_dapm_enable_pin(codec, "Line Jack");
Eric Miaofff14722008-09-05 22:15:23 +080083 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
84 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020085 break;
86 case SPITZ_HEADSET:
87 /* enable and unmute headset jack enable mic bias, mute L hp */
Liam Girdwooda5302182008-07-07 13:35:17 +010088 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
89 snd_soc_dapm_enable_pin(codec, "Mic Jack");
90 snd_soc_dapm_disable_pin(codec, "Line Jack");
91 snd_soc_dapm_enable_pin(codec, "Headset Jack");
Eric Miaofff14722008-09-05 22:15:23 +080092 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
93 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020094 break;
95 case SPITZ_HP_OFF:
96
97 /* jack removed, everything off */
Liam Girdwooda5302182008-07-07 13:35:17 +010098 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
99 snd_soc_dapm_disable_pin(codec, "Headset Jack");
100 snd_soc_dapm_disable_pin(codec, "Mic Jack");
101 snd_soc_dapm_disable_pin(codec, "Line Jack");
Eric Miaofff14722008-09-05 22:15:23 +0800102 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
103 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200104 break;
105 }
Liam Girdwooda5302182008-07-07 13:35:17 +0100106 snd_soc_dapm_sync(codec);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200107}
108
109static int spitz_startup(struct snd_pcm_substream *substream)
110{
111 struct snd_soc_pcm_runtime *rtd = substream->private_data;
112 struct snd_soc_codec *codec = rtd->socdev->codec;
113
114 /* check the jack status at stream startup */
115 spitz_ext_control(codec);
116 return 0;
117}
118
Liam Girdwood97952f62007-02-02 17:22:46 +0100119static int spitz_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 Girdwood97952f62007-02-02 17:22:46 +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 Girdwood97952f62007-02-02 17:22:46 +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 Girdwood97952f62007-02-02 17:22:46 +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, WM8750_SYSCLK, clk,
Liam Girdwood97952f62007-02-02 17:22:46 +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 Girdwood97952f62007-02-02 17:22:46 +0100162 SND_SOC_CLOCK_IN);
163 if (ret < 0)
164 return ret;
165
166 return 0;
167}
168
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200169static struct snd_soc_ops spitz_ops = {
170 .startup = spitz_startup,
Liam Girdwood97952f62007-02-02 17:22:46 +0100171 .hw_params = spitz_hw_params,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200172};
173
174static int spitz_get_jack(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
176{
177 ucontrol->value.integer.value[0] = spitz_jack_func;
178 return 0;
179}
180
181static int spitz_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 (spitz_jack_func == ucontrol->value.integer.value[0])
187 return 0;
188
189 spitz_jack_func = ucontrol->value.integer.value[0];
190 spitz_ext_control(codec);
191 return 1;
192}
193
194static int spitz_get_spk(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
196{
197 ucontrol->value.integer.value[0] = spitz_spk_func;
198 return 0;
199}
200
201static int spitz_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 (spitz_spk_func == ucontrol->value.integer.value[0])
207 return 0;
208
209 spitz_spk_func = ucontrol->value.integer.value[0];
210 spitz_ext_control(codec);
211 return 1;
212}
213
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100214static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
215 struct snd_kcontrol *k, int event)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200216{
Eric Miaofff14722008-09-05 22:15:23 +0800217 if (machine_is_borzoi() || machine_is_spitz())
218 gpio_set_value(SPITZ_GPIO_MIC_BIAS,
219 SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200220
Eric Miaof72de662008-09-06 08:46:23 +0800221 if (machine_is_akita())
222 gpio_set_value(AKITA_GPIO_MIC_BIAS,
223 SND_SOC_DAPM_EVENT_ON(event));
224
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200225 return 0;
226}
227
228/* spitz machine dapm widgets */
229static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
230 SND_SOC_DAPM_HP("Headphone Jack", NULL),
231 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
232 SND_SOC_DAPM_SPK("Ext Spk", NULL),
233 SND_SOC_DAPM_LINE("Line Jack", NULL),
234
235 /* headset is a mic and mono headphone */
236 SND_SOC_DAPM_HP("Headset Jack", NULL),
237};
238
239/* Spitz machine audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200240static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200241
242 /* headphone connected to LOUT1, ROUT1 */
243 {"Headphone Jack", NULL, "LOUT1"},
244 {"Headphone Jack", NULL, "ROUT1"},
245
246 /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
247 {"Headset Jack", NULL, "ROUT1"},
248
249 /* ext speaker connected to LOUT2, ROUT2 */
250 {"Ext Spk", NULL , "ROUT2"},
251 {"Ext Spk", NULL , "LOUT2"},
252
253 /* mic is connected to input 1 - with bias */
254 {"LINPUT1", NULL, "Mic Bias"},
255 {"Mic Bias", NULL, "Mic Jack"},
256
257 /* line is connected to input 1 - no bias */
258 {"LINPUT1", NULL, "Line Jack"},
Liam Girdwood7fb290d2006-10-12 14:32:13 +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 spitz_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 wm8750_spitz_controls[] = {
270 SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
271 spitz_set_jack),
272 SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
273 spitz_set_spk),
274};
275
276/*
277 * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
278 */
279static int spitz_wm8750_init(struct snd_soc_codec *codec)
280{
281 int i, err;
282
283 /* NC codec pins */
Mark Brown35f5e542008-09-27 10:45:09 +0100284 snd_soc_dapm_nc_pin(codec, "RINPUT1");
285 snd_soc_dapm_nc_pin(codec, "LINPUT2");
286 snd_soc_dapm_nc_pin(codec, "RINPUT2");
287 snd_soc_dapm_nc_pin(codec, "LINPUT3");
288 snd_soc_dapm_nc_pin(codec, "RINPUT3");
289 snd_soc_dapm_nc_pin(codec, "OUT3");
290 snd_soc_dapm_nc_pin(codec, "MONO1");
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200291
292 /* Add spitz specific controls */
293 for (i = 0; i < ARRAY_SIZE(wm8750_spitz_controls); i++) {
294 err = snd_ctl_add(codec->card,
295 snd_soc_cnew(&wm8750_spitz_controls[i], codec, NULL));
296 if (err < 0)
297 return err;
298 }
299
300 /* Add spitz specific widgets */
Mark Brown25191c42008-05-13 14:55:48 +0200301 snd_soc_dapm_new_controls(codec, wm8750_dapm_widgets,
302 ARRAY_SIZE(wm8750_dapm_widgets));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200303
Mark Brown25191c42008-05-13 14:55:48 +0200304 /* Set up spitz specific audio paths */
305 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200306
Liam Girdwooda5302182008-07-07 13:35:17 +0100307 snd_soc_dapm_sync(codec);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200308 return 0;
309}
310
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200311/* spitz digital audio interface glue - connects codec <--> CPU */
312static struct snd_soc_dai_link spitz_dai = {
313 .name = "wm8750",
314 .stream_name = "WM8750",
315 .cpu_dai = &pxa_i2s_dai,
316 .codec_dai = &wm8750_dai,
317 .init = spitz_wm8750_init,
Liam Girdwood97952f62007-02-02 17:22:46 +0100318 .ops = &spitz_ops,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200319};
320
321/* spitz audio machine driver */
322static struct snd_soc_machine snd_soc_machine_spitz = {
323 .name = "Spitz",
324 .dai_link = &spitz_dai,
325 .num_links = 1,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200326};
327
328/* spitz audio private data */
329static struct wm8750_setup_data spitz_wm8750_setup = {
Jean Delvareee1d0092008-09-01 18:47:00 +0100330 .i2c_bus = 0,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200331 .i2c_address = 0x1b,
332};
333
334/* spitz audio subsystem */
335static struct snd_soc_device spitz_snd_devdata = {
336 .machine = &snd_soc_machine_spitz,
337 .platform = &pxa2xx_soc_platform,
338 .codec_dev = &soc_codec_dev_wm8750,
339 .codec_data = &spitz_wm8750_setup,
340};
341
342static struct platform_device *spitz_snd_device;
343
344static int __init spitz_init(void)
345{
346 int ret;
347
348 if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
349 return -ENODEV;
350
351 spitz_snd_device = platform_device_alloc("soc-audio", -1);
352 if (!spitz_snd_device)
353 return -ENOMEM;
354
355 platform_set_drvdata(spitz_snd_device, &spitz_snd_devdata);
356 spitz_snd_devdata.dev = &spitz_snd_device->dev;
357 ret = platform_device_add(spitz_snd_device);
358
359 if (ret)
360 platform_device_put(spitz_snd_device);
361
362 return ret;
363}
364
365static void __exit spitz_exit(void)
366{
367 platform_device_unregister(spitz_snd_device);
368}
369
370module_init(spitz_init);
371module_exit(spitz_exit);
372
373MODULE_AUTHOR("Richard Purdie");
374MODULE_DESCRIPTION("ALSA SoC Spitz");
375MODULE_LICENSE("GPL");