| /* SPDX-License-Identifier: GPL-2.0 */ |
| /* |
| * Copyright 2019 Google, LLC |
| * |
| * 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 program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| */ |
| |
| |
| #ifndef MAX1720X_H_ |
| #define MAX1720X_H_ |
| |
| #include "max1720x_battery.h" |
| |
| #define MAX1720X_N_OF_HISTORY_PAGES 203 |
| |
| |
| enum max1720x_register { |
| /* ModelGauge m5 Register */ |
| MAX1720X_STATUS = 0x00, |
| MAX1720X_VALRTTH = 0x01, |
| MAX1720X_TALRTTH = 0x02, |
| MAX1720X_SALRTTH = 0x03, |
| MAX1720X_ATRATE = 0x04, |
| MAX1720X_REPCAP = 0x05, |
| MAX1720X_REPSOC = 0x06, |
| MAX1720X_AGE = 0x07, |
| MAX1720X_TEMP = 0x08, |
| MAX1720X_VCELL = 0x09, |
| MAX1720X_CURRENT = 0x0A, |
| MAX1720X_AVGCURRENT = 0x0B, |
| MAX1720X_QRESIDUAL = 0x0C, |
| MAX1720X_MIXSOC = 0x0D, |
| MAX1720X_AVSOC = 0x0E, |
| MAX1720X_MIXCAP = 0x0F, |
| |
| MAX1720X_FULLCAP = 0x10, |
| MAX1720X_TTE = 0x11, |
| MAX1720X_QRTABLE00 = 0x12, |
| MAX1720X_FULLSOCTHR = 0x13, |
| MAX1720X_RCELL = 0x14, |
| MAX1720X_RFAST = 0x15, |
| MAX1720X_AVGTA = 0x16, |
| MAX1720X_CYCLES = 0x17, |
| MAX1720X_DESIGNCAP = 0x18, |
| MAX1720X_AVGVCELL = 0x19, |
| MAX1720X_MAXMINTEMP = 0x1A, |
| MAX1720X_MAXMINVOLT = 0x1B, |
| MAX1720X_MAXMINCURR = 0x1C, |
| MAX1720X_CONFIG = 0x1D, |
| MAX1720X_ICHGTERM = 0x1E, |
| MAX1720X_AVCAP = 0x1F, |
| |
| MAX1720X_TTF = 0x20, |
| MAX1720X_DEVNAME = 0x21, |
| MAX1720X_QRTABLE10 = 0x22, |
| MAX1720X_FULLCAPNOM = 0x23, |
| MAX1720X_AIN0 = 0x27, |
| MAX1720X_LEARNCFG = 0x28, |
| MAX1720X_FILTERCFG = 0x29, |
| MAX1720X_RELAXCFG = 0x2A, |
| MAX1720X_MISCCFG = 0x2B, |
| MAX1720X_TGAIN = 0x2C, |
| Max1720x_TOff = 0x2D, |
| MAX1720X_CGAIN = 0x2E, |
| MAX1720X_COFF = 0x2F, |
| |
| MAX1720X_QRTABLE20 = 0x32, |
| MAX1720X_FULLCAPREP = 0x35, |
| MAX1720X_IAVGEMPTY = 0x36, |
| MAX1720X_RCOMP0 = 0x38, |
| MAX1720X_TEMPCO = 0x39, |
| MAX1720X_VEMPTY = 0x3A, |
| MAX1720X_FSTAT = 0x3D, |
| MAX1720X_TIMER = 0x3E, |
| MAX1720X_SHDNTIMER = 0x3F, |
| |
| MAX1720X_QRTABLE30 = 0x42, |
| MAX1720X_DQACC = 0x45, |
| MAX1720X_DPACC = 0x46, |
| MAX1720X_VFREMCAP = 0x4A, |
| MAX1720X_QH0 = 0x4C, |
| MAX1720X_QH = 0x4D, |
| |
| MAX1720X_STATUS2 = 0xB0, |
| MAX1720X_IALRTTH = 0xB4, |
| MAX1720X_VSHDNCFG = 0xB8, |
| MAX1720X_AGEFORECAST = 0xB9, |
| MAX1720X_HIBCFG = 0xBA, |
| MAX1720X_CONFIG2 = 0xBB, |
| MAX1720X_VRIPPLE = 0xBC, |
| MAX1720X_PACKCFG = 0xBD, |
| MAX1720X_TIMERH = 0xBE, |
| |
| MAX1720X_AVGCELL4 = 0xD1, |
| MAX1720X_AVGCELL3 = 0xD2, |
| MAX1720X_AVGCELL2 = 0xD3, |
| MAX1720X_AVGCELL1 = 0xD4, |
| MAX1720X_CELL4 = 0xD5, |
| MAX1720X_CELL3 = 0xD6, |
| MAX1720X_CELL2 = 0xD7, |
| MAX1720X_CELL1 = 0xD8, |
| MAX1720X_CELLX = 0xD9, |
| MAX1720X_BATT = 0xDA, |
| MAX1720X_ATQRESIDUAL = 0xDC, |
| MAX1720X_ATTTE = 0xDD, |
| MAX1720X_ATAVSOC = 0xDE, |
| MAX1720X_ATAVCAP = 0xDF, |
| |
| /* Individual Registers */ |
| MAX1720X_COMMAND = 0x60, |
| MAX1720X_COMMSTAT = 0x61, |
| MAX1720X_LOCK = 0x7F, |
| MAX1720X_ODSCTH = 0xF2, |
| MAX1720X_ODSCCFG = 0xF3, |
| MAX1720X_VFOCV = 0xFB, |
| MAX1720X_ALARM = 0xFD, |
| MAX1720X_VFSOC = 0xFF, |
| }; |
| |
| enum max1720x_status_bits { |
| MAX1720X_STATUS_POR = BIT(1), |
| MAX1720X_STATUS_IMN = BIT(2), |
| MAX1720X_STATUS_BST = BIT(3), |
| MAX1720X_STATUS_IMX = BIT(6), |
| MAX1720X_STATUS_DSOCI = BIT(7), |
| MAX1720X_STATUS_VMN = BIT(8), |
| MAX1720X_STATUS_TMN = BIT(9), |
| MAX1720X_STATUS_SMN = BIT(10), |
| MAX1720X_STATUS_BI = BIT(11), |
| MAX1720X_STATUS_VMX = BIT(12), |
| MAX1720X_STATUS_TMX = BIT(13), |
| MAX1720X_STATUS_SMX = BIT(14), |
| MAX1720X_STATUS_BR = BIT(15), |
| }; |
| |
| enum max1720x_commstat_bits { |
| MAX1720X_COMMSTAT_NVBUSY = BIT(1), |
| MAX1720X_COMMSTAT_NVERROR = BIT(2), |
| }; |
| |
| enum max1720x_config_bits { |
| MAX1720X_CONFIG_BER = BIT(0), |
| MAX1720X_CONFIG_BEI = BIT(1), |
| MAX1720X_CONFIG_AEN = BIT(2), |
| MAX1720X_CONFIG_FTHRM = BIT(3), |
| MAX1720X_CONFIG_ETHRM = BIT(4), |
| MAX1720X_CONFIG_COMMSH = BIT(6), |
| MAX1720X_CONFIG_SHDN = BIT(7), |
| MAX1720X_CONFIG_TEX = BIT(8), |
| MAX1720X_CONFIG_TEN = BIT(9), |
| MAX1720X_CONFIG_AINSH = BIT(10), |
| MAX1720X_CONFIG_ALRTP = BIT(11), |
| MAX1720X_CONFIG_VS = BIT(12), |
| MAX1720X_CONFIG_TS = BIT(13), |
| MAX1720X_CONFIG_SS = BIT(14), |
| }; |
| |
| enum max1720x_nnvcfg0_bits { |
| MAX1720X_NNVCFG0_ENSBS = BIT(0), |
| MAX1720X_NNVCFG0_ENHCFG = BIT(1), |
| MAX1720X_NNVCFG0_ENAF = BIT(2), |
| MAX1720X_NNVCFG0_ENMC = BIT(3), |
| MAX1720X_NNVCFG0_ENDC = BIT(4), |
| MAX1720X_NNVCFG0_ENVE = BIT(5), |
| MAX1720X_NNVCFG0_ENCG = BIT(6), |
| MAX1720X_NNVCFG0_ENICT = BIT(7), |
| MAX1720X_NNVCFG0_ENLCFG = BIT(8), |
| MAX1720X_NNVCFG0_ENRCFG = BIT(9), |
| MAX1720X_NNVCFG0_ENFCFG = BIT(10), |
| MAX1720X_NNVCFG0_ENCFG = BIT(11), |
| MAX1720X_NNVCFG0_ENX = BIT(14), |
| MAX1720X_NNVCFG0_ENOCV = BIT(15), |
| }; |
| |
| enum max1720x_command_bits { |
| MAX1720X_COMMAND_FUEL_GAUGE_RESET = 0x0001, |
| MAX1720X_COMMAND_HARDWARE_RESET = 0x000F, |
| MAX1720X_COMMAND_QUERY_REMAINING_UPDATES = 0xE2FA, |
| MAX1720X_COMMAND_COPY_NV_BLOCK = 0xE904, |
| MAX1720X_COMMAND_HISTORY_RECALL_WRITE_0 = 0xE2FB, |
| MAX1720X_COMMAND_HISTORY_RECALL_WRITE_1 = 0xE2FC, |
| MAX1720X_COMMAND_HISTORY_RECALL_VALID_0 = 0xE2FC, |
| MAX1720X_COMMAND_HISTORY_RECALL_VALID_1 = 0xE2FD, |
| MAX1720X_COMMAND_HISTORY_RECALL_VALID_2 = 0xE2FE, |
| MAX1720X_READ_HISTORY_CMD_BASE = 0xE226, |
| }; |
| |
| |
| /** Nonvolatile Register Memory Map */ |
| enum max1720x_nvram { |
| MAX1720X_NVRAM_START = 0x80, |
| MAX1720X_NUSER18C = 0x8C, /* reserved for QH capacity */ |
| MAX1720X_NUSER18D = 0x8D, /* reserved for QH QH */ |
| MAX1720X_NODSCTH = 0x8E, /* CCLC */ |
| MAX1720X_NODSCCFG = 0x8F, /* CCLC */ |
| MAX1720X_NLEARNCFG = 0x9F, /* not referred here */ |
| MAX1720X_NMISCCFG = 0xB2, /* CCLC */ |
| MAX1720X_NHIBCFG = 0xB4, /* CCLC */ |
| MAX1720X_NCONVGCFG = 0xB7, /* convergence configuration */ |
| MAX1720X_NNVCFG0 = 0xB8, /* 'NCG0' with NCG1 */ |
| MAX1720X_NUSER1C4 = 0xC4, /* CCLC */ |
| MAX1720X_NUSER1C5 = 0xC5, /* CCLC */ |
| MAX1720X_NTTFCFG = 0xC7, /* Average resistance */ |
| MAX1720X_NCGAIN = 0xC8, /* .... */ |
| MAX1720X_NMANFCTRNAME0 = 0xCC, /* SNUM */ |
| MAX1720X_NMANFCTRNAME1 = 0xCD, /* CCLC */ |
| MAX1720X_NMANFCTRNAME2 = 0xCE, /* CCLC */ |
| MAX1720X_NRSENSE = 0xCF, /* value of sense resistor */ |
| MAX1720X_NUSER1D0 = 0xD0, /* SNUM */ |
| MAX1720X_NUSER1D1 = 0xD1, /* SNUM */ |
| MAX1720X_NAGEFCCFG = 0xD2, |
| MAX1720X_NUSER1D4 = 0xD4, /* URST */ |
| MAX1720X_NMANFCTRDATE = 0xD6, /* SNUM */ |
| MAX1720X_NFIRSTUSED = 0xD7, /* CCLC */ |
| MAX1720X_NSERIALNUMBER0 = 0xD8, /* SNUM */ |
| MAX1720X_NSERIALNUMBER1 = 0xD9, /* SNUM */ |
| MAX1720X_NSERIALNUMBER2 = 0xDA, /* SNUM */ |
| MAX1720X_NDEVICENAME0 = 0xDB, /* SNUM */ |
| MAX1720X_NDEVICENAME1 = 0xDC, /* SNUM */ |
| MAX1720X_NDEVICENAME2 = 0xDD, /* SNUM */ |
| MAX1720X_NDEVICENAME3 = 0xDE, /* SNUM */ |
| MAX1720X_NDEVICENAME4 = 0xDF, /* CCLC */ |
| MAX1720X_NVRAM_END = MAX1720X_NDEVICENAME4, |
| |
| MAX1720X_HISTORY_START = 0xE0, |
| MAX1720X_NVRAM_HISTORY_WRITE_STATUS_START = 0xE1, |
| MAX1720X_NVRAM_HISTORY_VALID_STATUS_END = 0xE4, |
| MAX1720X_NVRAM_HISTORY_WRITE_STATUS_END = 0xEA, |
| MAX1720X_NVRAM_HISTORY_VALID_STATUS_START = 0xEB, |
| MAX1720X_NVRAM_REMAINING_UPDATES = 0xED, |
| MAX1720X_NVRAM_HISTORY_END = 0xEF, |
| }; |
| |
| #define MAX1720X_HISTORY_PAGE_SIZE \ |
| (MAX1720X_NVRAM_HISTORY_END - MAX1720X_HISTORY_START + 1) |
| |
| #define MAX1720X_N_OF_HISTORY_FLAGS_REG \ |
| (MAX1720X_NVRAM_HISTORY_END - \ |
| MAX1720X_NVRAM_HISTORY_WRITE_STATUS_START + 1 + \ |
| MAX1720X_NVRAM_HISTORY_WRITE_STATUS_END - \ |
| MAX1720X_HISTORY_START + 1) |
| |
| #define MAX1720X_N_OF_QRTABLES 4 |
| |
| /** ------------------------------------------------------------------------ */ |
| |
| static bool max1720x_is_reg(struct device *dev, unsigned int reg) |
| { |
| switch (reg) { |
| case MAX1720X_COMMAND: |
| case MAX1720X_COMMSTAT: |
| case MAX1720X_LOCK: |
| case MAX1720X_ODSCTH: |
| case MAX1720X_ODSCCFG: |
| case MAX1720X_VFOCV: |
| case MAX1720X_VFSOC: |
| case MAX1720X_ALARM: |
| case 0x00 ... 0x4F: |
| case 0xB0 ... 0xDF: |
| return true; |
| } |
| |
| return false; |
| } |
| |
| static const struct regmap_config max1720x_regmap_cfg = { |
| .reg_bits = 8, |
| .val_bits = 16, |
| .val_format_endian = REGMAP_ENDIAN_NATIVE, |
| .max_register = MAX1720X_VFSOC, |
| .readable_reg = max1720x_is_reg, |
| .volatile_reg = max1720x_is_reg, |
| }; |
| |
| static bool max1720x_is_nvram_reg(struct device *dev, unsigned int reg) |
| { |
| return (reg >= MAX1720X_NVRAM_START && |
| reg <= MAX1720X_NVRAM_HISTORY_END); |
| } |
| |
| const struct regmap_config max1720x_regmap_nvram_cfg = { |
| .reg_bits = 8, |
| .val_bits = 16, |
| .val_format_endian = REGMAP_ENDIAN_NATIVE, |
| .max_register = MAX1720X_NVRAM_HISTORY_END, |
| .readable_reg = max1720x_is_nvram_reg, |
| .volatile_reg = max1720x_is_nvram_reg, |
| }; |
| |
| /** ------------------------------------------------------------------------ */ |
| |
| static const struct max17x0x_reg max1720x[] = { |
| [MAX17X0X_TAG_avgc] = { ATOM_INIT_REG16(MAX1720X_AVGCURRENT)}, |
| [MAX17X0X_TAG_cnfg] = { ATOM_INIT_REG16(MAX1720X_CONFIG)}, |
| [MAX17X0X_TAG_mmdv] = { ATOM_INIT_REG16(MAX1720X_MAXMINVOLT)}, |
| [MAX17X0X_TAG_vcel] = { ATOM_INIT_REG16(MAX1720X_VCELL)}, |
| [MAX17X0X_TAG_temp] = { ATOM_INIT_REG16(MAX1720X_TEMP)}, |
| [MAX17X0X_TAG_curr] = { ATOM_INIT_REG16(MAX1720X_CURRENT)}, |
| [MAX17X0X_TAG_mcap] = { ATOM_INIT_REG16(MAX1720X_MIXCAP)}, |
| [MAX17X0X_TAG_avgr] = { ATOM_INIT_REG16(MAX1720X_NTTFCFG)}, |
| [MAX17X0X_TAG_vfsoc] = { ATOM_INIT_REG16(MAX1720X_VFSOC)}, |
| [MAX17X0X_TAG_vfocv] = { ATOM_INIT_REG16(MAX1720X_VFOCV)}, |
| |
| [MAX17X0X_TAG_BCNT] = { ATOM_INIT_MAP(0x8e, 0x8f, 0xb2, 0xb4, 0xc4, |
| 0xc5, 0xcd, 0xce, 0xd7, 0xdf) }, |
| [MAX17X0X_TAG_SNUM] = { ATOM_INIT_MAP(0xcc, 0xd8, 0xd9, 0xda, 0xd6, |
| 0xdb, 0xdc, 0xdd, 0xde, 0xd1, |
| 0xd0) }, |
| |
| [MAX17X0X_TAG_HSTY] = { ATOM_INIT_SET(0xe0, 0xe1, 0xe4, 0xea, 0xeb, |
| 0xed, 0xef) }, |
| [MAX17X0X_TAG_BCEA] = { ATOM_INIT_SET(MAX1720X_NUSER1D4, |
| MAX1720X_NAGEFCCFG, |
| MAX1720X_NMISCCFG) }, |
| [MAX17X0X_TAG_rset] = { ATOM_INIT_SET16(MAX1720X_CONFIG2, |
| MAX1720X_COMMAND_FUEL_GAUGE_RESET, |
| 700)}, |
| [MAX17X0X_TAG_BRES] = { ATOM_INIT_SET(MAX1720X_NTTFCFG, |
| MAX1720X_NUSER1D4) }, |
| }; |
| |
| |
| #endif |