blob: fd7bb1cf21a856b73a1e9975a05db6d471a56d35 [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{
516 int ret = 0;
517
518 ret = _ld9040_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
519
520 return ret;
521}
522
523
524static int ld9040_ldi_init(struct ld9040 *lcd)
525{
526 int ret, i;
527 static const unsigned short *init_seq[] = {
528 seq_user_setting,
529 seq_panel_condition,
530 seq_display_ctrl,
531 seq_manual_pwr,
532 seq_elvss_on,
533 seq_gtcon,
534 seq_gamma_set1,
535 seq_gamma_ctrl,
536 seq_sleep_out,
537 };
538
539 for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
540 ret = ld9040_panel_send_sequence(lcd, init_seq[i]);
541 /* workaround: minimum delay time for transferring CMD */
Jingoo Hand2fff292013-02-21 16:43:15 -0800542 usleep_range(300, 310);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700543 if (ret)
544 break;
545 }
546
547 return ret;
548}
549
550static int ld9040_ldi_enable(struct ld9040 *lcd)
551{
552 int ret = 0;
553
554 ret = ld9040_panel_send_sequence(lcd, seq_display_on);
555
556 return ret;
557}
558
559static int ld9040_ldi_disable(struct ld9040 *lcd)
560{
561 int ret;
562
563 ret = ld9040_panel_send_sequence(lcd, seq_display_off);
564 ret = ld9040_panel_send_sequence(lcd, seq_sleep_in);
565
566 return ret;
567}
568
Jingoo Hane2ffe852013-02-21 16:43:16 -0800569static int ld9040_power_is_on(int power)
570{
571 return power <= FB_BLANK_NORMAL;
572}
573
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700574static int ld9040_power_on(struct ld9040 *lcd)
575{
576 int ret = 0;
Jingoo Hane2ffe852013-02-21 16:43:16 -0800577 struct lcd_platform_data *pd;
578
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700579 pd = lcd->lcd_pd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700580
Donghwa Leeb148a272012-01-10 15:09:15 -0800581 /* lcd power on */
582 ld9040_regulator_enable(lcd);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700583
584 if (!pd->reset) {
585 dev_err(lcd->dev, "reset is NULL.\n");
Jingoo Han30f085c2013-02-21 16:43:17 -0800586 return -EINVAL;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700587 } else {
588 pd->reset(lcd->ld);
Jingoo Hand2fff292013-02-21 16:43:15 -0800589 msleep(pd->reset_delay);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700590 }
591
592 ret = ld9040_ldi_init(lcd);
593 if (ret) {
594 dev_err(lcd->dev, "failed to initialize ldi.\n");
595 return ret;
596 }
597
598 ret = ld9040_ldi_enable(lcd);
599 if (ret) {
600 dev_err(lcd->dev, "failed to enable ldi.\n");
601 return ret;
602 }
603
604 return 0;
605}
606
607static int ld9040_power_off(struct ld9040 *lcd)
608{
Jingoo Hane2ffe852013-02-21 16:43:16 -0800609 int ret;
610 struct lcd_platform_data *pd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700611
612 pd = lcd->lcd_pd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700613
614 ret = ld9040_ldi_disable(lcd);
615 if (ret) {
616 dev_err(lcd->dev, "lcd setting failed.\n");
617 return -EIO;
618 }
619
Jingoo Hand2fff292013-02-21 16:43:15 -0800620 msleep(pd->power_off_delay);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700621
Donghwa Leeb148a272012-01-10 15:09:15 -0800622 /* lcd power off */
623 ld9040_regulator_disable(lcd);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700624
625 return 0;
626}
627
628static int ld9040_power(struct ld9040 *lcd, int power)
629{
630 int ret = 0;
631
Jingoo Hane2ffe852013-02-21 16:43:16 -0800632 if (ld9040_power_is_on(power) && !ld9040_power_is_on(lcd->power))
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700633 ret = ld9040_power_on(lcd);
Jingoo Hane2ffe852013-02-21 16:43:16 -0800634 else if (!ld9040_power_is_on(power) && ld9040_power_is_on(lcd->power))
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700635 ret = ld9040_power_off(lcd);
636
637 if (!ret)
638 lcd->power = power;
639
640 return ret;
641}
642
643static int ld9040_set_power(struct lcd_device *ld, int power)
644{
645 struct ld9040 *lcd = lcd_get_data(ld);
646
647 if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
648 power != FB_BLANK_NORMAL) {
649 dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
650 return -EINVAL;
651 }
652
653 return ld9040_power(lcd, power);
654}
655
656static int ld9040_get_power(struct lcd_device *ld)
657{
658 struct ld9040 *lcd = lcd_get_data(ld);
659
660 return lcd->power;
661}
662
663static int ld9040_get_brightness(struct backlight_device *bd)
664{
665 return bd->props.brightness;
666}
667
668static int ld9040_set_brightness(struct backlight_device *bd)
669{
670 int ret = 0, brightness = bd->props.brightness;
671 struct ld9040 *lcd = bl_get_data(bd);
672
673 if (brightness < MIN_BRIGHTNESS ||
674 brightness > bd->props.max_brightness) {
675 dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
676 MIN_BRIGHTNESS, MAX_BRIGHTNESS);
677 return -EINVAL;
678 }
679
680 ret = ld9040_gamma_ctl(lcd, bd->props.brightness);
681 if (ret) {
682 dev_err(&bd->dev, "lcd brightness setting failed.\n");
683 return -EIO;
684 }
685
686 return ret;
687}
688
689static struct lcd_ops ld9040_lcd_ops = {
690 .set_power = ld9040_set_power,
691 .get_power = ld9040_get_power,
692};
693
694static const struct backlight_ops ld9040_backlight_ops = {
695 .get_brightness = ld9040_get_brightness,
696 .update_status = ld9040_set_brightness,
697};
698
699
700static int ld9040_probe(struct spi_device *spi)
701{
702 int ret = 0;
703 struct ld9040 *lcd = NULL;
704 struct lcd_device *ld = NULL;
705 struct backlight_device *bd = NULL;
Axel Linef22f6a2011-07-25 17:12:01 -0700706 struct backlight_properties props;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700707
Jingoo Han86f6be42012-05-29 15:07:23 -0700708 lcd = devm_kzalloc(&spi->dev, sizeof(struct ld9040), GFP_KERNEL);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700709 if (!lcd)
710 return -ENOMEM;
711
712 /* ld9040 lcd panel uses 3-wire 9bits SPI Mode. */
713 spi->bits_per_word = 9;
714
715 ret = spi_setup(spi);
716 if (ret < 0) {
717 dev_err(&spi->dev, "spi setup failed.\n");
Jingoo Han86f6be42012-05-29 15:07:23 -0700718 return ret;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700719 }
720
721 lcd->spi = spi;
722 lcd->dev = &spi->dev;
723
724 lcd->lcd_pd = spi->dev.platform_data;
725 if (!lcd->lcd_pd) {
726 dev_err(&spi->dev, "platform data is NULL.\n");
Jingoo Han30f085c2013-02-21 16:43:17 -0800727 return -EINVAL;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700728 }
729
Donghwa Leeb148a272012-01-10 15:09:15 -0800730 mutex_init(&lcd->lock);
731
732 ret = regulator_bulk_get(lcd->dev, ARRAY_SIZE(supplies), supplies);
733 if (ret) {
734 dev_err(lcd->dev, "Failed to get regulators: %d\n", ret);
Jingoo Han86f6be42012-05-29 15:07:23 -0700735 return ret;
Donghwa Leeb148a272012-01-10 15:09:15 -0800736 }
737
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700738 ld = lcd_device_register("ld9040", &spi->dev, lcd, &ld9040_lcd_ops);
739 if (IS_ERR(ld)) {
740 ret = PTR_ERR(ld);
Jingoo Han86f6be42012-05-29 15:07:23 -0700741 goto out_free_regulator;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700742 }
743
744 lcd->ld = ld;
745
Axel Linef22f6a2011-07-25 17:12:01 -0700746 memset(&props, 0, sizeof(struct backlight_properties));
747 props.type = BACKLIGHT_RAW;
748 props.max_brightness = MAX_BRIGHTNESS;
749
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700750 bd = backlight_device_register("ld9040-bl", &spi->dev,
Axel Linef22f6a2011-07-25 17:12:01 -0700751 lcd, &ld9040_backlight_ops, &props);
Axel Line2e7da92011-07-25 17:11:58 -0700752 if (IS_ERR(bd)) {
753 ret = PTR_ERR(bd);
754 goto out_unregister_lcd;
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700755 }
756
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700757 bd->props.brightness = MAX_BRIGHTNESS;
758 lcd->bd = bd;
759
760 /*
761 * if lcd panel was on from bootloader like u-boot then
762 * do not lcd on.
763 */
764 if (!lcd->lcd_pd->lcd_enabled) {
765 /*
766 * if lcd panel was off from bootloader then
767 * current lcd status is powerdown and then
768 * it enables lcd panel.
769 */
770 lcd->power = FB_BLANK_POWERDOWN;
771
772 ld9040_power(lcd, FB_BLANK_UNBLANK);
Jingoo Hane2ffe852013-02-21 16:43:16 -0800773 } else {
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700774 lcd->power = FB_BLANK_UNBLANK;
Jingoo Hane2ffe852013-02-21 16:43:16 -0800775 }
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700776
777 dev_set_drvdata(&spi->dev, lcd);
778
779 dev_info(&spi->dev, "ld9040 panel driver has been probed.\n");
780 return 0;
781
Axel Line2e7da92011-07-25 17:11:58 -0700782out_unregister_lcd:
783 lcd_device_unregister(lcd->ld);
Jingoo Han86f6be42012-05-29 15:07:23 -0700784out_free_regulator:
Donghwa Leeb148a272012-01-10 15:09:15 -0800785 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
786
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700787 return ret;
788}
789
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500790static int ld9040_remove(struct spi_device *spi)
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700791{
792 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
793
794 ld9040_power(lcd, FB_BLANK_POWERDOWN);
Axel Line2e7da92011-07-25 17:11:58 -0700795 backlight_device_unregister(lcd->bd);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700796 lcd_device_unregister(lcd->ld);
Donghwa Leeb148a272012-01-10 15:09:15 -0800797 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700798
799 return 0;
800}
801
802#if defined(CONFIG_PM)
803static int ld9040_suspend(struct spi_device *spi, pm_message_t mesg)
804{
805 int ret = 0;
806 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
807
808 dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
809
810 /*
811 * when lcd panel is suspend, lcd panel becomes off
812 * regardless of status.
813 */
814 ret = ld9040_power(lcd, FB_BLANK_POWERDOWN);
815
816 return ret;
817}
818
819static int ld9040_resume(struct spi_device *spi)
820{
821 int ret = 0;
822 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
823
824 lcd->power = FB_BLANK_POWERDOWN;
825
826 ret = ld9040_power(lcd, FB_BLANK_UNBLANK);
827
828 return ret;
829}
830#else
831#define ld9040_suspend NULL
832#define ld9040_resume NULL
833#endif
834
835/* Power down all displays on reboot, poweroff or halt. */
836static void ld9040_shutdown(struct spi_device *spi)
837{
838 struct ld9040 *lcd = dev_get_drvdata(&spi->dev);
839
840 ld9040_power(lcd, FB_BLANK_POWERDOWN);
841}
842
843static struct spi_driver ld9040_driver = {
844 .driver = {
845 .name = "ld9040",
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700846 .owner = THIS_MODULE,
847 },
848 .probe = ld9040_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500849 .remove = ld9040_remove,
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700850 .shutdown = ld9040_shutdown,
851 .suspend = ld9040_suspend,
852 .resume = ld9040_resume,
853};
854
Axel Lin462dd832012-03-23 15:01:59 -0700855module_spi_driver(ld9040_driver);
Donghwa Lee1baf0eb2011-03-22 16:30:18 -0700856
857MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
858MODULE_DESCRIPTION("ld9040 LCD Driver");
859MODULE_LICENSE("GPL");