summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/mxs_spdif.c
blob: af47dcb547cf9f7eaf2bc772fcd07e8a422294e9 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * ALSA SoC MXS SPDIF codec driver
 *
 * Copyright (C) 2008-2010 Freescale Semiconductor, Inc.
 *
 * Based on stmp3xxx_spdif.h by:
 * Vladimir Barinov <vbarinov@embeddedalley.com>
 *
 * Copyright 2008 SigmaTel, Inc
 * Copyright 2008 Embedded Alley Solutions, Inc
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2.  This program  is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 */
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/clk.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <asm/dma.h>
#include <mach/hardware.h>

#include "mxs_spdif.h"

#define REGS_SPDIF_BASE IO_ADDRESS(SPDIF_PHYS_ADDR)

#define BF(value, field) (((value) << BP_##field) & BM_##field)

struct mxs_codec_priv {
	struct clk *clk;
	struct snd_soc_codec codec;
};

/*
 * ALSA API
 */
static void __iomem *spdif_regmap[] = {
	REGS_SPDIF_BASE + HW_SPDIF_CTRL,
	REGS_SPDIF_BASE + HW_SPDIF_STAT,
	REGS_SPDIF_BASE + HW_SPDIF_FRAMECTRL,
	REGS_SPDIF_BASE + HW_SPDIF_SRR,
	REGS_SPDIF_BASE + HW_SPDIF_DEBUG,
	REGS_SPDIF_BASE + HW_SPDIF_DATA,
	REGS_SPDIF_BASE + HW_SPDIF_VERSION,
};

/*
 * ALSA core supports only 16 bit registers. It means we have to simulate it
 * by virtually splitting a 32bit SPDIF registers into two halves
 * high (bits 31:16) and low (bits 15:0). The routins abow detects which part
 * of 32bit register is accessed.
 */
static int mxs_codec_write(struct snd_soc_codec *codec,
			   unsigned int reg, unsigned int value)
{
	unsigned int reg_val;
	unsigned int mask = 0xffff;

	if (reg >= SPDIF_REGNUM)
		return -EIO;

	if (reg & 0x1) {
		mask <<= 16;
		value <<= 16;
	}

	reg_val = __raw_readl(spdif_regmap[reg >> 1]);
	reg_val = (reg_val & ~mask) | value;
	__raw_writel(reg_val, spdif_regmap[reg >> 1]);

	return 0;
}

static unsigned int mxs_codec_read(struct snd_soc_codec *codec,
				   unsigned int reg)
{
	unsigned int reg_val;

	if (reg >= SPDIF_REGNUM)
		return -1;

	reg_val = __raw_readl(spdif_regmap[reg >> 1]);
	if (reg & 1)
		reg_val >>= 16;

	return reg_val & 0xffff;
}

/* Codec controls */
static const struct snd_kcontrol_new mxs_snd_controls[] = {
	SOC_SINGLE("PRO", SPDIF_FRAMECTRL_L, 0, 0x1, 0),
	SOC_SINGLE("AUDIO", SPDIF_FRAMECTRL_L, 1, 0x1, 0),
	SOC_SINGLE("COPY", SPDIF_FRAMECTRL_L, 2, 0x1, 0),
	SOC_SINGLE("PRE", SPDIF_FRAMECTRL_L, 3, 0x1, 0),
	SOC_SINGLE("CC", SPDIF_FRAMECTRL_L, 4, 0x7F, 0),
	SOC_SINGLE("L", SPDIF_FRAMECTRL_L, 12, 0x1, 0),
	SOC_SINGLE("V", SPDIF_FRAMECTRL_L, 13, 0x1, 0),
	SOC_SINGLE("USER DATA", SPDIF_FRAMECTRL_L, 14, 0x1, 0),
	SOC_SINGLE("AUTO MUTE", SPDIF_FRAMECTRL_H, 16, 0x1, 0),
	SOC_SINGLE("V CONFIG", SPDIF_FRAMECTRL_H, 17, 0x1, 0),
};

struct spdif_srr {
	u32 rate;
	u32 basemult;
	u32 rate_factor;
};

static struct spdif_srr srr_values[] = {
	{96000, 0x2, 0x0BB80},
	{88200, 0x2, 0x0AC44},
	{64000, 0x2, 0x07D00},
	{48000, 0x1, 0x0BB80},
	{44100, 0x1, 0x0AC44},
	{32000, 0x1, 0x07D00},
};

static inline int get_srr_values(int rate)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(srr_values); i++)
		if (srr_values[i].rate == rate)
			return i;

	return -1;
}

