blob: bb95f77c609c3c5a55a3a36e3f25c42e02cef0ab [file] [log] [blame]
Dima Zavin0f88be22009-01-20 19:25:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08005 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
6 *
Dima Zavin0f88be22009-01-20 19:25:50 -08007 * 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 Zavin42f13d32009-01-29 11:47:57 -080036#include <stdlib.h>
37#include <string.h>
Dima Zavin0f88be22009-01-20 19:25:50 -080038#include <dev/keys.h>
39#include <dev/gpio.h>
40#include <dev/gpio_keypad.h>
Dima Zavind6455cd2009-01-26 16:35:06 -080041#include <kernel/event.h>
Dima Zavin0f88be22009-01-20 19:25:50 -080042#include <kernel/timer.h>
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080043#include <reg.h>
Dima Zavin0f88be22009-01-20 19:25:50 -080044
45struct gpio_kp {
46 struct gpio_keypad_info *keypad_info;
47 struct timer timer;
Dima Zavind6455cd2009-01-26 16:35:06 -080048 event_t full_scan;
Dima Zavin0f88be22009-01-20 19:25:50 -080049 int current_output;
50 unsigned int some_keys_pressed:2;
51 unsigned long keys_pressed[0];
52};
53
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080054struct gpio_qwerty_kp {
55 struct qwerty_keypad_info *keypad_info;
56 struct timer timer;
57 event_t full_scan;
58 int num_of_scans;
59 unsigned int some_keys_pressed:2;
60 unsigned long keys_pressed[0];
61};
62
63static struct gpio_qwerty_kp *qwerty_keypad;
Dima Zavin0f88be22009-01-20 19:25:50 -080064/* TODO: Support multiple keypads? */
65static struct gpio_kp *keypad;
66
67static void check_output(struct gpio_kp *kp, int out, int polarity)
68{
69 struct gpio_keypad_info *kpinfo = kp->keypad_info;
70 int key_index;
71 int in;
72 int gpio;
73 int changed = 0;
74
75 key_index = out * kpinfo->ninputs;
76 for (in = 0; in < kpinfo->ninputs; in++, key_index++) {
77 gpio = kpinfo->input_gpios[in];
78 changed = 0;
79 if (gpio_get(gpio) ^ !polarity) {
80 if (kp->some_keys_pressed < 3)
81 kp->some_keys_pressed++;
82 changed = !bitmap_set(kp->keys_pressed, key_index);
83 } else {
84 changed = bitmap_clear(kp->keys_pressed, key_index);
85 }
86 if (changed) {
87 int state = bitmap_test(kp->keys_pressed, key_index);
88 keys_post_event(kpinfo->keymap[key_index], state);
89 }
90 }
91
92 /* sets up the right state for the next poll cycle */
93 gpio = kpinfo->output_gpios[out];
94 if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)
95 gpio_set(gpio, !polarity);
96 else
97 gpio_config(gpio, GPIO_INPUT);
98}
99
100static enum handler_return
101gpio_keypad_timer_func(struct timer *timer, time_t now, void *arg)
102{
103 struct gpio_kp *kp = keypad;
104 struct gpio_keypad_info *kpinfo = kp->keypad_info;
105 int polarity = !!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH);
Dima Zavin42f13d32009-01-29 11:47:57 -0800106 int out;
Dima Zavin0f88be22009-01-20 19:25:50 -0800107 int gpio;
108
109 out = kp->current_output;
110 if (out == kpinfo->noutputs) {
111 out = 0;
112 kp->some_keys_pressed = 0;
113 } else {
114 check_output(kp, out, polarity);
115 out++;
116 }
117
118 kp->current_output = out;
119 if (out < kpinfo->noutputs) {
120 gpio = kpinfo->output_gpios[out];
121 if (kpinfo->flags & GPIOKPF_DRIVE_INACTIVE)
122 gpio_set(gpio, polarity);
123 else
124 gpio_config(gpio, polarity ? GPIO_OUTPUT : 0);
125 timer_set_oneshot(timer, kpinfo->settle_time,
126 gpio_keypad_timer_func, NULL);
127 goto done;
128 }
129
130 if (/*!kp->use_irq*/ 1 || kp->some_keys_pressed) {
Dima Zavind6455cd2009-01-26 16:35:06 -0800131 event_signal(&kp->full_scan, false);
Dima Zavin0f88be22009-01-20 19:25:50 -0800132 timer_set_oneshot(timer, kpinfo->poll_time,
133 gpio_keypad_timer_func, NULL);
134 goto done;
135 }
136
137#if 0
138 /* No keys are pressed, reenable interrupt */
139 for (out = 0; out < kpinfo->noutputs; out++) {
140 if (gpio_keypad_flags & GPIOKPF_DRIVE_INACTIVE)
141 gpio_set(kpinfo->output_gpios[out], polarity);
142 else
143 gpio_config(kpinfo->output_gpios[out], polarity ? GPIO_OUTPUT : 0);
144 }
145 for (in = 0; in < kpinfo->ninputs; in++)
146 enable_irq(gpio_to_irq(kpinfo->input_gpios[in]));
147 return INT_RESCHEDULE;
148#endif
149
150done:
151 return INT_RESCHEDULE;
152}
153
154void gpio_keypad_init(struct gpio_keypad_info *kpinfo)
155{
Dima Zavin0f88be22009-01-20 19:25:50 -0800156 int key_count;
157 int output_val;
158 int output_cfg;
159 int i;
160 int len;
161
162 ASSERT(kpinfo->keymap && kpinfo->input_gpios && kpinfo->output_gpios);
163 key_count = kpinfo->ninputs * kpinfo->noutputs;
164
165 len = sizeof(struct gpio_kp) + (sizeof(unsigned long) *
166 BITMAP_NUM_WORDS(key_count));
167 keypad = malloc(len);
168 ASSERT(keypad);
169
170 memset(keypad, 0, len);
171 keypad->keypad_info = kpinfo;
172
173 output_val = (!!(kpinfo->flags & GPIOKPF_ACTIVE_HIGH)) ^
174 (!!(kpinfo->flags & GPIOKPF_DRIVE_INACTIVE));
175 output_cfg = kpinfo->flags & GPIOKPF_DRIVE_INACTIVE ? GPIO_OUTPUT : 0;
176 for (i = 0; i < kpinfo->noutputs; i++) {
177 gpio_set(kpinfo->output_gpios[i], output_val);
178 gpio_config(kpinfo->output_gpios[i], output_cfg);
179 }
180 for (i = 0; i < kpinfo->ninputs; i++)
181 gpio_config(kpinfo->input_gpios[i], GPIO_INPUT);
182
183 keypad->current_output = kpinfo->noutputs;
184
Dima Zavind6455cd2009-01-26 16:35:06 -0800185 event_init(&keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL);
Dima Zavin0f88be22009-01-20 19:25:50 -0800186 timer_initialize(&keypad->timer);
187 timer_set_oneshot(&keypad->timer, 0, gpio_keypad_timer_func, NULL);
Dima Zavind6455cd2009-01-26 16:35:06 -0800188
189 /* wait for the keypad to complete one full scan */
190 event_wait(&keypad->full_scan);
Dima Zavin0f88be22009-01-20 19:25:50 -0800191}
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -0800192
193int i2c_ssbi_poll_for_device_ready(void)
194{
195 unsigned long timeout = SSBI_TIMEOUT_US;
196
197 while (!(readl(MSM_SSBI_BASE + SSBI2_STATUS) & SSBI_STATUS_READY)) {
198 if (--timeout == 0) {
199 dprintf(INFO, "In Device ready function:Timeout, status %x\n", readl(MSM_SSBI_BASE + SSBI2_STATUS));
200 return 1;
201 }
202 }
203
204 return 0;
205}
206
207int i2c_ssbi_poll_for_read_completed(void)
208{
209 unsigned long timeout = SSBI_TIMEOUT_US;
210
211 while (!(readl(MSM_SSBI_BASE + SSBI2_STATUS) & SSBI_STATUS_RD_READY)) {
212 if (--timeout == 0) {
213 dprintf(INFO, "In read completed function:Timeout, status %x\n", readl(MSM_SSBI_BASE + SSBI2_STATUS));
214 return 1;
215 }
216 }
217
218 return 0;
219}
220
221int i2c_ssbi_read_bytes(unsigned char *buffer, unsigned short length,
222 unsigned short slave_addr)
223{
224 int ret = 0;
225 unsigned char *buf = buffer;
226 unsigned short len = length;
227 unsigned short addr = slave_addr;
228 unsigned long read_cmd = SSBI_CMD_READ(addr);
229 unsigned long mode2 = readl(MSM_SSBI_BASE + SSBI2_MODE2);
230
231 //buf = alloc(len * sizeof(8));
232 if (mode2 & SSBI_MODE2_SSBI2_MODE)
233 writel(SSBI_MODE2_REG_ADDR_15_8(mode2, addr),
234 MSM_SSBI_BASE + SSBI2_MODE2);
235
236 while (len) {
237 ret = i2c_ssbi_poll_for_device_ready();
238 if (ret) {
239 dprintf (CRITICAL, "Error: device not ready\n");
240 return ret;
241 }
242
243 writel(read_cmd, MSM_SSBI_BASE + SSBI2_CMD);
244
245 ret = i2c_ssbi_poll_for_read_completed();
246 if (ret) {
247 dprintf (CRITICAL, "Error: read not completed\n");
248 return ret;
249 }
250
251 *buf++ = readl(MSM_SSBI_BASE + SSBI2_RD) & SSBI_RD_REG_DATA_MASK;
252 len--;
253 }
254 return 0;
255}
256
257int i2c_ssbi_write_bytes(unsigned char *buffer, unsigned short length,
258 unsigned short slave_addr)
259{
260 int ret = 0;
261 unsigned long timeout = SSBI_TIMEOUT_US;
262 unsigned char *buf = buffer;
263 unsigned short len = length;
264 unsigned short addr = slave_addr;
265 unsigned long mode2 = readl(MSM_SSBI_BASE + SSBI2_MODE2);
266
267 if (mode2 & SSBI_MODE2_SSBI2_MODE)
268 writel(SSBI_MODE2_REG_ADDR_15_8(mode2, addr),
269 MSM_SSBI_BASE + SSBI2_MODE2);
270
271 while (len) {
272 ret = i2c_ssbi_poll_for_device_ready();
273 if (ret) {
274 dprintf (CRITICAL, "Error: device not ready\n");
275 return ret;
276 }
277
278 writel(SSBI_CMD_WRITE(addr, *buf++), MSM_SSBI_BASE + SSBI2_CMD);
279
280 while (readl(MSM_SSBI_BASE + SSBI2_STATUS) & SSBI_STATUS_MCHN_BUSY) {
281 if (--timeout == 0) {
282 dprintf(INFO, "In Device ready function:Timeout, status %x\n", readl(MSM_SSBI_BASE + SSBI2_STATUS));
283 return 1;
284 }
285 }
286 len--;
287 }
288 return 0;
289}
290
291int pm8058_gpio_config(int gpio, struct pm8058_gpio *param)
292{
293 int rc;
294 unsigned char bank[8];
295 static int dir_map[] = {
296 PM8058_GPIO_MODE_OFF,
297 PM8058_GPIO_MODE_OUTPUT,
298 PM8058_GPIO_MODE_INPUT,
299 PM8058_GPIO_MODE_BOTH,
300 };
301
302 if (param == 0) {
303 dprintf (INFO, "pm8058_gpio struct not defined\n");
304 return -1;
305 }
306
307 /* Select banks and configure the gpio */
308 bank[0] = PM8058_GPIO_WRITE |
309 ((param->vin_sel << PM8058_GPIO_VIN_SHIFT) &
310 PM8058_GPIO_VIN_MASK) |
311 PM8058_GPIO_MODE_ENABLE;
312 bank[1] = PM8058_GPIO_WRITE |
313 ((1 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
314 ((dir_map[param->direction] << PM8058_GPIO_MODE_SHIFT) &
315 PM8058_GPIO_MODE_MASK) |
316 ((param->direction & PM_GPIO_DIR_OUT) ?
317 PM8058_GPIO_OUT_BUFFER : 0);
318 bank[2] = PM8058_GPIO_WRITE |
319 ((2 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
320 ((param->pull << PM8058_GPIO_PULL_SHIFT) &
321 PM8058_GPIO_PULL_MASK);
322 bank[3] = PM8058_GPIO_WRITE |
323 ((3 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
324 ((param->out_strength << PM8058_GPIO_OUT_STRENGTH_SHIFT) &
325 PM8058_GPIO_OUT_STRENGTH_MASK);
326 bank[4] = PM8058_GPIO_WRITE |
327 ((4 << PM8058_GPIO_BANK_SHIFT) & PM8058_GPIO_BANK_MASK) |
328 ((param->function << PM8058_GPIO_FUNC_SHIFT) &
329 PM8058_GPIO_FUNC_MASK);
330
331 rc = i2c_ssbi_write_bytes(bank, 5, SSBI_REG_ADDR_GPIO(gpio));
332 if (rc) {
333 dprintf(INFO, "Failed on 1st ssbi_write(): rc=%d.\n", rc);
334 return 1;
335 }
336 return 0;
337}
338
339int pm8058_gpio_config_kypd_drv(int gpio_start, int num_gpios)
340{
341 int rc;
342 struct pm8058_gpio kypd_drv = {
343 .direction = PM_GPIO_DIR_OUT,
344 .pull = PM_GPIO_PULL_NO,
345 .vin_sel = 2,
346 .out_strength = PM_GPIO_STRENGTH_LOW,
347 .function = PM_GPIO_FUNC_1,
348 .inv_int_pol = 1,
349 };
350
351 while (num_gpios--) {
352 rc = pm8058_gpio_config(gpio_start++, &kypd_drv);
353 if (rc) {
354 dprintf(INFO, "FAIL pm8058_gpio_config(): rc=%d.\n", rc);
355 return rc;
356 }
357 }
358
359 return 0;
360}
361
362int pm8058_gpio_config_kypd_sns(int gpio_start, int num_gpios)
363{
364 int rc;
365 struct pm8058_gpio kypd_sns = {
366 .direction = PM_GPIO_DIR_IN,
367 .pull = PM_GPIO_PULL_UP1,
368 .vin_sel = 2,
369 .out_strength = PM_GPIO_STRENGTH_NO,
370 .function = PM_GPIO_FUNC_NORMAL,
371 .inv_int_pol = 1,
372 };
373
374 while (num_gpios--) {
375 rc = pm8058_gpio_config(gpio_start++, &kypd_sns);
376 if (rc) {
377 dprintf(INFO, "FAIL pm8058_gpio_config(): rc=%d.\n", rc);
378 return rc;
379 }
380 }
381
382 return 0;
383}
384
385static enum handler_return
386scan_qwerty_keypad(struct timer *timer, time_t now, void *arg)
387{
388 int rows = (qwerty_keypad->keypad_info)->rows;
389 int columns = (qwerty_keypad->keypad_info)->columns;
390 unsigned char column_keys = 0x00;
391 int shift = 0;
392 static int key_detected = 0;
393
394 if (i2c_ssbi_read_bytes((qwerty_keypad->keypad_info)->rec_keys, NUM_OF_SSBI_READS,
395 SSBI_REG_KYPD_REC_DATA_ADDR))
396 dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
397
398 if (i2c_ssbi_read_bytes((qwerty_keypad->keypad_info)->old_keys, NUM_OF_SSBI_READS,
399 SSBI_REG_KYPD_OLD_DATA_ADDR))
400 dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
401
402 while (rows--) {
403 if ((qwerty_keypad->keypad_info)->rec_keys[rows]
404 != (qwerty_keypad->keypad_info)->old_keys[rows]) {
405 while (columns--) {
406 column_keys = ((qwerty_keypad->keypad_info)->rec_keys[rows]);
407 if ((0x01 << columns) & (~column_keys)) {
408 shift = (rows * (qwerty_keypad->keypad_info)->columns) + columns;
409 if ((qwerty_keypad->keypad_info)->keymap[shift]) {
410 if (shift != key_detected) {
411 // key_changed((qwerty_keypad->keypad_info)->keymap[shift], 1);
412 key_detected = shift;
413 keys_post_event((qwerty_keypad->keypad_info)->keymap[shift], 1);
414 event_signal(&qwerty_keypad->full_scan, false);
415 timer_set_oneshot(timer, (qwerty_keypad->keypad_info)->poll_time,
416 scan_qwerty_keypad, NULL);
417 return INT_RESCHEDULE;
418
419 }
420 break;
421 }
422 }
423 }
424 }
425 }
426 if (qwerty_keypad->num_of_scans < 10)
427 {
428 (qwerty_keypad->num_of_scans)++;
429 timer_set_oneshot(timer, (qwerty_keypad->keypad_info)->settle_time,
430 scan_qwerty_keypad, NULL);
431 return INT_RESCHEDULE;
432 }
433
434 event_signal(&qwerty_keypad->full_scan, false);
435 return INT_RESCHEDULE;
436
437}
438
439void ssbi_gpio_init(void)
440{
441 unsigned char kypd_cntl_init = 0xF8;
442 unsigned char kypd_scan_init = 0x20;
443
444 if (i2c_ssbi_write_bytes(&kypd_cntl_init, 1, SSBI_REG_KYPD_CNTL_ADDR))
445 dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_CNTL register\n");
446
447 if (i2c_ssbi_write_bytes(&kypd_scan_init, 1, SSBI_REG_KYPD_SCAN_ADDR))
448 dprintf (CRITICAL, "Error in initializing SSBI_REG_KYPD_SCAN register\n");
449
450 pm8058_gpio_config_kypd_sns(SSBI_OFFSET_ADDR_GPIO_KYPD_SNS, NUM_OF_KYPD_SNS_GPIOS);
451 pm8058_gpio_config_kypd_drv(SSBI_OFFSET_ADDR_GPIO_KYPD_DRV, NUM_OF_KYPD_DRV_GPIOS);
452
453
454}
455
456void ssbi_keypad_init(struct qwerty_keypad_info *qwerty_kp)
457{
458 int *modem_stat_check = (SMEM_BASE + 0x14);
459 int len;
460
461 /* Wait for modem to be ready before keypad init */
462 while (readl(modem_stat_check) != 1);
463
464 ssbi_gpio_init();
465
466 len = sizeof(struct gpio_qwerty_kp);
467 qwerty_keypad = malloc(len);
468 ASSERT(qwerty_keypad);
469
470 memset(qwerty_keypad, 0, len);
471 qwerty_keypad->keypad_info = qwerty_kp;
472 qwerty_keypad->num_of_scans = 0;
473
474 event_init(&qwerty_keypad->full_scan, false, EVENT_FLAG_AUTOUNSIGNAL);
475 timer_initialize(&qwerty_keypad->timer);
476 timer_set_oneshot(&qwerty_keypad->timer, 0, scan_qwerty_keypad, NULL);
477
478 /* wait for the keypad to complete one full scan */
479 event_wait(&qwerty_keypad->full_scan);
480}
481