blob: a105c84ef555d71d4803a5b881013dd226f2971b [file] [log] [blame]
Oder Chiou4a6180e2016-02-03 19:53:24 +08001/*
2 * rt5514.c -- RT5514 ALSA SoC audio codec driver
3 *
4 * Copyright 2015 Realtek Semiconductor Corp.
5 * Author: Oder Chiou <oder_chiou@realtek.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Oder Chiou6d3edf82017-05-15 19:02:07 +080012#include <linux/acpi.h>
Oder Chiou4a6180e2016-02-03 19:53:24 +080013#include <linux/fs.h>
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/regmap.h>
20#include <linux/i2c.h>
21#include <linux/platform_device.h>
22#include <linux/firmware.h>
23#include <linux/gpio.h>
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/soc-dapm.h>
29#include <sound/initval.h>
30#include <sound/tlv.h>
31
32#include "rl6231.h"
33#include "rt5514.h"
Oder Chiou6eebf352016-06-06 18:33:31 +080034#if defined(CONFIG_SND_SOC_RT5514_SPI)
35#include "rt5514-spi.h"
36#endif
Oder Chiou4a6180e2016-02-03 19:53:24 +080037
38static const struct reg_sequence rt5514_i2c_patch[] = {
39 {0x1800101c, 0x00000000},
40 {0x18001100, 0x0000031f},
41 {0x18001104, 0x00000007},
42 {0x18001108, 0x00000000},
43 {0x1800110c, 0x00000000},
44 {0x18001110, 0x00000000},
45 {0x18001114, 0x00000001},
46 {0x18001118, 0x00000000},
47 {0x18002f08, 0x00000006},
48 {0x18002f00, 0x00055149},
49 {0x18002f00, 0x0005514b},
50 {0x18002f00, 0x00055149},
51 {0xfafafafa, 0x00000001},
52 {0x18002f10, 0x00000001},
53 {0x18002f10, 0x00000000},
54 {0x18002f10, 0x00000001},
55 {0xfafafafa, 0x00000001},
56 {0x18002000, 0x000010ec},
57 {0xfafafafa, 0x00000000},
58};
59
60static const struct reg_sequence rt5514_patch[] = {
61 {RT5514_DIG_IO_CTRL, 0x00000040},
62 {RT5514_CLK_CTRL1, 0x38020041},
63 {RT5514_SRC_CTRL, 0x44000eee},
64 {RT5514_ANA_CTRL_LDO10, 0x00028604},
65 {RT5514_ANA_CTRL_ADCFED, 0x00000800},
66};
67
68static const struct reg_default rt5514_reg[] = {
69 {RT5514_RESET, 0x00000000},
70 {RT5514_PWR_ANA1, 0x00808880},
71 {RT5514_PWR_ANA2, 0x00220000},
72 {RT5514_I2S_CTRL1, 0x00000330},
73 {RT5514_I2S_CTRL2, 0x20000000},
74 {RT5514_VAD_CTRL6, 0xc00007d2},
75 {RT5514_EXT_VAD_CTRL, 0x80000080},
76 {RT5514_DIG_IO_CTRL, 0x00000040},
77 {RT5514_PAD_CTRL1, 0x00804000},
78 {RT5514_DMIC_DATA_CTRL, 0x00000005},
79 {RT5514_DIG_SOURCE_CTRL, 0x00000002},
80 {RT5514_SRC_CTRL, 0x44000eee},
81 {RT5514_DOWNFILTER2_CTRL1, 0x0000882f},
82 {RT5514_PLL_SOURCE_CTRL, 0x00000004},
83 {RT5514_CLK_CTRL1, 0x38020041},
84 {RT5514_CLK_CTRL2, 0x00000000},
85 {RT5514_PLL3_CALIB_CTRL1, 0x00400200},
86 {RT5514_PLL3_CALIB_CTRL5, 0x40220012},
87 {RT5514_DELAY_BUF_CTRL1, 0x7fff006a},
88 {RT5514_DELAY_BUF_CTRL3, 0x00000000},
89 {RT5514_DOWNFILTER0_CTRL1, 0x00020c2f},
90 {RT5514_DOWNFILTER0_CTRL2, 0x00020c2f},
91 {RT5514_DOWNFILTER0_CTRL3, 0x00000362},
92 {RT5514_DOWNFILTER1_CTRL1, 0x00020c2f},
93 {RT5514_DOWNFILTER1_CTRL2, 0x00020c2f},
94 {RT5514_DOWNFILTER1_CTRL3, 0x00000362},
95 {RT5514_ANA_CTRL_LDO10, 0x00028604},
96 {RT5514_ANA_CTRL_LDO18_16, 0x02000345},
97 {RT5514_ANA_CTRL_ADC12, 0x0000a2a8},
98 {RT5514_ANA_CTRL_ADC21, 0x00001180},
99 {RT5514_ANA_CTRL_ADC22, 0x0000aaa8},
100 {RT5514_ANA_CTRL_ADC23, 0x00151427},
101 {RT5514_ANA_CTRL_MICBST, 0x00002000},
102 {RT5514_ANA_CTRL_ADCFED, 0x00000800},
103 {RT5514_ANA_CTRL_INBUF, 0x00000143},
104 {RT5514_ANA_CTRL_VREF, 0x00008d50},
105 {RT5514_ANA_CTRL_PLL3, 0x0000000e},
106 {RT5514_ANA_CTRL_PLL1_1, 0x00000000},
107 {RT5514_ANA_CTRL_PLL1_2, 0x00030220},
108 {RT5514_DMIC_LP_CTRL, 0x00000000},
109 {RT5514_MISC_CTRL_DSP, 0x00000000},
110 {RT5514_DSP_CTRL1, 0x00055149},
111 {RT5514_DSP_CTRL3, 0x00000006},
112 {RT5514_DSP_CTRL4, 0x00000001},
113 {RT5514_VENDOR_ID1, 0x00000001},
114 {RT5514_VENDOR_ID2, 0x10ec5514},
115};
116
Oder Chiou6eebf352016-06-06 18:33:31 +0800117static void rt5514_enable_dsp_prepare(struct rt5514_priv *rt5514)
118{
119 /* Reset */
120 regmap_write(rt5514->i2c_regmap, 0x18002000, 0x000010ec);
121 /* LDO_I_limit */
122 regmap_write(rt5514->i2c_regmap, 0x18002200, 0x00028604);
123 /* I2C bypass enable */
124 regmap_write(rt5514->i2c_regmap, 0xfafafafa, 0x00000001);
125 /* mini-core reset */
126 regmap_write(rt5514->i2c_regmap, 0x18002f00, 0x0005514b);
127 regmap_write(rt5514->i2c_regmap, 0x18002f00, 0x00055149);
128 /* I2C bypass disable */
129 regmap_write(rt5514->i2c_regmap, 0xfafafafa, 0x00000000);
130 /* PIN config */
131 regmap_write(rt5514->i2c_regmap, 0x18002070, 0x00000040);
132 /* PLL3(QN)=RCOSC*(10+2) */
133 regmap_write(rt5514->i2c_regmap, 0x18002240, 0x0000000a);
134 /* PLL3 source=RCOSC, fsi=rt_clk */
135 regmap_write(rt5514->i2c_regmap, 0x18002100, 0x0000000b);
136 /* Power on RCOSC, pll3 */
137 regmap_write(rt5514->i2c_regmap, 0x18002004, 0x00808b81);
138 /* DSP clk source = pll3, ENABLE DSP clk */
139 regmap_write(rt5514->i2c_regmap, 0x18002f08, 0x00000005);
140 /* Enable DSP clk auto switch */
141 regmap_write(rt5514->i2c_regmap, 0x18001114, 0x00000001);
142 /* Reduce DSP power */
143 regmap_write(rt5514->i2c_regmap, 0x18001118, 0x00000001);
144}
145
Oder Chiou4a6180e2016-02-03 19:53:24 +0800146static bool rt5514_volatile_register(struct device *dev, unsigned int reg)
147{
148 switch (reg) {
149 case RT5514_VENDOR_ID1:
150 case RT5514_VENDOR_ID2:
151 return true;
152
153 default:
154 return false;
155 }
156}
157
158static bool rt5514_readable_register(struct device *dev, unsigned int reg)
159{
160 switch (reg) {
161 case RT5514_RESET:
162 case RT5514_PWR_ANA1:
163 case RT5514_PWR_ANA2:
164 case RT5514_I2S_CTRL1:
165 case RT5514_I2S_CTRL2:
166 case RT5514_VAD_CTRL6:
167 case RT5514_EXT_VAD_CTRL:
168 case RT5514_DIG_IO_CTRL:
169 case RT5514_PAD_CTRL1:
170 case RT5514_DMIC_DATA_CTRL:
171 case RT5514_DIG_SOURCE_CTRL:
172 case RT5514_SRC_CTRL:
173 case RT5514_DOWNFILTER2_CTRL1:
174 case RT5514_PLL_SOURCE_CTRL:
175 case RT5514_CLK_CTRL1:
176 case RT5514_CLK_CTRL2:
177 case RT5514_PLL3_CALIB_CTRL1:
178 case RT5514_PLL3_CALIB_CTRL5:
179 case RT5514_DELAY_BUF_CTRL1:
180 case RT5514_DELAY_BUF_CTRL3:
181 case RT5514_DOWNFILTER0_CTRL1:
182 case RT5514_DOWNFILTER0_CTRL2:
183 case RT5514_DOWNFILTER0_CTRL3:
184 case RT5514_DOWNFILTER1_CTRL1:
185 case RT5514_DOWNFILTER1_CTRL2:
186 case RT5514_DOWNFILTER1_CTRL3:
187 case RT5514_ANA_CTRL_LDO10:
188 case RT5514_ANA_CTRL_LDO18_16:
189 case RT5514_ANA_CTRL_ADC12:
190 case RT5514_ANA_CTRL_ADC21:
191 case RT5514_ANA_CTRL_ADC22:
192 case RT5514_ANA_CTRL_ADC23:
193 case RT5514_ANA_CTRL_MICBST:
194 case RT5514_ANA_CTRL_ADCFED:
195 case RT5514_ANA_CTRL_INBUF:
196 case RT5514_ANA_CTRL_VREF:
197 case RT5514_ANA_CTRL_PLL3:
198 case RT5514_ANA_CTRL_PLL1_1:
199 case RT5514_ANA_CTRL_PLL1_2:
200 case RT5514_DMIC_LP_CTRL:
201 case RT5514_MISC_CTRL_DSP:
202 case RT5514_DSP_CTRL1:
203 case RT5514_DSP_CTRL3:
204 case RT5514_DSP_CTRL4:
205 case RT5514_VENDOR_ID1:
206 case RT5514_VENDOR_ID2:
207 return true;
208
209 default:
210 return false;
211 }
212}
213
214static bool rt5514_i2c_readable_register(struct device *dev,
215 unsigned int reg)
216{
217 switch (reg) {
218 case RT5514_DSP_MAPPING | RT5514_RESET:
219 case RT5514_DSP_MAPPING | RT5514_PWR_ANA1:
220 case RT5514_DSP_MAPPING | RT5514_PWR_ANA2:
221 case RT5514_DSP_MAPPING | RT5514_I2S_CTRL1:
222 case RT5514_DSP_MAPPING | RT5514_I2S_CTRL2:
223 case RT5514_DSP_MAPPING | RT5514_VAD_CTRL6:
224 case RT5514_DSP_MAPPING | RT5514_EXT_VAD_CTRL:
225 case RT5514_DSP_MAPPING | RT5514_DIG_IO_CTRL:
226 case RT5514_DSP_MAPPING | RT5514_PAD_CTRL1:
227 case RT5514_DSP_MAPPING | RT5514_DMIC_DATA_CTRL:
228 case RT5514_DSP_MAPPING | RT5514_DIG_SOURCE_CTRL:
229 case RT5514_DSP_MAPPING | RT5514_SRC_CTRL:
230 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER2_CTRL1:
231 case RT5514_DSP_MAPPING | RT5514_PLL_SOURCE_CTRL:
232 case RT5514_DSP_MAPPING | RT5514_CLK_CTRL1:
233 case RT5514_DSP_MAPPING | RT5514_CLK_CTRL2:
234 case RT5514_DSP_MAPPING | RT5514_PLL3_CALIB_CTRL1:
235 case RT5514_DSP_MAPPING | RT5514_PLL3_CALIB_CTRL5:
236 case RT5514_DSP_MAPPING | RT5514_DELAY_BUF_CTRL1:
237 case RT5514_DSP_MAPPING | RT5514_DELAY_BUF_CTRL3:
238 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL1:
239 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL2:
240 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL3:
241 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL1:
242 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL2:
243 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL3:
244 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_LDO10:
245 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_LDO18_16:
246 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC12:
247 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC21:
248 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC22:
249 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC23:
250 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_MICBST:
251 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADCFED:
252 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_INBUF:
253 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_VREF:
254 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL3:
255 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL1_1:
256 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL1_2:
257 case RT5514_DSP_MAPPING | RT5514_DMIC_LP_CTRL:
258 case RT5514_DSP_MAPPING | RT5514_MISC_CTRL_DSP:
259 case RT5514_DSP_MAPPING | RT5514_DSP_CTRL1:
260 case RT5514_DSP_MAPPING | RT5514_DSP_CTRL3:
261 case RT5514_DSP_MAPPING | RT5514_DSP_CTRL4:
262 case RT5514_DSP_MAPPING | RT5514_VENDOR_ID1:
263 case RT5514_DSP_MAPPING | RT5514_VENDOR_ID2:
264 return true;
265
266 default:
267 return false;
268 }
269}
270
271/* {-3, 0, +3, +4.5, +7.5, +9.5, +12, +14, +17} dB */
272static const DECLARE_TLV_DB_RANGE(bst_tlv,
273 0, 2, TLV_DB_SCALE_ITEM(-300, 300, 0),
274 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
275 4, 4, TLV_DB_SCALE_ITEM(750, 0, 0),
276 5, 5, TLV_DB_SCALE_ITEM(950, 0, 0),
277 6, 6, TLV_DB_SCALE_ITEM(1200, 0, 0),
278 7, 7, TLV_DB_SCALE_ITEM(1400, 0, 0),
279 8, 8, TLV_DB_SCALE_ITEM(1700, 0, 0)
280);
281
Oder Chioua1338a72016-09-07 11:07:49 +0800282static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1725, 75, 0);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800283
Oder Chiou6eebf352016-06-06 18:33:31 +0800284static int rt5514_dsp_voice_wake_up_get(struct snd_kcontrol *kcontrol,
285 struct snd_ctl_elem_value *ucontrol)
286{
287 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
288 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
289
290 ucontrol->value.integer.value[0] = rt5514->dsp_enabled;
291
292 return 0;
293}
294
295static int rt5514_dsp_voice_wake_up_put(struct snd_kcontrol *kcontrol,
296 struct snd_ctl_elem_value *ucontrol)
297{
298 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
299 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
300 struct snd_soc_codec *codec = rt5514->codec;
301 const struct firmware *fw = NULL;
302
303 if (ucontrol->value.integer.value[0] == rt5514->dsp_enabled)
304 return 0;
305
306 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
307 rt5514->dsp_enabled = ucontrol->value.integer.value[0];
308
309 if (rt5514->dsp_enabled) {
310 rt5514_enable_dsp_prepare(rt5514);
311
312 request_firmware(&fw, RT5514_FIRMWARE1, codec->dev);
313 if (fw) {
314#if defined(CONFIG_SND_SOC_RT5514_SPI)
315 rt5514_spi_burst_write(0x4ff60000, fw->data,
316 ((fw->size/8)+1)*8);
317#else
318 dev_err(codec->dev, "There is no SPI driver for"
319 " loading the firmware\n");
320#endif
321 release_firmware(fw);
322 fw = NULL;
323 }
324
325 request_firmware(&fw, RT5514_FIRMWARE2, codec->dev);
326 if (fw) {
327#if defined(CONFIG_SND_SOC_RT5514_SPI)
328 rt5514_spi_burst_write(0x4ffc0000, fw->data,
329 ((fw->size/8)+1)*8);
330#else
331 dev_err(codec->dev, "There is no SPI driver for"
332 " loading the firmware\n");
333#endif
334 release_firmware(fw);
335 fw = NULL;
336 }
337
338 /* DSP run */
339 regmap_write(rt5514->i2c_regmap, 0x18002f00,
340 0x00055148);
341 } else {
342 regmap_multi_reg_write(rt5514->i2c_regmap,
343 rt5514_i2c_patch, ARRAY_SIZE(rt5514_i2c_patch));
344 regcache_mark_dirty(rt5514->regmap);
345 regcache_sync(rt5514->regmap);
346 }
347 }
348
349 return 0;
350}
351
Oder Chiou4a6180e2016-02-03 19:53:24 +0800352static const struct snd_kcontrol_new rt5514_snd_controls[] = {
353 SOC_DOUBLE_TLV("MIC Boost Volume", RT5514_ANA_CTRL_MICBST,
354 RT5514_SEL_BSTL_SFT, RT5514_SEL_BSTR_SFT, 8, 0, bst_tlv),
355 SOC_DOUBLE_R_TLV("ADC1 Capture Volume", RT5514_DOWNFILTER0_CTRL1,
Oder Chioua1338a72016-09-07 11:07:49 +0800356 RT5514_DOWNFILTER0_CTRL2, RT5514_AD_GAIN_SFT, 63, 0,
Oder Chiou4a6180e2016-02-03 19:53:24 +0800357 adc_vol_tlv),
358 SOC_DOUBLE_R_TLV("ADC2 Capture Volume", RT5514_DOWNFILTER1_CTRL1,
Oder Chioua1338a72016-09-07 11:07:49 +0800359 RT5514_DOWNFILTER1_CTRL2, RT5514_AD_GAIN_SFT, 63, 0,
Oder Chiou4a6180e2016-02-03 19:53:24 +0800360 adc_vol_tlv),
Oder Chiou6eebf352016-06-06 18:33:31 +0800361 SOC_SINGLE_EXT("DSP Voice Wake Up", SND_SOC_NOPM, 0, 1, 0,
362 rt5514_dsp_voice_wake_up_get, rt5514_dsp_voice_wake_up_put),
Oder Chiou4a6180e2016-02-03 19:53:24 +0800363};
364
365/* ADC Mixer*/
366static const struct snd_kcontrol_new rt5514_sto1_adc_l_mix[] = {
367 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER0_CTRL1,
368 RT5514_AD_DMIC_MIX_BIT, 1, 1),
369 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER0_CTRL1,
370 RT5514_AD_AD_MIX_BIT, 1, 1),
371};
372
373static const struct snd_kcontrol_new rt5514_sto1_adc_r_mix[] = {
374 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER0_CTRL2,
375 RT5514_AD_DMIC_MIX_BIT, 1, 1),
376 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER0_CTRL2,
377 RT5514_AD_AD_MIX_BIT, 1, 1),
378};
379
380static const struct snd_kcontrol_new rt5514_sto2_adc_l_mix[] = {
381 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER1_CTRL1,
382 RT5514_AD_DMIC_MIX_BIT, 1, 1),
383 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER1_CTRL1,
384 RT5514_AD_AD_MIX_BIT, 1, 1),
385};
386
387static const struct snd_kcontrol_new rt5514_sto2_adc_r_mix[] = {
388 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER1_CTRL2,
389 RT5514_AD_DMIC_MIX_BIT, 1, 1),
390 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER1_CTRL2,
391 RT5514_AD_AD_MIX_BIT, 1, 1),
392};
393
394/* DMIC Source */
395static const char * const rt5514_dmic_src[] = {
396 "DMIC1", "DMIC2"
397};
398
Arnd Bergmann03ba7912017-05-11 13:44:38 +0200399static SOC_ENUM_SINGLE_DECL(
Oder Chiou4a6180e2016-02-03 19:53:24 +0800400 rt5514_stereo1_dmic_enum, RT5514_DIG_SOURCE_CTRL,
401 RT5514_AD0_DMIC_INPUT_SEL_SFT, rt5514_dmic_src);
402
403static const struct snd_kcontrol_new rt5514_sto1_dmic_mux =
404 SOC_DAPM_ENUM("Stereo1 DMIC Source", rt5514_stereo1_dmic_enum);
405
Arnd Bergmann03ba7912017-05-11 13:44:38 +0200406static SOC_ENUM_SINGLE_DECL(
Oder Chiou4a6180e2016-02-03 19:53:24 +0800407 rt5514_stereo2_dmic_enum, RT5514_DIG_SOURCE_CTRL,
408 RT5514_AD1_DMIC_INPUT_SEL_SFT, rt5514_dmic_src);
409
410static const struct snd_kcontrol_new rt5514_sto2_dmic_mux =
411 SOC_DAPM_ENUM("Stereo2 DMIC Source", rt5514_stereo2_dmic_enum);
412
413/**
414 * rt5514_calc_dmic_clk - Calculate the frequency divider parameter of dmic.
415 *
416 * @rate: base clock rate.
417 *
418 * Choose divider parameter that gives the highest possible DMIC frequency in
419 * 1MHz - 3MHz range.
420 */
421static int rt5514_calc_dmic_clk(struct snd_soc_codec *codec, int rate)
422{
423 int div[] = {2, 3, 4, 8, 12, 16, 24, 32};
424 int i;
425
426 if (rate < 1000000 * div[0]) {
427 pr_warn("Base clock rate %d is too low\n", rate);
428 return -EINVAL;
429 }
430
431 for (i = 0; i < ARRAY_SIZE(div); i++) {
432 /* find divider that gives DMIC frequency below 3.072MHz */
433 if (3072000 * div[i] >= rate)
434 return i;
435 }
436
437 dev_warn(codec->dev, "Base clock rate %d is too high\n", rate);
438 return -EINVAL;
439}
440
441static int rt5514_set_dmic_clk(struct snd_soc_dapm_widget *w,
442 struct snd_kcontrol *kcontrol, int event)
443{
444 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
445 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
446 int idx;
447
448 idx = rt5514_calc_dmic_clk(codec, rt5514->sysclk);
449 if (idx < 0)
450 dev_err(codec->dev, "Failed to set DMIC clock\n");
451 else
452 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL1,
453 RT5514_CLK_DMIC_OUT_SEL_MASK,
454 idx << RT5514_CLK_DMIC_OUT_SEL_SFT);
455
Oder Chioua5461fd2016-10-25 19:27:26 +0800456 if (rt5514->pdata.dmic_init_delay)
457 msleep(rt5514->pdata.dmic_init_delay);
458
Oder Chiou4a6180e2016-02-03 19:53:24 +0800459 return idx;
460}
461
462static int rt5514_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
463 struct snd_soc_dapm_widget *sink)
464{
465 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
466 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
467
468 if (rt5514->sysclk_src == RT5514_SCLK_S_PLL1)
469 return 1;
470 else
471 return 0;
472}
473
474static const struct snd_soc_dapm_widget rt5514_dapm_widgets[] = {
475 /* Input Lines */
476 SND_SOC_DAPM_INPUT("DMIC1L"),
477 SND_SOC_DAPM_INPUT("DMIC1R"),
478 SND_SOC_DAPM_INPUT("DMIC2L"),
479 SND_SOC_DAPM_INPUT("DMIC2R"),
480
481 SND_SOC_DAPM_INPUT("AMICL"),
482 SND_SOC_DAPM_INPUT("AMICR"),
483
484 SND_SOC_DAPM_PGA("DMIC1", SND_SOC_NOPM, 0, 0, NULL, 0),
485 SND_SOC_DAPM_PGA("DMIC2", SND_SOC_NOPM, 0, 0, NULL, 0),
486
487 SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
488 rt5514_set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
489
490 SND_SOC_DAPM_SUPPLY("ADC CLK", RT5514_CLK_CTRL1,
491 RT5514_CLK_AD_ANA1_EN_BIT, 0, NULL, 0),
492
493 SND_SOC_DAPM_SUPPLY("LDO18 IN", RT5514_PWR_ANA1,
494 RT5514_POW_LDO18_IN_BIT, 0, NULL, 0),
495 SND_SOC_DAPM_SUPPLY("LDO18 ADC", RT5514_PWR_ANA1,
496 RT5514_POW_LDO18_ADC_BIT, 0, NULL, 0),
497 SND_SOC_DAPM_SUPPLY("LDO21", RT5514_PWR_ANA1, RT5514_POW_LDO21_BIT, 0,
498 NULL, 0),
499 SND_SOC_DAPM_SUPPLY("BG LDO18 IN", RT5514_PWR_ANA1,
500 RT5514_POW_BG_LDO18_IN_BIT, 0, NULL, 0),
501 SND_SOC_DAPM_SUPPLY("BG LDO21", RT5514_PWR_ANA1,
502 RT5514_POW_BG_LDO21_BIT, 0, NULL, 0),
503 SND_SOC_DAPM_SUPPLY("BG MBIAS", RT5514_PWR_ANA2,
504 RT5514_POW_BG_MBIAS_BIT, 0, NULL, 0),
505 SND_SOC_DAPM_SUPPLY("MBIAS", RT5514_PWR_ANA2, RT5514_POW_MBIAS_BIT, 0,
506 NULL, 0),
507 SND_SOC_DAPM_SUPPLY("VREF2", RT5514_PWR_ANA2, RT5514_POW_VREF2_BIT, 0,
508 NULL, 0),
509 SND_SOC_DAPM_SUPPLY("VREF1", RT5514_PWR_ANA2, RT5514_POW_VREF1_BIT, 0,
510 NULL, 0),
511 SND_SOC_DAPM_SUPPLY("ADC Power", SND_SOC_NOPM, 0, 0, NULL, 0),
512
513
514 SND_SOC_DAPM_SUPPLY("LDO16L", RT5514_PWR_ANA2, RT5514_POWL_LDO16_BIT, 0,
515 NULL, 0),
516 SND_SOC_DAPM_SUPPLY("ADC1L", RT5514_PWR_ANA2, RT5514_POW_ADC1_L_BIT, 0,
517 NULL, 0),
518 SND_SOC_DAPM_SUPPLY("BSTL2", RT5514_PWR_ANA2, RT5514_POW2_BSTL_BIT, 0,
519 NULL, 0),
520 SND_SOC_DAPM_SUPPLY("BSTL", RT5514_PWR_ANA2, RT5514_POW_BSTL_BIT, 0,
521 NULL, 0),
522 SND_SOC_DAPM_SUPPLY("ADCFEDL", RT5514_PWR_ANA2, RT5514_POW_ADCFEDL_BIT,
523 0, NULL, 0),
524 SND_SOC_DAPM_SUPPLY("ADCL Power", SND_SOC_NOPM, 0, 0, NULL, 0),
525
526 SND_SOC_DAPM_SUPPLY("LDO16R", RT5514_PWR_ANA2, RT5514_POWR_LDO16_BIT, 0,
527 NULL, 0),
528 SND_SOC_DAPM_SUPPLY("ADC1R", RT5514_PWR_ANA2, RT5514_POW_ADC1_R_BIT, 0,
529 NULL, 0),
530 SND_SOC_DAPM_SUPPLY("BSTR2", RT5514_PWR_ANA2, RT5514_POW2_BSTR_BIT, 0,
531 NULL, 0),
532 SND_SOC_DAPM_SUPPLY("BSTR", RT5514_PWR_ANA2, RT5514_POW_BSTR_BIT, 0,
533 NULL, 0),
534 SND_SOC_DAPM_SUPPLY("ADCFEDR", RT5514_PWR_ANA2, RT5514_POW_ADCFEDR_BIT,
535 0, NULL, 0),
536 SND_SOC_DAPM_SUPPLY("ADCR Power", SND_SOC_NOPM, 0, 0, NULL, 0),
537
538 SND_SOC_DAPM_SUPPLY("PLL1 LDO ENABLE", RT5514_ANA_CTRL_PLL1_2,
539 RT5514_EN_LDO_PLL1_BIT, 0, NULL, 0),
540 SND_SOC_DAPM_SUPPLY("PLL1 LDO", RT5514_PWR_ANA2,
541 RT5514_POW_PLL1_LDO_BIT, 0, NULL, 0),
542 SND_SOC_DAPM_SUPPLY("PLL1", RT5514_PWR_ANA2, RT5514_POW_PLL1_BIT, 0,
543 NULL, 0),
544
545 /* ADC Mux */
546 SND_SOC_DAPM_MUX("Stereo1 DMIC Mux", SND_SOC_NOPM, 0, 0,
547 &rt5514_sto1_dmic_mux),
548 SND_SOC_DAPM_MUX("Stereo2 DMIC Mux", SND_SOC_NOPM, 0, 0,
549 &rt5514_sto2_dmic_mux),
550
551 /* ADC Mixer */
552 SND_SOC_DAPM_SUPPLY("adc stereo1 filter", RT5514_CLK_CTRL1,
553 RT5514_CLK_AD0_EN_BIT, 0, NULL, 0),
554 SND_SOC_DAPM_SUPPLY("adc stereo2 filter", RT5514_CLK_CTRL1,
555 RT5514_CLK_AD1_EN_BIT, 0, NULL, 0),
556
557 SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", SND_SOC_NOPM, 0, 0,
558 rt5514_sto1_adc_l_mix, ARRAY_SIZE(rt5514_sto1_adc_l_mix)),
559 SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", SND_SOC_NOPM, 0, 0,
560 rt5514_sto1_adc_r_mix, ARRAY_SIZE(rt5514_sto1_adc_r_mix)),
561 SND_SOC_DAPM_MIXER("Sto2 ADC MIXL", SND_SOC_NOPM, 0, 0,
562 rt5514_sto2_adc_l_mix, ARRAY_SIZE(rt5514_sto2_adc_l_mix)),
563 SND_SOC_DAPM_MIXER("Sto2 ADC MIXR", SND_SOC_NOPM, 0, 0,
564 rt5514_sto2_adc_r_mix, ARRAY_SIZE(rt5514_sto2_adc_r_mix)),
565
566 SND_SOC_DAPM_ADC("Stereo1 ADC MIXL", NULL, RT5514_DOWNFILTER0_CTRL1,
567 RT5514_AD_AD_MUTE_BIT, 1),
568 SND_SOC_DAPM_ADC("Stereo1 ADC MIXR", NULL, RT5514_DOWNFILTER0_CTRL2,
569 RT5514_AD_AD_MUTE_BIT, 1),
570 SND_SOC_DAPM_ADC("Stereo2 ADC MIXL", NULL, RT5514_DOWNFILTER1_CTRL1,
571 RT5514_AD_AD_MUTE_BIT, 1),
572 SND_SOC_DAPM_ADC("Stereo2 ADC MIXR", NULL, RT5514_DOWNFILTER1_CTRL2,
573 RT5514_AD_AD_MUTE_BIT, 1),
574
575 /* ADC PGA */
576 SND_SOC_DAPM_PGA("Stereo1 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
577 SND_SOC_DAPM_PGA("Stereo2 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
578
579 /* Audio Interface */
580 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
581};
582
583static const struct snd_soc_dapm_route rt5514_dapm_routes[] = {
584 { "DMIC1", NULL, "DMIC1L" },
585 { "DMIC1", NULL, "DMIC1R" },
586 { "DMIC2", NULL, "DMIC2L" },
587 { "DMIC2", NULL, "DMIC2R" },
588
589 { "DMIC1L", NULL, "DMIC CLK" },
590 { "DMIC1R", NULL, "DMIC CLK" },
591 { "DMIC2L", NULL, "DMIC CLK" },
592 { "DMIC2R", NULL, "DMIC CLK" },
593
594 { "Stereo1 DMIC Mux", "DMIC1", "DMIC1" },
595 { "Stereo1 DMIC Mux", "DMIC2", "DMIC2" },
596
597 { "Sto1 ADC MIXL", "DMIC Switch", "Stereo1 DMIC Mux" },
598 { "Sto1 ADC MIXL", "ADC Switch", "AMICL" },
599 { "Sto1 ADC MIXR", "DMIC Switch", "Stereo1 DMIC Mux" },
600 { "Sto1 ADC MIXR", "ADC Switch", "AMICR" },
601
602 { "ADC Power", NULL, "LDO18 IN" },
603 { "ADC Power", NULL, "LDO18 ADC" },
604 { "ADC Power", NULL, "LDO21" },
605 { "ADC Power", NULL, "BG LDO18 IN" },
606 { "ADC Power", NULL, "BG LDO21" },
607 { "ADC Power", NULL, "BG MBIAS" },
608 { "ADC Power", NULL, "MBIAS" },
609 { "ADC Power", NULL, "VREF2" },
610 { "ADC Power", NULL, "VREF1" },
611
612 { "ADCL Power", NULL, "LDO16L" },
613 { "ADCL Power", NULL, "ADC1L" },
614 { "ADCL Power", NULL, "BSTL2" },
615 { "ADCL Power", NULL, "BSTL" },
616 { "ADCL Power", NULL, "ADCFEDL" },
617
618 { "ADCR Power", NULL, "LDO16R" },
619 { "ADCR Power", NULL, "ADC1R" },
620 { "ADCR Power", NULL, "BSTR2" },
621 { "ADCR Power", NULL, "BSTR" },
622 { "ADCR Power", NULL, "ADCFEDR" },
623
624 { "AMICL", NULL, "ADC CLK" },
625 { "AMICL", NULL, "ADC Power" },
626 { "AMICL", NULL, "ADCL Power" },
627 { "AMICR", NULL, "ADC CLK" },
628 { "AMICR", NULL, "ADC Power" },
629 { "AMICR", NULL, "ADCR Power" },
630
631 { "PLL1 LDO", NULL, "PLL1 LDO ENABLE" },
632 { "PLL1", NULL, "PLL1 LDO" },
633
634 { "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
635 { "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
636
637 { "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXL" },
638 { "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXR" },
639 { "Stereo1 ADC MIX", NULL, "adc stereo1 filter" },
640 { "adc stereo1 filter", NULL, "PLL1", rt5514_is_sys_clk_from_pll },
641
642 { "Stereo2 DMIC Mux", "DMIC1", "DMIC1" },
643 { "Stereo2 DMIC Mux", "DMIC2", "DMIC2" },
644
645 { "Sto2 ADC MIXL", "DMIC Switch", "Stereo2 DMIC Mux" },
646 { "Sto2 ADC MIXL", "ADC Switch", "AMICL" },
647 { "Sto2 ADC MIXR", "DMIC Switch", "Stereo2 DMIC Mux" },
648 { "Sto2 ADC MIXR", "ADC Switch", "AMICR" },
649
650 { "Stereo2 ADC MIXL", NULL, "Sto2 ADC MIXL" },
651 { "Stereo2 ADC MIXR", NULL, "Sto2 ADC MIXR" },
652
653 { "Stereo2 ADC MIX", NULL, "Stereo2 ADC MIXL" },
654 { "Stereo2 ADC MIX", NULL, "Stereo2 ADC MIXR" },
655 { "Stereo2 ADC MIX", NULL, "adc stereo2 filter" },
656 { "adc stereo2 filter", NULL, "PLL1", rt5514_is_sys_clk_from_pll },
657
658 { "AIF1TX", NULL, "Stereo1 ADC MIX"},
659 { "AIF1TX", NULL, "Stereo2 ADC MIX"},
660};
661
662static int rt5514_hw_params(struct snd_pcm_substream *substream,
663 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
664{
665 struct snd_soc_codec *codec = dai->codec;
666 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
667 int pre_div, bclk_ms, frame_size;
668 unsigned int val_len = 0;
669
670 rt5514->lrck = params_rate(params);
671 pre_div = rl6231_get_clk_info(rt5514->sysclk, rt5514->lrck);
672 if (pre_div < 0) {
673 dev_err(codec->dev, "Unsupported clock setting\n");
674 return -EINVAL;
675 }
676
677 frame_size = snd_soc_params_to_frame_size(params);
678 if (frame_size < 0) {
679 dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
680 return -EINVAL;
681 }
682
683 bclk_ms = frame_size > 32;
684 rt5514->bclk = rt5514->lrck * (32 << bclk_ms);
685
686 dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
687 rt5514->bclk, rt5514->lrck);
688 dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
689 bclk_ms, pre_div, dai->id);
690
691 switch (params_format(params)) {
692 case SNDRV_PCM_FORMAT_S16_LE:
693 break;
694 case SNDRV_PCM_FORMAT_S20_3LE:
695 val_len = RT5514_I2S_DL_20;
696 break;
697 case SNDRV_PCM_FORMAT_S24_LE:
698 val_len = RT5514_I2S_DL_24;
699 break;
700 case SNDRV_PCM_FORMAT_S8:
701 val_len = RT5514_I2S_DL_8;
702 break;
703 default:
704 return -EINVAL;
705 }
706
707 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1, RT5514_I2S_DL_MASK,
708 val_len);
709 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
710 RT5514_CLK_SYS_DIV_OUT_MASK | RT5514_SEL_ADC_OSR_MASK,
711 pre_div << RT5514_CLK_SYS_DIV_OUT_SFT |
712 pre_div << RT5514_SEL_ADC_OSR_SFT);
713
714 return 0;
715}
716
717static int rt5514_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
718{
719 struct snd_soc_codec *codec = dai->codec;
720 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
721 unsigned int reg_val = 0;
722
723 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
724 case SND_SOC_DAIFMT_NB_NF:
725 break;
726
727 case SND_SOC_DAIFMT_NB_IF:
728 reg_val |= RT5514_I2S_LR_INV;
729 break;
730
731 case SND_SOC_DAIFMT_IB_NF:
732 reg_val |= RT5514_I2S_BP_INV;
733 break;
734
735 case SND_SOC_DAIFMT_IB_IF:
736 reg_val |= RT5514_I2S_BP_INV | RT5514_I2S_LR_INV;
737 break;
738
739 default:
740 return -EINVAL;
741 }
742
743 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
744 case SND_SOC_DAIFMT_I2S:
745 break;
746
747 case SND_SOC_DAIFMT_LEFT_J:
748 reg_val |= RT5514_I2S_DF_LEFT;
749 break;
750
751 case SND_SOC_DAIFMT_DSP_A:
752 reg_val |= RT5514_I2S_DF_PCM_A;
753 break;
754
755 case SND_SOC_DAIFMT_DSP_B:
756 reg_val |= RT5514_I2S_DF_PCM_B;
757 break;
758
759 default:
760 return -EINVAL;
761 }
762
763 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1,
764 RT5514_I2S_DF_MASK | RT5514_I2S_BP_MASK | RT5514_I2S_LR_MASK,
765 reg_val);
766
767 return 0;
768}
769
770static int rt5514_set_dai_sysclk(struct snd_soc_dai *dai,
771 int clk_id, unsigned int freq, int dir)
772{
773 struct snd_soc_codec *codec = dai->codec;
774 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
775 unsigned int reg_val = 0;
776
777 if (freq == rt5514->sysclk && clk_id == rt5514->sysclk_src)
778 return 0;
779
780 switch (clk_id) {
781 case RT5514_SCLK_S_MCLK:
782 reg_val |= RT5514_CLK_SYS_PRE_SEL_MCLK;
783 break;
784
785 case RT5514_SCLK_S_PLL1:
786 reg_val |= RT5514_CLK_SYS_PRE_SEL_PLL;
787 break;
788
789 default:
790 dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
791 return -EINVAL;
792 }
793
794 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
795 RT5514_CLK_SYS_PRE_SEL_MASK, reg_val);
796
797 rt5514->sysclk = freq;
798 rt5514->sysclk_src = clk_id;
799
800 dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
801
802 return 0;
803}
804
805static int rt5514_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
806 unsigned int freq_in, unsigned int freq_out)
807{
808 struct snd_soc_codec *codec = dai->codec;
809 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
810 struct rl6231_pll_code pll_code;
811 int ret;
812
813 if (!freq_in || !freq_out) {
814 dev_dbg(codec->dev, "PLL disabled\n");
815
816 rt5514->pll_in = 0;
817 rt5514->pll_out = 0;
818 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
819 RT5514_CLK_SYS_PRE_SEL_MASK,
820 RT5514_CLK_SYS_PRE_SEL_MCLK);
821
822 return 0;
823 }
824
825 if (source == rt5514->pll_src && freq_in == rt5514->pll_in &&
826 freq_out == rt5514->pll_out)
827 return 0;
828
829 switch (source) {
830 case RT5514_PLL1_S_MCLK:
831 regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL,
832 RT5514_PLL_1_SEL_MASK, RT5514_PLL_1_SEL_MCLK);
833 break;
834
835 case RT5514_PLL1_S_BCLK:
836 regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL,
837 RT5514_PLL_1_SEL_MASK, RT5514_PLL_1_SEL_SCLK);
838 break;
839
840 default:
841 dev_err(codec->dev, "Unknown PLL source %d\n", source);
842 return -EINVAL;
843 }
844
845 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
846 if (ret < 0) {
847 dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
848 return ret;
849 }
850
851 dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n",
852 pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
853 pll_code.n_code, pll_code.k_code);
854
855 regmap_write(rt5514->regmap, RT5514_ANA_CTRL_PLL1_1,
856 pll_code.k_code << RT5514_PLL_K_SFT |
857 pll_code.n_code << RT5514_PLL_N_SFT |
858 (pll_code.m_bp ? 0 : pll_code.m_code) << RT5514_PLL_M_SFT);
859 regmap_update_bits(rt5514->regmap, RT5514_ANA_CTRL_PLL1_2,
860 RT5514_PLL_M_BP, pll_code.m_bp << RT5514_PLL_M_BP_SFT);
861
862 rt5514->pll_in = freq_in;
863 rt5514->pll_out = freq_out;
864 rt5514->pll_src = source;
865
866 return 0;
867}
868
869static int rt5514_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
870 unsigned int rx_mask, int slots, int slot_width)
871{
872 struct snd_soc_codec *codec = dai->codec;
873 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
oder_chiou@realtek.come8be3a52017-07-20 12:05:34 +0800874 unsigned int val = 0, val2 = 0;
Oder Chiou4a6180e2016-02-03 19:53:24 +0800875
876 if (rx_mask || tx_mask)
877 val |= RT5514_TDM_MODE;
878
oder_chiou@realtek.come8be3a52017-07-20 12:05:34 +0800879 switch (tx_mask) {
880 case 0x3:
881 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH2 |
882 RT5514_TDM_DOCKING_START_SLOT0;
883 break;
884
885 case 0x30:
886 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH2 |
887 RT5514_TDM_DOCKING_START_SLOT4;
888 break;
889
890 case 0xf:
891 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH4 |
892 RT5514_TDM_DOCKING_START_SLOT0;
893 break;
894
895 case 0xf0:
896 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH4 |
897 RT5514_TDM_DOCKING_START_SLOT4;
898 break;
899
900 default:
901 break;
902 }
903
904
905
Oder Chioud60bc8d2017-05-02 10:42:56 +0800906 switch (slots) {
907 case 4:
Oder Chiou4a6180e2016-02-03 19:53:24 +0800908 val |= RT5514_TDMSLOT_SEL_RX_4CH | RT5514_TDMSLOT_SEL_TX_4CH;
Oder Chioud60bc8d2017-05-02 10:42:56 +0800909 break;
Oder Chiou4a6180e2016-02-03 19:53:24 +0800910
Oder Chioud60bc8d2017-05-02 10:42:56 +0800911 case 6:
912 val |= RT5514_TDMSLOT_SEL_RX_6CH | RT5514_TDMSLOT_SEL_TX_6CH;
913 break;
914
915 case 8:
916 val |= RT5514_TDMSLOT_SEL_RX_8CH | RT5514_TDMSLOT_SEL_TX_8CH;
917 break;
918
919 case 2:
920 default:
921 break;
922 }
Oder Chiou4a6180e2016-02-03 19:53:24 +0800923
924 switch (slot_width) {
925 case 20:
926 val |= RT5514_CH_LEN_RX_20 | RT5514_CH_LEN_TX_20;
927 break;
928
929 case 24:
930 val |= RT5514_CH_LEN_RX_24 | RT5514_CH_LEN_TX_24;
931 break;
932
Oder Chioud60bc8d2017-05-02 10:42:56 +0800933 case 25:
934 val |= RT5514_TDM_MODE2;
935 break;
936
Oder Chiou4a6180e2016-02-03 19:53:24 +0800937 case 32:
938 val |= RT5514_CH_LEN_RX_32 | RT5514_CH_LEN_TX_32;
939 break;
940
941 case 16:
942 default:
943 break;
944 }
945
946 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1, RT5514_TDM_MODE |
947 RT5514_TDMSLOT_SEL_RX_MASK | RT5514_TDMSLOT_SEL_TX_MASK |
Oder Chioud60bc8d2017-05-02 10:42:56 +0800948 RT5514_CH_LEN_RX_MASK | RT5514_CH_LEN_TX_MASK |
949 RT5514_TDM_MODE2, val);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800950
oder_chiou@realtek.come8be3a52017-07-20 12:05:34 +0800951 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL2,
952 RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH_MASK |
953 RT5514_TDM_DOCKING_START_MASK, val2);
954
Oder Chiou4a6180e2016-02-03 19:53:24 +0800955 return 0;
956}
957
Oder Chiouc9506bb2016-06-17 11:02:24 +0800958static int rt5514_set_bias_level(struct snd_soc_codec *codec,
959 enum snd_soc_bias_level level)
960{
961 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
962 int ret;
963
964 switch (level) {
965 case SND_SOC_BIAS_PREPARE:
966 if (IS_ERR(rt5514->mclk))
967 break;
968
969 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_ON) {
970 clk_disable_unprepare(rt5514->mclk);
971 } else {
972 ret = clk_prepare_enable(rt5514->mclk);
973 if (ret)
974 return ret;
975 }
976 break;
977
oder_chiou@realtek.comea4daf82017-07-10 10:03:12 +0800978 case SND_SOC_BIAS_STANDBY:
979 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
980 /*
981 * If the DSP is enabled in start of recording, the DSP
982 * should be disabled, and sync back to normal recording
983 * settings to make sure recording properly.
984 */
985 if (rt5514->dsp_enabled) {
986 rt5514->dsp_enabled = 0;
987 regmap_multi_reg_write(rt5514->i2c_regmap,
988 rt5514_i2c_patch,
989 ARRAY_SIZE(rt5514_i2c_patch));
990 regcache_mark_dirty(rt5514->regmap);
991 regcache_sync(rt5514->regmap);
992 }
993 }
994 break;
995
Oder Chiouc9506bb2016-06-17 11:02:24 +0800996 default:
997 break;
998 }
999
1000 return 0;
1001}
1002
Oder Chiou4a6180e2016-02-03 19:53:24 +08001003static int rt5514_probe(struct snd_soc_codec *codec)
1004{
1005 struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
1006
Oder Chiouc9506bb2016-06-17 11:02:24 +08001007 rt5514->mclk = devm_clk_get(codec->dev, "mclk");
1008 if (PTR_ERR(rt5514->mclk) == -EPROBE_DEFER)
1009 return -EPROBE_DEFER;
1010
Oder Chiou4a6180e2016-02-03 19:53:24 +08001011 rt5514->codec = codec;
1012
1013 return 0;
1014}
1015
1016static int rt5514_i2c_read(void *context, unsigned int reg, unsigned int *val)
1017{
1018 struct i2c_client *client = context;
1019 struct rt5514_priv *rt5514 = i2c_get_clientdata(client);
1020
1021 regmap_read(rt5514->i2c_regmap, reg | RT5514_DSP_MAPPING, val);
1022
1023 return 0;
1024}
1025
1026static int rt5514_i2c_write(void *context, unsigned int reg, unsigned int val)
1027{
1028 struct i2c_client *client = context;
1029 struct rt5514_priv *rt5514 = i2c_get_clientdata(client);
1030
1031 regmap_write(rt5514->i2c_regmap, reg | RT5514_DSP_MAPPING, val);
1032
1033 return 0;
1034}
1035
1036#define RT5514_STEREO_RATES SNDRV_PCM_RATE_8000_192000
1037#define RT5514_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1038 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1039
Gustavo A. R. Silva9894dba2017-07-13 15:37:59 -05001040static const struct snd_soc_dai_ops rt5514_aif_dai_ops = {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001041 .hw_params = rt5514_hw_params,
1042 .set_fmt = rt5514_set_dai_fmt,
1043 .set_sysclk = rt5514_set_dai_sysclk,
1044 .set_pll = rt5514_set_dai_pll,
1045 .set_tdm_slot = rt5514_set_tdm_slot,
1046};
1047
1048struct snd_soc_dai_driver rt5514_dai[] = {
1049 {
1050 .name = "rt5514-aif1",
1051 .id = 0,
1052 .capture = {
1053 .stream_name = "AIF1 Capture",
1054 .channels_min = 1,
1055 .channels_max = 4,
1056 .rates = RT5514_STEREO_RATES,
1057 .formats = RT5514_FORMATS,
1058 },
1059 .ops = &rt5514_aif_dai_ops,
1060 }
1061};
1062
1063static struct snd_soc_codec_driver soc_codec_dev_rt5514 = {
1064 .probe = rt5514_probe,
1065 .idle_bias_off = true,
Oder Chiouc9506bb2016-06-17 11:02:24 +08001066 .set_bias_level = rt5514_set_bias_level,
Kuninori Morimotoa3470392016-08-08 09:21:55 +00001067 .component_driver = {
1068 .controls = rt5514_snd_controls,
1069 .num_controls = ARRAY_SIZE(rt5514_snd_controls),
1070 .dapm_widgets = rt5514_dapm_widgets,
1071 .num_dapm_widgets = ARRAY_SIZE(rt5514_dapm_widgets),
1072 .dapm_routes = rt5514_dapm_routes,
1073 .num_dapm_routes = ARRAY_SIZE(rt5514_dapm_routes),
1074 },
Oder Chiou4a6180e2016-02-03 19:53:24 +08001075};
1076
1077static const struct regmap_config rt5514_i2c_regmap = {
1078 .name = "i2c",
1079 .reg_bits = 32,
1080 .val_bits = 32,
1081
Oder Chiou4a6180e2016-02-03 19:53:24 +08001082 .readable_reg = rt5514_i2c_readable_register,
1083
1084 .cache_type = REGCACHE_NONE,
1085};
1086
1087static const struct regmap_config rt5514_regmap = {
1088 .reg_bits = 16,
1089 .val_bits = 32,
1090
1091 .max_register = RT5514_VENDOR_ID2,
1092 .volatile_reg = rt5514_volatile_register,
1093 .readable_reg = rt5514_readable_register,
1094 .reg_read = rt5514_i2c_read,
1095 .reg_write = rt5514_i2c_write,
1096
1097 .cache_type = REGCACHE_RBTREE,
1098 .reg_defaults = rt5514_reg,
1099 .num_reg_defaults = ARRAY_SIZE(rt5514_reg),
1100 .use_single_rw = true,
1101};
1102
1103static const struct i2c_device_id rt5514_i2c_id[] = {
1104 { "rt5514", 0 },
1105 { }
1106};
1107MODULE_DEVICE_TABLE(i2c, rt5514_i2c_id);
1108
1109#if defined(CONFIG_OF)
1110static const struct of_device_id rt5514_of_match[] = {
1111 { .compatible = "realtek,rt5514", },
1112 {},
1113};
1114MODULE_DEVICE_TABLE(of, rt5514_of_match);
1115#endif
1116
Oder Chiou6d3edf82017-05-15 19:02:07 +08001117#ifdef CONFIG_ACPI
1118static struct acpi_device_id rt5514_acpi_match[] = {
1119 { "10EC5514", 0},
1120 {},
1121};
1122MODULE_DEVICE_TABLE(acpi, rt5514_acpi_match);
1123#endif
1124
Oder Chioua5461fd2016-10-25 19:27:26 +08001125static int rt5514_parse_dt(struct rt5514_priv *rt5514, struct device *dev)
1126{
1127 device_property_read_u32(dev, "realtek,dmic-init-delay-ms",
1128 &rt5514->pdata.dmic_init_delay);
1129
1130 return 0;
1131}
1132
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001133static __maybe_unused int rt5514_i2c_resume(struct device *dev)
1134{
1135 struct rt5514_priv *rt5514 = dev_get_drvdata(dev);
1136 unsigned int val;
1137
1138 /*
1139 * Add a bogus read to avoid rt5514's confusion after s2r in case it
1140 * saw glitches on the i2c lines and thought the other side sent a
1141 * start bit.
1142 */
1143 regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
1144
1145 return 0;
1146}
1147
Oder Chiou4a6180e2016-02-03 19:53:24 +08001148static int rt5514_i2c_probe(struct i2c_client *i2c,
1149 const struct i2c_device_id *id)
1150{
Oder Chioua5461fd2016-10-25 19:27:26 +08001151 struct rt5514_platform_data *pdata = dev_get_platdata(&i2c->dev);
Oder Chiou4a6180e2016-02-03 19:53:24 +08001152 struct rt5514_priv *rt5514;
1153 int ret;
Douglas Anderson0a78b242017-04-14 09:40:31 -07001154 unsigned int val = ~0;
Oder Chiou4a6180e2016-02-03 19:53:24 +08001155
1156 rt5514 = devm_kzalloc(&i2c->dev, sizeof(struct rt5514_priv),
1157 GFP_KERNEL);
1158 if (rt5514 == NULL)
1159 return -ENOMEM;
1160
1161 i2c_set_clientdata(i2c, rt5514);
1162
Oder Chioua5461fd2016-10-25 19:27:26 +08001163 if (pdata)
1164 rt5514->pdata = *pdata;
1165 else if (i2c->dev.of_node)
1166 rt5514_parse_dt(rt5514, &i2c->dev);
1167
Oder Chiou4a6180e2016-02-03 19:53:24 +08001168 rt5514->i2c_regmap = devm_regmap_init_i2c(i2c, &rt5514_i2c_regmap);
1169 if (IS_ERR(rt5514->i2c_regmap)) {
1170 ret = PTR_ERR(rt5514->i2c_regmap);
1171 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1172 ret);
1173 return ret;
1174 }
1175
1176 rt5514->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt5514_regmap);
1177 if (IS_ERR(rt5514->regmap)) {
1178 ret = PTR_ERR(rt5514->regmap);
1179 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1180 ret);
1181 return ret;
1182 }
1183
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001184 /*
1185 * The rt5514 can get confused if the i2c lines glitch together, as
1186 * can happen at bootup as regulators are turned off and on. If it's
1187 * in this glitched state the first i2c read will fail, so we'll give
1188 * it one change to retry.
1189 */
Douglas Anderson0a78b242017-04-14 09:40:31 -07001190 ret = regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001191 if (ret || val != RT5514_DEVICE_ID)
1192 ret = regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
Douglas Anderson0a78b242017-04-14 09:40:31 -07001193 if (ret || val != RT5514_DEVICE_ID) {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001194 dev_err(&i2c->dev,
1195 "Device with ID register %x is not rt5514\n", val);
1196 return -ENODEV;
1197 }
1198
Oder Chiou6eebf352016-06-06 18:33:31 +08001199 ret = regmap_multi_reg_write(rt5514->i2c_regmap, rt5514_i2c_patch,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001200 ARRAY_SIZE(rt5514_i2c_patch));
1201 if (ret != 0)
1202 dev_warn(&i2c->dev, "Failed to apply i2c_regmap patch: %d\n",
1203 ret);
1204
1205 ret = regmap_register_patch(rt5514->regmap, rt5514_patch,
1206 ARRAY_SIZE(rt5514_patch));
1207 if (ret != 0)
1208 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
1209
1210 return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5514,
1211 rt5514_dai, ARRAY_SIZE(rt5514_dai));
1212}
1213
1214static int rt5514_i2c_remove(struct i2c_client *i2c)
1215{
1216 snd_soc_unregister_codec(&i2c->dev);
1217
1218 return 0;
1219}
1220
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001221static const struct dev_pm_ops rt5514_i2_pm_ops = {
1222 SET_SYSTEM_SLEEP_PM_OPS(NULL, rt5514_i2c_resume)
1223};
1224
Douglas Andersond0c02e12017-04-14 09:40:30 -07001225static struct i2c_driver rt5514_i2c_driver = {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001226 .driver = {
1227 .name = "rt5514",
Oder Chiou6d3edf82017-05-15 19:02:07 +08001228 .acpi_match_table = ACPI_PTR(rt5514_acpi_match),
Oder Chiou4a6180e2016-02-03 19:53:24 +08001229 .of_match_table = of_match_ptr(rt5514_of_match),
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001230 .pm = &rt5514_i2_pm_ops,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001231 },
1232 .probe = rt5514_i2c_probe,
1233 .remove = rt5514_i2c_remove,
1234 .id_table = rt5514_i2c_id,
1235};
1236module_i2c_driver(rt5514_i2c_driver);
1237
1238MODULE_DESCRIPTION("ASoC RT5514 driver");
1239MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
1240MODULE_LICENSE("GPL v2");