2 * cx20442.c -- CX20442 ALSA Soc Audio driver
4 * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6 * Initially based on sound/soc/codecs/wm8400.c
7 * Copyright 2008, 2009 Wolfson Microelectronics PLC.
8 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
16 #include <linux/tty.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/regulator/consumer.h>
21 #include <sound/core.h>
22 #include <sound/initval.h>
23 #include <sound/soc.h>
29 struct tty_struct
*tty
;
30 struct regulator
*por
;
34 #define CX20442_PM 0x0
36 #define CX20442_TELIN 0
37 #define CX20442_TELOUT 1
39 #define CX20442_SPKOUT 3
42 static 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"),
47 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
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),
53 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM
, 0, 0),
54 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
56 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
58 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM
, CX20442_TELIN
, 0),
59 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM
, CX20442_MIC
, 0),
61 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM
, CX20442_AGC
, 0, NULL
, 0),
63 SND_SOC_DAPM_INPUT("TELIN"),
64 SND_SOC_DAPM_INPUT("MIC"),
65 SND_SOC_DAPM_INPUT("AGCIN"),
68 static const struct snd_soc_dapm_route cx20442_audio_map
[] = {
69 {"TELOUT", NULL
, "TELOUT Amp"},
71 {"SPKOUT", NULL
, "SPKOUT Mixer"},
72 {"SPKOUT Mixer", NULL
, "SPKOUT Amp"},
74 {"TELOUT Amp", NULL
, "DAC"},
75 {"SPKOUT Amp", NULL
, "DAC"},
77 {"SPKOUT Mixer", NULL
, "SPKOUT AGC"},
78 {"SPKOUT AGC", NULL
, "AGCIN"},
80 {"AGCOUT", NULL
, "MIC AGC"},
81 {"MIC AGC", NULL
, "MIC"},
83 {"MIC Bias", NULL
, "MIC"},
84 {"Input Mixer", NULL
, "MIC Bias"},
86 {"TELIN Bias", NULL
, "TELIN"},
87 {"Input Mixer", NULL
, "TELIN Bias"},
89 {"ADC", NULL
, "Input Mixer"},
92 static unsigned int cx20442_read_reg_cache(struct snd_soc_component
*component
,
95 struct cx20442_priv
*cx20442
= snd_soc_component_get_drvdata(component
);
100 return cx20442
->reg_cache
;
127 static int cx20442_pm_to_v253_vls(u8 value
)
129 switch (value
& ~(1 << CX20442_AGC
)) {
132 case (1 << CX20442_SPKOUT
):
133 case (1 << CX20442_MIC
):
134 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
):
140 case (1 << CX20442_TELOUT
) | (1 << CX20442_MIC
):
141 return V253_VLS_NONE
;
145 static int cx20442_pm_to_v253_vsp(u8 value
)
147 switch (value
& ~(1 << CX20442_AGC
)) {
148 case (1 << CX20442_SPKOUT
):
149 case (1 << CX20442_MIC
):
150 case (1 << CX20442_SPKOUT
) | (1 << CX20442_MIC
):
151 return (bool)(value
& (1 << CX20442_AGC
));
153 return (value
& (1 << CX20442_AGC
)) ? -EINVAL
: 0;
156 static int cx20442_write(struct snd_soc_component
*component
, unsigned int reg
,
159 struct cx20442_priv
*cx20442
= snd_soc_component_get_drvdata(component
);
160 int vls
, vsp
, old
, len
;
166 /* tty and write pointers required for talking to the modem
167 * are expected to be set by the line discipline initialization code */
168 if (!cx20442
->tty
|| !cx20442
->tty
->ops
->write
)
171 old
= cx20442
->reg_cache
;
172 cx20442
->reg_cache
= value
;
174 vls
= cx20442_pm_to_v253_vls(value
);
178 vsp
= cx20442_pm_to_v253_vsp(value
);
182 if ((vls
== V253_VLS_T
) ||
183 (vls
== cx20442_pm_to_v253_vls(old
))) {
184 if (vsp
== cx20442_pm_to_v253_vsp(old
))
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
);
190 len
= snprintf(buf
, ARRAY_SIZE(buf
),
191 "at+vls=%d;+vsp=%d\r", vls
, vsp
);
193 if (unlikely(len
> (ARRAY_SIZE(buf
) - 1)))
196 dev_dbg(component
->dev
, "%s: %s\n", __func__
, buf
);
197 if (cx20442
->tty
->ops
->write(cx20442
->tty
, buf
, len
) != len
)
204 * Line discpline related code
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.
212 /* Modem init: echo off, digital speaker off, quiet off, voice mode */
213 static const char *v253_init
= "ate0m0q0+fclass=8\r";
215 /* Line discipline .open() */
216 static int v253_open(struct tty_struct
*tty
)
218 int ret
, len
= strlen(v253_init
);
220 /* Doesn't make sense without write callback */
221 if (!tty
->ops
->write
)
224 /* Won't work if no codec pointer has been passed by a card driver */
228 tty
->receive_room
= 16;
229 if (tty
->ops
->write(tty
, v253_init
, len
) != len
) {
233 /* Actual setup will be performed after the modem responds. */
236 tty
->disc_data
= NULL
;
240 /* Line discipline .close() */
241 static void v253_close(struct tty_struct
*tty
)
243 struct snd_soc_component
*component
= tty
->disc_data
;
244 struct cx20442_priv
*cx20442
;
246 tty
->disc_data
= NULL
;
251 cx20442
= snd_soc_component_get_drvdata(component
);
253 /* Prevent the codec driver from further accessing the modem */
255 component
->card
->pop_time
= 0;
258 /* Line discipline .hangup() */
259 static int v253_hangup(struct tty_struct
*tty
)
265 /* Line discipline .receive_buf() */
266 static void v253_receive(struct tty_struct
*tty
,
267 const unsigned char *cp
, char *fp
, int count
)
269 struct snd_soc_component
*component
= tty
->disc_data
;
270 struct cx20442_priv
*cx20442
;
275 cx20442
= snd_soc_component_get_drvdata(component
);
278 /* First modem response, complete setup procedure */
280 /* Set up codec driver access to modem controls */
282 component
->card
->pop_time
= 1;
286 /* Line discipline .write_wakeup() */
287 static void v253_wakeup(struct tty_struct
*tty
)
291 struct tty_ldisc_ops v253_ops
= {
292 .magic
= TTY_LDISC_MAGIC
,
294 .owner
= THIS_MODULE
,
297 .hangup
= v253_hangup
,
298 .receive_buf
= v253_receive
,
299 .write_wakeup
= v253_wakeup
,
301 EXPORT_SYMBOL_GPL(v253_ops
);
308 static struct snd_soc_dai_driver cx20442_dai
= {
309 .name
= "cx20442-voice",
311 .stream_name
= "Playback",
314 .rates
= SNDRV_PCM_RATE_8000
,
315 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
318 .stream_name
= "Capture",
321 .rates
= SNDRV_PCM_RATE_8000
,
322 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
326 static int cx20442_set_bias_level(struct snd_soc_component
*component
,
327 enum snd_soc_bias_level level
)
329 struct cx20442_priv
*cx20442
= snd_soc_component_get_drvdata(component
);
333 case SND_SOC_BIAS_PREPARE
:
334 if (snd_soc_component_get_bias_level(component
) != SND_SOC_BIAS_STANDBY
)
336 if (IS_ERR(cx20442
->por
))
337 err
= PTR_ERR(cx20442
->por
);
339 err
= regulator_enable(cx20442
->por
);
341 case SND_SOC_BIAS_STANDBY
:
342 if (snd_soc_component_get_bias_level(component
) != SND_SOC_BIAS_PREPARE
)
344 if (IS_ERR(cx20442
->por
))
345 err
= PTR_ERR(cx20442
->por
);
347 err
= regulator_disable(cx20442
->por
);
356 static int cx20442_component_probe(struct snd_soc_component
*component
)
358 struct cx20442_priv
*cx20442
;
360 cx20442
= kzalloc(sizeof(struct cx20442_priv
), GFP_KERNEL
);
364 cx20442
->por
= regulator_get(component
->dev
, "POR");
365 if (IS_ERR(cx20442
->por
)) {
366 int err
= PTR_ERR(cx20442
->por
);
368 dev_warn(component
->dev
, "failed to get POR supply (%d)", err
);
370 * When running on a non-dt platform and requested regulator
371 * is not available, regulator_get() never returns
372 * -EPROBE_DEFER as it is not able to justify if the regulator
373 * may still appear later. On the other hand, the board can
374 * still set full constraints flag at late_initcall in order
375 * to instruct regulator_get() to return a dummy one if
376 * sufficient. Hence, if we get -ENODEV here, let's convert
377 * it to -EPROBE_DEFER and wait for the board to decide or
378 * let Deferred Probe infrastructure handle this error.
388 snd_soc_component_set_drvdata(component
, cx20442
);
389 component
->card
->pop_time
= 0;
394 /* power down chip */
395 static void cx20442_component_remove(struct snd_soc_component
*component
)
397 struct cx20442_priv
*cx20442
= snd_soc_component_get_drvdata(component
);
400 struct tty_struct
*tty
= cx20442
->tty
;
404 if (!IS_ERR(cx20442
->por
)) {
405 /* should be already in STANDBY, hence disabled */
406 regulator_put(cx20442
->por
);
409 snd_soc_component_set_drvdata(component
, NULL
);
413 static const struct snd_soc_component_driver cx20442_component_dev
= {
414 .probe
= cx20442_component_probe
,
415 .remove
= cx20442_component_remove
,
416 .set_bias_level
= cx20442_set_bias_level
,
417 .read
= cx20442_read_reg_cache
,
418 .write
= cx20442_write
,
419 .dapm_widgets
= cx20442_dapm_widgets
,
420 .num_dapm_widgets
= ARRAY_SIZE(cx20442_dapm_widgets
),
421 .dapm_routes
= cx20442_audio_map
,
422 .num_dapm_routes
= ARRAY_SIZE(cx20442_audio_map
),
424 .use_pmdown_time
= 1,
426 .non_legacy_dai_naming
= 1,
429 static int cx20442_platform_probe(struct platform_device
*pdev
)
431 return devm_snd_soc_register_component(&pdev
->dev
,
432 &cx20442_component_dev
, &cx20442_dai
, 1);
435 static struct platform_driver cx20442_platform_driver
= {
437 .name
= "cx20442-codec",
439 .probe
= cx20442_platform_probe
,
442 module_platform_driver(cx20442_platform_driver
);
444 MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
445 MODULE_AUTHOR("Janusz Krzysztofik");
446 MODULE_LICENSE("GPL");
447 MODULE_ALIAS("platform:cx20442-codec");