2007-09-21 15:51:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-04-13 20:03:08 +00:00
|
|
|
* Copyright (C) 2008 by Michael Sevakis
|
2007-09-21 15:51:53 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2007-09-21 15:51:53 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-04-13 20:03:08 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "mc13783.h"
|
2008-07-18 23:28:54 +00:00
|
|
|
#include "adc.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "adc-target.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
/* Do this so we may read all channels in a single SPI message */
|
2008-04-14 04:22:47 +00:00
|
|
|
static const unsigned char reg_array[4] =
|
2007-09-21 15:51:53 +00:00
|
|
|
{
|
2008-04-13 20:03:08 +00:00
|
|
|
MC13783_ADC2,
|
|
|
|
MC13783_ADC2,
|
|
|
|
MC13783_ADC2,
|
|
|
|
MC13783_ADC2,
|
|
|
|
};
|
2007-09-21 15:51:53 +00:00
|
|
|
|
2008-04-14 04:22:47 +00:00
|
|
|
static uint32_t channels[2][4];
|
2011-03-02 08:49:38 +00:00
|
|
|
static struct semaphore adc_done_signal;
|
2008-04-13 20:03:08 +00:00
|
|
|
static struct mutex adc_mtx;
|
|
|
|
static long last_adc_read[2]; /* One for each input group */
|
|
|
|
|
|
|
|
/* Read 10-bit ADC channel */
|
|
|
|
unsigned short adc_read(int channel)
|
2007-09-21 15:51:53 +00:00
|
|
|
{
|
2008-04-13 20:03:08 +00:00
|
|
|
uint32_t data;
|
|
|
|
int input_select;
|
|
|
|
|
|
|
|
if ((unsigned)channel >= NUM_ADC_CHANNELS)
|
|
|
|
return ADC_READ_ERROR;
|
|
|
|
|
|
|
|
input_select = channel >> 3;
|
|
|
|
|
2008-04-14 04:22:47 +00:00
|
|
|
mutex_lock(&adc_mtx);
|
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
/* Limit the traffic through here */
|
2008-04-14 06:52:38 +00:00
|
|
|
if (current_tick != last_adc_read[input_select])
|
2008-04-13 20:03:08 +00:00
|
|
|
{
|
|
|
|
/* Keep enable, start conversion, increment from channel 0,
|
|
|
|
* increment from channel 4 */
|
2009-03-22 01:50:48 +00:00
|
|
|
uint32_t adc1 = MC13783_ADEN | MC13783_ASC |
|
|
|
|
(0 << MC13783_ADA1_POS) | (4 << MC13783_ADA2_POS);
|
2008-04-13 20:03:08 +00:00
|
|
|
|
|
|
|
if (input_select == 1)
|
|
|
|
adc1 |= MC13783_ADSEL; /* 2nd set of inputs */
|
|
|
|
|
|
|
|
/* Start conversion */
|
|
|
|
mc13783_write(MC13783_ADC1, adc1);
|
|
|
|
|
|
|
|
/* Wait for done signal */
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_wait(&adc_done_signal, TIMEOUT_BLOCK);
|
2008-04-13 20:03:08 +00:00
|
|
|
|
|
|
|
/* Read all 8 channels that are converted - two channels in each
|
|
|
|
* word. */
|
2010-05-04 10:07:53 +00:00
|
|
|
mc13783_read_regs(reg_array, channels[input_select], 4);
|
2008-04-13 20:03:08 +00:00
|
|
|
|
|
|
|
last_adc_read[input_select] = current_tick;
|
|
|
|
}
|
|
|
|
|
2008-04-27 22:35:24 +00:00
|
|
|
data = channels[input_select][channel & 3];
|
2008-04-13 20:03:08 +00:00
|
|
|
|
|
|
|
mutex_unlock(&adc_mtx);
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
/* Channels 0-3/8-11 in ADD1, 4-7/12-15 in ADD2 */
|
2009-03-22 01:50:48 +00:00
|
|
|
return (channel & 4) ?
|
|
|
|
((data & MC13783_ADD2) >> MC13783_ADD2_POS) :
|
|
|
|
((data & MC13783_ADD1) >> MC13783_ADD1_POS);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 18:10:36 +00:00
|
|
|
bool adc_enable_channel(int channel, bool enable)
|
|
|
|
{
|
|
|
|
uint32_t bit, mask;
|
|
|
|
|
|
|
|
switch (channel)
|
|
|
|
{
|
|
|
|
case ADC_CHARGER_CURRENT:
|
|
|
|
mask = MC13783_CHRGICON;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADC_BATTERY_TEMP:
|
|
|
|
mask = MC13783_RTHEN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bit = enable ? mask : 0;
|
|
|
|
|
|
|
|
return mc13783_write_masked(MC13783_ADC0, bit, mask)
|
|
|
|
!= MC13783_DATA_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-02 18:32:35 +00:00
|
|
|
/* ADC conversion complete event - called from PMIC ISR */
|
2008-04-13 20:03:08 +00:00
|
|
|
void adc_done(void)
|
2007-09-21 15:51:53 +00:00
|
|
|
{
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&adc_done_signal);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
void adc_init(void)
|
2007-09-21 15:51:53 +00:00
|
|
|
{
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_init(&adc_done_signal, 1, 0);
|
2008-04-13 20:03:08 +00:00
|
|
|
mutex_init(&adc_mtx);
|
|
|
|
|
|
|
|
/* Init so first reads get data */
|
|
|
|
last_adc_read[0] = last_adc_read[1] = current_tick-1;
|
|
|
|
|
2008-12-21 18:10:36 +00:00
|
|
|
/* Enable increment-by-read, turn off extra conversions. */
|
|
|
|
mc13783_write(MC13783_ADC0, MC13783_ADINC2 | MC13783_ADINC1);
|
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
/* Enable ADC, set multi-channel mode */
|
|
|
|
mc13783_write(MC13783_ADC1, MC13783_ADEN);
|
2008-05-21 08:42:11 +00:00
|
|
|
|
|
|
|
/* Enable ADCDONE event */
|
|
|
|
mc13783_write(MC13783_INTERRUPT_STATUS0, MC13783_ADCDONEI);
|
2012-01-02 18:32:35 +00:00
|
|
|
mc13783_enable_event(MC13783_ADCDONE_EVENT, true);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|