stm32-adc.c (3e27ef0ced49f8ae7883c25fadf76a2086e99025) | stm32-adc.c (7b0d54ae76e3c6e4f0222c5af3e3d7531f62b58e) |
---|---|
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * This file is part of STM32 ADC driver 4 * 5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved 6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>. 7 */ 8 --- 1979 unchanged lines hidden (view full) --- 1988} 1989 1990static int stm32_adc_get_legacy_chan_count(struct iio_dev *indio_dev, struct stm32_adc *adc) 1991{ 1992 struct device *dev = &indio_dev->dev; 1993 const struct stm32_adc_info *adc_info = adc->cfg->adc_info; 1994 int num_channels = 0, ret; 1995 | 1// SPDX-License-Identifier: GPL-2.0 2/* 3 * This file is part of STM32 ADC driver 4 * 5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved 6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>. 7 */ 8 --- 1979 unchanged lines hidden (view full) --- 1988} 1989 1990static int stm32_adc_get_legacy_chan_count(struct iio_dev *indio_dev, struct stm32_adc *adc) 1991{ 1992 struct device *dev = &indio_dev->dev; 1993 const struct stm32_adc_info *adc_info = adc->cfg->adc_info; 1994 int num_channels = 0, ret; 1995 |
1996 dev_dbg(&indio_dev->dev, "using legacy channel config\n"); 1997 |
|
1996 ret = device_property_count_u32(dev, "st,adc-channels"); 1997 if (ret > adc_info->max_channels) { 1998 dev_err(&indio_dev->dev, "Bad st,adc-channels?\n"); 1999 return -EINVAL; 2000 } else if (ret > 0) { 2001 num_channels += ret; 2002 } 2003 2004 /* 2005 * each st,adc-diff-channels is a group of 2 u32 so we divide @ret 2006 * to get the *real* number of channels. 2007 */ 2008 ret = device_property_count_u32(dev, "st,adc-diff-channels"); | 1998 ret = device_property_count_u32(dev, "st,adc-channels"); 1999 if (ret > adc_info->max_channels) { 2000 dev_err(&indio_dev->dev, "Bad st,adc-channels?\n"); 2001 return -EINVAL; 2002 } else if (ret > 0) { 2003 num_channels += ret; 2004 } 2005 2006 /* 2007 * each st,adc-diff-channels is a group of 2 u32 so we divide @ret 2008 * to get the *real* number of channels. 2009 */ 2010 ret = device_property_count_u32(dev, "st,adc-diff-channels"); |
2009 if (ret > 0) { 2010 ret /= (int)(sizeof(struct stm32_adc_diff_channel) / sizeof(u32)); 2011 if (ret > adc_info->max_channels) { 2012 dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n"); 2013 return -EINVAL; 2014 } else if (ret > 0) { 2015 adc->num_diff = ret; 2016 num_channels += ret; 2017 } | 2011 if (ret < 0) 2012 return ret; 2013 2014 ret /= (int)(sizeof(struct stm32_adc_diff_channel) / sizeof(u32)); 2015 if (ret > adc_info->max_channels) { 2016 dev_err(&indio_dev->dev, "Bad st,adc-diff-channels?\n"); 2017 return -EINVAL; 2018 } else if (ret > 0) { 2019 adc->num_diff = ret; 2020 num_channels += ret; |
2018 } 2019 2020 /* Optional sample time is provided either for each, or all channels */ 2021 adc->nsmps = device_property_count_u32(dev, "st,min-sample-time-nsecs"); 2022 if (adc->nsmps > 1 && adc->nsmps != num_channels) { 2023 dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n"); 2024 return -EINVAL; 2025 } --- 5 unchanged lines hidden (view full) --- 2031 struct stm32_adc *adc, 2032 struct iio_chan_spec *channels, 2033 int nchans) 2034{ 2035 const struct stm32_adc_info *adc_info = adc->cfg->adc_info; 2036 struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX]; 2037 struct device *dev = &indio_dev->dev; 2038 u32 num_diff = adc->num_diff; | 2021 } 2022 2023 /* Optional sample time is provided either for each, or all channels */ 2024 adc->nsmps = device_property_count_u32(dev, "st,min-sample-time-nsecs"); 2025 if (adc->nsmps > 1 && adc->nsmps != num_channels) { 2026 dev_err(&indio_dev->dev, "Invalid st,min-sample-time-nsecs\n"); 2027 return -EINVAL; 2028 } --- 5 unchanged lines hidden (view full) --- 2034 struct stm32_adc *adc, 2035 struct iio_chan_spec *channels, 2036 int nchans) 2037{ 2038 const struct stm32_adc_info *adc_info = adc->cfg->adc_info; 2039 struct stm32_adc_diff_channel diff[STM32_ADC_CH_MAX]; 2040 struct device *dev = &indio_dev->dev; 2041 u32 num_diff = adc->num_diff; |
2039 int num_se = nchans - num_diff; | |
2040 int size = num_diff * sizeof(*diff) / sizeof(u32); 2041 int scan_index = 0, ret, i, c; 2042 u32 smp = 0, smps[STM32_ADC_CH_MAX], chans[STM32_ADC_CH_MAX]; 2043 2044 if (num_diff) { 2045 ret = device_property_read_u32_array(dev, "st,adc-diff-channels", 2046 (u32 *)diff, size); 2047 if (ret) { --- 10 unchanged lines hidden (view full) --- 2058 } 2059 2060 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], 2061 diff[i].vinp, diff[i].vinn, 2062 scan_index, true); 2063 scan_index++; 2064 } 2065 } | 2042 int size = num_diff * sizeof(*diff) / sizeof(u32); 2043 int scan_index = 0, ret, i, c; 2044 u32 smp = 0, smps[STM32_ADC_CH_MAX], chans[STM32_ADC_CH_MAX]; 2045 2046 if (num_diff) { 2047 ret = device_property_read_u32_array(dev, "st,adc-diff-channels", 2048 (u32 *)diff, size); 2049 if (ret) { --- 10 unchanged lines hidden (view full) --- 2060 } 2061 2062 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], 2063 diff[i].vinp, diff[i].vinn, 2064 scan_index, true); 2065 scan_index++; 2066 } 2067 } |
2066 if (num_se > 0) { 2067 ret = device_property_read_u32_array(dev, "st,adc-channels", chans, num_se); 2068 if (ret) { 2069 dev_err(&indio_dev->dev, "Failed to get st,adc-channels %d\n", ret); 2070 return ret; | 2068 2069 ret = device_property_read_u32_array(dev, "st,adc-channels", chans, 2070 nchans); 2071 if (ret) 2072 return ret; 2073 2074 for (c = 0; c < nchans; c++) { 2075 if (chans[c] >= adc_info->max_channels) { 2076 dev_err(&indio_dev->dev, "Invalid channel %d\n", 2077 chans[c]); 2078 return -EINVAL; |
2071 } 2072 | 2079 } 2080 |
2073 for (c = 0; c < num_se; c++) { 2074 if (chans[c] >= adc_info->max_channels) { 2075 dev_err(&indio_dev->dev, "Invalid channel %d\n", 2076 chans[c]); | 2081 /* Channel can't be configured both as single-ended & diff */ 2082 for (i = 0; i < num_diff; i++) { 2083 if (chans[c] == diff[i].vinp) { 2084 dev_err(&indio_dev->dev, "channel %d misconfigured\n", chans[c]); |
2077 return -EINVAL; 2078 } | 2085 return -EINVAL; 2086 } |
2079 2080 /* Channel can't be configured both as single-ended & diff */ 2081 for (i = 0; i < num_diff; i++) { 2082 if (chans[c] == diff[i].vinp) { 2083 dev_err(&indio_dev->dev, "channel %d misconfigured\n", 2084 chans[c]); 2085 return -EINVAL; 2086 } 2087 } 2088 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], 2089 chans[c], 0, scan_index, false); 2090 scan_index++; | |
2091 } | 2087 } |
2088 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], 2089 chans[c], 0, scan_index, false); 2090 scan_index++; |
|
2092 } 2093 2094 if (adc->nsmps > 0) { 2095 ret = device_property_read_u32_array(dev, "st,min-sample-time-nsecs", 2096 smps, adc->nsmps); 2097 if (ret) 2098 return ret; 2099 } --- 204 unchanged lines hidden (view full) --- 2304 2305 channels = devm_kcalloc(&indio_dev->dev, num_channels, 2306 sizeof(struct iio_chan_spec), GFP_KERNEL); 2307 if (!channels) 2308 return -ENOMEM; 2309 2310 if (legacy) 2311 ret = stm32_adc_legacy_chan_init(indio_dev, adc, channels, | 2091 } 2092 2093 if (adc->nsmps > 0) { 2094 ret = device_property_read_u32_array(dev, "st,min-sample-time-nsecs", 2095 smps, adc->nsmps); 2096 if (ret) 2097 return ret; 2098 } --- 204 unchanged lines hidden (view full) --- 2303 2304 channels = devm_kcalloc(&indio_dev->dev, num_channels, 2305 sizeof(struct iio_chan_spec), GFP_KERNEL); 2306 if (!channels) 2307 return -ENOMEM; 2308 2309 if (legacy) 2310 ret = stm32_adc_legacy_chan_init(indio_dev, adc, channels, |
2312 timestamping ? num_channels - 1 : num_channels); | 2311 num_channels); |
2313 else 2314 ret = stm32_adc_generic_chan_init(indio_dev, adc, channels); 2315 if (ret < 0) 2316 return ret; 2317 scan_index = ret; 2318 2319 if (timestamping) { 2320 struct iio_chan_spec *timestamp = &channels[scan_index]; --- 352 unchanged lines hidden --- | 2312 else 2313 ret = stm32_adc_generic_chan_init(indio_dev, adc, channels); 2314 if (ret < 0) 2315 return ret; 2316 scan_index = ret; 2317 2318 if (timestamping) { 2319 struct iio_chan_spec *timestamp = &channels[scan_index]; --- 352 unchanged lines hidden --- |