From affc0c1841beacd8430af689f7f12dcab0cbaf28 Mon Sep 17 00:00:00 2001 From: Philip Avinash Date: Tue, 11 Jun 2013 11:07:00 +0530 Subject: [PATCH] gpio_test: Module for testing GPIO interrupt Build proedure: 1. Copy Examples folder to Linux source 2. change directory to Examples/gpio 3. build gpio test module using make all Test procedure 4. insert kernel module and generate interrupt. Signed-off-by: Philip Avinash --- Examples/gpio/Makefile | 16 ++++++ Examples/gpio/gpio_test.c | 111 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100755 Examples/gpio/Makefile create mode 100644 Examples/gpio/gpio_test.c diff --git a/Examples/gpio/Makefile b/Examples/gpio/Makefile new file mode 100755 index 00000000000..5e85156a287 --- /dev/null +++ b/Examples/gpio/Makefile @@ -0,0 +1,16 @@ +obj-m += gpio_test.o +PWD := $(shell pwd) +KERNEL_DIR := $(PWD)/../../ + +#INSTALL_DIR=$(FINAL_DEST)/gpio + +# Set KERNEL_DIR to your local kernel location + +all: + $(MAKE) -C $(KERNEL_DIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-arago-linux-gnueabi- + +install: + mkdir -p $(INSTALL_DIR) + cp gpio_test.ko $(INSTALL_DIR) +clean: + rm -rf *.o *.ko *.mod.c .*.cmd .tmp_versions *.order *.symvers diff --git a/Examples/gpio/gpio_test.c b/Examples/gpio/gpio_test.c new file mode 100644 index 00000000000..01c3b9ca1e8 --- /dev/null +++ b/Examples/gpio/gpio_test.c @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2009 Texas Instruments Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option)any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static int gpio_num; +static int gpio_pin; + +DECLARE_COMPLETION(work); + +static irqreturn_t handler (int irq, void * dev) +{ + complete_all(&work); + return IRQ_HANDLED; +} + + +int init_module() +{ + int status; + + init_completion(&work); + + if(cpu_is_davinci_da830()) { + gpio_num = 87; /* gpio_num = (bank_num * 16) + pin_num */ + gpio_pin = DA830_GPIO5_7; + } else { + gpio_num = 116; + gpio_pin = DA850_GPIO7_4; + } + + if (cpu_is_davinci_da830()) + printk("\nTesting gpio %d (connected to boot pin S2-7)\n", + gpio_num); + else + printk("\nTesting gpio %d (connected to boot pin S7-8)\n", + gpio_num); + + /* init/set pinmux */ + status = davinci_cfg_reg(gpio_pin); + if (status < 0) { + printk("pin could not be muxed for GPIO functionality %d\n", + gpio_num); + return status; + } + + status = gpio_request(gpio_num, "gpio_test\n"); + if (status < 0) { + printk("ERROR can not open GPIO %d\n", gpio_num); + return status; + } + + gpio_direction_input(gpio_num); + + if (cpu_is_davinci_da830()) + printk("The current state of S2-7 pin is "); + else + printk("The current state of S7-8 pin is "); + if(gpio_get_value(gpio_num) == 0) + printk("OFF. \n\tWaiting for the pin to be on..\n"); + else + printk("ON. \n\tWaiting for the pin to be off..\n"); + + status = request_irq(gpio_to_irq(gpio_num), handler, 0, "gpio_test", NULL); + if(status < 0) { + printk(KERN_ERR "error %d requesting GPIO IRQ %d\n", status, gpio_num); + return status; + } + + irq_set_irq_type(gpio_to_irq(gpio_num), IRQ_TYPE_EDGE_RISING); + + wait_for_completion_interruptible(&work); + + printk(".. done\n"); + + return 0; +} + +void cleanup_module(void) { + + gpio_free(gpio_num); + + free_irq(gpio_to_irq(gpio_num), NULL); +} + +MODULE_LICENSE("GPL");