bells.c 11.7 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 23 24 25 26 27 28 29
/*
 * Bells audio support
 *
 * Copyright 2012 Wolfson Microelectronics
 *
 * 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 <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/jack.h>
#include <linux/gpio.h>
#include <linux/module.h>

#include "../codecs/wm5102.h"
#include "../codecs/wm9081.h"

/* BCLK2 is fixed at this currently */
#define BCLK2_RATE (64 * 8000)

/*
 * Expect a 24.576MHz crystal if one is fitted (the driver will function
 * if this is not fitted).
 */
#define MCLK_RATE 24576000

M
Mark Brown 已提交
30
#define SYS_AUDIO_RATE 44100
D
Dimitris Papastamos 已提交
31
#define SYS_MCLK_RATE  (SYS_AUDIO_RATE * 512)
M
Mark Brown 已提交
32 33 34 35 36

#define DAI_AP_DSP    0
#define DAI_DSP_CODEC 1
#define DAI_CODEC_CP  2
#define DAI_CODEC_SUB 3
37

38 39 40 41 42
struct bells_drvdata {
	int sysclk_rate;
	int asyncclk_rate;
};

M
Mark Brown 已提交
43 44 45 46
static struct bells_drvdata wm2200_drvdata = {
	.sysclk_rate = 22579200,
};

47 48 49 50 51 52 53 54 55 56
static struct bells_drvdata wm5102_drvdata = {
	.sysclk_rate = 45158400,
	.asyncclk_rate = 49152000,
};

static struct bells_drvdata wm5110_drvdata = {
	.sysclk_rate = 135475200,
	.asyncclk_rate = 147456000,
};

57 58 59 60
static int bells_set_bias_level(struct snd_soc_card *card,
				struct snd_soc_dapm_context *dapm,
				enum snd_soc_bias_level level)
{
61 62
	struct snd_soc_pcm_runtime *rtd;
	struct snd_soc_dai *codec_dai;
63
	struct snd_soc_component *component;
64
	struct bells_drvdata *bells = card->drvdata;
65 66
	int ret;

67 68
	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[DAI_DSP_CODEC].name);
	codec_dai = rtd->codec_dai;
69
	component = codec_dai->component;
70

71 72 73 74 75
	if (dapm->dev != codec_dai->dev)
		return 0;

	switch (level) {
	case SND_SOC_BIAS_PREPARE:
76 77
		if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
			break;
78

79
		ret = snd_soc_component_set_pll(component, WM5102_FLL1,
80 81 82 83 84 85 86
					    ARIZONA_FLL_SRC_MCLK1,
					    MCLK_RATE,
					    bells->sysclk_rate);
		if (ret < 0)
			pr_err("Failed to start FLL: %d\n", ret);

		if (bells->asyncclk_rate) {
87
			ret = snd_soc_component_set_pll(component, WM5102_FLL2,
88 89
						    ARIZONA_FLL_SRC_AIF2BCLK,
						    BCLK2_RATE,
90
						    bells->asyncclk_rate);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
			if (ret < 0)
				pr_err("Failed to start FLL: %d\n", ret);
		}
		break;

	default:
		break;
	}

	return 0;
}

static int bells_set_bias_level_post(struct snd_soc_card *card,
				     struct snd_soc_dapm_context *dapm,
				     enum snd_soc_bias_level level)
{
107 108
	struct snd_soc_pcm_runtime *rtd;
	struct snd_soc_dai *codec_dai;
109
	struct snd_soc_component *component;
110
	struct bells_drvdata *bells = card->drvdata;
111 112
	int ret;

113 114
	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[DAI_DSP_CODEC].name);
	codec_dai = rtd->codec_dai;
115
	component = codec_dai->component;
116

117 118 119 120 121
	if (dapm->dev != codec_dai->dev)
		return 0;

	switch (level) {
	case SND_SOC_BIAS_STANDBY:
122
		ret = snd_soc_component_set_pll(component, WM5102_FLL1, 0, 0, 0);
123 124 125 126 127
		if (ret < 0) {
			pr_err("Failed to stop FLL: %d\n", ret);
			return ret;
		}

128
		if (bells->asyncclk_rate) {
129
			ret = snd_soc_component_set_pll(component, WM5102_FLL2,
130 131 132 133 134
						    0, 0, 0);
			if (ret < 0) {
				pr_err("Failed to stop FLL: %d\n", ret);
				return ret;
			}
135 136 137 138 139 140 141 142 143 144 145 146 147 148
		}
		break;

	default:
		break;
	}

	dapm->bias_level = level;

	return 0;
}

