linux3.4.2之ALSA声卡驱动

目录

一 ALSA框架分析

二  ALSA创建声卡流程

三 ASOC架构分析

四  配置使用UDA1341

五 从0写wm8976的ALSA驱动

六 使用trace跟踪函数调用过程

七 声卡测试

参考:


一 ALSA框架分析

1.打开sound/core/sound.c文件,定位到alsa_sound_init函数,发现调用了register_chrdev函数,所以音频驱动属于字符设备驱动。进一步可以发现file_operations结构体是从snd_minors数组中取出。

static int snd_open(struct inode *inode, struct file *file)
{
	mptr = snd_minors[minor];
	file->f_op = fops_get(mptr->f_ops);
}

static const struct file_operations snd_fops =
{
	.open =		snd_open,
};

static int __init alsa_sound_init(void)
{
	...
	register_chrdev(major, "alsa", &snd_fops);
    ...
	snd_info_minor_register();
    ...
}

2.进一步搜索snd_minors,可发现该数组通过snd_register_device_for_dev进行初始化.

int snd_register_device_for_dev(int type, struct snd_card *card, int dev,
				const struct file_operations *f_ops,
				void *private_data,
				const char *name, struct device *device)
{
    snd_minors[minor] = preg;
    preg->dev = device_create(sound_class, device, MKDEV(major, minor),
				  private_data, "%s", name);
}

3. 通过一步步搜索可以确定如下函数调用关系

->snd_card_create
  ->snd_ctl_create
    ->snd_ctl_dev_register
      ->snd_register_device
        ->snd_register_device_for_dev

->snd_pcm_new
   ->_snd_pcm_new
      ->snd_pcm_dev_register
        ->snd_register_device_for_dev

4. 通过ls /dev/snd 可以查看alsa驱动的设备文件结构

➜  linux ls /dev/snd 
by-path  controlC0  controlC1  controlC29  hwC0D0  hwC1D0  pcmC0D3p  pcmC1D0c  pcmC1D0p  seq  timer

其中,C0D0代表的是声卡0中的设备0,pcmC0D0c最后一个c代表capture,pcmC0D0p最后一个p代表playback,
这些都是alsa-driver中的命名规则
controlC0 -->   用于声卡的控制,例如通道选择,混音,麦克风的控制等
pcmC0D0c -->    用于录音的pcm设备
pcmC0D0p -->    用于播放的pcm设备
seq  -->        音序器
timer -->       定时器

二  ALSA创建声卡流程

1.创建snd_card

struct snd_card *card;
/*
index :该声卡的编号
id    :声卡的标识符
drvdata :私有数据的大小
card  :snd_card实例的指针
*/
snd_card_create(index, id, THIS_MODULE, 0, &card);

2.设置声卡

3.创建声卡的功能部件(逻辑设备),例如PCM,Mixer,MIDI等

PCM     --   snd_pcm_new()
RAWMIDI --   snd_rawmidi_new()
CONTROL --   snd_ctl_create()
TIMER   --   snd_timer_new()
INFO    --   snd_card_proc_new()
JACK    --   snd_jack_new()

4.注册声卡

snd_card_register(card);

三 ASOC架构分析

ASOC是ALSA System On Chip的缩写。包含Machine(某款开发板)、Platform(芯片型号)、Codec(编解码)三部分。

下面开始分析mini2440中的UDA134X音频驱动:

1.打开sound/soc/samsung/s3c24xx_uda134x.c,该部分为ASOC中的machine部分。
此处定义了一个名为“s3c24xx_uda134x”的平台驱动,并在probe函数中定义了一个名为“soc-audio”的平台设备。

static int s3c24xx_uda134x_probe(struct platform_device *pdev)
{
    //分配,添加"soc-audio"平台设备
    s3c24xx_uda134x_snd_device = platform_device_alloc("soc-audio", -1);
    platform_device_add(s3c24xx_uda134x_snd_device);
}

static struct platform_driver s3c24xx_uda134x_driver = {
	.probe  = s3c24xx_uda134x_probe,
	.remove = s3c24xx_uda134x_remove,
	.driver = {
		.name = "s3c24xx_uda134x",
		.owner = THIS_MODULE,
	},
};

2.打开arch/arm/mach-s3c24xx/mach-mini2440.c,该部分定义了名为“s3c24xx_uda134x”的平台设备

static struct platform_device mini2440_audio = {
	.name		= "s3c24xx_uda134x",
	.id		= 0,
	.dev		= {
		.platform_data	= &mini2440_audio_pins,
	},
};

3.打开sound/soc/soc-core.c,该处定义了名为“soc-audio”的平台驱动,且在probe函数注册snd_soc_card结构体。

static int soc_probe(struct platform_device *pdev)
{
    struct snd_soc_card *card = platform_get_drvdata(pdev);
    ret = snd_soc_register_card(card);
}
/* ASoC platform driver */
static struct platform_driver soc_driver = {
	.driver		= {
		.name		= "soc-audio",
		.owner		= THIS_MODULE,
		.pm		= &snd_soc_pm_ops,
	},
	.probe		= soc_probe,
	.remove		= soc_remove,
};

4.snd_soc_card结构体为machine部分的核心结构体,其定义在sound/soc/samsung/s3c24xx_uda134x.c,并通过snd_soc_dai_link结构体指定DAI(Digital Audio Interface)接口,且通过它将platform与codec接口关联起来。

static struct snd_soc_ops s3c24xx_uda134x_ops = {
	.startup = s3c24xx_uda134x_startup,
	.shutdown = s3c24xx_uda134x_shutdown,
	.hw_params = s3c24xx_uda134x_hw_params,
};
static struct snd_soc_dai_link s3c24xx_uda134x_dai_link = {
	.name = "UDA134X",
	.stream_name = "UDA134X",
	.codec_name = "uda134x-codec",    //编码名
	.codec_dai_name = "uda134x-hifi", //codec的DAI接口使用hifi
	.cpu_dai_name = "s3c24xx-iis",    //CPU的DAI接口使用IIS
	.ops = &s3c24xx_uda134x_ops,
	.platform_name	= "samsung-audio",//指定使用哪个DMA
};
static struct snd_soc_card snd_soc_s3c24xx_uda134x = {
	.name = "S3C24XX_UDA134X",
	.owner = THIS_MODULE,
	.dai_link = &s3c24xx_uda134x_dai_link,
	.num_links = 1,
};

5.在sound/soc/samsung/s3c24xx-i2s.c定义了名为"s3c24xx-iis"的平台驱动,且在probe函数中注册了DAI,其中snd_soc_dai_ops结构体主要是用于DIA接口

static const struct snd_soc_dai_ops s3c24xx_i2s_dai_ops = {
	.trigger	= s3c24xx_i2s_trigger,  //设置触发方式
	.hw_params	= s3c24xx_i2s_hw_params,//设置硬件参数
	.set_fmt	= s3c24xx_i2s_set_fmt,  //设置格式
	.set_clkdiv	= s3c24xx_i2s_set_clkdiv,//设置分频系数
	.set_sysclk	= s3c24xx_i2s_set_sysclk,//设置系统始终
};
static struct snd_soc_dai_driver s3c24xx_i2s_dai = {
	.probe = s3c24xx_i2s_probe,
	.suspend = s3c24xx_i2s_suspend,
	.resume = s3c24xx_i2s_resume,
	.playback = {         //控制播放
		.channels_min = 2,
		.channels_max = 2,
		.rates = S3C24XX_I2S_RATES,
		.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
	.capture = {        //控制录音
		.channels_min = 2,
		.channels_max = 2,
		.rates = S3C24XX_I2S_RATES,
		.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
	.ops = &s3c24xx_i2s_dai_ops,
};
static __devinit int s3c24xx_iis_dev_probe(struct platform_device *pdev)
{
	return snd_soc_register_dai(&pdev->dev, &s3c24xx_i2s_dai);
}
static struct platform_driver s3c24xx_iis_driver = {
	.probe  = s3c24xx_iis_dev_probe,
	.remove = __devexit_p(s3c24xx_iis_dev_remove),
	.driver = {
		.name = "s3c24xx-iis",
		.owner = THIS_MODULE,
	},
};

6.打开arch/arm/plat-samsung/devs.c,这里定义了名为”samsung-audio“的平台设备
   打开sound/soc/samsung/dma.c,这里定义了名为”samsung-audio“的平台驱动,其中snd_soc_platform_driver为主要结构体。

static struct snd_soc_platform_driver samsung_asoc_platform = {
	.ops		= &dma_ops,
	.pcm_new	= dma_new,
	.pcm_free	= dma_free_dma_buffers,
};
static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev)
{
	return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform);
}
static struct platform_driver asoc_dma_driver = {
	.driver = {
		.name = "samsung-audio",
		.owner = THIS_MODULE,
	},

	.probe = samsung_asoc_platform_probe,
	.remove = __devexit_p(samsung_asoc_platform_remove),
};
---------------------------------------------------------
struct platform_device samsung_asoc_dma = {
	.name		= "samsung-audio",
	.id		= -1,
	.dev		= {
		.dma_mask		= &samsung_device_dma_mask,
		.coherent_dma_mask	= DMA_BIT_MASK(32),
	}
};

