ams369fg06.c 13 KB

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