summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_jz47xx/xduoo_x3/sadc-xduoo_x3.c
blob: e1e578ae677b8522354821b434addd75c813d966 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 by Roman Stolyarov
 *
 * 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 "config.h"
#include "system.h"
#include "cpu.h"
#include "button.h"
#include "button-target.h"
#include "powermgmt.h"
#include "kernel.h"
#include "backlight.h"
#include "logf.h"
#include "adc.h"

#define PIN_BTN_POWER (32*0+30)
#define PIN_BTN_HOLD  (32*1+15)

#define PIN_KEY_INT   (32*4+13)
#define KEY_INT_IRQ   GPIO141

#define PIN_CHARGE_CON (32*1+7)

#define PIN_PH_DECT   (32*1+11)
#define IRQ_PH_DECT   GPIO_IRQ(PIN_PH_DECT)
#define GPIO_PH_DECT  GPIO43

#define PIN_LO_DECT   (32*1+12)
#define IRQ_LO_DECT   GPIO_IRQ(PIN_LO_DECT)
#define GPIO_LO_DECT  GPIO44

static volatile unsigned short bat_val,key_val;

bool headphones_inserted(void)
{
    return (__gpio_get_pin(PIN_PH_DECT) != 0);
}

bool lineout_inserted(void)
{
    /* We want to prevent LO being "enabled" if HP is attached
       to avoid potential eardrum damage */
    return (__gpio_get_pin(PIN_LO_DECT) == 0) && !headphones_inserted();
}

void button_init_device(void)
{
    key_val = 0xfff;

    __gpio_as_input(PIN_BTN_POWER);
    __gpio_as_input(PIN_BTN_HOLD);

    __gpio_disable_pull(PIN_BTN_POWER);
    __gpio_disable_pull(PIN_BTN_HOLD);

#if 0
    /* We poll this, no need to set it up for an interrupt */
    __gpio_as_irq_fall_edge(PIN_KEY_INT);
    system_enable_irq(GPIO_IRQ(PIN_KEY_INT));
#endif

    __gpio_set_pin(PIN_CHARGE_CON); /* 0.7 A */
    __gpio_as_output(PIN_CHARGE_CON);

    __gpio_as_input(PIN_PH_DECT);
    __gpio_disable_pull(PIN_PH_DECT);

    __gpio_as_input(PIN_LO_DECT);
    __gpio_disable_pull(PIN_LO_DECT);
}

bool button_hold(void)
{
   return (__gpio_get_pin(PIN_BTN_HOLD) ? true : false);
}

int button_read_device(void)
{
#ifndef BOOTLOADER
    static bool hold_button = false;
    bool hold_button_old;

    hold_button_old = hold_button;
    hold_button = (__gpio_get_pin(PIN_BTN_HOLD) ? true : false);
#endif

    int btn = BUTTON_NONE;
    bool gpio_btn = (__gpio_get_pin(PIN_BTN_POWER) ? false : true);

    REG_SADC_ADCFG = ADCFG_VBAT_SEL + ADCFG_CMD_AUX(1);
    REG_SADC_ADENA = ADENA_VBATEN + ADENA_AUXEN;

#ifndef BOOTLOADER
    if (hold_button != hold_button_old) {
        backlight_hold_changed(hold_button);
    }
    if (hold_button) {
        return BUTTON_NONE;
    }
#endif

    if (gpio_btn)
        btn |= BUTTON_POWER;

    if (key_val < 261)
        btn |= BUTTON_VOL_UP;
    else
    if (key_val < 653)
        btn |= BUTTON_VOL_DOWN;
    else
    if (key_val < 1101)
        btn |= BUTTON_PREV;
    else
    if (key_val < 1498)
        btn |= BUTTON_NEXT;
    else
    if (key_val < 1839)
        btn |= BUTTON_PLAY;
    else
    if (key_val < 2213)
        btn |= BUTTON_OPTION;
    else
    if (key_val < 2600)
        btn |= BUTTON_HOME;

    return btn;
}

/* called on button press interrupt */
void KEY_INT_IRQ(void)
{
}

const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
{
    /* 5% */
    3414
};

const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
{
    /* 0% */
    3307
};


/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
{
    { 3307, 3459, 3530, 3575, 3608, 3648, 3723, 3819, 3918, 4022, 4162 }
};

#if CONFIG_CHARGING
/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
const unsigned short percent_to_volt_charge[11] =
    { 3307, 3459, 3530, 3575, 3608, 3648, 3723, 3819, 3918, 4022, 4162 };
#endif /* CONFIG_CHARGING */

/* VBAT = (BDATA/1024) * 2.5V */
#define BATTERY_SCALE_FACTOR 2460

/* Returns battery voltage from ADC [millivolts] */
int _battery_voltage(void)
{
    return (bat_val*BATTERY_SCALE_FACTOR)>>10;
}

void adc_init(void)
{
    bat_val = 0xfff;

    __cpm_start_sadc();
    mdelay(10);
    REG_SADC_ADENA = 0; /* Power Up */
    mdelay(70);
    REG_SADC_ADSTATE = 0;
    REG_SADC_ADCTRL = ADCTRL_MASK_ALL - ADCTRL_ARDYM - ADCTRL_VRDYM;
    REG_SADC_ADCFG = ADCFG_VBAT_SEL + ADCFG_CMD_AUX(1);
    REG_SADC_ADCLK = (4 << 16) | (1 << 8) | 59; /* 200KHz */
    system_enable_irq(IRQ_SADC);
}

void adc_close(void)
{
    REG_SADC_ADENA = ADENA_POWER; /* Power Down */
    __intc_mask_irq(IRQ_SADC);
    mdelay(20);
    __cpm_stop_sadc();
}

/* Interrupt handler */
void SADC(void)
{
    unsigned char state;
    unsigned char sadcstate;

    sadcstate = REG_SADC_ADSTATE;
    state = REG_SADC_ADSTATE & (~REG_SADC_ADCTRL);
    REG_SADC_ADSTATE &= sadcstate;

    if(state & ADCTRL_ARDYM)
    {
        key_val = REG_SADC_ADADAT;
    }
    if(state & ADCTRL_VRDYM)
    {
        bat_val = REG_SADC_ADVDAT;
    }
}