iio: adc: stm32-adc: add support of generic channels binding

Add support of generic IIO channel binding:
./devicetree/bindings/iio/adc/adc.yaml
Keep support of st,adc-channels and st,adc-diff-channels
for backward compatibility.

Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
Reviewed-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
Link: https://lore.kernel.org/r/20211014131228.4692-5-olivier.moysan@foss.st.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Olivier Moysan 2021-10-14 15:12:25 +02:00 committed by Jonathan Cameron
parent 45571a361c
commit 95bc818404
1 changed files with 86 additions and 9 deletions

View File

@ -35,7 +35,7 @@
#define STM32H7_BOOST_CLKRATE 20000000UL
#define STM32_ADC_CH_MAX 20 /* max number of channels */
#define STM32_ADC_CH_SZ 10 /* max channel name size */
#define STM32_ADC_CH_SZ 16 /* max channel name size */
#define STM32_ADC_MAX_SQ 16 /* SQ1..SQ16 */
#define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */
#define STM32_ADC_TIMEOUT_US 100000
@ -1792,6 +1792,73 @@ static int stm32_adc_legacy_chan_init(struct iio_dev *indio_dev,
return scan_index;
}
static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev,
struct stm32_adc *adc,
struct iio_chan_spec *channels)
{
struct device_node *node = indio_dev->dev.of_node;
const struct stm32_adc_info *adc_info = adc->cfg->adc_info;
struct device_node *child;
const char *name;
int val, scan_index = 0, ret;
bool differential;
u32 vin[2];
for_each_available_child_of_node(node, child) {
ret = of_property_read_u32(child, "reg", &val);
if (ret) {
dev_err(&indio_dev->dev, "Missing channel index %d\n", ret);
goto err;
}
ret = of_property_read_string(child, "label", &name);
/* label is optional */
if (!ret) {
if (strlen(name) >= STM32_ADC_CH_SZ) {
dev_err(&indio_dev->dev, "Label %s exceeds %d characters\n",
name, STM32_ADC_CH_SZ);
return -EINVAL;
}
strncpy(adc->chan_name[val], name, STM32_ADC_CH_SZ);
} else if (ret != -EINVAL) {
dev_err(&indio_dev->dev, "Invalid label %d\n", ret);
goto err;
}
if (val >= adc_info->max_channels) {
dev_err(&indio_dev->dev, "Invalid channel %d\n", val);
ret = -EINVAL;
goto err;
}
differential = false;
ret = of_property_read_u32_array(child, "diff-channels", vin, 2);
/* diff-channels is optional */
if (!ret) {
differential = true;
if (vin[0] != val || vin[1] >= adc_info->max_channels) {
dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n",
vin[0], vin[1]);
goto err;
}
} else if (ret != -EINVAL) {
dev_err(&indio_dev->dev, "Invalid diff-channels property %d\n", ret);
goto err;
}
stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val,
vin[1], scan_index, differential);
scan_index++;
}
return scan_index;
err:
of_node_put(child);
return ret;
}
static int stm32_adc_chan_of_init(struct iio_dev *indio_dev, bool timestamping)
{
struct device_node *node = indio_dev->dev.of_node;
@ -1800,15 +1867,22 @@ static int stm32_adc_chan_of_init(struct iio_dev *indio_dev, bool timestamping)
struct iio_chan_spec *channels;
int scan_index = 0, num_channels = 0, ret, i;
u32 smp = 0;
bool legacy = false;
ret = stm32_adc_get_legacy_chan_count(indio_dev, adc);
if (ret < 0)
return ret;
num_channels = ret;
num_channels = of_get_available_child_count(node);
/* If no channels have been found, fallback to channels legacy properties. */
if (!num_channels) {
dev_err(&indio_dev->dev, "No channels configured\n");
return -ENODATA;
legacy = true;
ret = stm32_adc_get_legacy_chan_count(indio_dev, adc);
if (!ret) {
dev_err(indio_dev->dev.parent, "No channel found\n");
return -ENODATA;
} else if (ret < 0) {
return ret;
}
num_channels = ret;
}
if (num_channels > adc_info->max_channels) {
@ -1832,7 +1906,10 @@ static int stm32_adc_chan_of_init(struct iio_dev *indio_dev, bool timestamping)
if (!channels)
return -ENOMEM;
ret = stm32_adc_legacy_chan_init(indio_dev, adc, channels);
if (legacy)
ret = stm32_adc_legacy_chan_init(indio_dev, adc, channels);
else
ret = stm32_adc_generic_chan_init(indio_dev, adc, channels);
if (ret < 0)
return ret;
scan_index = ret;