/*
 * (c) 2010 STMicroelectronics Limited
 *
 * Author: Pawel Moll <pawel.moll@st.com>
 *
 * 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/init.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/stm/pad.h>
#include <linux/stm/sysconf.h>
#include <linux/stm/stx7141.h>
#include <asm/irq-ilc.h>



/* ASC resources ---------------------------------------------------------- */

static struct stm_pad_config stx7141_asc0_mcard_pad_config = {
	.sysconfs_num = 4, /* !!! see stx7141_configure_asc() */
	.sysconfs = (struct stm_pad_sysconf []) {
		/* MCARD_*: M-Card (0) or glue (1) */
		STM_PAD_SYS_CFG(36, 16, 16, 1),
		/* ASC0_TXD: output enabled, no pull-up, not open drain */
		STM_PAD_SYS_CFG(36, 19, 21, 2),
		/* ASC0_RXD: input */
		STM_PAD_SYS_CFG(36, 22, 24, 0),
		/* ASC0_CTS & ASC0_RTS: ??? */
		STM_PAD_SYS_CFG(36, 17, 18, 2),
	},
};

static struct stm_pad_config stx7141_asc1_pio10_pad_config = {
	.gpios_num = 4,
	.gpios = (struct stm_pad_gpio []) {
		STM_PAD_PIO_OUT(10, 0, 3),	/* TX */
		STM_PAD_PIO_IN(10, 1, -1),	/* RX */
		STM_PAD_PIO_IN_NAMED(10, 2, -1, "CTS"),
		STM_PAD_PIO_OUT_NAMED(10, 3, 3, "RTS"),
	},
	.sysconfs_num = 1,
	.sysconfs = (struct stm_pad_sysconf []) {
		/* ASC1_RXD/ASC1_CTS: from PIO10 (0) or from MCard (1) */
		STM_PAD_SYS_CFG(36, 29, 29, 0),
	},
};

static struct stm_pad_config stx7141_asc1_mcard_pad_config = {
	.sysconfs_num = 5,
	.sysconfs = (struct stm_pad_sysconf []) {
		/* MCARD_*: M-Card (0) or glue (1) */
		STM_PAD_SYS_CFG(36, 16, 16, 1),
		/* ASC1_TXD/ASC1_RTS: output enabled */
		STM_PAD_SYS_CFG(36, 15, 15, 1),
		/* ASC1_RXD/ASC1_CTS: from PIO10 (0) or from MCard (1) */
		STM_PAD_SYS_CFG(36, 29, 29, 1),
		/* ASC1_RXD: input */
		STM_PAD_SYS_CFG(36, 14, 14, 0),
		/* ASC1_RTS: input */
		STM_PAD_SYS_CFG(36, 13, 13, 0),
	},
};

static struct stm_pad_config stx7141_asc2_pio1_pad_config = {
	.gpios_num = 4, /* !!! see stx7141_configure_asc() */
	.gpios = (struct stm_pad_gpio []) {
		STM_PAD_PIO_OUT(1, 0, 3),	/* TX */
		STM_PAD_PIO_IN(1, 1, -1),	/* RX */
		STM_PAD_PIO_IN_NAMED(1, 2, -1, "CTS"),
		STM_PAD_PIO_OUT_NAMED(1, 3, 3, "RTS"),
	},
	.sysconfs_num = 1,
	.sysconfs = (struct stm_pad_sysconf []) {
		/* ASC2_RXD/ASC2_CTS: from PIO1 (0) or from PIO6 (3) */
		STM_PAD_SYS_CFG(36, 30, 31, 0),
	},
};

static struct stm_pad_config stx7141_asc2_pio6_pad_config = {
	.gpios_num = 4,
	.gpios = (struct stm_pad_gpio []) {
		STM_PAD_PIO_OUT(6, 0, 3),	/* TX */
		STM_PAD_PIO_IN(6, 1, -1),	/* RX */
		STM_PAD_PIO_IN_NAMED(6, 2, -1, "CTS"),
		STM_PAD_PIO_OUT_NAMED(6, 3, 3, "RTS"),
	},
	.sysconfs_num = 1,
	.sysconfs = (struct stm_pad_sysconf []) {
		/* ASC2_RXD/ASC2_CTS: from PIO1 (0) or from PIO6 (3) */
		STM_PAD_SYS_CFG(36, 30, 31, 3),
	},
};

