blob: 11beb1a77c4e29cc5dc4c1b33e0946f5e379f804 [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 u8 reg_cache[1];
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
40static 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
66static 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
90static int cx20442_add_widgets(struct snd_soc_codec *codec)
91{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020092 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020093
Liam Girdwoodce6120c2010-11-05 15:53:46 +020094 snd_soc_dapm_new_controls(dapm, cx20442_dapm_widgets,
95 ARRAY_SIZE(cx20442_dapm_widgets));
96 snd_soc_dapm_add_routes(dapm, cx20442_audio_map,
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020097 ARRAY_SIZE(cx20442_audio_map));
98
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020099 return 0;
100}
101
102static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
103 unsigned int reg)
104{
105 u8 *reg_cache = codec->reg_cache;
106
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000107 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200108 return -EINVAL;
109
110 return reg_cache[reg];
111}
112
113enum v253_vls {
114 V253_VLS_NONE = 0,
115 V253_VLS_T,
116 V253_VLS_L,
117 V253_VLS_LT,
118 V253_VLS_S,
119 V253_VLS_ST,
120 V253_VLS_M,
121 V253_VLS_MST,
122 V253_VLS_S1,
123 V253_VLS_S1T,
124 V253_VLS_MS1T,
125 V253_VLS_M1,
126 V253_VLS_M1ST,
127 V253_VLS_M1S1T,
128 V253_VLS_H,
129 V253_VLS_HT,
130 V253_VLS_MS,
131 V253_VLS_MS1,
132 V253_VLS_M1S,
133 V253_VLS_M1S1,
134 V253_VLS_TEST,
135};
136
137static int cx20442_pm_to_v253_vls(u8 value)
138{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200139 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200140 case 0:
141 return V253_VLS_T;
142 case (1 << CX20442_SPKOUT):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200143 case (1 << CX20442_MIC):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200144 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}
155static int cx20442_pm_to_v253_vsp(u8 value)
156{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200157 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200158 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
166static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
167 unsigned int value)
168{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000169 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200170 u8 *reg_cache = codec->reg_cache;
171 int vls, vsp, old, len;
172 char buf[18];
173
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000174 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200175 return -EINVAL;
176
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200177 /* hw_write and control_data pointers required for talking to the modem
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200178 * are expected to be set by the line discipline initialization code */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000179 if (!codec->hw_write || !cx20442->control_data)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200180 return -EIO;
181
182 old = reg_cache[reg];
183 reg_cache[reg] = value;
184
185 vls = cx20442_pm_to_v253_vls(value);
186 if (vls < 0)
187 return vls;
188
189 vsp = cx20442_pm_to_v253_vsp(value);
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200190 if (vsp < 0)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200191 return vsp;
192
193 if ((vls == V253_VLS_T) ||
194 (vls == cx20442_pm_to_v253_vls(old))) {
195 if (vsp == cx20442_pm_to_v253_vsp(old))
196 return 0;
197 len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
198 } else if (vsp == cx20442_pm_to_v253_vsp(old))
199 len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
200 else
201 len = snprintf(buf, ARRAY_SIZE(buf),
202 "at+vls=%d;+vsp=%d\r", vls, vsp);
203
204 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
205 return -ENOMEM;
206
Janusz Krzysztofik4977b032009-08-06 12:24:54 +0200207 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000208 if (codec->hw_write(cx20442->control_data, buf, len) != len)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200209 return -EIO;
210
211 return 0;
212}
213
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200214
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200215/*
216 * Line discpline related code
217 *
218 * Any of the callback functions below can be used in two ways:
219 * 1) registerd by a machine driver as one of line discipline operations,
220 * 2) called from a machine's provided line discipline callback function
221 * in case when extra machine specific code must be run as well.
222 */
223
224/* Modem init: echo off, digital speaker off, quiet off, voice mode */
225static const char *v253_init = "ate0m0q0+fclass=8\r";
226
227/* Line discipline .open() */
228static int v253_open(struct tty_struct *tty)
229{
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200230 int ret, len = strlen(v253_init);
231
232 /* Doesn't make sense without write callback */
233 if (!tty->ops->write)
234 return -EINVAL;
235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000236 /* Won't work if no codec pointer has been passed by a card driver */
237 if (!tty->disc_data)
238 return -ENODEV;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200239
240 if (tty->ops->write(tty, v253_init, len) != len) {
241 ret = -EIO;
242 goto err;
243 }
244 /* Actual setup will be performed after the modem responds. */
245 return 0;
246err:
247 tty->disc_data = NULL;
248 return ret;
249}
250
251/* Line discipline .close() */
252static void v253_close(struct tty_struct *tty)
253{
254 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000255 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200256
257 tty->disc_data = NULL;
258
259 if (!codec)
260 return;
261
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000262 cx20442 = snd_soc_codec_get_drvdata(codec);
263
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200264 /* Prevent the codec driver from further accessing the modem */
265 codec->hw_write = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000266 cx20442->control_data = NULL;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200267 codec->dapm.pop_time = 0;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200268}
269
270/* Line discipline .hangup() */
271static int v253_hangup(struct tty_struct *tty)
272{
273 v253_close(tty);
274 return 0;
275}
276
277/* Line discipline .receive_buf() */
278static 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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000282 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200283
284 if (!codec)
285 return;
286
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000287 cx20442 = snd_soc_codec_get_drvdata(codec);
288
289 if (!cx20442->control_data) {
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200290 /* First modem response, complete setup procedure */
291
292 /* Set up codec driver access to modem controls */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000293 cx20442->control_data = tty;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200294 codec->hw_write = (hw_write_t)tty->ops->write;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200295 codec->dapm.pop_time = 1;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200296 }
297}
298
299/* Line discipline .write_wakeup() */
300static void v253_wakeup(struct tty_struct *tty)
301{
302}
303
304struct tty_ldisc_ops v253_ops = {
305 .magic = TTY_LDISC_MAGIC,
306 .name = "cx20442",
307 .owner = THIS_MODULE,
308 .open = v253_open,
309 .close = v253_close,
310 .hangup = v253_hangup,
311 .receive_buf = v253_receive,
312 .write_wakeup = v253_wakeup,
313};
314EXPORT_SYMBOL_GPL(v253_ops);
315
316
317/*
318 * Codec DAI
319 */
320
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000321static struct snd_soc_dai_driver cx20442_dai = {
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200322 .name = "cx20442-voice",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200323 .playback = {
324 .stream_name = "Playback",
325 .channels_min = 1,
326 .channels_max = 1,
327 .rates = SNDRV_PCM_RATE_8000,
328 .formats = SNDRV_PCM_FMTBIT_S16_LE,
329 },
330 .capture = {
331 .stream_name = "Capture",
332 .channels_min = 1,
333 .channels_max = 1,
334 .rates = SNDRV_PCM_RATE_8000,
335 .formats = SNDRV_PCM_FMTBIT_S16_LE,
336 },
337};
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200338
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000339static int cx20442_codec_probe(struct snd_soc_codec *codec)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200340{
341 struct cx20442_priv *cx20442;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200342
343 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
344 if (cx20442 == NULL)
345 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000346 snd_soc_codec_set_drvdata(codec, cx20442);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200347
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000348 cx20442_add_widgets(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200349
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000350 cx20442->control_data = NULL;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200351 codec->hw_write = NULL;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200352 codec->dapm.pop_time = 0;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000354 return 0;
355}
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200356
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000357/* power down chip */
358static int cx20442_codec_remove(struct snd_soc_codec *codec)
359{
360 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
361
362 if (cx20442->control_data) {
363 struct tty_struct *tty = cx20442->control_data;
364 tty_hangup(tty);
365 }
366
367 kfree(cx20442);
368 return 0;
369}
370
371static struct snd_soc_codec_driver cx20442_codec_dev = {
372 .probe = cx20442_codec_probe,
373 .remove = cx20442_codec_remove,
374 .reg_cache_size = 1,
375 .reg_word_size = sizeof(u8),
376 .read = cx20442_read_reg_cache,
377 .write = cx20442_write,
378};
379
380static int cx20442_platform_probe(struct platform_device *pdev)
381{
382 return snd_soc_register_codec(&pdev->dev,
383 &cx20442_codec_dev, &cx20442_dai, 1);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200384}
385
386static int __exit cx20442_platform_remove(struct platform_device *pdev)
387{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000388 snd_soc_unregister_codec(&pdev->dev);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200389 return 0;
390}
391
392static struct platform_driver cx20442_platform_driver = {
393 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000394 .name = "cx20442-codec",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200395 .owner = THIS_MODULE,
396 },
397 .probe = cx20442_platform_probe,
398 .remove = __exit_p(cx20442_platform_remove),
399};
400
401static int __init cx20442_init(void)
402{
403 return platform_driver_register(&cx20442_platform_driver);
404}
405module_init(cx20442_init);
406
407static void __exit cx20442_exit(void)
408{
409 platform_driver_unregister(&cx20442_platform_driver);
410}
411module_exit(cx20442_exit);
412
413MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
414MODULE_AUTHOR("Janusz Krzysztofik");
415MODULE_LICENSE("GPL");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000416MODULE_ALIAS("platform:cx20442-codec");