magician.c 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * SoC audio for HTC Magician
 *
 * Copyright (c) 2006 Philipp Zabel <philipp.zabel@gmail.com>
 *
 * based on spitz.c,
 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
 *          Richard Purdie <richard@openedhand.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/gpio.h>
23
#include <linux/i2c.h>
24 25 26 27 28

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
29
#include <sound/uda1380.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include <mach/magician.h>
#include <asm/mach-types.h>
#include "../codecs/uda1380.h"
#include "pxa2xx-i2s.h"
#include "pxa-ssp.h"

#define MAGICIAN_MIC       0
#define MAGICIAN_MIC_EXT   1

static int magician_hp_switch;
static int magician_spk_switch = 1;
static int magician_in_sel = MAGICIAN_MIC;

static void magician_ext_control(struct snd_soc_codec *codec)
{
L
Liam Girdwood 已提交
46 47
	struct snd_soc_dapm_context *dapm = &codec->dapm;

48 49
	snd_soc_dapm_mutex_lock(dapm);

50
	if (magician_spk_switch)
51
		snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
52
	else
53
		snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
54
	if (magician_hp_switch)
55
		snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
56
	else
57
		snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
58 59 60

	switch (magician_in_sel) {
	case MAGICIAN_MIC:
61 62
		snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Mic");
		snd_soc_dapm_enable_pin_unlocked(dapm, "Call Mic");
63 64
		break;
	case MAGICIAN_MIC_EXT:
65 66
		snd_soc_dapm_disable_pin_unlocked(dapm, "Call Mic");
		snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Mic");
67 68 69
		break;
	}

70 71 72
	snd_soc_dapm_sync_unlocked(dapm);

	snd_soc_dapm_mutex_unlock(dapm);
73 74 75 76 77
}

static int magician_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
78
	struct snd_soc_codec *codec = rtd->codec;
79

80 81
	mutex_lock(&codec->mutex);

82 83 84
	/* check the jack status at stream startup */
	magician_ext_control(codec);

85 86
	mutex_unlock(&codec->mutex);

87 88 89 90 91 92 93 94 95 96
	return 0;
}

/*
 * Magician uses SSP port for playback.
 */
