Linux 4.18.10
[linux/fpc-iii.git] / drivers / video / backlight / lms501kf03.c
blob8aa3e7662496b3d1e6098dc6c1b9dad3900f9e66
1 /*
2 * lms501kf03 TFT LCD panel driver.
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Author: Jingoo Han <jg1.han@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/backlight.h>
14 #include <linux/delay.h>
15 #include <linux/fb.h>
16 #include <linux/gpio.h>
17 #include <linux/lcd.h>
18 #include <linux/module.h>
19 #include <linux/spi/spi.h>
20 #include <linux/wait.h>
22 #define COMMAND_ONLY 0x00
23 #define DATA_ONLY 0x01
25 struct lms501kf03 {
26 struct device *dev;
27 struct spi_device *spi;
28 unsigned int power;
29 struct lcd_device *ld;
30 struct lcd_platform_data *lcd_pd;
33 static const unsigned char seq_password[] = {
34 0xb9, 0xff, 0x83, 0x69,
37 static const unsigned char seq_power[] = {
38 0xb1, 0x01, 0x00, 0x34, 0x06, 0x00, 0x14, 0x14, 0x20, 0x28,
39 0x12, 0x12, 0x17, 0x0a, 0x01, 0xe6, 0xe6, 0xe6, 0xe6, 0xe6,
42 static const unsigned char seq_display[] = {
43 0xb2, 0x00, 0x2b, 0x03, 0x03, 0x70, 0x00, 0xff, 0x00, 0x00,
44 0x00, 0x00, 0x03, 0x03, 0x00, 0x01,
47 static const unsigned char seq_rgb_if[] = {
48 0xb3, 0x09,
51 static const unsigned char seq_display_inv[] = {
52 0xb4, 0x01, 0x08, 0x77, 0x0e, 0x06,
55 static const unsigned char seq_vcom[] = {
56 0xb6, 0x4c, 0x2e,
59 static const unsigned char seq_gate[] = {
60 0xd5, 0x00, 0x05, 0x03, 0x29, 0x01, 0x07, 0x17, 0x68, 0x13,
61 0x37, 0x20, 0x31, 0x8a, 0x46, 0x9b, 0x57, 0x13, 0x02, 0x75,
62 0xb9, 0x64, 0xa8, 0x07, 0x0f, 0x04, 0x07,
65 static const unsigned char seq_panel[] = {
66 0xcc, 0x02,
69 static const unsigned char seq_col_mod[] = {
70 0x3a, 0x77,
73 static const unsigned char seq_w_gamma[] = {
74 0xe0, 0x00, 0x04, 0x09, 0x0f, 0x1f, 0x3f, 0x1f, 0x2f, 0x0a,
75 0x0f, 0x10, 0x16, 0x18, 0x16, 0x17, 0x0d, 0x15, 0x00, 0x04,
76 0x09, 0x0f, 0x38, 0x3f, 0x20, 0x39, 0x0a, 0x0f, 0x10, 0x16,
77 0x18, 0x16, 0x17, 0x0d, 0x15,
80 static const unsigned char seq_rgb_gamma[] = {
81 0xc1, 0x01, 0x03, 0x07, 0x0f, 0x1a, 0x22, 0x2c, 0x33, 0x3c,
82 0x46, 0x4f, 0x58, 0x60, 0x69, 0x71, 0x79, 0x82, 0x89, 0x92,
83 0x9a, 0xa1, 0xa9, 0xb1, 0xb9, 0xc1, 0xc9, 0xcf, 0xd6, 0xde,
84 0xe5, 0xec, 0xf3, 0xf9, 0xff, 0xdd, 0x39, 0x07, 0x1c, 0xcb,
85 0xab, 0x5f, 0x49, 0x80, 0x03, 0x07, 0x0f, 0x19, 0x20, 0x2a,
86 0x31, 0x39, 0x42, 0x4b, 0x53, 0x5b, 0x63, 0x6b, 0x73, 0x7b,
87 0x83, 0x8a, 0x92, 0x9b, 0xa2, 0xaa, 0xb2, 0xba, 0xc2, 0xca,
88 0xd0, 0xd8, 0xe1, 0xe8, 0xf0, 0xf8, 0xff, 0xf7, 0xd8, 0xbe,
89 0xa7, 0x39, 0x40, 0x85, 0x8c, 0xc0, 0x04, 0x07, 0x0c, 0x17,
90 0x1c, 0x23, 0x2b, 0x34, 0x3b, 0x43, 0x4c, 0x54, 0x5b, 0x63,
91 0x6a, 0x73, 0x7a, 0x82, 0x8a, 0x91, 0x98, 0xa1, 0xa8, 0xb0,
92 0xb7, 0xc1, 0xc9, 0xcf, 0xd9, 0xe3, 0xea, 0xf4, 0xff, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 static const unsigned char seq_up_dn[] = {
97 0x36, 0x10,
100 static const unsigned char seq_sleep_in[] = {
101 0x10,
104 static const unsigned char seq_sleep_out[] = {
105 0x11,
108 static const unsigned char seq_display_on[] = {
109 0x29,
112 static const unsigned char seq_display_off[] = {
113 0x10,
116 static int lms501kf03_spi_write_byte(struct lms501kf03 *lcd, int addr, int data)
118 u16 buf[1];
119 struct spi_message msg;
121 struct spi_transfer xfer = {
122 .len = 2,
123 .tx_buf = buf,
126 buf[0] = (addr << 8) | data;
128 spi_message_init(&msg);
129 spi_message_add_tail(&xfer, &msg);
131 return spi_sync(lcd->spi, &msg);
134 static int lms501kf03_spi_write(struct lms501kf03 *lcd, unsigned char address,
135 unsigned char command)
137 return lms501kf03_spi_write_byte(lcd, address, command);
140 static int lms501kf03_panel_send_sequence(struct lms501kf03 *lcd,
141 const unsigned char *wbuf,
142 unsigned int len)
144 int ret = 0, i = 0;
146 while (i < len) {
147 if (i == 0)
148 ret = lms501kf03_spi_write(lcd, COMMAND_ONLY, wbuf[i]);
149 else
150 ret = lms501kf03_spi_write(lcd, DATA_ONLY, wbuf[i]);
151 if (ret)
152 break;
153 i += 1;
156 return ret;
159 static int lms501kf03_ldi_init(struct lms501kf03 *lcd)
161 int ret, i;
162 static const unsigned char *init_seq[] = {
163 seq_password,
164 seq_power,
165 seq_display,
166 seq_rgb_if,
167 seq_display_inv,
168 seq_vcom,
169 seq_gate,
170 seq_panel,
171 seq_col_mod,
172 seq_w_gamma,
173 seq_rgb_gamma,
174 seq_sleep_out,
177 static const unsigned int size_seq[] = {
178 ARRAY_SIZE(seq_password),
179 ARRAY_SIZE(seq_power),
180 ARRAY_SIZE(seq_display),
181 ARRAY_SIZE(seq_rgb_if),
182 ARRAY_SIZE(seq_display_inv),
183 ARRAY_SIZE(seq_vcom),
184 ARRAY_SIZE(seq_gate),
185 ARRAY_SIZE(seq_panel),
186 ARRAY_SIZE(seq_col_mod),
187 ARRAY_SIZE(seq_w_gamma),
188 ARRAY_SIZE(seq_rgb_gamma),
189 ARRAY_SIZE(seq_sleep_out),
192 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
193 ret = lms501kf03_panel_send_sequence(lcd, init_seq[i],
194 size_seq[i]);
195 if (ret)
196 break;
199 * According to the datasheet, 120ms delay time is required.
200 * After sleep out sequence, command is blocked for 120ms.
201 * Thus, LDI should wait for 120ms.
203 msleep(120);
205 return ret;
208 static int lms501kf03_ldi_enable(struct lms501kf03 *lcd)
210 return lms501kf03_panel_send_sequence(lcd, seq_display_on,
211 ARRAY_SIZE(seq_display_on));
214 static int lms501kf03_ldi_disable(struct lms501kf03 *lcd)
216 return lms501kf03_panel_send_sequence(lcd, seq_display_off,
217 ARRAY_SIZE(seq_display_off));
220 static int lms501kf03_power_is_on(int power)
222 return (power) <= FB_BLANK_NORMAL;
225 static int lms501kf03_power_on(struct lms501kf03 *lcd)
227 int ret = 0;
228 struct lcd_platform_data *pd;
230 pd = lcd->lcd_pd;
232 if (!pd->power_on) {
233 dev_err(lcd->dev, "power_on is NULL.\n");
234 return -EINVAL;
237 pd->power_on(lcd->ld, 1);
238 msleep(pd->power_on_delay);
240 if (!pd->reset) {
241 dev_err(lcd->dev, "reset is NULL.\n");
242 return -EINVAL;
245 pd->reset(lcd->ld);
246 msleep(pd->reset_delay);
248 ret = lms501kf03_ldi_init(lcd);
249 if (ret) {
250 dev_err(lcd->dev, "failed to initialize ldi.\n");
251 return ret;
254 ret = lms501kf03_ldi_enable(lcd);
255 if (ret) {
256 dev_err(lcd->dev, "failed to enable ldi.\n");
257 return ret;
260 return 0;
263 static int lms501kf03_power_off(struct lms501kf03 *lcd)
265 int ret = 0;
266 struct lcd_platform_data *pd;
268 pd = lcd->lcd_pd;
270 ret = lms501kf03_ldi_disable(lcd);
271 if (ret) {
272 dev_err(lcd->dev, "lcd setting failed.\n");
273 return -EIO;
276 msleep(pd->power_off_delay);
278 pd->power_on(lcd->ld, 0);
280 return 0;
283 static int lms501kf03_power(struct lms501kf03 *lcd, int power)
285 int ret = 0;
287 if (lms501kf03_power_is_on(power) &&
288 !lms501kf03_power_is_on(lcd->power))
289 ret = lms501kf03_power_on(lcd);
290 else if (!lms501kf03_power_is_on(power) &&
291 lms501kf03_power_is_on(lcd->power))
292 ret = lms501kf03_power_off(lcd);
294 if (!ret)
295 lcd->power = power;
297 return ret;
300 static int lms501kf03_get_power(struct lcd_device *ld)
302 struct lms501kf03 *lcd = lcd_get_data(ld);
304 return lcd->power;
307 static int lms501kf03_set_power(struct lcd_device *ld, int power)
309 struct lms501kf03 *lcd = lcd_get_data(ld);
311 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
312 power != FB_BLANK_NORMAL) {
313 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
314 return -EINVAL;
317 return lms501kf03_power(lcd, power);
320 static struct lcd_ops lms501kf03_lcd_ops = {
321 .get_power = lms501kf03_get_power,
322 .set_power = lms501kf03_set_power,
325 static int lms501kf03_probe(struct spi_device *spi)
327 struct lms501kf03 *lcd = NULL;
328 struct lcd_device *ld = NULL;
329 int ret = 0;
331 lcd = devm_kzalloc(&spi->dev, sizeof(struct lms501kf03), GFP_KERNEL);
332 if (!lcd)
333 return -ENOMEM;
335 /* lms501kf03 lcd panel uses 3-wire 9-bit SPI Mode. */
336 spi->bits_per_word = 9;
338 ret = spi_setup(spi);
339 if (ret < 0) {
340 dev_err(&spi->dev, "spi setup failed.\n");
341 return ret;
344 lcd->spi = spi;
345 lcd->dev = &spi->dev;
347 lcd->lcd_pd = dev_get_platdata(&spi->dev);
348 if (!lcd->lcd_pd) {
349 dev_err(&spi->dev, "platform data is NULL\n");
350 return -EINVAL;
353 ld = devm_lcd_device_register(&spi->dev, "lms501kf03", &spi->dev, lcd,
354 &lms501kf03_lcd_ops);
355 if (IS_ERR(ld))
356 return PTR_ERR(ld);
358 lcd->ld = ld;
360 if (!lcd->lcd_pd->lcd_enabled) {
362 * if lcd panel was off from bootloader then
363 * current lcd status is powerdown and then
364 * it enables lcd panel.
366 lcd->power = FB_BLANK_POWERDOWN;
368 lms501kf03_power(lcd, FB_BLANK_UNBLANK);
369 } else {
370 lcd->power = FB_BLANK_UNBLANK;
373 spi_set_drvdata(spi, lcd);
375 dev_info(&spi->dev, "lms501kf03 panel driver has been probed.\n");
377 return 0;
380 static int lms501kf03_remove(struct spi_device *spi)
382 struct lms501kf03 *lcd = spi_get_drvdata(spi);
384 lms501kf03_power(lcd, FB_BLANK_POWERDOWN);
385 return 0;
388 #ifdef CONFIG_PM_SLEEP
389 static int lms501kf03_suspend(struct device *dev)
391 struct lms501kf03 *lcd = dev_get_drvdata(dev);
393 dev_dbg(dev, "lcd->power = %d\n", lcd->power);
396 * when lcd panel is suspend, lcd panel becomes off
397 * regardless of status.
399 return lms501kf03_power(lcd, FB_BLANK_POWERDOWN);
402 static int lms501kf03_resume(struct device *dev)
404 struct lms501kf03 *lcd = dev_get_drvdata(dev);
406 lcd->power = FB_BLANK_POWERDOWN;
408 return lms501kf03_power(lcd, FB_BLANK_UNBLANK);
410 #endif
412 static SIMPLE_DEV_PM_OPS(lms501kf03_pm_ops, lms501kf03_suspend,
413 lms501kf03_resume);
415 static void lms501kf03_shutdown(struct spi_device *spi)
417 struct lms501kf03 *lcd = spi_get_drvdata(spi);
419 lms501kf03_power(lcd, FB_BLANK_POWERDOWN);
422 static struct spi_driver lms501kf03_driver = {
423 .driver = {
424 .name = "lms501kf03",
425 .pm = &lms501kf03_pm_ops,
427 .probe = lms501kf03_probe,
428 .remove = lms501kf03_remove,
429 .shutdown = lms501kf03_shutdown,
432 module_spi_driver(lms501kf03_driver);
434 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
435 MODULE_DESCRIPTION("lms501kf03 LCD Driver");
436 MODULE_LICENSE("GPL");