i2c总线实现driver驱动-38

i2c总线实现driver驱动

然后我们再来看driver部分。不管是使用设备树还是非设备树,driver部分就比较复杂了。和注册一个杂项设备或者是字符设备的套路一样,我们也是要先定一个一个i2c_driver的结构体,然后在对他进行初始化,我们来看一下这个结构体的定义,如下图所示:;

struct i2c_driver {
	unsigned int class;

	/* Notifies the driver that a new bus has appeared. You should avoid
	 * using this, it will be removed in a near future.
	 */
	int (*attach_adapter)(struct i2c_adapter *) __deprecated;

	/* Standard driver model interfaces */
	int (*probe)(struct i2c_client *, const struct i2c_device_id *);
	int (*remove)(struct i2c_client *);

	/* driver model interfaces that don't relate to enumeration  */
	void (*shutdown)(struct i2c_client *);

	/* Alert callback, for example for the SMBus alert protocol.
	 * The format and meaning of the data value depends on the protocol.
	 * For the SMBus alert protocol, there is a single bit of data passed
	 * as the alert response's low bit ("event flag").
	 */
	void (*alert)(struct i2c_client *, unsigned int data);

	/* a ioctl like command that can be used to perform specific functions
	 * with the device.
	 */
	int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

	struct device_driver driver;
	const struct i2c_device_id *id_table;

	/* Device detection callback for automatic device creation */
	int (*detect)(struct i2c_client *, struct i2c_board_info *);
	const unsigned short *address_list;
	struct list_head clients;
};

在驱动注册之前i2c_driver 结构体需要被正确地初始化,有4个成员要求必须被初始化,其中 id_table不管用不用设备树都要被初始化,否则不能匹配成功:

static struct i2c_driver xx_driver ={
    .driver = {
    	.name = "xxox",
    },
    .probe = xoox_probe,
    .remove = xxx_remove,
    .id_table = xxx_table,
};

  • 初始化完成以后就是把i2c_driver注册进内核,注册进内核我们使用是的是i2c_add_driver:

  • i2c_add_driver函数

作用:

注册一个i2c 驱动

函数原型:

#define i2c_add_driver(driver)  i2c_register_driver(THIS_MODULE,driver)

参数:

  • struct i2c_driver的指针。
  • 返回值:失败返回负值

  • i2c_del_driver函数

作用:

删除一个i2c驱动

函数原型:

extern void i2c_del_driver(struct i2c_driver*);

参数:

  • struct i2c_driver的指针。
  • 返回值:失败返回负值

通过make menuconfig 卸载系统驱动FT5X0X

device-drivers >> input device support >> Touchscreens >> FT5X0X

代码

  • dirver.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/i2c.h>


struct of_device_id  ft5x06_id[] = {
    {.compatible = "edt,ft5x0x_ts", 0},
    {.compatible = "edt,ft5x0x_ts", 0},
    {.compatible = "edt,ft5x0x_ts", 0},
    {}
};

struct i2c_device_id  ft5x06_id_ts[] = {
    {"xxxxx", 0},
    {},
};

int ft5x06_probe(struct i2c_client *i2c_client, const struct i2c_device_id *id){

    printk("this is  ft5x06_probe \n ");

    return 0;
}

int ft5x06_remove(struct i2c_client *i2c_client){
    printk("ft5x06_remove \n ");
    return 0;
}


static struct i2c_driver ft5x06_driver = {
    .probe = ft5x06_probe,
    .remove = ft5x06_remove,
    .id_table = ft5x06_id_ts,
    .driver = {
        .owner = THIS_MODULE,
        .name = "ft5x06_test",
        .of_match_table = ft5x06_id,
    },
};


static int ft5x06_driver_init(void){

    int ret;

    printk("ft5x06_driver_init \n");

    ret = i2c_add_driver(&ft5x06_driver);
    if (ret < 0)
    {
        printk("i2c_add_driver is error \n ");
        return  ret ;
    }

    printk("i2c_add_driver is success \n");

    return 0;

}

static void ft5x06_driver_exit(void){

    printk("ft5x06_driver_exit \n");
    i2c_del_driver(&ft5x06_driver);

}

module_init(ft5x06_driver_init);
module_exit(ft5x06_driver_exit);
MODULE_LICENSE("GPL");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值