1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019 Google LLC
6 #define LOG_CATEGORY UCLASS_SOUND
9 #include <audio_codec.h>
19 /* RT5677 has 256 8-bit register addresses, and 16-bit register data */
20 struct rt5677_init_reg {
25 static struct rt5677_init_reg init_list[] = {
26 {RT5677_LOUT1, 0x0800},
27 {RT5677_SIDETONE_CTRL, 0x0000},
28 {RT5677_STO1_ADC_DIG_VOL, 0x3F3F},
29 {RT5677_DAC1_DIG_VOL, 0x9090},
30 {RT5677_STO2_ADC_MIXER, 0xA441},
31 {RT5677_STO1_ADC_MIXER, 0x5480},
32 {RT5677_STO1_DAC_MIXER, 0x8A8A},
33 {RT5677_PWR_DIG1, 0x9800}, /* Power up I2S1 */
34 {RT5677_PWR_ANLG1, 0xE9D5},
35 {RT5677_PWR_ANLG2, 0x2CC0},
36 {RT5677_PWR_DSP2, 0x0C00},
37 {RT5677_I2S2_SDP, 0x0000},
38 {RT5677_CLK_TREE_CTRL1, 0x1111},
39 {RT5677_PLL1_CTRL1, 0x0000},
40 {RT5677_PLL1_CTRL2, 0x0000},
41 {RT5677_DIG_MISC, 0x0029},
42 {RT5677_GEN_CTRL1, 0x00FF},
43 {RT5677_GPIO_CTRL2, 0x0020},
44 {RT5677_PWR_DIG2, 0x9024}, /* Power on ADC Stereo Filters */
45 {RT5677_PDM_OUT_CTRL, 0x0088}, /* Unmute PDM, set stereo1 DAC */
46 {RT5677_PDM_DATA_CTRL1, 0x0001}, /* Sysclk to PDM filter divider 2 */
50 * rt5677_i2c_read() - Read a 16-bit register
52 * @priv: Private driver data
53 * @reg: Register number to read
54 * @returns data read or -ve on error
56 static int rt5677_i2c_read(struct rt5677_priv *priv, uint reg)
61 ret = dm_i2c_read(priv->dev, reg, buf, sizeof(u16));
64 return buf[0] << 8 | buf[1];
68 * rt5677_i2c_write() - Write a 16-bit register
70 * @priv: Private driver data
71 * @reg: Register number to read
72 * @data: Data to write
73 * @returns 0 if OK, -ve on error
75 static int rt5677_i2c_write(struct rt5677_priv *priv, uint reg, uint data)
79 buf[0] = (data >> 8) & 0xff;
82 return dm_i2c_write(priv->dev, reg, buf, sizeof(u16));
86 * rt5677_bic_or() - Set and clear bits of a codec register
88 * @priv: Private driver data
89 * @reg: Register number to update
90 * @bic: Mask of bits to clear
91 * @set: Mask of bits to set
92 * @returns 0 if OK, -ve on error
95 static int rt5677_bic_or(struct rt5677_priv *priv, uint reg, uint bic,
101 old = rt5677_i2c_read(priv, reg);
105 new_value = (old & ~bic) | (set & bic);
107 if (old != new_value) {
108 ret = rt5677_i2c_write(priv, reg, new_value);
117 * rt5677_reg_init() - Initialise codec regs w/static/base values
119 * @priv: Private driver data
120 * @returns 0 if OK, -ve on error
122 static int rt5677_reg_init(struct rt5677_priv *priv)
127 for (i = 0; i < ARRAY_SIZE(init_list); i++) {
128 ret = rt5677_i2c_write(priv, init_list[i].reg, init_list[i].val);
137 static void debug_dump_5677_regs(struct rt5677_priv *priv, int swap)
141 /* Show all 16-bit codec regs */
142 for (i = 0; i < RT5677_REG_CNT; i++) {
144 log_debug("\nMX%02x: ", i);
146 rt5677_i2c_read(priv, (u8)i, ®_word);
148 log_debug("%04x ", swap_bytes16(reg_word));
150 log_debug("%04x ", reg_word);
154 /* Show all 16-bit 'private' codec regs */
155 for (i = 0; i < RT5677_PR_REG_CNT; i++) {
157 log_debug("\nPR%02x: ", i);
159 rt5677_i2c_write(priv, RT5677_PRIV_INDEX, i);
160 rt5677_i2c_read(priv, RT5677_PRIV_DATA, ®_word);
162 log_debug("%04x ", swap_bytes16(reg_word));
164 log_debug("%04x ", reg_word);
170 static int rt5677_hw_params(struct rt5677_priv *priv, uint bits_per_sample)
174 switch (bits_per_sample) {
176 ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DL_MASK,
179 log_debug("Error updating I2S1 Interface Ctrl reg\n");
184 log_err("Illegal bits per sample %d\n", bits_per_sample);
192 * rt5677_set_fmt() - set rt5677 I2S format
194 * @priv: Private driver data
195 * @returns 0 if OK, -ve on error
197 static int rt5677_set_fmt(struct rt5677_priv *priv)
202 * Set format here: Assumes I2S, NB_NF, CBS_CFS
204 * CBS_CFS (Codec Bit Slave/Codec Frame Slave)
206 ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_MS_MASK,
209 /* NB_NF (Normal Bit/Normal Frame) */
210 ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_BP_MASK,
214 ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DF_MASK,
217 /* A44: I2S2 (going to speaker amp) is master */
218 ret |= rt5677_bic_or(priv, RT5677_I2S2_SDP, RT5677_I2S_MS_MASK,
222 log_err("Error updating I2S1 Interface Ctrl reg\n");
230 * rt5677_reset() - reset the audio codec
232 * @priv: Private driver data
233 * @returns 0 if OK, -ve on error
235 static int rt5677_reset(struct rt5677_priv *priv)
239 /* Reset the codec registers to their defaults */
240 ret = rt5677_i2c_write(priv, RT5677_RESET, RT5677_SW_RESET);
242 log_err("Error resetting codec\n");
250 * Initialise rt5677 codec device
252 * @priv: Private driver data
253 * @returns 0 if OK, -ve on error
255 int rt5677_device_init(struct rt5677_priv *priv)
259 /* Read status reg */
260 ret = rt5677_i2c_read(priv, RT5677_RESET);
263 log_debug("reg 00h, Software Reset & Status = 0x%04x\n", ret);
265 /* Reset the codec/regs */
266 ret = rt5677_reset(priv);
270 ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID1);
272 log_err("Error reading vendor ID\n");
275 log_debug("Hardware ID: %0xX\n", ret);
277 ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID2);
279 log_err("Error reading vendor rev\n");
282 log_debug("Hardware revision: %04x\n", ret);
287 static int rt5677_set_params(struct udevice *dev, int interface, int rate,
288 int mclk_freq, int bits_per_sample,
291 struct rt5677_priv *priv = dev_get_priv(dev);
294 /* Initialise codec regs w/static/base values, same as Linux driver */
295 ret = rt5677_reg_init(priv);
299 ret = rt5677_hw_params(priv, bits_per_sample);
303 ret = rt5677_set_fmt(priv);
310 static int rt5677_probe(struct udevice *dev)
312 struct rt5677_priv *priv = dev_get_priv(dev);
316 return rt5677_device_init(priv);
319 static const struct audio_codec_ops rt5677_ops = {
320 .set_params = rt5677_set_params,
323 static const struct udevice_id rt5677_ids[] = {
324 { .compatible = "realtek,rt5677" },
328 U_BOOT_DRIVER(rt5677_drv) = {
330 .id = UCLASS_AUDIO_CODEC,
331 .of_match = rt5677_ids,
333 .probe = rt5677_probe,
334 .priv_auto = sizeof(struct rt5677_priv),