AnsweredAssumed Answered

imx6ul adc in u-boot. read value is different from linux.

Question asked by Sergey Leshchenko on Aug 29, 2018
Latest reply on Aug 31, 2018 by Sergey Leshchenko



Please help. I've made an adc convertion in uboot but for some reason the read value is different from one that is done via iio in linux.


Linux raw value shows: 2564

My code in uboot shows: 3770


#define ADC1_BASE_ADDR  0x02198000
#define ADCx_HC0        0x00
#define ADCx_HS         0x08
#define ADCx_HS_C0      BIT(0)
#define ADCx_R0         0x0c
#define ADCx_CFG        0x14
#define ADCx_CFG_SWMODE 0x308
#define ADCx_GC         0x18
#define ADCx_GC_CAL     BIT(7)

static int read_adc(u32 *val)
     int ret;
     void __iomem *b = map_physmem(ADC1_BASE_ADDR, 0x100, MAP_NOCACHE);

     /* use software mode */
     writel(ADCx_CFG_SWMODE, b + ADCx_CFG);

     /* start auto calibration */
     setbits_le32(b + ADCx_GC, ADCx_GC_CAL);
     ret = wait_for_bit("Wait for calibration", b + ADCx_GC, ADCx_GC_CAL, ADCx_GC_CAL, 10, 0);
     if (ret)
          goto adc_exit;

     /* start conversion */
     writel(0, b + ADCx_HC0);

     /* wait for conversion */
     ret = wait_for_bit("Wait for conversion", b + ADCx_HS, ADCx_HS_C0, ADCx_HS_C0, 10, 0);
     if (ret)
          goto adc_exit;

     /* read result */
     *val = readl(b + ADCx_R0);

     if (ret)
          printf("ADC failure (ret=%i)\n", ret);
     unmap_physmem(b, MAP_NOCACHE);
     return ret;

static int check_battery(void)
     u32 val;
     int ret;

     ret = read_adc(&val);
     if (ret)
          return ret;

     printf("Battery voltage: %d\n", val);

     return ret;