static int mxs_codec_hw_params(struct snd_pcm_substream *substream,
			       struct snd_pcm_hw_params *params,
			       struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_device *socdev = rtd->socdev;
	struct snd_soc_codec *codec = socdev->card->codec;
	int playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0;
	int i;
	u32 srr_value = 0;
	u32 basemult;

	i = get_srr_values(params_rate(params));
	if (i < 0)
		printk(KERN_WARNING "%s doesn't support rate %d\n",
		       codec->name, params_rate(params));
	else {
		basemult = srr_values[i].basemult;

		srr_value = BF(basemult, SPDIF_SRR_BASEMULT) |
		    BF(srr_values[i].rate_factor, SPDIF_SRR_RATE);

		if (playback)
			__raw_writel(srr_value, REGS_SPDIF_BASE + HW_SPDIF_SRR);
	}

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		if (playback)
			__raw_writel(BM_SPDIF_CTRL_WORD_LENGTH,
				     REGS_SPDIF_BASE + HW_SPDIF_CTRL_SET);
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
	case SNDRV_PCM_FORMAT_S24_LE:
	case SNDRV_PCM_FORMAT_S32_LE:
		if (playback)
			__raw_writel(BM_SPDIF_CTRL_WORD_LENGTH,
				     REGS_SPDIF_BASE + HW_SPDIF_CTRL_CLR);
		break;
	default:
		printk(KERN_WARNING "%s doesn't support format %d\n",
		       codec->name, params_format(params));
	}

	return 0;
}

static void mxs_codec_spdif_enable(struct mxs_codec_priv *mxs_spdif)
{
	/* Move SPDIF codec out of reset */
	__raw_writel(BM_SPDIF_CTRL_SFTRST, REGS_SPDIF_BASE + HW_SPDIF_CTRL_CLR);

	/* Ungate SPDIF clocks */
	__raw_writel(BM_SPDIF_CTRL_CLKGATE,
		     REGS_SPDIF_BASE + HW_SPDIF_CTRL_CLR);

	/* 16 bit word length */
	__raw_writel(BM_SPDIF_CTRL_WORD_LENGTH,
		     REGS_SPDIF_BASE + HW_SPDIF_CTRL_SET);
}

static void mxs_codec_spdif_disable(struct mxs_codec_priv *mxs_spdif)
{
	/* Gate SPDIF clocks */
	__raw_writel(BM_SPDIF_CTRL_CLKGATE,
		     REGS_SPDIF_BASE + HW_SPDIF_CTRL_SET);
}

static void mxs_codec_init(struct snd_soc_codec *codec)
{
	struct mxs_codec_priv *mxs_spdif = codec->private_data;

	/* Soft reset SPDIF block */
	__raw_writel(BM_SPDIF_CTRL_SFTRST, REGS_SPDIF_BASE + HW_SPDIF_CTRL_SET);

	while (!(__raw_readl(REGS_SPDIF_BASE + HW_SPDIF_CTRL)
		 & BM_SPDIF_CTRL_CLKGATE))
		;

	mxs_codec_spdif_enable(mxs_spdif);

	snd_soc_add_controls(codec, mxs_snd_controls,
			     ARRAY_SIZE(mxs_snd_controls));
}

static void mxs_codec_exit(struct snd_soc_codec *codec)
{
	struct mxs_codec_priv *mxs_spdif = codec->private_data;

	mxs_codec_spdif_disable(mxs_spdif);
}

struct snd_soc_dai_ops mxs_spdif_codec_dai_ops = {
	.hw_params = mxs_codec_hw_params,
};

struct snd_soc_dai mxs_spdif_codec_dai = {
	.name = "mxs spdif",
	.playback = {
		     .stream_name = "Playback",
		     .channels_min = 2,
		     .channels_max = 2,
		     .rates = MXS_SPDIF_RATES,
		     .formats = MXS_SPDIF_FORMATS,
		     },
	.ops = &mxs_spdif_codec_dai_ops,
};
EXPORT_SYMBOL_GPL(mxs_spdif_codec_dai);

static struct snd_soc_codec *mxs_spdif_codec;

static int mxs_codec_probe(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec;
	int ret = 0;

	socdev->card->codec = mxs_spdif_codec;
	codec = mxs_spdif_codec;

	/* register pcms */
	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
	if (ret < 0) {
		dev_err(codec->dev, "failed to create pcms\n");
		return ret;
	}

	mxs_codec_init(codec);

	/* Register the socdev */
	ret = snd_soc_init_card(socdev);
	if (ret < 0) {
		dev_err(codec->dev, "failed to register card\n");
		snd_soc_dapm_free(socdev);
		snd_soc_free_pcms(socdev);
		return ret;
	}

	return 0;
}

static int mxs_codec_remove(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = socdev->card->codec;

	mxs_codec_exit(codec);

	snd_soc_free_pcms(socdev);
	snd_soc_dapm_free(socdev);

	return 0;
}

