blob: bc7067db8ae4ec54697c4163e23f97af3670bd19 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040018#include <linux/module.h>
Janusz Krzysztofikad120da2009-07-29 12:24:46 +020019
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020020#include <sound/core.h>
21#include <sound/initval.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020022#include <sound/soc.h>
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020023
24#include "cx20442.h"
25
26
27struct cx20442_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000028 enum snd_soc_control_type control_type;
29 void *control_data;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020030};
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
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020090static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
91 unsigned int reg)
92{
93 u8 *reg_cache = codec->reg_cache;
94
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000095 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020096 return -EINVAL;
97
98 return reg_cache[reg];
99}
100
101enum 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
125static int cx20442_pm_to_v253_vls(u8 value)
126{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200127 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200128 case 0:
129 return V253_VLS_T;
130 case (1 << CX20442_SPKOUT):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200131 case (1 << CX20442_MIC):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200132 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}
143static int cx20442_pm_to_v253_vsp(u8 value)
144{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200145 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200146 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
154static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
155 unsigned int value)
156{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000157 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200158 u8 *reg_cache = codec->reg_cache;
159 int vls, vsp, old, len;
160 char buf[18];
161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000162 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200163 return -EINVAL;
164
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200165 /* hw_write and control_data pointers required for talking to the modem
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200166 * are expected to be set by the line discipline initialization code */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000167 if (!codec->hw_write || !cx20442->control_data)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200168 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);
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200178 if (vsp < 0)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200179 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
Janusz Krzysztofik4977b032009-08-06 12:24:54 +0200195 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000196 if (codec->hw_write(cx20442->control_data, buf, len) != len)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200197 return -EIO;
198
199 return 0;
200}
201
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200202
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200203/*
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 */
213static const char *v253_init = "ate0m0q0+fclass=8\r";
214
215/* Line discipline .open() */
216static int v253_open(struct tty_struct *tty)
217{
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200218 int ret, len = strlen(v253_init);
219
220 /* Doesn't make sense without write callback */
221 if (!tty->ops->write)
222 return -EINVAL;
223
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000224 /* Won't work if no codec pointer has been passed by a card driver */
225 if (!tty->disc_data)
226 return -ENODEV;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200227
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;
234err:
235 tty->disc_data = NULL;
236 return ret;
237}
238
239/* Line discipline .close() */
240static void v253_close(struct tty_struct *tty)
241{
242 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000243 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200244
245 tty->disc_data = NULL;
246
247 if (!codec)
248 return;
249
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000250 cx20442 = snd_soc_codec_get_drvdata(codec);
251
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200252 /* Prevent the codec driver from further accessing the modem */
253 codec->hw_write = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000254 cx20442->control_data = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200255 codec->card->pop_time = 0;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200256}
257
258/* Line discipline .hangup() */
259static int v253_hangup(struct tty_struct *tty)
260{
261 v253_close(tty);
262 return 0;
263}
264
265/* Line discipline .receive_buf() */
Linus Torvaldsbb3d6bf2011-06-04 07:00:50 +0900266static void v253_receive(struct tty_struct *tty,
267 const unsigned char *cp, char *fp, int count)
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200268{
269 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000270 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200271
272 if (!codec)
Linus Torvaldsbb3d6bf2011-06-04 07:00:50 +0900273 return;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275 cx20442 = snd_soc_codec_get_drvdata(codec);
276
277 if (!cx20442->control_data) {
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200278 /* First modem response, complete setup procedure */
279
280 /* Set up codec driver access to modem controls */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 cx20442->control_data = tty;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200282 codec->hw_write = (hw_write_t)tty->ops->write;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200283 codec->card->pop_time = 1;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200284 }
285}
286
287/* Line discipline .write_wakeup() */
288static void v253_wakeup(struct tty_struct *tty)
289{
290}
291
292struct 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};
302EXPORT_SYMBOL_GPL(v253_ops);
303
304
305/*
306 * Codec DAI
307 */
308
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000309static struct snd_soc_dai_driver cx20442_dai = {
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200310 .name = "cx20442-voice",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200311 .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};
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000327static int cx20442_codec_probe(struct snd_soc_codec *codec)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200328{
329 struct cx20442_priv *cx20442;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200330
331 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
332 if (cx20442 == NULL)
333 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000334 snd_soc_codec_set_drvdata(codec, cx20442);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200335
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000336 cx20442->control_data = NULL;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200337 codec->hw_write = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200338 codec->card->pop_time = 0;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200339
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000340 return 0;
341}
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200342
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000343/* power down chip */
344static int cx20442_codec_remove(struct snd_soc_codec *codec)
345{
346 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
347
348 if (cx20442->control_data) {
349 struct tty_struct *tty = cx20442->control_data;
350 tty_hangup(tty);
351 }
352
353 kfree(cx20442);
354 return 0;
355}
356
Janusz Krzysztofik8e6bfb92011-02-10 13:24:32 +0100357static const u8 cx20442_reg;
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100358
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000359static struct snd_soc_codec_driver cx20442_codec_dev = {
360 .probe = cx20442_codec_probe,
361 .remove = cx20442_codec_remove,
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100362 .reg_cache_default = &cx20442_reg,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000363 .reg_cache_size = 1,
364 .reg_word_size = sizeof(u8),
365 .read = cx20442_read_reg_cache,
366 .write = cx20442_write,
Lu Guanqun51fb1a82011-03-30 21:53:09 +0800367 .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),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000371};
372
373static int cx20442_platform_probe(struct platform_device *pdev)
374{
375 return snd_soc_register_codec(&pdev->dev,
376 &cx20442_codec_dev, &cx20442_dai, 1);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200377}
378
379static int __exit cx20442_platform_remove(struct platform_device *pdev)
380{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000381 snd_soc_unregister_codec(&pdev->dev);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200382 return 0;
383}
384
385static struct platform_driver cx20442_platform_driver = {
386 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000387 .name = "cx20442-codec",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200388 .owner = THIS_MODULE,
389 },
390 .probe = cx20442_platform_probe,
391 .remove = __exit_p(cx20442_platform_remove),
392};
393
394static int __init cx20442_init(void)
395{
396 return platform_driver_register(&cx20442_platform_driver);
397}
398module_init(cx20442_init);
399
400static void __exit cx20442_exit(void)
401{
402 platform_driver_unregister(&cx20442_platform_driver);
403}
404module_exit(cx20442_exit);
405
406MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
407MODULE_AUTHOR("Janusz Krzysztofik");
408MODULE_LICENSE("GPL");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000409MODULE_ALIAS("platform:cx20442-codec");