代码如下:
#include <linux/init.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include<linux/i2c.h>
//设备树匹配表
static const struct of_device_id of_i2c_match[] = {
{ .compatible = "tsys02d", },
{ }
};
//传统设备表,最后一项必须为空,用来标识结束符
static const struct i2c_device_id tsys02d_id[] = {
{"tsys02d", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, tsys02d_id);
ssize_t MyRead (struct i2c_client *client)
{
struct i2c_msg msg[2];
unsigned char args, data;
args = 0;
/* 先传读地址 */
msg[0].addr = 0x40;
msg[0].buf = &args;
msg[0].len = 1;
msg[0].flags = 0;
/* 再 读 */
msg[1].addr = 0x40;
msg[1].buf = &data;
msg[1].len = 1;
msg[1].flags = 1; /* 读 */
printk("start i2c_transfer\n");
if (2 == i2c_transfer(client->adapter, msg, 2))
{
printk("read--->%d" , data);
/* 读成功 */
printk("success\n");
return 1;
}
else
{
printk("i2c_transfer !=2\n");
return -EIO;
}
}
static int tsys02d_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
printk("start in tsys02d_probe\n");
MyRead(client);
printk("out tsys02d_probe\n");
return 0;
}
static int tsys02d_remove(struct i2c_client *client)
{
printk("mxc_vga_i2c_remove_exit finished..\n");
return 0;
}
//I2c——driver
static struct i2c_driver tsys02d_driver = {
.driver = {
.name = "tsys02d",
.owner = THIS_MODULE,
.of_match_table= of_i2c_match,
},
.probe = tsys02d_probe,
.remove = tsys02d_remove,
.id_table = tsys02d_id,
};
#if 0
static int __init tsts02dinit(void)
{
printk("tsys02dinit\n");
int ret = 0;
ret = i2c_add_driver(&tsys02d_driver);
return ret;
}
static void __exit tsts02dexit(void)
{
printk("tsts02dexit\n");
i2c_del_driver(&tsys02d_driver);
}
#endif
//一:设备加入到外设队列中,
//二:告诉程序阅读者该设备是热插拔设备或是说该设备支持热插拔功能。
MODULE_DEVICE_TABLE(i2c, tsys02d_id);
//宏,拆解之后就是init和exit函数
module_i2c_driver(tsys02d_driver);
//module_init(tsts02dinit);
//module_exit(tsts02dexit);
MODULE_DESCRIPTION("20210605i2cdrivertest");
MODULE_AUTHOR("lw");
MODULE_LICENSE("GPL v2");