/*
 * MX3 Power Management Routines
 *
 * 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * History:
 *
 * Original code for the SA11x0:
 * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
 *
 * Modified for the PXA250 by Nicolas Pitre:
 * Copyright (c) 2002 Monta Vista Software, Inc.
 *
 * Modified for the OMAP1510 by David Singleton:
 * Copyright (c) 2002 Monta Vista Software, Inc.
 *
 * Cleanup 2004 for OMAP1510/1610 by Dirk Behme <dirk.behme@de.bosch.com>
 *
 * Modified for the MX31
 * Copyright 2005-2007 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * Adapted for newer kernel versions by Thomas Nemeth
 */

#include <linux/suspend.h>
#include <mach/hardware.h>
#include <mach/mx31.h>

#include "crm_regs.h"

#define MXC_CCM_CCMR_WAMO	(1 << 10)
#define MXC_CCM_CCMR_VSTBY	(1 << 28)
#define MXC_CCM_CCMR_L2PG	(1 << 29)

#define WAIT_MODE	0
#define DOZE_MODE	1
#define SRM_MODE	2
#define DSM_MODE	3

#define AVIC_INTENNUM	(MX31_AVIC_BASE_ADDR_VIRT + 0x08)
#define AVIC_INTDISNUM	(MX31_AVIC_BASE_ADDR_VIRT + 0x0C)

#define IPU_CONF	(MX31_AIPS2_BASE_ADDR_VIRT + 0xc0000)

static char 		*mode_name[4] = {"Wait", "Doze", "State Retention", "Deep Sleep"};
static unsigned long	ipu_conf;


static void mx31_pm_set_mode(int mode)
{
	unsigned long flags = MXC_CCM_CCMR_L2PG |  MXC_CCM_CCMR_VSTBY |  MXC_CCM_CCMR_WAMO;
	unsigned long reg;

	printk("Entering suspend mode #%d (%s Mode)...\n", mode, mode_name[mode]);
	switch (mode) {
		case DOZE_MODE: /* Doze mode */
			flags = 0;
			break;
		case SRM_MODE: /* State Retention mode */
			__raw_writel(MX31_INT_GPT, AVIC_INTDISNUM);
			__raw_writel(MX31_INT_GPIO1, AVIC_INTDISNUM);
			/* work-around for SR mode after camera related test */
			__raw_writel(0x51, IPU_CONF);
			break;
		case DSM_MODE: /* Deep Sleep mode */
			/* Disable timer interrupt */
			__raw_writel(MX31_INT_GPT, AVIC_INTDISNUM);
			flags |= MXC_CCM_CCMR_WBEN;
			break;
		default:
		case WAIT_MODE:
			break;
	}

	reg = __raw_readl(MXC_CCM_CCMR);
	reg = (mode & 0x3) << MXC_CCM_CCMR_LPM_OFFSET;
	reg |= flags;
	__raw_writel(reg, MXC_CCM_CCMR);

	/* Executing CP15 (Wait-for-Interrupt) Instruction */
        /* wait for interrupt */
        __asm__ __volatile__("mcr       p15, 0, r1, c7, c0, 4\n"
                             "nop\n" "nop\n" "nop\n" "nop\n" "nop\n"::);
}


static int mx31_pm_valid(suspend_state_t state)
{
	return PM_SUSPEND_STANDBY | PM_SUSPEND_MEM | PM_SUSPEND_MAX;
}


static int mx31_pm_prepare(void)
{
	/* TODO: backup system time if possible */
	ipu_conf = __raw_readl(IPU_CONF);
	return 0;
}


static int mx31_pm_enter(suspend_state_t state)
{
	int mode;

	switch (state) {
		case PM_SUSPEND_STANDBY: mode = DOZE_MODE; break;
		case PM_SUSPEND_MEM:     mode = SRM_MODE;  break;
		case PM_SUSPEND_MAX:     mode = DSM_MODE;  break;
		default:
		case PM_SUSPEND_ON:      mode = WAIT_MODE; break;
	}
	mx31_pm_set_mode(mode);

	return 0;
}


static void mx31_pm_wake(void)
{
	mx31_pm_set_mode(WAIT_MODE);

	__raw_writel(ipu_conf, IPU_CONF);
	__raw_writel(MX31_INT_GPT, AVIC_INTENNUM);
	__raw_writel(MX31_INT_GPIO1, AVIC_INTENNUM);
	/* TODO: restore system time if possible */
}


static struct platform_suspend_ops mx31_pm_ops = {
        .valid          = mx31_pm_valid,
        .prepare        = mx31_pm_prepare,
        .enter          = mx31_pm_enter,
        .wake           = mx31_pm_wake,
};


static int __init mx31_pm_init(void)
{
        suspend_set_ops(&mx31_pm_ops);
        return 0;
}

late_initcall(mx31_pm_init);
