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
| #include <linux/module.h> #include <linux/poll.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/miscdevice.h> #include <linux/kernel.h> #include <linux/major.h> #include <linux/mutex.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/stat.h> #include <linux/init.h> #include <linux/device.h> #include <linux/tty.h> #include <linux/kmod.h> #include <linux/gfp.h> #include <linux/gpio/consumer.h> #include <linux/platform_device.h> #include <linux/of_gpio.h> #include <linux/of_irq.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/slab.h> #include <linux/fcntl.h> #include <linux/timer.h> #include <linux/workqueue.h> #include <asm/current.h>
struct gpio_key{ int gpio; struct gpio_desc *gpiod; int flag; int irq; struct timer_list key_timer; struct tasklet_struct tasklet; struct work_struct work; } ; static struct gpio_key *gpio_keys_100ask; static int major = 0; static struct class *gpio_key_class;
#define BUF_LEN 128 static int g_keys[BUF_LEN]; static int r, w;
struct fasync_struct *button_fasync; #define NEXT_POS(x) ((x+1) % BUF_LEN) static int is_key_buf_empty(void){ return (r == w); }
static int is_key_buf_full(void){ return (r == NEXT_POS(w)); } static void put_key(int key){ if (!is_key_buf_full()){ g_keys[w] = key; w = NEXT_POS(w); } } static int get_key(void){ int key = 0; if (!is_key_buf_empty()){ key = g_keys[r]; r = NEXT_POS(r); } return key; }
static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait); static void key_timer_expire(unsigned long data){ struct gpio_key *gpio_key = data; int val; int key; val = gpiod_get_value(gpio_key->gpiod);
printk("key_timer_expire key %d %d\n", gpio_key->gpio, val); key = (gpio_key->gpio << 8) | val; put_key(key); wake_up_interruptible(&gpio_key_wait); kill_fasync(&button_fasync, SIGIO, POLL_IN); }
static void key_tasklet_func(unsigned long data){ struct gpio_key *gpio_key = data; int val; int key;
val = gpiod_get_value(gpio_key->gpiod); printk("key_tasklet_func key %d %d\n", gpio_key->gpio, val); } static void key_work_func(struct work_struct *work){ struct gpio_key *gpio_key = container_of(work, struct gpio_key, work); int val;
val = gpiod_get_value(gpio_key->gpiod); printk("key_work_func: the process is %s pid %d\n",current->comm, current->pid); printk("key_work_func key %d %d\n", gpio_key->gpio, val); } static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset){ int err; int key; if (is_key_buf_empty() && (file->f_flags & O_NONBLOCK)) return -EAGAIN; wait_event_interruptible(gpio_key_wait, !is_key_buf_empty()); key = get_key(); err = copy_to_user(buf, &key, 4); return 4; } static unsigned int gpio_key_drv_poll(struct file *fp, poll_table * wait){ poll_wait(fp, &gpio_key_wait, wait); return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM; }
static int gpio_key_drv_fasync(int fd, struct file *file, int on){ if (fasync_helper(fd, file, on, &button_fasync) >= 0) return 0; else return -EIO; } static struct file_operations gpio_key_drv = { .owner = THIS_MODULE, .read = gpio_key_drv_read, .poll = gpio_key_drv_poll, .fasync = gpio_key_drv_fasync, }; static irqreturn_t gpio_key_isr(int irq, void *dev_id){ struct gpio_key *gpio_key = dev_id; tasklet_schedule(&gpio_key->tasklet); mod_timer(&gpio_key->key_timer, jiffies + HZ/50); schedule_work(&gpio_key->work); return IRQ_WAKE_THREAD; }
static irqreturn_t gpio_key_thread_func(int irq, void *data){ struct gpio_key *gpio_key = data; int val;
val = gpiod_get_value(gpio_key->gpiod); printk("gpio_key_thread_func: the process is %s pid %d\n",current->comm, current->pid); printk("gpio_key_thread_func key %d %d\n", gpio_key->gpio, val); return IRQ_HANDLED; }
static int gpio_key_probe(struct platform_device *pdev){ int err; struct device_node *node = pdev->dev.of_node; int count; int i; enum of_gpio_flags flag; count = of_gpio_count(node); if (!count){ printk("%s %s line %d, there isn't any gpio available\n", __FILE__, __FUNCTION__, __LINE__); return -1; }
gpio_keys_100ask = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL); for (i = 0; i < count; i++){ gpio_keys_100ask[i].gpio = of_get_gpio_flags(node, i, &flag); if (gpio_keys_100ask[i].gpio < 0){ printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__); return -1; } gpio_keys_100ask[i].gpiod = gpio_to_desc(gpio_keys_100ask[i].gpio); gpio_keys_100ask[i].flag = flag & OF_GPIO_ACTIVE_LOW; gpio_keys_100ask[i].irq = gpio_to_irq(gpio_keys_100ask[i].gpio); setup_timer(&gpio_keys_100ask[i].key_timer, key_timer_expire, &gpio_keys_100ask[i]); gpio_keys_100ask[i].key_timer.expires = ~0; add_timer(&gpio_keys_100ask[i].key_timer); tasklet_init(&gpio_keys_100ask[i].tasklet, key_tasklet_func, &gpio_keys_100ask[i]); INIT_WORK(&gpio_keys_100ask[i].work, key_work_func); }
for (i = 0; i < count; i++){ err = request_threaded_irq(gpio_keys_100ask[i].irq, gpio_key_isr , gpio_key_thread_func, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING , "100ask_gpio_key", &gpio_keys_100ask[i]); }
major = register_chrdev(0, "100ask_gpio_key", &gpio_key_drv); gpio_key_class = class_create(THIS_MODULE, "100ask_gpio_key_class"); if (IS_ERR(gpio_key_class)) { printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); unregister_chrdev(major, "100ask_gpio_key"); return PTR_ERR(gpio_key_class); } device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, "100ask_gpio_key"); return 0; }
static int gpio_key_remove(struct platform_device *pdev){ struct device_node *node = pdev->dev.of_node; int count; int i; device_destroy(gpio_key_class, MKDEV(major, 0)); class_destroy(gpio_key_class); unregister_chrdev(major, "100ask_gpio_key"); count = of_gpio_count(node); for (i = 0; i < count; i++){ free_irq(gpio_keys_100ask[i].irq, &gpio_keys_100ask[i]); del_timer(&gpio_keys_100ask[i].key_timer); tasklet_kill(&gpio_keys_100ask[i].tasklet); } kfree(gpio_keys_100ask); return 0; } static const struct of_device_id ask100_keys[] = { { .compatible = "100ask,gpio_key" }, { }, }; static struct platform_driver gpio_keys_driver = { .probe = gpio_key_probe, .remove = gpio_key_remove, .driver = { .name = "100ask_gpio_key", .of_match_table = ask100_keys, }, }; static int __init gpio_key_init(void){ int err; err = platform_driver_register(&gpio_keys_driver); return err; }
static void __exit gpio_key_exit(void){ platform_driver_unregister(&gpio_keys_driver); } module_init(gpio_key_init); module_exit(gpio_key_exit); MODULE_LICENSE("GPL");
|