From e85a7dffb53329bd55137b3fa41929b2f64da498 Mon Sep 17 00:00:00 2001 From: Anshul Dalal Date: Thu, 8 Aug 2024 14:16:07 +0530 Subject: [PATCH] iio: addac: ad7294: code refactor --- drivers/iio/addac/ad7294.c | 195 ++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 98 deletions(-) diff --git a/drivers/iio/addac/ad7294.c b/drivers/iio/addac/ad7294.c index b623ce8c95b748..2760ce25ce28e7 100644 --- a/drivers/iio/addac/ad7294.c +++ b/drivers/iio/addac/ad7294.c @@ -20,7 +20,6 @@ #define AD7294_REG_CMD 0x00 #define AD7294_REG_RESULT 0x01 #define AD7294_REG_TEMP_BASE 0x02 -#define AD7294_REG_CURRENT_BASE 0x04 #define AD7294_REG_DAC(x) ((x) + 0x01) #define AD7294_VOLTAGE_STATUS 0x05 #define AD7294_CURRENT_STATUS 0x06 @@ -38,90 +37,22 @@ #define AD7294_ADC_EXTERNAL_REF_MASK BIT(5) #define AD7294_DAC_EXTERNAL_REF_MASK BIT(4) #define AD7294_ALERT_PIN BIT(2) +#define AD7294_ALERT_LOW(x) BIT((x) * 2) +#define AD7294_ALERT_HIGH(x) BIT((x) * 2 + 1) #define AD7294_ADC_INTERNAL_VREF_MV 2500 #define AD7294_DAC_INTERNAL_VREF_MV 2500 #define AD7294_RESOLUTION 12 #define AD7294_VOLTAGE_CHENNEL_COUNT 4 -#define AD7294_ALERT_LOW(x) BIT((x) * 2) -#define AD7294_ALERT_HIGH(x) BIT((x) * 2 + 1) - -static const struct iio_event_spec ad7294_events[] = { - { - .type = IIO_EV_TYPE_THRESH, - .dir = IIO_EV_DIR_RISING, - .mask_separate = BIT(IIO_EV_INFO_VALUE), - }, - { - .type = IIO_EV_TYPE_THRESH, - .dir = IIO_EV_DIR_FALLING, - .mask_separate = BIT(IIO_EV_INFO_VALUE) | - BIT(IIO_EV_INFO_ENABLE), - }, - { - .type = IIO_EV_TYPE_THRESH, - .dir = IIO_EV_DIR_EITHER, - .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), - }, -}; - -#define AD7294_DAC_CHAN(_chan_id) { \ - .type = IIO_VOLTAGE, \ - .channel = _chan_id, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ - .indexed = 1, \ - .output = 1, \ -} - -#define AD7294_VOLTAGE_CHAN(_type, _chan_id) { \ - .type = _type, \ - .channel = _chan_id, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ - .indexed = 1, \ - .output = 0, \ - .event_spec = ad7294_events, \ - .num_event_specs = ARRAY_SIZE(ad7294_events), \ -} - -#define AD7294_CURRENT_CHAN(_type, _chan_id) { \ - .type = _type, \ - .channel = _chan_id, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \ - | BIT(IIO_CHAN_INFO_SCALE), \ - .indexed = 1, \ - .output = 0, \ - .event_spec = ad7294_events, \ - .num_event_specs = ARRAY_SIZE(ad7294_events), \ -} - -#define AD7294_TEMP_CHAN(_chan_id) { \ - .type = IIO_TEMP, \ - .channel = _chan_id, \ - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ - .indexed = 1, \ - .output = 0, \ - .event_spec = ad7294_events, \ - .num_event_specs = ARRAY_SIZE(ad7294_events), \ -} - -enum ad7294_temp_chan { - TSENSE_1, - TSENSE_2, - TSENSE_INTERNAL, -}; - -static const char *const ad7294_power_supplies[] = { - "vdrive", - "avdd", -}; - -static bool ad7294_readable_reg(struct device *dev, unsigned int reg) -{ - return reg != AD7294_REG_CMD; +struct ad7294_state { + struct mutex lock; + struct regmap *regmap; + struct i2c_client *i2c; + struct regulator *adc_vref_reg; + struct regulator *dac_vref_reg; + u32 shunt_ohms[2]; + u16 dac_value[2]; }; static int ad7294_reg_size(unsigned int reg) @@ -138,22 +69,11 @@ static int ad7294_reg_size(unsigned int reg) } }; -struct ad7294_state { - struct mutex lock; - struct regmap *regmap; - struct i2c_client *i2c; - struct regulator *adc_vref_reg; - struct regulator *dac_vref_reg; - u32 shunt_ohms[2]; - u16 dac_value[2]; -}; - static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val) { int ret; struct i2c_client *client = context; unsigned char buffer[3] = { reg }; - int reg_size = ad7294_reg_size(reg); ret = i2c_master_send(client, buffer, 1); @@ -171,7 +91,6 @@ static int ad7294_reg_read(void *context, unsigned int reg, unsigned int *val) } else { *val = buffer[1] << 8 | buffer[2]; } - return 0; }; @@ -180,8 +99,8 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val) int ret; struct i2c_client *client = context; unsigned char buffer[3] = { reg }; - int reg_size = ad7294_reg_size(reg); + dev_dbg(&client->dev, "Write [%x] to reg: %x, size: %d", val, reg, reg_size); @@ -192,7 +111,6 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val) buffer[1] = val >> 8; buffer[2] = val & 0xff; } - ret = i2c_master_send(client, buffer, reg_size + 1); if (ret < 0) return ret; @@ -200,6 +118,11 @@ static int ad7294_reg_write(void *context, unsigned int reg, unsigned int val) return 0; }; +static bool ad7294_readable_reg(struct device *dev, unsigned int reg) +{ + return reg != AD7294_REG_CMD; +}; + static const struct regmap_config ad7294_regmap_config = { .reg_bits = 8, .val_bits = 16, @@ -209,6 +132,74 @@ static const struct regmap_config ad7294_regmap_config = { .readable_reg = ad7294_readable_reg, }; +static const struct iio_event_spec ad7294_events[] = { + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_RISING, + .mask_separate = BIT(IIO_EV_INFO_VALUE), + }, + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_FALLING, + .mask_separate = BIT(IIO_EV_INFO_VALUE), + }, + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), + }, +}; + +// clang-format off +#define AD7294_DAC_CHAN(_chan_id) { \ + .type = IIO_VOLTAGE, \ + .channel = _chan_id, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .indexed = 1, \ + .output = 1, \ +} + +#define AD7294_VOLTAGE_CHAN(_type, _chan_id) { \ + .type = _type, \ + .channel = _chan_id, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .indexed = 1, \ + .output = 0, \ + .event_spec = ad7294_events, \ + .num_event_specs = ARRAY_SIZE(ad7294_events), \ +} + +#define AD7294_CURRENT_CHAN(_type, _chan_id) { \ + .type = _type, \ + .channel = _chan_id, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \ + | BIT(IIO_CHAN_INFO_SCALE), \ + .indexed = 1, \ + .output = 0, \ + .event_spec = ad7294_events, \ + .num_event_specs = ARRAY_SIZE(ad7294_events), \ +} + +#define AD7294_TEMP_CHAN(_chan_id) { \ + .type = IIO_TEMP, \ + .channel = _chan_id, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .indexed = 1, \ + .output = 0, \ + .event_spec = ad7294_events, \ + .num_event_specs = ARRAY_SIZE(ad7294_events), \ +} +// clang-format on + +enum ad7294_temp_chan { + TSENSE_1, + TSENSE_2, + TSENSE_INTERNAL, +}; + static const struct iio_chan_spec ad7294_chan_spec[] = { AD7294_DAC_CHAN(0), AD7294_DAC_CHAN(1), @@ -225,11 +216,15 @@ static const struct iio_chan_spec ad7294_chan_spec[] = { AD7294_TEMP_CHAN(TSENSE_INTERNAL), }; +static const char *const ad7294_power_supplies[] = { + "vdrive", + "avdd", +}; + static irqreturn_t ad7294_event_handler(int irq, void *private) { int i; struct iio_dev *indio_dev = private; - dev_info(&indio_dev->dev, "IRQ requested\n"); struct ad7294_state *st = iio_priv(indio_dev); unsigned int voltage_status, temp_status, current_status; s64 timestamp = iio_get_time_ns(indio_dev); @@ -248,8 +243,6 @@ static irqreturn_t ad7294_event_handler(int irq, void *private) if (!(voltage_status || current_status || temp_status)) return IRQ_HANDLED; - dev_info(&indio_dev->dev, "Alert received: V: %x, C: %x, T: %x\n", - voltage_status, current_status, temp_status); for (i = 0; i < AD7294_VOLTAGE_CHENNEL_COUNT; i++) { if (voltage_status & AD7294_ALERT_LOW(i)) iio_push_event(indio_dev, @@ -537,6 +530,7 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st) ret = PTR_ERR(st->adc_vref_reg); if (ret != -ENODEV) return ret; + dev_info(&i2c->dev, "ADC Vref not found, using internal reference"); pwdn_config &= ~AD7294_ADC_EXTERNAL_REF_MASK; @@ -545,10 +539,12 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st) ret = regulator_enable(st->adc_vref_reg); if (ret) return ret; + ret = devm_add_action_or_reset(&i2c->dev, ad7294_reg_disable, st->adc_vref_reg); if (ret) return ret; + pwdn_config |= AD7294_ADC_EXTERNAL_REF_MASK; } @@ -557,6 +553,7 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st) ret = PTR_ERR(st->dac_vref_reg); if (ret != -ENODEV) return ret; + dev_info(&i2c->dev, "DAC Vref not found, using internal reference"); pwdn_config &= ~AD7294_DAC_EXTERNAL_REF_MASK; @@ -565,10 +562,12 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st) ret = regulator_enable(st->dac_vref_reg); if (ret) return ret; + ret = devm_add_action_or_reset(&i2c->dev, ad7294_reg_disable, st->dac_vref_reg); if (ret) return ret; + pwdn_config |= AD7294_DAC_EXTERNAL_REF_MASK; } @@ -578,8 +577,6 @@ static int ad7294_init(struct iio_dev *indio_dev, struct ad7294_state *st) dev_err(&i2c->dev, "Failed to read shunt resistor values"); return ret; } - dev_dbg(&i2c->dev, "Read shunt resistor values of %d and %d uohms", - st->shunt_ohms[0], st->shunt_ohms[1]); if (i2c->irq > 0) { 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) "ad7294", indio_dev); if (ret) return ret; + config_reg |= AD7294_ALERT_PIN; } ret = regmap_write(st->regmap, AD7294_REG_PWDN, pwdn_config); if (ret) return ret; + ret = regmap_write(st->regmap, AD7294_REG_CONFIG, config_reg); if (ret) return ret;