summaryrefslogtreecommitdiff
path: root/sound/soc/fsl/fsl_asrc.c
blob: dc230ffee9dd8813f56c562fbc6ce4e6b43b126b (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
 * Copyright (C) 2010-2013 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_i2c.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/mxc_asrc.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/dmaengine_pcm.h>

#include "fsl_asrc.h"
#include "imx-pcm.h"

static bool filter(struct dma_chan *chan, void *param)
{
	if (!imx_dma_is_general_purpose(chan))
		return false;

	chan->private = param;

	return true;
}

static int asrc_p2p_request_channel(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai     = rtd->cpu_dai;
	struct fsl_asrc_p2p *asrc_p2p   = snd_soc_dai_get_drvdata(cpu_dai);
	enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
	struct snd_dmaengine_dai_dma_data *dma_params_be = NULL;
	struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL;
	struct imx_dma_data *fe_filter_data = NULL;
	struct imx_dma_data *be_filter_data = NULL;

	struct dma_slave_config slave_config;
	dma_cap_mask_t mask;
	struct dma_chan *chan;
	int ret;
	struct snd_soc_dpcm *dpcm;

	/* find the be for this fe stream */
	list_for_each_entry(dpcm, &rtd->dpcm[substream->stream].be_clients, list_be) {
		if (dpcm->fe == rtd) {
			struct snd_soc_pcm_runtime *be = dpcm->be;
			struct snd_soc_dai *dai = be->cpu_dai;
			struct snd_pcm_substream *be_substream;
			be_substream = snd_soc_dpcm_get_substream(be, substream->stream);
			dma_params_be = snd_soc_dai_get_dma_data(dai, be_substream);
			break;
		}
	}

	if (!dma_params_be) {
		dev_err(rtd->card->dev, "can not get be substream\n");
		return -EINVAL;
	}

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		dma_params_fe = &asrc_p2p->dma_params_tx;
	else
		dma_params_fe = &asrc_p2p->dma_params_rx;

	fe_filter_data = dma_params_fe->filter_data;
	be_filter_data = dma_params_be->filter_data;

	if (asrc_p2p->output_width == 16)
		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
	else
		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;

	/* reconfig memory to FIFO dma request */
	dma_params_fe->addr = asrc_p2p->asrc_ops.asrc_p2p_per_addr(
						asrc_p2p->asrc_index, 1);
	fe_filter_data->dma_request0 = asrc_p2p->dmarx[asrc_p2p->asrc_index];
	dma_params_fe->maxburst = dma_params_be->maxburst;

	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);
	dma_cap_set(DMA_CYCLIC, mask);

	/* config p2p dma channel */
	asrc_p2p->asrc_p2p_dma_data.peripheral_type = IMX_DMATYPE_ASRC;
	asrc_p2p->asrc_p2p_dma_data.priority        = DMA_PRIO_HIGH;
	asrc_p2p->asrc_p2p_dma_data.dma_request1    = asrc_p2p->dmatx[asrc_p2p->asrc_index];
	/* need to get target device's dma dma_addr, burstsize */
	asrc_p2p->asrc_p2p_dma_data.dma_request0    = be_filter_data->dma_request0;

	/* Request channel */
	asrc_p2p->asrc_p2p_dma_chan =
		dma_request_channel(mask, filter, &asrc_p2p->asrc_p2p_dma_data);

	if (!asrc_p2p->asrc_p2p_dma_chan) {
		dev_err(rtd->card->dev, "can not request dma channel\n");
		goto error;
	}
	chan = asrc_p2p->asrc_p2p_dma_chan;

	/*
	 * Buswidth is not used in the sdma for p2p. Here we set the maxburst fix to
	 * twice of dma_params's burstsize.
	 */
	slave_config.direction      = DMA_DEV_TO_DEV;
	slave_config.src_addr       = asrc_p2p->asrc_ops.asrc_p2p_per_addr(asrc_p2p->asrc_index, 0);
	slave_config.src_addr_width = buswidth;
	slave_config.src_maxburst   = dma_params_be->maxburst * 2;
	slave_config.dst_addr       = dma_params_be->addr;
	slave_config.dst_addr_width = buswidth;
	slave_config.dst_maxburst   = dma_params_be->maxburst * 2;
	slave_config.dma_request0   = be_filter_data->dma_request0;
	slave_config.dma_request1   = asrc_p2p->dmatx[asrc_p2p->asrc_index];

	ret = dmaengine_slave_config(asrc_p2p->asrc_p2p_dma_chan,
							&slave_config);
	if (ret) {
		dev_err(rtd->card->dev, "can not config dma channel\n");
		goto error;
	}
	asrc_p2p->asrc_p2p_desc = dmaengine_prep_dma_cyclic(chan, 0xffff, 64,
							64, DMA_DEV_TO_DEV, 0);
	if (!asrc_p2p->asrc_p2p_desc) {
		dev_err(&chan->dev->device,
				"cannot prepare slave dma\n");
		goto error;
	}

	return 0;
error:
	if (asrc_p2p->asrc_p2p_dma_chan) {
		dma_release_channel(asrc_p2p->asrc_p2p_dma_chan);
		asrc_p2p->asrc_p2p_dma_chan = NULL;
	}

	return -EINVAL;
}

