therm_pm72: Convert to a new-style i2c driver
[linux-2.6/verdex.git] / sound / soc / pxa / magician.c
blobc89a3cdf31e4aea8ad464baf720512deb0ccd319
1 /*
2 * SoC audio for HTC Magician
4 * Copyright (c) 2006 Philipp Zabel <philipp.zabel@gmail.com>
6 * based on spitz.c,
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>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
30 #include <mach/magician.h>
31 #include <asm/mach-types.h>
32 #include "../codecs/uda1380.h"
33 #include "pxa2xx-pcm.h"
34 #include "pxa2xx-i2s.h"
35 #include "pxa-ssp.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_codec *codec)
46 if (magician_spk_switch)
47 snd_soc_dapm_enable_pin(codec, "Speaker");
48 else
49 snd_soc_dapm_disable_pin(codec, "Speaker");
50 if (magician_hp_switch)
51 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
52 else
53 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
55 switch (magician_in_sel) {
56 case MAGICIAN_MIC:
57 snd_soc_dapm_disable_pin(codec, "Headset Mic");
58 snd_soc_dapm_enable_pin(codec, "Call Mic");
59 break;
60 case MAGICIAN_MIC_EXT:
61 snd_soc_dapm_disable_pin(codec, "Call Mic");
62 snd_soc_dapm_enable_pin(codec, "Headset Mic");
63 break;
66 snd_soc_dapm_sync(codec);
69 static int magician_startup(struct snd_pcm_substream *substream)
71 struct snd_soc_pcm_runtime *rtd = substream->private_data;
72 struct snd_soc_codec *codec = rtd->socdev->card->codec;
74 /* check the jack status at stream startup */
75 magician_ext_control(codec);
77 return 0;
81 * Magician uses SSP port for playback.
83 static int magician_playback_hw_params(struct snd_pcm_substream *substream,
84 struct snd_pcm_hw_params *params)
86 struct snd_soc_pcm_runtime *rtd = substream->private_data;
87 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
88 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
89 unsigned int acps, acds, width, rate;
90 unsigned int div4 = PXA_SSP_CLK_SCDB_4;
91 int ret = 0;
93 rate = params_rate(params);
94 width = snd_pcm_format_physical_width(params_format(params));
97 * rate = SSPSCLK / (2 * width(16 or 32))
98 * SSPSCLK = (ACPS / ACDS) / SSPSCLKDIV(div4 or div1)
100 switch (params_rate(params)) {
101 case 8000:
102 /* off by a factor of 2: bug in the PXA27x audio clock? */
103 acps = 32842000;
104 switch (width) {
105 case 16:
106 /* 513156 Hz ~= _2_ * 8000 Hz * 32 (+0.23%) */
107 acds = PXA_SSP_CLK_AUDIO_DIV_16;
108 break;
109 default: /* 32 */
110 /* 1026312 Hz ~= _2_ * 8000 Hz * 64 (+0.23%) */
111 acds = PXA_SSP_CLK_AUDIO_DIV_8;
113 break;
114 case 11025:
115 acps = 5622000;
116 switch (width) {
117 case 16:
118 /* 351375 Hz ~= 11025 Hz * 32 (-0.41%) */
119 acds = PXA_SSP_CLK_AUDIO_DIV_4;
120 break;
121 default: /* 32 */
122 /* 702750 Hz ~= 11025 Hz * 64 (-0.41%) */
123 acds = PXA_SSP_CLK_AUDIO_DIV_2;
125 break;
126 case 22050:
127 acps = 5622000;
128 switch (width) {
129 case 16:
130 /* 702750 Hz ~= 22050 Hz * 32 (-0.41%) */
131 acds = PXA_SSP_CLK_AUDIO_DIV_2;
132 break;
133 default: /* 32 */
134 /* 1405500 Hz ~= 22050 Hz * 64 (-0.41%) */
135 acds = PXA_SSP_CLK_AUDIO_DIV_1;
137 break;
138 case 44100:
139 acps = 5622000;
140 switch (width) {
141 case 16:
142 /* 1405500 Hz ~= 44100 Hz * 32 (-0.41%) */
143 acds = PXA_SSP_CLK_AUDIO_DIV_2;
144 break;
145 default: /* 32 */
146 /* 2811000 Hz ~= 44100 Hz * 64 (-0.41%) */
147 acds = PXA_SSP_CLK_AUDIO_DIV_1;
149 break;
150 case 48000:
151 acps = 12235000;
152 switch (width) {
153 case 16:
154 /* 1529375 Hz ~= 48000 Hz * 32 (-0.44%) */
155 acds = PXA_SSP_CLK_AUDIO_DIV_2;
156 break;
157 default: /* 32 */
158 /* 3058750 Hz ~= 48000 Hz * 64 (-0.44%) */
159 acds = PXA_SSP_CLK_AUDIO_DIV_1;
161 break;
162 case 96000:
163 default:
164 acps = 12235000;
165 switch (width) {
166 case 16:
167 /* 3058750 Hz ~= 96000 Hz * 32 (-0.44%) */
168 acds = PXA_SSP_CLK_AUDIO_DIV_1;
169 break;
170 default: /* 32 */
171 /* 6117500 Hz ~= 96000 Hz * 64 (-0.44%) */
172 acds = PXA_SSP_CLK_AUDIO_DIV_2;
173 div4 = PXA_SSP_CLK_SCDB_1;
174 break;
176 break;
179 /* set codec DAI configuration */
180 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
181 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
182 if (ret < 0)
183 return ret;
185 /* set cpu DAI configuration */
186 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
187 SND_SOC_DAIFMT_IB_IF | SND_SOC_DAIFMT_CBS_CFS);
188 if (ret < 0)
189 return ret;
191 ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 1);
192 if (ret < 0)
193 return ret;
195 /* set audio clock as clock source */
196 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
197 SND_SOC_CLOCK_OUT);
198 if (ret < 0)
199 return ret;
201 /* set the SSP audio system clock ACDS divider */
202 ret = snd_soc_dai_set_clkdiv(cpu_dai,
203 PXA_SSP_AUDIO_DIV_ACDS, acds);
204 if (ret < 0)
205 return ret;
207 /* set the SSP audio system clock SCDB divider4 */
208 ret = snd_soc_dai_set_clkdiv(cpu_dai,
209 PXA_SSP_AUDIO_DIV_SCDB, div4);
210 if (ret < 0)
211 return ret;
213 /* set SSP audio pll clock */
214 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, acps);
215 if (ret < 0)
216 return ret;
218 return 0;
222 * Magician uses I2S for capture.
224 static int magician_capture_hw_params(struct snd_pcm_substream *substream,
225 struct snd_pcm_hw_params *params)
227 struct snd_soc_pcm_runtime *rtd = substream->private_data;
228 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
229 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
230 int ret = 0;
232 /* set codec DAI configuration */
233 ret = snd_soc_dai_set_fmt(codec_dai,
234 SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
235 SND_SOC_DAIFMT_CBS_CFS);
236 if (ret < 0)
237 return ret;
239 /* set cpu DAI configuration */
240 ret = snd_soc_dai_set_fmt(cpu_dai,
241 SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
242 SND_SOC_DAIFMT_CBS_CFS);
243 if (ret < 0)
244 return ret;
246 /* set the I2S system clock as output */
247 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
248 SND_SOC_CLOCK_OUT);
249 if (ret < 0)
250 return ret;
252 return 0;
255 static struct snd_soc_ops magician_capture_ops = {
256 .startup = magician_startup,
257 .hw_params = magician_capture_hw_params,
260 static struct snd_soc_ops magician_playback_ops = {
261 .startup = magician_startup,
262 .hw_params = magician_playback_hw_params,
265 static int magician_get_hp(struct snd_kcontrol *kcontrol,
266 struct snd_ctl_elem_value *ucontrol)
268 ucontrol->value.integer.value[0] = magician_hp_switch;
269 return 0;
272 static int magician_set_hp(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
275 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
277 if (magician_hp_switch == ucontrol->value.integer.value[0])
278 return 0;
280 magician_hp_switch = ucontrol->value.integer.value[0];
281 magician_ext_control(codec);
282 return 1;
285 static int magician_get_spk(struct snd_kcontrol *kcontrol,
286 struct snd_ctl_elem_value *ucontrol)
288 ucontrol->value.integer.value[0] = magician_spk_switch;
289 return 0;
292 static int magician_set_spk(struct snd_kcontrol *kcontrol,
293 struct snd_ctl_elem_value *ucontrol)
295 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
297 if (magician_spk_switch == ucontrol->value.integer.value[0])
298 return 0;
300 magician_spk_switch = ucontrol->value.integer.value[0];
301 magician_ext_control(codec);
302 return 1;
305 static int magician_get_input(struct snd_kcontrol *kcontrol,
306 struct snd_ctl_elem_value *ucontrol)
308 ucontrol->value.integer.value[0] = magician_in_sel;
309 return 0;
312 static int magician_set_input(struct snd_kcontrol *kcontrol,
313 struct snd_ctl_elem_value *ucontrol)
315 if (magician_in_sel == ucontrol->value.integer.value[0])
316 return 0;
318 magician_in_sel = ucontrol->value.integer.value[0];
320 switch (magician_in_sel) {
321 case MAGICIAN_MIC:
322 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
323 break;
324 case MAGICIAN_MIC_EXT:
325 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
328 return 1;
331 static int magician_spk_power(struct snd_soc_dapm_widget *w,
332 struct snd_kcontrol *k, int event)
334 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
335 return 0;
338 static int magician_hp_power(struct snd_soc_dapm_widget *w,
339 struct snd_kcontrol *k, int event)
341 gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
342 return 0;
345 static int magician_mic_bias(struct snd_soc_dapm_widget *w,
346 struct snd_kcontrol *k, int event)
348 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
349 return 0;
352 /* magician machine dapm widgets */
353 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
354 SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
355 SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
356 SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
357 SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
360 /* magician machine audio_map */
361 static const struct snd_soc_dapm_route audio_map[] = {
363 /* Headphone connected to VOUTL, VOUTR */
364 {"Headphone Jack", NULL, "VOUTL"},
365 {"Headphone Jack", NULL, "VOUTR"},
367 /* Speaker connected to VOUTL, VOUTR */
368 {"Speaker", NULL, "VOUTL"},
369 {"Speaker", NULL, "VOUTR"},
371 /* Mics are connected to VINM */
372 {"VINM", NULL, "Headset Mic"},
373 {"VINM", NULL, "Call Mic"},
376 static const char *input_select[] = {"Call Mic", "Headset Mic"};
377 static const struct soc_enum magician_in_sel_enum =
378 SOC_ENUM_SINGLE_EXT(2, input_select);
380 static const struct snd_kcontrol_new uda1380_magician_controls[] = {
381 SOC_SINGLE_BOOL_EXT("Headphone Switch",
382 (unsigned long)&magician_hp_switch,
383 magician_get_hp, magician_set_hp),
384 SOC_SINGLE_BOOL_EXT("Speaker Switch",
385 (unsigned long)&magician_spk_switch,
386 magician_get_spk, magician_set_spk),
387 SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
388 magician_get_input, magician_set_input),
392 * Logic for a uda1380 as connected on a HTC Magician
394 static int magician_uda1380_init(struct snd_soc_codec *codec)
396 int err;
398 /* NC codec pins */
399 snd_soc_dapm_nc_pin(codec, "VOUTLHP");
400 snd_soc_dapm_nc_pin(codec, "VOUTRHP");
402 /* FIXME: is anything connected here? */
403 snd_soc_dapm_nc_pin(codec, "VINL");
404 snd_soc_dapm_nc_pin(codec, "VINR");
406 /* Add magician specific controls */
407 err = snd_soc_add_controls(codec, uda1380_magician_controls,
408 ARRAY_SIZE(uda1380_magician_controls));
409 if (err < 0)
410 return err;
412 /* Add magician specific widgets */
413 snd_soc_dapm_new_controls(codec, uda1380_dapm_widgets,
414 ARRAY_SIZE(uda1380_dapm_widgets));
416 /* Set up magician specific audio path interconnects */
417 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
419 snd_soc_dapm_sync(codec);
420 return 0;
423 /* magician digital audio interface glue - connects codec <--> CPU */
424 static struct snd_soc_dai_link magician_dai[] = {
426 .name = "uda1380",
427 .stream_name = "UDA1380 Playback",
428 .cpu_dai = &pxa_ssp_dai[PXA_DAI_SSP1],
429 .codec_dai = &uda1380_dai[UDA1380_DAI_PLAYBACK],
430 .init = magician_uda1380_init,
431 .ops = &magician_playback_ops,
434 .name = "uda1380",
435 .stream_name = "UDA1380 Capture",
436 .cpu_dai = &pxa_i2s_dai,
437 .codec_dai = &uda1380_dai[UDA1380_DAI_CAPTURE],
438 .ops = &magician_capture_ops,
442 /* magician audio machine driver */
443 static struct snd_soc_card snd_soc_card_magician = {
444 .name = "Magician",
445 .dai_link = magician_dai,
446 .num_links = ARRAY_SIZE(magician_dai),
447 .platform = &pxa2xx_soc_platform,
450 /* magician audio private data */
451 static struct uda1380_setup_data magician_uda1380_setup = {
452 .i2c_address = 0x18,
453 .dac_clk = UDA1380_DAC_CLK_WSPLL,
456 /* magician audio subsystem */
457 static struct snd_soc_device magician_snd_devdata = {
458 .card = &snd_soc_card_magician,
459 .codec_dev = &soc_codec_dev_uda1380,
460 .codec_data = &magician_uda1380_setup,
463 static struct platform_device *magician_snd_device;
465 static int __init magician_init(void)
467 int ret;
469 if (!machine_is_magician())
470 return -ENODEV;
472 ret = gpio_request(EGPIO_MAGICIAN_CODEC_POWER, "CODEC_POWER");
473 if (ret)
474 goto err_request_power;
475 ret = gpio_request(EGPIO_MAGICIAN_CODEC_RESET, "CODEC_RESET");
476 if (ret)
477 goto err_request_reset;
478 ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
479 if (ret)
480 goto err_request_spk;
481 ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
482 if (ret)
483 goto err_request_ep;
484 ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
485 if (ret)
486 goto err_request_mic;
487 ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
488 if (ret)
489 goto err_request_in_sel0;
490 ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
491 if (ret)
492 goto err_request_in_sel1;
494 gpio_set_value(EGPIO_MAGICIAN_CODEC_POWER, 1);
495 gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);
497 /* we may need to have the clock running here - pH5 */
498 gpio_set_value(EGPIO_MAGICIAN_CODEC_RESET, 1);
499 udelay(5);
500 gpio_set_value(EGPIO_MAGICIAN_CODEC_RESET, 0);
502 magician_snd_device = platform_device_alloc("soc-audio", -1);
503 if (!magician_snd_device) {
504 ret = -ENOMEM;
505 goto err_pdev;
508 platform_set_drvdata(magician_snd_device, &magician_snd_devdata);
509 magician_snd_devdata.dev = &magician_snd_device->dev;
510 ret = platform_device_add(magician_snd_device);
511 if (ret) {
512 platform_device_put(magician_snd_device);
513 goto err_pdev;
516 return 0;
518 err_pdev:
519 gpio_free(EGPIO_MAGICIAN_IN_SEL1);
520 err_request_in_sel1:
521 gpio_free(EGPIO_MAGICIAN_IN_SEL0);
522 err_request_in_sel0:
523 gpio_free(EGPIO_MAGICIAN_MIC_POWER);
524 err_request_mic:
525 gpio_free(EGPIO_MAGICIAN_EP_POWER);
526 err_request_ep:
527 gpio_free(EGPIO_MAGICIAN_SPK_POWER);
528 err_request_spk:
529 gpio_free(EGPIO_MAGICIAN_CODEC_RESET);
530 err_request_reset:
531 gpio_free(EGPIO_MAGICIAN_CODEC_POWER);
532 err_request_power:
533 return ret;
536 static void __exit magician_exit(void)
538 platform_device_unregister(magician_snd_device);
540 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
541 gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
542 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);
543 gpio_set_value(EGPIO_MAGICIAN_CODEC_POWER, 0);
545 gpio_free(EGPIO_MAGICIAN_IN_SEL1);
546 gpio_free(EGPIO_MAGICIAN_IN_SEL0);
547 gpio_free(EGPIO_MAGICIAN_MIC_POWER);
548 gpio_free(EGPIO_MAGICIAN_EP_POWER);
549 gpio_free(EGPIO_MAGICIAN_SPK_POWER);
550 gpio_free(EGPIO_MAGICIAN_CODEC_RESET);
551 gpio_free(EGPIO_MAGICIAN_CODEC_POWER);
554 module_init(magician_init);
555 module_exit(magician_exit);
557 MODULE_AUTHOR("Philipp Zabel");
558 MODULE_DESCRIPTION("ALSA SoC Magician");
559 MODULE_LICENSE("GPL");