Linux学习笔记(23)——基于wm8960的音频设备驱动

  1. 根据WM8960与处理器接口SAI和I2C来修改设备树文件
    a) 首先在i2c2节点下增加wm8960子节点
codec: wm8960@1a {
		compatible = "wlf,wm8960";			/* 兼容属性 */
		reg = <0x1a>;						/* wm8960的I2C地址 */
		clocks = <&clks IMX6UL_CLK_SAI2>;	
		clock-names = "mclk";
		wlf,shared-lrclk;					/* 此属性决定WM8960的R24寄存器LRCM(bit2), 当LRCM为1时只有当ADCLRC和DACLRC时钟才会关闭 */
};

b) 然后在sai2节点下增加pinctrl属性

&sai2 {
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_sai2 &pinctrl_sai2_hp_det_b>;
	assigned-clocks = <&clks IMX6UL_CLK_SAI2_SEL>, <&clks IMX6UL_CLK_SAI2>;
	assigned-clock-parents = <&clks IMX6UL_CLK_PLL4_AUDIO_DIV>;
	assigned-clock-rates = <0>, <12288000>;

	status = "okay";
};

配置SAI2引脚

pinctrl_sai2: sai2grp {
	fsl,pins = <
		MX6UL_PAD_JTAG_TDI__SAI2_TX_BCLK	0x17088
		MX6UL_PAD_JTAG_TDO__SAI2_TX_SYNC	0x17088
		MX6UL_PAD_JTAG_TRST_B__SAI2_TX_DATA	0x11088
		MX6UL_PAD_JTAG_TCK__SAI2_RX_DATA	0x11088
		MX6UL_PAD_JTAG_TMS__SAI2_MCLK		0x17088
	>;
};

配置耳机检测引脚

pinctrl_sai2_hp_det_b: sai2_hp_det_grp {
	fsl,pins = <
		MX6ULL_PAD_SNVS_TAMPER4__GPIO5_IO04   0x17059
	>;
};

c) 在根节点下创建一个名为“sound"的子节点

	sound {
		compatible = "fsl,imx6ul-evk-wm8960",
			   "fsl,imx-audio-wm8960";	/* 用于匹配相应的驱动文件, 有两个属性值 */
		model = "wm8960-audio"; /* 最终用户看到的声卡名字 */
		cpu-dai = <&sai2>; /* CPU DAI句柄, 这里是sai2这个节点 */
		audio-codec = <&codec>; /* 音频解码芯片句柄, 也就是WM8960芯片, 也就是WM8960芯片 */
		asrc-controller = <&asrc>; /* asrc控制器, 异步采样频率转化器 */
		codec-master;
		gpr = <&gpr 4 0x100000 0x100000>;
		/*
         * hp-det = <hp-det-pin hp-det-polarity>;
		 * hp-det-pin: JD1 JD2  or JD3
		 * hp-det-polarity = 0: hp detect high for headphone
		 * hp-det-polarity = 1: hp detect high for speaker
		 */
		hp-det = <3 0>;	/* 耳机插入检测引脚 */
		/*
		hp-det-gpios = <&gpio5 4 0>;
		mic-det-gpios = <&gpio5 4 0>;
		*/
		audio-routing =
			"Headphone Jack", "HP_L",
			"Headphone Jack", "HP_R",
			"Ext Spk", "SPK_LP",
			"Ext Spk", "SPK_LN",
			"Ext Spk", "SPK_RP",
			"Ext Spk", "SPK_RN",
			"LINPUT2", "Mic Jack",
			"LINPUT3", "Mic Jack",
			"RINPUT1", "Main MIC",
			"RINPUT2", "Main MIC",
			"Mic Jack", "MICB",
			"Main MIC", "MICB",
			"CPU-Playback", "ASRC-Playback",
			"Playback", "CPU-Playback",
			"ASRC-Capture", "CPU-Capture",
			"CPU-Capture", "Capture";	/*  */
	};
编译设备树,并将设备树文件拷贝到NFS目录下

