2 * SoC audio for HTC Magician
4 * Copyright (c) 2006 Philipp Zabel <philipp.zabel@gmail.com>
7 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
8 * Richard Purdie <richard@openedhand.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.
17 #include <linux/module.h>
18 #include <linux/timer.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/delay.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/uda1380.h>
31 #include <mach/magician.h>
32 #include <asm/mach-types.h>
33 #include "../codecs/uda1380.h"
34 #include "pxa2xx-i2s.h"
37 #define MAGICIAN_MIC 0
38 #define MAGICIAN_MIC_EXT 1
40 static int magician_hp_switch
;
41 static int magician_spk_switch
= 1;
42 static int magician_in_sel
= MAGICIAN_MIC
;
44 static void magician_ext_control(struct snd_soc_dapm_context
*dapm
)
47 snd_soc_dapm_mutex_lock(dapm
);
49 if (magician_spk_switch
)
50 snd_soc_dapm_enable_pin_unlocked(dapm
, "Speaker");
52 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
53 if (magician_hp_switch
)
54 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headphone Jack");
56 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headphone Jack");
58 switch (magician_in_sel
) {
60 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headset Mic");
61 snd_soc_dapm_enable_pin_unlocked(dapm
, "Call Mic");
63 case MAGICIAN_MIC_EXT
:
64 snd_soc_dapm_disable_pin_unlocked(dapm
, "Call Mic");
65 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headset Mic");
69 snd_soc_dapm_sync_unlocked(dapm
);
71 snd_soc_dapm_mutex_unlock(dapm
);
74 static int magician_startup(struct snd_pcm_substream
*substream
)
76 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
78 /* check the jack status at stream startup */
79 magician_ext_control(&rtd
->card
->dapm
);
85 * Magician uses SSP port for playback.
87 static int magician_playback_hw_params(struct snd_pcm_substream
*substream
,
88 struct snd_pcm_hw_params
*params
)
90 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
91 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
92 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
93 unsigned int acps
, acds
, width
;
94 unsigned int div4
= PXA_SSP_CLK_SCDB_4
;
97 width
= snd_pcm_format_physical_width(params_format(params
));
100 * rate = SSPSCLK / (2 * width(16 or 32))
101 * SSPSCLK = (ACPS / ACDS) / SSPSCLKDIV(div4 or div1)
103 switch (params_rate(params
)) {
105 /* off by a factor of 2: bug in the PXA27x audio clock? */
109 /* 513156 Hz ~= _2_ * 8000 Hz * 32 (+0.23%) */
110 acds
= PXA_SSP_CLK_AUDIO_DIV_16
;
113 /* 1026312 Hz ~= _2_ * 8000 Hz * 64 (+0.23%) */
114 acds
= PXA_SSP_CLK_AUDIO_DIV_8
;
121 /* 351375 Hz ~= 11025 Hz * 32 (-0.41%) */
122 acds
= PXA_SSP_CLK_AUDIO_DIV_4
;
125 /* 702750 Hz ~= 11025 Hz * 64 (-0.41%) */
126 acds
= PXA_SSP_CLK_AUDIO_DIV_2
;
133 /* 702750 Hz ~= 22050 Hz * 32 (-0.41%) */
134 acds
= PXA_SSP_CLK_AUDIO_DIV_2
;
137 /* 1405500 Hz ~= 22050 Hz * 64 (-0.41%) */
138 acds
= PXA_SSP_CLK_AUDIO_DIV_1
;
145 /* 1405500 Hz ~= 44100 Hz * 32 (-0.41%) */
146 acds
= PXA_SSP_CLK_AUDIO_DIV_2
;
149 /* 2811000 Hz ~= 44100 Hz * 64 (-0.41%) */
150 acds
= PXA_SSP_CLK_AUDIO_DIV_1
;
157 /* 1529375 Hz ~= 48000 Hz * 32 (-0.44%) */
158 acds
= PXA_SSP_CLK_AUDIO_DIV_2
;
161 /* 3058750 Hz ~= 48000 Hz * 64 (-0.44%) */
162 acds
= PXA_SSP_CLK_AUDIO_DIV_1
;
170 /* 3058750 Hz ~= 96000 Hz * 32 (-0.44%) */
171 acds
= PXA_SSP_CLK_AUDIO_DIV_1
;
174 /* 6117500 Hz ~= 96000 Hz * 64 (-0.44%) */
175 acds
= PXA_SSP_CLK_AUDIO_DIV_2
;
176 div4
= PXA_SSP_CLK_SCDB_1
;
182 /* set codec DAI configuration */
183 ret
= snd_soc_dai_set_fmt(codec_dai
, SND_SOC_DAIFMT_MSB
|
184 SND_SOC_DAIFMT_NB_NF
| SND_SOC_DAIFMT_CBS_CFS
);
188 /* set cpu DAI configuration */
189 ret
= snd_soc_dai_set_fmt(cpu_dai
, SND_SOC_DAIFMT_DSP_A
|
190 SND_SOC_DAIFMT_NB_IF
| SND_SOC_DAIFMT_CBS_CFS
);
194 ret
= snd_soc_dai_set_tdm_slot(cpu_dai
, 1, 0, 1, width
);
198 /* set audio clock as clock source */
199 ret
= snd_soc_dai_set_sysclk(cpu_dai
, PXA_SSP_CLK_AUDIO
, 0,
204 /* set the SSP audio system clock ACDS divider */
205 ret
= snd_soc_dai_set_clkdiv(cpu_dai
,
206 PXA_SSP_AUDIO_DIV_ACDS
, acds
);
210 /* set the SSP audio system clock SCDB divider4 */
211 ret
= snd_soc_dai_set_clkdiv(cpu_dai
,
212 PXA_SSP_AUDIO_DIV_SCDB
, div4
);
216 /* set SSP audio pll clock */
217 ret
= snd_soc_dai_set_pll(cpu_dai
, 0, 0, 0, acps
);
225 * Magician uses I2S for capture.
227 static int magician_capture_hw_params(struct snd_pcm_substream
*substream
,
228 struct snd_pcm_hw_params
*params
)
230 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
231 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
232 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
235 /* set codec DAI configuration */
236 ret
= snd_soc_dai_set_fmt(codec_dai
,
237 SND_SOC_DAIFMT_MSB
| SND_SOC_DAIFMT_NB_NF
|
238 SND_SOC_DAIFMT_CBS_CFS
);
242 /* set cpu DAI configuration */
243 ret
= snd_soc_dai_set_fmt(cpu_dai
,
244 SND_SOC_DAIFMT_MSB
| SND_SOC_DAIFMT_NB_NF
|
245 SND_SOC_DAIFMT_CBS_CFS
);
249 /* set the I2S system clock as output */
250 ret
= snd_soc_dai_set_sysclk(cpu_dai
, PXA2XX_I2S_SYSCLK
, 0,
258 static struct snd_soc_ops magician_capture_ops
= {
259 .startup
= magician_startup
,
260 .hw_params
= magician_capture_hw_params
,
263 static struct snd_soc_ops magician_playback_ops
= {
264 .startup
= magician_startup
,
265 .hw_params
= magician_playback_hw_params
,
268 static int magician_get_hp(struct snd_kcontrol
*kcontrol
,
269 struct snd_ctl_elem_value
*ucontrol
)
271 ucontrol
->value
.integer
.value
[0] = magician_hp_switch
;
275 static int magician_set_hp(struct snd_kcontrol
*kcontrol
,
276 struct snd_ctl_elem_value
*ucontrol
)
278 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
280 if (magician_hp_switch
== ucontrol
->value
.integer
.value
[0])
283 magician_hp_switch
= ucontrol
->value
.integer
.value
[0];
284 magician_ext_control(&card
->dapm
);
288 static int magician_get_spk(struct snd_kcontrol
*kcontrol
,
289 struct snd_ctl_elem_value
*ucontrol
)
291 ucontrol
->value
.integer
.value
[0] = magician_spk_switch
;
295 static int magician_set_spk(struct snd_kcontrol
*kcontrol
,
296 struct snd_ctl_elem_value
*ucontrol
)
298 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
300 if (magician_spk_switch
== ucontrol
->value
.integer
.value
[0])
303 magician_spk_switch
= ucontrol
->value
.integer
.value
[0];
304 magician_ext_control(&card
->dapm
);
308 static int magician_get_input(struct snd_kcontrol
*kcontrol
,
309 struct snd_ctl_elem_value
*ucontrol
)
311 ucontrol
->value
.integer
.value
[0] = magician_in_sel
;
315 static int magician_set_input(struct snd_kcontrol
*kcontrol
,
316 struct snd_ctl_elem_value
*ucontrol
)
318 if (magician_in_sel
== ucontrol
->value
.integer
.value
[0])
321 magician_in_sel
= ucontrol
->value
.integer
.value
[0];
323 switch (magician_in_sel
) {
325 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1
, 1);
327 case MAGICIAN_MIC_EXT
:
328 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1
, 0);
334 static int magician_spk_power(struct snd_soc_dapm_widget
*w
,
335 struct snd_kcontrol
*k
, int event
)
337 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER
, SND_SOC_DAPM_EVENT_ON(event
));
341 static int magician_hp_power(struct snd_soc_dapm_widget
*w
,
342 struct snd_kcontrol
*k
, int event
)
344 gpio_set_value(EGPIO_MAGICIAN_EP_POWER
, SND_SOC_DAPM_EVENT_ON(event
));
348 static int magician_mic_bias(struct snd_soc_dapm_widget
*w
,
349 struct snd_kcontrol
*k
, int event
)
351 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER
, SND_SOC_DAPM_EVENT_ON(event
));
355 /* magician machine dapm widgets */
356 static const struct snd_soc_dapm_widget uda1380_dapm_widgets
[] = {
357 SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power
),
358 SND_SOC_DAPM_SPK("Speaker", magician_spk_power
),
359 SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias
),
360 SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias
),
363 /* magician machine audio_map */
364 static const struct snd_soc_dapm_route audio_map
[] = {
366 /* Headphone connected to VOUTL, VOUTR */
367 {"Headphone Jack", NULL
, "VOUTL"},
368 {"Headphone Jack", NULL
, "VOUTR"},
370 /* Speaker connected to VOUTL, VOUTR */
371 {"Speaker", NULL
, "VOUTL"},
372 {"Speaker", NULL
, "VOUTR"},
374 /* Mics are connected to VINM */
375 {"VINM", NULL
, "Headset Mic"},
376 {"VINM", NULL
, "Call Mic"},
379 static const char *input_select
[] = {"Call Mic", "Headset Mic"};
380 static const struct soc_enum magician_in_sel_enum
=
381 SOC_ENUM_SINGLE_EXT(2, input_select
);
383 static const struct snd_kcontrol_new uda1380_magician_controls
[] = {
384 SOC_SINGLE_BOOL_EXT("Headphone Switch",
385 (unsigned long)&magician_hp_switch
,
386 magician_get_hp
, magician_set_hp
),
387 SOC_SINGLE_BOOL_EXT("Speaker Switch",
388 (unsigned long)&magician_spk_switch
,
389 magician_get_spk
, magician_set_spk
),
390 SOC_ENUM_EXT("Input Select", magician_in_sel_enum
,
391 magician_get_input
, magician_set_input
),
395 * Logic for a uda1380 as connected on a HTC Magician
397 static int magician_uda1380_init(struct snd_soc_pcm_runtime
*rtd
)
399 struct snd_soc_codec
*codec
= rtd
->codec
;
400 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
403 snd_soc_dapm_nc_pin(dapm
, "VOUTLHP");
404 snd_soc_dapm_nc_pin(dapm
, "VOUTRHP");
406 /* FIXME: is anything connected here? */
407 snd_soc_dapm_nc_pin(dapm
, "VINL");
408 snd_soc_dapm_nc_pin(dapm
, "VINR");
413 /* magician digital audio interface glue - connects codec <--> CPU */
414 static struct snd_soc_dai_link magician_dai
[] = {
417 .stream_name
= "UDA1380 Playback",
418 .cpu_dai_name
= "pxa-ssp-dai.0",
419 .codec_dai_name
= "uda1380-hifi-playback",
420 .platform_name
= "pxa-pcm-audio",
421 .codec_name
= "uda1380-codec.0-0018",
422 .init
= magician_uda1380_init
,
423 .ops
= &magician_playback_ops
,
427 .stream_name
= "UDA1380 Capture",
428 .cpu_dai_name
= "pxa2xx-i2s",
429 .codec_dai_name
= "uda1380-hifi-capture",
430 .platform_name
= "pxa-pcm-audio",
431 .codec_name
= "uda1380-codec.0-0018",
432 .ops
= &magician_capture_ops
,
436 /* magician audio machine driver */
437 static struct snd_soc_card snd_soc_card_magician
= {
439 .owner
= THIS_MODULE
,
440 .dai_link
= magician_dai
,
441 .num_links
= ARRAY_SIZE(magician_dai
),
443 .controls
= uda1380_magician_controls
,
444 .num_controls
= ARRAY_SIZE(uda1380_magician_controls
),
445 .dapm_widgets
= uda1380_dapm_widgets
,
446 .num_dapm_widgets
= ARRAY_SIZE(uda1380_dapm_widgets
),
447 .dapm_routes
= audio_map
,
448 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
451 static struct platform_device
*magician_snd_device
;
454 * FIXME: move into magician board file once merged into the pxa tree
456 static struct uda1380_platform_data uda1380_info
= {
457 .gpio_power
= EGPIO_MAGICIAN_CODEC_POWER
,
458 .gpio_reset
= EGPIO_MAGICIAN_CODEC_RESET
,
459 .dac_clk
= UDA1380_DAC_CLK_WSPLL
,
462 static struct i2c_board_info i2c_board_info
[] = {
464 I2C_BOARD_INFO("uda1380", 0x18),
465 .platform_data
= &uda1380_info
,
469 static int __init
magician_init(void)
472 struct i2c_adapter
*adapter
;
473 struct i2c_client
*client
;
475 if (!machine_is_magician())
478 adapter
= i2c_get_adapter(0);
481 client
= i2c_new_device(adapter
, i2c_board_info
);
482 i2c_put_adapter(adapter
);
486 ret
= gpio_request(EGPIO_MAGICIAN_SPK_POWER
, "SPK_POWER");
488 goto err_request_spk
;
489 ret
= gpio_request(EGPIO_MAGICIAN_EP_POWER
, "EP_POWER");
492 ret
= gpio_request(EGPIO_MAGICIAN_MIC_POWER
, "MIC_POWER");
494 goto err_request_mic
;
495 ret
= gpio_request(EGPIO_MAGICIAN_IN_SEL0
, "IN_SEL0");
497 goto err_request_in_sel0
;
498 ret
= gpio_request(EGPIO_MAGICIAN_IN_SEL1
, "IN_SEL1");
500 goto err_request_in_sel1
;
502 gpio_set_value(EGPIO_MAGICIAN_IN_SEL0
, 0);
504 magician_snd_device
= platform_device_alloc("soc-audio", -1);
505 if (!magician_snd_device
) {
510 platform_set_drvdata(magician_snd_device
, &snd_soc_card_magician
);
511 ret
= platform_device_add(magician_snd_device
);
513 platform_device_put(magician_snd_device
);
520 gpio_free(EGPIO_MAGICIAN_IN_SEL1
);
522 gpio_free(EGPIO_MAGICIAN_IN_SEL0
);
524 gpio_free(EGPIO_MAGICIAN_MIC_POWER
);
526 gpio_free(EGPIO_MAGICIAN_EP_POWER
);
528 gpio_free(EGPIO_MAGICIAN_SPK_POWER
);
533 static void __exit
magician_exit(void)
535 platform_device_unregister(magician_snd_device
);
537 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER
, 0);
538 gpio_set_value(EGPIO_MAGICIAN_EP_POWER
, 0);
539 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER
, 0);
541 gpio_free(EGPIO_MAGICIAN_IN_SEL1
);
542 gpio_free(EGPIO_MAGICIAN_IN_SEL0
);
543 gpio_free(EGPIO_MAGICIAN_MIC_POWER
);
544 gpio_free(EGPIO_MAGICIAN_EP_POWER
);
545 gpio_free(EGPIO_MAGICIAN_SPK_POWER
);
548 module_init(magician_init
);
549 module_exit(magician_exit
);
551 MODULE_AUTHOR("Philipp Zabel");
552 MODULE_DESCRIPTION("ALSA SoC Magician");
553 MODULE_LICENSE("GPL");