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> |
Dima Zavin | 42f13d3 | 2009-01-29 11:47:57 -0800 | [diff] [blame] | 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 36 | #include <dev/keys.h> |
| 37 | #include <dev/gpio.h> |
| 38 | #include <dev/gpio_keypad.h> |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame^] | 39 | #include <kernel/event.h> |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 40 | #include <kernel/timer.h> |
| 41 | |
| 42 | struct gpio_kp { |
| 43 | struct gpio_keypad_info *keypad_info; |
| 44 | struct timer timer; |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame^] | 45 | event_t full_scan; |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 46 | int current_output; |
| 47 | unsigned int some_keys_pressed:2; |
| 48 | unsigned long keys_pressed[0]; |
| 49 | }; |
| 50 | |
| 51 | /* TODO: Support multiple keypads? */ |
| 52 | static struct gpio_kp *keypad; |
| 53 | |
| 54 | static void check_output(struct gpio_kp *kp, int out, int polarity) |
| 55 | { |
| 56 | struct gpio_keypad_info *kpinfo = kp->keypad_info; |
| 57 | int key_index; |
| 58 | int in; |
| 59 | int gpio; |
| 60 | int changed = 0; |
| 61 | |
| 62 | key_index = out * kpinfo->ninputs; |
| 63 | for (in = 0; in < kpinfo->ninputs; in++, key_index++) { |
| 64 | gpio = kpinfo->input_gpios[in]; |
| 65 | changed = 0; |
| 66 | if (gpio_get(gpio) ^ !polarity) { |
| 67 | if (kp->some_keys_pressed < 3) |
| 68 | kp->some_keys_pressed++; |
| 69 | changed = !bitmap_set(kp->keys_pressed, key_index); |
| 70 | } else { |
| 71 | changed = bitmap_clear(kp->keys_pressed, key_index); |
| 72 | } |
| 73 | if (changed) { |
| 74 | int state = bitmap_test(kp->keys_pressed, key_index); |
| 75 | keys_post_event(kpinfo->keymap[key_index], state); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* sets up the right state for the next poll cycle */ |
| 80 | gpio = kpinfo->output_gpios[out]; |
| 81 | if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE) |
| 82 | gpio_set(gpio, !polarity); |
| 83 | else |
| 84 | gpio_config(gpio, GPIO_INPUT); |
| 85 | } |
| 86 | |
| 87 | static enum handler_return |
| 88 | gpio_keypad_timer_func(struct timer *timer, time_t now, void *arg) |
| 89 | { |
| 90 | struct gpio_kp *kp = keypad; |
| 91 | struct gpio_keypad_info *kpinfo = kp->keypad_info; |
| 92 | int polarity = !!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH); |
Dima Zavin | 42f13d3 | 2009-01-29 11:47:57 -0800 | [diff] [blame] | 93 | int out; |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 94 | int gpio; |
| 95 | |
| 96 | out = kp->current_output; |
| 97 | if (out == kpinfo->noutputs) { |
| 98 | out = 0; |
| 99 | kp->some_keys_pressed = 0; |
| 100 | } else { |
| 101 | check_output(kp, out, polarity); |
| 102 | out++; |
| 103 | } |
| 104 | |
| 105 | kp->current_output = out; |
| 106 | if (out < kpinfo->noutputs) { |
| 107 | gpio = kpinfo->output_gpios[out]; |
| 108 | if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE) |
| 109 | gpio_set(gpio, polarity); |
| 110 | else |
| 111 | gpio_config(gpio, polarity ? GPIO_OUTPUT : 0); |
| 112 | timer_set_oneshot(timer, kpinfo->settle_time, |
| 113 | gpio_keypad_timer_func, NULL); |
| 114 | goto done; |
| 115 | } |
| 116 | |
| 117 | if (/*!kp->use_irq*/ 1 || kp->some_keys_pressed) { |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame^] | 118 | event_signal(&kp->full_scan, false); |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 119 | timer_set_oneshot(timer, kpinfo->poll_time, |
| 120 | gpio_keypad_timer_func, NULL); |
| 121 | goto done; |
| 122 | } |
| 123 | |
| 124 | #if 0 |
| 125 | /* No keys are pressed, reenable interrupt */ |
| 126 | for (out = 0; out < kpinfo->noutputs; out++) { |
| 127 | if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE) |
| 128 | gpio_set(kpinfo->output_gpios[out], polarity); |
| 129 | else |
| 130 | gpio_config(kpinfo->output_gpios[out], polarity ? GPIO_OUTPUT : 0); |
| 131 | } |
| 132 | for (in = 0; in < kpinfo->ninputs; in++) |
| 133 | enable_irq(gpio_to_irq(kpinfo->input_gpios[in])); |
| 134 | return INT_RESCHEDULE; |
| 135 | #endif |
| 136 | |
| 137 | done: |
| 138 | return INT_RESCHEDULE; |
| 139 | } |
| 140 | |
| 141 | void gpio_keypad_init(struct gpio_keypad_info *kpinfo) |
| 142 | { |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 143 | int key_count; |
| 144 | int output_val; |
| 145 | int output_cfg; |
| 146 | int i; |
| 147 | int len; |
| 148 | |
| 149 | ASSERT(kpinfo->keymap && kpinfo->input_gpios && kpinfo->output_gpios); |
| 150 | key_count = kpinfo->ninputs * kpinfo->noutputs; |
| 151 | |
| 152 | len = sizeof(struct gpio_kp) + (sizeof(unsigned long) * |
| 153 | BITMAP_NUM_WORDS(key_count)); |
| 154 | keypad = malloc(len); |
| 155 | ASSERT(keypad); |
| 156 | |
| 157 | memset(keypad, 0, len); |
| 158 | keypad->keypad_info = kpinfo; |
| 159 | |
| 160 | output_val = (!!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH)) ^ |
| 161 | (!!(kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)); |
| 162 | output_cfg = kpinfo->flags & GPIOKPF_DRIVE_INACTIVE ? GPIO_OUTPUT : 0; |
| 163 | for (i = 0; i < kpinfo->noutputs; i++) { |
| 164 | gpio_set(kpinfo->output_gpios[i], output_val); |
| 165 | gpio_config(kpinfo->output_gpios[i], output_cfg); |
| 166 | } |
| 167 | for (i = 0; i < kpinfo->ninputs; i++) |
| 168 | gpio_config(kpinfo->input_gpios[i], GPIO_INPUT); |
| 169 | |
| 170 | keypad->current_output = kpinfo->noutputs; |
| 171 | |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame^] | 172 | event_init(&keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL); |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 173 | timer_initialize(&keypad->timer); |
| 174 | timer_set_oneshot(&keypad->timer, 0, gpio_keypad_timer_func, NULL); |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame^] | 175 | |
| 176 | /* wait for the keypad to complete one full scan */ |
| 177 | event_wait(&keypad->full_scan); |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 178 | } |