blob: a6d680acd907ebd1e72c62d0c0e0d8969153645d [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
Axel Lin059ad762012-02-17 16:15:56 +080047static void spitz_ext_control(struct snd_soc_dapm_context *dapm)
Liam Girdwood7fb290d2006-10-12 14:32:13 +020048{
Charles Keepax1845a722014-02-18 15:22:27 +000049 snd_soc_dapm_mutex_lock(dapm);
50
Liam Girdwood7fb290d2006-10-12 14:32:13 +020051 if (spitz_spk_func == SPITZ_SPK_ON)
Charles Keepax1845a722014-02-18 15:22:27 +000052 snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020053 else
Charles Keepax1845a722014-02-18 15:22:27 +000054 snd_soc_dapm_disable_pin_unlocked(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 */
Charles Keepax1845a722014-02-18 15:22:27 +000060 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
61 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
62 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
63 snd_soc_dapm_enable_pin_unlocked(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 */
Charles Keepax1845a722014-02-18 15:22:27 +000069 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
70 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
71 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
72 snd_soc_dapm_enable_pin_unlocked(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 */
Charles Keepax1845a722014-02-18 15:22:27 +000078 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
79 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
80 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
81 snd_soc_dapm_enable_pin_unlocked(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 */
Charles Keepax1845a722014-02-18 15:22:27 +000087 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
88 snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
89 snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
90 snd_soc_dapm_enable_pin_unlocked(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 */
Charles Keepax1845a722014-02-18 15:22:27 +000097 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
98 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
99 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
100 snd_soc_dapm_disable_pin_unlocked(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 }
Charles Keepax1845a722014-02-18 15:22:27 +0000105
106 snd_soc_dapm_sync_unlocked(dapm);
107
108 snd_soc_dapm_mutex_unlock(dapm);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200109}
110
111static int spitz_startup(struct snd_pcm_substream *substream)
112{
113 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Mark Brown71a29562010-11-05 13:50:48 -0400114
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200115 /* check the jack status at stream startup */
Lars-Peter Clausene14de472014-03-01 15:48:13 +0100116 spitz_ext_control(&rtd->card->dapm);
Mark Brown71a29562010-11-05 13:50:48 -0400117
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200118 return 0;
119}
120
Liam Girdwood97952f62007-02-02 17:22:46 +0100121static int spitz_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 Girdwood97952f62007-02-02 17:22:46 +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
Liam Girdwood97952f62007-02-02 17:22:46 +0100144 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100145 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
Liam Girdwood97952f62007-02-02 17:22:46 +0100146 SND_SOC_CLOCK_IN);
147 if (ret < 0)
148 return ret;
149
150 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100151 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwood97952f62007-02-02 17:22:46 +0100152 SND_SOC_CLOCK_IN);
153 if (ret < 0)
154 return ret;
155
156 return 0;
157}
158
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200159static struct snd_soc_ops spitz_ops = {
160 .startup = spitz_startup,
Liam Girdwood97952f62007-02-02 17:22:46 +0100161 .hw_params = spitz_hw_params,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200162};
163
164static int spitz_get_jack(struct snd_kcontrol *kcontrol,
165 struct snd_ctl_elem_value *ucontrol)
166{
167 ucontrol->value.integer.value[0] = spitz_jack_func;
168 return 0;
169}
170
171static int spitz_set_jack(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *ucontrol)
173{
Liam Girdwood0e668212012-02-16 12:00:23 +0000174 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200175
176 if (spitz_jack_func == ucontrol->value.integer.value[0])
177 return 0;
178
179 spitz_jack_func = ucontrol->value.integer.value[0];
Axel Lin059ad762012-02-17 16:15:56 +0800180 spitz_ext_control(&card->dapm);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200181 return 1;
182}
183
184static int spitz_get_spk(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
186{
187 ucontrol->value.integer.value[0] = spitz_spk_func;
188 return 0;
189}
190
191static int spitz_set_spk(struct snd_kcontrol *kcontrol,
192 struct snd_ctl_elem_value *ucontrol)
193{
Axel Lin059ad762012-02-17 16:15:56 +0800194 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200195
196 if (spitz_spk_func == ucontrol->value.integer.value[0])
197 return 0;
198
199 spitz_spk_func = ucontrol->value.integer.value[0];
Axel Lin059ad762012-02-17 16:15:56 +0800200 spitz_ext_control(&card->dapm);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200201 return 1;
202}
203
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100204static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
205 struct snd_kcontrol *k, int event)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200206{
Marek Vasut04368d02011-04-02 03:43:15 +0200207 gpio_set_value_cansleep(spitz_mic_gpio, SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200208 return 0;
209}
210
211/* spitz machine dapm widgets */
212static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
213 SND_SOC_DAPM_HP("Headphone Jack", NULL),
214 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
215 SND_SOC_DAPM_SPK("Ext Spk", NULL),
216 SND_SOC_DAPM_LINE("Line Jack", NULL),
217
218 /* headset is a mic and mono headphone */
219 SND_SOC_DAPM_HP("Headset Jack", NULL),
220};
221
222/* Spitz machine audio_map */
Axel Lin7c274262011-12-31 08:44:04 +0800223static const struct snd_soc_dapm_route spitz_audio_map[] = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200224
225 /* headphone connected to LOUT1, ROUT1 */
226 {"Headphone Jack", NULL, "LOUT1"},
227 {"Headphone Jack", NULL, "ROUT1"},
228
229 /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
230 {"Headset Jack", NULL, "ROUT1"},
231
232 /* ext speaker connected to LOUT2, ROUT2 */
233 {"Ext Spk", NULL , "ROUT2"},
234 {"Ext Spk", NULL , "LOUT2"},
235
236 /* mic is connected to input 1 - with bias */
237 {"LINPUT1", NULL, "Mic Bias"},
238 {"Mic Bias", NULL, "Mic Jack"},
239
240 /* line is connected to input 1 - no bias */
241 {"LINPUT1", NULL, "Line Jack"},
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200242};
243
244static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
245 "Off"};
246static const char *spk_function[] = {"On", "Off"};
247static const struct soc_enum spitz_enum[] = {
248 SOC_ENUM_SINGLE_EXT(5, jack_function),
249 SOC_ENUM_SINGLE_EXT(2, spk_function),
250};
251
252static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
253 SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
254 spitz_set_jack),
255 SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
256 spitz_set_spk),
257};
258
259/*
260 * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
261 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000262static int spitz_wm8750_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200263{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000264 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200265 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200266
267 /* NC codec pins */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200268 snd_soc_dapm_nc_pin(dapm, "RINPUT1");
269 snd_soc_dapm_nc_pin(dapm, "LINPUT2");
270 snd_soc_dapm_nc_pin(dapm, "RINPUT2");
271 snd_soc_dapm_nc_pin(dapm, "LINPUT3");
272 snd_soc_dapm_nc_pin(dapm, "RINPUT3");
273 snd_soc_dapm_nc_pin(dapm, "OUT3");
274 snd_soc_dapm_nc_pin(dapm, "MONO1");
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200275
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200276 return 0;
277}
278
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200279/* spitz digital audio interface glue - connects codec <--> CPU */
280static struct snd_soc_dai_link spitz_dai = {
281 .name = "wm8750",
282 .stream_name = "WM8750",
Dmitry Eremin-Solenikova3adfa02011-01-21 22:14:17 +0300283 .cpu_dai_name = "pxa2xx-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284 .codec_dai_name = "wm8750-hifi",
285 .platform_name = "pxa-pcm-audio",
Mark Browndc5de622011-08-03 18:22:28 +0900286 .codec_name = "wm8750.0-001b",
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200287 .init = spitz_wm8750_init,
Axel Linc0e94232011-12-30 23:42:55 +0800288 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
289 SND_SOC_DAIFMT_CBS_CFS,
Liam Girdwood97952f62007-02-02 17:22:46 +0100290 .ops = &spitz_ops,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200291};
292
293/* spitz audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000294static struct snd_soc_card snd_soc_spitz = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200295 .name = "Spitz",
Axel Lin561c6a12011-12-22 09:44:43 +0800296 .owner = THIS_MODULE,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200297 .dai_link = &spitz_dai,
298 .num_links = 1,
Axel Lin7c274262011-12-31 08:44:04 +0800299
300 .controls = wm8750_spitz_controls,
301 .num_controls = ARRAY_SIZE(wm8750_spitz_controls),
302 .dapm_widgets = wm8750_dapm_widgets,
303 .num_dapm_widgets = ARRAY_SIZE(wm8750_dapm_widgets),
304 .dapm_routes = spitz_audio_map,
305 .num_dapm_routes = ARRAY_SIZE(spitz_audio_map),
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200306};
307
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400308static int spitz_probe(struct platform_device *pdev)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200309{
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400310 struct snd_soc_card *card = &snd_soc_spitz;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200311 int ret;
312
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400313 if (machine_is_akita())
Marek Vasut04368d02011-04-02 03:43:15 +0200314 spitz_mic_gpio = AKITA_GPIO_MIC_BIAS;
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400315 else
316 spitz_mic_gpio = SPITZ_GPIO_MIC_BIAS;
Marek Vasut04368d02011-04-02 03:43:15 +0200317
318 ret = gpio_request(spitz_mic_gpio, "MIC GPIO");
319 if (ret)
320 goto err1;
321
322 ret = gpio_direction_output(spitz_mic_gpio, 0);
323 if (ret)
324 goto err2;
325
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400326 card->dev = &pdev->dev;
327
328 ret = snd_soc_register_card(card);
329 if (ret) {
330 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
331 ret);
Marek Vasut04368d02011-04-02 03:43:15 +0200332 goto err2;
333 }
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200334
Marek Vasut04368d02011-04-02 03:43:15 +0200335 return 0;
336
Marek Vasut04368d02011-04-02 03:43:15 +0200337err2:
338 gpio_free(spitz_mic_gpio);
339err1:
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200340 return ret;
341}
342
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400343static int spitz_remove(struct platform_device *pdev)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200344{
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400345 struct snd_soc_card *card = platform_get_drvdata(pdev);
346
347 snd_soc_unregister_card(card);
Marek Vasut04368d02011-04-02 03:43:15 +0200348 gpio_free(spitz_mic_gpio);
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400349 return 0;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200350}
351
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400352static struct platform_driver spitz_driver = {
353 .driver = {
354 .name = "spitz-audio",
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400355 .pm = &snd_soc_pm_ops,
356 },
357 .probe = spitz_probe,
358 .remove = spitz_remove,
359};
360
361module_platform_driver(spitz_driver);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200362
363MODULE_AUTHOR("Richard Purdie");
364MODULE_DESCRIPTION("ALSA SoC Spitz");
365MODULE_LICENSE("GPL");
Dmitry Eremin-Solenikovecf00152014-10-24 21:50:05 +0400366MODULE_ALIAS("platform:spitz-audio");