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>
20 #include <sound/core.h>
21 #include <sound/initval.h>
22 #include <sound/soc.h>
28 enum snd_soc_control_type control_type
;
32 #define CX20442_PM 0x0
34 #define CX20442_TELIN 0
35 #define CX20442_TELOUT 1
37 #define CX20442_SPKOUT 3
40 static const struct snd_soc_dapm_widget cx20442_dapm_widgets
[] = {
41 SND_SOC_DAPM_OUTPUT("TELOUT"),
42 SND_SOC_DAPM_OUTPUT("SPKOUT"),
43 SND_SOC_DAPM_OUTPUT("AGCOUT"),
45 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
47 SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM
, CX20442_TELOUT
, 0, NULL
, 0),
48 SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM
, CX20442_SPKOUT
, 0, NULL
, 0),
49 SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM
, CX20442_AGC
, 0, NULL
, 0),
51 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM
, 0, 0),
52 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM
, 0, 0),
54 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
56 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM
, CX20442_TELIN
, 0),
57 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM
, CX20442_MIC
, 0),
59 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM
, CX20442_AGC
, 0, NULL
, 0),
61 SND_SOC_DAPM_INPUT("TELIN"),
62 SND_SOC_DAPM_INPUT("MIC"),
63 SND_SOC_DAPM_INPUT("AGCIN"),
66 static const struct snd_soc_dapm_route cx20442_audio_map
[] = {
67 {"TELOUT", NULL
, "TELOUT Amp"},
69 {"SPKOUT", NULL
, "SPKOUT Mixer"},
70 {"SPKOUT Mixer", NULL
, "SPKOUT Amp"},
72 {"TELOUT Amp", NULL
, "DAC"},
73 {"SPKOUT Amp", NULL
, "DAC"},
75 {"SPKOUT Mixer", NULL
, "SPKOUT AGC"},
76 {"SPKOUT AGC", NULL
, "AGCIN"},
78 {"AGCOUT", NULL
, "MIC AGC"},
79 {"MIC AGC", NULL
, "MIC"},
81 {"MIC Bias", NULL
, "MIC"},
82 {"Input Mixer", NULL
, "MIC Bias"},
84 {"TELIN Bias", NULL
, "TELIN"},
85 {"Input Mixer", NULL
, "TELIN Bias"},
87 {"ADC", NULL
, "Input Mixer"},
90 static unsigned int cx20442_read_reg_cache(struct snd_soc_codec
*codec
,
93 u8
*reg_cache
= codec
->reg_cache
;
95 if (reg
>= codec
->driver
->reg_cache_size
)
98 return reg_cache
[reg
];
125 static int cx20442_pm_to_v253_vls(u8 value
)
127 switch (value
& ~(1 << CX20442_AGC
)) {
130 case (1 << CX20442_SPKOUT
):
131 case (1 << CX20442_MIC
):
132 case (1 << CX20442_SPKOUT
) | (1 << CX20442_MIC
):
133 return V253_VLS_M1S1
;
134 case (1 << CX20442_TELOUT
):
135 case (1 << CX20442_TELIN
):
136 case (1 << CX20442_TELOUT
) | (1 << CX20442_TELIN
):
138 case (1 << CX20442_TELOUT
) | (1 << CX20442_MIC
):
139 return V253_VLS_NONE
;
143 static int cx20442_pm_to_v253_vsp(u8 value
)
145 switch (value
& ~(1 << CX20442_AGC
)) {
146 case (1 << CX20442_SPKOUT
):
147 case (1 << CX20442_MIC
):
148 case (1 << CX20442_SPKOUT
) | (1 << CX20442_MIC
):
149 return (bool)(value
& (1 << CX20442_AGC
));
151 return (value
& (1 << CX20442_AGC
)) ? -EINVAL
: 0;
154 static int cx20442_write(struct snd_soc_codec
*codec
, unsigned int reg
,
157 struct cx20442_priv
*cx20442
= snd_soc_codec_get_drvdata(codec
);
158 u8
*reg_cache
= codec
->reg_cache
;
159 int vls
, vsp
, old
, len
;
162 if (reg
>= codec
->driver
->reg_cache_size
)
165 /* hw_write and control_data pointers required for talking to the modem
166 * are expected to be set by the line discipline initialization code */
167 if (!codec
->hw_write
|| !cx20442
->control_data
)
170 old
= reg_cache
[reg
];
171 reg_cache
[reg
] = value
;
173 vls
= cx20442_pm_to_v253_vls(value
);
177 vsp
= cx20442_pm_to_v253_vsp(value
);
181 if ((vls
== V253_VLS_T
) ||
182 (vls
== cx20442_pm_to_v253_vls(old
))) {
183 if (vsp
== cx20442_pm_to_v253_vsp(old
))
185 len
= snprintf(buf
, ARRAY_SIZE(buf
), "at+vsp=%d\r", vsp
);
186 } else if (vsp
== cx20442_pm_to_v253_vsp(old
))
187 len
= snprintf(buf
, ARRAY_SIZE(buf
), "at+vls=%d\r", vls
);
189 len
= snprintf(buf
, ARRAY_SIZE(buf
),
190 "at+vls=%d;+vsp=%d\r", vls
, vsp
);
192 if (unlikely(len
> (ARRAY_SIZE(buf
) - 1)))
195 dev_dbg(codec
->dev
, "%s: %s\n", __func__
, buf
);
196 if (codec
->hw_write(cx20442
->control_data
, 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 if (tty
->ops
->write(tty
, v253_init
, len
) != len
) {
232 /* Actual setup will be performed after the modem responds. */
235 tty
->disc_data
= NULL
;
239 /* Line discipline .close() */
240 static void v253_close(struct tty_struct
*tty
)
242 struct snd_soc_codec
*codec
= tty
->disc_data
;
243 struct cx20442_priv
*cx20442
;
245 tty
->disc_data
= NULL
;
250 cx20442
= snd_soc_codec_get_drvdata(codec
);
252 /* Prevent the codec driver from further accessing the modem */
253 codec
->hw_write
= NULL
;
254 cx20442
->control_data
= NULL
;
255 codec
->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_codec
*codec
= tty
->disc_data
;
270 struct cx20442_priv
*cx20442
;
275 cx20442
= snd_soc_codec_get_drvdata(codec
);
277 if (!cx20442
->control_data
) {
278 /* First modem response, complete setup procedure */
280 /* Set up codec driver access to modem controls */
281 cx20442
->control_data
= tty
;
282 codec
->hw_write
= (hw_write_t
)tty
->ops
->write
;
283 codec
->card
->pop_time
= 1;
287 /* Line discipline .write_wakeup() */
288 static void v253_wakeup(struct tty_struct
*tty
)
292 struct tty_ldisc_ops v253_ops
= {
293 .magic
= TTY_LDISC_MAGIC
,
295 .owner
= THIS_MODULE
,
298 .hangup
= v253_hangup
,
299 .receive_buf
= v253_receive
,
300 .write_wakeup
= v253_wakeup
,
302 EXPORT_SYMBOL_GPL(v253_ops
);
309 static struct snd_soc_dai_driver cx20442_dai
= {
310 .name
= "cx20442-voice",
312 .stream_name
= "Playback",
315 .rates
= SNDRV_PCM_RATE_8000
,
316 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
319 .stream_name
= "Capture",
322 .rates
= SNDRV_PCM_RATE_8000
,
323 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
327 static int cx20442_codec_probe(struct snd_soc_codec
*codec
)
329 struct cx20442_priv
*cx20442
;
331 cx20442
= kzalloc(sizeof(struct cx20442_priv
), GFP_KERNEL
);
334 snd_soc_codec_set_drvdata(codec
, cx20442
);
336 cx20442
->control_data
= NULL
;
337 codec
->hw_write
= NULL
;
338 codec
->card
->pop_time
= 0;
343 /* power down chip */
344 static int cx20442_codec_remove(struct snd_soc_codec
*codec
)
346 struct cx20442_priv
*cx20442
= snd_soc_codec_get_drvdata(codec
);
348 if (cx20442
->control_data
) {
349 struct tty_struct
*tty
= cx20442
->control_data
;
357 static const u8 cx20442_reg
;
359 static struct snd_soc_codec_driver cx20442_codec_dev
= {
360 .probe
= cx20442_codec_probe
,
361 .remove
= cx20442_codec_remove
,
362 .reg_cache_default
= &cx20442_reg
,
364 .reg_word_size
= sizeof(u8
),
365 .read
= cx20442_read_reg_cache
,
366 .write
= cx20442_write
,
367 .dapm_widgets
= cx20442_dapm_widgets
,
368 .num_dapm_widgets
= ARRAY_SIZE(cx20442_dapm_widgets
),
369 .dapm_routes
= cx20442_audio_map
,
370 .num_dapm_routes
= ARRAY_SIZE(cx20442_audio_map
),
373 static int cx20442_platform_probe(struct platform_device
*pdev
)
375 return snd_soc_register_codec(&pdev
->dev
,
376 &cx20442_codec_dev
, &cx20442_dai
, 1);
379 static int __exit
cx20442_platform_remove(struct platform_device
*pdev
)
381 snd_soc_unregister_codec(&pdev
->dev
);
385 static struct platform_driver cx20442_platform_driver
= {
387 .name
= "cx20442-codec",
388 .owner
= THIS_MODULE
,
390 .probe
= cx20442_platform_probe
,
391 .remove
= __exit_p(cx20442_platform_remove
),
394 static int __init
cx20442_init(void)
396 return platform_driver_register(&cx20442_platform_driver
);
398 module_init(cx20442_init
);
400 static void __exit
cx20442_exit(void)
402 platform_driver_unregister(&cx20442_platform_driver
);
404 module_exit(cx20442_exit
);
406 MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
407 MODULE_AUTHOR("Janusz Krzysztofik");
408 MODULE_LICENSE("GPL");
409 MODULE_ALIAS("platform:cx20442-codec");