Skip to content

Commit ae8360f

Browse files
rodrigo455jic23
authored andcommitted
iio: dac: ad5686: create bus ops struct
Create struct with bus operations, which will be used to extend bus implementation features. Auxiliary functions ad5686_write() and ad5686_read() are created and ad5686_probe() now receives an ops struct pointer rather than individual read and write functions. Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com> Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 883a752 commit ae8360f

4 files changed

Lines changed: 47 additions & 28 deletions

File tree

drivers/iio/dac/ad5686-spi.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,15 @@ static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
9292
return be32_to_cpu(st->data[2].d32);
9393
}
9494

95+
static const struct ad5686_bus_ops ad5686_spi_ops = {
96+
.write = ad5686_spi_write,
97+
.read = ad5686_spi_read,
98+
};
99+
95100
static int ad5686_spi_probe(struct spi_device *spi)
96101
{
97102
return ad5686_probe(&spi->dev, spi_get_device_match_data(spi),
98-
spi->modalias, ad5686_spi_write, ad5686_spi_read);
103+
spi->modalias, &ad5686_spi_ops);
99104
}
100105

101106
static const struct spi_device_id ad5686_spi_id[] = {

drivers/iio/dac/ad5686.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ static int ad5310_control_sync(struct ad5686_state *st)
3030
{
3131
unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
3232

33-
return st->write(st, AD5686_CMD_CONTROL_REG, 0,
34-
FIELD_PREP(AD5310_PD_MSK, pd_val & AD5686_PD_MSK) |
35-
FIELD_PREP(AD5310_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
33+
return ad5686_write(st, AD5686_CMD_CONTROL_REG, 0,
34+
FIELD_PREP(AD5310_PD_MSK, pd_val & AD5686_PD_MSK) |
35+
FIELD_PREP(AD5310_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
3636
}
3737

3838
static int ad5683_control_sync(struct ad5686_state *st)
3939
{
4040
unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
4141

42-
return st->write(st, AD5686_CMD_CONTROL_REG, 0,
43-
FIELD_PREP(AD5683_PD_MSK, pd_val & AD5686_PD_MSK) |
44-
FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
42+
return ad5686_write(st, AD5686_CMD_CONTROL_REG, 0,
43+
FIELD_PREP(AD5683_PD_MSK, pd_val & AD5686_PD_MSK) |
44+
FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
4545
}
4646

4747
static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan)
@@ -153,7 +153,7 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
153153
address = 0x0;
154154
val = lower_16_bits(val);
155155
}
156-
ret = st->write(st, AD5686_CMD_POWERDOWN_DAC, address, val);
156+
ret = ad5686_write(st, AD5686_CMD_POWERDOWN_DAC, address, val);
157157
if (ret)
158158
return ret;
159159
break;
@@ -176,7 +176,7 @@ static int ad5686_read_raw(struct iio_dev *indio_dev,
176176
switch (m) {
177177
case IIO_CHAN_INFO_RAW:
178178
mutex_lock(&st->lock);
179-
ret = st->read(st, chan->address);
179+
ret = ad5686_read(st, chan->address);
180180
mutex_unlock(&st->lock);
181181
if (ret < 0)
182182
return ret;
@@ -206,10 +206,8 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
206206
return -EINVAL;
207207

208208
mutex_lock(&st->lock);
209-
ret = st->write(st,
210-
AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
211-
chan->address,
212-
val << chan->scan_type.shift);
209+
ret = ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
210+
chan->address, val << chan->scan_type.shift);
213211
mutex_unlock(&st->lock);
214212
break;
215213
default:
@@ -470,8 +468,7 @@ EXPORT_SYMBOL_NS_GPL(ad5679r_chip_info, "IIO_AD5686");
470468

471469
int ad5686_probe(struct device *dev,
472470
const struct ad5686_chip_info *chip_info,
473-
const char *name, ad5686_write_func write,
474-
ad5686_read_func read)
471+
const char *name, const struct ad5686_bus_ops *ops)
475472
{
476473
struct ad5686_state *st;
477474
struct iio_dev *indio_dev;
@@ -484,8 +481,7 @@ int ad5686_probe(struct device *dev,
484481
st = iio_priv(indio_dev);
485482

486483
st->dev = dev;
487-
st->write = write;
488-
st->read = read;
484+
st->ops = ops;
489485
st->chip_info = chip_info;
490486

491487
ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
@@ -529,8 +525,8 @@ int ad5686_probe(struct device *dev,
529525
return ret;
530526
break;
531527
case AD5686_REGMAP:
532-
ret = st->write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
533-
st->use_internal_vref ? 0 : AD5686_REF_BIT_MSK);
528+
ret = ad5686_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
529+
st->use_internal_vref ? 0 : AD5686_REF_BIT_MSK);
534530
if (ret)
535531
return ret;
536532
break;

drivers/iio/dac/ad5686.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,15 @@ enum ad5686_regmap_type {
6262

6363
struct ad5686_state;
6464

65-
typedef int (*ad5686_write_func)(struct ad5686_state *st,
66-
u8 cmd, u8 addr, u16 val);
67-
68-
typedef int (*ad5686_read_func)(struct ad5686_state *st, u8 addr);
65+
/**
66+
* struct ad5686_bus_ops - bus specific read/write operations
67+
* @read: read a register value at the given address
68+
* @write: write a command, address and value to the device
69+
*/
70+
struct ad5686_bus_ops {
71+
int (*read)(struct ad5686_state *st, u8 addr);
72+
int (*write)(struct ad5686_state *st, u8 cmd, u8 addr, u16 val);
73+
};
6974

7075
/**
7176
* struct ad5686_chip_info - chip specific information
@@ -113,6 +118,7 @@ extern const struct ad5686_chip_info ad5679r_chip_info;
113118
* struct ad5686_state - driver instance specific data
114119
* @dev: device instance
115120
* @chip_info: chip model specific constants, available modes etc
121+
* @ops: bus specific operations
116122
* @vref_mv: actual reference voltage used
117123
* @pwr_down_mask: power down mask
118124
* @pwr_down_mode: current power down mode
@@ -124,11 +130,10 @@ extern const struct ad5686_chip_info ad5679r_chip_info;
124130
struct ad5686_state {
125131
struct device *dev;
126132
const struct ad5686_chip_info *chip_info;
133+
const struct ad5686_bus_ops *ops;
127134
unsigned short vref_mv;
128135
unsigned int pwr_down_mask;
129136
unsigned int pwr_down_mode;
130-
ad5686_write_func write;
131-
ad5686_read_func read;
132137
bool use_internal_vref;
133138
struct mutex lock;
134139

@@ -147,8 +152,16 @@ struct ad5686_state {
147152

148153
int ad5686_probe(struct device *dev,
149154
const struct ad5686_chip_info *chip_info,
150-
const char *name, ad5686_write_func write,
151-
ad5686_read_func read);
155+
const char *name, const struct ad5686_bus_ops *ops);
156+
157+
static inline int ad5686_write(struct ad5686_state *st, u8 cmd, u8 addr, u16 val)
158+
{
159+
return st->ops->write(st, cmd, addr, val);
160+
}
152161

162+
static inline int ad5686_read(struct ad5686_state *st, u8 addr)
163+
{
164+
return st->ops->read(st, addr);
165+
}
153166

154167
#endif /* __DRIVERS_IIO_DAC_AD5686_H__ */

drivers/iio/dac/ad5696-i2c.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,15 @@ static int ad5686_i2c_write(struct ad5686_state *st,
6262
return (ret != 3) ? -EIO : 0;
6363
}
6464

65+
static const struct ad5686_bus_ops ad5686_i2c_ops = {
66+
.write = ad5686_i2c_write,
67+
.read = ad5686_i2c_read,
68+
};
69+
6570
static int ad5686_i2c_probe(struct i2c_client *i2c)
6671
{
6772
return ad5686_probe(&i2c->dev, i2c_get_match_data(i2c),
68-
i2c->name, ad5686_i2c_write, ad5686_i2c_read);
73+
i2c->name, &ad5686_i2c_ops);
6974
}
7075

7176
static const struct i2c_device_id ad5686_i2c_id[] = {

0 commit comments

Comments
 (0)