First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / staging / dream / gpio_axis.c
blobc801172aa9eecf024f9d38fe08f0f2037b7042c3
1 /* drivers/input/misc/gpio_axis.c
3 * Copyright (C) 2007 Google, Inc.
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.
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.
16 #include <linux/kernel.h>
17 #include <linux/gpio.h>
18 #include <linux/gpio_event.h>
19 #include <linux/interrupt.h>
21 struct gpio_axis_state {
22 struct input_dev *input_dev;
23 struct gpio_event_axis_info *info;
24 uint32_t pos;
27 uint16_t gpio_axis_4bit_gray_map_table[] = {
28 [0x0] = 0x0, [0x1] = 0x1, /* 0000 0001 */
29 [0x3] = 0x2, [0x2] = 0x3, /* 0011 0010 */
30 [0x6] = 0x4, [0x7] = 0x5, /* 0110 0111 */
31 [0x5] = 0x6, [0x4] = 0x7, /* 0101 0100 */
32 [0xc] = 0x8, [0xd] = 0x9, /* 1100 1101 */
33 [0xf] = 0xa, [0xe] = 0xb, /* 1111 1110 */
34 [0xa] = 0xc, [0xb] = 0xd, /* 1010 1011 */
35 [0x9] = 0xe, [0x8] = 0xf, /* 1001 1000 */
37 uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info *info, uint16_t in)
39 return gpio_axis_4bit_gray_map_table[in];
42 uint16_t gpio_axis_5bit_singletrack_map_table[] = {
43 [0x10] = 0x00, [0x14] = 0x01, [0x1c] = 0x02, /* 10000 10100 11100 */
44 [0x1e] = 0x03, [0x1a] = 0x04, [0x18] = 0x05, /* 11110 11010 11000 */
45 [0x08] = 0x06, [0x0a] = 0x07, [0x0e] = 0x08, /* 01000 01010 01110 */
46 [0x0f] = 0x09, [0x0d] = 0x0a, [0x0c] = 0x0b, /* 01111 01101 01100 */
47 [0x04] = 0x0c, [0x05] = 0x0d, [0x07] = 0x0e, /* 00100 00101 00111 */
48 [0x17] = 0x0f, [0x16] = 0x10, [0x06] = 0x11, /* 10111 10110 00110 */
49 [0x02] = 0x12, [0x12] = 0x13, [0x13] = 0x14, /* 00010 10010 10011 */
50 [0x1b] = 0x15, [0x0b] = 0x16, [0x03] = 0x17, /* 11011 01011 00011 */
51 [0x01] = 0x18, [0x09] = 0x19, [0x19] = 0x1a, /* 00001 01001 11001 */
52 [0x1d] = 0x1b, [0x15] = 0x1c, [0x11] = 0x1d, /* 11101 10101 10001 */
54 uint16_t gpio_axis_5bit_singletrack_map(
55 struct gpio_event_axis_info *info, uint16_t in)
57 return gpio_axis_5bit_singletrack_map_table[in];
60 static void gpio_event_update_axis(struct gpio_axis_state *as, int report)
62 struct gpio_event_axis_info *ai = as->info;
63 int i;
64 int change;
65 uint16_t state = 0;
66 uint16_t pos;
67 uint16_t old_pos = as->pos;
68 for (i = ai->count - 1; i >= 0; i--)
69 state = (state << 1) | gpio_get_value(ai->gpio[i]);
70 pos = ai->map(ai, state);
71 if (ai->flags & GPIOEAF_PRINT_RAW)
72 pr_info("axis %d-%d raw %x, pos %d -> %d\n",
73 ai->type, ai->code, state, old_pos, pos);
74 if (report && pos != old_pos) {
75 if (ai->type == EV_REL) {
76 change = (ai->decoded_size + pos - old_pos) %
77 ai->decoded_size;
78 if (change > ai->decoded_size / 2)
79 change -= ai->decoded_size;
80 if (change == ai->decoded_size / 2) {
81 if (ai->flags & GPIOEAF_PRINT_EVENT)
82 pr_info("axis %d-%d unknown direction, "
83 "pos %d -> %d\n", ai->type,
84 ai->code, old_pos, pos);
85 change = 0; /* no closest direction */
87 if (ai->flags & GPIOEAF_PRINT_EVENT)
88 pr_info("axis %d-%d change %d\n",
89 ai->type, ai->code, change);
90 input_report_rel(as->input_dev, ai->code, change);
91 } else {
92 if (ai->flags & GPIOEAF_PRINT_EVENT)
93 pr_info("axis %d-%d now %d\n",
94 ai->type, ai->code, pos);
95 input_event(as->input_dev, ai->type, ai->code, pos);
97 input_sync(as->input_dev);
99 as->pos = pos;
102 static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
104 struct gpio_axis_state *as = dev_id;
105 gpio_event_update_axis(as, 1);
106 return IRQ_HANDLED;
109 int gpio_event_axis_func(struct input_dev *input_dev,
110 struct gpio_event_info *info, void **data, int func)
112 int ret;
113 int i;
114 int irq;
115 struct gpio_event_axis_info *ai;
116 struct gpio_axis_state *as;
118 ai = container_of(info, struct gpio_event_axis_info, info);
119 if (func == GPIO_EVENT_FUNC_SUSPEND) {
120 for (i = 0; i < ai->count; i++)
121 disable_irq(gpio_to_irq(ai->gpio[i]));
122 return 0;
124 if (func == GPIO_EVENT_FUNC_RESUME) {
125 for (i = 0; i < ai->count; i++)
126 enable_irq(gpio_to_irq(ai->gpio[i]));
127 return 0;
130 if (func == GPIO_EVENT_FUNC_INIT) {
131 *data = as = kmalloc(sizeof(*as), GFP_KERNEL);
132 if (as == NULL) {
133 ret = -ENOMEM;
134 goto err_alloc_axis_state_failed;
136 as->input_dev = input_dev;
137 as->info = ai;
139 input_set_capability(input_dev, ai->type, ai->code);
140 if (ai->type == EV_ABS) {
141 input_set_abs_params(input_dev, ai->code, 0,
142 ai->decoded_size - 1, 0, 0);
144 for (i = 0; i < ai->count; i++) {
145 ret = gpio_request(ai->gpio[i], "gpio_event_axis");
146 if (ret < 0)
147 goto err_request_gpio_failed;
148 ret = gpio_direction_input(ai->gpio[i]);
149 if (ret < 0)
150 goto err_gpio_direction_input_failed;
151 ret = irq = gpio_to_irq(ai->gpio[i]);
152 if (ret < 0)
153 goto err_get_irq_num_failed;
154 ret = request_irq(irq, gpio_axis_irq_handler,
155 IRQF_TRIGGER_RISING |
156 IRQF_TRIGGER_FALLING,
157 "gpio_event_axis", as);
158 if (ret < 0)
159 goto err_request_irq_failed;
161 gpio_event_update_axis(as, 0);
162 return 0;
165 ret = 0;
166 as = *data;
167 for (i = ai->count - 1; i >= 0; i--) {
168 free_irq(gpio_to_irq(ai->gpio[i]), as);
169 err_request_irq_failed:
170 err_get_irq_num_failed:
171 err_gpio_direction_input_failed:
172 gpio_free(ai->gpio[i]);
173 err_request_gpio_failed:
176 kfree(as);
177 *data = NULL;
178 err_alloc_axis_state_failed:
179 return ret;