summaryrefslogtreecommitdiff
path: root/sound/soc/fsl/imx-amix.c
blob: d22ff25251f54698aef9fc5a75a5ec76ebfd9c29 (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
/*
 * Copyright 2017 NXP
 *
 * 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_platform.h>
#include <linux/clk.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <linux/pm_runtime.h>
#include "fsl_sai.h"
#include "fsl_amix.h"

struct imx_amix {
	struct platform_device *pdev;
	struct snd_soc_card card;
	struct platform_device *amix_pdev;
	struct platform_device *out_pdev;
	struct clk *cpu_mclk;
	int num_dai;
	struct snd_soc_dai_link *dai;
	int num_dai_conf;
	struct snd_soc_codec_conf *dai_conf;
	int num_dapm_routes;
	struct snd_soc_dapm_route *dapm_routes;
};

static const u32 imx_amix_rates[] = {
	8000, 12000, 16000, 24000, 32000, 48000, 64000, 96000,
};

static const struct snd_pcm_hw_constraint_list imx_amix_rate_constraints = {
	.count = ARRAY_SIZE(imx_amix_rates),
	.list = imx_amix_rates,
};

static int imx_amix_fe_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct imx_amix *priv = snd_soc_card_get_drvdata(rtd->card);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct device *dev = rtd->card->dev;
	unsigned long clk_rate = clk_get_rate(priv->cpu_mclk);
	int ret;

	if (clk_rate % 24576000 == 0) {
		ret = snd_pcm_hw_constraint_list(runtime, 0,
			SNDRV_PCM_HW_PARAM_RATE, &imx_amix_rate_constraints);
		if (ret < 0)
			return ret;
	} else
		dev_warn(dev, "mclk may be not supported %lu\n", clk_rate);

	ret = snd_pcm_hw_constraint_minmax(runtime,
			SNDRV_PCM_HW_PARAM_CHANNELS, 1, 8);
	if (ret < 0)
		return ret;

	return snd_pcm_hw_constraint_mask64(runtime,
			SNDRV_PCM_HW_PARAM_FORMAT, FSL_AMIX_FORMATS);
}

static int imx_amix_fe_hw_params(struct snd_pcm_substream *substream,
				     struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct device *dev = rtd->card->dev;
	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
	unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
	u32 channels = params_channels(params);
	int ret, dir;

	/* For playback the AMIX is slave, and for record is master */
	fmt |= tx ? SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
	dir  = tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN;

	/* set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
	if (ret) {
		dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
		return ret;
	}

	/* Specific configurations of DAIs starts from here */
	ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, FSL_SAI_CLK_MAST1, 0, dir);
	if (ret) {
		dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
		return ret;
	}

	/*
	 * Per datasheet, AMIX expects 8 slots and 32 bits
	 * for every slot in TDM mode.
	 */
	ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, BIT(channels) - 1,
				 BIT(channels) - 1, 8, 32);
	if (ret)
		dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);

	return ret;
}

static int imx_amix_be_hw_params(struct snd_pcm_substream *substream,
				     struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct device *dev = rtd->card->dev;
	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
	unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF;
	int ret;

	if (!tx)
		return 0;

	/* For playback the AMIX is slave, and for record is master */
	fmt |= tx ? SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBS_CFS;

	/* set AMIX DAI configuration */
	ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
	if (ret)
		dev_err(dev, "failed to set AMIX DAI fmt: %d\n", ret);

	return ret;
}

static struct snd_soc_ops imx_amix_fe_ops = {
	.startup = imx_amix_fe_startup,
	.hw_params = imx_amix_fe_hw_params,
};

static struct snd_soc_ops imx_amix_be_ops = {
	.hw_params = imx_amix_be_hw_params,
};

