diff options
Diffstat (limited to 'firmware/target/arm/imx233/button-imx233.h')
-rw-r--r-- | firmware/target/arm/imx233/button-imx233.h | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/firmware/target/arm/imx233/button-imx233.h b/firmware/target/arm/imx233/button-imx233.h new file mode 100644 index 0000000000..61adff8436 --- /dev/null +++ b/firmware/target/arm/imx233/button-imx233.h | |||
@@ -0,0 +1,174 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2014 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 | #ifndef __button_imx233__ | ||
22 | #define __button_imx233__ | ||
23 | |||
24 | #include "button.h" | ||
25 | #include "button-target.h" | ||
26 | #include "lradc-imx233.h" | ||
27 | #include "pinctrl-imx233.h" | ||
28 | #include "power-imx233.h" | ||
29 | |||
30 | /* special values for .btn field */ | ||
31 | #define IMX233_BUTTON_END -1 /* end of the list */ | ||
32 | #define IMX233_BUTTON_HOLD -2 /* HOLD button */ | ||
33 | #define IMX233_BUTTON_JACK -3 /* JACK detect */ | ||
34 | #define IMX233_BUTTON_VDDIO -4 /* VDDIO value */ | ||
35 | |||
36 | /* values for .periph field */ | ||
37 | #define IMX233_BUTTON_GPIO 0 /* GPIO pin */ | ||
38 | #define IMX233_BUTTON_LRADC 1 /* LRADC channel */ | ||
39 | #define IMX233_BUTTON_PSWITCH 2 /* PSWITCH */ | ||
40 | |||
41 | /* values for the .flags field */ | ||
42 | #define IMX233_BUTTON_INVERTED (1 << 0) /* invert button detection */ | ||
43 | #define IMX233_BUTTON_PULLUP (1 << 1) /* pin needs a pullup (GPIO) */ | ||
44 | |||
45 | /** target-defined | ||
46 | * NOTE for proper operation: | ||
47 | * - the table must end which a dummy entry of type IMX233_BUTTON_END | ||
48 | * - default is active: high for GPIO, around value for LRADC, exact value for PSWITCH | ||
49 | * - inverted flags reverses the above behaviour | ||
50 | * - if lradc channel is relative, the .relative field specify a entry in the | ||
51 | * button table to which it is relative. This entry must have <0 .btn value | ||
52 | * and be of type LRADC. For example, if a channel is VDDIO relative, set | ||
53 | * .relative to IMX233_BUTTON_VDDIO and create an entry which .btn set to | ||
54 | * IMX233_BUTTON_VDDIO, channel set to VDDIO source and value set to the ideal values | ||
55 | * for which no correction is necessary. Note that the IMX233_BUTTON_VDDIO value is | ||
56 | * defined for convenience */ | ||
57 | struct imx233_button_map_t | ||
58 | { | ||
59 | int btn; /* target button or magic value (END, HOLD, JACK) */ | ||
60 | int periph; /* GPIO, LRADC, or PSWITCH, ... */ | ||
61 | union | ||
62 | { | ||
63 | struct | ||
64 | { | ||
65 | int bank; | ||
66 | int pin; | ||
67 | }gpio; | ||
68 | struct | ||
69 | { | ||
70 | int src; /* source channel */ | ||
71 | int value; /* expected value */ | ||
72 | int relative; /* button to which it is relative or -1 if none */ | ||
73 | }lradc; | ||
74 | struct | ||
75 | { | ||
76 | int level; /* value of the PSWITCH field: low, mid, high */ | ||
77 | }pswitch; | ||
78 | }u; | ||
79 | int flags; /* flags */ | ||
80 | const char *name; /* name of the button */ | ||
81 | /** private fields */ | ||
82 | bool last_val; /* last interpreted value */ | ||
83 | int rounds; /* how many rounds it survived (debouncing) */ | ||
84 | int threshold; /* round threshold for acceptance */ | ||
85 | }; | ||
86 | |||
87 | /* macros for the common cases */ | ||
88 | #define IMX233_BUTTON_PATH_GPIO(bank_, pin_) .periph = IMX233_BUTTON_GPIO, \ | ||
89 | .u = {.gpio = {.bank = bank_, .pin = pin_}} | ||
90 | #define IMX233_BUTTON_PATH_LRADC_REL(src_, val_, rel_) .periph = IMX233_BUTTON_LRADC, \ | ||
91 | .u = {.lradc = {.src = src_, .value = val_, .relative = rel_}} | ||
92 | #define IMX233_BUTTON_PATH_LRADC(src_, val_) IMX233_BUTTON_PATH_LRADC_REL(src_, val_, -1) | ||
93 | #define IMX233_BUTTON_PATH_PSWITCH(lvl_) .periph = IMX233_BUTTON_PSWITCH, \ | ||
94 | .u = {.pswitch = {.level = lvl_}} | ||
95 | #define IMX233_BUTTON_PATH_END() .periph = -1 | ||
96 | /* special macro for VDDIO, for convenience */ | ||
97 | #define IMX233_BUTTON_PATH_VDDIO(val_) IMX233_BUTTON_PATH_LRADC(LRADC_SRC_VDDIO, val_) | ||
98 | |||
99 | #define IMX233_BUTTON_NAMEFLAGS1(_,name_) .name = name_ | ||
100 | #define IMX233_BUTTON_NAMEFLAGS2(_,name_,f1) .name = name_, .flags = IMX233_BUTTON_##f1 | ||
101 | #define IMX233_BUTTON_NAMEFLAGS3(_,name_,f1,f2) .name = name_, \ | ||
102 | .flags = IMX233_BUTTON_##f1 | IMX233_BUTTON_##f2 | ||
103 | #define IMX233_BUTTON_NAMEFLAGS4(_,name_,f1,f2,f3) .name = name_, \ | ||
104 | .flags =IMX233_BUTTON_##f1 | IMX233_BUTTON_##f2 | IMX233_BUTTON_##f3 | ||
105 | |||
106 | #define IMX233_BUTTON__(btn_, path_, ...) \ | ||
107 | {.btn = btn_, IMX233_BUTTON_PATH_##path_, \ | ||
108 | REG_VARIADIC(IMX233_BUTTON_NAMEFLAGS, dummy, __VA_ARGS__)} | ||
109 | #define IMX233_BUTTON_(btn_, path_, ...) \ | ||
110 | IMX233_BUTTON__(IMX233_BUTTON_##btn_, path_, __VA_ARGS__) | ||
111 | #define IMX233_BUTTON(btn_, path_, ...) \ | ||
112 | IMX233_BUTTON__(BUTTON_##btn_, path_, __VA_ARGS__) | ||
113 | |||
114 | #define IMX233_BUTTON_END_() \ | ||
115 | {.btn = IMX233_BUTTON_END} | ||
116 | |||
117 | /** | ||
118 | * This header provides macro to ease definition of buttons. There three types of | ||
119 | * buttons: | ||
120 | * - normal buttons: BUTTON_X -> defined using IMX233_BUTTON(X, ...) | ||
121 | * - special buttons IMX233_BUTTON_X -> defined using IMX233_BUTTON_(X, ...) | ||
122 | * - others: X -> defined using IMX233_BUTTON__(X, ...) | ||
123 | * A definition contains three mandatory parts and a fourth optional: | ||
124 | * IMX233_BUTTON(btn, path_spec, name [, flags]) | ||
125 | * The path_spec can be of the form: | ||
126 | * - GPIO(bank, pin) | ||
127 | * - LRADC(src, value) | ||
128 | * - LRADC_REL(src, value, rel_idx) | ||
129 | * - PSWITCH(level) | ||
130 | * - END() | ||
131 | * The optional flags are specified as a list, without the IMX233_BUTTON_ prefix. | ||
132 | * Examples: | ||
133 | * - BUTTON_POWER named "power", mapped to PSWITCH mid level, no flags: | ||
134 | * IMX233_BUTTON(POWER, PSWICTH(1), "power") | ||
135 | * - IMX233_BUTTON_JACK named "jack", mapped to GPIO B2P17 inverted and pullup: | ||
136 | * IMX233_BUTTON_(JACK, GPIO(2, 17), "jack", INVERTED, PULLUP) | ||
137 | * - BIZARRE named "bizarre", mapped to LRADC channel 0, expected value 42: | ||
138 | * IMX233_BUTTON__(BIZARRE, LRADC(0, 42), "bizarre") | ||
139 | * - BUTTON_PLAY named "play", mapped to LRADc channel 2, expected value 300 related | ||
140 | * to button entry IDX | ||
141 | * IMX233_BUTTON(PLAY, LRADC_REL(2, 300, IDX), "play") | ||
142 | * - VDDIO entry, expected value 4000 (for relative entries): | ||
143 | * IMX233_BUTTON_(VDDIO, VDDIO(4000), "vddio") | ||
144 | * - last entry: | ||
145 | * IMX233_BUTTON_(END, END(), "") | ||
146 | * | ||
147 | * The driver also provides default implementations for headphones_inserted() | ||
148 | * and button_hold() which can be overriden since they have weak linkage. | ||
149 | */ | ||
150 | |||
151 | extern struct imx233_button_map_t imx233_button_map[]; | ||
152 | |||
153 | /* initialise button driver */ | ||
154 | void imx233_button_init(void); | ||
155 | /* others gives the bitmask of other buttons: the function will OR the result | ||
156 | * with them except if hold is detected in which case 0 is always returned */ | ||
157 | int imx233_button_read(int others); | ||
158 | #ifdef HAS_BUTTON_HOLD | ||
159 | bool imx233_button_read_hold(void); | ||
160 | bool button_hold(void); | ||
161 | #endif | ||
162 | #ifdef HAVE_HEADPHONE_DETECTION | ||
163 | bool imx233_button_read_jack(void); | ||
164 | bool headphones_inserted(void); | ||
165 | #endif | ||
166 | /* return interpreted button value, after processing. | ||
167 | * Argument is an index into the imx233_button_map table */ | ||
168 | bool imx233_button_read_btn(int idx); | ||
169 | /* return raw value for a button, before any processing: GPIO state, LRADC | ||
170 | * channel value, PSWITCH value. Return -1 if there is no match. | ||
171 | * Argument is an index into the imx233_button_map table */ | ||
172 | int imx233_button_read_raw(int idx); | ||
173 | |||
174 | #endif /* __button_imx233__ */ \ No newline at end of file | ||