static int bells_late_probe(struct snd_soc_card *card)
{
149
	struct bells_drvdata *bells = card->drvdata;
150
	struct snd_soc_pcm_runtime *rtd;
151 152
	struct snd_soc_component *wm0010;
	struct snd_soc_component *component;
153
	struct snd_soc_dai *aif1_dai;
154 155 156
	struct snd_soc_dai *aif2_dai;
	struct snd_soc_dai *aif3_dai;
	struct snd_soc_dai *wm9081_dai;
157 158
	int ret;

159
	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[DAI_AP_DSP].name);
160
	wm0010 = rtd->codec_dai->component;
161 162

	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[DAI_DSP_CODEC].name);
163
	component = rtd->codec_dai->component;
164 165
	aif1_dai = rtd->codec_dai;

166
	ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK,
167 168 169 170
				       ARIZONA_CLK_SRC_FLL1,
				       bells->sysclk_rate,
				       SND_SOC_CLOCK_IN);
	if (ret != 0) {
171
		dev_err(component->dev, "Failed to set SYSCLK: %d\n", ret);
172 173 174
		return ret;
	}

175
	ret = snd_soc_component_set_sysclk(wm0010, 0, 0, SYS_MCLK_RATE, 0);
M
Mark Brown 已提交
176 177 178 179 180
	if (ret != 0) {
		dev_err(wm0010->dev, "Failed to set WM0010 clock: %d\n", ret);
		return ret;
	}

181
	ret = snd_soc_dai_set_sysclk(aif1_dai, ARIZONA_CLK_SYSCLK, 0, 0);
182
	if (ret != 0)
183 184
		dev_err(aif1_dai->dev, "Failed to set AIF1 clock: %d\n", ret);

185
	ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_OPCLK, 0,
186 187
				       SYS_MCLK_RATE, SND_SOC_CLOCK_OUT);
	if (ret != 0)
188
		dev_err(component->dev, "Failed to set OPCLK: %d\n", ret);
189

190 191
	if (card->num_rtd == DAI_CODEC_CP)
		return 0;
192

193
	ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_ASYNCCLK,
194 195
				       ARIZONA_CLK_SRC_FLL2,
				       bells->asyncclk_rate,
196 197
				       SND_SOC_CLOCK_IN);
	if (ret != 0) {
198
		dev_err(component->dev, "Failed to set ASYNCCLK: %d\n", ret);
199 200 201
		return ret;
	}

202 203
	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[DAI_CODEC_CP].name);
	aif2_dai = rtd->cpu_dai;
204 205

	ret = snd_soc_dai_set_sysclk(aif2_dai, ARIZONA_CLK_ASYNCCLK, 0, 0);
206
	if (ret != 0) {
207
		dev_err(aif2_dai->dev, "Failed to set AIF2 clock: %d\n", ret);
208 209 210
		return ret;
	}

211 212 213
	if (card->num_rtd == DAI_CODEC_SUB)
		return 0;

214 215 216
	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[DAI_CODEC_SUB].name);
	aif3_dai = rtd->cpu_dai;
	wm9081_dai = rtd->codec_dai;
217 218

	ret = snd_soc_dai_set_sysclk(aif3_dai, ARIZONA_CLK_SYSCLK, 0, 0);
219
	if (ret != 0) {
220
		dev_err(aif1_dai->dev, "Failed to set AIF1 clock: %d\n", ret);
221 222 223
		return ret;
	}

224
	ret = snd_soc_component_set_sysclk(wm9081_dai->component, WM9081_SYSCLK_MCLK,
M
Mark Brown 已提交
225
				       0, SYS_MCLK_RATE, 0);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	if (ret != 0) {
		dev_err(wm9081_dai->dev, "Failed to set MCLK: %d\n", ret);
		return ret;
	}

	return 0;
}

static const struct snd_soc_pcm_stream baseband_params = {
	.formats = SNDRV_PCM_FMTBIT_S32_LE,
	.rate_min = 8000,
	.rate_max = 8000,
	.channels_min = 2,
	.channels_max = 2,
};

