fpc1020_input.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* FPC1020 Touch sensor driver
  2. *
  3. * Copyright (c) 2013,2014 Fingerprint Cards AB <tech@fingerprints.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License Version 2
  7. * as published by the Free Software Foundation.
  8. */
  9. #define DEBUG
  10. #include <linux/input.h>
  11. #include <linux/delay.h>
  12. #ifndef CONFIG_OF
  13. #include <linux/spi/fpc1020_common.h>
  14. #include <linux/spi/fpc1020_input.h>
  15. #else
  16. #include "fpc1020_common.h"
  17. #include "fpc1020_input.h"
  18. #endif
  19. /* -------------------------------------------------------------------- */
  20. /* function prototypes */
  21. /* -------------------------------------------------------------------- */
  22. static int fpc1020_write_lpm_setup(fpc1020_data_t *fpc1020);
  23. static int fpc1020_wait_finger_present_lpm(fpc1020_data_t *fpc1020);
  24. /* -------------------------------------------------------------------- */
  25. /* driver constants */
  26. /* -------------------------------------------------------------------- */
  27. #define FPC1020_KEY_FINGER_PRESENT KEY_WAKEUP /* 143*/
  28. #define FPC1020_INPUT_POLL_TIME_MS 1000u
  29. /* -------------------------------------------------------------------- */
  30. /* function definitions */
  31. /* -------------------------------------------------------------------- */
  32. int fpc1020_input_init(fpc1020_data_t *fpc1020)
  33. {
  34. int error = 0;
  35. dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
  36. fpc1020->input_dev = input_allocate_device();
  37. if (!fpc1020->input_dev) {
  38. dev_err(&fpc1020->spi->dev, "Input_allocate_device failed.\n");
  39. error = -ENOMEM;
  40. }
  41. if (!error) {
  42. fpc1020->input_dev->name = FPC1020_DEV_NAME;
  43. set_bit(EV_KEY, fpc1020->input_dev->evbit);
  44. set_bit(FPC1020_KEY_FINGER_PRESENT, fpc1020->input_dev->keybit);
  45. error = input_register_device(fpc1020->input_dev);
  46. }
  47. if (error) {
  48. dev_err(&fpc1020->spi->dev, "Input_register_device failed.\n");
  49. input_free_device(fpc1020->input_dev);
  50. fpc1020->input_dev = NULL;
  51. }
  52. return error;
  53. }
  54. /* -------------------------------------------------------------------- */
  55. void fpc1020_input_destroy(fpc1020_data_t *fpc1020)
  56. {
  57. dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
  58. if (fpc1020->input_dev != NULL)
  59. input_free_device(fpc1020->input_dev);
  60. }
  61. /* -------------------------------------------------------------------- */
  62. int fpc1020_input_enable(fpc1020_data_t *fpc1020, bool enabled)
  63. {
  64. dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
  65. fpc1020->input.enabled = enabled;
  66. return 0;
  67. }
  68. /* -------------------------------------------------------------------- */
  69. int fpc1020_input_task(fpc1020_data_t *fpc1020)
  70. {
  71. int error = 0;
  72. dev_dbg(&fpc1020->spi->dev, "%s\n", __func__);
  73. while (!fpc1020->worker.stop_request && !error) {
  74. error = fpc1020_wait_finger_present_lpm(fpc1020);
  75. if (error == 0) {
  76. input_report_key(fpc1020->input_dev,
  77. FPC1020_KEY_FINGER_PRESENT, 1);
  78. input_report_key(fpc1020->input_dev,
  79. FPC1020_KEY_FINGER_PRESENT, 0);
  80. input_sync(fpc1020->input_dev);
  81. }
  82. }
  83. return error;
  84. }
  85. /* -------------------------------------------------------------------- */
  86. static int fpc1020_write_lpm_setup(fpc1020_data_t *fpc1020)
  87. {
  88. const int mux = FPC1020_MAX_ADC_SETTINGS - 1;
  89. int error = 0;
  90. u16 temp_u16;
  91. fpc1020_reg_access_t reg;
  92. dev_dbg(&fpc1020->spi->dev, "%s %d\n", __func__, mux);
  93. error = fpc1020_write_sensor_setup(fpc1020);
  94. if (error)
  95. goto out;
  96. temp_u16 = fpc1020->setup.adc_shift[mux];
  97. temp_u16 <<= 8;
  98. temp_u16 |= fpc1020->setup.adc_gain[mux];
  99. FPC1020_MK_REG_WRITE(reg, FPC102X_REG_ADC_SHIFT_GAIN, &temp_u16);
  100. error = fpc1020_reg_access(fpc1020, &reg);
  101. if (error)
  102. goto out;
  103. temp_u16 = fpc1020->setup.pxl_ctrl[mux];
  104. FPC1020_MK_REG_WRITE(reg, FPC102X_REG_PXL_CTRL, &temp_u16);
  105. error = fpc1020_reg_access(fpc1020, &reg);
  106. if (error)
  107. goto out;
  108. out:
  109. return error;
  110. }
  111. /* -------------------------------------------------------------------- */
  112. static int fpc1020_wait_finger_present_lpm(fpc1020_data_t *fpc1020)
  113. {
  114. const int lpm_poll_delay_ms = FPC1020_INPUT_POLL_TIME_MS;
  115. const int zmask_5 = 1 << 5;
  116. const int zmask_6 = 1 << 6;
  117. const int zmask_ext = FPC1020_FINGER_DETECT_ZONE_MASK;
  118. int error = 0;
  119. int zone_raw = 0;
  120. bool wakeup_center = false;
  121. bool wakeup_ext = false;
  122. bool wakeup = false;
  123. error = fpc1020_wake_up(fpc1020);
  124. if (!error)
  125. error = fpc1020_calc_finger_detect_threshold_min(fpc1020);
  126. if (error >= 0)
  127. error = fpc1020_set_finger_detect_threshold(fpc1020, error);
  128. if (error >= 0)
  129. error = fpc1020_write_lpm_setup(fpc1020);
  130. if (!error) {
  131. error = fpc1020_sleep(fpc1020, false);
  132. if (error == -EAGAIN) {
  133. error = fpc1020_sleep(fpc1020, false);
  134. if (error == -EAGAIN)
  135. error = 0;
  136. }
  137. }
  138. while (!fpc1020->worker.stop_request && !error && !wakeup) {
  139. if (!error)
  140. error = fpc1020_wait_finger_present(fpc1020);
  141. if (!error)
  142. error = fpc1020_check_finger_present_raw(fpc1020);
  143. zone_raw = (error >= 0) ? error : 0;
  144. if (error >= 0) {
  145. error = 0;
  146. wakeup_center = (zone_raw & zmask_5) ||
  147. (zone_raw & zmask_6);
  148. /* Todo: refined extended processing ? */
  149. wakeup_ext = ((zone_raw & zmask_ext) == zmask_ext);
  150. } else {
  151. wakeup_center =
  152. wakeup_ext = false;
  153. }
  154. if (wakeup_center && wakeup_ext) {
  155. dev_dbg(&fpc1020->spi->dev,
  156. "%s Wake up !\n", __func__);
  157. wakeup = true;
  158. }
  159. if (!wakeup && !error) {
  160. error = fpc1020_sleep(fpc1020, false);
  161. if (error == -EAGAIN)
  162. error = 0;
  163. if (!error)
  164. msleep(lpm_poll_delay_ms);
  165. }
  166. }
  167. if (error < 0)
  168. dev_dbg(&fpc1020->spi->dev,
  169. "%s %s %d!\n", __func__,
  170. (error == -EINTR) ? "TERMINATED" : "FAILED", error);
  171. return error;
  172. }
  173. /* -------------------------------------------------------------------- */