matrix-keymap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. static int matrix_keypad_parse_of_keymap(const char *propname,
  64. unsigned int rows, unsigned int cols,
  65. struct input_dev *input_dev)
  66. {
  67. struct device *dev = input_dev->dev.parent;
  68. struct device_node *np = dev->of_node;
  69. unsigned int row_shift = get_count_order(cols);
  70. unsigned int max_keys = rows << row_shift;
  71. unsigned int proplen, i, size;
  72. const __be32 *prop;
  73. if (!np)
  74. return -ENOENT;
  75. if (!propname)
  76. propname = "linux,keymap";
  77. prop = of_get_property(np, propname, &proplen);
  78. if (!prop) {
  79. dev_err(dev, "OF: %s property not defined in %s\n",
  80. propname, np->full_name);
  81. return -ENOENT;
  82. }
  83. if (proplen % sizeof(u32)) {
  84. dev_err(dev, "OF: Malformed keycode property %s in %s\n",
  85. propname, np->full_name);
  86. return -EINVAL;
  87. }
  88. size = proplen / sizeof(u32);
  89. if (size > max_keys) {
  90. dev_err(dev, "OF: %s size overflow\n", propname);
  91. return -EINVAL;
  92. }
  93. for (i = 0; i < size; i++) {
  94. unsigned int key = be32_to_cpup(prop + i);
  95. if (!matrix_keypad_map_key(input_dev, rows, cols,
  96. row_shift, key))
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. #else
  102. static int matrix_keypad_parse_of_keymap(const char *propname,
  103. unsigned int rows, unsigned int cols,
  104. struct input_dev *input_dev)
  105. {
  106. return -ENOSYS;
  107. }
  108. #endif
  109. /**
  110. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  111. * @keymap_data: keymap supplied by the platform code
  112. * @keymap_name: name of device tree property containing keymap (if device
  113. * tree support is enabled).
  114. * @rows: number of rows in target keymap array
  115. * @cols: number of cols in target keymap array
  116. * @keymap: expanded version of keymap that is suitable for use by
  117. * matrix keyboard driver
  118. * @input_dev: input devices for which we are setting up the keymap
  119. *
  120. * This function converts platform keymap (encoded with KEY() macro) into
  121. * an array of keycodes that is suitable for using in a standard matrix
  122. * keyboard driver that uses row and col as indices.
  123. *
  124. * If @keymap_data is not supplied and device tree support is enabled
  125. * it will attempt load the keymap from property specified by @keymap_name
  126. * argument (or "linux,keymap" if @keymap_name is %NULL).
  127. *
  128. * If @keymap is %NULL the function will automatically allocate managed
  129. * block of memory to store the keymap. This memory will be associated with
  130. * the parent device and automatically freed when device unbinds from the
  131. * driver.
  132. *
  133. * Callers are expected to set up input_dev->dev.parent before calling this
  134. * function.
  135. */
  136. int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  137. const char *keymap_name,
  138. unsigned int rows, unsigned int cols,
  139. unsigned short *keymap,
  140. struct input_dev *input_dev)
  141. {
  142. unsigned int row_shift = get_count_order(cols);
  143. size_t max_keys = rows << row_shift;
  144. int i;
  145. int error;
  146. if (WARN_ON(!input_dev->dev.parent))
  147. return -EINVAL;
  148. if (!keymap) {
  149. keymap = devm_kzalloc(input_dev->dev.parent,
  150. max_keys * sizeof(*keymap),
  151. GFP_KERNEL);
  152. if (!keymap) {
  153. dev_err(input_dev->dev.parent,
  154. "Unable to allocate memory for keymap");
  155. return -ENOMEM;
  156. }
  157. }
  158. input_dev->keycode = keymap;
  159. input_dev->keycodesize = sizeof(*keymap);
  160. input_dev->keycodemax = max_keys;
  161. __set_bit(EV_KEY, input_dev->evbit);
  162. if (keymap_data) {
  163. for (i = 0; i < keymap_data->keymap_size; i++) {
  164. unsigned int key = keymap_data->keymap[i];
  165. if (!matrix_keypad_map_key(input_dev, rows, cols,
  166. row_shift, key))
  167. return -EINVAL;
  168. }
  169. } else {
  170. error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols,
  171. input_dev);
  172. if (error)
  173. return error;
  174. }
  175. __clear_bit(KEY_RESERVED, input_dev->keybit);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(matrix_keypad_build_keymap);
  179. MODULE_LICENSE("GPL");