]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/dream/gpio_axis.c
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel...
[net-next-2.6.git] / drivers / staging / dream / gpio_axis.c
1 /* drivers/input/misc/gpio_axis.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio_event.h>
20 #include <linux/interrupt.h>
21
22 struct gpio_axis_state {
23         struct input_dev *input_dev;
24         struct gpio_event_axis_info *info;
25         uint32_t pos;
26 };
27
28 uint16_t gpio_axis_4bit_gray_map_table[] = {
29         [0x0] = 0x0, [0x1] = 0x1, /* 0000 0001 */
30         [0x3] = 0x2, [0x2] = 0x3, /* 0011 0010 */
31         [0x6] = 0x4, [0x7] = 0x5, /* 0110 0111 */
32         [0x5] = 0x6, [0x4] = 0x7, /* 0101 0100 */
33         [0xc] = 0x8, [0xd] = 0x9, /* 1100 1101 */
34         [0xf] = 0xa, [0xe] = 0xb, /* 1111 1110 */
35         [0xa] = 0xc, [0xb] = 0xd, /* 1010 1011 */
36         [0x9] = 0xe, [0x8] = 0xf, /* 1001 1000 */
37 };
38 uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info *info, uint16_t in)
39 {
40         return gpio_axis_4bit_gray_map_table[in];
41 }
42
43 uint16_t gpio_axis_5bit_singletrack_map_table[] = {
44         [0x10] = 0x00, [0x14] = 0x01, [0x1c] = 0x02, /*     10000 10100 11100 */
45         [0x1e] = 0x03, [0x1a] = 0x04, [0x18] = 0x05, /*     11110 11010 11000 */
46         [0x08] = 0x06, [0x0a] = 0x07, [0x0e] = 0x08, /*    01000 01010 01110  */
47         [0x0f] = 0x09, [0x0d] = 0x0a, [0x0c] = 0x0b, /*    01111 01101 01100  */
48         [0x04] = 0x0c, [0x05] = 0x0d, [0x07] = 0x0e, /*   00100 00101 00111   */
49         [0x17] = 0x0f, [0x16] = 0x10, [0x06] = 0x11, /*   10111 10110 00110   */
50         [0x02] = 0x12, [0x12] = 0x13, [0x13] = 0x14, /*  00010 10010 10011    */
51         [0x1b] = 0x15, [0x0b] = 0x16, [0x03] = 0x17, /*  11011 01011 00011    */
52         [0x01] = 0x18, [0x09] = 0x19, [0x19] = 0x1a, /* 00001 01001 11001     */
53         [0x1d] = 0x1b, [0x15] = 0x1c, [0x11] = 0x1d, /* 11101 10101 10001     */
54 };
55 uint16_t gpio_axis_5bit_singletrack_map(
56         struct gpio_event_axis_info *info, uint16_t in)
57 {
58         return gpio_axis_5bit_singletrack_map_table[in];
59 }
60
61 static void gpio_event_update_axis(struct gpio_axis_state *as, int report)
62 {
63         struct gpio_event_axis_info *ai = as->info;
64         int i;
65         int change;
66         uint16_t state = 0;
67         uint16_t pos;
68         uint16_t old_pos = as->pos;
69         for (i = ai->count - 1; i >= 0; i--)
70                 state = (state << 1) | gpio_get_value(ai->gpio[i]);
71         pos = ai->map(ai, state);
72         if (ai->flags & GPIOEAF_PRINT_RAW)
73                 pr_info("axis %d-%d raw %x, pos %d -> %d\n",
74                         ai->type, ai->code, state, old_pos, pos);
75         if (report && pos != old_pos) {
76                 if (ai->type == EV_REL) {
77                         change = (ai->decoded_size + pos - old_pos) %
78                                   ai->decoded_size;
79                         if (change > ai->decoded_size / 2)
80                                 change -= ai->decoded_size;
81                         if (change == ai->decoded_size / 2) {
82                                 if (ai->flags & GPIOEAF_PRINT_EVENT)
83                                         pr_info("axis %d-%d unknown direction, "
84                                                 "pos %d -> %d\n", ai->type,
85                                                 ai->code, old_pos, pos);
86                                 change = 0; /* no closest direction */
87                         }
88                         if (ai->flags & GPIOEAF_PRINT_EVENT)
89                                 pr_info("axis %d-%d change %d\n",
90                                         ai->type, ai->code, change);
91                         input_report_rel(as->input_dev, ai->code, change);
92                 } else {
93                         if (ai->flags & GPIOEAF_PRINT_EVENT)
94                                 pr_info("axis %d-%d now %d\n",
95                                         ai->type, ai->code, pos);
96                         input_event(as->input_dev, ai->type, ai->code, pos);
97                 }
98                 input_sync(as->input_dev);
99         }
100         as->pos = pos;
101 }
102
103 static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
104 {
105         struct gpio_axis_state *as = dev_id;
106         gpio_event_update_axis(as, 1);
107         return IRQ_HANDLED;
108 }
109
110 int gpio_event_axis_func(struct input_dev *input_dev,
111                          struct gpio_event_info *info, void **data, int func)
112 {
113         int ret;
114         int i;
115         int irq;
116         struct gpio_event_axis_info *ai;
117         struct gpio_axis_state *as;
118
119         ai = container_of(info, struct gpio_event_axis_info, info);
120         if (func == GPIO_EVENT_FUNC_SUSPEND) {
121                 for (i = 0; i < ai->count; i++)
122                         disable_irq(gpio_to_irq(ai->gpio[i]));
123                 return 0;
124         }
125         if (func == GPIO_EVENT_FUNC_RESUME) {
126                 for (i = 0; i < ai->count; i++)
127                         enable_irq(gpio_to_irq(ai->gpio[i]));
128                 return 0;
129         }
130
131         if (func == GPIO_EVENT_FUNC_INIT) {
132                 *data = as = kmalloc(sizeof(*as), GFP_KERNEL);
133                 if (as == NULL) {
134                         ret = -ENOMEM;
135                         goto err_alloc_axis_state_failed;
136                 }
137                 as->input_dev = input_dev;
138                 as->info = ai;
139
140                 input_set_capability(input_dev, ai->type, ai->code);
141                 if (ai->type == EV_ABS) {
142                         input_set_abs_params(input_dev, ai->code, 0,
143                                              ai->decoded_size - 1, 0, 0);
144                 }
145                 for (i = 0; i < ai->count; i++) {
146                         ret = gpio_request(ai->gpio[i], "gpio_event_axis");
147                         if (ret < 0)
148                                 goto err_request_gpio_failed;
149                         ret = gpio_direction_input(ai->gpio[i]);
150                         if (ret < 0)
151                                 goto err_gpio_direction_input_failed;
152                         ret = irq = gpio_to_irq(ai->gpio[i]);
153                         if (ret < 0)
154                                 goto err_get_irq_num_failed;
155                         ret = request_irq(irq, gpio_axis_irq_handler,
156                                           IRQF_TRIGGER_RISING |
157                                           IRQF_TRIGGER_FALLING,
158                                           "gpio_event_axis", as);
159                         if (ret < 0)
160                                 goto err_request_irq_failed;
161                 }
162                 gpio_event_update_axis(as, 0);
163                 return 0;
164         }
165
166         ret = 0;
167         as = *data;
168         for (i = ai->count - 1; i >= 0; i--) {
169                 free_irq(gpio_to_irq(ai->gpio[i]), as);
170 err_request_irq_failed:
171 err_get_irq_num_failed:
172 err_gpio_direction_input_failed:
173                 gpio_free(ai->gpio[i]);
174 err_request_gpio_failed:
175                 ;
176         }
177         kfree(as);
178         *data = NULL;
179 err_alloc_axis_state_failed:
180         return ret;
181 }