Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / sound / soc / samsung / h1940_uda1380.c
1 /*
2  * h1940-uda1380.c  --  ALSA Soc Audio Layer
3  *
4  * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
5  * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
6  *
7  * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  *
14  */
15
16 #include <linux/types.h>
17 #include <linux/gpio.h>
18
19 #include <sound/soc.h>
20 #include <sound/jack.h>
21
22 #include <plat/regs-iis.h>
23 #include <mach/h1940-latch.h>
24 #include <asm/mach-types.h>
25
26 #include "s3c24xx-i2s.h"
27
28 static unsigned int rates[] = {
29         11025,
30         22050,
31         44100,
32 };
33
34 static struct snd_pcm_hw_constraint_list hw_rates = {
35         .count = ARRAY_SIZE(rates),
36         .list = rates,
37         .mask = 0,
38 };
39
40 static struct snd_soc_jack hp_jack;
41
42 static struct snd_soc_jack_pin hp_jack_pins[] = {
43         {
44                 .pin    = "Headphone Jack",
45                 .mask   = SND_JACK_HEADPHONE,
46         },
47         {
48                 .pin    = "Speaker",
49                 .mask   = SND_JACK_HEADPHONE,
50                 .invert = 1,
51         },
52 };
53
54 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
55         {
56                 .gpio                   = S3C2410_GPG(4),
57                 .name                   = "hp-gpio",
58                 .report                 = SND_JACK_HEADPHONE,
59                 .invert                 = 1,
60                 .debounce_time          = 200,
61         },
62 };
63
64 static int h1940_startup(struct snd_pcm_substream *substream)
65 {
66         struct snd_pcm_runtime *runtime = substream->runtime;
67
68         runtime->hw.rate_min = hw_rates.list[0];
69         runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
70         runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
71
72         return snd_pcm_hw_constraint_list(runtime, 0,
73                                         SNDRV_PCM_HW_PARAM_RATE,
74                                         &hw_rates);
75 }
76
77 static int h1940_hw_params(struct snd_pcm_substream *substream,
78                                 struct snd_pcm_hw_params *params)
79 {
80         struct snd_soc_pcm_runtime *rtd = substream->private_data;
81         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
82         struct snd_soc_dai *codec_dai = rtd->codec_dai;
83         int div;
84         int ret;
85         unsigned int rate = params_rate(params);
86
87         switch (rate) {
88         case 11025:
89         case 22050:
90         case 44100:
91                 div = s3c24xx_i2s_get_clockrate() / (384 * rate);
92                 if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
93                         div++;
94                 break;
95         default:
96                 dev_err(&rtd->dev, "%s: rate %d is not supported\n",
97                         __func__, rate);
98                 return -EINVAL;
99         }
100
101         /* set codec DAI configuration */
102         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
103                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
104         if (ret < 0)
105                 return ret;
106
107         /* set cpu DAI configuration */
108         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
109                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
110         if (ret < 0)
111                 return ret;
112
113         /* select clock source */
114         ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
115                         SND_SOC_CLOCK_OUT);
116         if (ret < 0)
117                 return ret;
118
119         /* set MCLK division for sample rate */
120         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
121                 S3C2410_IISMOD_384FS);
122         if (ret < 0)
123                 return ret;
124
125         /* set BCLK division for sample rate */
126         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
127                 S3C2410_IISMOD_32FS);
128         if (ret < 0)
129                 return ret;
130
131         /* set prescaler division for sample rate */
132         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
133                 S3C24XX_PRESCALE(div, div));
134         if (ret < 0)
135                 return ret;
136
137         return 0;
138 }
139
140 static struct snd_soc_ops h1940_ops = {
141         .startup        = h1940_startup,
142         .hw_params      = h1940_hw_params,
143 };
144
145 static int h1940_spk_power(struct snd_soc_dapm_widget *w,
146                                 struct snd_kcontrol *kcontrol, int event)
147 {
148         if (SND_SOC_DAPM_EVENT_ON(event))
149                 gpio_set_value(H1940_LATCH_AUDIO_POWER, 1);
150         else
151                 gpio_set_value(H1940_LATCH_AUDIO_POWER, 0);
152
153         return 0;
154 }
155
156 /* h1940 machine dapm widgets */
157 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
158         SND_SOC_DAPM_HP("Headphone Jack", NULL),
159         SND_SOC_DAPM_MIC("Mic Jack", NULL),
160         SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
161 };
162
163 /* h1940 machine audio_map */
164 static const struct snd_soc_dapm_route audio_map[] = {
165         /* headphone connected to VOUTLHP, VOUTRHP */
166         {"Headphone Jack", NULL, "VOUTLHP"},
167         {"Headphone Jack", NULL, "VOUTRHP"},
168
169         /* ext speaker connected to VOUTL, VOUTR  */
170         {"Speaker", NULL, "VOUTL"},
171         {"Speaker", NULL, "VOUTR"},
172
173         /* mic is connected to VINM */
174         {"VINM", NULL, "Mic Jack"},
175 };
176
177 static struct platform_device *s3c24xx_snd_device;
178
179 static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
180 {
181         struct snd_soc_codec *codec = rtd->codec;
182         struct snd_soc_dapm_context *dapm = &codec->dapm;
183         int err;
184
185         /* Add h1940 specific widgets */
186         err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
187                                   ARRAY_SIZE(uda1380_dapm_widgets));
188         if (err)
189                 return err;
190
191         /* Set up h1940 specific audio path audio_mapnects */
192         err = snd_soc_dapm_add_routes(dapm, audio_map,
193                                       ARRAY_SIZE(audio_map));
194         if (err)
195                 return err;
196
197         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
198         snd_soc_dapm_enable_pin(dapm, "Speaker");
199         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
200
201         snd_soc_dapm_sync(dapm);
202
203         snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
204                 &hp_jack);
205
206         snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
207                 hp_jack_pins);
208
209         snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
210                 hp_jack_gpios);
211
212         return 0;
213 }
214
215 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
216 static struct snd_soc_dai_link h1940_uda1380_dai[] = {
217         {
218                 .name           = "uda1380",
219                 .stream_name    = "UDA1380 Duplex",
220                 .cpu_dai_name   = "s3c24xx-iis",
221                 .codec_dai_name = "uda1380-hifi",
222                 .init           = h1940_uda1380_init,
223                 .platform_name  = "samsung-audio",
224                 .codec_name     = "uda1380-codec.0-001a",
225                 .ops            = &h1940_ops,
226         },
227 };
228
229 static struct snd_soc_card h1940_asoc = {
230         .name = "h1940",
231         .dai_link = h1940_uda1380_dai,
232         .num_links = ARRAY_SIZE(h1940_uda1380_dai),
233 };
234
235 static int __init h1940_init(void)
236 {
237         int ret;
238
239         if (!machine_is_h1940())
240                 return -ENODEV;
241
242         /* configure some gpios */
243         ret = gpio_request(H1940_LATCH_AUDIO_POWER, "speaker-power");
244         if (ret)
245                 goto err_out;
246
247         ret = gpio_direction_output(H1940_LATCH_AUDIO_POWER, 0);
248         if (ret)
249                 goto err_gpio;
250
251         s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
252         if (!s3c24xx_snd_device) {
253                 ret = -ENOMEM;
254                 goto err_gpio;
255         }
256
257         platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
258         ret = platform_device_add(s3c24xx_snd_device);
259
260         if (ret)
261                 goto err_plat;
262
263         return 0;
264
265 err_plat:
266         platform_device_put(s3c24xx_snd_device);
267 err_gpio:
268         gpio_free(H1940_LATCH_AUDIO_POWER);
269
270 err_out:
271         return ret;
272 }
273
274 static void __exit h1940_exit(void)
275 {
276         platform_device_unregister(s3c24xx_snd_device);
277         snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
278                 hp_jack_gpios);
279         gpio_free(H1940_LATCH_AUDIO_POWER);
280 }
281
282 module_init(h1940_init);
283 module_exit(h1940_exit);
284
285 /* Module information */
286 MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
287 MODULE_DESCRIPTION("ALSA SoC H1940");
288 MODULE_LICENSE("GPL");