blob: b8677697cfbc15f3e2b8c9d3c8b22a5c5bd5d092 [file] [log] [blame]
Dima Zavin0f88be22009-01-20 19:25:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <assert.h>
33#include <bits.h>
Dima Zavin42f13d32009-01-29 11:47:57 -080034#include <stdlib.h>
35#include <string.h>
Dima Zavin0f88be22009-01-20 19:25:50 -080036#include <dev/keys.h>
37#include <dev/gpio.h>
38#include <dev/gpio_keypad.h>
39#include <kernel/timer.h>
40
41struct gpio_kp {
42 struct gpio_keypad_info *keypad_info;
43 struct timer timer;
44 int current_output;
45 unsigned int some_keys_pressed:2;
46 unsigned long keys_pressed[0];
47};
48
49/* TODO: Support multiple keypads? */
50static struct gpio_kp *keypad;
51
52static void check_output(struct gpio_kp *kp, int out, int polarity)
53{
54 struct gpio_keypad_info *kpinfo = kp->keypad_info;
55 int key_index;
56 int in;
57 int gpio;
58 int changed = 0;
59
60 key_index = out * kpinfo->ninputs;
61 for (in = 0; in < kpinfo->ninputs; in++, key_index++) {
62 gpio = kpinfo->input_gpios[in];
63 changed = 0;
64 if (gpio_get(gpio) ^ !polarity) {
65 if (kp->some_keys_pressed < 3)
66 kp->some_keys_pressed++;
67 changed = !bitmap_set(kp->keys_pressed, key_index);
68 } else {
69 changed = bitmap_clear(kp->keys_pressed, key_index);
70 }
71 if (changed) {
72 int state = bitmap_test(kp->keys_pressed, key_index);
73 keys_post_event(kpinfo->keymap[key_index], state);
74 }
75 }
76
77 /* sets up the right state for the next poll cycle */
78 gpio = kpinfo->output_gpios[out];
79 if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)
80 gpio_set(gpio, !polarity);
81 else
82 gpio_config(gpio, GPIO_INPUT);
83}
84
85static enum handler_return
86gpio_keypad_timer_func(struct timer *timer, time_t now, void *arg)
87{
88 struct gpio_kp *kp = keypad;
89 struct gpio_keypad_info *kpinfo = kp->keypad_info;
90 int polarity = !!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH);
Dima Zavin42f13d32009-01-29 11:47:57 -080091 int out;
Dima Zavin0f88be22009-01-20 19:25:50 -080092 int gpio;
93
94 out = kp->current_output;
95 if (out == kpinfo->noutputs) {
96 out = 0;
97 kp->some_keys_pressed = 0;
98 } else {
99 check_output(kp, out, polarity);
100 out++;
101 }
102
103 kp->current_output = out;
104 if (out < kpinfo->noutputs) {
105 gpio = kpinfo->output_gpios[out];
106 if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)
107 gpio_set(gpio, polarity);
108 else
109 gpio_config(gpio, polarity ? GPIO_OUTPUT : 0);
110 timer_set_oneshot(timer, kpinfo->settle_time,
111 gpio_keypad_timer_func, NULL);
112 goto done;
113 }
114
115 if (/*!kp->use_irq*/ 1 || kp->some_keys_pressed) {
116 timer_set_oneshot(timer, kpinfo->poll_time,
117 gpio_keypad_timer_func, NULL);
118 goto done;
119 }
120
121#if 0
122 /* No keys are pressed, reenable interrupt */
123 for (out = 0; out < kpinfo->noutputs; out++) {
124 if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
125 gpio_set(kpinfo->output_gpios[out], polarity);
126 else
127 gpio_config(kpinfo->output_gpios[out], polarity ? GPIO_OUTPUT : 0);
128 }
129 for (in = 0; in < kpinfo->ninputs; in++)
130 enable_irq(gpio_to_irq(kpinfo->input_gpios[in]));
131 return INT_RESCHEDULE;
132#endif
133
134done:
135 return INT_RESCHEDULE;
136}
137
138void gpio_keypad_init(struct gpio_keypad_info *kpinfo)
139{
Dima Zavin0f88be22009-01-20 19:25:50 -0800140 int key_count;
141 int output_val;
142 int output_cfg;
143 int i;
144 int len;
145
146 ASSERT(kpinfo->keymap && kpinfo->input_gpios && kpinfo->output_gpios);
147 key_count = kpinfo->ninputs * kpinfo->noutputs;
148
149 len = sizeof(struct gpio_kp) + (sizeof(unsigned long) *
150 BITMAP_NUM_WORDS(key_count));
151 keypad = malloc(len);
152 ASSERT(keypad);
153
154 memset(keypad, 0, len);
155 keypad->keypad_info = kpinfo;
156
157 output_val = (!!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH)) ^
158 (!!(kpinfo->flags & GPIOKPF_DRIVE_INACTIVE));
159 output_cfg = kpinfo->flags & GPIOKPF_DRIVE_INACTIVE ? GPIO_OUTPUT : 0;
160 for (i = 0; i < kpinfo->noutputs; i++) {
161 gpio_set(kpinfo->output_gpios[i], output_val);
162 gpio_config(kpinfo->output_gpios[i], output_cfg);
163 }
164 for (i = 0; i < kpinfo->ninputs; i++)
165 gpio_config(kpinfo->input_gpios[i], GPIO_INPUT);
166
167 keypad->current_output = kpinfo->noutputs;
168
169 timer_initialize(&keypad->timer);
170 timer_set_oneshot(&keypad->timer, 0, gpio_keypad_timer_func, NULL);
171}