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