PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / video / backlight / ltv350qv.c
blob2eb206bf73e65fb54026d318f281107ab601ec3f
1 /*
2 * Power control for Samsung LTV350QV Quarter VGA LCD Panel
4 * Copyright (C) 2006, 2007 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/fb.h>
13 #include <linux/init.h>
14 #include <linux/lcd.h>
15 #include <linux/module.h>
16 #include <linux/spi/spi.h>
18 #include "ltv350qv.h"
20 #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
22 struct ltv350qv {
23 struct spi_device *spi;
24 u8 *buffer;
25 int power;
26 struct lcd_device *ld;
30 * The power-on and power-off sequences are taken from the
31 * LTV350QV-F04 data sheet from Samsung. The register definitions are
32 * taken from the S6F2002 command list also from Samsung. Both
33 * documents are distributed with the AVR32 Linux BSP CD from Atmel.
35 * There's still some voodoo going on here, but it's a lot better than
36 * in the first incarnation of the driver where all we had was the raw
37 * numbers from the initialization sequence.
39 static int ltv350qv_write_reg(struct ltv350qv *lcd, u8 reg, u16 val)
41 struct spi_message msg;
42 struct spi_transfer index_xfer = {
43 .len = 3,
44 .cs_change = 1,
46 struct spi_transfer value_xfer = {
47 .len = 3,
50 spi_message_init(&msg);
52 /* register index */
53 lcd->buffer[0] = LTV_OPC_INDEX;
54 lcd->buffer[1] = 0x00;
55 lcd->buffer[2] = reg & 0x7f;
56 index_xfer.tx_buf = lcd->buffer;
57 spi_message_add_tail(&index_xfer, &msg);
59 /* register value */
60 lcd->buffer[4] = LTV_OPC_DATA;
61 lcd->buffer[5] = val >> 8;
62 lcd->buffer[6] = val;
63 value_xfer.tx_buf = lcd->buffer + 4;
64 spi_message_add_tail(&value_xfer, &msg);
66 return spi_sync(lcd->spi, &msg);
69 /* The comments are taken straight from the data sheet */
70 static int ltv350qv_power_on(struct ltv350qv *lcd)
72 int ret;
74 /* Power On Reset Display off State */
75 if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, 0x0000))
76 goto err;
77 msleep(15);
79 /* Power Setting Function 1 */
80 if (ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE))
81 goto err;
82 if (ltv350qv_write_reg(lcd, LTV_PWRCTL2, LTV_VCOML_ENABLE))
83 goto err_power1;
85 /* Power Setting Function 2 */
86 if (ltv350qv_write_reg(lcd, LTV_PWRCTL1,
87 LTV_VCOM_DISABLE | LTV_DRIVE_CURRENT(5)
88 | LTV_SUPPLY_CURRENT(5)))
89 goto err_power2;
91 msleep(55);
93 /* Instruction Setting */
94 ret = ltv350qv_write_reg(lcd, LTV_IFCTL,
95 LTV_NMD | LTV_REV | LTV_NL(0x1d));
96 ret |= ltv350qv_write_reg(lcd, LTV_DATACTL,
97 LTV_DS_SAME | LTV_CHS_480
98 | LTV_DF_RGB | LTV_RGB_BGR);
99 ret |= ltv350qv_write_reg(lcd, LTV_ENTRY_MODE,
100 LTV_VSPL_ACTIVE_LOW
101 | LTV_HSPL_ACTIVE_LOW
102 | LTV_DPL_SAMPLE_RISING
103 | LTV_EPL_ACTIVE_LOW
104 | LTV_SS_RIGHT_TO_LEFT);
105 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL1, LTV_CLW(3));
106 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
107 LTV_NW_INV_1LINE | LTV_FWI(3));
108 ret |= ltv350qv_write_reg(lcd, LTV_VBP, 0x000a);
109 ret |= ltv350qv_write_reg(lcd, LTV_HBP, 0x0021);
110 ret |= ltv350qv_write_reg(lcd, LTV_SOTCTL, LTV_SDT(3) | LTV_EQ(0));
111 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(0), 0x0103);
112 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(1), 0x0301);
113 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(2), 0x1f0f);
114 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(3), 0x1f0f);
115 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(4), 0x0707);
116 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(5), 0x0307);
117 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(6), 0x0707);
118 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(7), 0x0000);
119 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(8), 0x0004);
120 ret |= ltv350qv_write_reg(lcd, LTV_GAMMA(9), 0x0000);
121 if (ret)
122 goto err_settings;
124 /* Wait more than 2 frames */
125 msleep(20);
127 /* Display On Sequence */
128 ret = ltv350qv_write_reg(lcd, LTV_PWRCTL1,
129 LTV_VCOM_DISABLE | LTV_VCOMOUT_ENABLE
130 | LTV_POWER_ON | LTV_DRIVE_CURRENT(5)
131 | LTV_SUPPLY_CURRENT(5));
132 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
133 LTV_NW_INV_1LINE | LTV_DSC | LTV_FWI(3));
134 if (ret)
135 goto err_disp_on;
137 /* Display should now be ON. Phew. */
138 return 0;
140 err_disp_on:
142 * Try to recover. Error handling probably isn't very useful
143 * at this point, just make a best effort to switch the panel
144 * off.
146 ltv350qv_write_reg(lcd, LTV_PWRCTL1,
147 LTV_VCOM_DISABLE | LTV_DRIVE_CURRENT(5)
148 | LTV_SUPPLY_CURRENT(5));
149 ltv350qv_write_reg(lcd, LTV_GATECTL2,
150 LTV_NW_INV_1LINE | LTV_FWI(3));
151 err_settings:
152 err_power2:
153 err_power1:
154 ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
155 msleep(1);
156 err:
157 ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
158 return -EIO;
161 static int ltv350qv_power_off(struct ltv350qv *lcd)
163 int ret;
165 /* Display Off Sequence */
166 ret = ltv350qv_write_reg(lcd, LTV_PWRCTL1,
167 LTV_VCOM_DISABLE
168 | LTV_DRIVE_CURRENT(5)
169 | LTV_SUPPLY_CURRENT(5));
170 ret |= ltv350qv_write_reg(lcd, LTV_GATECTL2,
171 LTV_NW_INV_1LINE | LTV_FWI(3));
173 /* Power down setting 1 */
174 ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL2, 0x0000);
176 /* Wait at least 1 ms */
177 msleep(1);
179 /* Power down setting 2 */
180 ret |= ltv350qv_write_reg(lcd, LTV_PWRCTL1, LTV_VCOM_DISABLE);
183 * No point in trying to recover here. If we can't switch the
184 * panel off, what are we supposed to do other than inform the
185 * user about the failure?
187 if (ret)
188 return -EIO;
190 /* Display power should now be OFF */
191 return 0;
194 static int ltv350qv_power(struct ltv350qv *lcd, int power)
196 int ret = 0;
198 if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->power))
199 ret = ltv350qv_power_on(lcd);
200 else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->power))
201 ret = ltv350qv_power_off(lcd);
203 if (!ret)
204 lcd->power = power;
206 return ret;
209 static int ltv350qv_set_power(struct lcd_device *ld, int power)
211 struct ltv350qv *lcd = lcd_get_data(ld);
213 return ltv350qv_power(lcd, power);
216 static int ltv350qv_get_power(struct lcd_device *ld)
218 struct ltv350qv *lcd = lcd_get_data(ld);
220 return lcd->power;
223 static struct lcd_ops ltv_ops = {
224 .get_power = ltv350qv_get_power,
225 .set_power = ltv350qv_set_power,
228 static int __devinit ltv350qv_probe(struct spi_device *spi)
230 struct ltv350qv *lcd;
231 struct lcd_device *ld;
232 int ret;
234 lcd = kzalloc(sizeof(struct ltv350qv), GFP_KERNEL);
235 if (!lcd)
236 return -ENOMEM;
238 lcd->spi = spi;
239 lcd->power = FB_BLANK_POWERDOWN;
240 lcd->buffer = kzalloc(8, GFP_KERNEL);
242 ld = lcd_device_register("ltv350qv", &spi->dev, lcd, &ltv_ops);
243 if (IS_ERR(ld)) {
244 ret = PTR_ERR(ld);
245 goto out_free_lcd;
247 lcd->ld = ld;
249 ret = ltv350qv_power(lcd, FB_BLANK_UNBLANK);
250 if (ret)
251 goto out_unregister;
253 dev_set_drvdata(&spi->dev, lcd);
255 return 0;
257 out_unregister:
258 lcd_device_unregister(ld);
259 out_free_lcd:
260 kfree(lcd);
261 return ret;
264 static int __devexit ltv350qv_remove(struct spi_device *spi)
266 struct ltv350qv *lcd = dev_get_drvdata(&spi->dev);
268 ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
269 lcd_device_unregister(lcd->ld);
270 kfree(lcd);
272 return 0;
275 #ifdef CONFIG_PM
276 static int ltv350qv_suspend(struct spi_device *spi, pm_message_t state)
278 struct ltv350qv *lcd = dev_get_drvdata(&spi->dev);
280 return ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
283 static int ltv350qv_resume(struct spi_device *spi)
285 struct ltv350qv *lcd = dev_get_drvdata(&spi->dev);
287 return ltv350qv_power(lcd, FB_BLANK_UNBLANK);
289 #else
290 #define ltv350qv_suspend NULL
291 #define ltv350qv_resume NULL
292 #endif
294 /* Power down all displays on reboot, poweroff or halt */
295 static void ltv350qv_shutdown(struct spi_device *spi)
297 struct ltv350qv *lcd = dev_get_drvdata(&spi->dev);
299 ltv350qv_power(lcd, FB_BLANK_POWERDOWN);
302 static struct spi_driver ltv350qv_driver = {
303 .driver = {
304 .name = "ltv350qv",
305 .bus = &spi_bus_type,
306 .owner = THIS_MODULE,
309 .probe = ltv350qv_probe,
310 .remove = __devexit_p(ltv350qv_remove),
311 .shutdown = ltv350qv_shutdown,
312 .suspend = ltv350qv_suspend,
313 .resume = ltv350qv_resume,
316 static int __init ltv350qv_init(void)
318 return spi_register_driver(&ltv350qv_driver);
321 static void __exit ltv350qv_exit(void)
323 spi_unregister_driver(&ltv350qv_driver);
325 module_init(ltv350qv_init);
326 module_exit(ltv350qv_exit);
328 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
329 MODULE_DESCRIPTION("Samsung LTV350QV LCD Driver");
330 MODULE_LICENSE("GPL");