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