1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| #include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/libata.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 <linux/of.h> #include <linux/of_address.h> #include <linux/of_gpio.h> #include <asm/mach/map.h> #include <asm/uaccess.h> #include <asm/io.h> #include <linux/semaphore.h> #include <linux/timer.h> #include <linux/of_irq.h> #include <linux/irq.h>
#define IMX6UIRQ_CNT 1 #define IMX6UIRQ_NAME "imx6uirq" #define KEY0VALUE 0x01 #define INVAKEY 0xFF #define KEY_NUM 1
struct irq_keydesc { int gpio; int irqnum; unsigned char value; char name[10]; irqreturn_t (*handler)(int, void *); };
struct irq_led { int gpio; };
struct imx6uirq_dev { dev_t devid; struct cdev cdev; struct class *class; struct device *device; int major; int minor; struct device_node *nd; atomic_t keyvalue; atomic_t releasekey; struct timer_list timer; struct irq_keydesc irqkeydesc; struct irq_led irqled; };
struct imx6uirq_dev imx6uirq;
static irqreturn_t key0_handler(int irq, void *dev_id) { struct imx6uirq_dev *dev = (struct imx6uirq_dev *)dev_id;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10)); return IRQ_RETVAL(IRQ_HANDLED); }
void timer_function(struct timer_list *t) { unsigned char value; struct irq_keydesc *keydesc; struct imx6uirq_dev *dev = from_timer(dev, t, timer);
keydesc = &dev->irqkeydesc;
value = gpio_get_value(keydesc->gpio); if (value == 0){ int sta = gpio_get_value(dev->irqled.gpio); gpio_set_value(dev->irqled.gpio, !sta); atomic_set(&dev->keyvalue, keydesc->value); }else{ atomic_set(&dev->keyvalue, 0x80 | keydesc->value); atomic_set(&dev->releasekey, 1); } }
static int keyio_init(void) { int ret = 0;
imx6uirq.nd = of_find_node_by_path("/key"); if (imx6uirq.nd == NULL){ printk("find node failed\r\n"); return -EINVAL; }
imx6uirq.irqkeydesc.gpio = of_get_named_gpio(imx6uirq.nd, "key-gpio", 0); if (imx6uirq.irqkeydesc.gpio < 0){ printk("unable get key gpio\r\n"); return -EINVAL; }
imx6uirq.irqled.gpio = of_get_named_gpio(imx6uirq.nd, "led-gpio", 0); if (imx6uirq.irqled.gpio < 0){ printk("unable get led gpio\r\n"); return -EINVAL; }
sprintf(imx6uirq.irqkeydesc.name, "KEY0"); gpio_request(imx6uirq.irqkeydesc.gpio, imx6uirq.irqkeydesc.name); ret = gpio_direction_input(imx6uirq.irqkeydesc.gpio); if (ret < 0){ printk("unable set key pin level\r\n"); return ret; } imx6uirq.irqkeydesc.irqnum = irq_of_parse_and_map(imx6uirq.nd, 0); printk("key0: gpio = %d, irqnum = %d\r\n", imx6uirq.irqkeydesc.gpio, imx6uirq.irqkeydesc.irqnum);
gpio_request(imx6uirq.irqled.gpio, "LED"); ret = gpio_direction_output(imx6uirq.irqled.gpio, 1); if (ret < 0){ printk("unable set led pin level\r\n"); return ret; }
imx6uirq.irqkeydesc.handler = key0_handler; imx6uirq.irqkeydesc.value = KEY0VALUE;
ret = request_irq(imx6uirq.irqkeydesc.irqnum, imx6uirq.irqkeydesc.handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, imx6uirq.irqkeydesc.name, &imx6uirq);
if (ret < 0){ printk("IRQ request failed, error code: %d\r\n", ret); }
timer_setup(&imx6uirq.timer, timer_function, 0);
return 0; }
static int imx6uirq_open(struct inode *inode, struct file *filp) { filp->private_data = &imx6uirq; return 0; }
static ssize_t imx6uirq_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt) { int ret = 0; unsigned char keyvalue = 0; unsigned char releasekey = 0; struct imx6uirq_dev *dev = (struct imx6uirq_dev *)filp->private_data;
keyvalue = atomic_read(&dev->keyvalue); releasekey = atomic_read(&dev->releasekey);
if (releasekey){ if (keyvalue & 0x80){ keyvalue &= ~(0x80); ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue)); }else{ goto data_error; } atomic_set(&dev->releasekey, 0); }else{ goto data_error; } return 0;
data_error: return -EINVAL; }
static struct file_operations imx6uirq_fops = { .owner = THIS_MODULE, .open = imx6uirq_open, .read = imx6uirq_read, };
static int __init imx6uirq_init(void) { if (imx6uirq.major){ imx6uirq.devid = MKDEV(imx6uirq.major, 0); register_chrdev_region(imx6uirq.devid, IMX6UIRQ_CNT, IMX6UIRQ_NAME); }else{ alloc_chrdev_region(&imx6uirq.devid, 0, IMX6UIRQ_CNT, IMX6UIRQ_NAME); imx6uirq.major = MAJOR(imx6uirq.devid); imx6uirq.minor = MINOR(imx6uirq.devid); } printk("KEY reg ok, major = %d, minor = %d\r\n", imx6uirq.major, imx6uirq.minor);
cdev_init(&imx6uirq.cdev, &imx6uirq_fops); cdev_add(&imx6uirq.cdev, imx6uirq.devid, IMX6UIRQ_CNT);
imx6uirq.class = class_create(IMX6UIRQ_NAME); if (IS_ERR(imx6uirq.class)){ return PTR_ERR(imx6uirq.class); }
imx6uirq.device = device_create(imx6uirq.class, NULL, imx6uirq.devid, NULL, IMX6UIRQ_NAME); if (IS_ERR(imx6uirq.device)){ return PTR_ERR(imx6uirq.device); }
atomic_set(&imx6uirq.keyvalue, INVAKEY); atomic_set(&imx6uirq.releasekey, 0); keyio_init();
return 0; }
static void __exit imx6uirq_exit(void) { del_timer_sync(&imx6uirq.timer);
free_irq(imx6uirq.irqkeydesc.irqnum, &imx6uirq); gpio_free(imx6uirq.irqkeydesc.gpio);
cdev_del(&imx6uirq.cdev); unregister_chrdev_region(imx6uirq.devid, IMX6UIRQ_CNT); device_destroy(imx6uirq.class, imx6uirq.devid); class_destroy(imx6uirq.class); }
module_init(imx6uirq_init); module_exit(imx6uirq_exit); MODULE_LICENSE("GPL");
|