字符设备
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#define DEV_CNT x /* 设备号长度 */
#define DEV_NAME "xxxxxx" /* 设备名字 */
/* dev设备结构体 */
struct xxx_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
struct device_node *node; /* xxx设备节点 */
int xxx0; /* xxxGPIO标号 */
};
struct xxx_dev xxxdev; /* xxx设备 */
//打开设备
static int xxx_open(struct inode *inode, struct file *filp)
{
xxxxx;
}
//向设备写数据
static ssize_t xxx_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
xxxxx;
return 0;
}
/*设备操作函数*/
static struct file_operations xxx_fops = {
.owner = THIS_MODULE,
.open = xxx_open,
.write = xxx_write,
};
/*
* @description : platform驱动的probe函数,当驱动与
* 设备匹配以后此函数就会执行
* @param - dev : platform设备
* @return : 0,成功;其他负值,失败
*/
static int xxx_probe(struct platform_device *dev)
{
/*设置设备号*/
/*注册设备*/
cdev_init(...);
cdev_add(...);
/*创建类*/
/*创建设备*/
xxxdev.device = device_create(xxxdev.class,NULL,xxxdev.devid,NULL,LEDDEV_NAME);
if(IS_ERR(xxxdev.device))
{
return PTR_ERR(xxxdev.device)
}
/*初始化IO*/
xxxdev.node = of_find_node_by_path("/xxx");
if(xxxdev.node = NULL)
{
printk("xxxnode nost find\r\n");
return -EINVAL;
}
xxxx = of_get_named_gpio(xxxdev.node,"led_gpio",n);
gpio_request(...);
gpio_direction_output(xxx);//设置为输出,默认高电平
return 0;
}
static int xxx_remove(struct platform_device *dev)
{
xxxxx;
return 0;
}
/*匹配列表*/
static const struct of_device_id xxx_of_match[] = {
{.compatible = "xxxx"}, //设备树兼容性
{/*Sentinel*/}
};
/*platform驱动结构体*/
static struct platform_driver xxx_driver = {
.driver = {
.name = "xxxx", //驱动名字,用于和设备匹配
.of_match_table = xxx_of_match, //设备树匹配表
},
.probe = xxx_probe,
.remove = xxx_remove,
};
//驱动模块加载函数
static int _init xxxdriver_init(void)
{
return platform_driver_register(&xxx_driver); //platform驱动结构体
}
//驱动模块卸载模块
static int _exit xxxdriver_exit(void)
{
platform_driver_unregister(&xxx_driver);
}
module_init(&xxxdriver_init);
module_exit(&xxxdriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("name");