Linux学习笔记(23)——基于wm8960的音频设备驱动_第1张图片
2. 使能内核的WM8960驱动
打开linux内核的图形化配置界面,按下图路径取消OSS混合器 和OSS PCM API.
Linux学习笔记(23)——基于wm8960的音频设备驱动_第2张图片 按路径 ->Device Drivers ->Sound card support ->Advanced linux Sound Architecture ->ALSA for SoC audio support ->SoC Audio for Freescale CPUs 选中ASRC模块和SAI模块。
Linux学习笔记(23)——基于wm8960的音频设备驱动_第3张图片 编译内核,并将zImage文件拷贝到tftpboot目录下, 重新启动开发板,出现以下启动LOG信息:

imx-wm8960 sound: wm8960-hifi <-> 202c000.sai mapping ok
imx-wm8960 sound: snd-soc-dummy-dai <-> 2034000.asrc mapping ok
imx-wm8960 sound: wm8960-hifi <-> 202c000.sai mapping ok
  1. alsa-lib和alsa-utils移植
    3.1 在"nfs/rootfs/usr/"目录下创建"share/arm-alsa/"目录,在源码解压后先进行配置:
./configure --host=arm-linux-gnueabihf --prefix=/home/glen/linux/imx6ull/Tools/alsa-lib --with-configdir=/usr/share/arm-alsa/
"--with-configdir"用于设置alsa-lib编译出来的配置文件存放位置,即前面创建的"/home/glen/linux/nfs/rootfs/usr/share/arm-alsa"目录。

配置完成以后就可以编译了然后安装
sudo -s
source /etc/profile
make install
sudo glen
编译完成前面创建的"alsa-lib"目录就会保存相应的编译结果

在这里插入图片描述
把lib目录下的所有文件拷贝到开发板根文系统的/usr/lib目录下

cd alsa-lib //进入 alsa-lib
sudo cp lib/* /home/glen/linux/nfs/rootfs/lib/ -af
cd /usr/share/arm-alsa //进入 arm-alsa 目录,拷贝配置文件
sudo cp * /home/glen/linux/nfs/rootfs/usr/share/arm-alsa/ -raf

3.2 在"alsa-utils-1.2.2.tar.bz2"源码解压后先进行配置:

./configure --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc --disable-alsamixer --disable-xmlto CPPFLAGS=-I/usr/share/arm-alsa/include LDFLAGS=-L/usr/share/arm-alsa/lib --with-alsa-prefix=/usr/share/arm-alsa/lib --with-alsa-prefix=/usr/share/arm-alsa/lib --with-alsa-inc-prefix=/usr/share/arm-alsa/include --prefix=/usr/share/arm-alsa/alsa_utils
然后编译安装,并把 "alsa-utils/bin/"、 "alsa-utils/sbin/"、 "alsa-utils/share/"拷贝到根文件系统当中。这里不再展示。
  1. 修改内核里面wm8960.c文件
/*
 * wm8960 register cache
 * We can't read the WM8960 register space when we are
 * using 2 wire for device control, so we cache them instead.
 */
static const struct reg_default wm8960_reg_defaults[] = {
	{  0x0, 0x00a7 },
	{  0x1, 0x00a7 },
	{  0x2, 0x0000 },
	{  0x3, 0x0000 },
	{  0x4, 0x0000 },
	{  0x5, 0x0008 },
	{  0x6, 0x0000 },
	{  0x7, 0x000a },
	{  0x8, 0x01c0 },
	{  0x9, 0x0000 },
	{  0xa, 0x00ff },
	{  0xb, 0x00ff },

	{ 0x10, 0x0000 },
	{ 0x11, 0x007b },
	{ 0x12, 0x0100 },
	{ 0x13, 0x0032 },
	{ 0x14, 0x0000 },
	{ 0x15, 0x00c3 },
	{ 0x16, 0x00c3 },
	{ 0x17, 0x01c4/* 0x01c0 */},
	{ 0x18, 0x0000 },
	{ 0x19, 0x0000 },
	{ 0x1a, 0x0000 },
	{ 0x1b, 0x0000 },
	{ 0x1c, 0x0000 },
	{ 0x1d, 0x0000 },

	{ 0x20, 0x0100 },
	{ 0x21, 0x0100 },
	{ 0x22, 0x0050 },

	{ 0x25, 0x0050 },
	{ 0x26, 0x0000 },
	{ 0x27, 0x0000 },
	{ 0x28, 0x0000 },
	{ 0x29, 0x0000 },
	{ 0x2a, 0x0040 },
	{ 0x2b, 0x0000 },
	{ 0x2c, 0x0000 },
	{ 0x2d, 0x0050 },
	{ 0x2e, 0x0050 },
	{ 0x2f, 0x0000 },
	{ 0x30, 0x0002 },
	{ 0x31, 0x0037 },

	{ 0x33, 0x0080 },
	{ 0x34, 0x0008 },
	{ 0x35, 0x0031 },
	{ 0x36, 0x0026 },
	{ 0x37, 0x00e9 },
};
  1. mplayer播放器移植与使用
    5.1 移植zlib库
