blob: aea7f1b24e6be5eda3ce138891a1928f649a9529 [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
Vasily Khoruzhickb8487922011-08-12 17:53:00 +030020#include <linux/types.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030021#include <linux/gpio.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030022
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030023#include <sound/soc.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030024#include <sound/jack.h>
25
26#include <plat/regs-iis.h>
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +030027#include <asm/mach-types.h>
28
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030029#include "s3c24xx-i2s.h"
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030030
31static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
32static int rx1950_startup(struct snd_pcm_substream *substream);
33static int rx1950_hw_params(struct snd_pcm_substream *substream,
34 struct snd_pcm_hw_params *params);
35static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
36 struct snd_kcontrol *kcontrol, int event);
37
38static unsigned int rates[] = {
39 16000,
40 44100,
41 48000,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030042};
43
44static struct snd_pcm_hw_constraint_list hw_rates = {
45 .count = ARRAY_SIZE(rates),
46 .list = rates,
47 .mask = 0,
48};
49
50static struct snd_soc_jack hp_jack;
51
52static 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
64static 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
74static 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 */
80static 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,
Jassi Brar58bb4072010-11-22 15:35:50 +090087 .platform_name = "samsung-audio",
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030088 .codec_name = "uda1380-codec.0-001a",
89 .ops = &rx1950_ops,
90 },
91};
92
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030093/* rx1950 machine dapm widgets */
94static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
95 SND_SOC_DAPM_HP("Headphone Jack", NULL),
96 SND_SOC_DAPM_MIC("Mic Jack", NULL),
97 SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
98};
99
100/* rx1950 machine audio_map */
101static const struct snd_soc_dapm_route audio_map[] = {
102 /* headphone connected to VOUTLHP, VOUTRHP */
103 {"Headphone Jack", NULL, "VOUTLHP"},
104 {"Headphone Jack", NULL, "VOUTRHP"},
105
106 /* ext speaker connected to VOUTL, VOUTR */
107 {"Speaker", NULL, "VOUTL"},
108 {"Speaker", NULL, "VOUTR"},
109
110 /* mic is connected to VINM */
111 {"VINM", NULL, "Mic Jack"},
112};
113
Mark Brown8ae23222011-10-08 13:30:35 +0100114static struct snd_soc_card rx1950_asoc = {
115 .name = "rx1950",
116 .dai_link = rx1950_uda1380_dai,
117 .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
118
119 .dapm_widgets = uda1380_dapm_widgets,
120 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
121 .dapm_routes = audio_map,
122 .num_dapm_routes = ARRAY_SIZE(audio_map),
123};
124
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300125static struct platform_device *s3c24xx_snd_device;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300126
127static int rx1950_startup(struct snd_pcm_substream *substream)
128{
129 struct snd_pcm_runtime *runtime = substream->runtime;
130
131 runtime->hw.rate_min = hw_rates.list[0];
132 runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
133 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
134
135 return snd_pcm_hw_constraint_list(runtime, 0,
136 SNDRV_PCM_HW_PARAM_RATE,
137 &hw_rates);
138}
139
140static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
141 struct snd_kcontrol *kcontrol, int event)
142{
143 if (SND_SOC_DAPM_EVENT_ON(event))
144 gpio_set_value(S3C2410_GPA(1), 1);
145 else
146 gpio_set_value(S3C2410_GPA(1), 0);
147
148 return 0;
149}
150
151static int rx1950_hw_params(struct snd_pcm_substream *substream,
152 struct snd_pcm_hw_params *params)
153{
154 struct snd_soc_pcm_runtime *rtd = substream->private_data;
155 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
156 struct snd_soc_dai *codec_dai = rtd->codec_dai;
157 int div;
158 int ret;
159 unsigned int rate = params_rate(params);
160 int clk_source, fs_mode;
161
162 switch (rate) {
163 case 16000:
164 case 48000:
165 clk_source = S3C24XX_CLKSRC_PCLK;
Vasily Khoruzhick1fdc7dd2010-09-07 17:04:18 +0300166 fs_mode = S3C2410_IISMOD_256FS;
167 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
168 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300169 div++;
170 break;
171 case 44100:
172 case 88200:
173 clk_source = S3C24XX_CLKSRC_MPLL;
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200174 fs_mode = S3C2410_IISMOD_384FS;
175 div = 1;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300176 break;
177 default:
178 printk(KERN_ERR "%s: rate %d is not supported\n",
179 __func__, rate);
180 return -EINVAL;
181 }
182
183 /* set codec DAI configuration */
184 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
185 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
186 if (ret < 0)
187 return ret;
188
189 /* set cpu DAI configuration */
190 ret = snd_soc_dai_set_fmt(cpu_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 /* select clock source */
196 ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
197 SND_SOC_CLOCK_OUT);
198 if (ret < 0)
199 return ret;
200
201 /* set MCLK division for sample rate */
202 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200203 fs_mode);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300204 if (ret < 0)
205 return ret;
206
207 /* set BCLK division for sample rate */
208 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
209 S3C2410_IISMOD_32FS);
210 if (ret < 0)
211 return ret;
212
213 /* set prescaler division for sample rate */
214 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
215 S3C24XX_PRESCALE(div, div));
216 if (ret < 0)
217 return ret;
218
219 return 0;
220}
221
222static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
223{
224 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200225 struct snd_soc_dapm_context *dapm = &codec->dapm;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300226 int err;
227
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200228 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
229 snd_soc_dapm_enable_pin(dapm, "Speaker");
Vasily Khoruzhickb60fc602011-01-06 22:15:49 +0200230 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300231
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300232 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
233 &hp_jack);
234
235 snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
236 hp_jack_pins);
237
238 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
239 hp_jack_gpios);
240
241 return 0;
242}
243
244static int __init rx1950_init(void)
245{
246 int ret;
247
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +0300248 if (!machine_is_rx1950())
249 return -ENODEV;
250
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300251 /* configure some gpios */
252 ret = gpio_request(S3C2410_GPA(1), "speaker-power");
253 if (ret)
254 goto err_gpio;
255
256 ret = gpio_direction_output(S3C2410_GPA(1), 0);
257 if (ret)
258 goto err_gpio_conf;
259
260 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
261 if (!s3c24xx_snd_device) {
262 ret = -ENOMEM;
263 goto err_plat_alloc;
264 }
265
266 platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
267 ret = platform_device_add(s3c24xx_snd_device);
268
269 if (ret) {
270 platform_device_put(s3c24xx_snd_device);
271 goto err_plat_add;
272 }
273
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300274 return 0;
275
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300276err_plat_add:
277err_plat_alloc:
278err_gpio_conf:
279 gpio_free(S3C2410_GPA(1));
280
281err_gpio:
282 return ret;
283}
284
285static void __exit rx1950_exit(void)
286{
287 platform_device_unregister(s3c24xx_snd_device);
288 snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
289 hp_jack_gpios);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300290 gpio_free(S3C2410_GPA(1));
291}
292
293module_init(rx1950_init);
294module_exit(rx1950_exit);
295
296/* Module information */
297MODULE_AUTHOR("Vasily Khoruzhick");
298MODULE_DESCRIPTION("ALSA SoC RX1950");
299MODULE_LICENSE("GPL");