net/sh-eth: Check return value of sh_eth_reset when chip reset fail
[linux-2.6.git] / sound / soc / pxa / magician.c
blobaace19e0fe2c217f734dbd0164b4d242a93be1d1
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>
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"
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 struct snd_soc_dapm_context *dapm = &codec->dapm;
48 if (magician_spk_switch)
49 snd_soc_dapm_enable_pin(dapm, "Speaker");
50 else
51 snd_soc_dapm_disable_pin(dapm, "Speaker");
52 if (magician_hp_switch)
53 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
54 else
55 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
57 switch (magician_in_sel) {
58 case MAGICIAN_MIC:
59 snd_soc_dapm_disable_pin(dapm, "Headset Mic");
60 snd_soc_dapm_enable_pin(dapm, "Call Mic");
61 break;
62 case MAGICIAN_MIC_EXT:
63 snd_soc_dapm_disable_pin(dapm, "Call Mic");
64 snd_soc_dapm_enable_pin(dapm, "Headset Mic");
65 break;
68 snd_soc_dapm_sync(dapm);
71 static int magician_startup(struct snd_pcm_substream *substream)
73 struct snd_soc_pcm_runtime *rtd = substream->private_data;
74 struct snd_soc_codec *codec = rtd->codec;
76 mutex_lock(&codec->mutex);
78 /* check the jack status at stream startup */
79 magician_ext_control(codec);
81 mutex_unlock(&codec->mutex);
83 return 0;
87 * Magician uses SSP port for playback.
89 static int magician_playback_hw_params(struct snd_pcm_substream *substream,
90 struct snd_pcm_hw_params *params)
92 struct snd_soc_pcm_runtime *rtd = substream->private_data;
93 struct snd_soc_dai *codec_dai = rtd->codec_dai;
94 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
95 unsigned int acps, acds, width;
96 unsigned int div4 = PXA_SSP_CLK_SCDB_4;
97 int ret = 0;
99 width = snd_pcm_format_physical_width(params_format(params));
102 * rate = SSPSCLK / (2 * width(16 or 32))
103 * SSPSCLK = (ACPS / ACDS) / SSPSCLKDIV(div4 or div1)
105 switch (params_rate(params)) {
106 case 8000:
107 /* off by a factor of 2: bug in the PXA27x audio clock? */
108 acps = 32842000;
109 switch (width) {
110 case 16:
111 /* 513156 Hz ~= _2_ * 8000 Hz * 32 (+0.23%) */
112 acds = PXA_SSP_CLK_AUDIO_DIV_16;
113 break;
114 default: /* 32 */
115 /* 1026312 Hz ~= _2_ * 8000 Hz * 64 (+0.23%) */
116 acds = PXA_SSP_CLK_AUDIO_DIV_8;
118 break;
119 case 11025:
120 acps = 5622000;
121 switch (width) {
122 case 16:
123 /* 351375 Hz ~= 11025 Hz * 32 (-0.41%) */
124 acds = PXA_SSP_CLK_AUDIO_DIV_4;
125 break;
126 default: /* 32 */
127 /* 702750 Hz ~= 11025 Hz * 64 (-0.41%) */
128 acds = PXA_SSP_CLK_AUDIO_DIV_2;
130 break;
131 case 22050:
132 acps = 5622000;
133 switch (width) {
134 case 16:
135 /* 702750 Hz ~= 22050 Hz * 32 (-0.41%) */
136 acds = PXA_SSP_CLK_AUDIO_DIV_2;
137 break;
138 default: /* 32 */
139 /* 1405500 Hz ~= 22050 Hz * 64 (-0.41%) */
140 acds = PXA_SSP_CLK_AUDIO_DIV_1;
142 break;
143 case 44100:
144 acps = 5622000;
145 switch (width) {
146 case 16:
147 /* 1405500 Hz ~= 44100 Hz * 32 (-0.41%) */
148 acds = PXA_SSP_CLK_AUDIO_DIV_2;
149 break;
150 default: /* 32 */
151 /* 2811000 Hz ~= 44100 Hz * 64 (-0.41%) */
152 acds = PXA_SSP_CLK_AUDIO_DIV_1;
154 break;
155 case 48000:
156 acps = 12235000;
157 switch (width) {
158 case 16:
159 /* 1529375 Hz ~= 48000 Hz * 32 (-0.44%) */
160 acds = PXA_SSP_CLK_AUDIO_DIV_2;
161 break;
162 default: /* 32 */
163 /* 3058750 Hz ~= 48000 Hz * 64 (-0.44%) */
164 acds = PXA_SSP_CLK_AUDIO_DIV_1;
166 break;
167 case 96000:
168 default:
169 acps = 12235000;
170 switch (width) {
171 case 16:
172 /* 3058750 Hz ~= 96000 Hz * 32 (-0.44%) */
173 acds = PXA_SSP_CLK_AUDIO_DIV_1;
174 break;
175 default: /* 32 */
176 /* 6117500 Hz ~= 96000 Hz * 64 (-0.44%) */
177 acds = PXA_SSP_CLK_AUDIO_DIV_2;
178 div4 = PXA_SSP_CLK_SCDB_1;
179 break;
181 break;
184 /* set codec DAI configuration */
185 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
186 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
187 if (ret < 0)
188 return ret;
190 /* set cpu DAI configuration */
191 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
192 SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_CBS_CFS);
193 if (ret < 0)
194 return ret;
196 ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
197 if (ret < 0)
198 return ret;
200 /* set audio clock as clock source */
201 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
202 SND_SOC_CLOCK_OUT);
203 if (ret < 0)
204 return ret;
206 /* set the SSP audio system clock ACDS divider */
207 ret = snd_soc_dai_set_clkdiv(cpu_dai,
208 PXA_SSP_AUDIO_DIV_ACDS, acds);
209 if (ret < 0)
210 return ret;
212 /* set the SSP audio system clock SCDB divider4 */
213 ret = snd_soc_dai_set_clkdiv(cpu_dai,
214 PXA_SSP_AUDIO_DIV_SCDB, div4);
215 if (ret < 0)
216 return ret;
218 /* set SSP audio pll clock */
219 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, acps);
220 if (ret < 0)
221 return ret;
223 return 0;
227 * Magician uses I2S for capture.
229 static int magician_capture_hw_params(struct snd_pcm_substream *substream,
230 struct snd_pcm_hw_params *params)
232 struct snd_soc_pcm_runtime *rtd = substream->private_data;
233 struct snd_soc_dai *codec_dai = rtd->codec_dai;
234 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
235 int ret = 0;
237 /* set codec DAI configuration */
238 ret = snd_soc_dai_set_fmt(codec_dai,
239 SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
240 SND_SOC_DAIFMT_CBS_CFS);
241 if (ret < 0)
242 return ret;
244 /* set cpu DAI configuration */
245 ret = snd_soc_dai_set_fmt(cpu_dai,
246 SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
247 SND_SOC_DAIFMT_CBS_CFS);
248 if (ret < 0)
249 return ret;
251 /* set the I2S system clock as output */
252 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
253 SND_SOC_CLOCK_OUT);
254 if (ret < 0)
255 return ret;
257 return 0;
260 static struct snd_soc_ops magician_capture_ops = {
261 .startup = magician_startup,
262 .hw_params = magician_capture_hw_params,
265 static struct snd_soc_ops magician_playback_ops = {
266 .startup = magician_startup,
267 .hw_params = magician_playback_hw_params,
270 static int magician_get_hp(struct snd_kcontrol *kcontrol,
271 struct snd_ctl_elem_value *ucontrol)
273 ucontrol->value.integer.value[0] = magician_hp_switch;
274 return 0;
277 static int magician_set_hp(struct snd_kcontrol *kcontrol,
278 struct snd_ctl_elem_value *ucontrol)
280 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
282 if (magician_hp_switch == ucontrol->value.integer.value[0])
283 return 0;
285 magician_hp_switch = ucontrol->value.integer.value[0];
286 magician_ext_control(codec);
287 return 1;
290 static int magician_get_spk(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *ucontrol)
293 ucontrol->value.integer.value[0] = magician_spk_switch;
294 return 0;
297 static int magician_set_spk(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_value *ucontrol)
300 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
302 if (magician_spk_switch == ucontrol->value.integer.value[0])
303 return 0;
305 magician_spk_switch = ucontrol->value.integer.value[0];
306 magician_ext_control(codec);
307 return 1;
310 static int magician_get_input(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
313 ucontrol->value.integer.value[0] = magician_in_sel;
314 return 0;
317 static int magician_set_input(struct snd_kcontrol *kcontrol,
318 struct snd_ctl_elem_value *ucontrol)
320 if (magician_in_sel == ucontrol->value.integer.value[0])
321 return 0;
323 magician_in_sel = ucontrol->value.integer.value[0];
325 switch (magician_in_sel) {
326 case MAGICIAN_MIC:
327 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
328 break;
329 case MAGICIAN_MIC_EXT:
330 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
333 return 1;
336 static int magician_spk_power(struct snd_soc_dapm_widget *w,
337 struct snd_kcontrol *k, int event)
339 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
340 return 0;
343 static int magician_hp_power(struct snd_soc_dapm_widget *w,
344 struct snd_kcontrol *k, int event)
346 gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
347 return 0;
350 static int magician_mic_bias(struct snd_soc_dapm_widget *w,
351 struct snd_kcontrol *k, int event)
353 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
354 return 0;
357 /* magician machine dapm widgets */
358 static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
359 SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
360 SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
361 SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
362 SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
365 /* magician machine audio_map */
366 static const struct snd_soc_dapm_route audio_map[] = {
368 /* Headphone connected to VOUTL, VOUTR */
369 {"Headphone Jack", NULL, "VOUTL"},
370 {"Headphone Jack", NULL, "VOUTR"},
372 /* Speaker connected to VOUTL, VOUTR */
373 {"Speaker", NULL, "VOUTL"},
374 {"Speaker", NULL, "VOUTR"},
376 /* Mics are connected to VINM */
377 {"VINM", NULL, "Headset Mic"},
378 {"VINM", NULL, "Call Mic"},
381 static const char *input_select[] = {"Call Mic", "Headset Mic"};
382 static const struct soc_enum magician_in_sel_enum =
383 SOC_ENUM_SINGLE_EXT(2, input_select);
385 static const struct snd_kcontrol_new uda1380_magician_controls[] = {
386 SOC_SINGLE_BOOL_EXT("Headphone Switch",
387 (unsigned long)&magician_hp_switch,
388 magician_get_hp, magician_set_hp),
389 SOC_SINGLE_BOOL_EXT("Speaker Switch",
390 (unsigned long)&magician_spk_switch,
391 magician_get_spk, magician_set_spk),
392 SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
393 magician_get_input, magician_set_input),
397 * Logic for a uda1380 as connected on a HTC Magician
399 static int magician_uda1380_init(struct snd_soc_pcm_runtime *rtd)
401 struct snd_soc_codec *codec = rtd->codec;
402 struct snd_soc_dapm_context *dapm = &codec->dapm;
403 int err;
405 /* NC codec pins */
406 snd_soc_dapm_nc_pin(dapm, "VOUTLHP");
407 snd_soc_dapm_nc_pin(dapm, "VOUTRHP");
409 /* FIXME: is anything connected here? */
410 snd_soc_dapm_nc_pin(dapm, "VINL");
411 snd_soc_dapm_nc_pin(dapm, "VINR");
413 /* Add magician specific controls */
414 err = snd_soc_add_codec_controls(codec, uda1380_magician_controls,
415 ARRAY_SIZE(uda1380_magician_controls));
416 if (err < 0)
417 return err;
419 /* Add magician specific widgets */
420 snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
421 ARRAY_SIZE(uda1380_dapm_widgets));
423 /* Set up magician specific audio path interconnects */
424 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
426 return 0;
429 /* magician digital audio interface glue - connects codec <--> CPU */
430 static struct snd_soc_dai_link magician_dai[] = {
432 .name = "uda1380",
433 .stream_name = "UDA1380 Playback",
434 .cpu_dai_name = "pxa-ssp-dai.0",
435 .codec_dai_name = "uda1380-hifi-playback",
436 .platform_name = "pxa-pcm-audio",
437 .codec_name = "uda1380-codec.0-0018",
438 .init = magician_uda1380_init,
439 .ops = &magician_playback_ops,
442 .name = "uda1380",
443 .stream_name = "UDA1380 Capture",
444 .cpu_dai_name = "pxa2xx-i2s",
445 .codec_dai_name = "uda1380-hifi-capture",
446 .platform_name = "pxa-pcm-audio",
447 .codec_name = "uda1380-codec.0-0018",
448 .ops = &magician_capture_ops,
452 /* magician audio machine driver */
453 static struct snd_soc_card snd_soc_card_magician = {
454 .name = "Magician",
455 .owner = THIS_MODULE,
456 .dai_link = magician_dai,
457 .num_links = ARRAY_SIZE(magician_dai),
461 static struct platform_device *magician_snd_device;
464 * FIXME: move into magician board file once merged into the pxa tree
466 static struct uda1380_platform_data uda1380_info = {
467 .gpio_power = EGPIO_MAGICIAN_CODEC_POWER,
468 .gpio_reset = EGPIO_MAGICIAN_CODEC_RESET,
469 .dac_clk = UDA1380_DAC_CLK_WSPLL,
472 static struct i2c_board_info i2c_board_info[] = {
474 I2C_BOARD_INFO("uda1380", 0x18),
475 .platform_data = &uda1380_info,
479 static int __init magician_init(void)
481 int ret;
482 struct i2c_adapter *adapter;
483 struct i2c_client *client;
485 if (!machine_is_magician())
486 return -ENODEV;
488 adapter = i2c_get_adapter(0);
489 if (!adapter)
490 return -ENODEV;
491 client = i2c_new_device(adapter, i2c_board_info);
492 i2c_put_adapter(adapter);
493 if (!client)
494 return -ENODEV;
496 ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
497 if (ret)
498 goto err_request_spk;
499 ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
500 if (ret)
501 goto err_request_ep;
502 ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
503 if (ret)
504 goto err_request_mic;
505 ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
506 if (ret)
507 goto err_request_in_sel0;
508 ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
509 if (ret)
510 goto err_request_in_sel1;
512 gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);
514 magician_snd_device = platform_device_alloc("soc-audio", -1);
515 if (!magician_snd_device) {
516 ret = -ENOMEM;
517 goto err_pdev;
520 platform_set_drvdata(magician_snd_device, &snd_soc_card_magician);
521 ret = platform_device_add(magician_snd_device);
522 if (ret) {
523 platform_device_put(magician_snd_device);
524 goto err_pdev;
527 return 0;
529 err_pdev:
530 gpio_free(EGPIO_MAGICIAN_IN_SEL1);
531 err_request_in_sel1:
532 gpio_free(EGPIO_MAGICIAN_IN_SEL0);
533 err_request_in_sel0:
534 gpio_free(EGPIO_MAGICIAN_MIC_POWER);
535 err_request_mic:
536 gpio_free(EGPIO_MAGICIAN_EP_POWER);
537 err_request_ep:
538 gpio_free(EGPIO_MAGICIAN_SPK_POWER);
539 err_request_spk:
540 return ret;
543 static void __exit magician_exit(void)
545 platform_device_unregister(magician_snd_device);
547 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
548 gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
549 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);
551 gpio_free(EGPIO_MAGICIAN_IN_SEL1);
552 gpio_free(EGPIO_MAGICIAN_IN_SEL0);
553 gpio_free(EGPIO_MAGICIAN_MIC_POWER);
554 gpio_free(EGPIO_MAGICIAN_EP_POWER);
555 gpio_free(EGPIO_MAGICIAN_SPK_POWER);
558 module_init(magician_init);
559 module_exit(magician_exit);
561 MODULE_AUTHOR("Philipp Zabel");
562 MODULE_DESCRIPTION("ALSA SoC Magician");
563 MODULE_LICENSE("GPL");