WIP FPC-III support
[linux/fpc-iii.git] / drivers / video / backlight / tosa_lcd.c
blob38765544345b8d76f07aacfd6038f1855ad082e2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LCD / Backlight control code for Sharp SL-6000x (tosa)
5 * Copyright (c) 2005 Dirk Opfer
6 * Copyright (c) 2007,2008 Dmitry Baryshkov
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/spi/spi.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/delay.h>
17 #include <linux/lcd.h>
18 #include <linux/fb.h>
20 #include <asm/mach/sharpsl_param.h>
22 #include "tosa_bl.h"
24 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
26 #define TG_REG0_VQV 0x0001
27 #define TG_REG0_COLOR 0x0002
28 #define TG_REG0_UD 0x0004
29 #define TG_REG0_LR 0x0008
32 * Timing Generator
34 #define TG_PNLCTL 0x00
35 #define TG_TPOSCTL 0x01
36 #define TG_DUTYCTL 0x02
37 #define TG_GPOSR 0x03
38 #define TG_GPODR1 0x04
39 #define TG_GPODR2 0x05
40 #define TG_PINICTL 0x06
41 #define TG_HPOSCTL 0x07
44 #define DAC_BASE 0x4e
46 struct tosa_lcd_data {
47 struct spi_device *spi;
48 struct lcd_device *lcd;
49 struct i2c_client *i2c;
50 struct gpio_desc *gpiod_tg;
52 int lcd_power;
53 bool is_vga;
56 static int tosa_tg_send(struct spi_device *spi, int adrs, uint8_t data)
58 u8 buf[1];
59 struct spi_message msg;
60 struct spi_transfer xfer = {
61 .len = 1,
62 .cs_change = 0,
63 .tx_buf = buf,
66 buf[0] = ((adrs & 0x07) << 5) | (data & 0x1f);
67 spi_message_init(&msg);
68 spi_message_add_tail(&xfer, &msg);
70 return spi_sync(spi, &msg);
73 int tosa_bl_enable(struct spi_device *spi, int enable)
75 /* bl_enable GP04=1 otherwise GP04=0*/
76 return tosa_tg_send(spi, TG_GPODR2, enable ? 0x01 : 0x00);
78 EXPORT_SYMBOL(tosa_bl_enable);
80 static void tosa_lcd_tg_init(struct tosa_lcd_data *data)
82 /* TG on */
83 gpiod_set_value(data->gpiod_tg, 0);
85 mdelay(60);
87 /* delayed 0clk TCTL signal for VGA */
88 tosa_tg_send(data->spi, TG_TPOSCTL, 0x00);
89 /* GPOS0=powercontrol, GPOS1=GPIO, GPOS2=TCTL */
90 tosa_tg_send(data->spi, TG_GPOSR, 0x02);
93 static void tosa_lcd_tg_on(struct tosa_lcd_data *data)
95 struct spi_device *spi = data->spi;
96 int value = TG_REG0_COLOR | TG_REG0_UD | TG_REG0_LR;
98 if (data->is_vga)
99 value |= TG_REG0_VQV;
101 tosa_tg_send(spi, TG_PNLCTL, value);
103 /* TG LCD pannel power up */
104 tosa_tg_send(spi, TG_PINICTL, 0x4);
105 mdelay(50);
107 /* TG LCD GVSS */
108 tosa_tg_send(spi, TG_PINICTL, 0x0);
110 if (IS_ERR_OR_NULL(data->i2c)) {
112 * after the pannel is powered up the first time,
113 * we can access the i2c bus so probe for the DAC
115 struct i2c_adapter *adap = i2c_get_adapter(0);
116 struct i2c_board_info info = {
117 .dev_name = "tosa-bl",
118 .type = "tosa-bl",
119 .addr = DAC_BASE,
120 .platform_data = data->spi,
122 data->i2c = i2c_new_client_device(adap, &info);
126 static void tosa_lcd_tg_off(struct tosa_lcd_data *data)
128 struct spi_device *spi = data->spi;
130 /* TG LCD VHSA off */
131 tosa_tg_send(spi, TG_PINICTL, 0x4);
132 mdelay(50);
134 /* TG LCD signal off */
135 tosa_tg_send(spi, TG_PINICTL, 0x6);
136 mdelay(50);
138 /* TG Off */
139 gpiod_set_value(data->gpiod_tg, 1);
140 mdelay(100);
143 int tosa_lcd_set_power(struct lcd_device *lcd, int power)
145 struct tosa_lcd_data *data = lcd_get_data(lcd);
147 if (POWER_IS_ON(power) && !POWER_IS_ON(data->lcd_power))
148 tosa_lcd_tg_on(data);
150 if (!POWER_IS_ON(power) && POWER_IS_ON(data->lcd_power))
151 tosa_lcd_tg_off(data);
153 data->lcd_power = power;
154 return 0;
157 static int tosa_lcd_get_power(struct lcd_device *lcd)
159 struct tosa_lcd_data *data = lcd_get_data(lcd);
161 return data->lcd_power;
164 static int tosa_lcd_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
166 struct tosa_lcd_data *data = lcd_get_data(lcd);
168 if (mode->xres == 320 || mode->yres == 320)
169 data->is_vga = false;
170 else
171 data->is_vga = true;
173 if (POWER_IS_ON(data->lcd_power))
174 tosa_lcd_tg_on(data);
176 return 0;
179 static struct lcd_ops tosa_lcd_ops = {
180 .set_power = tosa_lcd_set_power,
181 .get_power = tosa_lcd_get_power,
182 .set_mode = tosa_lcd_set_mode,
185 static int tosa_lcd_probe(struct spi_device *spi)
187 int ret;
188 struct tosa_lcd_data *data;
190 data = devm_kzalloc(&spi->dev, sizeof(struct tosa_lcd_data),
191 GFP_KERNEL);
192 if (!data)
193 return -ENOMEM;
195 data->is_vga = true; /* default to VGA mode */
198 * bits_per_word cannot be configured in platform data
200 spi->bits_per_word = 8;
202 ret = spi_setup(spi);
203 if (ret < 0)
204 return ret;
206 data->spi = spi;
207 spi_set_drvdata(spi, data);
209 data->gpiod_tg = devm_gpiod_get(&spi->dev, "tg #pwr", GPIOD_OUT_LOW);
210 if (IS_ERR(data->gpiod_tg))
211 return PTR_ERR(data->gpiod_tg);
213 mdelay(60);
215 tosa_lcd_tg_init(data);
217 tosa_lcd_tg_on(data);
219 data->lcd = devm_lcd_device_register(&spi->dev, "tosa-lcd", &spi->dev,
220 data, &tosa_lcd_ops);
222 if (IS_ERR(data->lcd)) {
223 ret = PTR_ERR(data->lcd);
224 data->lcd = NULL;
225 goto err_register;
228 return 0;
230 err_register:
231 tosa_lcd_tg_off(data);
232 return ret;
235 static int tosa_lcd_remove(struct spi_device *spi)
237 struct tosa_lcd_data *data = spi_get_drvdata(spi);
239 i2c_unregister_device(data->i2c);
241 tosa_lcd_tg_off(data);
243 return 0;
246 #ifdef CONFIG_PM_SLEEP
247 static int tosa_lcd_suspend(struct device *dev)
249 struct tosa_lcd_data *data = dev_get_drvdata(dev);
251 tosa_lcd_tg_off(data);
253 return 0;
256 static int tosa_lcd_resume(struct device *dev)
258 struct tosa_lcd_data *data = dev_get_drvdata(dev);
260 tosa_lcd_tg_init(data);
261 if (POWER_IS_ON(data->lcd_power))
262 tosa_lcd_tg_on(data);
263 else
264 tosa_lcd_tg_off(data);
266 return 0;
268 #endif
270 static SIMPLE_DEV_PM_OPS(tosa_lcd_pm_ops, tosa_lcd_suspend, tosa_lcd_resume);
272 static struct spi_driver tosa_lcd_driver = {
273 .driver = {
274 .name = "tosa-lcd",
275 .pm = &tosa_lcd_pm_ops,
277 .probe = tosa_lcd_probe,
278 .remove = tosa_lcd_remove,
281 module_spi_driver(tosa_lcd_driver);
283 MODULE_AUTHOR("Dmitry Baryshkov");
284 MODULE_LICENSE("GPL v2");
285 MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
286 MODULE_ALIAS("spi:tosa-lcd");