demo.c
#include <stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,const char * argv[])
{
char buf[128]={0};
int fd=open("/dev/mychrdev",O_RDWR);
if(fd<0){
printf("打开设备文件失败\n");
}
while(1){
printf("请输入两个字符\n");
printf("第一个字符:1(LED1) 2(LED2) 3(LED3)\n");
printf("输入第二个字符:0(关灯) 1(开灯)\n");
printf("请输入>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}
pdir.c
#include <linux/init.h>
#include <linux/module.h>
#include<linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include<linux/of_gpio.h>
#include<linux/of.h>
#include<linux/uaccess.h>
char kbuf[128];
struct resource *res;
unsigned int irqno;
int major;
int gpiono,gpiono1,gpiono2;
int mycdev_open (struct inode *inode, struct file *file)
{
return 0;
}
ssize_t mycdev_read (struct file *file, char __user *user, size_t size, loff_t *lof)
{
return 0;
}
ssize_t mycdev_write (struct file *file, const char __user *user, size_t size, loff_t *lof)
{
unsigned long ret;
if(size>sizeof(kbuf)){
size=sizeof(kbuf);
}
ret=copy_from_user(kbuf,user,size);
if(ret){
printk("copy_to_user filed\n");
return ret;
}
switch(kbuf[0]){
case '1':
if(kbuf[1]=='0'){
gpio_set_value(gpiono,0);
}
else{
gpio_set_value(gpiono,1);
}
break;
case '2':
if(kbuf[1]=='0'){
gpio_set_value(gpiono1,0);
}
else{
gpio_set_value(gpiono1,1);
}
break;
case '3':
if(kbuf[1]=='0'){
gpio_set_value(gpiono2,0);
}
else{
gpio_set_value(gpiono2,1);
}
break;
}
return 0;
}
int mycdev_close (struct inode *inode, struct file *file)
{
return 0;
}
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
int pdrv_probe(struct platform_device*pdev)
{
major=register_chrdev(0,"mychrdev",&fops);
if(major<0){
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功%d\n",major);
gpiono=of_get_named_gpio(pdev->dev.of_node,"led1",0);
if(gpiono<0){
printk("解析GPIO编号失败\n");
return gpiono;
}
printk("获取GPIO编号成功%d\n",gpiono);
gpiono1=of_get_named_gpio(pdev->dev.of_node,"led2",0);
if(gpiono1<0){
printk("解析GPIO编号失败\n");
return gpiono1;
}
printk("获取GPIO编号成功%d\n",gpiono1);
gpiono2=of_get_named_gpio(pdev->dev.of_node,"led3",0);
if(gpiono2<0){
printk("解析GPIO编号失败\n");
return gpiono2;
}
printk("获取GPIO编号成功%d\t %d\t%d\n",gpiono,gpiono1,gpiono2);
gpio_request(gpiono,NULL);
gpio_request(gpiono1,NULL);
gpio_request(gpiono2,NULL);
gpio_direction_output(gpiono,0);
gpio_direction_output(gpiono1,0);
gpio_direction_output(gpiono2,0);
gpio_set_value(gpiono,0);
gpio_set_value(gpiono1,0);
gpio_set_value(gpiono2,0);
return 0;
}
int pdrv_remove(struct platform_device*pdev)
{
gpio_set_value(gpiono,0);
gpio_set_value(gpiono1,0);
gpio_set_value(gpiono2,0);
gpio_free(gpiono);
gpio_free(gpiono1);
gpio_free(gpiono2);
unregister_chrdev(major,"mychrdev");
return 0;
}
struct of_device_id oftable[]={
{.compatible="hqyj,myplatform"},
{.compatible="hqyj,myplatform1"},
{.compatible="hqyj,myplatform2"},
{},
};
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable,
},
};
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");