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>
30 struct regulator
*por
;
33 #define CX20442_PM 0x0
35 #define CX20442_TELIN 0
36 #define CX20442_TELOUT 1
38 #define CX20442_SPKOUT 3
41 static 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"),
46 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
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),
52 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM
, 0, 0),
53 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
55 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
57 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM
, CX20442_TELIN
, 0),
58 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM
, CX20442_MIC
, 0),
60 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM
, CX20442_AGC
, 0, NULL
, 0),
62 SND_SOC_DAPM_INPUT("TELIN"),
63 SND_SOC_DAPM_INPUT("MIC"),
64 SND_SOC_DAPM_INPUT("AGCIN"),
67 static const struct snd_soc_dapm_route cx20442_audio_map
[] = {
68 {"TELOUT", NULL
, "TELOUT Amp"},
70 {"SPKOUT", NULL
, "SPKOUT Mixer"},
71 {"SPKOUT Mixer", NULL
, "SPKOUT Amp"},
73 {"TELOUT Amp", NULL
, "DAC"},
74 {"SPKOUT Amp", NULL
, "DAC"},
76 {"SPKOUT Mixer", NULL
, "SPKOUT AGC"},
77 {"SPKOUT AGC", NULL
, "AGCIN"},
79 {"AGCOUT", NULL
, "MIC AGC"},
80 {"MIC AGC", NULL
, "MIC"},
82 {"MIC Bias", NULL
, "MIC"},
83 {"Input Mixer", NULL
, "MIC Bias"},
85 {"TELIN Bias", NULL
, "TELIN"},
86 {"Input Mixer", NULL
, "TELIN Bias"},
88 {"ADC", NULL
, "Input Mixer"},
91 static unsigned int cx20442_read_reg_cache(struct snd_soc_codec
*codec
,
94 u8
*reg_cache
= codec
->reg_cache
;
96 if (reg
>= codec
->driver
->reg_cache_size
)
99 return reg_cache
[reg
];
126 static int cx20442_pm_to_v253_vls(u8 value
)
128 switch (value
& ~(1 << CX20442_AGC
)) {
131 case (1 << CX20442_SPKOUT
):
132 case (1 << CX20442_MIC
):
133 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
):
139 case (1 << CX20442_TELOUT
) | (1 << CX20442_MIC
):
140 return V253_VLS_NONE
;
144 static int cx20442_pm_to_v253_vsp(u8 value
)
146 switch (value
& ~(1 << CX20442_AGC
)) {
147 case (1 << CX20442_SPKOUT
):
148 case (1 << CX20442_MIC
):
149 case (1 << CX20442_SPKOUT
) | (1 << CX20442_MIC
):
150 return (bool)(value
& (1 << CX20442_AGC
));
152 return (value
& (1 << CX20442_AGC
)) ? -EINVAL
: 0;
155 static int cx20442_write(struct snd_soc_codec
*codec
, unsigned int reg
,
158 struct cx20442_priv
*cx20442
= snd_soc_codec_get_drvdata(codec
);
159 u8
*reg_cache
= codec
->reg_cache
;
160 int vls
, vsp
, old
, len
;
163 if (reg
>= codec
->driver
->reg_cache_size
)
166 /* hw_write and control_data pointers required for talking to the modem
167 * are expected to be set by the line discipline initialization code */
168 if (!codec
->hw_write
|| !cx20442
->control_data
)
171 old
= reg_cache
[reg
];
172 reg_cache
[reg
] = 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(codec
->dev
, "%s: %s\n", __func__
, buf
);
197 if (codec
->hw_write(cx20442
->control_data
, buf
, len
) != len
)
205 * Line discpline related code
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.
213 /* Modem init: echo off, digital speaker off, quiet off, voice mode */
214 static const char *v253_init
= "ate0m0q0+fclass=8\r";
216 /* Line discipline .open() */
217 static int v253_open(struct tty_struct
*tty
)
219 int ret
, len
= strlen(v253_init
);
221 /* Doesn't make sense without write callback */
222 if (!tty
->ops
->write
)
225 /* Won't work if no codec pointer has been passed by a card driver */
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_codec
*codec
= tty
->disc_data
;
244 struct cx20442_priv
*cx20442
;
246 tty
->disc_data
= NULL
;
251 cx20442
= snd_soc_codec_get_drvdata(codec
);
253 /* Prevent the codec driver from further accessing the modem */
254 codec
->hw_write
= NULL
;
255 cx20442
->control_data
= NULL
;
256 codec
->component
.card
->pop_time
= 0;
259 /* Line discipline .hangup() */
260 static int v253_hangup(struct tty_struct
*tty
)
266 /* Line discipline .receive_buf() */
267 static void v253_receive(struct tty_struct
*tty
,
268 const unsigned char *cp
, char *fp
, int count
)
270 struct snd_soc_codec
*codec
= tty
->disc_data
;
271 struct cx20442_priv
*cx20442
;
276 cx20442
= snd_soc_codec_get_drvdata(codec
);
278 if (!cx20442
->control_data
) {
279 /* First modem response, complete setup procedure */
281 /* Set up codec driver access to modem controls */
282 cx20442
->control_data
= tty
;
283 codec
->hw_write
= (hw_write_t
)tty
->ops
->write
;
284 codec
->component
.card
->pop_time
= 1;
288 /* Line discipline .write_wakeup() */
289 static void v253_wakeup(struct tty_struct
*tty
)
293 struct tty_ldisc_ops v253_ops
= {
294 .magic
= TTY_LDISC_MAGIC
,
296 .owner
= THIS_MODULE
,
299 .hangup
= v253_hangup
,
300 .receive_buf
= v253_receive
,
301 .write_wakeup
= v253_wakeup
,
303 EXPORT_SYMBOL_GPL(v253_ops
);
310 static struct snd_soc_dai_driver cx20442_dai
= {
311 .name
= "cx20442-voice",
313 .stream_name
= "Playback",
316 .rates
= SNDRV_PCM_RATE_8000
,
317 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
320 .stream_name
= "Capture",
323 .rates
= SNDRV_PCM_RATE_8000
,
324 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
328 static int cx20442_set_bias_level(struct snd_soc_codec
*codec
,
329 enum snd_soc_bias_level level
)
331 struct cx20442_priv
*cx20442
= snd_soc_codec_get_drvdata(codec
);
335 case SND_SOC_BIAS_PREPARE
:
336 if (snd_soc_codec_get_bias_level(codec
) != SND_SOC_BIAS_STANDBY
)
338 if (IS_ERR(cx20442
->por
))
339 err
= PTR_ERR(cx20442
->por
);
341 err
= regulator_enable(cx20442
->por
);
343 case SND_SOC_BIAS_STANDBY
:
344 if (snd_soc_codec_get_bias_level(codec
) != SND_SOC_BIAS_PREPARE
)
346 if (IS_ERR(cx20442
->por
))
347 err
= PTR_ERR(cx20442
->por
);
349 err
= regulator_disable(cx20442
->por
);
358 static int cx20442_codec_probe(struct snd_soc_codec
*codec
)
360 struct cx20442_priv
*cx20442
;
362 cx20442
= kzalloc(sizeof(struct cx20442_priv
), GFP_KERNEL
);
366 cx20442
->por
= regulator_get(codec
->dev
, "POR");
367 if (IS_ERR(cx20442
->por
))
368 dev_warn(codec
->dev
, "failed to get the regulator");
369 cx20442
->control_data
= NULL
;
371 snd_soc_codec_set_drvdata(codec
, cx20442
);
372 codec
->hw_write
= NULL
;
373 codec
->component
.card
->pop_time
= 0;
378 /* power down chip */
379 static int cx20442_codec_remove(struct snd_soc_codec
*codec
)
381 struct cx20442_priv
*cx20442
= snd_soc_codec_get_drvdata(codec
);
383 if (cx20442
->control_data
) {
384 struct tty_struct
*tty
= cx20442
->control_data
;
388 if (!IS_ERR(cx20442
->por
)) {
389 /* should be already in STANDBY, hence disabled */
390 regulator_put(cx20442
->por
);
393 snd_soc_codec_set_drvdata(codec
, NULL
);
398 static const u8 cx20442_reg
;
400 static struct snd_soc_codec_driver cx20442_codec_dev
= {
401 .probe
= cx20442_codec_probe
,
402 .remove
= cx20442_codec_remove
,
403 .set_bias_level
= cx20442_set_bias_level
,
404 .reg_cache_default
= &cx20442_reg
,
406 .reg_word_size
= sizeof(u8
),
407 .read
= cx20442_read_reg_cache
,
408 .write
= cx20442_write
,
409 .dapm_widgets
= cx20442_dapm_widgets
,
410 .num_dapm_widgets
= ARRAY_SIZE(cx20442_dapm_widgets
),
411 .dapm_routes
= cx20442_audio_map
,
412 .num_dapm_routes
= ARRAY_SIZE(cx20442_audio_map
),
415 static int cx20442_platform_probe(struct platform_device
*pdev
)
417 return snd_soc_register_codec(&pdev
->dev
,
418 &cx20442_codec_dev
, &cx20442_dai
, 1);
421 static int cx20442_platform_remove(struct platform_device
*pdev
)
423 snd_soc_unregister_codec(&pdev
->dev
);
427 static struct platform_driver cx20442_platform_driver
= {
429 .name
= "cx20442-codec",
431 .probe
= cx20442_platform_probe
,
432 .remove
= cx20442_platform_remove
,
435 module_platform_driver(cx20442_platform_driver
);
437 MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
438 MODULE_AUTHOR("Janusz Krzysztofik");
439 MODULE_LICENSE("GPL");
440 MODULE_ALIAS("platform:cx20442-codec");