Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved. |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 6 | * |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this |
| 18 | * software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <bits.h> |
Dima Zavin | 42f13d3 | 2009-01-29 11:47:57 -0800 | [diff] [blame] | 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 38 | #include <dev/keys.h> |
| 39 | #include <dev/gpio.h> |
| 40 | #include <dev/gpio_keypad.h> |
Kinson Chik | ea64624 | 2011-09-01 13:53:16 -0700 | [diff] [blame] | 41 | #include <dev/ssbi.h> |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame] | 42 | #include <kernel/event.h> |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 43 | #include <kernel/timer.h> |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 44 | #include <reg.h> |
David Ng | 6e1711f | 2010-01-19 15:27:00 -0800 | [diff] [blame] | 45 | #include <platform/iomap.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 46 | #include <platform/timer.h> |
| 47 | #include <platform.h> |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 48 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 49 | #define LINUX_MACHTYPE_8660_QT 3298 |
| 50 | |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 51 | struct gpio_kp { |
| 52 | struct gpio_keypad_info *keypad_info; |
| 53 | struct timer timer; |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame] | 54 | event_t full_scan; |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 55 | int current_output; |
| 56 | unsigned int some_keys_pressed:2; |
| 57 | unsigned long keys_pressed[0]; |
| 58 | }; |
| 59 | |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 60 | struct gpio_qwerty_kp { |
| 61 | struct qwerty_keypad_info *keypad_info; |
| 62 | struct timer timer; |
| 63 | event_t full_scan; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 64 | unsigned int some_keys_pressed:2; |
| 65 | unsigned long keys_pressed[0]; |
| 66 | }; |
| 67 | |
| 68 | static struct gpio_qwerty_kp *qwerty_keypad; |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 69 | /* TODO: Support multiple keypads? */ |
| 70 | static struct gpio_kp *keypad; |
| 71 | |
| 72 | static void check_output(struct gpio_kp *kp, int out, int polarity) |
| 73 | { |
| 74 | struct gpio_keypad_info *kpinfo = kp->keypad_info; |
| 75 | int key_index; |
| 76 | int in; |
| 77 | int gpio; |
| 78 | int changed = 0; |
| 79 | |
| 80 | key_index = out * kpinfo->ninputs; |
| 81 | for (in = 0; in < kpinfo->ninputs; in++, key_index++) { |
| 82 | gpio = kpinfo->input_gpios[in]; |
| 83 | changed = 0; |
| 84 | if (gpio_get(gpio) ^ !polarity) { |
| 85 | if (kp->some_keys_pressed < 3) |
| 86 | kp->some_keys_pressed++; |
| 87 | changed = !bitmap_set(kp->keys_pressed, key_index); |
| 88 | } else { |
| 89 | changed = bitmap_clear(kp->keys_pressed, key_index); |
| 90 | } |
| 91 | if (changed) { |
| 92 | int state = bitmap_test(kp->keys_pressed, key_index); |
| 93 | keys_post_event(kpinfo->keymap[key_index], state); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* sets up the right state for the next poll cycle */ |
| 98 | gpio = kpinfo->output_gpios[out]; |
| 99 | if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE) |
| 100 | gpio_set(gpio, !polarity); |
| 101 | else |
| 102 | gpio_config(gpio, GPIO_INPUT); |
| 103 | } |
| 104 | |
| 105 | static enum handler_return |
| 106 | gpio_keypad_timer_func(struct timer *timer, time_t now, void *arg) |
| 107 | { |
| 108 | struct gpio_kp *kp = keypad; |
| 109 | struct gpio_keypad_info *kpinfo = kp->keypad_info; |
| 110 | int polarity = !!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH); |
Dima Zavin | 42f13d3 | 2009-01-29 11:47:57 -0800 | [diff] [blame] | 111 | int out; |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 112 | int gpio; |
| 113 | |
| 114 | out = kp->current_output; |
| 115 | if (out == kpinfo->noutputs) { |
| 116 | out = 0; |
| 117 | kp->some_keys_pressed = 0; |
| 118 | } else { |
| 119 | check_output(kp, out, polarity); |
| 120 | out++; |
| 121 | } |
| 122 | |
| 123 | kp->current_output = out; |
| 124 | if (out < kpinfo->noutputs) { |
| 125 | gpio = kpinfo->output_gpios[out]; |
| 126 | if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE) |
| 127 | gpio_set(gpio, polarity); |
| 128 | else |
| 129 | gpio_config(gpio, polarity ? GPIO_OUTPUT : 0); |
| 130 | timer_set_oneshot(timer, kpinfo->settle_time, |
| 131 | gpio_keypad_timer_func, NULL); |
| 132 | goto done; |
| 133 | } |
| 134 | |
| 135 | if (/*!kp->use_irq*/ 1 || kp->some_keys_pressed) { |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame] | 136 | event_signal(&kp->full_scan, false); |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 137 | timer_set_oneshot(timer, kpinfo->poll_time, |
| 138 | gpio_keypad_timer_func, NULL); |
| 139 | goto done; |
| 140 | } |
| 141 | |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 142 | done: |
| 143 | return INT_RESCHEDULE; |
| 144 | } |
| 145 | |
| 146 | void gpio_keypad_init(struct gpio_keypad_info *kpinfo) |
| 147 | { |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 148 | int key_count; |
| 149 | int output_val; |
| 150 | int output_cfg; |
| 151 | int i; |
| 152 | int len; |
| 153 | |
| 154 | ASSERT(kpinfo->keymap && kpinfo->input_gpios && kpinfo->output_gpios); |
| 155 | key_count = kpinfo->ninputs * kpinfo->noutputs; |
| 156 | |
| 157 | len = sizeof(struct gpio_kp) + (sizeof(unsigned long) * |
| 158 | BITMAP_NUM_WORDS(key_count)); |
| 159 | keypad = malloc(len); |
| 160 | ASSERT(keypad); |
| 161 | |
| 162 | memset(keypad, 0, len); |
| 163 | keypad->keypad_info = kpinfo; |
| 164 | |
| 165 | output_val = (!!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH)) ^ |
| 166 | (!!(kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)); |
| 167 | output_cfg = kpinfo->flags & GPIOKPF_DRIVE_INACTIVE ? GPIO_OUTPUT : 0; |
| 168 | for (i = 0; i < kpinfo->noutputs; i++) { |
| 169 | gpio_set(kpinfo->output_gpios[i], output_val); |
| 170 | gpio_config(kpinfo->output_gpios[i], output_cfg); |
| 171 | } |
| 172 | for (i = 0; i < kpinfo->ninputs; i++) |
| 173 | gpio_config(kpinfo->input_gpios[i], GPIO_INPUT); |
| 174 | |
| 175 | keypad->current_output = kpinfo->noutputs; |
| 176 | |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame] | 177 | event_init(&keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL); |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 178 | timer_initialize(&keypad->timer); |
| 179 | timer_set_oneshot(&keypad->timer, 0, gpio_keypad_timer_func, NULL); |
Dima Zavin | d6455cd | 2009-01-26 16:35:06 -0800 | [diff] [blame] | 180 | |
| 181 | /* wait for the keypad to complete one full scan */ |
| 182 | event_wait(&keypad->full_scan); |
Dima Zavin | 0f88be2 | 2009-01-20 19:25:50 -0800 | [diff] [blame] | 183 | } |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 184 | |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 185 | int pm8058_gpio_config(int gpio, struct pm8058_gpio *param) |
| 186 | { |
| 187 | int rc; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 188 | write_func wr_function = (qwerty_keypad->keypad_info)->wr_func; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 189 | unsigned char bank[8]; |
| 190 | static int dir_map[] = { |
| 191 | PM8058_GPIO_MODE_OFF, |
| 192 | PM8058_GPIO_MODE_OUTPUT, |
| 193 | PM8058_GPIO_MODE_INPUT, |
| 194 | PM8058_GPIO_MODE_BOTH, |
| 195 | }; |
| 196 | |
| 197 | if (param == 0) { |
| 198 | dprintf (INFO, "pm8058_gpio struct not defined\n"); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | /* Select banks and configure the gpio */ |
| 203 | bank[0] = PM8058_GPIO_WRITE | |
| 204 | ((param->vin_sel << PM8058_GPIO_VIN_SHIFT) & |
| 205 | PM8058_GPIO_VIN_MASK) | |
| 206 | PM8058_GPIO_MODE_ENABLE; |
| 207 | bank[1] = PM8058_GPIO_WRITE | |
| 208 | ((1 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) | |
| 209 | ((dir_map[param->direction] << PM8058_GPIO_MODE_SHIFT) & |
| 210 | PM8058_GPIO_MODE_MASK) | |
| 211 | ((param->direction & PM_GPIO_DIR_OUT) ? |
| 212 | PM8058_GPIO_OUT_BUFFER : 0); |
| 213 | bank[2] = PM8058_GPIO_WRITE | |
| 214 | ((2 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) | |
| 215 | ((param->pull << PM8058_GPIO_PULL_SHIFT) & |
| 216 | PM8058_GPIO_PULL_MASK); |
| 217 | bank[3] = PM8058_GPIO_WRITE | |
| 218 | ((3 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) | |
| 219 | ((param->out_strength << PM8058_GPIO_OUT_STRENGTH_SHIFT) & |
| 220 | PM8058_GPIO_OUT_STRENGTH_MASK); |
| 221 | bank[4] = PM8058_GPIO_WRITE | |
| 222 | ((4 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) | |
| 223 | ((param->function << PM8058_GPIO_FUNC_SHIFT) & |
| 224 | PM8058_GPIO_FUNC_MASK); |
| 225 | |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 226 | rc = (*wr_function)(bank, 5, SSBI_REG_ADDR_GPIO(gpio)); |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 227 | if (rc) { |
| 228 | dprintf(INFO, "Failed on 1st ssbi_write(): rc=%d.\n", rc); |
| 229 | return 1; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 234 | int pm8058_gpio_config_kypd_drv(int gpio_start, int num_gpios, unsigned mach_id) |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 235 | { |
| 236 | int rc; |
| 237 | struct pm8058_gpio kypd_drv = { |
| 238 | .direction = PM_GPIO_DIR_OUT, |
| 239 | .pull = PM_GPIO_PULL_NO, |
| 240 | .vin_sel = 2, |
| 241 | .out_strength = PM_GPIO_STRENGTH_LOW, |
| 242 | .function = PM_GPIO_FUNC_1, |
| 243 | .inv_int_pol = 1, |
| 244 | }; |
| 245 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 246 | if(mach_id == LINUX_MACHTYPE_8660_QT) |
| 247 | kypd_drv.function = PM_GPIO_FUNC_NORMAL; |
| 248 | |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 249 | while (num_gpios--) { |
| 250 | rc = pm8058_gpio_config(gpio_start++, &kypd_drv); |
| 251 | if (rc) { |
| 252 | dprintf(INFO, "FAIL pm8058_gpio_config(): rc=%d.\n", rc); |
| 253 | return rc; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int pm8058_gpio_config_kypd_sns(int gpio_start, int num_gpios) |
| 261 | { |
| 262 | int rc; |
| 263 | struct pm8058_gpio kypd_sns = { |
| 264 | .direction = PM_GPIO_DIR_IN, |
| 265 | .pull = PM_GPIO_PULL_UP1, |
| 266 | .vin_sel = 2, |
| 267 | .out_strength = PM_GPIO_STRENGTH_NO, |
| 268 | .function = PM_GPIO_FUNC_NORMAL, |
| 269 | .inv_int_pol = 1, |
| 270 | }; |
| 271 | |
| 272 | while (num_gpios--) { |
| 273 | rc = pm8058_gpio_config(gpio_start++, &kypd_sns); |
| 274 | if (rc) { |
| 275 | dprintf(INFO, "FAIL pm8058_gpio_config(): rc=%d.\n", rc); |
| 276 | return rc; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 283 | static void ssbi_gpio_init(unsigned int mach_id) |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 284 | { |
Dima Zavin | aacfed2 | 2011-01-14 15:40:29 -0800 | [diff] [blame] | 285 | unsigned char kypd_cntl_init; |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 286 | unsigned char kypd_scan_init = 0x20; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 287 | int rows = (qwerty_keypad->keypad_info)->rows; |
| 288 | int columns = (qwerty_keypad->keypad_info)->columns; |
| 289 | write_func wr_function = (qwerty_keypad->keypad_info)->wr_func; |
Dima Zavin | aacfed2 | 2011-01-14 15:40:29 -0800 | [diff] [blame] | 290 | unsigned char drv_bits[] = { |
| 291 | 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7 }; |
| 292 | unsigned char sns_bits[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3 }; |
| 293 | |
Ajay Dudani | 35e7aae | 2011-01-20 22:24:16 -0800 | [diff] [blame] | 294 | kypd_cntl_init = ((drv_bits[rows] << 2) | (sns_bits[columns] << 5) | (1 << 7)); |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 295 | |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 296 | if ((*wr_function)(&kypd_cntl_init, 1, SSBI_REG_KYPD_CNTL_ADDR)) |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 297 | dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n"); |
| 298 | |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 299 | if ((*wr_function)(&kypd_scan_init, 1, SSBI_REG_KYPD_SCAN_ADDR)) |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 300 | dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_SCAN register\n"); |
| 301 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 302 | if(mach_id == LINUX_MACHTYPE_8660_QT) |
| 303 | { |
| 304 | pm8058_gpio_config_kypd_sns(QT_PMIC_GPIO_KYPD_SNS, rows); |
| 305 | pm8058_gpio_config_kypd_drv(QT_PMIC_GPIO_KYPD_DRV, columns, mach_id); |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | pm8058_gpio_config_kypd_sns(SSBI_OFFSET_ADDR_GPIO_KYPD_SNS, columns); |
| 310 | pm8058_gpio_config_kypd_drv(SSBI_OFFSET_ADDR_GPIO_KYPD_DRV, rows, mach_id); |
| 311 | } |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 314 | static enum handler_return |
| 315 | scan_qwerty_keypad(struct timer *timer, time_t now, void *arg) |
| 316 | { |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 317 | unsigned int rows = (qwerty_keypad->keypad_info)->rows; |
| 318 | unsigned int columns = (qwerty_keypad->keypad_info)->columns; |
| 319 | unsigned int num_of_ssbi_reads = (qwerty_keypad->keypad_info)->num_of_reads; |
| 320 | read_func rd_function = (qwerty_keypad->keypad_info)->rd_func; |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 321 | unsigned char column_new_keys = 0x00; |
| 322 | unsigned char column_old_keys = 0x00; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 323 | int shift = 0; |
Ajay Dudani | 2547846 | 2011-04-11 19:36:57 -0700 | [diff] [blame] | 324 | static int key_detected = -1; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 325 | |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 326 | if ((*rd_function)((qwerty_keypad->keypad_info)->rec_keys, num_of_ssbi_reads, |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 327 | SSBI_REG_KYPD_REC_DATA_ADDR)) |
| 328 | dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n"); |
| 329 | |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 330 | if ((*rd_function)((qwerty_keypad->keypad_info)->old_keys, num_of_ssbi_reads, |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 331 | SSBI_REG_KYPD_OLD_DATA_ADDR)) |
| 332 | dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n"); |
| 333 | |
| 334 | while (rows--) { |
Dima Zavin | 8090690 | 2011-01-14 15:40:55 -0800 | [diff] [blame] | 335 | columns = qwerty_keypad->keypad_info->columns; |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 336 | if (((qwerty_keypad->keypad_info)->rec_keys[rows] |
| 337 | != (qwerty_keypad->keypad_info)->old_keys[rows]) |
| 338 | && ((qwerty_keypad->keypad_info)->rec_keys[rows] != 0x00) |
| 339 | && ((qwerty_keypad->keypad_info)->old_keys[rows] != 0x00)) { |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 340 | while (columns--) { |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 341 | column_new_keys = ((qwerty_keypad->keypad_info)->rec_keys[rows]); |
| 342 | column_old_keys = ((qwerty_keypad->keypad_info)->old_keys[rows]); |
| 343 | if (((0x01 << columns) & (~column_new_keys)) |
| 344 | && !((0x01 << columns) & (~column_old_keys))) { |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 345 | shift = (rows * 8) + columns; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 346 | if ((qwerty_keypad->keypad_info)->keymap[shift]) { |
Chandan Uddaraju | 2aba273 | 2010-02-24 13:31:43 -0800 | [diff] [blame] | 347 | if (shift != key_detected) { |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 348 | key_detected = shift; |
| 349 | keys_post_event((qwerty_keypad->keypad_info)->keymap[shift], 1); |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 350 | } |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 356 | |
| 357 | event_signal(&qwerty_keypad->full_scan, false); |
| 358 | return INT_RESCHEDULE; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 361 | static enum handler_return |
| 362 | scan_qt_keypad(struct timer *timer, time_t now, void *arg) |
| 363 | { |
| 364 | unsigned int gpio; |
| 365 | unsigned int last_state=0; |
| 366 | unsigned int new_state=0; |
| 367 | unsigned int bits_changed; |
Ajay Dudani | 2547846 | 2011-04-11 19:36:57 -0700 | [diff] [blame] | 368 | static int key_detected=-1; |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 369 | |
| 370 | /* Row GPIOs 8,9,10 are used for sensing here */ |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 371 | for (gpio=8;gpio<=10;gpio++) |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 372 | { |
| 373 | bool status; |
| 374 | status = pm8058_gpio_get(gpio); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 375 | if (status == 0) |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 376 | new_state |= (1<<(gpio-8)); |
| 377 | } |
| 378 | |
| 379 | bits_changed = last_state ^ new_state; |
| 380 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 381 | if (bits_changed) |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 382 | { |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 383 | int shift = 0; |
| 384 | for (unsigned int rows = 0; rows < (qwerty_keypad->keypad_info)->rows; rows++) |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 385 | { |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 386 | if ((bits_changed & (1<<rows)) == 0) |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 387 | continue; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 388 | shift = rows * 8 + 3; |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 389 | } |
| 390 | if ((qwerty_keypad->keypad_info)->keymap[shift]) |
| 391 | { |
| 392 | if (shift != key_detected) |
| 393 | { |
| 394 | key_detected = shift; |
| 395 | keys_post_event((qwerty_keypad->keypad_info)->keymap[shift], 1); |
| 396 | timer_set_oneshot(timer,(qwerty_keypad->keypad_info)->poll_time, |
| 397 | scan_qt_keypad, NULL); |
| 398 | return INT_RESCHEDULE; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | event_signal(&qwerty_keypad->full_scan, false); |
| 403 | return INT_RESCHEDULE; |
| 404 | } |
| 405 | |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 406 | void ssbi_keypad_init(struct qwerty_keypad_info *qwerty_kp) |
| 407 | { |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 408 | unsigned int mach_id; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 409 | int len; |
| 410 | |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 411 | len = sizeof(struct gpio_qwerty_kp); |
| 412 | qwerty_keypad = malloc(len); |
| 413 | ASSERT(qwerty_keypad); |
| 414 | |
| 415 | memset(qwerty_keypad, 0, len); |
| 416 | qwerty_keypad->keypad_info = qwerty_kp; |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 417 | |
| 418 | event_init(&qwerty_keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL); |
| 419 | timer_initialize(&qwerty_keypad->timer); |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 420 | |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 421 | mach_id = board_machtype(); |
Subbaraman Narayanamurthy | 8f0b045 | 2011-03-11 18:30:10 -0800 | [diff] [blame] | 422 | ssbi_gpio_init(mach_id); |
| 423 | |
| 424 | if(mach_id == LINUX_MACHTYPE_8660_QT) |
| 425 | { |
| 426 | mdelay((qwerty_keypad->keypad_info)->settle_time); |
| 427 | #ifdef QT_8660_KEYPAD_HW_BUG |
| 428 | timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qt_keypad, NULL); |
| 429 | #endif |
| 430 | } |
| 431 | else |
| 432 | timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qwerty_keypad, NULL); |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 433 | |
| 434 | /* wait for the keypad to complete one full scan */ |
| 435 | event_wait(&qwerty_keypad->full_scan); |
| 436 | } |
| 437 | |
Shashank Mittal | b3be37f | 2012-01-16 22:59:49 -0800 | [diff] [blame] | 438 | static enum handler_return |
| 439 | scan_qwerty_gpio_keypad(struct timer *timer, time_t now, void *arg) |
| 440 | { |
| 441 | int i=0; |
| 442 | int num =0; |
| 443 | uint8_t key_status; |
| 444 | struct qwerty_keypad_info *keypad = qwerty_keypad->keypad_info; |
| 445 | |
| 446 | num = keypad->mapsize; |
| 447 | |
| 448 | for(i=0; i < num; i++) |
| 449 | { |
| 450 | /*continue if not interested in key*/ |
| 451 | if(!keypad->keymap[i]) |
| 452 | continue; |
| 453 | |
| 454 | /*read key gpio*/ |
| 455 | keypad->key_gpio_get(keypad->gpiomap[i], &key_status); |
| 456 | |
| 457 | /*Post event if key pressed*/ |
| 458 | if(key_status) |
| 459 | { |
| 460 | keys_post_event(keypad->keymap[i], 1); |
| 461 | goto done; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | done: |
| 466 | event_signal(&qwerty_keypad->full_scan, false); |
| 467 | return INT_RESCHEDULE; |
| 468 | } |
| 469 | |
| 470 | void ssbi_gpio_keypad_init(struct qwerty_keypad_info *qwerty_kp) |
| 471 | { |
| 472 | int len; |
| 473 | |
| 474 | len = sizeof(struct gpio_qwerty_kp); |
| 475 | qwerty_keypad = malloc(len); |
| 476 | ASSERT(qwerty_keypad); |
| 477 | |
| 478 | memset(qwerty_keypad, 0, len); |
| 479 | qwerty_keypad->keypad_info = qwerty_kp; |
| 480 | |
| 481 | event_init(&qwerty_keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL); |
| 482 | timer_initialize(&qwerty_keypad->timer); |
| 483 | |
| 484 | timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qwerty_gpio_keypad, NULL); |
| 485 | |
| 486 | /* wait for the keypad to complete one full scan */ |
| 487 | event_wait(&qwerty_keypad->full_scan); |
| 488 | } |
| 489 | |
Shashank Mittal | 3704083 | 2010-08-24 15:57:57 -0700 | [diff] [blame] | 490 | void pmic_write(unsigned address, unsigned data) |
| 491 | { |
| 492 | write_func wr_function = &i2c_ssbi_write_bytes; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 493 | if (wr_function == NULL) |
Shashank Mittal | 3704083 | 2010-08-24 15:57:57 -0700 | [diff] [blame] | 494 | return; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 495 | if ((*wr_function)((unsigned char *) &data, 1, address)) |
Shashank Mittal | 3704083 | 2010-08-24 15:57:57 -0700 | [diff] [blame] | 496 | dprintf (CRITICAL, "Error in initializing register\n"); |
| 497 | |
| 498 | } |
Chandan Uddaraju | c009e4d | 2010-09-08 17:06:45 -0700 | [diff] [blame] | 499 | void toshiba_pmic_gpio_init(unsigned gpio) |
| 500 | { |
| 501 | pmic_write(gpio,0x85); |
| 502 | pmic_write(gpio,0x98); |
| 503 | pmic_write(gpio,0xB8); |
| 504 | pmic_write(gpio,0xC6); |
| 505 | } |