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