/*
 *  TPE-R1100 board support
 *
 *  Copyright (C) 2011 dongyuqi <729650915@qq.com>
 *  Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
 *  Copyright (C) 2013 alzhao <alzhao@gmail.com>
 *  Copyright (C) 2014 Michel Stempin <michel.stempin@wanadoo.fr>
 *  Copyright (C) 2015 Robert Call <bob@librcmc.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
*/

#include <linux/gpio.h>

#include <asm/mach-ath79/ath79.h>

#include "dev-eth.h"
#include "dev-gpio-buttons.h"
#include "dev-leds-gpio.h"
#include "dev-m25p80.h"
#include "dev-usb.h"
#include "dev-wmac.h"
#include "machtypes.h"

#define TPE_R1100_GPIO_LED_WLAN		   0
#define TPE_R1100_GPIO_LED_LAN		   13
#define TPE_R1100_GPIO_LED_WAN		   15 

#define TPE_R1100_GPIO_BIN_USB         6
#define TPE_R1100_GPIO_BTN_MANUAL      7
#define TPE_R1100_GPIO_BTN_AUTO	   	   8
#define TPE_R1100_GPIO_BTN_RESET	   11

#define TPE_R1100_KEYS_POLL_INTERVAL   20	/* msecs */
#define TPE_R1100_KEYS_DEBOUNCE_INTERVAL	(3 * TPE_R1100_KEYS_POLL_INTERVAL)

#define TPE_R1100_MAC0_OFFSET	0x0000
#define TPE_R1100_MAC1_OFFSET	0x0000
#define TPE_R1100_CALDATA_OFFSET	0x1000
#define TPE_R1100_WMAC_MAC_OFFSET	0x0000

static struct gpio_led tpe_r1100_leds_gpio[] __initdata = {
	{
		.name = "tpe_r1100:wlan",
		.gpio = TPE_R1100_GPIO_LED_WLAN,
		.active_low = 0,
	},
	{
		.name = "tpe_r1100:lan",
		.gpio = TPE_R1100_GPIO_LED_LAN,
		.active_low = 0,
	},
	{
		.name = "tpe_r1100:wan",
		.gpio = TPE_R1100_GPIO_LED_WAN,
		.active_low = 0,
 		.default_state = 1,
	},
};

static struct gpio_keys_button tpe_r1100_gpio_keys[] __initdata = {
	{
		.desc = "BTN_7",
		.type = EV_KEY,
		.code = BTN_7,
		.debounce_interval = TPE_R1100_KEYS_DEBOUNCE_INTERVAL,
		.gpio = TPE_R1100_GPIO_BTN_MANUAL,
		.active_low = 0,
	},
	{
		.desc = "BTN_8",
		.type = EV_KEY,
		.code = BTN_8,
		.debounce_interval = TPE_R1100_KEYS_DEBOUNCE_INTERVAL,
		.gpio = TPE_R1100_GPIO_BTN_AUTO,
		.active_low = 0,
	},
	{
		.desc = "reset",
		.type = EV_KEY,
		.code = KEY_RESTART,
		.debounce_interval = TPE_R1100_KEYS_DEBOUNCE_INTERVAL,
		.gpio = TPE_R1100_GPIO_BTN_RESET,
		.active_low = 0,
	},
};

static void __init tpe_r1100_setup(void)
{

	/* ART base address */
	u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);

	/* disable PHY_SWAP and PHY_ADDR_SWAP bits */
	ath79_setup_ar933x_phy4_switch(false, false);

	/* register flash. */
	ath79_register_m25p80(NULL);

	/* register gpio LEDs and keys */
	ath79_register_leds_gpio(-1, ARRAY_SIZE(tpe_r1100_leds_gpio),
				 tpe_r1100_leds_gpio);
	ath79_register_gpio_keys_polled(-1, TPE_R1100_KEYS_POLL_INTERVAL,
					ARRAY_SIZE(tpe_r1100_gpio_keys),
					tpe_r1100_gpio_keys);

	/* enable usb */
	ath79_register_usb();

	/* register eth0 as WAN, eth1 as LAN */
	ath79_init_mac(ath79_eth0_data.mac_addr, art+TPE_R1100_MAC1_OFFSET, 0);
	ath79_init_mac(ath79_eth1_data.mac_addr, art+TPE_R1100_MAC0_OFFSET, 0);
	ath79_register_mdio(0, 0x0);
	ath79_register_eth(0);
	ath79_register_eth(1);

	/* register wireless mac with cal data */
	ath79_register_wmac(art + TPE_R1100_CALDATA_OFFSET, art + TPE_R1100_WMAC_MAC_OFFSET);
}

MIPS_MACHINE(ATH79_MACH_TPE_R1100, "TPE-R1100", "Think Penguin Mini TPE-R1100",tpe_r1100_setup);
