matrix_keypad.h 3.5 KB

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