blob: bdc1a7e583377a0e0cf76beb343a1c105229c076 [file] [log] [blame]
Donghwa Lee1baf0eb2011-03-22 16:30:18 -07001/*
2 * ld9040 AMOLED LCD panel driver.
3 *
4 * Copyright (c) 2011 Samsung Electronics
5 * Author: Donghwa Lee <dh09.lee@samsung.com>
6 * Derived from drivers/video/backlight/s6e63m0.c
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <linux/wait.h>
24#include <linux/fb.h>
25#include <linux/delay.h>
26#include <linux/gpio.h>
27#include <linux/spi/spi.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
30#include <linux/kernel.h>
31#include <linux/lcd.h>
32#include <linux/backlight.h>
Paul Gortmaker355b2002011-07-03 16:17:28 -040033#include <linux/module.h>
Donghwa Leeb148a272012-01-10 15:09:15 -080034#include <linux/regulator/consumer.h>
Donghwa Lee1baf0eb2011-03-22 16:30:18 -070035
36#include "ld9040_gamma.h"
37
38#define SLEEPMSEC 0x1000
39#define ENDDEF 0x2000
40#define DEFMASK 0xFF00
41#define COMMAND_ONLY 0xFE
42#define DATA_ONLY 0xFF
43
44#define MIN_BRIGHTNESS 0
45#define MAX_BRIGHTNESS 24
Donghwa Lee1baf0eb2011-03-22 16:30:18 -070046
47struct ld9040 {
48 struct device *dev;
49 struct spi_device *spi;
50 unsigned int power;
51 unsigned int current_brightness;
52
53 struct lcd_device *ld;
54 struct backlight_device *bd;
55 struct lcd_platform_data *lcd_pd;
Donghwa Leeb148a272012-01-10 15:09:15 -080056
57 struct mutex lock;
58 bool enabled;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -070059};
60
Donghwa Leeb148a272012-01-10 15:09:15 -080061static struct regulator_bulk_data supplies[] = {
62 { .supply = "vdd3", },
63 { .supply = "vci", },
64};
65
66static void ld9040_regulator_enable(struct ld9040 *lcd)
67{
68 int ret = 0;
69 struct lcd_platform_data *pd = NULL;
70
71 pd = lcd->lcd_pd;
72 mutex_lock(&lcd->lock);
73 if (!lcd->enabled) {
74 ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
75 if (ret)
76 goto out;
77
78 lcd->enabled = true;
79 }
Jingoo Hand2fff292013-02-21 16:43:15 -080080 msleep(pd->power_on_delay);
Donghwa Leeb148a272012-01-10 15:09:15 -080081out:
82 mutex_unlock(&lcd->lock);
83}
84
85static void ld9040_regulator_disable(struct ld9040 *lcd)
86{
87 int ret = 0;
88
89 mutex_lock(&lcd->lock);
90 if (lcd->enabled) {
91 ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
92 if (ret)
93 goto out;
94
95 lcd->enabled = false;
96 }
97out:
98 mutex_unlock(&lcd->lock);
99}
100
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700101static const unsigned short seq_swreset[] = {
102 0x01, COMMAND_ONLY,
103 ENDDEF, 0x00
104};
105
106static const unsigned short seq_user_setting[] = {
107 0xF0, 0x5A,
108
109 DATA_ONLY, 0x5A,
110 ENDDEF, 0x00
111};
112
113static const unsigned short seq_elvss_on[] = {
114 0xB1, 0x0D,
115
116 DATA_ONLY, 0x00,
117 DATA_ONLY, 0x16,
118 ENDDEF, 0x00
119};
120
121static const unsigned short seq_gtcon[] = {
122 0xF7, 0x09,
123
124 DATA_ONLY, 0x00,
125 DATA_ONLY, 0x00,
126 ENDDEF, 0x00
127};
128
129static const unsigned short seq_panel_condition[] = {
130 0xF8, 0x05,
131
132 DATA_ONLY, 0x65,
133 DATA_ONLY, 0x96,
134 DATA_ONLY, 0x71,
135 DATA_ONLY, 0x7D,
136 DATA_ONLY, 0x19,
137 DATA_ONLY, 0x3B,
138 DATA_ONLY, 0x0D,
139 DATA_ONLY, 0x19,
140 DATA_ONLY, 0x7E,
141 DATA_ONLY, 0x0D,
142 DATA_ONLY, 0xE2,
143 DATA_ONLY, 0x00,
144 DATA_ONLY, 0x00,
145 DATA_ONLY, 0x7E,
146 DATA_ONLY, 0x7D,
147 DATA_ONLY, 0x07,
148 DATA_ONLY, 0x07,
149 DATA_ONLY, 0x20,
150 DATA_ONLY, 0x20,
151 DATA_ONLY, 0x20,
152 DATA_ONLY, 0x02,
153 DATA_ONLY, 0x02,
154 ENDDEF, 0x00
155};
156
157static const unsigned short seq_gamma_set1[] = {
158 0xF9, 0x00,
159
160 DATA_ONLY, 0xA7,
161 DATA_ONLY, 0xB4,
162 DATA_ONLY, 0xAE,
163 DATA_ONLY, 0xBF,
164 DATA_ONLY, 0x00,
165 DATA_ONLY, 0x91,
166 DATA_ONLY, 0x00,
167 DATA_ONLY, 0xB2,
168 DATA_ONLY, 0xB4,
169 DATA_ONLY, 0xAA,
170 DATA_ONLY, 0xBB,
171 DATA_ONLY, 0x00,
172 DATA_ONLY, 0xAC,
173 DATA_ONLY, 0x00,
174 DATA_ONLY, 0xB3,
175 DATA_ONLY, 0xB1,
176 DATA_ONLY, 0xAA,
177 DATA_ONLY, 0xBC,
178 DATA_ONLY, 0x00,
179 DATA_ONLY, 0xB3,
180 ENDDEF, 0x00
181};
182
183static const unsigned short seq_gamma_ctrl[] = {
184 0xFB, 0x02,
185
186 DATA_ONLY, 0x5A,
187 ENDDEF, 0x00
188};
189
190static const unsigned short seq_gamma_start[] = {
191 0xF9, COMMAND_ONLY,
192
193 ENDDEF, 0x00
194};
195
196static const unsigned short seq_apon[] = {
197 0xF3, 0x00,
198
199 DATA_ONLY, 0x00,
200 DATA_ONLY, 0x00,
201 DATA_ONLY, 0x0A,
202 DATA_ONLY, 0x02,
203 ENDDEF, 0x00
204};
205
206static const unsigned short seq_display_ctrl[] = {
207 0xF2, 0x02,
208
209 DATA_ONLY, 0x08,
210 DATA_ONLY, 0x08,
211 DATA_ONLY, 0x10,
212 DATA_ONLY, 0x10,
213 ENDDEF, 0x00
214};
215
216static const unsigned short seq_manual_pwr[] = {
217 0xB0, 0x04,
218 ENDDEF, 0x00
219};
220
221static const unsigned short seq_pwr_ctrl[] = {
222 0xF4, 0x0A,
223
224 DATA_ONLY, 0x87,
225 DATA_ONLY, 0x25,
226 DATA_ONLY, 0x6A,
227 DATA_ONLY, 0x44,
228 DATA_ONLY, 0x02,
229 DATA_ONLY, 0x88,
230 ENDDEF, 0x00
231};
232
233static const unsigned short seq_sleep_out[] = {
234 0x11, COMMAND_ONLY,
235 ENDDEF, 0x00
236};
237
238static const unsigned short seq_sleep_in[] = {
239 0x10, COMMAND_ONLY,
240 ENDDEF, 0x00
241};
242
243static const unsigned short seq_display_on[] = {
244 0x29, COMMAND_ONLY,
245 ENDDEF, 0x00
246};
247
248static const unsigned short seq_display_off[] = {
249 0x28, COMMAND_ONLY,
250 ENDDEF, 0x00
251};
252
253static const unsigned short seq_vci1_1st_en[] = {
254 0xF3, 0x10,
255
256 DATA_ONLY, 0x00,
257 DATA_ONLY, 0x00,
258 DATA_ONLY, 0x00,
259 DATA_ONLY, 0x02,
260 ENDDEF, 0x00
261};
262
263static const unsigned short seq_vl1_en[] = {
264 0xF3, 0x11,
265
266 DATA_ONLY, 0x00,
267 DATA_ONLY, 0x00,
268 DATA_ONLY, 0x00,
269 DATA_ONLY, 0x02,
270 ENDDEF, 0x00
271};
272
273static const unsigned short seq_vl2_en[] = {
274 0xF3, 0x13,
275
276 DATA_ONLY, 0x00,
277 DATA_ONLY, 0x00,
278 DATA_ONLY, 0x00,
279 DATA_ONLY, 0x02,
280 ENDDEF, 0x00
281};
282
283static const unsigned short seq_vci1_2nd_en[] = {
284 0xF3, 0x33,
285
286 DATA_ONLY, 0x00,
287 DATA_ONLY, 0x00,
288 DATA_ONLY, 0x00,
289 DATA_ONLY, 0x02,
290 ENDDEF, 0x00
291};
292
293static const unsigned short seq_vl3_en[] = {
294 0xF3, 0x37,
295
296 DATA_ONLY, 0x00,
297 DATA_ONLY, 0x00,
298 DATA_ONLY, 0x00,
299 DATA_ONLY, 0x02,
300 ENDDEF, 0x00
301};
302
303static const unsigned short seq_vreg1_amp_en[] = {
304 0xF3, 0x37,
305
306 DATA_ONLY, 0x01,
307 DATA_ONLY, 0x00,
308 DATA_ONLY, 0x00,
309 DATA_ONLY, 0x02,
310 ENDDEF, 0x00
311};
312
313static const unsigned short seq_vgh_amp_en[] = {
314 0xF3, 0x37,
315
316 DATA_ONLY, 0x11,
317 DATA_ONLY, 0x00,
318 DATA_ONLY, 0x00,
319 DATA_ONLY, 0x02,
320 ENDDEF, 0x00
321};
322
323static const unsigned short seq_vgl_amp_en[] = {
324 0xF3, 0x37,
325
326 DATA_ONLY, 0x31,
327 DATA_ONLY, 0x00,
328 DATA_ONLY, 0x00,
329 DATA_ONLY, 0x02,
330 ENDDEF, 0x00
331};
332
333static const unsigned short seq_vmos_amp_en[] = {
334 0xF3, 0x37,
335
336 DATA_ONLY, 0xB1,
337 DATA_ONLY, 0x00,
338 DATA_ONLY, 0x00,
339 DATA_ONLY, 0x03,
340 ENDDEF, 0x00
341};
342
343static const unsigned short seq_vint_amp_en[] = {
344 0xF3, 0x37,
345
346 DATA_ONLY, 0xF1,
347 /* DATA_ONLY, 0x71, VMOS/VBL/VBH not used */
348 DATA_ONLY, 0x00,
349 DATA_ONLY, 0x00,
350 DATA_ONLY, 0x03,
351 /* DATA_ONLY, 0x02, VMOS/VBL/VBH not used */
352 ENDDEF, 0x00
353};
354
355static const unsigned short seq_vbh_amp_en[] = {
356 0xF3, 0x37,
357
358 DATA_ONLY, 0xF9,
359 DATA_ONLY, 0x00,
360 DATA_ONLY, 0x00,
361 DATA_ONLY, 0x03,
362 ENDDEF, 0x00
363};
364
365static const unsigned short seq_vbl_amp_en[] = {
366 0xF3, 0x37,
367
368 DATA_ONLY, 0xFD,
369 DATA_ONLY, 0x00,
370 DATA_ONLY, 0x00,
371 DATA_ONLY, 0x03,
372 ENDDEF, 0x00
373};
374
375static const unsigned short seq_gam_amp_en[] = {
376 0xF3, 0x37,
377
378 DATA_ONLY, 0xFF,
379 /* DATA_ONLY, 0x73, VMOS/VBL/VBH not used */
380 DATA_ONLY, 0x00,
381 DATA_ONLY, 0x00,
382 DATA_ONLY, 0x03,
383 /* DATA_ONLY, 0x02, VMOS/VBL/VBH not used */
384 ENDDEF, 0x00
385};
386
387static const unsigned short seq_sd_amp_en[] = {
388 0xF3, 0x37,
389
390 DATA_ONLY, 0xFF,
391 /* DATA_ONLY, 0x73, VMOS/VBL/VBH not used */
392 DATA_ONLY, 0x80,
393 DATA_ONLY, 0x00,
394 DATA_ONLY, 0x03,
395 /* DATA_ONLY, 0x02, VMOS/VBL/VBH not used */
396 ENDDEF, 0x00
397};
398
399static const unsigned short seq_gls_en[] = {
400 0xF3, 0x37,
401
402 DATA_ONLY, 0xFF,
403 /* DATA_ONLY, 0x73, VMOS/VBL/VBH not used */
404 DATA_ONLY, 0x81,
405 DATA_ONLY, 0x00,
406 DATA_ONLY, 0x03,
407 /* DATA_ONLY, 0x02, VMOS/VBL/VBH not used */
408 ENDDEF, 0x00
409};
410
411static const unsigned short seq_els_en[] = {
412 0xF3, 0x37,
413
414 DATA_ONLY, 0xFF,
415 /* DATA_ONLY, 0x73, VMOS/VBL/VBH not used */
416 DATA_ONLY, 0x83,
417 DATA_ONLY, 0x00,
418 DATA_ONLY, 0x03,
419 /* DATA_ONLY, 0x02, VMOS/VBL/VBH not used */
420 ENDDEF, 0x00
421};
422
423static const unsigned short seq_el_on[] = {
424 0xF3, 0x37,
425
426 DATA_ONLY, 0xFF,
427 /* DATA_ONLY, 0x73, VMOS/VBL/VBH not used */
428 DATA_ONLY, 0x87,
429 DATA_ONLY, 0x00,
430 DATA_ONLY, 0x03,
431 /* DATA_ONLY, 0x02, VMOS/VBL/VBH not used */
432 ENDDEF, 0x00
433};
434
435static int ld9040_spi_write_byte(struct ld9040 *lcd, int addr, int data)
436{
437 u16 buf[1];
438 struct spi_message msg;
439
440 struct spi_transfer xfer = {
441 .len = 2,
442 .tx_buf = buf,
443 };
444
445 buf[0] = (addr << 8) | data;
446
447 spi_message_init(&msg);
448 spi_message_add_tail(&xfer, &msg);
449
450 return spi_sync(lcd->spi, &msg);
451}
452
453static int ld9040_spi_write(struct ld9040 *lcd, unsigned char address,
454 unsigned char command)
455{
456 int ret = 0;
457
458 if (address != DATA_ONLY)
459 ret = ld9040_spi_write_byte(lcd, 0x0, address);
460 if (command != COMMAND_ONLY)
461 ret = ld9040_spi_write_byte(lcd, 0x1, command);
462
463 return ret;
464}
465
466static int ld9040_panel_send_sequence(struct ld9040 *lcd,
467 const unsigned short *wbuf)
468{
469 int ret = 0, i = 0;
470
471 while ((wbuf[i] & DEFMASK) != ENDDEF) {
472 if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
473 ret = ld9040_spi_write(lcd, wbuf[i], wbuf[i+1]);
474 if (ret)
475 break;
Jingoo Hand2fff292013-02-21 16:43:15 -0800476 } else {
477 msleep(wbuf[i+1]);
478 }
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700479 i += 2;
480 }
481
482 return ret;
483}
484
485static int _ld9040_gamma_ctl(struct ld9040 *lcd, const unsigned int *gamma)
486{
487 unsigned int i = 0;
488 int ret = 0;
489
490 /* start gamma table updating. */
491 ret = ld9040_panel_send_sequence(lcd, seq_gamma_start);
492 if (ret) {
493 dev_err(lcd->dev, "failed to disable gamma table updating.\n");
494 goto gamma_err;
495 }
496
497 for (i = 0 ; i < GAMMA_TABLE_COUNT; i++) {
498 ret = ld9040_spi_write(lcd, DATA_ONLY, gamma[i]);
499 if (ret) {
500 dev_err(lcd->dev, "failed to set gamma table.\n");
501 goto gamma_err;
502 }
503 }
504
505 /* update gamma table. */
506 ret = ld9040_panel_send_sequence(lcd, seq_gamma_ctrl);
507 if (ret)
508 dev_err(lcd->dev, "failed to update gamma table.\n");
509
510gamma_err:
511 return ret;
512}
513
514static int ld9040_gamma_ctl(struct ld9040 *lcd, int gamma)
515{
Jingoo Han2ca8b902013-02-21 16:43:18 -0800516 return _ld9040_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700517}
518
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700519static int ld9040_ldi_init(struct ld9040 *lcd)
520{
521 int ret, i;
522 static const unsigned short *init_seq[] = {
523 seq_user_setting,
524 seq_panel_condition,
525 seq_display_ctrl,
526 seq_manual_pwr,
527 seq_elvss_on,
528 seq_gtcon,
529 seq_gamma_set1,
530 seq_gamma_ctrl,
531 seq_sleep_out,
532 };
533
534 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
535 ret = ld9040_panel_send_sequence(lcd, init_seq[i]);
536 /* workaround: minimum delay time for transferring CMD */
Jingoo Hand2fff292013-02-21 16:43:15 -0800537 usleep_range(300, 310);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700538 if (ret)
539 break;
540 }
541
542 return ret;
543}
544
545static int ld9040_ldi_enable(struct ld9040 *lcd)
546{
Jingoo Han2ca8b902013-02-21 16:43:18 -0800547 return ld9040_panel_send_sequence(lcd, seq_display_on);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700548}
549
550static int ld9040_ldi_disable(struct ld9040 *lcd)
551{
552 int ret;
553
554 ret = ld9040_panel_send_sequence(lcd, seq_display_off);
555 ret = ld9040_panel_send_sequence(lcd, seq_sleep_in);
556
557 return ret;
558}
559
Jingoo Hane2ffe852013-02-21 16:43:16 -0800560static int ld9040_power_is_on(int power)
561{
562 return power <= FB_BLANK_NORMAL;
563}
564
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700565static int ld9040_power_on(struct ld9040 *lcd)
566{
567 int ret = 0;
Jingoo Hane2ffe852013-02-21 16:43:16 -0800568 struct lcd_platform_data *pd;
569
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700570 pd = lcd->lcd_pd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700571
Donghwa Leeb148a272012-01-10 15:09:15 -0800572 /* lcd power on */
573 ld9040_regulator_enable(lcd);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700574
575 if (!pd->reset) {
576 dev_err(lcd->dev, "reset is NULL.\n");
Jingoo Han30f085c2013-02-21 16:43:17 -0800577 return -EINVAL;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700578 } else {
579 pd->reset(lcd->ld);
Jingoo Hand2fff292013-02-21 16:43:15 -0800580 msleep(pd->reset_delay);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700581 }
582
583 ret = ld9040_ldi_init(lcd);
584 if (ret) {
585 dev_err(lcd->dev, "failed to initialize ldi.\n");
586 return ret;
587 }
588
589 ret = ld9040_ldi_enable(lcd);
590 if (ret) {
591 dev_err(lcd->dev, "failed to enable ldi.\n");
592 return ret;
593 }
594
595 return 0;
596}
597
598static int ld9040_power_off(struct ld9040 *lcd)
599{
Jingoo Hane2ffe852013-02-21 16:43:16 -0800600 int ret;
601 struct lcd_platform_data *pd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700602
603 pd = lcd->lcd_pd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700604
605 ret = ld9040_ldi_disable(lcd);
606 if (ret) {
607 dev_err(lcd->dev, "lcd setting failed.\n");
608 return -EIO;
609 }
610
Jingoo Hand2fff292013-02-21 16:43:15 -0800611 msleep(pd->power_off_delay);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700612
Donghwa Leeb148a272012-01-10 15:09:15 -0800613 /* lcd power off */
614 ld9040_regulator_disable(lcd);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700615
616 return 0;
617}
618
619static int ld9040_power(struct ld9040 *lcd, int power)
620{
621 int ret = 0;
622
Jingoo Hane2ffe852013-02-21 16:43:16 -0800623 if (ld9040_power_is_on(power) && !ld9040_power_is_on(lcd->power))
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700624 ret = ld9040_power_on(lcd);
Jingoo Hane2ffe852013-02-21 16:43:16 -0800625 else if (!ld9040_power_is_on(power) && ld9040_power_is_on(lcd->power))
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700626 ret = ld9040_power_off(lcd);
627
628 if (!ret)
629 lcd->power = power;
630
631 return ret;
632}
633
634static int ld9040_set_power(struct lcd_device *ld, int power)
635{
636 struct ld9040 *lcd = lcd_get_data(ld);
637
638 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
639 power != FB_BLANK_NORMAL) {
640 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
641 return -EINVAL;
642 }
643
644 return ld9040_power(lcd, power);
645}
646
647static int ld9040_get_power(struct lcd_device *ld)
648{
649 struct ld9040 *lcd = lcd_get_data(ld);
650
651 return lcd->power;
652}
653
654static int ld9040_get_brightness(struct backlight_device *bd)
655{
656 return bd->props.brightness;
657}
658
659static int ld9040_set_brightness(struct backlight_device *bd)
660{
661 int ret = 0, brightness = bd->props.brightness;
662 struct ld9040 *lcd = bl_get_data(bd);
663
664 if (brightness < MIN_BRIGHTNESS ||
665 brightness > bd->props.max_brightness) {
666 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
667 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
668 return -EINVAL;
669 }
670
671 ret = ld9040_gamma_ctl(lcd, bd->props.brightness);
672 if (ret) {
673 dev_err(&bd->dev, "lcd brightness setting failed.\n");
674 return -EIO;
675 }
676
677 return ret;
678}
679
680static struct lcd_ops ld9040_lcd_ops = {
681 .set_power = ld9040_set_power,
682 .get_power = ld9040_get_power,
683};
684
685static const struct backlight_ops ld9040_backlight_ops = {
686 .get_brightness = ld9040_get_brightness,
687 .update_status = ld9040_set_brightness,
688};
689
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700690static int ld9040_probe(struct spi_device *spi)
691{
692 int ret = 0;
693 struct ld9040 *lcd = NULL;
694 struct lcd_device *ld = NULL;
695 struct backlight_device *bd = NULL;
Axel Linef22f6a2011-07-25 17:12:01 -0700696 struct backlight_properties props;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700697
Jingoo Han86f6be42012-05-29 15:07:23 -0700698 lcd = devm_kzalloc(&spi->dev, sizeof(struct ld9040), GFP_KERNEL);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700699 if (!lcd)
700 return -ENOMEM;
701
702 /* ld9040 lcd panel uses 3-wire 9bits SPI Mode. */
703 spi->bits_per_word = 9;
704
705 ret = spi_setup(spi);
706 if (ret < 0) {
707 dev_err(&spi->dev, "spi setup failed.\n");
Jingoo Han86f6be42012-05-29 15:07:23 -0700708 return ret;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700709 }
710
711 lcd->spi = spi;
712 lcd->dev = &spi->dev;
713
714 lcd->lcd_pd = spi->dev.platform_data;
715 if (!lcd->lcd_pd) {
716 dev_err(&spi->dev, "platform data is NULL.\n");
Jingoo Han30f085c2013-02-21 16:43:17 -0800717 return -EINVAL;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700718 }
719
Donghwa Leeb148a272012-01-10 15:09:15 -0800720 mutex_init(&lcd->lock);
721
722 ret = regulator_bulk_get(lcd->dev, ARRAY_SIZE(supplies), supplies);
723 if (ret) {
724 dev_err(lcd->dev, "Failed to get regulators: %d\n", ret);
Jingoo Han86f6be42012-05-29 15:07:23 -0700725 return ret;
Donghwa Leeb148a272012-01-10 15:09:15 -0800726 }
727
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700728 ld = lcd_device_register("ld9040", &spi->dev, lcd, &ld9040_lcd_ops);
729 if (IS_ERR(ld)) {
730 ret = PTR_ERR(ld);
Jingoo Han86f6be42012-05-29 15:07:23 -0700731 goto out_free_regulator;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700732 }
733
734 lcd->ld = ld;
735
Axel Linef22f6a2011-07-25 17:12:01 -0700736 memset(&props, 0, sizeof(struct backlight_properties));
737 props.type = BACKLIGHT_RAW;
738 props.max_brightness = MAX_BRIGHTNESS;
739
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700740 bd = backlight_device_register("ld9040-bl", &spi->dev,
Axel Linef22f6a2011-07-25 17:12:01 -0700741 lcd, &ld9040_backlight_ops, &props);
Axel Line2e7da92011-07-25 17:11:58 -0700742 if (IS_ERR(bd)) {
743 ret = PTR_ERR(bd);
744 goto out_unregister_lcd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700745 }
746
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700747 bd->props.brightness = MAX_BRIGHTNESS;
748 lcd->bd = bd;
749
750 /*
751 * if lcd panel was on from bootloader like u-boot then
752 * do not lcd on.
753 */
754 if (!lcd->lcd_pd->lcd_enabled) {
755 /*
756 * if lcd panel was off from bootloader then
757 * current lcd status is powerdown and then
758 * it enables lcd panel.
759 */
760 lcd->power = FB_BLANK_POWERDOWN;
761
762 ld9040_power(lcd, FB_BLANK_UNBLANK);
Jingoo Hane2ffe852013-02-21 16:43:16 -0800763 } else {
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700764 lcd->power = FB_BLANK_UNBLANK;
Jingoo Hane2ffe852013-02-21 16:43:16 -0800765 }
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700766
767 dev_set_drvdata(&spi->dev, lcd);
768
769 dev_info(&spi->dev, "ld9040 panel driver has been probed.\n");
770 return 0;
771
Axel Line2e7da92011-07-25 17:11:58 -0700772out_unregister_lcd:
773 lcd_device_unregister(lcd->ld);
Jingoo Han86f6be42012-05-29 15:07:23 -0700774out_free_regulator:
Donghwa Leeb148a272012-01-10 15:09:15 -0800775 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
776
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700777 return ret;
778}
779
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500780static int ld9040_remove(struct spi_device *spi)
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700781{
782 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
783
784 ld9040_power(lcd, FB_BLANK_POWERDOWN);
Axel Line2e7da92011-07-25 17:11:58 -0700785 backlight_device_unregister(lcd->bd);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700786 lcd_device_unregister(lcd->ld);
Donghwa Leeb148a272012-01-10 15:09:15 -0800787 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700788
789 return 0;
790}
791
792#if defined(CONFIG_PM)
793static int ld9040_suspend(struct spi_device *spi, pm_message_t mesg)
794{
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700795 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
796
797 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
798
799 /*
800 * when lcd panel is suspend, lcd panel becomes off
801 * regardless of status.
802 */
Jingoo Han2ca8b902013-02-21 16:43:18 -0800803 return ld9040_power(lcd, FB_BLANK_POWERDOWN);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700804}
805
806static int ld9040_resume(struct spi_device *spi)
807{
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700808 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
809
810 lcd->power = FB_BLANK_POWERDOWN;
811
Jingoo Han2ca8b902013-02-21 16:43:18 -0800812 return ld9040_power(lcd, FB_BLANK_UNBLANK);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700813}
814#else
815#define ld9040_suspend NULL
816#define ld9040_resume NULL
817#endif
818
819/* Power down all displays on reboot, poweroff or halt. */
820static void ld9040_shutdown(struct spi_device *spi)
821{
822 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
823
824 ld9040_power(lcd, FB_BLANK_POWERDOWN);
825}
826
827static struct spi_driver ld9040_driver = {
828 .driver = {
829 .name = "ld9040",
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700830 .owner = THIS_MODULE,
831 },
832 .probe = ld9040_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500833 .remove = ld9040_remove,
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700834 .shutdown = ld9040_shutdown,
835 .suspend = ld9040_suspend,
836 .resume = ld9040_resume,
837};
838
Axel Lin462dd832012-03-23 15:01:59 -0700839module_spi_driver(ld9040_driver);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700840
841MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
842MODULE_DESCRIPTION("ld9040 LCD Driver");
843MODULE_LICENSE("GPL");