blob: bc8c1676459f781b56b28e5fbcadd9ad7732f4a4 [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
93static struct snd_soc_card rx1950_asoc = {
94 .name = "rx1950",
95 .dai_link = rx1950_uda1380_dai,
96 .num_links = ARRAY_SIZE(rx1950_uda1380_dai),
97};
98
99/* rx1950 machine dapm widgets */
100static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
101 SND_SOC_DAPM_HP("Headphone Jack", NULL),
102 SND_SOC_DAPM_MIC("Mic Jack", NULL),
103 SND_SOC_DAPM_SPK("Speaker", rx1950_spk_power),
104};
105
106/* rx1950 machine audio_map */
107static const struct snd_soc_dapm_route audio_map[] = {
108 /* headphone connected to VOUTLHP, VOUTRHP */
109 {"Headphone Jack", NULL, "VOUTLHP"},
110 {"Headphone Jack", NULL, "VOUTRHP"},
111
112 /* ext speaker connected to VOUTL, VOUTR */
113 {"Speaker", NULL, "VOUTL"},
114 {"Speaker", NULL, "VOUTR"},
115
116 /* mic is connected to VINM */
117 {"VINM", NULL, "Mic Jack"},
118};
119
120static struct platform_device *s3c24xx_snd_device;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300121
122static int rx1950_startup(struct snd_pcm_substream *substream)
123{
124 struct snd_pcm_runtime *runtime = substream->runtime;
125
126 runtime->hw.rate_min = hw_rates.list[0];
127 runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
128 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
129
130 return snd_pcm_hw_constraint_list(runtime, 0,
131 SNDRV_PCM_HW_PARAM_RATE,
132 &hw_rates);
133}
134
135static int rx1950_spk_power(struct snd_soc_dapm_widget *w,
136 struct snd_kcontrol *kcontrol, int event)
137{
138 if (SND_SOC_DAPM_EVENT_ON(event))
139 gpio_set_value(S3C2410_GPA(1), 1);
140 else
141 gpio_set_value(S3C2410_GPA(1), 0);
142
143 return 0;
144}
145
146static int rx1950_hw_params(struct snd_pcm_substream *substream,
147 struct snd_pcm_hw_params *params)
148{
149 struct snd_soc_pcm_runtime *rtd = substream->private_data;
150 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
151 struct snd_soc_dai *codec_dai = rtd->codec_dai;
152 int div;
153 int ret;
154 unsigned int rate = params_rate(params);
155 int clk_source, fs_mode;
156
157 switch (rate) {
158 case 16000:
159 case 48000:
160 clk_source = S3C24XX_CLKSRC_PCLK;
Vasily Khoruzhick1fdc7dd2010-09-07 17:04:18 +0300161 fs_mode = S3C2410_IISMOD_256FS;
162 div = s3c24xx_i2s_get_clockrate() / (256 * rate);
163 if (s3c24xx_i2s_get_clockrate() % (256 * rate) > (128 * rate))
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300164 div++;
165 break;
166 case 44100:
167 case 88200:
168 clk_source = S3C24XX_CLKSRC_MPLL;
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200169 fs_mode = S3C2410_IISMOD_384FS;
170 div = 1;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300171 break;
172 default:
173 printk(KERN_ERR "%s: rate %d is not supported\n",
174 __func__, rate);
175 return -EINVAL;
176 }
177
178 /* set codec DAI configuration */
179 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
180 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
181 if (ret < 0)
182 return ret;
183
184 /* set cpu DAI configuration */
185 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
186 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
187 if (ret < 0)
188 return ret;
189
190 /* select clock source */
191 ret = snd_soc_dai_set_sysclk(cpu_dai, clk_source, rate,
192 SND_SOC_CLOCK_OUT);
193 if (ret < 0)
194 return ret;
195
196 /* set MCLK division for sample rate */
197 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Vasily Khoruzhickccb3b842010-11-13 14:53:41 +0200198 fs_mode);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300199 if (ret < 0)
200 return ret;
201
202 /* set BCLK division for sample rate */
203 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
204 S3C2410_IISMOD_32FS);
205 if (ret < 0)
206 return ret;
207
208 /* set prescaler division for sample rate */
209 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
210 S3C24XX_PRESCALE(div, div));
211 if (ret < 0)
212 return ret;
213
214 return 0;
215}
216
217static int rx1950_uda1380_init(struct snd_soc_pcm_runtime *rtd)
218{
219 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200220 struct snd_soc_dapm_context *dapm = &codec->dapm;
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300221 int err;
222
223 /* Add rx1950 specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200224 err = snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300225 ARRAY_SIZE(uda1380_dapm_widgets));
226
227 if (err)
228 return err;
229
230 /* Set up rx1950 specific audio path audio_mapnects */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200231 err = snd_soc_dapm_add_routes(dapm, audio_map,
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300232 ARRAY_SIZE(audio_map));
233
234 if (err)
235 return err;
236
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200237 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
238 snd_soc_dapm_enable_pin(dapm, "Speaker");
Vasily Khoruzhickb60fc602011-01-06 22:15:49 +0200239 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300240
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200241 snd_soc_dapm_sync(dapm);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300242
243 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
244 &hp_jack);
245
246 snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
247 hp_jack_pins);
248
249 snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
250 hp_jack_gpios);
251
252 return 0;
253}
254
255static int __init rx1950_init(void)
256{
257 int ret;
258
Vasily Khoruzhickdedc3cf2010-09-07 17:04:15 +0300259 if (!machine_is_rx1950())
260 return -ENODEV;
261
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300262 /* configure some gpios */
263 ret = gpio_request(S3C2410_GPA(1), "speaker-power");
264 if (ret)
265 goto err_gpio;
266
267 ret = gpio_direction_output(S3C2410_GPA(1), 0);
268 if (ret)
269 goto err_gpio_conf;
270
271 s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
272 if (!s3c24xx_snd_device) {
273 ret = -ENOMEM;
274 goto err_plat_alloc;
275 }
276
277 platform_set_drvdata(s3c24xx_snd_device, &rx1950_asoc);
278 ret = platform_device_add(s3c24xx_snd_device);
279
280 if (ret) {
281 platform_device_put(s3c24xx_snd_device);
282 goto err_plat_add;
283 }
284
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300285 return 0;
286
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300287err_plat_add:
288err_plat_alloc:
289err_gpio_conf:
290 gpio_free(S3C2410_GPA(1));
291
292err_gpio:
293 return ret;
294}
295
296static void __exit rx1950_exit(void)
297{
298 platform_device_unregister(s3c24xx_snd_device);
299 snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
300 hp_jack_gpios);
Vasily Khoruzhick81d97802010-08-30 11:28:08 +0300301 gpio_free(S3C2410_GPA(1));
302}
303
304module_init(rx1950_init);
305module_exit(rx1950_exit);
306
307/* Module information */
308MODULE_AUTHOR("Vasily Khoruzhick");
309MODULE_DESCRIPTION("ALSA SoC RX1950");
310MODULE_LICENSE("GPL");