Samsung yp-s3: clean up the lcd and button driver (making things static, rename varia...
[kugel-rb.git] / firmware / target / arm / s5l8700 / yps3 / button-yps3.c
blobbde322633c817398de5a4093d205f944fe21b5e0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Bertrik Sikken
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 <stdbool.h>
23 #include "config.h"
25 #include "inttypes.h"
26 #include "s5l8700.h"
27 #include "button.h"
28 #include "button-target.h"
30 /* Button driver for the touch keys on the Samsung YP-S3
32 The exact controller is not known, but it is likely from Melfas.
34 The protocol is as follows:
35 * the communication is done using three signals: DRDY, DCLK and DOUT
36 * in the idle state these signals are all high.
37 * when a key is touched or released, the key controller pulls down DRDY
38 and outputs the first bit of a 20-bit word on its DOUT signal.
39 * the CPU stores the bit, then acknowledges it by toggling the DCLK signal.
40 * the key controller prepares the next bit, then toggles its DRDY output,
41 unless all 20 bits have been transferred (in that case it stays high).
42 * the 20-bit word contains separate bits for each button, some fixed bits
43 and a bit indicating the number of keys pressed (modulo 2).
47 void button_init_device(void)
49 /* P0.5/P1.0 power switch input */
50 PCON0 &= ~(3 << 10);
51 PCON1 &= ~0x0000000F;
53 /* P1.3 headphones detect input */
54 PCON1 &= ~0x0000F000;
56 /* P1.5 DATA, P1.6 DRDY inputs (touch key controller) */
57 PCON1 &= ~0x0FF00000;
59 /* P3.4 DCLK output (touch key controller) */
60 PCON3 = (PCON3 & ~0x000F0000) | 0x00010000;
61 PDAT3 |= (1 << 4);
63 /* P4.3 hold switch input */
64 PCON4 &= ~0x0000F000;
67 /* returns the raw 20-bit word from the touch key controller */
68 static int tkey_read(void)
70 static int value = 0;
71 int i;
73 /* check activity */
74 if (PDAT1 & (1 << 6)) {
75 return value;
78 /* get key bits */
79 value = 0;
80 for (i = 0; i < 10; i++) {
81 /* sample bit from falling edge of DRDY */
82 while ((PDAT1 & (1 << 6)) != 0);
83 value <<= 1;
84 if (PDAT1 & (1 << 5)) {
85 value |= 1;
88 /* acknowledge on DCLK */
89 PDAT3 &= ~(1 << 4);
91 /* sample bit from rising edge of DRDY */
92 while ((PDAT1 & (1 << 6)) == 0);
93 value <<= 1;
94 if (PDAT1 & (1 << 5)) {
95 value |= 1;
98 /* acknowledge on DCLK */
99 PDAT3 |= (1 << 4);
101 return value;
105 int button_read_device(void)
107 int buttons = 0;
108 int tkey_data;
110 /* hold switch */
111 if (button_hold()) {
112 return 0;
115 /* power button */
116 if (PDAT1 & (1 << 0)) {
117 buttons |= BUTTON_POWER;
120 /* touch keys */
121 tkey_data = tkey_read();
122 if (tkey_data & (1 << 9)) {
123 buttons |= BUTTON_BACK;
125 if (tkey_data & (1 << 8)) {
126 buttons |= BUTTON_UP;
128 if (tkey_data & (1 << 7)) {
129 buttons |= BUTTON_MENU;
131 if (tkey_data & (1 << 6)) {
132 buttons |= BUTTON_LEFT;
134 if (tkey_data & (1 << 5)) {
135 buttons |= BUTTON_SELECT;
137 if (tkey_data & (1 << 4)) {
138 buttons |= BUTTON_RIGHT;
140 if (tkey_data & (1 << 3)) {
141 buttons |= BUTTON_DOWN;
144 return buttons;
147 bool button_hold(void)
149 return (PDAT4 & (1 << 3));
152 bool headphones_inserted(void)
154 return ((PDAT1 & (1 << 3)) == 0);