matrix-keymap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/gfp.h>
  21. #include <linux/kernel.h>
  22. #include <linux/types.h>
  23. #include <linux/input.h>
  24. #include <linux/of.h>
  25. #include <linux/export.h>
  26. #include <linux/module.h>
  27. #include <linux/input/matrix_keypad.h>
  28. static bool matrix_keypad_map_key(struct input_dev *input_dev,
  29. unsigned int rows, unsigned int cols,
  30. unsigned int row_shift, unsigned int key)
  31. {
  32. unsigned short *keymap = input_dev->keycode;
  33. unsigned int row = KEY_ROW(key);
  34. unsigned int col = KEY_COL(key);
  35. unsigned short code = KEY_VAL(key);
  36. if (row >= rows || col >= cols) {
  37. dev_err(input_dev->dev.parent,
  38. "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n",
  39. __func__, key, row, col, rows, cols);
  40. return false;
  41. }
  42. keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  43. __set_bit(code, input_dev->keybit);
  44. return true;
  45. }
  46. #ifdef CONFIG_OF
  47. int matrix_keypad_parse_of_params(struct device *dev,
  48. unsigned int *rows, unsigned int *cols)
  49. {
  50. struct device_node *np = dev->of_node;
  51. if (!np) {
  52. dev_err(dev, "missing DT data");
  53. return -EINVAL;
  54. }
  55. of_property_read_u32(np, "keypad,num-rows", rows);
  56. of_property_read_u32(np, "keypad,num-columns", cols);
  57. if (!*rows || !*cols) {
  58. dev_err(dev, "number of keypad rows/columns not specified\n");
  59. return -EINVAL;
  60. }
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(matrix_keypad_parse_of_params);
  64. static int matrix_keypad_parse_of_keymap(const char *propname,
  65. unsigned int rows, unsigned int cols,
  66. struct input_dev *input_dev)
  67. {
  68. struct device *dev = input_dev->dev.parent;
  69. struct device_node *np = dev->of_node;
  70. unsigned int row_shift = get_count_order(cols);
  71. unsigned int max_keys = rows << row_shift;
  72. unsigned int proplen, i, size;
  73. const __be32 *prop;
  74. if (!np)
  75. return -ENOENT;
  76. if (!propname)
  77. propname = "linux,keymap";
  78. prop = of_get_property(np, propname, &proplen);
  79. if (!prop) {
  80. dev_err(dev, "OF: %s property not defined in %s\n",
  81. propname, np->full_name);
  82. return -ENOENT;
  83. }
  84. if (proplen % sizeof(u32)) {
  85. dev_err(dev, "OF: Malformed keycode property %s in %s\n",
  86. propname, np->full_name);
  87. return -EINVAL;
  88. }
  89. size = proplen / sizeof(u32);
  90. if (size > max_keys) {
  91. dev_err(dev, "OF: %s size overflow\n", propname);
  92. return -EINVAL;
  93. }
  94. for (i = 0; i < size; i++) {
  95. unsigned int key = be32_to_cpup(prop + i);
  96. if (!matrix_keypad_map_key(input_dev, rows, cols,
  97. row_shift, key))
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. #else
  103. static int matrix_keypad_parse_of_keymap(const char *propname,
  104. unsigned int rows, unsigned int cols,
  105. struct input_dev *input_dev)
  106. {
  107. return -ENOSYS;
  108. }
  109. #endif
  110. /**
  111. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  112. * @keymap_data: keymap supplied by the platform code
  113. * @keymap_name: name of device tree property containing keymap (if device
  114. * tree support is enabled).
  115. * @rows: number of rows in target keymap array
  116. * @cols: number of cols in target keymap array
  117. * @keymap: expanded version of keymap that is suitable for use by
  118. * matrix keyboard driver
  119. * @input_dev: input devices for which we are setting up the keymap
  120. *
  121. * This function converts platform keymap (encoded with KEY() macro) into
  122. * an array of keycodes that is suitable for using in a standard matrix
  123. * keyboard driver that uses row and col as indices.
  124. *
  125. * If @keymap_data is not supplied and device tree support is enabled
  126. * it will attempt load the keymap from property specified by @keymap_name
  127. * argument (or "linux,keymap" if @keymap_name is %NULL).
  128. *
  129. * If @keymap is %NULL the function will automatically allocate managed
  130. * block of memory to store the keymap. This memory will be associated with
  131. * the parent device and automatically freed when device unbinds from the
  132. * driver.
  133. *
  134. * Callers are expected to set up input_dev->dev.parent before calling this
  135. * function.
  136. */
  137. int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  138. const char *keymap_name,
  139. unsigned int rows, unsigned int cols,
  140. unsigned short *keymap,
  141. struct input_dev *input_dev)
  142. {
  143. unsigned int row_shift = get_count_order(cols);
  144. size_t max_keys = rows << row_shift;
  145. int i;
  146. int error;
  147. if (WARN_ON(!input_dev->dev.parent))
  148. return -EINVAL;
  149. if (!keymap) {
  150. keymap = devm_kzalloc(input_dev->dev.parent,
  151. max_keys * sizeof(*keymap),
  152. GFP_KERNEL);
  153. if (!keymap) {
  154. dev_err(input_dev->dev.parent,
  155. "Unable to allocate memory for keymap");
  156. return -ENOMEM;
  157. }
  158. }
  159. input_dev->keycode = keymap;
  160. input_dev->keycodesize = sizeof(*keymap);
  161. input_dev->keycodemax = max_keys;
  162. __set_bit(EV_KEY, input_dev->evbit);
  163. if (keymap_data) {
  164. for (i = 0; i < keymap_data->keymap_size; i++) {
  165. unsigned int key = keymap_data->keymap[i];
  166. if (!matrix_keypad_map_key(input_dev, rows, cols,
  167. row_shift, key))
  168. return -EINVAL;
  169. }
  170. } else {
  171. error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols,
  172. input_dev);
  173. if (error)
  174. return error;
  175. }
  176. __clear_bit(KEY_RESERVED, input_dev->keybit);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(matrix_keypad_build_keymap);
  180. MODULE_LICENSE("GPL");