blob: 1941a357e8c4f69fddf94a9a682d2fa0ebaf7d61 [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"
31#include "pxa2xx-pcm.h"
Liam Girdwood97952f62007-02-02 17:22:46 +010032#include "pxa2xx-i2s.h"
Liam Girdwood7fb290d2006-10-12 14:32:13 +020033
34#define SPITZ_HP 0
35#define SPITZ_MIC 1
36#define SPITZ_LINE 2
37#define SPITZ_HEADSET 3
38#define SPITZ_HP_OFF 4
39#define SPITZ_SPK_ON 0
40#define SPITZ_SPK_OFF 1
41
42 /* audio clock in Hz - rounded from 12.235MHz */
43#define SPITZ_AUDIO_CLOCK 12288000
44
45static int spitz_jack_func;
46static int spitz_spk_func;
47
48static void spitz_ext_control(struct snd_soc_codec *codec)
49{
50 if (spitz_spk_func == SPITZ_SPK_ON)
Liam Girdwooda5302182008-07-07 13:35:17 +010051 snd_soc_dapm_enable_pin(codec, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020052 else
Liam Girdwooda5302182008-07-07 13:35:17 +010053 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Liam Girdwood7fb290d2006-10-12 14:32:13 +020054
55 /* set up jack connection */
56 switch (spitz_jack_func) {
57 case SPITZ_HP:
58 /* enable and unmute hp jack, disable mic bias */
Liam Girdwooda5302182008-07-07 13:35:17 +010059 snd_soc_dapm_disable_pin(codec, "Headset Jack");
60 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");
Eric Miaofff14722008-09-05 22:15:23 +080063 gpio_set_value(SPITZ_GPIO_MUTE_L, 1);
64 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020065 break;
66 case SPITZ_MIC:
67 /* enable mic jack and bias, mute hp */
Liam Girdwooda5302182008-07-07 13:35:17 +010068 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
69 snd_soc_dapm_disable_pin(codec, "Headset Jack");
70 snd_soc_dapm_disable_pin(codec, "Line Jack");
71 snd_soc_dapm_enable_pin(codec, "Mic Jack");
Eric Miaofff14722008-09-05 22:15:23 +080072 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
73 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020074 break;
75 case SPITZ_LINE:
76 /* enable line jack, disable mic bias and mute hp */
Liam Girdwooda5302182008-07-07 13:35:17 +010077 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
78 snd_soc_dapm_disable_pin(codec, "Headset Jack");
79 snd_soc_dapm_disable_pin(codec, "Mic Jack");
80 snd_soc_dapm_enable_pin(codec, "Line Jack");
Eric Miaofff14722008-09-05 22:15:23 +080081 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
82 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020083 break;
84 case SPITZ_HEADSET:
85 /* enable and unmute headset jack enable mic bias, mute L hp */
Liam Girdwooda5302182008-07-07 13:35:17 +010086 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
87 snd_soc_dapm_enable_pin(codec, "Mic Jack");
88 snd_soc_dapm_disable_pin(codec, "Line Jack");
89 snd_soc_dapm_enable_pin(codec, "Headset Jack");
Eric Miaofff14722008-09-05 22:15:23 +080090 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
91 gpio_set_value(SPITZ_GPIO_MUTE_R, 1);
Liam Girdwood7fb290d2006-10-12 14:32:13 +020092 break;
93 case SPITZ_HP_OFF:
94
95 /* jack removed, everything off */
Liam Girdwooda5302182008-07-07 13:35:17 +010096 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
97 snd_soc_dapm_disable_pin(codec, "Headset Jack");
98 snd_soc_dapm_disable_pin(codec, "Mic Jack");
99 snd_soc_dapm_disable_pin(codec, "Line Jack");
Eric Miaofff14722008-09-05 22:15:23 +0800100 gpio_set_value(SPITZ_GPIO_MUTE_L, 0);
101 gpio_set_value(SPITZ_GPIO_MUTE_R, 0);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200102 break;
103 }
Liam Girdwooda5302182008-07-07 13:35:17 +0100104 snd_soc_dapm_sync(codec);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200105}
106
107static int spitz_startup(struct snd_pcm_substream *substream)
108{
109 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwai9e30d772009-02-11 08:28:04 +0100110 struct snd_soc_codec *codec = rtd->socdev->card->codec;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200111
112 /* check the jack status at stream startup */
113 spitz_ext_control(codec);
114 return 0;
115}
116
Liam Girdwood97952f62007-02-02 17:22:46 +0100117static int spitz_hw_params(struct snd_pcm_substream *substream,
118 struct snd_pcm_hw_params *params)
119{
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood917f93a2008-07-07 16:08:11 +0100121 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
122 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Liam Girdwood97952f62007-02-02 17:22:46 +0100123 unsigned int clk = 0;
124 int ret = 0;
125
126 switch (params_rate(params)) {
127 case 8000:
128 case 16000:
129 case 48000:
130 case 96000:
131 clk = 12288000;
132 break;
133 case 11025:
134 case 22050:
135 case 44100:
136 clk = 11289600;
137 break;
138 }
139
140 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100141 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwood97952f62007-02-02 17:22:46 +0100142 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
143 if (ret < 0)
144 return ret;
145
146 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100147 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
Liam Girdwood97952f62007-02-02 17:22:46 +0100148 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
149 if (ret < 0)
150 return ret;
151
152 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100153 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
Liam Girdwood97952f62007-02-02 17:22:46 +0100154 SND_SOC_CLOCK_IN);
155 if (ret < 0)
156 return ret;
157
158 /* set the I2S system clock as input (unused) */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100159 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
Liam Girdwood97952f62007-02-02 17:22:46 +0100160 SND_SOC_CLOCK_IN);
161 if (ret < 0)
162 return ret;
163
164 return 0;
165}
166
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200167static struct snd_soc_ops spitz_ops = {
168 .startup = spitz_startup,
Liam Girdwood97952f62007-02-02 17:22:46 +0100169 .hw_params = spitz_hw_params,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200170};
171
172static int spitz_get_jack(struct snd_kcontrol *kcontrol,
173 struct snd_ctl_elem_value *ucontrol)
174{
175 ucontrol->value.integer.value[0] = spitz_jack_func;
176 return 0;
177}
178
179static int spitz_set_jack(struct snd_kcontrol *kcontrol,
180 struct snd_ctl_elem_value *ucontrol)
181{
182 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
183
184 if (spitz_jack_func == ucontrol->value.integer.value[0])
185 return 0;
186
187 spitz_jack_func = ucontrol->value.integer.value[0];
188 spitz_ext_control(codec);
189 return 1;
190}
191
192static int spitz_get_spk(struct snd_kcontrol *kcontrol,
193 struct snd_ctl_elem_value *ucontrol)
194{
195 ucontrol->value.integer.value[0] = spitz_spk_func;
196 return 0;
197}
198
199static int spitz_set_spk(struct snd_kcontrol *kcontrol,
200 struct snd_ctl_elem_value *ucontrol)
201{
202 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
203
204 if (spitz_spk_func == ucontrol->value.integer.value[0])
205 return 0;
206
207 spitz_spk_func = ucontrol->value.integer.value[0];
208 spitz_ext_control(codec);
209 return 1;
210}
211
Jarkko Nikula338c7ed2008-02-28 12:34:48 +0100212static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
213 struct snd_kcontrol *k, int event)
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200214{
Eric Miaofff14722008-09-05 22:15:23 +0800215 if (machine_is_borzoi() || machine_is_spitz())
216 gpio_set_value(SPITZ_GPIO_MIC_BIAS,
217 SND_SOC_DAPM_EVENT_ON(event));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200218
Eric Miaof72de662008-09-06 08:46:23 +0800219 if (machine_is_akita())
220 gpio_set_value(AKITA_GPIO_MIC_BIAS,
221 SND_SOC_DAPM_EVENT_ON(event));
222
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200223 return 0;
224}
225
226/* spitz machine dapm widgets */
227static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
228 SND_SOC_DAPM_HP("Headphone Jack", NULL),
229 SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
230 SND_SOC_DAPM_SPK("Ext Spk", NULL),
231 SND_SOC_DAPM_LINE("Line Jack", NULL),
232
233 /* headset is a mic and mono headphone */
234 SND_SOC_DAPM_HP("Headset Jack", NULL),
235};
236
237/* Spitz machine audio_map */
Mark Brown25191c42008-05-13 14:55:48 +0200238static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200239
240 /* headphone connected to LOUT1, ROUT1 */
241 {"Headphone Jack", NULL, "LOUT1"},
242 {"Headphone Jack", NULL, "ROUT1"},
243
244 /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
245 {"Headset Jack", NULL, "ROUT1"},
246
247 /* ext speaker connected to LOUT2, ROUT2 */
248 {"Ext Spk", NULL , "ROUT2"},
249 {"Ext Spk", NULL , "LOUT2"},
250
251 /* mic is connected to input 1 - with bias */
252 {"LINPUT1", NULL, "Mic Bias"},
253 {"Mic Bias", NULL, "Mic Jack"},
254
255 /* line is connected to input 1 - no bias */
256 {"LINPUT1", NULL, "Line Jack"},
Liam Girdwood7fb290d2006-10-12 14:32:13 +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 spitz_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 wm8750_spitz_controls[] = {
268 SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
269 spitz_set_jack),
270 SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
271 spitz_set_spk),
272};
273
274/*
275 * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
276 */
277static int spitz_wm8750_init(struct snd_soc_codec *codec)
278{
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100279 int err;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200280
281 /* NC codec pins */
Mark Brown35f5e542008-09-27 10:45:09 +0100282 snd_soc_dapm_nc_pin(codec, "RINPUT1");
283 snd_soc_dapm_nc_pin(codec, "LINPUT2");
284 snd_soc_dapm_nc_pin(codec, "RINPUT2");
285 snd_soc_dapm_nc_pin(codec, "LINPUT3");
286 snd_soc_dapm_nc_pin(codec, "RINPUT3");
287 snd_soc_dapm_nc_pin(codec, "OUT3");
288 snd_soc_dapm_nc_pin(codec, "MONO1");
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200289
290 /* Add spitz specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100291 err = snd_soc_add_controls(codec, wm8750_spitz_controls,
292 ARRAY_SIZE(wm8750_spitz_controls));
293 if (err < 0)
294 return err;
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200295
296 /* Add spitz specific widgets */
Mark Brown25191c42008-05-13 14:55:48 +0200297 snd_soc_dapm_new_controls(codec, wm8750_dapm_widgets,
298 ARRAY_SIZE(wm8750_dapm_widgets));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200299
Mark Brown25191c42008-05-13 14:55:48 +0200300 /* Set up spitz specific audio paths */
301 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200302
Liam Girdwooda5302182008-07-07 13:35:17 +0100303 snd_soc_dapm_sync(codec);
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200304 return 0;
305}
306
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200307/* spitz digital audio interface glue - connects codec <--> CPU */
308static struct snd_soc_dai_link spitz_dai = {
309 .name = "wm8750",
310 .stream_name = "WM8750",
311 .cpu_dai = &pxa_i2s_dai,
312 .codec_dai = &wm8750_dai,
313 .init = spitz_wm8750_init,
Liam Girdwood97952f62007-02-02 17:22:46 +0100314 .ops = &spitz_ops,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200315};
316
317/* spitz audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000318static struct snd_soc_card snd_soc_spitz = {
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200319 .name = "Spitz",
Mark Brown87689d52008-12-02 16:01:14 +0000320 .platform = &pxa2xx_soc_platform,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200321 .dai_link = &spitz_dai,
322 .num_links = 1,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200323};
324
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200325/* spitz audio subsystem */
326static struct snd_soc_device spitz_snd_devdata = {
Mark Brown87506542008-11-18 20:50:34 +0000327 .card = &snd_soc_spitz,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200328 .codec_dev = &soc_codec_dev_wm8750,
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200329};
330
Marek Vasut6ca0c222010-04-08 20:48:51 +0200331/*
332 * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
333 * New drivers should register the wm8750 I2C device in the machine
334 * setup code (under arch/arm for ARM systems).
335 */
336static int wm8750_i2c_register(void)
337{
338 struct i2c_board_info info;
339 struct i2c_adapter *adapter;
340 struct i2c_client *client;
341
342 memset(&info, 0, sizeof(struct i2c_board_info));
343 info.addr = 0x1b;
344 strlcpy(info.type, "wm8750", I2C_NAME_SIZE);
345
346 adapter = i2c_get_adapter(0);
347 if (!adapter) {
348 printk(KERN_ERR "can't get i2c adapter 0\n");
349 return -ENODEV;
350 }
351
352 client = i2c_new_device(adapter, &info);
353 i2c_put_adapter(adapter);
354 if (!client) {
355 printk(KERN_ERR "can't add i2c device at 0x%x\n",
356 (unsigned int)info.addr);
357 return -ENODEV;
358 }
359
360 return 0;
361}
362
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200363static struct platform_device *spitz_snd_device;
364
365static int __init spitz_init(void)
366{
367 int ret;
368
369 if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
370 return -ENODEV;
371
Marek Vasut6ca0c222010-04-08 20:48:51 +0200372 ret = wm8750_i2c_setup();
373 if (ret != 0)
374 return ret;
375
Liam Girdwood7fb290d2006-10-12 14:32:13 +0200376 spitz_snd_device = platform_device_alloc("soc-audio", -1);
377 if (!spitz_snd_device)
378 return -ENOMEM;
379
380 platform_set_drvdata(spitz_snd_device, &spitz_snd_devdata);
381 spitz_snd_devdata.dev = &spitz_snd_device->dev;
382 ret = platform_device_add(spitz_snd_device);
383
384 if (ret)
385 platform_device_put(spitz_snd_device);
386
387 return ret;
388}
389
390static void __exit spitz_exit(void)
391{
392 platform_device_unregister(spitz_snd_device);
393}
394
395module_init(spitz_init);
396module_exit(spitz_exit);
397
398MODULE_AUTHOR("Richard Purdie");
399MODULE_DESCRIPTION("ALSA SoC Spitz");
400MODULE_LICENSE("GPL");