#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 <asm/uaccess.h>
#include <asm/io.h>
#define DEV_CNT 1 /* 设备号个数 */
#define DEV_NAME "gpiocontrol" /* 名字 */
/* 寄存器物理地址 */
#define AP_PORTA_DATA_REG (0x04450000)
#define AP_PORTA_DIR_REG (0x04450004)
#define AP_PORTB_DATA_REG (0x0445000c)
#define AP_PORTB_DIR_REG (0x04450010)
#define CP_PORTA_DATA_REG (0x04d10000)
#define CP_PORTA_DIR_REG (0x04d10004)
#define CP_PORTB_DATA_REG (0x04d1000c)
#define CP_PORTB_DIR_REG (0x04d10010)
/* 映射后的寄存器虚拟地址指针 */
static void __iomem *VADD_AP_PORTA_DATA_REG;
static void __iomem *VADD_AP_PORTA_DIR_REG;
static void __iomem *VADD_AP_PORTB_DATA_REG;
static void __iomem *VADD_AP_PORTB_DIR_REG;
static void __iomem *VADD_CP_PORTA_DATA_REG;
static void __iomem *VADD_CP_PORTA_DIR_REG;
static void __iomem *VADD_CP_PORTB_DATA_REG;
static void __iomem *VADD_CP_PORTB_DIR_REG;
/* newchrled设备结构体 */
struct gpio_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
};
struct gpio_dev gpio_dev_data; /* gpio设备 */
static int gpio_open(struct inode *inode, struct file *filp)
{
filp->private_data = &gpio_dev_data; /* 设置私有数据 */
return 0;
}
/*
* @description : 从设备读取数据
* @param - filp : 要打开的设备文件(文件描述符)
* @param - buf : 返回给用户空间的数据缓冲区
* @param - cnt : 要读取的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 读取的字节数,如果为负值,表示读取失败
*/
static ssize_t gpio_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
/*
* @description : 向设备写数据
* @param - filp : 设备文件,表示打开的文件描述符
* @param - buf : 要写给设备写入的数据
* @param - cnt : 要写入的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 写入的字节数,如果为负值,表示写入失败
*/
static ssize_t gpio_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[3];
u32 val = 0;
retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}
switch(databuf[0]) //下标0存储寄存器标号,下标1存储设置哪个bit位(0~31) , 下标2 存储值 0或者1 对于方向寄存器1是输出 0是输入,对于值1高0低
{
case 0:
val = readl(VADD_AP_PORTA_DATA_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_AP_PORTA_DATA_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_AP_PORTA_DATA_REG);
}
break ;
case 1:
val = readl(VADD_AP_PORTA_DIR_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_AP_PORTA_DIR_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_AP_PORTA_DIR_REG);
}
break ;
case 2:
val = readl(VADD_AP_PORTB_DATA_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_AP_PORTB_DATA_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_AP_PORTB_DATA_REG);
}
break ;
case 3:
val = readl(VADD_AP_PORTB_DIR_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_AP_PORTB_DIR_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_AP_PORTB_DIR_REG);
}
break ;
case 4:
val = readl(VADD_CP_PORTA_DATA_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_CP_PORTA_DATA_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_CP_PORTA_DATA_REG);
}
break ;
case 5:
val = readl(VADD_CP_PORTA_DIR_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_CP_PORTA_DIR_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_CP_PORTA_DIR_REG);
}
break ;
case 6:
val = readl(VADD_CP_PORTB_DATA_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_CP_PORTB_DATA_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_CP_PORTB_DATA_REG);
}
break ;
case 7:
val = readl(VADD_CP_PORTB_DIR_REG );
if(databuf[2] == 0)
{
val &= ~(1 << databuf[1]);
writel(val, VADD_CP_PORTB_DIR_REG);
}else if(databuf[2] == 1)
{
val |= (1 << databuf[1]);
writel(val, VADD_CP_PORTB_DIR_REG);
}
break ;
default:
break;
}
return 0;
}
/*
* @description : 关闭/释放设备
* @param - filp : 要关闭的设备文件(文件描述符)
* @return : 0 成功;其他 失败
*/
static int gpio_release(struct inode *inode, struct file *filp)
{
return 0;
}
/* 设备操作函数 */
static struct file_operations gpio_dev_fops = {
.owner = THIS_MODULE,
.open = gpio_open,
.read = gpio_read,
.write = gpio_write,
.release = gpio_release,
};
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static int __init gpio_init(void)
{
/* 1、寄存器地址映射 */
VADD_AP_PORTA_DATA_REG = iorema