20
20
#define AD7294_REG_CMD 0x00
21
21
#define AD7294_REG_RESULT 0x01
22
22
#define AD7294_REG_TEMP_BASE 0x02
23
+ #define AD7294_REG_CURRENT_BASE 0x04
23
24
#define AD7294_REG_DAC (x ) ((x) + 0x01)
24
25
#define AD7294_VOLTAGE_STATUS 0x05
25
26
#define AD7294_CURRENT_STATUS 0x06
@@ -55,7 +56,8 @@ static const struct iio_event_spec ad7294_events[] = {
55
56
{
56
57
.type = IIO_EV_TYPE_THRESH ,
57
58
.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 ),
59
61
},
60
62
{
61
63
.type = IIO_EV_TYPE_THRESH ,
@@ -73,7 +75,7 @@ static const struct iio_event_spec ad7294_events[] = {
73
75
.output = 1, \
74
76
}
75
77
76
- #define AD7294_ADC_CHAN (_type , _chan_id ) { \
78
+ #define AD7294_VOLTAGE_CHAN (_type , _chan_id ) { \
77
79
.type = _type, \
78
80
.channel = _chan_id, \
79
81
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
@@ -84,6 +86,17 @@ static const struct iio_event_spec ad7294_events[] = {
84
86
.num_event_specs = ARRAY_SIZE(ad7294_events), \
85
87
}
86
88
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
+
87
100
#define AD7294_TEMP_CHAN (_chan_id ) { \
88
101
.type = IIO_TEMP, \
89
102
.channel = _chan_id, \
@@ -131,6 +144,7 @@ struct ad7294_state {
131
144
struct i2c_client * i2c ;
132
145
struct regulator * adc_vref_reg ;
133
146
struct regulator * dac_vref_reg ;
147
+ u32 shunt_ohms [2 ];
134
148
u16 dac_value [2 ];
135
149
};
136
150
@@ -151,7 +165,7 @@ static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val)
151
165
return ret ;
152
166
153
167
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 );
155
169
if (reg_size == 1 ) {
156
170
* val = buffer [1 ];
157
171
} else {
@@ -169,7 +183,7 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val)
169
183
170
184
int reg_size = ad7294_reg_size (reg );
171
185
dev_dbg (& client -> dev , "Write [%x] to reg: %x, size: %d" , val , reg ,
172
- reg_size );
186
+ reg_size );
173
187
174
188
if (reg_size == 1 ) {
175
189
/* 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[] = {
200
214
AD7294_DAC_CHAN (1 ),
201
215
AD7294_DAC_CHAN (2 ),
202
216
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 ),
209
223
AD7294_TEMP_CHAN (TSENSE_1 ),
210
224
AD7294_TEMP_CHAN (TSENSE_2 ),
211
225
AD7294_TEMP_CHAN (TSENSE_INTERNAL ),
@@ -266,7 +280,8 @@ static irqreturn_t ad7294_event_handler(int irq, void *private)
266
280
timestamp );
267
281
}
268
282
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 ) {
270
285
if (i & 1 )
271
286
iio_push_event (indio_dev ,
272
287
IIO_UNMOD_EVENT_CODE (IIO_CURRENT , i >> 1 ,
@@ -289,14 +304,15 @@ static int ad7294_read_raw(struct iio_dev *indio_dev,
289
304
int * val2 , long mask )
290
305
{
291
306
struct ad7294_state * st = iio_priv (indio_dev );
292
- int ret ;
307
+ int ret , base = 0 ;
293
308
unsigned int regval ;
294
309
295
310
guard (mutex )(& st -> lock );
296
311
switch (mask ) {
297
312
case IIO_CHAN_INFO_RAW :
298
313
switch (chan -> type ) {
299
314
case IIO_CURRENT :
315
+ base = AD7294_VOLTAGE_CHENNEL_COUNT ;
300
316
goto adc_read ;
301
317
case IIO_VOLTAGE :
302
318
if (chan -> output ) {
@@ -305,7 +321,7 @@ static int ad7294_read_raw(struct iio_dev *indio_dev,
305
321
}
306
322
adc_read :
307
323
ret = regmap_write (st -> regmap , AD7294_REG_CMD ,
308
- BIT (chan -> channel ));
324
+ BIT (chan -> channel + base ));
309
325
if (ret )
310
326
return ret ;
311
327
ret = regmap_read (st -> regmap , AD7294_REG_RESULT ,
@@ -357,7 +373,13 @@ static int ad7294_read_raw(struct iio_dev *indio_dev,
357
373
* val = 250 ;
358
374
return IIO_VAL_INT ;
359
375
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 ;
361
383
default :
362
384
return - EINVAL ;
363
385
}
@@ -550,6 +572,15 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
550
572
pwdn_config |= AD7294_DAC_EXTERNAL_REF_MASK ;
551
573
}
552
574
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
+
553
584
if (i2c -> irq > 0 ) {
554
585
ret = devm_request_threaded_irq (& i2c -> dev , i2c -> irq , NULL ,
555
586
ad7294_event_handler ,
0 commit comments