static int config_asrc(struct snd_pcm_substream *substream,
					 struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai     = rtd->cpu_dai;
	struct fsl_asrc_p2p *asrc_p2p   = snd_soc_dai_get_drvdata(cpu_dai);
	unsigned int rate    = params_rate(params);
	unsigned int channel = params_channels(params);
	struct asrc_config config = {0};
	int output_word_width = 0;
	int input_word_width = 0;
	int ret = 0;
	if ((channel != 2) && (channel != 4) && (channel != 6)) {
		dev_err(cpu_dai->dev, "param channel is not correct\n");
		return -EINVAL;
	}

	ret = asrc_p2p->asrc_ops.asrc_p2p_req_pair(channel, &asrc_p2p->asrc_index);
	if (ret < 0) {
		dev_err(cpu_dai->dev, "Fail to request asrc pair\n");
		return -EINVAL;
	}

	if (asrc_p2p->output_width == 16)
		output_word_width = ASRC_WIDTH_16_BIT;
	else
		output_word_width = ASRC_WIDTH_24_BIT;

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_U16:
	case SNDRV_PCM_FORMAT_S16_LE:
	case SNDRV_PCM_FORMAT_S16_BE:
		input_word_width = ASRC_WIDTH_16_BIT;
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
	case SNDRV_PCM_FORMAT_S20_3BE:
	case SNDRV_PCM_FORMAT_S24_3LE:
	case SNDRV_PCM_FORMAT_S24_3BE:
	case SNDRV_PCM_FORMAT_S24_BE:
	case SNDRV_PCM_FORMAT_S24_LE:
	case SNDRV_PCM_FORMAT_U24_BE:
	case SNDRV_PCM_FORMAT_U24_LE:
	case SNDRV_PCM_FORMAT_U24_3BE:
	case SNDRV_PCM_FORMAT_U24_3LE:
		input_word_width =  ASRC_WIDTH_24_BIT;
		break;
	case SNDRV_PCM_FORMAT_S8:
	case SNDRV_PCM_FORMAT_U8:
	case SNDRV_PCM_FORMAT_S32:
	case SNDRV_PCM_FORMAT_U32:
	default:
		dev_err(cpu_dai->dev, "Format is not support!\n");
		return -EINVAL;
	}

	config.input_word_width   = input_word_width;
	config.output_word_width  = output_word_width;
	config.pair               = asrc_p2p->asrc_index;
	config.channel_num        = channel;
	config.input_sample_rate  = rate;
	config.output_sample_rate = asrc_p2p->output_rate;
	config.inclk              = INCLK_NONE;

	switch (asrc_p2p->per_dev) {
	case SSI1:
		config.outclk    = OUTCLK_SSI1_TX;
		break;
	case SSI2:
		config.outclk    = OUTCLK_SSI2_TX;
		break;
	case SSI3:
		config.outclk    = OUTCLK_SSI3_TX;
		break;
	case ESAI:
		config.outclk    = OUTCLK_ESAI_TX;
		break;
	default:
		dev_err(cpu_dai->dev, "peripheral device is not correct\n");
		return -EINVAL;
	}

	ret = asrc_p2p->asrc_ops.asrc_p2p_config_pair(&config);
	if (ret < 0) {
		dev_err(cpu_dai->dev, "Fail to config asrc\n");
		return ret;
	}

	return 0;
}

static int fsl_asrc_p2p_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params,
				struct snd_soc_dai *cpu_dai)
{
	int ret = 0;

	ret = config_asrc(substream, params);
	if (ret < 0)
		return ret;

	return asrc_p2p_request_channel(substream);
}

static int fsl_asrc_p2p_hw_free(struct snd_pcm_substream *substream,
			      struct snd_soc_dai *cpu_dai)
{
	struct fsl_asrc_p2p *asrc_p2p = snd_soc_dai_get_drvdata(cpu_dai);

