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