+/* Magic number to write to ARM_SHIP_MODE register */
+#define LTC4162L_ARM_SHIP_MODE_MAGIC 21325
+
+struct ltc4162l_info {
+ struct i2c_client *client;
+ struct regmap *regmap;
+ struct power_supply *charger;
+ u32 rsnsb; /* Series resistor that sets charge current, microOhm */
+ u32 rsnsi; /* Series resistor to measure input current, microOhm */
+ u8 cell_count; /* Number of connected cells, 0 while unknown */
+};
+
+static u8 ltc4162l_get_cell_count(struct ltc4162l_info *info)
+{
+ int ret;
+ unsigned int val;
+
+ /* Once read successfully */
+ if (info->cell_count)
+ return info->cell_count;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHEM_CELLS_REG, &val);
+ if (ret)
+ return 0;
+
+ /* Lower 4 bits is the cell count, or 0 if the chip doesn't know yet */
+ val &= 0x0f;
+ if (!val)
+ return 0;
+
+ /* Once determined, keep the value */
+ info->cell_count = val;
+
+ return val;
+};
+
+/* Convert enum value to POWER_SUPPLY_STATUS value */
+static int ltc4162l_state_decode(enum ltc4162l_state value)
+{
+ switch (value) {
+ case precharge:
+ case cc_cv_charge:
+ return POWER_SUPPLY_STATUS_CHARGING;
+ case c_over_x_term:
+ return POWER_SUPPLY_STATUS_FULL;
+ case bat_missing_fault:
+ case bat_short_fault:
+ return POWER_SUPPLY_STATUS_UNKNOWN;
+ default:
+ return POWER_SUPPLY_STATUS_NOT_CHARGING;
+ }
+};
+
+static int ltc4162l_get_status(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, ®val);
+ if (ret) {
+ dev_err(&info->client->dev, "Failed to read CHARGER_STATE\n");
+ return ret;
+ }
+
+ val->intval = ltc4162l_state_decode(regval);
+
+ return 0;
+}
+
+static int ltc4162l_charge_status_decode(enum ltc4162l_charge_status value)
+{
+ if (!value)
+ return POWER_SUPPLY_CHARGE_TYPE_NONE;
+
+ /* constant voltage/current and input_current limit are "fast" modes */
+ if (value <= iin_limit_active)
+ return POWER_SUPPLY_CHARGE_TYPE_FAST;
+
+ /* Anything that's not fast we'll return as trickle */
+ return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+}
+
+static int ltc4162l_get_charge_type(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, ®val);
+ if (ret)
+ return ret;
+
+ val->intval = ltc4162l_charge_status_decode(regval);
+
+ return 0;
+}
+
+static int ltc4162l_state_to_health(enum ltc4162l_state value)
+{
+ switch (value) {
+ case ntc_pause:
+ return POWER_SUPPLY_HEALTH_OVERHEAT;
+ case timer_term:
+ return POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
+ case max_charge_time_fault:
+ return POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE;
+ case bat_missing_fault:
+ return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
+ case bat_short_fault:
+ return POWER_SUPPLY_HEALTH_DEAD;
+ default:
+ return POWER_SUPPLY_HEALTH_GOOD;
+ }
+}
+
+static int ltc4162l_get_health(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, ®val);
+ if (ret)
+ return ret;
+
+ val->intval = ltc4162l_state_to_health(regval);
+
+ return 0;
+}
+
+static int ltc4162l_get_online(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_SYSTEM_STATUS_REG, ®val);
+ if (ret)
+ return ret;
+
+ /* BIT(2) indicates if input voltage is sufficient to charge */
+ val->intval = !!(regval & BIT(2));
+
+ return 0;
+}
+
+
+static int ltc4162l_get_vbat(struct ltc4162l_info *info,
+ unsigned int reg,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, reg, ®val);
+ if (ret)
+ return ret;
+
+ /* cell_count × 192.4μV/LSB */
+ regval *= 1924;
+ regval *= ltc4162l_get_cell_count(info);
+ regval /= 10;
+ val->intval = regval;
+
+ return 0;
+}
+
+static int ltc4162l_get_ibat(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_IBAT, ®val);
+ if (ret)
+ return ret;
+
+ /* Signed 16-bit number, 1.466μV / RSNSB amperes/LSB. */
+ ret = (s16)(regval & 0xFFFF);
+ val->intval = 100 * mult_frac(ret, 14660, (int)info->rsnsb);
+
+ return 0;
+}
+
+static int ltc4162l_get_icharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, reg, ®val);
+ if (ret)
+ return ret;
+
+ regval &= BIT(6) - 1; /* Only the lower 5 bits */
+
+ /* The charge current servo level: (icharge_dac + 1) × 1mV/RSNSB */
+ ++regval;
+ val->intval = 10000u * mult_frac(regval, 100000u, info->rsnsb);
+
+ return 0;
+}
+
+static int ltc4162l_set_icharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ unsigned int value)
+{
+ value = mult_frac(value, info->rsnsb, 100000u);
+ value /= 10000u;
+
+ /* Round to lowest possible */
+ if (value)
+ --value;
+
+ if (value > 31)
+ return -EINVAL;
+
+ return regmap_write(info->regmap, reg, value);
+}
+
+
+static int ltc4162l_get_vcharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+ u32 voltage;
+
+ ret = regmap_read(info->regmap, reg, ®val);
+ if (ret)
+ return ret;
+
+ regval &= BIT(6) - 1; /* Only the lower 5 bits */
+
+ /*
+ * charge voltage setting can be computed from
+ * cell_count × (vcharge_setting × 12.5mV + 3.8125V)
+ * where vcharge_setting ranges from 0 to 31 (4.2V max).
+ */
+ voltage = 3812500 + (regval * 12500);
+ voltage *= ltc4162l_get_cell_count(info);
+ val->intval = voltage;
+
+ return 0;
+}
+
+static int ltc4162l_set_vcharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ unsigned int value)
+{
+ u8 cell_count = ltc4162l_get_cell_count(info);
+
+ if (!cell_count)
+ return -EBUSY; /* Not available yet, try again later */
+
+ value /= cell_count;
+
+ if (value < 3812500)
+ return -EINVAL;
+
+ value -= 3812500;
+ value /= 12500;
+
+ if (value > 31)
+ return -EINVAL;
+
+ return regmap_write(info->regmap, reg, value);
+}
+
+static int ltc4162l_get_iin_limit_dac(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_IIN_LIMIT_DAC, ®val);
+ if (ret)
+ return ret;
+
+ regval &= BIT(6) - 1; /* Only 6 bits */
+
+ /* (iin_limit_dac + 1) × 500μV / RSNSI */
+ ++regval;
+ regval *= 5000000u;
+ regval /= info->rsnsi;
+ val->intval = 100u * regval;
+
+ return 0;
+}
+
+static int ltc4162l_set_iin_limit(struct ltc4162l_info *info,
+ unsigned int value)
+{
+ unsigned int regval;
+
+ regval = mult_frac(value, info->rsnsi, 50000u);
+ regval /= 10000u;
+ if (regval)
+ --regval;
+ if (regval > 63)
+ regval = 63;
+
+ return regmap_write(info->regmap, LTC4162L_IIN_LIMIT_TARGET, regval);
+}
+
+static int ltc4162l_get_die_temp(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_DIE_TEMPERATURE, ®val);
+ if (ret)
+ return ret;
+
+ /* die_temp × 0.0215°C/LSB - 264.4°C */
+ ret = (s16)(regval & 0xFFFF);
+ ret *= 215;
+ ret /= 100; /* Centidegrees scale */
+ ret -= 26440;
+ val->intval = ret;
+
+ return 0;
+}
+
+static int ltc4162l_get_term_current(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGER_CONFIG_BITS, ®val);
+ if (ret)
+ return ret;
+
+ /* Check if C_OVER_X_THRESHOLD is enabled */
+ if (!(regval & BIT(2))) {
+ val->intval = 0;
+ return 0;
+ }
+
+ ret = regmap_read(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, ®val);
+ if (ret)
+ return ret;
+
+ /* 1.466μV / RSNSB amperes/LSB */
+ regval *= 14660u;
+ regval /= info->rsnsb;
+ val->intval = 100 * regval;
+
+ return 0;
+}
+
+static int ltc4162l_set_term_current(struct ltc4162l_info *info,
+ unsigned int value)
+{
+ int ret;
+ unsigned int regval;
+
+ if (!value) {
+ /* Disable en_c_over_x_term when set to zero */
+ return regmap_update_bits(info->regmap,
+ LTC4162L_CHARGER_CONFIG_BITS,
+ BIT(2), 0);
+ }
+
+ regval = mult_frac(value, info->rsnsb, 14660u);
+ regval /= 100u;
+
+ ret = regmap_write(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, regval);
+ if (ret)
+ return ret;
+
+ /* Set en_c_over_x_term after changing the threshold value */
+ return regmap_update_bits(info->regmap, LTC4162L_CHARGER_CONFIG_BITS,
+ BIT(2), BIT(2));
+}
+
+/* Custom properties */
+static const char * const ltc4162l_charge_status_name[] = {
+ "ilim_reg_active", /* 32 */
+ "thermal_reg_active",
+ "vin_uvcl_active",
+ "iin_limit_active",
+ "constant_current",
+ "constant_voltage",
+ "charger_off" /* 0 */
+};
+
+static ssize_t charge_status_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ const char *result = ltc4162l_charge_status_name[
+ ARRAY_SIZE(ltc4162l_charge_status_name) - 1];
+ unsigned int regval;
+ unsigned int mask;
+ unsigned int index;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, ®val);
+ if (ret)
+ return ret;
+
+ /* Only one bit is set according to datasheet, let's be safe here */
+ for (mask = 32, index = 0; mask != 0; mask >>= 1, ++index) {
+ if (regval & mask) {
+ result = ltc4162l_charge_status_name[index];
+ break;
+ }
+ }
+
+ return sprintf(buf, "%s\n", result);
+}
+static DEVICE_ATTR_RO(charge_status);
+
+static ssize_t input_voltage_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_VIN, ®val);
+ if (ret)
+ return ret;
+
+ /* 1.649mV/LSB */
+ regval *= 1694;
+
+ return sprintf(buf, "%u\n", regval);
+}
+static DEVICE_ATTR_RO(input_voltage);
+
+static ssize_t input_current_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_IIN, ®val);
+ if (ret)
+ return ret;
+
+ /* Signed 16-bit number, 1.466μV / RSNSI amperes/LSB. */
+ ret = (s16)(regval & 0xFFFF);
+ ret *= 14660;
+ ret /= info->rsnsi;
+ ret *= 100;
+
+ return sprintf(buf, "%d\n", ret);
+}
+static DEVICE_ATTR_RO(input_current);
+
+static ssize_t force_telemetry_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CONFIG_BITS_REG, ®val);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%u\n", regval & BIT(2) ? 1 : 0);
+}
+
+static ssize_t force_telemetry_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf,
+ size_t count)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ int ret;
+ unsigned int value;
+
+ ret = kstrtouint(buf, 0, &value);
+ if (ret < 0)
+ return ret;
+
+ ret = regmap_update_bits(info->regmap, LTC4162L_CONFIG_BITS_REG,
+ BIT(2), value ? BIT(2) : 0);
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(force_telemetry);
+
+static ssize_t arm_ship_mode_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_ARM_SHIP_MODE, ®val);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%u\n",
+ regval == LTC4162L_ARM_SHIP_MODE_MAGIC ? 1 : 0);
+}
+
+static ssize_t arm_ship_mode_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf,
+ size_t count)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ int ret;
+ unsigned int value;
+
+ ret = kstrtouint(buf, 0, &value);
+ if (ret < 0)
+ return ret;
+
+ ret = regmap_write(info->regmap, LTC4162L_ARM_SHIP_MODE,
+ value ? LTC4162L_ARM_SHIP_MODE_MAGIC : 0);
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(arm_ship_mode);
+
+static struct attribute *ltc4162l_sysfs_entries[] = {
+ &dev_attr_charge_status.attr,
+ &dev_attr_input_voltage.attr,
+ &dev_attr_input_current.attr,
+ &dev_attr_force_telemetry.attr,
+ &dev_attr_arm_ship_mode.attr,
+ NULL,
+};
+
+static struct attribute_group ltc4162l_attr_group = {
+ .name = NULL, /* put in device directory */
+ .attrs = ltc4162l_sysfs_entries,
+};
+
+static int ltc4162l_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ return ltc4162l_get_status(info, val);
+ case POWER_SUPPLY_PROP_CHARGE_TYPE:
+ return ltc4162l_get_charge_type(info, val);
+ case POWER_SUPPLY_PROP_HEALTH:
+ return ltc4162l_get_health(info, val);
+ case POWER_SUPPLY_PROP_ONLINE:
+ return ltc4162l_get_online(info, val);
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ return ltc4162l_get_vbat(info, LTC4162L_VBAT, val);
+ case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+ return ltc4162l_get_vbat(info, LTC4162L_VBAT_FILT, val);
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
+ return ltc4162l_get_ibat(info, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+ return ltc4162l_get_icharge(info,
+ LTC4162L_ICHARGE_DAC, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ return ltc4162l_get_icharge(info,
+ LTC4162L_CHARGE_CURRENT_SETTING, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+ return ltc4162l_get_vcharge(info,
+ LTC4162L_VCHARGE_DAC, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ return ltc4162l_get_vcharge(info,
+ LTC4162L_VCHARGE_SETTING, val);
+ case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+ return ltc4162l_get_iin_limit_dac(info, val);
+ case POWER_SUPPLY_PROP_TEMP:
+ return ltc4162l_get_die_temp(info, val);
+ case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
+ return ltc4162l_get_term_current(info, val);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ltc4162l_set_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ const union power_supply_propval *val)
+{
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ return ltc4162l_set_icharge(info,
+ LTC4162L_CHARGE_CURRENT_SETTING, val->intval);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ return ltc4162l_set_vcharge(info,
+ LTC4162L_VCHARGE_SETTING, val->intval);
+ case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+ return ltc4162l_set_iin_limit(info, val->intval);
+ case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
+ return ltc4162l_set_term_current(info, val->intval);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ltc4162l_property_is_writeable(struct power_supply *psy,
+ enum power_supply_property psp)
+{
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+ case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+/* Charger power supply property routines */
+static enum power_supply_property ltc4162l_properties[] = {
+ POWER_SUPPLY_PROP_STATUS,
+ POWER_SUPPLY_PROP_CHARGE_TYPE,
+ POWER_SUPPLY_PROP_HEALTH,
+ POWER_SUPPLY_PROP_ONLINE,
+ POWER_SUPPLY_PROP_VOLTAGE_NOW, /* Reports battery voltage */
+ POWER_SUPPLY_PROP_VOLTAGE_AVG,
+ POWER_SUPPLY_PROP_CURRENT_NOW, /* Reports battery current */