static struct platform_device stx7141_asc_devices[] = {
	[0] = {
		.name		= "stm-asc",
		/* .id set in stx7141_configure_asc() */
		.num_resources	= 4,
		.resource	= (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd030000, 0x2c),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(76), -1),
			STM_PLAT_RESOURCE_DMA_NAMED("rx_half_full", 11),
			STM_PLAT_RESOURCE_DMA_NAMED("tx_half_empty", 15),
		},
		.dev.platform_data = &(struct stm_plat_asc_data) {
			.pad_config = &stx7141_asc0_mcard_pad_config,
		},
	},
	[1] = {
		.name		= "stm-asc",
		/* .id set in stx7141_configure_asc() */
		.num_resources	= 4,
		.resource	= (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd031000, 0x2c),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(77), -1),
			STM_PLAT_RESOURCE_DMA_NAMED("rx_half_full", 12),
			STM_PLAT_RESOURCE_DMA_NAMED("tx_half_empty", 16),
		},
		.dev.platform_data = &(struct stm_plat_asc_data) {
			/* .pad_config set in stx7141_configure_asc() */
		},
	},
	[2] = {
		.name		= "stm-asc",
		/* .id set in stx7141_configure_asc() */
		.num_resources	= 4,
		.resource	= (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd032000, 0x2c),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(78), -1),
			STM_PLAT_RESOURCE_DMA_NAMED("rx_half_full", 13),
			STM_PLAT_RESOURCE_DMA_NAMED("tx_half_empty", 17),
		},
		.dev.platform_data = &(struct stm_plat_asc_data) {
			/* .pad_config set in stx7141_configure_asc() */
		},
	},
};

/* Note these three variables are global, and shared with the stasc driver
 * for console bring up prior to platform initialisation. */

/* the serial console device */
int __initdata stm_asc_console_device;

/* Platform devices to register */
unsigned int __initdata stm_asc_configured_devices_num = 0;
struct platform_device __initdata
		*stm_asc_configured_devices[ARRAY_SIZE(stx7141_asc_devices)];

void __init stx7141_configure_asc(int asc, struct stx7141_asc_config *config)
{
	static int configured[ARRAY_SIZE(stx7141_asc_devices)];
	static int tty_id;
	struct stx7141_asc_config default_config = {};
	struct platform_device *pdev;
	struct stm_plat_asc_data *plat_data;
	struct stm_pad_config *pad_config = NULL;

	BUG_ON(asc < 0 || asc >= ARRAY_SIZE(stx7141_asc_devices));

	BUG_ON(configured[asc]);
	configured[asc] = 1;

	if (!config)
		config = &default_config;

	pdev = &stx7141_asc_devices[asc];
	plat_data = pdev->dev.platform_data;

	pdev->id = tty_id++;
	plat_data->hw_flow_control = config->hw_flow_control;

	switch (asc) {
	case 0:
		pad_config = &stx7141_asc0_mcard_pad_config;
		if (!config->hw_flow_control) {
			stm_pad_set_pio_ignored(pad_config, "RTS");
			stm_pad_set_pio_ignored(pad_config, "CTS");
			pad_config->sysconfs_num--;
		}
		BUG_ON(config->routing.asc0 != stx7141_asc0_mcard);
		break;
	case 1:
		switch (config->routing.asc1) {
		case stx7141_asc1_pio10:
			pad_config = &stx7141_asc1_pio10_pad_config;
			if (!config->hw_flow_control) {
				stm_pad_set_pio_ignored(pad_config, "RTS");
				stm_pad_set_pio_ignored(pad_config, "CTS");
			}
			break;
		case stx7141_asc1_mcard:
			pad_config = &stx7141_asc1_mcard_pad_config;
			/* The HW flow control will be available anyway */
			WARN_ON(!config->hw_flow_control);
			break;
		default:
			BUG();
			break;
		}
		break;
	case 2:
		switch (config->routing.asc2) {
		case stx7141_asc2_pio1:
			pad_config = &stx7141_asc2_pio1_pad_config;
			break;
		case stx7141_asc2_pio6:
			pad_config = &stx7141_asc2_pio6_pad_config;
			break;
		default:
			BUG();
			break;
		}
		if (!config->hw_flow_control) {
			stm_pad_set_pio_ignored(pad_config, "RTS");
			stm_pad_set_pio_ignored(pad_config, "CTS");
		}
		break;
	default:
		BUG();
		break;
	}

	plat_data->pad_config = pad_config;

	if (config->is_console)
		stm_asc_console_device = pdev->id;

	stm_asc_configured_devices[stm_asc_configured_devices_num++] = pdev;
}

/* Add platform device as configured by board specific code */
static int __init stx7141_add_asc(void)
{
	return platform_add_devices(stm_asc_configured_devices,
			stm_asc_configured_devices_num);
}
arch_initcall(stx7141_add_asc);



/* SSC resources ---------------------------------------------------------- */

/* WARNING! SSCs were numbered starting from 1 in early documents.
 * Later it was changed and this approach is used below,
 * so the first SSC is SSC0 (zero). */

