当前位置: 代码迷 >> 驱动开发 >> 求教linux下的gpio输入驱动的一点有关问题
  详细解决方案

求教linux下的gpio输入驱动的一点有关问题

热度:96   发布时间:2016-04-28 11:07:49.0
求教linux下的gpio输入驱动的一点问题!
大家好,最近遇到驱动的小问题,到现在都没有能解决,希望大家帮帮忙!
想实现一个gpio口的很简单的输入,基于am3517的开发板,不知道为什么就是不行!先将代码贴上,
驱动程序代码:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/i2c/tsc2004.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/nand.h>
#include <linux/input.h>
#include <linux/tca6416_keypad.h>
#include <linux/davinci_emac.h>
#include <linux/i2c/pca953x.h>
#include <linux/regulator/machine.h>
#include <linux/can/platform/ti_hecc.h>
#include <linux/usb/android_composite.h>

#include <mach/hardware.h>
#include <mach/am35xx.h>
#include <asm/mach-types.h>
#include <asm/mach/arch.h>
#include <asm/mach/map.h>

#include <plat/board.h>
#include <plat/common.h>
#include <plat/control.h>
#include <plat/usb.h>
#include <plat/display.h>
#include <plat/gpmc.h>
#include <plat/nand.h>

#include <media/tvp514x.h>
#include <media/ti-media/vpfe_capture.h>


#define LED_GPIO_1 175


static int major;
static struct class *tel_dev_class;


static int tel_dev_open(struct inode *inode, struct file *file)
{

printk("open ok!\n");
return 0;
}


ssize_t tel_dev_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
char ker_buf[1];

if (size != 1)
return -EINVAL;

ker_buf[0] =gpio_get_value(LED_GPIO_1);

if (copy_to_user(buf, ker_buf, 2))
  return -EFAULT;

printk("read ok!\n");

return 1;
}

static const struct file_operations tel_dev_fops = {
.owner = THIS_MODULE,
.read = tel_dev_read,
.open = tel_dev_open,

};

int tel_dev_init(void)
{
major = register_chrdev(0, "tel_dev", &tel_dev_fops);
 
/* sysfs */
tel_dev_class = class_create(THIS_MODULE, "tel_dev_class");
device_create(tel_dev_class, NULL, MKDEV(major, 0), NULL, "tel_dev");
int ret;

ret = gpio_request(LED_GPIO_1, "gpio_input");
if(ret < 0)
{
  printk("GPIO1 request failed\n");
  gpio_free(LED_GPIO_1);
  return ret;
}
  if (gpio_direction_input(LED_GPIO_1)) {
printk(KERN_WARNING "GPIO#%d cannot be configured as "
"input\n", LED_GPIO_1);
return -ENXIO;
}

printk("success!\n");  
return 0;
}

void tel_dev_exit(void)
{
unregister_chrdev(major, "tel_dev");
device_destroy(tel_dev_class, MKDEV(major, 0));
class_destroy(tel_dev_class);
gpio_free(LED_GPIO_1);


}

module_init(tel_dev_init);
module_exit(tel_dev_exit);

MODULE_LICENSE("GPL");

测试代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void print_usage(char *str)
{
printf("Usage:\n");
printf("%s r : read status\n", str);
}
 
int main(int argc, char **argv)
{
int fd;
char buf[1];
int ret;

if (argc != 2)
{
print_usage(argv[0]);
return 0;
}

fd = open("/dev/tel_dev", O_RDWR);
if (fd < 0)
{
printf("can't open /dev/tel_dev\n");
goto error;
}


if (strcmp(argv[1], "r") == 0)