首页 > 其他分享 >Rockchip RK3399 - Codec驱动( Realtek ALC5651)

Rockchip RK3399 - Codec驱动( Realtek ALC5651)

时间:2023-07-30 14:45:04浏览次数:49  
标签:SOC DAC Rockchip ALC5651 Realtek DAPM SND RT5651 rt5651

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

开发板 :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;

  • #sound-dai-cells :指定DAI的单元格数量,这里为0表示不需要添加额外的 DAI 单元格(比如,不需要配置 SAI、PCM 等标志);
  • compatible:指定设备驱动程序的兼容性,即告诉内核该设备可以被哪些驱动程序所使用;
  • reg:指定了rt5651设备在I2C控制器上的设备地址;
  • clocks:定义了音频编解码器使用的时钟,这里使用了由 PLL(Phase Locked Loop)产生的 SCLK_I2S_8CH_OUT 时钟信号;
  • clock-names:定义了音频编解码器使用的时钟的名称,这里为mclk,表示主时钟;
  • status :指定设备状态为“正常”,表示该设备状态为正常运行;

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";
};

二、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",
     .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.3.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从设备,其名称为rt5651,因此会与I2C从设备驱动中rt5651_of_match匹配失败。

3.3.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从设备,其名称为rt5651,因此会与I2C从设备驱动中的rt5651_i2c_id匹配成功,从而进入执行probe探测函数;

3.3.3 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中并没有配置中断,因此申请中断会失败,内核启动的时候也会输出相关错误信息:

[    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之间的关系如下图:

四、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,检查是否存在完整的音频路径,如果存在完整的音频路径,则该路径上面的所有部件都是需要上电的,其他部件则下电;

(2) kcontrol上下电都是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中又找到了如下一张图:

上图中我们使用箭头标识了一条用于多媒体Line output的路径,音频通路是:

  • I2S1 ASRC --> I2S1;ASRC全称Asynchronous Sample Rate Converter,异步采样率转换器;
  • I2S1 --> IF1 DAC;
  • IF1 DAC --> IF1 DAC1 L;
  • IF1 DAC1 L --> DAC MIXL:通过rt5651_dac_l_mix(名称为INF1 Switch)控制通断,由MX29寄存器位14来实现静音控制(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 --> OUTVOL L:通过outvol_l_control(名称为Switch)控制通断,由MX03寄存器位14来实现静音控制(0非静音,1静音);
  • OUTVOL L --> LOUT MIX:通过rt5651_lout_mix(名称为OUTVOL L Switch)控制通断,由MX53寄存器位13来实现静音控制(0非静音,1静音);
  • LOUT MIX --> LOUT L Playback:通过lout_l_mute_control(名称为Switch)控制通断,由MX03寄存器位15来实现静音控制(0非静音,1静音);
  • LOUT L Playback --> LOUTL;

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

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.

在这些定义中我们可以找到位于多媒体Line output路径上的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),
};

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 outvol_l_control =
        SOC_DAPM_SINGLE("Switch", RT5651_LOUT_CTRL1,       // 寄存器配置为RT5651_LOUT_CTRL1=0x03,偏移位配置为14
                        RT5651_VOL_L_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=0x53,偏移位配置为15
                        RT5651_M_DAC_L1_LM_SFT, 1, 1),
        SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_LOUT_MIXER,  // 寄存器配置为RT5651_M_DAC_L1_LM_SFT=0x53,偏移位配置为14
                        RT5651_M_DAC_R1_LM_SFT, 1, 1),
        SOC_DAPM_SINGLE("OUTVOL L Switch", RT5651_LOUT_MIXER,  // 寄存器配置为RT5651_M_DAC_L1_LM_SFT=0x53,偏移位配置为13
                        RT5651_M_OV_L_LM_SFT, 1, 1),
        SOC_DAPM_SINGLE("OUTVOL R Switch", RT5651_LOUT_MIXER,  // 寄存器配置为RT5651_M_DAC_L1_LM_SFT=0x53,偏移位配置为12
                        RT5651_M_OV_R_LM_SFT, 1, 1),
};

static const struct snd_kcontrol_new lout_l_mute_control =
        SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_LOUT_CTRL1,   // 寄存器配置为RT5651_LOUT_CTRL1=0x03,偏移位配置为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中我们可以找到位于多媒体Line output路径上的widget;