7.打开sound/soc/codecs/uda134x.c,这里定义了名为"uda134x-codec"的平台驱动。
   打开arch/arm/mach-s3c24xx/mach-mini2440.c,这里定义了名为uda134x-codec"的平台设备


static const struct snd_soc_dai_ops uda134x_dai_ops = {
	.startup	= uda134x_startup,         //开始
	.shutdown	= uda134x_shutdown,        //停止
	.hw_params	= uda134x_hw_params,       //设置硬件参数
	.digital_mute	= uda134x_mute,
	.set_sysclk	= uda134x_set_dai_sysclk, //设置系统时钟
	.set_fmt	= uda134x_set_dai_fmt,    //设置格式
};
static struct snd_soc_dai_driver uda134x_dai = {
	.name = "uda134x-hifi",
	/* playback capabilities */
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 2,
		.rates = UDA134X_RATES,
		.formats = UDA134X_FORMATS,
	},
	/* capture capabilities */
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = UDA134X_RATES,
		.formats = UDA134X_FORMATS,
	},
	/* pcm operations */
	.ops = &uda134x_dai_ops,
};
static struct snd_soc_codec_driver soc_codec_dev_uda134x = {//用于读写寄存器
	.probe =        uda134x_soc_probe,
	.remove =       uda134x_soc_remove,
	.suspend =      uda134x_soc_suspend,
	.resume =       uda134x_soc_resume,
	.reg_cache_size = sizeof(uda134x_reg),
	.reg_word_size = sizeof(u8),
	.reg_cache_default = uda134x_reg,
	.reg_cache_step = 1,
	.read = uda134x_read_reg_cache,
	.write = uda134x_write,
	.set_bias_level = uda134x_set_bias_level,
};
static int __devinit uda134x_codec_probe(struct platform_device *pdev)
{
	return snd_soc_register_codec(&pdev->dev,&soc_codec_dev_uda134x, &uda134x_dai, 1);
}
static struct platform_driver uda134x_codec_driver = {
	.driver = {
		.name = "uda134x-codec",
		.owner = THIS_MODULE,
	},
	.probe = uda134x_codec_probe,
	.remove = __devexit_p(uda134x_codec_remove),
};
--------------------------------------------------------------------------------
static struct platform_device uda1340_codec = {
		.name = "uda134x-codec",
		.id = -1,
};

四  配置使用UDA1341

1. 配置内核支持uda1341

-> Device Drivers
   -> Sound card support
      -> Advanced Linux Sound Architecture
         -> ALSA for SoC audio support
            <*>   ASoC support for Samsung
            <*>   SoC I2S Audio support WM8976 wired to a S3C24XX

-> System Type
   [*] S3C2410 DMA support

2. 修改mach-smdk2440.c

添加如下代码

#include 
static struct s3c24xx_uda134x_platform_data smdk2440_audio_pins = {
	.l3_clk = S3C2410_GPB(4),
	.l3_mode = S3C2410_GPB(2),
	.l3_data = S3C2410_GPB(3),
	.model = UDA134X_UDA1341
};
static struct platform_device smdk2440_audio = {
	.name		= "s3c24xx_uda134x",
	.id		= 0,
	.dev		= {
		.platform_data	= &smdk2440_audio_pins,
	},
};
static struct platform_device uda1340_codec = {
		.name = "uda134x-codec",
		.id = -1,
};
static struct platform_device *smdk2440_devices[] __initdata = {
	&smdk2440_audio,
	&uda1340_codec,
	&s3c_device_iis,
	&samsung_asoc_dma,
};

3. 修改bug,修改sound/soc/samsung/dma.c文件中的dma_enqueue函数

将     dma_info.len = prtd->dma_period * limit;
修改为:dma_info.len = prtd->dma_period;

4.交叉编译alsa.lib,alsa.util使用声卡

  • 解压alsa-lib-1.0.27.2文件后,以root身份执行以下命令
./configure --host=arm-linux --prefix=/usr/local/arm-alsa  --disable-python

make & make install
  • 解压alsa-utils-1.0.27.2文件后,以root身份执行以下命令
./configure --host=arm-linux --prefix=/usr/local/arm-alsa --with-alsa-inc-prefix=/usr/local/arm-alsa/include --with-alsa-prefix=/usr/local/arm-alsa/lib --disable-alsamixer --disable-xmlto

touch alsaconf/po/t-ja.gmo

make & make install
  • 移植alsa-lib与alsa-util到开发板
sudo cp -rfa /usr/local/arm-alsa/lib/*  $rootfs/lib/

sudo cp -rfa /usr/local/arm-alsa/bin/*  $rootfs/sbin/

sudo cp -rfa /usr/local/arm-alsa/sbin/*  $rootfs/sbin/

sudo cp -rfa /usr/local/arm-alsa/share/alsa/*  $rootfs/usr/share/arm-alsa/share/alsa/
  • 在开发板执行以下命令
#播放音乐
aplay xxx.wav
#调音量
amixer controls
amixer cget numid=1
amixer cset numid=1 30

五 从0写wm8976的ALSA驱动

1.框架编写

创建如下图所示的文件夹与文件

linux3.4.2之ALSA声卡驱动_第1张图片

2.wm8976.c源码

/*参考:sound/soc/codecs/uda134x.c
1.构造 snd_soc_dai_driver 结构体
2.构造 snd_soc_codec_driver 结构体
3.注册它们
*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

/* WM8976 registers */

#define WM8976_REG_NUM  58

#define UDA1341_L3ADDR	5
#define UDA1341_DATA0_ADDR	((UDA1341_L3ADDR << 2) | 0)
#define UDA1341_DATA1_ADDR	((UDA1341_L3ADDR << 2) | 1)
#define UDA1341_STATUS_ADDR	((UDA1341_L3ADDR << 2) | 2)

#define UDA1341_EXTADDR_PREFIX	0xC0
#define UDA1341_EXTDATA_PREFIX	0xE0

#define WM8976_RATES SNDRV_PCM_RATE_8000_48000
#define WM8976_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
	        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)

/* 所有寄存器的默认值 :7bit地址,9bit数据*/
static const short wm8976_reg[WM8976_REG_NUM]={};


static volatile unsigned int *gpbdat;
static volatile unsigned int *gpbcon;

static void set_csb(int val)
{
    if (val){
        *gpbdat |= (1<<2);
    }else{
        *gpbdat &= ~(1<<2);
    }
}

static void set_clk(int val)
{
    if (val){
        *gpbdat |= (1<<4);
    }else{
        *gpbdat &= ~(1<<4);
    }
}

static void set_dat(int val)
{
    if (val){
        *gpbdat |= (1<<3);
    }else{
        *gpbdat &= ~(1<<3);
    }
}


/*
 * The codec has no support for reading its registers except for peak level...
 */
static inline unsigned int wm8976_read_reg_cache(struct snd_soc_codec *codec,unsigned int reg)
{
	u8 *cache = codec->reg_cache;

	if (reg >= WM8976_REG_NUM)
		return -1;
	return cache[reg];
}
/*
 * Write to the uda134x registers
 *
 */
static int wm8976_write_reg(struct snd_soc_codec *codec, unsigned int reg, unsigned int value)
{
	u8 *cache = codec->reg_cache;
	int i;
	unsigned short val = (reg << 9) | (value & 0x1ff);

	//先保存
	if(reg >= WM8976_REG_NUM){
		return -1;
	}
	cache[reg] = value;
	/* 再写入硬件 */
    set_csb(1);
    set_dat(1);
    set_clk(1);

	for (i = 0; i < 16; i++){
		if (val & (1<<15)){
            set_clk(0);
            set_dat(1);
			udelay(1);
            set_clk(1);
		}else{
            set_clk(0);
            set_dat(0);
			udelay(1);
            set_clk(1);
		}

		val = val << 1;
	}

    set_csb(0);
	udelay(1);
    set_csb(1);
    set_dat(1);
    set_clk(1);

	return 0;
}

void wm8976_init_regs(struct snd_soc_codec *codec)
{
	*gpbcon &= ~((3<<4)|(3<<6)|(3<<8));
	*gpbcon |= (1<<4)|(1<<6)|(1<<8);

	/* software reset */
	wm8976_write_reg(codec, 0, 0);

	/* OUT2的左/右声道打开
	 * 左/右通道输出混音打开
	 * 左/右DAC打开
	 */
	wm8976_write_reg(codec, 0x3, 0x6f);
	
	wm8976_write_reg(codec, 0x1, 0x1f);//biasen,BUFIOEN.VMIDSEL=11b  
	wm8976_write_reg(codec, 0x2, 0x185);//ROUT1EN LOUT1EN, inpu PGA enable ,ADC enable

	wm8976_write_reg(codec, 0x6, 0x0);//SYSCLK=MCLK  
	wm8976_write_reg(codec, 0x4, 0x10);//16bit 		
	wm8976_write_reg(codec, 0x2B,0x10);//BTL OUTPUT  
	wm8976_write_reg(codec, 0x9, 0x50);//Jack detect enable  
	wm8976_write_reg(codec, 0xD, 0x21);//Jack detect  
	wm8976_write_reg(codec, 0x7, 0x01);//Jack detect 
}

