设备树添加
49 myplatform {
50 compatible="song.yj,myplatform";
51 reg=<0x12345678 0x14>;
52 interrupt-parent=<&gpiof>;
53 interrupts=<9 0>,<7 0>,<8 0>;
54 led1=<&gpioe 10 0>;
55 led2=<&gpiof 10 0>;
56 led3=<&gpioe 8 0>;
57 };
mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include<linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
char kbuf[128]="";
int minor=0;
#if 0
unsigned int major=0;
#else
unsigned int major=500;
#endif
dev_t devno;
struct cdev *cdev;
struct class *cls;
struct device *dev;
const int count=3;
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
struct gpio_desc *gpiono_1,*gpiono_2,*gpiono_3;
unsigned int irqno[3];
irqreturn_t myirq_handler(int irqno,void *dev){
int which = (int )dev;
switch(which){
case 0:
printk("key 1 pressed:%d\n",irqno);
gpiod_set_value(gpiono_1,!gpiod_get_value(gpiono_1));
break;
case 1:
printk("key 2 pressed:%d\n",irqno);
gpiod_set_value(gpiono_2,!gpiod_get_value(gpiono_2));
break;
case 2:
printk("key 3 pressed:%d\n",irqno);
gpiod_set_value(gpiono_3,!gpiod_get_value(gpiono_3));
break;
}
return IRQ_HANDLED;
}
int mycdev_open(struct inode *inode,struct file *file){
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
int mycdev_close(struct inode *inode,struct file *file){
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof){
unsigned long ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which,ret;
ret=copy_from_user(&which,(void *)arg,sizeof(int));
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(cmd)
{
case LED_ON:
switch(which)
{
case 1:
gpiod_set_value(gpiono_1,1);
break;
case 2:
gpiod_set_value(gpiono_2,1);
break;
case 3:
gpiod_set_value(gpiono_3,1);
break;
}
break;
case LED_OFF:
switch(which)
{
case 1:
gpiod_set_value(gpiono_1,0);
break;
case 2:
gpiod_set_value(gpiono_2,0);
break;
case 3:
gpiod_set_value(gpiono_3,0);
break;
}
break;
}
return 0;
}
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
.unlocked_ioctl=mycdev_ioctl,
};
int pdrv_probe(struct platform_device *pdev){
int ret=0,i;
cdev = cdev_alloc();
if(cdev == NULL){
ret = -ENOMEM;
goto OUT1;
}
printk("字符设备对象实例化成功\n");
cdev_init(cdev,&fops);
if(major==0)
{
ret=alloc_chrdev_region(&devno,minor,3,"mycdev");
if(ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major=MAJOR(devno);
minor=MINOR(devno);
}
else
{
devno=MKDEV(major,0);
ret=register_chrdev_region(devno,3,"mycdev");
if(ret)
{
printk("静态设备号失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
ret=cdev_add(cdev,devno,3);
if(ret)
{
printk("注册字符设备驱动失败\n");
goto OUT3;
}
printk("注册字符设备驱动成功\n");
cls=class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
for(i=0;i<count;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mychrdev%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备信息失败\n");
ret=-PTR_ERR(dev);
goto OUT5;
}
}
printk("自动创建设备节点成功\n");
gpiono_1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono_1))
{
printk("GPIO led1信息解析失败\n");
ret = -PTR_ERR(gpiono_1);
goto OUT5;
}
gpiono_2=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono_2))
{
printk("GPIO led2信息解析失败\n");
ret = -PTR_ERR(gpiono_2);
goto OUT5;
}
gpiono_3=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono_3))
{
printk("GPIO led3信息解析失败\n");
ret = -PTR_ERR(gpiono_3);
goto OUT5;
}
gpiod_set_value(gpiono_1,1);
gpiod_set_value(gpiono_2,1);
gpiod_set_value(gpiono_3,1);
printk("led1/2/3 点亮成功\n");
for(i=0;i<3;i++){
irqno[i] = irq_of_parse_and_map(pdev->dev.of_node,i);
if(!irqno[i]){
printk("软中断号解析失败\n");
ret = -EPERM;
goto OUT5;
}
ret = request_irq(irqno[i],myirq_handler,IRQF_TRIGGER_FALLING,"key",(void *)i);
if(ret<0){
printk("中断注册失败\n");
ret = -EPERM;
goto OUT5;
}
}
printk("中断1/2/3 注册成功\n");
return 0;
OUT5:
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(devno,3);
OUT2:
kfree(cdev);
OUT1:
return ret;
}
int pdrv_remove(struct platform_device *pdev){
int i;
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major,minor),count);
kfree(cdev);
gpiod_set_value(gpiono_1,0);
gpiod_set_value(gpiono_2,0);
gpiod_set_value(gpiono_3,0);
gpiod_put(gpiono_1);
gpiod_put(gpiono_2);
gpiod_put(gpiono_3);
for(i=0;i<3;i++){
free_irq(irqno[i],(void *)i);
}
return 0;
}
struct of_device_id oftable[]={
{.compatible="song.yj,myplatform"},
{.compatible="song.yj,myplatform1"},
{},
};
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="mychrdev",
.of_match_table=oftable,
}
};
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
main.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
//定义功能码
#define LED_ON _IOW('l',1,int)//开灯
#define LED_OFF _IOW('l',0,int)//关灯
int main() {
char buf[128]={0};
int fd=open("/dev/mychrdev0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
int a,b;
while(1)
{
printf("请输入要实现的功能:1(开灯) 0(关灯)>");
scanf("%d",&a);
if(a==1)//开灯
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d",&b);
ioctl(fd,LED_ON,&b);
}
else //关灯
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d",&b);
ioctl(fd,LED_OFF,&b);
}
}
close(fd);
return 0;
}
mychrdev.sh
#!/bin/bash
make clean
make arch=arm modname=mychrdev
cp mychrdev.ko ~/nfs/rootfs/
arm-linux-gnueabihf-gcc main.c
cp a.out ~/nfs/rootfs/