Merge branch 'for-2.6.37' into for-2.6.38
[cascardo/linux.git] / sound / soc / pxa / e800_wm9712.c
1 /*
2  * e800-wm9712.c  --  SoC audio for e800
3  *
4  * Copyright 2007 (c) Ian Molton <spyro@f2s.com>
5  *
6  *  This program is free software; you can redistribute  it and/or modify it
7  *  under  the terms of  the GNU General  Public License as published by the
8  *  Free Software Foundation; version 2 ONLY.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/gpio.h>
15
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
20
21 #include <asm/mach-types.h>
22 #include <mach/audio.h>
23 #include <mach/eseries-gpio.h>
24
25 #include "../codecs/wm9712.h"
26 #include "pxa2xx-ac97.h"
27
28 static int e800_spk_amp_event(struct snd_soc_dapm_widget *w,
29                                 struct snd_kcontrol *kcontrol, int event)
30 {
31         if (event & SND_SOC_DAPM_PRE_PMU)
32                 gpio_set_value(GPIO_E800_SPK_AMP_ON, 1);
33         else if (event & SND_SOC_DAPM_POST_PMD)
34                 gpio_set_value(GPIO_E800_SPK_AMP_ON, 0);
35
36         return 0;
37 }
38
39 static int e800_hp_amp_event(struct snd_soc_dapm_widget *w,
40                                 struct snd_kcontrol *kcontrol, int event)
41 {
42         if (event & SND_SOC_DAPM_PRE_PMU)
43                 gpio_set_value(GPIO_E800_HP_AMP_OFF, 0);
44         else if (event & SND_SOC_DAPM_POST_PMD)
45                 gpio_set_value(GPIO_E800_HP_AMP_OFF, 1);
46
47         return 0;
48 }
49
50 static const struct snd_soc_dapm_widget e800_dapm_widgets[] = {
51         SND_SOC_DAPM_HP("Headphone Jack", NULL),
52         SND_SOC_DAPM_MIC("Mic (Internal1)", NULL),
53         SND_SOC_DAPM_MIC("Mic (Internal2)", NULL),
54         SND_SOC_DAPM_SPK("Speaker", NULL),
55         SND_SOC_DAPM_PGA_E("Headphone Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
56                         e800_hp_amp_event, SND_SOC_DAPM_PRE_PMU |
57                         SND_SOC_DAPM_POST_PMD),
58         SND_SOC_DAPM_PGA_E("Speaker Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
59                         e800_spk_amp_event, SND_SOC_DAPM_PRE_PMU |
60                         SND_SOC_DAPM_POST_PMD),
61 };
62
63 static const struct snd_soc_dapm_route audio_map[] = {
64         {"Headphone Jack", NULL, "HPOUTL"},
65         {"Headphone Jack", NULL, "HPOUTR"},
66         {"Headphone Jack", NULL, "Headphone Amp"},
67
68         {"Speaker Amp", NULL, "MONOOUT"},
69         {"Speaker", NULL, "Speaker Amp"},
70
71         {"MIC1", NULL, "Mic (Internal1)"},
72         {"MIC2", NULL, "Mic (Internal2)"},
73 };
74
75 static int e800_ac97_init(struct snd_soc_pcm_runtime *rtd)
76 {
77         struct snd_soc_codec *codec = rtd->codec;
78         struct snd_soc_dapm_context *dapm = &codec->dapm;
79
80         snd_soc_dapm_new_controls(dapm, e800_dapm_widgets,
81                                         ARRAY_SIZE(e800_dapm_widgets));
82
83         snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
84         snd_soc_dapm_sync(dapm);
85
86         return 0;
87 }
88
89 static struct snd_soc_dai_link e800_dai[] = {
90         {
91                 .name = "AC97",
92                 .stream_name = "AC97 HiFi",
93                 .cpu_dai_name = "pxa-ac97.0",
94                 .codec_dai_name = "wm9712-hifi",
95                 .platform_name = "pxa-pcm-audio",
96                 .codec_name = "wm9712-codec",
97                 .init = e800_ac97_init,
98         },
99         {
100                 .name = "AC97 Aux",
101                 .stream_name = "AC97 Aux",
102                 .cpu_dai_name = "pxa-ac97.1",
103                 .codec_dai_name ="wm9712-aux",
104                 .platform_name = "pxa-pcm-audio",
105                 .codec_name = "wm9712-codec",
106         },
107 };
108
109 static struct snd_soc_card e800 = {
110         .name = "Toshiba e800",
111         .dai_link = e800_dai,
112         .num_links = ARRAY_SIZE(e800_dai),
113 };
114
115 static struct platform_device *e800_snd_device;
116
117 static int __init e800_init(void)
118 {
119         int ret;
120
121         if (!machine_is_e800())
122                 return -ENODEV;
123
124         ret = gpio_request(GPIO_E800_HP_AMP_OFF,  "Headphone amp");
125         if (ret)
126                 return ret;
127
128         ret = gpio_request(GPIO_E800_SPK_AMP_ON, "Speaker amp");
129         if (ret)
130                 goto free_hp_amp_gpio;
131
132         ret = gpio_direction_output(GPIO_E800_HP_AMP_OFF, 1);
133         if (ret)
134                 goto free_spk_amp_gpio;
135
136         ret = gpio_direction_output(GPIO_E800_SPK_AMP_ON, 1);
137         if (ret)
138                 goto free_spk_amp_gpio;
139
140         e800_snd_device = platform_device_alloc("soc-audio", -1);
141         if (!e800_snd_device)
142                 return -ENOMEM;
143
144         platform_set_drvdata(e800_snd_device, &e800);
145         ret = platform_device_add(e800_snd_device);
146
147         if (!ret)
148                 return 0;
149
150 /* Fail gracefully */
151         platform_device_put(e800_snd_device);
152 free_spk_amp_gpio:
153         gpio_free(GPIO_E800_SPK_AMP_ON);
154 free_hp_amp_gpio:
155         gpio_free(GPIO_E800_HP_AMP_OFF);
156
157         return ret;
158 }
159
160 static void __exit e800_exit(void)
161 {
162         platform_device_unregister(e800_snd_device);
163         gpio_free(GPIO_E800_SPK_AMP_ON);
164         gpio_free(GPIO_E800_HP_AMP_OFF);
165 }
166
167 module_init(e800_init);
168 module_exit(e800_exit);
169
170 /* Module information */
171 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
172 MODULE_DESCRIPTION("ALSA SoC driver for e800");
173 MODULE_LICENSE("GPL v2");