ams369fg06.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. msleep(wbuf[i+1]);
  182. }
  183. i += 2;
  184. }
  185. return ret;
  186. }
  187. static int _ams369fg06_gamma_ctl(struct ams369fg06 *lcd,
  188. const unsigned int *gamma)
  189. {
  190. unsigned int i = 0;
  191. int ret = 0;
  192. for (i = 0 ; i < GAMMA_TABLE_COUNT / 3; i++) {
  193. ret = ams369fg06_spi_write(lcd, 0x40 + i, gamma[i]);
  194. ret = ams369fg06_spi_write(lcd, 0x50 + i, gamma[i+7*1]);
  195. ret = ams369fg06_spi_write(lcd, 0x60 + i, gamma[i+7*2]);
  196. if (ret) {
  197. dev_err(lcd->dev, "failed to set gamma table.\n");
  198. goto gamma_err;
  199. }
  200. }
  201. gamma_err:
  202. return ret;
  203. }
  204. static int ams369fg06_gamma_ctl(struct ams369fg06 *lcd, int brightness)
  205. {
  206. int ret = 0;
  207. int gamma = 0;
  208. if ((brightness >= 0) && (brightness <= 50))
  209. gamma = 0;
  210. else if ((brightness > 50) && (brightness <= 100))
  211. gamma = 1;
  212. else if ((brightness > 100) && (brightness <= 150))
  213. gamma = 2;
  214. else if ((brightness > 150) && (brightness <= 200))
  215. gamma = 3;
  216. else if ((brightness > 200) && (brightness <= 255))
  217. gamma = 4;
  218. ret = _ams369fg06_gamma_ctl(lcd, gamma_table.gamma_22_table[gamma]);
  219. return ret;
  220. }
  221. static int ams369fg06_ldi_init(struct ams369fg06 *lcd)
  222. {
  223. int ret, i;
  224. static const unsigned short *init_seq[] = {
  225. seq_setting,
  226. seq_stand_by_off,
  227. };
  228. for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
  229. ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
  230. if (ret)
  231. break;
  232. }
  233. return ret;
  234. }
  235. static int ams369fg06_ldi_enable(struct ams369fg06 *lcd)
  236. {
  237. int ret, i;
  238. static const unsigned short *init_seq[] = {
  239. seq_stand_by_off,
  240. seq_display_on,
  241. };
  242. for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
  243. ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
  244. if (ret)
  245. break;
  246. }
  247. return ret;
  248. }
  249. static int ams369fg06_ldi_disable(struct ams369fg06 *lcd)
  250. {
  251. int ret, i;
  252. static const unsigned short *init_seq[] = {
  253. seq_display_off,
  254. seq_stand_by_on,
  255. };
  256. for (i = 0; i < ARRAY_SIZE(init_seq); i++) {
  257. ret = ams369fg06_panel_send_sequence(lcd, init_seq[i]);
  258. if (ret)
  259. break;
  260. }
  261. return ret;
  262. }
  263. static int ams369fg06_power_is_on(int power)
  264. {
  265. return power <= FB_BLANK_NORMAL;
  266. }
  267. static int ams369fg06_power_on(struct ams369fg06 *lcd)
  268. {
  269. int ret = 0;
  270. struct lcd_platform_data *pd;
  271. struct backlight_device *bd;
  272. pd = lcd->lcd_pd;
  273. bd = lcd->bd;
  274. if (!pd->power_on) {
  275. dev_err(lcd->dev, "power_on is NULL.\n");
  276. return -EINVAL;
  277. } else {
  278. pd->power_on(lcd->ld, 1);
  279. msleep(pd->power_on_delay);
  280. }
  281. if (!pd->reset) {
  282. dev_err(lcd->dev, "reset is NULL.\n");
  283. return -EINVAL;
  284. } else {
  285. pd->reset(lcd->ld);
  286. msleep(pd->reset_delay);
  287. }
  288. ret = ams369fg06_ldi_init(lcd);
  289. if (ret) {
  290. dev_err(lcd->dev, "failed to initialize ldi.\n");
  291. return ret;
  292. }
  293. ret = ams369fg06_ldi_enable(lcd);
  294. if (ret) {
  295. dev_err(lcd->dev, "failed to enable ldi.\n");
  296. return ret;
  297. }
  298. /* set brightness to current value after power on or resume. */
  299. ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
  300. if (ret) {
  301. dev_err(lcd->dev, "lcd gamma setting failed.\n");
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. static int ams369fg06_power_off(struct ams369fg06 *lcd)
  307. {
  308. int ret;
  309. struct lcd_platform_data *pd;
  310. pd = lcd->lcd_pd;
  311. ret = ams369fg06_ldi_disable(lcd);
  312. if (ret) {
  313. dev_err(lcd->dev, "lcd setting failed.\n");
  314. return -EIO;
  315. }
  316. msleep(pd->power_off_delay);
  317. pd->power_on(lcd->ld, 0);
  318. return 0;
  319. }
  320. static int ams369fg06_power(struct ams369fg06 *lcd, int power)
  321. {
  322. int ret = 0;
  323. if (ams369fg06_power_is_on(power) &&
  324. !ams369fg06_power_is_on(lcd->power))
  325. ret = ams369fg06_power_on(lcd);
  326. else if (!ams369fg06_power_is_on(power) &&
  327. ams369fg06_power_is_on(lcd->power))
  328. ret = ams369fg06_power_off(lcd);
  329. if (!ret)
  330. lcd->power = power;
  331. return ret;
  332. }
  333. static int ams369fg06_get_power(struct lcd_device *ld)
  334. {
  335. struct ams369fg06 *lcd = lcd_get_data(ld);
  336. return lcd->power;
  337. }
  338. static int ams369fg06_set_power(struct lcd_device *ld, int power)
  339. {
  340. struct ams369fg06 *lcd = lcd_get_data(ld);
  341. if (power != FB_BLANK_UNBLANK && power != FB_BLANK_POWERDOWN &&
  342. power != FB_BLANK_NORMAL) {
  343. dev_err(lcd->dev, "power value should be 0, 1 or 4.\n");
  344. return -EINVAL;
  345. }
  346. return ams369fg06_power(lcd, power);
  347. }
  348. static int ams369fg06_get_brightness(struct backlight_device *bd)
  349. {
  350. return bd->props.brightness;
  351. }
  352. static int ams369fg06_set_brightness(struct backlight_device *bd)
  353. {
  354. int ret = 0;
  355. int brightness = bd->props.brightness;
  356. struct ams369fg06 *lcd = dev_get_drvdata(&bd->dev);
  357. if (brightness < MIN_BRIGHTNESS ||
  358. brightness > bd->props.max_brightness) {
  359. dev_err(&bd->dev, "lcd brightness should be %d to %d.\n",
  360. MIN_BRIGHTNESS, MAX_BRIGHTNESS);
  361. return -EINVAL;
  362. }
  363. ret = ams369fg06_gamma_ctl(lcd, bd->props.brightness);
  364. if (ret) {
  365. dev_err(&bd->dev, "lcd brightness setting failed.\n");
  366. return -EIO;
  367. }
  368. return ret;
  369. }
  370. static struct lcd_ops ams369fg06_lcd_ops = {
  371. .get_power = ams369fg06_get_power,
  372. .set_power = ams369fg06_set_power,
  373. };
  374. static const struct backlight_ops ams369fg06_backlight_ops = {
  375. .get_brightness = ams369fg06_get_brightness,
  376. .update_status = ams369fg06_set_brightness,
  377. };
  378. static int ams369fg06_probe(struct spi_device *spi)
  379. {
  380. int ret = 0;
  381. struct ams369fg06 *lcd = NULL;
  382. struct lcd_device *ld = NULL;
  383. struct backlight_device *bd = NULL;
  384. struct backlight_properties props;
  385. lcd = devm_kzalloc(&spi->dev, sizeof(struct ams369fg06), GFP_KERNEL);
  386. if (!lcd)
  387. return -ENOMEM;
  388. /* ams369fg06 lcd panel uses 3-wire 16bits SPI Mode. */
  389. spi->bits_per_word = 16;
  390. ret = spi_setup(spi);
  391. if (ret < 0) {
  392. dev_err(&spi->dev, "spi setup failed.\n");
  393. return ret;
  394. }
  395. lcd->spi = spi;
  396. lcd->dev = &spi->dev;
  397. lcd->lcd_pd = spi->dev.platform_data;
  398. if (!lcd->lcd_pd) {
  399. dev_err(&spi->dev, "platform data is NULL\n");
  400. return -EINVAL;
  401. }
  402. ld = lcd_device_register("ams369fg06", &spi->dev, lcd,
  403. &ams369fg06_lcd_ops);
  404. if (IS_ERR(ld))
  405. return PTR_ERR(ld);
  406. lcd->ld = ld;
  407. memset(&props, 0, sizeof(struct backlight_properties));
  408. props.type = BACKLIGHT_RAW;
  409. props.max_brightness = MAX_BRIGHTNESS;
  410. bd = backlight_device_register("ams369fg06-bl", &spi->dev, lcd,
  411. &ams369fg06_backlight_ops, &props);
  412. if (IS_ERR(bd)) {
  413. ret = PTR_ERR(bd);
  414. goto out_lcd_unregister;
  415. }
  416. bd->props.brightness = DEFAULT_BRIGHTNESS;
  417. lcd->bd = bd;
  418. if (!lcd->lcd_pd->lcd_enabled) {
  419. /*
  420. * if lcd panel was off from bootloader then
  421. * current lcd status is powerdown and then
  422. * it enables lcd panel.
  423. */
  424. lcd->power = FB_BLANK_POWERDOWN;
  425. ams369fg06_power(lcd, FB_BLANK_UNBLANK);
  426. } else {
  427. lcd->power = FB_BLANK_UNBLANK;
  428. }
  429. dev_set_drvdata(&spi->dev, lcd);
  430. dev_info(&spi->dev, "ams369fg06 panel driver has been probed.\n");
  431. return 0;
  432. out_lcd_unregister:
  433. lcd_device_unregister(ld);
  434. return ret;
  435. }
  436. static int ams369fg06_remove(struct spi_device *spi)
  437. {
  438. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  439. ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
  440. backlight_device_unregister(lcd->bd);
  441. lcd_device_unregister(lcd->ld);
  442. return 0;
  443. }
  444. #if defined(CONFIG_PM)
  445. static int ams369fg06_suspend(struct spi_device *spi, pm_message_t mesg)
  446. {
  447. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  448. dev_dbg(&spi->dev, "lcd->power = %d\n", lcd->power);
  449. /*
  450. * when lcd panel is suspend, lcd panel becomes off
  451. * regardless of status.
  452. */
  453. return ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
  454. }
  455. static int ams369fg06_resume(struct spi_device *spi)
  456. {
  457. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  458. lcd->power = FB_BLANK_POWERDOWN;
  459. return ams369fg06_power(lcd, FB_BLANK_UNBLANK);
  460. }
  461. #else
  462. #define ams369fg06_suspend NULL
  463. #define ams369fg06_resume NULL
  464. #endif
  465. static void ams369fg06_shutdown(struct spi_device *spi)
  466. {
  467. struct ams369fg06 *lcd = dev_get_drvdata(&spi->dev);
  468. ams369fg06_power(lcd, FB_BLANK_POWERDOWN);
  469. }
  470. static struct spi_driver ams369fg06_driver = {
  471. .driver = {
  472. .name = "ams369fg06",
  473. .owner = THIS_MODULE,
  474. },
  475. .probe = ams369fg06_probe,
  476. .remove = ams369fg06_remove,
  477. .shutdown = ams369fg06_shutdown,
  478. .suspend = ams369fg06_suspend,
  479. .resume = ams369fg06_resume,
  480. };
  481. module_spi_driver(ams369fg06_driver);
  482. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  483. MODULE_DESCRIPTION("ams369fg06 LCD Driver");
  484. MODULE_LICENSE("GPL");