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-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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mc13783_thread_stack[DEFAULT_STACK_SIZE/sizeof(int)];
|
|
|
|
static const char *mc13783_thread_name = "pmic";
|
|
|
|
static struct wakeup mc13783_wake;
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
/* The next two functions are rather target-specific but they'll just be left
|
|
|
|
* here for the moment */
|
2008-04-11 08:51:27 +00:00
|
|
|
static __attribute__((noreturn)) void mc13783_interrupt_thread(void)
|
|
|
|
{
|
2008-04-12 16:56:45 +00:00
|
|
|
const unsigned char status_regs[2] =
|
|
|
|
{
|
|
|
|
MC13783_INTERRUPT_STATUS0,
|
|
|
|
MC13783_INTERRUPT_STATUS1,
|
|
|
|
};
|
|
|
|
uint32_t pending[2];
|
|
|
|
uint32_t value;
|
|
|
|
|
|
|
|
mc13783_read_regset(status_regs, pending, 2);
|
|
|
|
mc13783_write_regset(status_regs, pending, 2);
|
|
|
|
|
|
|
|
gpio_enable_event(MC13783_GPIO_NUM, MC13783_EVENT_ID);
|
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
/* Check initial states for events with a sense bit */
|
2008-04-13 10:04:21 +00:00
|
|
|
value = mc13783_read(MC13783_INTERRUPT_SENSE0);
|
2008-04-18 16:42:50 +00:00
|
|
|
usb_set_status(value & MC13783_USB4V4);
|
2008-04-13 10:04:21 +00:00
|
|
|
set_charger_inserted(value & MC13783_CHGDET);
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
value = mc13783_read(MC13783_INTERRUPT_SENSE1);
|
2008-04-13 20:03:08 +00:00
|
|
|
button_power_set_state((value & MC13783_ONOFD1) == 0);
|
|
|
|
set_headphones_inserted((value & MC13783_ONOFD2) == 0);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
pending[0] = pending[1] = 0xffffff;
|
|
|
|
mc13783_write_regset(status_regs, pending, 2);
|
|
|
|
|
|
|
|
/* Enable desired PMIC interrupts - some are unmasked in the drivers that
|
|
|
|
* handle a specific task */
|
2008-04-13 10:04:21 +00:00
|
|
|
mc13783_clear(MC13783_INTERRUPT_MASK0, MC13783_CHGDET);
|
2008-04-13 20:03:08 +00:00
|
|
|
mc13783_clear(MC13783_INTERRUPT_MASK1, MC13783_ONOFD1 | MC13783_ONOFD2);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
wakeup_wait(&mc13783_wake, TIMEOUT_BLOCK);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
|
|
|
mc13783_read_regset(status_regs, pending, 2);
|
|
|
|
mc13783_write_regset(status_regs, pending, 2);
|
|
|
|
|
|
|
|
if (pending[0])
|
|
|
|
{
|
|
|
|
/* Handle ...PENDING0 */
|
2008-04-13 20:03:08 +00:00
|
|
|
|
|
|
|
/* Handle interrupts without a sense bit */
|
|
|
|
if (pending[0] & MC13783_ADCDONE)
|
|
|
|
adc_done();
|
|
|
|
|
|
|
|
/* Handle interrupts that have a sense bit that needs to
|
|
|
|
* be checked */
|
2008-04-18 16:42:50 +00:00
|
|
|
if (pending[0] & (MC13783_CHGDET | MC13783_USB4V4))
|
2008-04-13 10:04:21 +00:00
|
|
|
{
|
|
|
|
value = mc13783_read(MC13783_INTERRUPT_SENSE0);
|
|
|
|
|
|
|
|
if (pending[0] & MC13783_CHGDET)
|
|
|
|
set_charger_inserted(value & MC13783_CHGDET);
|
2008-04-18 16:42:50 +00:00
|
|
|
|
|
|
|
if (pending[0] & MC13783_USB4V4)
|
|
|
|
usb_set_status(value & MC13783_USB4V4);
|
2008-04-13 10:04:21 +00:00
|
|
|
}
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
2008-04-13 10:04:21 +00:00
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
if (pending[1])
|
|
|
|
{
|
|
|
|
/* Handle ...PENDING1 */
|
2008-04-13 20:03:08 +00:00
|
|
|
|
|
|
|
/* Handle interrupts without a sense bit */
|
|
|
|
/* ... */
|
|
|
|
|
|
|
|
/* Handle interrupts that have a sense bit that needs to
|
|
|
|
* be checked */
|
|
|
|
if (pending[1] & (MC13783_ONOFD1 | MC13783_ONOFD2))
|
2008-04-12 16:56:45 +00:00
|
|
|
{
|
|
|
|
value = mc13783_read(MC13783_INTERRUPT_SENSE1);
|
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
if (pending[1] & MC13783_ONOFD1)
|
|
|
|
button_power_set_state((value & MC13783_ONOFD1) == 0);
|
2008-04-12 16:56:45 +00:00
|
|
|
|
2008-04-13 20:03:08 +00:00
|
|
|
if (pending[1] & MC13783_ONOFD2)
|
|
|
|
set_headphones_inserted((value & MC13783_ONOFD2) == 0);
|
2008-04-12 16:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 16:56:45 +00:00
|
|
|
/* GPIO interrupt handler for mc13783 */
|
|
|
|
int 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);
|
2008-04-12 16:56:45 +00:00
|
|
|
return 1; /* Yes, it's handled */
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-12 16:56:45 +00:00
|
|
|
/* Mask any PMIC interrupts for now - poll initial status in thread
|
|
|
|
* and enable them there */
|
|
|
|
mc13783_write(MC13783_INTERRUPT_MASK0, 0xffffff);
|
|
|
|
mc13783_write(MC13783_INTERRUPT_MASK1, 0xffffff);
|
|
|
|
|
|
|
|
MC13783_GPIO_ISR = (1ul << MC13783_GPIO_LINE);
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
create_thread(mc13783_interrupt_thread, mc13783_thread_stack,
|
|
|
|
sizeof(mc13783_thread_stack), 0, mc13783_thread_name
|
|
|
|
IF_PRIO(, PRIORITY_REALTIME) IF_COP(, CPU));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|