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"
32 /* Driver for the Synaptics Touchpad based on the "Synaptics Modular Embedded
33 Protocol: 3-Wire Interface Specification" documentation */
35 #if defined(MROBE_100)
36 #define INT_ENABLE GPIO_CLEAR_BITWISE(GPIOD_INT_LEV, 0x2);\
37 GPIO_SET_BITWISE(GPIOD_INT_EN, 0x2)
38 #define INT_DISABLE GPIO_CLEAR_BITWISE(GPIOD_INT_EN, 0x2);\
39 GPIO_SET_BITWISE(GPIOD_INT_CLR, 0x2)
41 #define ACK (GPIOD_INPUT_VAL & 0x1)
42 #define ACK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x1)
43 #define ACK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x1)
45 #define CLK ((GPIOD_INPUT_VAL & 0x2) >> 1)
46 #define CLK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
47 #define CLK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
49 #define DATA ((GPIOD_INPUT_VAL & 0x4) >> 2)
50 #define DATA_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x4);\
51 GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x4)
52 #define DATA_LO GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x4);\
53 GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x4)
54 #define DATA_CL GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x4)
56 #elif defined(PHILIPS_HDD1630)
57 #define INT_ENABLE GPIO_CLEAR_BITWISE(GPIOA_INT_LEV, 0x20);\
58 GPIO_SET_BITWISE(GPIOA_INT_EN, 0x20)
59 #define INT_DISABLE GPIO_CLEAR_BITWISE(GPIOA_INT_EN, 0x20);\
60 GPIO_SET_BITWISE(GPIOA_INT_CLR, 0x20)
62 #define ACK (GPIOD_INPUT_VAL & 0x80)
63 #define ACK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x80)
64 #define ACK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x80)
66 #define CLK ((GPIOA_INPUT_VAL & 0x20) >> 5)
67 #define CLK_HI GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x20)
68 #define CLK_LO GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_VAL, 0x20)
70 #define DATA ((GPIOA_INPUT_VAL & 0x10) >> 4)
71 #define DATA_HI GPIO_SET_BITWISE(GPIOA_OUTPUT_EN, 0x10);\
72 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x10)
73 #define DATA_LO GPIO_SET_BITWISE(GPIOA_OUTPUT_EN, 0x10);\
74 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_VAL, 0x10)
75 #define DATA_CL GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_EN, 0x10)
77 #elif defined(PHILIPS_SA9200)
78 #define INT_ENABLE GPIO_CLEAR_BITWISE(GPIOD_INT_LEV, 0x2);\
79 GPIO_SET_BITWISE(GPIOD_INT_EN, 0x2)
80 #define INT_DISABLE GPIO_CLEAR_BITWISE(GPIOD_INT_EN, 0x2);\
81 GPIO_SET_BITWISE(GPIOD_INT_CLR, 0x2)
83 #define ACK (GPIOD_INPUT_VAL & 0x8)
84 #define ACK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x8)
85 #define ACK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x8)
87 #define CLK ((GPIOD_INPUT_VAL & 0x2) >> 1)
88 #define CLK_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
89 #define CLK_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x2)
91 #define DATA ((GPIOD_INPUT_VAL & 0x10) >> 4)
92 #define DATA_HI GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x10)
93 #define DATA_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x10);\
94 GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x10)
95 #define DATA_CL GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x10)
102 #define READ_ERROR -1
104 #define MEP_HELLO_HEADER 0x19
105 #define MEP_HELLO_ID 0x1
108 #define MEP_WRITE 0x3
110 static unsigned short syn_status
= 0;
112 static void syn_enable_int(bool enable
)
124 static int syn_wait_clk_change(unsigned int val
)
128 for (i
= 0; i
< 10000; i
++)
137 static void syn_set_ack(int val
)
149 static void syn_set_data(int val
)
161 static inline int syn_get_data(void)
167 static void syn_wait_guest_flush(void)
169 /* Flush receiving (flushee) state:
170 handshake until DATA goes high during P3 stage */
173 syn_set_ack(HI
); /* P1 -> P2 */
174 syn_wait_clk_change(HI
); /* P2 -> P3 */
177 while (syn_get_data() == LO
)
179 syn_set_ack(HI
); /* P3 -> P0 */
180 syn_wait_clk_change(LO
); /* P0 -> P1 */
181 syn_set_ack(LO
); /* P1 -> P2 */
182 syn_wait_clk_change(HI
); /* P2 -> P3 */
185 /* Continue handshaking until back to P0 */
186 syn_set_ack(HI
); /* P3 -> P0 */
189 static void syn_flush(void)
193 logf("syn_flush...");
195 /* Flusher holds DATA low for at least 36 handshake cycles */
198 for (i
= 0; i
< 36; i
++)
200 syn_wait_clk_change(LO
); /* P0 -> P1 */
201 syn_set_ack(LO
); /* P1 -> P2 */
202 syn_wait_clk_change(HI
); /* P2 -> P3 */
203 syn_set_ack(HI
); /* P3 -> P0 */
206 /* Raise DATA in P1 stage */
207 syn_wait_clk_change(LO
); /* P0 -> P1 */
210 /* After a flush, the flushing device enters a flush-receiving (flushee)
212 syn_wait_guest_flush();
215 static int syn_send(char *data
, int len
)
222 /* 1. Lower DATA line to issue a request-to-send to guest */
225 /* 2. Wait for guest to lower CLK */
226 syn_wait_clk_change(LO
);
228 /* 3. Lower ACK (with DATA still low) */
231 /* 4. Wait for guest to raise CLK */
232 syn_wait_clk_change(HI
);
235 for (i
= 0; i
< len
; i
++)
237 logf(" sending byte: %d", data
[i
]);
242 /* 5a. Drive data low if bit is 0, or high if bit is 1 */
243 if (data
[i
] & (1 << bit
))
254 /* 5b. Invert ACK to indicate that the data bit is ready */
257 /* 5c. Wait for guest to invert CLK */
258 syn_wait_clk_change(LO
);
260 /* Repeat for next bit */
261 if (data
[i
] & (1 << bit
))
274 syn_wait_clk_change(HI
);
278 /* 7. Transmission termination sequence: */
279 /* 7a. Host may put parity bit on DATA. Hosts that do not generate
280 parity should set DATA high. Parity is 1 if there's an odd
281 number of '1' bits, or 0 if there's an even number of '1' bits. */
291 logf(" send parity = %d", parity
);
293 /* 7b. Raise ACK to indicate that the optional parity bit is ready */
296 /* 7c. Guest lowers CLK */
297 syn_wait_clk_change(LO
);
299 /* 7d. Pull DATA high (if parity bit was 0) */
302 /* 7e. Lower ACK to indicate that the stop bit is ready */
305 /* 7f. Guest raises CLK */
306 syn_wait_clk_change(HI
);
308 /* 7g. If DATA is low, guest is flushing this transfer. Host should
309 enter the flushee state. */
310 if (syn_get_data() == LO
)
312 logf(" module flushing");
314 syn_wait_guest_flush();
318 /* 7h. Host raises ACK and the link enters the idle state */
324 static int syn_read_data(char *data
, int data_len
)
326 int i
, len
, bit
, parity
;
329 logf("syn_read_data...");
331 /* 1. Guest drives CLK low */
335 /* 1a. If the host is willing to receive a packet it lowers ACK */
338 /* 2. Guest may issue a request-to-send by lowering DATA. If the
339 guest decides not to transmit a packet, it may abort the
340 transmission by not lowering DATA. */
342 /* 3. The guest raises CLK */
343 syn_wait_clk_change(HI
);
345 /* 4. If the guest is still driving DATA low, the transfer is commited
346 to occur. Otherwise, the transfer is aborted. In either case,
347 the host raises ACK. */
348 if (syn_get_data() == HI
)
360 /* 5. Read the incoming data packet */
376 /* 5b. Guset inverts CLK to indicate that data is ready */
377 syn_wait_clk_change(LO
);
379 /* 5d. Read the data bit from DATA */
380 if (syn_get_data() == HI
)
382 *data_ptr
|= (1 << bit
);
387 /* 5e. Invert ACK to indicate that data has been read */
390 /* Repeat for next bit */
391 syn_wait_clk_change(HI
);
393 if (syn_get_data() == HI
)
395 *data_ptr
|= (1 << bit
);
403 /* First byte is the packet header */
406 /* Format control (bit 3) should be 1 */
409 /* Packet length is bits 0:2 */
410 len
= *data_ptr
& 0x7;
411 logf(" packet length = %d", len
);
415 logf(" invalid format ctrl bit");
423 /* 7. Transmission termination cycle */
424 /* 7a. The guest generates a parity bit on DATA */
425 /* 7b. The host waits for guest to lower CLK */
426 syn_wait_clk_change(LO
);
428 /* 7c. The host verifies the parity bit is correct */
430 logf(" parity check: %d / %d", syn_get_data(), parity
);
432 /* TODO: parity error handling */
434 /* 7d. The host lowers ACK */
437 /* 7e. The host waits for the guest to raise CLK indicating
438 that the stop bit is ready */
439 syn_wait_clk_change(HI
);
441 /* 7f. The host reads DATA and verifies that it is 1 */
442 if (syn_get_data() == LO
)
444 logf(" framing error");
455 static int syn_read(char *data
, int len
)
458 int ret
= READ_ERROR
;
460 for (i
= 0; i
< READ_RETRY
; i
++)
462 if (syn_wait_clk_change(LO
))
464 /* module is sending data */
465 ret
= syn_read_data(data
, len
);
466 if (ret
!= READ_ERROR
)
481 static int syn_reset(void)
486 logf("syn_reset...");
489 data
[0] = (0 << 4) | (1 << 3) | 0;
492 val
= syn_read(data
, 2);
495 val
= data
[0] & 0xff; /* packet header */
496 id
= (data
[1] >> 4) & 0xf; /* packet id */
497 if ((val
== MEP_HELLO_HEADER
) && (id
== MEP_HELLO_ID
))
499 logf(" module 0 reset");
504 logf(" reset failed");
508 int touchpad_init(void)
511 syn_status
= syn_reset();
515 /* reset interrupts */
516 syn_enable_int(false);
517 syn_enable_int(true);
519 CPU_INT_EN
|= HI_MASK
;
520 CPU_HI_INT_EN
|= GPIO0_MASK
;
526 int touchpad_read_device(char *data
, int len
)
534 /* disable interrupt while we read the touchpad */
535 syn_enable_int(false);
537 val
= syn_read(data
, len
);
540 val
= data
[0] & 0xff; /* packet header */
541 id
= (data
[1] >> 4) & 0xf; /* packet id */
544 logf(" data[0] = 0x%08x", data
[0]);
545 logf(" data[1] = 0x%08x", data
[1]);
546 logf(" data[2] = 0x%08x", data
[2]);
547 logf(" data[3] = 0x%08x", data
[3]);
549 if ((val
== MEP_BUTTON_HEADER
) && (id
== MEP_BUTTON_ID
))
551 /* an absolute packet should follow which we ignore */
554 else if (val
== MEP_ABSOLUTE_HEADER
)
556 logf(" pos %d", val
);
557 logf(" z %d", data
[3]);
558 logf(" finger %d", data
[1] & 0x1);
559 logf(" gesture %d", data
[1] & 0x2);
560 logf(" RelPosVld %d", data
[1] & 0x4);
562 if (!(data
[1] & 0x1))
564 /* finger is NOT on touch strip */
574 /* re-enable interrupts */
575 syn_enable_int(true);
581 int touchpad_set_buttonlights(int led_mask
, char brightness
)
588 syn_enable_int(false);
590 /* turn on all touchpad leds */
593 data
[2] = (brightness
& 0xf) << 4;
595 data
[4] = (led_mask
>> 8) & 0xff;
596 data
[5] = led_mask
& 0xff;
599 /* device responds with a single-byte ACK packet */
600 val
= syn_read(data
, 2);
602 syn_enable_int(true);
608 #ifdef ROCKBOX_HAS_LOGF
616 /* module base info */
617 logf("module base info:");
621 val
= syn_read(data
, 8);
624 for (i
= 0; i
< 8; i
++)
625 logf(" data[%d] = 0x%02x", i
, data
[i
]);
628 /* module product info */
629 logf("module product info:");
633 val
= syn_read(data
, 8);
636 for (i
= 0; i
< 8; i
++)
637 logf(" data[%d] = 0x%02x", i
, data
[i
]);
640 /* module serialization */
641 logf("module serialization:");
645 val
= syn_read(data
, 8);
648 for (i
= 0; i
< 8; i
++)
649 logf(" data[%d] = 0x%02x", i
, data
[i
]);
652 /* 1-D sensor info */
653 logf("1-d sensor info:");
655 data
[1] = 0x80 + 0x20;
657 val
= syn_read(data
, 8);
660 for (i
= 0; i
< 8; i
++)
661 logf(" data[%d] = 0x%02x", i
, data
[i
]);