/*
 * linux/arch/arm/mach-at91/board-eye.c
 *
 *  Copyright (C) 2005 SAN People
 *  Copyright (C) 2006 Atmel
 *  Copyright (C) 2009 Jan Breuer
 *
 * 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
 */

#include <linux/types.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>

//#include <linux/spi/at73c213.h>
#include <linux/clk.h>
#include <linux/gpio_keys.h>
#include <linux/input.h>


#include <asm/setup.h>
#include <asm/mach-types.h>
#include <asm/irq.h>

#include <asm/mach/arch.h>
#include <asm/mach/map.h>
#include <asm/mach/irq.h>

#include <mach/hardware.h>
#include <mach/board.h>
#include <mach/gpio.h>
#include <mach/at91_shdwc.h>

#ifdef CONFIG_TPS65010
#include <linux/i2c/tps65010.h>
#endif

#include "generic.h"


static void __init eye_map_io(void)
{
	/* Initialize processor: 18.432 MHz crystal */
	at91sam9260_initialize(18432000);

	/* DGBU on ttyS0. (Rx & Tx only) */
	at91_register_uart(0, 0, 0);

	/* USART0 on ttyS1. (Rx, Tx, CTS, RTS, DTR, DSR, DCD, RI) */
//	at91_register_uart(AT91SAM9260_ID_US0, 1, ATMEL_UART_CTS | ATMEL_UART_RTS
//			   | ATMEL_UART_DTR | ATMEL_UART_DSR | ATMEL_UART_DCD
//			   | ATMEL_UART_RI);

	/* USART1 on ttyS1. (Rx, Tx, RTS, CTS) */
	at91_register_uart(AT91SAM9260_ID_US1, 1, 0);

	/* USART2 on ttyS2. (Rx, Tx, RTS, CTS) */
	at91_register_uart(AT91SAM9260_ID_US2, 2, ATMEL_UART_CTS | ATMEL_UART_RTS);// | ATMEL_UART_DTR);

	/* set serial console to ttyS0 (ie, DBGU) */
	at91_set_serial_console(0);
	
	at91_set_gpio_output(AT91_PIN_PA22, 1);	/* Radio TX OFF */
}

static void __init eye_init_irq(void)
{
	at91sam9260_init_interrupts(NULL);
}

/*
 * GPIO Buttons
 */
#if defined(CONFIG_KEYBOARD_GPIO) || defined(CONFIG_KEYBOARD_GPIO_MODULE)
static struct gpio_keys_button eye_buttons[] = {
	{
		.gpio		= AT91_PIN_PC15,
		.code		= BTN_1,
		.desc		= "Button",
		.active_low	= 1,
		.wakeup		= 1,
	},
	{
		.gpio		= AT91_PIN_PA13,
		.code		= BTN_2,
		.desc		= "GSMON",
		.active_low	= 1,
		.wakeup		= 0,
	}
};

static struct gpio_keys_platform_data eye_button_data = {
	.buttons	= eye_buttons,
	.nbuttons	= ARRAY_SIZE(eye_buttons),
};

static struct platform_device eye_button_device = {
	.name		= "gpio-keys",
	.id		= -1,
	.num_resources	= 0,
	.dev		= {
		.platform_data	= &eye_button_data,
	}
};

static void __init eye_add_device_buttons(void)
{
	at91_set_gpio_input(AT91_PIN_PA13, 0);	/* GSM ON */
	at91_set_deglitch(AT91_PIN_PA13, 1);
	at91_set_gpio_input(AT91_PIN_PC15, 1);	/* Button */
	at91_set_deglitch(AT91_PIN_PC15, 1);
	
	platform_device_register(&eye_button_device);
}
#else
static void __init eye_add_device_buttons(void) {}
#endif

#if defined(CONFIG_VIDEO_ATMEL_ISI) || defined(CONFIG_VIDEO_ATMEL_ISI_MODULE)
static struct at91_isi_data eye_isi_data = {
	.clock_rate	= 24000000,
	.nr_data_pins	= 12,
};
static void __init eye_add_device_isi(void) {
	at91_add_device_isi(&eye_isi_data);
}
#else
static void __init eye_add_device_isi(void) {}
#endif


/*
 * ADC
 */
struct resource adc_resources[] = {
	[0] = {
		.start	= AT91SAM9260_BASE_ADC,
		.end	= AT91SAM9260_BASE_ADC + SZ_16K - 1,
		.flags	= IORESOURCE_MEM,
	},
	[1] = {
		.start	= AT91SAM9260_ID_ADC,
		.end	= AT91SAM9260_ID_ADC,
		.flags	= IORESOURCE_IRQ,
	},
};

static struct platform_device at91sam9260_adc_device = {
	.name		= "at91_adc",
	.id		= -1,
	.resource	= adc_resources,
	.num_resources	= ARRAY_SIZE(adc_resources),
};

