blob: 0f2385f6f6ac5423e4820bacbd0cdb4c3c9e8c33 [file] [log] [blame]
Subhransu S. Prusty996cc842014-11-19 15:13:27 +05301/*
2 * byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
3 *
4 * Copyright (C) 2014 Intel Corp
5 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060023#include <linux/acpi.h>
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053024#include <linux/device.h>
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060025#include <linux/dmi.h>
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053026#include <linux/slab.h>
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053027#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/soc.h>
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060030#include <sound/jack.h>
Jie Yange56c72d2015-04-02 15:37:02 +080031#include "../../codecs/rt5640.h"
Jie Yangb97169d2015-04-02 15:37:04 +080032#include "../atom/sst-atom-controls.h"
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053033
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060034static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053035 SND_SOC_DAPM_HP("Headphone", NULL),
36 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -060037 SND_SOC_DAPM_MIC("Internal Mic", NULL),
38 SND_SOC_DAPM_SPK("Speaker", NULL),
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053039};
40
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060041static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -060042 {"AIF1 Playback", NULL, "ssp2 Tx"},
43 {"ssp2 Tx", NULL, "codec_out0"},
44 {"ssp2 Tx", NULL, "codec_out1"},
45 {"codec_in0", NULL, "ssp2 Rx"},
46 {"codec_in1", NULL, "ssp2 Rx"},
47 {"ssp2 Rx", NULL, "AIF1 Capture"},
48
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053049 {"Headset Mic", NULL, "MICBIAS1"},
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -060050 {"IN2P", NULL, "Headset Mic"},
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053051 {"Headphone", NULL, "HPOL"},
52 {"Headphone", NULL, "HPOR"},
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -060053 {"Speaker", NULL, "SPOLP"},
54 {"Speaker", NULL, "SPOLN"},
55 {"Speaker", NULL, "SPORP"},
56 {"Speaker", NULL, "SPORN"},
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053057};
58
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060059static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
60 {"DMIC1", NULL, "Internal Mic"},
61};
62
63static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
64 {"DMIC2", NULL, "Internal Mic"},
65};
66
67static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
68 {"Internal Mic", NULL, "MICBIAS1"},
69 {"IN1P", NULL, "Internal Mic"},
70};
71
72enum {
73 BYT_RT5640_DMIC1_MAP,
74 BYT_RT5640_DMIC2_MAP,
75 BYT_RT5640_IN1_MAP,
76};
77
78#define BYT_RT5640_MAP(quirk) ((quirk) & 0xff)
79#define BYT_RT5640_DMIC_EN BIT(16)
80
81static unsigned long byt_rt5640_quirk = BYT_RT5640_DMIC1_MAP |
82 BYT_RT5640_DMIC_EN;
83
84static const struct snd_kcontrol_new byt_rt5640_controls[] = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053085 SOC_DAPM_PIN_SWITCH("Headphone"),
86 SOC_DAPM_PIN_SWITCH("Headset Mic"),
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -060087 SOC_DAPM_PIN_SWITCH("Internal Mic"),
88 SOC_DAPM_PIN_SWITCH("Speaker"),
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053089};
90
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060091static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053092 struct snd_pcm_hw_params *params)
93{
94 struct snd_soc_pcm_runtime *rtd = substream->private_data;
95 struct snd_soc_dai *codec_dai = rtd->codec_dai;
96 int ret;
97
98 snd_soc_dai_set_bclk_ratio(codec_dai, 50);
99
100 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
101 params_rate(params) * 512,
102 SND_SOC_CLOCK_IN);
103 if (ret < 0) {
104 dev_err(rtd->dev, "can't set codec clock %d\n", ret);
105 return ret;
106 }
107
108 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_BCLK1,
109 params_rate(params) * 50,
110 params_rate(params) * 512);
111 if (ret < 0) {
112 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
113 return ret;
114 }
115
116 return 0;
117}
118
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600119static int byt_rt5640_quirk_cb(const struct dmi_system_id *id)
120{
121 byt_rt5640_quirk = (unsigned long)id->driver_data;
122 return 1;
123}
124
125static const struct dmi_system_id byt_rt5640_quirk_table[] = {
126 {
127 .callback = byt_rt5640_quirk_cb,
128 .matches = {
129 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
130 DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
131 },
132 .driver_data = (unsigned long *)BYT_RT5640_IN1_MAP,
133 },
134 {
135 .callback = byt_rt5640_quirk_cb,
136 .matches = {
137 DMI_MATCH(DMI_SYS_VENDOR, "DellInc."),
138 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
139 },
140 .driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP |
141 BYT_RT5640_DMIC_EN),
142 },
143 {}
144};
145
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600146static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
147{
148 int ret;
149 struct snd_soc_codec *codec = runtime->codec;
150 struct snd_soc_card *card = runtime->card;
151 const struct snd_soc_dapm_route *custom_map;
152 int num_routes;
153
154 card->dapm.idle_bias_off = true;
155
Pierre-Louis Bossart0ec66e2d2016-01-04 17:20:27 -0600156 rt5640_sel_asrc_clk_src(codec,
157 RT5640_DA_STEREO_FILTER |
158 RT5640_AD_STEREO_FILTER,
159 RT5640_CLK_SEL_ASRC);
160
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600161 ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
162 ARRAY_SIZE(byt_rt5640_controls));
163 if (ret) {
164 dev_err(card->dev, "unable to add card controls\n");
165 return ret;
166 }
167
168 dmi_check_system(byt_rt5640_quirk_table);
169 switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
170 case BYT_RT5640_IN1_MAP:
171 custom_map = byt_rt5640_intmic_in1_map;
172 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
173 break;
174 case BYT_RT5640_DMIC2_MAP:
175 custom_map = byt_rt5640_intmic_dmic2_map;
176 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
177 break;
178 default:
179 custom_map = byt_rt5640_intmic_dmic1_map;
180 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
181 }
182
183 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
184 if (ret)
185 return ret;
186
187 if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
188 ret = rt5640_dmic_enable(codec, 0, 0);
189 if (ret)
190 return ret;
191 }
192
193 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
194 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
195
196 return ret;
197}
198
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600199static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530200 .formats = SNDRV_PCM_FMTBIT_S24_LE,
201 .rate_min = 48000,
202 .rate_max = 48000,
203 .channels_min = 2,
204 .channels_max = 2,
205};
206
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600207static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530208 struct snd_pcm_hw_params *params)
209{
210 struct snd_interval *rate = hw_param_interval(params,
211 SNDRV_PCM_HW_PARAM_RATE);
212 struct snd_interval *channels = hw_param_interval(params,
213 SNDRV_PCM_HW_PARAM_CHANNELS);
Sebastien Guiriec3f27ded2015-12-17 20:35:39 -0600214 int ret;
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530215
216 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
217 rate->min = rate->max = 48000;
218 channels->min = channels->max = 2;
219
220 /* set SSP2 to 24-bit */
Fang, Yang A369a9f52015-02-09 00:18:12 -0800221 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
Sebastien Guiriec3f27ded2015-12-17 20:35:39 -0600222
223 /*
224 * Default mode for SSP configuration is TDM 4 slot, override config
225 * with explicit setting to I2S 2ch 24-bit. The word length is set with
226 * dai_set_tdm_slot() since there is no other API exposed
227 */
228 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
229 SND_SOC_DAIFMT_I2S |
230 SND_SOC_DAIFMT_NB_IF |
231 SND_SOC_DAIFMT_CBS_CFS
232 );
233 if (ret < 0) {
234 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
235 return ret;
236 }
237
238 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
239 if (ret < 0) {
240 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
241 return ret;
242 }
243
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530244 return 0;
245}
246
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600247static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530248{
Lars-Peter Clausend0a1b662015-10-18 15:39:30 +0200249 return snd_pcm_hw_constraint_single(substream->runtime,
250 SNDRV_PCM_HW_PARAM_RATE, 48000);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530251}
252
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600253static struct snd_soc_ops byt_rt5640_aif1_ops = {
254 .startup = byt_rt5640_aif1_startup,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530255};
256
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600257static struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
258 .hw_params = byt_rt5640_aif1_hw_params,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530259};
260
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600261static struct snd_soc_dai_link byt_rt5640_dais[] = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530262 [MERR_DPCM_AUDIO] = {
263 .name = "Baytrail Audio Port",
264 .stream_name = "Baytrail Audio",
265 .cpu_dai_name = "media-cpu-dai",
266 .codec_dai_name = "snd-soc-dummy-dai",
267 .codec_name = "snd-soc-dummy",
268 .platform_name = "sst-mfld-platform",
269 .ignore_suspend = 1,
270 .dynamic = 1,
271 .dpcm_playback = 1,
272 .dpcm_capture = 1,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600273 .ops = &byt_rt5640_aif1_ops,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530274 },
Pierre-Louis Bossartd35eb962015-12-17 20:35:45 -0600275 [MERR_DPCM_DEEP_BUFFER] = {
276 .name = "Deep-Buffer Audio Port",
277 .stream_name = "Deep-Buffer Audio",
278 .cpu_dai_name = "deepbuffer-cpu-dai",
279 .codec_dai_name = "snd-soc-dummy-dai",
280 .codec_name = "snd-soc-dummy",
281 .platform_name = "sst-mfld-platform",
282 .ignore_suspend = 1,
283 .nonatomic = true,
284 .dynamic = 1,
285 .dpcm_playback = 1,
286 .ops = &byt_rt5640_aif1_ops,
287 },
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530288 [MERR_DPCM_COMPR] = {
289 .name = "Baytrail Compressed Port",
290 .stream_name = "Baytrail Compress",
291 .cpu_dai_name = "compress-cpu-dai",
292 .codec_dai_name = "snd-soc-dummy-dai",
293 .codec_name = "snd-soc-dummy",
294 .platform_name = "sst-mfld-platform",
295 },
296 /* back ends */
297 {
298 .name = "SSP2-Codec",
299 .be_id = 1,
300 .cpu_dai_name = "ssp2-port",
301 .platform_name = "sst-mfld-platform",
302 .no_pcm = 1,
303 .codec_dai_name = "rt5640-aif1",
304 .codec_name = "i2c-10EC5640:00",
305 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
306 | SND_SOC_DAIFMT_CBS_CFS,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600307 .be_hw_params_fixup = byt_rt5640_codec_fixup,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530308 .ignore_suspend = 1,
309 .dpcm_playback = 1,
310 .dpcm_capture = 1,
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600311 .init = byt_rt5640_init,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600312 .ops = &byt_rt5640_be_ssp2_ops,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530313 },
314};
315
316/* SoC card */
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600317static struct snd_soc_card byt_rt5640_card = {
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600318 .name = "bytcr-rt5640",
Axel Lin54d86972015-08-21 20:59:21 +0800319 .owner = THIS_MODULE,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600320 .dai_link = byt_rt5640_dais,
321 .num_links = ARRAY_SIZE(byt_rt5640_dais),
322 .dapm_widgets = byt_rt5640_widgets,
323 .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
324 .dapm_routes = byt_rt5640_audio_map,
325 .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600326 .fully_routed = true,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530327};
328
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600329static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530330{
331 int ret_val = 0;
332
333 /* register the soc card */
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600334 byt_rt5640_card.dev = &pdev->dev;
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530335
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600336 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
337
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530338 if (ret_val) {
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600339 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
340 ret_val);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530341 return ret_val;
342 }
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600343 platform_set_drvdata(pdev, &byt_rt5640_card);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530344 return ret_val;
345}
346
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600347static struct platform_driver snd_byt_rt5640_mc_driver = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530348 .driver = {
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600349 .name = "bytcr_rt5640",
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530350 .pm = &snd_soc_pm_ops,
351 },
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600352 .probe = snd_byt_rt5640_mc_probe,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530353};
354
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600355module_platform_driver(snd_byt_rt5640_mc_driver);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530356
357MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
358MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
359MODULE_LICENSE("GPL v2");
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600360MODULE_ALIAS("platform:bytcr_rt5640");