2008-04-11 08:51:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 by Michael Sevakis
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "system.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "spi-imx31.h"
|
2008-04-12 16:56:45 +00:00
|
|
|
#include "gpio-imx31.h"
|
2008-04-11 08:51:27 +00:00
|
|
|
#include "mc13783.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
|
2008-04-13 10:04:21 +00:00
|
|
|
#include "power-imx31.h"
|
2008-04-12 16:56:45 +00:00
|
|
|
#include "button-target.h"
|
2008-04-13 20:03:08 +00:00
|
|
|
#include "adc-target.h"
|
2008-04-18 16:42:50 +00:00
|
|
|
#include "usb-target.h"
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
#ifdef BOOTLOADER
|
|
|
|
#define PMIC_DRIVER_CLOSE
|
|
|
|
#endif
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
/* This is all based on communicating with the MC13783 PMU which is on
|
|
|
|
* CSPI2 with the chip select at 0. The LCD controller resides on
|
|
|
|
* CSPI3 cs1, but we have no idea how to communicate to it */
|
|
|
|
static struct spi_node mc13783_spi =
|
|
|
|
{
|
|
|
|
CSPI2_NUM, /* CSPI module 2 */
|
|
|
|
CSPI_CONREG_CHIP_SELECT_SS0 | /* Chip select 0 */
|
|
|
|
CSPI_CONREG_DRCTL_DONT_CARE | /* Don't care about CSPI_RDY */
|
|
|
|
CSPI_CONREG_DATA_RATE_DIV_4 | /* Clock = IPG_CLK/4 - 16.5MHz */
|
|
|
|
CSPI_BITCOUNT(32-1) | /* All 32 bits are to be transferred */
|
|
|
|
CSPI_CONREG_SSPOL | /* SS active high */
|
|
|
|
CSPI_CONREG_SSCTL | /* Negate SS between SPI bursts */
|
|
|
|
CSPI_CONREG_MODE, /* Master mode */
|
|
|
|
0, /* SPI clock - no wait states */
|
|
|
|
};
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
extern const struct mc13783_event_list mc13783_event_list;
|
|
|
|
|
|
|
|
static int mc13783_thread_stack[3*DEFAULT_STACK_SIZE/sizeof(int)];
|
2008-04-11 08:51:27 +00:00
|
|
|
static const char *mc13783_thread_name = "pmic";
|
|
|
|
static struct wakeup mc13783_wake;
|
2008-05-21 08:42:11 +00:00
|
|
|
|
|
|
|
/* Tracking for which interrupts are enabled */
|
|
|
|
static uint32_t pmic_int_enabled[2] =
|
|
|
|
{ 0x00000000, 0x00000000 };
|
|
|
|
|
|
|
|
static const unsigned char pmic_intm_regs[2] =
|
|
|
|
{ MC13783_INTERRUPT_MASK0, MC13783_INTERRUPT_MASK1 };
|
|
|
|
|
|
|
|
static const unsigned char pmic_ints_regs[2] =
|
|
|
|
{ MC13783_INTERRUPT_STATUS0, MC13783_INTERRUPT_STATUS1 };
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
#ifdef PMIC_DRIVER_CLOSE
|
|
|
|
static bool pmic_close = false;
|
|
|
|
static struct thread_entry *mc13783_thread_p = NULL;
|
|
|
|
#endif
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
static void mc13783_interrupt_thread(void)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2008-04-12 16:56:45 +00:00
|
|
|
uint32_t pending[2];
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
/* Enable mc13783 GPIO event */
|
|
|
|
gpio_enable_event(MC13783_EVENT_ID);
|
2008-04-13 20:03:08 +00:00
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2008-05-21 08:42:11 +00:00
|
|
|
const struct mc13783_event *event, *event_last;
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
wakeup_wait(&mc13783_wake, TIMEOUT_BLOCK);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
#ifdef PMIC_DRIVER_CLOSE
|
|
|
|
if (pmic_close)
|
2008-05-21 08:42:11 +00:00
|
|
|
break;
|
2008-05-10 18:00:11 +00:00
|
|
|
#endif
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
mc13783_read_regset(pmic_ints_regs, pending, 2);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
/* Only clear interrupts being dispatched */
|
|
|
|
pending[0] &= pmic_int_enabled[0];
|
|
|
|
pending[1] &= pmic_int_enabled[1];
|
2008-04-13 20:03:08 +00:00
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
mc13783_write_regset(pmic_ints_regs, pending, 2);
|
2008-04-13 10:04:21 +00:00
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
event = mc13783_event_list.events;
|
|
|
|
event_last = event + mc13783_event_list.count;
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
/* .count is surely expected to be > 0 */
|
|
|
|
do
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
2008-05-21 08:42:11 +00:00
|
|
|
enum mc13783_event_sets set = event->set;
|
|
|
|
uint32_t pnd = pending[set];
|
|
|
|
uint32_t mask = event->mask;
|
2008-04-13 20:03:08 +00:00
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
if (pnd & mask)
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
2008-05-21 08:42:11 +00:00
|
|
|
event->callback();
|
|
|
|
pnd &= ~mask;
|
|
|
|
pending[set] = pnd;
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
2008-05-21 08:42:11 +00:00
|
|
|
|
|
|
|
if ((pending[0] | pending[1]) == 0)
|
|
|
|
break; /* Teminate early if nothing more to service */
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
2008-05-21 08:42:11 +00:00
|
|
|
while (++event < event_last);
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
2008-05-21 08:42:11 +00:00
|
|
|
|
|
|
|
#ifdef PMIC_DRIVER_CLOSE
|
|
|
|
gpio_disable_event(MC13783_EVENT_ID);
|
|
|
|
#endif
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
/* GPIO interrupt handler for mc13783 */
|
2008-05-21 08:42:11 +00:00
|
|
|
void mc13783_event(void)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2008-04-12 16:56:45 +00:00
|
|
|
MC13783_GPIO_ISR = (1ul << MC13783_GPIO_LINE);
|
2008-04-11 08:51:27 +00:00
|
|
|
wakeup_signal(&mc13783_wake);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mc13783_init(void)
|
|
|
|
{
|
|
|
|
/* Serial interface must have been initialized first! */
|
|
|
|
wakeup_init(&mc13783_wake);
|
|
|
|
|
|
|
|
/* Enable the PMIC SPI module */
|
|
|
|
spi_enable_module(&mc13783_spi);
|
|
|
|
|
2008-05-21 08:42:11 +00:00
|
|
|
/* Mask any PMIC interrupts for now - modules will enable them as
|
|
|
|
* required */
|
2008-04-12 16:56:45 +00:00
|
|
|
mc13783_write(MC13783_INTERRUPT_MASK0, 0xffffff);
|
|
|
|
mc13783_write(MC13783_INTERRUPT_MASK1, 0xffffff);
|
|
|
|
|
|
|
|
MC13783_GPIO_ISR = (1ul << MC13783_GPIO_LINE);
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
#ifdef PMIC_DRIVER_CLOSE
|
|
|
|
mc13783_thread_p =
|
|
|
|
#endif
|
|
|
|
create_thread(mc13783_interrupt_thread,
|
|
|
|
mc13783_thread_stack, sizeof(mc13783_thread_stack), 0,
|
|
|
|
mc13783_thread_name IF_PRIO(, PRIORITY_REALTIME) IF_COP(, CPU));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PMIC_DRIVER_CLOSE
|
|
|
|
void mc13783_close(void)
|
|
|
|
{
|
|
|
|
struct thread_entry *thread = mc13783_thread_p;
|
|
|
|
|
|
|
|
if (thread == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mc13783_thread_p = NULL;
|
|
|
|
|
|
|
|
pmic_close = true;
|
|
|
|
wakeup_signal(&mc13783_wake);
|
|
|
|
thread_wait(thread);
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
2008-05-21 08:42:11 +00:00
|
|
|
#endif /* PMIC_DRIVER_CLOSE */
|
|
|
|
|
|
|
|
bool mc13783_enable_event(enum mc13783_event_ids id)
|
|
|
|
{
|
|
|
|
const struct mc13783_event * const event =
|
|
|
|
&mc13783_event_list.events[id];
|
|
|
|
int set = event->set;
|
|
|
|
uint32_t mask = event->mask;
|
|
|
|
|
|
|
|
spi_lock(&mc13783_spi);
|
|
|
|
|
|
|
|
pmic_int_enabled[set] |= mask;
|
|
|
|
mc13783_clear(pmic_intm_regs[set], mask);
|
|
|
|
|
|
|
|
spi_unlock(&mc13783_spi);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mc13783_disable_event(enum mc13783_event_ids id)
|
|
|
|
{
|
|
|
|
const struct mc13783_event * const event =
|
|
|
|
&mc13783_event_list.events[id];
|
|
|
|
int set = event->set;
|
|
|
|
uint32_t mask = event->mask;
|
|
|
|
|
|
|
|
spi_lock(&mc13783_spi);
|
|
|
|
|
|
|
|
pmic_int_enabled[set] &= ~mask;
|
|
|
|
mc13783_set(pmic_intm_regs[set], mask);
|
|
|
|
|
|
|
|
spi_unlock(&mc13783_spi);
|
|
|
|
}
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
uint32_t mc13783_set(unsigned address, uint32_t bits)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
|
|
|
spi_lock(&mc13783_spi);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
uint32_t data = mc13783_read(address);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
|
|
|
if (data != (uint32_t)-1)
|
|
|
|
mc13783_write(address, data | bits);
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
spi_unlock(&mc13783_spi);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
|
|
|
return data;
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
uint32_t mc13783_clear(unsigned address, uint32_t bits)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
|
|
|
spi_lock(&mc13783_spi);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
uint32_t data = mc13783_read(address);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
|
|
|
if (data != (uint32_t)-1)
|
|
|
|
mc13783_write(address, data & ~bits);
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
spi_unlock(&mc13783_spi);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
|
|
|
return data;
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mc13783_write(unsigned address, uint32_t data)
|
|
|
|
{
|
|
|
|
struct spi_transfer xfer;
|
|
|
|
uint32_t packet;
|
|
|
|
|
|
|
|
if (address >= MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
packet = (1 << 31) | (address << 25) | (data & 0xffffff);
|
|
|
|
xfer.txbuf = &packet;
|
|
|
|
xfer.rxbuf = &packet;
|
|
|
|
xfer.count = 1;
|
|
|
|
|
|
|
|
if (!spi_transfer(&mc13783_spi, &xfer))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1 - xfer.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mc13783_write_multiple(unsigned start, const uint32_t *data, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct spi_transfer xfer;
|
2008-04-12 16:56:45 +00:00
|
|
|
uint32_t packets[MC13783_NUM_REGS];
|
2008-04-11 08:51:27 +00:00
|
|
|
|
|
|
|
if (start + count > MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Prepare payload */
|
|
|
|
for (i = 0; i < count; i++, start++)
|
|
|
|
{
|
|
|
|
packets[i] = (1 << 31) | (start << 25) | (data[i] & 0xffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
xfer.txbuf = packets;
|
|
|
|
xfer.rxbuf = packets;
|
|
|
|
xfer.count = count;
|
|
|
|
|
|
|
|
if (!spi_transfer(&mc13783_spi, &xfer))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return count - xfer.count;
|
|
|
|
}
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
int mc13783_write_regset(const unsigned char *regs, const uint32_t *data,
|
|
|
|
int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct spi_transfer xfer;
|
|
|
|
uint32_t packets[MC13783_NUM_REGS];
|
|
|
|
|
|
|
|
if (count > MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
uint32_t reg = regs[i];
|
|
|
|
|
|
|
|
if (reg >= MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
packets[i] = (1 << 31) | (reg << 25) | (data[i] & 0xffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
xfer.txbuf = packets;
|
|
|
|
xfer.rxbuf = packets;
|
|
|
|
xfer.count = count;
|
|
|
|
|
|
|
|
if (!spi_transfer(&mc13783_spi, &xfer))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return count - xfer.count;
|
|
|
|
}
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
uint32_t mc13783_read(unsigned address)
|
|
|
|
{
|
|
|
|
uint32_t packet;
|
|
|
|
struct spi_transfer xfer;
|
|
|
|
|
|
|
|
if (address >= MC13783_NUM_REGS)
|
|
|
|
return (uint32_t)-1;
|
|
|
|
|
|
|
|
packet = address << 25;
|
|
|
|
|
|
|
|
xfer.txbuf = &packet;
|
|
|
|
xfer.rxbuf = &packet;
|
|
|
|
xfer.count = 1;
|
|
|
|
|
|
|
|
if (!spi_transfer(&mc13783_spi, &xfer))
|
|
|
|
return (uint32_t)-1;
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
return packet;
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mc13783_read_multiple(unsigned start, uint32_t *buffer, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct spi_transfer xfer;
|
|
|
|
|
|
|
|
if (start + count > MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
xfer.txbuf = buffer;
|
2008-04-11 08:51:27 +00:00
|
|
|
xfer.rxbuf = buffer;
|
|
|
|
xfer.count = count;
|
|
|
|
|
|
|
|
/* Prepare TX payload */
|
|
|
|
for (i = 0; i < count; i++, start++)
|
2008-04-12 16:56:45 +00:00
|
|
|
buffer[i] = start << 25;
|
|
|
|
|
|
|
|
if (!spi_transfer(&mc13783_spi, &xfer))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return count - xfer.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mc13783_read_regset(const unsigned char *regs, uint32_t *buffer,
|
|
|
|
int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct spi_transfer xfer;
|
|
|
|
|
|
|
|
if (count > MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
unsigned reg = regs[i];
|
|
|
|
|
|
|
|
if (reg >= MC13783_NUM_REGS)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
buffer[i] = reg << 25;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfer.txbuf = buffer;
|
|
|
|
xfer.rxbuf = buffer;
|
|
|
|
xfer.count = count;
|
2008-04-11 08:51:27 +00:00
|
|
|
|
|
|
|
if (!spi_transfer(&mc13783_spi, &xfer))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return count - xfer.count;
|
|
|
|
}
|