summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_x1000/erosqnative/power-erosqnative.c
blob: 9cf64cee01b965a33f6d9990e6f5c93303dfa402 (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
/***************************************************************************
 *             __________               __   ___.
 *   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.
 *
 ****************************************************************************/

// whole file copied from m3k

#include "power.h"
#include "adc.h"
#include "system.h"
#include "kernel.h"
#ifdef HAVE_USB_CHARGING_ENABLE
# include "usb_core.h"
#endif
#include "axp192.h"
#include "i2c-x1000.h"

const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
{
    3470
};

/* The OF shuts down at this voltage */
const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
{
    3400
};

/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
{
    { 3400, 3477, 3540, 3578, 3617, 3674, 3771, 3856, 3936, 4016, 4117 }
};

/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
const unsigned short percent_to_volt_charge[11] =
{
      3400, 3477, 3540, 3578, 3617, 3674, 3771, 3856, 3936, 4016, 4117
};

void power_init(void)
{
    /* Configure I2C bus */
    i2c_x1000_set_freq(AXP_PMU_BUS, I2C_FREQ_400K);

    /* FIXME: Copy paste from M3K. Probably not necessary */
    axp_modify(AXP_REG_DCDCMODE, 0, 0xc0);

    /* Power on required supplies
     * TODO: This should be checked, though likely all but EXTEN are needed */
    axp_set_enabled_supplies(
        (1 << AXP_SUPPLY_EXTEN) |
        (1 << AXP_SUPPLY_DCDC1) |
        (1 << AXP_SUPPLY_DCDC2) |
        (1 << AXP_SUPPLY_DCDC3) |
        (1 << AXP_SUPPLY_LDO2) |
        (1 << AXP_SUPPLY_LDO3));

    /* Enable required ADCs */
    axp_set_enabled_adcs(
        (1 << AXP_ADC_BATTERY_VOLTAGE) |
        (1 << AXP_ADC_CHARGE_CURRENT) |
        (1 << AXP_ADC_DISCHARGE_CURRENT) |
        (1 << AXP_ADC_VBUS_VOLTAGE) |
        (1 << AXP_ADC_VBUS_CURRENT) |
        (1 << AXP_ADC_INTERNAL_TEMP) |
        (1 << AXP_ADC_APS_VOLTAGE));

    /* Configure USB charging */
    axp_set_vhold_level(4400);
    usb_charging_maxcurrent_change(100);

    /* Delay to give power outputs time to stabilize.
     * With the power thread delay, this can apparently go as low as 50,
     * Keeping a higher value here just to ensure the bootloader works
     * correctly. */
    mdelay(200);
}

#ifdef HAVE_USB_CHARGING_ENABLE
void usb_charging_maxcurrent_change(int maxcurrent)
{
    int vbus_limit;
    int charge_current;

    /* Note that the charge current setting is a maximum: it will be
     * reduced dynamically by the AXP192 so the combined load is less
     * than the set VBUS current limit. */
    if(maxcurrent <= 100) {
        vbus_limit = AXP_VBUS_LIMIT_500mA;
        charge_current = 100;
    } else {
        vbus_limit = AXP_VBUS_LIMIT_500mA;
        charge_current = 550;
    }

    axp_set_vbus_limit(vbus_limit);
    axp_set_charge_current(charge_current);
}
#endif

void adc_init(void)
{
}

void power_off(void)
{
    axp_power_off();
    while(1);
}

bool charging_state(void)
{
    return axp_is_charging();
}

unsigned int power_input_status(void)
{
    return axp_power_input_status();
}

int _battery_voltage(void)
{
    return axp_read_adc(AXP_ADC_BATTERY_VOLTAGE);
}

#if CONFIG_BATTERY_MEASURE & CURRENT_MEASURE
int _battery_current(void)
{
    if(charging_state())
        return axp_read_adc(AXP_ADC_CHARGE_CURRENT);
    else
        return axp_read_adc(AXP_ADC_DISCHARGE_CURRENT);
}
#endif