Skip to content

Commit 6904726

Browse files
committed
iio: addac: ad7294: add current scale
1 parent a62a9d8 commit 6904726

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

arch/arm/boot/dts/overlays/rpi-ad7294-overlay.dts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
reg = <0x62>;
5757
interrupts = <4 IRQ_TYPE_EDGE_FALLING>;
5858
interrupt-parent = <&gpio>;
59+
shunt-resistor-ohms = <1000 1000>;
5960
adc-vref-supply = <&adc_vref>;
6061
dac-vref-supply = <&dac_vref>;
6162
avdd-supply = <&avdd>;

drivers/iio/addac/ad7294.c

+45-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define AD7294_REG_CMD 0x00
2121
#define AD7294_REG_RESULT 0x01
2222
#define AD7294_REG_TEMP_BASE 0x02
23+
#define AD7294_REG_CURRENT_BASE 0x04
2324
#define AD7294_REG_DAC(x) ((x) + 0x01)
2425
#define AD7294_VOLTAGE_STATUS 0x05
2526
#define AD7294_CURRENT_STATUS 0x06
@@ -55,7 +56,8 @@ static const struct iio_event_spec ad7294_events[] = {
5556
{
5657
.type = IIO_EV_TYPE_THRESH,
5758
.dir = IIO_EV_DIR_FALLING,
58-
.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
59+
.mask_separate = BIT(IIO_EV_INFO_VALUE) |
60+
BIT(IIO_EV_INFO_ENABLE),
5961
},
6062
{
6163
.type = IIO_EV_TYPE_THRESH,
@@ -73,7 +75,7 @@ static const struct iio_event_spec ad7294_events[] = {
7375
.output = 1, \
7476
}
7577

76-
#define AD7294_ADC_CHAN(_type, _chan_id) { \
78+
#define AD7294_VOLTAGE_CHAN(_type, _chan_id) { \
7779
.type = _type, \
7880
.channel = _chan_id, \
7981
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
@@ -84,6 +86,17 @@ static const struct iio_event_spec ad7294_events[] = {
8486
.num_event_specs = ARRAY_SIZE(ad7294_events), \
8587
}
8688

89+
#define AD7294_CURRENT_CHAN(_type, _chan_id) { \
90+
.type = _type, \
91+
.channel = _chan_id, \
92+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
93+
| BIT(IIO_CHAN_INFO_SCALE), \
94+
.indexed = 1, \
95+
.output = 0, \
96+
.event_spec = ad7294_events, \
97+
.num_event_specs = ARRAY_SIZE(ad7294_events), \
98+
}
99+
87100
#define AD7294_TEMP_CHAN(_chan_id) { \
88101
.type = IIO_TEMP, \
89102
.channel = _chan_id, \
@@ -131,6 +144,7 @@ struct ad7294_state {
131144
struct i2c_client *i2c;
132145
struct regulator *adc_vref_reg;
133146
struct regulator *dac_vref_reg;
147+
u32 shunt_ohms[2];
134148
u16 dac_value[2];
135149
};
136150

@@ -151,7 +165,7 @@ static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val)
151165
return ret;
152166

153167
dev_dbg(&client->dev, "Read [%x, %x] from reg:0x%x, size: %d",
154-
buffer[1], buffer[2], reg, reg_size);
168+
buffer[1], buffer[2], reg, reg_size);
155169
if (reg_size == 1) {
156170
*val = buffer[1];
157171
} else {
@@ -169,7 +183,7 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val)
169183

170184
int reg_size = ad7294_reg_size(reg);
171185
dev_dbg(&client->dev, "Write [%x] to reg: %x, size: %d", val, reg,
172-
reg_size);
186+
reg_size);
173187

