Prepare new maemo release
[maemo-rb.git] / firmware / drivers / synaptics-mep.c
blob01845bfe0c54f5b2859b13fe39fd58480ba17332
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdlib.h>
23 #include "config.h"
24 #include "cpu.h"
25 #include "system.h"
26 #include "kernel.h"
27 #include "button-target.h"
28 #include "synaptics-mep.h"
30 /*#define LOGF_ENABLE*/
31 #include "logf.h"
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)
98 #endif
100 #define LO 0
101 #define HI 1
103 #define READ_RETRY 8
104 #define READ_ERROR -1
106 #define MEP_HELLO_HEADER 0x19
107 #define MEP_HELLO_ID 0x1
109 #define MEP_READ 0x1
110 #define MEP_WRITE 0x3
112 static unsigned short syn_status = 0;
114 static void syn_enable_int(bool enable)
116 if (enable)
118 INT_ENABLE;
120 else
122 INT_DISABLE;
126 static int syn_wait_clk_change(unsigned int val)
128 int i;
130 for (i = 0; i < 10000; i++)
132 if (CLK == val)
133 return 1;
136 return 0;
139 static void syn_set_ack(int val)
141 if (val == HI)
143 ACK_HI;
145 else
147 ACK_LO;
151 static void syn_set_data(int val)
153 if (val == HI)
155 DATA_HI;
157 else
159 DATA_LO;
163 static inline int syn_get_data(void)
165 DATA_CL;
166 #if defined(PBELL_VIBE500) /* for EABI (touchpad doesn't work without it) */
167 udelay(0);
168 #endif
169 return DATA;
172 static void syn_wait_guest_flush(void)
174 /* Flush receiving (flushee) state:
175 handshake until DATA goes high during P3 stage */
176 if (CLK == LO)
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)
196 int i;
198 logf("syn_flush...");
200 /* Flusher holds DATA low for at least 36 handshake cycles */
201 syn_set_data(LO);
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 */
213 syn_set_data(HI);
215 /* After a flush, the flushing device enters a flush-receiving (flushee)
216 state */
217 syn_wait_guest_flush();
220 static int syn_send(char *data, int len)
222 int i, bit;
223 int parity = 0;
225 logf("syn_send...");
227 /* 1. Lower DATA line to issue a request-to-send to guest */
228 syn_set_data(LO);
230 /* 2. Wait for guest to lower CLK */
231 syn_wait_clk_change(LO);
233 /* 3. Lower ACK (with DATA still low) */
234 syn_set_ack(LO);
236 /* 4. Wait for guest to raise CLK */
237 syn_wait_clk_change(HI);
239 /* 5. Send data */
240 for (i = 0; i < len; i++)
242 logf(" sending byte: %d", data[i]);
244 bit = 0;
245 while (bit < 8)
247 /* 5a. Drive data low if bit is 0, or high if bit is 1 */
248 if (data[i] & (1 << bit))
250 syn_set_data(HI);
251 parity++;
253 else
255 syn_set_data(LO);
257 bit++;
259 /* 5b. Invert ACK to indicate that the data bit is ready */
260 syn_set_ack(HI);
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))
268 syn_set_data(HI);
269 parity++;
271 else
273 syn_set_data(LO);
275 bit++;
277 syn_set_ack(LO);
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. */
287 parity = parity % 2;
288 if (parity)
290 syn_set_data(HI);
292 else
294 syn_set_data(LO);
296 logf(" send parity = %d", parity);
298 /* 7b. Raise ACK to indicate that the optional parity bit is ready */
299 syn_set_ack(HI);
301 /* 7c. Guest lowers CLK */
302 syn_wait_clk_change(LO);
304 /* 7d. Pull DATA high (if parity bit was 0) */
305 syn_set_data(HI);
307 /* 7e. Lower ACK to indicate that the stop bit is ready */
308 syn_set_ack(LO);
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();
320 return -1;
323 /* 7h. Host raises ACK and the link enters the idle state */
324 syn_set_ack(HI);
326 return len;
329 static int syn_read_data(char *data, int data_len)
331 int i, len, bit, parity;
332 char *data_ptr, tmp;
334 logf("syn_read_data...");
336 /* 1. Guest drives CLK low */
337 if (CLK != LO)
338 return 0;
340 /* 1a. If the host is willing to receive a packet it lowers ACK */
341 syn_set_ack(LO);
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)
355 logf(" read abort");
357 syn_set_ack(HI);
358 return READ_ERROR;
360 else
362 syn_set_ack(HI);
365 /* 5. Read the incoming data packet */
366 i = 0;
367 len = 0;
368 parity = 0;
369 while (i <= len)
371 bit = 0;
373 if (i < data_len)
374 data_ptr = &data[i];
375 else
376 data_ptr = &tmp;
378 *data_ptr = 0;
379 while (bit < 8)
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);
388 parity++;
390 bit++;
392 /* 5e. Invert ACK to indicate that data has been read */
393 syn_set_ack(LO);
395 /* Repeat for next bit */
396 syn_wait_clk_change(HI);
398 if (syn_get_data() == HI)
400 *data_ptr |= (1 << bit);
401 parity++;
403 bit++;
405 syn_set_ack(HI);
408 /* First byte is the packet header */
409 if (i == 0)
411 /* Format control (bit 3) should be 1 */
412 if (*data_ptr & 0x8)
414 /* Packet length is bits 0:2 */
415 len = *data_ptr & 0x7;
416 logf(" packet length = %d", len);
418 else
420 logf(" invalid format ctrl bit");
421 return READ_ERROR;
425 i++;
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 */
434 parity = parity % 2;
435 logf(" parity check: %d / %d", syn_get_data(), parity);
437 /* TODO: parity error handling */
439 /* 7d. The host lowers ACK */
440 syn_set_ack(LO);
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");
451 syn_set_ack(HI);
452 return READ_ERROR;
455 syn_set_ack(HI);
457 return len;
460 static int syn_read(char *data, int len)
462 int i;
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)
472 return ret;
474 syn_flush();
476 else
478 /* module is idle */
479 return 0;
483 return ret;
486 static int syn_reset(void)
488 int val, id;
489 char data[2];
491 logf("syn_reset...");
493 /* reset module 0 */
494 data[0] = (0 << 4) | (1 << 3) | 0;
495 syn_send(data, 1);
497 val = syn_read(data, 2);
498 if (val == 1)
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");
505 return 1;
509 logf(" reset failed");
510 return 0;
513 int touchpad_init(void)
515 syn_flush();
516 syn_status = syn_reset();
518 if (syn_status)
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;
528 return syn_status;
531 int touchpad_read_device(char *data, int len)
533 char tmp[4];
534 int id;
535 int val = 0;
537 if (syn_status)
539 /* disable interrupt while we read the touchpad */
540 syn_enable_int(false);
542 val = syn_read(data, len);
543 if (val > 0)
545 val = data[0] & 0xff; /* packet header */
546 id = (data[1] >> 4) & 0xf; /* packet id */
548 logf("syn_read:");
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 */
557 syn_read(tmp, 4);
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; }
566 #endif
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 */
576 val = 0;
579 else
581 val = 0;
585 /* re-enable interrupts */
586 syn_enable_int(true);
589 return val;
592 int touchpad_set_parameter(char mod_nr, char par_nr, unsigned int param)
594 char data[4];
595 int i, val=0;
597 if (syn_status)
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 */
605 syn_send(data,4);
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);
619 return val;
622 #if 0
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)
627 char data[4];
628 int val = 0;
630 if (syn_status)
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 */
637 syn_send(data,2);
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] */
643 if (param_p)
644 *param_p = ((unsigned int)data[2] << 8) | data[3];
645 val = 3;
648 syn_enable_int(true);
651 return val;
653 #endif
655 int touchpad_set_buttonlights(unsigned int led_mask, char brightness)
657 char data[6];
658 int val = 0;
660 if (syn_status)
662 syn_enable_int(false);
664 /* turn on all touchpad leds */
665 #if defined(PHILIPS_HDD6330)
666 data[0] = 0x25; /* HDD6330: second module */
667 #else
668 data[0] = 0x05;
669 #endif
670 data[1] = 0x31;
671 data[2] = (brightness & 0xf) << 4;
672 data[3] = 0x00;
673 data[4] = (led_mask >> 8) & 0xff;
674 data[5] = led_mask & 0xff;
675 syn_send(data, 6);
677 /* device responds with a single-byte ACK packet */
678 val = syn_read(data, 2);
680 syn_enable_int(true);
683 return val;
686 #ifdef ROCKBOX_HAS_LOGF
687 void syn_info(void)
689 int i, val;
690 int data[8];
692 logf("syn_info...");
694 /* module base info */
695 logf("module base info:");
696 data[0] = MEP_READ;
697 data[1] = 0x80;
698 syn_send(data, 2);
699 val = syn_read(data, 8);
700 if (val > 0)
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:");
708 data[0] = MEP_READ;
709 data[1] = 0x81;
710 syn_send(data, 2);
711 val = syn_read(data, 8);
712 if (val > 0)
714 for (i = 0; i < 8; i++)
715 logf(" data[%d] = 0x%02x", i, data[i]);
718 /* module serialization */
719 logf("module serialization:");
720 data[0] = MEP_READ;
721 data[1] = 0x82;
722 syn_send(data, 2);
723 val = syn_read(data, 8);
724 if (val > 0)
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:");
732 data[0] = MEP_READ;
733 data[1] = 0x80 + 0x20;
734 syn_send(data, 2);
735 val = syn_read(data, 8);
736 if (val > 0)
738 for (i = 0; i < 8; i++)
739 logf(" data[%d] = 0x%02x", i, data[i]);
742 #endif