matrix-keymap.c 5.3 KB

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