summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_x1000/erosqnative/button-erosqnative.c
blob: c5abd7c93f7131f21c0a718ed743f78bf2da711d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 Aidan MacDonald, Dana Conrad
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "button.h"
#include "kernel.h"
#include "backlight.h"
#include "powermgmt.h"
#include "panic.h"
#include "axp-pmu.h"
#include "gpio-x1000.h"
#include "irq-x1000.h"
#include "i2c-x1000.h"
#include "eros_qn_codec.h"
#include <string.h>
#include <stdbool.h>
#include "devicedata.h"

#ifndef BOOTLOADER
# include "lcd.h"
# include "font.h"
#endif

/* ===========================================
 * | OLD STATE | NEW STATE |  DIRECTION      |
 * |   0  0    |   0  0    |  0: NO CHANGE   |
 * |   0  0    |   0  1    | -1: CCW         |
 * |   0  0    |   1  0    |  1: CW          |
 * |   0  0    |   1  1    |  0: INVALID     |
 * |   0  1    |   0  0    |  1: CW          |
 * |   0  1    |   0  1    |  0: NO CHANGE   |
 * |   0  1    |   1  0    |  0: INVALID     |
 * |   0  1    |   1  1    | -1: CCW         |
 * |   1  0    |   0  0    | -1: CCW         |
 * |   1  0    |   0  1    |  0: INVALID     |
 * |   1  0    |   1  0    |  0: NO CHANGE   |
 * |   1  0    |   1  1    |  1: CW          |
 * |   1  1    |   0  0    |  0: INVALID     |
 * |   1  1    |   0  1    |  1: CW          |
 * |   1  1    |   1  0    | -1: CCW         |
 * |   1  1    |   1  1    |  0: NO CHANGE   |
 * ===========================================
 *
 * Quadrature explanation since it's not plainly obvious how this works:
 *
 * If either of the quadrature lines change, we can look up the combination
 * of previous state and new state in the table above (enc_state[] below)
 * and it tells us whether to add 1, subtract 1, or no change from the sum (enc_position).
 * This also gives us a nice debounce, since each state can only have 1 pin change
 * at a time. I didn't come up with this, but I've used it before and it works well.
 *
 * Old state is 2 higher bits, new state is 2 lower bits of enc_current_state. */

