tosa_lcd.c 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  LCD / Backlight control code for Sharp SL-6000x (tosa)
 *
 *  Copyright (c) 2005		Dirk Opfer
 *  Copyright (c) 2007,2008	Dmitry Baryshkov
 *
 *  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/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/spi/spi.h>
#include <linux/i2c.h>
18
#include <linux/slab.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/lcd.h>
#include <linux/fb.h>

#include <asm/mach/sharpsl_param.h>

#include <mach/tosa.h>

#define POWER_IS_ON(pwr)	((pwr) <= FB_BLANK_NORMAL)

#define TG_REG0_VQV	0x0001
#define TG_REG0_COLOR	0x0002
#define TG_REG0_UD	0x0004
#define TG_REG0_LR	0x0008

#define	DAC_BASE	0x4e

struct tosa_lcd_data {
	struct spi_device *spi;
	struct lcd_device *lcd;
	struct i2c_client *i2c;

	int lcd_power;
43
	bool is_vga;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
};

static int tosa_tg_send(struct spi_device *spi, int adrs, uint8_t data)
{
	u8 buf[1];
	struct spi_message msg;
	struct spi_transfer xfer = {
		.len		= 1,
		.cs_change	= 1,
		.tx_buf		= buf,
	};

	buf[0] = ((adrs & 0x07) << 5) | (data & 0x1f);
	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	return spi_sync(spi, &msg);
}

int tosa_bl_enable(struct spi_device *spi, int enable)
{
	/* bl_enable GP04=1 otherwise GP04=0*/
66
	return tosa_tg_send(spi, TG_GPODR2, enable ? 0x01 : 0x00);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}
EXPORT_SYMBOL(tosa_bl_enable);

static void tosa_lcd_tg_init(struct tosa_lcd_data *data)
{
	/* TG on */
	gpio_set_value(TOSA_GPIO_TG_ON, 0);

	mdelay(60);

	/* delayed 0clk TCTL signal for VGA */
	tosa_tg_send(data->spi, TG_TPOSCTL, 0x00);
	/* GPOS0=powercontrol, GPOS1=GPIO, GPOS2=TCTL */
	tosa_tg_send(data->spi, TG_GPOSR, 0x02);
}

static void tosa_lcd_tg_on(struct tosa_lcd_data *data)
{
	struct spi_device *spi = data->spi;
86 87 88 89 90 91
	int value = TG_REG0_COLOR | TG_REG0_UD | TG_REG0_LR;

	if (data->is_vga)
		value |= TG_REG0_VQV;

	tosa_tg_send(spi, TG_PNLCTL, value);
92 93

	/* TG LCD pannel power up */
94
	tosa_tg_send(spi, TG_PINICTL, 0x4);
95 96 97
	mdelay(50);

	/* TG LCD GVSS */
98
	tosa_tg_send(spi, TG_PINICTL, 0x0);
99 100

	if (!data->i2c) {
101 102 103 104
		/*
		 * after the pannel is powered up the first time,
		 * we can access the i2c bus so probe for the DAC
		 */
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		struct i2c_adapter *adap = i2c_get_adapter(0);
		struct i2c_board_info info = {
			.type	= "tosa-bl",
			.addr	= DAC_BASE,
			.platform_data = data->spi,
		};
		data->i2c = i2c_new_device(adap, &info);
	}
}

static void tosa_lcd_tg_off(struct tosa_lcd_data *data)
{
	struct spi_device *spi = data->spi;

	/* TG LCD VHSA off */
120
	tosa_tg_send(spi, TG_PINICTL, 0x4);
121 122 123
	mdelay(50);

	/* TG LCD signal off */
124
	tosa_tg_send(spi, TG_PINICTL, 0x6);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	mdelay(50);

	/* TG Off */
	gpio_set_value(TOSA_GPIO_TG_ON, 1);
	mdelay(100);
}

int tosa_lcd_set_power(struct lcd_device *lcd, int power)
{
	struct tosa_lcd_data *data = lcd_get_data(lcd);

	if (POWER_IS_ON(power) && !POWER_IS_ON(data->lcd_power))
		tosa_lcd_tg_on(data);

	if (!POWER_IS_ON(power) && POWER_IS_ON(data->lcd_power))
		tosa_lcd_tg_off(data);

	data->lcd_power = power;
	return 0;
}

