blob: 76288ad905e28aeae7f62ecf78a96aa8c0e03df3 [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>
Liam Girdwood7fb290d2006-10-12 14:32:13 +020026
27#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/spitz.h>
Liam Girdwood7fb290d2006-10-12 14:32:13 +020029#include "../codecs/wm8750.h"
Liam Girdwood97952f62007-02-02 17:22:46 +010030#include "pxa2xx-i2s.h"
Liam Girdwood7fb290d2006-10-12 14:32:13 +020031
32#define SPITZ_HP 0
33#define SPITZ_MIC 1
34#define SPITZ_LINE 2
35#define SPITZ_HEADSET 3
36#define SPITZ_HP_OFF 4
37#define SPITZ_SPK_ON 0
38#define SPITZ_SPK_OFF 1
39
40 /* audio clock in Hz - rounded from 12.235MHz */
41#define SPITZ_AUDIO_CLOCK 12288000
42
43static int spitz_jack_func;
44static int spitz_spk_func;
Marek Vasut04368d02011-04-02 03:43:15 +020045static int spitz_mic_gpio;
Liam Girdwood7fb290d2006-10-12 14:32:13 +020046
47static void spitz_ext_control(struct snd_soc_codec *codec)
48{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020049 struct snd_soc_dapm_context *dapm = &codec->dapm;
50
Liam Girdwood7fb290d2006-10-12 14:32:13 +020051 if (spitz_spk_func == SPITZ_SPK_ON)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020052 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020053 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020054 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020055
56 /* set up jack connection */
57 switch (spitz_jack_func) {
58 case SPITZ_HP:
59 /* enable and unmute hp jack, disable mic bias */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020060 snd_soc_dapm_disable_pin(dapm, "Headset Jack");
61 snd_soc_dapm_disable_pin(dapm, "Mic Jack");
62 snd_soc_dapm_disable_pin(dapm, "Line Jack");
63 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
Eric Miaofff14722008-09-05 22:15:23 +080064 gpio_set_value(SPITZ_GPIO_MUTE_L, 1);
65 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020066 break;
67 case SPITZ_MIC:
68 /* enable mic jack and bias, mute hp */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020069 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
70 snd_soc_dapm_disable_pin(dapm, "Headset Jack");
71 snd_soc_dapm_disable_pin(dapm, "Line Jack");
72 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
Eric Miaofff14722008-09-05 22:15:23 +080073 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
74 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020075 break;
76 case SPITZ_LINE:
77 /* enable line jack, disable mic bias and mute hp */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020078 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
79 snd_soc_dapm_disable_pin(dapm, "Headset Jack");
80 snd_soc_dapm_disable_pin(dapm, "Mic Jack");
81 snd_soc_dapm_enable_pin(dapm, "Line Jack");
Eric Miaofff14722008-09-05 22:15:23 +080082 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
83 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020084 break;
85 case SPITZ_HEADSET:
86 /* enable and unmute headset jack enable mic bias, mute L hp */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020087 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
88 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
89 snd_soc_dapm_disable_pin(dapm, "Line Jack");
90 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
Eric Miaofff14722008-09-05 22:15:23 +080091 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
92 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020093 break;
94 case SPITZ_HP_OFF:
95
96 /* jack removed, everything off */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020097 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
98 snd_soc_dapm_disable_pin(dapm, "Headset Jack");
99 snd_soc_dapm_disable_pin(dapm, "Mic Jack");
100 snd_soc_dapm_disable_pin(dapm, "Line Jack");
Eric Miaofff14722008-09-05 22:15:23 +0800101 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
102 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200103 break;
104 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200105 snd_soc_dapm_sync(dapm);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200106}
107
108static int spitz_startup(struct snd_pcm_substream *substream)
109{
110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000111 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200112
Mark Brown71a29562010-11-05 13:50:48 -0400113 mutex_lock(&codec->mutex);
114
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200115 /* check the jack status at stream startup */
116 spitz_ext_control(codec);
Mark Brown71a29562010-11-05 13:50:48 -0400117
118 mutex_unlock(&codec->mutex);
119
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200120 return 0;
121}
122
Liam Girdwood97952f62007-02-02 17:22:46 +0100123static int spitz_hw_params(struct snd_pcm_substream *substream,
124 struct snd_pcm_hw_params *params)
125{
126 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000127 struct snd_soc_dai *codec_dai = rtd->codec_dai;
128 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwood97952f62007-02-02 17:22:46 +0100129 unsigned int clk = 0;
130 int ret = 0;
131
132 switch (params_rate(params)) {
133 case 8000:
134 case 16000:
135 case 48000:
136 case 96000:
137 clk = 12288000;
138 break;
139 case 11025:
140 case 22050:
141 case 44100:
142 clk = 11289600;
143 break;
144 }
145
Liam Girdwood97952f62007-02-02 17:22:46 +0100146 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100147 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
Liam Girdwood97952f62007-02-02 17:22:46 +0100148 SND_SOC_CLOCK_IN);
149 if (ret < 0)
150 return ret;
151
152 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100153 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwood97952f62007-02-02 17:22:46 +0100154 SND_SOC_CLOCK_IN);
155 if (ret < 0)
156 return ret;
157
158 return 0;
159}
160
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200161static struct snd_soc_ops spitz_ops = {
162 .startup = spitz_startup,
Liam Girdwood97952f62007-02-02 17:22:46 +0100163 .hw_params = spitz_hw_params,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200164};
165
166static int spitz_get_jack(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
168{
169 ucontrol->value.integer.value[0] = spitz_jack_func;
170 return 0;
171}
172
173static int spitz_set_jack(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
177
178 if (spitz_jack_func == ucontrol->value.integer.value[0])
179 return 0;
180
181 spitz_jack_func = ucontrol->value.integer.value[0];
182 spitz_ext_control(codec);
183 return 1;
184}
185
186static int spitz_get_spk(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
189 ucontrol->value.integer.value[0] = spitz_spk_func;
190 return 0;
191}
192
193static int spitz_set_spk(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
195{
196 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
197
198 if (spitz_spk_func == ucontrol->value.integer.value[0])
199 return 0;
200
201 spitz_spk_func = ucontrol->value.integer.value[0];
202 spitz_ext_control(codec);
203 return 1;
204}
205
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100206static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
207 struct snd_kcontrol *k, int event)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200208{
Marek Vasut04368d02011-04-02 03:43:15 +0200209 gpio_set_value_cansleep(spitz_mic_gpio, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200210 return 0;
211}
212
213/* spitz machine dapm widgets */
214static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
215 SND_SOC_DAPM_HP("Headphone Jack", NULL),
216 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
217 SND_SOC_DAPM_SPK("Ext Spk", NULL),
218 SND_SOC_DAPM_LINE("Line Jack", NULL),
219
220 /* headset is a mic and mono headphone */
221 SND_SOC_DAPM_HP("Headset Jack", NULL),
222};
223
224/* Spitz machine audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200225static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200226
227 /* headphone connected to LOUT1, ROUT1 */
228 {"Headphone Jack", NULL, "LOUT1"},
229 {"Headphone Jack", NULL, "ROUT1"},
230
231 /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
232 {"Headset Jack", NULL, "ROUT1"},
233
234 /* ext speaker connected to LOUT2, ROUT2 */
235 {"Ext Spk", NULL , "ROUT2"},
236 {"Ext Spk", NULL , "LOUT2"},
237
238 /* mic is connected to input 1 - with bias */
239 {"LINPUT1", NULL, "Mic Bias"},
240 {"Mic Bias", NULL, "Mic Jack"},
241
242 /* line is connected to input 1 - no bias */
243 {"LINPUT1", NULL, "Line Jack"},
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200244};
245
246static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
247 "Off"};
248static const char *spk_function[] = {"On", "Off"};
249static const struct soc_enum spitz_enum[] = {
250 SOC_ENUM_SINGLE_EXT(5, jack_function),
251 SOC_ENUM_SINGLE_EXT(2, spk_function),
252};
253
254static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
255 SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
256 spitz_set_jack),
257 SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
258 spitz_set_spk),
259};
260
261/*
262 * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
263 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000264static int spitz_wm8750_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200265{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000266 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200267 struct snd_soc_dapm_context *dapm = &codec->dapm;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100268 int err;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200269
270 /* NC codec pins */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271 snd_soc_dapm_nc_pin(dapm, "RINPUT1");
272 snd_soc_dapm_nc_pin(dapm, "LINPUT2");
273 snd_soc_dapm_nc_pin(dapm, "RINPUT2");
274 snd_soc_dapm_nc_pin(dapm, "LINPUT3");
275 snd_soc_dapm_nc_pin(dapm, "RINPUT3");
276 snd_soc_dapm_nc_pin(dapm, "OUT3");
277 snd_soc_dapm_nc_pin(dapm, "MONO1");
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200278
279 /* Add spitz specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100280 err = snd_soc_add_controls(codec, wm8750_spitz_controls,
281 ARRAY_SIZE(wm8750_spitz_controls));
282 if (err < 0)
283 return err;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200284
285 /* Add spitz specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200286 snd_soc_dapm_new_controls(dapm, wm8750_dapm_widgets,
Mark Brown25191c42008-05-13 14:55:48 +0200287 ARRAY_SIZE(wm8750_dapm_widgets));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200288
Mark Brown25191c42008-05-13 14:55:48 +0200289 /* Set up spitz specific audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200290 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200291
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200292 return 0;
293}
294
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200295/* spitz digital audio interface glue - connects codec <--> CPU */
296static struct snd_soc_dai_link spitz_dai = {
297 .name = "wm8750",
298 .stream_name = "WM8750",
Dmitry Eremin-Solenikova3adfa02011-01-21 22:14:17 +0300299 .cpu_dai_name = "pxa2xx-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000300 .codec_dai_name = "wm8750-hifi",
301 .platform_name = "pxa-pcm-audio",
Mark Browndc5de622011-08-03 18:22:28 +0900302 .codec_name = "wm8750.0-001b",
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200303 .init = spitz_wm8750_init,
Axel Linc0e94232011-12-30 23:42:55 +0800304 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
305 SND_SOC_DAIFMT_CBS_CFS,
Liam Girdwood97952f62007-02-02 17:22:46 +0100306 .ops = &spitz_ops,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200307};
308
309/* spitz audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000310static struct snd_soc_card snd_soc_spitz = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200311 .name = "Spitz",
Axel Lin561c6a12011-12-22 09:44:43 +0800312 .owner = THIS_MODULE,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200313 .dai_link = &spitz_dai,
314 .num_links = 1,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200315};
316
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200317static struct platform_device *spitz_snd_device;
318
319static int __init spitz_init(void)
320{
321 int ret;
322
323 if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
324 return -ENODEV;
325
Marek Vasut04368d02011-04-02 03:43:15 +0200326 if (machine_is_borzoi() || machine_is_spitz())
327 spitz_mic_gpio = SPITZ_GPIO_MIC_BIAS;
328 else
329 spitz_mic_gpio = AKITA_GPIO_MIC_BIAS;
330
331 ret = gpio_request(spitz_mic_gpio, "MIC GPIO");
332 if (ret)
333 goto err1;
334
335 ret = gpio_direction_output(spitz_mic_gpio, 0);
336 if (ret)
337 goto err2;
338
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200339 spitz_snd_device = platform_device_alloc("soc-audio", -1);
Marek Vasut04368d02011-04-02 03:43:15 +0200340 if (!spitz_snd_device) {
341 ret = -ENOMEM;
342 goto err2;
343 }
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200344
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000345 platform_set_drvdata(spitz_snd_device, &snd_soc_spitz);
Marek Vasut04368d02011-04-02 03:43:15 +0200346
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200347 ret = platform_device_add(spitz_snd_device);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200348 if (ret)
Marek Vasut04368d02011-04-02 03:43:15 +0200349 goto err3;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200350
Marek Vasut04368d02011-04-02 03:43:15 +0200351 return 0;
352
353err3:
354 platform_device_put(spitz_snd_device);
355err2:
356 gpio_free(spitz_mic_gpio);
357err1:
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200358 return ret;
359}
360
361static void __exit spitz_exit(void)
362{
363 platform_device_unregister(spitz_snd_device);
Marek Vasut04368d02011-04-02 03:43:15 +0200364 gpio_free(spitz_mic_gpio);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200365}
366
367module_init(spitz_init);
368module_exit(spitz_exit);
369
370MODULE_AUTHOR("Richard Purdie");
371MODULE_DESCRIPTION("ALSA SoC Spitz");
372MODULE_LICENSE("GPL");