blob: 47823b8efff060b77f1ef4db5bf19554b08e73c7 [file] [log] [blame]
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +04001/*
2 * LCD / Backlight control code for Sharp SL-6000x (tosa)
3 *
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2007,2008 Dmitry Baryshkov
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/spi/spi.h>
17#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040019#include <linux/gpio.h>
20#include <linux/delay.h>
21#include <linux/lcd.h>
22#include <linux/fb.h>
23
24#include <asm/mach/sharpsl_param.h>
25
26#include <mach/tosa.h>
27
28#define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
29
30#define TG_REG0_VQV 0x0001
31#define TG_REG0_COLOR 0x0002
32#define TG_REG0_UD 0x0004
33#define TG_REG0_LR 0x0008
34
35#define DAC_BASE 0x4e
36
37struct tosa_lcd_data {
38 struct spi_device *spi;
39 struct lcd_device *lcd;
40 struct i2c_client *i2c;
41
42 int lcd_power;
Dmitry Baryshkov0ec561f2008-12-04 16:54:42 +000043 bool is_vga;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040044};
45
46static int tosa_tg_send(struct spi_device *spi, int adrs, uint8_t data)
47{
48 u8 buf[1];
49 struct spi_message msg;
50 struct spi_transfer xfer = {
51 .len = 1,
52 .cs_change = 1,
53 .tx_buf = buf,
54 };
55
56 buf[0] = ((adrs & 0x07) << 5) | (data & 0x1f);
57 spi_message_init(&msg);
58 spi_message_add_tail(&xfer, &msg);
59
60 return spi_sync(spi, &msg);
61}
62
63int tosa_bl_enable(struct spi_device *spi, int enable)
64{
65 /* bl_enable GP04=1 otherwise GP04=0*/
66 return tosa_tg_send(spi, TG_GPODR2, enable? 0x01 : 0x00);
67}
68EXPORT_SYMBOL(tosa_bl_enable);
69
70static void tosa_lcd_tg_init(struct tosa_lcd_data *data)
71{
72 /* TG on */
73 gpio_set_value(TOSA_GPIO_TG_ON, 0);
74
75 mdelay(60);
76
77 /* delayed 0clk TCTL signal for VGA */
78 tosa_tg_send(data->spi, TG_TPOSCTL, 0x00);
79 /* GPOS0=powercontrol, GPOS1=GPIO, GPOS2=TCTL */
80 tosa_tg_send(data->spi, TG_GPOSR, 0x02);
81}
82
83static void tosa_lcd_tg_on(struct tosa_lcd_data *data)
84{
85 struct spi_device *spi = data->spi;
Dmitry Baryshkov0ec561f2008-12-04 16:54:42 +000086 int value = TG_REG0_COLOR | TG_REG0_UD | TG_REG0_LR;
87
88 if (data->is_vga)
89 value |= TG_REG0_VQV;
90
91 tosa_tg_send(spi, TG_PNLCTL, value);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040092
93 /* TG LCD pannel power up */
94 tosa_tg_send(spi, TG_PINICTL,0x4);
95 mdelay(50);
96
97 /* TG LCD GVSS */
98 tosa_tg_send(spi, TG_PINICTL,0x0);
99
100 if (!data->i2c) {
101 /* after the pannel is powered up the first time, we can access the i2c bus */
102 /* so probe for the DAC */
103 struct i2c_adapter *adap = i2c_get_adapter(0);
104 struct i2c_board_info info = {
105 .type = "tosa-bl",
106 .addr = DAC_BASE,
107 .platform_data = data->spi,
108 };
109 data->i2c = i2c_new_device(adap, &info);
110 }
111}
112
113static void tosa_lcd_tg_off(struct tosa_lcd_data *data)
114{
115 struct spi_device *spi = data->spi;
116
117 /* TG LCD VHSA off */
118 tosa_tg_send(spi, TG_PINICTL,0x4);
119 mdelay(50);
120
121 /* TG LCD signal off */
122 tosa_tg_send(spi, TG_PINICTL,0x6);
123 mdelay(50);
124
125 /* TG Off */
126 gpio_set_value(TOSA_GPIO_TG_ON, 1);
127 mdelay(100);
128}
129
130int tosa_lcd_set_power(struct lcd_device *lcd, int power)
131{
132 struct tosa_lcd_data *data = lcd_get_data(lcd);
133
134 if (POWER_IS_ON(power) && !POWER_IS_ON(data->lcd_power))
135 tosa_lcd_tg_on(data);
136
137 if (!POWER_IS_ON(power) && POWER_IS_ON(data->lcd_power))
138 tosa_lcd_tg_off(data);
139
140 data->lcd_power = power;
141 return 0;
142}
143
144static int tosa_lcd_get_power(struct lcd_device *lcd)
145{
146 struct tosa_lcd_data *data = lcd_get_data(lcd);
147
148 return data->lcd_power;
149}
150
Dmitry Baryshkov0ec561f2008-12-04 16:54:42 +0000151static int tosa_lcd_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
152{
153 struct tosa_lcd_data *data = lcd_get_data(lcd);
154
155 if (mode->xres == 320 || mode->yres == 320)
156 data->is_vga = false;
157 else
158 data->is_vga = true;
159
160 if (POWER_IS_ON(data->lcd_power))
161 tosa_lcd_tg_on(data);
162
163 return 0;
164}
165
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400166static struct lcd_ops tosa_lcd_ops = {
167 .set_power = tosa_lcd_set_power,
168 .get_power = tosa_lcd_get_power,
Dmitry Baryshkov0ec561f2008-12-04 16:54:42 +0000169 .set_mode = tosa_lcd_set_mode,
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400170};
171
172static int __devinit tosa_lcd_probe(struct spi_device *spi)
173{
174 int ret;
175 struct tosa_lcd_data *data;
176
Jingoo Hanc8515292012-05-29 15:07:25 -0700177 data = devm_kzalloc(&spi->dev, sizeof(struct tosa_lcd_data),
178 GFP_KERNEL);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400179 if (!data)
180 return -ENOMEM;
181
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200182 data->is_vga = true; /* default to VGA mode */
Dmitry Baryshkov0ec561f2008-12-04 16:54:42 +0000183
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400184 /*
185 * bits_per_word cannot be configured in platform data
186 */
187 spi->bits_per_word = 8;
188
189 ret = spi_setup(spi);
190 if (ret < 0)
Jingoo Hanc8515292012-05-29 15:07:25 -0700191 return ret;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400192
193 data->spi = spi;
194 dev_set_drvdata(&spi->dev, data);
195
196 ret = gpio_request(TOSA_GPIO_TG_ON, "tg #pwr");
197 if (ret < 0)
198 goto err_gpio_tg;
199
200 mdelay(60);
201
202 ret = gpio_direction_output(TOSA_GPIO_TG_ON, 0);
203 if (ret < 0)
204 goto err_gpio_dir;
205
206 mdelay(60);
207 tosa_lcd_tg_init(data);
208
209 tosa_lcd_tg_on(data);
210
211 data->lcd = lcd_device_register("tosa-lcd", &spi->dev, data,
212 &tosa_lcd_ops);
213
214 if (IS_ERR(data->lcd)) {
215 ret = PTR_ERR(data->lcd);
216 data->lcd = NULL;
217 goto err_register;
218 }
219
220 return 0;
221
222err_register:
223 tosa_lcd_tg_off(data);
224err_gpio_dir:
225 gpio_free(TOSA_GPIO_TG_ON);
226err_gpio_tg:
227 dev_set_drvdata(&spi->dev, NULL);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400228 return ret;
229}
230
231static int __devexit tosa_lcd_remove(struct spi_device *spi)
232{
233 struct tosa_lcd_data *data = dev_get_drvdata(&spi->dev);
234
235 lcd_device_unregister(data->lcd);
236
237 if (data->i2c)
238 i2c_unregister_device(data->i2c);
239
240 tosa_lcd_tg_off(data);
241
242 gpio_free(TOSA_GPIO_TG_ON);
243 dev_set_drvdata(&spi->dev, NULL);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400244
245 return 0;
246}
247
248#ifdef CONFIG_PM
249static int tosa_lcd_suspend(struct spi_device *spi, pm_message_t state)
250{
251 struct tosa_lcd_data *data = dev_get_drvdata(&spi->dev);
252
253 tosa_lcd_tg_off(data);
254
255 return 0;
256}
257
258static int tosa_lcd_resume(struct spi_device *spi)
259{
260 struct tosa_lcd_data *data = dev_get_drvdata(&spi->dev);
261
262 tosa_lcd_tg_init(data);
263 if (POWER_IS_ON(data->lcd_power))
264 tosa_lcd_tg_on(data);
265 else
266 tosa_lcd_tg_off(data);
267
268 return 0;
269}
270#else
271#define tosa_lcd_suspend NULL
Masanari Iida8da00ed2012-03-28 14:42:56 -0700272#define tosa_lcd_resume NULL
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400273#endif
274
275static struct spi_driver tosa_lcd_driver = {
276 .driver = {
277 .name = "tosa-lcd",
278 .owner = THIS_MODULE,
279 },
280 .probe = tosa_lcd_probe,
281 .remove = __devexit_p(tosa_lcd_remove),
282 .suspend = tosa_lcd_suspend,
283 .resume = tosa_lcd_resume,
284};
285
Axel Lin462dd832012-03-23 15:01:59 -0700286module_spi_driver(tosa_lcd_driver);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400287
288MODULE_AUTHOR("Dmitry Baryshkov");
289MODULE_LICENSE("GPL v2");
290MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700291MODULE_ALIAS("spi:tosa-lcd");