/* list of valid quadrature states and their directions */
signed char enc_state[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile unsigned char enc_current_state = 0;
volatile signed int enc_position = 0;

/* Value of headphone detect register */
static uint8_t hp_detect_reg = 0x00;
static uint8_t hp_detect_reg_old = 0x00;
#ifndef BOOTLOADER
static uint8_t hp_detect_debounce1 = 0x00;
#endif
static uint8_t hp_detect_debounce2 = 0x00;
static uint8_t debounce_count = 0;

/* Interval to poll the register */
#define HPD_POLL_TIME (HZ/4)

#ifndef BOOTLOADER
static int hp_detect_tmo_cb(struct timeout* tmo)
{
    if (hp_detect_debounce1 == hp_detect_debounce2){
        if (debounce_count >= 2){
            debounce_count = 2;
        } else {
            debounce_count = debounce_count + 1;
        }
    } else {
        debounce_count = 0;
        hp_detect_debounce2 = hp_detect_debounce1;
    }

    i2c_descriptor* d = (i2c_descriptor*)tmo->data;
    i2c_async_queue(AXP_PMU_BUS, TIMEOUT_NOBLOCK, I2C_Q_ADD, 0, d);
    return HPD_POLL_TIME;
}

static void hp_detect_init(int version)
{
    if (version <= 3) {
        static struct timeout tmo;
        static const uint8_t gpio_reg = AXP192_REG_GPIOSTATE1;
        static i2c_descriptor desc = {
            .slave_addr = AXP_PMU_ADDR,
            .bus_cond = I2C_START | I2C_STOP,
            .tran_mode = I2C_READ,
            .buffer[0] = (void*)&gpio_reg,
            .count[0] = 1,
            .buffer[1] = &hp_detect_debounce1,
            .count[1] = 1,
            .callback = NULL,
            .arg = 0,
            .next = NULL,
        };

        /* Headphone and LO detects are wired to AXP192 GPIOs 0 and 1,
        * set them to inputs. */
        i2c_reg_write1(AXP_PMU_BUS, AXP_PMU_ADDR, AXP192_REG_GPIO0FUNCTION, 0x01); /* HP detect */
        i2c_reg_write1(AXP_PMU_BUS, AXP_PMU_ADDR, AXP192_REG_GPIO1FUNCTION, 0x01); /* LO detect */

        /* Get an initial reading before startup */
        int r = i2c_reg_read1(AXP_PMU_BUS, AXP_PMU_ADDR, gpio_reg);
        if(r >= 0)
        {
            hp_detect_reg = r;
            hp_detect_debounce1 = r;
            hp_detect_debounce2 = r;
            hp_detect_reg_old = hp_detect_reg;
        }

        /* Poll the register every second */
        timeout_register(&tmo, &hp_detect_tmo_cb, HPD_POLL_TIME, (intptr_t)&desc);
    } else {
        uint32_t b = REG_GPIO_PIN(GPIO_B);

        // initialize headphone detect variables
        // HP_detect PB14 --> bit 4
        // LO_detect PB22 --> bit 5
        hp_detect_reg = ( (b>>10)&(0x10) | (b>>17)&(0x20) );
        hp_detect_debounce1 = hp_detect_reg;
        hp_detect_debounce2 = hp_detect_reg;
        hp_detect_reg_old = hp_detect_reg;
    }
}
#endif

bool headphones_inserted(void)
{
    if (debounce_count > 1){
        hp_detect_reg = hp_detect_debounce2;
    }
    /* if the status has changed, set the output volume accordingly */
    if ((hp_detect_reg & 0x30) != (hp_detect_reg_old & 0x30))
    {
        hp_detect_reg_old = hp_detect_reg;
#if !defined(BOOTLOADER)
        eros_qn_set_outputs();
#endif
    }
    return hp_detect_reg & 0x10 ? false : true;
}

bool lineout_inserted(void)
{
    if (debounce_count > 1){
        hp_detect_reg = hp_detect_debounce2;
    }
    /* if the status has changed, set the output volume accordingly */
    if ((hp_detect_reg & 0x30) != (hp_detect_reg_old & 0x30))
    {
        hp_detect_reg_old = hp_detect_reg;
#if !defined(BOOTLOADER)
        eros_qn_set_outputs();
#endif
    }
    return hp_detect_reg & 0x20 ? false : true;
}

/* Rockbox interface */
void button_init_device(void)
{
    /* set both quadrature lines to interrupts */
    gpio_set_function(GPIO_BTN_SCROLL_A, GPIOF_IRQ_EDGE(1));
    gpio_set_function(GPIO_BTN_SCROLL_B, GPIOF_IRQ_EDGE(1));

    /* set interrupts to fire on the next edge based on current state */
    gpio_flip_edge_irq(GPIO_BTN_SCROLL_A);
    gpio_flip_edge_irq(GPIO_BTN_SCROLL_B);

    /* get current state of both encoder gpios */
    enc_current_state = (REG_GPIO_PIN(GPIO_B)>>21) & 0x0c;

    /* enable quadrature interrupts */
    gpio_enable_irq(GPIO_BTN_SCROLL_A);
    gpio_enable_irq(GPIO_BTN_SCROLL_B);

    /* Set up headphone and line out detect polling */
#ifndef BOOTLOADER
    hp_detect_init(device_data.lcd_version);
#endif
}

/* wheel Quadrature line A interrupt */
void GPIOB24(void)
{
    /* fill state with previous (2 higher bits) and current (2 lower bits) */
    enc_current_state = (enc_current_state & 0x0c) | ((REG_GPIO_PIN(GPIO_B)>>23) & 0x03);

    /* look up in table */
    enc_position = enc_position + enc_state[(enc_current_state)];

    /* move current state to previous state if valid data */
    if (enc_state[(enc_current_state)] != 0)
        enc_current_state = (enc_current_state << 2);

    /* we want the other edge next time */
    gpio_flip_edge_irq(GPIO_BTN_SCROLL_A);
}

/* wheel Quadrature line B interrupt */
void GPIOB23(void)
{
    /* fill state with previous (2 higher bits) and current (2 lower bits) */
    enc_current_state = (enc_current_state & 0x0c) | ((REG_GPIO_PIN(GPIO_B)>>23) & 0x03);

    /* look up in table */
    enc_position = enc_position + enc_state[(enc_current_state)];

    /* move current state to previous state if valid data */
    if (enc_state[(enc_current_state)] != 0)
        enc_current_state = (enc_current_state << 2);

    /* we want the other edge next time */
    gpio_flip_edge_irq(GPIO_BTN_SCROLL_B);
}

int button_read_device(void)
{
    int r = 0;

    /* Read GPIOs for normal buttons */
    uint32_t a = REG_GPIO_PIN(GPIO_A);
    uint32_t b = REG_GPIO_PIN(GPIO_B);
    uint32_t c = REG_GPIO_PIN(GPIO_C);
    uint32_t d = REG_GPIO_PIN(GPIO_D);

    /* All buttons are active low */
    if((a & (1 << 16)) == 0) r  |= BUTTON_PLAY;
    if((a & (1 << 17)) == 0) r  |= BUTTON_VOL_UP;
    if((a & (1 << 19)) == 0) r  |= BUTTON_VOL_DOWN;
#ifdef BOOTLOADER
# if EROSQN_VER >= 4
    if((b & (1 << 31)) == 0) r |= BUTTON_POWER;
    if((a & (1 << 18)) == 0) r |= BUTTON_BACK;
# else
    if((b & (1 << 7)) == 0) r  |= BUTTON_POWER;
    if((d & (1 << 5)) == 0) r  |= BUTTON_BACK;
# endif
#else
    if (device_data.lcd_version >= 4){
        if((b & (1 << 31)) == 0) r |= BUTTON_POWER;
        if((a & (1 << 18)) == 0) r |= BUTTON_BACK;
    } else {
        if((b & (1 << 7)) == 0) r  |= BUTTON_POWER;
        if((d & (1 << 5)) == 0) r  |= BUTTON_BACK;
    }
#endif
    if((b & (1 << 28)) == 0) r  |= BUTTON_MENU;
    
    if((d & (1 <<  4)) == 0) r  |= BUTTON_PREV;
    if((c & (1 << 24)) == 0) r  |= BUTTON_NEXT;

#ifndef BOOTLOADER
    if (device_data.lcd_version >= 4){
        // get new HP/LO detect states
        // HP_detect PB14 --> hp_detect bit 4
        // LO_detect PB22 --> hp_detect bit 5
        hp_detect_debounce1 = ( (b>>10)&(0x10) | (b>>17)&(0x20) );

        // enter them into the debounce process
        if (hp_detect_debounce1 == hp_detect_debounce2){
        if (debounce_count >= 2){
            debounce_count = 2;
        } else {
            debounce_count = debounce_count + 1;
        }
        } else {
            debounce_count = 0;
            hp_detect_debounce2 = hp_detect_debounce1;
        }
    }
#endif

    /* check encoder - from testing, each indent is 2 state changes or so */
    if (enc_position > 1)
    {
        /* need to use queue_post() in order to do BUTTON_SCROLL_*,
         * Rockbox treats these buttons differently. */
        queue_post(&button_queue, BUTTON_SCROLL_FWD, 0);
        enc_position = 0;
        reset_poweroff_timer();
        backlight_on();
    }
    else if (enc_position < -1)
    {
        /* need to use queue_post() in order to do BUTTON_SCROLL_*,
         * Rockbox treats these buttons differently. */
        queue_post(&button_queue, BUTTON_SCROLL_BACK, 0);
        enc_position = 0;
        reset_poweroff_timer();
        backlight_on();
    }

    return r;
}