blob: f9ad6e36ab16bc02e33820a5accc155193af56d8 [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_chiou@realtek.comc2bf2522017-07-26 18:32:20 +080034#if IS_ENABLED(CONFIG_SND_SOC_RT5514_SPI)
Oder Chiou6eebf352016-06-06 18:33:31 +080035#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 Chiou6f0a2562018-08-15 14:47:49 +080067 {RT5514_DOWNFILTER0_CTRL3, 0x10000342},
68 {RT5514_DOWNFILTER1_CTRL3, 0x10000342},
Oder Chiou4a6180e2016-02-03 19:53:24 +080069};
70
71static const struct reg_default rt5514_reg[] = {
72 {RT5514_RESET, 0x00000000},
73 {RT5514_PWR_ANA1, 0x00808880},
74 {RT5514_PWR_ANA2, 0x00220000},
75 {RT5514_I2S_CTRL1, 0x00000330},
76 {RT5514_I2S_CTRL2, 0x20000000},
77 {RT5514_VAD_CTRL6, 0xc00007d2},
78 {RT5514_EXT_VAD_CTRL, 0x80000080},
79 {RT5514_DIG_IO_CTRL, 0x00000040},
80 {RT5514_PAD_CTRL1, 0x00804000},
81 {RT5514_DMIC_DATA_CTRL, 0x00000005},
82 {RT5514_DIG_SOURCE_CTRL, 0x00000002},
83 {RT5514_SRC_CTRL, 0x44000eee},
84 {RT5514_DOWNFILTER2_CTRL1, 0x0000882f},
85 {RT5514_PLL_SOURCE_CTRL, 0x00000004},
86 {RT5514_CLK_CTRL1, 0x38020041},
87 {RT5514_CLK_CTRL2, 0x00000000},
88 {RT5514_PLL3_CALIB_CTRL1, 0x00400200},
89 {RT5514_PLL3_CALIB_CTRL5, 0x40220012},
90 {RT5514_DELAY_BUF_CTRL1, 0x7fff006a},
91 {RT5514_DELAY_BUF_CTRL3, 0x00000000},
oder_chiou@realtek.com5ef5ac82018-03-30 15:41:55 +080092 {RT5514_ASRC_IN_CTRL1, 0x00000003},
Oder Chiou4a6180e2016-02-03 19:53:24 +080093 {RT5514_DOWNFILTER0_CTRL1, 0x00020c2f},
94 {RT5514_DOWNFILTER0_CTRL2, 0x00020c2f},
Oder Chiou6f0a2562018-08-15 14:47:49 +080095 {RT5514_DOWNFILTER0_CTRL3, 0x10000342},
Oder Chiou4a6180e2016-02-03 19:53:24 +080096 {RT5514_DOWNFILTER1_CTRL1, 0x00020c2f},
97 {RT5514_DOWNFILTER1_CTRL2, 0x00020c2f},
Oder Chiou6f0a2562018-08-15 14:47:49 +080098 {RT5514_DOWNFILTER1_CTRL3, 0x10000342},
Oder Chiou4a6180e2016-02-03 19:53:24 +080099 {RT5514_ANA_CTRL_LDO10, 0x00028604},
100 {RT5514_ANA_CTRL_LDO18_16, 0x02000345},
101 {RT5514_ANA_CTRL_ADC12, 0x0000a2a8},
102 {RT5514_ANA_CTRL_ADC21, 0x00001180},
103 {RT5514_ANA_CTRL_ADC22, 0x0000aaa8},
104 {RT5514_ANA_CTRL_ADC23, 0x00151427},
105 {RT5514_ANA_CTRL_MICBST, 0x00002000},
106 {RT5514_ANA_CTRL_ADCFED, 0x00000800},
107 {RT5514_ANA_CTRL_INBUF, 0x00000143},
108 {RT5514_ANA_CTRL_VREF, 0x00008d50},
109 {RT5514_ANA_CTRL_PLL3, 0x0000000e},
110 {RT5514_ANA_CTRL_PLL1_1, 0x00000000},
111 {RT5514_ANA_CTRL_PLL1_2, 0x00030220},
112 {RT5514_DMIC_LP_CTRL, 0x00000000},
113 {RT5514_MISC_CTRL_DSP, 0x00000000},
114 {RT5514_DSP_CTRL1, 0x00055149},
115 {RT5514_DSP_CTRL3, 0x00000006},
116 {RT5514_DSP_CTRL4, 0x00000001},
117 {RT5514_VENDOR_ID1, 0x00000001},
118 {RT5514_VENDOR_ID2, 0x10ec5514},
119};
120
Oder Chiou6eebf352016-06-06 18:33:31 +0800121static void rt5514_enable_dsp_prepare(struct rt5514_priv *rt5514)
122{
123 /* Reset */
124 regmap_write(rt5514->i2c_regmap, 0x18002000, 0x000010ec);
125 /* LDO_I_limit */
126 regmap_write(rt5514->i2c_regmap, 0x18002200, 0x00028604);
127 /* I2C bypass enable */
128 regmap_write(rt5514->i2c_regmap, 0xfafafafa, 0x00000001);
129 /* mini-core reset */
130 regmap_write(rt5514->i2c_regmap, 0x18002f00, 0x0005514b);
131 regmap_write(rt5514->i2c_regmap, 0x18002f00, 0x00055149);
132 /* I2C bypass disable */
133 regmap_write(rt5514->i2c_regmap, 0xfafafafa, 0x00000000);
134 /* PIN config */
135 regmap_write(rt5514->i2c_regmap, 0x18002070, 0x00000040);
136 /* PLL3(QN)=RCOSC*(10+2) */
137 regmap_write(rt5514->i2c_regmap, 0x18002240, 0x0000000a);
138 /* PLL3 source=RCOSC, fsi=rt_clk */
139 regmap_write(rt5514->i2c_regmap, 0x18002100, 0x0000000b);
140 /* Power on RCOSC, pll3 */
141 regmap_write(rt5514->i2c_regmap, 0x18002004, 0x00808b81);
142 /* DSP clk source = pll3, ENABLE DSP clk */
143 regmap_write(rt5514->i2c_regmap, 0x18002f08, 0x00000005);
144 /* Enable DSP clk auto switch */
145 regmap_write(rt5514->i2c_regmap, 0x18001114, 0x00000001);
146 /* Reduce DSP power */
147 regmap_write(rt5514->i2c_regmap, 0x18001118, 0x00000001);
148}
149
Oder Chiou4a6180e2016-02-03 19:53:24 +0800150static bool rt5514_volatile_register(struct device *dev, unsigned int reg)
151{
152 switch (reg) {
153 case RT5514_VENDOR_ID1:
154 case RT5514_VENDOR_ID2:
155 return true;
156
157 default:
158 return false;
159 }
160}
161
162static bool rt5514_readable_register(struct device *dev, unsigned int reg)
163{
164 switch (reg) {
165 case RT5514_RESET:
166 case RT5514_PWR_ANA1:
167 case RT5514_PWR_ANA2:
168 case RT5514_I2S_CTRL1:
169 case RT5514_I2S_CTRL2:
170 case RT5514_VAD_CTRL6:
171 case RT5514_EXT_VAD_CTRL:
172 case RT5514_DIG_IO_CTRL:
173 case RT5514_PAD_CTRL1:
174 case RT5514_DMIC_DATA_CTRL:
175 case RT5514_DIG_SOURCE_CTRL:
176 case RT5514_SRC_CTRL:
177 case RT5514_DOWNFILTER2_CTRL1:
178 case RT5514_PLL_SOURCE_CTRL:
179 case RT5514_CLK_CTRL1:
180 case RT5514_CLK_CTRL2:
181 case RT5514_PLL3_CALIB_CTRL1:
182 case RT5514_PLL3_CALIB_CTRL5:
183 case RT5514_DELAY_BUF_CTRL1:
184 case RT5514_DELAY_BUF_CTRL3:
oder_chiou@realtek.com5ef5ac82018-03-30 15:41:55 +0800185 case RT5514_ASRC_IN_CTRL1:
Oder Chiou4a6180e2016-02-03 19:53:24 +0800186 case RT5514_DOWNFILTER0_CTRL1:
187 case RT5514_DOWNFILTER0_CTRL2:
188 case RT5514_DOWNFILTER0_CTRL3:
189 case RT5514_DOWNFILTER1_CTRL1:
190 case RT5514_DOWNFILTER1_CTRL2:
191 case RT5514_DOWNFILTER1_CTRL3:
192 case RT5514_ANA_CTRL_LDO10:
193 case RT5514_ANA_CTRL_LDO18_16:
194 case RT5514_ANA_CTRL_ADC12:
195 case RT5514_ANA_CTRL_ADC21:
196 case RT5514_ANA_CTRL_ADC22:
197 case RT5514_ANA_CTRL_ADC23:
198 case RT5514_ANA_CTRL_MICBST:
199 case RT5514_ANA_CTRL_ADCFED:
200 case RT5514_ANA_CTRL_INBUF:
201 case RT5514_ANA_CTRL_VREF:
202 case RT5514_ANA_CTRL_PLL3:
203 case RT5514_ANA_CTRL_PLL1_1:
204 case RT5514_ANA_CTRL_PLL1_2:
205 case RT5514_DMIC_LP_CTRL:
206 case RT5514_MISC_CTRL_DSP:
207 case RT5514_DSP_CTRL1:
208 case RT5514_DSP_CTRL3:
209 case RT5514_DSP_CTRL4:
210 case RT5514_VENDOR_ID1:
211 case RT5514_VENDOR_ID2:
212 return true;
213
214 default:
215 return false;
216 }
217}
218
219static bool rt5514_i2c_readable_register(struct device *dev,
220 unsigned int reg)
221{
222 switch (reg) {
223 case RT5514_DSP_MAPPING | RT5514_RESET:
224 case RT5514_DSP_MAPPING | RT5514_PWR_ANA1:
225 case RT5514_DSP_MAPPING | RT5514_PWR_ANA2:
226 case RT5514_DSP_MAPPING | RT5514_I2S_CTRL1:
227 case RT5514_DSP_MAPPING | RT5514_I2S_CTRL2:
228 case RT5514_DSP_MAPPING | RT5514_VAD_CTRL6:
229 case RT5514_DSP_MAPPING | RT5514_EXT_VAD_CTRL:
230 case RT5514_DSP_MAPPING | RT5514_DIG_IO_CTRL:
231 case RT5514_DSP_MAPPING | RT5514_PAD_CTRL1:
232 case RT5514_DSP_MAPPING | RT5514_DMIC_DATA_CTRL:
233 case RT5514_DSP_MAPPING | RT5514_DIG_SOURCE_CTRL:
234 case RT5514_DSP_MAPPING | RT5514_SRC_CTRL:
235 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER2_CTRL1:
236 case RT5514_DSP_MAPPING | RT5514_PLL_SOURCE_CTRL:
237 case RT5514_DSP_MAPPING | RT5514_CLK_CTRL1:
238 case RT5514_DSP_MAPPING | RT5514_CLK_CTRL2:
239 case RT5514_DSP_MAPPING | RT5514_PLL3_CALIB_CTRL1:
240 case RT5514_DSP_MAPPING | RT5514_PLL3_CALIB_CTRL5:
241 case RT5514_DSP_MAPPING | RT5514_DELAY_BUF_CTRL1:
242 case RT5514_DSP_MAPPING | RT5514_DELAY_BUF_CTRL3:
oder_chiou@realtek.com5ef5ac82018-03-30 15:41:55 +0800243 case RT5514_DSP_MAPPING | RT5514_ASRC_IN_CTRL1:
Oder Chiou4a6180e2016-02-03 19:53:24 +0800244 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL1:
245 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL2:
246 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL3:
247 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL1:
248 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL2:
249 case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL3:
250 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_LDO10:
251 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_LDO18_16:
252 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC12:
253 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC21:
254 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC22:
255 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC23:
256 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_MICBST:
257 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADCFED:
258 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_INBUF:
259 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_VREF:
260 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL3:
261 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL1_1:
262 case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL1_2:
263 case RT5514_DSP_MAPPING | RT5514_DMIC_LP_CTRL:
264 case RT5514_DSP_MAPPING | RT5514_MISC_CTRL_DSP:
265 case RT5514_DSP_MAPPING | RT5514_DSP_CTRL1:
266 case RT5514_DSP_MAPPING | RT5514_DSP_CTRL3:
267 case RT5514_DSP_MAPPING | RT5514_DSP_CTRL4:
268 case RT5514_DSP_MAPPING | RT5514_VENDOR_ID1:
269 case RT5514_DSP_MAPPING | RT5514_VENDOR_ID2:
270 return true;
271
272 default:
273 return false;
274 }
275}
276
277/* {-3, 0, +3, +4.5, +7.5, +9.5, +12, +14, +17} dB */
278static const DECLARE_TLV_DB_RANGE(bst_tlv,
279 0, 2, TLV_DB_SCALE_ITEM(-300, 300, 0),
280 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
281 4, 4, TLV_DB_SCALE_ITEM(750, 0, 0),
282 5, 5, TLV_DB_SCALE_ITEM(950, 0, 0),
283 6, 6, TLV_DB_SCALE_ITEM(1200, 0, 0),
284 7, 7, TLV_DB_SCALE_ITEM(1400, 0, 0),
285 8, 8, TLV_DB_SCALE_ITEM(1700, 0, 0)
286);
287
Oder Chioua1338a72016-09-07 11:07:49 +0800288static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1725, 75, 0);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800289
Oder Chiou6eebf352016-06-06 18:33:31 +0800290static int rt5514_dsp_voice_wake_up_get(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *ucontrol)
292{
293 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
294 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
295
296 ucontrol->value.integer.value[0] = rt5514->dsp_enabled;
297
298 return 0;
299}
300
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800301static int rt5514_calibration(struct rt5514_priv *rt5514, bool on)
302{
303 if (on) {
304 regmap_write(rt5514->regmap, RT5514_ANA_CTRL_PLL3, 0x0000000a);
305 regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL, 0xf,
306 0xa);
307 regmap_update_bits(rt5514->regmap, RT5514_PWR_ANA1, 0x301,
308 0x301);
309 regmap_write(rt5514->regmap, RT5514_PLL3_CALIB_CTRL4,
310 0x80000000 | rt5514->pll3_cal_value);
311 regmap_write(rt5514->regmap, RT5514_PLL3_CALIB_CTRL1,
312 0x8bb80800);
313 regmap_update_bits(rt5514->regmap, RT5514_PLL3_CALIB_CTRL5,
314 0xc0000000, 0x80000000);
315 regmap_update_bits(rt5514->regmap, RT5514_PLL3_CALIB_CTRL5,
316 0xc0000000, 0xc0000000);
317 } else {
318 regmap_update_bits(rt5514->regmap, RT5514_PLL3_CALIB_CTRL5,
319 0xc0000000, 0x40000000);
320 regmap_update_bits(rt5514->regmap, RT5514_PWR_ANA1, 0x301, 0);
321 regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL, 0xf,
322 0x4);
323 }
324
325 return 0;
326}
327
Oder Chiou6eebf352016-06-06 18:33:31 +0800328static int rt5514_dsp_voice_wake_up_put(struct snd_kcontrol *kcontrol,
329 struct snd_ctl_elem_value *ucontrol)
330{
331 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
332 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou6eebf352016-06-06 18:33:31 +0800333 const struct firmware *fw = NULL;
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800334 u8 buf[8];
Oder Chiou6eebf352016-06-06 18:33:31 +0800335
336 if (ucontrol->value.integer.value[0] == rt5514->dsp_enabled)
337 return 0;
338
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000339 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
Oder Chiou6eebf352016-06-06 18:33:31 +0800340 rt5514->dsp_enabled = ucontrol->value.integer.value[0];
341
342 if (rt5514->dsp_enabled) {
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800343 if (rt5514->pdata.dsp_calib_clk_name &&
344 !IS_ERR(rt5514->dsp_calib_clk)) {
345 if (clk_set_rate(rt5514->dsp_calib_clk,
346 rt5514->pdata.dsp_calib_clk_rate))
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000347 dev_err(component->dev,
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800348 "Can't set rate for mclk");
349
350 if (clk_prepare_enable(rt5514->dsp_calib_clk))
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000351 dev_err(component->dev,
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800352 "Can't enable dsp_calib_clk");
353
354 rt5514_calibration(rt5514, true);
355
356 msleep(20);
357#if IS_ENABLED(CONFIG_SND_SOC_RT5514_SPI)
358 rt5514_spi_burst_read(RT5514_PLL3_CALIB_CTRL6 |
Geert Uytterhoeven29838a52018-01-30 14:19:42 +0100359 RT5514_DSP_MAPPING, buf, sizeof(buf));
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800360#else
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000361 dev_err(component->dev, "There is no SPI driver for"
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800362 " loading the firmware\n");
Geert Uytterhoeven890861a2018-01-30 14:19:43 +0100363 memset(buf, 0, sizeof(buf));
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800364#endif
365 rt5514->pll3_cal_value = buf[0] | buf[1] << 8 |
366 buf[2] << 16 | buf[3] << 24;
367
368 rt5514_calibration(rt5514, false);
369 clk_disable_unprepare(rt5514->dsp_calib_clk);
370 }
371
Oder Chiou6eebf352016-06-06 18:33:31 +0800372 rt5514_enable_dsp_prepare(rt5514);
373
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000374 request_firmware(&fw, RT5514_FIRMWARE1, component->dev);
Oder Chiou6eebf352016-06-06 18:33:31 +0800375 if (fw) {
oder_chiou@realtek.comc2bf2522017-07-26 18:32:20 +0800376#if IS_ENABLED(CONFIG_SND_SOC_RT5514_SPI)
Oder Chiou6eebf352016-06-06 18:33:31 +0800377 rt5514_spi_burst_write(0x4ff60000, fw->data,
378 ((fw->size/8)+1)*8);
379#else
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000380 dev_err(component->dev, "There is no SPI driver for"
Oder Chiou6eebf352016-06-06 18:33:31 +0800381 " loading the firmware\n");
382#endif
383 release_firmware(fw);
384 fw = NULL;
385 }
386
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000387 request_firmware(&fw, RT5514_FIRMWARE2, component->dev);
Oder Chiou6eebf352016-06-06 18:33:31 +0800388 if (fw) {
oder_chiou@realtek.comc2bf2522017-07-26 18:32:20 +0800389#if IS_ENABLED(CONFIG_SND_SOC_RT5514_SPI)
Oder Chiou6eebf352016-06-06 18:33:31 +0800390 rt5514_spi_burst_write(0x4ffc0000, fw->data,
391 ((fw->size/8)+1)*8);
392#else
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000393 dev_err(component->dev, "There is no SPI driver for"
Oder Chiou6eebf352016-06-06 18:33:31 +0800394 " loading the firmware\n");
395#endif
396 release_firmware(fw);
397 fw = NULL;
398 }
399
400 /* DSP run */
401 regmap_write(rt5514->i2c_regmap, 0x18002f00,
402 0x00055148);
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +0800403
404 if (rt5514->pdata.dsp_calib_clk_name &&
405 !IS_ERR(rt5514->dsp_calib_clk)) {
406 msleep(20);
407
408 regmap_write(rt5514->i2c_regmap, 0x1800211c,
409 rt5514->pll3_cal_value);
410 regmap_write(rt5514->i2c_regmap, 0x18002124,
411 0x00220012);
412 regmap_write(rt5514->i2c_regmap, 0x18002124,
413 0x80220042);
414 regmap_write(rt5514->i2c_regmap, 0x18002124,
415 0xe0220042);
416 }
Oder Chiou6eebf352016-06-06 18:33:31 +0800417 } else {
418 regmap_multi_reg_write(rt5514->i2c_regmap,
419 rt5514_i2c_patch, ARRAY_SIZE(rt5514_i2c_patch));
420 regcache_mark_dirty(rt5514->regmap);
421 regcache_sync(rt5514->regmap);
422 }
423 }
424
425 return 0;
426}
427
Oder Chiou4a6180e2016-02-03 19:53:24 +0800428static const struct snd_kcontrol_new rt5514_snd_controls[] = {
429 SOC_DOUBLE_TLV("MIC Boost Volume", RT5514_ANA_CTRL_MICBST,
430 RT5514_SEL_BSTL_SFT, RT5514_SEL_BSTR_SFT, 8, 0, bst_tlv),
431 SOC_DOUBLE_R_TLV("ADC1 Capture Volume", RT5514_DOWNFILTER0_CTRL1,
Oder Chioua1338a72016-09-07 11:07:49 +0800432 RT5514_DOWNFILTER0_CTRL2, RT5514_AD_GAIN_SFT, 63, 0,
Oder Chiou4a6180e2016-02-03 19:53:24 +0800433 adc_vol_tlv),
434 SOC_DOUBLE_R_TLV("ADC2 Capture Volume", RT5514_DOWNFILTER1_CTRL1,
Oder Chioua1338a72016-09-07 11:07:49 +0800435 RT5514_DOWNFILTER1_CTRL2, RT5514_AD_GAIN_SFT, 63, 0,
Oder Chiou4a6180e2016-02-03 19:53:24 +0800436 adc_vol_tlv),
Oder Chiou6eebf352016-06-06 18:33:31 +0800437 SOC_SINGLE_EXT("DSP Voice Wake Up", SND_SOC_NOPM, 0, 1, 0,
438 rt5514_dsp_voice_wake_up_get, rt5514_dsp_voice_wake_up_put),
Oder Chiou4a6180e2016-02-03 19:53:24 +0800439};
440
441/* ADC Mixer*/
442static const struct snd_kcontrol_new rt5514_sto1_adc_l_mix[] = {
443 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER0_CTRL1,
444 RT5514_AD_DMIC_MIX_BIT, 1, 1),
445 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER0_CTRL1,
446 RT5514_AD_AD_MIX_BIT, 1, 1),
447};
448
449static const struct snd_kcontrol_new rt5514_sto1_adc_r_mix[] = {
450 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER0_CTRL2,
451 RT5514_AD_DMIC_MIX_BIT, 1, 1),
452 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER0_CTRL2,
453 RT5514_AD_AD_MIX_BIT, 1, 1),
454};
455
456static const struct snd_kcontrol_new rt5514_sto2_adc_l_mix[] = {
457 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER1_CTRL1,
458 RT5514_AD_DMIC_MIX_BIT, 1, 1),
459 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER1_CTRL1,
460 RT5514_AD_AD_MIX_BIT, 1, 1),
461};
462
463static const struct snd_kcontrol_new rt5514_sto2_adc_r_mix[] = {
464 SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER1_CTRL2,
465 RT5514_AD_DMIC_MIX_BIT, 1, 1),
466 SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER1_CTRL2,
467 RT5514_AD_AD_MIX_BIT, 1, 1),
468};
469
470/* DMIC Source */
471static const char * const rt5514_dmic_src[] = {
472 "DMIC1", "DMIC2"
473};
474
Arnd Bergmann03ba7912017-05-11 13:44:38 +0200475static SOC_ENUM_SINGLE_DECL(
Oder Chiou4a6180e2016-02-03 19:53:24 +0800476 rt5514_stereo1_dmic_enum, RT5514_DIG_SOURCE_CTRL,
477 RT5514_AD0_DMIC_INPUT_SEL_SFT, rt5514_dmic_src);
478
479static const struct snd_kcontrol_new rt5514_sto1_dmic_mux =
480 SOC_DAPM_ENUM("Stereo1 DMIC Source", rt5514_stereo1_dmic_enum);
481
Arnd Bergmann03ba7912017-05-11 13:44:38 +0200482static SOC_ENUM_SINGLE_DECL(
Oder Chiou4a6180e2016-02-03 19:53:24 +0800483 rt5514_stereo2_dmic_enum, RT5514_DIG_SOURCE_CTRL,
484 RT5514_AD1_DMIC_INPUT_SEL_SFT, rt5514_dmic_src);
485
486static const struct snd_kcontrol_new rt5514_sto2_dmic_mux =
487 SOC_DAPM_ENUM("Stereo2 DMIC Source", rt5514_stereo2_dmic_enum);
488
489/**
490 * rt5514_calc_dmic_clk - Calculate the frequency divider parameter of dmic.
491 *
Pierre-Louis Bossartdc22a402019-01-04 20:02:32 -0600492 * @component: only used for dev_warn
Oder Chiou4a6180e2016-02-03 19:53:24 +0800493 * @rate: base clock rate.
494 *
495 * Choose divider parameter that gives the highest possible DMIC frequency in
496 * 1MHz - 3MHz range.
497 */
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000498static int rt5514_calc_dmic_clk(struct snd_soc_component *component, int rate)
Oder Chiou4a6180e2016-02-03 19:53:24 +0800499{
500 int div[] = {2, 3, 4, 8, 12, 16, 24, 32};
501 int i;
502
503 if (rate < 1000000 * div[0]) {
504 pr_warn("Base clock rate %d is too low\n", rate);
505 return -EINVAL;
506 }
507
508 for (i = 0; i < ARRAY_SIZE(div); i++) {
509 /* find divider that gives DMIC frequency below 3.072MHz */
510 if (3072000 * div[i] >= rate)
511 return i;
512 }
513
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000514 dev_warn(component->dev, "Base clock rate %d is too high\n", rate);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800515 return -EINVAL;
516}
517
518static int rt5514_set_dmic_clk(struct snd_soc_dapm_widget *w,
519 struct snd_kcontrol *kcontrol, int event)
520{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000521 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
522 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800523 int idx;
524
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000525 idx = rt5514_calc_dmic_clk(component, rt5514->sysclk);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800526 if (idx < 0)
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000527 dev_err(component->dev, "Failed to set DMIC clock\n");
Oder Chiou4a6180e2016-02-03 19:53:24 +0800528 else
529 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL1,
530 RT5514_CLK_DMIC_OUT_SEL_MASK,
531 idx << RT5514_CLK_DMIC_OUT_SEL_SFT);
532
Oder Chioua5461fd2016-10-25 19:27:26 +0800533 if (rt5514->pdata.dmic_init_delay)
534 msleep(rt5514->pdata.dmic_init_delay);
535
Oder Chiou4a6180e2016-02-03 19:53:24 +0800536 return idx;
537}
538
539static int rt5514_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
540 struct snd_soc_dapm_widget *sink)
541{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000542 struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
543 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800544
545 if (rt5514->sysclk_src == RT5514_SCLK_S_PLL1)
546 return 1;
547 else
548 return 0;
549}
550
oder_chiou@realtek.com1628ef82017-07-24 15:34:23 +0800551static int rt5514_i2s_use_asrc(struct snd_soc_dapm_widget *source,
552 struct snd_soc_dapm_widget *sink)
Oder Chiou6eebf352016-06-06 18:33:31 +0800553{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000554 struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
555 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou6eebf352016-06-06 18:33:31 +0800556
oder_chiou@realtek.com1628ef82017-07-24 15:34:23 +0800557 return (rt5514->sysclk > rt5514->lrck * 384);
Oder Chiou6eebf352016-06-06 18:33:31 +0800558}
559
Oder Chiou4a6180e2016-02-03 19:53:24 +0800560static const struct snd_soc_dapm_widget rt5514_dapm_widgets[] = {
561 /* Input Lines */
562 SND_SOC_DAPM_INPUT("DMIC1L"),
563 SND_SOC_DAPM_INPUT("DMIC1R"),
564 SND_SOC_DAPM_INPUT("DMIC2L"),
565 SND_SOC_DAPM_INPUT("DMIC2R"),
566
567 SND_SOC_DAPM_INPUT("AMICL"),
568 SND_SOC_DAPM_INPUT("AMICR"),
569
570 SND_SOC_DAPM_PGA("DMIC1", SND_SOC_NOPM, 0, 0, NULL, 0),
571 SND_SOC_DAPM_PGA("DMIC2", SND_SOC_NOPM, 0, 0, NULL, 0),
572
oder_chiou@realtek.com254beff2017-11-24 16:11:22 +0800573 SND_SOC_DAPM_SUPPLY_S("DMIC CLK", 1, SND_SOC_NOPM, 0, 0,
Oder Chiou4a6180e2016-02-03 19:53:24 +0800574 rt5514_set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
575
576 SND_SOC_DAPM_SUPPLY("ADC CLK", RT5514_CLK_CTRL1,
577 RT5514_CLK_AD_ANA1_EN_BIT, 0, NULL, 0),
578
579 SND_SOC_DAPM_SUPPLY("LDO18 IN", RT5514_PWR_ANA1,
580 RT5514_POW_LDO18_IN_BIT, 0, NULL, 0),
581 SND_SOC_DAPM_SUPPLY("LDO18 ADC", RT5514_PWR_ANA1,
582 RT5514_POW_LDO18_ADC_BIT, 0, NULL, 0),
583 SND_SOC_DAPM_SUPPLY("LDO21", RT5514_PWR_ANA1, RT5514_POW_LDO21_BIT, 0,
584 NULL, 0),
585 SND_SOC_DAPM_SUPPLY("BG LDO18 IN", RT5514_PWR_ANA1,
586 RT5514_POW_BG_LDO18_IN_BIT, 0, NULL, 0),
587 SND_SOC_DAPM_SUPPLY("BG LDO21", RT5514_PWR_ANA1,
588 RT5514_POW_BG_LDO21_BIT, 0, NULL, 0),
589 SND_SOC_DAPM_SUPPLY("BG MBIAS", RT5514_PWR_ANA2,
590 RT5514_POW_BG_MBIAS_BIT, 0, NULL, 0),
591 SND_SOC_DAPM_SUPPLY("MBIAS", RT5514_PWR_ANA2, RT5514_POW_MBIAS_BIT, 0,
592 NULL, 0),
593 SND_SOC_DAPM_SUPPLY("VREF2", RT5514_PWR_ANA2, RT5514_POW_VREF2_BIT, 0,
594 NULL, 0),
595 SND_SOC_DAPM_SUPPLY("VREF1", RT5514_PWR_ANA2, RT5514_POW_VREF1_BIT, 0,
596 NULL, 0),
597 SND_SOC_DAPM_SUPPLY("ADC Power", SND_SOC_NOPM, 0, 0, NULL, 0),
598
599
600 SND_SOC_DAPM_SUPPLY("LDO16L", RT5514_PWR_ANA2, RT5514_POWL_LDO16_BIT, 0,
601 NULL, 0),
602 SND_SOC_DAPM_SUPPLY("ADC1L", RT5514_PWR_ANA2, RT5514_POW_ADC1_L_BIT, 0,
603 NULL, 0),
604 SND_SOC_DAPM_SUPPLY("BSTL2", RT5514_PWR_ANA2, RT5514_POW2_BSTL_BIT, 0,
605 NULL, 0),
606 SND_SOC_DAPM_SUPPLY("BSTL", RT5514_PWR_ANA2, RT5514_POW_BSTL_BIT, 0,
607 NULL, 0),
608 SND_SOC_DAPM_SUPPLY("ADCFEDL", RT5514_PWR_ANA2, RT5514_POW_ADCFEDL_BIT,
609 0, NULL, 0),
610 SND_SOC_DAPM_SUPPLY("ADCL Power", SND_SOC_NOPM, 0, 0, NULL, 0),
611
612 SND_SOC_DAPM_SUPPLY("LDO16R", RT5514_PWR_ANA2, RT5514_POWR_LDO16_BIT, 0,
613 NULL, 0),
614 SND_SOC_DAPM_SUPPLY("ADC1R", RT5514_PWR_ANA2, RT5514_POW_ADC1_R_BIT, 0,
615 NULL, 0),
616 SND_SOC_DAPM_SUPPLY("BSTR2", RT5514_PWR_ANA2, RT5514_POW2_BSTR_BIT, 0,
617 NULL, 0),
618 SND_SOC_DAPM_SUPPLY("BSTR", RT5514_PWR_ANA2, RT5514_POW_BSTR_BIT, 0,
619 NULL, 0),
620 SND_SOC_DAPM_SUPPLY("ADCFEDR", RT5514_PWR_ANA2, RT5514_POW_ADCFEDR_BIT,
621 0, NULL, 0),
622 SND_SOC_DAPM_SUPPLY("ADCR Power", SND_SOC_NOPM, 0, 0, NULL, 0),
623
624 SND_SOC_DAPM_SUPPLY("PLL1 LDO ENABLE", RT5514_ANA_CTRL_PLL1_2,
625 RT5514_EN_LDO_PLL1_BIT, 0, NULL, 0),
626 SND_SOC_DAPM_SUPPLY("PLL1 LDO", RT5514_PWR_ANA2,
627 RT5514_POW_PLL1_LDO_BIT, 0, NULL, 0),
628 SND_SOC_DAPM_SUPPLY("PLL1", RT5514_PWR_ANA2, RT5514_POW_PLL1_BIT, 0,
629 NULL, 0),
oder_chiou@realtek.com1628ef82017-07-24 15:34:23 +0800630 SND_SOC_DAPM_SUPPLY_S("ASRC AD1", 1, RT5514_CLK_CTRL2,
631 RT5514_CLK_AD0_ASRC_EN_BIT, 0, NULL, 0),
632 SND_SOC_DAPM_SUPPLY_S("ASRC AD2", 1, RT5514_CLK_CTRL2,
633 RT5514_CLK_AD1_ASRC_EN_BIT, 0, NULL, 0),
Oder Chiou4a6180e2016-02-03 19:53:24 +0800634
635 /* ADC Mux */
636 SND_SOC_DAPM_MUX("Stereo1 DMIC Mux", SND_SOC_NOPM, 0, 0,
637 &rt5514_sto1_dmic_mux),
638 SND_SOC_DAPM_MUX("Stereo2 DMIC Mux", SND_SOC_NOPM, 0, 0,
639 &rt5514_sto2_dmic_mux),
640
641 /* ADC Mixer */
642 SND_SOC_DAPM_SUPPLY("adc stereo1 filter", RT5514_CLK_CTRL1,
643 RT5514_CLK_AD0_EN_BIT, 0, NULL, 0),
644 SND_SOC_DAPM_SUPPLY("adc stereo2 filter", RT5514_CLK_CTRL1,
645 RT5514_CLK_AD1_EN_BIT, 0, NULL, 0),
646
647 SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", SND_SOC_NOPM, 0, 0,
648 rt5514_sto1_adc_l_mix, ARRAY_SIZE(rt5514_sto1_adc_l_mix)),
649 SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", SND_SOC_NOPM, 0, 0,
650 rt5514_sto1_adc_r_mix, ARRAY_SIZE(rt5514_sto1_adc_r_mix)),
651 SND_SOC_DAPM_MIXER("Sto2 ADC MIXL", SND_SOC_NOPM, 0, 0,
652 rt5514_sto2_adc_l_mix, ARRAY_SIZE(rt5514_sto2_adc_l_mix)),
653 SND_SOC_DAPM_MIXER("Sto2 ADC MIXR", SND_SOC_NOPM, 0, 0,
654 rt5514_sto2_adc_r_mix, ARRAY_SIZE(rt5514_sto2_adc_r_mix)),
655
656 SND_SOC_DAPM_ADC("Stereo1 ADC MIXL", NULL, RT5514_DOWNFILTER0_CTRL1,
657 RT5514_AD_AD_MUTE_BIT, 1),
658 SND_SOC_DAPM_ADC("Stereo1 ADC MIXR", NULL, RT5514_DOWNFILTER0_CTRL2,
659 RT5514_AD_AD_MUTE_BIT, 1),
660 SND_SOC_DAPM_ADC("Stereo2 ADC MIXL", NULL, RT5514_DOWNFILTER1_CTRL1,
661 RT5514_AD_AD_MUTE_BIT, 1),
662 SND_SOC_DAPM_ADC("Stereo2 ADC MIXR", NULL, RT5514_DOWNFILTER1_CTRL2,
663 RT5514_AD_AD_MUTE_BIT, 1),
664
665 /* ADC PGA */
666 SND_SOC_DAPM_PGA("Stereo1 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
667 SND_SOC_DAPM_PGA("Stereo2 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
668
669 /* Audio Interface */
670 SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
671};
672
673static const struct snd_soc_dapm_route rt5514_dapm_routes[] = {
674 { "DMIC1", NULL, "DMIC1L" },
675 { "DMIC1", NULL, "DMIC1R" },
676 { "DMIC2", NULL, "DMIC2L" },
677 { "DMIC2", NULL, "DMIC2R" },
678
679 { "DMIC1L", NULL, "DMIC CLK" },
680 { "DMIC1R", NULL, "DMIC CLK" },
681 { "DMIC2L", NULL, "DMIC CLK" },
682 { "DMIC2R", NULL, "DMIC CLK" },
683
684 { "Stereo1 DMIC Mux", "DMIC1", "DMIC1" },
685 { "Stereo1 DMIC Mux", "DMIC2", "DMIC2" },
686
687 { "Sto1 ADC MIXL", "DMIC Switch", "Stereo1 DMIC Mux" },
688 { "Sto1 ADC MIXL", "ADC Switch", "AMICL" },
689 { "Sto1 ADC MIXR", "DMIC Switch", "Stereo1 DMIC Mux" },
690 { "Sto1 ADC MIXR", "ADC Switch", "AMICR" },
691
692 { "ADC Power", NULL, "LDO18 IN" },
693 { "ADC Power", NULL, "LDO18 ADC" },
694 { "ADC Power", NULL, "LDO21" },
695 { "ADC Power", NULL, "BG LDO18 IN" },
696 { "ADC Power", NULL, "BG LDO21" },
697 { "ADC Power", NULL, "BG MBIAS" },
698 { "ADC Power", NULL, "MBIAS" },
699 { "ADC Power", NULL, "VREF2" },
700 { "ADC Power", NULL, "VREF1" },
701
702 { "ADCL Power", NULL, "LDO16L" },
703 { "ADCL Power", NULL, "ADC1L" },
704 { "ADCL Power", NULL, "BSTL2" },
705 { "ADCL Power", NULL, "BSTL" },
706 { "ADCL Power", NULL, "ADCFEDL" },
707
708 { "ADCR Power", NULL, "LDO16R" },
709 { "ADCR Power", NULL, "ADC1R" },
710 { "ADCR Power", NULL, "BSTR2" },
711 { "ADCR Power", NULL, "BSTR" },
712 { "ADCR Power", NULL, "ADCFEDR" },
713
714 { "AMICL", NULL, "ADC CLK" },
715 { "AMICL", NULL, "ADC Power" },
716 { "AMICL", NULL, "ADCL Power" },
717 { "AMICR", NULL, "ADC CLK" },
718 { "AMICR", NULL, "ADC Power" },
719 { "AMICR", NULL, "ADCR Power" },
720
721 { "PLL1 LDO", NULL, "PLL1 LDO ENABLE" },
722 { "PLL1", NULL, "PLL1 LDO" },
723
724 { "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
725 { "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
726
727 { "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXL" },
728 { "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXR" },
729 { "Stereo1 ADC MIX", NULL, "adc stereo1 filter" },
730 { "adc stereo1 filter", NULL, "PLL1", rt5514_is_sys_clk_from_pll },
oder_chiou@realtek.com1628ef82017-07-24 15:34:23 +0800731 { "adc stereo1 filter", NULL, "ASRC AD1", rt5514_i2s_use_asrc },
Oder Chiou4a6180e2016-02-03 19:53:24 +0800732
733 { "Stereo2 DMIC Mux", "DMIC1", "DMIC1" },
734 { "Stereo2 DMIC Mux", "DMIC2", "DMIC2" },
735
736 { "Sto2 ADC MIXL", "DMIC Switch", "Stereo2 DMIC Mux" },
737 { "Sto2 ADC MIXL", "ADC Switch", "AMICL" },
738 { "Sto2 ADC MIXR", "DMIC Switch", "Stereo2 DMIC Mux" },
739 { "Sto2 ADC MIXR", "ADC Switch", "AMICR" },
740
741 { "Stereo2 ADC MIXL", NULL, "Sto2 ADC MIXL" },
742 { "Stereo2 ADC MIXR", NULL, "Sto2 ADC MIXR" },
743
744 { "Stereo2 ADC MIX", NULL, "Stereo2 ADC MIXL" },
745 { "Stereo2 ADC MIX", NULL, "Stereo2 ADC MIXR" },
746 { "Stereo2 ADC MIX", NULL, "adc stereo2 filter" },
747 { "adc stereo2 filter", NULL, "PLL1", rt5514_is_sys_clk_from_pll },
oder_chiou@realtek.com1628ef82017-07-24 15:34:23 +0800748 { "adc stereo2 filter", NULL, "ASRC AD2", rt5514_i2s_use_asrc },
Oder Chiou4a6180e2016-02-03 19:53:24 +0800749
750 { "AIF1TX", NULL, "Stereo1 ADC MIX"},
751 { "AIF1TX", NULL, "Stereo2 ADC MIX"},
752};
753
754static int rt5514_hw_params(struct snd_pcm_substream *substream,
755 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
756{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000757 struct snd_soc_component *component = dai->component;
758 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800759 int pre_div, bclk_ms, frame_size;
760 unsigned int val_len = 0;
761
762 rt5514->lrck = params_rate(params);
763 pre_div = rl6231_get_clk_info(rt5514->sysclk, rt5514->lrck);
764 if (pre_div < 0) {
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000765 dev_err(component->dev, "Unsupported clock setting\n");
Oder Chiou4a6180e2016-02-03 19:53:24 +0800766 return -EINVAL;
767 }
768
769 frame_size = snd_soc_params_to_frame_size(params);
770 if (frame_size < 0) {
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000771 dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800772 return -EINVAL;
773 }
774
775 bclk_ms = frame_size > 32;
776 rt5514->bclk = rt5514->lrck * (32 << bclk_ms);
777
778 dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
779 rt5514->bclk, rt5514->lrck);
780 dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
781 bclk_ms, pre_div, dai->id);
782
783 switch (params_format(params)) {
784 case SNDRV_PCM_FORMAT_S16_LE:
785 break;
786 case SNDRV_PCM_FORMAT_S20_3LE:
787 val_len = RT5514_I2S_DL_20;
788 break;
789 case SNDRV_PCM_FORMAT_S24_LE:
790 val_len = RT5514_I2S_DL_24;
791 break;
792 case SNDRV_PCM_FORMAT_S8:
793 val_len = RT5514_I2S_DL_8;
794 break;
795 default:
796 return -EINVAL;
797 }
798
799 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1, RT5514_I2S_DL_MASK,
800 val_len);
oder_chiou@realtek.com1628ef82017-07-24 15:34:23 +0800801 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL1,
802 RT5514_CLK_AD_ANA1_SEL_MASK,
803 (pre_div + 1) << RT5514_CLK_AD_ANA1_SEL_SFT);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800804 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
805 RT5514_CLK_SYS_DIV_OUT_MASK | RT5514_SEL_ADC_OSR_MASK,
806 pre_div << RT5514_CLK_SYS_DIV_OUT_SFT |
807 pre_div << RT5514_SEL_ADC_OSR_SFT);
808
809 return 0;
810}
811
812static int rt5514_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
813{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000814 struct snd_soc_component *component = dai->component;
815 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800816 unsigned int reg_val = 0;
817
818 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
819 case SND_SOC_DAIFMT_NB_NF:
820 break;
821
822 case SND_SOC_DAIFMT_NB_IF:
823 reg_val |= RT5514_I2S_LR_INV;
824 break;
825
826 case SND_SOC_DAIFMT_IB_NF:
827 reg_val |= RT5514_I2S_BP_INV;
828 break;
829
830 case SND_SOC_DAIFMT_IB_IF:
831 reg_val |= RT5514_I2S_BP_INV | RT5514_I2S_LR_INV;
832 break;
833
834 default:
835 return -EINVAL;
836 }
837
838 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
839 case SND_SOC_DAIFMT_I2S:
840 break;
841
842 case SND_SOC_DAIFMT_LEFT_J:
843 reg_val |= RT5514_I2S_DF_LEFT;
844 break;
845
846 case SND_SOC_DAIFMT_DSP_A:
847 reg_val |= RT5514_I2S_DF_PCM_A;
848 break;
849
850 case SND_SOC_DAIFMT_DSP_B:
851 reg_val |= RT5514_I2S_DF_PCM_B;
852 break;
853
854 default:
855 return -EINVAL;
856 }
857
858 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1,
859 RT5514_I2S_DF_MASK | RT5514_I2S_BP_MASK | RT5514_I2S_LR_MASK,
860 reg_val);
861
862 return 0;
863}
864
865static int rt5514_set_dai_sysclk(struct snd_soc_dai *dai,
866 int clk_id, unsigned int freq, int dir)
867{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000868 struct snd_soc_component *component = dai->component;
869 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800870 unsigned int reg_val = 0;
871
872 if (freq == rt5514->sysclk && clk_id == rt5514->sysclk_src)
873 return 0;
874
875 switch (clk_id) {
876 case RT5514_SCLK_S_MCLK:
877 reg_val |= RT5514_CLK_SYS_PRE_SEL_MCLK;
878 break;
879
880 case RT5514_SCLK_S_PLL1:
881 reg_val |= RT5514_CLK_SYS_PRE_SEL_PLL;
882 break;
883
884 default:
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000885 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800886 return -EINVAL;
887 }
888
889 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
890 RT5514_CLK_SYS_PRE_SEL_MASK, reg_val);
891
892 rt5514->sysclk = freq;
893 rt5514->sysclk_src = clk_id;
894
895 dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
896
897 return 0;
898}
899
900static int rt5514_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
901 unsigned int freq_in, unsigned int freq_out)
902{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000903 struct snd_soc_component *component = dai->component;
904 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800905 struct rl6231_pll_code pll_code;
906 int ret;
907
908 if (!freq_in || !freq_out) {
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000909 dev_dbg(component->dev, "PLL disabled\n");
Oder Chiou4a6180e2016-02-03 19:53:24 +0800910
911 rt5514->pll_in = 0;
912 rt5514->pll_out = 0;
913 regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
914 RT5514_CLK_SYS_PRE_SEL_MASK,
915 RT5514_CLK_SYS_PRE_SEL_MCLK);
916
917 return 0;
918 }
919
920 if (source == rt5514->pll_src && freq_in == rt5514->pll_in &&
921 freq_out == rt5514->pll_out)
922 return 0;
923
924 switch (source) {
925 case RT5514_PLL1_S_MCLK:
926 regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL,
927 RT5514_PLL_1_SEL_MASK, RT5514_PLL_1_SEL_MCLK);
928 break;
929
930 case RT5514_PLL1_S_BCLK:
931 regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL,
932 RT5514_PLL_1_SEL_MASK, RT5514_PLL_1_SEL_SCLK);
933 break;
934
935 default:
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000936 dev_err(component->dev, "Unknown PLL source %d\n", source);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800937 return -EINVAL;
938 }
939
940 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
941 if (ret < 0) {
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000942 dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
Oder Chiou4a6180e2016-02-03 19:53:24 +0800943 return ret;
944 }
945
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000946 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
Oder Chiou4a6180e2016-02-03 19:53:24 +0800947 pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
948 pll_code.n_code, pll_code.k_code);
949
950 regmap_write(rt5514->regmap, RT5514_ANA_CTRL_PLL1_1,
951 pll_code.k_code << RT5514_PLL_K_SFT |
952 pll_code.n_code << RT5514_PLL_N_SFT |
953 (pll_code.m_bp ? 0 : pll_code.m_code) << RT5514_PLL_M_SFT);
954 regmap_update_bits(rt5514->regmap, RT5514_ANA_CTRL_PLL1_2,
955 RT5514_PLL_M_BP, pll_code.m_bp << RT5514_PLL_M_BP_SFT);
956
957 rt5514->pll_in = freq_in;
958 rt5514->pll_out = freq_out;
959 rt5514->pll_src = source;
960
961 return 0;
962}
963
964static int rt5514_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
965 unsigned int rx_mask, int slots, int slot_width)
966{
Kuninori Morimoto04e82622018-01-29 03:45:07 +0000967 struct snd_soc_component *component = dai->component;
968 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
oder_chiou@realtek.come8be3a52017-07-20 12:05:34 +0800969 unsigned int val = 0, val2 = 0;
Oder Chiou4a6180e2016-02-03 19:53:24 +0800970
971 if (rx_mask || tx_mask)
972 val |= RT5514_TDM_MODE;
973
oder_chiou@realtek.come8be3a52017-07-20 12:05:34 +0800974 switch (tx_mask) {
975 case 0x3:
976 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH2 |
977 RT5514_TDM_DOCKING_START_SLOT0;
978 break;
979
980 case 0x30:
981 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH2 |
982 RT5514_TDM_DOCKING_START_SLOT4;
983 break;
984
985 case 0xf:
986 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH4 |
987 RT5514_TDM_DOCKING_START_SLOT0;
988 break;
989
990 case 0xf0:
991 val2 |= RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH4 |
992 RT5514_TDM_DOCKING_START_SLOT4;
993 break;
994
995 default:
996 break;
997 }
998
999
1000
Oder Chioud60bc8d2017-05-02 10:42:56 +08001001 switch (slots) {
1002 case 4:
Oder Chiou4a6180e2016-02-03 19:53:24 +08001003 val |= RT5514_TDMSLOT_SEL_RX_4CH | RT5514_TDMSLOT_SEL_TX_4CH;
Oder Chioud60bc8d2017-05-02 10:42:56 +08001004 break;
Oder Chiou4a6180e2016-02-03 19:53:24 +08001005
Oder Chioud60bc8d2017-05-02 10:42:56 +08001006 case 6:
1007 val |= RT5514_TDMSLOT_SEL_RX_6CH | RT5514_TDMSLOT_SEL_TX_6CH;
1008 break;
1009
1010 case 8:
1011 val |= RT5514_TDMSLOT_SEL_RX_8CH | RT5514_TDMSLOT_SEL_TX_8CH;
1012 break;
1013
1014 case 2:
1015 default:
1016 break;
1017 }
Oder Chiou4a6180e2016-02-03 19:53:24 +08001018
1019 switch (slot_width) {
1020 case 20:
1021 val |= RT5514_CH_LEN_RX_20 | RT5514_CH_LEN_TX_20;
1022 break;
1023
1024 case 24:
1025 val |= RT5514_CH_LEN_RX_24 | RT5514_CH_LEN_TX_24;
1026 break;
1027
Oder Chioud60bc8d2017-05-02 10:42:56 +08001028 case 25:
1029 val |= RT5514_TDM_MODE2;
1030 break;
1031
Oder Chiou4a6180e2016-02-03 19:53:24 +08001032 case 32:
1033 val |= RT5514_CH_LEN_RX_32 | RT5514_CH_LEN_TX_32;
1034 break;
1035
1036 case 16:
1037 default:
1038 break;
1039 }
1040
1041 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1, RT5514_TDM_MODE |
1042 RT5514_TDMSLOT_SEL_RX_MASK | RT5514_TDMSLOT_SEL_TX_MASK |
Oder Chioud60bc8d2017-05-02 10:42:56 +08001043 RT5514_CH_LEN_RX_MASK | RT5514_CH_LEN_TX_MASK |
1044 RT5514_TDM_MODE2, val);
Oder Chiou4a6180e2016-02-03 19:53:24 +08001045
oder_chiou@realtek.come8be3a52017-07-20 12:05:34 +08001046 regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL2,
1047 RT5514_TDM_DOCKING_MODE | RT5514_TDM_DOCKING_VALID_CH_MASK |
1048 RT5514_TDM_DOCKING_START_MASK, val2);
1049
Oder Chiou4a6180e2016-02-03 19:53:24 +08001050 return 0;
1051}
1052
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001053static int rt5514_set_bias_level(struct snd_soc_component *component,
Oder Chiouc9506bb2016-06-17 11:02:24 +08001054 enum snd_soc_bias_level level)
1055{
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001056 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
Oder Chiouc9506bb2016-06-17 11:02:24 +08001057 int ret;
1058
1059 switch (level) {
1060 case SND_SOC_BIAS_PREPARE:
1061 if (IS_ERR(rt5514->mclk))
1062 break;
1063
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001064 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) {
Oder Chiouc9506bb2016-06-17 11:02:24 +08001065 clk_disable_unprepare(rt5514->mclk);
1066 } else {
1067 ret = clk_prepare_enable(rt5514->mclk);
1068 if (ret)
1069 return ret;
1070 }
1071 break;
1072
oder_chiou@realtek.comea4daf82017-07-10 10:03:12 +08001073 case SND_SOC_BIAS_STANDBY:
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001074 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
oder_chiou@realtek.comea4daf82017-07-10 10:03:12 +08001075 /*
1076 * If the DSP is enabled in start of recording, the DSP
1077 * should be disabled, and sync back to normal recording
1078 * settings to make sure recording properly.
1079 */
1080 if (rt5514->dsp_enabled) {
1081 rt5514->dsp_enabled = 0;
1082 regmap_multi_reg_write(rt5514->i2c_regmap,
1083 rt5514_i2c_patch,
1084 ARRAY_SIZE(rt5514_i2c_patch));
1085 regcache_mark_dirty(rt5514->regmap);
1086 regcache_sync(rt5514->regmap);
1087 }
1088 }
1089 break;
1090
Oder Chiouc9506bb2016-06-17 11:02:24 +08001091 default:
1092 break;
1093 }
1094
1095 return 0;
1096}
1097
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001098static int rt5514_probe(struct snd_soc_component *component)
Oder Chiou4a6180e2016-02-03 19:53:24 +08001099{
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001100 struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
1101 struct platform_device *pdev = container_of(component->dev,
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +08001102 struct platform_device, dev);
Oder Chiou4a6180e2016-02-03 19:53:24 +08001103
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001104 rt5514->mclk = devm_clk_get(component->dev, "mclk");
Oder Chiouc9506bb2016-06-17 11:02:24 +08001105 if (PTR_ERR(rt5514->mclk) == -EPROBE_DEFER)
1106 return -EPROBE_DEFER;
1107
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +08001108 if (rt5514->pdata.dsp_calib_clk_name) {
1109 rt5514->dsp_calib_clk = devm_clk_get(&pdev->dev,
1110 rt5514->pdata.dsp_calib_clk_name);
1111 if (PTR_ERR(rt5514->dsp_calib_clk) == -EPROBE_DEFER)
1112 return -EPROBE_DEFER;
1113 }
1114
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001115 rt5514->component = component;
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +08001116 rt5514->pll3_cal_value = 0x0078b000;
Oder Chiou4a6180e2016-02-03 19:53:24 +08001117
1118 return 0;
1119}
1120
1121static int rt5514_i2c_read(void *context, unsigned int reg, unsigned int *val)
1122{
1123 struct i2c_client *client = context;
1124 struct rt5514_priv *rt5514 = i2c_get_clientdata(client);
1125
1126 regmap_read(rt5514->i2c_regmap, reg | RT5514_DSP_MAPPING, val);
1127
1128 return 0;
1129}
1130
1131static int rt5514_i2c_write(void *context, unsigned int reg, unsigned int val)
1132{
1133 struct i2c_client *client = context;
1134 struct rt5514_priv *rt5514 = i2c_get_clientdata(client);
1135
1136 regmap_write(rt5514->i2c_regmap, reg | RT5514_DSP_MAPPING, val);
1137
1138 return 0;
1139}
1140
1141#define RT5514_STEREO_RATES SNDRV_PCM_RATE_8000_192000
1142#define RT5514_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1143 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1144
Gustavo A. R. Silva9894dba2017-07-13 15:37:59 -05001145static const struct snd_soc_dai_ops rt5514_aif_dai_ops = {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001146 .hw_params = rt5514_hw_params,
1147 .set_fmt = rt5514_set_dai_fmt,
1148 .set_sysclk = rt5514_set_dai_sysclk,
1149 .set_pll = rt5514_set_dai_pll,
1150 .set_tdm_slot = rt5514_set_tdm_slot,
1151};
1152
Colin Ian Kinge5e8e612017-08-13 16:08:03 +01001153static struct snd_soc_dai_driver rt5514_dai[] = {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001154 {
1155 .name = "rt5514-aif1",
1156 .id = 0,
1157 .capture = {
1158 .stream_name = "AIF1 Capture",
1159 .channels_min = 1,
1160 .channels_max = 4,
1161 .rates = RT5514_STEREO_RATES,
1162 .formats = RT5514_FORMATS,
1163 },
1164 .ops = &rt5514_aif_dai_ops,
1165 }
1166};
1167
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001168static const struct snd_soc_component_driver soc_component_dev_rt5514 = {
1169 .probe = rt5514_probe,
1170 .set_bias_level = rt5514_set_bias_level,
1171 .controls = rt5514_snd_controls,
1172 .num_controls = ARRAY_SIZE(rt5514_snd_controls),
1173 .dapm_widgets = rt5514_dapm_widgets,
1174 .num_dapm_widgets = ARRAY_SIZE(rt5514_dapm_widgets),
1175 .dapm_routes = rt5514_dapm_routes,
1176 .num_dapm_routes = ARRAY_SIZE(rt5514_dapm_routes),
1177 .use_pmdown_time = 1,
1178 .endianness = 1,
1179 .non_legacy_dai_naming = 1,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001180};
1181
1182static const struct regmap_config rt5514_i2c_regmap = {
1183 .name = "i2c",
1184 .reg_bits = 32,
1185 .val_bits = 32,
1186
Oder Chiou4a6180e2016-02-03 19:53:24 +08001187 .readable_reg = rt5514_i2c_readable_register,
1188
1189 .cache_type = REGCACHE_NONE,
1190};
1191
1192static const struct regmap_config rt5514_regmap = {
1193 .reg_bits = 16,
1194 .val_bits = 32,
1195
1196 .max_register = RT5514_VENDOR_ID2,
1197 .volatile_reg = rt5514_volatile_register,
1198 .readable_reg = rt5514_readable_register,
1199 .reg_read = rt5514_i2c_read,
1200 .reg_write = rt5514_i2c_write,
1201
1202 .cache_type = REGCACHE_RBTREE,
1203 .reg_defaults = rt5514_reg,
1204 .num_reg_defaults = ARRAY_SIZE(rt5514_reg),
David Frey1c96a2f2018-09-01 09:50:41 -07001205 .use_single_read = true,
1206 .use_single_write = true,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001207};
1208
1209static const struct i2c_device_id rt5514_i2c_id[] = {
1210 { "rt5514", 0 },
1211 { }
1212};
1213MODULE_DEVICE_TABLE(i2c, rt5514_i2c_id);
1214
1215#if defined(CONFIG_OF)
1216static const struct of_device_id rt5514_of_match[] = {
1217 { .compatible = "realtek,rt5514", },
1218 {},
1219};
1220MODULE_DEVICE_TABLE(of, rt5514_of_match);
1221#endif
1222
Oder Chiou6d3edf82017-05-15 19:02:07 +08001223#ifdef CONFIG_ACPI
Arvind Yadav8a43c222017-07-23 22:53:24 +05301224static const struct acpi_device_id rt5514_acpi_match[] = {
Oder Chiou6d3edf82017-05-15 19:02:07 +08001225 { "10EC5514", 0},
1226 {},
1227};
1228MODULE_DEVICE_TABLE(acpi, rt5514_acpi_match);
1229#endif
1230
oder_chiou@realtek.com28aef242017-11-02 19:21:16 +08001231static int rt5514_parse_dp(struct rt5514_priv *rt5514, struct device *dev)
Oder Chioua5461fd2016-10-25 19:27:26 +08001232{
1233 device_property_read_u32(dev, "realtek,dmic-init-delay-ms",
1234 &rt5514->pdata.dmic_init_delay);
oder_chiou@realtek.comfc9cab02017-11-07 12:31:14 +08001235 device_property_read_string(dev, "realtek,dsp-calib-clk-name",
1236 &rt5514->pdata.dsp_calib_clk_name);
1237 device_property_read_u32(dev, "realtek,dsp-calib-clk-rate",
1238 &rt5514->pdata.dsp_calib_clk_rate);
Oder Chioua5461fd2016-10-25 19:27:26 +08001239
1240 return 0;
1241}
1242
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001243static __maybe_unused int rt5514_i2c_resume(struct device *dev)
1244{
1245 struct rt5514_priv *rt5514 = dev_get_drvdata(dev);
1246 unsigned int val;
1247
1248 /*
1249 * Add a bogus read to avoid rt5514's confusion after s2r in case it
1250 * saw glitches on the i2c lines and thought the other side sent a
1251 * start bit.
1252 */
1253 regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
1254
1255 return 0;
1256}
1257
Oder Chiou4a6180e2016-02-03 19:53:24 +08001258static int rt5514_i2c_probe(struct i2c_client *i2c,
1259 const struct i2c_device_id *id)
1260{
Oder Chioua5461fd2016-10-25 19:27:26 +08001261 struct rt5514_platform_data *pdata = dev_get_platdata(&i2c->dev);
Oder Chiou4a6180e2016-02-03 19:53:24 +08001262 struct rt5514_priv *rt5514;
1263 int ret;
Douglas Anderson0a78b242017-04-14 09:40:31 -07001264 unsigned int val = ~0;
Oder Chiou4a6180e2016-02-03 19:53:24 +08001265
1266 rt5514 = devm_kzalloc(&i2c->dev, sizeof(struct rt5514_priv),
1267 GFP_KERNEL);
1268 if (rt5514 == NULL)
1269 return -ENOMEM;
1270
1271 i2c_set_clientdata(i2c, rt5514);
1272
Oder Chioua5461fd2016-10-25 19:27:26 +08001273 if (pdata)
1274 rt5514->pdata = *pdata;
oder_chiou@realtek.com28aef242017-11-02 19:21:16 +08001275 else
1276 rt5514_parse_dp(rt5514, &i2c->dev);
Oder Chioua5461fd2016-10-25 19:27:26 +08001277
Oder Chiou4a6180e2016-02-03 19:53:24 +08001278 rt5514->i2c_regmap = devm_regmap_init_i2c(i2c, &rt5514_i2c_regmap);
1279 if (IS_ERR(rt5514->i2c_regmap)) {
1280 ret = PTR_ERR(rt5514->i2c_regmap);
1281 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1282 ret);
1283 return ret;
1284 }
1285
1286 rt5514->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt5514_regmap);
1287 if (IS_ERR(rt5514->regmap)) {
1288 ret = PTR_ERR(rt5514->regmap);
1289 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1290 ret);
1291 return ret;
1292 }
1293
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001294 /*
1295 * The rt5514 can get confused if the i2c lines glitch together, as
1296 * can happen at bootup as regulators are turned off and on. If it's
1297 * in this glitched state the first i2c read will fail, so we'll give
1298 * it one change to retry.
1299 */
Douglas Anderson0a78b242017-04-14 09:40:31 -07001300 ret = regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001301 if (ret || val != RT5514_DEVICE_ID)
1302 ret = regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
Douglas Anderson0a78b242017-04-14 09:40:31 -07001303 if (ret || val != RT5514_DEVICE_ID) {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001304 dev_err(&i2c->dev,
1305 "Device with ID register %x is not rt5514\n", val);
1306 return -ENODEV;
1307 }
1308
Oder Chiou6eebf352016-06-06 18:33:31 +08001309 ret = regmap_multi_reg_write(rt5514->i2c_regmap, rt5514_i2c_patch,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001310 ARRAY_SIZE(rt5514_i2c_patch));
1311 if (ret != 0)
1312 dev_warn(&i2c->dev, "Failed to apply i2c_regmap patch: %d\n",
1313 ret);
1314
1315 ret = regmap_register_patch(rt5514->regmap, rt5514_patch,
1316 ARRAY_SIZE(rt5514_patch));
1317 if (ret != 0)
1318 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
1319
Kuninori Morimoto04e82622018-01-29 03:45:07 +00001320 return devm_snd_soc_register_component(&i2c->dev,
1321 &soc_component_dev_rt5514,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001322 rt5514_dai, ARRAY_SIZE(rt5514_dai));
1323}
1324
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001325static const struct dev_pm_ops rt5514_i2_pm_ops = {
1326 SET_SYSTEM_SLEEP_PM_OPS(NULL, rt5514_i2c_resume)
1327};
1328
Douglas Andersond0c02e12017-04-14 09:40:30 -07001329static struct i2c_driver rt5514_i2c_driver = {
Oder Chiou4a6180e2016-02-03 19:53:24 +08001330 .driver = {
1331 .name = "rt5514",
Oder Chiou6d3edf82017-05-15 19:02:07 +08001332 .acpi_match_table = ACPI_PTR(rt5514_acpi_match),
Oder Chiou4a6180e2016-02-03 19:53:24 +08001333 .of_match_table = of_match_ptr(rt5514_of_match),
Douglas Anderson7952b4b2017-04-14 09:40:32 -07001334 .pm = &rt5514_i2_pm_ops,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001335 },
1336 .probe = rt5514_i2c_probe,
Oder Chiou4a6180e2016-02-03 19:53:24 +08001337 .id_table = rt5514_i2c_id,
1338};
1339module_i2c_driver(rt5514_i2c_driver);
1340
1341MODULE_DESCRIPTION("ASoC RT5514 driver");
1342MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
1343MODULE_LICENSE("GPL v2");