Commit 17b600cc authored by Jeremy Fertic's avatar Jeremy Fertic Committed by Greg Kroah-Hartman

staging: iio: adt7316: fix the dac write calculation

commit 78accaea upstream.

The lsb calculation is not masking the correct bits from the user input.
Subtract 1 from (1 << offset) to correctly set up the mask to be applied
to user input.

The lsb register stores its value starting at the bit 7 position.
adt7316_store_DAC() currently assumes the value is at the other end of the
register. Shift the lsb value before storing it in a new variable lsb_reg,
and write this variable to the lsb register.

Fixes: 35f6b6b8 ("staging: iio: new ADT7316/7/8 and ADT7516/7/9 driver")
Signed-off-by: default avatarJeremy Fertic <jeremyfertic@gmail.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 8c5a4013
...@@ -1450,7 +1450,7 @@ static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip, ...@@ -1450,7 +1450,7 @@ static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip,
static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip, static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
int channel, const char *buf, size_t len) int channel, const char *buf, size_t len)
{ {
u8 msb, lsb, offset; u8 msb, lsb, lsb_reg, offset;
u16 data; u16 data;
int ret; int ret;
...@@ -1468,9 +1468,13 @@ static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip, ...@@ -1468,9 +1468,13 @@ static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
return -EINVAL; return -EINVAL;
if (chip->dac_bits > 8) { if (chip->dac_bits > 8) {
lsb = data & (1 << offset); lsb = data & ((1 << offset) - 1);
if (chip->dac_bits == 12)
lsb_reg = lsb << ADT7316_DA_12_BIT_LSB_SHIFT;
else
lsb_reg = lsb << ADT7316_DA_10_BIT_LSB_SHIFT;
ret = chip->bus.write(chip->bus.client, ret = chip->bus.write(chip->bus.client,
ADT7316_DA_DATA_BASE + channel * 2, lsb); ADT7316_DA_DATA_BASE + channel * 2, lsb_reg);
if (ret) if (ret)
return -EIO; return -EIO;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment