matrix-keymap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Helpers for matrix keyboard bindings
  3. *
  4. * Copyright (C) 2012 Google, Inc
  5. *
  6. * Author:
  7. * Olof Johansson <olof@lixom.net>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <linux/input.h>
  23. #include <linux/of.h>
  24. #include <linux/export.h>
  25. #include <linux/module.h>
  26. #include <linux/input/matrix_keypad.h>
  27. static bool matrix_keypad_map_key(struct input_dev *input_dev,
  28. unsigned int rows, unsigned int cols,
  29. unsigned int row_shift, unsigned int key)
  30. {
  31. unsigned short *keymap = input_dev->keycode;
  32. unsigned int row = KEY_ROW(key);
  33. unsigned int col = KEY_COL(key);
  34. unsigned short code = KEY_VAL(key);
  35. if (row >= rows || col >= cols) {
  36. dev_err(input_dev->dev.parent,
  37. "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n",
  38. __func__, key, row, col, rows, cols);
  39. return false;
  40. }
  41. keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  42. __set_bit(code, input_dev->keybit);
  43. return true;
  44. }
  45. #ifdef CONFIG_OF
  46. static int matrix_keypad_parse_of_keymap(const char *propname,
  47. unsigned int rows, unsigned int cols,
  48. struct input_dev *input_dev)
  49. {
  50. struct device *dev = input_dev->dev.parent;
  51. struct device_node *np = dev->of_node;
  52. unsigned int row_shift = get_count_order(cols);
  53. unsigned int max_keys = rows << row_shift;
  54. unsigned int proplen, i, size;
  55. const __be32 *prop;
  56. if (!np)
  57. return -ENOENT;
  58. if (!propname)
  59. propname = "linux,keymap";
  60. prop = of_get_property(np, propname, &proplen);
  61. if (!prop) {
  62. dev_err(dev, "OF: %s property not defined in %s\n",
  63. propname, np->full_name);
  64. return -ENOENT;
  65. }
  66. if (proplen % sizeof(u32)) {
  67. dev_err(dev, "OF: Malformed keycode property %s in %s\n",
  68. propname, np->full_name);
  69. return -EINVAL;
  70. }
  71. size = proplen / sizeof(u32);
  72. if (size > max_keys) {
  73. dev_err(dev, "OF: %s size overflow\n", propname);
  74. return -EINVAL;
  75. }
  76. for (i = 0; i < size; i++) {
  77. unsigned int key = be32_to_cpup(prop + i);
  78. if (!matrix_keypad_map_key(input_dev, rows, cols,
  79. row_shift, key))
  80. return -EINVAL;
  81. }
  82. return 0;
  83. }
  84. #else
  85. static int matrix_keypad_parse_of_keymap(const char *propname,
  86. unsigned int rows, unsigned int cols,
  87. struct input_dev *input_dev)
  88. {
  89. return -ENOSYS;
  90. }
  91. #endif
  92. /**
  93. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  94. * @keymap_data: keymap supplied by the platform code
  95. * @keymap_name: name of device tree property containing keymap (if device
  96. * tree support is enabled).
  97. * @rows: number of rows in target keymap array
  98. * @cols: number of cols in target keymap array
  99. * @keymap: expanded version of keymap that is suitable for use by
  100. * matrix keyboard driver
  101. * @input_dev: input devices for which we are setting up the keymap
  102. *
  103. * This function converts platform keymap (encoded with KEY() macro) into
  104. * an array of keycodes that is suitable for using in a standard matrix
  105. * keyboard driver that uses row and col as indices.
  106. *
  107. * If @keymap_data is not supplied and device tree support is enabled
  108. * it will attempt load the keymap from property specified by @keymap_name
  109. * argument (or "linux,keymap" if @keymap_name is %NULL).
  110. *
  111. * Callers are expected to set up input_dev->dev.parent before calling this
  112. * function.
  113. */
  114. int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  115. const char *keymap_name,
  116. unsigned int rows, unsigned int cols,
  117. unsigned short *keymap,
  118. struct input_dev *input_dev)
  119. {
  120. unsigned int row_shift = get_count_order(cols);
  121. int i;
  122. int error;
  123. input_dev->keycode = keymap;
  124. input_dev->keycodesize = sizeof(*keymap);
  125. input_dev->keycodemax = rows << row_shift;
  126. __set_bit(EV_KEY, input_dev->evbit);
  127. if (keymap_data) {
  128. for (i = 0; i < keymap_data->keymap_size; i++) {
  129. unsigned int key = keymap_data->keymap[i];
  130. if (!matrix_keypad_map_key(input_dev, rows, cols,
  131. row_shift, key))
  132. return -EINVAL;
  133. }
  134. } else {
  135. error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols,
  136. input_dev);
  137. if (error)
  138. return error;
  139. }
  140. __clear_bit(KEY_RESERVED, input_dev->keybit);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(matrix_keypad_build_keymap);
  144. MODULE_LICENSE("GPL");