/* Pad configuration for I2C mode */
static struct stm_pad_config stx7141_ssc_i2c_pad_configs[] = {
	[0] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(2, 0, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(2, 1, 1, "SDA"),
		},
	},
	[1] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(2, 3, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(2, 4, 1, "SDA"),
		},
	},
	[2] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(2, 6, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(2, 7, 1, "SDA"),
		},
	},
	[3] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(3, 0, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(3, 1, 1, "SDA"),
		},
	},
	[4] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(3, 2, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(3, 3, 1, "SDA"),
		},
	},
	[5] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(3, 4, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(3, 5, 1, "SDA"),
		},
	},
	[6] = {
		.gpios_num = 2,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_BIDIR_NAMED(4, 0, 1, "SCL"),
			STM_PAD_PIO_BIDIR_NAMED(4, 1, 1, "SDA"),
		},
	},
};

/* Pad configuration for SPI mode */
static struct stm_pad_config stx7141_ssc_spi_pad_configs[] = {
	[0] = {
		.gpios_num = 3,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_OUT(2, 0, 1),	/* SCK */
			STM_PAD_PIO_OUT(2, 1, 1),	/* MOSI */
			STM_PAD_PIO_IN(2, 2, -1),	/* MISO */
		},
	},
	[1] = {
		.gpios_num = 3,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_OUT(2, 3, 1),	/* SCK */
			STM_PAD_PIO_OUT(2, 4, 1),	/* MOSI */
			STM_PAD_PIO_IN(2, 5, -1),	/* MISO */
		},
	},
	[5] = {
		.gpios_num = 3,
		.gpios = (struct stm_pad_gpio []) {
			STM_PAD_PIO_OUT(3, 4, 1),	/* SCK */
			STM_PAD_PIO_OUT(3, 5, 1),	/* MOSI */
			STM_PAD_PIO_IN(3, 6, -1),	/* MISO */
		},
	},
};

static struct platform_device stx7141_ssc_devices[] = {
	[0] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd040000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(69), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
	[1] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd041000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(70), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
	[2] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd042000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(71), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
	[3] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd043000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(72), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
	[4] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd044000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(73), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
	[5] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd045000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(74), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
	[6] = {
		/* .name & .id set in stx7141_configure_ssc_*() */
		.num_resources = 2,
		.resource = (struct resource[]) {
			STM_PLAT_RESOURCE_MEM(0xfd046000, 0x110),
			STM_PLAT_RESOURCE_IRQ(ILC_IRQ(75), -1),
		},
		.dev.platform_data = &(struct stm_plat_ssc_data) {
			/* .pad_config_* set in stx7141_configure_ssc_*() */
		},
	},
};

static int __initdata stx7141_ssc_configured[ARRAY_SIZE(stx7141_ssc_devices)];

int __init stx7141_configure_ssc_i2c(int ssc)
{
	static int i2c_busnum;
	struct stm_plat_ssc_data *plat_data;

	BUG_ON(ssc < 0 || ssc >= ARRAY_SIZE(stx7141_ssc_devices));

	BUG_ON(stx7141_ssc_configured[ssc]);
	stx7141_ssc_configured[ssc] = 1;

	stx7141_ssc_devices[ssc].name = "i2c-stm";
	stx7141_ssc_devices[ssc].id = i2c_busnum;

	plat_data = stx7141_ssc_devices[ssc].dev.platform_data;
	plat_data->pad_config = &stx7141_ssc_i2c_pad_configs[ssc];

	/* I2C bus number reservation (to prevent any hot-plug device
	 * from using it) */
	i2c_register_board_info(i2c_busnum, NULL, 0);

	platform_device_register(&stx7141_ssc_devices[ssc]);

	return i2c_busnum++;
}

int __init stx7141_configure_ssc_spi(int ssc,
		struct stx7141_ssc_spi_config *config)
{
	static int spi_busnum;
	struct stm_plat_ssc_data *plat_data;

	BUG_ON(ssc < 0 || ssc >= ARRAY_SIZE(stx7141_ssc_devices));

	BUG_ON(stx7141_ssc_configured[ssc]);
	stx7141_ssc_configured[ssc] = 1;

	/* These two SSC can't be used in SPI mode - there is no
	 * MRST pin available */
	BUG_ON(ssc == 2 || ssc == 4);

	stx7141_ssc_devices[ssc].name = "spi-stm";
	stx7141_ssc_devices[ssc].id = spi_busnum;

	plat_data = stx7141_ssc_devices[ssc].dev.platform_data;
	if (config)
		plat_data->spi_chipselect = config->chipselect;
	plat_data->pad_config = &stx7141_ssc_spi_pad_configs[ssc];

	platform_device_register(&stx7141_ssc_devices[ssc]);

	return spi_busnum++;
}



