Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 34 | #include <dev/keys.h> |
| 35 | #include <dev/gpio.h> |
| 36 | #include <dev/gpio_keypad.h> |
| 37 | #include <kernel/timer.h> |
| 38 | |
| 39 | struct gpio_kp { |
| 40 | struct gpio_keypad_info *keypad_info; |
| 41 | struct timer timer; |
| 42 | int current_output; |
| 43 | unsigned int some_keys_pressed:2; |
| 44 | unsigned long keys_pressed[0]; |
| 45 | }; |
| 46 | |
| 47 | /* TODO: Support multiple keypads? */ |
| 48 | static struct gpio_kp *keypad; |
| 49 | |
| 50 | static void check_output(struct gpio_kp *kp, int out, int polarity) |
| 51 | { |
| 52 | struct gpio_keypad_info *kpinfo = kp->keypad_info; |
| 53 | int key_index; |
| 54 | int in; |
| 55 | int gpio; |
| 56 | int changed = 0; |
| 57 | |
| 58 | key_index = out * kpinfo->ninputs; |
| 59 | for (in = 0; in < kpinfo->ninputs; in++, key_index++) { |
| 60 | gpio = kpinfo->input_gpios[in]; |
| 61 | changed = 0; |
| 62 | if (gpio_get(gpio) ^ !polarity) { |
| 63 | if (kp->some_keys_pressed < 3) |
| 64 | kp->some_keys_pressed++; |
| 65 | changed = !bitmap_set(kp->keys_pressed, key_index); |
| 66 | } else { |
| 67 | changed = bitmap_clear(kp->keys_pressed, key_index); |
| 68 | } |
| 69 | if (changed) { |
| 70 | int state = bitmap_test(kp->keys_pressed, key_index); |
| 71 | keys_post_event(kpinfo->keymap[key_index], state); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* sets up the right state for the next poll cycle */ |
| 76 | gpio = kpinfo->output_gpios[out]; |
| 77 | if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE) |
| 78 | gpio_set(gpio, !polarity); |
| 79 | else |
| 80 | gpio_config(gpio, GPIO_INPUT); |
| 81 | } |
| 82 | |
| 83 | static enum handler_return |
| 84 | gpio_keypad_timer_func(struct timer *timer, time_t now, void *arg) |
| 85 | { |
| 86 | struct gpio_kp *kp = keypad; |
| 87 | struct gpio_keypad_info *kpinfo = kp->keypad_info; |
| 88 | int polarity = !!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH); |
| 89 | int out, in; |
| 90 | int key_index; |
| 91 | int gpio; |
| 92 | |
| 93 | out = kp->current_output; |
| 94 | if (out == kpinfo->noutputs) { |
| 95 | out = 0; |
| 96 | kp->some_keys_pressed = 0; |
| 97 | } else { |
| 98 | check_output(kp, out, polarity); |
| 99 | out++; |
| 100 | } |
| 101 | |
| 102 | kp->current_output = out; |
| 103 | if (out < kpinfo->noutputs) { |
| 104 | gpio = kpinfo->output_gpios[out]; |
| 105 | if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE) |
| 106 | gpio_set(gpio, polarity); |
| 107 | else |
| 108 | gpio_config(gpio, polarity ? GPIO_OUTPUT : 0); |
| 109 | timer_set_oneshot(timer, kpinfo->settle_time, |
| 110 | gpio_keypad_timer_func, NULL); |
| 111 | goto done; |
| 112 | } |
| 113 | |
| 114 | if (/*!kp->use_irq*/ 1 || kp->some_keys_pressed) { |
| 115 | timer_set_oneshot(timer, kpinfo->poll_time, |
| 116 | gpio_keypad_timer_func, NULL); |
| 117 | goto done; |
| 118 | } |
| 119 | |
| 120 | #if 0 |
| 121 | /* No keys are pressed, reenable interrupt */ |
| 122 | for (out = 0; out < kpinfo->noutputs; out++) { |
| 123 | if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE) |
| 124 | gpio_set(kpinfo->output_gpios[out], polarity); |
| 125 | else |
| 126 | gpio_config(kpinfo->output_gpios[out], polarity ? GPIO_OUTPUT : 0); |
| 127 | } |
| 128 | for (in = 0; in < kpinfo->ninputs; in++) |
| 129 | enable_irq(gpio_to_irq(kpinfo->input_gpios[in])); |
| 130 | return INT_RESCHEDULE; |
| 131 | #endif |
| 132 | |
| 133 | done: |
| 134 | return INT_RESCHEDULE; |
| 135 | } |
| 136 | |
| 137 | void gpio_keypad_init(struct gpio_keypad_info *kpinfo) |
| 138 | { |
| 139 | struct gpio_kp *kp; |
| 140 | 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 | } |