ASoC: machine driver for Toshiba e750
[safe/jmp/linux-2.6] / sound / soc / pxa / e750_wm9705.c
1 /*
2  * e750-wm9705.c  --  SoC audio for e750
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 <mach/pxa-regs.h>
22 #include <mach/hardware.h>
23 #include <mach/audio.h>
24 #include <mach/eseries-gpio.h>
25
26 #include <asm/mach-types.h>
27
28 #include "../codecs/wm9705.h"
29 #include "pxa2xx-pcm.h"
30 #include "pxa2xx-ac97.h"
31
32 static int e750_spk_amp_event(struct snd_soc_dapm_widget *w,
33                                 struct snd_kcontrol *kcontrol, int event)
34 {
35         if (event & SND_SOC_DAPM_PRE_PMU)
36                 gpio_set_value(GPIO_E750_SPK_AMP_OFF, 0);
37         else if (event & SND_SOC_DAPM_POST_PMD)
38                 gpio_set_value(GPIO_E750_SPK_AMP_OFF, 1);
39
40         return 0;
41 }
42
43 static int e750_hp_amp_event(struct snd_soc_dapm_widget *w,
44                                 struct snd_kcontrol *kcontrol, int event)
45 {
46         if (event & SND_SOC_DAPM_PRE_PMU)
47                 gpio_set_value(GPIO_E750_HP_AMP_OFF, 0);
48         else if (event & SND_SOC_DAPM_POST_PMD)
49                 gpio_set_value(GPIO_E750_HP_AMP_OFF, 1);
50
51         return 0;
52 }
53
54 static const struct snd_soc_dapm_widget e750_dapm_widgets[] = {
55         SND_SOC_DAPM_HP("Headphone Jack", NULL),
56         SND_SOC_DAPM_SPK("Speaker", NULL),
57         SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
58         SND_SOC_DAPM_PGA_E("Headphone Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
59                         e750_hp_amp_event, SND_SOC_DAPM_PRE_PMU |
60                         SND_SOC_DAPM_POST_PMD),
61         SND_SOC_DAPM_PGA_E("Speaker Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
62                         e750_spk_amp_event, SND_SOC_DAPM_PRE_PMU |
63                         SND_SOC_DAPM_POST_PMD),
64 };
65
66 static const struct snd_soc_dapm_route audio_map[] = {
67         {"Headphone Amp", NULL, "HPOUTL"},
68         {"Headphone Amp", NULL, "HPOUTR"},
69         {"Headphone Jack", NULL, "Headphone Amp"},
70
71         {"Speaker Amp", NULL, "MONOOUT"},
72         {"Speaker", NULL, "Speaker Amp"},
73
74         {"MIC1", NULL, "Mic (Internal)"},
75 };
76
77 static int e750_ac97_init(struct snd_soc_codec *codec)
78 {
79         snd_soc_dapm_nc_pin(codec, "LOUT");
80         snd_soc_dapm_nc_pin(codec, "ROUT");
81         snd_soc_dapm_nc_pin(codec, "PHONE");
82         snd_soc_dapm_nc_pin(codec, "LINEINL");
83         snd_soc_dapm_nc_pin(codec, "LINEINR");
84         snd_soc_dapm_nc_pin(codec, "CDINL");
85         snd_soc_dapm_nc_pin(codec, "CDINR");
86         snd_soc_dapm_nc_pin(codec, "PCBEEP");
87         snd_soc_dapm_nc_pin(codec, "MIC2");
88
89         snd_soc_dapm_new_controls(codec, e750_dapm_widgets,
90                                         ARRAY_SIZE(e750_dapm_widgets));
91
92         snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
93
94         snd_soc_dapm_sync(codec);
95
96         return 0;
97 }
98
99 static struct snd_soc_dai_link e750_dai[] = {
100         {
101                 .name = "AC97",
102                 .stream_name = "AC97 HiFi",
103                 .cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_HIFI],
104                 .codec_dai = &wm9705_dai[WM9705_DAI_AC97_HIFI],
105                 .init = e750_ac97_init,
106                 /* use ops to check startup state */
107         },
108         {
109                 .name = "AC97 Aux",
110                 .stream_name = "AC97 Aux",
111                 .cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_AUX],
112                 .codec_dai = &wm9705_dai[WM9705_DAI_AC97_AUX],
113         },
114 };
115
116 static struct snd_soc_card e750 = {
117         .name = "Toshiba e750",
118         .platform = &pxa2xx_soc_platform,
119         .dai_link = e750_dai,
120         .num_links = ARRAY_SIZE(e750_dai),
121 };
122
123 static struct snd_soc_device e750_snd_devdata = {
124         .card = &e750,
125         .codec_dev = &soc_codec_dev_wm9705,
126 };
127
128 static struct platform_device *e750_snd_device;
129
130 static int __init e750_init(void)
131 {
132         int ret;
133
134         if (!machine_is_e750())
135                 return -ENODEV;
136
137         ret = gpio_request(GPIO_E750_HP_AMP_OFF,  "Headphone amp");
138         if (ret)
139                 return ret;
140
141         ret = gpio_request(GPIO_E750_SPK_AMP_OFF, "Speaker amp");
142         if (ret)
143                 goto free_hp_amp_gpio;
144
145         ret = gpio_direction_output(GPIO_E750_HP_AMP_OFF, 1);
146         if (ret)
147                 goto free_spk_amp_gpio;
148
149         ret = gpio_direction_output(GPIO_E750_SPK_AMP_OFF, 1);
150         if (ret)
151                 goto free_spk_amp_gpio;
152
153         e750_snd_device = platform_device_alloc("soc-audio", -1);
154         if (!e750_snd_device) {
155                 ret = -ENOMEM;
156                 goto free_spk_amp_gpio;
157         }
158
159         platform_set_drvdata(e750_snd_device, &e750_snd_devdata);
160         e750_snd_devdata.dev = &e750_snd_device->dev;
161         ret = platform_device_add(e750_snd_device);
162
163         if (!ret)
164                 return 0;
165
166 /* Fail gracefully */
167         platform_device_put(e750_snd_device);
168 free_spk_amp_gpio:
169         gpio_free(GPIO_E750_SPK_AMP_OFF);
170 free_hp_amp_gpio:
171         gpio_free(GPIO_E750_HP_AMP_OFF);
172
173         return ret;
174 }
175
176 static void __exit e750_exit(void)
177 {
178         platform_device_unregister(e750_snd_device);
179         gpio_free(GPIO_E750_SPK_AMP_OFF);
180         gpio_free(GPIO_E750_HP_AMP_OFF);
181 }
182
183 module_init(e750_init);
184 module_exit(e750_exit);
185
186 /* Module information */
187 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
188 MODULE_DESCRIPTION("ALSA SoC driver for e750");
189 MODULE_LICENSE("GPL v2");