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);
565 logf(" pos %d", val
);
566 logf(" z %d", data
[3]);
567 logf(" finger %d", data
[1] & 0x1);
568 logf(" gesture %d", data
[1] & 0x2);
569 logf(" RelPosVld %d", data
[1] & 0x4);
571 if (!(data
[1] & 0x1))
573 /* finger is NOT on touch strip */
583 /* re-enable interrupts */
584 syn_enable_int(true);
590 int touchpad_set_parameter(char mod_nr
, char par_nr
, unsigned int param
)
597 syn_enable_int(false);
599 data
[0]=0x03 | (mod_nr
<< 5); /* header - addr=mod_nr,global:0,ctrl:0,len:3 */
600 data
[1]=0x40+par_nr
; /* parameter number */
601 data
[2]=(param
>> 8) & 0xff; /* param_hi */
602 data
[3]=param
& 0xff; /* param_lo */
604 val
=syn_read(data
,1); /* get the simple ACK = 0x18 */
606 syn_enable_int(true);
612 /* Not used normally, but useful for pulling settings or determining
613 which parameters are supported */
614 int touchpad_get_parameter(char mod_nr
, char par_nr
, unsigned int *param_p
)
621 syn_enable_int(false);
623 /* 'Get MEP Parameter' command packet */
624 data
[0]=0x01 | (mod_nr
<< 5); /* header - addr=mod_nr,global:0,ctrl:0,len:1 */
625 data
[1]=0x40+par_nr
; /* parameter number */
628 /* Must not be an error packet; check size */
629 if (syn_read(data
,4) == 3)
631 /* ACK: param_hi[15:8], param_lo[7:0] */
633 *param_p
= ((unsigned int)data
[2] << 8) | data
[3];
637 syn_enable_int(true);
644 int touchpad_set_buttonlights(unsigned int led_mask
, char brightness
)
651 syn_enable_int(false);
653 /* turn on all touchpad leds */
654 #if defined(PHILIPS_HDD6330)
655 data
[0] = 0x25; /* HDD6330: second module */
660 data
[2] = (brightness
& 0xf) << 4;
662 data
[4] = (led_mask
>> 8) & 0xff;
663 data
[5] = led_mask
& 0xff;
666 /* device responds with a single-byte ACK packet */
667 val
= syn_read(data
, 2);
669 syn_enable_int(true);
675 #ifdef ROCKBOX_HAS_LOGF
683 /* module base info */
684 logf("module base info:");
688 val
= syn_read(data
, 8);
691 for (i
= 0; i
< 8; i
++)
692 logf(" data[%d] = 0x%02x", i
, data
[i
]);
695 /* module product info */
696 logf("module product info:");
700 val
= syn_read(data
, 8);
703 for (i
= 0; i
< 8; i
++)
704 logf(" data[%d] = 0x%02x", i
, data
[i
]);
707 /* module serialization */
708 logf("module serialization:");
712 val
= syn_read(data
, 8);
715 for (i
= 0; i
< 8; i
++)
716 logf(" data[%d] = 0x%02x", i
, data
[i
]);
719 /* 1-D sensor info */
720 logf("1-d sensor info:");
722 data
[1] = 0x80 + 0x20;
724 val
= syn_read(data
, 8);
727 for (i
= 0; i
< 8; i
++)
728 logf(" data[%d] = 0x%02x", i
, data
[i
]);