blob: 0680b11c268560fa12cb84bb38ebd1d7aca27a31 [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/spitz.h>
Liam Girdwood7fb290d2006-10-12 14:32:13 +020030#include "../codecs/wm8750.h"
Liam Girdwood97952f62007-02-02 17:22:46 +010031#include "pxa2xx-i2s.h"
Liam Girdwood7fb290d2006-10-12 14:32:13 +020032
33#define SPITZ_HP 0
34#define SPITZ_MIC 1
35#define SPITZ_LINE 2
36#define SPITZ_HEADSET 3
37#define SPITZ_HP_OFF 4
38#define SPITZ_SPK_ON 0
39#define SPITZ_SPK_OFF 1
40
41 /* audio clock in Hz - rounded from 12.235MHz */
42#define SPITZ_AUDIO_CLOCK 12288000
43
44static int spitz_jack_func;
45static int spitz_spk_func;
46
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
113 /* check the jack status at stream startup */
114 spitz_ext_control(codec);
115 return 0;
116}
117
Liam Girdwood97952f62007-02-02 17:22:46 +0100118static int spitz_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 Girdwoodf0fba2a2010-03-17 20:15:21 +0000122 struct snd_soc_dai *codec_dai = rtd->codec_dai;
123 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwood97952f62007-02-02 17:22:46 +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 Girdwood97952f62007-02-02 17:22:46 +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 Girdwood97952f62007-02-02 17:22:46 +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, WM8750_SYSCLK, clk,
Liam Girdwood97952f62007-02-02 17:22:46 +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 Girdwood97952f62007-02-02 17:22:46 +0100161 SND_SOC_CLOCK_IN);
162 if (ret < 0)
163 return ret;
164
165 return 0;
166}
167
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200168static struct snd_soc_ops spitz_ops = {
169 .startup = spitz_startup,
Liam Girdwood97952f62007-02-02 17:22:46 +0100170 .hw_params = spitz_hw_params,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200171};
172
173static int spitz_get_jack(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176 ucontrol->value.integer.value[0] = spitz_jack_func;
177 return 0;
178}
179
180static int spitz_set_jack(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *ucontrol)
182{
183 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
184
185 if (spitz_jack_func == ucontrol->value.integer.value[0])
186 return 0;
187
188 spitz_jack_func = ucontrol->value.integer.value[0];
189 spitz_ext_control(codec);
190 return 1;
191}
192
193static int spitz_get_spk(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
195{
196 ucontrol->value.integer.value[0] = spitz_spk_func;
197 return 0;
198}
199
200static int spitz_set_spk(struct snd_kcontrol *kcontrol,
201 struct snd_ctl_elem_value *ucontrol)
202{
203 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
204
205 if (spitz_spk_func == ucontrol->value.integer.value[0])
206 return 0;
207
208 spitz_spk_func = ucontrol->value.integer.value[0];
209 spitz_ext_control(codec);
210 return 1;
211}
212
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100213static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
214 struct snd_kcontrol *k, int event)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200215{
Eric Miaofff14722008-09-05 22:15:23 +0800216 if (machine_is_borzoi() || machine_is_spitz())
217 gpio_set_value(SPITZ_GPIO_MIC_BIAS,
218 SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200219
Eric Miaof72de662008-09-06 08:46:23 +0800220 if (machine_is_akita())
221 gpio_set_value(AKITA_GPIO_MIC_BIAS,
222 SND_SOC_DAPM_EVENT_ON(event));
223
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200224 return 0;
225}
226
227/* spitz machine dapm widgets */
228static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
229 SND_SOC_DAPM_HP("Headphone Jack", NULL),
230 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
231 SND_SOC_DAPM_SPK("Ext Spk", NULL),
232 SND_SOC_DAPM_LINE("Line Jack", NULL),
233
234 /* headset is a mic and mono headphone */
235 SND_SOC_DAPM_HP("Headset Jack", NULL),
236};
237
238/* Spitz machine audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200239static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200240
241 /* headphone connected to LOUT1, ROUT1 */
242 {"Headphone Jack", NULL, "LOUT1"},
243 {"Headphone Jack", NULL, "ROUT1"},
244
245 /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
246 {"Headset Jack", NULL, "ROUT1"},
247
248 /* ext speaker connected to LOUT2, ROUT2 */
249 {"Ext Spk", NULL , "ROUT2"},
250 {"Ext Spk", NULL , "LOUT2"},
251
252 /* mic is connected to input 1 - with bias */
253 {"LINPUT1", NULL, "Mic Bias"},
254 {"Mic Bias", NULL, "Mic Jack"},
255
256 /* line is connected to input 1 - no bias */
257 {"LINPUT1", NULL, "Line Jack"},
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200258};
259
260static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
261 "Off"};
262static const char *spk_function[] = {"On", "Off"};
263static const struct soc_enum spitz_enum[] = {
264 SOC_ENUM_SINGLE_EXT(5, jack_function),
265 SOC_ENUM_SINGLE_EXT(2, spk_function),
266};
267
268static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
269 SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
270 spitz_set_jack),
271 SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
272 spitz_set_spk),
273};
274
275/*
276 * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
277 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000278static int spitz_wm8750_init(struct snd_soc_pcm_runtime *rtd)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200279{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200281 struct snd_soc_dapm_context *dapm = &codec->dapm;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100282 int err;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200283
284 /* NC codec pins */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200285 snd_soc_dapm_nc_pin(dapm, "RINPUT1");
286 snd_soc_dapm_nc_pin(dapm, "LINPUT2");
287 snd_soc_dapm_nc_pin(dapm, "RINPUT2");
288 snd_soc_dapm_nc_pin(dapm, "LINPUT3");
289 snd_soc_dapm_nc_pin(dapm, "RINPUT3");
290 snd_soc_dapm_nc_pin(dapm, "OUT3");
291 snd_soc_dapm_nc_pin(dapm, "MONO1");
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200292
293 /* Add spitz specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100294 err = snd_soc_add_controls(codec, wm8750_spitz_controls,
295 ARRAY_SIZE(wm8750_spitz_controls));
296 if (err < 0)
297 return err;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200298
299 /* Add spitz specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200300 snd_soc_dapm_new_controls(dapm, wm8750_dapm_widgets,
Mark Brown25191c42008-05-13 14:55:48 +0200301 ARRAY_SIZE(wm8750_dapm_widgets));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200302
Mark Brown25191c42008-05-13 14:55:48 +0200303 /* Set up spitz specific audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200304 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200305
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200306 snd_soc_dapm_sync(dapm);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200307 return 0;
308}
309
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200310/* spitz digital audio interface glue - connects codec <--> CPU */
311static struct snd_soc_dai_link spitz_dai = {
312 .name = "wm8750",
313 .stream_name = "WM8750",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000314 .cpu_dai_name = "pxa-is2",
315 .codec_dai_name = "wm8750-hifi",
316 .platform_name = "pxa-pcm-audio",
317 .codec_name = "wm8750-codec.0-001a",
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200318 .init = spitz_wm8750_init,
Liam Girdwood97952f62007-02-02 17:22:46 +0100319 .ops = &spitz_ops,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200320};
321
322/* spitz audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000323static struct snd_soc_card snd_soc_spitz = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200324 .name = "Spitz",
325 .dai_link = &spitz_dai,
326 .num_links = 1,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200327};
328
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200329static struct platform_device *spitz_snd_device;
330
331static int __init spitz_init(void)
332{
333 int ret;
334
335 if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
336 return -ENODEV;
337
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200338 spitz_snd_device = platform_device_alloc("soc-audio", -1);
339 if (!spitz_snd_device)
340 return -ENOMEM;
341
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000342 platform_set_drvdata(spitz_snd_device, &snd_soc_spitz);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200343 ret = platform_device_add(spitz_snd_device);
344
345 if (ret)
346 platform_device_put(spitz_snd_device);
347
348 return ret;
349}
350
351static void __exit spitz_exit(void)
352{
353 platform_device_unregister(spitz_snd_device);
354}
355
356module_init(spitz_init);
357module_exit(spitz_exit);
358
359MODULE_AUTHOR("Richard Purdie");
360MODULE_DESCRIPTION("ALSA SoC Spitz");
361MODULE_LICENSE("GPL");