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