matrix_keypad.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef _MATRIX_KEYPAD_H
  2. #define _MATRIX_KEYPAD_H
  3. #include <linux/types.h>
  4. #include <linux/input.h>
  5. #include <linux/of.h>
  6. #define MATRIX_MAX_ROWS 18
  7. #define MATRIX_MAX_COLS 18
  8. #define KEY(row, col, val) ((((row) % (MATRIX_MAX_ROWS)) << 24) |\
  9. (((col) % (MATRIX_MAX_COLS)) << 16) |\
  10. (val & 0xffff))
  11. #define KEY_ROW(k) (((k) >> 24) & 0xff)
  12. #define KEY_COL(k) (((k) >> 16) & 0xff)
  13. #define KEY_VAL(k) ((k) & 0xffff)
  14. #define MATRIX_SCAN_CODE(row, col, row_shift) (((row) << (row_shift)) + (col))
  15. /**
  16. * struct matrix_keymap_data - keymap for matrix keyboards
  17. * @keymap: pointer to array of uint32 values encoded with KEY() macro
  18. * representing keymap
  19. * @keymap_size: number of entries (initialized) in this keymap
  20. *
  21. * This structure is supposed to be used by platform code to supply
  22. * keymaps to drivers that implement matrix-like keypads/keyboards.
  23. */
  24. struct matrix_keymap_data {
  25. const uint32_t *keymap;
  26. unsigned int keymap_size;
  27. };
  28. /**
  29. * struct matrix_keypad_platform_data - platform-dependent keypad data
  30. * @keymap_data: pointer to &matrix_keymap_data
  31. * @row_gpios: pointer to array of gpio numbers representing rows
  32. * @col_gpios: pointer to array of gpio numbers reporesenting colums
  33. * @num_row_gpios: actual number of row gpios used by device
  34. * @num_col_gpios: actual number of col gpios used by device
  35. * @col_scan_delay_us: delay, measured in microseconds, that is
  36. * needed before we can keypad after activating column gpio
  37. * @debounce_ms: debounce interval in milliseconds
  38. * @clustered_irq: may be specified if interrupts of all row/column GPIOs
  39. * are bundled to one single irq
  40. * @clustered_irq_flags: flags that are needed for the clustered irq
  41. * @active_low: gpio polarity
  42. * @wakeup: controls whether the device should be set up as wakeup
  43. * source
  44. * @no_autorepeat: disable key autorepeat
  45. *
  46. * This structure represents platform-specific data that use used by
  47. * matrix_keypad driver to perform proper initialization.
  48. */
  49. struct matrix_keypad_platform_data {
  50. const struct matrix_keymap_data *keymap_data;
  51. #ifdef CONFIG_SEC_PATEK_PROJECT
  52. unsigned int *row_gpios;
  53. #else
  54. const unsigned int *row_gpios;
  55. #endif
  56. const unsigned int *col_gpios;
  57. unsigned int num_row_gpios;
  58. unsigned int num_col_gpios;
  59. unsigned int col_scan_delay_us;
  60. /* key debounce interval in milli-second */
  61. unsigned int debounce_ms;
  62. unsigned int clustered_irq;
  63. unsigned int clustered_irq_flags;
  64. bool active_low;
  65. bool wakeup;
  66. bool no_autorepeat;
  67. };
  68. /**
  69. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  70. * @keymap_data: keymap supplied by the platform code
  71. * @row_shift: number of bits to shift row value by to advance to the next
  72. * line in the keymap
  73. * @keymap: expanded version of keymap that is suitable for use by
  74. * matrix keyboad driver
  75. * @keybit: pointer to bitmap of keys supported by input device
  76. *
  77. * This function converts platform keymap (encoded with KEY() macro) into
  78. * an array of keycodes that is suitable for using in a standard matrix
  79. * keyboard driver that uses row and col as indices.
  80. */
  81. static inline void
  82. matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  83. unsigned int row_shift,
  84. unsigned short *keymap, unsigned long *keybit)
  85. {
  86. int i;
  87. for (i = 0; i < keymap_data->keymap_size; i++) {
  88. unsigned int key = keymap_data->keymap[i];
  89. unsigned int row = KEY_ROW(key);
  90. unsigned int col = KEY_COL(key);
  91. unsigned short code = KEY_VAL(key);
  92. keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  93. __set_bit(code, keybit);
  94. }
  95. __clear_bit(KEY_RESERVED, keybit);
  96. }
  97. #ifdef CONFIG_INPUT_OF_MATRIX_KEYMAP
  98. struct matrix_keymap_data *
  99. matrix_keyboard_of_fill_keymap(struct device_node *np, const char *propname);
  100. void matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd);
  101. #else
  102. static inline struct matrix_keymap_data *
  103. matrix_keyboard_of_fill_keymap(struct device_node *np, const char *propname)
  104. {
  105. return NULL;
  106. }
  107. static inline void
  108. matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd)
  109. {
  110. }
  111. #endif
  112. #endif /* _MATRIX_KEYPAD_H */