matrix-keymap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/input/matrix_keypad.h>
  26. static bool matrix_keypad_map_key(struct input_dev *input_dev,
  27. unsigned int rows, unsigned int cols,
  28. unsigned int row_shift, unsigned int key)
  29. {
  30. unsigned short *keymap = input_dev->keycode;
  31. unsigned int row = KEY_ROW(key);
  32. unsigned int col = KEY_COL(key);
  33. unsigned short code = KEY_VAL(key);
  34. if (row >= rows || col >= cols) {
  35. dev_err(input_dev->dev.parent,
  36. "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n",
  37. __func__, key, row, col, rows, cols);
  38. return false;
  39. }
  40. keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  41. __set_bit(code, input_dev->keybit);
  42. return true;
  43. }
  44. #ifdef CONFIG_OF
  45. static int matrix_keypad_parse_of_keymap(const char *propname,
  46. unsigned int rows, unsigned int cols,
  47. struct input_dev *input_dev)
  48. {
  49. struct device *dev = input_dev->dev.parent;
  50. struct device_node *np = dev->of_node;
  51. unsigned int row_shift = get_count_order(cols);
  52. unsigned int max_keys = rows << row_shift;
  53. unsigned int proplen, i, size;
  54. const __be32 *prop;
  55. if (!np)
  56. return -ENOENT;
  57. if (!propname)
  58. propname = "linux,keymap";
  59. prop = of_get_property(np, propname, &proplen);
  60. if (!prop) {
  61. dev_err(dev, "OF: %s property not defined in %s\n",
  62. propname, np->full_name);
  63. return -ENOENT;
  64. }
  65. if (proplen % sizeof(u32)) {
  66. dev_err(dev, "OF: Malformed keycode property %s in %s\n",
  67. propname, np->full_name);
  68. return -EINVAL;
  69. }
  70. size = proplen / sizeof(u32);
  71. if (size > max_keys) {
  72. dev_err(dev, "OF: %s size overflow\n", propname);
  73. return -EINVAL;
  74. }
  75. for (i = 0; i < size; i++) {
  76. unsigned int key = be32_to_cpup(prop + i);
  77. if (!matrix_keypad_map_key(input_dev, rows, cols,
  78. row_shift, key))
  79. return -EINVAL;
  80. }
  81. return 0;
  82. }
  83. #else
  84. static int matrix_keypad_parse_of_keymap(const char *propname,
  85. unsigned int rows, unsigned int cols,
  86. struct input_dev *input_dev)
  87. {
  88. return -ENOSYS;
  89. }
  90. #endif
  91. /**
  92. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  93. * @keymap_data: keymap supplied by the platform code
  94. * @keymap_name: name of device tree property containing keymap (if device
  95. * tree support is enabled).
  96. * @rows: number of rows in target keymap array
  97. * @cols: number of cols in target keymap array
  98. * @keymap: expanded version of keymap that is suitable for use by
  99. * matrix keyboard driver
  100. * @input_dev: input devices for which we are setting up the keymap
  101. *
  102. * This function converts platform keymap (encoded with KEY() macro) into
  103. * an array of keycodes that is suitable for using in a standard matrix
  104. * keyboard driver that uses row and col as indices.
  105. *
  106. * If @keymap_data is not supplied and device tree support is enabled
  107. * it will attempt load the keymap from property specified by @keymap_name
  108. * argument (or "linux,keymap" if @keymap_name is %NULL).
  109. *
  110. * Callers are expected to set up input_dev->dev.parent before calling this
  111. * function.
  112. */
  113. int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  114. const char *keymap_name,
  115. unsigned int rows, unsigned int cols,
  116. unsigned short *keymap,
  117. struct input_dev *input_dev)
  118. {
  119. unsigned int row_shift = get_count_order(cols);
  120. int i;
  121. int error;
  122. input_dev->keycode = keymap;
  123. input_dev->keycodesize = sizeof(*keymap);
  124. input_dev->keycodemax = rows << row_shift;
  125. __set_bit(EV_KEY, input_dev->evbit);
  126. if (keymap_data) {
  127. for (i = 0; i < keymap_data->keymap_size; i++) {
  128. unsigned int key = keymap_data->keymap[i];
  129. if (!matrix_keypad_map_key(input_dev, rows, cols,
  130. row_shift, key))
  131. return -EINVAL;
  132. }
  133. } else {
  134. error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols,
  135. input_dev);
  136. if (error)
  137. return error;
  138. }
  139. __clear_bit(KEY_RESERVED, input_dev->keybit);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(matrix_keypad_build_keymap);