/* 2012-09-16: File added and changed by Sony Corporation */
/*
 * icx_lcdc.h
 *
 * Copyright 2011 Sony Corporation.
 *
 * 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
 *
 */

#ifndef __ICX_LCDC_H__
#define __ICX_LCDC_H__


/******************/
/* PIN definition */
/******************/

#ifdef CONFIG_ICX
#define GPIO_LCD_XRST       GPIO_P129
#else
#define GPIO_LCD_XRST       GPIO_P138
#define GPIO_LCD_CD			GPIO_P139
#endif


/***************/
/* Definitions */
/***************/

/* resolution */
#define LCDC_WIDTH_QVGA   240
#define LCDC_HEIGHT_QVGA  320

#define LCDC_WIDTH_WQVGA  240
#define LCDC_HEIGHT_WQVGA 400

#define LCDC_WIDTH_MONO   172
#define LCDC_HEIGHT_MONO  104

#define LCDC_WIDTH_TORINO   100
#define LCDC_HEIGHT_TORINO   96

/* bit per pixel */
#define BITS_PER_PIXEL_MONO     8
#define BITS_PER_PIXEL_RGB565   16
#define BITS_PER_PIXEL_RGB666   18
#define BITS_PER_PIXEL_RGB888   24
#ifdef CONFIG_ICD
#define BITS_PER_PIXEL_INPUT    (BITS_PER_PIXEL_MONO)
#else
#define BITS_PER_PIXEL_INPUT    (BITS_PER_PIXEL_RGB565)
#endif
#define BITS_PER_PIXEL_OUTPUT   (BITS_PER_PIXEL_RGB888)

/* frame buffer address */
#ifdef CONFIG_ICX
#define LCDC_FRAMEADR_DEFAULT	(0x41D00000)	/* same to FB_FRAME_BUFFER_ADDR  at emxx_mem.h */
#define LCDC_AREAADR_DEFAULT	(0x41DBC000)	/* same to LCD_FRAME_BUFFER_ADDR at emxx_mem.h */
#else
#define LCDC_FRAMEADR_DEFAULT	(0x41F00000)	/* same to FB_FRAME_BUFFER_ADDR  at emxx_mem.h */
#define LCDC_AREAADR_DEFAULT	(0x41FBC000)	/* same to LCD_FRAME_BUFFER_ADDR at emxx_mem.h */
#endif

/* polarity */
#define LCDC_CLKPOL_POS			(0 << 3)
#define LCDC_CLKPOL_NEG			(1 << 3)

#define LCDC_HPOL_POS			(0 << 2)
#define LCDC_HPOL_NEG			(1 << 2)

#define LCDC_VPOL_POS			(0 << 1)
#define LCDC_VPOL_NEG			(1 << 1)

#define LCDC_ENPOL_HI			(0 << 0)
#define LCDC_ENPOL_LOW			(1 << 0)

/* bussel */
#define LCDC_BUSSEL_LOCAL		0x00000000
#define LCDC_BUSSEL_DP			0x00000001
#define LCDC_BUSSEL_WB_TO_LOCAL	0x00000002
#define LCDC_BUSSEL_WB_TO_DP	0x00000003
#define LCDC_BUSSEL_BLACK		0x00000004
#define LCDC_BUSSEL_BACKCOLOR	0x00000005

/* backlight polarity */
#define LCDC_BLPOL_POS			(1)
#define LCDC_BLPOL_NEG			(0)


/*******************/
/* Type definition */
/*******************/

#define LCDC_FRAME_SIZE(w, h)	((w * h * BITS_PER_PIXEL_INPUT) / 8)

/* backlight config */
typedef struct {
	u_char dev;
	u_int  pol;
	u_int  full;
	u_int  div;
} backlight_config_t;

/* LCDC config */
typedef struct {

	/* general */
	u_int width;    /* Panel width                        */
	u_int height;   /* Panel height                       */

	/* clkdiv */
	u_int clkdiv;   /* clock divisor                      */

	/* setting to registers */
	u_int frameadr; /* FrameBuffer address(L1A)           */
	u_int areaadr;  /* area address(WB)                   */
	u_int pol;      /* polarity : CLK|HPOL|VPOL|ENPOL     */
	u_int htotal;   /* the number of PXCLK for horizontal */
	u_int harea;    /* the number of pixel for horizontal */
	u_int hedge1;   /* the point of edge1 for horizontal  */
	u_int hedge2;   /* the point of edge2 for horizontal  */
	u_int vtotal;   /* the number of HSYNC for vertical   */
	u_int varea;    /* the number of pixel for vertical   */
	u_int vedge1;   /* the point of edge1 for vertical    */
	u_int vedge2;   /* the point of edge2 for vertical    */

	/* backlight */
	backlight_config_t backlight;
} lcdc_config_t;


struct lcdc_rect {
	unsigned int x;
	unsigned int y;
	unsigned int w;
	unsigned int h;
};

/**************/
/* Prototypes */
/**************/

void lcdc_init(void);
void lcdc_hw_init(void);
void lcdc_hw_exit(void);
void lcdc_hw_set(const lcdc_config_t* config);
void lcdc_hw_switch(int on);
void lcdc_hw_bussel(u_int bussel);
void lcdc_hw_start(void);
void lcdc_hw_stop(void);

void lcdc_backlight_init(const lcdc_config_t *config);
void lcdc_backlight_exit(const lcdc_config_t *config);
int  lcdc_backlight_control(const lcdc_config_t *config, int deg);

#endif /* __ICX_LCDC_H__ */
