Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: dac: mcp4922: fix error handling in mcp4922_write_raw

Do not try to write negative values and make sure that the write goes well.

Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Marcus Folkesson and committed by
Jonathan Cameron
0833627f e2b01faf

+8 -3
+8 -3
drivers/iio/dac/mcp4922.c
··· 94 94 long mask) 95 95 { 96 96 struct mcp4922_state *state = iio_priv(indio_dev); 97 + int ret; 97 98 98 99 if (val2 != 0) 99 100 return -EINVAL; 100 101 101 102 switch (mask) { 102 103 case IIO_CHAN_INFO_RAW: 103 - if (val > GENMASK(chan->scan_type.realbits-1, 0)) 104 + if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0)) 104 105 return -EINVAL; 105 106 val <<= chan->scan_type.shift; 106 - state->value[chan->channel] = val; 107 - return mcp4922_spi_write(state, chan->channel, val); 107 + 108 + ret = mcp4922_spi_write(state, chan->channel, val); 109 + if (!ret) 110 + state->value[chan->channel] = val; 111 + return ret; 112 + 108 113 default: 109 114 return -EINVAL; 110 115 }