Skip to content

Commit fc5cdff

Browse files
dumitruceclanjic23
authored andcommitted
iio: adc: ad7173: refactor ain and vref selection
Move validation of analog inputs and reference voltage selection to separate functions to reduce the size of the channel config parsing function and improve readability. Add defines for the number of analog inputs in a channel. Signed-off-by: Dumitru Ceclan <dumitru.ceclan@analog.com> Reviewed-by: Nuno Sa <nuno.sa@analog.com> Reviewed-by: David Lechner <dlechner@baylibre.com> Link: https://patch.msgid.link/20240607-ad4111-v7-5-97e3855900a0@analog.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 137a83a commit fc5cdff

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

drivers/iio/adc/ad7173.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define AD7173_CH_SETUP_AINPOS_MASK GENMASK(9, 5)
6161
#define AD7173_CH_SETUP_AINNEG_MASK GENMASK(4, 0)
6262

63+
#define AD7173_NO_AINS_PER_CHANNEL 2
6364
#define AD7173_CH_ADDRESS(pos, neg) \
6465
(FIELD_PREP(AD7173_CH_SETUP_AINPOS_MASK, pos) | \
6566
FIELD_PREP(AD7173_CH_SETUP_AINNEG_MASK, neg))
@@ -905,13 +906,48 @@ static int ad7173_register_clk_provider(struct iio_dev *indio_dev)
905906
&st->int_clk_hw);
906907
}
907908

909+
static int ad7173_validate_voltage_ain_inputs(struct ad7173_state *st,
910+
unsigned int ain0, unsigned int ain1)
911+
{
912+
struct device *dev = &st->sd.spi->dev;
913+
914+
if (ain0 >= st->info->num_inputs ||
915+
ain1 >= st->info->num_inputs)
916+
return dev_err_probe(dev, -EINVAL,
917+
"Input pin number out of range for pair (%d %d).\n",
918+
ain0, ain1);
919+
920+
return 0;
921+
}
922+
923+
static int ad7173_validate_reference(struct ad7173_state *st, int ref_sel)
924+
{
925+
struct device *dev = &st->sd.spi->dev;
926+
int ret;
927+
928+
if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF && !st->info->has_int_ref)
929+
return dev_err_probe(dev, -EINVAL,
930+
"Internal reference is not available on current model.\n");
931+
932+
if (ref_sel == AD7173_SETUP_REF_SEL_EXT_REF2 && !st->info->has_ref2)
933+
return dev_err_probe(dev, -EINVAL,
934+
"External reference 2 is not available on current model.\n");
935+
936+
ret = ad7173_get_ref_voltage_milli(st, ref_sel);
937+
if (ret < 0)
938+
return dev_err_probe(dev, ret, "Cannot use reference %u\n",
939+
ref_sel);
940+
941+
return 0;
942+
}
943+
908944
static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev)
909945
{
910946
struct ad7173_channel *chans_st_arr, *chan_st_priv;
911947
struct ad7173_state *st = iio_priv(indio_dev);
912948
struct device *dev = indio_dev->dev.parent;
913949
struct iio_chan_spec *chan_arr, *chan;
914-
unsigned int ain[2], chan_index = 0;
950+
unsigned int ain[AD7173_NO_AINS_PER_CHANNEL], chan_index = 0;
915951
int ref_sel, ret, num_channels;
916952

917953
num_channels = device_get_child_node_count(dev);
@@ -965,11 +1001,9 @@ static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev)
9651001
if (ret)
9661002
return ret;
9671003

968-
if (ain[0] >= st->info->num_inputs ||
969-
ain[1] >= st->info->num_inputs)
970-
return dev_err_probe(dev, -EINVAL,
971-
"Input pin number out of range for pair (%d %d).\n",
972-
ain[0], ain[1]);
1004+
ret = ad7173_validate_voltage_ain_inputs(st, ain[0], ain[1]);
1005+
if (ret)
1006+
return ret;
9731007

9741008
ret = fwnode_property_match_property_string(child,
9751009
"adi,reference-select",
@@ -980,19 +1014,9 @@ static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev)
9801014
else
9811015
ref_sel = ret;
9821016

983-
if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF &&
984-
!st->info->has_int_ref)
985-
return dev_err_probe(dev, -EINVAL,
986-
"Internal reference is not available on current model.\n");
987-
988-
if (ref_sel == AD7173_SETUP_REF_SEL_EXT_REF2 && !st->info->has_ref2)
989-
return dev_err_probe(dev, -EINVAL,
990-
"External reference 2 is not available on current model.\n");
991-
992-
ret = ad7173_get_ref_voltage_milli(st, ref_sel);
993-
if (ret < 0)
994-
return dev_err_probe(dev, ret,
995-
"Cannot use reference %u\n", ref_sel);
1017+
ret = ad7173_validate_reference(st, ref_sel);
1018+
if (ret)
1019+
return ret;
9961020

9971021
if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF)
9981022
st->adc_mode |= AD7173_ADC_MODE_REF_EN;

0 commit comments

Comments
 (0)