blob: 95bb10ba80dcfb447d811f04032a7eb600168b51 [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 {
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +000029 struct tty_struct *tty;
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +010030 struct regulator *por;
Kuninori Morimoto3c897242018-01-16 02:00:18 +000031 u8 reg_cache;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +020032};
33
34#define CX20442_PM 0x0
35
36#define CX20442_TELIN 0
37#define CX20442_TELOUT 1
38#define CX20442_MIC 2
39#define CX20442_SPKOUT 3
40#define CX20442_AGC 4
41
42static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
43 SND_SOC_DAPM_OUTPUT("TELOUT"),
44 SND_SOC_DAPM_OUTPUT("SPKOUT"),
45 SND_SOC_DAPM_OUTPUT("AGCOUT"),
46
47 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
48
49 SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
50 SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
51 SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
52
53 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
54 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
55
56 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
57
58 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
59 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
60
61 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
62
63 SND_SOC_DAPM_INPUT("TELIN"),
64 SND_SOC_DAPM_INPUT("MIC"),
65 SND_SOC_DAPM_INPUT("AGCIN"),
66};
67
68static const struct snd_soc_dapm_route cx20442_audio_map[] = {
69 {"TELOUT", NULL, "TELOUT Amp"},
70
71 {"SPKOUT", NULL, "SPKOUT Mixer"},
72 {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
73
74 {"TELOUT Amp", NULL, "DAC"},
75 {"SPKOUT Amp", NULL, "DAC"},
76
77 {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
78 {"SPKOUT AGC", NULL, "AGCIN"},
79
80 {"AGCOUT", NULL, "MIC AGC"},
81 {"MIC AGC", NULL, "MIC"},
82
83 {"MIC Bias", NULL, "MIC"},
84 {"Input Mixer", NULL, "MIC Bias"},
85
86 {"TELIN Bias", NULL, "TELIN"},
87 {"Input Mixer", NULL, "TELIN Bias"},
88
89 {"ADC", NULL, "Input Mixer"},
90};
91
Kuninori Morimoto3c897242018-01-16 02:00:18 +000092static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
93 unsigned int reg)
94{
95 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
96
97 if (reg >= 1)
98 return -EINVAL;
99
100 return cx20442->reg_cache;
101}
102
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200103enum v253_vls {
104 V253_VLS_NONE = 0,
105 V253_VLS_T,
106 V253_VLS_L,
107 V253_VLS_LT,
108 V253_VLS_S,
109 V253_VLS_ST,
110 V253_VLS_M,
111 V253_VLS_MST,
112 V253_VLS_S1,
113 V253_VLS_S1T,
114 V253_VLS_MS1T,
115 V253_VLS_M1,
116 V253_VLS_M1ST,
117 V253_VLS_M1S1T,
118 V253_VLS_H,
119 V253_VLS_HT,
120 V253_VLS_MS,
121 V253_VLS_MS1,
122 V253_VLS_M1S,
123 V253_VLS_M1S1,
124 V253_VLS_TEST,
125};
126
127static int cx20442_pm_to_v253_vls(u8 value)
128{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200129 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200130 case 0:
131 return V253_VLS_T;
132 case (1 << CX20442_SPKOUT):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200133 case (1 << CX20442_MIC):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200134 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
135 return V253_VLS_M1S1;
136 case (1 << CX20442_TELOUT):
137 case (1 << CX20442_TELIN):
138 case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
139 return V253_VLS_L;
140 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
141 return V253_VLS_NONE;
142 }
143 return -EINVAL;
144}
145static int cx20442_pm_to_v253_vsp(u8 value)
146{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200147 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200148 case (1 << CX20442_SPKOUT):
149 case (1 << CX20442_MIC):
150 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
151 return (bool)(value & (1 << CX20442_AGC));
152 }
153 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
154}
155
156static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
157 unsigned int value)
158{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200160 int vls, vsp, old, len;
161 char buf[18];
162
Kuninori Morimoto3c897242018-01-16 02:00:18 +0000163 if (reg >= 1)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200164 return -EINVAL;
165
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000166 /* tty and write 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 */
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000168 if (!cx20442->tty || !cx20442->tty->ops->write)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200169 return -EIO;
170
Kuninori Morimoto3c897242018-01-16 02:00:18 +0000171 old = cx20442->reg_cache;
172 cx20442->reg_cache = value;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200173
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);
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000197 if (cx20442->tty->ops->write(cx20442->tty, buf, len) != len)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200198 return -EIO;
199
200 return 0;
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
Janusz Krzysztofik86c0ae72016-06-16 22:04:35 +0200228 tty->receive_room = 16;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200229 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 */
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000254 cx20442->tty = NULL;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200255 codec->component.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
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000277 if (!cx20442->tty) {
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200278 /* First modem response, complete setup procedure */
279
280 /* Set up codec driver access to modem controls */
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000281 cx20442->tty = tty;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200282 codec->component.card->pop_time = 1;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200283 }
284}
285
286/* Line discipline .write_wakeup() */
287static void v253_wakeup(struct tty_struct *tty)
288{
289}
290
291struct tty_ldisc_ops v253_ops = {
292 .magic = TTY_LDISC_MAGIC,
293 .name = "cx20442",
294 .owner = THIS_MODULE,
295 .open = v253_open,
296 .close = v253_close,
297 .hangup = v253_hangup,
298 .receive_buf = v253_receive,
299 .write_wakeup = v253_wakeup,
300};
301EXPORT_SYMBOL_GPL(v253_ops);
302
303
304/*
305 * Codec DAI
306 */
307
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000308static struct snd_soc_dai_driver cx20442_dai = {
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200309 .name = "cx20442-voice",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200310 .playback = {
311 .stream_name = "Playback",
312 .channels_min = 1,
313 .channels_max = 1,
314 .rates = SNDRV_PCM_RATE_8000,
315 .formats = SNDRV_PCM_FMTBIT_S16_LE,
316 },
317 .capture = {
318 .stream_name = "Capture",
319 .channels_min = 1,
320 .channels_max = 1,
321 .rates = SNDRV_PCM_RATE_8000,
322 .formats = SNDRV_PCM_FMTBIT_S16_LE,
323 },
324};
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200325
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100326static int cx20442_set_bias_level(struct snd_soc_codec *codec,
327 enum snd_soc_bias_level level)
328{
329 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
330 int err = 0;
331
332 switch (level) {
333 case SND_SOC_BIAS_PREPARE:
Lars-Peter Clausen3f36f3c2015-05-11 09:42:29 +0200334 if (snd_soc_codec_get_bias_level(codec) != SND_SOC_BIAS_STANDBY)
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100335 break;
336 if (IS_ERR(cx20442->por))
337 err = PTR_ERR(cx20442->por);
338 else
339 err = regulator_enable(cx20442->por);
340 break;
341 case SND_SOC_BIAS_STANDBY:
Lars-Peter Clausen3f36f3c2015-05-11 09:42:29 +0200342 if (snd_soc_codec_get_bias_level(codec) != SND_SOC_BIAS_PREPARE)
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100343 break;
344 if (IS_ERR(cx20442->por))
345 err = PTR_ERR(cx20442->por);
346 else
347 err = regulator_disable(cx20442->por);
348 break;
349 default:
350 break;
351 }
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100352
353 return err;
354}
355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000356static int cx20442_codec_probe(struct snd_soc_codec *codec)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200357{
358 struct cx20442_priv *cx20442;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200359
360 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
361 if (cx20442 == NULL)
362 return -ENOMEM;
363
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100364 cx20442->por = regulator_get(codec->dev, "POR");
365 if (IS_ERR(cx20442->por))
366 dev_warn(codec->dev, "failed to get the regulator");
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000367 cx20442->tty = NULL;
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100368
369 snd_soc_codec_set_drvdata(codec, cx20442);
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200370 codec->component.card->pop_time = 0;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200371
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000372 return 0;
373}
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200374
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000375/* power down chip */
376static int cx20442_codec_remove(struct snd_soc_codec *codec)
377{
378 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
379
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000380 if (cx20442->tty) {
381 struct tty_struct *tty = cx20442->tty;
Mark Brownaaed2a62014-03-04 17:20:56 +0800382 tty_hangup(tty);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383 }
384
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100385 if (!IS_ERR(cx20442->por)) {
386 /* should be already in STANDBY, hence disabled */
387 regulator_put(cx20442->por);
388 }
389
390 snd_soc_codec_set_drvdata(codec, NULL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391 kfree(cx20442);
392 return 0;
393}
394
Bhumika Goyala180ba42017-08-03 21:30:19 +0530395static const struct snd_soc_codec_driver cx20442_codec_dev = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000396 .probe = cx20442_codec_probe,
397 .remove = cx20442_codec_remove,
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100398 .set_bias_level = cx20442_set_bias_level,
Kuninori Morimoto3c897242018-01-16 02:00:18 +0000399 .read = cx20442_read_reg_cache,
400 .write = cx20442_write,
Kuninori Morimoto39b5a0f2017-11-14 01:04:42 +0000401
Kuninori Morimotoa2c9c0f2016-08-08 09:14:19 +0000402 .component_driver = {
403 .dapm_widgets = cx20442_dapm_widgets,
404 .num_dapm_widgets = ARRAY_SIZE(cx20442_dapm_widgets),
405 .dapm_routes = cx20442_audio_map,
406 .num_dapm_routes = ARRAY_SIZE(cx20442_audio_map),
407 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000408};
409
410static int cx20442_platform_probe(struct platform_device *pdev)
411{
412 return snd_soc_register_codec(&pdev->dev,
413 &cx20442_codec_dev, &cx20442_dai, 1);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200414}
415
Dmitry Torokhov32556392015-03-09 11:15:28 -0700416static int cx20442_platform_remove(struct platform_device *pdev)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200417{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 snd_soc_unregister_codec(&pdev->dev);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200419 return 0;
420}
421
422static struct platform_driver cx20442_platform_driver = {
423 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 .name = "cx20442-codec",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200425 },
426 .probe = cx20442_platform_probe,
Dmitry Torokhov32556392015-03-09 11:15:28 -0700427 .remove = cx20442_platform_remove,
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200428};
429
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000430module_platform_driver(cx20442_platform_driver);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200431
432MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
433MODULE_AUTHOR("Janusz Krzysztofik");
434MODULE_LICENSE("GPL");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000435MODULE_ALIAS("platform:cx20442-codec");