程序项目代做,有需求私信(vue、React、Java、爬虫、电路板设计、嵌入式linux等)

Rockchip RK3399 - Codec驱动( Realtek ALC5651)

----------------------------------------------------------------------------------------------------------------------------

开发板 :NanoPC-T4开发板
eMMC :16GB
LPDDR3 :4GB
显示屏 :15.6英寸HDMI接口显示屏
u-boot :2023.04
linux  :6.3
----------------------------------------------------------------------------------------------------------------------------

Rockchip RK3399 - ASoC Codec驱动基础中我们介绍了codec驱动涉及到的数据结构以及核心API。并且已经了解到每个codec driver必须提供以下功能:

  • codec dai和pcm的配置信息:通过struct snd_soc_dai_driver描述,包括dai的能力描述和操作接口;
  • codec的控制接口:其控制接口一般是I2C或SPI。控制接口用于读写codec的寄存器。在struct snd_soc_component_driver结构体中,有大量字段描述codec的控制接口,比如read、write等;
  • Mixer和其它音频控件;
  • codec的音频操作:通过结构体struct snd_soc_dai_ops描述;
  • DAPM描述信息;
  • DAPM事件处理程序;

本节我们将会以rt5651驱动为例进行分析,驱动源码位于sound/soc/codecs/rt5651.c文件。

一、设备树配置

1.1 设备节点rt5651

我们在arch/arm64/boot/dts/rockchip/rk3399-evb.dts文件添加rt5651设备节点,该节点位于i2c1节点下:

&i2c1 {
    status = "okay";
    i2c-scl-rising-time-ns = <300>;
    i2c-scl-falling-time-ns = <15>;

    rt5651: rt5651@1a {
        #sound-dai-cells = <0>;
        compatible = "rockchip,rt5651";
        reg = <0x1a>;
        clocks = <&cru SCLK_I2S_8CH_OUT>;
        clock-names = "mclk";
        status = "okay";
    };
};

 其中:

  • status :指定设备状态为“正常”,表示该设备状态为正常运行;
  • i2c-scl-rising-time-ns:定义了SCL信号上升时间的最小值,单位是纳秒;
  • i2c-scl-falling-time-ns:定义了SCL信号下降时间的最小值,单位是纳秒;

接着定义I2C从设备节点rt5651,即音频编解码器的设备节点,其名称为 rt5651,I2C从设备7位地址为0x1a;

  • compatible:指定设备驱动程序的兼容性,即告诉内核该设备可以被哪些驱动程序所使用;
  • reg:指定了rt5651设备在I2C控制器上的设备地址;
  • clock-names:指定时钟名称,"mclk"表示MCLK时钟;
  • clocks:mclk时钟来自SCLK_I2S_8CH_OUT;
  • status :指定设备状态为“正常”,表示该设备状态为正常运行;

关于rt5651设备节点更多属性可以参考文档:Documentation/devicetree/bindings/sound/rt5651.txt。

i2c1设备节点定义在arch/arm64/boot/dts/rockchip/rk3399.dtsi,内容如下:

i2c1: i2c@ff110000 {
        compatible = "rockchip,rk3399-i2c";
        reg = <0x0 0xff110000 0x0 0x1000>;
        assigned-clocks = <&cru SCLK_I2C1>;
        assigned-clock-rates = <200000000>;
        clocks = <&cru SCLK_I2C1>, <&cru PCLK_I2C1>;
        clock-names = "i2c", "pclk";
        interrupts = <GIC_SPI 59 IRQ_TYPE_LEVEL_HIGH 0>;
        pinctrl-names = "default";
        pinctrl-0 = <&i2c1_xfer>;
        #address-cells = <1>;
        #size-cells = <0>;
        status = "disabled";
};

1.2 时钟频率

这里我们看看一下时钟频率配置:

clocks = <&cru SCLK_I2S_8CH_OUT>;
clock-names = "mclk";
1.2.1 clk_i2sout

SCLK_I2S_8CH_OUT为平台为时钟分配的特定的id,定义在drivers/clk/rockchip/clk-rk3399.c:

COMPOSITE_NODIV(SCLK_I2S_8CH_OUT, "clk_i2sout", mux_i2sout_p, CLK_SET_RATE_PARENT,
                RK3399_CLKSEL_CON(31), 2, 1, MFLAGS,
                RK3399_CLKGATE_CON(8), 12, GFLAGS)

这是composite类型的时钟,其中COMPOSITE_NODIV宏定义在drivers/clk/rockchip/clk.h:

#define COMPOSITE_NODIV(_id, cname, pnames, f, mo, ms, mw, mf,  \
                        go, gs, gf)                             \
        {                                                       \
                .id             = _id,                          \
                .branch_type    = branch_composite,             \
                .name           = cname,                        \
                .parent_names   = pnames,                       \
                .num_parents    = ARRAY_SIZE(pnames),           \
                .flags          = f,                            \
                .muxdiv_offset  = mo,                           \
                .mux_shift      = ms,                           \
                .mux_width      = mw,                           \
                .mux_flags      = mf,                           \
                .gate_offset    = go,                           \
                .gate_shift     = gs,                           \
                .gate_flags     = gf,                           \
        }

(1) 在RK3399 datasheet中,我们可以找到名字为clk_i2sout的时钟的信息,从下图可以看到它有两个父时钟,一个ID为62,可以在datasheet表中找到62代表的是clk_i2sout_src;另一个ID为64,可以在datasheet表中找到64代表的是clk_12m;

 实际上mux_i2sout_p中存放的就是这两个父时钟的名称;

PNAME(mux_i2sout_p)  = { "clk_i2sout_src", "xin12m" };

我们可以找到时钟clk_i2sout_src的定义,它是一个多路选择类型的时钟,如下所示;

MUX(0, "clk_i2sout_src", mux_i2sch_p, CLK_SET_RATE_PARENT,
RK3399_CLKSEL_CON(31), 0, 2, MFLAGS),

而xin12m应该是一个fixed rate clock(有源晶振、无源晶振)。

(2) 宏RK3399_CLKGATE_CON定义在drivers/clk/rockchip/clk.h:

#define RK3399_CLKGATE_CON(x)           ((x) * 0x4 + 0x300)

通过RK3399_CLKGATE_CON(8)可以得到寄存器偏移地址8*0x04+0x300=0x320,偏移0x320是CRU_CLKGATE_CON8寄存器。

接着我们看一下CRU_CLKGATE_CON8寄存器,CRU_CLKGATE_CON8为Internal clock gating register8,其中位[12]含义如下:

可以看到位12为clk_i2sout时钟使能位,低电平使能,高电平禁用。 那clk_i2sout到底是什么时钟呢?
RK3399平台有三路I2S(其中一路内部使用,可以不管),但是MCLK只有一个,也就是说I2S0、I2S1只有一路能占用,因此我猜测clk_i2sout应该就是MCLK信号线的时钟。
(3) 宏
RK3399_CLKSEL_CON定义在drivers/clk/rockchip/clk.h:

#define RK3399_CLKSEL_CON(x)            ((x) * 0x4 + 0x100)

通过RK3399_CLKSEL_CON(31)可以得到寄存器偏移地址31*0x04+0x100=0x17C,偏移0x17C是CRU_CLKSEL_CON31寄存器。

接着我们看一下CRU_CLKSEL_CON31寄存器,CRU_CLKSEL_CON31为Internal clock select and divide register31,其中位[2]含义如下:

可以看到位2用于clk_i2sout时钟源选择,这里需要配置为clk_i2s。

1.2.2 时钟链路

经过上面的分析,我们不难推断出clk_i2sout的时钟链路如下所示:

其中clk_i2sout_src的时钟源由clk_i2s0、clk_i2s1、clk_i2s2,其定义在mux_i2sch_p:

PNAME(mux_i2sch_p)  = { "clk_i2s0", "clk_i2s1","clk_i2s2" };

由CRU_CLKSEL_CON31寄存器的位[1:0]控制时钟源的选择:

关于时钟源clk_i2s0以及之前的时钟链路我们在Rockchip RK3399 - Platform驱动(DMA&i2s0)中介绍。

二、I2C控制器驱动

RK3399这款SOC的I2C结构,其内部有9个I2C控制器,这里我们以I2C1为例,其中I2C1_SCL连接GPIO4_A2引脚,I2C1_SDA连接GPIO4_A1引脚。

关于RK3399 I2C控制器驱动实现位于drivers/i2c/busses/i2c-rk3x.c文件,I2C控制器驱动是基于platform模型的,主要提供一个algorithm底层的I2C协议的收发函数。

在platform driver中probe函数中:

  • 动态分配i2c_adapter,并进行成员初始化,包括设置algo;
  • 初始化I2C总线所使用的的GPIO功能复用为I2C;
  • 初始化I2C控制器相关的寄存器;
  • 获取资源信息,并注册I2C中断处理函数;
  • 最后调用i2c_add_adapter将i2c_adapter注册到i2c_bus_type总线,并且注册时会:
    • 调用of_i2c_register_devices,解析I2C控制器设备节点的子设备节点,从而调用of_i2c_register_device完成I2C从设备的注册;
    • 调用i2c_scan_board_info,扫描并使用i2c_new_device注册I2C从设备。

of_i2c_register_device内部通过调用of_i2c_get_board_info函数解析设备节点rt5651可以得到如下定义的I2C从设备:

struct i2c_board_info info = {
  //  type的值是取自rt5651设备节点compatible属性值,之后的内容 ,compatible = "rockchip,rt5651"得到的就是rt5651
     .type      = "rt5651",   // 会赋值给i2c_client的name字段
     .addr      = 0x1a,
     .of_node   = rt5651设备节点,
};

然后将该I2C从设备注册到系统,更多的细节在这一节我们不去研究。有关I2C驱动的内容可以先参考linux驱动移植-I2C总线设备驱动linux驱动移植-I2C适配器驱动移植linux驱动移植-I2C驱动移植(OLED SSD1306),关于RK3399 I2C控制器驱动后面有时间再单独介绍。

三、Codec驱动

3.1 模块入口函数

我们定位到sound/soc/codecs/rt5651.c文件的最后:

module_i2c_driver(rt5651_i2c_driver);
3.1.1 module_i2c_driver

module_i2c_driver宏可以展开为相应驱动模块的init和exit接口,其定义在include/linux/i2c.h:

/**
 * module_i2c_driver() - Helper macro for registering a modular I2C driver
 * @__i2c_driver: i2c_driver struct
 *
 * Helper macro for I2C drivers which do not do anything special in module
 * init/exit. This eliminates a lot of boilerplate. Each module may only
 * use this macro once, and calling it replaces module_init() and module_exit()
 */
#define module_i2c_driver(__i2c_driver) \
        module_driver(__i2c_driver, i2c_add_driver, \
                        i2c_del_driver)
3.1.2 module_driver

module_driver定义在include/linux/device/driver.h:

/**
 * module_driver() - Helper macro for drivers that don't do anything
 * special in module init/exit. This eliminates a lot of boilerplate.
 * Each module may only use this macro once, and calling it replaces
 * module_init() and module_exit().
 *
 * @__driver: driver name
 * @__register: register function for this driver type
 * @__unregister: unregister function for this driver type
 * @...: Additional arguments to be passed to __register and __unregister.
 *
 * Use this macro to construct bus specific macros for registering
 * drivers, and do not use it on its own.
 */