static int tosa_lcd_get_power(struct lcd_device *lcd)
{
	struct tosa_lcd_data *data = lcd_get_data(lcd);

	return data->lcd_power;
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static int tosa_lcd_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
{
	struct tosa_lcd_data *data = lcd_get_data(lcd);

	if (mode->xres == 320 || mode->yres == 320)
		data->is_vga = false;
	else
		data->is_vga = true;

	if (POWER_IS_ON(data->lcd_power))
		tosa_lcd_tg_on(data);

	return 0;
}

168 169 170
static struct lcd_ops tosa_lcd_ops = {
	.set_power = tosa_lcd_set_power,
	.get_power = tosa_lcd_get_power,
171
	.set_mode = tosa_lcd_set_mode,
172 173
};

174
static int tosa_lcd_probe(struct spi_device *spi)
175 176 177 178
{
	int ret;
	struct tosa_lcd_data *data;

179 180
	data = devm_kzalloc(&spi->dev, sizeof(struct tosa_lcd_data),
				GFP_KERNEL);
181 182 183
	if (!data)
		return -ENOMEM;

184
	data->is_vga = true; /* default to VGA mode */
185

186 187 188 189 190 191 192
	/*
	 * bits_per_word cannot be configured in platform data
	 */
	spi->bits_per_word = 8;

	ret = spi_setup(spi);
	if (ret < 0)
193
		return ret;
194 195

	data->spi = spi;
196
	spi_set_drvdata(spi, data);
197

198 199
	ret = devm_gpio_request_one(&spi->dev, TOSA_GPIO_TG_ON,
				GPIOF_OUT_INIT_LOW, "tg #pwr");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	if (ret < 0)
		goto err_gpio_tg;

	mdelay(60);

	tosa_lcd_tg_init(data);

	tosa_lcd_tg_on(data);

	data->lcd = lcd_device_register("tosa-lcd", &spi->dev, data,
			&tosa_lcd_ops);

	if (IS_ERR(data->lcd)) {
		ret = PTR_ERR(data->lcd);
		data->lcd = NULL;
		goto err_register;
	}

	return 0;

err_register:
	tosa_lcd_tg_off(data);
err_gpio_tg:
223
	spi_set_drvdata(spi, NULL);
224 225 226
	return ret;
}

227
static int tosa_lcd_remove(struct spi_device *spi)
228
{
229
	struct tosa_lcd_data *data = spi_get_drvdata(spi);
230 231 232 233 234 235 236 237

	lcd_device_unregister(data->lcd);

	if (data->i2c)
		i2c_unregister_device(data->i2c);

	tosa_lcd_tg_off(data);

238
	spi_set_drvdata(spi, NULL);
239 240 241 242 243 244 245

	return 0;
}

#ifdef CONFIG_PM
static int tosa_lcd_suspend(struct spi_device *spi, pm_message_t state)
{
246
	struct tosa_lcd_data *data = spi_get_drvdata(spi);
247 248 249 250 251 252 253 254

	tosa_lcd_tg_off(data);

	return 0;
}

static int tosa_lcd_resume(struct spi_device *spi)
{
255
	struct tosa_lcd_data *data = spi_get_drvdata(spi);
256 257 258 259 260 261 262 263 264 265 266

	tosa_lcd_tg_init(data);
	if (POWER_IS_ON(data->lcd_power))
		tosa_lcd_tg_on(data);
	else
		tosa_lcd_tg_off(data);

	return 0;
}
#else
#define tosa_lcd_suspend	NULL
267
#define tosa_lcd_resume NULL
268 269 270 271 272 273 274 275
#endif

static struct spi_driver tosa_lcd_driver = {
	.driver = {
		.name		= "tosa-lcd",
		.owner		= THIS_MODULE,
	},
	.probe		= tosa_lcd_probe,
276
	.remove		= tosa_lcd_remove,
277 278 279 280
	.suspend	= tosa_lcd_suspend,
	.resume		= tosa_lcd_resume,
};

281
module_spi_driver(tosa_lcd_driver);
282 283 284 285

MODULE_AUTHOR("Dmitry Baryshkov");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
286
MODULE_ALIAS("spi:tosa-lcd");