Lines Matching +full:custom +full:- +full:temp

1 // SPDX-License-Identifier: GPL-2.0+
12 #include <linux/devm-helpers.h>
94 static int max17042_get_temperature(struct max17042_chip *chip, int *temp) in max17042_get_temperature() argument
98 struct regmap *map = chip->regmap; in max17042_get_temperature()
104 *temp = sign_extend32(data, 15); in max17042_get_temperature()
105 /* The value is converted into deci-centigrade scale */ in max17042_get_temperature()
107 *temp = *temp * 10 / 256; in max17042_get_temperature()
117 ret = power_supply_am_i_supplied(chip->battery); in max17042_get_status()
128 * The MAX170xx has builtin end-of-charge detection and will update in max17042_get_status()
133 * continuously once end-of-charge detection kicks in, so allow the in max17042_get_status()
137 ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full); in max17042_get_status()
141 ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now); in max17042_get_status()
145 if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) { in max17042_get_status()
154 if (!chip->pdata->enable_current_sense) { in max17042_get_status()
159 ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data); in max17042_get_status()
164 avg_current *= 1562500 / chip->pdata->r_sns; in max17042_get_status()
176 int temp, vavg, vbatt, ret; in max17042_get_battery_health() local
179 ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val); in max17042_get_battery_health()
183 /* bits [0-3] unused */ in max17042_get_battery_health()
188 ret = regmap_read(chip->regmap, MAX17042_VCELL, &val); in max17042_get_battery_health()
192 /* bits [0-3] unused */ in max17042_get_battery_health()
197 if (vavg < chip->pdata->vmin) { in max17042_get_battery_health()
202 if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) { in max17042_get_battery_health()
207 ret = max17042_get_temperature(chip, &temp); in max17042_get_battery_health()
211 if (temp < chip->pdata->temp_min) { in max17042_get_battery_health()
216 if (temp > chip->pdata->temp_max) { in max17042_get_battery_health()
235 struct regmap *map = chip->regmap; in max17042_get_property()
240 if (!chip->init_complete) in max17042_get_property()
241 return -EAGAIN; in max17042_get_property()
245 ret = max17042_get_status(chip, &val->intval); in max17042_get_property()
255 val->intval = 0; in max17042_get_property()
257 val->intval = 1; in max17042_get_property()
260 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; in max17042_get_property()
267 val->intval = data; in max17042_get_property()
274 val->intval = data >> 8; in max17042_get_property()
275 val->intval *= 20000; /* Units of LSB = 20mV */ in max17042_get_property()
282 val->intval = (data & 0xff) * 20000; /* Units of 20mV */ in max17042_get_property()
285 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) in max17042_get_property()
292 val->intval = data >> 7; in max17042_get_property()
293 val->intval *= 10000; /* Units of LSB = 10mV */ in max17042_get_property()
300 val->intval = data * 625 / 8; in max17042_get_property()
307 val->intval = data * 625 / 8; in max17042_get_property()
314 val->intval = data * 625 / 8; in max17042_get_property()
317 if (chip->pdata->enable_current_sense) in max17042_get_property()
324 val->intval = data >> 8; in max17042_get_property()
332 do_div(data64, chip->pdata->r_sns); in max17042_get_property()
333 val->intval = data64; in max17042_get_property()
341 do_div(data64, chip->pdata->r_sns); in max17042_get_property()
342 val->intval = data64; in max17042_get_property()
350 do_div(data64, chip->pdata->r_sns); in max17042_get_property()
351 val->intval = data64; in max17042_get_property()
359 val->intval = div_s64(data64, chip->pdata->r_sns); in max17042_get_property()
362 ret = max17042_get_temperature(chip, &val->intval); in max17042_get_property()
370 /* LSB is Alert Minimum. In deci-centigrade */ in max17042_get_property()
371 val->intval = sign_extend32(data & 0xff, 7) * 10; in max17042_get_property()
377 /* MSB is Alert Maximum. In deci-centigrade */ in max17042_get_property()
378 val->intval = sign_extend32(data >> 8, 7) * 10; in max17042_get_property()
381 val->intval = chip->pdata->temp_min; in max17042_get_property()
384 val->intval = chip->pdata->temp_max; in max17042_get_property()
387 ret = max17042_get_battery_health(chip, &val->intval); in max17042_get_property()
392 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; in max17042_get_property()
395 if (chip->pdata->enable_current_sense) { in max17042_get_property()
401 val->intval = div_s64(data64, chip->pdata->r_sns); in max17042_get_property()
403 return -EINVAL; in max17042_get_property()
407 if (chip->pdata->enable_current_sense) { in max17042_get_property()
413 val->intval = div_s64(data64, chip->pdata->r_sns); in max17042_get_property()
415 return -EINVAL; in max17042_get_property()
424 val->intval = div_s64(data64, chip->pdata->r_sns); in max17042_get_property()
431 val->intval = data * 5625 / 1000; in max17042_get_property()
434 return -EINVAL; in max17042_get_property()
444 struct regmap *map = chip->regmap; in max17042_set_property()
447 int8_t temp; in max17042_set_property() local
455 /* Input in deci-centigrade, convert to centigrade */ in max17042_set_property()
456 temp = val->intval / 10; in max17042_set_property()
458 if (temp >= (int8_t)(data >> 8)) in max17042_set_property()
459 temp = (int8_t)(data >> 8) - 1; in max17042_set_property()
461 data = (data & 0xff00) + temp; in max17042_set_property()
469 /* Input in Deci-Centigrade, convert to centigrade */ in max17042_set_property()
470 temp = val->intval / 10; in max17042_set_property()
472 if (temp <= (int8_t)(data & 0xff)) in max17042_set_property()
473 temp = (int8_t)(data & 0xff) + 1; in max17042_set_property()
475 data = (data & 0xff) + (temp << 8); in max17042_set_property()
479 ret = -EINVAL; in max17042_set_property()
512 ret = -EIO; in max17042_write_verify_reg()
513 retries--; in max17042_write_verify_reg()
532 struct regmap *map = chip->regmap; in max17042_unlock_model()
540 struct regmap *map = chip->regmap; in max17042_lock_model()
549 struct regmap *map = chip->regmap; in max17042_write_model_data()
554 chip->pdata->config_data->cell_char_tbl[i]); in max17042_write_model_data()
560 struct regmap *map = chip->regmap; in max17042_read_model_data()
576 dev_err(&chip->client->dev, "%s compare failed\n", __func__); in max17042_model_data_compare()
578 dev_info(&chip->client->dev, "0x%x, 0x%x", in max17042_model_data_compare()
580 dev_info(&chip->client->dev, "\n"); in max17042_model_data_compare()
581 return -EINVAL; in max17042_model_data_compare()
589 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); in max17042_init_model()
594 return -ENOMEM; in max17042_init_model()
604 chip->pdata->config_data->cell_char_tbl, in max17042_init_model()
617 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); in max17042_verify_model_lock()
623 return -ENOMEM; in max17042_verify_model_lock()
629 ret = -EINVAL; in max17042_verify_model_lock()
637 struct max17042_config_data *config = chip->pdata->config_data; in max17042_write_config_regs()
638 struct regmap *map = chip->regmap; in max17042_write_config_regs()
640 regmap_write(map, MAX17042_CONFIG, config->config); in max17042_write_config_regs()
641 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg); in max17042_write_config_regs()
643 config->filter_cfg); in max17042_write_config_regs()
644 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg); in max17042_write_config_regs()
645 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 || in max17042_write_config_regs()
646 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050 || in max17042_write_config_regs()
647 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055) in max17042_write_config_regs()
649 config->full_soc_thresh); in max17042_write_config_regs()
654 struct max17042_config_data *config = chip->pdata->config_data; in max17042_write_custom_regs()
655 struct regmap *map = chip->regmap; in max17042_write_custom_regs()
657 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0); in max17042_write_custom_regs()
658 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0); in max17042_write_custom_regs()
659 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term); in max17042_write_custom_regs()
660 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { in max17042_write_custom_regs()
661 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco); in max17042_write_custom_regs()
663 config->kempty0); in max17042_write_custom_regs()
666 config->qrtbl00); in max17042_write_custom_regs()
668 config->qrtbl10); in max17042_write_custom_regs()
670 config->qrtbl20); in max17042_write_custom_regs()
672 config->qrtbl30); in max17042_write_custom_regs()
678 struct max17042_config_data *config = chip->pdata->config_data; in max17042_update_capacity_regs()
679 struct regmap *map = chip->regmap; in max17042_update_capacity_regs()
682 config->fullcap); in max17042_update_capacity_regs()
683 regmap_write(map, MAX17042_DesignCap, config->design_cap); in max17042_update_capacity_regs()
685 config->fullcapnom); in max17042_update_capacity_regs()
691 struct regmap *map = chip->regmap; in max17042_reset_vfsoc0_reg()
704 struct max17042_config_data *config = chip->pdata->config_data; in max17042_load_new_capacity_params()
705 struct regmap *map = chip->regmap; in max17042_load_new_capacity_params()
721 dq_acc = config->fullcap / dQ_ACC_DIV; in max17042_load_new_capacity_params()
726 config->fullcap); in max17042_load_new_capacity_params()
728 config->design_cap); in max17042_load_new_capacity_params()
730 config->fullcapnom); in max17042_load_new_capacity_params()
742 struct regmap *map = chip->regmap; in max17042_override_por_values()
743 struct max17042_config_data *config = chip->pdata->config_data; in max17042_override_por_values()
745 max17042_override_por(map, MAX17042_TGAIN, config->tgain); in max17042_override_por_values()
746 max17042_override_por(map, MAX17042_TOFF, config->toff); in max17042_override_por_values()
747 max17042_override_por(map, MAX17042_CGAIN, config->cgain); in max17042_override_por_values()
748 max17042_override_por(map, MAX17042_COFF, config->coff); in max17042_override_por_values()
750 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh); in max17042_override_por_values()
751 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh); in max17042_override_por_values()
753 config->soc_alrt_thresh); in max17042_override_por_values()
754 max17042_override_por(map, MAX17042_CONFIG, config->config); in max17042_override_por_values()
755 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer); in max17042_override_por_values()
757 max17042_override_por(map, MAX17042_DesignCap, config->design_cap); in max17042_override_por_values()
758 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term); in max17042_override_por_values()
760 max17042_override_por(map, MAX17042_AtRate, config->at_rate); in max17042_override_por_values()
761 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg); in max17042_override_por_values()
762 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg); in max17042_override_por_values()
763 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg); in max17042_override_por_values()
764 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg); in max17042_override_por_values()
766 max17042_override_por(map, MAX17042_FullCAP, config->fullcap); in max17042_override_por_values()
767 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom); in max17042_override_por_values()
768 max17042_override_por(map, MAX17042_dQacc, config->dqacc); in max17042_override_por_values()
769 max17042_override_por(map, MAX17042_dPacc, config->dpacc); in max17042_override_por_values()
771 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0); in max17042_override_por_values()
772 max17042_override_por(map, MAX17042_TempCo, config->tcompc0); in max17042_override_por_values()
774 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { in max17042_override_por_values()
775 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc); in max17042_override_por_values()
776 max17042_override_por(map, MAX17042_SOC_empty, config->socempty); in max17042_override_por_values()
777 max17042_override_por(map, MAX17042_V_empty, config->vempty); in max17042_override_por_values()
778 max17042_override_por(map, MAX17042_EmptyTempCo, config->empty_tempco); in max17042_override_por_values()
779 max17042_override_por(map, MAX17042_K_empty0, config->kempty0); in max17042_override_por_values()
782 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) || in max17042_override_por_values()
783 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || in max17042_override_por_values()
784 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { in max17042_override_por_values()
785 max17042_override_por(map, MAX17042_IAvg_empty, config->iavg_empty); in max17042_override_por_values()
786 max17042_override_por(map, MAX17042_TempNom, config->temp_nom); in max17042_override_por_values()
787 max17042_override_por(map, MAX17042_TempLim, config->temp_lim); in max17042_override_por_values()
788 max17042_override_por(map, MAX17042_FCTC, config->fctc); in max17042_override_por_values()
791 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || in max17042_override_por_values()
792 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050) || in max17042_override_por_values()
793 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)) { in max17042_override_por_values()
794 max17042_override_por(map, MAX17047_V_empty, config->vempty); in max17042_override_por_values()
800 struct regmap *map = chip->regmap; in max17042_init_chip()
815 dev_err(&chip->client->dev, "%s init failed\n", in max17042_init_chip()
817 return -EIO; in max17042_init_chip()
822 dev_err(&chip->client->dev, "%s lock verify failed\n", in max17042_init_chip()
824 return -EIO; in max17042_init_chip()
826 /* write custom parameters */ in max17042_init_chip()
850 struct regmap *map = chip->regmap; in max17042_set_soc_threshold()
856 if (chip->pdata->enable_current_sense) in max17042_set_soc_threshold()
863 soc_tr |= soc - off; in max17042_set_soc_threshold()
873 ret = regmap_read(chip->regmap, MAX17042_STATUS, &val); in max17042_thread_handler()
878 dev_dbg(&chip->client->dev, "SOC threshold INTR\n"); in max17042_thread_handler()
883 regmap_clear_bits(chip->regmap, MAX17042_STATUS, in max17042_thread_handler()
886 power_supply_changed(chip->battery); in max17042_thread_handler()
897 if (chip->pdata->enable_por_init && chip->pdata->config_data) { in max17042_init_worker()
903 chip->init_complete = 1; in max17042_init_worker()
910 struct device *dev = &chip->client->dev; in max17042_get_of_pdata()
911 struct device_node *np = dev->of_node; in max17042_get_of_pdata()
921 * current-sense functionality to be enabled at all. in max17042_get_of_pdata()
923 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) { in max17042_get_of_pdata()
924 pdata->r_sns = prop; in max17042_get_of_pdata()
925 pdata->enable_current_sense = true; in max17042_get_of_pdata()
928 if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min)) in max17042_get_of_pdata()
929 pdata->temp_min = INT_MIN; in max17042_get_of_pdata()
930 if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max)) in max17042_get_of_pdata()
931 pdata->temp_max = INT_MAX; in max17042_get_of_pdata()
932 if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin)) in max17042_get_of_pdata()
933 pdata->vmin = INT_MIN; in max17042_get_of_pdata()
934 if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax)) in max17042_get_of_pdata()
935 pdata->vmax = INT_MAX; in max17042_get_of_pdata()
943 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
952 struct device *dev = &chip->client->dev; in max17042_get_default_pdata()
958 * the firmware will already have initialized the fuel-gauge and provide in max17042_get_default_pdata()
965 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || in max17042_get_default_pdata()
966 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { in max17042_get_default_pdata()
967 pdata->init_data = max17047_default_pdata_init_regs; in max17042_get_default_pdata()
968 pdata->num_init_data = in max17042_get_default_pdata()
972 ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg); in max17042_get_default_pdata()
976 /* If bits 0-1 are set to 3 then only Voltage readings are used */ in max17042_get_default_pdata()
978 pdata->enable_current_sense = false; in max17042_get_default_pdata()
980 pdata->enable_current_sense = true; in max17042_get_default_pdata()
982 pdata->vmin = MAX17042_DEFAULT_VMIN; in max17042_get_default_pdata()
983 pdata->vmax = MAX17042_DEFAULT_VMAX; in max17042_get_default_pdata()
984 pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN; in max17042_get_default_pdata()
985 pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX; in max17042_get_default_pdata()
993 struct device *dev = &chip->client->dev; in max17042_get_pdata()
996 if (dev->of_node) in max17042_get_pdata()
999 if (dev->platform_data) in max17042_get_pdata()
1000 return dev->platform_data; in max17042_get_pdata()
1029 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
1035 struct i2c_adapter *adapter = client->adapter; in max17042_probe()
1039 struct device *dev = &client->dev; in max17042_probe()
1046 return -EIO; in max17042_probe()
1048 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); in max17042_probe()
1050 return -ENOMEM; in max17042_probe()
1052 chip->client = client; in max17042_probe()
1054 chip->chip_type = id->driver_data; in max17042_probe()
1056 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); in max17042_probe()
1058 return -ENODEV; in max17042_probe()
1060 chip->chip_type = acpi_id->driver_data; in max17042_probe()
1062 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config); in max17042_probe()
1063 if (IS_ERR(chip->regmap)) { in max17042_probe()
1064 dev_err(&client->dev, "Failed to initialize regmap\n"); in max17042_probe()
1065 return -EINVAL; in max17042_probe()
1068 chip->pdata = max17042_get_pdata(chip); in max17042_probe()
1069 if (!chip->pdata) { in max17042_probe()
1070 dev_err(&client->dev, "no platform data provided\n"); in max17042_probe()
1071 return -EINVAL; in max17042_probe()
1076 psy_cfg.of_node = dev->of_node; in max17042_probe()
1080 if (!chip->pdata->enable_current_sense) in max17042_probe()
1083 if (chip->pdata->r_sns == 0) in max17042_probe()
1084 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; in max17042_probe()
1086 if (chip->pdata->init_data) in max17042_probe()
1087 for (i = 0; i < chip->pdata->num_init_data; i++) in max17042_probe()
1088 regmap_write(chip->regmap, in max17042_probe()
1089 chip->pdata->init_data[i].addr, in max17042_probe()
1090 chip->pdata->init_data[i].data); in max17042_probe()
1092 if (!chip->pdata->enable_current_sense) { in max17042_probe()
1093 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000); in max17042_probe()
1094 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003); in max17042_probe()
1095 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); in max17042_probe()
1098 chip->battery = devm_power_supply_register(&client->dev, max17042_desc, in max17042_probe()
1100 if (IS_ERR(chip->battery)) { in max17042_probe()
1101 dev_err(&client->dev, "failed: power supply register\n"); in max17042_probe()
1102 return PTR_ERR(chip->battery); in max17042_probe()
1105 if (client->irq) { in max17042_probe()
1109 * On ACPI systems the IRQ may be handled by ACPI-event code, in max17042_probe()
1115 ret = devm_request_threaded_irq(&client->dev, client->irq, in max17042_probe()
1118 chip->battery->desc->name, in max17042_probe()
1121 regmap_update_bits(chip->regmap, MAX17042_CONFIG, in max17042_probe()
1126 client->irq = 0; in max17042_probe()
1127 if (ret != -EBUSY) in max17042_probe()
1128 dev_err(&client->dev, "Failed to get IRQ\n"); in max17042_probe()
1131 /* Not able to update the charge threshold when exceeded? -> disable */ in max17042_probe()
1132 if (!client->irq) in max17042_probe()
1133 regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00); in max17042_probe()
1135 regmap_read(chip->regmap, MAX17042_STATUS, &val); in max17042_probe()
1137 ret = devm_work_autocancel(&client->dev, &chip->work, in max17042_probe()
1141 schedule_work(&chip->work); in max17042_probe()
1143 chip->init_complete = 1; in max17042_probe()
1158 if (chip->client->irq) { in max17042_suspend()
1159 disable_irq(chip->client->irq); in max17042_suspend()
1160 enable_irq_wake(chip->client->irq); in max17042_suspend()
1170 if (chip->client->irq) { in max17042_resume()
1171 disable_irq_wake(chip->client->irq); in max17042_resume()
1172 enable_irq(chip->client->irq); in max17042_resume()
1173 /* re-program the SOC thresholds to 1% change */ in max17042_resume()
1198 { .compatible = "maxim,max77849-battery" },
1209 { "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047 },