blob: d5fd00a64748097ad1794ed9cc45178c513c6b68 [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 Krzysztofikf75a8ff2011-12-30 04:04:54 +010019#include <linux/regulator/consumer.h>
Janusz Krzysztofikad120da2009-07-29 12:24:46 +020020
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020021#include <sound/core.h>
22#include <sound/initval.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020023#include <sound/soc.h>
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020024
25#include "cx20442.h"
26
27
28struct cx20442_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000029 void *control_data;
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +010030 struct regulator *por;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020031};
32
33#define CX20442_PM 0x0
34
35#define CX20442_TELIN 0
36#define CX20442_TELOUT 1
37#define CX20442_MIC 2
38#define CX20442_SPKOUT 3
39#define CX20442_AGC 4
40
41static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
42 SND_SOC_DAPM_OUTPUT("TELOUT"),
43 SND_SOC_DAPM_OUTPUT("SPKOUT"),
44 SND_SOC_DAPM_OUTPUT("AGCOUT"),
45
46 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
47
48 SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
49 SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
50 SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
51
52 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
53 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
54
55 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
56
57 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
58 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
59
60 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
61
62 SND_SOC_DAPM_INPUT("TELIN"),
63 SND_SOC_DAPM_INPUT("MIC"),
64 SND_SOC_DAPM_INPUT("AGCIN"),
65};
66
67static const struct snd_soc_dapm_route cx20442_audio_map[] = {
68 {"TELOUT", NULL, "TELOUT Amp"},
69
70 {"SPKOUT", NULL, "SPKOUT Mixer"},
71 {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
72
73 {"TELOUT Amp", NULL, "DAC"},
74 {"SPKOUT Amp", NULL, "DAC"},
75
76 {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
77 {"SPKOUT AGC", NULL, "AGCIN"},
78
79 {"AGCOUT", NULL, "MIC AGC"},
80 {"MIC AGC", NULL, "MIC"},
81
82 {"MIC Bias", NULL, "MIC"},
83 {"Input Mixer", NULL, "MIC Bias"},
84
85 {"TELIN Bias", NULL, "TELIN"},
86 {"Input Mixer", NULL, "TELIN Bias"},
87
88 {"ADC", NULL, "Input Mixer"},
89};
90
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020091static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
92 unsigned int reg)
93{
94 u8 *reg_cache = codec->reg_cache;
95
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000096 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020097 return -EINVAL;
98
99 return reg_cache[reg];
100}
101
102enum v253_vls {
103 V253_VLS_NONE = 0,
104 V253_VLS_T,
105 V253_VLS_L,
106 V253_VLS_LT,
107 V253_VLS_S,
108 V253_VLS_ST,
109 V253_VLS_M,
110 V253_VLS_MST,
111 V253_VLS_S1,
112 V253_VLS_S1T,
113 V253_VLS_MS1T,
114 V253_VLS_M1,
115 V253_VLS_M1ST,
116 V253_VLS_M1S1T,
117 V253_VLS_H,
118 V253_VLS_HT,
119 V253_VLS_MS,
120 V253_VLS_MS1,
121 V253_VLS_M1S,
122 V253_VLS_M1S1,
123 V253_VLS_TEST,
124};
125
126static int cx20442_pm_to_v253_vls(u8 value)
127{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200128 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200129 case 0:
130 return V253_VLS_T;
131 case (1 << CX20442_SPKOUT):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200132 case (1 << CX20442_MIC):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200133 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
134 return V253_VLS_M1S1;
135 case (1 << CX20442_TELOUT):
136 case (1 << CX20442_TELIN):
137 case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
138 return V253_VLS_L;
139 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
140 return V253_VLS_NONE;
141 }
142 return -EINVAL;
143}
144static int cx20442_pm_to_v253_vsp(u8 value)
145{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200146 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200147 case (1 << CX20442_SPKOUT):
148 case (1 << CX20442_MIC):
149 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
150 return (bool)(value & (1 << CX20442_AGC));
151 }
152 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
153}
154
155static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
156 unsigned int value)
157{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200159 u8 *reg_cache = codec->reg_cache;
160 int vls, vsp, old, len;
161 char buf[18];
162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000163 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200164 return -EINVAL;
165
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200166 /* hw_write and control_data pointers required for talking to the modem
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200167 * are expected to be set by the line discipline initialization code */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000168 if (!codec->hw_write || !cx20442->control_data)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200169 return -EIO;
170
171 old = reg_cache[reg];
172 reg_cache[reg] = value;
173
174 vls = cx20442_pm_to_v253_vls(value);
175 if (vls < 0)
176 return vls;
177
178 vsp = cx20442_pm_to_v253_vsp(value);
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200179 if (vsp < 0)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200180 return vsp;
181
182 if ((vls == V253_VLS_T) ||
183 (vls == cx20442_pm_to_v253_vls(old))) {
184 if (vsp == cx20442_pm_to_v253_vsp(old))
185 return 0;
186 len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
187 } else if (vsp == cx20442_pm_to_v253_vsp(old))
188 len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
189 else
190 len = snprintf(buf, ARRAY_SIZE(buf),
191 "at+vls=%d;+vsp=%d\r", vls, vsp);
192
193 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
194 return -ENOMEM;
195
Janusz Krzysztofik4977b032009-08-06 12:24:54 +0200196 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000197 if (codec->hw_write(cx20442->control_data, buf, len) != len)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200198 return -EIO;
199
200 return 0;
201}
202
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200203
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200204/*
205 * Line discpline related code
206 *
207 * Any of the callback functions below can be used in two ways:
208 * 1) registerd by a machine driver as one of line discipline operations,
209 * 2) called from a machine's provided line discipline callback function
210 * in case when extra machine specific code must be run as well.
211 */
212
213/* Modem init: echo off, digital speaker off, quiet off, voice mode */
214static const char *v253_init = "ate0m0q0+fclass=8\r";
215
216/* Line discipline .open() */
217static int v253_open(struct tty_struct *tty)
218{
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200219 int ret, len = strlen(v253_init);
220
221 /* Doesn't make sense without write callback */
222 if (!tty->ops->write)
223 return -EINVAL;
224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000225 /* Won't work if no codec pointer has been passed by a card driver */
226 if (!tty->disc_data)
227 return -ENODEV;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200228
229 if (tty->ops->write(tty, v253_init, len) != len) {
230 ret = -EIO;
231 goto err;
232 }
233 /* Actual setup will be performed after the modem responds. */
234 return 0;
235err:
236 tty->disc_data = NULL;
237 return ret;
238}
239
240/* Line discipline .close() */
241static void v253_close(struct tty_struct *tty)
242{
243 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000244 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200245
246 tty->disc_data = NULL;
247
248 if (!codec)
249 return;
250
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000251 cx20442 = snd_soc_codec_get_drvdata(codec);
252
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200253 /* Prevent the codec driver from further accessing the modem */
254 codec->hw_write = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000255 cx20442->control_data = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200256 codec->card->pop_time = 0;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200257}
258
259/* Line discipline .hangup() */
260static int v253_hangup(struct tty_struct *tty)
261{
262 v253_close(tty);
263 return 0;
264}
265
266/* Line discipline .receive_buf() */
Linus Torvaldsbb3d6bf2011-06-04 07:00:50 +0900267static void v253_receive(struct tty_struct *tty,
268 const unsigned char *cp, char *fp, int count)
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200269{
270 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000271 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200272
273 if (!codec)
Linus Torvaldsbb3d6bf2011-06-04 07:00:50 +0900274 return;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200275
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000276 cx20442 = snd_soc_codec_get_drvdata(codec);
277
278 if (!cx20442->control_data) {
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200279 /* First modem response, complete setup procedure */
280
281 /* Set up codec driver access to modem controls */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000282 cx20442->control_data = tty;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200283 codec->hw_write = (hw_write_t)tty->ops->write;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200284 codec->card->pop_time = 1;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200285 }
286}
287
288/* Line discipline .write_wakeup() */
289static void v253_wakeup(struct tty_struct *tty)
290{
291}
292
293struct tty_ldisc_ops v253_ops = {
294 .magic = TTY_LDISC_MAGIC,
295 .name = "cx20442",
296 .owner = THIS_MODULE,
297 .open = v253_open,
298 .close = v253_close,
299 .hangup = v253_hangup,
300 .receive_buf = v253_receive,
301 .write_wakeup = v253_wakeup,
302};
303EXPORT_SYMBOL_GPL(v253_ops);
304
305
306/*
307 * Codec DAI
308 */
309
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000310static struct snd_soc_dai_driver cx20442_dai = {
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200311 .name = "cx20442-voice",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200312 .playback = {
313 .stream_name = "Playback",
314 .channels_min = 1,
315 .channels_max = 1,
316 .rates = SNDRV_PCM_RATE_8000,
317 .formats = SNDRV_PCM_FMTBIT_S16_LE,
318 },
319 .capture = {
320 .stream_name = "Capture",
321 .channels_min = 1,
322 .channels_max = 1,
323 .rates = SNDRV_PCM_RATE_8000,
324 .formats = SNDRV_PCM_FMTBIT_S16_LE,
325 },
326};
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200327
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100328static int cx20442_set_bias_level(struct snd_soc_codec *codec,
329 enum snd_soc_bias_level level)
330{
331 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
332 int err = 0;
333
334 switch (level) {
335 case SND_SOC_BIAS_PREPARE:
336 if (codec->dapm.bias_level != SND_SOC_BIAS_STANDBY)
337 break;
338 if (IS_ERR(cx20442->por))
339 err = PTR_ERR(cx20442->por);
340 else
341 err = regulator_enable(cx20442->por);
342 break;
343 case SND_SOC_BIAS_STANDBY:
344 if (codec->dapm.bias_level != SND_SOC_BIAS_PREPARE)
345 break;
346 if (IS_ERR(cx20442->por))
347 err = PTR_ERR(cx20442->por);
348 else
349 err = regulator_disable(cx20442->por);
350 break;
351 default:
352 break;
353 }
354 if (!err)
355 codec->dapm.bias_level = level;
356
357 return err;
358}
359
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000360static int cx20442_codec_probe(struct snd_soc_codec *codec)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200361{
362 struct cx20442_priv *cx20442;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200363
364 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
365 if (cx20442 == NULL)
366 return -ENOMEM;
367
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100368 cx20442->por = regulator_get(codec->dev, "POR");
369 if (IS_ERR(cx20442->por))
370 dev_warn(codec->dev, "failed to get the regulator");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000371 cx20442->control_data = NULL;
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100372
373 snd_soc_codec_set_drvdata(codec, cx20442);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200374 codec->hw_write = NULL;
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200375 codec->card->pop_time = 0;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377 return 0;
378}
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200379
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000380/* power down chip */
381static int cx20442_codec_remove(struct snd_soc_codec *codec)
382{
383 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
384
385 if (cx20442->control_data) {
386 struct tty_struct *tty = cx20442->control_data;
387 tty_hangup(tty);
388 }
389
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100390 if (!IS_ERR(cx20442->por)) {
391 /* should be already in STANDBY, hence disabled */
392 regulator_put(cx20442->por);
393 }
394
395 snd_soc_codec_set_drvdata(codec, NULL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000396 kfree(cx20442);
397 return 0;
398}
399
Janusz Krzysztofik8e6bfb92011-02-10 13:24:32 +0100400static const u8 cx20442_reg;
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100401
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000402static struct snd_soc_codec_driver cx20442_codec_dev = {
403 .probe = cx20442_codec_probe,
404 .remove = cx20442_codec_remove,
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100405 .set_bias_level = cx20442_set_bias_level,
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100406 .reg_cache_default = &cx20442_reg,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000407 .reg_cache_size = 1,
408 .reg_word_size = sizeof(u8),
409 .read = cx20442_read_reg_cache,
410 .write = cx20442_write,
Lu Guanqun51fb1a82011-03-30 21:53:09 +0800411 .dapm_widgets = cx20442_dapm_widgets,
412 .num_dapm_widgets = ARRAY_SIZE(cx20442_dapm_widgets),
413 .dapm_routes = cx20442_audio_map,
414 .num_dapm_routes = ARRAY_SIZE(cx20442_audio_map),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000415};
416
417static int cx20442_platform_probe(struct platform_device *pdev)
418{
419 return snd_soc_register_codec(&pdev->dev,
420 &cx20442_codec_dev, &cx20442_dai, 1);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200421}
422
423static int __exit cx20442_platform_remove(struct platform_device *pdev)
424{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000425 snd_soc_unregister_codec(&pdev->dev);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200426 return 0;
427}
428
429static struct platform_driver cx20442_platform_driver = {
430 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000431 .name = "cx20442-codec",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200432 .owner = THIS_MODULE,
433 },
434 .probe = cx20442_platform_probe,
435 .remove = __exit_p(cx20442_platform_remove),
436};
437
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000438module_platform_driver(cx20442_platform_driver);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200439
440MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
441MODULE_AUTHOR("Janusz Krzysztofik");
442MODULE_LICENSE("GPL");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000443MODULE_ALIAS("platform:cx20442-codec");