Raspberry Pi ZeroのI2Sオーディオドライバに非Codecドライバを追加登録する方法

概要

  • ALSA SoCドライバで電子ボリュームを利用する方法を調べてみた。
  • 今回は電子ボリューム内蔵ヘッドフォンアンプIC TPA6130A2ドライバをhifiberry-dacドライバに登録してみる。

仕組み

  • TPA6130A2のドライバはsound/soc/codecs/tpa6130a2.cを使用する
  • TPA6130A2のようなCodecではないボリューム制御のみのICはaux devとして登録する
  • aux devはcodecではないため、codecとしてdai link登録はできない
  • dai linkではなく、soc_card構造体のaux devsに登録してregisterする

ソース修正例

  • サウンドカード(soc_card)を登録するDAI LINKドライバにdevice treeのi2s-auxdevエントリ参照処理を追加する
    • device treeにi2s-auxdevエントリがあればそのデバイスノードをaux devに登録する
    • i2s_auxdevに加えてi2s-codecをdevice treeから指定する機能も組み込んでいます。
  • 修正コード sound/soc/bcm/hifiberry_dac.c
@@ -14,6 +14,8 @@
  * General Public License for more details.
  */

+#define DEBUG
+
 #include <linux/module.h>
 #include <linux/platform_device.h>

@@ -22,6 +24,8 @@
 #include <sound/pcm_params.h>
 #include <sound/soc.h>
 #include <sound/jack.h>
+#include <linux/of_device.h>
+#include <linux/of_graph.h>

 static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
 {
@@ -72,12 +76,14 @@ static struct snd_soc_card snd_rpi_hifiberry_dac = {
 static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
 {
        int ret = 0;
+       int cnt;

        snd_rpi_hifiberry_dac.dev = &pdev->dev;

        if (pdev->dev.of_node) {
                struct device_node *i2s_node;
                struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_dac_dai[0];
+
                i2s_node = of_parse_phandle(pdev->dev.of_node,
                                        "i2s-controller", 0);

@@ -87,6 +93,67 @@ static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
                        dai->platform_name = NULL;
                        dai->platform_of_node = i2s_node;
                }
+
+               cnt = of_count_phandle_with_args(pdev->dev.of_node, "i2s-codec", "#sound-dai-cells");
+               dev_info(&pdev->dev, "%s: i2s-codec sound-dai = %d", __func__, cnt);
+
+               if ( cnt > 0 ) {
+                       struct snd_soc_dai_link_component *codecs;
+                       struct of_phandle_args args;
+                       int i;
+
+                       dai->codec_name = NULL;
+                       dai->codec_dai_name = NULL;
+                       dai->codec_of_node = NULL;
+
+                       codecs = devm_kzalloc(&pdev->dev, sizeof(struct snd_soc_dai_link_component)*cnt, GFP_KERNEL);
+                       if (!codecs)
+                               return -ENOMEM;
+
+                       for ( i = 0; i < cnt ; i++ ) {
+                               ret = of_parse_phandle_with_args(pdev->dev.of_node, "i2s-codec",
+                                       "#sound-dai-cells", i, &args);
+                               if (ret) {
+                                       dev_err(&pdev->dev, "%s: Can't find codec by \"i2s-codec\"\n", __func__);
+                                       return 0;
+                               }
+                               codecs[i].of_node = args.np;
+                               if (snd_soc_get_dai_name(&args, &codecs[i].dai_name) < 0 ) {
+                                       dev_err(&pdev->dev, "%s: failed to find dai name, use codec's name as dai name.\n", __func__);
+                                       codecs[i].dai_name = codecs[i].of_node->name;
+                               }
+                               dev_dbg(&pdev->dev, "add codec %s @ %s", args.np->name, codecs[i].dai_name);
+                       }
+                       dai->codecs = codecs;
+                       dai->num_codecs = cnt;
+               }
+
+               cnt = of_count_phandle_with_args(pdev->dev.of_node, "i2s-auxdev", "#sound-dai-cells");
+               dev_info(&pdev->dev, "%s: i2c-auxdev sound-dai = %d", __func__, cnt);
+
+               if ( cnt > 0 ){
+                       struct snd_soc_aux_dev *auxdevs;
+                       struct of_phandle_args args;
+                       int i;
+
+                       auxdevs = devm_kzalloc(&pdev->dev, sizeof(struct snd_soc_dai_link_component)*cnt, GFP_KERNEL);
+                       if (!auxdevs)
+                               return -ENOMEM;
+
+                       for ( i = 0; i < cnt ; i++ ) {
+                               ret = of_parse_phandle_with_args(pdev->dev.of_node, "i2s-auxdev",
+                                                               "#sound-dai-cells", i, &args);
+                               if (ret) {
+                                       dev_err(&pdev->dev, "%s: Can't find codec by \"i2s-auxdev\"\n", __func__);
+                                       return 0;
+                               }
+                               auxdevs[i].codec_of_node = args.np;
+                               auxdevs[i].name    = args.np->name;
+                               dev_dbg(&pdev->dev, "add aux dev %s", auxdevs[i].name);
+                       }
+                       snd_rpi_hifiberry_dac.aux_dev = auxdevs;
+                       snd_rpi_hifiberry_dac.num_aux_devs = cnt;
+               }
        }

        ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);

device tree overlay設定例

  • fragment@1でTPA6130A2を定義
  • fragment@3のi2s-auxdevでTPA6130A2を参照するように定義
// Definitions for HiFiBerry DAC
/dts-v1/;
/plugin/;

/ {
        compatible = "brcm,bcm2708";

        fragment@0 {
                target = <&i2s>;
                __overlay__ {
                        status = "okay";
                };
        };

        fragment@1 {
                target = <&i2c1>;
                __overlay__ {
                        #address-cells = <1>;
                        #size-cells = <0>;
                        status = "okay";

                        tpa6130a2: tpa6130a2@60 {
                                #sound-dai-cells = <0>;
                                compatible = "ti,tpa6130a2";
                                reg = <0x60>;
                                status = "okay";
                        };

                };
        };

        fragment@2 {
                target-path = "/";
                __overlay__ {
                        pcm5102a: pcm5102a-codec {
                                #sound-dai-cells = <0>;
                                compatible = "ti,pcm5102a";
                                status = "okay";
                        };
                };
        };


        fragment@3 {
                target = <&sound>;
                __overlay__ {
                        compatible = "hifiberry,hifiberry-dac";
                        i2s-controller = <&i2s>;
                        i2s-codec = <&pcm5102a>;
                        i2s-auxdev = <&tpa6130a2>;
                        status = "okay";
                };
        };
};