1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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"
23 #include "system-target.h"
24 #include "pinctrl-imx233.h"
25 #include "power-imx233.h"
28 #include "backlight.h"
31 static struct mpr121_config_t config
=
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
},
49 .rising
= {.mhd
= 1, .nhd
= 1, .ncl
= 1, .fdl
= 1 },
50 .falling
= {.mhd
= 1, .nhd
= 1, .ncl
= 0xff, .fdl
= 2 },
55 .en
= true, .ren
= true, .retry
= RETRY_NEVER
,
56 .usl
= 0xc4, .lsl
= 0x7f, .tl
= 0xb0
58 .ele_en
= ELE_EN0_x(7),
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
)
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
;
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
);
91 else if(ev
.id
!= MPR121_INTERRUPT
)
93 /* clear interrupt and get status */
96 if(!mpr121_get_touch_status(&status
))
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)
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)
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
;
143 bool hold
= button_hold();
148 backlight_hold_changed(hold
);
149 #endif /* BOOTLOADER */
151 power_ignore_counter
= HZ
;
154 if(power_ignore_counter
)
155 power_ignore_counter
--;
161 uint32_t mask
= imx233_get_gpio_input_mask(2, 0x180);
163 res
|= BUTTON_VOL_DOWN
;
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)
170 return res
| touchpad_btns
;