summaryrefslogtreecommitdiff
path: root/firmware/target/arm/imx233/creative-zenxfi3/button-zenxfi3.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/imx233/creative-zenxfi3/button-zenxfi3.c')
-rw-r--r--firmware/target/arm/imx233/creative-zenxfi3/button-zenxfi3.c171
1 files changed, 171 insertions, 0 deletions
diff --git a/firmware/target/arm/imx233/creative-zenxfi3/button-zenxfi3.c b/firmware/target/arm/imx233/creative-zenxfi3/button-zenxfi3.c
new file mode 100644
index 0000000000..174ddc217c
--- /dev/null
+++ b/firmware/target/arm/imx233/creative-zenxfi3/button-zenxfi3.c
@@ -0,0 +1,171 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2012 by Amaury Pouly
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "button-target.h"
22#include "system.h"
23#include "system-target.h"
24#include "pinctrl-imx233.h"
25#include "power-imx233.h"
26#include "string.h"
27#include "usb.h"
28#include "backlight.h"
29#include "mpr121.h"
30
31static struct mpr121_config_t config =
32{
33 .ele =
34 {
35 [0] = {.tth = 5, .rth = 4 },
36 [1] = {.tth = 5, .rth = 4 },
37 [2] = {.tth = 5, .rth = 4 },
38 [3] = {.tth = 5, .rth = 4 },
39 [4] = {.tth = 4, .rth = 3 },
40 [5] = {.tth = 4, .rth = 3 },
41 [6] = {.tth = 5, .rth = 4 },
42 [7] = {.tth = 5, .rth = 4 },
43 [8] = {.gpio = ELE_GPIO_OUTPUT_OPEN_LED },
44 },
45 .filters =
46 {
47 .ele =
48 {
49 .rising = {.mhd = 1, .nhd = 1, .ncl = 1, .fdl = 1 },
50 .falling = {.mhd = 1, .nhd = 1, .ncl = 0xff, .fdl = 2 },
51 }
52 },
53 .autoconf =
54 {
55 .en = true, .ren = true, .retry = RETRY_NEVER,
56 .usl = 0xc4, .lsl = 0x7f, .tl = 0xb0
57 },
58 .ele_en = ELE_EN0_x(7),
59 .cal_lock = CL_TRACK
60};
61
62#define MPR121_INTERRUPT 1
63
64static int touchpad_btns = 0;
65static long mpr121_stack[DEFAULT_STACK_SIZE/sizeof(long)];
66static const char mpr121_thread_name[] = "mpr121";
67static struct event_queue mpr121_queue;
68
69static void mpr121_irq_cb(int bank, int pin)
70{
71 (void) bank;
72 (void) pin;
73 /* the callback will not be fired until interrupt is enabled back so
74 * the queue will not overflow or contain multiple MPR121_INTERRUPT events */
75 queue_post(&mpr121_queue, MPR121_INTERRUPT, 0);
76}
77
78static void mpr121_thread(void)
79{
80 struct queue_event ev;
81
82 while(1)
83 {
84 queue_wait(&mpr121_queue, &ev);
85 /* handle usb connect and ignore all messages except rmi interrupts */
86 if(ev.id == SYS_USB_CONNECTED)
87 {
88 usb_acknowledge(SYS_USB_CONNECTED_ACK);
89 continue;
90 }
91 else if(ev.id != MPR121_INTERRUPT)
92 continue;
93 /* clear interrupt and get status */
94 unsigned status;
95 touchpad_btns = 0;
96 if(!mpr121_get_touch_status(&status))
97 {
98 /* ELE3: up
99 * ELE4: back
100 * ELE5: menu
101 * ELE6: down
102 * ELE7: play */
103 if(status & 0x8) touchpad_btns |= BUTTON_UP;
104 if(status & 0x10) touchpad_btns |= BUTTON_BACK;
105 if(status & 0x20) touchpad_btns |= BUTTON_MENU;
106 if(status & 0x40) touchpad_btns |= BUTTON_DOWN;
107 if(status & 0x80) touchpad_btns |= BUTTON_PLAY;
108 }
109 /* enable interrupt */
110 imx233_setup_pin_irq(0, 18, true, true, false, &mpr121_irq_cb);
111 }
112}
113
114/* B0P18 is #IRQ line of the touchpad */
115void button_init_device(void)
116{
117 mpr121_init(0xb4);
118 mpr121_soft_reset();
119 mpr121_set_config(&config);
120
121 queue_init(&mpr121_queue, true);
122 create_thread(mpr121_thread, mpr121_stack, sizeof(mpr121_stack), 0,
123 mpr121_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
124 /* enable interrupt */
125 imx233_set_pin_function(0, 18, PINCTRL_FUNCTION_GPIO);
126 imx233_enable_gpio_output(0, 18, false);
127 imx233_setup_pin_irq(0, 18, true, true, false, &mpr121_irq_cb);
128}
129
130bool button_hold(void)
131{
132 /* B0P04: #hold */
133 return !imx233_get_gpio_input_mask(0, 0x10);
134}
135
136int button_read_device(void)
137{
138 /* since sliding hold will usually trigger power, ignore power button
139 * for one second after hold is released */
140 static int power_ignore_counter = 0;
141 static bool old_hold;
142 /* light handling */
143 bool hold = button_hold();
144 if(hold != old_hold)
145 {
146 old_hold = hold;
147#ifndef BOOTLOADER
148 backlight_hold_changed(hold);
149#endif /* BOOTLOADER */
150 if(!hold)
151 power_ignore_counter = HZ;
152 }
153
154 if(power_ignore_counter)
155 power_ignore_counter--;
156
157 int res = 0;
158 /* B2P07: #volume-
159 * B2P08: #volume+
160 * PSWITCH: power */
161 uint32_t mask = imx233_get_gpio_input_mask(2, 0x180);
162 if(!(mask & 0x80))
163 res |= BUTTON_VOL_DOWN;
164 if(!(mask & 0x100))
165 res |= BUTTON_VOL_UP;
166 /* WARNING: it seems that the voltage on PSWITCH depends on whether
167 * usb is connected or not ! Thus the value of this field can be 1 or 3 */
168 if(__XTRACT(HW_POWER_STS, PSWITCH) != 0 && power_ignore_counter == 0)
169 res |= BUTTON_POWER;
170 return res | touchpad_btns;
171}