static int magician_playback_hw_params(struct snd_pcm_substream *substream,
				       struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
97 98
	struct snd_soc_dai *codec_dai = rtd->codec_dai;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99
	unsigned int acps, acds, width;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	unsigned int div4 = PXA_SSP_CLK_SCDB_4;
	int ret = 0;

	width = snd_pcm_format_physical_width(params_format(params));

	/*
	 * rate = SSPSCLK / (2 * width(16 or 32))
	 * SSPSCLK = (ACPS / ACDS) / SSPSCLKDIV(div4 or div1)
	 */
	switch (params_rate(params)) {
	case 8000:
		/* off by a factor of 2: bug in the PXA27x audio clock? */
		acps = 32842000;
		switch (width) {
		case 16:
			/* 513156 Hz ~= _2_ * 8000 Hz * 32 (+0.23%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_16;
			break;
118
		default: /* 32 */
119 120 121 122 123 124 125 126 127 128 129
			/* 1026312 Hz ~= _2_ * 8000 Hz * 64 (+0.23%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_8;
		}
		break;
	case 11025:
		acps = 5622000;
		switch (width) {
		case 16:
			/* 351375 Hz ~= 11025 Hz * 32 (-0.41%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_4;
			break;
130
		default: /* 32 */
131 132 133 134 135 136 137 138 139 140 141
			/* 702750 Hz ~= 11025 Hz * 64 (-0.41%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_2;
		}
		break;
	case 22050:
		acps = 5622000;
		switch (width) {
		case 16:
			/* 702750 Hz ~= 22050 Hz * 32 (-0.41%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_2;
			break;
142
		default: /* 32 */
143 144 145 146 147 148 149 150 151 152 153
			/* 1405500 Hz ~= 22050 Hz * 64 (-0.41%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_1;
		}
		break;
	case 44100:
		acps = 5622000;
		switch (width) {
		case 16:
			/* 1405500 Hz ~= 44100 Hz * 32 (-0.41%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_2;
			break;
154
		default: /* 32 */
155 156 157 158 159 160 161 162 163 164 165
			/* 2811000 Hz ~= 44100 Hz * 64 (-0.41%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_1;
		}
		break;
	case 48000:
		acps = 12235000;
		switch (width) {
		case 16:
			/* 1529375 Hz ~= 48000 Hz * 32 (-0.44%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_2;
			break;
166
		default: /* 32 */
167 168 169 170 171
			/* 3058750 Hz ~= 48000 Hz * 64 (-0.44%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_1;
		}
		break;
	case 96000:
172
	default:
173 174 175 176 177 178
		acps = 12235000;
		switch (width) {
		case 16:
			/* 3058750 Hz ~= 96000 Hz * 32 (-0.44%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_1;
			break;
179
		default: /* 32 */
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
			/* 6117500 Hz ~= 96000 Hz * 64 (-0.44%) */
			acds = PXA_SSP_CLK_AUDIO_DIV_2;
			div4 = PXA_SSP_CLK_SCDB_1;
			break;
		}
		break;
	}

	/* set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
			SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		return ret;

	/* set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
196
			SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_CBS_CFS);
197 198 199
	if (ret < 0)
		return ret;

200
	ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	if (ret < 0)
		return ret;

	/* set audio clock as clock source */
	ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
			SND_SOC_CLOCK_OUT);
	if (ret < 0)
		return ret;

	/* set the SSP audio system clock ACDS divider */
	ret = snd_soc_dai_set_clkdiv(cpu_dai,
			PXA_SSP_AUDIO_DIV_ACDS, acds);
	if (ret < 0)
		return ret;

	/* set the SSP audio system clock SCDB divider4 */
	ret = snd_soc_dai_set_clkdiv(cpu_dai,
			PXA_SSP_AUDIO_DIV_SCDB, div4);
	if (ret < 0)
		return ret;

	/* set SSP audio pll clock */
223
	ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, acps);
224 225 226 227 228 229 230 231 232 233 234 235 236
	if (ret < 0)
		return ret;

	return 0;
}

/*
 * Magician uses I2S for capture.
 */
static int magician_capture_hw_params(struct snd_pcm_substream *substream,
				      struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 238
	struct snd_soc_dai *codec_dai = rtd->codec_dai;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	int ret = 0;

	/* set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai,
			SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
			SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		return ret;

	/* set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai,
			SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
			SND_SOC_DAIFMT_CBS_CFS);
	if (ret < 0)
		return ret;

	/* set the I2S system clock as output */
	ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
			SND_SOC_CLOCK_OUT);
	if (ret < 0)
		return ret;

	return 0;
}

static struct snd_soc_ops magician_capture_ops = {
	.startup = magician_startup,
	.hw_params = magician_capture_hw_params,
};

static struct snd_soc_ops magician_playback_ops = {
	.startup = magician_startup,
	.hw_params = magician_playback_hw_params,
};

static int magician_get_hp(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
{
	ucontrol->value.integer.value[0] = magician_hp_switch;
	return 0;
}

static int magician_set_hp(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);

	if (magician_hp_switch == ucontrol->value.integer.value[0])
		return 0;

	magician_hp_switch = ucontrol->value.integer.value[0];
	magician_ext_control(codec);
	return 1;
}

static int magician_get_spk(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
{
	ucontrol->value.integer.value[0] = magician_spk_switch;
	return 0;
}

static int magician_set_spk(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);

	if (magician_spk_switch == ucontrol->value.integer.value[0])
		return 0;

	magician_spk_switch = ucontrol->value.integer.value[0];
	magician_ext_control(codec);
	return 1;
}

static int magician_get_input(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	ucontrol->value.integer.value[0] = magician_in_sel;
	return 0;
}

