/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 by Bertrik Sikken
 *
 * 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.
 *
 ****************************************************************************/

#include "config.h"
#include "as3525.h"
#include "dbop-as3525.h"

#define DBOP_IDLE_VALUE 0xE0FF

static int dbop_locked = 0;
static short int dbop_input_value = DBOP_IDLE_VALUE;

/* read the DBOP data pins */
static unsigned short dbop_read_input(void)
{
    unsigned int dbop_ctrl_old = DBOP_CTRL;
    unsigned int dbop_timpol23_old = DBOP_TIMPOL_23;
    unsigned short value;

    /* make sure that the DBOP FIFO is empty */
    while ((DBOP_STAT & (1<<10)) == 0);

    /* write DBOP_DOUT to pre-charge DBOP data lines with a high level */
    DBOP_TIMPOL_23 = 0xe167006e;    /* no strobe towards lcd */

    if (dbop_locked <= 0)
    {   /* can't write to dbop while updating */
        DBOP_CTRL = (1 << 16) |         /* enw=1 (enable write) */
                    (1 << 12);          /* ow=1 (16-bit data width) */
        DBOP_DOUT = DBOP_IDLE_VALUE;    /* all pins high */
        while ((DBOP_STAT & (1<<10)) == 0);
    }

    /* perform a DBOP read */
    DBOP_CTRL = (1 << 15) |         /* strd=1 (start read) */
                (1 << 12) |         /* ow=1 (16-bit data width) */
                (31 << 0);          /* rs_t=31 (read DBOP at end of cycle) */
    while ((DBOP_STAT & (1<<16)) == 0);
    value = DBOP_DIN;

    /* restore previous values */
    DBOP_TIMPOL_23 = dbop_timpol23_old;
    DBOP_CTRL = dbop_ctrl_old;

    if (dbop_locked > 0)
    {   /* if we were blocked, use cached data partly */
        int old_value = value;
        value = dbop_input_value & ~(1<<14|1<<13);
        value |= old_value & (1<<14|1<<13);
    }
    return value;
}

/* get the DBOP input value, either directly or cached if DBOP is busy */
unsigned short int dbop_input(void)
{
    return (dbop_input_value = dbop_read_input());
}

/* increase dbop lock count, if not locked yet a DBOP input sample is taken */
void dbop_lock(void)
{
    dbop_input();
    dbop_locked++;
}

/* decrease dbop lock count */
void dbop_unlock(void)
{
    dbop_locked--;
}