1 /*
2  * Copyright 2021 Matija Tudan
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17262_H_
8 #define ZEPHYR_DRIVERS_SENSOR_BATTERY_MAX17262_H_
9 
10 #include <zephyr/drivers/i2c.h>
11 
12 #define VOLTAGE_MULTIPLIER_UV	1250 / 16
13 #define CURRENT_MULTIPLIER_NA	156250
14 #define TIME_MULTIPLIER_MS	5625
15 
16 /* Register addresses */
17 enum {
18 	STATUS          = 0x00,
19 	REP_CAP         = 0x05,
20 	REP_SOC         = 0x06,
21 	INT_TEMP        = 0x08,
22 	VCELL           = 0x09,
23 	AVG_CURRENT     = 0x0b,
24 	FULL_CAP_REP    = 0x10,
25 	TTE             = 0x11,
26 	CYCLES          = 0x17,
27 	DESIGN_CAP      = 0x18,
28 	ICHG_TERM       = 0x1E,
29 	TTF             = 0x20,
30 	VEMPTY          = 0x3A,
31 	FSTAT           = 0x3D,
32 	COULOMB_COUNTER = 0x4D,
33 	SOFT_WAKEUP     = 0x60,
34 	HIBCFG          = 0xBA,
35 	MODELCFG        = 0xDB,
36 };
37 
38 /* Masks */
39 enum {
40 	FSTAT_DNR        = 0x01,
41 	STATUS_POR       = 0x02,
42 	MODELCFG_REFRESH = 0x8000,
43 };
44 
45 /* MAX17262 specific channels */
46 enum max17262_channel {
47 	MAX17262_COULOMB_COUNTER,
48 };
49 
50 struct max17262_data {
51 	/* Current cell voltage in units of 1.25/16mV */
52 	uint16_t voltage;
53 	/* Average current in units of 156.25uA */
54 	int16_t avg_current;
55 	/* Desired charging current in mA */
56 	uint16_t ichg_term;
57 	/* Remaining capacity as a %age */
58 	uint16_t state_of_charge;
59 	/* Internal temperature in units of 1/256 degrees C */
60 	int16_t internal_temp;
61 	/* Full charge capacity in mAh */
62 	uint16_t full_cap;
63 	/* Remaining capacity in mAh */
64 	uint16_t remaining_cap;
65 	/* Time to empty in seconds */
66 	uint16_t time_to_empty;
67 	/* Time to full in seconds */
68 	uint16_t time_to_full;
69 	/* Cycle count in 1/100ths (number of charge/discharge cycles) */
70 	uint16_t cycle_count;
71 	/* Battery capacity in mAh */
72 	uint16_t design_cap;
73 	/* Spent capacity in mAh */
74 	uint16_t coulomb_counter;
75 };
76 
77 struct max17262_config {
78 	struct i2c_dt_spec i2c;
79 	/* Value of Rsense resistor in milliohms (typically 5 or 10) */
80 	uint16_t rsense_mohms;
81 	/* Design voltage of cell in mV */
82 	uint16_t design_voltage;
83 	/* Desired voltage of cell in mV */
84 	uint16_t desired_voltage;
85 	/* Desired charging current in mA */
86 	uint16_t desired_charging_current;
87 	/* Battery capacity in mAh */
88 	uint16_t design_cap;
89 	/* Empty voltage detection in mV */
90 	uint16_t empty_voltage;
91 	/* Recovery voltage detection in mV */
92 	uint16_t recovery_voltage;
93 	/* Defined charge voltage value in mV */
94 	uint16_t charge_voltage;
95 };
96 
97 #endif
98