gpio_output.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* drivers/input/misc/gpio_output.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/gpio.h>
  17. #include <linux/gpio_event.h>
  18. int gpio_event_output_event(
  19. struct gpio_event_input_devs *input_devs, struct gpio_event_info *info,
  20. void **data, unsigned int dev, unsigned int type,
  21. unsigned int code, int value)
  22. {
  23. int i;
  24. struct gpio_event_output_info *oi;
  25. oi = container_of(info, struct gpio_event_output_info, info);
  26. if (type != oi->type)
  27. return 0;
  28. if (!(oi->flags & GPIOEDF_ACTIVE_HIGH))
  29. value = !value;
  30. for (i = 0; i < oi->keymap_size; i++)
  31. if (dev == oi->keymap[i].dev && code == oi->keymap[i].code)
  32. gpio_set_value(oi->keymap[i].gpio, value);
  33. return 0;
  34. }
  35. int gpio_event_output_func(
  36. struct gpio_event_input_devs *input_devs, struct gpio_event_info *info,
  37. void **data, int func)
  38. {
  39. int ret;
  40. int i;
  41. struct gpio_event_output_info *oi;
  42. oi = container_of(info, struct gpio_event_output_info, info);
  43. if (func == GPIO_EVENT_FUNC_SUSPEND || func == GPIO_EVENT_FUNC_RESUME)
  44. return 0;
  45. if (func == GPIO_EVENT_FUNC_INIT) {
  46. int output_level = !(oi->flags & GPIOEDF_ACTIVE_HIGH);
  47. for (i = 0; i < oi->keymap_size; i++) {
  48. int dev = oi->keymap[i].dev;
  49. if (dev >= input_devs->count) {
  50. pr_err("gpio_event_output_func: bad device "
  51. "index %d >= %d for key code %d\n",
  52. dev, input_devs->count,
  53. oi->keymap[i].code);
  54. ret = -EINVAL;
  55. goto err_bad_keymap;
  56. }
  57. input_set_capability(input_devs->dev[dev], oi->type,
  58. oi->keymap[i].code);
  59. }
  60. for (i = 0; i < oi->keymap_size; i++) {
  61. ret = gpio_request(oi->keymap[i].gpio,
  62. "gpio_event_output");
  63. if (ret) {
  64. pr_err("gpio_event_output_func: gpio_request "
  65. "failed for %d\n", oi->keymap[i].gpio);
  66. goto err_gpio_request_failed;
  67. }
  68. ret = gpio_direction_output(oi->keymap[i].gpio,
  69. output_level);
  70. if (ret) {
  71. pr_err("gpio_event_output_func: "
  72. "gpio_direction_output failed for %d\n",
  73. oi->keymap[i].gpio);
  74. goto err_gpio_direction_output_failed;
  75. }
  76. }
  77. return 0;
  78. }
  79. ret = 0;
  80. for (i = oi->keymap_size - 1; i >= 0; i--) {
  81. err_gpio_direction_output_failed:
  82. gpio_free(oi->keymap[i].gpio);
  83. err_gpio_request_failed:
  84. ;
  85. }
  86. err_bad_keymap:
  87. return ret;
  88. }