/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/kernel_stat.h>
#include <linux/delay.h>

#include <asm/mips-boards/ar7.h>
#include <asm/mach_avm.h>
#include <asm/mach-ar7/hw_clock.h>

static struct _ar7_gpio_power_bits {
    unsigned int Bit_Mask;
    unsigned int Polarity_Mask;
} ar7_gpio_power_bits;

static spinlock_t ar7_power_spinlock;

/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
int ar7_power_down_init(void) {
    spin_lock_init(&ar7_power_spinlock);
    return 0;
}

/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
int ar7_put_device_into_power_down(unsigned int Bit) {
    struct _hw_clock *clock = (struct _hw_clock *)AR7_CLOCK_BASE;
    if(Bit < AR7_POWER_START_GPIO) {
        unsigned int flags;
        spin_lock_irqsave(&ar7_power_spinlock, flags);
        clock->PDCR.Register |= (1 << Bit);
        spin_unlock_irqrestore(&ar7_power_spinlock, flags);
        return 0;
    }
    if(Bit < AR7_POWER_END_GPIO) {
        Bit -= AR7_POWER_START_GPIO;
        ar7_gpio_out_bit(Bit, (ar7_gpio_power_bits.Polarity_Mask & (1 << Bit)) ? 1 : 0);
        return 0;
    }
#if defined(CONFIG_VLYNQ_SUPPORT)
    if(Bit < AR7_POWER_END_VIRTUAL) {
        return 0;
    }
#endif /*--- #if defined(CONFIG_VLYNQ_SUPPORT) ---*/
    return 1;
}

/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
int ar7_take_device_out_of_power_down(unsigned int Bit) {
    struct _hw_clock *clock = (struct _hw_clock *)AR7_CLOCK_BASE;
    if(Bit < AR7_POWER_START_GPIO) {
        unsigned int flags;
        spin_lock_irqsave(&ar7_power_spinlock, flags);
        clock->PDCR.Register &= ~(1 << Bit);
        spin_unlock_irqrestore(&ar7_power_spinlock, flags);
        return 0;
    }
    if(Bit < AR7_POWER_END_GPIO) {
        Bit -= AR7_POWER_START_GPIO;
        ar7_gpio_out_bit(Bit, (ar7_gpio_power_bits.Polarity_Mask & (1 << Bit)) ? 0 : 1);
        return 0;
    }
#if defined(CONFIG_VLYNQ_SUPPORT)
    if(Bit < AR7_POWER_END_VIRTUAL) {
        return 0;
    }
#endif /*--- #if defined(CONFIG_VLYNQ_SUPPORT) ---*/
    return 1;
}

/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
int ar7_register_power_down_gpio(unsigned int gpio_pin, unsigned int Polarity) {

    if(gpio_pin >= 32)
        return (unsigned int)-1;

    if(ar7_gpio_power_bits.Bit_Mask & (1 << gpio_pin)) 
        return (unsigned int)-1;

    ar7_gpio_power_bits.Bit_Mask      |= (1 << gpio_pin);
    if(Polarity)
        ar7_gpio_power_bits.Polarity_Mask |=  (1 << Polarity);
    else
        ar7_gpio_power_bits.Polarity_Mask &= ~(1 << Polarity);

    ar7_gpio_ctrl(gpio_pin, GPIO_PIN, GPIO_OUTPUT_PIN);
    return gpio_pin + AR7_POWER_START_GPIO;
}

/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
int ar7_release_power_down_gpio(unsigned int Bit) {
    unsigned int gpio_pin = Bit - AR7_POWER_START_GPIO;
    if(gpio_pin >= 32)
        return (unsigned int)-1;
    if(!(ar7_gpio_power_bits.Bit_Mask & (1 << gpio_pin)))
        return (unsigned int)-1;
    ar7_gpio_power_bits.Bit_Mask &= ~(1 << gpio_pin);
    return 0;
}

/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
unsigned int ar7_power_down_status(void) {
    struct _hw_clock *clock = (struct _hw_clock *)AR7_CLOCK_BASE;
    return (unsigned int)clock->PDCR.Register;
}

EXPORT_SYMBOL(ar7_put_device_into_power_down);
EXPORT_SYMBOL(ar7_take_device_out_of_power_down);
EXPORT_SYMBOL(ar7_register_power_down_gpio);
EXPORT_SYMBOL(ar7_release_power_down_gpio);
EXPORT_SYMBOL(ar7_power_down_status);