CC=arm-linux-gnueabihf-gcc 
LD=arm-linux-gnueabihf-ld 
AD=arm-linux-gnueabihfas 
./configure --prefix=/home/glen/linux/Tools/zlib
make
make install
在"/home/glen/linux/Tools/zlib"目录下产生以下几个文件

在这里插入图片描述 将"zlib/lib"目录下文件拷贝到根文件系统/lib目录下

sudo cp lib/* /home/glen/linux/nfs/rootfs/lib/ -rfa

5.2 移植mplayer

./configure --cc=arm-linux-gnueabihf-gcc --host-cc=gcc --target=arm-linux-gnueabihf --disable-ossaudio --enable-alsa --prefix=/home/glen/linux/Tools/MPlayer --extra-cflags=-I/home     /glen/linux/Tools/zlib/include -I/home/glen/linux/nfs/rootfs/usr/share/arm-alsa/include --extra-ldflags=-L/home/glen/linux/Tools/zlib/lib -Iz -L/usr/share/arm-alsa/lib/ -lasound --en     able-fbdev --disable-mencoder
make
--extra-cflags 指定 zlib 和 alsa-lib 的头文件路径, --extra-ldflags 指定 zlib 和 alsa-lib 的库文件路径.
编译完成以后打开 config.mak 文件,找到“INSTALLSTRIP = -s”这一行,取消掉后面

的"-s", 然后使用"make install"安装,产生以下几个目录文件
在这里插入图片描述
将其拷贝到根文件系统的/bin目录下

sudo cp bin/mplayer /home/glen/linux/nfs/rootfs/bin/ -f
  1. mplayer使用
    启动开发板,在控制台执行以下命令,产生如下播放细节:
/music # mplayer 10.flv
MPlayer 1.4-4.9.4 (C) 2000-2019 MPlayer Team

Playing 10.flv.
libavformat version 58.27.102 (internal)
libavformat file format detected.
[lavf] stream 0: video (h264), -vid 0
[lavf] stream 1: audio (aac), -aid 0
VIDEO:  [H264]  960x720  0bpp  8.000 fps  206.5 kbps (25.2 kbyte/s)
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 58.51.100 (internal)
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
Clip info:
 description: Codec by Bilibili XCode Worker v4.8.33(fixed_gap:False)
 metadatacreator: Version 1.9
 hasKeyframes: true
 hasVideo: true
 hasAudio: true
 hasMetadata: true
 canSeekToEnd: true
 datasize: 3134250
 videosize: 1874450
 audiosize: 1244984
 lasttimestamp: 72
 lastkeyframetimestamp: 72
 lastkeyframelocation: 3135198
Load subtitles in ./
==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 44100 Hz, 2 ch, floatle, 132.9 kbit/4.71% (ratio: 16607->352800)
Selected audio codec: [ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))
==========================================================================
AO: [alsa] 44100Hz 2ch floatle (4 bytes per sample)
Starting playback...
Could not find matching colorspace - retrying with -vf scale...
Opening video filter: [scale]
Movie-Aspect is 1.33:1 - prescaling to correct movie aspect.
[swscaler @ 0xe42fb0]bicubic scaler, from yuv420p to bgra using C
[swscaler @ 0xe42fb0]No accelerated colorspace conversion found from yuv420p to bgra.
[swscaler @ 0xe42fb0]using unscaled yuv420p -> bgra special converter
VO: [fbdev] 960x720 => 960x720 BGRA
A:  36.5 V:  36.5 A-V:  0.001 ct:  0.209   0/  0 42% 25%  4.0% 5 0


MPlayer interrupted by signal 2 in module: decode video
A:  36.6 V:  36.6 A-V: -0.008 ct:  0.208   0/  0 42% 25%  4.0% 5 0
下面是mplayer视频播放显示内容:

你可能感兴趣的:(Linux)