• 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 
21 struct gpio_axis_state {
22 	struct gpio_event_input_devs *input_devs;
23 	struct gpio_event_axis_info *info;
24 	uint32_t pos;
25 };
26 
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 */
36 };
gpio_axis_4bit_gray_map(struct gpio_event_axis_info * info,uint16_t in)37 uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info *info, uint16_t in)
38 {
39 	return gpio_axis_4bit_gray_map_table[in];
40 }
41 
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     */
53 };
gpio_axis_5bit_singletrack_map(struct gpio_event_axis_info * info,uint16_t in)54 uint16_t gpio_axis_5bit_singletrack_map(
55 	struct gpio_event_axis_info *info, uint16_t in)
56 {
57 	return gpio_axis_5bit_singletrack_map_table[in];
58 }
59 
gpio_event_update_axis(struct gpio_axis_state * as,int report)60 static void gpio_event_update_axis(struct gpio_axis_state *as, int report)
61 {
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 */
86 			}
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_devs->dev[ai->dev],
91 						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_devs->dev[ai->dev],
97 					ai->type, ai->code, pos);
98 		}
99 		input_sync(as->input_devs->dev[ai->dev]);
100 	}
101 	as->pos = pos;
102 }
103 
gpio_axis_irq_handler(int irq,void * dev_id)104 static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
105 {
106 	struct gpio_axis_state *as = dev_id;
107 	gpio_event_update_axis(as, 1);
108 	return IRQ_HANDLED;
109 }
110 
gpio_event_axis_func(struct gpio_event_input_devs * input_devs,struct gpio_event_info * info,void ** data,int func)111 int gpio_event_axis_func(struct gpio_event_input_devs *input_devs,
112 			 struct gpio_event_info *info, void **data, int func)
113 {
114 	int ret;
115 	int i;
116 	int irq;
117 	struct gpio_event_axis_info *ai;
118 	struct gpio_axis_state *as;
119 
120 	ai = container_of(info, struct gpio_event_axis_info, info);
121 	if (func == GPIO_EVENT_FUNC_SUSPEND) {
122 		for (i = 0; i < ai->count; i++)
123 			disable_irq(gpio_to_irq(ai->gpio[i]));
124 		return 0;
125 	}
126 	if (func == GPIO_EVENT_FUNC_RESUME) {
127 		for (i = 0; i < ai->count; i++)
128 			enable_irq(gpio_to_irq(ai->gpio[i]));
129 		return 0;
130 	}
131 
132 	if (func == GPIO_EVENT_FUNC_INIT) {
133 		*data = as = kmalloc(sizeof(*as), GFP_KERNEL);
134 		if (as == NULL) {
135 			ret = -ENOMEM;
136 			goto err_alloc_axis_state_failed;
137 		}
138 		as->input_devs = input_devs;
139 		as->info = ai;
140 		if (ai->dev >= input_devs->count) {
141 			pr_err("gpio_event_axis: bad device index %d >= %d "
142 				"for %d:%d\n", ai->dev, input_devs->count,
143 				ai->type, ai->code);
144 			ret = -EINVAL;
145 			goto err_bad_device_index;
146 		}
147 
148 		input_set_capability(input_devs->dev[ai->dev],
149 				     ai->type, ai->code);
150 		if (ai->type == EV_ABS) {
151 			input_set_abs_params(input_devs->dev[ai->dev], ai->code,
152 					     0, ai->decoded_size - 1, 0, 0);
153 		}
154 		for (i = 0; i < ai->count; i++) {
155 			ret = gpio_request(ai->gpio[i], "gpio_event_axis");
156 			if (ret < 0)
157 				goto err_request_gpio_failed;
158 			ret = gpio_direction_input(ai->gpio[i]);
159 			if (ret < 0)
160 				goto err_gpio_direction_input_failed;
161 			ret = irq = gpio_to_irq(ai->gpio[i]);
162 			if (ret < 0)
163 				goto err_get_irq_num_failed;
164 			ret = request_irq(irq, gpio_axis_irq_handler,
165 					  IRQF_TRIGGER_RISING |
166 					  IRQF_TRIGGER_FALLING,
167 					  "gpio_event_axis", as);
168 			if (ret < 0)
169 				goto err_request_irq_failed;
170 		}
171 		gpio_event_update_axis(as, 0);
172 		return 0;
173 	}
174 
175 	ret = 0;
176 	as = *data;
177 	for (i = ai->count - 1; i >= 0; i--) {
178 		free_irq(gpio_to_irq(ai->gpio[i]), as);
179 err_request_irq_failed:
180 err_get_irq_num_failed:
181 err_gpio_direction_input_failed:
182 		gpio_free(ai->gpio[i]);
183 err_request_gpio_failed:
184 		;
185 	}
186 err_bad_device_index:
187 	kfree(as);
188 	*data = NULL;
189 err_alloc_axis_state_failed:
190 	return ret;
191 }
192