4dd8f35312b300ea5a1c1e41ec4b431a0d74085e
[sfrench/cifs-2.6.git] / sound / soc / pxa / spitz.c
1 /*
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  *
7  * Authors: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
8  *          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  *
15  *  Revision history
16  *    30th Nov 2005   Initial version.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30
31 #include <asm/mach-types.h>
32 #include <asm/hardware/scoop.h>
33 #include <asm/arch/pxa-regs.h>
34 #include <asm/arch/hardware.h>
35 #include <asm/arch/akita.h>
36 #include <asm/arch/spitz.h>
37 #include "../codecs/wm8750.h"
38 #include "pxa2xx-pcm.h"
39 #include "pxa2xx-i2s.h"
40
41 #define SPITZ_HP        0
42 #define SPITZ_MIC       1
43 #define SPITZ_LINE      2
44 #define SPITZ_HEADSET   3
45 #define SPITZ_HP_OFF    4
46 #define SPITZ_SPK_ON    0
47 #define SPITZ_SPK_OFF   1
48
49  /* audio clock in Hz - rounded from 12.235MHz */
50 #define SPITZ_AUDIO_CLOCK 12288000
51
52 static int spitz_jack_func;
53 static int spitz_spk_func;
54
55 static void spitz_ext_control(struct snd_soc_codec *codec)
56 {
57         if (spitz_spk_func == SPITZ_SPK_ON)
58                 snd_soc_dapm_set_endpoint(codec, "Ext Spk", 1);
59         else
60                 snd_soc_dapm_set_endpoint(codec, "Ext Spk", 0);
61
62         /* set up jack connection */
63         switch (spitz_jack_func) {
64         case SPITZ_HP:
65                 /* enable and unmute hp jack, disable mic bias */
66                 snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
67                 snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0);
68                 snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
69                 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 1);
70                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
71                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
72                 break;
73         case SPITZ_MIC:
74                 /* enable mic jack and bias, mute hp */
75                 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
76                 snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
77                 snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
78                 snd_soc_dapm_set_endpoint(codec, "Mic Jack", 1);
79                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
80                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
81                 break;
82         case SPITZ_LINE:
83                 /* enable line jack, disable mic bias and mute hp */
84                 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
85                 snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
86                 snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0);
87                 snd_soc_dapm_set_endpoint(codec, "Line Jack", 1);
88                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
89                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
90                 break;
91         case SPITZ_HEADSET:
92                 /* enable and unmute headset jack enable mic bias, mute L hp */
93                 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
94                 snd_soc_dapm_set_endpoint(codec, "Mic Jack", 1);
95                 snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
96                 snd_soc_dapm_set_endpoint(codec, "Headset Jack", 1);
97                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
98                 set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
99                 break;
100         case SPITZ_HP_OFF:
101
102                 /* jack removed, everything off */
103                 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
104                 snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0);
105                 snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0);
106                 snd_soc_dapm_set_endpoint(codec, "Line Jack", 0);
107                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L);
108                 reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R);
109                 break;
110         }
111         snd_soc_dapm_sync_endpoints(codec);
112 }
113
114 static int spitz_startup(struct snd_pcm_substream *substream)
115 {
116         struct snd_soc_pcm_runtime *rtd = substream->private_data;
117         struct snd_soc_codec *codec = rtd->socdev->codec;
118
119         /* check the jack status at stream startup */
120         spitz_ext_control(codec);
121         return 0;
122 }
123
124 static int spitz_hw_params(struct snd_pcm_substream *substream,
125         struct snd_pcm_hw_params *params)
126 {
127         struct snd_soc_pcm_runtime *rtd = substream->private_data;
128         struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
129         struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
130         unsigned int clk = 0;
131         int ret = 0;
132
133         switch (params_rate(params)) {
134         case 8000:
135         case 16000:
136         case 48000:
137         case 96000:
138                 clk = 12288000;
139                 break;
140         case 11025:
141         case 22050:
142         case 44100:
143                 clk = 11289600;
144                 break;
145         }
146
147         /* set codec DAI configuration */
148         ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
149                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
150         if (ret < 0)
151                 return ret;
152
153         /* set cpu DAI configuration */
154         ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
155                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
156         if (ret < 0)
157                 return ret;
158
159         /* set the codec system clock for DAC and ADC */
160         ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8750_SYSCLK, clk,
161                 SND_SOC_CLOCK_IN);
162         if (ret < 0)
163                 return ret;
164
165         /* set the I2S system clock as input (unused) */
166         ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
167                 SND_SOC_CLOCK_IN);
168         if (ret < 0)
169                 return ret;
170
171         return 0;
172 }
173
174 static struct snd_soc_ops spitz_ops = {
175         .startup = spitz_startup,
176         .hw_params = spitz_hw_params,
177 };
178
179 static int spitz_get_jack(struct snd_kcontrol *kcontrol,
180         struct snd_ctl_elem_value *ucontrol)
181 {
182         ucontrol->value.integer.value[0] = spitz_jack_func;
183         return 0;
184 }
185
186 static int spitz_set_jack(struct snd_kcontrol *kcontrol,
187         struct snd_ctl_elem_value *ucontrol)
188 {
189         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
190
191         if (spitz_jack_func == ucontrol->value.integer.value[0])
192                 return 0;
193
194         spitz_jack_func = ucontrol->value.integer.value[0];
195         spitz_ext_control(codec);
196         return 1;
197 }
198
199 static int spitz_get_spk(struct snd_kcontrol *kcontrol,
200         struct snd_ctl_elem_value *ucontrol)
201 {
202         ucontrol->value.integer.value[0] = spitz_spk_func;
203         return 0;
204 }
205
206 static int spitz_set_spk(struct snd_kcontrol *kcontrol,
207         struct snd_ctl_elem_value *ucontrol)
208 {
209         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
210
211         if (spitz_spk_func == ucontrol->value.integer.value[0])
212                 return 0;
213
214         spitz_spk_func = ucontrol->value.integer.value[0];
215         spitz_ext_control(codec);
216         return 1;
217 }
218
219 static int spitz_mic_bias(struct snd_soc_dapm_widget *w, int event)
220 {
221         if (machine_is_borzoi() || machine_is_spitz()) {
222                 if (SND_SOC_DAPM_EVENT_ON(event))
223                         set_scoop_gpio(&spitzscoop2_device.dev,
224                                 SPITZ_SCP2_MIC_BIAS);
225                 else
226                         reset_scoop_gpio(&spitzscoop2_device.dev,
227                                 SPITZ_SCP2_MIC_BIAS);
228         }
229
230         if (machine_is_akita()) {
231                 if (SND_SOC_DAPM_EVENT_ON(event))
232                         akita_set_ioexp(&akitaioexp_device.dev,
233                                 AKITA_IOEXP_MIC_BIAS);
234                 else
235                         akita_reset_ioexp(&akitaioexp_device.dev,
236                                 AKITA_IOEXP_MIC_BIAS);
237         }
238         return 0;
239 }
240
241 /* spitz machine dapm widgets */
242 static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
243         SND_SOC_DAPM_HP("Headphone Jack", NULL),
244         SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
245         SND_SOC_DAPM_SPK("Ext Spk", NULL),
246         SND_SOC_DAPM_LINE("Line Jack", NULL),
247
248         /* headset is a mic and mono headphone */
249         SND_SOC_DAPM_HP("Headset Jack", NULL),
250 };
251
252 /* Spitz machine audio_map */
253 static const char *audio_map[][3] = {
254
255         /* headphone connected to LOUT1, ROUT1 */
256         {"Headphone Jack", NULL, "LOUT1"},
257         {"Headphone Jack", NULL, "ROUT1"},
258
259         /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
260         {"Headset Jack", NULL, "ROUT1"},
261
262         /* ext speaker connected to LOUT2, ROUT2  */
263         {"Ext Spk", NULL , "ROUT2"},
264         {"Ext Spk", NULL , "LOUT2"},
265
266         /* mic is connected to input 1 - with bias */
267         {"LINPUT1", NULL, "Mic Bias"},
268         {"Mic Bias", NULL, "Mic Jack"},
269
270         /* line is connected to input 1 - no bias */
271         {"LINPUT1", NULL, "Line Jack"},
272
273         {NULL, NULL, NULL},
274 };
275
276 static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset",
277         "Off"};
278 static const char *spk_function[] = {"On", "Off"};
279 static const struct soc_enum spitz_enum[] = {
280         SOC_ENUM_SINGLE_EXT(5, jack_function),
281         SOC_ENUM_SINGLE_EXT(2, spk_function),
282 };
283
284 static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
285         SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
286                 spitz_set_jack),
287         SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
288                 spitz_set_spk),
289 };
290
291 /*
292  * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device
293  */
294 static int spitz_wm8750_init(struct snd_soc_codec *codec)
295 {
296         int i, err;
297
298         /* NC codec pins */
299         snd_soc_dapm_set_endpoint(codec, "RINPUT1", 0);
300         snd_soc_dapm_set_endpoint(codec, "LINPUT2", 0);
301         snd_soc_dapm_set_endpoint(codec, "RINPUT2", 0);
302         snd_soc_dapm_set_endpoint(codec, "LINPUT3", 0);
303         snd_soc_dapm_set_endpoint(codec, "RINPUT3", 0);
304         snd_soc_dapm_set_endpoint(codec, "OUT3", 0);
305         snd_soc_dapm_set_endpoint(codec, "MONO", 0);
306
307         /* Add spitz specific controls */
308         for (i = 0; i < ARRAY_SIZE(wm8750_spitz_controls); i++) {
309                 err = snd_ctl_add(codec->card,
310                         snd_soc_cnew(&wm8750_spitz_controls[i], codec, NULL));
311                 if (err < 0)
312                         return err;
313         }
314
315         /* Add spitz specific widgets */
316         for (i = 0; i < ARRAY_SIZE(wm8750_dapm_widgets); i++) {
317                 snd_soc_dapm_new_control(codec, &wm8750_dapm_widgets[i]);
318         }
319
320         /* Set up spitz specific audio path audio_map */
321         for (i = 0; audio_map[i][0] != NULL; i++) {
322                 snd_soc_dapm_connect_input(codec, audio_map[i][0],
323                         audio_map[i][1], audio_map[i][2]);
324         }
325
326         snd_soc_dapm_sync_endpoints(codec);
327         return 0;
328 }
329
330 /* spitz digital audio interface glue - connects codec <--> CPU */
331 static struct snd_soc_dai_link spitz_dai = {
332         .name = "wm8750",
333         .stream_name = "WM8750",
334         .cpu_dai = &pxa_i2s_dai,
335         .codec_dai = &wm8750_dai,
336         .init = spitz_wm8750_init,
337         .ops = &spitz_ops,
338 };
339
340 /* spitz audio machine driver */
341 static struct snd_soc_machine snd_soc_machine_spitz = {
342         .name = "Spitz",
343         .dai_link = &spitz_dai,
344         .num_links = 1,
345 };
346
347 /* spitz audio private data */
348 static struct wm8750_setup_data spitz_wm8750_setup = {
349         .i2c_address = 0x1b,
350 };
351
352 /* spitz audio subsystem */
353 static struct snd_soc_device spitz_snd_devdata = {
354         .machine = &snd_soc_machine_spitz,
355         .platform = &pxa2xx_soc_platform,
356         .codec_dev = &soc_codec_dev_wm8750,
357         .codec_data = &spitz_wm8750_setup,
358 };
359
360 static struct platform_device *spitz_snd_device;
361
362 static int __init spitz_init(void)
363 {
364         int ret;
365
366         if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita()))
367                 return -ENODEV;
368
369         spitz_snd_device = platform_device_alloc("soc-audio", -1);
370         if (!spitz_snd_device)
371                 return -ENOMEM;
372
373         platform_set_drvdata(spitz_snd_device, &spitz_snd_devdata);
374         spitz_snd_devdata.dev = &spitz_snd_device->dev;
375         ret = platform_device_add(spitz_snd_device);
376
377         if (ret)
378                 platform_device_put(spitz_snd_device);
379
380         return ret;
381 }
382
383 static void __exit spitz_exit(void)
384 {
385         platform_device_unregister(spitz_snd_device);
386 }
387
388 module_init(spitz_init);
389 module_exit(spitz_exit);
390
391 MODULE_AUTHOR("Richard Purdie");
392 MODULE_DESCRIPTION("ALSA SoC Spitz");
393 MODULE_LICENSE("GPL");