/* ASRC */
SND_SOC_DAPM_SUPPLY_S("I2S1 ASRC", 1, RT5651_PLL_MODE_2, // ASRC控制寄存器2, 寄存器地址0x84
                      15, 0, NULL, 0),   // 位15:Enable Control for I2S1 0:正常模式 1:ASRC模式
/* Digital Interface */
SND_SOC_DAPM_SUPPLY("I2S1", RT5651_PWR_DIG1,          // 电源控制寄存器1,地址为0x61
                    RT5651_PWR_I2S1_BIT, 0, NULL, 0), // 位15用于I2S1数字音频接口电源控制,0:下电,1上电
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("OUTVOL L", RT5651_PWR_VOL,            // Left Output Volume包含1个kcontrol,用于控制开关的通断
                    RT5651_PWR_OV_L_BIT, 0, &outvol_l_control),             
/* HPO/LOUT/Mono Mixer */
SND_SOC_DAPM_MIXER("LOUT MIX", RT5651_PWR_ANLG1, RT5651_PWR_LM_BIT, 0,  // Left Output Mixer包含4个kcontrol,每个kcontrol控制着Mixer的一个输入端的开启和关闭
                   rt5651_lout_mix, ARRAY_SIZE(rt5651_lout_mix)), 
                   
SND_SOC_DAPM_SWITCH("LOUT L Playback", SND_SOC_NOPM, 0, 0,  // Left Output Playback包含1个kcontrol,用于控制开关的通断
                    &lout_l_mute_control),

/* Output Lines */
SND_SOC_DAPM_OUTPUT("LOUTL"),     // LOUTL为ALC5651的Leftt Line Output引脚,可以用来外接扬声器
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中我们可以找到位于多媒体Line output路径上的route;

{"I2S1", NULL, "I2S1 ASRC"},
{"IF1 DAC", NULL, "I2S1"},
{"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"}
{"OUTVOL L", "Switch", "OUT MIXL"},
{"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
{"LOUT L Playback", "Switch", "LOUT MIX"}

三个参数分别为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函数定义如下:

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

五、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, .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, .formats = RT5651_FORMATS, }, .ops = &rt5651_aif_dai_ops, }, { .name = "rt5651-aif2", .id = RT5651_AIF2, .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的能力;如回放设备所支持的声道数、采样率、音频格式;非常重要的字段;
  • playbackk:描述playback的能力;如录制设备所支持声道数、采样率、音频格式;非常重要的字段;
  • ops:codec dai的操作函数集,这些函数集非常重要,用于dai的时钟配置、格式配置、硬件参数配置。

在注册ASoC声卡的时候,会为每一个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 .... --> LOUT L Playback --> LOUTL  。

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:用于设置ALC5651的系统时钟,当上层打开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函数用于设置ALC5651的系统时钟,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)
{
        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:
                reg_val |= RT5651_SCLK_SRC_MCLK;     // 0<<14
                break;
        case RT5651_SCLK_S_PLL1:
                reg_val |= RT5651_SCLK_SRC_PLL1;     // 1<<14
                pll_bit |= RT5651_PWR_PLL;           // 1<<9
                break;
        case RT5651_SCLK_S_RCCLK:                    // 2<<14
                reg_val |= RT5651_SCLK_SRC_RCCLK;
                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下电,系统时钟频率则为外部输入的MCLK的时钟频率;
  • 如果配置系统时钟时钟源rt5651->sysclk_src为PLL,则配置PLL上电;此时PLL使能,因此可以根据系统时钟频率动态计算PLL分频系数,并通过set_pll设置PLL分频系数;
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: // 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: 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: 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,由于计算过程比较复杂,咱们就不研究了。

5.3 set_fmt 

set_fmt用于设置ALC565数字音频接口格式的格式,set_pll被设置为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);

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);
        unsigned int val_len = 0, val_clk, mask_clk;
        int pre_div, bclk_ms, frame_size;

        rt5651->lrck[dai->id] = params_rate(params);
        pre_div = rl6231_get_clk_info(rt5651->sysclk, rt5651->lrck[dai->id]);

        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);

        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:
                mask_clk = RT5651_I2S_PD1_MASK;
                val_clk = pre_div << RT5651_I2S_PD1_SFT;
                snd_soc_component_update_bits(component, RT5651_I2S1_SDP,
                        RT5651_I2S_DL_MASK, val_len);
                snd_soc_component_update_bits(component, RT5651_ADDA_CLK1, mask_clk, val_clk);
                break;
        case RT5651_AIF2:
                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;
}

