summaryrefslogtreecommitdiffstats
path: root/sound/soc/intel/boards/ehl_rt5660.c
blob: 2e3244d5496b8c15c73103b534e2e8830b66b8ea (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
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2020 Intel Corporation

/*
 * ehl_rt5660 - ASOC Machine driver for Elkhart Lake platforms
 * with rt5660 codec
 */

#include <linux/acpi.h>
#include <sound/core.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/gfp.h>
#include <sound/jack.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-acpi.h>

#include "hda_dsp_common.h"
#include "../../codecs/rt5660.h"

#define DUAL_CHANNEL 2
#define HDMI_LINK_START 3
#define HDMI_LINE_END 6
#define NAME_SIZE	32
#define IDISP_CODEC_MASK	0x4

struct sof_card_private {
	struct list_head hdmi_pcm_list;
	bool idisp_codec;
};

static const struct snd_kcontrol_new rt5660_controls[] = {
	SOC_DAPM_PIN_SWITCH("Speaker"),
	/* There are two MICBIAS in rt5660, each for one MIC */
	SOC_DAPM_PIN_SWITCH("Headset Mic"),
	SOC_DAPM_PIN_SWITCH("Headset Mic2"),
	SOC_DAPM_PIN_SWITCH("Line Out"),
};

static const struct snd_soc_dapm_widget rt5660_widgets[] = {
	SND_SOC_DAPM_SPK("Speaker", NULL),
	SND_SOC_DAPM_MIC("Headset Mic", NULL),
	SND_SOC_DAPM_MIC("Headset Mic2", NULL),
	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
	SND_SOC_DAPM_LINE("Line Out", NULL),
};

static const struct snd_soc_dapm_route rt5660_map[] = {
	{"Speaker", NULL, "SPO"},

	{"Headset Mic", NULL, "MICBIAS1"},
	{"Headset Mic2", NULL, "MICBIAS2"},

	{"IN1P", NULL, "Headset Mic"},
	{"IN2P", NULL, "Headset Mic2"},

	{"Line Out", NULL, "LOUTL"},
	{"Line Out", NULL, "LOUTR"},

	{"DMic", NULL, "SoC DMIC"},
};

struct sof_hdmi_pcm {
	struct list_head head;
	struct snd_soc_dai *codec_dai;
	int device;
};

static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
{
	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
	struct sof_hdmi_pcm *pcm;

	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
	if (!pcm)
		return -ENOMEM;

	/* dai_link id is 1:1 mapped to the PCM device */
	pcm->device = rtd->dai_link->id;
	pcm->codec_dai = dai;

	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);

	return 0;
}

static int card_late_probe(struct snd_soc_card *card)
{
	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
	struct sof_hdmi_pcm *pcm;

	if (list_empty(&ctx->hdmi_pcm_list))
		return -ENOENT;

	if (!ctx->idisp_codec)
		return 0;

	pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);

	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
}

static int rt5660_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
	int ret;

	ret = snd_soc_dai_set_sysclk(codec_dai,
				     RT5660_SCLK_S_PLL1,
				     params_rate(params) * 512,
				     SND_SOC_CLOCK_IN);
	if (ret < 0) {
		dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
		return ret;
	}

	ret = snd_soc_dai_set_pll(codec_dai, 0,
				  RT5660_PLL1_S_BCLK,
				  params_rate(params) * 50,
				  params_rate(params) * 512);
	if (ret < 0)
		dev_err(codec_dai->dev, "can't set codec pll: %d\n", ret);

	return ret;
}

static struct snd_soc_ops rt5660_ops = {
	.hw_params = rt5660_hw_params,
};

SND_SOC_DAILINK_DEF(ssp0_pin,
	DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));

SND_SOC_DAILINK_DEF(rt5660_codec,
	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5660:00", "rt5660-aif1")));

SND_SOC_DAILINK_DEF(platform,
	DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));

