diff options
Diffstat (limited to 'firmware/drivers/axp173.c')
-rw-r--r-- | firmware/drivers/axp173.c | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/firmware/drivers/axp173.c b/firmware/drivers/axp173.c new file mode 100644 index 0000000000..22417650fc --- /dev/null +++ b/firmware/drivers/axp173.c | |||
@@ -0,0 +1,419 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
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 | |||
22 | #include "axp173.h" | ||
23 | #include "power.h" | ||
24 | #include "i2c-async.h" | ||
25 | |||
26 | /* Headers for the debug menu */ | ||
27 | #ifndef BOOTLOADER | ||
28 | # include "action.h" | ||
29 | # include "list.h" | ||
30 | # include <stdio.h> | ||
31 | #endif | ||
32 | |||
33 | static const struct axp173_adc_info { | ||
34 | uint8_t reg; | ||
35 | uint8_t en_reg; | ||
36 | uint8_t en_bit; | ||
37 | } axp173_adc_info[NUM_ADC_CHANNELS] = { | ||
38 | {0x56, 0x82, 5}, /* ACIN_VOLTAGE */ | ||
39 | {0x58, 0x82, 4}, /* ACIN_CURRENT */ | ||
40 | {0x5a, 0x82, 3}, /* VBUS_VOLTAGE */ | ||
41 | {0x5c, 0x82, 2}, /* VBUS_CURRENT */ | ||
42 | {0x5e, 0x83, 7}, /* INTERNAL_TEMP */ | ||
43 | {0x62, 0x82, 1}, /* TS_INPUT */ | ||
44 | {0x78, 0x82, 7}, /* BATTERY_VOLTAGE */ | ||
45 | {0x7a, 0x82, 6}, /* CHARGE_CURRENT */ | ||
46 | {0x7c, 0x82, 6}, /* DISCHARGE_CURRENT */ | ||
47 | {0x7e, 0x82, 1}, /* APS_VOLTAGE */ | ||
48 | {0x70, 0xff, 0}, /* BATTERY_POWER */ | ||
49 | }; | ||
50 | |||
51 | static struct axp173 { | ||
52 | int adc_enable; | ||
53 | } axp173; | ||
54 | |||
55 | static void axp173_init_enabled_adcs(void) | ||
56 | { | ||
57 | axp173.adc_enable = 0; | ||
58 | |||
59 | /* Read enabled ADCs from the hardware */ | ||
60 | uint8_t regs[2]; | ||
61 | int rc = i2c_reg_read(AXP173_BUS, AXP173_ADDR, 0x82, 2, ®s[0]); | ||
62 | if(rc != I2C_STATUS_OK) | ||
63 | return; | ||
64 | |||
65 | /* Parse registers to set ADC enable bits */ | ||
66 | const struct axp173_adc_info* info = axp173_adc_info; | ||
67 | for(int i = 0; i < NUM_ADC_CHANNELS; ++i) { | ||
68 | if(info[i].en_reg == 0xff) | ||
69 | continue; | ||
70 | |||
71 | if(regs[info[i].en_reg - 0x82] & info[i].en_bit) | ||
72 | axp173.adc_enable |= 1 << i; | ||
73 | } | ||
74 | |||
75 | /* Handle battery power ADC */ | ||
76 | if((axp173.adc_enable & (1 << ADC_BATTERY_VOLTAGE)) && | ||
77 | (axp173.adc_enable & (1 << ADC_DISCHARGE_CURRENT))) { | ||
78 | axp173.adc_enable |= (1 << ADC_BATTERY_POWER); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | void axp173_init(void) | ||
83 | { | ||
84 | axp173_init_enabled_adcs(); | ||
85 | |||
86 | /* We need discharge current ADC to reliably poll for a full battery */ | ||
87 | int bits = axp173.adc_enable; | ||
88 | bits |= (1 << ADC_DISCHARGE_CURRENT); | ||
89 | axp173_adc_set_enabled(bits); | ||
90 | } | ||
91 | |||
92 | /* TODO: this can STILL indicate some false positives! */ | ||
93 | int axp173_battery_status(void) | ||
94 | { | ||
95 | int r = i2c_reg_read1(AXP173_BUS, AXP173_ADDR, 0x00); | ||
96 | if(r >= 0) { | ||
97 | /* Charging bit indicates we're currently charging */ | ||
98 | if((r & 0x04) != 0) | ||
99 | return AXP173_BATT_CHARGING; | ||
100 | |||
101 | /* Not plugged in means we're discharging */ | ||
102 | if((r & 0xf0) == 0) | ||
103 | return AXP173_BATT_DISCHARGING; | ||
104 | } else { | ||
105 | /* Report discharging if we can't find out power status */ | ||
106 | return AXP173_BATT_DISCHARGING; | ||
107 | } | ||
108 | |||
109 | /* If the battery is full and not in use, the charging bit will be 0, | ||
110 | * there will be an external power source, AND the discharge current | ||
111 | * will be zero. Seems to rule out all false positives. */ | ||
112 | int d = axp173_adc_read_raw(ADC_DISCHARGE_CURRENT); | ||
113 | if(d == 0) | ||
114 | return AXP173_BATT_FULL; | ||
115 | |||
116 | return AXP173_BATT_DISCHARGING; | ||
117 | } | ||
118 | |||
119 | int axp173_input_status(void) | ||
120 | { | ||
121 | #ifdef HAVE_BATTERY_SWITCH | ||
122 | int input_status = 0; | ||
123 | #else | ||
124 | int input_status = AXP173_INPUT_BATTERY; | ||
125 | #endif | ||
126 | |||
127 | int r = i2c_reg_read1(AXP173_BUS, AXP173_ADDR, 0x00); | ||
128 | if(r < 0) | ||
129 | return input_status; | ||
130 | |||
131 | /* Check for AC input */ | ||
132 | if(r & 0x80) | ||
133 | input_status |= AXP173_INPUT_AC; | ||
134 | |||
135 | /* Only report USB if ACIN and VBUS are not shorted */ | ||
136 | if((r & 0x20) != 0 && (r & 0x02) == 0) | ||
137 | input_status |= AXP173_INPUT_USB; | ||
138 | |||
139 | #ifdef HAVE_BATTERY_SWITCH | ||
140 | /* Check for battery presence if target defines it as removable */ | ||
141 | r = i2c_reg_read1(AXP173_BUS, AXP173_ADDR, 0x01); | ||
142 | if(r >= 0 && (r & 0x20) != 0) | ||
143 | input_status |= AXP173_INPUT_BATTERY; | ||
144 | #endif | ||
145 | |||
146 | return input_status; | ||
147 | } | ||
148 | |||
149 | int axp173_adc_read(int adc) | ||
150 | { | ||
151 | int value = axp173_adc_read_raw(adc); | ||
152 | if(value == INT_MIN) | ||
153 | return INT_MIN; | ||
154 | |||
155 | return axp173_adc_conv_raw(adc, value); | ||
156 | } | ||
157 | |||
158 | int axp173_adc_read_raw(int adc) | ||
159 | { | ||
160 | /* Don't give a reading if the ADC is not enabled */ | ||
161 | if((axp173.adc_enable & (1 << adc)) == 0) | ||
162 | return INT_MIN; | ||
163 | |||
164 | /* Read the ADC */ | ||
165 | uint8_t buf[3]; | ||
166 | int count = (adc == ADC_BATTERY_POWER) ? 3 : 2; | ||
167 | uint8_t reg = axp173_adc_info[adc].reg; | ||
168 | int rc = i2c_reg_read(AXP173_BUS, AXP173_ADDR, reg, count, &buf[0]); | ||
169 | if(rc != I2C_STATUS_OK) | ||
170 | return INT_MIN; | ||
171 | |||
172 | /* Parse the value */ | ||
173 | if(adc == ADC_BATTERY_POWER) | ||
174 | return (buf[0] << 16) | (buf[1] << 8) | buf[2]; | ||
175 | else if(adc == ADC_CHARGE_CURRENT || adc == ADC_DISCHARGE_CURRENT) | ||
176 | return (buf[0] << 5) | (buf[1] & 0x1f); | ||
177 | else | ||
178 | return (buf[0] << 4) | (buf[1] & 0xf); | ||
179 | } | ||
180 | |||
181 | int axp173_adc_conv_raw(int adc, int value) | ||
182 | { | ||
183 | switch(adc) { | ||
184 | case ADC_ACIN_VOLTAGE: | ||
185 | case ADC_VBUS_VOLTAGE: | ||
186 | /* 0 mV ... 6.9615 mV, step 1.7 mV */ | ||
187 | return value * 17 / 10; | ||
188 | case ADC_ACIN_CURRENT: | ||
189 | /* 0 mA ... 2.5594 A, step 0.625 mA */ | ||
190 | return value * 5 / 8; | ||
191 | case ADC_VBUS_CURRENT: | ||
192 | /* 0 mA ... 1.5356 A, step 0.375 mA */ | ||
193 | return value * 3 / 8; | ||
194 | case ADC_INTERNAL_TEMP: | ||
195 | /* -144.7 C ... 264.8 C, step 0.1 C */ | ||
196 | return value - 1447; | ||
197 | case ADC_TS_INPUT: | ||
198 | /* 0 mV ... 3.276 V, step 0.8 mV */ | ||
199 | return value * 4 / 5; | ||
200 | case ADC_BATTERY_VOLTAGE: | ||
201 | /* 0 mV ... 4.5045 V, step 1.1 mV */ | ||
202 | return value * 11 / 10; | ||
203 | case ADC_CHARGE_CURRENT: | ||
204 | case ADC_DISCHARGE_CURRENT: | ||
205 | /* 0 mA to 4.095 A, step 0.5 mA */ | ||
206 | return value / 2; | ||
207 | case ADC_APS_VOLTAGE: | ||
208 | /* 0 mV to 5.733 V, step 1.4 mV */ | ||
209 | return value * 7 / 5; | ||
210 | case ADC_BATTERY_POWER: | ||
211 | /* 0 uW to 23.6404 W, step 0.55 uW */ | ||
212 | return value * 11 / 20; | ||
213 | default: | ||
214 | /* Shouldn't happen */ | ||
215 | return INT_MIN; | ||
216 | } | ||
217 | } | ||
218 | |||
219 | int axp173_adc_get_enabled(void) | ||
220 | { | ||
221 | return axp173.adc_enable; | ||
222 | } | ||
223 | |||
224 | void axp173_adc_set_enabled(int adc_bits) | ||
225 | { | ||
226 | /* Ignore no-op */ | ||
227 | if(adc_bits == axp173.adc_enable) | ||
228 | return; | ||
229 | |||
230 | /* Compute the new register values */ | ||
231 | const struct axp173_adc_info* info = axp173_adc_info; | ||
232 | uint8_t regs[2] = {0, 0}; | ||
233 | for(int i = 0; i < NUM_ADC_CHANNELS; ++i) { | ||
234 | if(info[i].en_reg == 0xff) | ||
235 | continue; | ||
236 | |||
237 | if(adc_bits & (1 << i)) | ||
238 | regs[info[i].en_reg - 0x82] |= 1 << info[i].en_bit; | ||
239 | } | ||
240 | |||
241 | /* These ADCs share an enable bit */ | ||
242 | if(adc_bits & ((1 << ADC_CHARGE_CURRENT)|(1 << ADC_DISCHARGE_CURRENT))) { | ||
243 | adc_bits |= (1 << ADC_CHARGE_CURRENT); | ||
244 | adc_bits |= (1 << ADC_DISCHARGE_CURRENT); | ||
245 | } | ||
246 | |||
247 | /* Enable required bits for battery power ADC */ | ||
248 | if(adc_bits & (1 << ADC_BATTERY_POWER)) { | ||
249 | regs[0] |= 1 << info[ADC_DISCHARGE_CURRENT].en_bit; | ||
250 | regs[0] |= 1 << info[ADC_BATTERY_VOLTAGE].en_bit; | ||
251 | } | ||
252 | |||
253 | /* Update the configuration */ | ||
254 | i2c_reg_write(AXP173_BUS, AXP173_ADDR, 0x82, 2, ®s[0]); | ||
255 | axp173.adc_enable = adc_bits; | ||
256 | } | ||
257 | |||
258 | int axp173_adc_get_rate(void) | ||
259 | { | ||
260 | int r = i2c_reg_read1(AXP173_BUS, AXP173_ADDR, 0x84); | ||
261 | if(r < 0) | ||
262 | return AXP173_ADC_RATE_100HZ; /* an arbitrary value */ | ||
263 | |||
264 | return (r >> 6) & 3; | ||
265 | } | ||
266 | |||
267 | void axp173_adc_set_rate(int rate) | ||
268 | { | ||
269 | i2c_reg_modify1(AXP173_BUS, AXP173_ADDR, 0x84, | ||
270 | 0xc0, (rate & 3) << 6, NULL); | ||
271 | } | ||
272 | |||
273 | static uint32_t axp173_cc_parse(const uint8_t* buf) | ||
274 | { | ||
275 | return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; | ||
276 | } | ||
277 | |||
278 | void axp173_cc_read(uint32_t* charge, uint32_t* discharge) | ||
279 | { | ||
280 | uint8_t buf[8]; | ||
281 | int rc = i2c_reg_read(AXP173_BUS, AXP173_ADDR, 0xb0, 8, &buf[0]); | ||
282 | if(rc != I2C_STATUS_OK) { | ||
283 | if(charge) | ||
284 | *charge = 0; | ||
285 | if(discharge) | ||
286 | *discharge = 0; | ||
287 | return; | ||
288 | } | ||
289 | |||
290 | if(charge) | ||
291 | *charge = axp173_cc_parse(&buf[0]); | ||
292 | if(discharge) | ||
293 | *discharge = axp173_cc_parse(&buf[4]); | ||
294 | } | ||
295 | |||
296 | void axp173_cc_clear(void) | ||
297 | { | ||
298 | i2c_reg_setbit1(AXP173_BUS, AXP173_ADDR, 0xb8, 5, 1, NULL); | ||
299 | } | ||
300 | |||
301 | void axp173_cc_enable(bool en) | ||
302 | { | ||
303 | i2c_reg_setbit1(AXP173_BUS, AXP173_ADDR, 0xb8, 7, en ? 1 : 0, NULL); | ||
304 | } | ||
305 | |||
306 | #ifndef BOOTLOADER | ||
307 | #define AXP173_DEBUG_BATTERY_STATUS 0 | ||
308 | #define AXP173_DEBUG_INPUT_STATUS 1 | ||
309 | #define AXP173_DEBUG_ADC_RATE 2 | ||
310 | #define AXP173_DEBUG_FIRST_ADC 3 | ||
311 | #define AXP173_DEBUG_ENTRIES (AXP173_DEBUG_FIRST_ADC + NUM_ADC_CHANNELS) | ||
312 | |||
313 | static int axp173_debug_menu_cb(int action, struct gui_synclist* lists) | ||
314 | { | ||
315 | (void)lists; | ||
316 | |||
317 | if(action == ACTION_NONE) | ||
318 | action = ACTION_REDRAW; | ||
319 | |||
320 | return action; | ||
321 | } | ||
322 | |||
323 | static const char* axp173_debug_menu_get_name(int item, void* data, | ||
324 | char* buf, size_t buflen) | ||
325 | { | ||
326 | (void)data; | ||
327 | |||
328 | static const char* const adc_names[] = { | ||
329 | "V_acin", "I_acin", "V_vbus", "I_vbus", "T_int", | ||
330 | "V_ts", "V_batt", "I_chrg", "I_dchg", "V_aps", "P_batt" | ||
331 | }; | ||
332 | |||
333 | static const char* const adc_units[] = { | ||
334 | "mV", "mA", "mV", "mA", "C", "mV", "mV", "mA", "mA", "mV", "uW", | ||
335 | }; | ||
336 | |||
337 | int adc = item - AXP173_DEBUG_FIRST_ADC; | ||
338 | if(item >= AXP173_DEBUG_FIRST_ADC && adc < NUM_ADC_CHANNELS) { | ||
339 | int raw_value = axp173_adc_read_raw(adc); | ||
340 | if(raw_value == INT_MIN) { | ||
341 | snprintf(buf, buflen, "%s: [Disabled]", adc_names[adc]); | ||
342 | return buf; | ||
343 | } | ||
344 | |||
345 | int value = axp173_adc_conv_raw(adc, raw_value); | ||
346 | if(adc == ADC_INTERNAL_TEMP) { | ||
347 | snprintf(buf, buflen, "%s: %d.%d %s", adc_names[adc], | ||
348 | value/10, value%10, adc_units[adc]); | ||
349 | } else { | ||
350 | snprintf(buf, buflen, "%s: %d %s", adc_names[adc], | ||
351 | value, adc_units[adc]); | ||
352 | } | ||
353 | |||
354 | return buf; | ||
355 | } | ||
356 | |||
357 | switch(item) { | ||
358 | case AXP173_DEBUG_BATTERY_STATUS: { | ||
359 | switch(axp173_battery_status()) { | ||
360 | case AXP173_BATT_FULL: | ||
361 | return "Battery: Full"; | ||
362 | case AXP173_BATT_CHARGING: | ||
363 | return "Battery: Charging"; | ||
364 | case AXP173_BATT_DISCHARGING: | ||
365 | return "Battery: Discharging"; | ||
366 | default: | ||
367 | return "Battery: Unknown"; | ||
368 | } | ||
369 | } break; | ||
370 | |||
371 | case AXP173_DEBUG_INPUT_STATUS: { | ||
372 | int s = axp173_input_status(); | ||
373 | const char* ac = (s & AXP173_INPUT_AC) ? " AC" : ""; | ||
374 | const char* usb = (s & AXP173_INPUT_USB) ? " USB" : ""; | ||
375 | const char* batt = (s & AXP173_INPUT_BATTERY) ? " Battery" : ""; | ||
376 | snprintf(buf, buflen, "Inputs:%s%s%s", ac, usb, batt); | ||
377 | return buf; | ||
378 | } break; | ||
379 | |||
380 | case AXP173_DEBUG_ADC_RATE: { | ||
381 | int rate = 25 << axp173_adc_get_rate(); | ||
382 | snprintf(buf, buflen, "ADC sample rate: %d Hz", rate); | ||
383 | return buf; | ||
384 | } break; | ||
385 | |||
386 | default: | ||
387 | return "---"; | ||
388 | } | ||
389 | } | ||
390 | |||
391 | bool axp173_debug_menu(void) | ||
392 | { | ||
393 | struct simplelist_info info; | ||
394 | simplelist_info_init(&info, "AXP173 debug", AXP173_DEBUG_ENTRIES, NULL); | ||
395 | info.action_callback = axp173_debug_menu_cb; | ||
396 | info.get_name = axp173_debug_menu_get_name; | ||
397 | return simplelist_show_list(&info); | ||
398 | } | ||
399 | #endif /* !BOOTLOADER */ | ||
400 | |||
401 | /* This is basically the only valid implementation, so define it here */ | ||
402 | unsigned int power_input_status(void) | ||
403 | { | ||
404 | unsigned int state = 0; | ||
405 | int input_status = axp173_input_status(); | ||
406 | |||
407 | if(input_status & AXP173_INPUT_AC) | ||
408 | state |= POWER_INPUT_MAIN_CHARGER; | ||
409 | |||
410 | if(input_status & AXP173_INPUT_USB) | ||
411 | state |= POWER_INPUT_USB_CHARGER; | ||
412 | |||
413 | #ifdef HAVE_BATTERY_SWITCH | ||
414 | if(input_status & AXP173_INPUT_BATTERY) | ||
415 | state |= POWER_INPUT_BATTERY; | ||
416 | #endif | ||
417 | |||
418 | return state; | ||
419 | } | ||