Initial commit for the Creative ZEN X-Fi2 and X-Fi3 ports
[maemo-rb.git] / firmware / target / arm / imx233 / creative-zenxfi3 / button-zenxfi3.c
blob174ddc217c05c7ad8a5ba08fce83bd53321b0a21
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2012 by Amaury Pouly
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 ****************************************************************************/
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"
31 static struct mpr121_config_t config =
33 .ele =
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 },
45 .filters =
47 .ele =
49 .rising = {.mhd = 1, .nhd = 1, .ncl = 1, .fdl = 1 },
50 .falling = {.mhd = 1, .nhd = 1, .ncl = 0xff, .fdl = 2 },
53 .autoconf =
55 .en = true, .ren = true, .retry = RETRY_NEVER,
56 .usl = 0xc4, .lsl = 0x7f, .tl = 0xb0
58 .ele_en = ELE_EN0_x(7),
59 .cal_lock = CL_TRACK
62 #define MPR121_INTERRUPT 1
64 static int touchpad_btns = 0;
65 static long mpr121_stack[DEFAULT_STACK_SIZE/sizeof(long)];
66 static const char mpr121_thread_name[] = "mpr121";
67 static struct event_queue mpr121_queue;
69 static void mpr121_irq_cb(int bank, int pin)
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);
78 static void mpr121_thread(void)
80 struct queue_event ev;
82 while(1)
84 queue_wait(&mpr121_queue, &ev);
85 /* handle usb connect and ignore all messages except rmi interrupts */
86 if(ev.id == SYS_USB_CONNECTED)
88 usb_acknowledge(SYS_USB_CONNECTED_ACK);
89 continue;
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))
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;
109 /* enable interrupt */
110 imx233_setup_pin_irq(0, 18, true, true, false, &mpr121_irq_cb);
114 /* B0P18 is #IRQ line of the touchpad */
115 void button_init_device(void)
117 mpr121_init(0xb4);
118 mpr121_soft_reset();
119 mpr121_set_config(&config);
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);
130 bool button_hold(void)
132 /* B0P04: #hold */
133 return !imx233_get_gpio_input_mask(0, 0x10);
136 int button_read_device(void)
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)
146 old_hold = hold;
147 #ifndef BOOTLOADER
148 backlight_hold_changed(hold);
149 #endif /* BOOTLOADER */
150 if(!hold)
151 power_ignore_counter = HZ;
154 if(power_ignore_counter)
155 power_ignore_counter--;
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;