asoc 音频驱动学习笔记2

接下来我们看看codecprobe函数吧

static int wm8960_probe(struct snd_soc_codec *codec)
{
	struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
	struct wm8960_data *pdata = dev_get_platdata(codec->dev);
	int ret;
	u16 reg;

	wm8960->set_bias_level = wm8960_set_bias_level_out3;
	codec->control_data = wm8960->control_data;

先看第一个snd_soc_codec_get_drvdata(codec);

static inline void *snd_soc_codec_get_drvdata(struct snd_soc_codec *codec)
{
	return dev_get_drvdata(codec->dev);
}
    void *dev_get_drvdata(const struct device *dev)
{
	if (dev && dev->p)
		return dev->p->driver_data;
	return NULL;
}

由上面可知,最终得到的结构为codec->dev->p->driver_data,这个结构在哪呢,i2c_set_clientdata(i2c, wm8960)  ->  ret = snd_soc_register_codec(&i2c->dev,

&soc_codec_dev_wm8960, &wm8960_dai, 1); -> codec->dev = dev;,由此可知,这个driver_data为班级配置中定义的平台设备的driver_data

然后看第二个 dev_get_platdata(codec->dev);

static inline void *dev_get_platdata(const struct device *dev)
{
	return dev->platform_data;
}

在板级配置中

#ifdef CONFIG_SND_SOC_WM8960_MINI210
	{
		I2C_BOARD_INFO("wm8960", 0x1a),
		.platform_data = &wm8960_pdata,
	},
#endif
#ifdef CONFIG_SND_SOC_WM8960_MINI210
#include 
static struct wm8960_data wm8960_pdata = {
	.capless		= 0,
	.dres			= WM8960_DRES_400R,
};
#endif

////////////////////////////////////////////////////////////////////////////////////////////接着进行probe函数分析

wm8960->set_bias_level = wm8960_set_bias_level_out3;
	codec->control_data = wm8960->control_data;这个地方的值为i2c
if (!pdata) {进行pdata的判断
		dev_warn(codec->dev, "No platform data supplied\n");
	} else {
		if (pdata->dres > WM8960_DRES_MAX) {
			dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
			pdata->dres = 0;
		}

		if (pdata->capless)
			wm8960->set_bias_level = wm8960_set_bias_level_capless;
	}
  ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8960->control_type);这个函数值得研究
	if (ret < 0) {
		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
		return ret;
	}
查看snd_soc_codec_set_cache_io函数
/**建立标准io功能
 * snd_soc_codec_set_cache_io: Set up standard I/O functions.
 *
 * @codec: CODEC to configure.
 * @addr_bits: Number of bits of register address data.
 * @data_bits: Number of bits of data per register.
 * @control: Control bus used.使用的控制总线
 *寄存器格式被频繁的分享在i2c和spi设备间,为了提高代码的重用率,ASOC核心提供了标准的codec read 和write操作
 * Register formats are frequently shared between many I2C and SPI
 * devices.  In order to promote code reuse the ASoC core provides
 * some standard implementations of CODEC read and write operations
 * which can be set up using this function.
 *
 * The caller is responsible for allocating and initialising the
 * actual cache.
 *
 * Note that at present this code cannot be used by CODECs with
 * volatile registers.
 */
int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
			       int addr_bits, int data_bits,
			       enum snd_soc_control_type control)
{
	int i;
寻找匹配的寄存器操作格式,8960为7位地址,9位数据
	for (i = 0; i < ARRAY_SIZE(io_types); i++)
		if (io_types[i].addr_bits == addr_bits &&
		    io_types[i].data_bits == data_bits)
			break;
	if (i == ARRAY_SIZE(io_types)) {如果循环完了还没找到合适的格式,那么打印信息
		printk(KERN_ERR
		       "No I/O functions for %d bit address %d bit data\n",
		       addr_bits, data_bits);
		return -EINVAL;
	}
     函数赋值
	codec->write = io_types[i].write;这个结构赋值后,最终会调用回codec->hw_write函数
	codec->read = io_types[i].read;read和write的处理方法不太一样,不深究了吧
	codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;

