Root/target/linux/s3c24xx/files-2.6.30/sound/soc/s3c24xx/gta02_wm8753.c

1/*
2 * neo1973_gta02_wm8753.c -- SoC audio for Openmoko freerunner
3 *
4 * Copyright 2007 Openmoko Inc
5 * Author: Graeme Gregory <graeme@openmoko.org>
6 * Copyright 2007 Wolfson Microelectronics PLC.
7 * Author: Graeme Gregory <linux@wolfsonmicro.com>
8 * Copyright 2009 Wolfson Microelectronics
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
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/timer.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/soc.h>
24#include <sound/soc-dapm.h>
25#include <sound/jack.h>
26
27#include <asm/mach-types.h>
28
29#include <plat/regs-iis.h>
30
31#include <mach/regs-clock.h>
32#include <mach/regs-gpio.h>
33#include <mach/hardware.h>
34#include <asm/io.h>
35#include <mach/regs-gpioj.h>
36#include <mach/gta02.h>
37#include "../codecs/wm8753.h"
38#include "s3c24xx-pcm.h"
39#include "s3c24xx-i2s.h"
40
41static struct snd_soc_card neo1973_gta02;
42static struct snd_soc_jack gta02_hs_jack;
43
44static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
45    struct snd_pcm_hw_params *params)
46{
47    struct snd_soc_pcm_runtime *rtd = substream->private_data;
48    struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
49    struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
50    unsigned int pll_out = 0, bclk = 0;
51    int ret = 0;
52    unsigned long iis_clkrate;
53
54    iis_clkrate = s3c24xx_i2s_get_clockrate();
55
56    switch (params_rate(params)) {
57    case 8000:
58    case 16000:
59        pll_out = 12288000;
60        break;
61    case 48000:
62        bclk = WM8753_BCLK_DIV_4;
63        pll_out = 12288000;
64        break;
65    case 96000:
66        bclk = WM8753_BCLK_DIV_2;
67        pll_out = 12288000;
68        break;
69    case 11025:
70        bclk = WM8753_BCLK_DIV_16;
71        pll_out = 11289600;
72        break;
73    case 22050:
74        bclk = WM8753_BCLK_DIV_8;
75        pll_out = 11289600;
76        break;
77    case 44100:
78        bclk = WM8753_BCLK_DIV_4;
79        pll_out = 11289600;
80        break;
81    case 88200:
82        bclk = WM8753_BCLK_DIV_2;
83        pll_out = 11289600;
84        break;
85    }
86
87    /* set codec DAI configuration */
88    ret = snd_soc_dai_set_fmt(codec_dai,
89        SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
90        SND_SOC_DAIFMT_CBM_CFM);
91    if (ret < 0)
92        return ret;
93
94    /* set cpu DAI configuration */
95    ret = snd_soc_dai_set_fmt(cpu_dai,
96        SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
97        SND_SOC_DAIFMT_CBM_CFM);
98    if (ret < 0)
99        return ret;
100
101    /* set the codec system clock for DAC and ADC */
102    ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
103        SND_SOC_CLOCK_IN);
104    if (ret < 0)
105        return ret;
106
107    /* set MCLK division for sample rate */
108    ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
109        S3C2410_IISMOD_32FS);
110    if (ret < 0)
111        return ret;
112
113    /* set codec BCLK division for sample rate */
114    ret = snd_soc_dai_set_clkdiv(codec_dai,
115                    WM8753_BCLKDIV, bclk);
116    if (ret < 0)
117        return ret;
118
119    /* set prescaler division for sample rate */
120    ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
121        S3C24XX_PRESCALE(4, 4));
122    if (ret < 0)
123        return ret;
124
125    /* codec PLL input is PCLK/4 */
126    ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
127        iis_clkrate / 4, pll_out);
128    if (ret < 0)
129        return ret;
130
131    return 0;
132}
133
134static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
135{
136    struct snd_soc_pcm_runtime *rtd = substream->private_data;
137    struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
138
139    /* disable the PLL */
140    return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
141}
142
143/*
144 * Neo1973 WM8753 HiFi DAI opserations.
145 */
146static struct snd_soc_ops neo1973_gta02_hifi_ops = {
147    .hw_params = neo1973_gta02_hifi_hw_params,
148    .hw_free = neo1973_gta02_hifi_hw_free,
149};
150
151static int neo1973_gta02_voice_hw_params(
152    struct snd_pcm_substream *substream,
153    struct snd_pcm_hw_params *params)
154{
155    struct snd_soc_pcm_runtime *rtd = substream->private_data;
156    struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
157    unsigned int pcmdiv = 0;
158    int ret = 0;
159    unsigned long iis_clkrate;
160
161    iis_clkrate = s3c24xx_i2s_get_clockrate();
162
163    if (params_rate(params) != 8000)
164        return -EINVAL;
165    if (params_channels(params) != 1)
166        return -EINVAL;
167
168    pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
169
170    /* todo: gg check mode (DSP_B) against CSR datasheet */
171    /* set codec DAI configuration */
172    ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
173        SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
174    if (ret < 0)
175        return ret;
176
177    /* set the codec system clock for DAC and ADC */
178    ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK,
179        12288000, SND_SOC_CLOCK_IN);
180    if (ret < 0)
181        return ret;
182
183    /* set codec PCM division for sample rate */
184    ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
185                    pcmdiv);
186    if (ret < 0)
187        return ret;
188
189    /* configue and enable PLL for 12.288MHz output */
190    ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
191        iis_clkrate / 4, 12288000);
192    if (ret < 0)
193        return ret;
194
195    return 0;
196}
197
198static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
199{
200    struct snd_soc_pcm_runtime *rtd = substream->private_data;
201    struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
202
203    /* disable the PLL */
204    return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
205}
206
207static struct snd_soc_ops neo1973_gta02_voice_ops = {
208    .hw_params = neo1973_gta02_voice_hw_params,
209    .hw_free = neo1973_gta02_voice_hw_free,
210};
211
212#define LM4853_AMP 1
213#define LM4853_SPK 2
214
215static u8 lm4853_state;
216
217/* This has no effect, it exists only to maintain compatibility with
218 * existing ALSA state files.
219 */
220static int lm4853_set_state(struct snd_kcontrol *kcontrol,
221    struct snd_ctl_elem_value *ucontrol)
222{
223    int val = ucontrol->value.integer.value[0];
224
225    if (val)
226        lm4853_state |= LM4853_AMP;
227    else
228        lm4853_state &= ~LM4853_AMP;
229
230    return 0;
231}
232
233static int lm4853_get_state(struct snd_kcontrol *kcontrol,
234    struct snd_ctl_elem_value *ucontrol)
235{
236    ucontrol->value.integer.value[0] = lm4853_state & LM4853_AMP;
237
238    return 0;
239}
240
241static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
242    struct snd_ctl_elem_value *ucontrol)
243{
244    int val = ucontrol->value.integer.value[0];
245
246    if (val) {
247        lm4853_state |= LM4853_SPK;
248        s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 0);
249    } else {
250        lm4853_state &= ~LM4853_SPK;
251        s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
252    }
253
254    return 0;
255}
256
257static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
258    struct snd_ctl_elem_value *ucontrol)
259{
260    ucontrol->value.integer.value[0] = (lm4853_state & LM4853_SPK) >> 1;
261
262    return 0;
263}
264
265static int lm4853_event(struct snd_soc_dapm_widget *w,
266            struct snd_kcontrol *k,
267            int event)
268{
269    if (SND_SOC_DAPM_EVENT_ON(event))
270        s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 0);
271
272    if (SND_SOC_DAPM_EVENT_OFF(event))
273        s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
274
275    return 0;
276}
277
278static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
279    SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
280    SND_SOC_DAPM_LINE("GSM Line Out", NULL),
281    SND_SOC_DAPM_LINE("GSM Line In", NULL),
282    SND_SOC_DAPM_MIC("Headset Mic", NULL),
283    SND_SOC_DAPM_MIC("Handset Mic", NULL),
284    SND_SOC_DAPM_SPK("Handset Spk", NULL),
285};
286
287
288/* example machine audio_mapnections */
289static const struct snd_soc_dapm_route audio_map[] = {
290
291    /* Connections to the lm4853 amp */
292    {"Stereo Out", NULL, "LOUT1"},
293    {"Stereo Out", NULL, "ROUT1"},
294
295    /* Connections to the GSM Module */
296    {"GSM Line Out", NULL, "MONO1"},
297    {"GSM Line Out", NULL, "MONO2"},
298    {"RXP", NULL, "GSM Line In"},
299    {"RXN", NULL, "GSM Line In"},
300
301    /* Connections to Headset */
302    {"MIC1", NULL, "Mic Bias"},
303    {"Mic Bias", NULL, "Headset Mic"},
304
305    /* Call Mic */
306    {"MIC2", NULL, "Mic Bias"},
307    {"MIC2N", NULL, "Mic Bias"},
308    {"Mic Bias", NULL, "Handset Mic"},
309
310    /* Call Speaker */
311    {"Handset Spk", NULL, "LOUT2"},
312    {"Handset Spk", NULL, "ROUT2"},
313
314    /* Connect the ALC pins */
315    {"ACIN", NULL, "ACOP"},
316};
317
318static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
319    SOC_DAPM_PIN_SWITCH("Stereo Out"),
320    SOC_DAPM_PIN_SWITCH("GSM Line Out"),
321    SOC_DAPM_PIN_SWITCH("GSM Line In"),
322    SOC_DAPM_PIN_SWITCH("Headset Mic"),
323    SOC_DAPM_PIN_SWITCH("Handset Mic"),
324    SOC_DAPM_PIN_SWITCH("Handset Spk"),
325
326    /* This has no effect, it exists only to maintain compatibility with
327     * existing ALSA state files.
328     */
329    SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
330        lm4853_get_state,
331        lm4853_set_state),
332    SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
333        lm4853_get_spk,
334        lm4853_set_spk),
335};
336
337static struct snd_soc_jack_pin gta02_hs_jack_pins[] = {
338    {
339        .pin = "Headset Mic",
340        .mask = SND_JACK_MICROPHONE
341    },
342    {
343        .pin = "Stereo Out",
344        .mask = SND_JACK_HEADPHONE,
345        .invert = 1,
346    },
347};
348
349static struct snd_soc_jack_gpio gta02_hs_jack_gpios[] = {
350    {
351        .gpio = GTA02_GPIO_JACK_INSERT,
352        .name = "headset-gpio",
353        .report = SND_JACK_HEADSET,
354        .debounce_time = 100,
355    },
356};
357
358/*
359 * This is an example machine initialisation for a wm8753 connected to a
360 * neo1973 GTA02.
361 */
362static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
363{
364    int err;
365
366    /* set up NC codec pins */
367    snd_soc_dapm_nc_pin(codec, "OUT3");
368    snd_soc_dapm_nc_pin(codec, "OUT4");
369    snd_soc_dapm_nc_pin(codec, "LINE1");
370    snd_soc_dapm_nc_pin(codec, "LINE2");
371
372    /* Add neo1973 gta02 specific widgets */
373    snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
374                  ARRAY_SIZE(wm8753_dapm_widgets));
375
376    /* add neo1973 gta02 specific controls */
377    err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
378        ARRAY_SIZE(wm8753_neo1973_gta02_controls));
379
380    if (err < 0)
381        return err;
382
383    /* set up neo1973 gta02 specific audio path audio_map */
384    snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
385
386    /* set endpoints to default off mode */
387    snd_soc_dapm_disable_pin(codec, "Stereo Out");
388    snd_soc_dapm_disable_pin(codec, "GSM Line Out");
389    snd_soc_dapm_disable_pin(codec, "GSM Line In");
390    snd_soc_dapm_disable_pin(codec, "Headset Mic");
391    snd_soc_dapm_disable_pin(codec, "Handset Mic");
392    snd_soc_dapm_disable_pin(codec, "Handset Spk");
393
394    snd_soc_dapm_sync(codec);
395
396    err = snd_soc_jack_new(&neo1973_gta02, "Headset Jack",
397                    SND_JACK_HEADSET, &gta02_hs_jack);
398    if (err) {
399        dev_err(codec->card->dev, "failed to alloc headset jack\n");
400        return err;
401    }
402
403    err = snd_soc_jack_add_pins(&gta02_hs_jack, ARRAY_SIZE(gta02_hs_jack_pins),
404                    gta02_hs_jack_pins);
405    if (err) {
406        dev_err(codec->card->dev, "failed to add headset jack pins\n");
407        return err;
408    }
409
410    err = snd_soc_jack_add_gpios(&gta02_hs_jack, ARRAY_SIZE(gta02_hs_jack_gpios),
411                    gta02_hs_jack_gpios);
412    if (err) {
413        dev_err(codec->card->dev, "failed to add headset jack gpios\n");
414        return err;
415    }
416
417
418    return 0;
419}
420
421/*
422 * BT Codec DAI
423 */
424static struct snd_soc_dai bt_dai = {
425    .name = "Bluetooth",
426    .id = 0,
427    .playback = {
428        .channels_min = 1,
429        .channels_max = 1,
430        .rates = SNDRV_PCM_RATE_8000,
431        .formats = SNDRV_PCM_FMTBIT_S16_LE,},
432    .capture = {
433        .channels_min = 1,
434        .channels_max = 1,
435        .rates = SNDRV_PCM_RATE_8000,
436        .formats = SNDRV_PCM_FMTBIT_S16_LE,},
437};
438
439static struct snd_soc_dai_link neo1973_gta02_dai[] = {
440{ /* Hifi Playback - for similatious use with voice below */
441    .name = "WM8753",
442    .stream_name = "WM8753 HiFi",
443    .cpu_dai = &s3c24xx_i2s_dai,
444    .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
445    .init = neo1973_gta02_wm8753_init,
446    .ops = &neo1973_gta02_hifi_ops,
447},
448{ /* Voice via BT */
449    .name = "Bluetooth",
450    .stream_name = "Voice",
451    .cpu_dai = &bt_dai,
452    .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
453    .ops = &neo1973_gta02_voice_ops,
454},
455};
456
457static struct snd_soc_card neo1973_gta02 = {
458    .name = "neo1973-gta02",
459    .platform = &s3c24xx_soc_platform,
460    .dai_link = neo1973_gta02_dai,
461    .num_links = ARRAY_SIZE(neo1973_gta02_dai),
462};
463
464static struct snd_soc_device neo1973_gta02_snd_devdata = {
465    .card = &neo1973_gta02,
466    .codec_dev = &soc_codec_dev_wm8753,
467};
468
469static struct platform_device *neo1973_gta02_snd_device;
470
471
472static int __init neo1973_gta02_init(void)
473{
474    int ret;
475
476    if (!machine_is_neo1973_gta02()) {
477        printk(KERN_INFO
478               "Only GTA02 is supported by this ASoC driver\n");
479        return -ENODEV;
480    }
481
482    /* register bluetooth DAI here */
483    ret = snd_soc_register_dai(&bt_dai);
484    if (ret)
485        return ret;
486
487    neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
488    if (!neo1973_gta02_snd_device) {
489        ret = -ENOMEM;
490        goto err_unregister_dai;
491    }
492
493    platform_set_drvdata(neo1973_gta02_snd_device,
494            &neo1973_gta02_snd_devdata);
495    neo1973_gta02_snd_devdata.dev = &neo1973_gta02_snd_device->dev;
496    ret = platform_device_add(neo1973_gta02_snd_device);
497
498    if (ret) {
499        platform_device_put(neo1973_gta02_snd_device);
500        goto err_unregister_dai;
501    }
502
503    /* Initialise GPIOs used by amp */
504    s3c2410_gpio_cfgpin(GTA02_GPIO_HP_IN, S3C2410_GPIO_OUTPUT);
505    s3c2410_gpio_cfgpin(GTA02_GPIO_AMP_SHUT, S3C2410_GPIO_OUTPUT);
506
507    /* Amp off by default */
508    s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
509
510    /* Speaker off by default */
511    s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
512
513
514    return 0;
515
516err_unregister_dai:
517    snd_soc_unregister_dai(&bt_dai);
518    return ret;
519}
520module_init(neo1973_gta02_init);
521
522static void __exit neo1973_gta02_exit(void)
523{
524    snd_soc_unregister_dai(&bt_dai);
525    snd_soc_jack_free_gpios(&gta02_hs_jack, ARRAY_SIZE(gta02_hs_jack_gpios),
526            gta02_hs_jack_gpios);
527
528    platform_device_unregister(neo1973_gta02_snd_device);
529}
530module_exit(neo1973_gta02_exit);
531
532/* Module information */
533MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
534MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
535MODULE_LICENSE("GPL");
536

Archive Download this file



interactive