2011-05-01 13:02:46 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 by Amaury Pouly
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-19 19:04:03 +00:00
|
|
|
#include "adc-target.h"
|
2013-11-19 22:12:42 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "powermgmt-target.h"
|
|
|
|
#include "lradc-imx233.h"
|
2013-06-16 18:59:36 +00:00
|
|
|
#include "power-imx233.h"
|
2011-07-23 11:45:22 +00:00
|
|
|
|
2013-11-19 19:04:03 +00:00
|
|
|
/* Virtual channels */
|
|
|
|
#define IMX233_ADC_BATTERY -1 /* Battery voltage (mV) */
|
|
|
|
#define IMX233_ADC_DIE_TEMP -2 /* Die temperature (°C) */
|
|
|
|
#define IMX233_ADC_VDDIO -3 /* VddIO voltage (mV) */
|
|
|
|
#if IMX233_SUBTARGET >= 3700
|
|
|
|
#define IMX233_ADC_VDD5V -4 /* Vdd5V voltage (mV) */
|
|
|
|
#endif
|
|
|
|
#ifdef IMX233_BATT_TEMP_SENSOR
|
|
|
|
#define IMX233_ADC_BATT_TEMP -5 /* Battery temperature (°C) */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *imx233_adc_channel_name[NUM_ADC_CHANNELS] =
|
|
|
|
{
|
|
|
|
[ADC_BATTERY] = "Battery(raw)",
|
|
|
|
[ADC_DIE_TEMP] = "Die temperature(°C)",
|
|
|
|
[ADC_VDDIO] = "VddIO(mV)",
|
|
|
|
#if IMX233_SUBTARGET >= 3700
|
|
|
|
[ADC_VDD5V] = "Vdd5V(mV)",
|
|
|
|
#endif
|
|
|
|
#ifdef IMX233_BATT_TEMP_SENSOR
|
|
|
|
[ADC_BATT_TEMP] = "Battery temperature(raw)",
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static int imx233_adc_mapping[NUM_ADC_CHANNELS] =
|
|
|
|
{
|
|
|
|
[ADC_BATTERY] = IMX233_ADC_BATTERY,
|
|
|
|
[ADC_DIE_TEMP] = IMX233_ADC_DIE_TEMP,
|
|
|
|
[ADC_VDDIO] = IMX233_ADC_VDDIO,
|
|
|
|
#if IMX233_SUBTARGET >= 3700
|
|
|
|
[ADC_VDD5V] = IMX233_ADC_VDD5V,
|
|
|
|
#endif
|
|
|
|
#ifdef IMX233_BATT_TEMP_SENSOR
|
|
|
|
[ADC_BATT_TEMP] = IMX233_ADC_BATT_TEMP,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-07-23 11:45:22 +00:00
|
|
|
void adc_init(void)
|
|
|
|
{
|
2011-09-13 23:40:09 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 16:39:27 +00:00
|
|
|
static short adc_read_physical_ex(int virt)
|
2011-09-13 23:40:09 +00:00
|
|
|
{
|
|
|
|
imx233_lradc_clear_channel(virt);
|
|
|
|
imx233_lradc_kick_channel(virt);
|
|
|
|
imx233_lradc_wait_channel(virt);
|
2011-11-14 21:05:14 +00:00
|
|
|
return imx233_lradc_read_channel(virt);
|
2011-09-13 23:40:09 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 16:39:27 +00:00
|
|
|
static short adc_read_physical(int src, bool div2)
|
2011-09-13 23:40:09 +00:00
|
|
|
{
|
2013-06-16 18:59:36 +00:00
|
|
|
int virt = imx233_lradc_acquire_channel(src, TIMEOUT_BLOCK);
|
2011-09-13 23:40:09 +00:00
|
|
|
// divide by two for wider ranger
|
2012-03-17 16:39:27 +00:00
|
|
|
imx233_lradc_setup_channel(virt, div2, false, 0, src);
|
2011-09-13 23:40:09 +00:00
|
|
|
int val = adc_read_physical_ex(virt);
|
|
|
|
imx233_lradc_release_channel(virt);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-03-17 16:39:27 +00:00
|
|
|
static short adc_read_virtual(int c)
|
2011-09-13 23:40:09 +00:00
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case IMX233_ADC_BATTERY:
|
2012-03-17 16:39:27 +00:00
|
|
|
return imx233_lradc_read_battery_voltage();
|
2011-09-13 23:40:09 +00:00
|
|
|
case IMX233_ADC_VDDIO:
|
2012-03-17 16:39:27 +00:00
|
|
|
/* VddIO pin has a builtin 2:1 divide */
|
2013-11-19 19:04:03 +00:00
|
|
|
return adc_read_physical(LRADC_SRC_VDDIO, true) * 2;
|
2013-06-16 18:59:36 +00:00
|
|
|
#if IMX233_SUBTARGET >= 3700
|
2012-03-17 16:39:27 +00:00
|
|
|
case IMX233_ADC_VDD5V:
|
|
|
|
/* Vdd5V pin has a builtin 4:1 divide */
|
2013-11-19 19:04:03 +00:00
|
|
|
return adc_read_physical(LRADC_SRC_5V, true) * 4;
|
2013-06-16 18:59:36 +00:00
|
|
|
#endif
|
2011-09-13 23:40:09 +00:00
|
|
|
case IMX233_ADC_DIE_TEMP:
|
2012-03-17 16:39:27 +00:00
|
|
|
{
|
2013-06-16 18:59:36 +00:00
|
|
|
#if IMX233_SUBTARGET >= 3700
|
2012-03-17 16:39:27 +00:00
|
|
|
// don't block on second channel otherwise we might deadlock !
|
2013-06-16 18:59:36 +00:00
|
|
|
int nmos_chan = imx233_lradc_acquire_channel(LRADC_SRC_NMOS_THIN, TIMEOUT_BLOCK);
|
|
|
|
int pmos_chan = imx233_lradc_acquire_channel(LRADC_SRC_PMOS_THIN, TIMEOUT_NOBLOCK);
|
2012-03-17 16:39:27 +00:00
|
|
|
int val = 0;
|
|
|
|
if(pmos_chan >= 0)
|
|
|
|
{
|
|
|
|
val = imx233_lradc_sense_die_temperature(nmos_chan, pmos_chan) - 273;
|
|
|
|
imx233_lradc_release_channel(pmos_chan);
|
|
|
|
}
|
|
|
|
imx233_lradc_release_channel(nmos_chan);
|
2013-06-16 18:59:36 +00:00
|
|
|
#else
|
|
|
|
int min, max, val;
|
|
|
|
if(imx233_power_sense_die_temperature(&min, &max) < 0)
|
|
|
|
val = -1;
|
|
|
|
else
|
|
|
|
val = (max + min) / 2;
|
|
|
|
#endif
|
2012-03-17 16:39:27 +00:00
|
|
|
return val;
|
|
|
|
}
|
2012-05-19 11:08:05 +00:00
|
|
|
#ifdef IMX233_BATT_TEMP_SENSOR
|
2012-03-17 16:39:27 +00:00
|
|
|
case IMX233_ADC_BATT_TEMP:
|
|
|
|
{
|
2013-06-16 18:59:36 +00:00
|
|
|
int virt = imx233_lradc_acquire_channel(IMX233_BATT_TEMP_SENSOR, TIMEOUT_BLOCK);
|
2012-05-19 11:08:05 +00:00
|
|
|
int val = imx233_lradc_sense_ext_temperature(virt, IMX233_BATT_TEMP_SENSOR);
|
2012-03-17 16:39:27 +00:00
|
|
|
imx233_lradc_release_channel(virt);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-13 23:40:09 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-23 11:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short adc_read(int channel)
|
|
|
|
{
|
2011-09-13 23:40:09 +00:00
|
|
|
int c = imx233_adc_mapping[channel];
|
|
|
|
if(c < 0)
|
|
|
|
return adc_read_virtual(c);
|
|
|
|
else
|
2012-03-17 16:39:27 +00:00
|
|
|
return adc_read_physical(c, true);
|
2011-07-23 11:45:22 +00:00
|
|
|
}
|
2013-11-19 19:04:03 +00:00
|
|
|
|
|
|
|
const char *adc_name(int channel)
|
|
|
|
{
|
|
|
|
return imx233_adc_channel_name[channel];
|
|
|
|
}
|