//获得音量信息,比如最小值,最大值
int wm8976_info_volsw(struct snd_kcontrol *kcontrol,struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 63;
	return 0;
}

//获得当前音量值
int wm8976_get_volsw(struct snd_kcontrol *kcontrol,struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);

	ucontrol->value.integer.value[1] = snd_soc_read(codec, 53) & 0x3f;
	ucontrol->value.integer.value[0] = snd_soc_read(codec, 52) & 0x3f;

	return 0;
}

//设置音量
int wm8976_put_volsw(struct snd_kcontrol *kcontrol,struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	unsigned int val;
	
	val = ucontrol->value.integer.value[0];
	snd_soc_write(codec, 52, (1<<8)|val);

	val = ucontrol->value.integer.value[1];
    snd_soc_write(codec, 53, (1<<8)|val);

	return 0;
}

static const struct snd_kcontrol_new wm8976_vol_controls = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, 
	.name = "Master Playback Volume",
	.info = wm8976_info_volsw, 
	.get = wm8976_get_volsw,
	.put = wm8976_put_volsw,
};

static int wm8976_soc_probe(struct snd_soc_codec *codec)
{
	snd_soc_add_codec_controls(codec, &wm8976_vol_controls, 1);
	wm8976_init_regs(codec);
	return 0;
}

static struct snd_soc_codec_driver soc_codec_dev_wm8976 = {
	.probe =        wm8976_soc_probe,
	//寄存器不支持读操作,所以用一段缓存保存寄存器值,每次写寄存器的同时写该缓存。
	.reg_cache_size = sizeof(wm8976_reg),//保存寄存器的缓存有多大
	.reg_word_size = sizeof(u16),    //每个寄存器长度为2字节
	.reg_cache_default = wm8976_reg,//默认值保存在哪里
	.reg_cache_step = 2,
	.read = wm8976_read_reg_cache,//读寄存器
	.write = wm8976_write_reg,        //写寄存器
};

static int wm8976_hw_params(struct snd_pcm_substream *substream,
                             struct snd_pcm_hw_params *params,
							 struct snd_soc_dai *dai)
{
	//根据para设置wm8976的寄存器

	return 0;
}

static const struct snd_soc_dai_ops wm8976_dai_ops = {
	.hw_params	= wm8976_hw_params,
};
static struct snd_soc_dai_driver wm8976_dai = {
	.name = "wm8976-iis",
	/* playback capabilities */
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 2,
		.rates = WM8976_RATES,//采样率
		.formats = WM8976_FORMATS,//格式
	},
	/* capture capabilities */
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = WM8976_RATES,
		.formats = WM8976_FORMATS,
	},
	/* pcm operations */
	.ops = &wm8976_dai_ops,
};

static int wm8976_probe(struct platform_device * pdev)
{
	return snd_soc_register_codec(&pdev->dev,&soc_codec_dev_wm8976, &wm8976_dai, 1);
}

int wm8976_remove(struct platform_device *pdev)
{
    snd_soc_unregister_codec(&pdev->dev);
	return 0;
}

static void wm8976_dev_release (struct device *dev)
{

}

static struct platform_device wm8976_device = {
	.name = "wm8976-codec",
	.id = -1,
	.dev = {
		.release = wm8976_dev_release,
	},
};

struct platform_driver wm8976_drv = {
	.probe = wm8976_probe,
	.remove = wm8976_remove,
	.driver = {
		.name = "wm8976-codec",//一定需要同名,才会调用probe函数
	},
};


static int __init wm8976_init(void)
{
	gpbcon = ioremap(0x56000010,4);
	gpbdat = ioremap(0x56000014,4);
    platform_device_register(&wm8976_device);
    platform_driver_register(&wm8976_drv);
    return 0;
}

static void wm8976_exit(void)
{
	platform_device_unregister(&wm8976_device);
    platform_driver_unregister(&wm8976_drv);
	iounmap(gpbcon);
	iounmap(gpbdat);
}

module_init(wm8976_init);
mudule_exit(wm8976_exit);
MODULE_LICENSE("GPL");

2.s3c2440_wm8976.c源码

/*参考:sound/soc/samsung/s3c24xx_uda134x.c
1.分配注册一个名为“soc-audio”的平台设备
2.给该平台设备添加一个snd_soc_card的私有数据,snd_soc_card中有一个snd_soc_dai_link,
  snd_soc_dai_link来决定ASOC的各部分驱动
*/
#include 
#include 
#include 
#include 
#include 

static void s3c24xx_wm8976_release (struct device *dev)
{

}
static struct platform_device s3c24xx_wm8976_snd_device = {
	.name = "soc-audio",
	.id = -1,
	.dev = {
		.release = s3c24xx_wm8976_release,
	}
};

static struct snd_soc_ops s3c24xx_wm8976_ops = {

};

static struct snd_soc_dai_link s3c24xx_wm8976_dai_link = {
	.name = "100ask_wm8976",
	.stream_name = "100ask_wm8976",
	.codec_name = "wm8976-codec",
	.codec_dai_name = "wm8976-iis",
	.cpu_dai_name = "s3c2440-iis",
	.ops = &s3c24xx_wm8976_ops,
	.platform_name	= "s3c2440-dma",
};

static struct snd_soc_card snd_soc_s3c24xx_wm8976 = {
	.name = "S3C24XX_WM8976",
	.owner = THIS_MODULE,
	.dai_link = &s3c24xx_wm8976_dai_link,
	.num_links = 1,
};

static int __init s3c2440_wm8976_init(void)
{
    platform_set_drvdata(&s3c24xx_wm8976_snd_device, &snd_soc_s3c24xx_wm8976);//设置私有数据
  	platform_device_register(&s3c24xx_wm8976_snd_device);
    return 0;
}

static void __exit s3c2440_wm8976_exit(void)
{
	platform_device_unregister(&s3c24xx_wm8976_snd_device);
}

module_init(s3c2440_wm8976_init);
mudule_exit(s3c2440_wm8976_exit);
MODULE_LICENSE("GPL");

3.s3c2440_iis.c源码

/*参考:sound/soc/samsung/s3c24xx-i2s.c
1.构造 snd_soc_dai_driver 结构体
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ABS(a, b) ((a>b)?(a-b):(b-a))

struct s3c2440_iis_regs{
	unsigned int iiscon;
	unsigned int iismod;
	unsigned int iispsr;
	unsigned int iisfcon;
	unsigned int iisfifo;
};
static volatile struct s3c2440_iis_regs *iis_regs;
static volatile unsigned int *gpecon;

static void s3c2440_iis_start(void)
{
	iis_regs->iiscon |= 1;
}

static void s3c2440_iis_stop(void)
{
	iis_regs->iiscon &= ~1;
}

static int s3c2440_i2s_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params,
				 struct snd_soc_dai *dai)
{
	//根据para设置iis控制器
	int tmp_fs;
    int i;
    int min = 0xffff;
    int pre = 0;
	unsigned int fs;
	struct clk *clk = clk_get(NULL,"pclk");
    /* 配置GPIO用于IIS */
    *gpecon &= ~((3<<0) | (3<<2) | (3<<4) | (3<<6) | (3<<8));
    *gpecon |= ((2<<0) | (2<<2) | (2<<4) | (2<<6) | (2<<8));
    

    if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE)
        iis_regs->iismod = S3C2410_IISMOD_TXMODE | S3C2410_IISMOD_IIS |
		                   S3C2410_IISMOD_16BIT | S3C2410_IISMOD_384FS | S3C2410_IISMOD_32FS;
    else if(params_format(params) == SNDRV_PCM_FORMAT_S8)
        iis_regs->iismod = S3C2410_IISMOD_TXMODE | S3C2410_IISMOD_IIS |
		                   S3C2410_IISMOD_8BIT | S3C2410_IISMOD_384FS | S3C2410_IISMOD_32FS;
	else
	    return -EINVAL;
    /* Master clock = PCLK/(n+1)
     * fs = Master clock / 384
     * fs = PCLK / (n+1) / 384 */
	fs = params_rate(params);
    for (i = 0; i <= 31; i++)
    {
        tmp_fs = clk_get_rate(clk)/384/(i+1);
        if (ABS(tmp_fs, fs) < min)
        {
            min = ABS(tmp_fs, fs);
            pre = i;
        }
    }
    iis_regs->iispsr = (pre << 5) | (pre);

    /*
     * bit15 : Transmit FIFO access mode select, 1-DMA
     * bit13 : Transmit FIFO, 1-enable
     */
    iis_regs->iisfcon = (1<<15) | (1<<13);
    
    /*
     * bit[5] : Transmit DMA service request, 1-enable
     * bit[1] : IIS prescaler, 1-enable
     */
    iis_regs->iiscon = (1<<5) | (1<<1) ;
	clk_put(clk);
	return 0;
}


static int s3c2440_i2s_trigger(struct snd_pcm_substream *substream, int cmd,struct snd_soc_dai *dai)
{
	int ret = 0;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		s3c2440_iis_start();//开始传输
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		s3c2440_iis_stop();//停止
		break;
	default:
		s3c2440_iis_stop();//停止
		ret = -EINVAL;
		break;
	}
	return ret;
}

