2 * h1940-uda1380.c -- ALSA Soc Audio Layer
4 * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
5 * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
7 * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/types.h>
17 #include <linux/gpio.h>
18 #include <linux/module.h>
20 #include <sound/soc.h>
21 #include <sound/jack.h>
24 #include <asm/mach-types.h>
26 #include "s3c24xx-i2s.h"
28 static unsigned int rates
[] = {
34 static struct snd_pcm_hw_constraint_list hw_rates
= {
35 .count
= ARRAY_SIZE(rates
),
40 static struct snd_soc_jack hp_jack
;
42 static struct snd_soc_jack_pin hp_jack_pins
[] = {
44 .pin
= "Headphone Jack",
45 .mask
= SND_JACK_HEADPHONE
,
49 .mask
= SND_JACK_HEADPHONE
,
54 static struct snd_soc_jack_gpio hp_jack_gpios
[] = {
56 .gpio
= S3C2410_GPG(4),
58 .report
= SND_JACK_HEADPHONE
,
64 static int h1940_startup(struct snd_pcm_substream
*substream
)
66 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
68 runtime
->hw
.rate_min
= hw_rates
.list
[0];
69 runtime
->hw
.rate_max
= hw_rates
.list
[hw_rates
.count
- 1];
70 runtime
->hw
.rates
= SNDRV_PCM_RATE_KNOT
;
72 return snd_pcm_hw_constraint_list(runtime
, 0,
73 SNDRV_PCM_HW_PARAM_RATE
,
77 static int h1940_hw_params(struct snd_pcm_substream
*substream
,
78 struct snd_pcm_hw_params
*params
)
80 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
81 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
82 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
85 unsigned int rate
= params_rate(params
);
91 div
= s3c24xx_i2s_get_clockrate() / (384 * rate
);
92 if (s3c24xx_i2s_get_clockrate() % (384 * rate
) > (192 * rate
))
96 dev_err(&rtd
->dev
, "%s: rate %d is not supported\n",
101 /* set codec DAI configuration */
102 ret
= snd_soc_dai_set_fmt(codec_dai
, SND_SOC_DAIFMT_I2S
|
103 SND_SOC_DAIFMT_NB_NF
| SND_SOC_DAIFMT_CBS_CFS
);
107 /* set cpu DAI configuration */
108 ret
= snd_soc_dai_set_fmt(cpu_dai
, SND_SOC_DAIFMT_I2S
|
109 SND_SOC_DAIFMT_NB_NF
| SND_SOC_DAIFMT_CBS_CFS
);
113 /* select clock source */
114 ret
= snd_soc_dai_set_sysclk(cpu_dai
, S3C24XX_CLKSRC_PCLK
, rate
,
119 /* set MCLK division for sample rate */
120 ret
= snd_soc_dai_set_clkdiv(cpu_dai
, S3C24XX_DIV_MCLK
,
121 S3C2410_IISMOD_384FS
);
125 /* set BCLK division for sample rate */
126 ret
= snd_soc_dai_set_clkdiv(cpu_dai
, S3C24XX_DIV_BCLK
,
127 S3C2410_IISMOD_32FS
);
131 /* set prescaler division for sample rate */
132 ret
= snd_soc_dai_set_clkdiv(cpu_dai
, S3C24XX_DIV_PRESCALER
,
133 S3C24XX_PRESCALE(div
, div
));
140 static struct snd_soc_ops h1940_ops
= {
141 .startup
= h1940_startup
,
142 .hw_params
= h1940_hw_params
,
145 static int h1940_spk_power(struct snd_soc_dapm_widget
*w
,
146 struct snd_kcontrol
*kcontrol
, int event
)
148 if (SND_SOC_DAPM_EVENT_ON(event
))
149 gpio_set_value(S3C_GPIO_END
+ 9, 1);
151 gpio_set_value(S3C_GPIO_END
+ 9, 0);
156 /* h1940 machine dapm widgets */
157 static const struct snd_soc_dapm_widget uda1380_dapm_widgets
[] = {
158 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
159 SND_SOC_DAPM_MIC("Mic Jack", NULL
),
160 SND_SOC_DAPM_SPK("Speaker", h1940_spk_power
),
163 /* h1940 machine audio_map */
164 static const struct snd_soc_dapm_route audio_map
[] = {
165 /* headphone connected to VOUTLHP, VOUTRHP */
166 {"Headphone Jack", NULL
, "VOUTLHP"},
167 {"Headphone Jack", NULL
, "VOUTRHP"},
169 /* ext speaker connected to VOUTL, VOUTR */
170 {"Speaker", NULL
, "VOUTL"},
171 {"Speaker", NULL
, "VOUTR"},
173 /* mic is connected to VINM */
174 {"VINM", NULL
, "Mic Jack"},
177 static struct platform_device
*s3c24xx_snd_device
;
179 static int h1940_uda1380_init(struct snd_soc_pcm_runtime
*rtd
)
181 struct snd_soc_codec
*codec
= rtd
->codec
;
182 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
185 snd_soc_dapm_enable_pin(dapm
, "Headphone Jack");
186 snd_soc_dapm_enable_pin(dapm
, "Speaker");
187 snd_soc_dapm_enable_pin(dapm
, "Mic Jack");
189 snd_soc_jack_new(codec
, "Headphone Jack", SND_JACK_HEADPHONE
,
192 snd_soc_jack_add_pins(&hp_jack
, ARRAY_SIZE(hp_jack_pins
),
195 snd_soc_jack_add_gpios(&hp_jack
, ARRAY_SIZE(hp_jack_gpios
),
201 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
202 static struct snd_soc_dai_link h1940_uda1380_dai
[] = {
205 .stream_name
= "UDA1380 Duplex",
206 .cpu_dai_name
= "s3c24xx-iis",
207 .codec_dai_name
= "uda1380-hifi",
208 .init
= h1940_uda1380_init
,
209 .platform_name
= "s3c24xx-iis",
210 .codec_name
= "uda1380-codec.0-001a",
215 static struct snd_soc_card h1940_asoc
= {
217 .owner
= THIS_MODULE
,
218 .dai_link
= h1940_uda1380_dai
,
219 .num_links
= ARRAY_SIZE(h1940_uda1380_dai
),
221 .dapm_widgets
= uda1380_dapm_widgets
,
222 .num_dapm_widgets
= ARRAY_SIZE(uda1380_dapm_widgets
),
223 .dapm_routes
= audio_map
,
224 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
227 static int __init
h1940_init(void)
231 if (!machine_is_h1940())
234 /* configure some gpios */
235 ret
= gpio_request(S3C_GPIO_END
+ 9, "speaker-power");
239 ret
= gpio_direction_output(S3C_GPIO_END
+ 9, 0);
243 s3c24xx_snd_device
= platform_device_alloc("soc-audio", -1);
244 if (!s3c24xx_snd_device
) {
249 platform_set_drvdata(s3c24xx_snd_device
, &h1940_asoc
);
250 ret
= platform_device_add(s3c24xx_snd_device
);
258 platform_device_put(s3c24xx_snd_device
);
260 gpio_free(S3C_GPIO_END
+ 9);
266 static void __exit
h1940_exit(void)
268 platform_device_unregister(s3c24xx_snd_device
);
269 snd_soc_jack_free_gpios(&hp_jack
, ARRAY_SIZE(hp_jack_gpios
),
271 gpio_free(S3C_GPIO_END
+ 9);
274 module_init(h1940_init
);
275 module_exit(h1940_exit
);
277 /* Module information */
278 MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
279 MODULE_DESCRIPTION("ALSA SoC H1940");
280 MODULE_LICENSE("GPL");