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
24
23
#define AD7294_REG_DAC (x ) ((x) + 0x01)
25
24
#define AD7294_VOLTAGE_STATUS 0x05
26
25
#define AD7294_CURRENT_STATUS 0x06
38
37
#define AD7294_ADC_EXTERNAL_REF_MASK BIT(5)
39
38
#define AD7294_DAC_EXTERNAL_REF_MASK BIT(4)
40
39
#define AD7294_ALERT_PIN BIT(2)
40
+ #define AD7294_ALERT_LOW (x ) BIT((x) * 2)
41
+ #define AD7294_ALERT_HIGH (x ) BIT((x) * 2 + 1)
41
42
42
43
#define AD7294_ADC_INTERNAL_VREF_MV 2500
43
44
#define AD7294_DAC_INTERNAL_VREF_MV 2500
44
45
#define AD7294_RESOLUTION 12
45
46
#define AD7294_VOLTAGE_CHENNEL_COUNT 4
46
47
47
- #define AD7294_ALERT_LOW (x ) BIT((x) * 2)
48
- #define AD7294_ALERT_HIGH (x ) BIT((x) * 2 + 1)
49
-
50
- static const struct iio_event_spec ad7294_events [] = {
51
- {
52
- .type = IIO_EV_TYPE_THRESH ,
53
- .dir = IIO_EV_DIR_RISING ,
54
- .mask_separate = BIT (IIO_EV_INFO_VALUE ),
55
- },
56
- {
57
- .type = IIO_EV_TYPE_THRESH ,
58
- .dir = IIO_EV_DIR_FALLING ,
59
- .mask_separate = BIT (IIO_EV_INFO_VALUE ) |
60
- BIT (IIO_EV_INFO_ENABLE ),
61
- },
62
- {
63
- .type = IIO_EV_TYPE_THRESH ,
64
- .dir = IIO_EV_DIR_EITHER ,
65
- .mask_separate = BIT (IIO_EV_INFO_HYSTERESIS ),
66
- },
67
- };
68
-
69
- #define AD7294_DAC_CHAN (_chan_id ) { \
70
- .type = IIO_VOLTAGE, \
71
- .channel = _chan_id, \
72
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
73
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
74
- .indexed = 1, \
75
- .output = 1, \
76
- }
77
-
78
- #define AD7294_VOLTAGE_CHAN (_type , _chan_id ) { \
79
- .type = _type, \
80
- .channel = _chan_id, \
81
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
82
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
83
- .indexed = 1, \
84
- .output = 0, \
85
- .event_spec = ad7294_events, \
86
- .num_event_specs = ARRAY_SIZE(ad7294_events), \
87
- }
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
-
100
- #define AD7294_TEMP_CHAN (_chan_id ) { \
101
- .type = IIO_TEMP, \
102
- .channel = _chan_id, \
103
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
104
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
105
- .indexed = 1, \
106
- .output = 0, \
107
- .event_spec = ad7294_events, \
108
- .num_event_specs = ARRAY_SIZE(ad7294_events), \
109
- }
110
-
111
- enum ad7294_temp_chan {
112
- TSENSE_1 ,
113
- TSENSE_2 ,
114
- TSENSE_INTERNAL ,
115
- };
116
-
117
- static const char * const ad7294_power_supplies [] = {
118
- "vdrive" ,
119
- "avdd" ,
120
- };
121
-
122
- static bool ad7294_readable_reg (struct device * dev , unsigned int reg )
123
- {
124
- return reg != AD7294_REG_CMD ;
48
+ struct ad7294_state {
49
+ struct mutex lock ;
50
+ struct regmap * regmap ;
51
+ struct i2c_client * i2c ;
52
+ struct regulator * adc_vref_reg ;
53
+ struct regulator * dac_vref_reg ;
54
+ u32 shunt_ohms [2 ];
55
+ u16 dac_value [2 ];
125
56
};
126
57
127
58
static int ad7294_reg_size (unsigned int reg )
@@ -138,22 +69,11 @@ static int ad7294_reg_size(unsigned int reg)
138
69
}
139
70
};
140
71
141
- struct ad7294_state {
142
- struct mutex lock ;
143
- struct regmap * regmap ;
144
- struct i2c_client * i2c ;
145
- struct regulator * adc_vref_reg ;
146
- struct regulator * dac_vref_reg ;
147
- u32 shunt_ohms [2 ];
148
- u16 dac_value [2 ];
149
- };
150
-
151
72
static int ad7294_reg_read (void * context , unsigned int reg , unsigned int * val )
152
73
{
153
74
int ret ;
154
75
struct i2c_client * client = context ;
155
76
unsigned char buffer [3 ] = { reg };
156
-
157
77
int reg_size = ad7294_reg_size (reg );
158
78
159
79
ret = i2c_master_send (client , buffer , 1 );
@@ -171,7 +91,6 @@ static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val)
171
91
} else {
172
92
* val = buffer [1 ] << 8 | buffer [2 ];
173
93
}
174
-
175
94
return 0 ;
176
95
};
177
96
@@ -180,8 +99,8 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val)
180
99
int ret ;
181
100
struct i2c_client * client = context ;
182
101
unsigned char buffer [3 ] = { reg };
183
-
184
102
int reg_size = ad7294_reg_size (reg );
103
+
185
104
dev_dbg (& client -> dev , "Write [%x] to reg: %x, size: %d" , val , reg ,
186
105
reg_size );
187
106
@@ -192,14 +111,18 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val)
192
111
buffer [1 ] = val >> 8 ;
193
112
buffer [2 ] = val & 0xff ;
194
113
}
195
-
196
114
ret = i2c_master_send (client , buffer , reg_size + 1 );
197
115
if (ret < 0 )
198
116
return ret ;
199
117
200
118
return 0 ;
201
119
};
202
120
121
+ static bool ad7294_readable_reg (struct device * dev , unsigned int reg )
122
+ {
123
+ return reg != AD7294_REG_CMD ;
124
+ };
125
+
203
126
static const struct regmap_config ad7294_regmap_config = {
204
127
.reg_bits = 8 ,
205
128
.val_bits = 16 ,
@@ -209,6 +132,74 @@ static const struct regmap_config ad7294_regmap_config = {
209
132
.readable_reg = ad7294_readable_reg ,
210
133
};
211
134
135
+ static const struct iio_event_spec ad7294_events [] = {
136
+ {
137
+ .type = IIO_EV_TYPE_THRESH ,
138
+ .dir = IIO_EV_DIR_RISING ,
139
+ .mask_separate = BIT (IIO_EV_INFO_VALUE ),
140
+ },
141
+ {
142
+ .type = IIO_EV_TYPE_THRESH ,
143
+ .dir = IIO_EV_DIR_FALLING ,
144
+ .mask_separate = BIT (IIO_EV_INFO_VALUE ),
145
+ },
146
+ {
147
+ .type = IIO_EV_TYPE_THRESH ,
148
+ .dir = IIO_EV_DIR_EITHER ,
149
+ .mask_separate = BIT (IIO_EV_INFO_HYSTERESIS ),
150
+ },
151
+ };
152
+
153
+ // clang-format off
154
+ #define AD7294_DAC_CHAN (_chan_id ) { \
155
+ .type = IIO_VOLTAGE, \
156
+ .channel = _chan_id, \
157
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
158
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
159
+ .indexed = 1, \
160
+ .output = 1, \
161
+ }
162
+
163
+ #define AD7294_VOLTAGE_CHAN (_type , _chan_id ) { \
164
+ .type = _type, \
165
+ .channel = _chan_id, \
166
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
167
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
168
+ .indexed = 1, \
169
+ .output = 0, \
170
+ .event_spec = ad7294_events, \
171
+ .num_event_specs = ARRAY_SIZE(ad7294_events), \
172
+ }
173
+
174
+ #define AD7294_CURRENT_CHAN (_type , _chan_id ) { \
175
+ .type = _type, \
176
+ .channel = _chan_id, \
177
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
178
+ | BIT(IIO_CHAN_INFO_SCALE), \
179
+ .indexed = 1, \
180
+ .output = 0, \
181
+ .event_spec = ad7294_events, \
182
+ .num_event_specs = ARRAY_SIZE(ad7294_events), \
183
+ }
184
+
185
+ #define AD7294_TEMP_CHAN (_chan_id ) { \
186
+ .type = IIO_TEMP, \
187
+ .channel = _chan_id, \
188
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
189
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
190
+ .indexed = 1, \
191
+ .output = 0, \
192
+ .event_spec = ad7294_events, \
193
+ .num_event_specs = ARRAY_SIZE(ad7294_events), \
194
+ }
195
+ // clang-format on
196
+
197
+ enum ad7294_temp_chan {
198
+ TSENSE_1 ,
199
+ TSENSE_2 ,
200
+ TSENSE_INTERNAL ,
201
+ };
202
+
212
203
static const struct iio_chan_spec ad7294_chan_spec [] = {
213
204
AD7294_DAC_CHAN (0 ),
214
205
AD7294_DAC_CHAN (1 ),
@@ -225,11 +216,15 @@ static const struct iio_chan_spec ad7294_chan_spec[] = {
225
216
AD7294_TEMP_CHAN (TSENSE_INTERNAL ),
226
217
};
227
218
219
+ static const char * const ad7294_power_supplies [] = {
220
+ "vdrive" ,
221
+ "avdd" ,
222
+ };
223
+
228
224
static irqreturn_t ad7294_event_handler (int irq , void * private )
229
225
{
230
226
int i ;
231
227
struct iio_dev * indio_dev = private ;
232
- dev_info (& indio_dev -> dev , "IRQ requested\n" );
233
228
struct ad7294_state * st = iio_priv (indio_dev );
234
229
unsigned int voltage_status , temp_status , current_status ;
235
230
s64 timestamp = iio_get_time_ns (indio_dev );
@@ -248,8 +243,6 @@ static irqreturn_t ad7294_event_handler(int irq, void *private)
248
243
if (!(voltage_status || current_status || temp_status ))
249
244
return IRQ_HANDLED ;
250
245
251
- dev_info (& indio_dev -> dev , "Alert received: V: %x, C: %x, T: %x\n" ,
252
- voltage_status , current_status , temp_status );
253
246
for (i = 0 ; i < AD7294_VOLTAGE_CHENNEL_COUNT ; i ++ ) {
254
247
if (voltage_status & AD7294_ALERT_LOW (i ))
255
248
iio_push_event (indio_dev ,
@@ -537,6 +530,7 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
537
530
ret = PTR_ERR (st -> adc_vref_reg );
538
531
if (ret != - ENODEV )
539
532
return ret ;
533
+
540
534
dev_info (& i2c -> dev ,
541
535
"ADC Vref not found, using internal reference" );
542
536
pwdn_config &= ~AD7294_ADC_EXTERNAL_REF_MASK ;
@@ -545,10 +539,12 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
545
539
ret = regulator_enable (st -> adc_vref_reg );
546
540
if (ret )
547
541
return ret ;
542
+
548
543
ret = devm_add_action_or_reset (& i2c -> dev , ad7294_reg_disable ,
549
544
st -> adc_vref_reg );
550
545
if (ret )
551
546
return ret ;
547
+
552
548
pwdn_config |= AD7294_ADC_EXTERNAL_REF_MASK ;
553
549
}
554
550
@@ -557,6 +553,7 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
557
553
ret = PTR_ERR (st -> dac_vref_reg );
558
554
if (ret != - ENODEV )
559
555
return ret ;
556
+
560
557
dev_info (& i2c -> dev ,
561
558
"DAC Vref not found, using internal reference" );
562
559
pwdn_config &= ~AD7294_DAC_EXTERNAL_REF_MASK ;
@@ -565,10 +562,12 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
565
562
ret = regulator_enable (st -> dac_vref_reg );
566
563
if (ret )
567
564
return ret ;
565
+
568
566
ret = devm_add_action_or_reset (& i2c -> dev , ad7294_reg_disable ,
569
567
st -> dac_vref_reg );
570
568
if (ret )
571
569
return ret ;
570
+
572
571
pwdn_config |= AD7294_DAC_EXTERNAL_REF_MASK ;
573
572
}
574
573
@@ -578,8 +577,6 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
578
577
dev_err (& i2c -> dev , "Failed to read shunt resistor values" );
579
578
return ret ;
580
579
}
581
- dev_dbg (& i2c -> dev , "Read shunt resistor values of %d and %d uohms" ,
582
- st -> shunt_ohms [0 ], st -> shunt_ohms [1 ]);
583
580
584
581
if (i2c -> irq > 0 ) {
585
582
ret = devm_request_threaded_irq (& i2c -> dev , i2c -> irq , NULL ,
@@ -588,12 +585,14 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
588
585
"ad7294" , indio_dev );
589
586
if (ret )
590
587
return ret ;
588
+
591
589
config_reg |= AD7294_ALERT_PIN ;
592
590
}
593
591
594
592
ret = regmap_write (st -> regmap , AD7294_REG_PWDN , pwdn_config );
595
593
if (ret )
596
594
return ret ;
595
+
597
596
ret = regmap_write (st -> regmap , AD7294_REG_CONFIG , config_reg );
598
597
if (ret )
599
598
return ret ;
0 commit comments