diff options
Diffstat (limited to 'drivers/power/max17042_battery.c')
| -rw-r--r-- | drivers/power/max17042_battery.c | 148 |
1 files changed, 124 insertions, 24 deletions
diff --git a/drivers/power/max17042_battery.c b/drivers/power/max17042_battery.c index 6d0327f120a..c6c70c4afeb 100644 --- a/drivers/power/max17042_battery.c +++ b/drivers/power/max17042_battery.c @@ -64,15 +64,23 @@ CONFIG_TS_BIT_ENBL | CONFIG_SS_BIT_ENBL) #define MODEL_LOCK1 0X0000 #define MODEL_LOCK2 0X0000 -#define dQ_ACC_DIV 0x4 -#define dP_ACC_100 0x1900 -#define dP_ACC_200 0x3200 +#define MAX17042_INIT_NUM_CYCLES 160 +#define MAX17047_INIT_NUM_CYCLES 96 + +#define MAX17042_dQ_ACC_DIV 4 +#define MAX17047_dQ_ACC_DIV 16 + +#define MAX17042_dP_ACC_200 0x3200 +#define MAX17047_dP_ACC_200 0x0C80 #define MAX17042_IC_VERSION 0x0092 #define MAX17047_IC_VERSION 0x00AC /* same for max17050 */ +#define MAX17042_AGE_DIV 256 + #define INIT_DATA_PROPERTY "maxim,regs-init-data" #define CONFIG_NODE "maxim,configuration" +#define VERSION_PROPERTY "version" #define CONFIG_PROPERTY "config" #define FULL_SOC_THRESH_PROPERTY "full_soc_thresh" #define DESIGN_CAP_PROPERTY "design_cap" @@ -119,6 +127,26 @@ struct max17042_chip { int malicious_online; }; +#ifdef CONFIG_OF +const char *get_dts_batt_id(struct device *dev) +{ + int lenp; + const char *retval = NULL; + struct device_node *n = of_find_node_by_path("/chosen"); + + if (n) { + retval = of_get_property(n, "batt-id", &lenp); + if (!retval || !lenp) { + dev_err(dev, "%s: batt-id len %d\n", __func__, lenp); + retval = NULL; + } + of_node_put(n); + } + + return retval; +} +#endif + static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value) { int ret = i2c_smbus_write_word_data(client, reg, value); @@ -598,8 +626,9 @@ static void max17042_update_capacity_regs(struct max17042_chip *chip) max17042_write_verify_reg(chip->client, MAX17042_FullCAP, config->fullcap); + /* Set DesignCap to fullcapnom here */ max17042_write_reg(chip->client, MAX17042_DesignCap, - config->design_cap); + config->fullcapnom); max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom, config->fullcapnom); } @@ -614,10 +643,21 @@ static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK); } +static void max17042_advance_to_coulomb_counter_mode(struct max17042_chip *chip) +{ + u16 value = (chip->chip_type == MAX17042 ? + MAX17042_INIT_NUM_CYCLES : MAX17047_INIT_NUM_CYCLES); + max17042_write_verify_reg(chip->client, MAX17042_Cycles, value); +} + static void max17042_load_new_capacity_params(struct max17042_chip *chip) { u16 rep_cap, dq_acc, vfSoc; u32 rem_cap; + u16 dQ_ACC_DIV = (chip->chip_type == MAX17042 ? + MAX17042_dQ_ACC_DIV : MAX17047_dQ_ACC_DIV); + u16 dP_ACC_200 = (chip->chip_type == MAX17042 ? + MAX17042_dP_ACC_200 : MAX17047_dP_ACC_200); struct max17042_config_data *config = chip->pdata->config_data; @@ -669,25 +709,14 @@ static inline void max17042_override_por_values(struct max17042_chip *chip) config->soc_alrt_thresh); max17042_override_por(client, MAX17042_CONFIG, config->config); max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer); - - max17042_override_por(client, MAX17042_DesignCap, config->design_cap); - max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term); - max17042_override_por(client, MAX17042_AtRate, config->at_rate); - max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg); - max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg); - max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg); max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg); max17042_override_por(client, MAX17042_MaskSOC, config->masksoc); - max17042_override_por(client, MAX17042_FullCAP, config->fullcap); - max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom); if (chip->chip_type == MAX17042) max17042_override_por(client, MAX17042_SOC_empty, config->socempty); max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty); - max17042_override_por(client, MAX17042_dQacc, config->dqacc); - max17042_override_por(client, MAX17042_dPacc, config->dpacc); if (chip->chip_type == MAX17042) max17042_override_por(client, MAX17042_V_empty, config->vempty); @@ -696,8 +725,6 @@ static inline void max17042_override_por_values(struct max17042_chip *chip) max17042_override_por(client, MAX17042_TempNom, config->temp_nom); max17042_override_por(client, MAX17042_TempLim, config->temp_lim); max17042_override_por(client, MAX17042_FCTC, config->fctc); - max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0); - max17042_override_por(client, MAX17042_TempCo, config->tcompc0); if (chip->chip_type == MAX17042) { max17042_override_por(client, MAX17042_EmptyTempCo, config->empty_tempco); @@ -748,6 +775,9 @@ static int max17042_init_chip(struct max17042_chip *chip) /* reset vfsoc0 reg */ max17042_reset_vfsoc0_reg(chip); + /* advance to coulomb-counter mode */ + max17042_advance_to_coulomb_counter_mode(chip); + /* load new capacity params */ max17042_load_new_capacity_params(chip); @@ -821,8 +851,13 @@ static void max17042_init_worker(struct work_struct *work) ret = max17042_init_chip(chip); } - if (!ret) + if (!ret) { chip->init_complete = 1; + if (chip->chip_type == MAX17047) { + max17042_write_reg(chip->client, MAX17047_Config_Ver, + chip->pdata->config_data->version); + } + } mutex_unlock(&chip->lock); } @@ -831,11 +866,16 @@ static void max17042_malicious_removed_worker(struct work_struct *work) { struct max17042_chip *chip = container_of(work, struct max17042_chip, work_malicious_removed); + int ret; mutex_lock(&chip->lock); max17042_perform_soft_POR(chip); - max17042_init_chip(chip); + ret = max17042_init_chip(chip); + if (!ret && chip->chip_type == MAX17047) + max17042_write_reg(chip->client, MAX17047_Config_Ver, + chip->pdata->config_data->version); + dev_info(&chip->client->dev, "malicious ps removed, chip re-inited\n"); mutex_unlock(&chip->lock); @@ -949,6 +989,10 @@ static int max17042_cfg_rqrd_prop(struct device *dev, struct device_node *np, struct max17042_config_data *config_data) { + if (of_property_read_u16(np, VERSION_PROPERTY, + &config_data->version)) + return -EINVAL; + if (of_property_read_u16(np, CONFIG_PROPERTY, &config_data->config)) return -EINVAL; @@ -1008,13 +1052,26 @@ static void max17042_cfg_optnl_prop(struct device_node *np, static struct max17042_config_data * max17042_get_config_data(struct device *dev) { + char *config_node = NULL; + char config_node_path[64]; struct max17042_config_data *config_data; struct device_node *np = dev->of_node; if (!np) return NULL; - np = of_get_child_by_name(np, CONFIG_NODE); + config_node = (char *)get_dts_batt_id(dev); + if (config_node) { + snprintf(config_node_path, sizeof(config_node_path), + "%s-%s", CONFIG_NODE, config_node); + config_node = config_node_path; + } else { + config_node = CONFIG_NODE; + } + + dev_info(dev, "using %s profile\n", config_node); + + np = of_get_child_by_name(np, config_node); if (!np) return NULL; @@ -1175,6 +1232,23 @@ static int max17042_debugfs_write_data(void *data, u64 val) DEFINE_SIMPLE_ATTRIBUTE(data_fops, max17042_debugfs_read_data, max17042_debugfs_write_data, "0x%02llx\n"); +static int max17042_debugfs_read_capacity(void *data, u64 *val) +{ + struct max17042_chip *chip = (struct max17042_chip *)data; + *val = chip->debugfs_capacity; + return 0; +} + +static int max17042_debugfs_write_capacity(void *data, u64 val) +{ + struct max17042_chip *chip = (struct max17042_chip *)data; + chip->debugfs_capacity = val; + power_supply_changed(&chip->battery); + return 0; +} +DEFINE_SIMPLE_ATTRIBUTE(capacity_fops, max17042_debugfs_read_capacity, + max17042_debugfs_write_capacity, "%llu\n"); + static int max17042_debugfs_create(struct max17042_chip *chip) { chip->debugfs_root = debugfs_create_dir(dev_name(&chip->client->dev), @@ -1191,8 +1265,8 @@ static int max17042_debugfs_create(struct max17042_chip *chip) goto err_debugfs; chip->debugfs_capacity = 0xFF; - if (!debugfs_create_u8("capacity", S_IRUGO | S_IWUSR, - chip->debugfs_root, &chip->debugfs_capacity)) + if (!debugfs_create_file("capacity", S_IRUGO | S_IWUSR, + chip->debugfs_root, chip, &capacity_fops)) goto err_debugfs; return 0; @@ -1226,7 +1300,6 @@ static void max17042_external_power_changed(struct power_supply *psy) } } - static ssize_t max17042_show_alert_threshold(struct device *dev, struct device_attribute *attr, char *buf) @@ -1256,8 +1329,21 @@ static ssize_t max17042_store_alert_threshold(struct device *dev, static DEVICE_ATTR(alert_threshold, S_IRUGO | S_IWUSR, max17042_show_alert_threshold, max17042_store_alert_threshold); + +static ssize_t max17042_show_battery_age(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct max17042_chip *chip = dev_get_drvdata(dev); + int ret = max17042_read_reg(chip->client, MAX17042_Age); + + return ret < 0 ? ret : sprintf(buf, "%u\n", ret / MAX17042_AGE_DIV); +} +static DEVICE_ATTR(battery_age, S_IRUGO, max17042_show_battery_age, NULL); + static struct attribute *max17042_attrs[] = { &dev_attr_alert_threshold.attr, + &dev_attr_battery_age.attr, NULL, }; @@ -1265,6 +1351,20 @@ static struct attribute_group max17042_attr_group = { .attrs = max17042_attrs, }; +static bool max17042_new_config_data(struct max17042_chip *chip) +{ + int ret; + + if (chip->chip_type == MAX17042) + return false; + + ret = max17042_read_reg(chip->client, MAX17047_Config_Ver); + if (ret < 0) + return false; + + return (chip->pdata->config_data->version != ret); +} + static int max17042_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -1384,7 +1484,7 @@ static int max17042_probe(struct i2c_client *client, } reg = max17042_read_reg(chip->client, MAX17042_STATUS); - if (reg & STATUS_POR_BIT) { + if (reg & STATUS_POR_BIT || max17042_new_config_data(chip)) { INIT_WORK(&chip->work, max17042_init_worker); schedule_work(&chip->work); } else { |