Correct manual keymaps for plugins recently converted to PLA, and use manual PLA...
[maemo-rb.git] / firmware / drivers / synaptics-mep.c
blobd200e7ca79a0a08e51bdce81768b087a60c970da
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Mark Arigo
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdlib.h>
23 #include "config.h"
24 #include "cpu.h"
25 #include "system.h"
26 #include "kernel.h"
27 #include "button-target.h"
28 #include "synaptics-mep.h"
30 /*#define LOGF_ENABLE*/
31 #include "logf.h"
33 /* Driver for the Synaptics Touchpad based on the "Synaptics Modular Embedded
34 Protocol: 3-Wire Interface Specification" documentation */
36 #if defined(MROBE_100)
37 #define INT_ENABLE GPIO_CLEAR_BITWISE(GPIOD_INT_LEV, 0x2);\
38 GPIO_SET_BITWISE(GPIOD_INT_EN, 0x2)
39 #define INT_DISABLE GPIO_CLEAR_BITWISE(GPIOD_INT_EN, 0x2);\
40 GPIO_SET_BITWISE(GPIOD_INT_CLR, 0x2)
42 #define ACK (GPIOD_INPUT_VAL & 0x1)
43 #define ACK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x1)
44 #define ACK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x1)
46 #define CLK ((GPIOD_INPUT_VAL & 0x2) >> 1)
47 #define CLK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
48 #define CLK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
50 #define DATA ((GPIOD_INPUT_VAL & 0x4) >> 2)
51 #define DATA_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x4);\
52 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x4)
53 #define DATA_LO GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x4);\
54 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x4)
55 #define DATA_CL GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x4)
57 #elif defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || \
58 defined(PBELL_VIBE500)
59 #define INT_ENABLE GPIO_CLEAR_BITWISE(GPIOA_INT_LEV, 0x20);\
60 GPIO_SET_BITWISE(GPIOA_INT_EN, 0x20)
61 #define INT_DISABLE GPIO_CLEAR_BITWISE(GPIOA_INT_EN, 0x20);\
62 GPIO_SET_BITWISE(GPIOA_INT_CLR, 0x20)
64 #define ACK (GPIOD_INPUT_VAL & 0x80)
65 #define ACK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x80)
66 #define ACK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x80)
68 #define CLK ((GPIOA_INPUT_VAL & 0x20) >> 5)
69 #define CLK_HI GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x20)
70 #define CLK_LO GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_VAL, 0x20)
72 #define DATA ((GPIOA_INPUT_VAL & 0x10) >> 4)
73 #define DATA_HI GPIO_SET_BITWISE(GPIOA_OUTPUT_EN, 0x10);\
74 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x10)
75 #define DATA_LO GPIO_SET_BITWISE(GPIOA_OUTPUT_EN, 0x10);\
76 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_VAL, 0x10)
77 #define DATA_CL GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x10)
79 #elif defined(PHILIPS_SA9200)
80 #define INT_ENABLE GPIO_CLEAR_BITWISE(GPIOD_INT_LEV, 0x2);\
81 GPIO_SET_BITWISE(GPIOD_INT_EN, 0x2)
82 #define INT_DISABLE GPIO_CLEAR_BITWISE(GPIOD_INT_EN, 0x2);\
83 GPIO_SET_BITWISE(GPIOD_INT_CLR, 0x2)
85 #define ACK (GPIOD_INPUT_VAL & 0x8)
86 #define ACK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x8)
87 #define ACK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x8)
89 #define CLK ((GPIOD_INPUT_VAL & 0x2) >> 1)
90 #define CLK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
91 #define CLK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
93 #define DATA ((GPIOD_INPUT_VAL & 0x10) >> 4)
94 #define DATA_HI GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x10)
95 #define DATA_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x10);\
96 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x10)
97 #define DATA_CL GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x10)
98 #endif
100 #define LO 0
101 #define HI 1
103 #define READ_RETRY 8
104 #define READ_ERROR -1
106 #define MEP_HELLO_HEADER 0x19
107 #define MEP_HELLO_ID 0x1
109 #define MEP_READ 0x1
110 #define MEP_WRITE 0x3
112 static unsigned short syn_status = 0;
114 static void syn_enable_int(bool enable)
116 if (enable)
118 INT_ENABLE;
120 else
122 INT_DISABLE;
126 static int syn_wait_clk_change(unsigned int val)
128 int i;
130 for (i = 0; i < 10000; i++)
132 if (CLK == val)
133 return 1;
136 return 0;
139 static void syn_set_ack(int val)
141 if (val == HI)
143 ACK_HI;
145 else
147 ACK_LO;
151 static void syn_set_data(int val)
153 if (val == HI)
155 DATA_HI;
157 else
159 DATA_LO;
163 static inline int syn_get_data(void)
165 DATA_CL;
166 return DATA;
169 static void syn_wait_guest_flush(void)
171 /* Flush receiving (flushee) state:
172 handshake until DATA goes high during P3 stage */
173 if (CLK == LO)
175 syn_set_ack(HI); /* P1 -> P2 */
176 syn_wait_clk_change(HI); /* P2 -> P3 */
179 while (syn_get_data() == LO)
181 syn_set_ack(HI); /* P3 -> P0 */
182 syn_wait_clk_change(LO); /* P0 -> P1 */
183 syn_set_ack(LO); /* P1 -> P2 */
184 syn_wait_clk_change(HI); /* P2 -> P3 */
187 /* Continue handshaking until back to P0 */
188 syn_set_ack(HI); /* P3 -> P0 */
191 static void syn_flush(void)
193 int i;
195 logf("syn_flush...");
197 /* Flusher holds DATA low for at least 36 handshake cycles */
198 syn_set_data(LO);
200 for (i = 0; i < 36; i++)
202 syn_wait_clk_change(LO); /* P0 -> P1 */
203 syn_set_ack(LO); /* P1 -> P2 */
204 syn_wait_clk_change(HI); /* P2 -> P3 */
205 syn_set_ack(HI); /* P3 -> P0 */
208 /* Raise DATA in P1 stage */
209 syn_wait_clk_change(LO); /* P0 -> P1 */
210 syn_set_data(HI);
212 /* After a flush, the flushing device enters a flush-receiving (flushee)
213 state */
214 syn_wait_guest_flush();
217 static int syn_send(char *data, int len)
219 int i, bit;
220 int parity = 0;
222 logf("syn_send...");
224 /* 1. Lower DATA line to issue a request-to-send to guest */
225 syn_set_data(LO);
227 /* 2. Wait for guest to lower CLK */
228 syn_wait_clk_change(LO);
230 /* 3. Lower ACK (with DATA still low) */
231 syn_set_ack(LO);
233 /* 4. Wait for guest to raise CLK */
234 syn_wait_clk_change(HI);
236 /* 5. Send data */
237 for (i = 0; i < len; i++)
239 logf(" sending byte: %d", data[i]);
241 bit = 0;
242 while (bit < 8)
244 /* 5a. Drive data low if bit is 0, or high if bit is 1 */
245 if (data[i] & (1 << bit))
247 syn_set_data(HI);
248 parity++;
250 else
252 syn_set_data(LO);
254 bit++;
256 /* 5b. Invert ACK to indicate that the data bit is ready */
257 syn_set_ack(HI);
259 /* 5c. Wait for guest to invert CLK */
260 syn_wait_clk_change(LO);
262 /* Repeat for next bit */
263 if (data[i] & (1 << bit))
265 syn_set_data(HI);
266 parity++;
268 else
270 syn_set_data(LO);
272 bit++;
274 syn_set_ack(LO);
276 syn_wait_clk_change(HI);
280 /* 7. Transmission termination sequence: */
281 /* 7a. Host may put parity bit on DATA. Hosts that do not generate
282 parity should set DATA high. Parity is 1 if there's an odd
283 number of '1' bits, or 0 if there's an even number of '1' bits. */
284 parity = parity % 2;
285 if (parity)
287 syn_set_data(HI);
289 else
291 syn_set_data(LO);
293 logf(" send parity = %d", parity);
295 /* 7b. Raise ACK to indicate that the optional parity bit is ready */
296 syn_set_ack(HI);
298 /* 7c. Guest lowers CLK */
299 syn_wait_clk_change(LO);
301 /* 7d. Pull DATA high (if parity bit was 0) */
302 syn_set_data(HI);
304 /* 7e. Lower ACK to indicate that the stop bit is ready */
305 syn_set_ack(LO);
307 /* 7f. Guest raises CLK */
308 syn_wait_clk_change(HI);
310 /* 7g. If DATA is low, guest is flushing this transfer. Host should
311 enter the flushee state. */
312 if (syn_get_data() == LO)
314 logf(" module flushing");
316 syn_wait_guest_flush();
317 return -1;
320 /* 7h. Host raises ACK and the link enters the idle state */
321 syn_set_ack(HI);
323 return len;
326 static int syn_read_data(char *data, int data_len)
328 int i, len, bit, parity;
329 char *data_ptr, tmp;
331 logf("syn_read_data...");
333 /* 1. Guest drives CLK low */
334 if (CLK != LO)
335 return 0;
337 /* 1a. If the host is willing to receive a packet it lowers ACK */
338 syn_set_ack(LO);
340 /* 2. Guest may issue a request-to-send by lowering DATA. If the
341 guest decides not to transmit a packet, it may abort the
342 transmission by not lowering DATA. */
344 /* 3. The guest raises CLK */
345 syn_wait_clk_change(HI);
347 /* 4. If the guest is still driving DATA low, the transfer is commited
348 to occur. Otherwise, the transfer is aborted. In either case,
349 the host raises ACK. */
350 if (syn_get_data() == HI)
352 logf(" read abort");
354 syn_set_ack(HI);
355 return READ_ERROR;
357 else
359 syn_set_ack(HI);
362 /* 5. Read the incoming data packet */
363 i = 0;
364 len = 0;
365 parity = 0;
366 while (i <= len)
368 bit = 0;
370 if (i < data_len)
371 data_ptr = &data[i];
372 else
373 data_ptr = &tmp;
375 *data_ptr = 0;
376 while (bit < 8)
378 /* 5b. Guset inverts CLK to indicate that data is ready */
379 syn_wait_clk_change(LO);
381 /* 5d. Read the data bit from DATA */
382 if (syn_get_data() == HI)
384 *data_ptr |= (1 << bit);
385 parity++;
387 bit++;
389 /* 5e. Invert ACK to indicate that data has been read */
390 syn_set_ack(LO);
392 /* Repeat for next bit */
393 syn_wait_clk_change(HI);
395 if (syn_get_data() == HI)
397 *data_ptr |= (1 << bit);
398 parity++;
400 bit++;
402 syn_set_ack(HI);
405 /* First byte is the packet header */
406 if (i == 0)
408 /* Format control (bit 3) should be 1 */
409 if (*data_ptr & 0x8)
411 /* Packet length is bits 0:2 */
412 len = *data_ptr & 0x7;
413 logf(" packet length = %d", len);
415 else
417 logf(" invalid format ctrl bit");
418 return READ_ERROR;
422 i++;
425 /* 7. Transmission termination cycle */
426 /* 7a. The guest generates a parity bit on DATA */
427 /* 7b. The host waits for guest to lower CLK */
428 syn_wait_clk_change(LO);
430 /* 7c. The host verifies the parity bit is correct */
431 parity = parity % 2;
432 logf(" parity check: %d / %d", syn_get_data(), parity);
434 /* TODO: parity error handling */
436 /* 7d. The host lowers ACK */
437 syn_set_ack(LO);
439 /* 7e. The host waits for the guest to raise CLK indicating
440 that the stop bit is ready */
441 syn_wait_clk_change(HI);
443 /* 7f. The host reads DATA and verifies that it is 1 */
444 if (syn_get_data() == LO)
446 logf(" framing error");
448 syn_set_ack(HI);
449 return READ_ERROR;
452 syn_set_ack(HI);
454 return len;
457 static int syn_read(char *data, int len)
459 int i;
460 int ret = READ_ERROR;
462 for (i = 0; i < READ_RETRY; i++)
464 if (syn_wait_clk_change(LO))
466 /* module is sending data */
467 ret = syn_read_data(data, len);
468 if (ret != READ_ERROR)
469 return ret;
471 syn_flush();
473 else
475 /* module is idle */
476 return 0;
480 return ret;
483 static int syn_reset(void)
485 int val, id;
486 char data[2];
488 logf("syn_reset...");
490 /* reset module 0 */
491 data[0] = (0 << 4) | (1 << 3) | 0;
492 syn_send(data, 1);
494 val = syn_read(data, 2);
495 if (val == 1)
497 val = data[0] & 0xff; /* packet header */
498 id = (data[1] >> 4) & 0xf; /* packet id */
499 if ((val == MEP_HELLO_HEADER) && (id == MEP_HELLO_ID))
501 logf(" module 0 reset");
502 return 1;
506 logf(" reset failed");
507 return 0;
510 int touchpad_init(void)
512 syn_flush();
513 syn_status = syn_reset();
515 if (syn_status)
517 /* reset interrupts */
518 syn_enable_int(false);
519 syn_enable_int(true);
521 CPU_INT_EN |= HI_MASK;
522 CPU_HI_INT_EN |= GPIO0_MASK;
525 return syn_status;
528 int touchpad_read_device(char *data, int len)
530 char tmp[4];
531 int id;
532 int val = 0;
534 if (syn_status)
536 /* disable interrupt while we read the touchpad */
537 syn_enable_int(false);
539 val = syn_read(data, len);
540 if (val > 0)
542 val = data[0] & 0xff; /* packet header */
543 id = (data[1] >> 4) & 0xf; /* packet id */
545 logf("syn_read:");
546 logf(" data[0] = 0x%08x", data[0]);
547 logf(" data[1] = 0x%08x", data[1]);
548 logf(" data[2] = 0x%08x", data[2]);
549 logf(" data[3] = 0x%08x", data[3]);
551 if ((val == MEP_BUTTON_HEADER) && (id == MEP_BUTTON_ID))
553 /* an absolute packet should follow which we ignore */
554 syn_read(tmp, 4);
556 else if (val == MEP_ABSOLUTE_HEADER)
558 logf(" pos %d", val);
559 logf(" z %d", data[3]);
560 logf(" finger %d", data[1] & 0x1);
561 logf(" gesture %d", data[1] & 0x2);
562 logf(" RelPosVld %d", data[1] & 0x4);
564 if (!(data[1] & 0x1))
566 /* finger is NOT on touch strip */
567 val = 0;
570 else
572 val = 0;
576 /* re-enable interrupts */
577 syn_enable_int(true);
580 return val;
583 int touchpad_set_parameter(char par_nr, unsigned int param)
585 char data[4];
586 int val=0;
588 if (syn_status)
590 syn_enable_int(false);
592 data[0]=0x03; /* header - addr:0,global:0,control:0,len:3 */
593 data[1]=0x40+par_nr; /* parameter number */
594 data[2]=(param >> 8) & 0xff; /* param_hi */
595 data[3]=param & 0xff; /* param_lo */
596 syn_send(data,4);
597 val=syn_read(data,1); /* get the simple ACK = 0x18 */
599 syn_enable_int(true);
601 return val;
604 int touchpad_set_buttonlights(unsigned int led_mask, char brightness)
606 char data[6];
607 int val = 0;
609 if (syn_status)
611 syn_enable_int(false);
613 /* turn on all touchpad leds */
614 data[0] = 0x05;
615 data[1] = 0x31;
616 data[2] = (brightness & 0xf) << 4;
617 data[3] = 0x00;
618 data[4] = (led_mask >> 8) & 0xff;
619 data[5] = led_mask & 0xff;
620 syn_send(data, 6);
622 /* device responds with a single-byte ACK packet */
623 val = syn_read(data, 2);
625 syn_enable_int(true);
628 return val;
631 #ifdef ROCKBOX_HAS_LOGF
632 void syn_info(void)
634 int i, val;
635 int data[8];
637 logf("syn_info...");
639 /* module base info */
640 logf("module base info:");
641 data[0] = MEP_READ;
642 data[1] = 0x80;
643 syn_send(data, 2);
644 val = syn_read(data, 8);
645 if (val > 0)
647 for (i = 0; i < 8; i++)
648 logf(" data[%d] = 0x%02x", i, data[i]);
651 /* module product info */
652 logf("module product info:");
653 data[0] = MEP_READ;
654 data[1] = 0x81;
655 syn_send(data, 2);
656 val = syn_read(data, 8);
657 if (val > 0)
659 for (i = 0; i < 8; i++)
660 logf(" data[%d] = 0x%02x", i, data[i]);
663 /* module serialization */
664 logf("module serialization:");
665 data[0] = MEP_READ;
666 data[1] = 0x82;
667 syn_send(data, 2);
668 val = syn_read(data, 8);
669 if (val > 0)
671 for (i = 0; i < 8; i++)
672 logf(" data[%d] = 0x%02x", i, data[i]);
675 /* 1-D sensor info */
676 logf("1-d sensor info:");
677 data[0] = MEP_READ;
678 data[1] = 0x80 + 0x20;
679 syn_send(data, 2);
680 val = syn_read(data, 8);
681 if (val > 0)
683 for (i = 0; i < 8; i++)
684 logf(" data[%d] = 0x%02x", i, data[i]);
687 #endif