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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| #include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/ide.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 <linux/semaphore.h> #include <linux/timer.h> #include <linux/of_irq.h> #include <linux/irq.h> #include <linux/fcntl.h> #include <linux/wait.h> #include <linux/poll.h> #include <asm/mach/map.h> #include <asm/uaccess.h> #include <asm/io.h>
#define im6uirq_CNT 1 #define im6uirq_NAME "imx6uirq"
#define KEY_NUM 1 #define KEY0VALUE 0x01 #define INVAKEY 0XFF
struct irq_key_dev { int gpio; int irqnum; unsigned char value; char name[10];
irqreturn_t (*handler)(int,void *);
struct tasklet_struct tasklet; };
struct im6uirq_dev { dev_t devid; int major; int minor;
struct cdev cdev;
struct class *class; struct device *device; struct device_node *nd;
struct irq_key_dev irq_key[KEY_NUM];
struct timer_list timer;
atomic_t keyvalue; atomic_t releasekey;
struct fasync_struct *fasync_queue;
};
struct im6uirq_dev im6uirq;
static int im6uirq_open(struct inode *inode, struct file *filp) { filp->private_data = &im6uirq; return 0; }
static int im6uirq_fasync(int fd, struct file *filp, int on) { struct im6uirq_dev *dev = filp->private_data; return fasync_helper(fd, filp, on, &dev->fasync_queue); } static int im6uirq_release(struct inode *inode,struct file *filp) {
return im6uirq_fasync(-1, filp, 0); }
static ssize_t im6uirq_write(struct file *filp,const char __user *buf,size_t count,loff_t *offt) { return 0; }
static ssize_t im6uirq_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos) { int ret; unsigned char keyvalue; unsigned char releasekey; struct im6uirq_dev *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 ret; data_error: return -EINVAL;
}
static const struct file_operations im6uirq_fops = { .owner = THIS_MODULE, .open = im6uirq_open, .release = im6uirq_release, .write = im6uirq_write, .read = im6uirq_read, .fasync = im6uirq_fasync, };
static irqreturn_t key0_irq_handler(int irq,void *dev_id) { struct im6uirq_dev *dev = dev_id;
tasklet_schedule(&dev->irq_key[0].tasklet); return IRQ_HANDLED; }
static void timer_func(unsigned long arg) { int value = 0; struct im6uirq_dev *dev = (struct im6uirq_dev *)arg;
value = gpio_get_value(dev->irq_key[0].gpio);
if (value == 0) { atomic_set(&dev->keyvalue, dev->irq_key[0].value); }else if (value == 1) { atomic_set(&dev->keyvalue, 0x80 | dev->irq_key[0].value); atomic_set(&dev->releasekey,1); }
if(atomic_read(&dev->releasekey)) { if (dev->fasync_queue) kill_fasync(&dev->fasync_queue, SIGIO, POLL_IN); } }
static void key_tasklet(unsigned long data) { struct im6uirq_dev *dev = (struct im6uirq_dev *)data;
dev->timer.data = (volatile unsigned long)data; mod_timer(&dev->timer, jiffies + msecs_to_jiffies(20)); }
static int keyio_init(struct im6uirq_dev *dev) { int ret; int i; dev->nd = of_find_node_by_path("/key"); if (dev->nd == NULL) { printk("fail find nd\r\n"); ret = -EINVAL; goto fail_nd; }
for (i = 0; i < KEY_NUM; i++) { ret = dev->irq_key[i].gpio = of_get_named_gpio(dev->nd,"key-gpios",i); if (ret < 0){ printk("can't get key%d\r\n",i); goto fail_get_gpio; } }
for (i = 0; i < KEY_NUM; i++) { memset(dev->irq_key[i].name,0,sizeof(dev->irq_key[i].name)); sprintf(dev->irq_key[i].name,"key%d",i); gpio_request(dev->irq_key[i].gpio,dev->irq_key[i].name); gpio_direction_input(dev->irq_key[i].gpio);
dev->irq_key[i].irqnum = gpio_to_irq(dev->irq_key[i].gpio); #if 0 dev->irq_key[i].irqnum = irq_of_parse_and_map(dev->nd,i); #endif }
dev->irq_key[0].handler = key0_irq_handler; dev->irq_key[0].value = KEY0VALUE; for(i = 0; i < KEY_NUM; i++) { ret = request_irq(dev->irq_key[i].irqnum,dev->irq_key[i].handler, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, dev->irq_key[i].name,&im6uirq);
if (ret) { printk("request %d irq failed\r\n",dev->irq_key[i].irqnum); ret = -EINVAL; goto fail_requese_irq; } tasklet_init(&dev->irq_key[0].tasklet,key_tasklet,(unsigned long)dev); }
init_timer(&dev->timer); dev->timer.function = timer_func; return 0; fail_requese_irq: for (i = 0; i < KEY_NUM; i++){ gpio_free(dev->irq_key[i].gpio); } fail_get_gpio: fail_nd: return ret; }
static int __init im6uirq_init(void) { int ret;
im6uirq.major = 0; if (im6uirq.major) { im6uirq.devid = MKDEV(im6uirq.major, 0); register_chrdev_region(im6uirq.devid,im6uirq_CNT,im6uirq_NAME); } else{ ret = alloc_chrdev_region(&im6uirq.devid,0,im6uirq_CNT,im6uirq_NAME); im6uirq.major = MAJOR(im6uirq.devid); im6uirq.minor = MINOR(im6uirq.devid); } printk("major = %d,minor = %d\r\n",im6uirq.major,im6uirq.minor);
if(ret < 0) { printk("fail devid\r\n"); goto fail_devid; }
im6uirq.cdev.owner = THIS_MODULE; cdev_init(&im6uirq.cdev,&im6uirq_fops);
ret = cdev_add(&im6uirq.cdev,im6uirq.devid,im6uirq_CNT); if (ret < 0) { printk("fail cdevadd\r\n"); goto fail_cdevadd; }
im6uirq.class = class_create(THIS_MODULE,im6uirq_NAME); if (IS_ERR(im6uirq.class)) { printk("fail class\r\n"); ret = PTR_ERR(im6uirq.class); goto fail_class; }
im6uirq.device = device_create(im6uirq.class,NULL,im6uirq.devid,NULL,im6uirq_NAME); if (IS_ERR(im6uirq.device)) { printk("fail device\r\n"); ret = PTR_ERR(im6uirq.device); goto fail_device; }
ret = keyio_init(&im6uirq); if (ret) { printk("fail keyio_init\r\n"); goto fail_keyio_init; }
atomic_set(&im6uirq.keyvalue,INVAKEY); atomic_set(&im6uirq.releasekey,0);
printk("all init suggess!!!\r\n");
return 0;
fail_keyio_init:
fail_device: class_destroy(im6uirq.class); fail_class: cdev_del(&im6uirq.cdev); fail_cdevadd: unregister_chrdev_region(im6uirq.devid,im6uirq_CNT); fail_devid: return ret; }
static void __exit im6uirq_exit(void) { int i = 0; for (i = 0; i < KEY_NUM; i++) { free_irq(im6uirq.irq_key[i].irqnum,&im6uirq); } for (i = 0; i < KEY_NUM; i++){ gpio_free(im6uirq.irq_key[i].gpio); }
del_timer_sync(&im6uirq.timer);
cdev_del(&im6uirq.cdev); unregister_chrdev_region(im6uirq.devid,im6uirq_CNT); device_destroy(im6uirq.class,im6uirq.devid); class_destroy(im6uirq.class);
printk("im6uirq exit!\r\n"); }
module_init(im6uirq_init); module_exit(im6uirq_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("zwl");
|