Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe8a6534 authored by Shawn Nematbakhsh's avatar Shawn Nematbakhsh Committed by Sebastian Reichel
Browse files

power: supply: sbs-battery: Prevent CAPACITY_MODE races



A subset of smart battery commands return charge or energy depending on
the CAPACITY_MODE bit setting of BatteryMode(). In order to
unambiguously read a charge or energy value, it is necessary to ensure
that CAPACITY_MODE is set as desired, and not changed for the duration
of the attribute read.

Signed-off-by: default avatarShawn Nematbakhsh <shawnn@chromium.org>
Reviewed-by: default avatarGuenter Roeck <groeck@chromium.org>
Signed-off-by: default avatarSebastian Reichel <sebastian.reichel@collabora.co.uk>
parent 48f680c0
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -171,6 +171,7 @@ struct sbs_info {
	u32				i2c_retry_count;
	u32				i2c_retry_count;
	u32				poll_retry_count;
	u32				poll_retry_count;
	struct delayed_work		work;
	struct delayed_work		work;
	struct mutex			mode_lock;
};
};


static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
@@ -622,7 +623,13 @@ static int sbs_get_property(struct power_supply *psy,
		if (ret < 0)
		if (ret < 0)
			break;
			break;


		/* sbs_get_battery_capacity() will change the battery mode
		 * temporarily to read the requested attribute. Ensure we stay
		 * in the desired mode for the duration of the attribute read.
		 */
		mutex_lock(&chip->mode_lock);
		ret = sbs_get_battery_capacity(client, ret, psp, val);
		ret = sbs_get_battery_capacity(client, ret, psp, val);
		mutex_unlock(&chip->mode_lock);
		break;
		break;


	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
@@ -807,6 +814,7 @@ static int sbs_probe(struct i2c_client *client,
	psy_cfg.of_node = client->dev.of_node;
	psy_cfg.of_node = client->dev.of_node;
	psy_cfg.drv_data = chip;
	psy_cfg.drv_data = chip;
	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
	mutex_init(&chip->mode_lock);


	/* use pdata if available, fall back to DT properties,
	/* use pdata if available, fall back to DT properties,
	 * or hardcoded defaults if not
	 * or hardcoded defaults if not