ARM: socfpga: Upgrade clk driver for socfpga to make use of dts clock entries
[linux-2.6.git] / sound / soc / omap / ams-delta.c
blob2600447fa74f823542f2668e216071d2240dfdf3
1 /*
2 * ams-delta.c -- SoC audio for Amstrad E3 (Delta) videophone
4 * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6 * Initially based on sound/soc/omap/osk5912.x
7 * Copyright (C) 2008 Mistral Solutions
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
25 #include <linux/gpio.h>
26 #include <linux/spinlock.h>
27 #include <linux/tty.h>
28 #include <linux/module.h>
30 #include <sound/soc.h>
31 #include <sound/jack.h>
33 #include <asm/mach-types.h>
35 #include <mach/board-ams-delta.h>
36 #include <linux/platform_data/asoc-ti-mcbsp.h>
38 #include "omap-mcbsp.h"
39 #include "omap-pcm.h"
40 #include "../codecs/cx20442.h"
43 /* Board specific DAPM widgets */
44 static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
45 /* Handset */
46 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
47 SND_SOC_DAPM_HP("Earpiece", NULL),
48 /* Handsfree/Speakerphone */
49 SND_SOC_DAPM_MIC("Microphone", NULL),
50 SND_SOC_DAPM_SPK("Speaker", NULL),
53 /* How they are connected to codec pins */
54 static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
55 {"TELIN", NULL, "Mouthpiece"},
56 {"Earpiece", NULL, "TELOUT"},
58 {"MIC", NULL, "Microphone"},
59 {"Speaker", NULL, "SPKOUT"},
63 * Controls, functional after the modem line discipline is activated.
66 /* Virtual switch: audio input/output constellations */
67 static const char *ams_delta_audio_mode[] =
68 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
70 /* Selection <-> pin translation */
71 #define AMS_DELTA_MOUTHPIECE 0
72 #define AMS_DELTA_EARPIECE 1
73 #define AMS_DELTA_MICROPHONE 2
74 #define AMS_DELTA_SPEAKER 3
75 #define AMS_DELTA_AGC 4
77 #define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
78 (1 << AMS_DELTA_MICROPHONE))
79 #define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
80 (1 << AMS_DELTA_EARPIECE))
81 #define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
82 (1 << AMS_DELTA_SPEAKER))
83 #define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
85 static const unsigned short ams_delta_audio_mode_pins[] = {
86 AMS_DELTA_MIXED,
87 AMS_DELTA_HANDSET,
88 AMS_DELTA_HANDSFREE,
89 AMS_DELTA_SPEAKERPHONE,
92 static unsigned short ams_delta_audio_agc;
94 static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_value *ucontrol)
97 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
98 struct snd_soc_dapm_context *dapm = &codec->dapm;
99 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
100 unsigned short pins;
101 int pin, changed = 0;
103 /* Refuse any mode changes if we are not able to control the codec. */
104 if (!codec->hw_write)
105 return -EUNATCH;
107 if (ucontrol->value.enumerated.item[0] >= control->max)
108 return -EINVAL;
110 mutex_lock(&codec->mutex);
112 /* Translate selection to bitmap */
113 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
115 /* Setup pins after corresponding bits if changed */
116 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
117 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
118 changed = 1;
119 if (pin)
120 snd_soc_dapm_enable_pin(dapm, "Mouthpiece");
121 else
122 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
124 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
125 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
126 changed = 1;
127 if (pin)
128 snd_soc_dapm_enable_pin(dapm, "Earpiece");
129 else
130 snd_soc_dapm_disable_pin(dapm, "Earpiece");
132 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
133 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
134 changed = 1;
135 if (pin)
136 snd_soc_dapm_enable_pin(dapm, "Microphone");
137 else
138 snd_soc_dapm_disable_pin(dapm, "Microphone");
140 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
141 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
142 changed = 1;
143 if (pin)
144 snd_soc_dapm_enable_pin(dapm, "Speaker");
145 else
146 snd_soc_dapm_disable_pin(dapm, "Speaker");
148 pin = !!(pins & (1 << AMS_DELTA_AGC));
149 if (pin != ams_delta_audio_agc) {
150 ams_delta_audio_agc = pin;
151 changed = 1;
152 if (pin)
153 snd_soc_dapm_enable_pin(dapm, "AGCIN");
154 else
155 snd_soc_dapm_disable_pin(dapm, "AGCIN");
157 if (changed)
158 snd_soc_dapm_sync(dapm);
160 mutex_unlock(&codec->mutex);
162 return changed;
165 static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
166 struct snd_ctl_elem_value *ucontrol)
168 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
169 struct snd_soc_dapm_context *dapm = &codec->dapm;
170 unsigned short pins, mode;
172 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
173 AMS_DELTA_MOUTHPIECE) |
174 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
175 AMS_DELTA_EARPIECE));
176 if (pins)
177 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
178 AMS_DELTA_MICROPHONE);
179 else
180 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
181 AMS_DELTA_MICROPHONE) |
182 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
183 AMS_DELTA_SPEAKER) |
184 (ams_delta_audio_agc << AMS_DELTA_AGC));
186 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
187 if (pins == ams_delta_audio_mode_pins[mode])
188 break;
190 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
191 return -EINVAL;
193 ucontrol->value.enumerated.item[0] = mode;
195 return 0;
198 static const struct soc_enum ams_delta_audio_enum[] = {
199 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ams_delta_audio_mode),
200 ams_delta_audio_mode),
203 static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
204 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum[0],
205 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
208 /* Hook switch */
209 static struct snd_soc_jack ams_delta_hook_switch;
210 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
212 .gpio = 4,
213 .name = "hook_switch",
214 .report = SND_JACK_HEADSET,
215 .invert = 1,
216 .debounce_time = 150,
220 /* After we are able to control the codec over the modem,
221 * the hook switch can be used for dynamic DAPM reconfiguration. */
222 static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
223 /* Handset */
225 .pin = "Mouthpiece",
226 .mask = SND_JACK_MICROPHONE,
229 .pin = "Earpiece",
230 .mask = SND_JACK_HEADPHONE,
232 /* Handsfree */
234 .pin = "Microphone",
235 .mask = SND_JACK_MICROPHONE,
236 .invert = 1,
239 .pin = "Speaker",
240 .mask = SND_JACK_HEADPHONE,
241 .invert = 1,
247 * Modem line discipline, required for making above controls functional.
248 * Activated from userspace with ldattach, possibly invoked from udev rule.
251 /* To actually apply any modem controlled configuration changes to the codec,
252 * we must connect codec DAI pins to the modem for a moment. Be careful not
253 * to interfere with our digital mute function that shares the same hardware. */
254 static struct timer_list cx81801_timer;
255 static bool cx81801_cmd_pending;
256 static bool ams_delta_muted;
257 static DEFINE_SPINLOCK(ams_delta_lock);
259 static void cx81801_timeout(unsigned long data)
261 int muted;
263 spin_lock(&ams_delta_lock);
264 cx81801_cmd_pending = 0;
265 muted = ams_delta_muted;
266 spin_unlock(&ams_delta_lock);
268 /* Reconnect the codec DAI back from the modem to the CPU DAI
269 * only if digital mute still off */
270 if (!muted)
271 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC, 0);
275 * Used for passing a codec structure pointer
276 * from the board initialization code to the tty line discipline.
278 static struct snd_soc_codec *cx20442_codec;
280 /* Line discipline .open() */
281 static int cx81801_open(struct tty_struct *tty)
283 int ret;
285 if (!cx20442_codec)
286 return -ENODEV;
289 * Pass the codec structure pointer for use by other ldisc callbacks,
290 * both the card and the codec specific parts.
292 tty->disc_data = cx20442_codec;
294 ret = v253_ops.open(tty);
296 if (ret < 0)
297 tty->disc_data = NULL;
299 return ret;
302 /* Line discipline .close() */
303 static void cx81801_close(struct tty_struct *tty)
305 struct snd_soc_codec *codec = tty->disc_data;
306 struct snd_soc_dapm_context *dapm = &codec->dapm;
308 del_timer_sync(&cx81801_timer);
310 /* Prevent the hook switch from further changing the DAPM pins */
311 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
313 if (!codec)
314 return;
316 v253_ops.close(tty);
318 /* Revert back to default audio input/output constellation */
319 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
320 snd_soc_dapm_enable_pin(dapm, "Earpiece");
321 snd_soc_dapm_enable_pin(dapm, "Microphone");
322 snd_soc_dapm_disable_pin(dapm, "Speaker");
323 snd_soc_dapm_disable_pin(dapm, "AGCIN");
324 snd_soc_dapm_sync(dapm);
327 /* Line discipline .hangup() */
328 static int cx81801_hangup(struct tty_struct *tty)
330 cx81801_close(tty);
331 return 0;
334 /* Line discipline .receive_buf() */
335 static void cx81801_receive(struct tty_struct *tty,
336 const unsigned char *cp, char *fp, int count)
338 struct snd_soc_codec *codec = tty->disc_data;
339 const unsigned char *c;
340 int apply, ret;
342 if (!codec)
343 return;
345 if (!codec->hw_write) {
346 /* First modem response, complete setup procedure */
348 /* Initialize timer used for config pulse generation */
349 setup_timer(&cx81801_timer, cx81801_timeout, 0);
351 v253_ops.receive_buf(tty, cp, fp, count);
353 /* Link hook switch to DAPM pins */
354 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
355 ARRAY_SIZE(ams_delta_hook_switch_pins),
356 ams_delta_hook_switch_pins);
357 if (ret)
358 dev_warn(codec->dev,
359 "Failed to link hook switch to DAPM pins, "
360 "will continue with hook switch unlinked.\n");
362 return;
365 v253_ops.receive_buf(tty, cp, fp, count);
367 for (c = &cp[count - 1]; c >= cp; c--) {
368 if (*c != '\r')
369 continue;
370 /* Complete modem response received, apply config to codec */
372 spin_lock_bh(&ams_delta_lock);
373 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
374 apply = !ams_delta_muted && !cx81801_cmd_pending;
375 cx81801_cmd_pending = 1;
376 spin_unlock_bh(&ams_delta_lock);
378 /* Apply config pulse by connecting the codec to the modem
379 * if not already done */
380 if (apply)
381 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
382 AMS_DELTA_LATCH2_MODEM_CODEC);
383 break;
387 /* Line discipline .write_wakeup() */
388 static void cx81801_wakeup(struct tty_struct *tty)
390 v253_ops.write_wakeup(tty);
393 static struct tty_ldisc_ops cx81801_ops = {
394 .magic = TTY_LDISC_MAGIC,
395 .name = "cx81801",
396 .owner = THIS_MODULE,
397 .open = cx81801_open,
398 .close = cx81801_close,
399 .hangup = cx81801_hangup,
400 .receive_buf = cx81801_receive,
401 .write_wakeup = cx81801_wakeup,
406 * Even if not very useful, the sound card can still work without any of the
407 * above functonality activated. You can still control its audio input/output
408 * constellation and speakerphone gain from userspace by issuing AT commands
409 * over the modem port.
412 static int ams_delta_hw_params(struct snd_pcm_substream *substream,
413 struct snd_pcm_hw_params *params)
415 struct snd_soc_pcm_runtime *rtd = substream->private_data;
417 /* Set cpu DAI configuration */
418 return snd_soc_dai_set_fmt(rtd->cpu_dai,
419 SND_SOC_DAIFMT_DSP_A |
420 SND_SOC_DAIFMT_NB_NF |
421 SND_SOC_DAIFMT_CBM_CFM);
424 static struct snd_soc_ops ams_delta_ops = {
425 .hw_params = ams_delta_hw_params,
429 /* Digital mute implemented using modem/CPU multiplexer.
430 * Shares hardware with codec config pulse generation */
431 static bool ams_delta_muted = 1;
433 static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
435 int apply;
437 if (ams_delta_muted == mute)
438 return 0;
440 spin_lock_bh(&ams_delta_lock);
441 ams_delta_muted = mute;
442 apply = !cx81801_cmd_pending;
443 spin_unlock_bh(&ams_delta_lock);
445 if (apply)
446 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
447 mute ? AMS_DELTA_LATCH2_MODEM_CODEC : 0);
448 return 0;
451 /* Our codec DAI probably doesn't have its own .ops structure */
452 static const struct snd_soc_dai_ops ams_delta_dai_ops = {
453 .digital_mute = ams_delta_digital_mute,
456 /* Will be used if the codec ever has its own digital_mute function */
457 static int ams_delta_startup(struct snd_pcm_substream *substream)
459 return ams_delta_digital_mute(NULL, 0);
462 static void ams_delta_shutdown(struct snd_pcm_substream *substream)
464 ams_delta_digital_mute(NULL, 1);
469 * Card initialization
472 static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
474 struct snd_soc_codec *codec = rtd->codec;
475 struct snd_soc_dapm_context *dapm = &codec->dapm;
476 struct snd_soc_dai *codec_dai = rtd->codec_dai;
477 struct snd_soc_card *card = rtd->card;
478 int ret;
479 /* Codec is ready, now add/activate board specific controls */
481 /* Store a pointer to the codec structure for tty ldisc use */
482 cx20442_codec = codec;
484 /* Set up digital mute if not provided by the codec */
485 if (!codec_dai->driver->ops) {
486 codec_dai->driver->ops = &ams_delta_dai_ops;
487 } else {
488 ams_delta_ops.startup = ams_delta_startup;
489 ams_delta_ops.shutdown = ams_delta_shutdown;
492 /* Add hook switch - can be used to control the codec from userspace
493 * even if line discipline fails */
494 ret = snd_soc_jack_new(rtd->codec, "hook_switch",
495 SND_JACK_HEADSET, &ams_delta_hook_switch);
496 if (ret)
497 dev_warn(card->dev,
498 "Failed to allocate resources for hook switch, "
499 "will continue without one.\n");
500 else {
501 ret = snd_soc_jack_add_gpios(&ams_delta_hook_switch,
502 ARRAY_SIZE(ams_delta_hook_switch_gpios),
503 ams_delta_hook_switch_gpios);
504 if (ret)
505 dev_warn(card->dev,
506 "Failed to set up hook switch GPIO line, "
507 "will continue with hook switch inactive.\n");
510 /* Register optional line discipline for over the modem control */
511 ret = tty_register_ldisc(N_V253, &cx81801_ops);
512 if (ret) {
513 dev_warn(card->dev,
514 "Failed to register line discipline, "
515 "will continue without any controls.\n");
516 return 0;
519 /* Add board specific DAPM widgets and routes */
520 ret = snd_soc_dapm_new_controls(dapm, ams_delta_dapm_widgets,
521 ARRAY_SIZE(ams_delta_dapm_widgets));
522 if (ret) {
523 dev_warn(card->dev,
524 "Failed to register DAPM controls, "
525 "will continue without any.\n");
526 return 0;
529 ret = snd_soc_dapm_add_routes(dapm, ams_delta_audio_map,
530 ARRAY_SIZE(ams_delta_audio_map));
531 if (ret) {
532 dev_warn(card->dev,
533 "Failed to set up DAPM routes, "
534 "will continue with codec default map.\n");
535 return 0;
538 /* Set up initial pin constellation */
539 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
540 snd_soc_dapm_enable_pin(dapm, "Earpiece");
541 snd_soc_dapm_enable_pin(dapm, "Microphone");
542 snd_soc_dapm_disable_pin(dapm, "Speaker");
543 snd_soc_dapm_disable_pin(dapm, "AGCIN");
544 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
546 /* Add virtual switch */
547 ret = snd_soc_add_codec_controls(codec, ams_delta_audio_controls,
548 ARRAY_SIZE(ams_delta_audio_controls));
549 if (ret)
550 dev_warn(card->dev,
551 "Failed to register audio mode control, "
552 "will continue without it.\n");
554 return 0;
557 /* DAI glue - connects codec <--> CPU */
558 static struct snd_soc_dai_link ams_delta_dai_link = {
559 .name = "CX20442",
560 .stream_name = "CX20442",
561 .cpu_dai_name = "omap-mcbsp.1",
562 .codec_dai_name = "cx20442-voice",
563 .init = ams_delta_cx20442_init,
564 .platform_name = "omap-pcm-audio",
565 .codec_name = "cx20442-codec",
566 .ops = &ams_delta_ops,
569 /* Audio card driver */
570 static struct snd_soc_card ams_delta_audio_card = {
571 .name = "AMS_DELTA",
572 .owner = THIS_MODULE,
573 .dai_link = &ams_delta_dai_link,
574 .num_links = 1,
577 /* Module init/exit */
578 static int ams_delta_probe(struct platform_device *pdev)
580 struct snd_soc_card *card = &ams_delta_audio_card;
581 int ret;
583 card->dev = &pdev->dev;
585 ret = snd_soc_register_card(card);
586 if (ret) {
587 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
588 card->dev = NULL;
589 return ret;
591 return 0;
594 static int ams_delta_remove(struct platform_device *pdev)
596 struct snd_soc_card *card = platform_get_drvdata(pdev);
598 if (tty_unregister_ldisc(N_V253) != 0)
599 dev_warn(&pdev->dev,
600 "failed to unregister V253 line discipline\n");
602 snd_soc_jack_free_gpios(&ams_delta_hook_switch,
603 ARRAY_SIZE(ams_delta_hook_switch_gpios),
604 ams_delta_hook_switch_gpios);
606 snd_soc_unregister_card(card);
607 card->dev = NULL;
608 return 0;
611 #define DRV_NAME "ams-delta-audio"
613 static struct platform_driver ams_delta_driver = {
614 .driver = {
615 .name = DRV_NAME,
616 .owner = THIS_MODULE,
618 .probe = ams_delta_probe,
619 .remove = ams_delta_remove,
622 module_platform_driver(ams_delta_driver);
624 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
625 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
626 MODULE_LICENSE("GPL");
627 MODULE_ALIAS("platform:" DRV_NAME);