ams369fg06.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * ams369fg06 AMOLED LCD panel driver.
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. *
  7. * Derived from drivers/video/s6e63m0.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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/lcd.h>
  29. #include <linux/backlight.h>
  30. #define SLEEPMSEC 0x1000
  31. #define ENDDEF 0x2000
  32. #define DEFMASK 0xFF00
  33. #define COMMAND_ONLY 0xFE
  34. #define DATA_ONLY 0xFF
  35. #define MAX_GAMMA_LEVEL 5
  36. #define GAMMA_TABLE_COUNT 21
  37. #define MIN_BRIGHTNESS 0
  38. #define MAX_BRIGHTNESS 255
  39. #define DEFAULT_BRIGHTNESS 150
  40. struct ams369fg06 {
  41. struct device *dev;
  42. struct spi_device *spi;
  43. unsigned int power;
  44. struct lcd_device *ld;
  45. struct backlight_device *bd;
  46. struct lcd_platform_data *lcd_pd;
  47. };
  48. static const unsigned short seq_display_on[] = {
  49. 0x14, 0x03,
  50. ENDDEF, 0x0000
  51. };
  52. static const unsigned short seq_display_off[] = {
  53. 0x14, 0x00,
  54. ENDDEF, 0x0000
  55. };
  56. static const unsigned short seq_stand_by_on[] = {
  57. 0x1D, 0xA1,
  58. SLEEPMSEC, 200,
  59. ENDDEF, 0x0000
  60. };
  61. static const unsigned short seq_stand_by_off[] = {
  62. 0x1D, 0xA0,
  63. SLEEPMSEC, 250,
  64. ENDDEF, 0x0000
  65. };
  66. static const unsigned short seq_setting[] = {
  67. 0x31, 0x08,
  68. 0x32, 0x14,
  69. 0x30, 0x02,
  70. 0x27, 0x01,
  71. 0x12, 0x08,
  72. 0x13, 0x08,
  73. 0x15, 0x00,
  74. 0x16, 0x00,
  75. 0xef, 0xd0,
  76. DATA_ONLY, 0xe8,
  77. 0x39, 0x44,
  78. 0x40, 0x00,
  79. 0x41, 0x3f,
  80. 0x42, 0x2a,
  81. 0x43, 0x27,
  82. 0x44, 0x27,
  83. 0x45, 0x1f,
  84. 0x46, 0x44,
  85. 0x50, 0x00,
  86. 0x51, 0x00,
  87. 0x52, 0x17,
  88. 0x53, 0x24,
  89. 0x54, 0x26,
  90. 0x55, 0x1f,
  91. 0x56, 0x43,
  92. 0x60, 0x00,
  93. 0x61, 0x3f,
  94. 0x62, 0x2a,
  95. 0x63, 0x25,
  96. 0x64, 0x24,
  97. 0x65, 0x1b,
  98. 0x66, 0x5c,
  99. 0x17, 0x22,
  100. 0x18, 0x33,
  101. 0x19, 0x03,
  102. 0x1a, 0x01,
  103. 0x22, 0xa4,
  104. 0x23, 0x00,
  105. 0x26, 0xa0,
  106. 0x1d, 0xa0,
  107. SLEEPMSEC, 300,
  108. 0x14, 0x03,
  109. ENDDEF, 0x0000
  110. };
  111. /* gamma value: 2.2 */
  112. static const unsigned int ams369fg06_22_250[] = {
  113. 0x00, 0x3f, 0x2a, 0x27, 0x27, 0x1f, 0x44,
  114. 0x00, 0x00, 0x17, 0x24, 0x26, 0x1f, 0x43,
  115. 0x00, 0x3f, 0x2a, 0x25, 0x24, 0x1b, 0x5c,
  116. };
  117. static const unsigned int ams369fg06_22_200[] = {
  118. 0x00, 0x3f, 0x28, 0x29, 0x27, 0x21, 0x3e,
  119. 0x00, 0x00, 0x10, 0x25, 0x27, 0x20, 0x3d,
  120. 0x00, 0x3f, 0x28, 0x27, 0x25, 0x1d, 0x53,
  121. };
  122. static const unsigned int ams369fg06_22_150[] = {
  123. 0x00, 0x3f, 0x2d, 0x29, 0x28, 0x23, 0x37,
  124. 0x00, 0x00, 0x0b, 0x25, 0x28, 0x22, 0x36,
  125. 0x00, 0x3f, 0x2b, 0x28, 0x26, 0x1f, 0x4a,
  126. };
  127. static const unsigned int ams369fg06_22_100[] = {
  128. 0x00, 0x3f, 0x30, 0x2a, 0x2b, 0x24, 0x2f,
  129. 0x00, 0x00, 0x00, 0x25, 0x29, 0x24, 0x2e,
  130. 0x00, 0x3f, 0x2f, 0x29, 0x29, 0x21, 0x3f,
  131. };
  132. static const unsigned int ams369fg06_22_50[] = {
  133. 0x00, 0x3f, 0x3c, 0x2c, 0x2d, 0x27, 0x24,
  134. 0x00, 0x00, 0x00, 0x22, 0x2a, 0x27, 0x23,
  135. 0x00, 0x3f, 0x3b, 0x2c, 0x2b, 0x24, 0x31,
  136. };
  137. struct ams369fg06_gamma {
  138. unsigned int *gamma_22_table[MAX_GAMMA_LEVEL];
  139. };
  140. static struct ams369fg06_gamma gamma_table = {
  141. .gamma_22_table[0] = (unsigned int *)&ams369fg06_22_50,
  142. .gamma_22_table[1] = (unsigned int *)&ams369fg06_22_100,
  143. .gamma_22_table[2] = (unsigned int *)&ams369fg06_22_150,
  144. .gamma_22_table[3] = (unsigned int *)&ams369fg06_22_200,
  145. .gamma_22_table[4] = (unsigned int *)&ams369fg06_22_250,
  146. };
  147. static int ams369fg06_spi_write_byte(struct ams369fg06 *lcd, int addr, int data)
  148. {
  149. u16 buf[1];
  150. struct spi_message msg;
  151. struct spi_transfer xfer = {
  152. .len = 2,
  153. .tx_buf = buf,
  154. };
  155. buf[0] = (addr << 8) | data;
  156. spi_message_init(&msg);
  157. spi_message_add_tail(&xfer, &msg);
  158. return spi_sync(lcd->spi, &msg);
  159. }
  160. static int ams369fg06_spi_write(struct ams369fg06 *lcd, unsigned char address,
  161. unsigned char command)
  162. {
  163. int ret = 0;
  164. if (address != DATA_ONLY)
  165. ret = ams369fg06_spi_write_byte(lcd, 0x70, address);
  166. if (command != COMMAND_ONLY)
  167. ret = ams369fg06_spi_write_byte(lcd, 0x72, command);
  168. return ret;
  169. }
  170. static int ams369fg06_panel_send_sequence(struct ams369fg06 *lcd,
  171. const unsigned short *wbuf)
  172. {
  173. int ret = 0, i = 0;
  174. while ((wbuf[i] & DEFMASK) != ENDDEF) {
  175. if ((wbuf[i] & DEFMASK) != SLEEPMSEC) {
  176. ret = ams369fg06_spi_write(lcd, wbuf[i], wbuf[i+1]);
  177. if (ret)
  178. break;
  179. } else
  180. mdelay(wbuf[i+1]);
  181. i += 2;
  182. }
  183. return ret;
  184. }
  185. static int _ams369fg06_gamma_ctl(struct ams369fg06 *lcd,
  186. const unsigned int *gamma)
  187. {
  188. unsigned int i = 0;
  189. int ret = 0;
  190. for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
  191. ret = ams369fg06_spi_write(lcd, 0x40 + i, gamma[i]);
  192. ret = ams369fg06_spi_write(lcd, 0x50 + i, gamma[i+7*1]);
  193. ret = ams369fg06_spi_write(lcd, 0x60 + i, gamma[i+7*2]);
  194. if (ret) {
  195. dev_err(lcd->dev, "failed to set gamma table.\n");
  196. goto gamma_err;
  197. }
  198. }
  199. gamma_err:
  200. return ret;
  201. }
  202. static int ams369fg06_gamma_ctl(struct ams369fg06 *lcd, int brightness)
  203. {
  204. int ret = 0;
  205. int gamma = 0;
  206. if ((brightness >= 0) && (brightness <= 50))
  207. gamma = 0;
  208. else if ((brightness > 50) && (brightness <= 100))
  209. gamma = 1;
  210. else if ((brightness > 100) && (brightness <= 150))
  211. gamma = 2;
  212. else if ((brightness > 150) && (brightness <= 200))
  213. gamma = 3;
  214. else if ((brightness > 200) && (brightness <= 255))
  215. gamma = 4;
  216. ret = _ams369fg06_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
  217. return ret;
  218. }
  219. static int ams369fg06_ldi_init(struct ams369fg06 *lcd)
  220. {
  221. int ret, i;
  222. static const unsigned short *init_seq[] = {
  223. seq_setting,
  224. seq_stand_by_off,
  225. };
  226. for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
  227. ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
  228. if (ret)
  229. break;
  230. }
  231. return ret;
  232. }
  233. static int ams369fg06_ldi_enable(struct ams369fg06 *lcd)
  234. {
  235. int ret, i;
  236. static const unsigned short *init_seq[] = {
  237. seq_stand_by_off,
  238. seq_display_on,
  239. };
  240. for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
  241. ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
  242. if (ret)
  243. break;
  244. }
  245. return ret;
  246. }
  247. static int ams369fg06_ldi_disable(struct ams369fg06 *lcd)
  248. {
  249. int ret, i;
  250. static const unsigned short *init_seq[] = {
  251. seq_display_off,
  252. seq_stand_by_on,
  253. };
  254. for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
  255. ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
  256. if (ret)
  257. break;
  258. }
  259. return ret;
  260. }
  261. static int ams369fg06_power_is_on(int power)
  262. {
  263. return ((power) <= FB_BLANK_NORMAL);
  264. }
  265. static int ams369fg06_power_on(struct ams369fg06 *lcd)
  266. {
  267. int ret = 0;
  268. struct lcd_platform_data *pd = NULL;
  269. struct backlight_device *bd = NULL;
  270. pd = lcd->lcd_pd;
  271. if (!pd) {
  272. dev_err(lcd->dev, "platform data is NULL.\n");
  273. return -EFAULT;
  274. }
  275. bd = lcd->bd;
  276. if (!bd) {
  277. dev_err(lcd->dev, "backlight device is NULL.\n");
  278. return -EFAULT;
  279. }
  280. if (!pd->power_on) {
  281. dev_err(lcd->dev, "power_on is NULL.\n");
  282. return -EFAULT;
  283. } else {
  284. pd->power_on(lcd->ld, 1);
  285. mdelay(pd->power_on_delay);
  286. }
  287. if (!pd->reset) {
  288. dev_err(lcd->dev, "reset is NULL.\n");
  289. return -EFAULT;
  290. } else {
  291. pd->reset(lcd->ld);
  292. mdelay(pd->reset_delay);
  293. }
  294. ret = ams369fg06_ldi_init(lcd);
  295. if (ret) {
  296. dev_err(lcd->dev, "failed to initialize ldi.\n");
  297. return ret;
  298. }
  299. ret = ams369fg06_ldi_enable(lcd);
  300. if (ret) {
  301. dev_err(lcd->dev, "failed to enable ldi.\n");
  302. return ret;
  303. }
  304. /* set brightness to current value after power on or resume. */
  305. ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
  306. if (ret) {
  307. dev_err(lcd->dev, "lcd gamma setting failed.\n");
  308. return ret;
  309. }
  310. return 0;
  311. }
  312. static int ams369fg06_power_off(struct ams369fg06 *lcd)
  313. {
  314. int ret = 0;
  315. struct lcd_platform_data *pd = NULL;
  316. pd = lcd->lcd_pd;
  317. if (!pd) {
  318. dev_err(lcd->dev, "platform data is NULL\n");
  319. return -EFAULT;
  320. }
  321. ret = ams369fg06_ldi_disable(lcd);
  322. if (ret) {
  323. dev_err(lcd->dev, "lcd setting failed.\n");
  324. return -EIO;
  325. }
  326. mdelay(pd->power_off_delay);
  327. if (!pd->power_on) {
  328. dev_err(lcd->dev, "power_on is NULL.\n");
  329. return -EFAULT;
  330. } else
  331. pd->power_on(lcd->ld, 0);
  332. return 0;
  333. }
  334. static int ams369fg06_power(struct ams369fg06 *lcd, int power)
  335. {
  336. int ret = 0;
  337. if (ams369fg06_power_is_on(power) &&
  338. !ams369fg06_power_is_on(lcd->power))
  339. ret = ams369fg06_power_on(lcd);
  340. else if (!ams369fg06_power_is_on(power) &&
  341. ams369fg06_power_is_on(lcd->power))
  342. ret = ams369fg06_power_off(lcd);
  343. if (!ret)
  344. lcd->power = power;
  345. return ret;
  346. }
  347. static int ams369fg06_get_power(struct lcd_device *ld)
  348. {
  349. struct ams369fg06 *lcd = lcd_get_data(ld);
  350. return lcd->power;
  351. }
  352. static int ams369fg06_set_power(struct lcd_device *ld, int power)
  353. {
  354. struct ams369fg06 *lcd = lcd_get_data(ld);
  355. if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
  356. power != FB_BLANK_NORMAL) {
  357. dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
  358. return -EINVAL;
  359. }
  360. return ams369fg06_power(lcd, power);
  361. }
  362. static int ams369fg06_get_brightness(struct backlight_device *bd)
  363. {
  364. return bd->props.brightness;
  365. }
  366. static int ams369fg06_set_brightness(struct backlight_device *bd)
  367. {
  368. int ret = 0;
  369. int brightness = bd->props.brightness;
  370. struct ams369fg06 *lcd = dev_get_drvdata(&bd->dev);
  371. if (brightness < MIN_BRIGHTNESS ||
  372. brightness > bd->props.max_brightness) {
  373. dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
  374. MIN_BRIGHTNESS, MAX_BRIGHTNESS);
  375. return -EINVAL;
  376. }
  377. ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
  378. if (ret) {
  379. dev_err(&bd->dev, "lcd brightness setting failed.\n");
  380. return -EIO;
  381. }
  382. return ret;
  383. }
  384. static struct lcd_ops ams369fg06_lcd_ops = {
  385. .get_power = ams369fg06_get_power,
  386. .set_power = ams369fg06_set_power,
  387. };
  388. static const struct backlight_ops ams369fg06_backlight_ops = {
  389. .get_brightness = ams369fg06_get_brightness,
  390. .update_status = ams369fg06_set_brightness,
  391. };
  392. static int __devinit ams369fg06_probe(struct spi_device *spi)
  393. {
  394. int ret = 0;
  395. struct ams369fg06 *lcd = NULL;
  396. struct lcd_device *ld = NULL;
  397. struct backlight_device *bd = NULL;
  398. struct backlight_properties props;
  399. lcd = kzalloc(sizeof(struct ams369fg06), GFP_KERNEL);
  400. if (!lcd)
  401. return -ENOMEM;
  402. /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
  403. spi->bits_per_word = 16;
  404. ret = spi_setup(spi);
  405. if (ret < 0) {
  406. dev_err(&spi->dev, "spi setup failed.\n");
  407. goto out_free_lcd;
  408. }
  409. lcd->spi = spi;
  410. lcd->dev = &spi->dev;
  411. lcd->lcd_pd = spi->dev.platform_data;
  412. if (!lcd->lcd_pd) {
  413. dev_err(&spi->dev, "platform data is NULL\n");
  414. goto out_free_lcd;
  415. }
  416. ld = lcd_device_register("ams369fg06", &spi->dev, lcd,
  417. &ams369fg06_lcd_ops);
  418. if (IS_ERR(ld)) {
  419. ret = PTR_ERR(ld);
  420. goto out_free_lcd;
  421. }
  422. lcd->ld = ld;
  423. memset(&props, 0, sizeof(struct backlight_properties));
  424. props.type = BACKLIGHT_RAW;
  425. props.max_brightness = MAX_BRIGHTNESS;
  426. bd = backlight_device_register("ams369fg06-bl", &spi->dev, lcd,
  427. &ams369fg06_backlight_ops, &props);
  428. if (IS_ERR(bd)) {
  429. ret = PTR_ERR(bd);
  430. goto out_lcd_unregister;
  431. }
  432. bd->props.brightness = DEFAULT_BRIGHTNESS;
  433. lcd->bd = bd;
  434. if (!lcd->lcd_pd->lcd_enabled) {
  435. /*
  436. * if lcd panel was off from bootloader then
  437. * current lcd status is powerdown and then
  438. * it enables lcd panel.
  439. */
  440. lcd->power = FB_BLANK_POWERDOWN;
  441. ams369fg06_power(lcd, FB_BLANK_UNBLANK);
  442. } else
  443. lcd->power = FB_BLANK_UNBLANK;
  444. dev_set_drvdata(&spi->dev, lcd);
  445. dev_info(&spi->dev, "ams369fg06 panel driver has been probed.\n");
  446. return 0;
  447. out_lcd_unregister:
  448. lcd_device_unregister(ld);
  449. out_free_lcd:
  450. kfree(lcd);
  451. return ret;
  452. }
  453. static int __devexit ams369fg06_remove(struct spi_device *spi)
  454. {
  455. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  456. ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
  457. backlight_device_unregister(lcd->bd);
  458. lcd_device_unregister(lcd->ld);
  459. kfree(lcd);
  460. return 0;
  461. }
  462. #if defined(CONFIG_PM)
  463. static unsigned int before_power;
  464. static int ams369fg06_suspend(struct spi_device *spi, pm_message_t mesg)
  465. {
  466. int ret = 0;
  467. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  468. dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
  469. before_power = lcd->power;
  470. /*
  471. * when lcd panel is suspend, lcd panel becomes off
  472. * regardless of status.
  473. */
  474. ret = ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
  475. return ret;
  476. }
  477. static int ams369fg06_resume(struct spi_device *spi)
  478. {
  479. int ret = 0;
  480. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  481. /*
  482. * after suspended, if lcd panel status is FB_BLANK_UNBLANK
  483. * (at that time, before_power is FB_BLANK_UNBLANK) then
  484. * it changes that status to FB_BLANK_POWERDOWN to get lcd on.
  485. */
  486. if (before_power == FB_BLANK_UNBLANK)
  487. lcd->power = FB_BLANK_POWERDOWN;
  488. dev_dbg(&spi->dev, "before_power = %d\n", before_power);
  489. ret = ams369fg06_power(lcd, before_power);
  490. return ret;
  491. }
  492. #else
  493. #define ams369fg06_suspend NULL
  494. #define ams369fg06_resume NULL
  495. #endif
  496. static void ams369fg06_shutdown(struct spi_device *spi)
  497. {
  498. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  499. ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
  500. }
  501. static struct spi_driver ams369fg06_driver = {
  502. .driver = {
  503. .name = "ams369fg06",
  504. .bus = &spi_bus_type,
  505. .owner = THIS_MODULE,
  506. },
  507. .probe = ams369fg06_probe,
  508. .remove = __devexit_p(ams369fg06_remove),
  509. .shutdown = ams369fg06_shutdown,
  510. .suspend = ams369fg06_suspend,
  511. .resume = ams369fg06_resume,
  512. };
  513. static int __init ams369fg06_init(void)
  514. {
  515. return spi_register_driver(&ams369fg06_driver);
  516. }
  517. static void __exit ams369fg06_exit(void)
  518. {
  519. spi_unregister_driver(&ams369fg06_driver);
  520. }
  521. module_init(ams369fg06_init);
  522. module_exit(ams369fg06_exit);
  523. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  524. MODULE_DESCRIPTION("ams369fg06 LCD Driver");
  525. MODULE_LICENSE("GPL");