static int magician_set_input(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	if (magician_in_sel == ucontrol->value.integer.value[0])
		return 0;

	magician_in_sel = ucontrol->value.integer.value[0];

	switch (magician_in_sel) {
	case MAGICIAN_MIC:
		gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
		break;
	case MAGICIAN_MIC_EXT:
		gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
	}

	return 1;
}

static int magician_spk_power(struct snd_soc_dapm_widget *w,
				struct snd_kcontrol *k, int event)
{
	gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
	return 0;
}

static int magician_hp_power(struct snd_soc_dapm_widget *w,
				struct snd_kcontrol *k, int event)
{
	gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
	return 0;
}

static int magician_mic_bias(struct snd_soc_dapm_widget *w,
				struct snd_kcontrol *k, int event)
{
	gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
	return 0;
}

/* magician machine dapm widgets */
static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
	SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
	SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
	SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
	SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
};

/* magician machine audio_map */
static const struct snd_soc_dapm_route audio_map[] = {

	/* Headphone connected to VOUTL, VOUTR */
	{"Headphone Jack", NULL, "VOUTL"},
	{"Headphone Jack", NULL, "VOUTR"},

	/* Speaker connected to VOUTL, VOUTR */
	{"Speaker", NULL, "VOUTL"},
	{"Speaker", NULL, "VOUTR"},

	/* Mics are connected to VINM */
	{"VINM", NULL, "Headset Mic"},
	{"VINM", NULL, "Call Mic"},
};

static const char *input_select[] = {"Call Mic", "Headset Mic"};
static const struct soc_enum magician_in_sel_enum =
	SOC_ENUM_SINGLE_EXT(2, input_select);

static const struct snd_kcontrol_new uda1380_magician_controls[] = {
	SOC_SINGLE_BOOL_EXT("Headphone Switch",
			(unsigned long)&magician_hp_switch,
			magician_get_hp, magician_set_hp),
	SOC_SINGLE_BOOL_EXT("Speaker Switch",
			(unsigned long)&magician_spk_switch,
			magician_get_spk, magician_set_spk),
	SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
			magician_get_input, magician_set_input),
};

/*
 * Logic for a uda1380 as connected on a HTC Magician
 */
403
static int magician_uda1380_init(struct snd_soc_pcm_runtime *rtd)
404
{
405
	struct snd_soc_codec *codec = rtd->codec;
L
Liam Girdwood 已提交
406
	struct snd_soc_dapm_context *dapm = &codec->dapm;
407 408 409
	int err;

	/* NC codec pins */
L
Liam Girdwood 已提交
410 411
	snd_soc_dapm_nc_pin(dapm, "VOUTLHP");
	snd_soc_dapm_nc_pin(dapm, "VOUTRHP");
412 413

	/* FIXME: is anything connected here? */
L
Liam Girdwood 已提交
414 415
	snd_soc_dapm_nc_pin(dapm, "VINL");
	snd_soc_dapm_nc_pin(dapm, "VINR");
416 417

	/* Add magician specific controls */
418
	err = snd_soc_add_codec_controls(codec, uda1380_magician_controls,
419 420 421 422 423
				ARRAY_SIZE(uda1380_magician_controls));
	if (err < 0)
		return err;

	/* Add magician specific widgets */
L
Liam Girdwood 已提交
424
	snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
425 426 427
				  ARRAY_SIZE(uda1380_dapm_widgets));

	/* Set up magician specific audio path interconnects */
L
Liam Girdwood 已提交
428
	snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
429 430 431 432 433 434 435 436 437

	return 0;
}

