matrix-keymap.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/input.h>
  22. #include <linux/of.h>
  23. #include <linux/export.h>
  24. #include <linux/gfp.h>
  25. #include <linux/slab.h>
  26. #include <linux/input/matrix_keypad.h>
  27. /**
  28. * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
  29. * @keymap_data: keymap supplied by the platform code
  30. * @keymap_name: name of device tree property containing keymap (if device
  31. * tree support is enabled).
  32. * @rows: number of rows in target keymap array
  33. * @cols: number of cols in target keymap array
  34. * @keymap: expanded version of keymap that is suitable for use by
  35. * matrix keyboard driver
  36. * @input_dev: input devices for which we are setting up the keymap
  37. *
  38. * This function converts platform keymap (encoded with KEY() macro) into
  39. * an array of keycodes that is suitable for using in a standard matrix
  40. * keyboard driver that uses row and col as indices.
  41. */
  42. int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
  43. const char *keymap_name,
  44. unsigned int rows, unsigned int cols,
  45. unsigned short *keymap,
  46. struct input_dev *input_dev)
  47. {
  48. unsigned int row_shift = get_count_order(cols);
  49. int i;
  50. input_dev->keycode = keymap;
  51. input_dev->keycodesize = sizeof(*keymap);
  52. input_dev->keycodemax = rows << row_shift;
  53. __set_bit(EV_KEY, input_dev->evbit);
  54. for (i = 0; i < keymap_data->keymap_size; i++) {
  55. unsigned int key = keymap_data->keymap[i];
  56. unsigned int row = KEY_ROW(key);
  57. unsigned int col = KEY_COL(key);
  58. unsigned short code = KEY_VAL(key);
  59. if (row >= rows || col >= cols) {
  60. dev_err(input_dev->dev.parent,
  61. "%s: invalid keymap entry %d (row: %d, col: %d, rows: %d, cols: %d)\n",
  62. __func__, i, row, col, rows, cols);
  63. return -EINVAL;
  64. }
  65. keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
  66. __set_bit(code, input_dev->keybit);
  67. }
  68. __clear_bit(KEY_RESERVED, input_dev->keybit);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(matrix_keypad_build_keymap);
  72. #ifdef CONFIG_OF
  73. struct matrix_keymap_data *
  74. matrix_keyboard_of_fill_keymap(struct device_node *np,
  75. const char *propname)
  76. {
  77. struct matrix_keymap_data *kd;
  78. u32 *keymap;
  79. int proplen, i;
  80. const __be32 *prop;
  81. if (!np)
  82. return NULL;
  83. if (!propname)
  84. propname = "linux,keymap";
  85. prop = of_get_property(np, propname, &proplen);
  86. if (!prop)
  87. return NULL;
  88. if (proplen % sizeof(u32)) {
  89. pr_warn("Malformed keymap property %s in %s\n",
  90. propname, np->full_name);
  91. return NULL;
  92. }
  93. kd = kzalloc(sizeof(*kd), GFP_KERNEL);
  94. if (!kd)
  95. return NULL;
  96. kd->keymap = keymap = kzalloc(proplen, GFP_KERNEL);
  97. if (!kd->keymap) {
  98. kfree(kd);
  99. return NULL;
  100. }
  101. kd->keymap_size = proplen / sizeof(u32);
  102. for (i = 0; i < kd->keymap_size; i++) {
  103. u32 tmp = be32_to_cpup(prop + i);
  104. int key_code, row, col;
  105. row = (tmp >> 24) & 0xff;
  106. col = (tmp >> 16) & 0xff;
  107. key_code = tmp & 0xffff;
  108. keymap[i] = KEY(row, col, key_code);
  109. }
  110. return kd;
  111. }
  112. EXPORT_SYMBOL_GPL(matrix_keyboard_of_fill_keymap);
  113. void matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd)
  114. {
  115. if (kd) {
  116. kfree(kd->keymap);
  117. kfree(kd);
  118. }
  119. }
  120. EXPORT_SYMBOL_GPL(matrix_keyboard_of_free_keymap);
  121. #endif