I2C: Add helper macro for i2c_driver boilerplate
[zen-stable.git] / sound / soc / codecs / cx20442.c
blobbc7067db8ae4ec54697c4163e23f97af3670bd19
1 /*
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>
24 #include "cx20442.h"
27 struct cx20442_priv {
28 enum snd_soc_control_type control_type;
29 void *control_data;
32 #define CX20442_PM 0x0
34 #define CX20442_TELIN 0
35 #define CX20442_TELOUT 1
36 #define CX20442_MIC 2
37 #define CX20442_SPKOUT 3
38 #define CX20442_AGC 4
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,
91 unsigned int reg)
93 u8 *reg_cache = codec->reg_cache;
95 if (reg >= codec->driver->reg_cache_size)
96 return -EINVAL;
98 return reg_cache[reg];
101 enum v253_vls {
102 V253_VLS_NONE = 0,
103 V253_VLS_T,
104 V253_VLS_L,
105 V253_VLS_LT,
106 V253_VLS_S,
107 V253_VLS_ST,
108 V253_VLS_M,
109 V253_VLS_MST,
110 V253_VLS_S1,
111 V253_VLS_S1T,
112 V253_VLS_MS1T,
113 V253_VLS_M1,
114 V253_VLS_M1ST,
115 V253_VLS_M1S1T,
116 V253_VLS_H,
117 V253_VLS_HT,
118 V253_VLS_MS,
119 V253_VLS_MS1,
120 V253_VLS_M1S,
121 V253_VLS_M1S1,
122 V253_VLS_TEST,
125 static int cx20442_pm_to_v253_vls(u8 value)
127 switch (value & ~(1 << CX20442_AGC)) {
128 case 0:
129 return V253_VLS_T;
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):
137 return V253_VLS_L;
138 case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
139 return V253_VLS_NONE;
141 return -EINVAL;
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,
155 unsigned int value)
157 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
158 u8 *reg_cache = codec->reg_cache;
159 int vls, vsp, old, len;
160 char buf[18];
162 if (reg >= codec->driver->reg_cache_size)
163 return -EINVAL;
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)
168 return -EIO;
170 old = reg_cache[reg];
171 reg_cache[reg] = value;
173 vls = cx20442_pm_to_v253_vls(value);
174 if (vls < 0)
175 return vls;
177 vsp = cx20442_pm_to_v253_vsp(value);
178 if (vsp < 0)
179 return vsp;
181 if ((vls == V253_VLS_T) ||
182 (vls == cx20442_pm_to_v253_vls(old))) {
183 if (vsp == cx20442_pm_to_v253_vsp(old))
184 return 0;
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);
188 else
189 len = snprintf(buf, ARRAY_SIZE(buf),
190 "at+vls=%d;+vsp=%d\r", vls, vsp);
192 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
193 return -ENOMEM;
195 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
196 if (codec->hw_write(cx20442->control_data, buf, len) != len)
197 return -EIO;
199 return 0;
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)
222 return -EINVAL;
224 /* Won't work if no codec pointer has been passed by a card driver */
225 if (!tty->disc_data)
226 return -ENODEV;
228 if (tty->ops->write(tty, v253_init, len) != len) {
229 ret = -EIO;
230 goto err;
232 /* Actual setup will be performed after the modem responds. */
233 return 0;
234 err:
235 tty->disc_data = NULL;
236 return ret;
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;
247 if (!codec)
248 return;
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)
261 v253_close(tty);
262 return 0;
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;
272 if (!codec)
273 return;
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,
294 .name = "cx20442",
295 .owner = THIS_MODULE,
296 .open = v253_open,
297 .close = v253_close,
298 .hangup = v253_hangup,
299 .receive_buf = v253_receive,
300 .write_wakeup = v253_wakeup,
302 EXPORT_SYMBOL_GPL(v253_ops);
306 * Codec DAI
309 static struct snd_soc_dai_driver cx20442_dai = {
310 .name = "cx20442-voice",
311 .playback = {
312 .stream_name = "Playback",
313 .channels_min = 1,
314 .channels_max = 1,
315 .rates = SNDRV_PCM_RATE_8000,
316 .formats = SNDRV_PCM_FMTBIT_S16_LE,
318 .capture = {
319 .stream_name = "Capture",
320 .channels_min = 1,
321 .channels_max = 1,
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);
332 if (cx20442 == NULL)
333 return -ENOMEM;
334 snd_soc_codec_set_drvdata(codec, cx20442);
336 cx20442->control_data = NULL;
337 codec->hw_write = NULL;
338 codec->card->pop_time = 0;
340 return 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;
350 tty_hangup(tty);
353 kfree(cx20442);
354 return 0;
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,
363 .reg_cache_size = 1,
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);
382 return 0;
385 static struct platform_driver cx20442_platform_driver = {
386 .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");