LINUX IIC总线 MISC driver

1.IIC函数

   1.1读写一个字节

/**
 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
 * @client: Handle to slave device
 * @command: Byte interpreted by slave
 * @value: Byte being written
 *
 * This executes the SMBus "write byte" protocol, returning negative errno
 * else zero on success.
 */
s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,u8 value)
NOTE:start+device addr(write) + command (register addr)+value(register value)

/**
 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
 * @client: Handle to slave device
 * @command: Byte interpreted by slave
 *
 * This executes the SMBus "read byte" protocol, returning negative errno
 * else a data byte received from the device.
 */
s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
NOTE:start+device addr(write) + command (register addr)+repeat start+device addr(write)->返回command地址的value

 1.2读写多个字节

/* Returns the number of read bytes */
s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,u8 length, u8 *values)
/**
 * i2c_smbus_write_block_data - SMBus "block write" protocol
 * @client: Handle to slave device
 * @command: Byte interpreted by slave
 * @length: Size of data block; SMBus allows at most 32 bytes
 * @values: Byte array which will be written.
 *
 * This executes the SMBus "block write" protocol, returning negative errno
 * else zero on success.
 */
s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, u8 length, const u8 *values)





2.IIC MISC最小驱动:

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/of.h>

struct myiic_data {
	struct	device *dev;
	struct  regmap *regmap;
	struct	mutex lock;
/*myiic 设备数据*/
	int number;
};

struct i2c_client *ptr_myiic_client=NULL;
/* sysfs callbacks -----> write*/
static ssize_t set_number(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct myiic_data *data = dev_get_drvdata(dev);
	unsigned long number;
	int err = kstrtoul(buf, 10, &number);
	
	printk("Func=%s,%d number=%d\n",__FUNCTION__,__LINE__,number);
	if (err == 0) {
		mutex_lock(&data->lock);
		data->number = number;
		mutex_unlock(&data->lock);
		return count;
	}
	return err;
}
/* sysfs callbacks -----> read*/
static ssize_t show_number(struct device *dev,
			     struct device_attribute *attr, char *buf)
{	
	struct myiic_data *data = dev_get_drvdata(dev);	
	/*读数据*/
	//dev_dat = i2c_smbus_read_byte_data(ptr_myiic_client,0xD0);
	return sprintf(buf, "%d\n",data->number);
}
/*
S_IWUSR:write enable
S_IRUGO:read enable
*/
			/*     文件名字    读写属性      读回调函数    写回调函数           */
static DEVICE_ATTR(number, S_IWUSR|S_IRUGO, show_number, set_number);

static struct attribute *myiic_attributes[] = {
	&dev_attr_number.attr,
	NULL
};
static const struct attribute_group myiic_attr_group = {
	.attrs = myiic_attributes,
};
static int myiic_i2c_probe(struct i2c_client *client,
				      const struct i2c_device_id *id)
{
	int retValue = 0;
	int err = 0;
	struct myiic_data *data;
	
	
	data = kzalloc(sizeof(struct myiic_data), GFP_KERNEL);
	if (!data) 
	{
		err = -ENOMEM;
		goto exit;
	}
	ptr_myiic_client =	 client;
	dev_set_drvdata(&client->dev, data);
	data->dev = &client->dev;
	//data->regmap = regmap;
	mutex_init(&data->lock);

	retValue = i2c_smbus_read_byte_data(client,0xD0);
	printk("Func=%s,%d retValue=0x%x\n",__FUNCTION__,__LINE__,retValue);
	
	/* Register sysfs hooks */
	err = sysfs_create_group(&client->dev.kobj, &myiic_attr_group);
	printk("Func=%s,%d  Register sysfs hooks err=%d\n",__FUNCTION__,__LINE__,err);	
	if (err)
		goto exit_free;
	printk("Func=%s,%d ,myiic init success!!\n",__FUNCTION__,__LINE__);	
	return 0;	

exit_free:
	ptr_myiic_client = NULL;
	kfree(data);
exit:
	return err;

}
static int myiic_i2c_remove(struct i2c_client *client)
{
	struct myiic_data *data = dev_get_drvdata(&client->dev);

	sysfs_remove_group(&data->dev->kobj, &myiic_attr_group);
	kfree(data);
	ptr_myiic_client = NULL;
	
	printk("Func=%s,%d ,myiic remove success!!\n",__FUNCTION__,__LINE__);	
	return 0;
}

static const struct i2c_device_id myiic_id[] = {
	{  "xxxx", 0 },/*修改设备树ID*/
	{}
};
MODULE_DEVICE_TABLE(i2c, myiic_id);

static struct i2c_driver myiic_i2c_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "xxxx",/*MISC name*/

	},
	.id_table	= myiic_id,
	.probe		= myiic_i2c_probe,
	.remove		= myiic_i2c_remove,
/*
	.detect		= bme680_i2c_detect,
	.address_list	= normal_i2c
*/
};

module_i2c_driver(myiic_i2c_driver);

MODULE_AUTHOR("crazyuav <crazyuav@xxx.com>");
MODULE_DESCRIPTION("myiic I2C bus driver");
MODULE_LICENSE("GPL");



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值