mirror of
https://github.com/torvalds/linux.git
synced 2026-09-24 06:24:02 +02:00
iio: dac: m62332: Fix regulator reference count imbalance
m62332_set_value() enables the Vcc regulator on every write of a
non-zero value and disables it on every write of zero, without tracking
the channel's current state. Because the regulator is reference counted,
changing a channel directly from one non-zero value to another enables
it more than once, while a later write of zero disables it only once.
The reference count never returns to zero and the regulator is left
enabled indefinitely.
Only enable the regulator on the transition from zero to non-zero, and
only disable it on the transition from non-zero to zero, using the
previously stored channel value to detect the edge. Balance the
regulator on the I2C error path so the reference count stays consistent
if the write fails.
Fixes: b87b0c0f81 ("iio: add m62332 DAC driver")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260418130322.106769-1-erick.henrique.rodrigues%40usp.br
Cc: stable@vger.kernel.org
Signed-off-by: Erick Henrique <erick.henrique.rodrigues@usp.br>
Signed-off-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
This commit is contained in:
parent
967d066f53
commit
a130404ce0
|
|
@ -32,6 +32,7 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
|
|||
{
|
||||
struct m62332_data *data = iio_priv(indio_dev);
|
||||
struct i2c_client *client = data->client;
|
||||
bool enabling, disabling;
|
||||
u8 outbuf[2];
|
||||
int res;
|
||||
|
||||
|
|
@ -43,7 +44,10 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
|
|||
|
||||
mutex_lock(&data->mutex);
|
||||
|
||||
if (val) {
|
||||
enabling = val && !data->raw[channel];
|
||||
disabling = !val && data->raw[channel];
|
||||
|
||||
if (enabling) {
|
||||
res = regulator_enable(data->vcc);
|
||||
if (res)
|
||||
goto out;
|
||||
|
|
@ -52,14 +56,17 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
|
|||
res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
|
||||
if (res >= 0 && res != ARRAY_SIZE(outbuf))
|
||||
res = -EIO;
|
||||
if (res < 0)
|
||||
if (res < 0) {
|
||||
if (enabling)
|
||||
regulator_disable(data->vcc);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (disabling)
|
||||
regulator_disable(data->vcc);
|
||||
|
||||
data->raw[channel] = val;
|
||||
|
||||
if (!val)
|
||||
regulator_disable(data->vcc);
|
||||
|
||||
mutex_unlock(&data->mutex);
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user