SND_SOC_DAILINK_DEF(dmic_pin,
	DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
SND_SOC_DAILINK_DEF(dmic_codec,
	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
SND_SOC_DAILINK_DEF(dmic16k,
	DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin")));

SND_SOC_DAILINK_DEF(idisp1_pin,
	DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
SND_SOC_DAILINK_DEF(idisp1_codec,
	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));

SND_SOC_DAILINK_DEF(idisp2_pin,
	DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
SND_SOC_DAILINK_DEF(idisp2_codec,
	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));

SND_SOC_DAILINK_DEF(idisp3_pin,
	DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
SND_SOC_DAILINK_DEF(idisp3_codec,
	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));

SND_SOC_DAILINK_DEF(idisp4_pin,
	DAILINK_COMP_ARRAY(COMP_CPU("iDisp4 Pin")));
SND_SOC_DAILINK_DEF(idisp4_codec,
	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi4")));

static struct snd_soc_dai_link ehl_rt5660_dailink[] = {
	/* back ends */
	{
		.name = "SSP0-Codec",
		.id = 0,
		.no_pcm = 1,
		.dpcm_playback = 1,
		.dpcm_capture = 1,
		.ops = &rt5660_ops,
		.nonatomic = true,
		SND_SOC_DAILINK_REG(ssp0_pin, rt5660_codec, platform),
	},
	{
		.name = "dmic48k",
		.id = 1,
		.ignore_suspend = 1,
		.dpcm_capture = 1,
		.no_pcm = 1,
		SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
	},
	{
		.name = "dmic16k",
		.id = 2,
		.ignore_suspend = 1,
		.dpcm_capture = 1,
		.no_pcm = 1,
		SND_SOC_DAILINK_REG(dmic16k, dmic_codec, platform),
	},
	{
		.name = "iDisp1",
		.id = 5,
		.init = hdmi_init,
		.dpcm_playback = 1,
		.no_pcm = 1,
		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
	},
	{
		.name = "iDisp2",
		.id = 6,
		.init = hdmi_init,
		.dpcm_playback = 1,
		.no_pcm = 1,
		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
	},
	{
		.name = "iDisp3",
		.id = 7,
		.init = hdmi_init,
		.dpcm_playback = 1,
		.no_pcm = 1,
		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
	},
	{
		.name = "iDisp4",
		.id = 8,
		.init = hdmi_init,
		.dpcm_playback = 1,
		.no_pcm = 1,
		SND_SOC_DAILINK_REG(idisp4_pin, idisp4_codec, platform),
	},
};

/* SoC card */
static struct snd_soc_card snd_soc_card_ehl_rt5660 = {
	.name = "ehl-rt5660",
	.owner = THIS_MODULE,
	.dai_link = ehl_rt5660_dailink,
	.num_links = ARRAY_SIZE(ehl_rt5660_dailink),
	.dapm_widgets = rt5660_widgets,
	.num_dapm_widgets = ARRAY_SIZE(rt5660_widgets),
	.dapm_routes = rt5660_map,
	.num_dapm_routes = ARRAY_SIZE(rt5660_map),
	.controls = rt5660_controls,
	.num_controls = ARRAY_SIZE(rt5660_controls),
	.fully_routed = true,
	.late_probe = card_late_probe,
};

/* If hdmi codec is not supported, switch to use dummy codec */
static void hdmi_link_init(struct snd_soc_card *card,
			   struct sof_card_private *ctx,
			   struct snd_soc_acpi_mach *mach)
{
	struct snd_soc_dai_link *link;
	int i;

	if (mach->mach_params.common_hdmi_codec_drv &&
	    (mach->mach_params.codec_mask & IDISP_CODEC_MASK)) {
		ctx->idisp_codec = true;
		return;
	}

	/*
	 * if HDMI is not enabled in kernel config, or
	 * hdmi codec is not supported
	 */
	for (i = HDMI_LINK_START; i <= HDMI_LINE_END; i++) {
		link = &card->dai_link[i];
		link->codecs[0].name = "snd-soc-dummy";
		link->codecs[0].dai_name = "snd-soc-dummy-dai";
	}
}

static int snd_ehl_rt5660_probe(struct platform_device *pdev)
{
	struct snd_soc_acpi_mach *mach;
	struct snd_soc_card *card = &snd_soc_card_ehl_rt5660;
	struct sof_card_private *ctx;
	int ret;

	card->dev = &pdev->dev;

	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;
	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
	snd_soc_card_set_drvdata(card, ctx);

	mach = pdev->dev.platform_data;
	ret = snd_soc_fixup_dai_links_platform_name(card,
						    mach->mach_params.platform);
	if (ret)
		return ret;

	hdmi_link_init(card, ctx, mach);

	return devm_snd_soc_register_card(&pdev->dev, card);
}

static const struct platform_device_id ehl_board_ids[] = {
	{ .name = "ehl_rt5660" },
	{ }
};

static struct platform_driver snd_ehl_rt5660_driver = {
	.driver = {
		.name = "ehl_rt5660",
		.pm = &snd_soc_pm_ops,
	},
	.probe = snd_ehl_rt5660_probe,
	.id_table = ehl_board_ids,
};

module_platform_driver(snd_ehl_rt5660_driver);

MODULE_DESCRIPTION("ASoC Intel(R) Elkhartlake + rt5660 Machine driver");
MODULE_AUTHOR("libin.yang@intel.com");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:ehl_rt5660");