blob: a5826ea9cad63af16d20612155744c42c94320f0 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030023
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030024#include <sound/soc.h>
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030025#include <sound/jack.h>
26
27#include <plat/regs-iis.h>
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +030028#include <asm/mach-types.h>
29
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030030#include "s3c24xx-i2s.h"
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030031
32static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd);
33static int rx1950_startup(struct snd_pcm_substream *substream);
34static int rx1950_hw_params(struct snd_pcm_substream *substream,
35 struct snd_pcm_hw_params *params);
36static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
37 struct snd_kcontrol *kcontrol, int event);
38
39static unsigned int rates[] = {
40 16000,
41 44100,
42 48000,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030043};
44
45static struct snd_pcm_hw_constraint_list hw_rates = {
46 .count = ARRAY_SIZE(rates),
47 .list = rates,
48 .mask = 0,
49};
50
51static struct snd_soc_jack hp_jack;
52
53static struct snd_soc_jack_pin hp_jack_pins[] = {
54 {
55 .pin = "Headphone Jack",
56 .mask = SND_JACK_HEADPHONE,
57 },
58 {
59 .pin = "Speaker",
60 .mask = SND_JACK_HEADPHONE,
61 .invert = 1,
62 },
63};
64
65static struct snd_soc_jack_gpio hp_jack_gpios[] = {
66 [0] = {
67 .gpio = S3C2410_GPG(12),
68 .name = "hp-gpio",
69 .report = SND_JACK_HEADPHONE,
70 .invert = 1,
71 .debounce_time = 200,
72 },
73};
74
75static struct snd_soc_ops rx1950_ops = {
76 .startup = rx1950_startup,
77 .hw_params = rx1950_hw_params,
78};
79
80/* s3c24xx digital audio interface glue - connects codec <--> CPU */
81static struct snd_soc_dai_link rx1950_uda1380_dai[] = {
82 {
83 .name = "uda1380",
84 .stream_name = "UDA1380 Duplex",
85 .cpu_dai_name = "s3c24xx-iis",
86 .codec_dai_name = "uda1380-hifi",
87 .init = rx1950_uda1380_init,
Padmavathi Vennaa08485d2012-12-07 13:59:21 +053088 .platform_name = "s3c24xx-iis",
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030089 .codec_name = "uda1380-codec.0-001a",
90 .ops = &rx1950_ops,
91 },
92};
93
Vasily Khoruzhick81d97802010-08-30 11:28:08 +030094/* rx1950 machine dapm widgets */
95static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
96 SND_SOC_DAPM_HP("Headphone Jack", NULL),
97 SND_SOC_DAPM_MIC("Mic Jack", NULL),
98 SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
99};
100
101/* rx1950 machine audio_map */
102static const struct snd_soc_dapm_route audio_map[] = {
103 /* headphone connected to VOUTLHP, VOUTRHP */
104 {"Headphone Jack", NULL, "VOUTLHP"},
105 {"Headphone Jack", NULL, "VOUTRHP"},
106
107 /* ext speaker connected to VOUTL, VOUTR */
108 {"Speaker", NULL, "VOUTL"},
109 {"Speaker", NULL, "VOUTR"},
110
111 /* mic is connected to VINM */
112 {"VINM", NULL, "Mic Jack"},
113};
114
Mark Brown8ae23222011-10-08 13:30:35 +0100115static struct snd_soc_card rx1950_asoc = {
116 .name = "rx1950",
Axel Lin095d79d2011-12-22 10:53:15 +0800117 .owner = THIS_MODULE,
Mark Brown8ae23222011-10-08 13:30:35 +0100118 .dai_link = rx1950_uda1380_dai,
119 .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
120
121 .dapm_widgets = uda1380_dapm_widgets,
122 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
123 .dapm_routes = audio_map,
124 .num_dapm_routes = ARRAY_SIZE(audio_map),
125};
126
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300127static struct platform_device *s3c24xx_snd_device;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300128
129static int rx1950_startup(struct snd_pcm_substream *substream)
130{
131 struct snd_pcm_runtime *runtime = substream->runtime;
132
133 runtime->hw.rate_min = hw_rates.list[0];
134 runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
135 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
136
137 return snd_pcm_hw_constraint_list(runtime, 0,
138 SNDRV_PCM_HW_PARAM_RATE,
139 &hw_rates);
140}
141
142static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
143 struct snd_kcontrol *kcontrol, int event)
144{
145 if (SND_SOC_DAPM_EVENT_ON(event))
146 gpio_set_value(S3C2410_GPA(1), 1);
147 else
148 gpio_set_value(S3C2410_GPA(1), 0);
149
150 return 0;
151}
152
153static int rx1950_hw_params(struct snd_pcm_substream *substream,
154 struct snd_pcm_hw_params *params)
155{
156 struct snd_soc_pcm_runtime *rtd = substream->private_data;
157 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
158 struct snd_soc_dai *codec_dai = rtd->codec_dai;
159 int div;
160 int ret;
161 unsigned int rate = params_rate(params);
162 int clk_source, fs_mode;
163
164 switch (rate) {
165 case 16000:
166 case 48000:
167 clk_source = S3C24XX_CLKSRC_PCLK;
Vasily Khoruzhick1fdc7dd2010-09-07 17:04:18 +0300168 fs_mode = S3C2410_IISMOD_256FS;
169 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
170 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300171 div++;
172 break;
173 case 44100:
174 case 88200:
175 clk_source = S3C24XX_CLKSRC_MPLL;
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200176 fs_mode = S3C2410_IISMOD_384FS;
177 div = 1;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300178 break;
179 default:
180 printk(KERN_ERR "%s: rate %d is not supported\n",
181 __func__, rate);
182 return -EINVAL;
183 }
184
185 /* set codec DAI configuration */
186 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
187 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
188 if (ret < 0)
189 return ret;
190
191 /* set cpu DAI configuration */
192 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
193 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
194 if (ret < 0)
195 return ret;
196
197 /* select clock source */
198 ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
199 SND_SOC_CLOCK_OUT);
200 if (ret < 0)
201 return ret;
202
203 /* set MCLK division for sample rate */
204 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200205 fs_mode);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300206 if (ret < 0)
207 return ret;
208
209 /* set BCLK division for sample rate */
210 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
211 S3C2410_IISMOD_32FS);
212 if (ret < 0)
213 return ret;
214
215 /* set prescaler division for sample rate */
216 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
217 S3C24XX_PRESCALE(div, div));
218 if (ret < 0)
219 return ret;
220
221 return 0;
222}
223
224static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
225{
226 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200227 struct snd_soc_dapm_context *dapm = &codec->dapm;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300228 int err;
229
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200230 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
231 snd_soc_dapm_enable_pin(dapm, "Speaker");
Vasily Khoruzhickb60fc602011-01-06 22:15:49 +0200232 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300233
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300234 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
235 &hp_jack);
236
237 snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
238 hp_jack_pins);
239
240 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
241 hp_jack_gpios);
242
243 return 0;
244}
245
246static int __init rx1950_init(void)
247{
248 int ret;
249
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +0300250 if (!machine_is_rx1950())
251 return -ENODEV;
252
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300253 /* configure some gpios */
254 ret = gpio_request(S3C2410_GPA(1), "speaker-power");
255 if (ret)
256 goto err_gpio;
257
258 ret = gpio_direction_output(S3C2410_GPA(1), 0);
259 if (ret)
260 goto err_gpio_conf;
261
262 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
263 if (!s3c24xx_snd_device) {
264 ret = -ENOMEM;
265 goto err_plat_alloc;
266 }
267
268 platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
269 ret = platform_device_add(s3c24xx_snd_device);
270
271 if (ret) {
272 platform_device_put(s3c24xx_snd_device);
273 goto err_plat_add;
274 }
275
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300276 return 0;
277
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300278err_plat_add:
279err_plat_alloc:
280err_gpio_conf:
281 gpio_free(S3C2410_GPA(1));
282
283err_gpio:
284 return ret;
285}
286
287static void __exit rx1950_exit(void)
288{
289 platform_device_unregister(s3c24xx_snd_device);
290 snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
291 hp_jack_gpios);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300292 gpio_free(S3C2410_GPA(1));
293}
294
295module_init(rx1950_init);
296module_exit(rx1950_exit);
297
298/* Module information */
299MODULE_AUTHOR("Vasily Khoruzhick");
300MODULE_DESCRIPTION("ALSA SoC RX1950");
301MODULE_LICENSE("GPL");