linux platform 驱动程序框架

 字符设备

#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");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值