static const struct snd_soc_dai_ops s3c2440_i2s_dai_ops = {
	.hw_params	= s3c2440_i2s_hw_params,
	.trigger    = s3c2440_i2s_trigger,
};

#define S3C24XX_I2S_RATES \
	(SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
	SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
	SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)

static struct snd_soc_dai_driver s3c24xx_i2s_dai = {
	.playback = {
		.channels_min = 2,
		.channels_max = 2,
		.rates = S3C24XX_I2S_RATES,
		.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
	.capture = {
		.channels_min = 2,
		.channels_max = 2,
		.rates = S3C24XX_I2S_RATES,
		.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
	.ops = &s3c2440_i2s_dai_ops,
};

static void s3c2440_iis_dev_release (struct device *dev)
{

}
static struct platform_device s3c2440_iis_device = {
	.name = "s3c2440-iis",
	.id = -1,
	.dev = {
		.release = s3c2440_iis_dev_release,
	},
};

static int s3c2440_iis_probe(struct platform_device * pdev)
{
    return snd_soc_register_dai(&pdev->dev, &s3c24xx_i2s_dai);
}

int s3c2440_iis_remove(struct platform_device *pdev)
{
    snd_soc_unregister_dai(&pdev->dev);
	return 0;
}

struct platform_driver s3c2440_iis_drv = {
	.probe = s3c2440_iis_probe,
	.remove = s3c2440_iis_remove,
	.driver = {.name = "s3c2440-iis",}//一定需要同名,才会调用probe函数
};

static int s3c2440_iis_init(void)
{
	struct clk *clk;
	clk = clk_get(NULL,"iis");
	clk_enable(clk);
	clk_put(clk);

	gpecon = ioremap(0x56000040,4);
	iis_regs = ioremap(0x55000000, sizeof(struct s3c2440_iis_regs));
    platform_device_register(&s3c2440_iis_device);//注册平台设备
    platform_driver_register(&s3c2440_iis_drv);//注册平台驱动
    return 0;
}

static void s3c2440_iis_exit(void)
{
	struct clk *clk;
	clk = clk_get(NULL,"iis");
	clk_disable(clk);
	clk_put(clk);

	iounmap(gpecon);
	iounmap(iis_regs);
	platform_device_unregister(&s3c2440_iis_device);
    platform_driver_unregister(&s3c2440_iis_drv);
}

module_init(s3c2440_iis_init);
mudule_exit(s3c2440_iis_exit);
MODULE_LICENSE("GPL");

5.s3c2440_dma.c源码

/*参考:sound/soc/samsung/dma.c
1.分配DMA BUFFER
2.从BUFFER里取出period
3.启动DMA传输
4.传输完毕,更新状态
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct s3c_dma_regs {
	unsigned long disrc;
	unsigned long disrcc;
	unsigned long didst;
	unsigned long didstc;
	unsigned long dcon;
	unsigned long dstat;
	unsigned long dcsrc;
	unsigned long dcdst;
	unsigned long dmasktrig;
};
static volatile struct s3c_dma_regs *dma_regs;

static const struct snd_pcm_hardware s3c2440_dma_hardware = {
	.info			= SNDRV_PCM_INFO_INTERLEAVED |
				    SNDRV_PCM_INFO_BLOCK_TRANSFER |
				    SNDRV_PCM_INFO_MMAP |
				    SNDRV_PCM_INFO_MMAP_VALID |
				    SNDRV_PCM_INFO_PAUSE |
				    SNDRV_PCM_INFO_RESUME,
	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
				    SNDRV_PCM_FMTBIT_U16_LE |
				    SNDRV_PCM_FMTBIT_U8 |
				    SNDRV_PCM_FMTBIT_S8,
	.channels_min		= 2,
	.channels_max		= 2,
	.buffer_bytes_max	= 128*1024,
	.period_bytes_min	= PAGE_SIZE,
	.period_bytes_max	= PAGE_SIZE*2,
	.periods_min		= 2,
	.periods_max		= 128,
	.fifo_size		= 32,
};

struct s3c2440_dma_info{
	unsigned int buf_max_size;
	unsigned int period_size;
	unsigned int buffer_size;
	unsigned int phy_addr;
	unsigned int virt_addr;
	unsigned int addr_ofs;
	unsigned int be_running;
};
static struct s3c2440_dma_info dma_info;


/* 数据传输: 源,目的,长度 */
static void load_dma_period(void)
{
	/* 把源,目的,长度告诉DMA */
	dma_regs->disrc      = dma_info.phy_addr + dma_info.addr_ofs;/* 源的物理地址 */
	dma_regs->disrcc     = (0<<1) | (0<<0); /* 源位于AHB总线, 源地址递增 */
	dma_regs->didst      = 0x55000010;        /* 目的的物理地址 */
	dma_regs->didstc     = (0<<2) | (1<<1) | (1<<0); /* 目的位于APB总线, 目的地址不变 */
	dma_regs->dcon       = (1<<31)|(0<<30)|(1<<29)|(0<<28)|(0<<27)|(0<<24)|(1<<23)|(1<<22)|(1<<20)|(dma_info.period_size/2);  /* 使能中断,单个传输,硬件触发 */
}

static void s3c2440_dma_start(void)
{
	dma_regs->dmasktrig  = (1<<1);/* 启动DMA */
}

static void s3c2440_dma_stop(void)
{
	dma_regs->dmasktrig  &= ~(1<<1);/* 停止DMA */
}


static irqreturn_t irq_handler_dma2(int irq, void *dev_id)
{
	struct snd_pcm_substream *substream = dev_id;

	//更新状态信息
	dma_info.addr_ofs += dma_info.period_size;
	if(dma_info.addr_ofs >= dma_info.buffer_size){
		dma_info.addr_ofs = 0;
	}
	//如果buffer里没有数据了,则调用triger函数停止DMA
	snd_pcm_period_elapsed(substream);
	if(dma_info.be_running){
		load_dma_period();
		s3c2440_dma_start();
	}
	//如果还有数据:加载下一个period,再次触发DMA
	return IRQ_HANDLED;
}

static int s3c2440_dma_open(struct snd_pcm_substream *substream)
{
	int ret;
	struct snd_pcm_runtime *runtime = substream->runtime;
	//设置属性
	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
	snd_soc_set_runtime_hwparams(substream, &s3c2440_dma_hardware);
	//申请中断
	request_irq(IRQ_DMA2,irq_handler_dma2,IRQF_DISABLED,"alsa for play",substream);
	if(ret){
		printk("request_irq err\n");
		return -EIO;
	}
	return 0;
}

static int s3c2440_dma_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	unsigned long total_bytes = params_buffer_bytes(params);
	//根据params设置DMA相关寄存器
	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
	runtime->dma_bytes = total_bytes;
	dma_info.buffer_size = total_bytes;
	dma_info.period_size = params_period_bytes(params);
	return 0;
}

static int s3c2440_dma_prepare(struct snd_pcm_substream *substream)
{
	//准备DMA传输
	
	//复位各状态
	dma_info.addr_ofs = 0;
	dma_info.be_running = 0;
	//加载第一个period
	load_dma_period();
	return 0;
}

static int s3c2440_dma_trigger(struct snd_pcm_substream *substream, int cmd)
{
	//根据cmd启动或者停止DMA传输
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		//启动DMA传输
		s3c2440_dma_start();
		dma_info.be_running = 1;
		break;

	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		//停止DMA传输
		s3c2440_dma_stop();
		dma_info.be_running = 0;
		break;

	default:
		return -EINVAL;
		break;
	}
	return 0;
}

static snd_pcm_uframes_t s3c2440_dma_pointer(struct snd_pcm_substream *substream)
{
	return bytes_to_frames(substream->runtime, dma_info.addr_ofs);
}

static int s3c2440_dma_close(struct snd_pcm_substream *substream)
{
	free_irq(IRQ_DMA2,substream);
	return 0;
}

static struct snd_pcm_ops s3c2440_dma_ops = {
	.open		= s3c2440_dma_open,
	.ioctl		= snd_pcm_lib_ioctl,
	.hw_params	= s3c2440_dma_hw_params,
	.prepare	= s3c2440_dma_prepare,
	.trigger	= s3c2440_dma_trigger,
	.pointer	= s3c2440_dma_pointer,
	.close		= s3c2440_dma_close,
};

static u64 dma_mask = DMA_BIT_MASK(32);
static int s3c2440_dma_new(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_card *card = rtd->card->snd_card;
	struct snd_pcm *pcm = rtd->pcm;
	struct snd_pcm_substream *substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
	struct snd_dma_buffer *buf = &substream->dma_buffer;

	if (!card->dev->dma_mask)
		card->dev->dma_mask = &dma_mask;
	if (!card->dev->coherent_dma_mask)
		card->dev->coherent_dma_mask = DMA_BIT_MASK(32);

	//分配DMA buffer
	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
		dma_info.virt_addr = dma_alloc_writecombine(pcm->card->dev, 
		                                 s3c2440_dma_hardware.buffer_bytes_max,
					                     &dma_info.phy_addr, GFP_KERNEL);
		if(!dma_info.virt_addr){
			return -ENOMEM;
		}
		dma_info.buf_max_size = s3c2440_dma_hardware.buffer_bytes_max;
		buf->dev.type = SNDRV_DMA_TYPE_DEV;
		buf->dev.dev = pcm->card->dev;
		buf->private_data = NULL;
		buf->area = dma_info.virt_addr;
		buf->bytes = dma_info.buf_max_size;
	}

	return 0;
}

