Merge git://git.samba.org/sfrench/cifs-2.6
[deliverable/linux.git] / sound / soc / codecs / cx20442.c
1 /*
2 * cx20442.c -- CX20442 ALSA Soc Audio driver
3 *
4 * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
5 *
6 * Initially based on sound/soc/codecs/wm8400.c
7 * Copyright 2008, 2009 Wolfson Microelectronics PLC.
8 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 *
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.
14 */
15
16 #include <linux/tty.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19
20 #include <sound/core.h>
21 #include <sound/initval.h>
22 #include <sound/soc.h>
23
24 #include "cx20442.h"
25
26
27 struct cx20442_priv {
28 enum snd_soc_control_type control_type;
29 void *control_data;
30 };
31
32 #define CX20442_PM 0x0
33
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
39
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"),
44
45 SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
46
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),
50
51 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
52 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
53
54 SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
55
56 SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
57 SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),
58
59 SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),
60
61 SND_SOC_DAPM_INPUT("TELIN"),
62 SND_SOC_DAPM_INPUT("MIC"),
63 SND_SOC_DAPM_INPUT("AGCIN"),
64 };
65
66 static const struct snd_soc_dapm_route cx20442_audio_map[] = {
67 {"TELOUT", NULL, "TELOUT Amp"},
68
69 {"SPKOUT", NULL, "SPKOUT Mixer"},
70 {"SPKOUT Mixer", NULL, "SPKOUT Amp"},
71
72 {"TELOUT Amp", NULL, "DAC"},
73 {"SPKOUT Amp", NULL, "DAC"},
74
75 {"SPKOUT Mixer", NULL, "SPKOUT AGC"},
76 {"SPKOUT AGC", NULL, "AGCIN"},
77
78 {"AGCOUT", NULL, "MIC AGC"},
79 {"MIC AGC", NULL, "MIC"},
80
81 {"MIC Bias", NULL, "MIC"},
82 {"Input Mixer", NULL, "MIC Bias"},
83
84 {"TELIN Bias", NULL, "TELIN"},
85 {"Input Mixer", NULL, "TELIN Bias"},
86
87 {"ADC", NULL, "Input Mixer"},
88 };
89
90 static unsigned int cx20442_read_reg_cache(struct snd_soc_codec *codec,
91 unsigned int reg)
92 {
93 u8 *reg_cache = codec->reg_cache;
94
95 if (reg >= codec->driver->reg_cache_size)
96 return -EINVAL;
97
98 return reg_cache[reg];
99 }
100
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,
123 };
124
125 static int cx20442_pm_to_v253_vls(u8 value)
126 {
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;
140 }
141 return -EINVAL;
142 }
143 static int cx20442_pm_to_v253_vsp(u8 value)
144 {
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));
150 }
151 return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
152 }
153
154 static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
155 unsigned int value)
156 {
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];
161
162 if (reg >= codec->driver->reg_cache_size)
163 return -EINVAL;
164
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;
169
170 old = reg_cache[reg];
171 reg_cache[reg] = value;
172
173 vls = cx20442_pm_to_v253_vls(value);
174 if (vls < 0)
175 return vls;
176
177 vsp = cx20442_pm_to_v253_vsp(value);
178 if (vsp < 0)
179 return vsp;
180
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);
191
192 if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
193 return -ENOMEM;
194
195 dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
196 if (codec->hw_write(cx20442->control_data, buf, len) != len)
197 return -EIO;
198
199 return 0;
200 }
201
202
203 /*
204 * Line discpline related code
205 *
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.
210 */
211
212 /* Modem init: echo off, digital speaker off, quiet off, voice mode */
213 static const char *v253_init = "ate0m0q0+fclass=8\r";
214
215 /* Line discipline .open() */
216 static int v253_open(struct tty_struct *tty)
217 {
218 int ret, len = strlen(v253_init);
219
220 /* Doesn't make sense without write callback */
221 if (!tty->ops->write)
222 return -EINVAL;
223
224 /* Won't work if no codec pointer has been passed by a card driver */
225 if (!tty->disc_data)
226 return -ENODEV;
227
228 if (tty->ops->write(tty, v253_init, len) != len) {
229 ret = -EIO;
230 goto err;
231 }
232 /* Actual setup will be performed after the modem responds. */
233 return 0;
234 err:
235 tty->disc_data = NULL;
236 return ret;
237 }
238
239 /* Line discipline .close() */
240 static void v253_close(struct tty_struct *tty)
241 {
242 struct snd_soc_codec *codec = tty->disc_data;
243 struct cx20442_priv *cx20442;
244
245 tty->disc_data = NULL;
246
247 if (!codec)
248 return;
249
250 cx20442 = snd_soc_codec_get_drvdata(codec);
251
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;
256 }
257
258 /* Line discipline .hangup() */
259 static int v253_hangup(struct tty_struct *tty)
260 {
261 v253_close(tty);
262 return 0;
263 }
264
265 /* Line discipline .receive_buf() */
266 static void v253_receive(struct tty_struct *tty,
267 const unsigned char *cp, char *fp, int count)
268 {
269 struct snd_soc_codec *codec = tty->disc_data;
270 struct cx20442_priv *cx20442;
271
272 if (!codec)
273 return;
274
275 cx20442 = snd_soc_codec_get_drvdata(codec);
276
277 if (!cx20442->control_data) {
278 /* First modem response, complete setup procedure */
279
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;
284 }
285 }
286
287 /* Line discipline .write_wakeup() */
288 static void v253_wakeup(struct tty_struct *tty)
289 {
290 }
291
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,
301 };
302 EXPORT_SYMBOL_GPL(v253_ops);
303
304
305 /*
306 * Codec DAI
307 */
308
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,
317 },
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,
324 },
325 };
326
327 static int cx20442_codec_probe(struct snd_soc_codec *codec)
328 {
329 struct cx20442_priv *cx20442;
330
331 cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
332 if (cx20442 == NULL)
333 return -ENOMEM;
334 snd_soc_codec_set_drvdata(codec, cx20442);
335
336 cx20442->control_data = NULL;
337 codec->hw_write = NULL;
338 codec->card->pop_time = 0;
339
340 return 0;
341 }
342
343 /* power down chip */
344 static int cx20442_codec_remove(struct snd_soc_codec *codec)
345 {
346 struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
347
348 if (cx20442->control_data) {
349 struct tty_struct *tty = cx20442->control_data;
350 tty_hangup(tty);
351 }
352
353 kfree(cx20442);
354 return 0;
355 }
356
357 static const u8 cx20442_reg;
358
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),
371 };
372
373 static int cx20442_platform_probe(struct platform_device *pdev)
374 {
375 return snd_soc_register_codec(&pdev->dev,
376 &cx20442_codec_dev, &cx20442_dai, 1);
377 }
378
379 static int __exit cx20442_platform_remove(struct platform_device *pdev)
380 {
381 snd_soc_unregister_codec(&pdev->dev);
382 return 0;
383 }
384
385 static struct platform_driver cx20442_platform_driver = {
386 .driver = {
387 .name = "cx20442-codec",
388 .owner = THIS_MODULE,
389 },
390 .probe = cx20442_platform_probe,
391 .remove = __exit_p(cx20442_platform_remove),
392 };
393
394 static int __init cx20442_init(void)
395 {
396 return platform_driver_register(&cx20442_platform_driver);
397 }
398 module_init(cx20442_init);
399
400 static void __exit cx20442_exit(void)
401 {
402 platform_driver_unregister(&cx20442_platform_driver);
403 }
404 module_exit(cx20442_exit);
405
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");
This page took 0.040729 seconds and 6 git commands to generate.