GPIO驱动框架编写流程:
1、在设备树中添加gpio_pin的pinctrl信息(配置gpio复用,电气属性)
2、在引用该gpio的节点添加gpio信息
3、编写驱动
3.1 定义设备名+设备号个数
3.2 定义设备结构体
设备号 dev_t devid
主次设备号 major、minor
设备节点 device_node *nd
cdev cdev cdev
类 class *class
设备 device *device
gpio编号 gpio_num
3.3 ops函数
static int xxx_open(struct inode *inode, struct file *filp);
static int xxx_release(struct inode *inode, struct file *filp);
static ssize_t xxx_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt);
static ssize_t xxx_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt);
static struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.open = gpio_open,
.release = gpio_release,
.write = gpio_write,
.read = gpio_read,
};
3.4 module_init
static int __init xxx_init(void);
3.4.1 设备树节点属性获取
3.4.2 定义或申请设备号
3.4.3 cdev_init & cdev_add
3.4.4 class_create
3.4.5 device_create
3.5 module_exit
static void __exit xxx_exit(void);
3.5.1 cdev_del
3.5.2 unregister_chrdev_region
3.5.3 device_destroy
3.5.4 class_destroy
3.6 license
module_init(xxx_init);
module_exit(xxx_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("XXX");
4、编写测试用例