#define module_driver(__driver, __register, __unregister, ...) \
static int __init __driver##_init(void) \
{ \
        return __register(&(__driver) , ##__VA_ARGS__); \
} \
module_init(__driver##_init); \
static void __exit __driver##_exit(void) \
{ \
        __unregister(&(__driver) , ##__VA_ARGS__); \
} \
module_exit(__driver##_exit);
3.1.3 展开后

因此如下定义:

module_i2c_driver(rt5651_i2c_driver);

经过上述宏的作用之后,就成为如下形式:

static int __init ov4689_i2c_driver_init(void)
{
    return i2c_add_driver(&rt5651_i2c_driver);
}
 
static void __exit ov4689_i2c_driver_exit(void)
{
    return i2c_del_driver(&rt5651_i2c_driver);
}

其中i2c_add_driver函数用于注册I2C设备驱动。

3.2 rt5651_i2c_driver 

这里我们需要关注一下i2c_driver结构体变量rt5651_i2c_driver :

static struct i2c_driver rt5651_i2c_driver = {
        .driver = {
                .name = "rt5651",
                .acpi_match_table = ACPI_PTR(rt5651_acpi_match),
                .of_match_table = of_match_ptr(rt5651_of_match),  // 用于设备树匹配
        },
        .probe_new = rt5651_i2c_probe,
        .id_table = rt5651_i2c_id,
};

其成员:

  • driver.of_match_table:用于设备树匹配;
  • probe:当I2C驱动和I2C从设备信息匹配成功之后,就会调用probe函数;
  • id_table:id列表,用于和I2C从设备名称进行匹配;
3.2.1 rt5651_of_match

如果使用了设备树,rt5651_of_match被定义为:

#if defined(CONFIG_OF)
static const struct of_device_id rt5651_of_match[] = {
        { .compatible = "realtek,rt5651", },   // 用来匹配的I2C从设备,匹配设备节点rt5651
        {},  /* 最后一个必须为空,表示结束 */
};
MODULE_DEVICE_TABLE(of, rt5651_of_match);
#endif

由于在I2C控制器注册的时候为声卡设备注册了I2C从设备(对应数据结构struct i2c_client),其名称为rt5651,因此会与I2C从设备驱动中rt5651_of_match匹配失败。

3.2.2 rt5651_i2c_id

i2c_device_id中存放的是和I2C驱动匹配的I2C从设备的名称,以rt5651_i2c_id为例:

static const struct i2c_device_id rt5651_i2c_id[] = {
        { "rt5651", 0 },                // 用来匹配的I2C从设备
        { }  /* 最后一个必须为空,表示结束 */
};

由于在I2C控制器注册的时候为声卡设备注册了I2C从设备(对应数据结构struct i2c_client),其名称为rt5651,因此会与I2C从设备驱动中的rt5651_i2c_id匹配成功,从而进入执行probe探测函数;

3.2.3 rt5651_i2c_probe

probe探测函数rt5651_i2c_probe定义如下:

static int rt5651_i2c_probe(struct i2c_client *i2c)  // 参数为I2C从设备
{
        struct rt5651_priv *rt5651;
        int ret;
        int err;

        rt5651 = devm_kzalloc(&i2c->dev, sizeof(*rt5651),   // 动态申请内存,数据结构类型为struct rt5651_priv
                                GFP_KERNEL);
        if (NULL == rt5651)
                return -ENOMEM;

        i2c_set_clientdata(i2c, rt5651); //  i2c->dev.driver_data = rt5651 设置为驱动数据

        rt5651->regmap = devm_regmap_init_i2c(i2c, &rt5651_regmap);  // 注册regmap实例
        if (IS_ERR(rt5651->regmap)) {
                ret = PTR_ERR(rt5651->regmap);
                dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
                        ret);
                return ret;
        }

    // 读取RT5651_DEVICE_ID寄存器的值,RT5651_DEVICE_ID值为0xff,寄存器地址0xff存放的是设备ID,数据位宽为16位 err
= regmap_read(rt5651->regmap, RT5651_DEVICE_ID, &ret);if (err) // 读取失败 return err; if (ret != RT5651_DEVICE_ID_VALUE) { // 0x6281 dev_err(&i2c->dev, "Device with ID register %#x is not rt5651\n", ret); return -ENODEV; } regmap_write(rt5651->regmap, RT5651_RESET, 0); // 寄存器地址0x00为软件复位寄存器,写入0x00将会复位所有寄存器的 ret = regmap_register_patch(rt5651->regmap, init_list, // 用于初始化rt5651,向一组寄存器中写入值 ARRAY_SIZE(init_list)); if (ret != 0) dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret); rt5651->irq = i2c->irq; // I2C从设备所使用的的中断编号; 由于rt5651设备节点中并没有配置中断,所以i2c->irq默认值为0 rt5651->hp_mute = true; INIT_DELAYED_WORK(&rt5651->bp_work, rt5651_button_press_work); // 初始化延迟的工作rt5651->bp_work,设置工作函数为rt5651_button_press_work INIT_WORK(&rt5651->jack_detect_work, rt5651_jack_detect_work); // 初始化工作rt5651->jack_detect_work,设置工作函数为rt5651_jack_detect_work /* Make sure work is stopped on probe-error / remove */ ret = devm_add_action_or_reset(&i2c->dev, rt5651_cancel_work, rt5651); if (ret) return ret; ret = devm_request_irq(&i2c->dev, rt5651->irq, rt5651_irq, // 申请I2C中断,中断处理函数为rt5651_irq,;因为没有配置中断,所以这里中断会申请失败 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_NO_AUTOEN, "rt5651", rt5651); if (ret) { dev_warn(&i2c->dev, "Failed to reguest IRQ %d: %d\n", rt5651->irq, ret); rt5651->irq = -ENXIO; } ret = devm_snd_soc_register_component(&i2c->dev, // 注册component &soc_component_dev_rt5651, rt5651_dai, ARRAY_SIZE(rt5651_dai)); return ret; }

(1) 动态申请内存,数据结构类型为struct rt5651_priv,并调用i2c_set_clientdata将其设置为驱动数据;

(2) 调用devm_regmap_init_i2c注册regmap实例,这样i2c驱动驱动就可以正常调用regmap_write和regmap_read函数进行i2c数据传输了;

(3) 读取rt5651设备寄存器地址0xff的值,对于rt5651芯片寄存器地址0xff存放的是设备ID,因此读取到的为0x6281;

(4) 向rt5651软件复位寄存器地址0x00写入0,将所有寄存器的值复位;

(5) 调用regmap_register_patch向一组寄存器中写入值;其中init_list设置为:

static const struct reg_sequence init_list[] = {
        {RT5651_PR_BASE + 0x3d, 0x3e00},
};

regmap_register_patch定义在drivers/base/regmap/regmap.c:

/**
 * regmap_register_patch - Register and apply register updates to be applied
 *                         on device initialistion
 *
 * @map: Register map to apply updates to.
 * @regs: Values to update.
 * @num_regs: Number of entries in regs.
 *
 * Register a set of register updates to be applied to the device
 * whenever the device registers are synchronised with the cache and
 * apply them immediately.  Typically this is used to apply
 * corrections to be applied to the device defaults on startup, such
 * as the updates some vendors provide to undocumented registers.
 *
 * The caller must ensure that this function cannot be called
 * concurrently with either itself or regcache_sync().
 */
int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
                          int num_regs)
{
        struct reg_sequence *p;
        int ret;
        bool bypass;

        if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
            num_regs))
                return 0;

        p = krealloc(map->patch,
                     sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
                     GFP_KERNEL);
        if (p) {
                memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
                map->patch = p;
                map->patch_regs += num_regs;
        } else {
                return -ENOMEM;
        }

        map->lock(map->lock_arg);

        bypass = map->cache_bypass;

        map->cache_bypass = true;
        map->async = true;

        ret = _regmap_multi_reg_write(map, regs, num_regs);  // 写入多个寄存器

        map->async = false;
        map->cache_bypass = bypass;

        map->unlock(map->lock_arg);

        regmap_async_complete(map);

        return ret;
}
View Code

(6) 初始化rt5651成员irq、hp_mute;初始化延迟的工作rt5651->bp_work,设置工作函数为rt5651_button_press_work;初始化工作rt5651->jack_detect_work,设置工作函数为rt5651_jack_detect_work;

(7) 调用devm_add_action_or_reset函数Make sure work is stopped on probe-error / remove;

static void rt5651_cancel_work(void *data)
{
        struct rt5651_priv *rt5651 = data;

        cancel_work_sync(&rt5651->jack_detect_work);
        cancel_delayed_work_sync(&rt5651->bp_work);
}

(8) 申请I2C中断,中断处理函数为rt5651_irq;

static irqreturn_t rt5651_irq(int irq, void *data)
{
        struct rt5651_priv *rt5651 = data;

        queue_work(system_power_efficient_wq, &rt5651->jack_detect_work);

        return IRQ_HANDLED;
}

由于我们设备节点rt5651中并没有配置中断,因此申请中断会失败,内核启动的时候也会输出相关错误信息;其中rt5651为模块的名称,1-001a为i2c_client->dev设备的名称;

[    3.465917] rt5651 1-001a: Failed to reguest IRQ 0: -22

(9) 调用devm_snd_soc_register_component注册的component,该函数会动态申请一个component,并将其添加到全局链表component_list中,同时会建立dai_driver与component的关系。

注册component完成后,snd_soc_dai,snd_soc_dai_driver、snd_soc_component、snd_soc_component_driver之间的关系如下图:

其中:

  • 新建的snd_soc_component的名称为i2c从设备对应的struct device_driver、struct device实例的名字拼接而成,即rt5651.1-001a;
  • snd_soc_component的dai_list链表包含两个dai,第一个dai的名称为rt5651-aif1,第二个dai的名称为rt5651-aif2;
  • 每个dai对应一个dai driver,第一个dai driver的名称为rt5651-aif1,第二个dai driver的名称为rt5651-aif2;

3.3 总结

我们在分析了platform驱动的源码之后,我们大致可以得到一个结论,codec驱动注册流程主要包含一下几个步骤:

(1) 构造一个struct snd_soc_component_driver实例,比如这里的soc_component_dev_rt5651,用于描述codec driver;需要初始化成员name、controls、dapm_widgets、dapm_routes等;

(2) 构造一个struct snd_soc_dai_driver,比如这里的rt5651_dai数组,用于描述dai和 pcm的能力和操作;需要初始化成员name、probe、playback、capture、ops等;

(3) 调用devm_snd_soc_register_component注册component;

四、soc_component_dev_rt5651

devm_snd_soc_register_component函数第二个参数为soc_component_dev_rt5651:

static const struct snd_soc_component_driver soc_component_dev_rt5651 = {
        .probe                  = rt5651_probe,
        .suspend                = rt5651_suspend,
        .resume                 = rt5651_resume,
        .set_bias_level         = rt5651_set_bias_level,
        .set_jack               = rt5651_set_jack,
        .controls               = rt5651_snd_controls,                 // kcontrol定义
        .num_controls           = ARRAY_SIZE(rt5651_snd_controls), 
        .dapm_widgets           = rt5651_dapm_widgets,                 // widget定义 
        .num_dapm_widgets       = ARRAY_SIZE(rt5651_dapm_widgets),
        .dapm_routes            = rt5651_dapm_routes,                  // route定义  
        .num_dapm_routes        = ARRAY_SIZE(rt5651_dapm_routes),
        .use_pmdown_time        = 1,
        .endianness             = 1,
};

其中controls、dapm_widgets、dapm_routes是与dapm相关的,可以用来表述codec内部的音频路径,具体参考Rockchip RK3399 - DAPM Widget&Route&Path

这里我们简单提一下dapm:

(1) 当音频路径发生改变(比如上层使用tinymix工具设置音频通路)时,或发生数据流事件(比如启动或停止播放)时,都会触发dapm去遍历所有邻近的widget,检查是否存在完整的音频路径,如果存在完整的音频路径,则该路径上面的所有widget都是需要上电的,其他widget则下电;

(2) widget的上下电都是dapm根据策略自主控制的,外部无法干预,可以说dapm是一个专门为音频系统设计的自成体系的电源管理模块,独立于Linux电源管理之外。即使SoC休眠了,codec仍可以在正常工作,试想下这个情景:语音通话,modem dai连接到codec dai,语音数据不经过SoC,因此这种情形下SoC可以进入睡眠以降低功耗,只保持Codec正常工作就行了。

在Macine驱动中会进行ASoC声卡的注册,其中会执行soc_probe_component(card, component),即进行component的探测工作,具体流程如下:

  • 注册component->driver->dapm_widgets到声卡card的widgets链表中;
  • 执行component->driver->probe(component);
  • 根据component->driver->controls数组创建并添加多个kcontrol到声卡card的controls链表;
  • 遍历component->driver->dapm_routes数组,调用snd_soc_dapm_add_routes将snd_soc_dapm_route动态地生成所需要的snd_soc_dapm_path结构,然后将snd_soc_dapm_path注册到声卡card的paths链表;
  • 将component添加到声卡component_dev_list链表;

4.1 DAPM描述

ALC5651功能框图如下所示,该图来自ALC5651 datasheet:

由于这种图只是一张框图,无法看到音频数据流具体路径细节,因此我们在datasheet中又找到了如下一张图:

上图中我们使用箭头标识了一条用于多媒体音频播放右声道的路径,音频通路是:

  • AIF1 Playback(snd_soc_dapm_dai_in类型的playback dai widget) --> AIF1RX :AIF表示音频数字接口;
  • AIF1RX --> IF1 DAC;
  • IF1 DAC --> IF1 DAC1 R;
  • IF1 DAC1 R --> DAC MIXR:通过rt5651_dac_r_mix(名称为INF1 Switch)控制通断,由MX29寄存器位14来实现静音控制(0非静音,1静音);
  •  DAC MIXR -->  Audio DSP;
  •  Audio DSP --> Stereo DAC MIXR:通过rt5651_sto_dac_r_mix(名称DAC R1 Switch)控制通断,由MX2A寄存器位6来实现静音控制(0非静音,1静音);
  •  Stereo DAC MIXR --> DAC R1;
  • DAC R1 --> OUT MIXR :通过rt5651_out_r_mix(名称为DAC R1 Switch)控制通断,由MX52寄存器位0来实现静音控制(0非静音,1静音);
  • OUT MIXR -->  HPOVOL R:通过hpovol_r_control(名称为Switch)控制通断,由MX02寄存器位6来实现静音控制(0非静音,1静音);
  •  HPOVOL R --> HPOR MIX:通过rt5651_hpo_mix(名称为HPO MIX HPVOL Switch)控制通断,由MX45寄存器位13来实现静音控制(0非静音,1静音);
  • HPOR MIX --> HP Amp;
  • HP Amp -> HPO R Playback:通过hpo_r_mute_control(名称为Switch)控制通断,由MX02寄存器位7来实现静音控制(0非静音,1静音);
  • HPO R Playback --> HPOR 
  • HPOR  --> Headphones(最后一个path定义在Machine驱动中)

其中红色部分表示有相应的kcontrol,即需要switch打开,在该路径中 HPOL 为SND_SOC_DAPM_EP_SINK类型端点,但是路径中并没有SND_SOC_DAPM_EP_SOURCE类型端点。

此外对应的左声道音频路径:

  • AIF1 Playback(snd_soc_dapm_dai_in类型的playback dai widget) --> AIF1RX :AIF表示音频数字接口;
  • AIF1RX --> IF1 DAC;
  • IF1 DAC --> IF1 DAC1 L;
  • IF1 DAC1 L --> DAC MIXL:通过rt5651_dac_l_mix(名称为INF1 Switch)控制通断,由MX29寄存器位6来实现静音控制(0非静音,1静音);
  •  DAC MIXL --> Audio DSP;
  • Audio DSP --> Stereo DAC MIXL:通过rt5651_sto_dac_l_mix(名称DAC L1 Switch)控制通断,由MX2A寄存器位14来实现静音控制(0非静音,1静音);
  •  Stereo DAC MIXL --> DAC L1;
  • DAC L1 --> OUT MIXL :通过rt5651_out_l_mix(名称为DAC L1 Switch)控制通断,由MX4F寄存器位0来实现静音控制(0非静音,1静音);
  • OUT MIXL -->  HPOVOL L:通过hpovol_l_control(名称为Switch)控制通断,由MX02寄存器位14来实现静音控制(0非静音,1静音);
  •  HPOVOL L --> HPOL MIX:通过rt5651_hpo_mix(名称为HPO MIX HPVOL Switch)控制通断,由MX45寄存器位13来实现静音控制(0非静音,1静音);
  • HPOL MIX --> HP Amp;
  • HP Amp -> HPO L Playback:通过hpo_l_mute_control(名称为Switch)控制通断,由MX02寄存器位15来实现静音控制(0非静音,1静音);
  • HPO L Playback --> HPOL ;
  • HPOL  --> Headphones(最后一个path定义在Machine驱动中)
4.1.1 定义kcontrol

在sound/soc/codecs/rt5651.c定义了大量的kcontrol,包括普通kcontrol和dapm kcontrol:

static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
static const DECLARE_TLV_DB_MINMAX(dac_vol_tlv, -6562, 0);
static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
static const DECLARE_TLV_DB_MINMAX(adc_vol_tlv, -1762, 3000);
static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);

/* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
static const DECLARE_TLV_DB_RANGE(bst_tlv,
        0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
        1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
        2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
        3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
        6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
        7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
        8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
);

/* Interface data select */
static const char * const rt5651_data_select[] = {
        "Normal", "Swap", "left copy to right", "right copy to left"};

static SOC_ENUM_SINGLE_DECL(rt5651_if2_dac_enum, RT5651_DIG_INF_DATA,
                                RT5651_IF2_DAC_SEL_SFT, rt5651_data_select);

static SOC_ENUM_SINGLE_DECL(rt5651_if2_adc_enum, RT5651_DIG_INF_DATA,
                                RT5651_IF2_ADC_SEL_SFT, rt5651_data_select);


static const struct snd_kcontrol_new rt5651_snd_controls[] = {
        /* Headphone Output Volume */
        SOC_DOUBLE_TLV("HP Playback Volume", RT5651_HP_VOL,
                RT5651_L_VOL_SFT, RT5651_R_VOL_SFT, 39, 1, out_vol_tlv),
        /* OUTPUT Control */
        SOC_DOUBLE_TLV("OUT Playback Volume", RT5651_LOUT_CTRL1,
                RT5651_L_VOL_SFT, RT5651_R_VOL_SFT, 39, 1, out_vol_tlv),

        /* DAC Digital Volume */
        SOC_DOUBLE("DAC2 Playback Switch", RT5651_DAC2_CTRL,
                RT5651_M_DAC_L2_VOL_SFT, RT5651_M_DAC_R2_VOL_SFT, 1, 1),
        SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5651_DAC1_DIG_VOL,
                        RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
                        175, 0, dac_vol_tlv),
        SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5651_DAC2_DIG_VOL,
                        RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
                        175, 0, dac_vol_tlv),
        /* IN1/IN2/IN3 Control */
        SOC_SINGLE_TLV("IN1 Boost", RT5651_IN1_IN2,
                RT5651_BST_SFT1, 8, 0, bst_tlv),
        SOC_SINGLE_TLV("IN2 Boost", RT5651_IN1_IN2,
                RT5651_BST_SFT2, 8, 0, bst_tlv),
        SOC_SINGLE_TLV("IN3 Boost", RT5651_IN3,
                RT5651_BST_SFT1, 8, 0, bst_tlv),
        /* INL/INR Volume Control */
        SOC_DOUBLE_TLV("IN Capture Volume", RT5651_INL1_INR1_VOL,
                        RT5651_INL_VOL_SFT, RT5651_INR_VOL_SFT,
                        31, 1, in_vol_tlv),
        /* ADC Digital Volume Control */
        SOC_DOUBLE("ADC Capture Switch", RT5651_ADC_DIG_VOL,
                RT5651_L_MUTE_SFT, RT5651_R_MUTE_SFT, 1, 1),
        SOC_DOUBLE_TLV("ADC Capture Volume", RT5651_ADC_DIG_VOL,
                        RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
                        127, 0, adc_vol_tlv),
        SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5651_ADC_DATA,
                        RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
                        127, 0, adc_vol_tlv),
        /* ADC Boost Volume Control */
        SOC_DOUBLE_TLV("ADC Boost Gain", RT5651_ADC_BST_VOL,
                        RT5651_ADC_L_BST_SFT, RT5651_ADC_R_BST_SFT,
                        3, 0, adc_bst_tlv),

        /* ASRC */
        SOC_SINGLE("IF1 ASRC Switch", RT5651_PLL_MODE_1,
                RT5651_STO1_T_SFT, 1, 0),
        SOC_SINGLE("IF2 ASRC Switch", RT5651_PLL_MODE_1,
                RT5651_STO2_T_SFT, 1, 0),
        SOC_SINGLE("DMIC ASRC Switch", RT5651_PLL_MODE_1,
                RT5651_DMIC_1_M_SFT, 1, 0),

        SOC_ENUM("ADC IF2 Data Switch", rt5651_if2_adc_enum),
        SOC_ENUM("DAC IF2 Data Switch", rt5651_if2_dac_enum),
};

