of_keymap.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Helpers for open firmware 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/input/matrix_keypad.h>
  24. #include <linux/export.h>
  25. #include <linux/gfp.h>
  26. #include <linux/slab.h>
  27. struct matrix_keymap_data *
  28. matrix_keyboard_of_fill_keymap(struct device_node *np,
  29. const char *propname)
  30. {
  31. struct matrix_keymap_data *kd;
  32. u32 *keymap;
  33. int proplen, i;
  34. const __be32 *prop;
  35. if (!np)
  36. return NULL;
  37. if (!propname)
  38. propname = "linux,keymap";
  39. prop = of_get_property(np, propname, &proplen);
  40. if (!prop)
  41. return NULL;
  42. if (proplen % sizeof(u32)) {
  43. pr_warn("Malformed keymap property %s in %s\n",
  44. propname, np->full_name);
  45. return NULL;
  46. }
  47. kd = kzalloc(sizeof(*kd), GFP_KERNEL);
  48. if (!kd)
  49. return NULL;
  50. kd->keymap = keymap = kzalloc(proplen, GFP_KERNEL);
  51. if (!kd->keymap) {
  52. kfree(kd);
  53. return NULL;
  54. }
  55. kd->keymap_size = proplen / sizeof(u32);
  56. for (i = 0; i < kd->keymap_size; i++) {
  57. u32 tmp = be32_to_cpup(prop + i);
  58. int key_code, row, col;
  59. row = (tmp >> 24) & 0xff;
  60. col = (tmp >> 16) & 0xff;
  61. key_code = tmp & 0xffff;
  62. keymap[i] = KEY(row, col, key_code);
  63. }
  64. return kd;
  65. }
  66. EXPORT_SYMBOL_GPL(matrix_keyboard_of_fill_keymap);
  67. void matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd)
  68. {
  69. if (kd) {
  70. kfree(kd->keymap);
  71. kfree(kd);
  72. }
  73. }
  74. EXPORT_SYMBOL_GPL(matrix_keyboard_of_free_keymap);