blob: 09ec3d718357671417a8275c349608cbdd7bbd98 [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>
Pierre-Louis Bossarte214f5e2016-08-12 16:27:53 -050027#include <asm/cpu_device_id.h>
28#include <asm/platform_sst_audio.h>
Irina Tirdeadf1a2772016-08-12 16:27:57 -050029#include <linux/clk.h>
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053030#include <sound/pcm.h>
31#include <sound/pcm_params.h>
32#include <sound/soc.h>
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -060033#include <sound/jack.h>
Jie Yange56c72d2015-04-02 15:37:02 +080034#include "../../codecs/rt5640.h"
Jie Yangb97169d2015-04-02 15:37:04 +080035#include "../atom/sst-atom-controls.h"
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -060036#include "../common/sst-acpi.h"
Pierre-Louis Bossarte214f5e2016-08-12 16:27:53 -050037#include "../common/sst-dsp.h"
Subhransu S. Prusty996cc842014-11-19 15:13:27 +053038
Pierre-Louis Bossartab738e42016-03-03 21:36:37 -060039enum {
40 BYT_RT5640_DMIC1_MAP,
41 BYT_RT5640_DMIC2_MAP,
42 BYT_RT5640_IN1_MAP,
Pierre-Louis Bossart59e8b652016-08-12 16:27:54 -050043 BYT_RT5640_IN3_MAP,
Pierre-Louis Bossartab738e42016-03-03 21:36:37 -060044};
45
46#define BYT_RT5640_MAP(quirk) ((quirk) & 0xff)
47#define BYT_RT5640_DMIC_EN BIT(16)
Pierre-Louis Bossart68817cd2016-08-12 16:27:45 -050048#define BYT_RT5640_MONO_SPEAKER BIT(17)
Pierre-Louis Bossart5d98f582016-08-12 16:27:46 -050049#define BYT_RT5640_DIFF_MIC BIT(18) /* defaut is single-ended */
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -050050#define BYT_RT5640_SSP2_AIF2 BIT(19) /* default is using AIF1 */
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -050051#define BYT_RT5640_SSP0_AIF1 BIT(20)
52#define BYT_RT5640_SSP0_AIF2 BIT(21)
Irina Tirdeadf1a2772016-08-12 16:27:57 -050053#define BYT_RT5640_MCLK_EN BIT(22)
54#define BYT_RT5640_MCLK_25MHZ BIT(23)
55
56struct byt_rt5640_private {
57 struct clk *mclk;
58};
Pierre-Louis Bossartab738e42016-03-03 21:36:37 -060059
60static unsigned long byt_rt5640_quirk = BYT_RT5640_DMIC1_MAP |
Irina Tirdeadf1a2772016-08-12 16:27:57 -050061 BYT_RT5640_DMIC_EN |
62 BYT_RT5640_MCLK_EN;
63
Pierre-Louis Bossartd7e60d52016-08-12 16:28:00 -050064static void log_quirks(struct device *dev)
65{
66 if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_DMIC1_MAP)
67 dev_info(dev, "quirk DMIC1_MAP enabled");
68 if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_DMIC2_MAP)
69 dev_info(dev, "quirk DMIC2_MAP enabled");
70 if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_IN1_MAP)
71 dev_info(dev, "quirk IN1_MAP enabled");
72 if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_IN3_MAP)
73 dev_info(dev, "quirk IN3_MAP enabled");
74 if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN)
75 dev_info(dev, "quirk DMIC enabled");
76 if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
77 dev_info(dev, "quirk MONO_SPEAKER enabled");
78 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
79 dev_info(dev, "quirk DIFF_MIC enabled");
80 if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2)
81 dev_info(dev, "quirk SSP2_AIF2 enabled");
82 if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1)
83 dev_info(dev, "quirk SSP0_AIF1 enabled");
84 if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)
85 dev_info(dev, "quirk SSP0_AIF2 enabled");
86 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
87 dev_info(dev, "quirk MCLK_EN enabled");
88 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
89 dev_info(dev, "quirk MCLK_25MHZ enabled");
90}
91
92
Irina Tirdeadf1a2772016-08-12 16:27:57 -050093#define BYT_CODEC_DAI1 "rt5640-aif1"
94#define BYT_CODEC_DAI2 "rt5640-aif2"
95
96static inline struct snd_soc_dai *byt_get_codec_dai(struct snd_soc_card *card)
97{
98 struct snd_soc_pcm_runtime *rtd;
99
100 list_for_each_entry(rtd, &card->rtd_list, list) {
101 if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI1,
102 strlen(BYT_CODEC_DAI1)))
103 return rtd->codec_dai;
104 if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI2,
105 strlen(BYT_CODEC_DAI2)))
106 return rtd->codec_dai;
107
108 }
109 return NULL;
110}
111
112static int platform_clock_control(struct snd_soc_dapm_widget *w,
113 struct snd_kcontrol *k, int event)
114{
115 struct snd_soc_dapm_context *dapm = w->dapm;
116 struct snd_soc_card *card = dapm->card;
117 struct snd_soc_dai *codec_dai;
118 struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
119 int ret;
120
121 codec_dai = byt_get_codec_dai(card);
122 if (!codec_dai) {
123 dev_err(card->dev,
124 "Codec dai not found; Unable to set platform clock\n");
125 return -EIO;
126 }
127
128 if (SND_SOC_DAPM_EVENT_ON(event)) {
129 if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
130 ret = clk_prepare_enable(priv->mclk);
131 if (ret < 0) {
132 dev_err(card->dev,
133 "could not configure MCLK state");
134 return ret;
135 }
136 }
137 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
138 48000 * 512,
139 SND_SOC_CLOCK_IN);
140 } else {
141 /*
142 * Set codec clock source to internal clock before
143 * turning off the platform clock. Codec needs clock
144 * for Jack detection and button press
145 */
146 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
147 0,
148 SND_SOC_CLOCK_IN);
149 if (!ret) {
150 if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk)
151 clk_disable_unprepare(priv->mclk);
152 }
153 }
154
155 if (ret < 0) {
156 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
157 return ret;
158 }
159
160 return 0;
161}
Pierre-Louis Bossartab738e42016-03-03 21:36:37 -0600162
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600163static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530164 SND_SOC_DAPM_HP("Headphone", NULL),
165 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -0600166 SND_SOC_DAPM_MIC("Internal Mic", NULL),
167 SND_SOC_DAPM_SPK("Speaker", NULL),
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500168 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
169 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
170 SND_SOC_DAPM_POST_PMD),
171
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530172};
173
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600174static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500175 {"Headphone", NULL, "Platform Clock"},
176 {"Headset Mic", NULL, "Platform Clock"},
177 {"Internal Mic", NULL, "Platform Clock"},
178 {"Speaker", NULL, "Platform Clock"},
179
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530180 {"Headset Mic", NULL, "MICBIAS1"},
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -0600181 {"IN2P", NULL, "Headset Mic"},
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530182 {"Headphone", NULL, "HPOL"},
183 {"Headphone", NULL, "HPOR"},
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530184};
185
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600186static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
187 {"DMIC1", NULL, "Internal Mic"},
188};
189
190static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
191 {"DMIC2", NULL, "Internal Mic"},
192};
193
194static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
195 {"Internal Mic", NULL, "MICBIAS1"},
196 {"IN1P", NULL, "Internal Mic"},
197};
198
Pierre-Louis Bossart59e8b652016-08-12 16:27:54 -0500199static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
200 {"Internal Mic", NULL, "MICBIAS1"},
201 {"IN3P", NULL, "Internal Mic"},
202};
203
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -0500204static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
205 {"ssp2 Tx", NULL, "codec_out0"},
206 {"ssp2 Tx", NULL, "codec_out1"},
207 {"codec_in0", NULL, "ssp2 Rx"},
208 {"codec_in1", NULL, "ssp2 Rx"},
209
210 {"AIF1 Playback", NULL, "ssp2 Tx"},
211 {"ssp2 Rx", NULL, "AIF1 Capture"},
212};
213
214static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
215 {"ssp2 Tx", NULL, "codec_out0"},
216 {"ssp2 Tx", NULL, "codec_out1"},
217 {"codec_in0", NULL, "ssp2 Rx"},
218 {"codec_in1", NULL, "ssp2 Rx"},
219
220 {"AIF2 Playback", NULL, "ssp2 Tx"},
221 {"ssp2 Rx", NULL, "AIF2 Capture"},
222};
223
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -0500224static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
225 {"ssp0 Tx", NULL, "modem_out"},
226 {"modem_in", NULL, "ssp0 Rx"},
227
228 {"AIF1 Playback", NULL, "ssp0 Tx"},
229 {"ssp0 Rx", NULL, "AIF1 Capture"},
230};
231
232static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
233 {"ssp0 Tx", NULL, "modem_out"},
234 {"modem_in", NULL, "ssp0 Rx"},
235
236 {"AIF2 Playback", NULL, "ssp0 Tx"},
237 {"ssp0 Rx", NULL, "AIF2 Capture"},
238};
239
Pierre-Louis Bossart68817cd2016-08-12 16:27:45 -0500240static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
241 {"Speaker", NULL, "SPOLP"},
242 {"Speaker", NULL, "SPOLN"},
243 {"Speaker", NULL, "SPORP"},
244 {"Speaker", NULL, "SPORN"},
245};
246
247static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
248 {"Speaker", NULL, "SPOLP"},
249 {"Speaker", NULL, "SPOLN"},
250};
251
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600252static const struct snd_kcontrol_new byt_rt5640_controls[] = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530253 SOC_DAPM_PIN_SWITCH("Headphone"),
254 SOC_DAPM_PIN_SWITCH("Headset Mic"),
Pierre-Louis Bossarte2be1da2015-12-17 20:35:40 -0600255 SOC_DAPM_PIN_SWITCH("Internal Mic"),
256 SOC_DAPM_PIN_SWITCH("Speaker"),
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530257};
258
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600259static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530260 struct snd_pcm_hw_params *params)
261{
262 struct snd_soc_pcm_runtime *rtd = substream->private_data;
263 struct snd_soc_dai *codec_dai = rtd->codec_dai;
264 int ret;
265
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530266 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
267 params_rate(params) * 512,
268 SND_SOC_CLOCK_IN);
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500269
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530270 if (ret < 0) {
271 dev_err(rtd->dev, "can't set codec clock %d\n", ret);
272 return ret;
273 }
274
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500275 if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
276 /* use bitclock as PLL input */
277 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
278 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
Pierre-Louis Bossart038a50e2016-08-12 16:27:52 -0500279
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500280 /* 2x16 bit slots on SSP0 */
281 ret = snd_soc_dai_set_pll(codec_dai, 0,
282 RT5640_PLL1_S_BCLK1,
283 params_rate(params) * 32,
284 params_rate(params) * 512);
285 } else {
286 /* 2x15 bit slots on SSP2 */
287 ret = snd_soc_dai_set_pll(codec_dai, 0,
288 RT5640_PLL1_S_BCLK1,
289 params_rate(params) * 50,
290 params_rate(params) * 512);
291 }
Pierre-Louis Bossart038a50e2016-08-12 16:27:52 -0500292 } else {
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500293 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
294 ret = snd_soc_dai_set_pll(codec_dai, 0,
295 RT5640_PLL1_S_MCLK,
296 25000000,
297 params_rate(params) * 512);
298 } else {
299 ret = snd_soc_dai_set_pll(codec_dai, 0,
300 RT5640_PLL1_S_MCLK,
301 19200000,
302 params_rate(params) * 512);
303 }
Pierre-Louis Bossart038a50e2016-08-12 16:27:52 -0500304 }
305
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530306 if (ret < 0) {
307 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
308 return ret;
309 }
310
311 return 0;
312}
313
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600314static int byt_rt5640_quirk_cb(const struct dmi_system_id *id)
315{
316 byt_rt5640_quirk = (unsigned long)id->driver_data;
317 return 1;
318}
319
320static const struct dmi_system_id byt_rt5640_quirk_table[] = {
321 {
322 .callback = byt_rt5640_quirk_cb,
323 .matches = {
Pierre-Louis Bossart73442e32016-08-12 16:27:44 -0500324 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
325 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600326 },
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500327 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
328 BYT_RT5640_MCLK_EN),
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600329 },
330 {
331 .callback = byt_rt5640_quirk_cb,
332 .matches = {
Pierre-Louis Bossart73442e32016-08-12 16:27:44 -0500333 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
334 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
335 },
Pierre-Louis Bossart68817cd2016-08-12 16:27:45 -0500336 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
Pierre-Louis Bossart5d98f582016-08-12 16:27:46 -0500337 BYT_RT5640_MONO_SPEAKER |
Pierre-Louis Bossarte214f5e2016-08-12 16:27:53 -0500338 BYT_RT5640_DIFF_MIC |
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500339 BYT_RT5640_SSP0_AIF2 |
340 BYT_RT5640_MCLK_EN
Pierre-Louis Bossart68817cd2016-08-12 16:27:45 -0500341 ),
Pierre-Louis Bossart73442e32016-08-12 16:27:44 -0500342 },
343 {
344 .callback = byt_rt5640_quirk_cb,
345 .matches = {
346 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "DellInc."),
347 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600348 },
349 .driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP |
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500350 BYT_RT5640_DMIC_EN |
351 BYT_RT5640_MCLK_EN),
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600352 },
Jorge Fernandez Monteagudo55fc2052016-01-04 17:20:30 -0600353 {
354 .callback = byt_rt5640_quirk_cb,
355 .matches = {
Pierre-Louis Bossart73442e32016-08-12 16:27:44 -0500356 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
357 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
Jorge Fernandez Monteagudo55fc2052016-01-04 17:20:30 -0600358 },
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500359 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
360 BYT_RT5640_MCLK_EN),
Jorge Fernandez Monteagudo55fc2052016-01-04 17:20:30 -0600361 },
Irina Tirdea0565e772016-08-12 16:27:58 -0500362 {
363 .callback = byt_rt5640_quirk_cb,
364 .matches = {
365 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
366 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
367 },
368 .driver_data = (unsigned long *)(BYT_RT5640_DMIC1_MAP |
369 BYT_RT5640_DMIC_EN),
370 },
Pierre-Louis Bossartec1c90e2016-08-12 16:27:59 -0500371 {
372 .callback = byt_rt5640_quirk_cb,
373 .matches = {
374 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
375 DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
376 },
377 .driver_data = (unsigned long *)(BYT_RT5640_IN3_MAP |
378 BYT_RT5640_MCLK_EN |
379 BYT_RT5640_SSP0_AIF1),
380 },
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600381 {}
382};
383
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600384static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
385{
386 int ret;
387 struct snd_soc_codec *codec = runtime->codec;
388 struct snd_soc_card *card = runtime->card;
389 const struct snd_soc_dapm_route *custom_map;
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500390 struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600391 int num_routes;
392
393 card->dapm.idle_bias_off = true;
394
Pierre-Louis Bossart0ec66e2d2016-01-04 17:20:27 -0600395 rt5640_sel_asrc_clk_src(codec,
396 RT5640_DA_STEREO_FILTER |
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500397 RT5640_DA_MONO_L_FILTER |
398 RT5640_DA_MONO_R_FILTER |
399 RT5640_AD_STEREO_FILTER |
400 RT5640_AD_MONO_L_FILTER |
401 RT5640_AD_MONO_R_FILTER,
Pierre-Louis Bossart0ec66e2d2016-01-04 17:20:27 -0600402 RT5640_CLK_SEL_ASRC);
403
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600404 ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
405 ARRAY_SIZE(byt_rt5640_controls));
406 if (ret) {
407 dev_err(card->dev, "unable to add card controls\n");
408 return ret;
409 }
410
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600411 switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
412 case BYT_RT5640_IN1_MAP:
413 custom_map = byt_rt5640_intmic_in1_map;
414 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
415 break;
Pierre-Louis Bossart59e8b652016-08-12 16:27:54 -0500416 case BYT_RT5640_IN3_MAP:
417 custom_map = byt_rt5640_intmic_in3_map;
418 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
419 break;
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600420 case BYT_RT5640_DMIC2_MAP:
421 custom_map = byt_rt5640_intmic_dmic2_map;
422 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
423 break;
424 default:
425 custom_map = byt_rt5640_intmic_dmic1_map;
426 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
427 }
428
429 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
430 if (ret)
431 return ret;
432
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -0500433 if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
434 ret = snd_soc_dapm_add_routes(&card->dapm,
435 byt_rt5640_ssp2_aif2_map,
436 ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -0500437 } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
438 ret = snd_soc_dapm_add_routes(&card->dapm,
439 byt_rt5640_ssp0_aif1_map,
440 ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
441 } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
442 ret = snd_soc_dapm_add_routes(&card->dapm,
443 byt_rt5640_ssp0_aif2_map,
444 ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -0500445 } else {
446 ret = snd_soc_dapm_add_routes(&card->dapm,
447 byt_rt5640_ssp2_aif1_map,
448 ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
449 }
450 if (ret)
451 return ret;
452
Pierre-Louis Bossart68817cd2016-08-12 16:27:45 -0500453 if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
454 ret = snd_soc_dapm_add_routes(&card->dapm,
455 byt_rt5640_mono_spk_map,
456 ARRAY_SIZE(byt_rt5640_mono_spk_map));
457 } else {
458 ret = snd_soc_dapm_add_routes(&card->dapm,
459 byt_rt5640_stereo_spk_map,
460 ARRAY_SIZE(byt_rt5640_stereo_spk_map));
461 }
462 if (ret)
463 return ret;
464
Pierre-Louis Bossart5d98f582016-08-12 16:27:46 -0500465 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) {
466 snd_soc_update_bits(codec, RT5640_IN1_IN2, RT5640_IN_DF1,
467 RT5640_IN_DF1);
468 }
469
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600470 if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
471 ret = rt5640_dmic_enable(codec, 0, 0);
472 if (ret)
473 return ret;
474 }
475
476 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
477 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
478
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500479 if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
480 /*
481 * The firmware might enable the clock at
482 * boot (this information may or may not
483 * be reflected in the enable clock register).
484 * To change the rate we must disable the clock
485 * first to cover these cases. Due to common
486 * clock framework restrictions that do not allow
487 * to disable a clock that has not been enabled,
488 * we need to enable the clock first.
489 */
490 ret = clk_prepare_enable(priv->mclk);
491 if (!ret)
492 clk_disable_unprepare(priv->mclk);
493
494 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
495 ret = clk_set_rate(priv->mclk, 25000000);
496 else
497 ret = clk_set_rate(priv->mclk, 19200000);
498
499 if (ret)
500 dev_err(card->dev, "unable to set MCLK rate\n");
501 }
502
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600503 return ret;
504}
505
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600506static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530507 .formats = SNDRV_PCM_FMTBIT_S24_LE,
508 .rate_min = 48000,
509 .rate_max = 48000,
510 .channels_min = 2,
511 .channels_max = 2,
512};
513
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600514static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530515 struct snd_pcm_hw_params *params)
516{
517 struct snd_interval *rate = hw_param_interval(params,
518 SNDRV_PCM_HW_PARAM_RATE);
519 struct snd_interval *channels = hw_param_interval(params,
520 SNDRV_PCM_HW_PARAM_CHANNELS);
Sebastien Guiriec3f27ded2015-12-17 20:35:39 -0600521 int ret;
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530522
Pierre-Louis Bossart038a50e2016-08-12 16:27:52 -0500523 /* The DSP will covert the FE rate to 48k, stereo */
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530524 rate->min = rate->max = 48000;
525 channels->min = channels->max = 2;
526
Pierre-Louis Bossart038a50e2016-08-12 16:27:52 -0500527 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
528 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
Sebastien Guiriec3f27ded2015-12-17 20:35:39 -0600529
Pierre-Louis Bossart038a50e2016-08-12 16:27:52 -0500530 /* set SSP2 to 16-bit */
531 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
532
533 /*
534 * Default mode for SSP configuration is TDM 4 slot, override config
535 * with explicit setting to I2S 2ch 16-bit. The word length is set with
536 * dai_set_tdm_slot() since there is no other API exposed
537 */
538 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
539 SND_SOC_DAIFMT_I2S |
540 SND_SOC_DAIFMT_NB_IF |
541 SND_SOC_DAIFMT_CBS_CFS
542 );
543 if (ret < 0) {
544 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
545 return ret;
546 }
547
548 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
549 if (ret < 0) {
550 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
551 return ret;
552 }
553
554 } else {
555
556 /* set SSP2 to 24-bit */
557 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
558
559 /*
560 * Default mode for SSP configuration is TDM 4 slot, override config
561 * with explicit setting to I2S 2ch 24-bit. The word length is set with
562 * dai_set_tdm_slot() since there is no other API exposed
563 */
564 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
565 SND_SOC_DAIFMT_I2S |
566 SND_SOC_DAIFMT_NB_IF |
567 SND_SOC_DAIFMT_CBS_CFS
568 );
569 if (ret < 0) {
570 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
571 return ret;
572 }
573
574 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
575 if (ret < 0) {
576 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
577 return ret;
578 }
Sebastien Guiriec3f27ded2015-12-17 20:35:39 -0600579 }
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530580 return 0;
581}
582
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600583static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530584{
Lars-Peter Clausend0a1b662015-10-18 15:39:30 +0200585 return snd_pcm_hw_constraint_single(substream->runtime,
586 SNDRV_PCM_HW_PARAM_RATE, 48000);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530587}
588
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600589static struct snd_soc_ops byt_rt5640_aif1_ops = {
590 .startup = byt_rt5640_aif1_startup,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530591};
592
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600593static struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
594 .hw_params = byt_rt5640_aif1_hw_params,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530595};
596
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600597static struct snd_soc_dai_link byt_rt5640_dais[] = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530598 [MERR_DPCM_AUDIO] = {
599 .name = "Baytrail Audio Port",
600 .stream_name = "Baytrail Audio",
601 .cpu_dai_name = "media-cpu-dai",
602 .codec_dai_name = "snd-soc-dummy-dai",
603 .codec_name = "snd-soc-dummy",
604 .platform_name = "sst-mfld-platform",
605 .ignore_suspend = 1,
606 .dynamic = 1,
607 .dpcm_playback = 1,
608 .dpcm_capture = 1,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600609 .ops = &byt_rt5640_aif1_ops,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530610 },
Pierre-Louis Bossartd35eb962015-12-17 20:35:45 -0600611 [MERR_DPCM_DEEP_BUFFER] = {
612 .name = "Deep-Buffer Audio Port",
613 .stream_name = "Deep-Buffer Audio",
614 .cpu_dai_name = "deepbuffer-cpu-dai",
615 .codec_dai_name = "snd-soc-dummy-dai",
616 .codec_name = "snd-soc-dummy",
617 .platform_name = "sst-mfld-platform",
618 .ignore_suspend = 1,
619 .nonatomic = true,
620 .dynamic = 1,
621 .dpcm_playback = 1,
622 .ops = &byt_rt5640_aif1_ops,
623 },
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530624 [MERR_DPCM_COMPR] = {
625 .name = "Baytrail Compressed Port",
626 .stream_name = "Baytrail Compress",
627 .cpu_dai_name = "compress-cpu-dai",
628 .codec_dai_name = "snd-soc-dummy-dai",
629 .codec_name = "snd-soc-dummy",
630 .platform_name = "sst-mfld-platform",
631 },
632 /* back ends */
633 {
634 .name = "SSP2-Codec",
Mengdong Lin2f0ad492016-04-19 13:12:35 +0800635 .id = 1,
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -0500636 .cpu_dai_name = "ssp2-port", /* overwritten for ssp0 routing */
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530637 .platform_name = "sst-mfld-platform",
638 .no_pcm = 1,
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -0500639 .codec_dai_name = "rt5640-aif1", /* changed w/ quirk */
Pierre-Louis Bossart7762ef42016-01-04 17:20:29 -0600640 .codec_name = "i2c-10EC5640:00", /* overwritten with HID */
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530641 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
642 | SND_SOC_DAIFMT_CBS_CFS,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600643 .be_hw_params_fixup = byt_rt5640_codec_fixup,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530644 .ignore_suspend = 1,
645 .dpcm_playback = 1,
646 .dpcm_capture = 1,
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600647 .init = byt_rt5640_init,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600648 .ops = &byt_rt5640_be_ssp2_ops,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530649 },
650};
651
652/* SoC card */
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600653static struct snd_soc_card byt_rt5640_card = {
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600654 .name = "bytcr-rt5640",
Axel Lin54d86972015-08-21 20:59:21 +0800655 .owner = THIS_MODULE,
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600656 .dai_link = byt_rt5640_dais,
657 .num_links = ARRAY_SIZE(byt_rt5640_dais),
658 .dapm_widgets = byt_rt5640_widgets,
659 .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
660 .dapm_routes = byt_rt5640_audio_map,
661 .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600662 .fully_routed = true,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530663};
664
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -0600665static char byt_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -0500666static char byt_rt5640_codec_aif_name[12]; /* = "rt5640-aif[1|2]" */
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -0500667static char byt_rt5640_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -0600668
Pierre-Louis Bossarte214f5e2016-08-12 16:27:53 -0500669static bool is_valleyview(void)
670{
671 static const struct x86_cpu_id cpu_ids[] __initconst = {
672 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
673 {}
674 };
675
676 if (!x86_match_cpu(cpu_ids))
677 return false;
678 return true;
679}
680
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500681
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600682static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530683{
684 int ret_val = 0;
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -0600685 struct sst_acpi_mach *mach;
Pierre-Louis Bossarta232b962016-03-03 21:36:38 -0600686 const char *i2c_name = NULL;
687 int i;
688 int dai_index;
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500689 struct byt_rt5640_private *priv;
690
691 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_ATOMIC);
692 if (!priv)
693 return -ENOMEM;
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530694
695 /* register the soc card */
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600696 byt_rt5640_card.dev = &pdev->dev;
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -0600697 mach = byt_rt5640_card.dev->platform_data;
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500698 snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -0600699
Pierre-Louis Bossarta232b962016-03-03 21:36:38 -0600700 /* fix index of codec dai */
701 dai_index = MERR_DPCM_COMPR + 1;
702 for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
703 if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) {
704 dai_index = i;
705 break;
706 }
707 }
708
Pierre-Louis Bossartcaf94ed2016-01-04 17:20:28 -0600709 /* fixup codec name based on HID */
Pierre-Louis Bossarta232b962016-03-03 21:36:38 -0600710 i2c_name = sst_acpi_find_name_from_hid(mach->id);
711 if (i2c_name != NULL) {
712 snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
713 "%s%s", "i2c-", i2c_name);
714
715 byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name;
716 }
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530717
Pierre-Louis Bossarte214f5e2016-08-12 16:27:53 -0500718 /*
719 * swap SSP0 if bytcr is detected
720 * (will be overridden if DMI quirk is detected)
721 */
722 if (is_valleyview()) {
723 struct sst_platform_info *p_info = mach->pdata;
724 const struct sst_res_info *res_info = p_info->res_info;
725
726 /* TODO: use CHAN package info from BIOS to detect AIF1/AIF2 */
727 if (res_info->acpi_ipc_irq_index == 0) {
728 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
729 }
730 }
731
Pierre-Louis Bossartab738e42016-03-03 21:36:37 -0600732 /* check quirks before creating card */
733 dmi_check_system(byt_rt5640_quirk_table);
Pierre-Louis Bossartd7e60d52016-08-12 16:28:00 -0500734 log_quirks(&pdev->dev);
Pierre-Louis Bossartab738e42016-03-03 21:36:37 -0600735
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -0500736 if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
737 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
Pierre-Louis Bossart89b89072016-08-12 16:27:50 -0500738
739 /* fixup codec aif name */
740 snprintf(byt_rt5640_codec_aif_name,
741 sizeof(byt_rt5640_codec_aif_name),
742 "%s", "rt5640-aif2");
743
744 byt_rt5640_dais[dai_index].codec_dai_name =
745 byt_rt5640_codec_aif_name;
746 }
747
Pierre-Louis Bossartf47088d2016-08-12 16:27:51 -0500748 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
749 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
750
751 /* fixup cpu dai name name */
752 snprintf(byt_rt5640_cpu_dai_name,
753 sizeof(byt_rt5640_cpu_dai_name),
754 "%s", "ssp0-port");
755
756 byt_rt5640_dais[dai_index].cpu_dai_name =
757 byt_rt5640_cpu_dai_name;
758 }
759
Irina Tirdeadf1a2772016-08-12 16:27:57 -0500760 if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && (is_valleyview())) {
761 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
762 if (IS_ERR(priv->mclk)) {
763 dev_err(&pdev->dev,
764 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
765 PTR_ERR(priv->mclk));
766 return PTR_ERR(priv->mclk);
767 }
768 }
769
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600770 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
771
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530772 if (ret_val) {
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600773 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
774 ret_val);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530775 return ret_val;
776 }
Pierre-Louis Bossart9fd57472015-12-17 20:35:42 -0600777 platform_set_drvdata(pdev, &byt_rt5640_card);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530778 return ret_val;
779}
780
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600781static struct platform_driver snd_byt_rt5640_mc_driver = {
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530782 .driver = {
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600783 .name = "bytcr_rt5640",
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530784 .pm = &snd_soc_pm_ops,
785 },
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600786 .probe = snd_byt_rt5640_mc_probe,
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530787};
788
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600789module_platform_driver(snd_byt_rt5640_mc_driver);
Subhransu S. Prusty996cc842014-11-19 15:13:27 +0530790
791MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
792MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
793MODULE_LICENSE("GPL v2");
Pierre-Louis Bossarta2d55632015-12-17 20:35:41 -0600794MODULE_ALIAS("platform:bytcr_rt5640");