ams369fg06.c 12 KB

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