static void s3c2440_dma_free(struct snd_pcm *pcm)
{
	//释放DMA buffer
	dma_free_writecombine(pcm->card->dev, dma_info.buf_max_size,
						 dma_info.virt_addr, dma_info.phy_addr);
}

static struct snd_soc_platform_driver s3c2440_dma_platform = {
	.ops		= &s3c2440_dma_ops,
	.pcm_new	= s3c2440_dma_new,
	.pcm_free	= s3c2440_dma_free,
};

static int s3c2440_dma_probe(struct platform_device * pdev)
{
    return snd_soc_register_platform(&pdev->dev, &s3c2440_dma_platform);
}

int s3c2440_dma_remove(struct platform_device *pdev)
{
    snd_soc_unregister_platform(&pdev->dev);
    return 0;
}

struct platform_driver s3c2440_dma_drv = {
	.probe = s3c2440_dma_probe,
	.remove = s3c2440_dma_remove,
	.driver = {.name = "s3c2440-dma",}//一定需要同名,才会调用probe函数
};


static void s3c2440_dma_dev_release (struct device *dev)
{

}
static struct platform_device s3c2440_dma_device = {
	.name = "s3c2440-dma",
	.id = -1,
	.dev = {
		.release = s3c2440_dma_dev_release,
	}
};

static int s3c2440_dma_init(void)
{
	dma_regs = ioremap(0x4B000080, sizeof(struct s3c_dma_regs));
    platform_device_register(&s3c2440_dma_device);//注册平台设备
    platform_driver_register(&s3c2440_dma_drv);//注册平台驱动
    return 0;
}

static void s3c2440_dma_exit(void)
{
	iounmap(dma_regs);
	platform_device_unregister(&s3c2440_dma_device);
    platform_driver_unregister(&s3c2440_dma_drv);
}

module_init(s3c2440_dma_init);
mudule_exit(s3c2440_dma_exit);
MODULE_LICENSE("GPL");

6. Makefile源码

KERN_DIR = /home/ningjw/linux-3.4.2

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m   += machine/
obj-m   += platform/
obj-m   += codec/

六 移植官方wm8976驱动

1.移植好的wm8976.c源码

/*
 * wm8976.c  --  WM8976 ALSA Soc Audio driver
 *
 * Copyright 2007-9 Wolfson Microelectronics PLC.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "wm8976.h"

static volatile unsigned int *gpbdat;
static volatile unsigned int *gpbcon;


/*
 * wm8976 register cache
 * We can't read the WM8976 register space when we are
 * using 2 wire for device control, so we cache them instead.
 */
static const u16 wm8976_reg[WM8976_CACHEREGNUM] = {
	0x0000, 0x0000, 0x0000, 0x0000,
	0x0050, 0x0000, 0x0140, 0x0000,
	0x0000, 0x0000, 0x0000, 0x00ff,
	0x00ff, 0x0000, 0x0100, 0x00ff,
	0x00ff, 0x0000, 0x012c, 0x002c,
	0x002c, 0x002c, 0x002c, 0x0000,
	0x0032, 0x0000, 0x0000, 0x0000,
	0x0000, 0x0000, 0x0000, 0x0000,
	0x0038, 0x000b, 0x0032, 0x0000,
	0x0008, 0x000c, 0x0093, 0x00e9,
	0x0000, 0x0000, 0x0000, 0x0000,
	0x0033, 0x0010, 0x0010, 0x0100,
	0x0100, 0x0002, 0x0001, 0x0001,
	0x0039, 0x0039, 0x0039, 0x0039,
	0x0001, 0x0001,
};

struct wm8976_priv {
	struct snd_soc_codec codec;
	u16 reg_cache[WM8976_CACHEREGNUM];
};

/*
 * read wm8976 register cache
 */
static inline unsigned int wm8976_read_reg_cache(struct snd_soc_codec  *codec,
	unsigned int reg)
{
	u16 *cache = codec->reg_cache;
	if (reg == WM8976_RESET)
		return 0;
	if (reg >= WM8976_CACHEREGNUM)
		return -1;
	return cache[reg];
}

/*
 * write wm8976 register cache
 */
static inline void wm8976_write_reg_cache(struct snd_soc_codec  *codec,
	u16 reg, unsigned int value)
{
	u16 *cache = codec->reg_cache;
	if (reg >= WM8976_CACHEREGNUM)
		return;
	cache[reg] = value;
}

static void set_csb(int val)
{
    if (val)
    {
        *gpbdat |= (1<<2);
    }
    else
    {
        *gpbdat &= ~(1<<2);
    }
}

static void set_clk(int val)
{
    if (val)
    {
        *gpbdat |= (1<<4);
    }
    else
    {
        *gpbdat &= ~(1<<4);
    }
}

static void set_dat(int val)
{
    if (val)
    {
        *gpbdat |= (1<<3);
    }
    else
    {
        *gpbdat &= ~(1<<3);
    }
}


/*
 * write to the WM8976 register space
 */
static int wm8976_write(struct snd_soc_codec  *codec, unsigned int reg,
	unsigned int value)
{
	int i;
	unsigned short val = (reg << 9) | (value & 0x1ff);

    /* save */
	wm8976_write_reg_cache (codec, reg, value);

    if ((reg == WM8976_HPVOLL) || (reg == WM8976_HPVOLR))
        val |= (1<<8);

    /* write to register */
    set_csb(1);
    set_dat(1);
    set_clk(1);

	for (i = 0; i < 16; i++){
		if (val & (1<<15))
		{
            set_clk(0);
            set_dat(1);
			udelay(1);
            set_clk(1);
		}
		else
		{
            set_clk(0);
            set_dat(0);
			udelay(1);
            set_clk(1);
		}

		val = val << 1;
	}

    set_csb(0);
	udelay(1);
    set_csb(1);
    set_dat(1);
    set_clk(1);
    
    return 0;    
}

#define wm8976_reset(c)	wm8976_write(c, WM8976_RESET, 0)

static const char *wm8976_companding[] = {"Off", "NC", "u-law", "A-law" };
static const char *wm8976_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz" };
static const char *wm8976_eqmode[] = {"Capture", "Playback" };
static const char *wm8976_bw[] = {"Narrow", "Wide" };
static const char *wm8976_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz" };
static const char *wm8976_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz" };
static const char *wm8976_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz" };
static const char *wm8976_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz" };
static const char *wm8976_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz" };
static const char *wm8976_alc[] =
    {"ALC both on", "ALC left only", "ALC right only", "Limiter" };

static const struct soc_enum wm8976_enum[] = {
	SOC_ENUM_SINGLE(WM8976_COMP, 1, 4, wm8976_companding), /* adc */
	SOC_ENUM_SINGLE(WM8976_COMP, 3, 4, wm8976_companding), /* dac */
	SOC_ENUM_SINGLE(WM8976_DAC,  4, 4, wm8976_deemp),
	SOC_ENUM_SINGLE(WM8976_EQ1,  8, 2, wm8976_eqmode),

	SOC_ENUM_SINGLE(WM8976_EQ1,  5, 4, wm8976_eq1),
	SOC_ENUM_SINGLE(WM8976_EQ2,  8, 2, wm8976_bw),
	SOC_ENUM_SINGLE(WM8976_EQ2,  5, 4, wm8976_eq2),
	SOC_ENUM_SINGLE(WM8976_EQ3,  8, 2, wm8976_bw),

	SOC_ENUM_SINGLE(WM8976_EQ3,  5, 4, wm8976_eq3),
	SOC_ENUM_SINGLE(WM8976_EQ4,  8, 2, wm8976_bw),
	SOC_ENUM_SINGLE(WM8976_EQ4,  5, 4, wm8976_eq4),
	SOC_ENUM_SINGLE(WM8976_EQ5,  8, 2, wm8976_bw),

	SOC_ENUM_SINGLE(WM8976_EQ5,  5, 4, wm8976_eq5),
	SOC_ENUM_SINGLE(WM8976_ALC3,  8, 2, wm8976_alc),
};

