1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019 Google LLC
6 #define LOG_CATEGORY UCLASS_SOUND
8 #include <audio_codec.h>
18 /* RT5677 has 256 8-bit register addresses, and 16-bit register data */
19 struct rt5677_init_reg {
24 static struct rt5677_init_reg init_list[] = {
25 {RT5677_LOUT1, 0x0800},
26 {RT5677_SIDETONE_CTRL, 0x0000},
27 {RT5677_STO1_ADC_DIG_VOL, 0x3F3F},
28 {RT5677_DAC1_DIG_VOL, 0x9090},
29 {RT5677_STO2_ADC_MIXER, 0xA441},
30 {RT5677_STO1_ADC_MIXER, 0x5480},
31 {RT5677_STO1_DAC_MIXER, 0x8A8A},
32 {RT5677_PWR_DIG1, 0x9800}, /* Power up I2S1 */
33 {RT5677_PWR_ANLG1, 0xE9D5},
34 {RT5677_PWR_ANLG2, 0x2CC0},
35 {RT5677_PWR_DSP2, 0x0C00},
36 {RT5677_I2S2_SDP, 0x0000},
37 {RT5677_CLK_TREE_CTRL1, 0x1111},
38 {RT5677_PLL1_CTRL1, 0x0000},
39 {RT5677_PLL1_CTRL2, 0x0000},
40 {RT5677_DIG_MISC, 0x0029},
41 {RT5677_GEN_CTRL1, 0x00FF},
42 {RT5677_GPIO_CTRL2, 0x0020},
43 {RT5677_PWR_DIG2, 0x9024}, /* Power on ADC Stereo Filters */
44 {RT5677_PDM_OUT_CTRL, 0x0088}, /* Unmute PDM, set stereo1 DAC */
45 {RT5677_PDM_DATA_CTRL1, 0x0001}, /* Sysclk to PDM filter divider 2 */
49 * rt5677_i2c_read() - Read a 16-bit register
51 * @priv: Private driver data
52 * @reg: Register number to read
53 * @returns data read or -ve on error
55 static int rt5677_i2c_read(struct rt5677_priv *priv, uint reg)
60 ret = dm_i2c_read(priv->dev, reg, buf, sizeof(u16));
63 return buf[0] << 8 | buf[1];
67 * rt5677_i2c_write() - Write a 16-bit register
69 * @priv: Private driver data
70 * @reg: Register number to read
71 * @data: Data to write
72 * @returns 0 if OK, -ve on error
74 static int rt5677_i2c_write(struct rt5677_priv *priv, uint reg, uint data)
78 buf[0] = (data >> 8) & 0xff;
81 return dm_i2c_write(priv->dev, reg, buf, sizeof(u16));
85 * rt5677_bic_or() - Set and clear bits of a codec register
87 * @priv: Private driver data
88 * @reg: Register number to update
89 * @bic: Mask of bits to clear
90 * @set: Mask of bits to set
91 * @returns 0 if OK, -ve on error
94 static int rt5677_bic_or(struct rt5677_priv *priv, uint reg, uint bic,
100 old = rt5677_i2c_read(priv, reg);
104 new_value = (old & ~bic) | (set & bic);
106 if (old != new_value) {
107 ret = rt5677_i2c_write(priv, reg, new_value);
116 * rt5677_reg_init() - Initialise codec regs w/static/base values
118 * @priv: Private driver data
119 * @returns 0 if OK, -ve on error
121 static int rt5677_reg_init(struct rt5677_priv *priv)
126 for (i = 0; i < ARRAY_SIZE(init_list); i++) {
127 ret = rt5677_i2c_write(priv, init_list[i].reg, init_list[i].val);
136 static void debug_dump_5677_regs(struct rt5677_priv *priv, int swap)
140 /* Show all 16-bit codec regs */
141 for (i = 0; i < RT5677_REG_CNT; i++) {
143 log_debug("\nMX%02x: ", i);
145 rt5677_i2c_read(priv, (u8)i, ®_word);
147 log_debug("%04x ", swap_bytes16(reg_word));
149 log_debug("%04x ", reg_word);
153 /* Show all 16-bit 'private' codec regs */
154 for (i = 0; i < RT5677_PR_REG_CNT; i++) {
156 log_debug("\nPR%02x: ", i);
158 rt5677_i2c_write(priv, RT5677_PRIV_INDEX, i);
159 rt5677_i2c_read(priv, RT5677_PRIV_DATA, ®_word);
161 log_debug("%04x ", swap_bytes16(reg_word));
163 log_debug("%04x ", reg_word);
169 static int rt5677_hw_params(struct rt5677_priv *priv, uint bits_per_sample)
173 switch (bits_per_sample) {
175 ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DL_MASK,
178 log_debug("Error updating I2S1 Interface Ctrl reg\n");
183 log_err("Illegal bits per sample %d\n", bits_per_sample);
191 * rt5677_set_fmt() - set rt5677 I2S format
193 * @priv: Private driver data
194 * @returns 0 if OK, -ve on error
196 static int rt5677_set_fmt(struct rt5677_priv *priv)
201 * Set format here: Assumes I2S, NB_NF, CBS_CFS
203 * CBS_CFS (Codec Bit Slave/Codec Frame Slave)
205 ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_MS_MASK,
208 /* NB_NF (Normal Bit/Normal Frame) */
209 ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_BP_MASK,
213 ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DF_MASK,
216 /* A44: I2S2 (going to speaker amp) is master */
217 ret |= rt5677_bic_or(priv, RT5677_I2S2_SDP, RT5677_I2S_MS_MASK,
221 log_err("Error updating I2S1 Interface Ctrl reg\n");
229 * rt5677_reset() - reset the audio codec
231 * @priv: Private driver data
232 * @returns 0 if OK, -ve on error
234 static int rt5677_reset(struct rt5677_priv *priv)
238 /* Reset the codec registers to their defaults */
239 ret = rt5677_i2c_write(priv, RT5677_RESET, RT5677_SW_RESET);
241 log_err("Error resetting codec\n");
249 * Initialise rt5677 codec device
251 * @priv: Private driver data
252 * @returns 0 if OK, -ve on error
254 int rt5677_device_init(struct rt5677_priv *priv)
258 /* Read status reg */
259 ret = rt5677_i2c_read(priv, RT5677_RESET);
262 log_debug("reg 00h, Software Reset & Status = 0x%04x\n", ret);
264 /* Reset the codec/regs */
265 ret = rt5677_reset(priv);
269 ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID1);
271 log_err("Error reading vendor ID\n");
274 log_debug("Hardware ID: %0xX\n", ret);
276 ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID2);
278 log_err("Error reading vendor rev\n");
281 log_debug("Hardware revision: %04x\n", ret);
286 static int rt5677_set_params(struct udevice *dev, int interface, int rate,
287 int mclk_freq, int bits_per_sample,
290 struct rt5677_priv *priv = dev_get_priv(dev);
293 /* Initialise codec regs w/static/base values, same as Linux driver */
294 ret = rt5677_reg_init(priv);
298 ret = rt5677_hw_params(priv, bits_per_sample);
302 ret = rt5677_set_fmt(priv);
309 static int rt5677_probe(struct udevice *dev)
311 struct rt5677_priv *priv = dev_get_priv(dev);
315 return rt5677_device_init(priv);
318 static const struct audio_codec_ops rt5677_ops = {
319 .set_params = rt5677_set_params,
322 static const struct udevice_id rt5677_ids[] = {
323 { .compatible = "realtek,rt5677" },
327 U_BOOT_DRIVER(rt5677_drv) = {
329 .id = UCLASS_AUDIO_CODEC,
330 .of_match = rt5677_ids,
332 .probe = rt5677_probe,
333 .priv_auto = sizeof(struct rt5677_priv),