174188
if (reg_size == 1) {
175189
/* Only take LSB of the data when writing to 1 byte reg */
@@ -200,12 +214,12 @@ static const struct iio_chan_spec ad7294_chan_spec[] = {
200214
AD7294_DAC_CHAN(1),
201215
AD7294_DAC_CHAN(2),
202216
AD7294_DAC_CHAN(3),
203-
AD7294_ADC_CHAN(IIO_VOLTAGE, 0),
204-
AD7294_ADC_CHAN(IIO_VOLTAGE, 1),
205-
AD7294_ADC_CHAN(IIO_VOLTAGE, 2),
206-
AD7294_ADC_CHAN(IIO_VOLTAGE, 3),
207-
AD7294_ADC_CHAN(IIO_CURRENT, 4),
208-
AD7294_ADC_CHAN(IIO_CURRENT, 5),
217+
AD7294_VOLTAGE_CHAN(IIO_VOLTAGE, 0),
218+
AD7294_VOLTAGE_CHAN(IIO_VOLTAGE, 1),
219+
AD7294_VOLTAGE_CHAN(IIO_VOLTAGE, 2),
220+
AD7294_VOLTAGE_CHAN(IIO_VOLTAGE, 3),
221+
AD7294_CURRENT_CHAN(IIO_CURRENT, 0),
222+
AD7294_CURRENT_CHAN(IIO_CURRENT, 1),
209223
AD7294_TEMP_CHAN(TSENSE_1),
210224
AD7294_TEMP_CHAN(TSENSE_2),
211225
AD7294_TEMP_CHAN(TSENSE_INTERNAL),
@@ -266,7 +280,8 @@ static irqreturn_t ad7294_event_handler(int irq, void *private)
266280
timestamp);
267281
}
268282

269-
for_each_set_bit(i, (long *)&current_status, AD7294_CURRENT_ALERT_MASK) {
283+
for_each_set_bit(i, (long *)&current_status,
284+
AD7294_CURRENT_ALERT_MASK) {
270285
if (i & 1)
271286
iio_push_event(indio_dev,
272287
IIO_UNMOD_EVENT_CODE(IIO_CURRENT, i >> 1,
@@ -289,14 +304,15 @@ static int ad7294_read_raw(struct iio_dev *indio_dev,
289304
int *val2, long mask)
290305
{
291306
struct ad7294_state *st = iio_priv(indio_dev);
292-
int ret;
307+
int ret, base = 0;
293308
unsigned int regval;
294309

295310
guard(mutex)(&st->lock);
296311
switch (mask) {
297312
case IIO_CHAN_INFO_RAW:
298313
switch (chan->type) {
299314
case IIO_CURRENT:
315+
base = AD7294_VOLTAGE_CHENNEL_COUNT;
300316
goto adc_read;
301317
case IIO_VOLTAGE:
302318
if (chan->output) {
@@ -305,7 +321,7 @@ static int ad7294_read_raw(struct iio_dev *indio_dev,
305321
}
306322
adc_read:
307323
ret = regmap_write(st->regmap, AD7294_REG_CMD,
308-
BIT(chan->channel));
324+
BIT(chan->channel + base));
309325
if (ret)
310326
return ret;
311327
ret = regmap_read(st->regmap, AD7294_REG_RESULT,
@@ -357,7 +373,13 @@ static int ad7294_read_raw(struct iio_dev *indio_dev,
357373
*val = 250;
358374
return IIO_VAL_INT;
359375
case IIO_CURRENT:
360-
/* TODO */
376+
/*
377+
Current(in mA) =
378+
ADC_READING * 100 / Shunt resistance (in ohm)
379+
*/
380+
*val = 100;
381+
*val2 = st->shunt_ohms[chan->channel & 1];
382+
return IIO_VAL_FRACTIONAL;
361383
default:
362384
return -EINVAL;
363385
}
@@ -550,6 +572,15 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
550572
pwdn_config |= AD7294_DAC_EXTERNAL_REF_MASK;
551573
}
552574

575+
ret = device_property_read_u32_array(&i2c->dev, "shunt-resistor-ohms",
576+
st->shunt_ohms, 2);
577+
if (ret) {
578+
dev_err(&i2c->dev, "Failed to read shunt resistor values");
579+
return ret;
580+
}
581+
dev_dbg(&i2c->dev, "Read shunt resistor values of %d and %d uohms",
582+
st->shunt_ohms[0], st->shunt_ohms[1]);
583+
553584
if (i2c->irq > 0) {
554585
ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
555586
ad7294_event_handler,

0 commit comments

Comments
 (0)