/*
 * (C) Copyright 2006 by OpenMoko, Inc.
 * Author: Harald Welte <laforge@openmoko.org>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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
 */

/*
 * Boot support
 */
#include <common.h>
#include <command.h>
#include <net.h>		/* for print_IPaddr */
#include <s3c2410.h>

#include "neo1973.h"
#include "pcf50606.h"

DECLARE_GLOBAL_DATA_PTR;

#if (CONFIG_COMMANDS & CFG_CMD_BDI)

static const char *chgstate_names[] = {
	[PCF50606_MBCC1_CHGMOD_QUAL]		= "qualification",
	[PCF50606_MBCC1_CHGMOD_PRE]		= "pre",
	[PCF50606_MBCC1_CHGMOD_TRICKLE]		= "trickle",
	[PCF50606_MBCC1_CHGMOD_FAST_CCCV]	= "fast_cccv",
	[PCF50606_MBCC1_CHGMOD_FAST_NOCC]	= "fast_nocc",
	[PCF50606_MBCC1_CHGMOD_FAST_NOCV]	= "fast_nocv",
	[PCF50606_MBCC1_CHGMOD_FAST_SW]		= "fast_switch",
	[PCF50606_MBCC1_CHGMOD_IDLE]		= "idle",
};

int do_neo1973 ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int i;

	if (!strcmp(argv[1], "info")) {
		printf("FIC GTA01 Hardware Revision 0x%04x\n", get_board_rev());
	} else if (!strcmp(argv[1], "power-off")) {
		neo1973_poweroff();
	} else if (!strcmp(argv[1], "charger") || !strcmp(argv[1], "charge")) {
		if (argc < 3)
			goto out_help;
		if (!strcmp(argv[2], "status") || !strcmp(argv[2], "state")) {
			u_int8_t mbcc1 = pcf50606_reg_read(PCF50606_REG_MBCC1);
			u_int8_t chgmod = (mbcc1 & PCF50606_MBCC1_CHGMOD_MASK);
			printf("%s\n", chgstate_names[chgmod]);
		} else if (!strcmp(argv[2], "autofast")) {
			pcf50606_reg_set_bit_mask(PCF50606_REG_MBCC1,
						  PCF50606_MBCC1_AUTOFST,
						  PCF50606_MBCC1_AUTOFST);
		} else if (!strcmp(argv[2], "!autofast")) {
			pcf50606_reg_set_bit_mask(PCF50606_REG_MBCC1,
						  PCF50606_MBCC1_AUTOFST, 0);
		} else if (!strcmp(argv[2], "off")) {
			pcf50606_reg_set_bit_mask(PCF50606_REG_MBCC1,
						  PCF50606_MBCC1_CHGMOD_MASK,
						  PCF50606_MBCC1_CHGMOD_IDLE);
		} else if (!strcmp(argv[2], "fast")) {
			pcf50606_reg_set_bit_mask(PCF50606_REG_MBCC1,
						  PCF50606_MBCC1_CHGMOD_MASK,
						  PCF50606_MBCC1_CHGMOD_FAST_CCCV);
		} else
			goto out_help;
	} else if (!strcmp(argv[1], "backlight")) {
		if (argc < 3)
			goto out_help;
		if (!strcmp(argv[2], "on"))
			neo1973_backlight(1);
		else
			neo1973_backlight(0);
	} else if (!strcmp(argv[1], "vibrator")) {
		if (argc < 3)
			goto out_help;
		if (!strcmp(argv[2], "on"))
			neo1973_vibrator(1);
		else
			neo1973_vibrator(0);
	} else if (!strcmp(argv[1], "udc")) {
		if (argc < 3)
			goto out_help;
		if (!strcmp(argv[2], "udc")) {
			if (argc < 4)
				goto out_help;
			if (!strcmp(argv[3], "on"))
				udc_connect();
			else
				udc_disconnect();
		} else
			goto out_help;
	} else {
out_help:
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

	return 0;
}

/* -------------------------------------------------------------------- */

U_BOOT_CMD(
	neo1973,	4,	1,	do_neo1973,
	"neo1973 - phone specific commands\n",
	"neo1973 info - display phone informantion\n"
	"neo1973 power-off - switch off the phone\n"
	"neo1973 charger status - display charger status\n"
	"neo1973 charger autofast - enable automatic fast (500mA) charging\n"
	"neo1973 charger !autofast - disable automatic fast (500mA) charging\n"
	"neo1973 charger fast - enable fast (500mA) charging\n"
	"neo1973 charger off - disable charging\n"
	"neo1973 backlight (on|off) - switch backlight on or off\n"
	"neo1973 vibrator (on|off) - switch vibrator on or off\n"
	"neo1973 udc pullup (on|off) - switch pull-up on or off\n"
);
#endif	/* CFG_CMD_BDI */
