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