matrix-keymap.c 5.1 KB

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