summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/sonynwz/power-nwz.c
blob: a5b19d6ead7395e83c0c2371fc4ae464aab6ad9d (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 Amaury Pouly
 *
 * 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 "system.h"
#include "power-nwz.h"
#include "button-target.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/reboot.h>

static int power_fd = -1; /* file descriptor */

void power_init(void)
{
    power_fd = open(NWZ_POWER_DEV, O_RDWR);
}

void power_close(void)
{
    close(power_fd);
}

int nwz_power_get_status(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_STATUS, &status) < 0)
        return -1;
    return status;
}

static int nwz_power_adval_to_mv(int adval, int ad_base)
{
    if(adval == -1)
        return -1;
    /* the AD base corresponds to the millivolt value if adval was 255 */
    return (adval * ad_base) / 255;
}

int nwz_power_get_vbus_adval(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_VBUS_ADVAL, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_vbus_voltage(void)
{
    return nwz_power_adval_to_mv(nwz_power_get_vbus_adval(), NWZ_POWER_AD_BASE_VBUS);
}

int nwz_power_get_vbus_limit(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_VBUS_LIMIT, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_charge_switch(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_CHARGE_SWITCH, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_charge_current(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_CHARGE_CURRENT, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_battery_gauge(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_BAT_GAUGE, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_battery_adval(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_BAT_ADVAL, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_battery_voltage(void)
{
    return nwz_power_adval_to_mv(nwz_power_get_battery_adval(), NWZ_POWER_AD_BASE_VBAT);
}

int nwz_power_get_vbat_adval(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_VBAT_ADVAL, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_vbat_voltage(void)
{
    return nwz_power_adval_to_mv(nwz_power_get_vbat_adval(), NWZ_POWER_AD_BASE_VBAT);
}

int nwz_power_get_sample_count(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_SAMPLE_COUNT, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_vsys_adval(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_VSYS_ADVAL, &status) < 0)
        return -1;
    return status;
}

int nwz_power_get_vsys_voltage(void)
{
    return nwz_power_adval_to_mv(nwz_power_get_vsys_adval(), NWZ_POWER_AD_BASE_VSYS);
}

int nwz_power_get_acc_charge_mode(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_GET_ACCESSARY_CHARGE_MODE, &status) < 0)
        return -1;
    return status;
}

int nwz_power_is_fully_charged(void)
{
    int status;
    if(ioctl(power_fd, NWZ_POWER_IS_FULLY_CHARGED, &status) < 0)
        return -1;
    return status;
}

static int write_string_to_file(const char *file, const char *msg)
{
    int fd = open(file, O_WRONLY);
    if(fd < 0)
        return -1;
    const int len = strlen(msg);
    int res = write(fd, msg, len);
    close(fd);
    return res == len ? 0 : -1;
}

int do_nwz_power_suspend(void)
{
    /* older devices use /proc/pm, while the new one use the standard sys file */
    if(write_string_to_file("/proc/pm", "S3") == 0)
        return 0;
    return write_string_to_file("/sys/power/state", "mem");
}

int nwz_power_suspend(void)
{
    int ret = do_nwz_power_suspend();
    /* the button driver tracks button status using events, but the kernel does
     * not generate an event if a key is changed during suspend, so make sure we
     * reload as much information as possible */
    nwz_button_reload_after_suspend();
    return ret;
}

int nwz_power_shutdown(void)
{
    sync(); /* man page advises to sync to avoid data loss */
    return reboot(RB_POWER_OFF);
}

int nwz_power_restart(void)
{
    sync(); /* man page advises to sync to avoid data loss */
    return reboot(RB_AUTOBOOT);
}