1. 代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#define NEWCHRDEV_NAME "newChrLed"
#define NEWCHRDEV_COUNT 1
#define LED_MAJOR 200 //主设备号
#define LED_NAME "led"
/* 寄存器的物理地址 */
#define CCM_CCGR1_BASE (0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIR_BASE (0X0209C004)
#if 1
/* 地址映射后的虚拟地址指针 */ //iomem是虚拟地址的类型
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;
#define LEDOFF 0 /* 关灯 */
#define LEDON 1 /* 开灯 */
#endif
/* LED设备结构体 */
struct newchrled_dev{
struct cdev cDev; /* 字符设备 */
dev_t devId; /* 设备号 */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
};
static struct newchrled_dev newchrled; /* led设备 */
/* 驱动开闭读写 */
/* LED灯打开或者关闭 */
static void led_switch(u8 sta)
{
u32 val = 0;
if(sta == LEDON){
val = readl(GPIO1_DR);
val &= ~(1 << 3);
writel(val, GPIO1_DR);
} else if(sta == LEDOFF) {
val = readl(GPIO1_DR);
val |= (1 << 3);
writel(val, GPIO1_DR);
}
}
static int newchrled_open(struct inode *inode, struct file *filp)
{
/* 在这里添加私有数据 */
filp->private_data = &newchrled;
return 0;
}
static int newchrled_release(struct inode *inode, struct file *filp)
{
struct newchrled_dev *dev = (struct newchrled_dev*)filp->private_data;
return 0;
}
static ssize_t newchrled_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
int retValue;
unsigned char dataBuf[1]; /* 1表示1位,开灯关灯 */
retValue = copy_from_user(dataBuf, buf, count);
if(retValue < 0){
printk("kernel write failed! \r\n");
return -EFAULT;
}
/* 判断开灯还是关灯 */
led_switch(dataBuf[0]);
return 0;
}
static const struct file_operations newchrled_fops = {
.owner = THIS_MODULE,
.open = newchrled_open,
.write = newchrled_write,
.release = newchrled_release
};
static void led_init(void)
{
/* 初始化LED灯 */
int val;
/* 1.1 地址映射 */
IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);
GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);
/* 1.2 初始化 */
val = readl(IMX6U_CCM_CCGR1);
val &= ~(3 << 26); /* 先清楚以前的bit26 27 */
val |= 3 << 26; /* 26 27 置1 */
writel(val, IMX6U_CCM_CCGR1);
/* 1.3 设置 GPIO1_IO03 的复用功能,将其复用为
* GPIO1_IO03,最后设置 IO 属性。
*/
writel(0x5, SW_MUX_GPIO1_IO03); /* 设置复用 */
writel(0x10B0, SW_PAD_GPIO1_IO03); /* 设置电气属性 */
/* 1.4 设置 GPIO1_IO03 为输出功能 */
val = readl(GPIO1_GDIR);
val &= ~(1 << 3);
val |= 1 << 3;
writel(val, GPIO1_GDIR);
/* 1.5 默认开启 LED */
val = readl(GPIO1_DR);
val &= ~(1 << 3);
writel(val, GPIO1_DR);
}
/* 驱动入口 */
static int __init newchrled_init(void)
{
unsigned int ret = 0;
/* 1.初始化LED */
led_init();
printk("led init success \r\n");
/* 2.分配设备号 */
newchrled.major = 0; /* 手动申请0,表示需要分配 */
if(newchrled.major){
newchrled.devId = MKDEV(newchrled.major, 0);
ret = register_chrdev_region(newchrled.devId,
NEWCHRDEV_COUNT,
NEWCHRDEV_NAME);/* 主设备号,申请1个,名字 */
} else {
ret = alloc_chrdev_region(&newchrled.devId, 0,
NEWCHRDEV_COUNT,
NEWCHRDEV_NAME);/* 注意第一个参数是取地址,次设备号是0 */
newchrled.major = MAJOR(newchrled.devId);
newchrled.minor = MINOR(newchrled.devId);
}
if(ret < 0){
printk("chrdev region error \r\n");
goto fail_devId;
}
printk("major = %d, minor = %d\r\n", newchrled.major, newchrled.minor);
/* 3.注册字符设备 */
newchrled.cDev.owner = THIS_MODULE;
cdev_init(&newchrled.cDev, &newchrled_fops); /* cdev初始化 */
ret = cdev_add(&newchrled.cDev, newchrled.devId, NEWCHRDEV_COUNT);
if(ret < 0){
goto fail_cdev;
}
printk("register chrdev success \r\n");
/* 4.自动创建设备节点 */
newchrled.class = class_create(THIS_MODULE, NEWCHRDEV_NAME);
if(IS_ERR(newchrled.class)){
ret = PTR_ERR(newchrled.class);
goto fail_class;
}
printk("create class success \r\n");
/* 5. 创建之后要创建设备*/
/* 下面函数
* 第一个参数代表本类,第二个参数代表父类
* 第三个参数是dev_t
* 第四个参数是设备可能会使用一些数据,一般null
* 第五个参数是fint,=xxx 会创建/dev/xxx
* */
newchrled.device = device_create(newchrled.class,
NULL, newchrled.devId, NULL,
NEWCHRDEV_NAME);
if(IS_ERR(newchrled.device)){
ret = PTR_ERR(newchrled.device);
goto fail_device;
}
printk("create device success \r\n");
printk("module init... \r\n");
return 0;
fail_device:
class_destroy(newchrled.class); /* 这块出问题了,表示前面都没出问题, 所以把前面释放t */
fail_class:
cdev_del(&newchrled.cDev);
fail_cdev:
unregister_chrdev_region(newchrled.devId, NEWCHRDEV_COUNT);
fail_devId:
return ret;
}
/* 驱动出口 */
static void __exit newchrled_exit(void)
{
/* 1.删除字符设备 */
cdev_del(&newchrled.cDev);
printk("delete chrdev success \r\n");
/* 2.注销设备号 */
unregister_chrdev_region(newchrled.devId, NEWCHRDEV_COUNT);
printk("unregister chrdev success \r\n");
/* 3.先摧毁设备再摧毁类 */
device_destroy(newchrled.class, newchrled.devId);
printk("device destroy success \r\n");
/* 4.摧毁类 */
class_destroy(newchrled.class);
printk("class destroy success \r\n");
printk("module exit \r\n");
}
/* 注册和卸载驱动 */
module_init(newchrled_init);
module_exit(newchrled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shao Zheming");