static const struct snd_soc_pcm_stream sub_params = {
	.formats = SNDRV_PCM_FMTBIT_S32_LE,
M
Mark Brown 已提交
244 245
	.rate_min = SYS_AUDIO_RATE,
	.rate_max = SYS_AUDIO_RATE,
246 247 248 249
	.channels_min = 2,
	.channels_max = 2,
};

M
Mark Brown 已提交
250 251 252 253 254 255
static struct snd_soc_dai_link bells_dai_wm2200[] = {
	{
		.name = "CPU-DSP",
		.stream_name = "CPU-DSP",
		.cpu_dai_name = "samsung-i2s.0",
		.codec_dai_name = "wm0010-sdi1",
256
		.platform_name = "samsung-i2s.0",
M
Mark Brown 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		.codec_name = "spi0.0",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
	},
	{
		.name = "DSP-CODEC",
		.stream_name = "DSP-CODEC",
		.cpu_dai_name = "wm0010-sdi2",
		.codec_dai_name = "wm2200",
		.codec_name = "wm2200.1-003a",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
		.params = &sub_params,
		.ignore_suspend = 1,
	},
};

274 275
static struct snd_soc_dai_link bells_dai_wm5102[] = {
	{
M
Mark Brown 已提交
276 277
		.name = "CPU-DSP",
		.stream_name = "CPU-DSP",
278
		.cpu_dai_name = "samsung-i2s.0",
M
Mark Brown 已提交
279
		.codec_dai_name = "wm0010-sdi1",
280
		.platform_name = "samsung-i2s.0",
M
Mark Brown 已提交
281 282 283 284 285 286 287 288 289
		.codec_name = "spi0.0",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
	},
	{
		.name = "DSP-CODEC",
		.stream_name = "DSP-CODEC",
		.cpu_dai_name = "wm0010-sdi2",
		.codec_dai_name = "wm5102-aif1",
290 291 292
		.codec_name = "wm5102-codec",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
M
Mark Brown 已提交
293 294
		.params = &sub_params,
		.ignore_suspend = 1,
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
	},
	{
		.name = "Baseband",
		.stream_name = "Baseband",
		.cpu_dai_name = "wm5102-aif2",
		.codec_dai_name = "wm1250-ev1",
		.codec_name = "wm1250-ev1.1-0027",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
		.ignore_suspend = 1,
		.params = &baseband_params,
	},
	{
		.name = "Sub",
		.stream_name = "Sub",
		.cpu_dai_name = "wm5102-aif3",
		.codec_dai_name = "wm9081-hifi",
		.codec_name = "wm9081.1-006c",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBS_CFS,
		.ignore_suspend = 1,
		.params = &sub_params,
	},
};

static struct snd_soc_dai_link bells_dai_wm5110[] = {
	{
M
Mark Brown 已提交
322 323
		.name = "CPU-DSP",
		.stream_name = "CPU-DSP",
324
		.cpu_dai_name = "samsung-i2s.0",
M
Mark Brown 已提交
325
		.codec_dai_name = "wm0010-sdi1",
326
		.platform_name = "samsung-i2s.0",
M
Mark Brown 已提交
327 328 329 330 331 332 333 334 335
		.codec_name = "spi0.0",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
	},
	{
		.name = "DSP-CODEC",
		.stream_name = "DSP-CODEC",
		.cpu_dai_name = "wm0010-sdi2",
		.codec_dai_name = "wm5110-aif1",
336 337 338
		.codec_name = "wm5110-codec",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
M
Mark Brown 已提交
339 340
		.params = &sub_params,
		.ignore_suspend = 1,
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	},
	{
		.name = "Baseband",
		.stream_name = "Baseband",
		.cpu_dai_name = "wm5110-aif2",
		.codec_dai_name = "wm1250-ev1",
		.codec_name = "wm1250-ev1.1-0027",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBM_CFM,
		.ignore_suspend = 1,
		.params = &baseband_params,
	},
	{
		.name = "Sub",
		.stream_name = "Sub",
356
		.cpu_dai_name = "wm5110-aif3",
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
		.codec_dai_name = "wm9081-hifi",
		.codec_name = "wm9081.1-006c",
		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
				| SND_SOC_DAIFMT_CBS_CFS,
		.ignore_suspend = 1,
		.params = &sub_params,
	},
};

