blob: 6b6f8e44369b01ea3342eaab01aa14413acf38c5 [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;
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 +020091enum v253_vls {
92 V253_VLS_NONE = 0,
93 V253_VLS_T,
94 V253_VLS_L,
95 V253_VLS_LT,
96 V253_VLS_S,
97 V253_VLS_ST,
98 V253_VLS_M,
99 V253_VLS_MST,
100 V253_VLS_S1,
101 V253_VLS_S1T,
102 V253_VLS_MS1T,
103 V253_VLS_M1,
104 V253_VLS_M1ST,
105 V253_VLS_M1S1T,
106 V253_VLS_H,
107 V253_VLS_HT,
108 V253_VLS_MS,
109 V253_VLS_MS1,
110 V253_VLS_M1S,
111 V253_VLS_M1S1,
112 V253_VLS_TEST,
113};
114
Kuninori Morimoto39b5a0f2017-11-14 01:04:42 +0000115#if 0
116/* FIXME : these function will be re-used */
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200117static int cx20442_pm_to_v253_vls(u8 value)
118{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200119 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200120 case 0:
121 return V253_VLS_T;
122 case (1 << CX20442_SPKOUT):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200123 case (1 << CX20442_MIC):
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200124 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
125 return V253_VLS_M1S1;
126 case (1 << CX20442_TELOUT):
127 case (1 << CX20442_TELIN):
128 case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
129 return V253_VLS_L;
130 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
131 return V253_VLS_NONE;
132 }
133 return -EINVAL;
134}
135static int cx20442_pm_to_v253_vsp(u8 value)
136{
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200137 switch (value & ~(1 << CX20442_AGC)) {
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200138 case (1 << CX20442_SPKOUT):
139 case (1 << CX20442_MIC):
140 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
141 return (bool)(value & (1 << CX20442_AGC));
142 }
143 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
144}
145
146static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
147 unsigned int value)
148{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200150 u8 *reg_cache = codec->reg_cache;
151 int vls, vsp, old, len;
152 char buf[18];
153
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000154 if (reg >= codec->driver->reg_cache_size)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200155 return -EINVAL;
156
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000157 /* tty and write pointers required for talking to the modem
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200158 * are expected to be set by the line discipline initialization code */
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000159 if (!cx20442->tty || !cx20442->tty->ops->write)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200160 return -EIO;
161
162 old = reg_cache[reg];
163 reg_cache[reg] = value;
164
165 vls = cx20442_pm_to_v253_vls(value);
166 if (vls < 0)
167 return vls;
168
169 vsp = cx20442_pm_to_v253_vsp(value);
Janusz Krzysztofikb84eab02009-07-28 20:24:12 +0200170 if (vsp < 0)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200171 return vsp;
172
173 if ((vls == V253_VLS_T) ||
174 (vls == cx20442_pm_to_v253_vls(old))) {
175 if (vsp == cx20442_pm_to_v253_vsp(old))
176 return 0;
177 len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
178 } else if (vsp == cx20442_pm_to_v253_vsp(old))
179 len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
180 else
181 len = snprintf(buf, ARRAY_SIZE(buf),
182 "at+vls=%d;+vsp=%d\r", vls, vsp);
183
184 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
185 return -ENOMEM;
186
Janusz Krzysztofik4977b032009-08-06 12:24:54 +0200187 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000188 if (cx20442->tty->ops->write(cx20442->tty, buf, len) != len)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200189 return -EIO;
190
191 return 0;
192}
Kuninori Morimoto39b5a0f2017-11-14 01:04:42 +0000193#endif
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200194
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200195/*
196 * Line discpline related code
197 *
198 * Any of the callback functions below can be used in two ways:
199 * 1) registerd by a machine driver as one of line discipline operations,
200 * 2) called from a machine's provided line discipline callback function
201 * in case when extra machine specific code must be run as well.
202 */
203
204/* Modem init: echo off, digital speaker off, quiet off, voice mode */
205static const char *v253_init = "ate0m0q0+fclass=8\r";
206
207/* Line discipline .open() */
208static int v253_open(struct tty_struct *tty)
209{
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200210 int ret, len = strlen(v253_init);
211
212 /* Doesn't make sense without write callback */
213 if (!tty->ops->write)
214 return -EINVAL;
215
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216 /* Won't work if no codec pointer has been passed by a card driver */
217 if (!tty->disc_data)
218 return -ENODEV;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200219
Janusz Krzysztofik86c0ae72016-06-16 22:04:35 +0200220 tty->receive_room = 16;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200221 if (tty->ops->write(tty, v253_init, len) != len) {
222 ret = -EIO;
223 goto err;
224 }
225 /* Actual setup will be performed after the modem responds. */
226 return 0;
227err:
228 tty->disc_data = NULL;
229 return ret;
230}
231
232/* Line discipline .close() */
233static void v253_close(struct tty_struct *tty)
234{
235 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000236 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200237
238 tty->disc_data = NULL;
239
240 if (!codec)
241 return;
242
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000243 cx20442 = snd_soc_codec_get_drvdata(codec);
244
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200245 /* Prevent the codec driver from further accessing the modem */
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000246 cx20442->tty = NULL;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200247 codec->component.card->pop_time = 0;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200248}
249
250/* Line discipline .hangup() */
251static int v253_hangup(struct tty_struct *tty)
252{
253 v253_close(tty);
254 return 0;
255}
256
257/* Line discipline .receive_buf() */
Linus Torvaldsbb3d6bf2011-06-04 07:00:50 +0900258static void v253_receive(struct tty_struct *tty,
259 const unsigned char *cp, char *fp, int count)
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200260{
261 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000262 struct cx20442_priv *cx20442;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200263
264 if (!codec)
Linus Torvaldsbb3d6bf2011-06-04 07:00:50 +0900265 return;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200266
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000267 cx20442 = snd_soc_codec_get_drvdata(codec);
268
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000269 if (!cx20442->tty) {
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200270 /* First modem response, complete setup procedure */
271
272 /* Set up codec driver access to modem controls */
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000273 cx20442->tty = tty;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200274 codec->component.card->pop_time = 1;
Janusz Krzysztofikad120da2009-07-29 12:24:46 +0200275 }
276}
277
278/* Line discipline .write_wakeup() */
279static void v253_wakeup(struct tty_struct *tty)
280{
281}
282
283struct tty_ldisc_ops v253_ops = {
284 .magic = TTY_LDISC_MAGIC,
285 .name = "cx20442",
286 .owner = THIS_MODULE,
287 .open = v253_open,
288 .close = v253_close,
289 .hangup = v253_hangup,
290 .receive_buf = v253_receive,
291 .write_wakeup = v253_wakeup,
292};
293EXPORT_SYMBOL_GPL(v253_ops);
294
295
296/*
297 * Codec DAI
298 */
299
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000300static struct snd_soc_dai_driver cx20442_dai = {
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200301 .name = "cx20442-voice",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200302 .playback = {
303 .stream_name = "Playback",
304 .channels_min = 1,
305 .channels_max = 1,
306 .rates = SNDRV_PCM_RATE_8000,
307 .formats = SNDRV_PCM_FMTBIT_S16_LE,
308 },
309 .capture = {
310 .stream_name = "Capture",
311 .channels_min = 1,
312 .channels_max = 1,
313 .rates = SNDRV_PCM_RATE_8000,
314 .formats = SNDRV_PCM_FMTBIT_S16_LE,
315 },
316};
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200317
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100318static int cx20442_set_bias_level(struct snd_soc_codec *codec,
319 enum snd_soc_bias_level level)
320{
321 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
322 int err = 0;
323
324 switch (level) {
325 case SND_SOC_BIAS_PREPARE:
Lars-Peter Clausen3f36f3c2015-05-11 09:42:29 +0200326 if (snd_soc_codec_get_bias_level(codec) != SND_SOC_BIAS_STANDBY)
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100327 break;
328 if (IS_ERR(cx20442->por))
329 err = PTR_ERR(cx20442->por);
330 else
331 err = regulator_enable(cx20442->por);
332 break;
333 case SND_SOC_BIAS_STANDBY:
Lars-Peter Clausen3f36f3c2015-05-11 09:42:29 +0200334 if (snd_soc_codec_get_bias_level(codec) != SND_SOC_BIAS_PREPARE)
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_disable(cx20442->por);
340 break;
341 default:
342 break;
343 }
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100344
345 return err;
346}
347
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000348static int cx20442_codec_probe(struct snd_soc_codec *codec)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200349{
350 struct cx20442_priv *cx20442;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200351
352 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
353 if (cx20442 == NULL)
354 return -ENOMEM;
355
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100356 cx20442->por = regulator_get(codec->dev, "POR");
357 if (IS_ERR(cx20442->por))
358 dev_warn(codec->dev, "failed to get the regulator");
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000359 cx20442->tty = NULL;
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100360
361 snd_soc_codec_set_drvdata(codec, cx20442);
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200362 codec->component.card->pop_time = 0;
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200363
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000364 return 0;
365}
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200366
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000367/* power down chip */
368static int cx20442_codec_remove(struct snd_soc_codec *codec)
369{
370 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
371
Kuninori Morimotofac3f5e2017-11-09 01:04:09 +0000372 if (cx20442->tty) {
373 struct tty_struct *tty = cx20442->tty;
Mark Brownaaed2a62014-03-04 17:20:56 +0800374 tty_hangup(tty);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000375 }
376
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100377 if (!IS_ERR(cx20442->por)) {
378 /* should be already in STANDBY, hence disabled */
379 regulator_put(cx20442->por);
380 }
381
382 snd_soc_codec_set_drvdata(codec, NULL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383 kfree(cx20442);
384 return 0;
385}
386
Janusz Krzysztofik8e6bfb92011-02-10 13:24:32 +0100387static const u8 cx20442_reg;
Janusz Krzysztofikf019ee52011-02-01 13:01:17 +0100388
Bhumika Goyala180ba42017-08-03 21:30:19 +0530389static const struct snd_soc_codec_driver cx20442_codec_dev = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000390 .probe = cx20442_codec_probe,
391 .remove = cx20442_codec_remove,
Janusz Krzysztofikf75a8ff2011-12-30 04:04:54 +0100392 .set_bias_level = cx20442_set_bias_level,
Kuninori Morimoto39b5a0f2017-11-14 01:04:42 +0000393
Kuninori Morimotoa2c9c0f2016-08-08 09:14:19 +0000394 .component_driver = {
395 .dapm_widgets = cx20442_dapm_widgets,
396 .num_dapm_widgets = ARRAY_SIZE(cx20442_dapm_widgets),
397 .dapm_routes = cx20442_audio_map,
398 .num_dapm_routes = ARRAY_SIZE(cx20442_audio_map),
399 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000400};
401
402static int cx20442_platform_probe(struct platform_device *pdev)
403{
404 return snd_soc_register_codec(&pdev->dev,
405 &cx20442_codec_dev, &cx20442_dai, 1);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200406}
407
Dmitry Torokhov32556392015-03-09 11:15:28 -0700408static int cx20442_platform_remove(struct platform_device *pdev)
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200409{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000410 snd_soc_unregister_codec(&pdev->dev);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200411 return 0;
412}
413
414static struct platform_driver cx20442_platform_driver = {
415 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000416 .name = "cx20442-codec",
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200417 },
418 .probe = cx20442_platform_probe,
Dmitry Torokhov32556392015-03-09 11:15:28 -0700419 .remove = cx20442_platform_remove,
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200420};
421
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000422module_platform_driver(cx20442_platform_driver);
Janusz Krzysztofik459dc352009-07-22 05:22:28 +0200423
424MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
425MODULE_AUTHOR("Janusz Krzysztofik");
426MODULE_LICENSE("GPL");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000427MODULE_ALIAS("platform:cx20442-codec");