• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpio.h>
18 #include <linux/gpio_event.h>
19 #include <linux/interrupt.h>
20 #include <linux/slab.h>
21 
22 struct gpio_axis_state {
23 	struct gpio_event_input_devs *input_devs;
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 };
gpio_axis_4bit_gray_map(struct gpio_event_axis_info * info,uint16_t in)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 };
gpio_axis_5bit_singletrack_map(struct gpio_event_axis_info * info,uint16_t in)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 
gpio_event_update_axis(struct gpio_axis_state * as,int report)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_devs->dev[ai->dev],
92 						ai->code, change);
93 		} else {
94 			if (ai->flags & GPIOEAF_PRINT_EVENT)
95 				pr_info("axis %d-%d now %d\n",
96 					ai->type, ai->code, pos);
97 			input_event(as->input_devs->dev[ai->dev],
98 					ai->type, ai->code, pos);
99 		}
100 		input_sync(as->input_devs->dev[ai->dev]);
101 	}
102 	as->pos = pos;
103 }
104 
gpio_axis_irq_handler(int irq,void * dev_id)105 static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
106 {
107 	struct gpio_axis_state *as = dev_id;
108 	gpio_event_update_axis(as, 1);
109 	return IRQ_HANDLED;
110 }
111 
gpio_event_axis_func(struct gpio_event_input_devs * input_devs,struct gpio_event_info * info,void ** data,int func)112 int gpio_event_axis_func(struct gpio_event_input_devs *input_devs,
113 			 struct gpio_event_info *info, void **data, int func)
114 {
115 	int ret;
116 	int i;
117 	int irq;
118 	struct gpio_event_axis_info *ai;
119 	struct gpio_axis_state *as;
120 
121 	ai = container_of(info, struct gpio_event_axis_info, info);
122 	if (func == GPIO_EVENT_FUNC_SUSPEND) {
123 		for (i = 0; i < ai->count; i++)
124 			disable_irq(gpio_to_irq(ai->gpio[i]));
125 		return 0;
126 	}
127 	if (func == GPIO_EVENT_FUNC_RESUME) {
128 		for (i = 0; i < ai->count; i++)
129 			enable_irq(gpio_to_irq(ai->gpio[i]));
130 		return 0;
131 	}
132 
133 	if (func == GPIO_EVENT_FUNC_INIT) {
134 		*data = as = kmalloc(sizeof(*as), GFP_KERNEL);
135 		if (as == NULL) {
136 			ret = -ENOMEM;
137 			goto err_alloc_axis_state_failed;
138 		}
139 		as->input_devs = input_devs;
140 		as->info = ai;
141 		if (ai->dev >= input_devs->count) {
142 			pr_err("gpio_event_axis: bad device index %d >= %d "
143 				"for %d:%d\n", ai->dev, input_devs->count,
144 				ai->type, ai->code);
145 			ret = -EINVAL;
146 			goto err_bad_device_index;
147 		}
148 
149 		input_set_capability(input_devs->dev[ai->dev],
150 				     ai->type, ai->code);
151 		if (ai->type == EV_ABS) {
152 			input_set_abs_params(input_devs->dev[ai->dev], ai->code,
153 					     0, ai->decoded_size - 1, 0, 0);
154 		}
155 		for (i = 0; i < ai->count; i++) {
156 			ret = gpio_request(ai->gpio[i], "gpio_event_axis");
157 			if (ret < 0)
158 				goto err_request_gpio_failed;
159 			ret = gpio_direction_input(ai->gpio[i]);
160 			if (ret < 0)
161 				goto err_gpio_direction_input_failed;
162 			ret = irq = gpio_to_irq(ai->gpio[i]);
163 			if (ret < 0)
164 				goto err_get_irq_num_failed;
165 			ret = request_irq(irq, gpio_axis_irq_handler,
166 					  IRQF_TRIGGER_RISING |
167 					  IRQF_TRIGGER_FALLING,
168 					  "gpio_event_axis", as);
169 			if (ret < 0)
170 				goto err_request_irq_failed;
171 		}
172 		gpio_event_update_axis(as, 0);
173 		return 0;
174 	}
175 
176 	ret = 0;
177 	as = *data;
178 	for (i = ai->count - 1; i >= 0; i--) {
179 		free_irq(gpio_to_irq(ai->gpio[i]), as);
180 err_request_irq_failed:
181 err_get_irq_num_failed:
182 err_gpio_direction_input_failed:
183 		gpio_free(ai->gpio[i]);
184 err_request_gpio_failed:
185 		;
186 	}
187 err_bad_device_index:
188 	kfree(as);
189 	*data = NULL;
190 err_alloc_axis_state_failed:
191 	return ret;
192 }
193