gpio_axis.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include <linux/kernel.h>
  16. #include <linux/gpio.h>
  17. #include <linux/gpio_event.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/slab.h>
  20. struct gpio_axis_state {
  21. struct gpio_event_input_devs *input_devs;
  22. struct gpio_event_axis_info *info;
  23. uint32_t pos;
  24. };
  25. uint16_t gpio_axis_4bit_gray_map_table[] = {
  26. [0x0] = 0x0, [0x1] = 0x1, /* 0000 0001 */
  27. [0x3] = 0x2, [0x2] = 0x3, /* 0011 0010 */
  28. [0x6] = 0x4, [0x7] = 0x5, /* 0110 0111 */
  29. [0x5] = 0x6, [0x4] = 0x7, /* 0101 0100 */
  30. [0xc] = 0x8, [0xd] = 0x9, /* 1100 1101 */
  31. [0xf] = 0xa, [0xe] = 0xb, /* 1111 1110 */
  32. [0xa] = 0xc, [0xb] = 0xd, /* 1010 1011 */
  33. [0x9] = 0xe, [0x8] = 0xf, /* 1001 1000 */
  34. };
  35. uint16_t gpio_axis_4bit_gray_map(struct gpio_event_axis_info *info, uint16_t in)
  36. {
  37. return gpio_axis_4bit_gray_map_table[in];
  38. }
  39. uint16_t gpio_axis_5bit_singletrack_map_table[] = {
  40. [0x10] = 0x00, [0x14] = 0x01, [0x1c] = 0x02, /* 10000 10100 11100 */
  41. [0x1e] = 0x03, [0x1a] = 0x04, [0x18] = 0x05, /* 11110 11010 11000 */
  42. [0x08] = 0x06, [0x0a] = 0x07, [0x0e] = 0x08, /* 01000 01010 01110 */
  43. [0x0f] = 0x09, [0x0d] = 0x0a, [0x0c] = 0x0b, /* 01111 01101 01100 */
  44. [0x04] = 0x0c, [0x05] = 0x0d, [0x07] = 0x0e, /* 00100 00101 00111 */
  45. [0x17] = 0x0f, [0x16] = 0x10, [0x06] = 0x11, /* 10111 10110 00110 */
  46. [0x02] = 0x12, [0x12] = 0x13, [0x13] = 0x14, /* 00010 10010 10011 */
  47. [0x1b] = 0x15, [0x0b] = 0x16, [0x03] = 0x17, /* 11011 01011 00011 */
  48. [0x01] = 0x18, [0x09] = 0x19, [0x19] = 0x1a, /* 00001 01001 11001 */
  49. [0x1d] = 0x1b, [0x15] = 0x1c, [0x11] = 0x1d, /* 11101 10101 10001 */
  50. };
  51. uint16_t gpio_axis_5bit_singletrack_map(
  52. struct gpio_event_axis_info *info, uint16_t in)
  53. {
  54. return gpio_axis_5bit_singletrack_map_table[in];
  55. }
  56. static void gpio_event_update_axis(struct gpio_axis_state *as, int report)
  57. {
  58. struct gpio_event_axis_info *ai = as->info;
  59. int i;
  60. int change;
  61. uint16_t state = 0;
  62. uint16_t pos;
  63. uint16_t old_pos = as->pos;
  64. for (i = ai->count - 1; i >= 0; i--)
  65. state = (state << 1) | gpio_get_value(ai->gpio[i]);
  66. pos = ai->map(ai, state);
  67. if (ai->flags & GPIOEAF_PRINT_RAW)
  68. pr_info("axis %d-%d raw %x, pos %d -> %d\n",
  69. ai->type, ai->code, state, old_pos, pos);
  70. if (report && pos != old_pos) {
  71. if (ai->type == EV_REL) {
  72. change = (ai->decoded_size + pos - old_pos) %
  73. ai->decoded_size;
  74. if (change > ai->decoded_size / 2)
  75. change -= ai->decoded_size;
  76. if (change == ai->decoded_size / 2) {
  77. if (ai->flags & GPIOEAF_PRINT_EVENT)
  78. pr_info("axis %d-%d unknown direction, "
  79. "pos %d -> %d\n", ai->type,
  80. ai->code, old_pos, pos);
  81. change = 0; /* no closest direction */
  82. }
  83. if (ai->flags & GPIOEAF_PRINT_EVENT)
  84. pr_info("axis %d-%d change %d\n",
  85. ai->type, ai->code, change);
  86. input_report_rel(as->input_devs->dev[ai->dev],
  87. ai->code, change);
  88. } else {
  89. if (ai->flags & GPIOEAF_PRINT_EVENT)
  90. pr_info("axis %d-%d now %d\n",
  91. ai->type, ai->code, pos);
  92. input_event(as->input_devs->dev[ai->dev],
  93. ai->type, ai->code, pos);
  94. }
  95. input_sync(as->input_devs->dev[ai->dev]);
  96. }
  97. as->pos = pos;
  98. }
  99. static irqreturn_t gpio_axis_irq_handler(int irq, void *dev_id)
  100. {
  101. struct gpio_axis_state *as = dev_id;
  102. gpio_event_update_axis(as, 1);
  103. return IRQ_HANDLED;
  104. }
  105. int gpio_event_axis_func(struct gpio_event_input_devs *input_devs,
  106. struct gpio_event_info *info, void **data, int func)
  107. {
  108. int ret;
  109. int i;
  110. int irq;
  111. struct gpio_event_axis_info *ai;
  112. struct gpio_axis_state *as;
  113. ai = container_of(info, struct gpio_event_axis_info, info);
  114. if (func == GPIO_EVENT_FUNC_SUSPEND) {
  115. for (i = 0; i < ai->count; i++)
  116. disable_irq(gpio_to_irq(ai->gpio[i]));
  117. return 0;
  118. }
  119. if (func == GPIO_EVENT_FUNC_RESUME) {
  120. for (i = 0; i < ai->count; i++)
  121. enable_irq(gpio_to_irq(ai->gpio[i]));
  122. return 0;
  123. }
  124. if (func == GPIO_EVENT_FUNC_INIT) {
  125. *data = as = kmalloc(sizeof(*as), GFP_KERNEL);
  126. if (as == NULL) {
  127. ret = -ENOMEM;
  128. goto err_alloc_axis_state_failed;
  129. }
  130. as->input_devs = input_devs;
  131. as->info = ai;
  132. if (ai->dev >= input_devs->count) {
  133. pr_err("gpio_event_axis: bad device index %d >= %d "
  134. "for %d:%d\n", ai->dev, input_devs->count,
  135. ai->type, ai->code);
  136. ret = -EINVAL;
  137. goto err_bad_device_index;
  138. }
  139. input_set_capability(input_devs->dev[ai->dev],
  140. ai->type, ai->code);
  141. if (ai->type == EV_ABS) {
  142. input_set_abs_params(input_devs->dev[ai->dev], ai->code,
  143. 0, 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. 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:
  174. ;
  175. }
  176. err_bad_device_index:
  177. kfree(as);
  178. *data = NULL;
  179. err_alloc_axis_state_failed:
  180. return ret;
  181. }