static int imx_amix_probe(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct device_node *amix_np = NULL, *out_cpu_np = NULL;
	struct platform_device *amix_pdev = NULL;
	struct platform_device *cpu_pdev;
	struct of_phandle_args args;
	struct imx_amix *priv;
	int i, num_dai, ret;
	const char *fe_name_pref = "HiFi-AMIX-FE-";
	char *be_name, *be_pb, *be_cp, *dai_name, *capture_dai_name;

	num_dai = of_count_phandle_with_args(np, "dais", NULL);
	if (num_dai != FSL_AMIX_MAX_DAIS) {
		dev_err(&pdev->dev, "Need 2 dais to be provided for %s\n",
			np->full_name);
		return -EINVAL;
	}

	amix_np = of_parse_phandle(np, "amix-controller", 0);
	if (!amix_np) {
		dev_err(&pdev->dev, "Missing amix-controller phandle at %s\n",
			np->full_name);
		return -EINVAL;
	}

	amix_pdev = of_find_device_by_node(amix_np);
	if (!amix_pdev) {
		dev_err(&pdev->dev, "Missing AMIX platform device for %s\n",
			np->full_name);
		return -EINVAL;
	}

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->num_dai = 2 * num_dai;
	priv->dai = devm_kzalloc(&pdev->dev,
			priv->num_dai * sizeof(struct snd_soc_dai_link),
			GFP_KERNEL);
	if (!priv->dai)
		return -ENOMEM;

	priv->num_dai_conf = num_dai;
	priv->dai_conf = devm_kzalloc(&pdev->dev,
			priv->num_dai_conf * sizeof(struct snd_soc_codec_conf),
			GFP_KERNEL);
	if (!priv->dai_conf)
		return -ENOMEM;

	priv->num_dapm_routes = 3 * num_dai;
	priv->dapm_routes = devm_kzalloc(&pdev->dev,
			priv->num_dapm_routes * sizeof(struct snd_soc_dapm_route),
			GFP_KERNEL);
	if (!priv->dapm_routes)
		return -ENOMEM;

	for (i = 0; i < num_dai; i++) {
		ret = of_parse_phandle_with_args(np, "dais", NULL, i, &args);
		if (ret < 0) {
			dev_err(&pdev->dev,
				"of_parse_phandle_with_args failed (%d)\n", ret);
			return ret;
		}

		cpu_pdev = of_find_device_by_node(args.np);
		if (!cpu_pdev) {
			dev_err(&pdev->dev, "failed to find SAI platform device\n");
			return -EINVAL;
		}

		dai_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%s",
				fe_name_pref, args.np->full_name + 1);

		dev_info(&pdev->dev, "DAI FE name:%s\n", dai_name);

		if (i == 0) {
			out_cpu_np = args.np;
			capture_dai_name =
				devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s",
					dai_name, "CPU-Capture");
		}

		priv->dai[i].name = dai_name;
		priv->dai[i].stream_name = "HiFi-AMIX-FE";
		priv->dai[i].codec_dai_name = "snd-soc-dummy-dai";
		priv->dai[i].codec_name = "snd-soc-dummy";
		priv->dai[i].cpu_of_node = args.np;
		priv->dai[i].cpu_dai_name = dev_name(&cpu_pdev->dev);
		priv->dai[i].platform_of_node = args.np;
		priv->dai[i].dynamic = 1;
		priv->dai[i].dpcm_playback = 1;
		priv->dai[i].dpcm_capture = (i == 0 ? 1 : 0);
		priv->dai[i].ignore_pmdown_time = 1;
		priv->dai[i].ops = &imx_amix_fe_ops;

		/* Add AMIX Backend */
		be_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "amix-%d", i);
		be_pb = devm_kasprintf(&pdev->dev, GFP_KERNEL, "AMIX-Playback-%d", i);
		be_cp = devm_kasprintf(&pdev->dev, GFP_KERNEL, "AMIX-Capture-%d", i);

		priv->dai[num_dai+i].name = be_name;
		priv->dai[num_dai+i].codec_dai_name = "snd-soc-dummy-dai";
		priv->dai[num_dai+i].codec_name = "snd-soc-dummy";
		priv->dai[num_dai+i].cpu_of_node = amix_np;
		priv->dai[num_dai+i].cpu_dai_name = be_name;
		priv->dai[num_dai+i].platform_name = "snd-soc-dummy";
		priv->dai[num_dai+i].no_pcm = 1;
		priv->dai[num_dai+i].dpcm_playback = 1;
		priv->dai[num_dai+i].dpcm_capture  = 1;
		priv->dai[num_dai+i].ignore_pmdown_time = 1;
		priv->dai[num_dai+i].ops = &imx_amix_be_ops;

		priv->dai_conf[i].of_node = args.np;
		priv->dai_conf[i].name_prefix = dai_name;

		priv->dapm_routes[i].source = devm_kasprintf(&pdev->dev,
			GFP_KERNEL, "%s %s", dai_name, "CPU-Playback");
		priv->dapm_routes[i].sink = be_pb;
		priv->dapm_routes[num_dai+i].source   = be_pb;
		priv->dapm_routes[num_dai+i].sink     = be_cp;
		priv->dapm_routes[2*num_dai+i].source = be_cp;
		priv->dapm_routes[2*num_dai+i].sink   = capture_dai_name;
	}

	cpu_pdev = of_find_device_by_node(out_cpu_np);
	if (!cpu_pdev) {
		dev_err(&pdev->dev, "failed to find SAI platform device\n");
		return -EINVAL;
	}
	priv->cpu_mclk = devm_clk_get(&cpu_pdev->dev, "mclk1");
	if (IS_ERR(priv->cpu_mclk)) {
		ret = PTR_ERR(priv->cpu_mclk);
		dev_err(&cpu_pdev->dev, "failed to get DAI mclk1: %d\n", ret);
		return -EINVAL;
	}

	priv->amix_pdev = amix_pdev;
	priv->out_pdev  = cpu_pdev;

	priv->card.dai_link = priv->dai;
	priv->card.num_links = priv->num_dai;
	priv->card.codec_conf = priv->dai_conf;
	priv->card.num_configs = priv->num_dai_conf;
	priv->card.dapm_routes = priv->dapm_routes;
	priv->card.num_dapm_routes = priv->num_dapm_routes;
	priv->card.dev = &pdev->dev;
	priv->card.owner = THIS_MODULE;

	ret = snd_soc_of_parse_card_name(&priv->card, "model");
	if (ret) {
		dev_err(&pdev->dev, "snd_soc_of_parse_card_name failed (%d)\n", ret);
		return ret;
	}

	platform_set_drvdata(pdev, &priv->card);
	snd_soc_card_set_drvdata(&priv->card, priv);

	ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
	if (ret) {
		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
		return ret;
	}

	return ret;
}

static const struct of_device_id imx_amix_dt_ids[] = {
	{ .compatible = "fsl,imx-audio-amix", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_amix_dt_ids);

static struct platform_driver imx_amix_driver = {
	.probe = imx_amix_probe,
	.driver = {
		.name = "imx-amix",
		.of_match_table = imx_amix_dt_ids,
		.pm = &snd_soc_pm_ops,
	},
};
module_platform_driver(imx_amix_driver);

MODULE_DESCRIPTION("NXP AMIX ASoC machine driver");
MODULE_AUTHOR("Viorel Suman <viorel.suman@nxp.com>");
MODULE_ALIAS("platform:imx-amix");
MODULE_LICENSE("GPL v2");