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

Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 drivers/iio/dac/mcp4922.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/dac/mcp4922.c b/drivers/iio/dac/mcp4922.c
index bf9aa3fc0534..b5190d1dae8e 100644
--- a/drivers/iio/dac/mcp4922.c
+++ b/drivers/iio/dac/mcp4922.c
@@ -94,17 +94,22 @@ static int mcp4922_write_raw(struct iio_dev *indio_dev,
                long mask)
 {
        struct mcp4922_state *state = iio_priv(indio_dev);
+       int ret;
 
        if (val2 != 0)
                return -EINVAL;
 
        switch (mask) {
        case IIO_CHAN_INFO_RAW:
-               if (val > GENMASK(chan->scan_type.realbits-1, 0))
+               if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0))
                        return -EINVAL;
                val <<= chan->scan_type.shift;
-               state->value[chan->channel] = val;
-               return mcp4922_spi_write(state, chan->channel, val);
+
+               ret = mcp4922_spi_write(state, chan->channel, val);
+               if (!ret)
+                       state->value[chan->channel] = val;
+               return ret;
+
        default:
                return -EINVAL;
        }
-- 
2.18.0

Reply via email to