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 ****************************************************************************/
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 ACK (GPIOD_INPUT_VAL & 0x1)
37 #define ACK_HI GPIOD_OUTPUT_VAL |= 0x1
38 #define ACK_LO GPIOD_OUTPUT_VAL &= ~0x1
40 #define CLK ((GPIOD_INPUT_VAL & 0x2) >> 1)
41 #define CLK_HI GPIOD_OUTPUT_VAL |= 0x2
42 #define CLK_LO GPIOD_OUTPUT_VAL &= ~0x2
44 #define DATA ((GPIOD_INPUT_VAL & 0x4) >> 2)
45 #define DATA_HI GPIOD_OUTPUT_EN |= 0x4; GPIOD_OUTPUT_VAL |= 0x4
46 #define DATA_LO GPIOD_OUTPUT_EN |= 0x4; GPIOD_OUTPUT_VAL &= ~0x4
47 #define DATA_CL GPIOD_OUTPUT_EN &= ~0x4
49 #elif defined(PHILIPS_HDD1630)
50 #define ACK (GPIOD_INPUT_VAL & 0x80)
51 #define ACK_HI GPIOD_OUTPUT_VAL |= 0x80
52 #define ACK_LO GPIOD_OUTPUT_VAL &= ~0x80
54 #define CLK ((GPIOA_INPUT_VAL & 0x20) >> 5)
55 #define CLK_HI GPIOA_OUTPUT_VAL |= 0x20
56 #define CLK_LO GPIOA_OUTPUT_VAL &= ~0x20
58 #define DATA ((GPIOA_INPUT_VAL & 0x10) >> 4)
59 #define DATA_HI GPIOA_OUTPUT_EN |= 0x10; GPIOA_OUTPUT_VAL |= 0x10
60 #define DATA_LO GPIOA_OUTPUT_EN |= 0x10; GPIOA_OUTPUT_VAL &= ~0x10
61 #define DATA_CL GPIOA_OUTPUT_EN &= ~0x10
70 #define MEP_HELLO_HEADER 0x19
71 #define MEP_HELLO_ID 0x1
76 static int syn_wait_clk_change(unsigned int val
)
80 for (i
= 0; i
< 10000; i
++)
89 static inline int syn_get_data(void)
95 static void syn_wait_guest_flush(void)
97 /* Flush receiving (flushee) state:
98 handshake until DATA goes high during P3 stage */
101 ACK_HI
; /* P1 -> P2 */
102 syn_wait_clk_change(HI
); /* P2 -> P3 */
105 while (syn_get_data() == LO
)
107 ACK_HI
; /* P3 -> P0 */
108 syn_wait_clk_change(LO
); /* P0 -> P1 */
109 ACK_LO
; /* P1 -> P2 */
110 syn_wait_clk_change(HI
); /* P2 -> P3 */
113 /* Continue handshaking until back to P0 */
114 ACK_HI
; /* P3 -> P0 */
117 static void syn_flush(void)
121 logf("syn_flush...");
123 /* Flusher holds DATA low for at least 36 handshake cycles */
126 for (i
= 0; i
< 36; i
++)
128 syn_wait_clk_change(LO
); /* P0 -> P1 */
129 ACK_LO
; /* P1 -> P2 */
130 syn_wait_clk_change(HI
); /* P2 -> P3 */
131 ACK_HI
; /* P3 -> P0 */
134 /* Raise DATA in P1 stage */
135 syn_wait_clk_change(LO
); /* P0 -> P1 */
138 /* After a flush, the flushing device enters a flush-receiving (flushee)
140 syn_wait_guest_flush();
143 static int syn_send_data(int *data
, int len
)
148 logf("syn_send_data...");
150 /* 1. Lower DATA line to issue a request-to-send to guest */
153 /* 2. Wait for guest to lower CLK */
154 syn_wait_clk_change(LO
);
156 /* 3. Lower ACK (with DATA still low) */
159 /* 4. Wait for guest to raise CLK */
160 syn_wait_clk_change(HI
);
163 for (i
= 0; i
< len
; i
++)
165 logf(" sending byte: %d", data
[i
]);
170 /* 5a. Drive data low if bit is 0, or high if bit is 1 */
171 if (data
[i
] & (1 << bit
))
182 /* 5b. Invert ACK to indicate that the data bit is ready */
185 /* 5c. Wait for guest to invert CLK */
186 syn_wait_clk_change(LO
);
188 /* Repeat for next bit */
189 if (data
[i
] & (1 << bit
))
202 syn_wait_clk_change(HI
);
206 /* 7. Transmission termination sequence: */
207 /* 7a. Host may put parity bit on DATA. Hosts that do not generate
208 parity should set DATA high. Parity is 1 if there's an odd
209 number of '1' bits, or 0 if there's an even number of '1' bits. */
219 logf(" send parity = %d", parity
);
221 /* 7b. Raise ACK to indicate that the optional parity bit is ready */
224 /* 7c. Guest lowers CLK */
225 syn_wait_clk_change(LO
);
227 /* 7d. Pull DATA high (if parity bit was 0) */
230 /* 7e. Lower ACK to indicate that the stop bit is ready */
233 /* 7f. Guest raises CLK */
234 syn_wait_clk_change(HI
);
236 /* 7g. If DATA is low, guest is flushing this transfer. Host should
237 enter the flushee state. */
238 if (syn_get_data() == LO
)
240 logf(" module flushing");
242 syn_wait_guest_flush();
246 /* 7h. Host raises ACK and the link enters the idle state */
252 static int syn_read_data(int *data
, int data_len
)
254 int i
, len
, bit
, parity
, tmp
;
257 logf("syn_read_data...");
259 /* 1. Guest drives CLK low */
263 /* 1a. If the host is willing to receive a packet it lowers ACK */
266 /* 2. Guest may issue a request-to-send by lowering DATA. If the
267 guest decides not to transmit a packet, it may abort the
268 transmission by not lowering DATA. */
270 /* 3. The guest raises CLK */
271 syn_wait_clk_change(HI
);
273 /* 4. If the guest is still driving DATA low, the transfer is commited
274 to occur. Otherwise, the transfer is aborted. In either case,
275 the host raises ACK. */
276 if (syn_get_data() == HI
)
288 /* 5. Read the incoming data packet */
304 /* 5b. Guset inverts CLK to indicate that data is ready */
305 syn_wait_clk_change(LO
);
307 /* 5d. Read the data bit from DATA */
308 if (syn_get_data() == HI
)
310 *data_ptr
|= (1 << bit
);
315 /* 5e. Invert ACK to indicate that data has been read */
318 /* Repeat for next bit */
319 syn_wait_clk_change(HI
);
321 if (syn_get_data() == HI
)
323 *data_ptr
|= (1 << bit
);
331 /* First byte is the packet header */
334 /* Format control (bit 3) should be 1 */
337 /* Packet length is bits 0:2 */
338 len
= *data_ptr
& 0x7;
339 logf(" packet length = %d", len
);
343 logf(" invalid format ctrl bit");
351 /* 7. Transmission termination cycle */
352 /* 7a. The guest generates a parity bit on DATA */
353 /* 7b. The host waits for guest to lower CLK */
354 syn_wait_clk_change(LO
);
356 /* 7c. The host verifies the parity bit is correct */
358 logf(" parity check: %d / %d", syn_get_data(), parity
);
360 /* TODO: parity error handling */
362 /* 7d. The host lowers ACK */
365 /* 7e. The host waits for the guest to raise CLK indicating
366 that the stop bit is ready */
367 syn_wait_clk_change(HI
);
369 /* 7f. The host reads DATA and verifies that it is 1 */
370 if (syn_get_data() == LO
)
372 logf(" framing error");
383 int syn_read_device(int *data
, int len
)
386 int ret
= READ_ERROR
;
388 for (i
= 0; i
< READ_RETRY
; i
++)
390 if (syn_wait_clk_change(LO
))
392 /* module is sending data */
393 ret
= syn_read_data(data
, len
);
394 if (ret
!= READ_ERROR
)
409 static int syn_reset(void)
414 logf("syn_reset...");
417 val
= (0 << 4) | (1 << 3) | 0;
418 syn_send_data(&val
, 1);
420 val
= syn_read_device(data
, 2);
423 val
= data
[0] & 0xff; /* packet header */
424 id
= (data
[1] >> 4) & 0xf; /* packet id */
425 if ((val
== MEP_HELLO_HEADER
) && (id
== MEP_HELLO_ID
))
427 logf(" module 0 reset");
432 logf(" reset failed");
442 #ifdef ROCKBOX_HAS_LOGF
450 /* module base info */
451 logf("module base info:");
454 syn_send_data(data
, 2);
455 val
= syn_read_device(data
, 8);
458 for (i
= 0; i
< 8; i
++)
459 logf(" data[%d] = 0x%02x", i
, data
[i
]);
462 /* module product info */
463 logf("module product info:");
466 syn_send_data(data
, 2);
467 val
= syn_read_device(data
, 8);
470 for (i
= 0; i
< 8; i
++)
471 logf(" data[%d] = 0x%02x", i
, data
[i
]);
474 /* module serialization */
475 logf("module serialization:");
478 syn_send_data(data
, 2);
479 val
= syn_read_device(data
, 8);
482 for (i
= 0; i
< 8; i
++)
483 logf(" data[%d] = 0x%02x", i
, data
[i
]);
486 /* 1-D sensor info */
487 logf("1-d sensor info:");
489 data
[1] = 0x80 + 0x20;
490 syn_send_data(data
, 2);
491 val
= syn_read_device(data
, 8);
494 for (i
= 0; i
< 8; i
++)
495 logf(" data[%d] = 0x%02x", i
, data
[i
]);