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