/* Digital Mixer */
static const struct snd_kcontrol_new rt5651_sto1_adc_l_mix[] = {
        SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO1_ADC_MIXER,
                        RT5651_M_STO1_ADC_L1_SFT, 1, 1),
        SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO1_ADC_MIXER,
                        RT5651_M_STO1_ADC_L2_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_sto1_adc_r_mix[] = {
        SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO1_ADC_MIXER,
                        RT5651_M_STO1_ADC_R1_SFT, 1, 1),
        SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO1_ADC_MIXER,
                        RT5651_M_STO1_ADC_R2_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_sto2_adc_l_mix[] = {
        SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO2_ADC_MIXER,
                        RT5651_M_STO2_ADC_L1_SFT, 1, 1),
        SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO2_ADC_MIXER,
                        RT5651_M_STO2_ADC_L2_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_sto2_adc_r_mix[] = {
        SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO2_ADC_MIXER,
                        RT5651_M_STO2_ADC_R1_SFT, 1, 1),
        SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO2_ADC_MIXER,
                        RT5651_M_STO2_ADC_R2_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_dac_l_mix[] = {
        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,
                        RT5651_M_ADCMIX_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,
                        RT5651_M_IF1_DAC_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_dac_r_mix[] = {
        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,
                        RT5651_M_ADCMIX_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,
                        RT5651_M_IF1_DAC_R_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_sto_dac_l_mix[] = {
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_STO_DAC_MIXER,
                        RT5651_M_DAC_L1_MIXL_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_STO_DAC_MIXER,
                        RT5651_M_DAC_L2_MIXL_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_STO_DAC_MIXER,
                        RT5651_M_DAC_R1_MIXL_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_sto_dac_r_mix[] = {
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_STO_DAC_MIXER,
                        RT5651_M_DAC_R1_MIXR_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_STO_DAC_MIXER,
                        RT5651_M_DAC_R2_MIXR_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_STO_DAC_MIXER,
                        RT5651_M_DAC_L1_MIXR_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_dd_dac_l_mix[] = {
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_DD_MIXER,
                        RT5651_M_STO_DD_L1_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_DD_MIXER,
                        RT5651_M_STO_DD_L2_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_DD_MIXER,
                        RT5651_M_STO_DD_R2_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_dd_dac_r_mix[] = {
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_DD_MIXER,
                        RT5651_M_STO_DD_R1_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_DD_MIXER,
                        RT5651_M_STO_DD_R2_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_DD_MIXER,
                        RT5651_M_STO_DD_L2_R_SFT, 1, 1),
};

/* Analog Input Mixer */
static const struct snd_kcontrol_new rt5651_rec_l_mix[] = {
        SOC_DAPM_SINGLE("INL1 Switch", RT5651_REC_L2_MIXER,
                        RT5651_M_IN1_L_RM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST3 Switch", RT5651_REC_L2_MIXER,
                        RT5651_M_BST3_RM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST2 Switch", RT5651_REC_L2_MIXER,
                        RT5651_M_BST2_RM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST1 Switch", RT5651_REC_L2_MIXER,
                        RT5651_M_BST1_RM_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_rec_r_mix[] = {
        SOC_DAPM_SINGLE("INR1 Switch", RT5651_REC_R2_MIXER,
                        RT5651_M_IN1_R_RM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST3 Switch", RT5651_REC_R2_MIXER,
                        RT5651_M_BST3_RM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST2 Switch", RT5651_REC_R2_MIXER,
                        RT5651_M_BST2_RM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST1 Switch", RT5651_REC_R2_MIXER,
                        RT5651_M_BST1_RM_R_SFT, 1, 1),
};

/* Analog Output Mixer */

static const struct snd_kcontrol_new rt5651_out_l_mix[] = {
        SOC_DAPM_SINGLE("BST1 Switch", RT5651_OUT_L3_MIXER,
                        RT5651_M_BST1_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST2 Switch", RT5651_OUT_L3_MIXER,
                        RT5651_M_BST2_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("INL1 Switch", RT5651_OUT_L3_MIXER,
                        RT5651_M_IN1_L_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("REC MIXL Switch", RT5651_OUT_L3_MIXER,
                        RT5651_M_RM_L_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_OUT_L3_MIXER,
                        RT5651_M_DAC_L1_OM_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_out_r_mix[] = {
        SOC_DAPM_SINGLE("BST2 Switch", RT5651_OUT_R3_MIXER,
                        RT5651_M_BST2_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST1 Switch", RT5651_OUT_R3_MIXER,
                        RT5651_M_BST1_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("INR1 Switch", RT5651_OUT_R3_MIXER,
                        RT5651_M_IN1_R_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("REC MIXR Switch", RT5651_OUT_R3_MIXER,
                        RT5651_M_RM_R_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_OUT_R3_MIXER,
                        RT5651_M_DAC_R1_OM_R_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_hpo_mix[] = {
        SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5651_HPO_MIXER,
                        RT5651_M_DAC1_HM_SFT, 1, 1),
        SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5651_HPO_MIXER,
                        RT5651_M_HPVOL_HM_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_lout_mix[] = {
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_LOUT_MIXER,
                        RT5651_M_DAC_L1_LM_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_LOUT_MIXER,
                        RT5651_M_DAC_R1_LM_SFT, 1, 1),
        SOC_DAPM_SINGLE("OUTVOL L Switch", RT5651_LOUT_MIXER,
                        RT5651_M_OV_L_LM_SFT, 1, 1),
        SOC_DAPM_SINGLE("OUTVOL R Switch", RT5651_LOUT_MIXER,
                        RT5651_M_OV_R_LM_SFT, 1, 1),
};

static const struct snd_kcontrol_new outvol_l_control =
        SOC_DAPM_SINGLE("Switch", RT5651_LOUT_CTRL1,
                        RT5651_VOL_L_SFT, 1, 1);

static const struct snd_kcontrol_new outvol_r_control =
        SOC_DAPM_SINGLE("Switch", RT5651_LOUT_CTRL1,
                        RT5651_VOL_R_SFT, 1, 1);

static const struct snd_kcontrol_new lout_l_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_LOUT_CTRL1,
                                    RT5651_L_MUTE_SFT, 1, 1);

static const struct snd_kcontrol_new lout_r_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_LOUT_CTRL1,
                                    RT5651_R_MUTE_SFT, 1, 1);

static const struct snd_kcontrol_new hpovol_l_control =
        SOC_DAPM_SINGLE("Switch", RT5651_HP_VOL,
                        RT5651_VOL_L_SFT, 1, 1);

static const struct snd_kcontrol_new hpovol_r_control =
        SOC_DAPM_SINGLE("Switch", RT5651_HP_VOL,
                        RT5651_VOL_R_SFT, 1, 1);

static const struct snd_kcontrol_new hpo_l_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_HP_VOL,
                                    RT5651_L_MUTE_SFT, 1, 1);

static const struct snd_kcontrol_new hpo_r_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_HP_VOL,
                                    RT5651_R_MUTE_SFT, 1, 1);

/* Stereo ADC source */
static const char * const rt5651_stereo1_adc1_src[] = {"DD MIX", "ADC"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_stereo1_adc1_enum, RT5651_STO1_ADC_MIXER,
        RT5651_STO1_ADC_1_SRC_SFT, rt5651_stereo1_adc1_src);

static const struct snd_kcontrol_new rt5651_sto1_adc_l1_mux =
        SOC_DAPM_ENUM("Stereo1 ADC L1 source", rt5651_stereo1_adc1_enum);

static const struct snd_kcontrol_new rt5651_sto1_adc_r1_mux =
        SOC_DAPM_ENUM("Stereo1 ADC R1 source", rt5651_stereo1_adc1_enum);

static const char * const rt5651_stereo1_adc2_src[] = {"DMIC", "DD MIX"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_stereo1_adc2_enum, RT5651_STO1_ADC_MIXER,
        RT5651_STO1_ADC_2_SRC_SFT, rt5651_stereo1_adc2_src);

static const struct snd_kcontrol_new rt5651_sto1_adc_l2_mux =
        SOC_DAPM_ENUM("Stereo1 ADC L2 source", rt5651_stereo1_adc2_enum);

static const struct snd_kcontrol_new rt5651_sto1_adc_r2_mux =
        SOC_DAPM_ENUM("Stereo1 ADC R2 source", rt5651_stereo1_adc2_enum);

/* Mono ADC source */
static const char * const rt5651_sto2_adc_l1_src[] = {"DD MIXL", "ADCL"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_sto2_adc_l1_enum, RT5651_STO1_ADC_MIXER,
        RT5651_STO2_ADC_L1_SRC_SFT, rt5651_sto2_adc_l1_src);

static const struct snd_kcontrol_new rt5651_sto2_adc_l1_mux =
        SOC_DAPM_ENUM("Stereo2 ADC1 left source", rt5651_sto2_adc_l1_enum);

static const char * const rt5651_sto2_adc_l2_src[] = {"DMIC L", "DD MIXL"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_sto2_adc_l2_enum, RT5651_STO1_ADC_MIXER,
        RT5651_STO2_ADC_L2_SRC_SFT, rt5651_sto2_adc_l2_src);

static const struct snd_kcontrol_new rt5651_sto2_adc_l2_mux =
        SOC_DAPM_ENUM("Stereo2 ADC2 left source", rt5651_sto2_adc_l2_enum);

static const char * const rt5651_sto2_adc_r1_src[] = {"DD MIXR", "ADCR"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_sto2_adc_r1_enum, RT5651_STO1_ADC_MIXER,
        RT5651_STO2_ADC_R1_SRC_SFT, rt5651_sto2_adc_r1_src);

static const struct snd_kcontrol_new rt5651_sto2_adc_r1_mux =
        SOC_DAPM_ENUM("Stereo2 ADC1 right source", rt5651_sto2_adc_r1_enum);

static const char * const rt5651_sto2_adc_r2_src[] = {"DMIC R", "DD MIXR"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_sto2_adc_r2_enum, RT5651_STO1_ADC_MIXER,
        RT5651_STO2_ADC_R2_SRC_SFT, rt5651_sto2_adc_r2_src);

static const struct snd_kcontrol_new rt5651_sto2_adc_r2_mux =
        SOC_DAPM_ENUM("Stereo2 ADC2 right source", rt5651_sto2_adc_r2_enum);

/* DAC2 channel source */

static const char * const rt5651_dac_src[] = {"IF1", "IF2"};

static SOC_ENUM_SINGLE_DECL(rt5651_dac_l2_enum, RT5651_DAC2_CTRL,
                                RT5651_SEL_DAC_L2_SFT, rt5651_dac_src);

static const struct snd_kcontrol_new rt5651_dac_l2_mux =
        SOC_DAPM_ENUM("DAC2 left channel source", rt5651_dac_l2_enum);

static SOC_ENUM_SINGLE_DECL(
        rt5651_dac_r2_enum, RT5651_DAC2_CTRL,
        RT5651_SEL_DAC_R2_SFT, rt5651_dac_src);

static const struct snd_kcontrol_new rt5651_dac_r2_mux =
        SOC_DAPM_ENUM("DAC2 right channel source", rt5651_dac_r2_enum);

/* IF2_ADC channel source */

static const char * const rt5651_adc_src[] = {"IF1 ADC1", "IF1 ADC2"};

static SOC_ENUM_SINGLE_DECL(rt5651_if2_adc_src_enum, RT5651_DIG_INF_DATA,
                                RT5651_IF2_ADC_SRC_SFT, rt5651_adc_src);

static const struct snd_kcontrol_new rt5651_if2_adc_src_mux =
        SOC_DAPM_ENUM("IF2 ADC channel source", rt5651_if2_adc_src_enum);

/* PDM select */
static const char * const rt5651_pdm_sel[] = {"DD MIX", "Stereo DAC MIX"};

static SOC_ENUM_SINGLE_DECL(
        rt5651_pdm_l_sel_enum, RT5651_PDM_CTL,
        RT5651_PDM_L_SEL_SFT, rt5651_pdm_sel);

static SOC_ENUM_SINGLE_DECL(
        rt5651_pdm_r_sel_enum, RT5651_PDM_CTL,
        RT5651_PDM_R_SEL_SFT, rt5651_pdm_sel);

static const struct snd_kcontrol_new rt5651_pdm_l_mux =
        SOC_DAPM_ENUM("PDM L select", rt5651_pdm_l_sel_enum);

static const struct snd_kcontrol_new rt5651_pdm_r_mux =
        SOC_DAPM_ENUM("PDM R select", rt5651_pdm_r_sel_enum);
View Code

有关kcontrol辅助定义宏我们在Rockchip RK3399 - ASoC 声卡之Control设备 kcontrol章节中已经介绍,比如:

  • SOC_SINGLE:最简单的控件了,这种控件只有一个控制量,比如一个开关,或者是一个数值变量(比如Codec中某个频率,FIFO大小等等);
  • SOC_SINGLE_TLV:是SOC_SINGLE的一种扩展,主要用于定义那些有增益控制的控件,例如音量控制器,EQ均衡器等等;
  • SOC_DOUBLE:与SOC_SINGLE相对应,区别是SOC_SINGLE只控制一个变量,而SOC_DOUBLE则可以同时在一个寄存器中控制两个相似的变量,最常用的就是用于一些立体声的控件;
  • SOC_DOUBLE_TLV:与SOC_SINGLE_TLV对应的立体声版本,通常用于立体声音量控件的定义;
  • SOC_ENUM:用于定义一个定义一个Mux控件;

关dapm kcontrol辅助定义宏我们在Rockchip RK3399 - DAPM Widget&Route&Path kcontrol章节中已经介绍,相比普通的kcontrol控件,dapm的kcontrol只是把info,get,put回调函数换掉了,比如:SOC_DAPM_SINGLE:对应普通控件的SOC_SINGLE.

在这些定义中我们可以找到位于多媒体音频播放右声道路径上的dapm kcontrol;

static const struct snd_kcontrol_new rt5651_dac_r_mix[] = {
        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,   // 寄存器配置为RT5651_AD_DA_MIXER=0x29,偏移位配置为7
                        RT5651_M_ADCMIX_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,         // 寄存器配置为RT5651_AD_DA_MIXER=0x29,偏移位配置为6 
                        RT5651_M_IF1_DAC_R_SFT, 1, 1),
};         

static const struct snd_kcontrol_new rt5651_sto_dac_r_mix[] = {
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_STO_DAC_MIXER, // 寄存器配置为RT5651_STO_DAC_MIXER=0x2a,偏移位配置为6
                        RT5651_M_DAC_R1_MIXR_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_STO_DAC_MIXER, // 寄存器配置为RT5651_STO_DAC_MIXER=0x2a,偏移位配置为4
                        RT5651_M_DAC_R2_MIXR_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_STO_DAC_MIXER, // 寄存器配置为RT5651_STO_DAC_MIXER=0x2a,偏移位配置为1
                        RT5651_M_DAC_L1_MIXR_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_out_r_mix[] = {
        SOC_DAPM_SINGLE("BST2 Switch", RT5651_OUT_R3_MIXER,    // 寄存器配置为RT5651_OUT_R3_MIXER=0x52,偏移位配置为5
                        RT5651_M_BST2_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST1 Switch", RT5651_OUT_R3_MIXER,    // 寄存器配置为RT5651_OUT_R3_MIXER=0x52,偏移位配置为6
                        RT5651_M_BST1_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("INR1 Switch", RT5651_OUT_R3_MIXER,    // 寄存器配置为RRT5651_OUT_R3_MIXER=0x52,偏移位配置为4
                        RT5651_M_IN1_R_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("REC MIXR Switch", RT5651_OUT_R3_MIXER,   // 寄存器配置为RT5651_OUT_R3_MIXER=0x4f,偏移位配置为3
                        RT5651_M_RM_R_OM_R_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_OUT_R3_MIXER,     // 寄存器配置为RT5651_OUT_R3_MIXER,偏移位配置为0
                        RT5651_M_DAC_R1_OM_R_SFT, 1, 1),
};

static const struct snd_kcontrol_new hpovol_r_control =
        SOC_DAPM_SINGLE("Switch", RT5651_HP_VOL,     // 寄存器配置为RT5651_HP_VOL=0x02,偏移位配置为6
                        RT5651_VOL_R_SFT, 1, 1);

static const struct snd_kcontrol_new rt5651_hpo_mix[] = {
        SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5651_HPO_MIXER,    // 寄存器配置为RT5651_HPO_MIXER=0x45,偏移位配置为14
                        RT5651_M_DAC1_HM_SFT, 1, 1),
        SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5651_HPO_MIXER,   // 寄存器配置为RT5651_HPO_MIXER=0x45,偏移位配置为13
                        RT5651_M_HPVOL_HM_SFT, 1, 1),
};

static const struct snd_kcontrol_new hpo_r_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_HP_VOL,      // 寄存器配置为RT5651_HP_VOL=0x02,偏移位配置为7
                                    RT5651_R_MUTE_SFT, 1, 1);

左声道:

static const struct snd_kcontrol_new rt5651_dac_l_mix[] = {
        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,  // 寄存器配置为RT5651_AD_DA_MIXER=0x29,偏移位配置为15
                        RT5651_M_ADCMIX_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,        // 寄存器配置为RT5651_AD_DA_MIXER=0x29,偏移位配置为14
                        RT5651_M_IF1_DAC_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new rt5651_sto_dac_l_mix[] = {
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_STO_DAC_MIXER,  // 寄存器配置为RT5651_STO_DAC_MIXER=0x2a,偏移位配置为14
                        RT5651_M_DAC_L1_MIXL_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_STO_DAC_MIXER,  // 寄存器配置为RT5651_STO_DAC_MIXER=0x2a,偏移位配置为12
                        RT5651_M_DAC_L2_MIXL_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_STO_DAC_MIXER,  // 寄存器配置为RT5651_STO_DAC_MIXER=0x2a,偏移位配置为9
                        RT5651_M_DAC_R1_MIXL_SFT, 1, 1),
};


static const struct snd_kcontrol_new rt5651_out_l_mix[] = {
        SOC_DAPM_SINGLE("BST1 Switch", RT5651_OUT_L3_MIXER,  // 寄存器配置为RT5651_OUT_L3_MIXER=0x4f,偏移位配置为5
                        RT5651_M_BST1_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("BST2 Switch", RT5651_OUT_L3_MIXER,  // 寄存器配置为RT5651_OUT_L3_MIXER=0x4f,偏移位配置为6
                        RT5651_M_BST2_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("INL1 Switch", RT5651_OUT_L3_MIXER,  // 寄存器配置为RT5651_OUT_L3_MIXER=0x4f,偏移位配置为4
                        RT5651_M_IN1_L_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("REC MIXL Switch", RT5651_OUT_L3_MIXER,  // 寄存器配置为RT5651_OUT_L3_MIXER=0x4f,偏移位配置为3
                        RT5651_M_RM_L_OM_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_OUT_L3_MIXER,   // 寄存器配置为RT5651_OUT_L3_MIXER=0x4f,偏移位配置为0
                        RT5651_M_DAC_L1_OM_L_SFT, 1, 1),
};

static const struct snd_kcontrol_new hpovol_l_control =
        SOC_DAPM_SINGLE("Switch", RT5651_HP_VOL,     // 寄存器配置为RT5651_HP_VOL=0x02,偏移位配置为14
                        RT5651_VOL_L_SFT, 1, 1);
                        

static const struct snd_kcontrol_new rt5651_hpo_mix[] = {
        SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5651_HPO_MIXER,    // 寄存器配置为RT5651_HPO_MIXER=0x45,偏移位配置为14
                        RT5651_M_DAC1_HM_SFT, 1, 1),
        SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5651_HPO_MIXER,   // 寄存器配置为RT5651_HPO_MIXER=0x45,偏移位配置为13
                        RT5651_M_HPVOL_HM_SFT, 1, 1),
};

static const struct snd_kcontrol_new hpo_l_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_HP_VOL,  // 寄存器配置为RT5651_HP_VOL=0x02,偏移位配置为15
                                    RT5651_L_MUTE_SFT, 1, 1);
4.1.2 定义widget

在sound/soc/codecs/rt5651.c定义了大量的widget:

static const struct snd_soc_dapm_widget rt5651_dapm_widgets[] = {
        /* ASRC */
        SND_SOC_DAPM_SUPPLY_S("I2S1 ASRC", 1, RT5651_PLL_MODE_2,
                              15, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5651_PLL_MODE_2,
                              14, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY_S("STO1 DAC ASRC", 1, RT5651_PLL_MODE_2,
                              13, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY_S("STO2 DAC ASRC", 1, RT5651_PLL_MODE_2,
                              12, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY_S("ADC ASRC", 1, RT5651_PLL_MODE_2,
                              11, 0, NULL, 0),

        /* micbias */
        SND_SOC_DAPM_SUPPLY("LDO", RT5651_PWR_ANLG1,
                        RT5651_PWR_LDO_BIT, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("micbias1", RT5651_PWR_ANLG2,
                        RT5651_PWR_MB1_BIT, 0, NULL, 0),
        /* Input Lines */
        SND_SOC_DAPM_INPUT("MIC1"),
        SND_SOC_DAPM_INPUT("MIC2"),
        SND_SOC_DAPM_INPUT("MIC3"),

        SND_SOC_DAPM_INPUT("IN1P"),
        SND_SOC_DAPM_INPUT("IN2P"),
        SND_SOC_DAPM_INPUT("IN2N"),
        SND_SOC_DAPM_INPUT("IN3P"),
        SND_SOC_DAPM_INPUT("DMIC L1"),
        SND_SOC_DAPM_INPUT("DMIC R1"),
        SND_SOC_DAPM_SUPPLY("DMIC CLK", RT5651_DMIC, RT5651_DMIC_1_EN_SFT,
                            0, set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
        /* Boost */
        SND_SOC_DAPM_PGA_E("BST1", RT5651_PWR_ANLG2,
                RT5651_PWR_BST1_BIT, 0, NULL, 0, rt5651_bst1_event,
                SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
        SND_SOC_DAPM_PGA_E("BST2", RT5651_PWR_ANLG2,
                RT5651_PWR_BST2_BIT, 0, NULL, 0, rt5651_bst2_event,
                SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
        SND_SOC_DAPM_PGA_E("BST3", RT5651_PWR_ANLG2,
                RT5651_PWR_BST3_BIT, 0, NULL, 0, rt5651_bst3_event,
                SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
        /* Input Volume */
        SND_SOC_DAPM_PGA("INL1 VOL", RT5651_PWR_VOL,
                         RT5651_PWR_IN1_L_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("INR1 VOL", RT5651_PWR_VOL,
                         RT5651_PWR_IN1_R_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("INL2 VOL", RT5651_PWR_VOL,
                         RT5651_PWR_IN2_L_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("INR2 VOL", RT5651_PWR_VOL,
                         RT5651_PWR_IN2_R_BIT, 0, NULL, 0),

        /* REC Mixer */
        SND_SOC_DAPM_MIXER("RECMIXL", RT5651_PWR_MIXER, RT5651_PWR_RM_L_BIT, 0,
                           rt5651_rec_l_mix, ARRAY_SIZE(rt5651_rec_l_mix)),
        SND_SOC_DAPM_MIXER("RECMIXR", RT5651_PWR_MIXER, RT5651_PWR_RM_R_BIT, 0,
                           rt5651_rec_r_mix, ARRAY_SIZE(rt5651_rec_r_mix)),
        /* ADCs */
        SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_SUPPLY("ADC L Power", RT5651_PWR_DIG1,
                            RT5651_PWR_ADC_L_BIT, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("ADC R Power", RT5651_PWR_DIG1,
                            RT5651_PWR_ADC_R_BIT, 0, NULL, 0),
        /* ADC Mux */
        SND_SOC_DAPM_MUX("Stereo1 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto1_adc_l2_mux),
        SND_SOC_DAPM_MUX("Stereo1 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto1_adc_r2_mux),
        SND_SOC_DAPM_MUX("Stereo1 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto1_adc_l1_mux),
        SND_SOC_DAPM_MUX("Stereo1 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto1_adc_r1_mux),
        SND_SOC_DAPM_MUX("Stereo2 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto2_adc_l2_mux),
        SND_SOC_DAPM_MUX("Stereo2 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto2_adc_l1_mux),
        SND_SOC_DAPM_MUX("Stereo2 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto2_adc_r1_mux),
        SND_SOC_DAPM_MUX("Stereo2 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
                         &rt5651_sto2_adc_r2_mux),
        /* ADC Mixer */
        SND_SOC_DAPM_SUPPLY("Stereo1 Filter", RT5651_PWR_DIG2,
                            RT5651_PWR_ADC_STO1_F_BIT, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("Stereo2 Filter", RT5651_PWR_DIG2,
                            RT5651_PWR_ADC_STO2_F_BIT, 0, NULL, 0),
        SND_SOC_DAPM_MIXER("Stereo1 ADC MIXL", SND_SOC_NOPM, 0, 0,
                           rt5651_sto1_adc_l_mix,
                           ARRAY_SIZE(rt5651_sto1_adc_l_mix)),
        SND_SOC_DAPM_MIXER("Stereo1 ADC MIXR", SND_SOC_NOPM, 0, 0,
                           rt5651_sto1_adc_r_mix,
                           ARRAY_SIZE(rt5651_sto1_adc_r_mix)),
        SND_SOC_DAPM_MIXER("Stereo2 ADC MIXL", SND_SOC_NOPM, 0, 0,
                           rt5651_sto2_adc_l_mix,
                           ARRAY_SIZE(rt5651_sto2_adc_l_mix)),
        SND_SOC_DAPM_MIXER("Stereo2 ADC MIXR", SND_SOC_NOPM, 0, 0,
                           rt5651_sto2_adc_r_mix,
                           ARRAY_SIZE(rt5651_sto2_adc_r_mix)),

        /* Digital Interface */
        SND_SOC_DAPM_SUPPLY("I2S1", RT5651_PWR_DIG1,
                            RT5651_PWR_I2S1_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 ADC1", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 DAC2 L", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 DAC2 R", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF1 ADC2", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("I2S2", RT5651_PWR_DIG1,
                            RT5651_PWR_I2S2_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_MUX("IF2 ADC", SND_SOC_NOPM, 0, 0,
                         &rt5651_if2_adc_src_mux),

        /* Digital Interface Select */

        SND_SOC_DAPM_MUX("PDM L Mux", RT5651_PDM_CTL,
                         RT5651_M_PDM_L_SFT, 1, &rt5651_pdm_l_mux),
        SND_SOC_DAPM_MUX("PDM R Mux", RT5651_PDM_CTL,
                         RT5651_M_PDM_R_SFT, 1, &rt5651_pdm_r_mux),
        /* Audio Interface */
        SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),

        /* Audio DSP */
        SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),

        /* Output Side */
        /* DAC mixer before sound effect  */
        SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
                           rt5651_dac_l_mix, ARRAY_SIZE(rt5651_dac_l_mix)),
        SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
                           rt5651_dac_r_mix, ARRAY_SIZE(rt5651_dac_r_mix)),

        /* DAC2 channel Mux */
        SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0, &rt5651_dac_l2_mux),
        SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0, &rt5651_dac_r2_mux),
        SND_SOC_DAPM_PGA("DAC L2 Volume", SND_SOC_NOPM, 0, 0, NULL, 0),
        SND_SOC_DAPM_PGA("DAC R2 Volume", SND_SOC_NOPM, 0, 0, NULL, 0),

        SND_SOC_DAPM_SUPPLY("Stero1 DAC Power", RT5651_PWR_DIG2,
                            RT5651_PWR_DAC_STO1_F_BIT, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("Stero2 DAC Power", RT5651_PWR_DIG2,
                            RT5651_PWR_DAC_STO2_F_BIT, 0, NULL, 0),
        /* DAC Mixer */
        SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
                           rt5651_sto_dac_l_mix,
                           ARRAY_SIZE(rt5651_sto_dac_l_mix)),
        SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
                           rt5651_sto_dac_r_mix,
                           ARRAY_SIZE(rt5651_sto_dac_r_mix)),
        SND_SOC_DAPM_MIXER("DD MIXL", SND_SOC_NOPM, 0, 0,
                           rt5651_dd_dac_l_mix,
                           ARRAY_SIZE(rt5651_dd_dac_l_mix)),
        SND_SOC_DAPM_MIXER("DD MIXR", SND_SOC_NOPM, 0, 0,
                           rt5651_dd_dac_r_mix,
                           ARRAY_SIZE(rt5651_dd_dac_r_mix)),

        /* DACs */
        SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5651_PWR_DIG1,
                            RT5651_PWR_DAC_L1_BIT, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5651_PWR_DIG1,
                            RT5651_PWR_DAC_R1_BIT, 0, NULL, 0),
        /* OUT Mixer */
        SND_SOC_DAPM_MIXER("OUT MIXL", RT5651_PWR_MIXER, RT5651_PWR_OM_L_BIT,
                           0, rt5651_out_l_mix, ARRAY_SIZE(rt5651_out_l_mix)),
        SND_SOC_DAPM_MIXER("OUT MIXR", RT5651_PWR_MIXER, RT5651_PWR_OM_R_BIT,
                           0, rt5651_out_r_mix, ARRAY_SIZE(rt5651_out_r_mix)),
        /* Ouput Volume */
        SND_SOC_DAPM_SWITCH("OUTVOL L", RT5651_PWR_VOL,
                            RT5651_PWR_OV_L_BIT, 0, &outvol_l_control),
        SND_SOC_DAPM_SWITCH("OUTVOL R", RT5651_PWR_VOL,
                            RT5651_PWR_OV_R_BIT, 0, &outvol_r_control),
        SND_SOC_DAPM_SWITCH("HPOVOL L", RT5651_PWR_VOL,
                            RT5651_PWR_HV_L_BIT, 0, &hpovol_l_control),
        SND_SOC_DAPM_SWITCH("HPOVOL R", RT5651_PWR_VOL,
                            RT5651_PWR_HV_R_BIT, 0, &hpovol_r_control),
        SND_SOC_DAPM_PGA("INL1", RT5651_PWR_VOL,
                         RT5651_PWR_IN1_L_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("INR1", RT5651_PWR_VOL,
                         RT5651_PWR_IN1_R_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("INL2", RT5651_PWR_VOL,
                         RT5651_PWR_IN2_L_BIT, 0, NULL, 0),
        SND_SOC_DAPM_PGA("INR2", RT5651_PWR_VOL,
                         RT5651_PWR_IN2_R_BIT, 0, NULL, 0),
        /* HPO/LOUT/Mono Mixer */
        SND_SOC_DAPM_MIXER("HPOL MIX", SND_SOC_NOPM, 0, 0,
                           rt5651_hpo_mix, ARRAY_SIZE(rt5651_hpo_mix)),
        SND_SOC_DAPM_MIXER("HPOR MIX", SND_SOC_NOPM, 0, 0,
                           rt5651_hpo_mix, ARRAY_SIZE(rt5651_hpo_mix)),
        SND_SOC_DAPM_SUPPLY("HP L Amp", RT5651_PWR_ANLG1,
                            RT5651_PWR_HP_L_BIT, 0, NULL, 0),
        SND_SOC_DAPM_SUPPLY("HP R Amp", RT5651_PWR_ANLG1,
                            RT5651_PWR_HP_R_BIT, 0, NULL, 0),
        SND_SOC_DAPM_MIXER("LOUT MIX", RT5651_PWR_ANLG1, RT5651_PWR_LM_BIT, 0,
                           rt5651_lout_mix, ARRAY_SIZE(rt5651_lout_mix)),

        SND_SOC_DAPM_SUPPLY("Amp Power", RT5651_PWR_ANLG1,
                            RT5651_PWR_HA_BIT, 0, rt5651_amp_power_event,
                            SND_SOC_DAPM_POST_PMU),
        SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5651_hp_event,
                           SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
        SND_SOC_DAPM_SWITCH("HPO L Playback", SND_SOC_NOPM, 0, 0,
                            &hpo_l_mute_control),
        SND_SOC_DAPM_SWITCH("HPO R Playback", SND_SOC_NOPM, 0, 0,
                            &hpo_r_mute_control),
        SND_SOC_DAPM_SWITCH("LOUT L Playback", SND_SOC_NOPM, 0, 0,
                            &lout_l_mute_control),
        SND_SOC_DAPM_SWITCH("LOUT R Playback", SND_SOC_NOPM, 0, 0,
                            &lout_r_mute_control),
        SND_SOC_DAPM_POST("HP Post", rt5651_hp_post_event),

        /* Output Lines */
        SND_SOC_DAPM_OUTPUT("HPOL"),
        SND_SOC_DAPM_OUTPUT("HPOR"),
        SND_SOC_DAPM_OUTPUT("LOUTL"),
        SND_SOC_DAPM_OUTPUT("LOUTR"),
        SND_SOC_DAPM_OUTPUT("PDML"),
        SND_SOC_DAPM_OUTPUT("PDMR"),
};
View Code

在rt5651_dapm_widgets中我们可以找到位于多媒体音频播放右声道路径上的widget;

/* Audio Interface */
SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0), // 无寄存器

/* Digital Interface */
SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),  // 虚拟widget
SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),  // 虚拟widget

/* Output Side */
/* DAC mixer before sound effect  */
SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,            //Right DAC Mixer包含2个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   rt5651_dac_r_mix, ARRAY_SIZE(rt5651_dac_r_mix)),

/* Audio DSP */
SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),

/* DAC Mixer */   
SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,     // Right Stereo DAC Mixer包含3个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭  
                   rt5651_sto_dac_r_mix,
                   ARRAY_SIZE(rt5651_sto_dac_r_mix)),
                   
/* DACs */
SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM, 0, 0),

/* OUT Mixer */
SND_SOC_DAPM_MIXER("OUT MIXR", RT5651_PWR_MIXER, RT5651_PWR_OM_R_BIT,   // Right Output Mixer包含5个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   0, rt5651_out_r_mix, ARRAY_SIZE(rt5651_out_r_mix)),
                           
/* Ouput Volume */                      
SND_SOC_DAPM_SWITCH("HPOVOL R", RT5651_PWR_VOL,     // Right HPO Volume包含1个kcontrol,用于控制开关的通断
                    RT5651_PWR_HV_R_BIT, 0, &hpovol_r_control),
                    
/* HPO/LOUT/Mono Mixer */
SND_SOC_DAPM_MIXER("HPOR MIX", SND_SOC_NOPM, 0, 0,   // Right HPO Mixer包含2个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   rt5651_hpo_mix, ARRAY_SIZE(rt5651_hpo_mix)),                   
SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5651_hp_event,
                   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),    
SND_SOC_DAPM_SWITCH("HPO R Playback", SND_SOC_NOPM, 0, 0,  // Right HPO Playback包含1个kcontrol,用于控制开关的通断
                    &hpo_r_mute_control),     
                   
/* Output Lines */
SND_SOC_DAPM_OUTPUT("HPOR"),     // HPOR为ALC5651的Right HPO Output引脚,可以用来外耳机

左声道:

/* Audio Interface */
SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0), // 无寄存器

/* Digital Interface */
SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),  // 虚拟widget
SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),  // 虚拟widget

/* Output Side */
/* DAC mixer before sound effect  */
SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,              // Left DAC Mixer包含2个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   rt5651_dac_l_mix, ARRAY_SIZE(rt5651_dac_l_mix)),

/* Audio DSP */
SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
        
/* DAC Mixer */
SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,     // Left Stereo DAC Mixer包含3个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭  
                   rt5651_sto_dac_l_mix,
                   ARRAY_SIZE(rt5651_sto_dac_l_mix)),       
                   
/* DACs */
SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM, 0, 0),

/* OUT Mixer */
SND_SOC_DAPM_MIXER("OUT MIXL", RT5651_PWR_MIXER, RT5651_PWR_OM_L_BIT,   // Left Output Mixer包含5个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   0, rt5651_out_l_mix, ARRAY_SIZE(rt5651_out_l_mix)),
                   
/* Ouput Volume */                      
SND_SOC_DAPM_SWITCH("HPOVOL L", RT5651_PWR_VOL,     // Left HPO Volume包含1个kcontrol,用于控制开关的通断
                    RT5651_PWR_HV_L_BIT, 0, &hpovol_l_control),
                    
/* HPO/LOUT/Mono Mixer */
SND_SOC_DAPM_MIXER("HPOL MIX", SND_SOC_NOPM, 0, 0,  // Left HPO Mixer包含2个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   rt5651_hpo_mix, ARRAY_SIZE(rt5651_hpo_mix)),                   
SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5651_hp_event,
                   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),    
SND_SOC_DAPM_SWITCH("HPO L Playback", SND_SOC_NOPM, 0, 0,  // Left HPO Playback包含1个kcontrol,用于控制开关的通断
                    &hpo_l_mute_control),                   

/* Output Lines */
SND_SOC_DAPM_OUTPUT("HPOL"),     // HPOL为ALC5651的Left HPO Output引脚,可以用来外耳机

不知道你有没有注意到,上面定义的这些widget都没有设置相应的电源控制寄存器。

4.1.3 定义route

在sound/soc/codecs/rt5651.c定义了widget的链接路径:

static const struct snd_soc_dapm_route rt5651_dapm_routes[] = {
        {"Stero1 DAC Power", NULL, "STO1 DAC ASRC"},
        {"Stero2 DAC Power", NULL, "STO2 DAC ASRC"},
        {"I2S1", NULL, "I2S1 ASRC"},
        {"I2S2", NULL, "I2S2 ASRC"},

        {"IN1P", NULL, "LDO"},
        {"IN2P", NULL, "LDO"},
        {"IN3P", NULL, "LDO"},

        {"IN1P", NULL, "MIC1"},
        {"IN2P", NULL, "MIC2"},
        {"IN2N", NULL, "MIC2"},
        {"IN3P", NULL, "MIC3"},

        {"BST1", NULL, "IN1P"},
        {"BST2", NULL, "IN2P"},
        {"BST2", NULL, "IN2N"},
        {"BST3", NULL, "IN3P"},

        {"INL1 VOL", NULL, "IN2P"},
        {"INR1 VOL", NULL, "IN2N"},

        {"RECMIXL", "INL1 Switch", "INL1 VOL"},
        {"RECMIXL", "BST3 Switch", "BST3"},
        {"RECMIXL", "BST2 Switch", "BST2"},
        {"RECMIXL", "BST1 Switch", "BST1"},

        {"RECMIXR", "INR1 Switch", "INR1 VOL"},
        {"RECMIXR", "BST3 Switch", "BST3"},
        {"RECMIXR", "BST2 Switch", "BST2"},
        {"RECMIXR", "BST1 Switch", "BST1"},

        {"ADC L", NULL, "RECMIXL"},
        {"ADC L", NULL, "ADC L Power"},
        {"ADC R", NULL, "RECMIXR"},
        {"ADC R", NULL, "ADC R Power"},

        {"DMIC L1", NULL, "DMIC CLK"},
        {"DMIC R1", NULL, "DMIC CLK"},

        {"Stereo1 ADC L2 Mux", "DMIC", "DMIC L1"},
        {"Stereo1 ADC L2 Mux", "DD MIX", "DD MIXL"},
        {"Stereo1 ADC L1 Mux", "ADC", "ADC L"},
        {"Stereo1 ADC L1 Mux", "DD MIX", "DD MIXL"},

        {"Stereo1 ADC R1 Mux", "ADC", "ADC R"},
        {"Stereo1 ADC R1 Mux", "DD MIX", "DD MIXR"},
        {"Stereo1 ADC R2 Mux", "DMIC", "DMIC R1"},
        {"Stereo1 ADC R2 Mux", "DD MIX", "DD MIXR"},

        {"Stereo2 ADC L2 Mux", "DMIC L", "DMIC L1"},
        {"Stereo2 ADC L2 Mux", "DD MIXL", "DD MIXL"},
        {"Stereo2 ADC L1 Mux", "DD MIXL", "DD MIXL"},
        {"Stereo2 ADC L1 Mux", "ADCL", "ADC L"},
        {"Stereo2 ADC R1 Mux", "DD MIXR", "DD MIXR"},
        {"Stereo2 ADC R1 Mux", "ADCR", "ADC R"},
        {"Stereo2 ADC R2 Mux", "DMIC R", "DMIC R1"},
        {"Stereo2 ADC R2 Mux", "DD MIXR", "DD MIXR"},

        {"Stereo1 ADC MIXL", "ADC1 Switch", "Stereo1 ADC L1 Mux"},
        {"Stereo1 ADC MIXL", "ADC2 Switch", "Stereo1 ADC L2 Mux"},
        {"Stereo1 ADC MIXL", NULL, "Stereo1 Filter"},
        {"Stereo1 Filter", NULL, "ADC ASRC"},

        {"Stereo1 ADC MIXR", "ADC1 Switch", "Stereo1 ADC R1 Mux"},
        {"Stereo1 ADC MIXR", "ADC2 Switch", "Stereo1 ADC R2 Mux"},
        {"Stereo1 ADC MIXR", NULL, "Stereo1 Filter"},

        {"Stereo2 ADC MIXL", "ADC1 Switch", "Stereo2 ADC L1 Mux"},
        {"Stereo2 ADC MIXL", "ADC2 Switch", "Stereo2 ADC L2 Mux"},
        {"Stereo2 ADC MIXL", NULL, "Stereo2 Filter"},
        {"Stereo2 Filter", NULL, "ADC ASRC"},

        {"Stereo2 ADC MIXR", "ADC1 Switch", "Stereo2 ADC R1 Mux"},
        {"Stereo2 ADC MIXR", "ADC2 Switch", "Stereo2 ADC R2 Mux"},
        {"Stereo2 ADC MIXR", NULL, "Stereo2 Filter"},

        {"IF1 ADC2", NULL, "Stereo2 ADC MIXL"},
        {"IF1 ADC2", NULL, "Stereo2 ADC MIXR"},
        {"IF1 ADC1", NULL, "Stereo1 ADC MIXL"},
        {"IF1 ADC1", NULL, "Stereo1 ADC MIXR"},

        {"IF1 ADC1", NULL, "I2S1"},

        {"IF2 ADC", "IF1 ADC1", "IF1 ADC1"},
        {"IF2 ADC", "IF1 ADC2", "IF1 ADC2"},
        {"IF2 ADC", NULL, "I2S2"},

        {"AIF1TX", NULL, "IF1 ADC1"},
        {"AIF1TX", NULL, "IF1 ADC2"},
        {"AIF2TX", NULL, "IF2 ADC"},

        {"IF1 DAC", NULL, "AIF1RX"},
        {"IF1 DAC", NULL, "I2S1"},
        {"IF2 DAC", NULL, "AIF2RX"},
        {"IF2 DAC", NULL, "I2S2"},

        {"IF1 DAC1 L", NULL, "IF1 DAC"},
        {"IF1 DAC1 R", NULL, "IF1 DAC"},
        {"IF1 DAC2 L", NULL, "IF1 DAC"},
        {"IF1 DAC2 R", NULL, "IF1 DAC"},
        {"IF2 DAC L", NULL, "IF2 DAC"},
        {"IF2 DAC R", NULL, "IF2 DAC"},

        {"DAC MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL"},
        {"DAC MIXL", "INF1 Switch", "IF1 DAC1 L"},
        {"DAC MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR"},
        {"DAC MIXR", "INF1 Switch", "IF1 DAC1 R"},

        {"Audio DSP", NULL, "DAC MIXL"},
        {"Audio DSP", NULL, "DAC MIXR"},

        {"DAC L2 Mux", "IF1", "IF1 DAC2 L"},
        {"DAC L2 Mux", "IF2", "IF2 DAC L"},
        {"DAC L2 Volume", NULL, "DAC L2 Mux"},
        {"DAC R2 Mux", "IF1", "IF1 DAC2 R"},
        {"DAC R2 Mux", "IF2", "IF2 DAC R"},
        {"DAC R2 Volume", NULL, "DAC R2 Mux"},

        {"Stereo DAC MIXL", "DAC L1 Switch", "Audio DSP"},
        {"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Volume"},
        {"Stereo DAC MIXL", "DAC R1 Switch", "DAC MIXR"},
        {"Stereo DAC MIXL", NULL, "Stero1 DAC Power"},
        {"Stereo DAC MIXL", NULL, "Stero2 DAC Power"},
        {"Stereo DAC MIXR", "DAC R1 Switch", "Audio DSP"},
        {"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Volume"},
        {"Stereo DAC MIXR", "DAC L1 Switch", "DAC MIXL"},
        {"Stereo DAC MIXR", NULL, "Stero1 DAC Power"},
        {"Stereo DAC MIXR", NULL, "Stero2 DAC Power"},

        {"PDM L Mux", "Stereo DAC MIX", "Stereo DAC MIXL"},
        {"PDM L Mux", "DD MIX", "DAC MIXL"},
        {"PDM R Mux", "Stereo DAC MIX", "Stereo DAC MIXR"},
        {"PDM R Mux", "DD MIX", "DAC MIXR"},

        {"DAC L1", NULL, "Stereo DAC MIXL"},
        {"DAC L1", NULL, "DAC L1 Power"},
        {"DAC R1", NULL, "Stereo DAC MIXR"},
        {"DAC R1", NULL, "DAC R1 Power"},

        {"DD MIXL", "DAC L1 Switch", "DAC MIXL"},
        {"DD MIXL", "DAC L2 Switch", "DAC L2 Volume"},
        {"DD MIXL", "DAC R2 Switch", "DAC R2 Volume"},
        {"DD MIXL", NULL, "Stero2 DAC Power"},

        {"DD MIXR", "DAC R1 Switch", "DAC MIXR"},
        {"DD MIXR", "DAC R2 Switch", "DAC R2 Volume"},
        {"DD MIXR", "DAC L2 Switch", "DAC L2 Volume"},
        {"DD MIXR", NULL, "Stero2 DAC Power"},

        {"OUT MIXL", "BST1 Switch", "BST1"},
        {"OUT MIXL", "BST2 Switch", "BST2"},
        {"OUT MIXL", "INL1 Switch", "INL1 VOL"},
        {"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
        {"OUT MIXL", "DAC L1 Switch", "DAC L1"},

        {"OUT MIXR", "BST2 Switch", "BST2"},
        {"OUT MIXR", "BST1 Switch", "BST1"},
        {"OUT MIXR", "INR1 Switch", "INR1 VOL"},
        {"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
        {"OUT MIXR", "DAC R1 Switch", "DAC R1"},

        {"HPOVOL L", "Switch", "OUT MIXL"},
        {"HPOVOL R", "Switch", "OUT MIXR"},
        {"OUTVOL L", "Switch", "OUT MIXL"},
        {"OUTVOL R", "Switch", "OUT MIXR"},

        {"HPOL MIX", "HPO MIX DAC1 Switch", "DAC L1"},
        {"HPOL MIX", "HPO MIX HPVOL Switch", "HPOVOL L"},
        {"HPOL MIX", NULL, "HP L Amp"},
        {"HPOR MIX", "HPO MIX DAC1 Switch", "DAC R1"},
        {"HPOR MIX", "HPO MIX HPVOL Switch", "HPOVOL R"},
        {"HPOR MIX", NULL, "HP R Amp"},

        {"LOUT MIX", "DAC L1 Switch", "DAC L1"},
        {"LOUT MIX", "DAC R1 Switch", "DAC R1"},
        {"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
        {"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},

        {"HP Amp", NULL, "HPOL MIX"},
        {"HP Amp", NULL, "HPOR MIX"},
        {"HP Amp", NULL, "Amp Power"},
        {"HPO L Playback", "Switch", "HP Amp"},
        {"HPO R Playback", "Switch", "HP Amp"},
        {"HPOL", NULL, "HPO L Playback"},
        {"HPOR", NULL, "HPO R Playback"},

        {"LOUT L Playback", "Switch", "LOUT MIX"},
        {"LOUT R Playback", "Switch", "LOUT MIX"},
        {"LOUTL", NULL, "LOUT L Playback"},
        {"LOUTL", NULL, "Amp Power"},
        {"LOUTR", NULL, "LOUT R Playback"},
        {"LOUTR", NULL, "Amp Power"},

        {"PDML", NULL, "PDM L Mux"},
        {"PDMR", NULL, "PDM R Mux"},
};
View Code

在rt5651_dapm_widgets中我们可以找到位于多媒体音频播放右声道路径上的route;

{"IF1 DAC", NULL, "AIF1RX"},
{"IF1 DAC1 R", NULL, "IF1 DAC"},
{"DAC MIXR", "INF1 Switch", "IF1 DAC1 R"},  
{"Audio DSP", NULL, "DAC MIXR"},  
{"Stereo DAC MIXR", "DAC R1 Switch", "Audio DSP"},
{"DAC R1", NULL, "Stereo DAC MIXR"},
{"OUT MIXR", "DAC R1 Switch", "DAC R1"}
{"HPOVOL R", "Switch", "OUT MIXR"},
{"HPOR MIX", "HPO MIX HPVOL Switch", "HPOVOL R"},
{"HP Amp", NULL, "HPOR MIX"},
{"HPO R Playback", "Switch", "HP Amp"},
{"HPOR", NULL, "HPO R Playback"},

左声道:

{"IF1 DAC", NULL, "AIF1RX"},
{"IF1 DAC1 L", NULL, "IF1 DAC"},
{"DAC MIXL", "INF1 Switch", "IF1 DAC1 L"},    
{"Audio DSP", NULL, "DAC MIXL"},  
{"Stereo DAC MIXL", "DAC L1 Switch", "Audio DSP"},
{"DAC L1", NULL, "Stereo DAC MIXL"},
{"OUT MIXL", "DAC L1 Switch", "DAC L1"}
{"HPOVOL L", "Switch", "OUT MIXL"},
{"HPOL MIX", "HPO MIX HPVOL Switch", "HPOVOL L"},
{"HP Amp", NULL, "HPOL MIX"},
{"HPO L Playback", "Switch", "HP Amp"},
{"HPOL", NULL, "HPO L Playback"},

三个参数分别为sink、control、source,比如:

{"DAC MIXL", "INF1 Switch", "IF1 DAC1 L"}

输入端的widget名称为IF1 DAC1 L,输出端的widget名称为DAC MIXL,名称为INF1 Switch的kcontrol用于控制这两个widget之间连接的通断(需要注意的是该kcontrol位于输出端的kcontrol中);

// 输入端widget
SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0);    //  寄存器设置为SND_SOC_NOPM,表示没有寄存器可以控制该widget的上下电
// 输出端widget
SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,               // 寄存器设置为SND_SOC_NOPM,表示没有寄存器可以控制该widget的上下电 
                   rt5651_dac_l_mix, ARRAY_SIZE(rt5651_dac_l_mix)); // Left DAC Mixer包含2个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
// 输出端widget关联的kcontrol
static const struct snd_kcontrol_new rt5651_dac_l_mix[] = {
        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,  // 寄存器配置为RT5651_AD_DA_MIXER=0x29,偏移位配置为15
                        RT5651_M_ADCMIX_L_SFT, 1, 1),
        SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,        // 寄存器配置为RT5651_AD_DA_MIXER=0x29,偏移位配置为14
                        RT5651_M_IF1_DAC_L_SFT, 1, 1),
};

4.2 rt5651_probe

rt5651_probe函数在sound/soc/codecs/rt5651.c中定义:

static int rt5651_probe(struct snd_soc_component *component)
{
        struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);  // 取出component->dev设备的driver_data,就是上面介绍的rt5651结构变量

        rt5651->component = component;   // 设置cpmponent

        snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,      // 向寄存器写入值,RT5651_PWR_ANLG1的值为0x63,寄存器地址0x63用于电源控制寄存器3
                RT5651_PWR_LDO_DVO_MASK, RT5651_PWR_LDO_DVO_1_2V);      // RT5651_PWR_LDO_DVO_MASK值为0x03 RT5651_PWR_LDO_DVO_1_2V值为2  因此这里向位[1:0]写入10'b
                                                                        // 即配置LDO output电压为1.2V    
        snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);   // Set the COMPONENT DAPM bias level,即dapm->bias_level=0

        rt5651_apply_properties(component);

        return 0;
}

snd_soc_component_update_bits函数定义在sound/soc/soc-component.c, 用于进行i2c控制传输,向i2c从设备指定寄存器地址写入值;

static int snd_soc_component_update_bits_legacy(
        struct snd_soc_component *component, unsigned int reg,
        unsigned int mask, unsigned int val, bool *change)  // mask描述的是需要更改的位
{
        unsigned int old, new;
        int ret = 0;

        mutex_lock(&component->io_mutex);

        old = soc_component_read_no_lock(component, reg);   // 读取寄存器的值

        new = (old & ~mask) | (val & mask);  // 计算新的值
        *change = old != new;  // 判断旧值和新值是否一样
        if (*change)       // 如果发生改变,则写入新的值
                ret = soc_component_write_no_lock(component, reg, new);

        mutex_unlock(&component->io_mutex);

        return soc_component_ret_reg_rw(component, ret, reg);
}

/**
 * snd_soc_component_update_bits() - Perform read/modify/write cycle
 * @component: Component to update
 * @reg: Register to update
 * @mask: Mask that specifies which bits to update
 * @val: New value for the bits specified by mask
 *
 * Return: 1 if the operation was successful and the value of the register
 * changed, 0 if the operation was successful, but the value did not change.
 * Returns a negative error code otherwise.
 */
int snd_soc_component_update_bits(struct snd_soc_component *component,
                                  unsigned int reg, unsigned int mask, unsigned int val)
{
        bool change;
        int ret;

        if (component->regmap) // 走这里
           // 当前使用regmap,调用regmap接口,其中component->regmap是regmap私有数据
                ret = regmap_update_bits_check(component->regmap, reg, mask,
                                               val, &change);
        else
                ret = snd_soc_component_update_bits_legacy(component, reg,
                                                           mask, val, &change);

        if (ret < 0)
                return soc_component_ret_reg_rw(component, ret, reg);
        return change;
}
View Code

4.3 set_bias_level

set_bias_level用于设置codec域的偏置电压,那什么是偏置电压呢?在电容式麦克风中,为了使麦克风的工作点稳定,需要加一个直流电压,这个直流电压就是偏置电压,偏置电压的作用主要有以下几个方面:

  • 稳定麦克风的工作点;在没有偏置电压的情况下,麦克风的输出信号会受到温度、湿度等环境因素的影响,导致输出信号的偏移,而偏置电压可以保持麦克风的工作不变,保证输出信号的稳定性;
  • 提高麦克风的灵敏度,偏置电压可以使麦克风的灵敏度增加,从而提高声音的捕获能力;
  • 降低麦克风的噪声;偏移电压可以降低麦克风的噪声水平,使得麦克风的输出信号更清晰;

对于ALC5651芯片MICBIAS1引脚会输出一个偏置电压,提供给外置麦克风;

set_bias_level函数会被dapm_power_widgets函数调用,这里被设置为了rt5651_set_bias_level。set_bias_level函数在sound/soc/codecs/rt5651.c中定义:

static int rt5651_set_bias_level(struct snd_soc_component *component,
                        enum snd_soc_bias_level level)
{
        switch (level) {
        case SND_SOC_BIAS_PREPARE:      // 准备状态
                // 获取dapm->bias_level,待机->准备
                if (SND_SOC_BIAS_STANDBY == snd_soc_component_get_bias_level(component)) {
                        // RT5651_PLL_MODE_1的值为0x83,寄存器0x83为ASRC控制寄存器,这里是判断位[15]、[12]、[9]是否为1
                        // 其中位[15]为I2S1模式选择控制  0:正常模式  1:ASRC模式
                        // 位[12]为I2S2模式选择控制  0:正常模式  1:ASRC模式 
                        // 位[9] Select Control for ASRC Mode in DMIC1 Function 0:正常模式  1:ASRC模式
                        if (snd_soc_component_read(component, RT5651_PLL_MODE_1) & 0x9200)                                                                                                                                                                                                             
                                // RT5651_D_MISC的值为0xFA,寄存器地址0xFA为基本控制寄存器
                                snd_soc_component_update_bits(component, RT5651_D_MISC,  
                                                    0xc00, 0xc00); // 这里向位[11:10]写入11'b,芯片手册中并没有描述这两位有什么作用
                }
                break;
        case SND_SOC_BIAS_STANDBY:     // 待机状态
                // 获取dapm->bias_level,关闭状态->待机状态
                if (SND_SOC_BIAS_OFF == snd_soc_component_get_bias_level(component)) {
                        //  RT5651_PWR_ANLG1的值为0x63,寄存器地址0x63用于电源控制寄存器3
                        snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,  
                                // RT5651_PWR_VREF1位[15],VREF1 Power Control:0下电、1上电
                                // RT5651_PWR_MB位[13],MBIAS Power Control:0下电、1上电
                                // RT5651_PWR_BG位[11],MBIAS Bandgap Power Control:0下电、1上电
                                // RT5651_PWR_VREF2位[4],VREF2 Power Control:0下电、1上电
                                RT5651_PWR_VREF1 | RT5651_PWR_MB |  
                                RT5651_PWR_BG | RT5651_PWR_VREF2,   
                                RT5651_PWR_VREF1 | RT5651_PWR_MB |
                                RT5651_PWR_BG | RT5651_PWR_VREF2);
                        usleep_range(10000, 15000);
                        // RT5651_PWR_FV1位[14],VREF1 Fast Mode Control:0 Fast VREF、1  Slow VREF, (For good analog performance)
                        // RT5651_PWR_FV2位[0],VREF2 Fast Mode Control:0 Fast VREF、1  Slow VREF, (For good analog performance)
                        snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,
                                RT5651_PWR_FV1 | RT5651_PWR_FV2,
                                RT5651_PWR_FV1 | RT5651_PWR_FV2);
                        // RT5651_D_MISC的值为0xFA,寄存器地址0xFA为基本控制寄存器,芯片手册中并没有描述位[1]有什么作用
                        snd_soc_component_update_bits(component, RT5651_D_MISC, 0x1, 0x1);
                }
                break;

        case SND_SOC_BIAS_OFF:        // 关闭状态
                snd_soc_component_write(component, RT5651_D_MISC, 0x0010);
                snd_soc_component_write(component, RT5651_PWR_DIG1, 0x0000);
                snd_soc_component_write(component, RT5651_PWR_DIG2, 0x0000);
                snd_soc_component_write(component, RT5651_PWR_VOL, 0x0000);
                snd_soc_component_write(component, RT5651_PWR_MIXER, 0x0000);
                /* Do not touch the LDO voltage select bits on bias-off */
                snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,
                        ~RT5651_PWR_LDO_DVO_MASK, 0);
                /* Leave PLL1 and jack-detect power as is, all others off */
                snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
                                    ~(RT5651_PWR_PLL | RT5651_PWR_JD_M), 0);
                break;

        default:
                break;
        }

        return 0;
}

这里寄存器配置有点看不懂,在datasheet中有些寄存器的位描述并没有找到,有兴趣自行研究吧。 

4.4 set_jack 

set_jack用于设置ACL5651的jack,这里被设置为了rt5651_set_jack,函数在sound/soc/codecs/rt5651.c中定义:

static int rt5651_set_jack(struct snd_soc_component *component,
                           struct snd_soc_jack *jack, void *data)
{
        if (jack)
                rt5651_enable_jack_detect(component, jack, data);
        else
                rt5651_disable_jack_detect(component);

        return 0;
}

在ASoC中使用struct snd_soc_jack来描述jack,并提供了对其状态、引脚等进行管理和通知的功能。这里如何定义了jack,将会调用rt5651_enable_jack_detect,用于实现麦克风插入/拔出的检测。

4.4.1 麦克风检测

我大概说一下ALC5651麦克风插入/拔出的检测原理,ACL5651支持对sta_gpio_jd、、sta_jd1_1、sta_jd1_2、sta_jd2、sta_micbias1_ovcd的的检测(JD英文为jack dection),具体如下:

 这里以sta_jd1_1、sta_jd1_2、sta_jd2为例,分别连接ALC5651的JD1、JD2引脚,用于三路麦克风1的检测;

一般情况下,IRQ输出需要与JD功能结合使用。当 JD被触发时,IRQ会向主机输出一个标志,通知软件驱动程序。软件驱动程序将根据系统设计执行相应的操作。以下是流程图:

Jack dection功能可用于打开或关闭相关的输出端口。当插孔检测引脚被触发时,选择的输出端口将打开或关闭。例如,在HP(耳机)和LOUT(扬声器)自动切换功能中,当JD被触发时会进行自动切换。

设置步骤如下:

  • 选择JD触发源:将sta_jd1_1作为JD状态。MX-BC[11:9] = 001’b;
    • 000’b: From sta_gpio_jd;
    • 001’b: From sta_jd1_1;
    • 010’b: From sta_jd1_2;
    • 011’b: From sta_jd2;
    • Others: Reserved;
  • 根据JD的激活状态设置目标行为:当JD被触发时,进行HP和LOUT的自动切换。 MX-BB[11:10] = 11’b & MX-BB[3:2] = 10’b;
    • 位[11]:Enable Jack Detect Trigger HPOUT,0:禁止,1:使能;
    • 位[10]:Select Jack Detect Polarity Trigger HPOUT,0:低电平触发,1:高平触发;
    • 位[3]:Enable Jack Detect Trigger LOUT,0:禁止,1:使能;
    • 位[2]:Select Jack Detect Polarity Trigger LOUT:0:低电平触发,1:高平触发;
  • 当JD状态为低电平时,HP_OUT(耳机输出)将静音,LOUT(扬声器输出)将恢复正常声音。 当JD状态由低电平变为高电平时,HP将恢复正常声音,LOUT将静音。 

注意:对于HP和SPK插孔切换功能,驱动程序需要先打开DAC通往HP路径的通路和DAC通往LOUT路径的通路。MX-BB的寄存器控制仅用于对HP和SPK进行静音/取消静音功能。

当我们移植完了声卡驱动,并通过开发板ubuntu系统进行音频播放的时候,我特意查看了一下0xBB、0xBC寄存器的值:

  • 0xBB:Jack Dection控制寄存器1,值为0x00;
  • 0xBC:Jack Dection控制寄存器2,值为0x00.
4.4.2 rt5651_enable_jack_detect

函数rt5651_enable_jack_detect定义如下,这里我们就不分析了:

static void rt5651_enable_jack_detect(struct snd_soc_component *component,
                                      struct snd_soc_jack *hp_jack,
                                      struct gpio_desc *gpiod_hp_det)
{
        struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
        bool using_internal_jack_detect = true;

        /* Select jack detect source */
        switch (rt5651->jd_src) {
        case RT5651_JD_NULL:
                rt5651->gpiod_hp_det = gpiod_hp_det;
                if (!rt5651->gpiod_hp_det)
                        return; /* No jack detect */
                using_internal_jack_detect = false;
                break;
        case RT5651_JD1_1:
                snd_soc_component_update_bits(component, RT5651_JD_CTRL2,   // RT5651_JD_CTRL2值为0xBC
                        RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD1_1);
                /* active-low is normal, set inv flag for active-high */
                if (rt5651->jd_active_high)
                        snd_soc_component_update_bits(component,
                                RT5651_IRQ_CTRL1,          // RT5651_JD_CTRL1值为0xBB
                                RT5651_JD1_1_IRQ_EN | RT5651_JD1_1_INV,
                                RT5651_JD1_1_IRQ_EN | RT5651_JD1_1_INV);
                else
                        snd_soc_component_update_bits(component,
                                RT5651_IRQ_CTRL1,
                                RT5651_JD1_1_IRQ_EN | RT5651_JD1_1_INV,
                                RT5651_JD1_1_IRQ_EN);
                break;
        case RT5651_JD1_2:
                snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
                        RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD1_2);
                /* active-low is normal, set inv flag for active-high */
                if (rt5651->jd_active_high)
                        snd_soc_component_update_bits(component,
                                RT5651_IRQ_CTRL1,
                                RT5651_JD1_2_IRQ_EN | RT5651_JD1_2_INV,
                                RT5651_JD1_2_IRQ_EN | RT5651_JD1_2_INV);
                else
                        snd_soc_component_update_bits(component,
                                RT5651_IRQ_CTRL1,
                                RT5651_JD1_2_IRQ_EN | RT5651_JD1_2_INV,
                                RT5651_JD1_2_IRQ_EN);
                break;
        case RT5651_JD2:
                snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
                        RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD2);
                /* active-low is normal, set inv flag for active-high */
                if (rt5651->jd_active_high)
                        snd_soc_component_update_bits(component,
                                RT5651_IRQ_CTRL1,
                                RT5651_JD2_IRQ_EN | RT5651_JD2_INV,
                                RT5651_JD2_IRQ_EN | RT5651_JD2_INV);
                else
                        snd_soc_component_update_bits(component,
                                RT5651_IRQ_CTRL1,
                                RT5651_JD2_IRQ_EN | RT5651_JD2_INV,
                                RT5651_JD2_IRQ_EN);
                break;
        default:
                dev_err(component->dev, "Currently only JD1_1 / JD1_2 / JD2 are supported\n");
                return;
        }

        if (using_internal_jack_detect) {
                /* IRQ output on GPIO1 */
                snd_soc_component_update_bits(component, RT5651_GPIO_CTRL1,
                        RT5651_GP1_PIN_MASK, RT5651_GP1_PIN_IRQ);

                /* Enable jack detect power */
                snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
                        RT5651_PWR_JD_M, RT5651_PWR_JD_M);
        }

        /* Set OVCD threshold current and scale-factor */
        snd_soc_component_write(component, RT5651_PR_BASE + RT5651_BIAS_CUR4,
                                0xa800 | rt5651->ovcd_sf);

        snd_soc_component_update_bits(component, RT5651_MICBIAS,
                                      RT5651_MIC1_OVCD_MASK |
                                      RT5651_MIC1_OVTH_MASK |
                                      RT5651_PWR_CLK12M_MASK |
                                      RT5651_PWR_MB_MASK,
                                      RT5651_MIC1_OVCD_EN |
                                      rt5651->ovcd_th |
                                      RT5651_PWR_MB_PU |
                                      RT5651_PWR_CLK12M_PU);

        /*
         * The over-current-detect is only reliable in detecting the absence
         * of over-current, when the mic-contact in the jack is short-circuited,
         * the hardware periodically retries if it can apply the bias-current
         * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
         * 10% of the time, as we poll the ovcd status bit we might hit that
         * 10%, so we enable sticky mode and when checking OVCD we clear the
         * status, msleep() a bit and then check to get a reliable reading.
         */
        snd_soc_component_update_bits(component, RT5651_IRQ_CTRL2,
                RT5651_MB1_OC_STKY_MASK, RT5651_MB1_OC_STKY_EN);

        rt5651->hp_jack = hp_jack;
        if (rt5651_support_button_press(rt5651)) {
                rt5651_enable_micbias1_for_ovcd(component);
                rt5651_enable_micbias1_ovcd_irq(component);
        }

        enable_irq(rt5651->irq);  // 使能中断,在设备树中我们并没有配置该项,如果配置了可以用来检测检测麦克风的插入/拔出
        /* sync initial jack state */
        queue_work(system_power_efficient_wq, &rt5651->jack_detect_work);
}

五、rt5651_dai

由于ALC5651有两组I2S接口,可以同时用于耳机输出以及Line output;RK3399与ALC5651连线如下:

                    ****************                         ***********
                    *              *                         * <------ * <---- MIC
 RAM <--------PCM-> * <----------> * <-I2S------------I2S1-> *         *
                    *              *            |            * ------> * ----> Line output
                    ****************            |            *         *
                          RK3399                |            *         *
                                                 -----I2S2-> * ------> * ----> HEADPHONE
                                                             ***********
                                                               ALC5651

codec dai和pcm配置信息通过结构体snd_soc_dai_driver描述,包括了dai的能力描述和操作接口;devm_snd_soc_register_component函数第三个参数为rt5651_dai,数组中长度为2,分别与ALC5651的两组I2S接口一一对应;

static struct snd_soc_dai_driver rt5651_dai[] = {
        {
                .name = "rt5651-aif1", // dai的名称,会赋值给与其关联的snd_soc_dai的name成员
// 音频数据链路是通过dai_name到ALC5651的component的dai_list链表中来查找dai的
.id
= RT5651_AIF1, // 0,dai的id .playback = { // 声卡注册的时候会为其创建一个类型为snd_soc_dapm_dai_in的playback dai widget,其name以及sname均设置为"AIF1 Playback" .stream_name = "AIF1 Playback", .channels_min = 1, // 最小通道数 .channels_max = 2, // 最大通道数 .rates = RT5651_STEREO_RATES, .formats = RT5651_FORMATS, }, .capture = { // 声卡注册的时候会为其创建一个类型为snd_soc_dapm_dai_out的capture dai widget,其name以及sname均设置为"AIF1 Capture" .stream_name = "AIF1 Capture", .channels_min = 1, .channels_max = 2, .rates = RT5651_STEREO_RATES, // 支持的采样率 SNDRV_PCM_RATE_8000_96000 999~96000之间 .formats = RT5651_FORMATS, // 支持的位深度 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8) }, .ops = &rt5651_aif_dai_ops, }, { .name = "rt5651-aif2", .id = RT5651_AIF2, // 1,dai的id .playback = { .stream_name = "AIF2 Playback", .channels_min = 1, .channels_max = 2, .rates = RT5651_STEREO_RATES, .formats = RT5651_FORMATS, }, .capture = { .stream_name = "AIF2 Capture", .channels_min = 1, .channels_max = 2, .rates = RT5651_STEREO_RATES, .formats = RT5651_FORMATS, }, .ops = &rt5651_aif_dai_ops, }, };

其中:

  • name:codec dai的名称标识,dai_link通过配置codec dai_name来找到对应的codec dai;
  • capture:描述capture的能力;如回放设备所支持的声道数、采样率、音频格式;非常重要的字段;
  • playback:描述playback的能力;如录制设备所支持声道数、采样率、音频格式;非常重要的字段;
  • ops:codec dai的操作函数集,这些函数集非常重要,用于dai的时钟配置、格式配置、硬件参数配置。

在注册ASoC声卡的时候,会调用soc_probe_link_components,每一个dai创建两个音频数据流的widget,以名字为 rt5651-aif1的dai为例:

  • 一个是类型为snd_soc_dapm_dai_in名称为AIF1 Playback的播放流widget;
  • 另一个是类型为snd_soc_dapm_dai_out名称为AIF1 Capture的录音流widget;

同时也会为构造以播放流widget作为输入端的路径,其连接sname为"AIF1 Playback"的widget;在rt5651_dapm_widgets数组中可以定位名字为AIF1RX的widge其sname与之匹配;

SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0)

同时也会构造以录音流widget作为输出端的路径,其输入端连接sname为"AIF1 Capture"的widget;在rt5651_dapm_widgets数组中可以定位名字为AIF1RX的widge其sname与之匹配;

SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0)

此时我们就可以得到一个从dai widget作为输入端的路径:AIF1 Playback --> AIF1RX --> IF1 DAC --> IF1 DAC1 L .... -->HPO L Playback --> HPOL --> Headphones  。

5.1 rt5651_aif_dai_ops 

音频操作接口通过结构体struct snd_soc_dai_ops描述:

static const struct snd_soc_dai_ops rt5651_aif_dai_ops = {
        .hw_params = rt5651_hw_params,
        .set_fmt = rt5651_set_dai_fmt,
        .set_sysclk = rt5651_set_dai_sysclk,
        .set_pll = rt5651_set_dai_pll,
};

其中:

  • set_sysclk:用于设置系统时钟,对于codec dai来说系统时钟指的是ALC5651 i2s1接口的MCLK信号线输入的时钟,同时也是RK3399 i2s0接口的MCLK信号线输出的时钟(对应的时钟名称为clk_i2sout);当上层打开pcm设备时,需要回调该接口设置ALC5651的系统时钟,ALC5651才能正常工作;
  • set_pll:用于设置ALC5651的PLL的分频系数,ALC5651一般接了一个MCLK作为ALC5651的PLL输入时钟源,回调该函数基于MCLK来产生ALC5651 PLL时钟;
  • set_fmt:设置数字音频接口格式,具体见 include/sound/soc-dai.h;
    • SND_SOC_DAIFMT_I2S:数字音频接口是I2S格式,常用于多媒体音频;
    • SND_SOC_DAIFMT_RIGHT_J:数字音频接口是I2S右对齐格式;
    • SND_SOC_DAIFMT_LEFT_J:数字音频接口是I2S左对齐格式;
    • SND_SOC_DAIFMT_DSP_A:数字音频接口是PCM格式,常用于语音通话;
    • SND_SOC_DAIFMT_DSP_B:数字音频接口是PCM格式,常用于语音通话;
    • SND_SOC_DAIFMT_CBM_CFM:ALC5651作为主机,BCLK 和 LRCLK由ALC5651提供;
    • SND_SOC_DAIFMT_CBS_CFS:ALC5651作为从机,BCLK和LRCLK由SoC/CPU提供;
    • .......
  • hw_params:codec dai硬件参数设置,根据上层设定的声道数、采样率、数据格式,来配置codec dai相关寄存器;

5.2 时钟

ALC5651的系统时钟(指的是下图MX80[15:14]寄存器控制的Mux的输出时钟)可以从MCLK和PLL中选择,MCLK时钟由外部时钟源提供,而PLL的参考时钟可以从MCLK、BCLK1/2。

5.2.1 set_sysclk

ALC5651音频操作接口set_sysclk函数用于设置系统时钟,set_sysclk被设置为rt5651_set_dai_sysclk,定义在sound/soc/codecs/rt5651.c:

static int rt5651_set_dai_sysclk(struct snd_soc_dai *dai,
                int clk_id, unsigned int freq, int dir)   // clk_id传入的0,dir也是传入的0,freq传入的是系统时钟频率
{
        struct snd_soc_component *component = dai->component;
        // 取出component->dev设备的driver_data,就是上面介绍的rt5651结构变量
        struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
        unsigned int reg_val = 0;
        unsigned int pll_bit = 0;

        if (freq == rt5651->sysclk && clk_id == rt5651->sysclk_src)   // 如果已经配置,直接返回
                return 0;

        switch (clk_id) {     // 系统时钟时钟源  MCLK、PLL
        case RT5651_SCLK_S_MCLK:      // 0 走这里
                reg_val |= RT5651_SCLK_SRC_MCLK;     // 0<<14
                break;
        case RT5651_SCLK_S_PLL1:     // 1
                reg_val |= RT5651_SCLK_SRC_PLL1;     // 1<<14
                pll_bit |= RT5651_PWR_PLL;           // 1<<9
                break;
        case RT5651_SCLK_S_RCCLK:    // 2               
                reg_val |= RT5651_SCLK_SRC_RCCLK;  // 2<<14
                break;
        default:
                dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
                return -EINVAL;
        }

        // 向寄存器写入值,RT5651_PWR_ANLG2的值为0x64,寄存器地址0x64用于电源控制寄存器4;
       // RT5651_PWR_LDO_DVO_MASK值为1<<9,因此这里向位[9]写入0/1,即PLL下电/上电
        snd_soc_component_update_bits(component, RT5651_PWR_ANLG2, 
                RT5651_PWR_PLL, pll_bit); 

       // 向寄存器写入值,RT5651_GLB_CLK的值为0x80,寄存器地址0x84全局时钟控制
      // RT5651_SCLK_SRC_MASK值为3<<14,因此这里向位[15:14]写入reg_val
        snd_soc_component_update_bits(component, RT5651_GLB_CLK,
                RT5651_SCLK_SRC_MASK, reg_val);
        rt5651->sysclk = freq;        // 设置系统时钟频率
        rt5651->sysclk_src = clk_id;  // 设置系统时钟时钟源

        dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);

        return 0;
}

该函数主要是用来配置ALC5651系统时钟,通过配置ALC5651相关寄存器来实现:

  • RT5651_PWR_ANLG2:电源控制寄存器4,寄存器地址为0x64,位[9]用来进行PLL Power Control(0:PLL Power Down、1:PLL Power Up);
  • RT5651_GLB_CLK:全局时钟控制寄存器,寄存器地址为0x80,位[15:14]用来进行System Clock Source MUX Control(00'b:MCLK、01'b:PLL、10'b:保留、11'b:保留);

因此我们可以得出结论:

  • 如果配置系统时钟时钟源rt5651->sysclk_src为MCLK,则配置PLL下电;此时PLL禁用,系统时钟频率则为外部输入的MCLK的时钟频率;
  • 如果配置系统时钟时钟源rt5651->sysclk_src为PLL,则配置PLL上电;此时PLL使能,因此可以根据系统时钟频率动态计算PLL分频系数,并通过set_pll设置PLL分频系数;

当我们移植完了声卡驱动,并通过开发板ubuntu系统进行音频播放的时候,我特意查看了一下0x64、0x80寄存器的值:

  • 0x64:寄存器值为0x00,表明PLL处于禁用状态;
  • 0x80:寄存器值为0x00,表明ALC5651系统时钟时钟源选择为MCLK;
5.2.2 set_pll

如果我们在set_sysclk中设置了系统时钟类型rt5651->sysclk_src为PLL,则需要通过ALC5651音频操作接口set_pll函数设置PLL分频系数,set_pll被设置为rt5651_set_dai_pll,定义在sound/soc/codecs/rt5651.c:

函数由若干个参数,其中:

  • source:PLL的输入时钟源类型,可以是MCLK、BCLK1、BLCK2中任意一个;一般设置为MCLK;
  • freq_in:为PLL输入时钟源的频率;
  • freq_out:经过PLL分频后的输出频率;

函数源代码如下:

static int rt5651_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
                        unsigned int freq_in, unsigned int freq_out)  // 输入频率 -> 输出频率
{
        struct snd_soc_component *component = dai->component;
     // 取出component->dev设备的driver_data,就是上面介绍的rt5651结构变量
struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component); struct rl6231_pll_code pll_code; int ret; if (source == rt5651->pll_src && freq_in == rt5651->pll_in && // 如果已经配置,直接返回 freq_out == rt5651->pll_out) return 0; if (!freq_in || !freq_out) { // 如果PLL输入、或者输出时钟频率为0 dev_dbg(component->dev, "PLL disabled\n"); rt5651->pll_in = 0; rt5651->pll_out = 0; snd_soc_component_update_bits(component, RT5651_GLB_CLK, RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_MCLK); return 0; } switch (source) { // 时钟类型 case RT5651_PLL1_S_MCLK: // 0 MCLK作为PLL的输入时钟源 snd_soc_component_update_bits(component, RT5651_GLB_CLK, // 配置MX80寄存器位[13:12]为00'b,PLL时钟源选择为MCLK RT5651_PLL1_SRC_MASK, RT5651_PLL1_SRC_MCLK); break; case RT5651_PLL1_S_BCLK1: // 1 snd_soc_component_update_bits(component, RT5651_GLB_CLK, // 配置MX80寄存器位[13:12]为01'b,PLL时钟源选择为BLCK1 RT5651_PLL1_SRC_MASK, RT5651_PLL1_SRC_BCLK1); break; case RT5651_PLL1_S_BCLK2: // 2 snd_soc_component_update_bits(component, RT5651_GLB_CLK, // 配置MX80寄存器位[13:12]为10'b,PLL时钟源选择为MCLK2 RT5651_PLL1_SRC_MASK, RT5651_PLL1_SRC_BCLK2); break; default: dev_err(component->dev, "Unknown PLL source %d\n", source); return -EINVAL; } ret = rl6231_pll_calc(freq_in, freq_out, &pll_code); // 根据输入、输出频率 计算pll_code if (ret < 0) { dev_err(component->dev, "Unsupported input clock %d\n", freq_in); return ret; } dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n", pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code), pll_code.n_code, pll_code.k_code); snd_soc_component_write(component, RT5651_PLL_CTRL1, pll_code.n_code << RT5651_PLL_N_SFT | pll_code.k_code); // 配置N RT5651_PLL_N_SFT=7 snd_soc_component_write(component, RT5651_PLL_CTRL2, ((pll_code.m_bp ? 0 : pll_code.m_code) << RT5651_PLL_M_SFT) | // 配置M RT5651_PLL_M_SFT=12 (pll_code.m_bp << RT5651_PLL_M_BP_SFT)); // RT5651_PLL_M_BP_SFT=11 rt5651->pll_in = freq_in; // 保存配置 rt5651->pll_out = freq_out; rt5651->pll_src = source; return 0; }

AL5651 PLL控制寄存器有两个MX81:PLL控制寄存器1,MX82:PLL控制寄存器2;

PLL的输出频率计算公式如下:

$$$F_{OUT}=\frac{MCLK*(N+2)}{(M+2)*(K+2)}  {典型的K=2} $

当输出时钟频率为44.1kHz:

rl6231_pll_calc函数是根据输入时钟频率、和输出时钟频率来计算N和M的,函数定义在sound/soc/codecs/rl6231.c,由于计算过程比较复杂,咱们就不研究了。

当我们移植完了声卡驱动,并通过开发板ubuntu系统进行音频播放的时候,我特意查看了一下0x81、0x82寄存器的值:

  • 0x81:0x00,由于PLL被禁用了,因此配置PLL控制寄存器没啥意义了;
  • 0x82:0x00,由于PLL被禁用了,因此配置PLL控制寄存器没啥意义了;

5.3 set_fmt 

set_fmt用于设置ALC565数字音频接口格式,set_fmt被设置为rt5651_set_dai_fmt,定义在sound/soc/codecs/rt5651.c:

static int rt5651_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
        struct snd_soc_component *component = dai->component;
   // 取出component->dev设备的driver_data,就是上面介绍的rt5651结构变量
struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component); unsigned int reg_val = 0; switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { // 0xf000 case SND_SOC_DAIFMT_CBM_CFM: // ALC5651作为主机 1<<12 rt5651->master[dai->id] = 1; break; case SND_SOC_DAIFMT_CBS_CFS: // ALC5651作为从机 4<<12 reg_val |= RT5651_I2S_MS_S; rt5651->master[dai->id] = 0; break; default: return -EINVAL; } switch (fmt & SND_SOC_DAIFMT_INV_MASK) { // BCLK极性配置 case SND_SOC_DAIFMT_NB_NF: break; case SND_SOC_DAIFMT_IB_NF: reg_val |= RT5651_I2S_BP_INV; break; default: return -EINVAL; } switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { // 音频数据接口格式,0x000f case SND_SOC_DAIFMT_I2S: // 标准I2S模式 break; case SND_SOC_DAIFMT_LEFT_J: // I2S左对齐模式 reg_val |= RT5651_I2S_DF_LEFT; break; case SND_SOC_DAIFMT_DSP_A: reg_val |= RT5651_I2S_DF_PCM_A; //PCM模式A break; case SND_SOC_DAIFMT_DSP_B: reg_val |= RT5651_I2S_DF_PCM_B; // PCM模式B break; default: return -EINVAL; } switch (dai->id) { // I2S1 或者 I2S2 case RT5651_AIF1: // I2S1 snd_soc_component_update_bits(component, RT5651_I2S1_SDP, // MX70 RT5651_I2S_MS_MASK | RT5651_I2S_BP_MASK | // 1<<15 1<<7 RT5651_I2S_DF_MASK, reg_val); // 3 break; case RT5651_AIF2: // I2S2 snd_soc_component_update_bits(component, RT5651_I2S2_SDP, RT5651_I2S_MS_MASK | RT5651_I2S_BP_MASK | RT5651_I2S_DF_MASK, reg_val); break; default: dev_err(component->dev, "Wrong dai->id: %d\n", dai->id); return -EINVAL; } return 0; }

该函数主要是用来配置ALC5651数字音频接口格式,通过配置ALC5651 数字音频接口控制寄存器来实现,针对I2S1、I2S2接口各有一个配置寄存器。咱们以RT5651_I2S1_SDP为例介绍,I2S1数字音频接口控制寄存器,寄存器地址为0x70;

  • 位[15]:I2S1 Digital Interface Mode Control,0’b: Master Mode(主机模式,即ALC5651作为主设备),1’b: Slave Mode(从机模式,即ALC5651作为从设备);
  • 位[7]:I2S1 BCLK Polarity Control,0’b: Normal,1’b: Invert(BLCK极性控制);
  • 位[1:0]:I2S1 PCM Data Format Selection;
    • 00’b:  I2S format(I2S标准模式);
    • 01’b: Left justified(I2S左对齐模式);
    • 10’b: PCM Mode A (LRCK One Plus at Master Mode)(PCM模式A);
    • 11’b: PCM Mode B (LRCK One Plus at Master Mode)(PCM模式B);

当我们移植完了声卡驱动,并通过开发板ubuntu系统进行音频播放的时候,我特意查看了一下0x70寄存器的值:

  • 0x70:值为0x8000,第15位被设置为1,表明ALC5651设备作为丛机模式;第7位被设置为0,BLCK Polarity正常;位[1:0]被设置为00'b,I2S标准模式;

5.4 hw_params

hw_params被设置为rt5651_hw_params,用于codec dai硬件参数设置,根据上层设定的声道数、采样率、数据格式,来配置codec dai相关寄存器;

static int rt5651_hw_params(struct snd_pcm_substream *substream,
        struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
        struct snd_soc_component *component = dai->component;
        struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component); // 取出component->dev设备的driver_data,就是上面介绍的rt5651结构变量
        unsigned int val_len = 0, val_clk, mask_clk;
        int pre_div, bclk_ms, frame_size;

        rt5651->lrck[dai->id] = params_rate(params);  // LRCK,声道选择信号,其频率等于采样频率
        pre_div = rl6231_get_clk_info(rt5651->sysclk, rt5651->lrck[dai->id]);  // 计算预分频值,用于DIV_F1(参考ALC5651是时钟模块架构)

        if (pre_div < 0) {
                dev_err(component->dev, "Unsupported clock setting\n");
                return -EINVAL;
        }
        frame_size = snd_soc_params_to_frame_size(params); // 获取帧大小,即每帧所占用的位数;那什么是帧呢,以双声道为例,一帧指的就是包含左、右声道采样点的数据,可以理解为通道数*采样位数
        if (frame_size < 0) {
                dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
                return -EINVAL;
        }
        bclk_ms = frame_size > 32 ? 1 : 0;        
        rt5651->bclk[dai->id] = rt5651->lrck[dai->id] * (32 << bclk_ms); // 计算位时钟,BCLK=通道数×采样率×采样位数

        dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
                rt5651->bclk[dai->id], rt5651->lrck[dai->id]);
        dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
                                bclk_ms, pre_div, dai->id);

        switch (params_width(params)) {  // 设置位深度
        case 16:
                break;
        case 20:
                val_len |= RT5651_I2S_DL_20;
                break;
        case 24:
                val_len |= RT5651_I2S_DL_24;
                break;
        case 8:
                val_len |= RT5651_I2S_DL_8;
                break;
        default:
                return -EINVAL;
        }

        switch (dai->id) {
        case RT5651_AIF1:   // I2S1接口
                mask_clk = RT5651_I2S_PD1_MASK;             // 7<<12
                val_clk = pre_div << RT5651_I2S_PD1_SFT;    // RT5651_I2S_PD1_SFT=12
                snd_soc_component_update_bits(component, RT5651_I2S1_SDP,  // 0x70寄存器,更新位[3:2]
                        RT5651_I2S_DL_MASK, val_len);        // 3<<2
                snd_soc_component_update_bits(component, RT5651_ADDA_CLK1, mask_clk, val_clk);  // 0x73寄存器,更新位[14:12]
                break; 
        case RT5651_AIF2:     // I2S2接口
                mask_clk = RT5651_I2S_BCLK_MS2_MASK | RT5651_I2S_PD2_MASK;
                val_clk = pre_div << RT5651_I2S_PD2_SFT;
                snd_soc_component_update_bits(component, RT5651_I2S2_SDP,
                        RT5651_I2S_DL_MASK, val_len);
                snd_soc_component_update_bits(component, RT5651_ADDA_CLK1, mask_clk, val_clk);
                break;
        default:
                dev_err(component->dev, "Wrong dai->id: %d\n", dai->id);
                return -EINVAL;
        }
        return 0;
}

这段代码是一个 RT5651音频芯片的硬件参数配置函数。它被用于设置数字音频接口的参数,包括采样率、帧大小等。

  • 函数首先从传入的参数中获取采样率,并通过一个特定的函数(rl6231_get_clk_info)计算出相应的预分频值(pre_div);
  • 接下来,函数使用snd_soc_params_to_frame_size函数将参数转换成帧大小;
  • 根据帧大小的值,函数计算位时钟(bit clock,即BCLK)的频率。这里使用了一个简单的公式:BCLK = 采样率 × 帧大小。同时,也计算了 BCLK 模式(bclk_ms)的值,该值取决于帧大小是否大于 32。函数会打印 BCLK和 LRCK 的频率以及bclk_ms和pre_div的值;
  • 之后,计算I2S的位深度;
  • 最后,根据不同的音频接口(dai->id),函数设置相应的寄存器位;

这里硬件寄存器操作涉及到0x70、以及0x73寄存器:

  • RT5651_I2S1_SDP:I2S1数字音频接口控制寄存器,寄存器地址为0x70;位[3:2]表示I2S1位深度选择;
    • 00'b:16位;
    • 01'b:20位;
    • 10‘b:24位;
    • 11'b:8位;
  • RT5651_ADDA_CLK1:ADC/DAC时钟控制寄存器,寄存器地址位0x73,位[14:12]用于控制I2S Clock Pre-Divider 1:
    • 000'b:+1;
    • 001'b:+2;
    • 010'b:+3;
    • 011'b:+4;
    • 100'b:+6;
    • 101'b:+8;
    • 110'b:+12;
    • 111'b:+16;

当我们移植完了声卡驱动,并通过开发板ubuntu系统进行音频播放的时候,我特意查看了一下0x70、0x73寄存器的值:

  • 0x70:值为0x8000,位[3:2]被设置为0,表示位深度为16位;
  • 0x73:值位0x0104,位[14:12]被设置为0,表示分频系数位1,则Clk_sys_i2s1 = MCLK=256*FS(采样频率);

播放音频时,硬件参数配置具体如下(不同音乐文件,看到的数据format、rate可能不一样):

root@rk3399:/proc/asound/card0/pcm0p/sub0# cat hw_params
access: RW_INTERLEAVED
format: S16_LE
subformat: STD
channels: 2
rate: 44100 (44100/1)    
period_size: 5513
buffer_size: 22052
5.4.1 params_rate

params_rate用于计算采样频率,函数定义在include/sound/pcm.h:

/**
 * params_rate - Get the sample rate from the hw params
 * @p: hw params
 *
 * Return: the sample rate
 */
static inline unsigned int params_rate(const struct snd_pcm_hw_params *p)
{
        return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_RATE)->min;        // 宏SNDRV_PCM_HW_PARAM_RATE的值为11
}

