ASoC: Add HP iPAQ RX1950 support
[pandora-kernel.git] / sound / soc / s3c24xx / 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/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/timer.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/spinlock.h>
27 #include <linux/i2c.h>
28 #include <linux/gpio.h>
29 #include <linux/clk.h>
30
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/soc-dapm.h>
36 #include <sound/uda1380.h>
37 #include <sound/jack.h>
38
39 #include <plat/regs-iis.h>
40
41 #include <mach/regs-clock.h>
42
43 #include "s3c-dma.h"
44 #include "s3c24xx-i2s.h"
45 #include "../codecs/uda1380.h"
46
47 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
48 static int rx1950_startup(struct snd_pcm_substream *substream);
49 static int rx1950_hw_params(struct snd_pcm_substream *substream,
50                                 struct snd_pcm_hw_params *params);
51 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
52                                 struct snd_kcontrol *kcontrol, int event);
53
54 static unsigned int rates[] = {
55         16000,
56         44100,
57         48000,
58         88200,
59 };
60
61 static struct snd_pcm_hw_constraint_list hw_rates = {
62         .count = ARRAY_SIZE(rates),
63         .list = rates,
64         .mask = 0,
65 };
66
67 static struct snd_soc_jack hp_jack;
68
69 static struct snd_soc_jack_pin hp_jack_pins[] = {
70         {
71                 .pin    = "Headphone Jack",
72                 .mask   = SND_JACK_HEADPHONE,
73         },
74         {
75                 .pin    = "Speaker",
76                 .mask   = SND_JACK_HEADPHONE,
77                 .invert = 1,
78         },
79 };
80
81 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
82         [0] = {
83                 .gpio                   = S3C2410_GPG(12),
84                 .name                   = "hp-gpio",
85                 .report                 = SND_JACK_HEADPHONE,
86                 .invert                 = 1,
87                 .debounce_time          = 200,
88         },
89 };
90
91 static struct snd_soc_ops rx1950_ops = {
92         .startup        = rx1950_startup,
93         .hw_params      = rx1950_hw_params,
94 };
95
96 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
97 static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
98         {
99                 .name           = "uda1380",
100                 .stream_name    = "UDA1380 Duplex",
101                 .cpu_dai_name   = "s3c24xx-iis",
102                 .codec_dai_name = "uda1380-hifi",
103                 .init           = rx1950_uda1380_init,
104                 .platform_name  = "s3c24xx-pcm-audio",
105                 .codec_name     = "uda1380-codec.0-001a",
106                 .ops            = &rx1950_ops,
107         },
108 };
109
110 static struct snd_soc_card rx1950_asoc = {
111         .name = "rx1950",
112         .dai_link = rx1950_uda1380_dai,
113         .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
114 };
115
116 /* rx1950 machine dapm widgets */
117 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
118         SND_SOC_DAPM_HP("Headphone Jack", NULL),
119         SND_SOC_DAPM_MIC("Mic Jack", NULL),
120         SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
121 };
122
123 /* rx1950 machine audio_map */
124 static const struct snd_soc_dapm_route audio_map[] = {
125         /* headphone connected to VOUTLHP, VOUTRHP */
126         {"Headphone Jack", NULL, "VOUTLHP"},
127         {"Headphone Jack", NULL, "VOUTRHP"},
128
129         /* ext speaker connected to VOUTL, VOUTR  */
130         {"Speaker", NULL, "VOUTL"},
131         {"Speaker", NULL, "VOUTR"},
132
133         /* mic is connected to VINM */
134         {"VINM", NULL, "Mic Jack"},
135 };
136
137 static struct platform_device *s3c24xx_snd_device;
138 static struct clk *xtal;
139
140 static int rx1950_startup(struct snd_pcm_substream *substream)
141 {
142         struct snd_pcm_runtime *runtime = substream->runtime;
143
144         runtime->hw.rate_min = hw_rates.list[0];
145         runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
146         runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
147
148         return snd_pcm_hw_constraint_list(runtime, 0,
149                                         SNDRV_PCM_HW_PARAM_RATE,
150                                         &hw_rates);
151 }
152
153 static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
154                                 struct snd_kcontrol *kcontrol, int event)
155 {
156         if (SND_SOC_DAPM_EVENT_ON(event))
157                 gpio_set_value(S3C2410_GPA(1), 1);
158         else
159                 gpio_set_value(S3C2410_GPA(1), 0);
160
161         return 0;
162 }
163
164 static int rx1950_hw_params(struct snd_pcm_substream *substream,
165                                 struct snd_pcm_hw_params *params)
166 {
167         struct snd_soc_pcm_runtime *rtd = substream->private_data;
168         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
169         struct snd_soc_dai *codec_dai = rtd->codec_dai;
170         int div;
171         int ret;
172         unsigned int rate = params_rate(params);
173         int clk_source, fs_mode;
174
175         switch (rate) {
176         case 16000:
177         case 48000:
178                 clk_source = S3C24XX_CLKSRC_PCLK;
179                 fs_mode = S3C2410_IISMOD_384FS;
180                 div = s3c24xx_i2s_get_clockrate() / (384 * rate);
181                 if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (182 * rate))
182                         div++;
183                 break;
184         case 44100:
185         case 88200:
186                 clk_source = S3C24XX_CLKSRC_MPLL;
187                 fs_mode = S3C2410_IISMOD_256FS;
188                 div = clk_get_rate(xtal) / (256 * rate);
189                 if (clk_get_rate(xtal) % (256 * rate) > (128 * rate))
190                         div++;
191                 break;
192         default:
193                 printk(KERN_ERR "%s: rate %d is not supported\n",
194                         __func__, rate);
195                 return -EINVAL;
196         }
197
198         /* set codec DAI configuration */
199         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
200                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
201         if (ret < 0)
202                 return ret;
203
204         /* set cpu DAI configuration */
205         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
206                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
207         if (ret < 0)
208                 return ret;
209
210         /* select clock source */
211         ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
212                         SND_SOC_CLOCK_OUT);
213         if (ret < 0)
214                 return ret;
215
216         /* set MCLK division for sample rate */
217         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
218                 S3C2410_IISMOD_384FS);
219         if (ret < 0)
220                 return ret;
221
222         /* set BCLK division for sample rate */
223         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
224                 S3C2410_IISMOD_32FS);
225         if (ret < 0)
226                 return ret;
227
228         /* set prescaler division for sample rate */
229         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
230                 S3C24XX_PRESCALE(div, div));
231         if (ret < 0)
232                 return ret;
233
234         return 0;
235 }
236
237 static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
238 {
239         struct snd_soc_codec *codec = rtd->codec;
240         int err;
241
242         /* Add rx1950 specific widgets */
243         err = snd_soc_dapm_new_controls(codec, uda1380_dapm_widgets,
244                                   ARRAY_SIZE(uda1380_dapm_widgets));
245
246         if (err)
247                 return err;
248
249         /* Set up rx1950 specific audio path audio_mapnects */
250         err = snd_soc_dapm_add_routes(codec, audio_map,
251                                       ARRAY_SIZE(audio_map));
252
253         if (err)
254                 return err;
255
256         snd_soc_dapm_enable_pin(codec, "Headphone Jack");
257         snd_soc_dapm_enable_pin(codec, "Speaker");
258
259         snd_soc_dapm_sync(codec);
260
261         snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
262                 &hp_jack);
263
264         snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
265                 hp_jack_pins);
266
267         snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
268                 hp_jack_gpios);
269
270         return 0;
271 }
272
273 static int __init rx1950_init(void)
274 {
275         int ret;
276
277         /* configure some gpios */
278         ret = gpio_request(S3C2410_GPA(1), "speaker-power");
279         if (ret)
280                 goto err_gpio;
281
282         ret = gpio_direction_output(S3C2410_GPA(1), 0);
283         if (ret)
284                 goto err_gpio_conf;
285
286         s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
287         if (!s3c24xx_snd_device) {
288                 ret = -ENOMEM;
289                 goto err_plat_alloc;
290         }
291
292         platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
293         ret = platform_device_add(s3c24xx_snd_device);
294
295         if (ret) {
296                 platform_device_put(s3c24xx_snd_device);
297                 goto err_plat_add;
298         }
299
300         xtal = clk_get(&s3c24xx_snd_device->dev, "xtal");
301
302         if (IS_ERR(xtal)) {
303                 ret = PTR_ERR(xtal);
304                 platform_device_unregister(s3c24xx_snd_device);
305                 goto err_clk;
306         }
307
308         return 0;
309
310 err_clk:
311 err_plat_add:
312 err_plat_alloc:
313 err_gpio_conf:
314         gpio_free(S3C2410_GPA(1));
315
316 err_gpio:
317         return ret;
318 }
319
320 static void __exit rx1950_exit(void)
321 {
322         platform_device_unregister(s3c24xx_snd_device);
323         snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
324                 hp_jack_gpios);
325         clk_put(xtal);
326         gpio_free(S3C2410_GPA(1));
327 }
328
329 module_init(rx1950_init);
330 module_exit(rx1950_exit);
331
332 /* Module information */
333 MODULE_AUTHOR("Vasily Khoruzhick");
334 MODULE_DESCRIPTION("ALSA SoC RX1950");
335 MODULE_LICENSE("GPL");