Skip to content

Commit dcf72cd

Browse files
agnerssashalevin
authored andcommitted
iio: adc: vf610: use ADC clock within specification
[ Upstream commit f54e9f2 ] Depending on conversion mode used, the ADC clock (ADCK) needs to be below a maximum frequency. According to Vybrid's data sheet this is 20MHz for the low power conversion mode. The ADC clock is depending on input clock, which is the bus clock by default. Vybrid SoC are typically clocked at at 400MHz or 500MHz, which leads to 66MHz or 83MHz bus clock respectively. Hence, a divider of 8 is required to stay below the specified maximum clock of 20MHz. Due to the different bus clock speeds, the resulting sampling frequency is not static. Hence use the ADC clock and calculate the actual available sampling frequency dynamically. This fixes bogous values observed on some 500MHz clocked Vybrid SoC. The resulting value usually showed Bit 9 being stuck at 1, or 0, which lead to a value of +/-512. Signed-off-by: Stefan Agner <stefan@agner.ch> Acked-by: Fugang Duan <B38611@freescale.com> Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent 1930ba0 commit dcf72cd

1 file changed

Lines changed: 61 additions & 30 deletions

File tree

‎drivers/iio/adc/vf610_adc.c‎

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,13 @@ struct vf610_adc {
141141
struct regulator *vref;
142142
struct vf610_adc_feature adc_feature;
143143

144+
u32 sample_freq_avail[5];
145+
144146
struct completion completion;
145147
};
146148

149+
static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 };
150+
147151
#define VF610_ADC_CHAN(_idx, _chan_type) { \
148152
.type = (_chan_type), \
149153
.indexed = 1, \
@@ -173,35 +177,47 @@ static const struct iio_chan_spec vf610_adc_iio_channels[] = {
173177
/* sentinel */
174178
};
175179

176-
/*
177-
* ADC sample frequency, unit is ADCK cycles.
178-
* ADC clk source is ipg clock, which is the same as bus clock.
179-
*
180-
* ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
181-
* SFCAdder: fixed to 6 ADCK cycles
182-
* AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
183-
* BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
184-
* LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
185-
*
186-
* By default, enable 12 bit resolution mode, clock source
187-
* set to ipg clock, So get below frequency group:
188-
*/
189-
static const u32 vf610_sample_freq_avail[5] =
190-
{1941176, 559332, 286957, 145374, 73171};
180+
static inline void vf610_adc_calculate_rates(struct vf610_adc *info)
181+
{
182+
unsigned long adck_rate, ipg_rate = clk_get_rate(info->clk);
183+
int i;
184+
185+
/*
186+
* Calculate ADC sample frequencies
187+
* Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
188+
* which is the same as bus clock.
189+
*
190+
* ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
191+
* SFCAdder: fixed to 6 ADCK cycles
192+
* AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
193+
* BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
194+
* LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
195+
*/
196+
adck_rate = ipg_rate / info->adc_feature.clk_div;
197+
for (i = 0; i < ARRAY_SIZE(vf610_hw_avgs); i++)
198+
info->sample_freq_avail[i] =
199+
adck_rate / (6 + vf610_hw_avgs[i] * (25 + 3));
200+
}
191201

192202
static inline void vf610_adc_cfg_init(struct vf610_adc *info)
193203
{
204+
struct vf610_adc_feature *adc_feature = &info->adc_feature;
205+
194206
/* set default Configuration for ADC controller */
195-
info->adc_feature.clk_sel = VF610_ADCIOC_BUSCLK_SET;
196-
info->adc_feature.vol_ref = VF610_ADCIOC_VR_VREF_SET;
207+
adc_feature->clk_sel = VF610_ADCIOC_BUSCLK_SET;
208+
adc_feature->vol_ref = VF610_ADCIOC_VR_VREF_SET;
209+
210+
adc_feature->calibration = true;
211+
adc_feature->ovwren = true;
212+
213+
adc_feature->res_mode = 12;
214+
adc_feature->sample_rate = 1;
215+
adc_feature->lpm = true;
197216

198-
info->adc_feature.calibration = true;
199-
info->adc_feature.ovwren = true;
217+
/* Use a save ADCK which is below 20MHz on all devices */
218+
adc_feature->clk_div = 8;
200219

201-
info->adc_feature.clk_div = 1;
202-
info->adc_feature.res_mode = 12;
203-
info->adc_feature.sample_rate = 1;
204-
info->adc_feature.lpm = true;
220+
vf610_adc_calculate_rates(info);
205221
}
206222

207223
static void vf610_adc_cfg_post_set(struct vf610_adc *info)
@@ -283,12 +299,10 @@ static void vf610_adc_cfg_set(struct vf610_adc *info)
283299

284300
cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
285301

286-
/* low power configuration */
287302
cfg_data &= ~VF610_ADC_ADLPC_EN;
288303
if (adc_feature->lpm)
289304
cfg_data |= VF610_ADC_ADLPC_EN;
290305

291-
/* disable high speed */
292306
cfg_data &= ~VF610_ADC_ADHSC_EN;
293307

294308
writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
@@ -428,10 +442,27 @@ static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
428442
return IRQ_HANDLED;
429443
}
430444

431-
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1941176, 559332, 286957, 145374, 73171");
445+
static ssize_t vf610_show_samp_freq_avail(struct device *dev,
446+
struct device_attribute *attr, char *buf)
447+
{
448+
struct vf610_adc *info = iio_priv(dev_to_iio_dev(dev));
449+
size_t len = 0;
450+
int i;
451+
452+
for (i = 0; i < ARRAY_SIZE(info->sample_freq_avail); i++)
453+
len += scnprintf(buf + len, PAGE_SIZE - len,
454+
"%u ", info->sample_freq_avail[i]);
455+
456+
/* replace trailing space by newline */
457+
buf[len - 1] = '\n';
458+
459+
return len;
460+
}
461+
462+
static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail);
432463

433464
static struct attribute *vf610_attributes[] = {
434-
&iio_const_attr_sampling_frequency_available.dev_attr.attr,
465+
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
435466
NULL
436467
};
437468

@@ -478,7 +509,7 @@ static int vf610_read_raw(struct iio_dev *indio_dev,
478509
return IIO_VAL_FRACTIONAL_LOG2;
479510

480511
case IIO_CHAN_INFO_SAMP_FREQ:
481-
*val = vf610_sample_freq_avail[info->adc_feature.sample_rate];
512+
*val = info->sample_freq_avail[info->adc_feature.sample_rate];
482513
*val2 = 0;
483514
return IIO_VAL_INT;
484515

@@ -501,9 +532,9 @@ static int vf610_write_raw(struct iio_dev *indio_dev,
501532
switch (mask) {
502533
case IIO_CHAN_INFO_SAMP_FREQ:
503534
for (i = 0;
504-
i < ARRAY_SIZE(vf610_sample_freq_avail);
535+
i < ARRAY_SIZE(info->sample_freq_avail);
505536
i++)
506-
if (val == vf610_sample_freq_avail[i]) {
537+
if (val == info->sample_freq_avail[i]) {
507538
info->adc_feature.sample_rate = i;
508539
vf610_adc_sample_set(info);
509540
return 0;

0 commit comments

Comments
 (0)