函数内部又调用了hw_param_interval_c:

static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
                                                     snd_pcm_hw_param_t var)
{
        return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];   // 宏SNDRV_PCM_HW_PARAM_SAMPLE_BITS的值为8
}

这里实际上就是获取的params->intervals[3]->min的值,intervals数组第三个元素存放的就是近似采样率的信息。

5.4.2 snd_soc_params_to_frame_size

snd_soc_params_to_frame_size用于计算获取帧大小,函数定义在sound/soc/soc-utils.c:

int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
{
        int sample_size;

        sample_size = snd_pcm_format_width(params_format(params));
        if (sample_size < 0)
                return sample_size;

        return snd_soc_calc_frame_size(sample_size, params_channels(params),
                                       1);
}

其中params_format定义在include/sound/pcm_params.h:

/**
 * params_format - get the sample format from the hw params
 * @p: hw params
 */
static inline snd_pcm_format_t params_format(const struct snd_pcm_hw_params *p)
{
        return (__force snd_pcm_format_t)snd_mask_min(hw_param_mask_c(p,
                SNDRV_PCM_HW_PARAM_FORMAT));
}

这里就不继续深究了,没啥意义。

5.4.3 params_width

函数params_width用于获取位深度,函数定义在include/sound/pcm_params.h:

/**
 * params_width - get the number of bits of the sample format from the hw params
 * @p: hw params
 *
 * This function returns the number of bits per sample that the selected sample
 * format of the hw params has.
 */
