Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: adc: vf610: Determine sampling frequencies by using minimum sample time

The driver currently does not take into account the minimum sample time
as per the Figure 6-8 Chapter 9.1.1 12-bit ADC electrical characteristics.
We set a static amount of cycles instead of considering the sample time
as a given value, which depends on hardware characteristics.

Determine sampling frequencies by first reading the device tree property
node and then calculating the required Long Sample Time Adder (LSTAdder)
value, based on the ADC clock frequency and sample time value obtained
from the device tree. This LSTAdder value is then used for calculating
the sampling frequencies possible.

In case the sample time property is not specified through the device
tree, a safe default value of 1000ns is assumed.

Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
Acked-by: Stefan Agner <stefan@agner.ch>
Acked-by: Fugang Duan <B38611@freescale.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Sanchayan Maity and committed by
Jonathan Cameron
5e9972cd f686a36b

+80 -4
+5
Documentation/devicetree/bindings/iio/adc/vf610-adc.txt
··· 17 17 - Frequency in normal mode (ADLPC=0, ADHSC=0) 18 18 - Frequency in high-speed mode (ADLPC=0, ADHSC=1) 19 19 - Frequency in low-power mode (ADLPC=1, ADHSC=0) 20 + - min-sample-time: Minimum sampling time in nanoseconds. This value has 21 + to be chosen according to the conversion mode and the connected analog 22 + source resistance (R_as) and capacitance (C_as). Refer the datasheet's 23 + operating requirements. A safe default across a wide range of R_as and 24 + C_as as well as conversion modes is 1000ns. 20 25 21 26 Example: 22 27 adc0: adc@4003b000 {
+75 -4
drivers/iio/adc/vf610_adc.c
··· 68 68 #define VF610_ADC_CLK_DIV8 0x60 69 69 #define VF610_ADC_CLK_MASK 0x60 70 70 #define VF610_ADC_ADLSMP_LONG 0x10 71 + #define VF610_ADC_ADSTS_SHORT 0x100 72 + #define VF610_ADC_ADSTS_NORMAL 0x200 73 + #define VF610_ADC_ADSTS_LONG 0x300 71 74 #define VF610_ADC_ADSTS_MASK 0x300 72 75 #define VF610_ADC_ADLPC_EN 0x80 73 76 #define VF610_ADC_ADHSC_EN 0x400 ··· 101 98 #define VF610_ADC_CALF 0x2 102 99 #define VF610_ADC_TIMEOUT msecs_to_jiffies(100) 103 100 101 + #define DEFAULT_SAMPLE_TIME 1000 102 + 104 103 enum clk_sel { 105 104 VF610_ADCIOC_BUSCLK_SET, 106 105 VF610_ADCIOC_ALTCLK_SET, ··· 129 124 VF610_ADC_CONV_LOW_POWER, 130 125 }; 131 126 127 + enum lst_adder_sel { 128 + VF610_ADCK_CYCLES_3, 129 + VF610_ADCK_CYCLES_5, 130 + VF610_ADCK_CYCLES_7, 131 + VF610_ADCK_CYCLES_9, 132 + VF610_ADCK_CYCLES_13, 133 + VF610_ADCK_CYCLES_17, 134 + VF610_ADCK_CYCLES_21, 135 + VF610_ADCK_CYCLES_25, 136 + }; 137 + 132 138 struct vf610_adc_feature { 133 139 enum clk_sel clk_sel; 134 140 enum vol_ref vol_ref; ··· 148 132 int clk_div; 149 133 int sample_rate; 150 134 int res_mode; 135 + u32 lst_adder_index; 136 + u32 default_sample_time; 151 137 152 138 bool calibration; 153 139 bool ovwren; ··· 173 155 }; 174 156 175 157 static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 }; 158 + static const u32 vf610_lst_adder[] = { 3, 5, 7, 9, 13, 17, 21, 25 }; 176 159 177 160 static inline void vf610_adc_calculate_rates(struct vf610_adc *info) 178 161 { 179 162 struct vf610_adc_feature *adc_feature = &info->adc_feature; 180 163 unsigned long adck_rate, ipg_rate = clk_get_rate(info->clk); 164 + u32 adck_period, lst_addr_min; 181 165 int divisor, i; 182 166 183 167 adck_rate = info->max_adck_rate[adc_feature->conv_mode]; ··· 194 174 } 195 175 196 176 /* 177 + * Determine the long sample time adder value to be used based 178 + * on the default minimum sample time provided. 179 + */ 180 + adck_period = NSEC_PER_SEC / adck_rate; 181 + lst_addr_min = adc_feature->default_sample_time / adck_period; 182 + for (i = 0; i < ARRAY_SIZE(vf610_lst_adder); i++) { 183 + if (vf610_lst_adder[i] > lst_addr_min) { 184 + adc_feature->lst_adder_index = i; 185 + break; 186 + } 187 + } 188 + 189 + /* 197 190 * Calculate ADC sample frequencies 198 191 * Sample time unit is ADCK cycles. ADCK clk source is ipg clock, 199 192 * which is the same as bus clock. ··· 215 182 * SFCAdder: fixed to 6 ADCK cycles 216 183 * AverageNum: 1, 4, 8, 16, 32 samples for hardware average. 217 184 * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode 218 - * LSTAdder(Long Sample Time): fixed to 3 ADCK cycles 185 + * LSTAdder(Long Sample Time): 3, 5, 7, 9, 13, 17, 21, 25 ADCK cycles 219 186 */ 220 187 adck_rate = ipg_rate / info->adc_feature.clk_div; 221 188 for (i = 0; i < ARRAY_SIZE(vf610_hw_avgs); i++) 222 189 info->sample_freq_avail[i] = 223 - adck_rate / (6 + vf610_hw_avgs[i] * (25 + 3)); 190 + adck_rate / (6 + vf610_hw_avgs[i] * 191 + (25 + vf610_lst_adder[adc_feature->lst_adder_index])); 224 192 } 225 193 226 194 static inline void vf610_adc_cfg_init(struct vf610_adc *info) ··· 381 347 break; 382 348 } 383 349 384 - /* Use the short sample mode */ 385 - cfg_data &= ~(VF610_ADC_ADLSMP_LONG | VF610_ADC_ADSTS_MASK); 350 + /* 351 + * Set ADLSMP and ADSTS based on the Long Sample Time Adder value 352 + * determined. 353 + */ 354 + switch (adc_feature->lst_adder_index) { 355 + case VF610_ADCK_CYCLES_3: 356 + break; 357 + case VF610_ADCK_CYCLES_5: 358 + cfg_data |= VF610_ADC_ADSTS_SHORT; 359 + break; 360 + case VF610_ADCK_CYCLES_7: 361 + cfg_data |= VF610_ADC_ADSTS_NORMAL; 362 + break; 363 + case VF610_ADCK_CYCLES_9: 364 + cfg_data |= VF610_ADC_ADSTS_LONG; 365 + break; 366 + case VF610_ADCK_CYCLES_13: 367 + cfg_data |= VF610_ADC_ADLSMP_LONG; 368 + break; 369 + case VF610_ADCK_CYCLES_17: 370 + cfg_data |= VF610_ADC_ADLSMP_LONG; 371 + cfg_data |= VF610_ADC_ADSTS_SHORT; 372 + break; 373 + case VF610_ADCK_CYCLES_21: 374 + cfg_data |= VF610_ADC_ADLSMP_LONG; 375 + cfg_data |= VF610_ADC_ADSTS_NORMAL; 376 + break; 377 + case VF610_ADCK_CYCLES_25: 378 + cfg_data |= VF610_ADC_ADLSMP_LONG; 379 + cfg_data |= VF610_ADC_ADSTS_NORMAL; 380 + break; 381 + default: 382 + dev_err(info->dev, "error in sample time select\n"); 383 + } 386 384 387 385 /* update hardware average selection */ 388 386 cfg_data &= ~VF610_ADC_AVGS_MASK; ··· 778 712 779 713 of_property_read_u32_array(pdev->dev.of_node, "fsl,adck-max-frequency", 780 714 info->max_adck_rate, 3); 715 + 716 + ret = of_property_read_u32(pdev->dev.of_node, "min-sample-time", 717 + &info->adc_feature.default_sample_time); 718 + if (ret) 719 + info->adc_feature.default_sample_time = DEFAULT_SAMPLE_TIME; 781 720 782 721 platform_set_drvdata(pdev, indio_dev); 783 722