static const struct snd_kcontrol_new wm8976_snd_controls[] = {
SOC_SINGLE("Digital Loopback Switch", WM8976_COMP, 0, 1, 0),

SOC_ENUM("ADC Companding", wm8976_enum[0]),
SOC_ENUM("DAC Companding", wm8976_enum[1]),

SOC_SINGLE("Jack Detection Enable", WM8976_JACK1, 6, 1, 0),

SOC_DOUBLE("DAC Inversion Switch", WM8976_DAC, 0, 1, 1, 0),

//SOC_DOUBLE_R("Headphone Playback Volume", WM8976_DACVOLL, WM8976_DACVOLR, 0, 127, 0),

SOC_SINGLE("High Pass Filter Switch", WM8976_ADC, 8, 1, 0),
//SOC_SINGLE("High Pass Filter Switch", WM8976_ADC, 8, 1, 0),
SOC_SINGLE("High Pass Cut Off", WM8976_ADC, 4, 7, 0),

SOC_DOUBLE("ADC Inversion Switch", WM8976_ADC, 0, 1, 1, 0),

SOC_SINGLE("Capture Volume", WM8976_ADCVOL,  0, 127, 0),

SOC_ENUM("Equaliser Function", wm8976_enum[3]),
SOC_ENUM("EQ1 Cut Off", wm8976_enum[4]),
SOC_SINGLE("EQ1 Volume", WM8976_EQ1,  0, 31, 1),

SOC_ENUM("Equaliser EQ2 Bandwith", wm8976_enum[5]),
SOC_ENUM("EQ2 Cut Off", wm8976_enum[6]),
SOC_SINGLE("EQ2 Volume", WM8976_EQ2,  0, 31, 1),

SOC_ENUM("Equaliser EQ3 Bandwith", wm8976_enum[7]),
SOC_ENUM("EQ3 Cut Off", wm8976_enum[8]),
SOC_SINGLE("EQ3 Volume", WM8976_EQ3,  0, 31, 1),

SOC_ENUM("Equaliser EQ4 Bandwith", wm8976_enum[9]),
SOC_ENUM("EQ4 Cut Off", wm8976_enum[10]),
SOC_SINGLE("EQ4 Volume", WM8976_EQ4,  0, 31, 1),

SOC_ENUM("Equaliser EQ5 Bandwith", wm8976_enum[11]),
SOC_ENUM("EQ5 Cut Off", wm8976_enum[12]),
SOC_SINGLE("EQ5 Volume", WM8976_EQ5,  0, 31, 1),

SOC_SINGLE("DAC Playback Limiter Switch", WM8976_DACLIM1,  8, 1, 0),
SOC_SINGLE("DAC Playback Limiter Decay", WM8976_DACLIM1,  4, 15, 0),
SOC_SINGLE("DAC Playback Limiter Attack", WM8976_DACLIM1,  0, 15, 0),

SOC_SINGLE("DAC Playback Limiter Threshold", WM8976_DACLIM2,  4, 7, 0),
SOC_SINGLE("DAC Playback Limiter Boost", WM8976_DACLIM2,  0, 15, 0),

SOC_SINGLE("ALC Enable Switch", WM8976_ALC1,  8, 1, 0),
SOC_SINGLE("ALC Capture Max Gain", WM8976_ALC1,  3, 7, 0),
SOC_SINGLE("ALC Capture Min Gain", WM8976_ALC1,  0, 7, 0),

SOC_SINGLE("ALC Capture ZC Switch", WM8976_ALC2,  8, 1, 0),
SOC_SINGLE("ALC Capture Hold", WM8976_ALC2,  4, 7, 0),
SOC_SINGLE("ALC Capture Target", WM8976_ALC2,  0, 15, 0),

SOC_ENUM("ALC Capture Mode", wm8976_enum[13]),
SOC_SINGLE("ALC Capture Decay", WM8976_ALC3,  4, 15, 0),
SOC_SINGLE("ALC Capture Attack", WM8976_ALC3,  0, 15, 0),

SOC_SINGLE("ALC Capture Noise Gate Switch", WM8976_NGATE,  3, 1, 0),
SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8976_NGATE,  0, 7, 0),

SOC_SINGLE("Capture PGA ZC Switch", WM8976_INPPGA,  7, 1, 0),
SOC_SINGLE("Capture PGA Volume", WM8976_INPPGA,  0, 63, 0),

SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8976_HPVOLL,  WM8976_HPVOLR, 7, 1, 0),
SOC_DOUBLE_R("Headphone Playback Switch", WM8976_HPVOLL,  WM8976_HPVOLR, 6, 1, 1),
SOC_DOUBLE_R("Headphone Playback Volume", WM8976_HPVOLL,  WM8976_HPVOLR, 0, 63, 0),

SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8976_SPKVOLL,  WM8976_SPKVOLR, 7, 1, 0),
SOC_DOUBLE_R("Speaker Playback Switch", WM8976_SPKVOLL,  WM8976_SPKVOLR, 6, 1, 1),
SOC_DOUBLE_R("Speaker Playback Volume", WM8976_SPKVOLL,  WM8976_SPKVOLR, 0, 63, 0),

SOC_SINGLE("Capture Boost(+20dB)", WM8976_ADCBOOST, 8, 1, 0),
};

/* Left Output Mixer */
static const struct snd_kcontrol_new wm8976_left_mixer_controls[] = {
SOC_DAPM_SINGLE("Right PCM Playback Switch", WM8976_OUTPUT, 6, 1, 1),
SOC_DAPM_SINGLE("Left PCM Playback Switch", WM8976_MIXL, 0, 1, 1),
SOC_DAPM_SINGLE("Line Bypass Switch", WM8976_MIXL, 1, 1, 0),
SOC_DAPM_SINGLE("Aux Playback Switch", WM8976_MIXL, 5, 1, 0),
};

/* Right Output Mixer */
static const struct snd_kcontrol_new wm8976_right_mixer_controls[] = {
SOC_DAPM_SINGLE("Left PCM Playback Switch", WM8976_OUTPUT, 5, 1, 1),
SOC_DAPM_SINGLE("Right PCM Playback Switch", WM8976_MIXR, 0, 1, 1),
SOC_DAPM_SINGLE("Line Bypass Switch", WM8976_MIXR, 1, 1, 0),
SOC_DAPM_SINGLE("Aux Playback Switch", WM8976_MIXR, 5, 1, 0),
};

/* Left AUX Input boost vol */
static const struct snd_kcontrol_new wm8976_laux_boost_controls =
SOC_DAPM_SINGLE("Aux Volume", WM8976_ADCBOOST, 0, 3, 0);

/* Left Input boost vol */
static const struct snd_kcontrol_new wm8976_lmic_boost_controls =
SOC_DAPM_SINGLE("Input Volume", WM8976_ADCBOOST, 4, 3, 0);

/* Left Aux In to PGA */
static const struct snd_kcontrol_new wm8976_laux_capture_boost_controls =
SOC_DAPM_SINGLE("Capture Switch", WM8976_ADCBOOST,  8, 1, 0);

/* Left Input P In to PGA */
static const struct snd_kcontrol_new wm8976_lmicp_capture_boost_controls =
SOC_DAPM_SINGLE("Input P Capture Boost Switch", WM8976_INPUT,  0, 1, 0);

/* Left Input N In to PGA */
static const struct snd_kcontrol_new wm8976_lmicn_capture_boost_controls =
SOC_DAPM_SINGLE("Input N Capture Boost Switch", WM8976_INPUT,  1, 1, 0);