/* magician digital audio interface glue - connects codec <--> CPU */
static struct snd_soc_dai_link magician_dai[] = {
{
	.name = "uda1380",
	.stream_name = "UDA1380 Playback",
438 439 440 441
	.cpu_dai_name = "pxa-ssp-dai.0",
	.codec_dai_name = "uda1380-hifi-playback",
	.platform_name = "pxa-pcm-audio",
	.codec_name = "uda1380-codec.0-0018",
442 443 444 445 446 447
	.init = magician_uda1380_init,
	.ops = &magician_playback_ops,
},
{
	.name = "uda1380",
	.stream_name = "UDA1380 Capture",
448
	.cpu_dai_name = "pxa2xx-i2s",
449 450 451
	.codec_dai_name = "uda1380-hifi-capture",
	.platform_name = "pxa-pcm-audio",
	.codec_name = "uda1380-codec.0-0018",
452 453 454 455 456 457 458
	.ops = &magician_capture_ops,
}
};

/* magician audio machine driver */
static struct snd_soc_card snd_soc_card_magician = {
	.name = "Magician",
459
	.owner = THIS_MODULE,
460 461 462 463 464 465 466
	.dai_link = magician_dai,
	.num_links = ARRAY_SIZE(magician_dai),

};

static struct platform_device *magician_snd_device;

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/*
 * FIXME: move into magician board file once merged into the pxa tree
 */
static struct uda1380_platform_data uda1380_info = {
	.gpio_power = EGPIO_MAGICIAN_CODEC_POWER,
	.gpio_reset = EGPIO_MAGICIAN_CODEC_RESET,
	.dac_clk    = UDA1380_DAC_CLK_WSPLL,
};

static struct i2c_board_info i2c_board_info[] = {
	{
		I2C_BOARD_INFO("uda1380", 0x18),
		.platform_data = &uda1380_info,
	},
};

483 484 485
static int __init magician_init(void)
{
	int ret;
486 487
	struct i2c_adapter *adapter;
	struct i2c_client *client;
488 489 490 491

	if (!machine_is_magician())
		return -ENODEV;

492 493 494 495 496 497 498 499
	adapter = i2c_get_adapter(0);
	if (!adapter)
		return -ENODEV;
	client = i2c_new_device(adapter, i2c_board_info);
	i2c_put_adapter(adapter);
	if (!client)
		return -ENODEV;

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
	if (ret)
		goto err_request_spk;
	ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
	if (ret)
		goto err_request_ep;
	ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
	if (ret)
		goto err_request_mic;
	ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
	if (ret)
		goto err_request_in_sel0;
	ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
	if (ret)
		goto err_request_in_sel1;

	gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);

	magician_snd_device = platform_device_alloc("soc-audio", -1);
	if (!magician_snd_device) {
		ret = -ENOMEM;
		goto err_pdev;
	}

524
	platform_set_drvdata(magician_snd_device, &snd_soc_card_magician);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	ret = platform_device_add(magician_snd_device);
	if (ret) {
		platform_device_put(magician_snd_device);
		goto err_pdev;
	}

	return 0;

err_pdev:
	gpio_free(EGPIO_MAGICIAN_IN_SEL1);
err_request_in_sel1:
	gpio_free(EGPIO_MAGICIAN_IN_SEL0);
err_request_in_sel0:
	gpio_free(EGPIO_MAGICIAN_MIC_POWER);
err_request_mic:
	gpio_free(EGPIO_MAGICIAN_EP_POWER);
err_request_ep:
	gpio_free(EGPIO_MAGICIAN_SPK_POWER);
err_request_spk:
	return ret;
}

static void __exit magician_exit(void)
{
	platform_device_unregister(magician_snd_device);

	gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
	gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
	gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);

	gpio_free(EGPIO_MAGICIAN_IN_SEL1);
	gpio_free(EGPIO_MAGICIAN_IN_SEL0);
	gpio_free(EGPIO_MAGICIAN_MIC_POWER);
	gpio_free(EGPIO_MAGICIAN_EP_POWER);
	gpio_free(EGPIO_MAGICIAN_SPK_POWER);
}

module_init(magician_init);
module_exit(magician_exit);

MODULE_AUTHOR("Philipp Zabel");
MODULE_DESCRIPTION("ALSA SoC Magician");
MODULE_LICENSE("GPL");