/* LiRC resources --------------------------------------------------------- */

static struct platform_device stx7141_lirc_device = {
	.name = "lirc-stm",
	.id = -1,
	.num_resources = 2,
	.resource = (struct resource []) {
		STM_PLAT_RESOURCE_MEM(0xfd018000, 0x234),
		STM_PLAT_RESOURCE_IRQ(ILC_IRQ(81), -1),
		STM_PLAT_RESOURCE_IRQ(COMMS_ILC_EXT_IRQ(86), -1),
	},
	.dev.platform_data = &(struct stm_plat_lirc_data) {
		/* The clock settings will be calculated by
		 * the driver from the system clock */
		.irbclock	= 0, /* use current_cpu data */
		.irbclkdiv	= 0, /* automatically calculate */
		.irbperiodmult	= 0,
		.irbperioddiv	= 0,
		.irbontimemult	= 0,
		.irbontimediv	= 0,
		.irbrxmaxperiod = 0x5000,
		.sysclkdiv	= 1,
		.rxpolarity	= 1,
	},
};

void __init stx7141_configure_lirc(struct stx7141_lirc_config *config)
{
	static int configured;
	struct stx7141_lirc_config default_config = {};
	struct stm_plat_lirc_data *plat_data =
			stx7141_lirc_device.dev.platform_data;
	struct stm_pad_config *pad_config;

	BUG_ON(configured);
	configured = 1;

	if (!config)
		config = &default_config;

	pad_config = stm_pad_config_alloc(3, 0);
	BUG_ON(!pad_config);

	plat_data->txenabled = config->tx_enabled || config->tx_od_enabled;
	plat_data->pads = pad_config;

	switch (config->rx_mode) {
	case stx7141_lirc_rx_disabled:
		/* Nothing to do */
		break;
	case stx7141_lirc_rx_mode_ir:
		plat_data->rxuhfmode = 0;
		stm_pad_config_add_pio_in(pad_config, 5, 2, -1);
		break;
	case stx7141_lirc_rx_mode_uhf:
		plat_data->rxuhfmode = 1;
		stm_pad_config_add_pio_in(pad_config, 3, 7, -1);
		break;
	default:
		BUG();
		break;
	}

	if (config->tx_enabled)
		stm_pad_config_add_pio_out(pad_config, 5, 3, 1);

	if (config->tx_od_enabled)
		stm_pad_config_add_pio_out(pad_config, 5, 4, 1);

	platform_device_register(&stx7141_lirc_device);
}



/* PWM resources ---------------------------------------------------------- */

static struct stm_plat_pwm_data stx7141_pwm_platform_data = {
	.channel_pad_config = {
		[0] = &(struct stm_pad_config) {
			.gpios_num = 1,
			.gpios = (struct stm_pad_gpio []) {
				STM_PAD_PIO_OUT(4, 3, 2),
			},
		},
		[1] = &(struct stm_pad_config) {
			.gpios_num = 1,
			.gpios = (struct stm_pad_gpio []) {
				STM_PAD_PIO_OUT(4, 2, 2),
			},
		},
	},
};

static struct platform_device stx7141_pwm_device = {
	.name = "stm-pwm",
	.id = -1,
	.num_resources = 2,
	.resource = (struct resource[]) {
		STM_PLAT_RESOURCE_MEM(0xfd010000, 0x68),
		STM_PLAT_RESOURCE_IRQ(ILC_IRQ(85), -1),
	},
	.dev.platform_data = &stx7141_pwm_platform_data,
};

void __init stx7141_configure_pwm(struct stx7141_pwm_config *config)
{
	static int configured;

	BUG_ON(configured);
	configured = 1;

	if (config) {
		stx7141_pwm_platform_data.channel_enabled[0] =
			config->out0_enabled;
		stx7141_pwm_platform_data.channel_enabled[1] =
			config->out1_enabled;
	}

	platform_device_register(&stx7141_pwm_device);
}

/* Low Power Controller ---------------------------------------------------- */

static struct platform_device stx7141_lpc_device = {
	.name           = "stm-rtc",
	.id             = -1,
	.num_resources  = 2,
	.resource       = (struct resource[]){
		STM_PLAT_RESOURCE_MEM(0xfd008000, 0x600),
		STM_PLAT_RESOURCE_IRQ(ILC_IRQ(83), -1),
	},
	.dev.platform_data = &(struct stm_plat_rtc_lpc) {
		.clk_id = "CLKB_LPC",
		.need_wdt_reset = 1,
		.irq_edge_level = IRQ_TYPE_EDGE_FALLING,
	}
};

static int __init stx7141_add_lpc(void)
{
	return platform_device_register(&stx7141_lpc_device);
}

module_init(stx7141_add_lpc);