// TODO Widgets
static const struct snd_soc_dapm_widget wm8976_dapm_widgets[] = {
#if 0
//SND_SOC_DAPM_MUTE("Mono Mute", WM8976_MONOMIX, 6, 0),
//SND_SOC_DAPM_MUTE("Speaker Mute", WM8976_SPKMIX, 6, 0),

SND_SOC_DAPM_MIXER("Speaker Mixer", WM8976_POWER3, 2, 0,
	&wm8976_speaker_mixer_controls[0],
	ARRAY_SIZE(wm8976_speaker_mixer_controls)),
SND_SOC_DAPM_MIXER("Mono Mixer", WM8976_POWER3, 3, 0,
	&wm8976_mono_mixer_controls[0],
	ARRAY_SIZE(wm8976_mono_mixer_controls)),
SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8976_POWER3, 0, 0),
SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8976_POWER3, 0, 0),
SND_SOC_DAPM_PGA("Aux Input", WM8976_POWER1, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("SpkN Out", WM8976_POWER3, 5, 0, NULL, 0),
SND_SOC_DAPM_PGA("SpkP Out", WM8976_POWER3, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("Mono Out", WM8976_POWER3, 7, 0, NULL, 0),
SND_SOC_DAPM_PGA("Mic PGA", WM8976_POWER2, 2, 0, NULL, 0),

SND_SOC_DAPM_PGA("Aux Boost", SND_SOC_NOPM, 0, 0,
	&wm8976_aux_boost_controls, 1),
SND_SOC_DAPM_PGA("Mic Boost", SND_SOC_NOPM, 0, 0,
	&wm8976_mic_boost_controls, 1),
SND_SOC_DAPM_SWITCH("Capture Boost", SND_SOC_NOPM, 0, 0,
	&wm8976_capture_boost_controls),

SND_SOC_DAPM_MIXER("Boost Mixer", WM8976_POWER2, 4, 0, NULL, 0),

SND_SOC_DAPM_MICBIAS("Mic Bias", WM8976_POWER1, 4, 0),

SND_SOC_DAPM_INPUT("MICN"),
SND_SOC_DAPM_INPUT("MICP"),
SND_SOC_DAPM_INPUT("AUX"),
SND_SOC_DAPM_OUTPUT("MONOOUT"),
SND_SOC_DAPM_OUTPUT("SPKOUTP"),
SND_SOC_DAPM_OUTPUT("SPKOUTN"),
#endif
};

static const struct snd_soc_dapm_route audio_map[] = {
	/* Mono output mixer */
	{"Mono Mixer", "PCM Playback Switch", "DAC"},
	{"Mono Mixer", "Aux Playback Switch", "Aux Input"},
	{"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},

	/* Speaker output mixer */
	{"Speaker Mixer", "PCM Playback Switch", "DAC"},
	{"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
	{"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},

	/* Outputs */
	{"Mono Out", NULL, "Mono Mixer"},
	{"MONOOUT", NULL, "Mono Out"},
	{"SpkN Out", NULL, "Speaker Mixer"},
	{"SpkP Out", NULL, "Speaker Mixer"},
	{"SPKOUTN", NULL, "SpkN Out"},
	{"SPKOUTP", NULL, "SpkP Out"},

	/* Boost Mixer */
	{"Boost Mixer", NULL, "ADC"},
	{"Capture Boost Switch", "Aux Capture Boost Switch", "AUX"},
	{"Aux Boost", "Aux Volume", "Boost Mixer"},
	{"Capture Boost", "Capture Switch", "Boost Mixer"},
	{"Mic Boost", "Mic Volume", "Boost Mixer"},

	/* Inputs */
	{"MICP", NULL, "Mic Boost"},
	{"MICN", NULL, "Mic PGA"},
	{"Mic PGA", NULL, "Capture Boost"},
	{"AUX", NULL, "Aux Input"},
};

static int wm8976_add_widgets(struct snd_soc_codec *codec)
{
	struct snd_soc_dapm_context *dapm = &codec->dapm;
	snd_soc_dapm_new_controls(dapm, wm8976_dapm_widgets,
				  ARRAY_SIZE(wm8976_dapm_widgets));

	snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));

	snd_soc_dapm_new_widgets(dapm);
	return 0;
}

struct _pll_div {
	unsigned int pre:4; /* prescale - 1 */
	unsigned int n:4;
	unsigned int k;
};

static struct _pll_div pll_div;

/* The size in bits of the pll divide multiplied by 10
 * to allow rounding later */
#define FIXED_PLL_SIZE ((1 << 24) * 10)

static void pll_factors(unsigned int target, unsigned int source)
{
	unsigned long long Kpart;
	unsigned int K, Ndiv, Nmod;

	Ndiv = target / source;
	if (Ndiv < 6) {
		source >>= 1;
		pll_div.pre = 1;
		Ndiv = target / source;
	} else
		pll_div.pre = 0;

	if ((Ndiv < 6) || (Ndiv > 12))
		printk(KERN_WARNING
			"WM8976 N value outwith recommended range! N = %d\n",Ndiv);

	pll_div.n = Ndiv;
	Nmod = target % source;
	Kpart = FIXED_PLL_SIZE * (long long)Nmod;

	do_div(Kpart, source);

	K = Kpart & 0xFFFFFFFF;

	/* Check if we need to round */
	if ((K % 10) >= 5)
		K += 5;

	/* Move down to proper range now rounding is done */
	K /= 10;

	pll_div.k = K;
}

static int wm8976_set_dai_pll(struct snd_soc_dai *codec_dai,
		int pll_id, int source, unsigned int freq_in, unsigned int freq_out)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;

	if(freq_in == 0 || freq_out == 0) {
		reg = wm8976_read_reg_cache(codec, WM8976_POWER1);
		wm8976_write(codec, WM8976_POWER1, reg & 0x1df);
		return 0;
	}

	pll_factors(freq_out * 8, freq_in);

	wm8976_write(codec, WM8976_PLLN, (pll_div.pre << 4) | pll_div.n);
	wm8976_write(codec, WM8976_PLLK1, pll_div.k >> 18);
	wm8976_write(codec, WM8976_PLLK1, (pll_div.k >> 9) && 0x1ff);
	wm8976_write(codec, WM8976_PLLK1, pll_div.k && 0x1ff);
	reg = wm8976_read_reg_cache(codec, WM8976_POWER1);
	wm8976_write(codec, WM8976_POWER1, reg | 0x020);
	
	
	return 0;
}

static int wm8976_set_dai_fmt(struct snd_soc_dai *codec_dai,
		unsigned int fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 iface = wm8976_read_reg_cache(codec, WM8976_IFACE) & 0x3;
	u16 clk = wm8976_read_reg_cache(codec, WM8976_CLOCK) & 0xfffe;

	/* set master/slave audio interface */
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		clk |= 0x0001;
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		break;
	default:
		return -EINVAL;
	}

	/* interface format */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		iface |= 0x0010;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		iface |= 0x0008;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		iface |= 0x00018;
		break;
	default:
		return -EINVAL;
	}

	/* clock inversion */
	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		break;
	case SND_SOC_DAIFMT_IB_IF:
		iface |= 0x0180;
		break;
	case SND_SOC_DAIFMT_IB_NF:
		iface |= 0x0100;
		break;
	case SND_SOC_DAIFMT_NB_IF:
		iface |= 0x0080;
		break;
	default:
		return -EINVAL;
	}

	wm8976_write(codec, WM8976_IFACE, iface);
	wm8976_write(codec, WM8976_CLOCK, clk);

	return 0;
}

static int wm8976_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_codec *codec = rtd->codec;
	u16 iface = wm8976_read_reg_cache(codec, WM8976_IFACE) & 0xff9f;
	u16 adn = wm8976_read_reg_cache(codec, WM8976_ADD) & 0x1f1;

	/* bit size */
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		iface |= 0x0020;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		iface |= 0x0040;
		break;
	}

	/* filter coefficient */
	switch (params_rate(params)) {
	case SNDRV_PCM_RATE_8000:
		adn |= 0x5 << 1;
		break;
	case SNDRV_PCM_RATE_11025:
		adn |= 0x4 << 1;
		break;
	case SNDRV_PCM_RATE_16000:
		adn |= 0x3 << 1;
		break;
	case SNDRV_PCM_RATE_22050:
		adn |= 0x2 << 1;
		break;
	case SNDRV_PCM_RATE_32000:
		adn |= 0x1 << 1;
		break;
	}

	/* set iface */
	wm8976_write(codec, WM8976_IFACE, iface);
	wm8976_write(codec, WM8976_ADD, adn);
	return 0;
}

static int wm8976_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
		int div_id, int div)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;
	switch (div_id) {
	case WM8976_MCLKDIV:
		reg = wm8976_read_reg_cache(codec, WM8976_CLOCK) & 0x11f;
		wm8976_write(codec, WM8976_CLOCK, reg | div);
		break;
	case WM8976_BCLKDIV:
		reg = wm8976_read_reg_cache(codec, WM8976_CLOCK) & 0x1c7;
		wm8976_write(codec, WM8976_CLOCK, reg | div);
		break;
	case WM8976_OPCLKDIV:
		reg = wm8976_read_reg_cache(codec, WM8976_GPIO) & 0x1cf;
		wm8976_write(codec, WM8976_GPIO, reg | div);
		break;
	case WM8976_DACOSR:
		reg = wm8976_read_reg_cache(codec, WM8976_DAC) & 0x1f7;
		wm8976_write(codec, WM8976_DAC, reg | div);
		break;
	case WM8976_ADCOSR:
		reg = wm8976_read_reg_cache(codec, WM8976_ADC) & 0x1f7;
		wm8976_write(codec, WM8976_ADC, reg | div);
		break;
	case WM8976_MCLKSEL:
		reg = wm8976_read_reg_cache(codec, WM8976_CLOCK) & 0x0ff;
		wm8976_write(codec, WM8976_CLOCK, reg | div);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int wm8976_mute(struct snd_soc_dai *dai, int mute)
{
	struct snd_soc_codec *codec = dai->codec;
	u16 mute_reg = wm8976_read_reg_cache(codec, WM8976_DAC) & 0xffbf;

	if(mute)
		wm8976_write(codec, WM8976_DAC, mute_reg | 0x40);
	else {
		wm8976_write(codec, WM8976_DAC, mute_reg);
	}

	return 0;
}

/* TODO: liam need to make this lower power with dapm */
static int wm8976_set_bias_level(struct snd_soc_codec *codec,
	enum snd_soc_bias_level level)
{

	switch (level) {
	case SND_SOC_BIAS_ON:
		wm8976_write(codec, WM8976_POWER1, 0x1ff);
		wm8976_write(codec, WM8976_POWER2, 0x1ff & ~(1<<6));
		wm8976_write(codec, WM8976_POWER3, 0x1ff);
		break;
	case SND_SOC_BIAS_STANDBY:
	case SND_SOC_BIAS_PREPARE:
		break;
	case SND_SOC_BIAS_OFF:
		wm8976_write(codec, WM8976_POWER1, 0x0);
		wm8976_write(codec, WM8976_POWER2, 0x0);
		wm8976_write(codec, WM8976_POWER3, 0x0);
		break;
	}
	codec->dapm.bias_level = level;
	return 0;
}

#define WM8976_RATES \
	(SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
	SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
	SNDRV_PCM_RATE_48000)

#define WM8976_FORMATS \
	(SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
	SNDRV_PCM_FORMAT_S24_3LE | SNDRV_PCM_FORMAT_S24_LE)

static int wm8976_set_sysclk(struct snd_soc_dai *dai, int clk_id, unsigned int freq, int dir)
{
	return 0;
}

static struct snd_soc_dai_ops wm8976_dai_ops = {
	.hw_params = wm8976_hw_params,
	.digital_mute = wm8976_mute,
	.set_fmt = wm8976_set_dai_fmt,
	.set_clkdiv = wm8976_set_dai_clkdiv,
	.set_sysclk = wm8976_set_sysclk,
	.set_pll = wm8976_set_dai_pll,
};

struct snd_soc_dai_driver wm8976_dai = {
	.name = "wm8976-iis",
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 2,
		.rates = WM8976_RATES,
		.formats = WM8976_FORMATS,},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = WM8976_RATES,
		.formats = WM8976_FORMATS,},
	.ops = &wm8976_dai_ops,
};

