sound: Add a driver for RealTek RT5677

This audio codec is used on samus. Add a driver for it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
This commit is contained in:
Simon Glass 2019-02-16 20:25:05 -07:00 committed by Bin Meng
parent cbdfe59918
commit cf885929a6
4 changed files with 1772 additions and 0 deletions

View File

@ -95,6 +95,15 @@ config SOUND_MAX98095
audio data and I2C for codec control. At present it only works
with the Samsung I2S driver.
config SOUND_RT5677
bool "Support Realtek RT5677 audio codec"
depends on SOUND
help
Enable the Realtek RT5677 audio codec. This is an I2S device used on
some Chromebooks from around 2015 ('auron'). It is configured using
an I2C interface and supports multiple sound inputs and outputs,
including digital microphones.
config SOUND_SANDBOX
bool "Support sandbox emulated audio codec"
depends on SANDBOX && SOUND

View File

@ -17,5 +17,6 @@ obj-$(CONFIG_SOUND_MAX98090) += max98090.o maxim_codec.o
obj-$(CONFIG_SOUND_MAX98095) += max98095.o maxim_codec.o
obj-$(CONFIG_SOUND_INTEL_HDA) += hda_codec.o
obj-$(CONFIG_SOUND_I8254) += i8254_beep.o
obj-$(CONFIG_SOUND_RT5677) += rt5677.o
obj-$(CONFIG_INTEL_BROADWELL) += broadwell_i2s.o
obj-$(CONFIG_SOUND_IVYBRIDGE) += ivybridge_sound.o

334
drivers/sound/rt5677.c Normal file
View File

