Samsung YP-S3: implement headphones detection
[kugel-rb.git] / firmware / target / arm / s5l8700 / yps3 / button-yps3.c
blobafcf89666b2c9e0afd4c0224aba53bdbafd944cb
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-target.h"
29 /* Button driver for the touch keys on the Samsung YP-S3
31 The exact controller is not known, but it is likely from Melfas.
33 The protocol is as follows:
34 * the communication is done using three signals: DRDY, DCLK and DOUT
35 * in the idle state these signals are all high.
36 * when a key is touched or released, the key controller pulls down DRDY
37 and outputs the first bit of a 20-bit word on its DOUT signal.
38 * the CPU stores the bit, then acknowledges it by toggling the DCLK signal.
39 * the key controller prepares the next bit, then toggles its DRDY output,
40 unless all 20 bits have been transferred (in that case it stays high).
41 * the 20-bit word contains separate bits for each button, some fixed bits
42 and a bit indicating the number of keys pressed (modulo 2).
46 void button_init_device(void)
48 /* P0.5/P1.0 power switch input */
49 PCON0 &= ~(3 << 10);
50 PCON1 &= ~0x0000000F;
52 /* P1.3 headphones detect input */
53 PCON1 &= ~0x0000F000;
55 /* P1.5 DATA, P1.6 DRDY inputs (touch key controller) */
56 PCON1 &= ~0x0FF00000;
58 /* P3.4 DCLK output (touch key controller) */
59 PCON3 = (PCON3 & ~0x000F0000) | 0x00010000;
60 PDAT3 |= (1 << 4);
62 /* P4.3 hold switch input */
63 PCON4 &= ~0x0000F000;
66 static unsigned int tkey_read(void)
68 static int value = 0;
69 int i;
71 /* check activity */
72 if (PDAT1 & (1 << 6)) {
73 return value;
76 /* get key bits */
77 value = 0;
78 for (i = 0; i < 10; i++) {
79 /* sample bit from falling edge of DRDY */
80 while ((PDAT1 & (1 << 6)) != 0);
81 value <<= 1;
82 if (PDAT1 & (1 << 5)) {
83 value |= 1;
86 /* acknowledge on DCLK */
87 PDAT3 &= ~(1 << 4);
89 /* sample bit from rising edge of DRDY */
90 while ((PDAT1 & (1 << 6)) == 0);
91 value <<= 1;
92 if (PDAT1 & (1 << 5)) {
93 value |= 1;
96 /* acknowledge on DCLK */
97 PDAT3 |= (1 << 4);
99 return value;
103 int button_read_device(void)
105 int buttons = 0;
106 static unsigned int data;
108 /* hold switch */
109 if (button_hold()) {
110 return 0;
113 /* power button */
114 if (PDAT1 & (1 << 0)) {
115 buttons |= BUTTON_POWER;
118 /* touch keys */
119 data = tkey_read();
120 if (data & (1 << 9)) {
121 buttons |= BUTTON_BACK;
123 if (data & (1 << 8)) {
124 buttons |= BUTTON_UP;
126 if (data & (1 << 7)) {
127 buttons |= BUTTON_MENU;
129 if (data & (1 << 6)) {
130 buttons |= BUTTON_LEFT;
132 if (data & (1 << 5)) {
133 buttons |= BUTTON_SELECT;
135 if (data & (1 << 4)) {
136 buttons |= BUTTON_RIGHT;
138 if (data & (1 << 3)) {
139 buttons |= BUTTON_DOWN;
142 return buttons;
145 bool button_hold(void)
147 return (PDAT4 & (1 << 3));
150 bool headphones_inserted(void)
152 return ((PDAT1 & (1 << 3)) == 0);