static void __init at91_add_device_adc(void)
{
	at91_set_gpio_input(AT91_PIN_PC0, 0);	/* PIR AD0 */
	at91_set_gpio_input(AT91_PIN_PC14, 0);	/* PIR IRQ ? */
	at91_set_gpio_input(AT91_PIN_PC15, 0);	/* PIR IRQ ? */

	platform_device_register(&at91sam9260_adc_device);
}

/*
 * USB Host port
 */
//static struct at91_usbh_data __initdata eye_usbh_data = {
//	.ports		= 0,
//	.ports		= 2,
//};

/*
 * USB Device port
 */
static struct at91_udc_data __initdata eye_udc_data = {
	.vbus_pin	= AT91_PIN_PA31,
	.pullup_pin	= 0,		/* pull-up driven by UDC */
};

/*
 * SPI devices.
 */

// static struct spi_board_info eye_spi_devices[] = {
// 	{
// 		.modalias	= "mtd_dataflash",
// 		.chip_select	= 0,
// 		.max_speed_hz	= 15 * 1000 * 1000,
// 		.bus_num	= 0,
// 	},
// };
//

#ifdef CONFIG_I2C_BOARDINFO
static struct i2c_board_info __initdata eye_i2c_devices[] = {
	{
		I2C_BOARD_INFO("tps65011", 0x48),
		.type           = "tps65010",
	},
	{
		I2C_BOARD_INFO("24lc128", 0x50),
		.type           = "eeprom",
	},
	{
		I2C_BOARD_INFO("mt9v011", 0x5d),
	},
};
#endif


/*
 * MCI (SD/MMC)
 */
static struct at91_mmc_data __initdata eye_mmc_data = {
	.slot_b		= 0,
	.wire4		= 1,
//	.det_pin	= ... not connected
//	.wp_pin		= ... not connected
//	.vcc_pin	= ... not connected
};


/*
 * LEDs
 */
static struct gpio_led eye_leds[] = {
	{	/* green led */
		.name			= "green",
		.gpio			= AT91_PIN_PA17,
		.active_low		= 0,
		.default_trigger	= "none",
	},
	{	/* red led */
		.name			= "red",
		.gpio			= AT91_PIN_PA18,
		.default_trigger	= "heartbeat",
	},
	{	/* infra red led */
		.name			= "ir",
		.gpio			= AT91_PIN_PA19,
		.active_low		= 0,
		.default_trigger	= "none",
	},
};

static void __init eye_board_init(void)
{
	/* Serial */
	at91_add_device_serial();

	//	/* USB Host */
	//	at91_add_device_usbh(&eye_usbh_data);
	
	/* USB Device */
	at91_add_device_udc(&eye_udc_data);
	
	/* SPI */
	//	at91_add_device_spi(eye_spi_devices, ARRAY_SIZE(eye_spi_devices));

	//	/* NAND */
	//	at91_add_device_nand(&ek_nand_data);
	
	/* Ethernet */
	//	at91_add_device_eth(&ek_macb_data);
	//#if defined(CONFIG_MMC_AT91)
	/* disable spi0 clock */
	//	at91_set_gpio_input(AT91_PIN_PA2, 0);  /* SPI0_SPCK shared with MCCK */
	
	/* MMC */
	at91_add_device_mmc(1, &eye_mmc_data);
	//#endif
	
	/* I2C */
	at91_add_device_i2c(eye_i2c_devices, ARRAY_SIZE(eye_i2c_devices));
	
	/* SSC (to AT73C213) */
	//	at73c213_set_clk(&at73c213_data);
	//	at91_add_device_ssc(AT91SAM9260_ID_SSC, ATMEL_SSC_TX);
	
	/* ISI */
	eye_add_device_isi();
	
	/* LEDs */
	at91_gpio_leds(eye_leds, ARRAY_SIZE(eye_leds));
	
	/* ADC */	
	at91_add_device_adc();

	/* Push Buttons */
	eye_add_device_buttons();
	
	/* shutdown controller, wakeup button (5 msec low) */
	at91_sys_write(AT91_SHDW_MR, AT91_SHDW_CPTWK0_(10) | AT91_SHDW_WKMODE0_LOW
				| AT91_SHDW_RTTWKEN);
}

#ifdef CONFIG_TPS65010
static int __init eye_tps_init(void)
{
	if (!machine_is_eye02())
		return 0;
		
	/* Enable LOW_PWR */
	tps65010_set_low_pwr(ON);
		return 0;
}
fs_initcall(eye_tps_init);
#endif


MACHINE_START(EYE02, "JabloCOM EYE-02")
	/* Maintainer: Atmel */
	.phys_io	= AT91_BASE_SYS,
	.io_pg_offst	= (AT91_VA_BASE_SYS >> 18) & 0xfffc,
	.boot_params	= AT91_SDRAM_BASE + 0x100,
	.timer		= &at91sam926x_timer,
	.map_io		= eye_map_io,
	.init_irq	= eye_init_irq,
	.init_machine	= eye_board_init,
MACHINE_END
