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)
169 static void syn_wait_guest_flush(void)
171 /* Flush receiving (flushee) state:
172 handshake until DATA goes high during P3 stage */
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)
195 logf("syn_flush...");
197 /* Flusher holds DATA low for at least 36 handshake cycles */
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 */
212 /* After a flush, the flushing device enters a flush-receiving (flushee)
214 syn_wait_guest_flush();
217 static int syn_send(char *data
, int len
)
224 /* 1. Lower DATA line to issue a request-to-send to guest */
227 /* 2. Wait for guest to lower CLK */
228 syn_wait_clk_change(LO
);
230 /* 3. Lower ACK (with DATA still low) */
233 /* 4. Wait for guest to raise CLK */
234 syn_wait_clk_change(HI
);
237 for (i
= 0; i
< len
; i
++)
239 logf(" sending byte: %d", data
[i
]);
244 /* 5a. Drive data low if bit is 0, or high if bit is 1 */
245 if (data
[i
] & (1 << bit
))
256 /* 5b. Invert ACK to indicate that the data bit is ready */
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
))
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. */
293 logf(" send parity = %d", parity
);
295 /* 7b. Raise ACK to indicate that the optional parity bit is ready */
298 /* 7c. Guest lowers CLK */
299 syn_wait_clk_change(LO
);
301 /* 7d. Pull DATA high (if parity bit was 0) */
304 /* 7e. Lower ACK to indicate that the stop bit is ready */
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();
320 /* 7h. Host raises ACK and the link enters the idle state */
326 static int syn_read_data(char *data
, int data_len
)
328 int i
, len
, bit
, parity
;
331 logf("syn_read_data...");
333 /* 1. Guest drives CLK low */
337 /* 1a. If the host is willing to receive a packet it lowers ACK */
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
)
362 /* 5. Read the incoming data packet */
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
);
389 /* 5e. Invert ACK to indicate that data has been read */
392 /* Repeat for next bit */
393 syn_wait_clk_change(HI
);
395 if (syn_get_data() == HI
)
397 *data_ptr
|= (1 << bit
);
405 /* First byte is the packet header */
408 /* Format control (bit 3) should be 1 */
411 /* Packet length is bits 0:2 */
412 len
= *data_ptr
& 0x7;
413 logf(" packet length = %d", len
);
417 logf(" invalid format ctrl bit");
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 */
432 logf(" parity check: %d / %d", syn_get_data(), parity
);
434 /* TODO: parity error handling */
436 /* 7d. The host lowers ACK */
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");
457 static int syn_read(char *data
, int len
)
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
)
483 static int syn_reset(void)
488 logf("syn_reset...");
491 data
[0] = (0 << 4) | (1 << 3) | 0;
494 val
= syn_read(data
, 2);
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");
506 logf(" reset failed");
510 int touchpad_init(void)
513 syn_status
= syn_reset();
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
;
528 int touchpad_read_device(char *data
, int len
)
536 /* disable interrupt while we read the touchpad */
537 syn_enable_int(false);
539 val
= syn_read(data
, len
);
542 val
= data
[0] & 0xff; /* packet header */
543 id
= (data
[1] >> 4) & 0xf; /* packet id */
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 */
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 */
576 /* re-enable interrupts */
577 syn_enable_int(true);
583 int touchpad_set_parameter(char par_nr
, unsigned int param
)
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 */
597 val
=syn_read(data
,1); /* get the simple ACK = 0x18 */
599 syn_enable_int(true);
604 int touchpad_set_buttonlights(unsigned int led_mask
, char brightness
)
611 syn_enable_int(false);
613 /* turn on all touchpad leds */
616 data
[2] = (brightness
& 0xf) << 4;
618 data
[4] = (led_mask
>> 8) & 0xff;
619 data
[5] = led_mask
& 0xff;
622 /* device responds with a single-byte ACK packet */
623 val
= syn_read(data
, 2);
625 syn_enable_int(true);
631 #ifdef ROCKBOX_HAS_LOGF
639 /* module base info */
640 logf("module base info:");
644 val
= syn_read(data
, 8);
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:");
656 val
= syn_read(data
, 8);
659 for (i
= 0; i
< 8; i
++)
660 logf(" data[%d] = 0x%02x", i
, data
[i
]);
663 /* module serialization */
664 logf("module serialization:");
668 val
= syn_read(data
, 8);
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:");
678 data
[1] = 0x80 + 0x20;
680 val
= syn_read(data
, 8);
683 for (i
= 0; i
< 8; i
++)
684 logf(" data[%d] = 0x%02x", i
, data
[i
]);