Prepare new maemo release
[maemo-rb.git] / firmware / target / arm / imx233 / adc-imx233.c
blob2d2f0de5175b2a68a4bb739bbe532230fdbe8fc7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 by Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "adc-imx233.h"
24 void adc_init(void)
28 static short adc_read_physical_ex(int virt)
30 imx233_lradc_clear_channel(virt);
31 imx233_lradc_kick_channel(virt);
32 imx233_lradc_wait_channel(virt);
33 return imx233_lradc_read_channel(virt);
36 static short adc_read_physical(int src, bool div2)
38 int virt = imx233_lradc_acquire_channel(TIMEOUT_BLOCK);
39 // divide by two for wider ranger
40 imx233_lradc_setup_channel(virt, div2, false, 0, src);
41 int val = adc_read_physical_ex(virt);
42 imx233_lradc_release_channel(virt);
43 return val;
46 static short adc_read_virtual(int c)
48 switch(c)
50 case IMX233_ADC_BATTERY:
51 return imx233_lradc_read_battery_voltage();
52 case IMX233_ADC_VDDIO:
53 /* VddIO pin has a builtin 2:1 divide */
54 return adc_read_physical(HW_LRADC_CHANNEL_VDDIO, false);
55 case IMX233_ADC_VDD5V:
56 /* Vdd5V pin has a builtin 4:1 divide */
57 return adc_read_physical(HW_LRADC_CHANNEL_5V, false) * 2;
58 case IMX233_ADC_DIE_TEMP:
60 // don't block on second channel otherwise we might deadlock !
61 int nmos_chan = imx233_lradc_acquire_channel(TIMEOUT_BLOCK);
62 int pmos_chan = imx233_lradc_acquire_channel(TIMEOUT_NOBLOCK);
63 int val = 0;
64 if(pmos_chan >= 0)
66 val = imx233_lradc_sense_die_temperature(nmos_chan, pmos_chan) - 273;
67 imx233_lradc_release_channel(pmos_chan);
69 imx233_lradc_release_channel(nmos_chan);
70 return val;
72 #ifdef IMX233_BATT_TEMP_SENSOR
73 case IMX233_ADC_BATT_TEMP:
75 int virt = imx233_lradc_acquire_channel(TIMEOUT_BLOCK);
76 int val = imx233_lradc_sense_ext_temperature(virt, IMX233_BATT_TEMP_SENSOR);
77 imx233_lradc_release_channel(virt);
78 return val;
80 #endif
81 default:
82 return 0;
86 unsigned short adc_read(int channel)
88 int c = imx233_adc_mapping[channel];
89 if(c < 0)
90 return adc_read_virtual(c);
91 else
92 return adc_read_physical(c, true);