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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-07-23 11:45:22 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "adc-target.h"
|
2011-09-25 15:35:39 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "adc-imx233.h"
|
2011-07-23 11:45:22 +00:00
|
|
|
|
2011-09-13 23:40:09 +00:00
|
|
|
/* dedicate two channels to temperature sensing
|
|
|
|
* dedicate channel 7 to battery
|
|
|
|
* and channel 6 to vddio */
|
|
|
|
static int pmos_chan, nmos_chan;
|
|
|
|
static int battery_chan, vddio_chan;
|
2011-11-14 21:05:14 +00:00
|
|
|
static int battery_delay_chan;
|
2011-09-13 23:40:09 +00:00
|
|
|
|
2011-07-23 11:45:22 +00:00
|
|
|
void adc_init(void)
|
|
|
|
{
|
2011-09-13 23:40:09 +00:00
|
|
|
imx233_lradc_init();
|
2011-11-14 21:05:14 +00:00
|
|
|
/* reserve channels 6 for vddio and 7 for battery (special for conversion) */
|
2011-09-13 23:40:09 +00:00
|
|
|
battery_chan = 7;
|
|
|
|
vddio_chan = 6;
|
2011-11-14 19:48:45 +00:00
|
|
|
imx233_lradc_reserve_channel(battery_chan);
|
|
|
|
imx233_lradc_reserve_channel(vddio_chan);
|
2011-11-14 21:05:14 +00:00
|
|
|
/* reserve any channels for PMOS and NMOS */
|
2011-11-14 19:48:45 +00:00
|
|
|
pmos_chan = imx233_lradc_acquire_channel(TIMEOUT_NOBLOCK);
|
2011-09-13 23:40:09 +00:00
|
|
|
if(pmos_chan < 0) panicf("No LRADC channel for PMOS !");
|
2011-11-14 19:48:45 +00:00
|
|
|
nmos_chan = imx233_lradc_acquire_channel(TIMEOUT_NOBLOCK);
|
2011-09-13 23:40:09 +00:00
|
|
|
if(nmos_chan < 0) panicf("No LRADC channel for NMOS !");
|
|
|
|
|
2011-11-14 21:05:14 +00:00
|
|
|
/* setup them for the simplest use: no accumulation, no division*/
|
2011-09-13 23:40:09 +00:00
|
|
|
imx233_lradc_setup_channel(battery_chan, false, false, 0, HW_LRADC_CHANNEL_BATTERY);
|
|
|
|
imx233_lradc_setup_channel(vddio_chan, false, false, 0, HW_LRADC_CHANNEL_VDDIO);
|
|
|
|
imx233_lradc_setup_channel(nmos_chan, false, false, 0, HW_LRADC_CHANNEL_NMOS_THIN);
|
|
|
|
imx233_lradc_setup_channel(pmos_chan, false, false, 0, HW_LRADC_CHANNEL_PMOS_THIN);
|
2011-11-14 21:05:14 +00:00
|
|
|
/* setup delay channel for battery for automatic reading and scaling */
|
|
|
|
battery_delay_chan = 0;
|
|
|
|
imx233_lradc_reserve_delay(battery_delay_chan);
|
|
|
|
/* setup delay to trigger battery channel and retrigger itself.
|
|
|
|
* The counter runs at 2KHz so a delay of 200 will trigger 10
|
|
|
|
* conversions per seconds */
|
|
|
|
imx233_lradc_setup_delay(battery_delay_chan, 1 << battery_chan,
|
|
|
|
1 << battery_delay_chan, 0, 200);
|
2011-11-14 21:42:43 +00:00
|
|
|
imx233_lradc_kick_delay(battery_delay_chan);
|
2011-11-14 21:05:14 +00:00
|
|
|
/* enable automatic conversion, use Li-Ion type battery */
|
|
|
|
imx233_lradc_setup_battery_conversion(true, HW_LRADC_CONVERSION__SCALE_FACTOR__LI_ION);
|
2011-09-13 23:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int adc_read_physical_ex(int virt)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int adc_read_physical(int src)
|
|
|
|
{
|
|
|
|
int virt = imx233_lradc_acquire_channel(TIMEOUT_BLOCK);
|
|
|
|
// divide by two for wider ranger
|
|
|
|
imx233_lradc_setup_channel(virt, false, false, 0, src);
|
|
|
|
int val = adc_read_physical_ex(virt);
|
|
|
|
imx233_lradc_release_channel(virt);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short adc_read_virtual(int c)
|
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case IMX233_ADC_BATTERY:
|
2011-11-14 21:42:43 +00:00
|
|
|
return adc_read_physical_ex(battery_chan);
|
2011-09-13 23:40:09 +00:00
|
|
|
case IMX233_ADC_VDDIO:
|
|
|
|
return adc_read_physical_ex(vddio_chan);
|
|
|
|
case IMX233_ADC_DIE_TEMP:
|
|
|
|
// do kelvin to celsius conversion
|
|
|
|
return imx233_lradc_sense_die_temperature(nmos_chan, pmos_chan) - 273;
|
|
|
|
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
|
|
|
|
return adc_read_physical(c);
|
2011-07-23 11:45:22 +00:00
|
|
|
}
|