	switch (control) {
	case SND_SOC_I2C:
#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
		codec->hw_write = (hw_write_t)i2c_master_send;
#endif
		if (io_types[i].i2c_read)
			codec->hw_read = io_types[i].i2c_read;
	codec->control_data = container_of(codec->dev,
						   struct i2c_client,
						   dev);这个和函数的意思是根据codec->dev找到它属于的结构struct i2c_client的地址,codec->dev,为dev型的
		break;

	case SND_SOC_SPI:
#ifdef CONFIG_SPI_MASTER
		codec->hw_write = do_spi_write;
#endif

		codec->control_data = container_of(codec->dev,
						   struct spi_device,
						   dev);
		break;
	}

	return 0;
}

接着进行wm8960_probe函数分析

ret = wm8960_reset(codec);
	if (ret < 0) {
		dev_err(codec->dev, "Failed to issue reset\n");
		return ret;
	}

////////////////////////////////展开wm8960_reset()

8960进行复位

#define wm8960_reset(c)	snd_soc_write(c, WM8960_RESET, 0)
unsigned int snd_soc_write(struct snd_soc_codec *codec,
			   unsigned int reg, unsigned int val)
{
	dev_dbg(codec->dev, "write %x = %x\n", reg, val);
	trace_snd_soc_reg_write(codec, reg, val);
	return codec->write(codec, reg, val);
}

//////////////////////回到probe函数

if (ret < 0) {
		dev_err(codec->dev, "Failed to issue reset\n");
		return ret;
	}

	wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);

	/* Latch the update bits */
	reg = snd_soc_read(codec, WM8960_LINVOL);
	snd_soc_write(codec, WM8960_LINVOL, reg | 0x100);
reg = snd_soc_read(codec, WM8960_RINVOL);
	snd_soc_write(codec, WM8960_RINVOL, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_LADC);
	snd_soc_write(codec, WM8960_LADC, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_RADC);
	snd_soc_write(codec, WM8960_RADC, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_LDAC);
	snd_soc_write(codec, WM8960_LDAC, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_RDAC);
	snd_soc_write(codec, WM8960_RDAC, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_LOUT1);
	snd_soc_write(codec, WM8960_LOUT1, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_ROUT1);
	snd_soc_write(codec, WM8960_ROUT1, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_LOUT2);
	snd_soc_write(codec, WM8960_LOUT2, reg | 0x100);
	reg = snd_soc_read(codec, WM8960_ROUT2);
	snd_soc_write(codec, WM8960_ROUT2, reg | 0x100);

以上是9860进行初始化操作

nd_soc_add_controls(codec, wm8960_snd_controls,
				     ARRAY_SIZE(wm8960_snd_controls));
	wm8960_add_widgets(codec);

展开snd_soc_add_controls,添加一个控制队列到一个codec

/**
 * snd_soc_add_controls - add an array of controls to a codec.
 * Convienience function to add a list of controls. Many codecs were
 * duplicating this code.
 *
 * @codec: codec to add controls to
 * @controls: array of controls to add
 * @num_controls: number of elements in the array
 *
 * Return 0 for success, else error.
 */
int snd_soc_add_controls(struct snd_soc_codec *codec,
	const struct snd_kcontrol_new *controls, int num_controls)
{
	struct snd_card *card = codec->card->snd_card;
	int err, i;

	for (i = 0; i < num_controls; i++) {
		const struct snd_kcontrol_new *control = &controls[i];新的control
		err = snd_ctl_add(card, snd_soc_cnew(control, codec,
						     control->name,codec->name_prefix));
if (err < 0) {
			dev_err(codec->dev, "%s: Failed to add %s: %d\n",
				codec->name, control->name, err);
			return err;
		}
	}

	return 0;
}
/**
 * snd_ctl_add - add the control instance to the card
 * @card: the card instance
 * @kcontrol: the control instance to add
 *
 * Adds the control instance created via snd_ctl_new() or
 * snd_ctl_new1() to the given card. Assigns also an unique
 * numid used for fast search.
 *
 * Returns zero if successful, or a negative error code on failure.
 *
 * It frees automatically the control which cannot be added.
 */
int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)

这个函数的作用是添加一个control 实例到声卡Adds the control instance created via snd_ctl_new() or snd_ctl_new1() to the given card.

snd_soc_cnew(control, codec,control->name,codec->name_prefix)

    kcontrol = snd_ctl_new1(&template, data);

    return snd_ctl_new(&kctl, access);

Codec的驱动大体看到这,挺乱的,都是看代码时的顺序乱记的,写博客应该总结总结,以后再总结吧




 

 

 









 


 




 

你可能感兴趣的:(linux驱动)