static struct snd_soc_codec_conf bells_codec_conf[] = {
	{
		.dev_name = "wm9081.1-006c",
		.name_prefix = "Sub",
	},
};

373 374 375 376
static struct snd_soc_dapm_widget bells_widgets[] = {
	SND_SOC_DAPM_MIC("DMIC", NULL),
};

377 378
static struct snd_soc_dapm_route bells_routes[] = {
	{ "Sub CLK_SYS", NULL, "OPCLK" },
379
	{ "CLKIN", NULL, "OPCLK" },
380 381 382 383

	{ "DMIC", NULL, "MICBIAS2" },
	{ "IN2L", NULL, "DMIC" },
	{ "IN2R", NULL, "DMIC" },
384 385 386
};

static struct snd_soc_card bells_cards[] = {
M
Mark Brown 已提交
387 388 389 390 391 392 393 394 395 396
	{
		.name = "Bells WM2200",
		.owner = THIS_MODULE,
		.dai_link = bells_dai_wm2200,
		.num_links = ARRAY_SIZE(bells_dai_wm2200),
		.codec_conf = bells_codec_conf,
		.num_configs = ARRAY_SIZE(bells_codec_conf),

		.late_probe = bells_late_probe,

397 398
		.dapm_widgets = bells_widgets,
		.num_dapm_widgets = ARRAY_SIZE(bells_widgets),
M
Mark Brown 已提交
399 400 401 402 403 404 405 406
		.dapm_routes = bells_routes,
		.num_dapm_routes = ARRAY_SIZE(bells_routes),

		.set_bias_level = bells_set_bias_level,
		.set_bias_level_post = bells_set_bias_level_post,

		.drvdata = &wm2200_drvdata,
	},
407 408 409 410 411 412 413 414 415 416
	{
		.name = "Bells WM5102",
		.owner = THIS_MODULE,
		.dai_link = bells_dai_wm5102,
		.num_links = ARRAY_SIZE(bells_dai_wm5102),
		.codec_conf = bells_codec_conf,
		.num_configs = ARRAY_SIZE(bells_codec_conf),

		.late_probe = bells_late_probe,

417 418
		.dapm_widgets = bells_widgets,
		.num_dapm_widgets = ARRAY_SIZE(bells_widgets),
419 420 421 422 423
		.dapm_routes = bells_routes,
		.num_dapm_routes = ARRAY_SIZE(bells_routes),

		.set_bias_level = bells_set_bias_level,
		.set_bias_level_post = bells_set_bias_level_post,
424 425

		.drvdata = &wm5102_drvdata,
426 427 428 429 430 431 432 433 434 435 436
	},
	{
		.name = "Bells WM5110",
		.owner = THIS_MODULE,
		.dai_link = bells_dai_wm5110,
		.num_links = ARRAY_SIZE(bells_dai_wm5110),
		.codec_conf = bells_codec_conf,
		.num_configs = ARRAY_SIZE(bells_codec_conf),

		.late_probe = bells_late_probe,

437 438
		.dapm_widgets = bells_widgets,
		.num_dapm_widgets = ARRAY_SIZE(bells_widgets),
439 440 441 442 443
		.dapm_routes = bells_routes,
		.num_dapm_routes = ARRAY_SIZE(bells_routes),

		.set_bias_level = bells_set_bias_level,
		.set_bias_level_post = bells_set_bias_level_post,
444 445

		.drvdata = &wm5110_drvdata,
446 447 448
	},
};

449
static int bells_probe(struct platform_device *pdev)
450 451 452 453 454
{
	int ret;

	bells_cards[pdev->id].dev = &pdev->dev;

455 456
	ret = devm_snd_soc_register_card(&pdev->dev, &bells_cards[pdev->id]);
	if (ret)
457 458 459 460
		dev_err(&pdev->dev,
			"snd_soc_register_card(%s) failed: %d\n",
			bells_cards[pdev->id].name, ret);

461
	return ret;
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
}

static struct platform_driver bells_driver = {
	.driver = {
		.name = "bells",
		.pm = &snd_soc_pm_ops,
	},
	.probe = bells_probe,
};

module_platform_driver(bells_driver);

MODULE_DESCRIPTION("Bells audio support");
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:bells");