Commit | Line | Data |
---|---|---|
459dc352 JK |
1 | /* |
2 | * cx20442.c -- CX20442 ALSA Soc Audio driver | |
3 | * | |
4 | * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl> | |
5 | * | |
6 | * Initially based on sound/soc/codecs/wm8400.c | |
7 | * Copyright 2008, 2009 Wolfson Microelectronics PLC. | |
8 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | */ | |
15 | ||
ad120dae | 16 | #include <linux/tty.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
da155d5b | 18 | #include <linux/module.h> |
ad120dae | 19 | |
459dc352 JK |
20 | #include <sound/core.h> |
21 | #include <sound/initval.h> | |
ce6120cc | 22 | #include <sound/soc.h> |
459dc352 JK |
23 | |
24 | #include "cx20442.h" | |
25 | ||
26 | ||
27 | struct cx20442_priv { | |
f0fba2ad LG |
28 | enum snd_soc_control_type control_type; |
29 | void *control_data; | |
459dc352 JK |
30 | }; |
31 | ||
32 | #define CX20442_PM 0x0 | |
33 | ||
34 | #define CX20442_TELIN 0 | |
35 | #define CX20442_TELOUT 1 | |
36 | #define CX20442_MIC 2 | |
37 | #define CX20442_SPKOUT 3 | |
38 | #define CX20442_AGC 4 | |
39 | ||
40 | static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = { | |
41 | SND_SOC_DAPM_OUTPUT("TELOUT"), | |
42 | SND_SOC_DAPM_OUTPUT("SPKOUT"), | |
43 | SND_SOC_DAPM_OUTPUT("AGCOUT"), | |
44 | ||
45 | SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), | |
46 | ||
47 | SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0), | |
48 | SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0), | |
49 | SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0), | |
50 | ||
51 | SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0), | |
52 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), | |
53 | ||
54 | SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), | |
55 | ||
56 | SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0), | |
57 | SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0), | |
58 | ||
59 | SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0), | |
60 | ||
61 | SND_SOC_DAPM_INPUT("TELIN"), | |
62 | SND_SOC_DAPM_INPUT("MIC"), | |
63 | SND_SOC_DAPM_INPUT("AGCIN"), | |
64 | }; | |
65 | ||
66 | static const struct snd_soc_dapm_route cx20442_audio_map[] = { | |
67 | {"TELOUT", NULL, "TELOUT Amp"}, | |
68 | ||
69 | {"SPKOUT", NULL, "SPKOUT Mixer"}, | |
70 | {"SPKOUT Mixer", NULL, "SPKOUT Amp"}, | |
71 | ||
72 | {"TELOUT Amp", NULL, "DAC"}, | |
73 | {"SPKOUT Amp", NULL, "DAC"}, | |
74 | ||
75 | {"SPKOUT Mixer", NULL, "SPKOUT AGC"}, | |
76 | {"SPKOUT AGC", NULL, "AGCIN"}, | |
77 | ||
78 | {"AGCOUT", NULL, "MIC AGC"}, | |
79 | {"MIC AGC", NULL, "MIC"}, | |
80 | ||
81 | {"MIC Bias", NULL, "MIC"}, | |
82 | {"Input Mixer", NULL, "MIC Bias"}, | |
83 | ||
84 | {"TELIN Bias", NULL, "TELIN"}, | |
85 | {"Input Mixer", NULL, "TELIN Bias"}, | |
86 | ||
87 | {"ADC", NULL, "Input Mixer"}, | |
88 | }; | |
89 | ||
459dc352 JK |
90 | static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec, |
91 | unsigned int reg) | |
92 | { | |
93 | u8 *reg_cache = codec->reg_cache; | |
94 | ||
f0fba2ad | 95 | if (reg >= codec->driver->reg_cache_size) |
459dc352 JK |
96 | return -EINVAL; |
97 | ||
98 | return reg_cache[reg]; | |
99 | } | |
100 | ||
101 | enum v253_vls { | |
102 | V253_VLS_NONE = 0, | |
103 | V253_VLS_T, | |
104 | V253_VLS_L, | |
105 | V253_VLS_LT, | |
106 | V253_VLS_S, | |
107 | V253_VLS_ST, | |
108 | V253_VLS_M, | |
109 | V253_VLS_MST, | |
110 | V253_VLS_S1, | |
111 | V253_VLS_S1T, | |
112 | V253_VLS_MS1T, | |
113 | V253_VLS_M1, | |
114 | V253_VLS_M1ST, | |
115 | V253_VLS_M1S1T, | |
116 | V253_VLS_H, | |
117 | V253_VLS_HT, | |
118 | V253_VLS_MS, | |
119 | V253_VLS_MS1, | |
120 | V253_VLS_M1S, | |
121 | V253_VLS_M1S1, | |
122 | V253_VLS_TEST, | |
123 | }; | |
124 | ||
125 | static int cx20442_pm_to_v253_vls(u8 value) | |
126 | { | |
b84eab08 | 127 | switch (value & ~(1 << CX20442_AGC)) { |
459dc352 JK |
128 | case 0: |
129 | return V253_VLS_T; | |
130 | case (1 << CX20442_SPKOUT): | |
459dc352 | 131 | case (1 << CX20442_MIC): |
459dc352 JK |
132 | case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC): |
133 | return V253_VLS_M1S1; | |
134 | case (1 << CX20442_TELOUT): | |
135 | case (1 << CX20442_TELIN): | |
136 | case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN): | |
137 | return V253_VLS_L; | |
138 | case (1 << CX20442_TELOUT) | (1 << CX20442_MIC): | |
139 | return V253_VLS_NONE; | |
140 | } | |
141 | return -EINVAL; | |
142 | } | |
143 | static int cx20442_pm_to_v253_vsp(u8 value) | |
144 | { | |
b84eab08 | 145 | switch (value & ~(1 << CX20442_AGC)) { |
459dc352 JK |
146 | case (1 << CX20442_SPKOUT): |
147 | case (1 << CX20442_MIC): | |
148 | case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC): | |
149 | return (bool)(value & (1 << CX20442_AGC)); | |
150 | } | |
151 | return (value & (1 << CX20442_AGC)) ? -EINVAL : 0; | |
152 | } | |
153 | ||
154 | static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg, | |
155 | unsigned int value) | |
156 | { | |
f0fba2ad | 157 | struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec); |
459dc352 JK |
158 | u8 *reg_cache = codec->reg_cache; |
159 | int vls, vsp, old, len; | |
160 | char buf[18]; | |
161 | ||
f0fba2ad | 162 | if (reg >= codec->driver->reg_cache_size) |
459dc352 JK |
163 | return -EINVAL; |
164 | ||
b84eab08 | 165 | /* hw_write and control_data pointers required for talking to the modem |
ad120dae | 166 | * are expected to be set by the line discipline initialization code */ |
f0fba2ad | 167 | if (!codec->hw_write || !cx20442->control_data) |
459dc352 JK |
168 | return -EIO; |
169 | ||
170 | old = reg_cache[reg]; | |
171 | reg_cache[reg] = value; | |
172 | ||
173 | vls = cx20442_pm_to_v253_vls(value); | |
174 | if (vls < 0) | |
175 | return vls; | |
176 | ||
177 | vsp = cx20442_pm_to_v253_vsp(value); | |
b84eab08 | 178 | if (vsp < 0) |
459dc352 JK |
179 | return vsp; |
180 | ||
181 | if ((vls == V253_VLS_T) || | |
182 | (vls == cx20442_pm_to_v253_vls(old))) { | |
183 | if (vsp == cx20442_pm_to_v253_vsp(old)) | |
184 | return 0; | |
185 | len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp); | |
186 | } else if (vsp == cx20442_pm_to_v253_vsp(old)) | |
187 | len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls); | |
188 | else | |
189 | len = snprintf(buf, ARRAY_SIZE(buf), | |
190 | "at+vls=%d;+vsp=%d\r", vls, vsp); | |
191 | ||
192 | if (unlikely(len > (ARRAY_SIZE(buf) - 1))) | |
193 | return -ENOMEM; | |
194 | ||
4977b03e | 195 | dev_dbg(codec->dev, "%s: %s\n", __func__, buf); |
f0fba2ad | 196 | if (codec->hw_write(cx20442->control_data, buf, len) != len) |
459dc352 JK |
197 | return -EIO; |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
ad120dae | 202 | |
ad120dae JK |
203 | /* |
204 | * Line discpline related code | |
205 | * | |
206 | * Any of the callback functions below can be used in two ways: | |
207 | * 1) registerd by a machine driver as one of line discipline operations, | |
208 | * 2) called from a machine's provided line discipline callback function | |
209 | * in case when extra machine specific code must be run as well. | |
210 | */ | |
211 | ||
212 | /* Modem init: echo off, digital speaker off, quiet off, voice mode */ | |
213 | static const char *v253_init = "ate0m0q0+fclass=8\r"; | |
214 | ||
215 | /* Line discipline .open() */ | |
216 | static int v253_open(struct tty_struct *tty) | |
217 | { | |
ad120dae JK |
218 | int ret, len = strlen(v253_init); |
219 | ||
220 | /* Doesn't make sense without write callback */ | |
221 | if (!tty->ops->write) | |
222 | return -EINVAL; | |
223 | ||
f0fba2ad LG |
224 | /* Won't work if no codec pointer has been passed by a card driver */ |
225 | if (!tty->disc_data) | |
226 | return -ENODEV; | |
ad120dae JK |
227 | |
228 | if (tty->ops->write(tty, v253_init, len) != len) { | |
229 | ret = -EIO; | |
230 | goto err; | |
231 | } | |
232 | /* Actual setup will be performed after the modem responds. */ | |
233 | return 0; | |
234 | err: | |
235 | tty->disc_data = NULL; | |
236 | return ret; | |
237 | } | |
238 | ||
239 | /* Line discipline .close() */ | |
240 | static void v253_close(struct tty_struct *tty) | |
241 | { | |
242 | struct snd_soc_codec *codec = tty->disc_data; | |
f0fba2ad | 243 | struct cx20442_priv *cx20442; |
ad120dae JK |
244 | |
245 | tty->disc_data = NULL; | |
246 | ||
247 | if (!codec) | |
248 | return; | |
249 | ||
f0fba2ad LG |
250 | cx20442 = snd_soc_codec_get_drvdata(codec); |
251 | ||
ad120dae JK |
252 | /* Prevent the codec driver from further accessing the modem */ |
253 | codec->hw_write = NULL; | |
f0fba2ad | 254 | cx20442->control_data = NULL; |
3a45b867 | 255 | codec->card->pop_time = 0; |
ad120dae JK |
256 | } |
257 | ||
258 | /* Line discipline .hangup() */ | |
259 | static int v253_hangup(struct tty_struct *tty) | |
260 | { | |
261 | v253_close(tty); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | /* Line discipline .receive_buf() */ | |
bb3d6bf1 LT |
266 | static void v253_receive(struct tty_struct *tty, |
267 | const unsigned char *cp, char *fp, int count) | |
ad120dae JK |
268 | { |
269 | struct snd_soc_codec *codec = tty->disc_data; | |
f0fba2ad | 270 | struct cx20442_priv *cx20442; |
ad120dae JK |
271 | |
272 | if (!codec) | |
bb3d6bf1 | 273 | return; |
ad120dae | 274 | |
f0fba2ad LG |
275 | cx20442 = snd_soc_codec_get_drvdata(codec); |
276 | ||
277 | if (!cx20442->control_data) { | |
ad120dae JK |
278 | /* First modem response, complete setup procedure */ |
279 | ||
280 | /* Set up codec driver access to modem controls */ | |
f0fba2ad | 281 | cx20442->control_data = tty; |
ad120dae | 282 | codec->hw_write = (hw_write_t)tty->ops->write; |
3a45b867 | 283 | codec->card->pop_time = 1; |
ad120dae JK |
284 | } |
285 | } | |
286 | ||
287 | /* Line discipline .write_wakeup() */ | |
288 | static void v253_wakeup(struct tty_struct *tty) | |
289 | { | |
290 | } | |
291 | ||
292 | struct tty_ldisc_ops v253_ops = { | |
293 | .magic = TTY_LDISC_MAGIC, | |
294 | .name = "cx20442", | |
295 | .owner = THIS_MODULE, | |
296 | .open = v253_open, | |
297 | .close = v253_close, | |
298 | .hangup = v253_hangup, | |
299 | .receive_buf = v253_receive, | |
300 | .write_wakeup = v253_wakeup, | |
301 | }; | |
302 | EXPORT_SYMBOL_GPL(v253_ops); | |
303 | ||
304 | ||
305 | /* | |
306 | * Codec DAI | |
307 | */ | |
308 | ||
f0fba2ad | 309 | static struct snd_soc_dai_driver cx20442_dai = { |
5394637a | 310 | .name = "cx20442-voice", |
459dc352 JK |
311 | .playback = { |
312 | .stream_name = "Playback", | |
313 | .channels_min = 1, | |
314 | .channels_max = 1, | |
315 | .rates = SNDRV_PCM_RATE_8000, | |
316 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
317 | }, | |
318 | .capture = { | |
319 | .stream_name = "Capture", | |
320 | .channels_min = 1, | |
321 | .channels_max = 1, | |
322 | .rates = SNDRV_PCM_RATE_8000, | |
323 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
324 | }, | |
325 | }; | |
459dc352 | 326 | |
f0fba2ad | 327 | static int cx20442_codec_probe(struct snd_soc_codec *codec) |
459dc352 | 328 | { |
f0fba2ad | 329 | struct cx20442_priv *cx20442; |
459dc352 | 330 | |
f0fba2ad LG |
331 | cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL); |
332 | if (cx20442 == NULL) | |
333 | return -ENOMEM; | |
334 | snd_soc_codec_set_drvdata(codec, cx20442); | |
459dc352 | 335 | |
f0fba2ad LG |
336 | cx20442->control_data = NULL; |
337 | codec->hw_write = NULL; | |
3a45b867 | 338 | codec->card->pop_time = 0; |
f0fba2ad LG |
339 | |
340 | return 0; | |
459dc352 JK |
341 | } |
342 | ||
343 | /* power down chip */ | |
f0fba2ad | 344 | static int cx20442_codec_remove(struct snd_soc_codec *codec) |
459dc352 | 345 | { |
f0fba2ad | 346 | struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec); |
459dc352 | 347 | |
f0fba2ad LG |
348 | if (cx20442->control_data) { |
349 | struct tty_struct *tty = cx20442->control_data; | |
350 | tty_hangup(tty); | |
351 | } | |
459dc352 | 352 | |
f0fba2ad | 353 | kfree(cx20442); |
459dc352 JK |
354 | return 0; |
355 | } | |
356 | ||
8e6bfb9b | 357 | static const u8 cx20442_reg; |
f019ee5f | 358 | |
f0fba2ad | 359 | static struct snd_soc_codec_driver cx20442_codec_dev = { |
459dc352 JK |
360 | .probe = cx20442_codec_probe, |
361 | .remove = cx20442_codec_remove, | |
f019ee5f | 362 | .reg_cache_default = &cx20442_reg, |
f0fba2ad LG |
363 | .reg_cache_size = 1, |
364 | .reg_word_size = sizeof(u8), | |
365 | .read = cx20442_read_reg_cache, | |
366 | .write = cx20442_write, | |
51fb1a87 LG |
367 | .dapm_widgets = cx20442_dapm_widgets, |
368 | .num_dapm_widgets = ARRAY_SIZE(cx20442_dapm_widgets), | |
369 | .dapm_routes = cx20442_audio_map, | |
370 | .num_dapm_routes = ARRAY_SIZE(cx20442_audio_map), | |
459dc352 | 371 | }; |
459dc352 JK |
372 | |
373 | static int cx20442_platform_probe(struct platform_device *pdev) | |
374 | { | |
f0fba2ad LG |
375 | return snd_soc_register_codec(&pdev->dev, |
376 | &cx20442_codec_dev, &cx20442_dai, 1); | |
459dc352 JK |
377 | } |
378 | ||
379 | static int __exit cx20442_platform_remove(struct platform_device *pdev) | |
380 | { | |
f0fba2ad | 381 | snd_soc_unregister_codec(&pdev->dev); |
459dc352 JK |
382 | return 0; |
383 | } | |
384 | ||
385 | static struct platform_driver cx20442_platform_driver = { | |
386 | .driver = { | |
f0fba2ad | 387 | .name = "cx20442-codec", |
459dc352 JK |
388 | .owner = THIS_MODULE, |
389 | }, | |
390 | .probe = cx20442_platform_probe, | |
391 | .remove = __exit_p(cx20442_platform_remove), | |
392 | }; | |
393 | ||
5bbcc3c0 | 394 | module_platform_driver(cx20442_platform_driver); |
459dc352 JK |
395 | |
396 | MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver"); | |
397 | MODULE_AUTHOR("Janusz Krzysztofik"); | |
398 | MODULE_LICENSE("GPL"); | |
f0fba2ad | 399 | MODULE_ALIAS("platform:cx20442-codec"); |