Skip to content

Commit e85a7df

Browse files
committed
iio: addac: ad7294: code refactor
1 parent d771574 commit e85a7df

File tree

1 file changed

+97
-98
lines changed

1 file changed

+97
-98
lines changed

drivers/iio/addac/ad7294.c

+97-98
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
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
2423
#define AD7294_REG_DAC(x) ((x) + 0x01)
2524
#define AD7294_VOLTAGE_STATUS 0x05
2625
#define AD7294_CURRENT_STATUS 0x06
@@ -38,90 +37,22 @@
3837
#define AD7294_ADC_EXTERNAL_REF_MASK BIT(5)
3938
#define AD7294_DAC_EXTERNAL_REF_MASK BIT(4)
4039
#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)
4142

4243
#define AD7294_ADC_INTERNAL_VREF_MV 2500
4344
#define AD7294_DAC_INTERNAL_VREF_MV 2500
4445
#define AD7294_RESOLUTION 12
4546
#define AD7294_VOLTAGE_CHENNEL_COUNT 4
4647

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];
12556
};
12657

12758
static int ad7294_reg_size(unsigned int reg)
@@ -138,22 +69,11 @@ static int ad7294_reg_size(unsigned int reg)
13869
}
13970
};
14071

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-
15172
static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val)
15273
{
15374
int ret;
15475
struct i2c_client *client = context;
15576
unsigned char buffer[3] = { reg };
156-
15777
int reg_size = ad7294_reg_size(reg);
15878

15979
ret = i2c_master_send(client, buffer, 1);
@@ -171,7 +91,6 @@ static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val)
17191
} else {
17292
*val = buffer[1] << 8 | buffer[2];
17393
}
174-
17594
return 0;
17695
};
17796

@@ -180,8 +99,8 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val)
18099
int ret;
181100
struct i2c_client *client = context;
182101
unsigned char buffer[3] = { reg };
183-
184102
int reg_size = ad7294_reg_size(reg);
103+
185104
dev_dbg(&client->dev, "Write [%x] to reg: %x, size: %d", val, reg,
186105
reg_size);
187106

@@ -192,14 +111,18 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val)
192111
buffer[1] = val >> 8;
193112
buffer[2] = val & 0xff;
194113
}
195-
196114
ret = i2c_master_send(client, buffer, reg_size + 1);
197115
if (ret < 0)
198116
return ret;
199117

200118
return 0;
201119
};
202120

121+
static bool ad7294_readable_reg(struct device *dev, unsigned int reg)
122+
{
123+
return reg != AD7294_REG_CMD;
124+
};
125+
203126
static const struct regmap_config ad7294_regmap_config = {
204127
.reg_bits = 8,
205128
.val_bits = 16,
@@ -209,6 +132,74 @@ static const struct regmap_config ad7294_regmap_config = {
209132
.readable_reg = ad7294_readable_reg,
210133
};
211134

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+
212203
static const struct iio_chan_spec ad7294_chan_spec[] = {
213204
AD7294_DAC_CHAN(0),
214205
AD7294_DAC_CHAN(1),
@@ -225,11 +216,15 @@ static const struct iio_chan_spec ad7294_chan_spec[] = {
225216
AD7294_TEMP_CHAN(TSENSE_INTERNAL),
226217
};
227218

219+
static const char *const ad7294_power_supplies[] = {
220+
"vdrive",
221+
"avdd",
222+
};
223+
228224
static irqreturn_t ad7294_event_handler(int irq, void *private)
229225
{
230226
int i;
231227
struct iio_dev *indio_dev = private;
232-
dev_info(&indio_dev->dev, "IRQ requested\n");
233228
struct ad7294_state *st = iio_priv(indio_dev);
234229
unsigned int voltage_status, temp_status, current_status;
235230
s64 timestamp = iio_get_time_ns(indio_dev);
@@ -248,8 +243,6 @@ static irqreturn_t ad7294_event_handler(int irq, void *private)
248243
if (!(voltage_status || current_status || temp_status))
249244
return IRQ_HANDLED;
250245

251-
dev_info(&indio_dev->dev, "Alert received: V: %x, C: %x, T: %x\n",
252-
voltage_status, current_status, temp_status);
253246
for (i = 0; i < AD7294_VOLTAGE_CHENNEL_COUNT; i++) {
254247
if (voltage_status & AD7294_ALERT_LOW(i))
255248
iio_push_event(indio_dev,
@@ -537,6 +530,7 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
537530
ret = PTR_ERR(st->adc_vref_reg);
538531
if (ret != -ENODEV)
539532
return ret;
533+
540534
dev_info(&i2c->dev,
541535
"ADC Vref not found, using internal reference");
542536
pwdn_config &= ~AD7294_ADC_EXTERNAL_REF_MASK;
@@ -545,10 +539,12 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
545539
ret = regulator_enable(st->adc_vref_reg);
546540
if (ret)
547541
return ret;
542+
548543
ret = devm_add_action_or_reset(&i2c->dev, ad7294_reg_disable,
549544
st->adc_vref_reg);
550545
if (ret)
551546
return ret;
547+
552548
pwdn_config |= AD7294_ADC_EXTERNAL_REF_MASK;
553549
}
554550

@@ -557,6 +553,7 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
557553
ret = PTR_ERR(st->dac_vref_reg);
558554
if (ret != -ENODEV)
559555
return ret;
556+
560557
dev_info(&i2c->dev,
561558
"DAC Vref not found, using internal reference");
562559
pwdn_config &= ~AD7294_DAC_EXTERNAL_REF_MASK;
@@ -565,10 +562,12 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
565562
ret = regulator_enable(st->dac_vref_reg);
566563
if (ret)
567564
return ret;
565+
568566
ret = devm_add_action_or_reset(&i2c->dev, ad7294_reg_disable,
569567
st->dac_vref_reg);
570568
if (ret)
571569
return ret;
570+
572571
pwdn_config |= AD7294_DAC_EXTERNAL_REF_MASK;
573572
}
574573

@@ -578,8 +577,6 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st)
578577
dev_err(&i2c->dev, "Failed to read shunt resistor values");
579578
return ret;
580579
}
581-
dev_dbg(&i2c->dev, "Read shunt resistor values of %d and %d uohms",
582-
st->shunt_ohms[0], st->shunt_ohms[1]);
583580

584581
if (i2c->irq > 0) {
585582
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)
588585
"ad7294", indio_dev);
589586
if (ret)
590587
return ret;
588+
591589
config_reg |= AD7294_ALERT_PIN;
592590
}
593591

594592
ret = regmap_write(st->regmap, AD7294_REG_PWDN, pwdn_config);
595593
if (ret)
596594
return ret;
595+
597596
ret = regmap_write(st->regmap, AD7294_REG_CONFIG, config_reg);
598597
if (ret)
599598
return ret;

0 commit comments

Comments
 (0)