Add linux-next specific files for 20110831
[linux-2.6/next.git] / sound / soc / samsung / rx1950_uda1380.c
blobbc8c1676459f781b56b28e5fbcadd9ad7732f4a4
1 /*
2 * rx1950.c -- ALSA Soc Audio Layer
4 * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
6 * Based on smdk2440.c and magician.c
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>
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.
20 #include <linux/types.h>
21 #include <linux/gpio.h>
23 #include <sound/soc.h>
24 #include <sound/jack.h>
26 #include <plat/regs-iis.h>
27 #include <asm/mach-types.h>
29 #include "s3c24xx-i2s.h"
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);
38 static unsigned int rates[] = {
39 16000,
40 44100,
41 48000,
44 static struct snd_pcm_hw_constraint_list hw_rates = {
45 .count = ARRAY_SIZE(rates),
46 .list = rates,
47 .mask = 0,
50 static struct snd_soc_jack hp_jack;
52 static struct snd_soc_jack_pin hp_jack_pins[] = {
54 .pin = "Headphone Jack",
55 .mask = SND_JACK_HEADPHONE,
58 .pin = "Speaker",
59 .mask = SND_JACK_HEADPHONE,
60 .invert = 1,
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,
74 static struct snd_soc_ops rx1950_ops = {
75 .startup = rx1950_startup,
76 .hw_params = rx1950_hw_params,
79 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
80 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
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,
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),
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),
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"},
112 /* ext speaker connected to VOUTL, VOUTR */
113 {"Speaker", NULL, "VOUTL"},
114 {"Speaker", NULL, "VOUTR"},
116 /* mic is connected to VINM */
117 {"VINM", NULL, "Mic Jack"},
120 static struct platform_device *s3c24xx_snd_device;
122 static int rx1950_startup(struct snd_pcm_substream *substream)
124 struct snd_pcm_runtime *runtime = substream->runtime;
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;
130 return snd_pcm_hw_constraint_list(runtime, 0,
131 SNDRV_PCM_HW_PARAM_RATE,
132 &hw_rates);
135 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
136 struct snd_kcontrol *kcontrol, int event)
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);
143 return 0;
146 static int rx1950_hw_params(struct snd_pcm_substream *substream,
147 struct snd_pcm_hw_params *params)
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;
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;
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;
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;
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;
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;
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;
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;
214 return 0;
217 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
219 struct snd_soc_codec *codec = rtd->codec;
220 struct snd_soc_dapm_context *dapm = &codec->dapm;
221 int err;
223 /* Add rx1950 specific widgets */
224 err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
225 ARRAY_SIZE(uda1380_dapm_widgets));
227 if (err)
228 return err;
230 /* Set up rx1950 specific audio path audio_mapnects */
231 err = snd_soc_dapm_add_routes(dapm, audio_map,
232 ARRAY_SIZE(audio_map));
234 if (err)
235 return err;
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");
241 snd_soc_dapm_sync(dapm);
243 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
244 &hp_jack);
246 snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
247 hp_jack_pins);
249 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
250 hp_jack_gpios);
252 return 0;
255 static int __init rx1950_init(void)
257 int ret;
259 if (!machine_is_rx1950())
260 return -ENODEV;
262 /* configure some gpios */
263 ret = gpio_request(S3C2410_GPA(1), "speaker-power");
264 if (ret)
265 goto err_gpio;
267 ret = gpio_direction_output(S3C2410_GPA(1), 0);
268 if (ret)
269 goto err_gpio_conf;
271 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
272 if (!s3c24xx_snd_device) {
273 ret = -ENOMEM;
274 goto err_plat_alloc;
277 platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
278 ret = platform_device_add(s3c24xx_snd_device);
280 if (ret) {
281 platform_device_put(s3c24xx_snd_device);
282 goto err_plat_add;
285 return 0;
287 err_plat_add:
288 err_plat_alloc:
289 err_gpio_conf:
290 gpio_free(S3C2410_GPA(1));
292 err_gpio:
293 return ret;
296 static void __exit rx1950_exit(void)
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));
304 module_init(rx1950_init);
305 module_exit(rx1950_exit);
307 /* Module information */
308 MODULE_AUTHOR("Vasily Khoruzhick");
309 MODULE_DESCRIPTION("ALSA SoC RX1950");
310 MODULE_LICENSE("GPL");