Lines Matching +full:cros +full:- +full:ec +full:- +full:i2c
1 // SPDX-License-Identifier: GPL-2.0-or-later
10 #include <linux/devm-helpers.h>
13 #include <linux/i2c.h>
20 #include <linux/power/sbs-battery.h>
103 SBS_DATA(-1, 0x03, 0, 65535),
109 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
111 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
229 return chip->strings[i];
231 return ERR_PTR(-EINVAL);
238 chip->technology = -1;
241 chip->strings[i][0] = 0;
251 int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
257 val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);
261 dev_err(&chip->client->dev,
264 dev_dbg(&chip->client->dev, "%s\n", __func__);
269 struct i2c_client *client = chip->client;
270 int retries = chip->i2c_retry_count;
274 if (chip->is_present == is_present)
278 chip->is_present = false;
280 client->flags &= ~I2C_CLIENT_PEC;
293 * I2C bus is properly connected. This works around the
298 retries--;
302 dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
305 client->flags &= ~I2C_CLIENT_PEC;
306 chip->is_present = true;
314 client->flags |= I2C_CLIENT_PEC;
316 client->flags &= ~I2C_CLIENT_PEC;
318 if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel")
319 && client->flags & I2C_CLIENT_PEC) {
320 dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n");
321 client->flags &= ~I2C_CLIENT_PEC;
324 dev_dbg(&client->dev, "PEC: %s\n",
325 str_enabled_disabled(client->flags & I2C_CLIENT_PEC));
327 if (!chip->is_present && is_present && !chip->charger_broadcasts)
330 chip->is_present = true;
338 int retries = chip->i2c_retry_count;
345 retries--;
349 dev_dbg(&client->dev,
350 "%s: i2c read at address 0x%x failed\n",
365 retries_length = chip->i2c_retry_count;
366 retries_block = chip->i2c_retry_count;
368 dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
372 if (!i2c_check_functionality(client->adapter,
375 return -ENODEV;
383 retries_length--;
387 dev_dbg(&client->dev,
388 "%s: i2c read at address 0x%x failed\n",
396 dev_err(&client->dev,
399 return -EINVAL;
409 retries_block--;
413 dev_dbg(&client->dev,
414 "%s: i2c read at address 0x%x failed\n",
429 int retries = chip->i2c_retry_count;
432 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
433 bool pec = client->flags & I2C_CLIENT_PEC;
434 client->flags &= ~I2C_CLIENT_PEC;
437 client->flags |= I2C_CLIENT_PEC;
445 retries--;
449 dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
462 int retries = chip->i2c_retry_count;
469 retries--;
473 dev_dbg(&client->dev,
474 "%s: i2c write to address 0x%x failed\n",
492 /* Not drawing current -> not charging (i.e. idle) */
532 val->intval = 0; /* battery removed */
539 val->intval = 0; /* battery removed */
545 val->intval = 0;
557 val->intval = 0;
559 val->intval = 1;
562 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
564 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
566 val->intval = POWER_SUPPLY_HEALTH_DEAD;
568 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
570 val->intval = POWER_SUPPLY_HEALTH_GOOD;
583 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
591 val->intval = 0;
598 val->intval = 1; /* battery present */
601 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
604 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
628 val->intval = ret;
631 val->intval =
634 val->intval =
637 val->intval =
640 val->intval =
648 val->intval = POWER_SUPPLY_STATUS_FULL;
650 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
652 val->intval = POWER_SUPPLY_STATUS_CHARGING;
654 sbs_status_correct(client, &val->intval);
656 if (chip->poll_time == 0)
657 chip->last_state = val->intval;
658 else if (chip->last_state != val->intval) {
659 cancel_delayed_work_sync(&chip->work);
660 power_supply_changed(chip->power_supply);
661 chip->poll_time = 0;
665 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
670 val->intval = min(ret, 100);
672 val->intval = 0;
687 dev_warn(&client->dev,
688 "%s: Invalid Property - %d\n", __func__, psp);
690 return -EINVAL;
705 ret = sbs_get_property_index(chip->client, psp);
711 ret = sbs_read_string_data(chip->client, addr, buf);
733 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
746 val->intval *= BASE_UNIT_CONVERSION;
753 val->intval -= TEMP_KELVIN_TO_CELSIUS;
762 val->intval *= TIME_UNIT_CONVERSION;
766 dev_dbg(&client->dev,
815 val->intval = ret;
835 val->strval = sbs_serial;
845 if (chip->technology != -1) {
846 val->intval = chip->technology;
856 chip->technology = POWER_SUPPLY_TECHNOLOGY_LION;
858 chip->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
860 chip->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
862 chip->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
864 chip->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
866 if (chip->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
867 dev_warn(&chip->client->dev, "Unknown chemistry: %s\n", chemistry);
869 val->intval = chip->technology;
891 val->intval = year;
894 val->intval = month;
897 val->intval = day;
900 return -EINVAL;
912 struct i2c_client *client = chip->client;
915 if (chip->gpio_detect) {
916 ret = gpiod_get_value_cansleep(chip->gpio_detect);
920 val->intval = ret;
925 return -ENODATA;
959 mutex_lock(&chip->mode_lock);
961 mutex_unlock(&chip->mode_lock);
997 val->strval = str;
1007 dev_err(&client->dev,
1009 return -EINVAL;
1012 if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
1013 bool old_present = chip->is_present;
1020 if (old_present != chip->is_present)
1021 power_supply_changed(chip->power_supply);
1028 dev_dbg(&client->dev,
1030 psp, val->intval);
1031 } else if (!chip->is_present) {
1033 ret = -ENODATA;
1040 struct power_supply *battery = chip->power_supply;
1043 ret = gpiod_get_value_cansleep(chip->gpio_detect);
1067 cancel_delayed_work_sync(&chip->work);
1069 schedule_delayed_work(&chip->work, HZ);
1070 chip->poll_time = chip->poll_retry_count;
1080 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
1083 chip->poll_time = 0;
1094 sbs_status_correct(chip->client, &ret);
1096 if (chip->last_state != ret) {
1097 chip->poll_time = 0;
1098 power_supply_changed(chip->power_supply);
1101 if (chip->poll_time > 0) {
1102 schedule_delayed_work(&chip->work, HZ);
1103 chip->poll_time--;
1120 struct sbs_platform_data *pdata = client->dev.platform_data;
1125 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
1128 return -ENOMEM;
1130 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
1131 dev_name(&client->dev));
1132 if (!sbs_desc->name)
1133 return -ENOMEM;
1135 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
1137 return -ENOMEM;
1139 chip->flags = (uintptr_t)i2c_get_match_data(client);
1140 chip->client = client;
1141 psy_cfg.fwnode = dev_fwnode(&client->dev);
1143 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
1145 mutex_init(&chip->mode_lock);
1150 rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1151 &chip->i2c_retry_count);
1153 chip->i2c_retry_count = 0;
1155 rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1156 &chip->poll_retry_count);
1158 chip->poll_retry_count = 0;
1161 chip->poll_retry_count = pdata->poll_retry_count;
1162 chip->i2c_retry_count = pdata->i2c_retry_count;
1164 chip->i2c_retry_count = chip->i2c_retry_count + 1;
1166 chip->charger_broadcasts = !device_property_read_bool(&client->dev,
1167 "sbs,disable-charger-broadcasts");
1169 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
1170 "sbs,battery-detect", GPIOD_IN);
1171 if (IS_ERR(chip->gpio_detect))
1172 return dev_err_probe(&client->dev, PTR_ERR(chip->gpio_detect),
1177 if (!chip->gpio_detect)
1180 irq = gpiod_to_irq(chip->gpio_detect);
1182 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
1186 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
1188 dev_name(&client->dev), chip);
1190 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
1199 if (!(force_load || chip->gpio_detect)) {
1205 return dev_err_probe(&client->dev, -ENODEV,
1209 rc = devm_delayed_work_autocancel(&client->dev, &chip->work,
1214 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
1216 if (IS_ERR(chip->power_supply))
1217 return dev_err_probe(&client->dev, PTR_ERR(chip->power_supply),
1220 dev_info(&client->dev,
1221 "%s: battery gas gauge device registered\n", client->name);
1234 if (chip->poll_time > 0)
1235 cancel_delayed_work_sync(&chip->work);
1237 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
1242 if (chip->is_present && ret < 0)
1259 { "sbs-battery", 0 },
1262 MODULE_DEVICE_TABLE(i2c, sbs_id);
1265 { .compatible = "sbs,sbs-battery" },
1283 .name = "sbs-battery",