#ifdef CONFIG_PM
static int mxs_codec_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = socdev->card->codec;
	struct mxs_codec_priv *mxs_spdif;
	int ret = -EINVAL;

	if (codec == NULL)
		goto out;

	mxs_spdif = codec->private_data;

	mxs_codec_spdif_disable(mxs_spdif);
	clk_disable(mxs_spdif->clk);
	ret = 0;

out:
	return ret;
}

static int mxs_codec_resume(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = socdev->card->codec;
	struct mxs_codec_priv *mxs_spdif;
	int ret = -EINVAL;

	if (codec == NULL)
		goto out;

	mxs_spdif = codec->private_data;
	clk_enable(mxs_spdif->clk);

	/* Soft reset SPDIF block */
	__raw_writel(BM_SPDIF_CTRL_SFTRST, REGS_SPDIF_BASE + HW_SPDIF_CTRL_SET);
	while (!(__raw_readl(REGS_SPDIF_BASE + HW_SPDIF_CTRL)
		 & BM_SPDIF_CTRL_CLKGATE))
		;

	mxs_codec_spdif_enable(mxs_spdif);

	ret = 0;

out:
	return ret;
}
#else
#define mxs_codec_suspend	NULL
#define mxs_codec_resume	NULL
#endif /* CONFIG_PM */

struct snd_soc_codec_device soc_spdif_codec_dev_mxs = {
	.probe = mxs_codec_probe,
	.remove = mxs_codec_remove,
	.suspend = mxs_codec_suspend,
	.resume = mxs_codec_resume,
};
EXPORT_SYMBOL_GPL(soc_spdif_codec_dev_mxs);

static int __init mxs_spdif_probe(struct platform_device *pdev)
{
	struct snd_soc_codec *codec;
	struct mxs_codec_priv *mxs_spdif;
	int ret = 0;

	dev_info(&pdev->dev, "MXS SPDIF Audio Transmitter\n");

	mxs_spdif = kzalloc(sizeof(struct mxs_codec_priv), GFP_KERNEL);
	if (mxs_spdif == NULL)
		return -ENOMEM;

	codec = &mxs_spdif->codec;
	codec->name = "mxs spdif";
	codec->owner = THIS_MODULE;
	codec->private_data = mxs_spdif;
	codec->read = mxs_codec_read;
	codec->write = mxs_codec_write;
	codec->dai = &mxs_spdif_codec_dai;
	codec->num_dai = 1;

	platform_set_drvdata(pdev, mxs_spdif);
	mxs_spdif_codec = codec;

	mutex_init(&codec->mutex);
	INIT_LIST_HEAD(&codec->dapm_widgets);
	INIT_LIST_HEAD(&codec->dapm_paths);

	/* Turn on audio clock */
	mxs_spdif->clk = clk_get(&pdev->dev, "spdif");
	if (IS_ERR(mxs_spdif->clk)) {
		ret = PTR_ERR(mxs_spdif->clk);
		dev_err(&pdev->dev, "Clocks initialization failed\n");
		goto clk_err;
	}
	clk_enable(mxs_spdif->clk);

	ret = snd_soc_register_codec(codec);
	if (ret) {
		dev_err(&pdev->dev, "failed to register card\n");
		goto card_err;
	}

	ret = snd_soc_register_dai(&mxs_spdif_codec_dai);
	if (ret) {
		dev_err(&pdev->dev, "failed to register card\n");
		goto dai_err;
	}

	return 0;

dai_err:
	snd_soc_unregister_codec(codec);
card_err:
	clk_disable(mxs_spdif->clk);
	clk_put(mxs_spdif->clk);
clk_err:
	kfree(mxs_spdif);
	return ret;
}

static int __devexit mxs_spdif_remove(struct platform_device *pdev)
{
	struct mxs_codec_priv *mxs_spdif = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = &mxs_spdif->codec;

	snd_soc_unregister_codec(codec);

	clk_disable(mxs_spdif->clk);
	clk_put(mxs_spdif->clk);

	kfree(mxs_spdif);

	return 0;
}

struct platform_driver mxs_spdif_driver = {
	.driver = {
		   .name = "mxs-spdif",
		   },
	.probe = mxs_spdif_probe,
	.remove = __devexit_p(mxs_spdif_remove),
};

static int __init mxs_spdif_init(void)
{
	return platform_driver_register(&mxs_spdif_driver);
}

static void __exit mxs_spdif_exit(void)
{
	return platform_driver_unregister(&mxs_spdif_driver);
}

module_init(mxs_spdif_init);
module_exit(mxs_spdif_exit);

MODULE_DESCRIPTION("MXS SPDIF transmitter");
MODULE_AUTHOR("Vladimir Barinov");
MODULE_LICENSE("GPL");