blob: bb4bf65b9e7e4c213e0887fd9dd00fe355a65259 [file] [log] [blame]
Janusz Krzysztofik459dc352009-07-22 05:22:28 +02001/*
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
Janusz Krzysztofikad120da2009-07-29 12:24:46 +020016#include <linux/tty.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Janusz Krzysztofikad120da2009-07-29 12:24:46 +020018
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020019#include <sound/core.h>
20#include <sound/initval.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020021#include <sound/soc.h>
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020022
23#include "cx20442.h"
24
25
26struct cx20442_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000027 enum snd_soc_control_type control_type;
28 void *control_data;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020029};
30
31#define CX20442_PM 0x0
32
33#define CX20442_TELIN 0
34#define CX20442_TELOUT 1
35#define CX20442_MIC 2
36#define CX20442_SPKOUT 3
37#define CX20442_AGC 4
38
39static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
40 SND_SOC_DAPM_OUTPUT("TELOUT"),
41 SND_SOC_DAPM_OUTPUT("SPKOUT"),
42 SND_SOC_DAPM_OUTPUT("AGCOUT"),
43
44 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
45
46 SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
47 SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
48 SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
49
50 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
51 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
52
53 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
54
55 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
56 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
57
58 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
59
60 SND_SOC_DAPM_INPUT("TELIN"),
61 SND_SOC_DAPM_INPUT("MIC"),
62 SND_SOC_DAPM_INPUT("AGCIN"),
63};
64
65static const struct snd_soc_dapm_route cx20442_audio_map[] = {
66 {"TELOUT", NULL, "TELOUT Amp"},
67
68 {"SPKOUT", NULL, "SPKOUT Mixer"},
69 {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
70
71 {"TELOUT Amp", NULL, "DAC"},
72 {"SPKOUT Amp", NULL, "DAC"},
73
74 {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
75 {"SPKOUT AGC", NULL, "AGCIN"},
76
77 {"AGCOUT", NULL, "MIC AGC"},
78 {"MIC AGC", NULL, "MIC"},
79
80 {"MIC Bias", NULL, "MIC"},
81 {"Input Mixer", NULL, "MIC Bias"},
82
83 {"TELIN Bias", NULL, "TELIN"},
84 {"Input Mixer", NULL, "TELIN Bias"},
85
86 {"ADC", NULL, "Input Mixer"},
87};
88
89static int cx20442_add_widgets(struct snd_soc_codec *codec)
90{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020091 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020092
Liam Girdwoodce6120c2010-11-05 15:53:46 +020093 snd_soc_dapm_new_controls(dapm, cx20442_dapm_widgets,
94 ARRAY_SIZE(cx20442_dapm_widgets));
95 snd_soc_dapm_add_routes(dapm, cx20442_audio_map,
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020096 ARRAY_SIZE(cx20442_audio_map));
97
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020098 return 0;
99}
100
101static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
102 unsigned int reg)
103{
104 u8 *reg_cache = codec->reg_cache;
105
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000106 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200107 return -EINVAL;
108
109 return reg_cache[reg];
110}
111
112enum v253_vls {
113 V253_VLS_NONE = 0,
114 V253_VLS_T,
115 V253_VLS_L,
116 V253_VLS_LT,
117 V253_VLS_S,
118 V253_VLS_ST,
119 V253_VLS_M,
120 V253_VLS_MST,
121 V253_VLS_S1,
122 V253_VLS_S1T,
123 V253_VLS_MS1T,
124 V253_VLS_M1,
125 V253_VLS_M1ST,
126 V253_VLS_M1S1T,
127 V253_VLS_H,
128 V253_VLS_HT,
129 V253_VLS_MS,
130 V253_VLS_MS1,
131 V253_VLS_M1S,
132 V253_VLS_M1S1,
133 V253_VLS_TEST,
134};
135
136static int cx20442_pm_to_v253_vls(u8 value)
137{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200138 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200139 case 0:
140 return V253_VLS_T;
141 case (1 << CX20442_SPKOUT):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200142 case (1 << CX20442_MIC):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200143 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
144 return V253_VLS_M1S1;
145 case (1 << CX20442_TELOUT):
146 case (1 << CX20442_TELIN):
147 case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
148 return V253_VLS_L;
149 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
150 return V253_VLS_NONE;
151 }
152 return -EINVAL;
153}
154static int cx20442_pm_to_v253_vsp(u8 value)
155{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200156 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200157 case (1 << CX20442_SPKOUT):
158 case (1 << CX20442_MIC):
159 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
160 return (bool)(value & (1 << CX20442_AGC));
161 }
162 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
163}
164
165static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
166 unsigned int value)
167{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000168 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200169 u8 *reg_cache = codec->reg_cache;
170 int vls, vsp, old, len;
171 char buf[18];
172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200174 return -EINVAL;
175
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200176 /* hw_write and control_data pointers required for talking to the modem
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200177 * are expected to be set by the line discipline initialization code */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000178 if (!codec->hw_write || !cx20442->control_data)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200179 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);
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200189 if (vsp < 0)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200190 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
Janusz Krzysztofik4977b032009-08-06 12:24:54 +0200206 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000207 if (codec->hw_write(cx20442->control_data, buf, len) != len)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200208 return -EIO;
209
210 return 0;
211}
212
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200213
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200214/*
215 * Line discpline related code
216 *
217 * Any of the callback functions below can be used in two ways:
218 * 1) registerd by a machine driver as one of line discipline operations,
219 * 2) called from a machine's provided line discipline callback function
220 * in case when extra machine specific code must be run as well.
221 */
222
223/* Modem init: echo off, digital speaker off, quiet off, voice mode */
224static const char *v253_init = "ate0m0q0+fclass=8\r";
225
226/* Line discipline .open() */
227static int v253_open(struct tty_struct *tty)
228{
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200229 int ret, len = strlen(v253_init);
230
231 /* Doesn't make sense without write callback */
232 if (!tty->ops->write)
233 return -EINVAL;
234
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000235 /* Won't work if no codec pointer has been passed by a card driver */
236 if (!tty->disc_data)
237 return -ENODEV;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200238
239 if (tty->ops->write(tty, v253_init, len) != len) {
240 ret = -EIO;
241 goto err;
242 }
243 /* Actual setup will be performed after the modem responds. */
244 return 0;
245err:
246 tty->disc_data = NULL;
247 return ret;
248}
249
250/* Line discipline .close() */
251static void v253_close(struct tty_struct *tty)
252{
253 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000254 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200255
256 tty->disc_data = NULL;
257
258 if (!codec)
259 return;
260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000261 cx20442 = snd_soc_codec_get_drvdata(codec);
262
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200263 /* Prevent the codec driver from further accessing the modem */
264 codec->hw_write = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000265 cx20442->control_data = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200266 codec->card->pop_time = 0;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200267}
268
269/* Line discipline .hangup() */
270static int v253_hangup(struct tty_struct *tty)
271{
272 v253_close(tty);
273 return 0;
274}
275
276/* Line discipline .receive_buf() */
277static void v253_receive(struct tty_struct *tty,
278 const unsigned char *cp, char *fp, int count)
279{
280 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200282
283 if (!codec)
284 return;
285
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000286 cx20442 = snd_soc_codec_get_drvdata(codec);
287
288 if (!cx20442->control_data) {
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200289 /* First modem response, complete setup procedure */
290
291 /* Set up codec driver access to modem controls */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000292 cx20442->control_data = tty;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200293 codec->hw_write = (hw_write_t)tty->ops->write;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200294 codec->card->pop_time = 1;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200295 }
296}
297
298/* Line discipline .write_wakeup() */
299static void v253_wakeup(struct tty_struct *tty)
300{
301}
302
303struct tty_ldisc_ops v253_ops = {
304 .magic = TTY_LDISC_MAGIC,
305 .name = "cx20442",
306 .owner = THIS_MODULE,
307 .open = v253_open,
308 .close = v253_close,
309 .hangup = v253_hangup,
310 .receive_buf = v253_receive,
311 .write_wakeup = v253_wakeup,
312};
313EXPORT_SYMBOL_GPL(v253_ops);
314
315
316/*
317 * Codec DAI
318 */
319
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320static struct snd_soc_dai_driver cx20442_dai = {
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200321 .name = "cx20442-voice",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200322 .playback = {
323 .stream_name = "Playback",
324 .channels_min = 1,
325 .channels_max = 1,
326 .rates = SNDRV_PCM_RATE_8000,
327 .formats = SNDRV_PCM_FMTBIT_S16_LE,
328 },
329 .capture = {
330 .stream_name = "Capture",
331 .channels_min = 1,
332 .channels_max = 1,
333 .rates = SNDRV_PCM_RATE_8000,
334 .formats = SNDRV_PCM_FMTBIT_S16_LE,
335 },
336};
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200337
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000338static int cx20442_codec_probe(struct snd_soc_codec *codec)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200339{
340 struct cx20442_priv *cx20442;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200341
342 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
343 if (cx20442 == NULL)
344 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000345 snd_soc_codec_set_drvdata(codec, cx20442);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200346
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000347 cx20442_add_widgets(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200348
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000349 cx20442->control_data = NULL;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200350 codec->hw_write = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200351 codec->card->pop_time = 0;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200352
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000353 return 0;
354}
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000356/* power down chip */
357static int cx20442_codec_remove(struct snd_soc_codec *codec)
358{
359 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
360
361 if (cx20442->control_data) {
362 struct tty_struct *tty = cx20442->control_data;
363 tty_hangup(tty);
364 }
365
366 kfree(cx20442);
367 return 0;
368}
369
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100370static const u8 cx20442_reg = CX20442_TELOUT | CX20442_MIC;
371
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000372static struct snd_soc_codec_driver cx20442_codec_dev = {
373 .probe = cx20442_codec_probe,
374 .remove = cx20442_codec_remove,
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100375 .reg_cache_default = &cx20442_reg,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000376 .reg_cache_size = 1,
377 .reg_word_size = sizeof(u8),
378 .read = cx20442_read_reg_cache,
379 .write = cx20442_write,
380};
381
382static int cx20442_platform_probe(struct platform_device *pdev)
383{
384 return snd_soc_register_codec(&pdev->dev,
385 &cx20442_codec_dev, &cx20442_dai, 1);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200386}
387
388static int __exit cx20442_platform_remove(struct platform_device *pdev)
389{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000390 snd_soc_unregister_codec(&pdev->dev);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200391 return 0;
392}
393
394static struct platform_driver cx20442_platform_driver = {
395 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000396 .name = "cx20442-codec",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200397 .owner = THIS_MODULE,
398 },
399 .probe = cx20442_platform_probe,
400 .remove = __exit_p(cx20442_platform_remove),
401};
402
403static int __init cx20442_init(void)
404{
405 return platform_driver_register(&cx20442_platform_driver);
406}
407module_init(cx20442_init);
408
409static void __exit cx20442_exit(void)
410{
411 platform_driver_unregister(&cx20442_platform_driver);
412}
413module_exit(cx20442_exit);
414
415MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
416MODULE_AUTHOR("Janusz Krzysztofik");
417MODULE_LICENSE("GPL");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418MODULE_ALIAS("platform:cx20442-codec");