blob: 5e96e7d02733c147483e617c5a01cd6eb205afbe [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -06002/*
3 * bytcht-da7213.c - ASoc Machine driver for Intel Baytrail and
4 * Cherrytrail-based platforms, with Dialog DA7213 codec
5 *
6 * Copyright (C) 2017 Intel Corporation
7 * Author: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -060011 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 */
13
14#include <linux/module.h>
15#include <linux/acpi.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -060018#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/soc.h>
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050021#include <sound/soc-acpi.h>
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -060022#include "../../codecs/da7213.h"
23#include "../atom/sst-atom-controls.h"
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -060024
25static const struct snd_kcontrol_new controls[] = {
26 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
27 SOC_DAPM_PIN_SWITCH("Headset Mic"),
28 SOC_DAPM_PIN_SWITCH("Mic"),
29 SOC_DAPM_PIN_SWITCH("Aux In"),
30};
31
32static const struct snd_soc_dapm_widget dapm_widgets[] = {
33 SND_SOC_DAPM_HP("Headphone Jack", NULL),
34 SND_SOC_DAPM_MIC("Headset Mic", NULL),
35 SND_SOC_DAPM_MIC("Mic", NULL),
36 SND_SOC_DAPM_LINE("Aux In", NULL),
37};
38
39static const struct snd_soc_dapm_route audio_map[] = {
40 {"Headphone Jack", NULL, "HPL"},
41 {"Headphone Jack", NULL, "HPR"},
42
43 {"AUXL", NULL, "Aux In"},
44 {"AUXR", NULL, "Aux In"},
45
46 /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
47 {"MIC1", NULL, "Headset Mic"},
48 {"MIC2", NULL, "Mic"},
49
50 /* SOC-codec link */
51 {"ssp2 Tx", NULL, "codec_out0"},
52 {"ssp2 Tx", NULL, "codec_out1"},
53 {"codec_in0", NULL, "ssp2 Rx"},
54 {"codec_in1", NULL, "ssp2 Rx"},
55
56 {"Playback", NULL, "ssp2 Tx"},
57 {"ssp2 Rx", NULL, "Capture"},
58};
59
60static int codec_fixup(struct snd_soc_pcm_runtime *rtd,
61 struct snd_pcm_hw_params *params)
62{
63 int ret;
64 struct snd_interval *rate = hw_param_interval(params,
65 SNDRV_PCM_HW_PARAM_RATE);
66 struct snd_interval *channels = hw_param_interval(params,
67 SNDRV_PCM_HW_PARAM_CHANNELS);
68
69 /* The DSP will convert the FE rate to 48k, stereo, 24bits */
70 rate->min = rate->max = 48000;
71 channels->min = channels->max = 2;
72
73 /* set SSP2 to 24-bit */
74 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
75
76 /*
77 * Default mode for SSP configuration is TDM 4 slot, override config
78 * with explicit setting to I2S 2ch 24-bit. The word length is set with
79 * dai_set_tdm_slot() since there is no other API exposed
80 */
Kuninori Morimoto0d1571c2020-03-23 14:19:05 +090081 ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -060082 SND_SOC_DAIFMT_I2S |
83 SND_SOC_DAIFMT_NB_NF |
84 SND_SOC_DAIFMT_CBS_CFS);
85 if (ret < 0) {
86 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
87 return ret;
88 }
89
Kuninori Morimoto0d1571c2020-03-23 14:19:05 +090090 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 24);
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -060091 if (ret < 0) {
92 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
93 return ret;
94 }
95
96 return 0;
97}
98
99static int aif1_startup(struct snd_pcm_substream *substream)
100{
101 return snd_pcm_hw_constraint_single(substream->runtime,
102 SNDRV_PCM_HW_PARAM_RATE, 48000);
103}
104
105static int aif1_hw_params(struct snd_pcm_substream *substream,
106 struct snd_pcm_hw_params *params)
107{
108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0d1571c2020-03-23 14:19:05 +0900109 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600110 int ret;
111
112 ret = snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK,
113 19200000, SND_SOC_CLOCK_IN);
114 if (ret < 0)
115 dev_err(codec_dai->dev, "can't set codec sysclk configuration\n");
116
117 ret = snd_soc_dai_set_pll(codec_dai, 0,
118 DA7213_SYSCLK_PLL_SRM, 0, DA7213_PLL_FREQ_OUT_98304000);
119 if (ret < 0) {
120 dev_err(codec_dai->dev, "failed to start PLL: %d\n", ret);
121 return -EIO;
122 }
123
124 return ret;
125}
126
127static int aif1_hw_free(struct snd_pcm_substream *substream)
128{
129 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0d1571c2020-03-23 14:19:05 +0900130 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600131 int ret;
132
133 ret = snd_soc_dai_set_pll(codec_dai, 0,
134 DA7213_SYSCLK_MCLK, 0, 0);
135 if (ret < 0) {
136 dev_err(codec_dai->dev, "failed to stop PLL: %d\n", ret);
137 return -EIO;
138 }
139
140 return ret;
141}
142
143static const struct snd_soc_ops aif1_ops = {
144 .startup = aif1_startup,
145};
146
147static const struct snd_soc_ops ssp2_ops = {
148 .hw_params = aif1_hw_params,
149 .hw_free = aif1_hw_free,
150
151};
152
Kuninori Morimotofb1d4862019-06-06 13:20:30 +0900153SND_SOC_DAILINK_DEF(dummy,
154 DAILINK_COMP_ARRAY(COMP_DUMMY()));
155
156SND_SOC_DAILINK_DEF(media,
157 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
158
159SND_SOC_DAILINK_DEF(deepbuffer,
160 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
161
162SND_SOC_DAILINK_DEF(ssp2_port,
163 DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
164SND_SOC_DAILINK_DEF(ssp2_codec,
165 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7213:00",
166 "da7213-hifi")));
167
168SND_SOC_DAILINK_DEF(platform,
169 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
170
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600171static struct snd_soc_dai_link dailink[] = {
172 [MERR_DPCM_AUDIO] = {
173 .name = "Audio Port",
174 .stream_name = "Audio",
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600175 .nonatomic = true,
176 .dynamic = 1,
177 .dpcm_playback = 1,
178 .dpcm_capture = 1,
179 .ops = &aif1_ops,
Kuninori Morimotofb1d4862019-06-06 13:20:30 +0900180 SND_SOC_DAILINK_REG(media, dummy, platform),
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600181 },
182 [MERR_DPCM_DEEP_BUFFER] = {
183 .name = "Deep-Buffer Audio Port",
184 .stream_name = "Deep-Buffer Audio",
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600185 .nonatomic = true,
186 .dynamic = 1,
187 .dpcm_playback = 1,
188 .ops = &aif1_ops,
Kuninori Morimotofb1d4862019-06-06 13:20:30 +0900189 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600190 },
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600191 /* CODEC<->CODEC link */
192 /* back ends */
193 {
194 .name = "SSP2-Codec",
Pierre-Louis Bossart149f7752017-10-12 18:38:04 -0500195 .id = 0,
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600196 .no_pcm = 1,
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600197 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
198 | SND_SOC_DAIFMT_CBS_CFS,
199 .be_hw_params_fixup = codec_fixup,
200 .nonatomic = true,
201 .dpcm_playback = 1,
202 .dpcm_capture = 1,
203 .ops = &ssp2_ops,
Kuninori Morimotofb1d4862019-06-06 13:20:30 +0900204 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600205 },
206};
207
208/* SoC card */
209static struct snd_soc_card bytcht_da7213_card = {
210 .name = "bytcht-da7213",
211 .owner = THIS_MODULE,
212 .dai_link = dailink,
213 .num_links = ARRAY_SIZE(dailink),
214 .controls = controls,
215 .num_controls = ARRAY_SIZE(controls),
216 .dapm_widgets = dapm_widgets,
217 .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
218 .dapm_routes = audio_map,
219 .num_dapm_routes = ARRAY_SIZE(audio_map),
220};
221
Pierre-Louis Bossart2be2d572018-01-11 13:52:09 -0600222static char codec_name[SND_ACPI_I2C_ID_LEN];
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600223
224static int bytcht_da7213_probe(struct platform_device *pdev)
225{
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600226 struct snd_soc_card *card;
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500227 struct snd_soc_acpi_mach *mach;
Pierre-Louis Bossart686338c2019-01-25 14:35:01 -0600228 const char *platform_name;
Andy Shevchenko1b55f1c2019-03-28 19:17:23 +0200229 struct acpi_device *adev;
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600230 int dai_index = 0;
Pierre-Louis Bossart22a317a2017-10-12 18:38:00 -0500231 int ret_val = 0;
232 int i;
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600233
Guennadi Liakhovetski424321962020-03-12 14:48:56 -0500234 mach = pdev->dev.platform_data;
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600235 card = &bytcht_da7213_card;
236 card->dev = &pdev->dev;
237
238 /* fix index of codec dai */
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600239 for (i = 0; i < ARRAY_SIZE(dailink); i++) {
Kuninori Morimotofb1d4862019-06-06 13:20:30 +0900240 if (!strcmp(dailink[i].codecs->name, "i2c-DLGS7213:00")) {
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600241 dai_index = i;
242 break;
243 }
244 }
245
246 /* fixup codec name based on HID */
Andy Shevchenko1b55f1c2019-03-28 19:17:23 +0200247 adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
248 if (adev) {
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600249 snprintf(codec_name, sizeof(codec_name),
Andy Shevchenko1b55f1c2019-03-28 19:17:23 +0200250 "i2c-%s", acpi_dev_name(adev));
251 put_device(&adev->dev);
Kuninori Morimotofb1d4862019-06-06 13:20:30 +0900252 dailink[dai_index].codecs->name = codec_name;
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600253 }
254
Pierre-Louis Bossart686338c2019-01-25 14:35:01 -0600255 /* override plaform name, if required */
256 platform_name = mach->mach_params.platform;
257
258 ret_val = snd_soc_fixup_dai_links_platform_name(card, platform_name);
259 if (ret_val)
260 return ret_val;
261
Pierre-Louis Bossart82cf89d2017-03-09 18:18:59 -0600262 ret_val = devm_snd_soc_register_card(&pdev->dev, card);
263 if (ret_val) {
264 dev_err(&pdev->dev,
265 "snd_soc_register_card failed %d\n", ret_val);
266 return ret_val;
267 }
268 platform_set_drvdata(pdev, card);
269 return ret_val;
270}
271
272static struct platform_driver bytcht_da7213_driver = {
273 .driver = {
274 .name = "bytcht_da7213",
275 },
276 .probe = bytcht_da7213_probe,
277};
278module_platform_driver(bytcht_da7213_driver);
279
280MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail+DA7213 Machine driver");
281MODULE_AUTHOR("Pierre-Louis Bossart");
282MODULE_LICENSE("GPL v2");
283MODULE_ALIAS("platform:bytcht_da7213");