1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * cx20442.c -- CX20442 ALSA Soc Audio driver
5 * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
7 * Initially based on sound/soc/codecs/wm8400.c
8 * Copyright 2008, 2009 Wolfson Microelectronics PLC.
9 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
12 #include <linux/tty.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
17 #include <sound/core.h>
18 #include <sound/initval.h>
19 #include <sound/soc.h>
25 struct tty_struct *tty;
26 struct regulator *por;
30 #define CX20442_PM 0x0
32 #define CX20442_TELIN 0
33 #define CX20442_TELOUT 1
35 #define CX20442_SPKOUT 3
38 static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
39 SND_SOC_DAPM_OUTPUT("TELOUT"),
40 SND_SOC_DAPM_OUTPUT("SPKOUT"),
41 SND_SOC_DAPM_OUTPUT("AGCOUT"),
43 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
45 SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
46 SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
47 SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
49 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
50 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
52 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
54 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
55 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
57 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
59 SND_SOC_DAPM_INPUT("TELIN"),
60 SND_SOC_DAPM_INPUT("MIC"),
61 SND_SOC_DAPM_INPUT("AGCIN"),
64 static const struct snd_soc_dapm_route cx20442_audio_map[] = {
65 {"TELOUT", NULL, "TELOUT Amp"},
67 {"SPKOUT", NULL, "SPKOUT Mixer"},
68 {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
70 {"TELOUT Amp", NULL, "DAC"},
71 {"SPKOUT Amp", NULL, "DAC"},
73 {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
74 {"SPKOUT AGC", NULL, "AGCIN"},
76 {"AGCOUT", NULL, "MIC AGC"},
77 {"MIC AGC", NULL, "MIC"},
79 {"MIC Bias", NULL, "MIC"},
80 {"Input Mixer", NULL, "MIC Bias"},
82 {"TELIN Bias", NULL, "TELIN"},
83 {"Input Mixer", NULL, "TELIN Bias"},
85 {"ADC", NULL, "Input Mixer"},
88 static unsigned int cx20442_read_reg_cache(struct snd_soc_component *component,
91 struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
96 return cx20442->reg_cache;
123 static int cx20442_pm_to_v253_vls(u8 value)
125 switch (value & ~(1 << CX20442_AGC)) {
128 case (1 << CX20442_SPKOUT):
129 case (1 << CX20442_MIC):
130 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
131 return V253_VLS_M1S1;
132 case (1 << CX20442_TELOUT):
133 case (1 << CX20442_TELIN):
134 case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
136 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
137 return V253_VLS_NONE;
141 static int cx20442_pm_to_v253_vsp(u8 value)
143 switch (value & ~(1 << CX20442_AGC)) {
144 case (1 << CX20442_SPKOUT):
145 case (1 << CX20442_MIC):
146 case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
147 return (bool)(value & (1 << CX20442_AGC));
149 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
152 static int cx20442_write(struct snd_soc_component *component, unsigned int reg,
155 struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
156 int vls, vsp, old, len;
162 /* tty and write pointers required for talking to the modem
163 * are expected to be set by the line discipline initialization code */
164 if (!cx20442->tty || !cx20442->tty->ops->write)
167 old = cx20442->reg_cache;
168 cx20442->reg_cache = value;
170 vls = cx20442_pm_to_v253_vls(value);
174 vsp = cx20442_pm_to_v253_vsp(value);
178 if ((vls == V253_VLS_T) ||
179 (vls == cx20442_pm_to_v253_vls(old))) {
180 if (vsp == cx20442_pm_to_v253_vsp(old))
182 len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
183 } else if (vsp == cx20442_pm_to_v253_vsp(old))
184 len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
186 len = snprintf(buf, ARRAY_SIZE(buf),
187 "at+vls=%d;+vsp=%d\r", vls, vsp);
189 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
192 dev_dbg(component->dev, "%s: %s\n", __func__, buf);
193 if (cx20442->tty->ops->write(cx20442->tty, buf, len) != len)
200 * Line discpline related code
202 * Any of the callback functions below can be used in two ways:
203 * 1) registerd by a machine driver as one of line discipline operations,
204 * 2) called from a machine's provided line discipline callback function
205 * in case when extra machine specific code must be run as well.
208 /* Modem init: echo off, digital speaker off, quiet off, voice mode */
209 static const char v253_init[] = "ate0m0q0+fclass=8\r";
211 /* Line discipline .open() */
212 static int v253_open(struct tty_struct *tty)
214 int ret, len = strlen(v253_init);
216 /* Doesn't make sense without write callback */
217 if (!tty->ops->write)
220 /* Won't work if no codec pointer has been passed by a card driver */
224 tty->receive_room = 16;
225 if (tty->ops->write(tty, v253_init, len) != len) {
229 /* Actual setup will be performed after the modem responds. */
232 tty->disc_data = NULL;
236 /* Line discipline .close() */
237 static void v253_close(struct tty_struct *tty)
239 struct snd_soc_component *component = tty->disc_data;
240 struct cx20442_priv *cx20442;
242 tty->disc_data = NULL;
247 cx20442 = snd_soc_component_get_drvdata(component);
249 /* Prevent the codec driver from further accessing the modem */
251 component->card->pop_time = 0;
254 /* Line discipline .hangup() */
255 static int v253_hangup(struct tty_struct *tty)
261 /* Line discipline .receive_buf() */
262 static void v253_receive(struct tty_struct *tty, const unsigned char *cp,
263 const char *fp, int count)
265 struct snd_soc_component *component = tty->disc_data;
266 struct cx20442_priv *cx20442;
271 cx20442 = snd_soc_component_get_drvdata(component);
274 /* First modem response, complete setup procedure */
276 /* Set up codec driver access to modem controls */
278 component->card->pop_time = 1;
282 struct tty_ldisc_ops v253_ops = {
284 .owner = THIS_MODULE,
287 .hangup = v253_hangup,
288 .receive_buf = v253_receive,
290 EXPORT_SYMBOL_GPL(v253_ops);
297 static struct snd_soc_dai_driver cx20442_dai = {
298 .name = "cx20442-voice",
300 .stream_name = "Playback",
303 .rates = SNDRV_PCM_RATE_8000,
304 .formats = SNDRV_PCM_FMTBIT_S16_LE,
307 .stream_name = "Capture",
310 .rates = SNDRV_PCM_RATE_8000,
311 .formats = SNDRV_PCM_FMTBIT_S16_LE,
315 static int cx20442_set_bias_level(struct snd_soc_component *component,
316 enum snd_soc_bias_level level)
318 struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
322 case SND_SOC_BIAS_PREPARE:
323 if (snd_soc_component_get_bias_level(component) != SND_SOC_BIAS_STANDBY)
325 if (IS_ERR(cx20442->por))
326 err = PTR_ERR(cx20442->por);
328 err = regulator_enable(cx20442->por);
330 case SND_SOC_BIAS_STANDBY:
331 if (snd_soc_component_get_bias_level(component) != SND_SOC_BIAS_PREPARE)
333 if (IS_ERR(cx20442->por))
334 err = PTR_ERR(cx20442->por);
336 err = regulator_disable(cx20442->por);
345 static int cx20442_component_probe(struct snd_soc_component *component)
347 struct cx20442_priv *cx20442;
349 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
353 cx20442->por = regulator_get(component->dev, "POR");
354 if (IS_ERR(cx20442->por)) {
355 int err = PTR_ERR(cx20442->por);
357 dev_warn(component->dev, "failed to get POR supply (%d)", err);
359 * When running on a non-dt platform and requested regulator
360 * is not available, regulator_get() never returns
361 * -EPROBE_DEFER as it is not able to justify if the regulator
362 * may still appear later. On the other hand, the board can
363 * still set full constraints flag at late_initcall in order
364 * to instruct regulator_get() to return a dummy one if
365 * sufficient. Hence, if we get -ENODEV here, let's convert
366 * it to -EPROBE_DEFER and wait for the board to decide or
367 * let Deferred Probe infrastructure handle this error.
377 snd_soc_component_set_drvdata(component, cx20442);
378 component->card->pop_time = 0;
383 /* power down chip */
384 static void cx20442_component_remove(struct snd_soc_component *component)
386 struct cx20442_priv *cx20442 = snd_soc_component_get_drvdata(component);
389 struct tty_struct *tty = cx20442->tty;
393 if (!IS_ERR(cx20442->por)) {
394 /* should be already in STANDBY, hence disabled */
395 regulator_put(cx20442->por);
398 snd_soc_component_set_drvdata(component, NULL);
402 static const struct snd_soc_component_driver cx20442_component_dev = {
403 .probe = cx20442_component_probe,
404 .remove = cx20442_component_remove,
405 .set_bias_level = cx20442_set_bias_level,
406 .read = cx20442_read_reg_cache,
407 .write = cx20442_write,
408 .dapm_widgets = cx20442_dapm_widgets,
409 .num_dapm_widgets = ARRAY_SIZE(cx20442_dapm_widgets),
410 .dapm_routes = cx20442_audio_map,
411 .num_dapm_routes = ARRAY_SIZE(cx20442_audio_map),
413 .use_pmdown_time = 1,
415 .non_legacy_dai_naming = 1,
418 static int cx20442_platform_probe(struct platform_device *pdev)
420 return devm_snd_soc_register_component(&pdev->dev,
421 &cx20442_component_dev, &cx20442_dai, 1);
424 static struct platform_driver cx20442_platform_driver = {
426 .name = "cx20442-codec",
428 .probe = cx20442_platform_probe,
431 module_platform_driver(cx20442_platform_driver);
433 MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
434 MODULE_AUTHOR("Janusz Krzysztofik");
435 MODULE_LICENSE("GPL");
436 MODULE_ALIAS("platform:cx20442-codec");