参考文章

[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

标签:SOC,DAC,Rockchip,ALC5651,Realtek,DAPM,SND,RT5651,rt5651
From: https://www.cnblogs.com/zyly/p/17591411.html

相关文章

  • Rockchip RK3399 - Platform驱动(DMA&i2s0)
    Platfromdriver提供了配置/使能SoC音频接口的能力;Plaftrom驱动分为两个部分:snd_soc_platform_driver、snd_soc_dai_driver。snd_soc_platform_driver:负责管理音频数据,把音频数据通过DMA或其他操作传送至CPUDAI中;snd_soc_dai_driver:负责完成SoC一侧的DAI参数配置,同时也会通过......
  • Rockchip RK3399 - 音频调试
    一、内核配置1.1配置内核修改sound/soc/codecs/Kconfig文件,搜索configSND_SOC_RT5651,将该项修configSND_SOC_RT5651tristatedependsonI2C改为,如果不修改的话,makemenuconfig是看不到该配置项的;configSND_SOC_RT5651tristate"RealtekAL......
  • Rockchip RK3399 - DAPM
    DAPM是DynamicAudioPowerManagement的缩写,即动态音频电源管理,旨在允许便携式Linux设备在任何时候使用音频子系统中的最小电量。它独立于其他内核PowerManager,故可以很容易地与其他PM系统共存。DAPM对所有用户空间应用程序来说也是完全透明的,因为所有电源切换都是在ASoC核心内......
  • Rockchip RK3399 - Machine驱动
    Machinedriver描述了如何控制platform、codec、cpudai(DigitalAudioInterface,数字音频接口)和codecdai,使得互相配合在一起工作,Machine驱动代码位于sound/soc/generic/simple-card.c文件。一、设备节点1.1设备节点rt5651-sound我们在arch/arm64/boot/dts/rockchip/rk3399-ev......
  • Rockchip RK3399 - Codec驱动基础
    在上一篇博客我们介绍了ALSA子系统的软件架构,同时介绍了ALSACORE核心数据结构和相关API。本节我们将会介绍ASoC软件体系中音频三大驱动模块:Codec、Platform和Machine。一、ASoC核心数据结构我们首先来了解Codec、Platform和Machine驱动中涉及到的数据结构,知道每个数据结构以......
  • Rockchip RK3399 - Platform驱动
    Platfromdriver提供了配置/使能SoC音频接口的能力;Plaftrom驱动分为两个部分:snd_soc_platform_driver、snd_soc_dai_driver。snd_soc_platform_driver:负责管理音频数据,把音频数据通过DMA或其他操作传送至CPUDAI中;snd_soc_dai_driver:负责完成SoC一侧的DAI参数配置,同时也会通过......
  • Rockchip RK3399 - Codec驱动( Realtek ALC5651)
    Codecdriver提供了配置/使能Codec的能力,驱动代码位于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......
  • Rockchip rk3588 U-Boot详解 (三)
    Rockchiprk3588U-Boot详解(三)专栏总目录1.1Environment-VariablesENV(Environment-Variables)是U-Boot支持的一种全局数据管理和传递方式,原理是构建一张HASH映射表,把用户的数据以"键值-数据"作为表项进行管理。EVN通常用于定义平台配置参数:固件加载地址、网络配置(ipaddr、serve......
  • RK3588(YD-88)瑞芯微 Rockchip RK3588 开发板套件,支持8G内存,32G eMMC存储
     一、产品简介1.产品简述:YD-88 是基于瑞芯微RK3588 的一款核心板RK3588是一颗高性能、低功耗的应用处理器芯片,专为ARMPC、边缘计算、个人移动互联网设备和其它多媒体应用而设计,是由4个A76和4个A55与独立的NEON协处理器集成的。RK3588内置了多种功能强大的嵌入式......
  • Rockchip rk3588 U-Boot详解 (三)
    Rockchiprk3588U-Boot详解(三)专栏总目录1.1Environment-VariablesENV(Environment-Variables)是U-Boot支持的一种全局数据管理和传递方式,原理是构建一张HASH映射表,把用户的数据以"键值-数据"作为表项进行管理。EVN通常用于定义平台配置参数:固件加载地址、网络配置(i......