static int snd_soc_wm8976_suspend(struct snd_soc_codec *codec)
{
	wm8976_set_bias_level(codec, SND_SOC_BIAS_OFF);
	return 0;
}

static int snd_soc_wm8976_resume(struct snd_soc_codec *codec)
{
	int i;
	u16 *cache = codec->reg_cache;

	/* Sync reg_cache with the hardware */
	for (i = 0; i < ARRAY_SIZE(wm8976_reg); i++) {
		codec->write(codec->control_data, i, cache[i]);
	}
	wm8976_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
	wm8976_set_bias_level(codec, SND_SOC_BIAS_ON);
	return 0;
}

static int snd_soc_wm8976_probe(struct snd_soc_codec *codec)
{
    gpbcon = ioremap(0x56000010, 4);
    gpbdat = ioremap(0x56000014, 4);
    
	/* GPB 4: L3CLOCK */
	/* GPB 3: L3DATA */
	/* GPB 2: L3MODE */
    *gpbcon &= ~((3<<4) | (3<<6) | (3<<8));
    *gpbcon |= ((1<<4) | (1<<6) | (1<<8));



	snd_soc_add_codec_controls(codec, wm8976_snd_controls,
			     ARRAY_SIZE(wm8976_snd_controls));
	//wm8976_add_widgets(codec);

	return 0;

}

/* power down chip */
static int snd_soc_wm8976_remove(struct snd_soc_codec *codec)
{
	struct snd_soc_dapm_context *dapm = &codec->dapm;

	//snd_soc_dapm_free(dapm);

    iounmap(gpbcon);
    iounmap(gpbdat);

	return 0;
}


struct snd_soc_codec_driver soc_codec_dev_wm8976 = {
	.probe = 	snd_soc_wm8976_probe,
	.remove = 	snd_soc_wm8976_remove,
	.suspend = 	snd_soc_wm8976_suspend,
	.resume =	snd_soc_wm8976_resume,
	.reg_cache_size = sizeof(wm8976_reg),
	.reg_word_size = sizeof(u16),
	.reg_cache_default = wm8976_reg,
	.reg_cache_step = 2,
	.read = wm8976_read_reg_cache,
	.write = wm8976_write,
	.set_bias_level = wm8976_set_bias_level,
};

/* 通过注册平台设备、平台驱动来实现对snd_soc_register_codec的调用
 *
 */

static void wm8976_dev_release(struct device * dev)
{
}

static int wm8976_probe(struct platform_device *pdev)
{
	return snd_soc_register_codec(&pdev->dev,
			&soc_codec_dev_wm8976, &wm8976_dai, 1);
}

static int wm8976_remove(struct platform_device *pdev)
{
    snd_soc_unregister_codec(&pdev->dev);
    return 0;
}

static struct platform_device wm8976_dev = {
    .name         = "wm8976-codec",
    .id       = -1,
    .dev = { 
    	.release = wm8976_dev_release, 
	},
};
struct platform_driver wm8976_drv = {
	.probe		= wm8976_probe,
	.remove		= wm8976_remove,
	.driver		= {
		.name	= "wm8976-codec",
	}
};

static int wm8976_init(void)
{    
    platform_device_register(&wm8976_dev);
    platform_driver_register(&wm8976_drv);
    return 0;
}

static void wm8976_exit(void)
{
    platform_device_unregister(&wm8976_dev);
    platform_driver_unregister(&wm8976_drv);
}

module_init(wm8976_init);
module_exit(wm8976_exit);

MODULE_LICENSE("GPL");

2.wm8976.h文件

/*
 * wm8976.h  --  WM8976 Soc Audio driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef _WM8976_H
#define _WM8976_H

/* WM8976 register space */

#define WM8976_RESET		0x0
#define WM8976_POWER1		0x1
#define WM8976_POWER2		0x2
#define WM8976_POWER3		0x3
#define WM8976_IFACE		0x4
#define WM8976_COMP			0x5
#define WM8976_CLOCK		0x6
#define WM8976_ADD			0x7
#define WM8976_GPIO			0x8
#define WM8976_JACK1        0x9
#define WM8976_DAC			0xa
#define WM8976_DACVOLL	    0xb
#define WM8976_DACVOLR      0xc
#define WM8976_JACK2        0xd
#define WM8976_ADC			0xe
#define WM8976_ADCVOL		0xf
#define WM8976_EQ1			0x12
#define WM8976_EQ2			0x13
#define WM8976_EQ3			0x14
#define WM8976_EQ4			0x15
#define WM8976_EQ5			0x16
#define WM8976_DACLIM1		0x18
#define WM8976_DACLIM2		0x19
#define WM8976_NOTCH1		0x1b
#define WM8976_NOTCH2		0x1c
#define WM8976_NOTCH3		0x1d
#define WM8976_NOTCH4		0x1e
#define WM8976_ALC1			0x20
#define WM8976_ALC2			0x21
#define WM8976_ALC3			0x22
#define WM8976_NGATE		0x23
#define WM8976_PLLN			0x24
#define WM8976_PLLK1		0x25
#define WM8976_PLLK2		0x26
#define WM8976_PLLK3		0x27
#define WM8976_3D           0x29
#define WM8976_BEEP         0x2b
#define WM8976_INPUT		0x2c
#define WM8976_INPPGA	  	0x2d
#define WM8976_ADCBOOST		0x2f
#define WM8976_OUTPUT		0x31
#define WM8976_MIXL	        0x32
#define WM8976_MIXR         0x33
#define WM8976_HPVOLL		0x34
#define WM8976_HPVOLR       0x35
#define WM8976_SPKVOLL      0x36
#define WM8976_SPKVOLR      0x37
#define WM8976_OUT3MIX		0x38
#define WM8976_MONOMIX      0x39

#define WM8976_CACHEREGNUM 	58

/*
 * WM8976 Clock dividers
 */
#define WM8976_MCLKDIV 		0
#define WM8976_BCLKDIV		1
#define WM8976_OPCLKDIV		2
#define WM8976_DACOSR		3
#define WM8976_ADCOSR		4
#define WM8976_MCLKSEL		5

#define WM8976_MCLK_MCLK		(0 << 8)
#define WM8976_MCLK_PLL			(1 << 8)

#define WM8976_MCLK_DIV_1		(0 << 5)
#define WM8976_MCLK_DIV_1_5		(1 << 5)
#define WM8976_MCLK_DIV_2		(2 << 5)
#define WM8976_MCLK_DIV_3		(3 << 5)
#define WM8976_MCLK_DIV_4		(4 << 5)
#define WM8976_MCLK_DIV_5_5		(5 << 5)
#define WM8976_MCLK_DIV_6		(6 << 5)

#define WM8976_BCLK_DIV_1		(0 << 2)
#define WM8976_BCLK_DIV_2		(1 << 2)
#define WM8976_BCLK_DIV_4		(2 << 2)
#define WM8976_BCLK_DIV_8		(3 << 2)
#define WM8976_BCLK_DIV_16		(4 << 2)
#define WM8976_BCLK_DIV_32		(5 << 2)

#define WM8976_DACOSR_64		(0 << 3)
#define WM8976_DACOSR_128		(1 << 3)

#define WM8976_ADCOSR_64		(0 << 3)
#define WM8976_ADCOSR_128		(1 << 3)

#define WM8976_OPCLK_DIV_1		(0 << 4)
#define WM8976_OPCLK_DIV_2		(1 << 4)
#define WM8976_OPCLK_DIV_3		(2 << 4)
#define WM8976_OPCLK_DIV_4		(3 << 4)


#endif

 

七 使用strace跟踪函数调用过程

  • 解压strace-4.8.tar.xz后,进入目录执行以下命令
./configure --host=arm-linux --prefix=$PWD/__install
make & make install
cp __install/bin /home/ningjw/linux/root_fs/bin/
  • 使用trace 跟踪命令,并输出到applay.log 与amixer.log
strace -o applay.log aplay xxx.wav
strace -o amixer.log amixer cset numid=1 30

八 声卡测试

1.去掉自带声卡

-> Device Drivers
   -> Sound card support
      -> Advanced Linux Sound Architecture
         -> ALSA for SoC audio support
            <>   ASoC support for Samsung
            <>   SoC I2S Audio support WM8976 wired to a S3C24XX

2.使用新内核启动,并安装声卡驱动,播放音乐

#安装驱动
insmod alsa/codec/wm8976.ko
insmod alsa/machine/s3c2440_wm8976.ko
insmod alsa/platform/s3c2440_iis.ko
insmod alsa/platform/s3c2440_dma.ko

#播放音乐
aplay xxx.wav

 

参考:

大牛ALSA驱动分析

你可能感兴趣的:(嵌入式linux)