1 /*
2  * h1940-uda1380.c  --  ALSA Soc Audio Layer
3  *
4  * Copyright (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
5  * Copyright (c) 2010 Vasily Khoruzhick <anarsoul@gmail.com>
6  *
7  * Based on version from Arnaud Patard <arnaud.patard@rtp-net.org>
8  *
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.
13  *
14  */
15 
16 #include <linux/types.h>
17 #include <linux/gpio.h>
18 #include <linux/module.h>
19 
20 #include <sound/soc.h>
21 #include <sound/jack.h>
22 
23 #include <plat/regs-iis.h>
24 #include <mach/h1940-latch.h>
25 #include <asm/mach-types.h>
26 
27 #include "s3c24xx-i2s.h"
28 
29 static unsigned int rates[] = {
30 	11025,
31 	22050,
32 	44100,
33 };
34 
35 static struct snd_pcm_hw_constraint_list hw_rates = {
36 	.count = ARRAY_SIZE(rates),
37 	.list = rates,
38 	.mask = 0,
39 };
40 
41 static struct snd_soc_jack hp_jack;
42 
43 static struct snd_soc_jack_pin hp_jack_pins[] = {
44 	{
45 		.pin	= "Headphone Jack",
46 		.mask	= SND_JACK_HEADPHONE,
47 	},
48 	{
49 		.pin	= "Speaker",
50 		.mask	= SND_JACK_HEADPHONE,
51 		.invert	= 1,
52 	},
53 };
54 
55 static struct snd_soc_jack_gpio hp_jack_gpios[] = {
56 	{
57 		.gpio			= S3C2410_GPG(4),
58 		.name			= "hp-gpio",
59 		.report			= SND_JACK_HEADPHONE,
60 		.invert			= 1,
61 		.debounce_time		= 200,
62 	},
63 };
64 
h1940_startup(struct snd_pcm_substream * substream)65 static int h1940_startup(struct snd_pcm_substream *substream)
66 {
67 	struct snd_pcm_runtime *runtime = substream->runtime;
68 
69 	runtime->hw.rate_min = hw_rates.list[0];
70 	runtime->hw.rate_max = hw_rates.list[hw_rates.count - 1];
71 	runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
72 
73 	return snd_pcm_hw_constraint_list(runtime, 0,
74 					SNDRV_PCM_HW_PARAM_RATE,
75 					&hw_rates);
76 }
77 
h1940_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)78 static int h1940_hw_params(struct snd_pcm_substream *substream,
79 				struct snd_pcm_hw_params *params)
80 {
81 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
82 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
83 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
84 	int div;
85 	int ret;
86 	unsigned int rate = params_rate(params);
87 
88 	switch (rate) {
89 	case 11025:
90 	case 22050:
91 	case 44100:
92 		div = s3c24xx_i2s_get_clockrate() / (384 * rate);
93 		if (s3c24xx_i2s_get_clockrate() % (384 * rate) > (192 * rate))
94 			div++;
95 		break;
96 	default:
97 		dev_err(&rtd->dev, "%s: rate %d is not supported\n",
98 			__func__, rate);
99 		return -EINVAL;
100 	}
101 
102 	/* set codec DAI configuration */
103 	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
104 		SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
105 	if (ret < 0)
106 		return ret;
107 
108 	/* set cpu DAI configuration */
109 	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
110 		SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
111 	if (ret < 0)
112 		return ret;
113 
114 	/* select clock source */
115 	ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_PCLK, rate,
116 			SND_SOC_CLOCK_OUT);
117 	if (ret < 0)
118 		return ret;
119 
120 	/* set MCLK division for sample rate */
121 	ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
122 		S3C2410_IISMOD_384FS);
123 	if (ret < 0)
124 		return ret;
125 
126 	/* set BCLK division for sample rate */
127 	ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_BCLK,
128 		S3C2410_IISMOD_32FS);
129 	if (ret < 0)
130 		return ret;
131 
132 	/* set prescaler division for sample rate */
133 	ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
134 		S3C24XX_PRESCALE(div, div));
135 	if (ret < 0)
136 		return ret;
137 
138 	return 0;
139 }
140 
141 static struct snd_soc_ops h1940_ops = {
142 	.startup	= h1940_startup,
143 	.hw_params	= h1940_hw_params,
144 };
145 
h1940_spk_power(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)146 static int h1940_spk_power(struct snd_soc_dapm_widget *w,
147 				struct snd_kcontrol *kcontrol, int event)
148 {
149 	if (SND_SOC_DAPM_EVENT_ON(event))
150 		gpio_set_value(H1940_LATCH_AUDIO_POWER, 1);
151 	else
152 		gpio_set_value(H1940_LATCH_AUDIO_POWER, 0);
153 
154 	return 0;
155 }
156 
157 /* h1940 machine dapm widgets */
158 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
159 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
160 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
161 	SND_SOC_DAPM_SPK("Speaker", h1940_spk_power),
162 };
163 
164 /* h1940 machine audio_map */
165 static const struct snd_soc_dapm_route audio_map[] = {
166 	/* headphone connected to VOUTLHP, VOUTRHP */
167 	{"Headphone Jack", NULL, "VOUTLHP"},
168 	{"Headphone Jack", NULL, "VOUTRHP"},
169 
170 	/* ext speaker connected to VOUTL, VOUTR  */
171 	{"Speaker", NULL, "VOUTL"},
172 	{"Speaker", NULL, "VOUTR"},
173 
174 	/* mic is connected to VINM */
175 	{"VINM", NULL, "Mic Jack"},
176 };
177 
178 static struct platform_device *s3c24xx_snd_device;
179 
h1940_uda1380_init(struct snd_soc_pcm_runtime * rtd)180 static int h1940_uda1380_init(struct snd_soc_pcm_runtime *rtd)
181 {
182 	struct snd_soc_codec *codec = rtd->codec;
183 	struct snd_soc_dapm_context *dapm = &codec->dapm;
184 	int err;
185 
186 	snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
187 	snd_soc_dapm_enable_pin(dapm, "Speaker");
188 	snd_soc_dapm_enable_pin(dapm, "Mic Jack");
189 
190 	snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
191 		&hp_jack);
192 
193 	snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
194 		hp_jack_pins);
195 
196 	snd_soc_jack_add_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
197 		hp_jack_gpios);
198 
199 	return 0;
200 }
201 
202 /* s3c24xx digital audio interface glue - connects codec <--> CPU */
203 static struct snd_soc_dai_link h1940_uda1380_dai[] = {
204 	{
205 		.name		= "uda1380",
206 		.stream_name	= "UDA1380 Duplex",
207 		.cpu_dai_name	= "s3c24xx-iis",
208 		.codec_dai_name	= "uda1380-hifi",
209 		.init		= h1940_uda1380_init,
210 		.platform_name	= "samsung-audio",
211 		.codec_name	= "uda1380-codec.0-001a",
212 		.ops		= &h1940_ops,
213 	},
214 };
215 
216 static struct snd_soc_card h1940_asoc = {
217 	.name = "h1940",
218 	.owner = THIS_MODULE,
219 	.dai_link = h1940_uda1380_dai,
220 	.num_links = ARRAY_SIZE(h1940_uda1380_dai),
221 
222 	.dapm_widgets = uda1380_dapm_widgets,
223 	.num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
224 	.dapm_routes = audio_map,
225 	.num_dapm_routes = ARRAY_SIZE(audio_map),
226 };
227 
h1940_init(void)228 static int __init h1940_init(void)
229 {
230 	int ret;
231 
232 	if (!machine_is_h1940())
233 		return -ENODEV;
234 
235 	/* configure some gpios */
236 	ret = gpio_request(H1940_LATCH_AUDIO_POWER, "speaker-power");
237 	if (ret)
238 		goto err_out;
239 
240 	ret = gpio_direction_output(H1940_LATCH_AUDIO_POWER, 0);
241 	if (ret)
242 		goto err_gpio;
243 
244 	s3c24xx_snd_device = platform_device_alloc("soc-audio", -1);
245 	if (!s3c24xx_snd_device) {
246 		ret = -ENOMEM;
247 		goto err_gpio;
248 	}
249 
250 	platform_set_drvdata(s3c24xx_snd_device, &h1940_asoc);
251 	ret = platform_device_add(s3c24xx_snd_device);
252 
253 	if (ret)
254 		goto err_plat;
255 
256 	return 0;
257 
258 err_plat:
259 	platform_device_put(s3c24xx_snd_device);
260 err_gpio:
261 	gpio_free(H1940_LATCH_AUDIO_POWER);
262 
263 err_out:
264 	return ret;
265 }
266 
h1940_exit(void)267 static void __exit h1940_exit(void)
268 {
269 	platform_device_unregister(s3c24xx_snd_device);
270 	snd_soc_jack_free_gpios(&hp_jack, ARRAY_SIZE(hp_jack_gpios),
271 		hp_jack_gpios);
272 	gpio_free(H1940_LATCH_AUDIO_POWER);
273 }
274 
275 module_init(h1940_init);
276 module_exit(h1940_exit);
277 
278 /* Module information */
279 MODULE_AUTHOR("Arnaud Patard, Vasily Khoruzhick");
280 MODULE_DESCRIPTION("ALSA SoC H1940");
281 MODULE_LICENSE("GPL");
282