Skip to content

Commit 16fc40c

Browse files
rodrigo455jic23
authored andcommitted
iio: dac: ad5686: add helpers to handle powerdown masks
Add ad5686_pd_field_set() and ad5686_pd_field_get() helpers to cleanup powerdown mask control. Define AD5686_PD_* constants, e.g. AD5686_PD_MSK to hold powerdown mask value for a single channel. AD5686_LDAC_PWRDN_* macros are replaced by AD5686_PD_MODE_*, because they are unused and the LDAC feature for async load of DAC channel values is not related to power down control. Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 34222a4 commit 16fc40c

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

drivers/iio/dac/ad5686.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,40 @@ static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan
3333
return __ffs(chan->address) * 2;
3434
}
3535

36+
static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
37+
unsigned int *pd, unsigned int val)
38+
{
39+
unsigned int shift = ad5686_pd_mask_shift(chan);
40+
41+
*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);
42+
}
43+
44+
static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
45+
unsigned int pd)
46+
{
47+
unsigned int shift = ad5686_pd_mask_shift(chan);
48+
49+
return (pd >> shift) & AD5686_PD_MSK;
50+
}
51+
3652
static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,
3753
const struct iio_chan_spec *chan)
3854
{
39-
unsigned int shift = ad5686_pd_mask_shift(chan);
4055
struct ad5686_state *st = iio_priv(indio_dev);
4156

4257
guard(mutex)(&st->lock);
4358

44-
return ((st->pwr_down_mode >> shift) & 0x3U) - 1;
59+
return ad5686_pd_field_get(chan, st->pwr_down_mode) - 1;
4560
}
4661

4762
static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev,
4863
const struct iio_chan_spec *chan,
4964
unsigned int mode)
5065
{
51-
unsigned int shift = ad5686_pd_mask_shift(chan);
5266
struct ad5686_state *st = iio_priv(indio_dev);
5367

5468
guard(mutex)(&st->lock);
55-
56-
st->pwr_down_mode &= ~(0x3U << shift);
57-
st->pwr_down_mode |= (mode + 1) << shift;
69+
ad5686_pd_field_set(chan, &st->pwr_down_mode, mode + 1);
5870

5971
return 0;
6072
}
@@ -69,12 +81,12 @@ static const struct iio_enum ad5686_powerdown_mode_enum = {
6981
static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
7082
uintptr_t private, const struct iio_chan_spec *chan, char *buf)
7183
{
72-
unsigned int shift = ad5686_pd_mask_shift(chan);
7384
struct ad5686_state *st = iio_priv(indio_dev);
7485

7586
guard(mutex)(&st->lock);
7687

77-
return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask & (0x3U << shift)));
88+
return sysfs_emit(buf, "%d\n",
89+
!!ad5686_pd_field_get(chan, st->pwr_down_mask));
7890
}
7991

8092
static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
@@ -96,9 +108,9 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
96108
guard(mutex)(&st->lock);
97109

98110
if (readin)
99-
st->pwr_down_mask |= 0x3U << ad5686_pd_mask_shift(chan);
111+
ad5686_pd_field_set(chan, &st->pwr_down_mask, AD5686_PD_MSK_PWR_DOWN);
100112
else
101-
st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan));
113+
ad5686_pd_field_set(chan, &st->pwr_down_mask, AD5686_PD_MSK_PWR_UP);
102114

103115
switch (st->chip_info->regmap_type) {
104116
case AD5310_REGMAP:
@@ -471,10 +483,10 @@ int ad5686_probe(struct device *dev,
471483

472484
/* Set all the power down mode for all channels to 1K pulldown */
473485
for (i = 0; i < st->chip_info->num_channels; i++) {
474-
shift = ad5686_pd_mask_shift(&st->chip_info->channels[i]);
475-
st->pwr_down_mask &= ~(0x3U << shift); /* powered up state */
476-
st->pwr_down_mode &= ~(0x3U << shift);
477-
st->pwr_down_mode |= 0x01U << shift;
486+
ad5686_pd_field_set(&st->chip_info->channels[i],
487+
&st->pwr_down_mask, AD5686_PD_MSK_PWR_UP);
488+
ad5686_pd_field_set(&st->chip_info->channels[i],
489+
&st->pwr_down_mode, AD5686_PD_MODE_1K_TO_GND);
478490
}
479491

480492
indio_dev->name = name;

drivers/iio/dac/ad5686.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@
3535
#define AD5686_CMD_DAISY_CHAIN_ENABLE 0x8
3636
#define AD5686_CMD_READBACK_ENABLE 0x9
3737

38-
#define AD5686_LDAC_PWRDN_NONE 0x0
39-
#define AD5686_LDAC_PWRDN_1K 0x1
40-
#define AD5686_LDAC_PWRDN_100K 0x2
41-
#define AD5686_LDAC_PWRDN_3STATE 0x3
42-
4338
#define AD5686_CMD_CONTROL_REG 0x4
4439
#define AD5686_CMD_READBACK_ENABLE_V2 0x5
4540

4641
#define AD5310_REF_BIT_MSK BIT(8)
4742
#define AD5683_REF_BIT_MSK BIT(12)
4843
#define AD5686_REF_BIT_MSK BIT(0)
4944

45+
#define AD5686_PD_MSK GENMASK(1, 0)
46+
47+
#define AD5686_PD_MODE_1K_TO_GND 0x1
48+
#define AD5686_PD_MODE_100K_TO_GND 0x2
49+
#define AD5686_PD_MODE_THREE_STATE 0x3
50+
51+
#define AD5686_PD_MSK_PWR_UP 0x0
52+
#define AD5686_PD_MSK_PWR_DOWN 0x3
5053

5154
enum ad5686_regmap_type {
5255
AD5310_REGMAP,

0 commit comments

Comments
 (0)