@ -0,0 +1,334 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2019 Google LLC
*/
#define LOG_CATEGORY UCLASS_SOUND
#include <common.h>
#include <audio_codec.h>
#include <dm.h>
#include <i2c.h>
#include "rt5677.h"
struct rt5677_priv {
struct udevice *dev;
};
/* RT5677 has 256 8-bit register addresses, and 16-bit register data */
struct rt5677_init_reg {
u8 reg;
u16 val;
};
static struct rt5677_init_reg init_list[] = {
{RT5677_LOUT1, 0x0800},
{RT5677_SIDETONE_CTRL, 0x0000},
{RT5677_STO1_ADC_DIG_VOL, 0x3F3F},
{RT5677_DAC1_DIG_VOL, 0x9090},
{RT5677_STO2_ADC_MIXER, 0xA441},
{RT5677_STO1_ADC_MIXER, 0x5480},
{RT5677_STO1_DAC_MIXER, 0x8A8A},
{RT5677_PWR_DIG1, 0x9800}, /* Power up I2S1 */
{RT5677_PWR_ANLG1, 0xE9D5},
{RT5677_PWR_ANLG2, 0x2CC0},
{RT5677_PWR_DSP2, 0x0C00},
{RT5677_I2S2_SDP, 0x0000},
{RT5677_CLK_TREE_CTRL1, 0x1111},
{RT5677_PLL1_CTRL1, 0x0000},
{RT5677_PLL1_CTRL2, 0x0000},
{RT5677_DIG_MISC, 0x0029},
{RT5677_GEN_CTRL1, 0x00FF},
{RT5677_GPIO_CTRL2, 0x0020},
{RT5677_PWR_DIG2, 0x9024}, /* Power on ADC Stereo Filters */
{RT5677_PDM_OUT_CTRL, 0x0088}, /* Unmute PDM, set stereo1 DAC */
{RT5677_PDM_DATA_CTRL1, 0x0001}, /* Sysclk to PDM filter divider 2 */
};
/**
* rt5677_i2c_read() - Read a 16-bit register
*
* @priv: Private driver data
* @reg: Register number to read
* @returns data read or -ve on error
*/
static int rt5677_i2c_read(struct rt5677_priv *priv, uint reg)
{
u8 buf[2];
int ret;
ret = dm_i2c_read(priv->dev, reg, buf, sizeof(u16));
if (ret)
return ret;
return buf[0] << 8 | buf[1];
}
/**
* rt5677_i2c_write() - Write a 16-bit register
*
* @priv: Private driver data
* @reg: Register number to read
* @data: Data to write
* @returns 0 if OK, -ve on error
*/
static int rt5677_i2c_write(struct rt5677_priv *priv, uint reg, uint data)
{
u8 buf[2];
buf[0] = (data >> 8) & 0xff;
buf[1] = data & 0xff;
return dm_i2c_write(priv->dev, reg, buf, sizeof(u16));
}
/**
* rt5677_bic_or() - Set and clear bits of a codec register
*
* @priv: Private driver data
* @reg: Register number to update
* @bic: Mask of bits to clear
* @set: Mask of bits to set
* @returns 0 if OK, -ve on error
*
*/
static int rt5677_bic_or(struct rt5677_priv *priv, uint reg, uint bic,
uint set)
{
uint old, new_value;
int ret;
old = rt5677_i2c_read(priv, reg);
if (old < 0)
return old;
new_value = (old & ~bic) | (set & bic);
if (old != new_value) {
ret = rt5677_i2c_write(priv, reg, new_value);
if (ret)
return ret;
}
return 0;
}
/**
* rt5677_reg_init() - Initialise codec regs w/static/base values
*
* @priv: Private driver data
* @returns 0 if OK, -ve on error
*/
static int rt5677_reg_init(struct rt5677_priv *priv)
{
int ret;
int i;
for (i = 0; i < ARRAY_SIZE(init_list); i++) {
ret = rt5677_i2c_write(priv, init_list[i].reg, init_list[i].val);
if (ret)
return ret;
}
return 0;
}
#ifdef DEBUG
static void debug_dump_5677_regs(struct rt5677_priv *priv, int swap)
{
uint i, reg_word;
/* Show all 16-bit codec regs */
for (i = 0; i < RT5677_REG_CNT; i++) {
if (i % 8 == 0)
log_debug("\nMX%02x: ", i);
rt5677_i2c_read(priv, (u8)i, &reg_word);
if (swap)
log_debug("%04x ", swap_bytes16(reg_word));
else
log_debug("%04x ", reg_word);
}
log_debug("\n");
/* Show all 16-bit 'private' codec regs */
for (i = 0; i < RT5677_PR_REG_CNT; i++) {
if (i % 8 == 0)
log_debug("\nPR%02x: ", i);
rt5677_i2c_write(priv, RT5677_PRIV_INDEX, i);
rt5677_i2c_read(priv, RT5677_PRIV_DATA, &reg_word);
if (swap)
log_debug("%04x ", swap_bytes16(reg_word));
else
log_debug("%04x ", reg_word);
}
log_debug("\n");
}
#endif /* DEBUG */
static int rt5677_hw_params(struct rt5677_priv *priv, uint bits_per_sample)
{
int ret;
switch (bits_per_sample) {
case 16:
ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DL_MASK,
0);
if (ret) {
log_debug("Error updating I2S1 Interface Ctrl reg\n");
return 1;
}
break;
default:
log_err("Illegal bits per sample %d\n", bits_per_sample);
return -EINVAL;
}
return 0;
}
/**
* rt5677_set_fmt() - set rt5677 I2S format
*
* @priv: Private driver data
* @returns 0 if OK, -ve on error
*/
static int rt5677_set_fmt(struct rt5677_priv *priv)
{
int ret = 0;
/*
* Set format here: Assumes I2S, NB_NF, CBS_CFS
*
* CBS_CFS (Codec Bit Slave/Codec Frame Slave)
*/
ret = rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_MS_MASK,
RT5677_I2S_MS_S);
/* NB_NF (Normal Bit/Normal Frame) */
ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_BP_MASK,
RT5677_I2S_BP_NOR);
/* I2S mode */
ret |= rt5677_bic_or(priv, RT5677_I2S1_SDP, RT5677_I2S_DF_MASK,
RT5677_I2S_DF_I2S);
/* A44: I2S2 (going to speaker amp) is master */
ret |= rt5677_bic_or(priv, RT5677_I2S2_SDP, RT5677_I2S_MS_MASK,
RT5677_I2S_MS_M);
if (ret) {
log_err("Error updating I2S1 Interface Ctrl reg\n");
return ret;
}
return 0;
}
/**
* rt5677_reset() - reset the audio codec
*
* @priv: Private driver data
* @returns 0 if OK, -ve on error
*/
static int rt5677_reset(struct rt5677_priv *priv)
{
int ret;
/* Reset the codec registers to their defaults */
ret = rt5677_i2c_write(priv, RT5677_RESET, RT5677_SW_RESET);
if (ret) {
log_err("Error resetting codec\n");
return ret;
}
return 0;
}
/**
* Initialise rt5677 codec device
*
* @priv: Private driver data
* @returns 0 if OK, -ve on error
*/
int rt5677_device_init(struct rt5677_priv *priv)
{
int ret;
/* Read status reg */
ret = rt5677_i2c_read(priv, RT5677_RESET);
if (ret < 0)
return ret;
log_debug("reg 00h, Software Reset & Status = 0x%04x\n", ret);
/* Reset the codec/regs */
ret = rt5677_reset(priv);
if (ret)
return ret;
ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID1);
if (ret < 0) {
log_err("Error reading vendor ID\n");
return 1;
}
log_debug("Hardware ID: %0xX\n", ret);
ret = rt5677_i2c_read(priv, RT5677_VENDOR_ID2);
if (ret < 0) {
log_err("Error reading vendor rev\n");
return 1;
}
log_debug("Hardware revision: %04x\n", ret);
return 0;
}
static int rt5677_set_params(struct udevice *dev, int interface, int rate,
int mclk_freq, int bits_per_sample,
uint channels)
{
struct rt5677_priv *priv = dev_get_priv(dev);
int ret;
/* Initialise codec regs w/static/base values, same as Linux driver */
ret = rt5677_reg_init(priv);
if (ret)
return ret;
ret = rt5677_hw_params(priv, bits_per_sample);
if (ret)
return ret;
ret = rt5677_set_fmt(priv);
if (ret)
return ret;
return 0;
}
static int rt5677_probe(struct udevice *dev)
{
struct rt5677_priv *priv = dev_get_priv(dev);
priv->dev = dev;
return rt5677_device_init(priv);
}
static const struct audio_codec_ops rt5677_ops = {
.set_params = rt5677_set_params,
};
static const struct udevice_id rt5677_ids[] = {
{ .compatible = "realtek,rt5677" },
{ }
};
U_BOOT_DRIVER(rt5677_drv) = {
.name = "rt5677",
.id = UCLASS_AUDIO_CODEC,
.of_match = rt5677_ids,
.ops = &rt5677_ops,
.probe = rt5677_probe,
.priv_auto_alloc_size = sizeof(struct rt5677_priv),
};

1428
drivers/sound/rt5677.h Normal file

File diff suppressed because it is too large Load Diff