matrix-keymap.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Helpers for matrix keyboard bindings
  4. *
  5. * Copyright (C) 2012 Google, Inc
  6. *
  7. * Author:
  8. * Olof Johansson <olof@lixom.net>
  9. */
  10. #include <linux/device.h>
  11. #include <linux/export.h>
  12. #include <linux/gfp.h>
  13. #include <linux/input.h>
  14. #include <linux/input/matrix_keypad.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/property.h>
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. static bool matrix_keypad_map_key(struct input_dev *input_dev,
  21. unsigned int rows, unsigned int cols,
  22. unsigned int row_shift, unsigned int key)
  23. {
  24. unsigned short *keymap = input_dev->keycode;
  25. unsigned int row = KEY_ROW(key);
  26. unsigned int col = KEY_COL(key);
  27. unsigned short code = KEY_VAL(key);
  28. if (row >= rows || col >= cols) {
  29. dev_err(input_dev->dev.parent,
  30. "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n",
  31. __func__, key, row, col, rows, cols);
  32. return false;
  33. }
  34. keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  35. __set_bit(code, input_dev->keybit);
  36. return true;
  37. }
  38. /**
  39. * matrix_keypad_parse_properties() - Read properties of matrix keypad
  40. *
  41. * @dev: Device containing properties
  42. * @rows: Returns number of matrix rows
  43. * @cols: Returns number of matrix columns
  44. * @return 0 if OK, <0 on error
  45. */
  46. int matrix_keypad_parse_properties(struct device *dev,
  47. unsigned int *rows, unsigned int *cols)
  48. {
  49. *rows = *cols = 0;
  50. device_property_read_u32(dev, "keypad,num-rows", rows);
  51. device_property_read_u32(dev, "keypad,num-columns", cols);
  52. if (!*rows || !*cols) {
  53. dev_err(dev, "number of keypad rows/columns not specified\n");
  54. return -EINVAL;
  55. }
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties);
  59. static int matrix_keypad_parse_keymap(const char *propname,
  60. unsigned int rows, unsigned int cols,
  61. struct input_dev *input_dev)
  62. {
  63. struct device *dev = input_dev->dev.parent;
  64. unsigned int row_shift = get_count_order(cols);
  65. unsigned int max_keys = rows << row_shift;
  66. u32 *keys;
  67. int i;
  68. int size;
  69. int retval;
  70. if (!propname)
  71. propname = "linux,keymap";
  72. size = device_property_count_u32(dev, propname);
  73. if (size <= 0) {
  74. dev_err(dev, "missing or malformed property %s: %d\n",
  75. propname, size);
  76. return size < 0 ? size : -EINVAL;
  77. }
  78. if (size > max_keys) {
  79. dev_err(dev, "%s size overflow (%d vs max %u)\n",
  80. propname, size, max_keys);
  81. return -EINVAL;
  82. }
  83. keys = kmalloc_array(size, sizeof(u32), GFP_KERNEL);
  84. if (!keys)
  85. return -ENOMEM;
  86. retval = device_property_read_u32_array(dev, propname, keys, size);
  87. if (retval) {
  88. dev_err(dev, "failed to read %s property: %d\n",
  89. propname, retval);
  90. goto out;
  91. }
  92. for (i = 0; i < size; i++) {
  93. if (!matrix_keypad_map_key(input_dev, rows, cols,
  94. row_shift, keys[i])) {
  95. retval = -EINVAL;
  96. goto out;
  97. }
  98. }
  99. retval = 0;
  100. out:
  101. kfree(keys);
  102. return retval;
  103. }
  104. /**
  105. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  106. * @keymap_data: keymap supplied by the platform code
  107. * @keymap_name: name of device tree property containing keymap (if device
  108. * tree support is enabled).
  109. * @rows: number of rows in target keymap array
  110. * @cols: number of cols in target keymap array
  111. * @keymap: expanded version of keymap that is suitable for use by
  112. * matrix keyboard driver
  113. * @input_dev: input devices for which we are setting up the keymap
  114. *
  115. * This function converts platform keymap (encoded with KEY() macro) into
  116. * an array of keycodes that is suitable for using in a standard matrix
  117. * keyboard driver that uses row and col as indices.
  118. *
  119. * If @keymap_data is not supplied and device tree support is enabled
  120. * it will attempt load the keymap from property specified by @keymap_name
  121. * argument (or "linux,keymap" if @keymap_name is %NULL).
  122. *
  123. * If @keymap is %NULL the function will automatically allocate managed
  124. * block of memory to store the keymap. This memory will be associated with
  125. * the parent device and automatically freed when device unbinds from the
  126. * driver.
  127. *
  128. * Callers are expected to set up input_dev->dev.parent before calling this
  129. * function.
  130. */
  131. int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  132. const char *keymap_name,
  133. unsigned int rows, unsigned int cols,
  134. unsigned short *keymap,
  135. struct input_dev *input_dev)
  136. {
  137. unsigned int row_shift = get_count_order(cols);
  138. size_t max_keys = rows << row_shift;
  139. int i;
  140. int error;
  141. if (WARN_ON(!input_dev->dev.parent))
  142. return -EINVAL;
  143. if (!keymap) {
  144. keymap = devm_kcalloc(input_dev->dev.parent,
  145. max_keys, sizeof(*keymap),
  146. GFP_KERNEL);
  147. if (!keymap) {
  148. dev_err(input_dev->dev.parent,
  149. "Unable to allocate memory for keymap");
  150. return -ENOMEM;
  151. }
  152. }
  153. input_dev->keycode = keymap;
  154. input_dev->keycodesize = sizeof(*keymap);
  155. input_dev->keycodemax = max_keys;
  156. __set_bit(EV_KEY, input_dev->evbit);
  157. if (keymap_data) {
  158. for (i = 0; i < keymap_data->keymap_size; i++) {
  159. unsigned int key = keymap_data->keymap[i];
  160. if (!matrix_keypad_map_key(input_dev, rows, cols,
  161. row_shift, key))
  162. return -EINVAL;
  163. }
  164. } else {
  165. error = matrix_keypad_parse_keymap(keymap_name, rows, cols,
  166. input_dev);
  167. if (error)
  168. return error;
  169. }
  170. __clear_bit(KEY_RESERVED, input_dev->keybit);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(matrix_keypad_build_keymap);
  174. MODULE_LICENSE("GPL");