1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #include "button-target.h"
28 #include "synaptics-mep.h"
30 /*#define LOGF_ENABLE*/
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)
104 #define READ_ERROR -1
106 #define MEP_HELLO_HEADER 0x19
107 #define MEP_HELLO_ID 0x1
110 #define MEP_WRITE 0x3
112 static unsigned short syn_status
= 0;
114 static void syn_enable_int(bool enable
)
126 static int syn_wait_clk_change(unsigned int val
)
130 for (i
= 0; i
< 10000; i
++)
139 static void syn_set_ack(int val
)
151 static void syn_set_data(int val
)
163 static inline int syn_get_data(void)
166 #if defined(PBELL_VIBE500) /* for EABI (touchpad doesn't work without it) */
172 static void syn_wait_guest_flush(void)
174 /* Flush receiving (flushee) state:
175 handshake until DATA goes high during P3 stage */
178 syn_set_ack(HI
); /* P1 -> P2 */
179 syn_wait_clk_change(HI
); /* P2 -> P3 */
182 while (syn_get_data() == LO
)
184 syn_set_ack(HI
); /* P3 -> P0 */
185 syn_wait_clk_change(LO
); /* P0 -> P1 */
186 syn_set_ack(LO
); /* P1 -> P2 */
187 syn_wait_clk_change(HI
); /* P2 -> P3 */
190 /* Continue handshaking until back to P0 */
191 syn_set_ack(HI
); /* P3 -> P0 */
194 static void syn_flush(void)
198 logf("syn_flush...");
200 /* Flusher holds DATA low for at least 36 handshake cycles */
203 for (i
= 0; i
< 36; i
++)
205 syn_wait_clk_change(LO
); /* P0 -> P1 */
206 syn_set_ack(LO
); /* P1 -> P2 */
207 syn_wait_clk_change(HI
); /* P2 -> P3 */
208 syn_set_ack(HI
); /* P3 -> P0 */
211 /* Raise DATA in P1 stage */
212 syn_wait_clk_change(LO
); /* P0 -> P1 */
215 /* After a flush, the flushing device enters a flush-receiving (flushee)
217 syn_wait_guest_flush();
220 static int syn_send(char *data
, int len
)
227 /* 1. Lower DATA line to issue a request-to-send to guest */
230 /* 2. Wait for guest to lower CLK */
231 syn_wait_clk_change(LO
);
233 /* 3. Lower ACK (with DATA still low) */
236 /* 4. Wait for guest to raise CLK */
237 syn_wait_clk_change(HI
);
240 for (i
= 0; i
< len
; i
++)
242 logf(" sending byte: %d", data
[i
]);
247 /* 5a. Drive data low if bit is 0, or high if bit is 1 */
248 if (data
[i
] & (1 << bit
))
259 /* 5b. Invert ACK to indicate that the data bit is ready */
262 /* 5c. Wait for guest to invert CLK */
263 syn_wait_clk_change(LO
);
265 /* Repeat for next bit */
266 if (data
[i
] & (1 << bit
))
279 syn_wait_clk_change(HI
);
283 /* 7. Transmission termination sequence: */
284 /* 7a. Host may put parity bit on DATA. Hosts that do not generate
285 parity should set DATA high. Parity is 1 if there's an odd
286 number of '1' bits, or 0 if there's an even number of '1' bits. */
296 logf(" send parity = %d", parity
);
298 /* 7b. Raise ACK to indicate that the optional parity bit is ready */
301 /* 7c. Guest lowers CLK */
302 syn_wait_clk_change(LO
);
304 /* 7d. Pull DATA high (if parity bit was 0) */
307 /* 7e. Lower ACK to indicate that the stop bit is ready */
310 /* 7f. Guest raises CLK */
311 syn_wait_clk_change(HI
);
313 /* 7g. If DATA is low, guest is flushing this transfer. Host should
314 enter the flushee state. */
315 if (syn_get_data() == LO
)
317 logf(" module flushing");
319 syn_wait_guest_flush();
323 /* 7h. Host raises ACK and the link enters the idle state */
329 static int syn_read_data(char *data
, int data_len
)
331 int i
, len
, bit
, parity
;
334 logf("syn_read_data...");
336 /* 1. Guest drives CLK low */
340 /* 1a. If the host is willing to receive a packet it lowers ACK */
343 /* 2. Guest may issue a request-to-send by lowering DATA. If the
344 guest decides not to transmit a packet, it may abort the
345 transmission by not lowering DATA. */
347 /* 3. The guest raises CLK */
348 syn_wait_clk_change(HI
);
350 /* 4. If the guest is still driving DATA low, the transfer is commited
351 to occur. Otherwise, the transfer is aborted. In either case,
352 the host raises ACK. */
353 if (syn_get_data() == HI
)
365 /* 5. Read the incoming data packet */
381 /* 5b. Guset inverts CLK to indicate that data is ready */
382 syn_wait_clk_change(LO
);
384 /* 5d. Read the data bit from DATA */
385 if (syn_get_data() == HI
)
387 *data_ptr
|= (1 << bit
);
392 /* 5e. Invert ACK to indicate that data has been read */
395 /* Repeat for next bit */
396 syn_wait_clk_change(HI
);
398 if (syn_get_data() == HI
)
400 *data_ptr
|= (1 << bit
);
408 /* First byte is the packet header */
411 /* Format control (bit 3) should be 1 */
414 /* Packet length is bits 0:2 */
415 len
= *data_ptr
& 0x7;
416 logf(" packet length = %d", len
);
420 logf(" invalid format ctrl bit");
428 /* 7. Transmission termination cycle */
429 /* 7a. The guest generates a parity bit on DATA */
430 /* 7b. The host waits for guest to lower CLK */
431 syn_wait_clk_change(LO
);
433 /* 7c. The host verifies the parity bit is correct */
435 logf(" parity check: %d / %d", syn_get_data(), parity
);
437 /* TODO: parity error handling */
439 /* 7d. The host lowers ACK */
442 /* 7e. The host waits for the guest to raise CLK indicating
443 that the stop bit is ready */
444 syn_wait_clk_change(HI
);
446 /* 7f. The host reads DATA and verifies that it is 1 */
447 if (syn_get_data() == LO
)
449 logf(" framing error");
460 static int syn_read(char *data
, int len
)
463 int ret
= READ_ERROR
;
465 for (i
= 0; i
< READ_RETRY
; i
++)
467 if (syn_wait_clk_change(LO
))
469 /* module is sending data */
470 ret
= syn_read_data(data
, len
);
471 if (ret
!= READ_ERROR
)
486 static int syn_reset(void)
491 logf("syn_reset...");
494 data
[0] = (0 << 4) | (1 << 3) | 0;
497 val
= syn_read(data
, 2);
500 val
= data
[0] & 0xff; /* packet header */
501 id
= (data
[1] >> 4) & 0xf; /* packet id */
502 if ((val
== MEP_HELLO_HEADER
) && (id
== MEP_HELLO_ID
))
504 logf(" module 0 reset");
509 logf(" reset failed");
513 int touchpad_init(void)
516 syn_status
= syn_reset();
520 /* reset interrupts */
521 syn_enable_int(false);
522 syn_enable_int(true);
524 CPU_INT_EN
|= HI_MASK
;
525 CPU_HI_INT_EN
|= GPIO0_MASK
;
531 int touchpad_read_device(char *data
, int len
)
539 /* disable interrupt while we read the touchpad */
540 syn_enable_int(false);
542 val
= syn_read(data
, len
);
545 val
= data
[0] & 0xff; /* packet header */
546 id
= (data
[1] >> 4) & 0xf; /* packet id */
549 logf(" data[0] = 0x%08x", data
[0]);
550 logf(" data[1] = 0x%08x", data
[1]);
551 logf(" data[2] = 0x%08x", data
[2]);
552 logf(" data[3] = 0x%08x", data
[3]);
554 if ((val
== MEP_BUTTON_HEADER
) && (id
== MEP_BUTTON_ID
))
556 /* an absolute packet should follow which we ignore */
559 else if (val
== MEP_ABSOLUTE_HEADER
)
561 /* for HDD6330 an absolute packet will follow for sensor nr 0 which we ignore */
562 #if defined(PHILIPS_HDD6330)
563 if ((data
[3]>>6) == 0) syn_read(tmp
, 4);
564 // relay tap gesture packet
565 if (tmp
[1]==0x02) { data
[1]=0x02; data
[2]=0x00; data
[3]=0x00; }
567 logf(" pos %d", val
);
568 logf(" z %d", data
[3]);
569 logf(" finger %d", data
[1] & 0x1);
570 logf(" gesture %d", data
[1] & 0x2);
571 logf(" RelPosVld %d", data
[1] & 0x4);
573 if (!(data
[1] & 0x1))
575 /* finger is NOT on touch strip */
585 /* re-enable interrupts */
586 syn_enable_int(true);
592 int touchpad_set_parameter(char mod_nr
, char par_nr
, unsigned int param
)
599 syn_enable_int(false);
601 data
[0]=0x03 | (mod_nr
<< 5); /* header - addr=mod_nr,global:0,ctrl:0,len:3 */
602 data
[1]=0x40+par_nr
; /* parameter number */
603 data
[2]=(param
>> 8) & 0xff; /* param_hi */
604 data
[3]=param
& 0xff; /* param_lo */
606 val
=syn_read(data
,4); /* try to get the simple ACK = 0x18 */
608 /* modules > 0 sometimes don't give ACK immediately but other packets like */
609 /* absolute from the scroll strip, so it has to be ignored until we receive ACK */
610 if ((mod_nr
> 0) && ((data
[0] & 7) != 0))
611 for (i
= 0; i
< 2; i
++)
613 val
=syn_read(data
,4);
614 if (data
[0] == 0x18) break;
617 syn_enable_int(true);
623 /* Not used normally, but useful for pulling settings or determining
624 which parameters are supported */
625 int touchpad_get_parameter(char mod_nr
, char par_nr
, unsigned int *param_p
)
632 syn_enable_int(false);
634 /* 'Get MEP Parameter' command packet */
635 data
[0]=0x01 | (mod_nr
<< 5); /* header - addr=mod_nr,global:0,ctrl:0,len:1 */
636 data
[1]=0x40+par_nr
; /* parameter number */
639 /* Must not be an error packet; check size */
640 if (syn_read(data
,4) == 3)
642 /* ACK: param_hi[15:8], param_lo[7:0] */
644 *param_p
= ((unsigned int)data
[2] << 8) | data
[3];
648 syn_enable_int(true);
655 int touchpad_set_buttonlights(unsigned int led_mask
, char brightness
)
662 syn_enable_int(false);
664 /* turn on all touchpad leds */
665 #if defined(PHILIPS_HDD6330)
666 data
[0] = 0x25; /* HDD6330: second module */
671 data
[2] = (brightness
& 0xf) << 4;
673 data
[4] = (led_mask
>> 8) & 0xff;
674 data
[5] = led_mask
& 0xff;
677 /* device responds with a single-byte ACK packet */
678 val
= syn_read(data
, 2);
680 syn_enable_int(true);
686 #ifdef ROCKBOX_HAS_LOGF
694 /* module base info */
695 logf("module base info:");
699 val
= syn_read(data
, 8);
702 for (i
= 0; i
< 8; i
++)
703 logf(" data[%d] = 0x%02x", i
, data
[i
]);
706 /* module product info */
707 logf("module product info:");
711 val
= syn_read(data
, 8);
714 for (i
= 0; i
< 8; i
++)
715 logf(" data[%d] = 0x%02x", i
, data
[i
]);
718 /* module serialization */
719 logf("module serialization:");
723 val
= syn_read(data
, 8);
726 for (i
= 0; i
< 8; i
++)
727 logf(" data[%d] = 0x%02x", i
, data
[i
]);
730 /* 1-D sensor info */
731 logf("1-d sensor info:");
733 data
[1] = 0x80 + 0x20;
735 val
= syn_read(data
, 8);
738 for (i
= 0; i
< 8; i
++)
739 logf(" data[%d] = 0x%02x", i
, data
[i
]);