	if (asrc_p2p->asrc_p2p_dma_chan) {
		/* Release p2p dma resource */
		dma_release_channel(asrc_p2p->asrc_p2p_dma_chan);
		asrc_p2p->asrc_p2p_dma_chan = NULL;
	}

	if (asrc_p2p->asrc_index != -1) {
		asrc_p2p->asrc_ops.asrc_p2p_release_pair(asrc_p2p->asrc_index);
		asrc_p2p->asrc_ops.asrc_p2p_finish_conv(asrc_p2p->asrc_index);
	}
	asrc_p2p->asrc_index = -1;

	return 0;
}

static int fsl_asrc_p2p_trigger(struct snd_pcm_substream *substream, int cmd,
			    struct snd_soc_dai *cpu_dai)
{
	struct fsl_asrc_p2p *asrc_p2p = snd_soc_dai_get_drvdata(cpu_dai);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		dmaengine_submit(asrc_p2p->asrc_p2p_desc);
		dma_async_issue_pending(asrc_p2p->asrc_p2p_dma_chan);
		asrc_p2p->asrc_ops.asrc_p2p_start_conv(asrc_p2p->asrc_index);
		break;
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_STOP:
		dmaengine_terminate_all(asrc_p2p->asrc_p2p_dma_chan);
		asrc_p2p->asrc_ops.asrc_p2p_stop_conv(asrc_p2p->asrc_index);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

#define IMX_ASRC_RATES  SNDRV_PCM_RATE_8000_192000

#define IMX_ASRC_FORMATS \
	(SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE | \
	SNDRV_PCM_FORMAT_S20_3LE)

static struct snd_soc_dai_ops fsl_asrc_p2p_dai_ops = {
	.trigger      = fsl_asrc_p2p_trigger,
	.hw_params    = fsl_asrc_p2p_hw_params,
	.hw_free      = fsl_asrc_p2p_hw_free,
};

static int fsl_asrc_p2p_dai_probe(struct snd_soc_dai *dai)
{
	struct fsl_asrc_p2p *asrc_p2p = snd_soc_dai_get_drvdata(dai);

	dai->playback_dma_data = &asrc_p2p->dma_params_tx;
	dai->capture_dma_data = &asrc_p2p->dma_params_rx;

	return 0;
}

static struct snd_soc_dai_driver fsl_asrc_p2p_dai = {
	.probe = fsl_asrc_p2p_dai_probe,
	.playback = {
		.stream_name = "asrc-Playback",
		.channels_min = 1,
		.channels_max = 10,
		.rates = IMX_ASRC_RATES,
		.formats = IMX_ASRC_FORMATS,
	},
	.capture  = {
		.stream_name = "asrc-Capture",
		.channels_min = 1,
		.channels_max = 4,
		.rates = IMX_ASRC_RATES,
		.formats = IMX_ASRC_FORMATS,
	},
	.ops      = &fsl_asrc_p2p_dai_ops,
};

static const struct snd_soc_component_driver fsl_asrc_p2p_component = {
	.name		= "fsl-asrc-p2p",
};

/*
 * This function will register the snd_soc_pcm_link drivers.
 */
static int fsl_asrc_p2p_probe(struct platform_device *pdev)
{
	struct fsl_asrc_p2p *asrc_p2p;
	struct device_node *np = pdev->dev.of_node;
	const char *p;
	const uint32_t *iprop_rate, *iprop_width;
	int ret = 0;

	if (!of_device_is_available(np)) {
		dev_err(&pdev->dev, "There is no device node\n");
		return -ENODEV;
	}

	asrc_p2p = devm_kzalloc(&pdev->dev, sizeof(struct fsl_asrc_p2p), GFP_KERNEL);
	if (!asrc_p2p) {
		dev_err(&pdev->dev, "can not alloc memory\n");
		return -ENOMEM;
	}
	asrc_p2p->asrc_ops.asrc_p2p_start_conv      = asrc_start_conv;
	asrc_p2p->asrc_ops.asrc_p2p_stop_conv       = asrc_stop_conv;
	asrc_p2p->asrc_ops.asrc_p2p_per_addr        = asrc_get_per_addr;
	asrc_p2p->asrc_ops.asrc_p2p_req_pair        = asrc_req_pair;
	asrc_p2p->asrc_ops.asrc_p2p_config_pair     = asrc_config_pair;
	asrc_p2p->asrc_ops.asrc_p2p_release_pair    = asrc_release_pair;
	asrc_p2p->asrc_ops.asrc_p2p_finish_conv     = asrc_finish_conv;

	asrc_p2p->asrc_index = -1;

	iprop_rate = of_get_property(np, "fsl,output-rate", NULL);
	if (iprop_rate)
		asrc_p2p->output_rate = be32_to_cpup(iprop_rate);
	else {
		dev_err(&pdev->dev, "There is no output-rate in dts\n");
		return -EINVAL;
	}
	iprop_width = of_get_property(np, "fsl,output-width", NULL);
	if (iprop_width)
		asrc_p2p->output_width = be32_to_cpup(iprop_width);

	if (asrc_p2p->output_width != 16 && asrc_p2p->output_width != 24) {
		dev_err(&pdev->dev, "output_width is not acceptable\n");
		return -EINVAL;
	}

	ret = of_property_read_u32_array(np,
			"fsl,asrc-dma-tx-events", asrc_p2p->dmatx, 3);
	if (ret) {
		dev_err(&pdev->dev, "Failed to get fsl,asrc-dma-tx-events.\n");
		return -EINVAL;
	}

	ret = of_property_read_u32_array(np,
			"fsl,asrc-dma-rx-events", asrc_p2p->dmarx, 3);
	if (ret) {
		dev_err(&pdev->dev, "Failed to get fsl,asrc-dma-rx-events.\n");
		return -EINVAL;
	}

	asrc_p2p->filter_data_tx.peripheral_type = IMX_DMATYPE_ASRC;
	asrc_p2p->filter_data_rx.peripheral_type = IMX_DMATYPE_ASRC;

	asrc_p2p->dma_params_tx.filter_data = &asrc_p2p->filter_data_tx;
	asrc_p2p->dma_params_rx.filter_data = &asrc_p2p->filter_data_rx;

	platform_set_drvdata(pdev, asrc_p2p);

	p = strrchr(np->full_name, '/') + 1;
	strcpy(asrc_p2p->name, p);
	fsl_asrc_p2p_dai.name = asrc_p2p->name;

	ret = snd_soc_register_component(&pdev->dev, &fsl_asrc_p2p_component,
					 &fsl_asrc_p2p_dai, 1);
	if (ret) {
		dev_err(&pdev->dev, "register DAI failed\n");
		goto failed_register;
	}

	asrc_p2p->soc_platform_pdev = platform_device_register_simple(
					"imx-pcm-asrc", -1, NULL, 0);
	if (IS_ERR(asrc_p2p->soc_platform_pdev)) {
		ret = PTR_ERR(asrc_p2p->soc_platform_pdev);
		goto failed_pdev_alloc;
	}

	ret = imx_pcm_dma_init(asrc_p2p->soc_platform_pdev, SND_DMAENGINE_PCM_FLAG_NO_RESIDUE |
				     SND_DMAENGINE_PCM_FLAG_NO_DT |
				     SND_DMAENGINE_PCM_FLAG_COMPAT);
	if (ret) {
		dev_err(&pdev->dev, "init pcm dma failed\n");
		goto failed_pcm_init;
	}

	return 0;

failed_pcm_init:
	platform_device_unregister(asrc_p2p->soc_platform_pdev);
failed_pdev_alloc:
	snd_soc_unregister_component(&pdev->dev);
failed_register:

	return ret;
}

static int fsl_asrc_p2p_remove(struct platform_device *pdev)
{
	struct fsl_asrc_p2p *asrc_p2p = platform_get_drvdata(pdev);

	imx_pcm_dma_exit(asrc_p2p->soc_platform_pdev);
	platform_device_unregister(asrc_p2p->soc_platform_pdev);
	snd_soc_unregister_component(&pdev->dev);

	return 0;
}

static const struct of_device_id fsl_asrc_p2p_dt_ids[] = {
	{ .compatible = "fsl,imx6q-asrc-p2p", },
	{ /* sentinel */ }
};

static struct platform_driver fsl_asrc_p2p_driver = {
	.probe = fsl_asrc_p2p_probe,
	.remove = fsl_asrc_p2p_remove,
	.driver = {
		.name = "fsl-asrc-p2p",
		.owner = THIS_MODULE,
		.of_match_table = fsl_asrc_p2p_dt_ids,
	},
};
module_platform_driver(fsl_asrc_p2p_driver);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("i.MX ASoC ASRC P2P driver");
MODULE_ALIAS("platform:fsl-asrc-p2p");
MODULE_LICENSE("GPL");