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