Skip to content

Commit 108e4d4

Browse files
bebarinojic23
authored andcommitted
iio:proximity:sx9324: Fix hardware gain read/write
There are four possible gain values according to 'sx9324_gain_vals[]': 1, 2, 4, and 8 The values are off by one when writing and reading the register. The bits should be set according to this equation: ilog2(<gain>) + 1 so that a gain of 8 is 0x4 in the register field and a gain of 4 is 0x3 in the register field, etc. Note that a gain of 0 is reserved per the datasheet. The default gain (SX9324_REG_PROX_CTRL0_GAIN_1) is also wrong. It should be 0x1 << 3, i.e. 0x8, not 0x80 which is setting the reserved bit 7. Fix this all up to properly handle the hardware gain and return errors for invalid settings. Fixes: 4c18a89 ("iio:proximity:sx9324: Add SX9324 support") Signed-off-by: Stephen Boyd <[email protected]> Reviewed-by: Gwendal Grignou <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 74a53a9 commit 108e4d4

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

drivers/iio/proximity/sx9324.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@
7676

7777
#define SX9324_REG_PROX_CTRL0 0x30
7878
#define SX9324_REG_PROX_CTRL0_GAIN_MASK GENMASK(5, 3)
79-
#define SX9324_REG_PROX_CTRL0_GAIN_1 0x80
79+
#define SX9324_REG_PROX_CTRL0_GAIN_SHIFT 3
80+
#define SX9324_REG_PROX_CTRL0_GAIN_RSVD 0x0
81+
#define SX9324_REG_PROX_CTRL0_GAIN_1 0x1
82+
#define SX9324_REG_PROX_CTRL0_GAIN_8 0x4
8083
#define SX9324_REG_PROX_CTRL0_RAWFILT_MASK GENMASK(2, 0)
8184
#define SX9324_REG_PROX_CTRL0_RAWFILT_1P50 0x01
8285
#define SX9324_REG_PROX_CTRL1 0x31
@@ -379,7 +382,14 @@ static int sx9324_read_gain(struct sx_common_data *data,
379382
if (ret)
380383
return ret;
381384

382-
*val = 1 << FIELD_GET(SX9324_REG_PROX_CTRL0_GAIN_MASK, regval);
385+
regval = FIELD_GET(SX9324_REG_PROX_CTRL0_GAIN_MASK, regval);
386+
if (regval)
387+
regval--;
388+
else if (regval == SX9324_REG_PROX_CTRL0_GAIN_RSVD ||
389+
regval > SX9324_REG_PROX_CTRL0_GAIN_8)
390+
return -EINVAL;
391+
392+
*val = 1 << regval;
383393

384394
return IIO_VAL_INT;
385395
}
@@ -725,8 +735,12 @@ static int sx9324_write_gain(struct sx_common_data *data,
725735
unsigned int gain, reg;
726736
int ret;
727737

728-
gain = ilog2(val);
729738
reg = SX9324_REG_PROX_CTRL0 + chan->channel / 2;
739+
740+
gain = ilog2(val) + 1;
741+
if (val <= 0 || gain > SX9324_REG_PROX_CTRL0_GAIN_8)
742+
return -EINVAL;
743+
730744
gain = FIELD_PREP(SX9324_REG_PROX_CTRL0_GAIN_MASK, gain);
731745

732746
mutex_lock(&data->mutex);
@@ -784,9 +798,11 @@ static const struct sx_common_reg_default sx9324_default_regs[] = {
784798
{ SX9324_REG_AFE_CTRL8, SX9324_REG_AFE_CTRL8_RESFILTN_4KOHM },
785799
{ SX9324_REG_AFE_CTRL9, SX9324_REG_AFE_CTRL9_AGAIN_1 },
786800

787-
{ SX9324_REG_PROX_CTRL0, SX9324_REG_PROX_CTRL0_GAIN_1 |
801+
{ SX9324_REG_PROX_CTRL0,
802+
SX9324_REG_PROX_CTRL0_GAIN_1 << SX9324_REG_PROX_CTRL0_GAIN_SHIFT |
788803
SX9324_REG_PROX_CTRL0_RAWFILT_1P50 },
789-
{ SX9324_REG_PROX_CTRL1, SX9324_REG_PROX_CTRL0_GAIN_1 |
804+
{ SX9324_REG_PROX_CTRL1,
805+
SX9324_REG_PROX_CTRL0_GAIN_1 << SX9324_REG_PROX_CTRL0_GAIN_SHIFT |
790806
SX9324_REG_PROX_CTRL0_RAWFILT_1P50 },
791807
{ SX9324_REG_PROX_CTRL2, SX9324_REG_PROX_CTRL2_AVGNEG_THRESH_16K },
792808
{ SX9324_REG_PROX_CTRL3, SX9324_REG_PROX_CTRL3_AVGDEB_2SAMPLES |

0 commit comments

Comments
 (0)