0e54adcc97956253582a6923d448a5a48a9acdc9
[deliverable/linux.git] / sound / soc / codecs / cx20442.c
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
16 #include <linux/tty.h>
17
18 #include <sound/core.h>
19 #include <sound/initval.h>
20 #include <sound/soc-dapm.h>
21
22 #include "cx20442.h"
23
24
25 struct cx20442_priv {
26 struct snd_soc_codec codec;
27 u8 reg_cache[1];
28 };
29
30 #define CX20442_PM 0x0
31
32 #define CX20442_TELIN 0
33 #define CX20442_TELOUT 1
34 #define CX20442_MIC 2
35 #define CX20442_SPKOUT 3
36 #define CX20442_AGC 4
37
38 static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
39 SND_SOC_DAPM_OUTPUT("TELOUT"),
40 SND_SOC_DAPM_OUTPUT("SPKOUT"),
41 SND_SOC_DAPM_OUTPUT("AGCOUT"),
42
43 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
44
45 SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
46 SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
47 SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
48
49 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
50 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
51
52 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
53
54 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
55 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
56
57 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
58
59 SND_SOC_DAPM_INPUT("TELIN"),
60 SND_SOC_DAPM_INPUT("MIC"),
61 SND_SOC_DAPM_INPUT("AGCIN"),
62 };
63
64 static const struct snd_soc_dapm_route cx20442_audio_map[] = {
65 {"TELOUT", NULL, "TELOUT Amp"},
66
67 {"SPKOUT", NULL, "SPKOUT Mixer"},
68 {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
69
70 {"TELOUT Amp", NULL, "DAC"},
71 {"SPKOUT Amp", NULL, "DAC"},
72
73 {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
74 {"SPKOUT AGC", NULL, "AGCIN"},
75
76 {"AGCOUT", NULL, "MIC AGC"},
77 {"MIC AGC", NULL, "MIC"},
78
79 {"MIC Bias", NULL, "MIC"},
80 {"Input Mixer", NULL, "MIC Bias"},
81
82 {"TELIN Bias", NULL, "TELIN"},
83 {"Input Mixer", NULL, "TELIN Bias"},
84
85 {"ADC", NULL, "Input Mixer"},
86 };
87
88 static int cx20442_add_widgets(struct snd_soc_codec *codec)
89 {
90 snd_soc_dapm_new_controls(codec, cx20442_dapm_widgets,
91 ARRAY_SIZE(cx20442_dapm_widgets));
92
93 snd_soc_dapm_add_routes(codec, cx20442_audio_map,
94 ARRAY_SIZE(cx20442_audio_map));
95
96 snd_soc_dapm_new_widgets(codec);
97 return 0;
98 }
99
100 static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
101 unsigned int reg)
102 {
103 u8 *reg_cache = codec->reg_cache;
104
105 if (reg >= codec->reg_cache_size)
106 return -EINVAL;
107
108 return reg_cache[reg];
109 }
110
111 enum v253_vls {
112 V253_VLS_NONE = 0,
113 V253_VLS_T,
114 V253_VLS_L,
115 V253_VLS_LT,
116 V253_VLS_S,
117 V253_VLS_ST,
118 V253_VLS_M,
119 V253_VLS_MST,
120 V253_VLS_S1,
121 V253_VLS_S1T,
122 V253_VLS_MS1T,
123 V253_VLS_M1,
124 V253_VLS_M1ST,
125 V253_VLS_M1S1T,
126 V253_VLS_H,
127 V253_VLS_HT,
128 V253_VLS_MS,
129 V253_VLS_MS1,
130 V253_VLS_M1S,
131 V253_VLS_M1S1,
132 V253_VLS_TEST,
133 };
134
135 static int cx20442_pm_to_v253_vls(u8 value)
136 {
137 switch (value & ~(1 << CX20442_AGC)) {
138 case 0:
139 return V253_VLS_T;
140 case (1 << CX20442_SPKOUT):
141 return V253_VLS_S1;
142 case (1 << CX20442_MIC):
143 return V253_VLS_M1;
144 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
145 return V253_VLS_M1S1;
146 case (1 << CX20442_TELOUT):
147 case (1 << CX20442_TELIN):
148 case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
149 return V253_VLS_L;
150 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
151 return V253_VLS_NONE;
152 }
153 return -EINVAL;
154 }
155 static int cx20442_pm_to_v253_vsp(u8 value)
156 {
157 switch (value & ~(1 << CX20442_AGC)) {
158 case (1 << CX20442_SPKOUT):
159 case (1 << CX20442_MIC):
160 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
161 return (bool)(value & (1 << CX20442_AGC));
162 }
163 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
164 }
165
166 static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
167 unsigned int value)
168 {
169 u8 *reg_cache = codec->reg_cache;
170 int vls, vsp, old, len;
171 char buf[18];
172
173 if (reg >= codec->reg_cache_size)
174 return -EINVAL;
175
176 /* hw_write and control_data pointers required for talking to the modem
177 * are expected to be set by the line discipline initialization code */
178 if (!codec->hw_write || !codec->control_data)
179 return -EIO;
180
181 old = reg_cache[reg];
182 reg_cache[reg] = value;
183
184 vls = cx20442_pm_to_v253_vls(value);
185 if (vls < 0)
186 return vls;
187
188 vsp = cx20442_pm_to_v253_vsp(value);
189 if (vsp < 0)
190 return vsp;
191
192 if ((vls == V253_VLS_T) ||
193 (vls == cx20442_pm_to_v253_vls(old))) {
194 if (vsp == cx20442_pm_to_v253_vsp(old))
195 return 0;
196 len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
197 } else if (vsp == cx20442_pm_to_v253_vsp(old))
198 len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
199 else
200 len = snprintf(buf, ARRAY_SIZE(buf),
201 "at+vls=%d;+vsp=%d\r", vls, vsp);
202
203 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
204 return -ENOMEM;
205
206 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
207 if (codec->hw_write(codec->control_data, buf, len) != len)
208 return -EIO;
209
210 return 0;
211 }
212
213
214 /* Moved up here as line discipline referres it during initialization */
215 static struct snd_soc_codec *cx20442_codec;
216
217
218 /*
219 * Line discpline related code
220 *
221 * Any of the callback functions below can be used in two ways:
222 * 1) registerd by a machine driver as one of line discipline operations,
223 * 2) called from a machine's provided line discipline callback function
224 * in case when extra machine specific code must be run as well.
225 */
226
227 /* Modem init: echo off, digital speaker off, quiet off, voice mode */
228 static const char *v253_init = "ate0m0q0+fclass=8\r";
229
230 /* Line discipline .open() */
231 static int v253_open(struct tty_struct *tty)
232 {
233 struct snd_soc_codec *codec = cx20442_codec;
234 int ret, len = strlen(v253_init);
235
236 /* Doesn't make sense without write callback */
237 if (!tty->ops->write)
238 return -EINVAL;
239
240 /* Pass the codec structure address for use by other ldisc callbacks */
241 tty->disc_data = codec;
242
243 if (tty->ops->write(tty, v253_init, len) != len) {
244 ret = -EIO;
245 goto err;
246 }
247 /* Actual setup will be performed after the modem responds. */
248 return 0;
249 err:
250 tty->disc_data = NULL;
251 return ret;
252 }
253
254 /* Line discipline .close() */
255 static void v253_close(struct tty_struct *tty)
256 {
257 struct snd_soc_codec *codec = tty->disc_data;
258
259 tty->disc_data = NULL;
260
261 if (!codec)
262 return;
263
264 /* Prevent the codec driver from further accessing the modem */
265 codec->hw_write = NULL;
266 codec->control_data = NULL;
267 codec->pop_time = 0;
268 }
269
270 /* Line discipline .hangup() */
271 static int v253_hangup(struct tty_struct *tty)
272 {
273 v253_close(tty);
274 return 0;
275 }
276
277 /* Line discipline .receive_buf() */
278 static void v253_receive(struct tty_struct *tty,
279 const unsigned char *cp, char *fp, int count)
280 {
281 struct snd_soc_codec *codec = tty->disc_data;
282
283 if (!codec)
284 return;
285
286 if (!codec->control_data) {
287 /* First modem response, complete setup procedure */
288
289 /* Set up codec driver access to modem controls */
290 codec->control_data = tty;
291 codec->hw_write = (hw_write_t)tty->ops->write;
292 codec->pop_time = 1;
293 }
294 }
295
296 /* Line discipline .write_wakeup() */
297 static void v253_wakeup(struct tty_struct *tty)
298 {
299 }
300
301 struct tty_ldisc_ops v253_ops = {
302 .magic = TTY_LDISC_MAGIC,
303 .name = "cx20442",
304 .owner = THIS_MODULE,
305 .open = v253_open,
306 .close = v253_close,
307 .hangup = v253_hangup,
308 .receive_buf = v253_receive,
309 .write_wakeup = v253_wakeup,
310 };
311 EXPORT_SYMBOL_GPL(v253_ops);
312
313
314 /*
315 * Codec DAI
316 */
317
318 struct snd_soc_dai cx20442_dai = {
319 .name = "CX20442",
320 .playback = {
321 .stream_name = "Playback",
322 .channels_min = 1,
323 .channels_max = 1,
324 .rates = SNDRV_PCM_RATE_8000,
325 .formats = SNDRV_PCM_FMTBIT_S16_LE,
326 },
327 .capture = {
328 .stream_name = "Capture",
329 .channels_min = 1,
330 .channels_max = 1,
331 .rates = SNDRV_PCM_RATE_8000,
332 .formats = SNDRV_PCM_FMTBIT_S16_LE,
333 },
334 };
335 EXPORT_SYMBOL_GPL(cx20442_dai);
336
337 static int cx20442_codec_probe(struct platform_device *pdev)
338 {
339 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
340 struct snd_soc_codec *codec;
341 int ret;
342
343 if (!cx20442_codec) {
344 dev_err(&pdev->dev, "cx20442 not yet discovered\n");
345 return -ENODEV;
346 }
347 codec = cx20442_codec;
348
349 socdev->card->codec = codec;
350
351 /* register pcms */
352 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
353 if (ret < 0) {
354 dev_err(&pdev->dev, "failed to create pcms\n");
355 goto pcm_err;
356 }
357
358 cx20442_add_widgets(codec);
359
360 ret = snd_soc_init_card(socdev);
361 if (ret < 0) {
362 dev_err(&pdev->dev, "failed to register card\n");
363 goto card_err;
364 }
365
366 return ret;
367
368 card_err:
369 snd_soc_free_pcms(socdev);
370 snd_soc_dapm_free(socdev);
371 pcm_err:
372 return ret;
373 }
374
375 /* power down chip */
376 static int cx20442_codec_remove(struct platform_device *pdev)
377 {
378 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
379
380 snd_soc_free_pcms(socdev);
381 snd_soc_dapm_free(socdev);
382
383 return 0;
384 }
385
386 struct snd_soc_codec_device cx20442_codec_dev = {
387 .probe = cx20442_codec_probe,
388 .remove = cx20442_codec_remove,
389 };
390 EXPORT_SYMBOL_GPL(cx20442_codec_dev);
391
392 static int cx20442_register(struct cx20442_priv *cx20442)
393 {
394 struct snd_soc_codec *codec = &cx20442->codec;
395 int ret;
396
397 mutex_init(&codec->mutex);
398 INIT_LIST_HEAD(&codec->dapm_widgets);
399 INIT_LIST_HEAD(&codec->dapm_paths);
400
401 codec->name = "CX20442";
402 codec->owner = THIS_MODULE;
403 codec->private_data = cx20442;
404
405 codec->dai = &cx20442_dai;
406 codec->num_dai = 1;
407
408 codec->reg_cache = &cx20442->reg_cache;
409 codec->reg_cache_size = ARRAY_SIZE(cx20442->reg_cache);
410 codec->read = cx20442_read_reg_cache;
411 codec->write = cx20442_write;
412
413 codec->bias_level = SND_SOC_BIAS_OFF;
414
415 cx20442_dai.dev = codec->dev;
416
417 cx20442_codec = codec;
418
419 ret = snd_soc_register_codec(codec);
420 if (ret != 0) {
421 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
422 goto err;
423 }
424
425 ret = snd_soc_register_dai(&cx20442_dai);
426 if (ret != 0) {
427 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
428 goto err_codec;
429 }
430
431 return 0;
432
433 err_codec:
434 snd_soc_unregister_codec(codec);
435 err:
436 cx20442_codec = NULL;
437 kfree(cx20442);
438 return ret;
439 }
440
441 static void cx20442_unregister(struct cx20442_priv *cx20442)
442 {
443 snd_soc_unregister_dai(&cx20442_dai);
444 snd_soc_unregister_codec(&cx20442->codec);
445
446 cx20442_codec = NULL;
447 kfree(cx20442);
448 }
449
450 static int cx20442_platform_probe(struct platform_device *pdev)
451 {
452 struct cx20442_priv *cx20442;
453 struct snd_soc_codec *codec;
454
455 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
456 if (cx20442 == NULL)
457 return -ENOMEM;
458
459 codec = &cx20442->codec;
460
461 codec->control_data = NULL;
462 codec->hw_write = NULL;
463 codec->pop_time = 0;
464
465 codec->dev = &pdev->dev;
466 platform_set_drvdata(pdev, cx20442);
467
468 return cx20442_register(cx20442);
469 }
470
471 static int __exit cx20442_platform_remove(struct platform_device *pdev)
472 {
473 struct cx20442_priv *cx20442 = platform_get_drvdata(pdev);
474
475 cx20442_unregister(cx20442);
476 return 0;
477 }
478
479 static struct platform_driver cx20442_platform_driver = {
480 .driver = {
481 .name = "cx20442",
482 .owner = THIS_MODULE,
483 },
484 .probe = cx20442_platform_probe,
485 .remove = __exit_p(cx20442_platform_remove),
486 };
487
488 static int __init cx20442_init(void)
489 {
490 return platform_driver_register(&cx20442_platform_driver);
491 }
492 module_init(cx20442_init);
493
494 static void __exit cx20442_exit(void)
495 {
496 platform_driver_unregister(&cx20442_platform_driver);
497 }
498 module_exit(cx20442_exit);
499
500 MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
501 MODULE_AUTHOR("Janusz Krzysztofik");
502 MODULE_LICENSE("GPL");
503 MODULE_ALIAS("platform:cx20442");
This page took 0.041601 seconds and 4 git commands to generate.