static inline int params_width(const struct snd_pcm_hw_params *p)
{
        return snd_pcm_format_width(params_format(p));
}

参考文章

[1] 理解ALSA(三):从零写ASoC驱动

[2] 08.音频系统:第003课_LINUX音频驱动程序:第002节_ASOC音频驱动框架

[3] Linux ALSA 音频系统:物理链路篇

[4] Linux ALSA声卡驱动之七:ASoC架构中的Codec

[5] Linux音频驱动-AOSC之Codec

[6] 08.音频系统:第003课_Linux音频驱动程序:第005节_DAPM_widget_route_path

[7] [RK3399][Android7.1] 调试笔记 --- I2S1工作输出是12MHz问题

[8] RK3399平台适配TI-tlv320aic3111音频芯片

[9] Rockchip RK3399 - ALC5651 & I2S基础

[10] RK3399平台适配TI-tlv320aic3111音频芯片

[11]  基于RK3399分析Linux系统下的CPU时钟管理 - 第1篇

[12] 基于RK3399分析Linux系统下的CPU时钟管理 - 第2篇

[13] 基于RK3399分析Linux系统下的CPU时钟管理 - 第3篇

posted @ 2023-07-30 14:28  大奥特曼打小怪兽  阅读(2127)  评论(2编辑  收藏  举报
如果有任何技术小问题,欢迎大家交流沟通,共同进步