lcdc_samsung_oled_pt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* Copyright (c) 2009-2010, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/pwm.h>
  14. #ifdef CONFIG_SPI_QUP
  15. #include <linux/spi/spi.h>
  16. #else
  17. #include <mach/gpio.h>
  18. #endif
  19. #include "msm_fb.h"
  20. #define DEBUG
  21. /* #define SYSFS_DEBUG_CMD */
  22. #ifdef CONFIG_SPI_QUP
  23. #define LCDC_SAMSUNG_SPI_DEVICE_NAME "lcdc_samsung_ams367pe02"
  24. static struct spi_device *lcdc_spi_client;
  25. #else
  26. static int spi_cs;
  27. static int spi_sclk;
  28. static int spi_mosi;
  29. #endif
  30. struct samsung_state_type {
  31. boolean disp_initialized;
  32. boolean display_on;
  33. boolean disp_powered_up;
  34. int brightness;
  35. };
  36. struct samsung_spi_data {
  37. u8 addr;
  38. u8 len;
  39. u8 data[22];
  40. };
  41. static struct samsung_spi_data panel_sequence[] = {
  42. { .addr = 0xf8, .len = 14, .data = { 0x01, 0x27, 0x27, 0x07, 0x07,
  43. 0x54, 0x9f, 0x63, 0x86, 0x1a, 0x33, 0x0d, 0x00, 0x00 } },
  44. };
  45. static struct samsung_spi_data display_sequence[] = {
  46. { .addr = 0xf2, .len = 5, .data = { 0x02, 0x03, 0x1c, 0x10, 0x10 } },
  47. { .addr = 0xf7, .len = 3, .data = { 0x00, 0x00, 0x30 } },
  48. };
  49. /* lum=300 cd/m2 */
  50. static struct samsung_spi_data gamma_sequence_300[] = {
  51. { .addr = 0xfa, .len = 22, .data = { 0x02, 0x18, 0x08, 0x24, 0x7d, 0x77,
  52. 0x5b, 0xbe, 0xc1, 0xb1, 0xb3, 0xb7, 0xa6, 0xc3, 0xc5, 0xb9, 0x00, 0xb3,
  53. 0x00, 0xaf, 0x00, 0xe8 } },
  54. { .addr = 0xFA, .len = 1, .data = { 0x03 } },
  55. };
  56. /* lum = 180 cd/m2*/
  57. static struct samsung_spi_data gamma_sequence_180[] = {
  58. { .addr = 0xfa, .len = 22, .data = { 0x02, 0x18, 0x08, 0x24, 0x83, 0x78,
  59. 0x60, 0xc5, 0xc6, 0xb8, 0xba, 0xbe, 0xad, 0xcb, 0xcd, 0xc2, 0x00, 0x92,
  60. 0x00, 0x8e, 0x00, 0xbc } },
  61. { .addr = 0xFA, .len = 1, .data = { 0x03 } },
  62. };
  63. /* lum = 80 cd/m2*/
  64. static struct samsung_spi_data gamma_sequence_80[] = {
  65. { .addr = 0xfa, .len = 22, .data = { 0x02, 0x18, 0x08, 0x24, 0x94, 0x73,
  66. 0x6c, 0xcb, 0xca, 0xbe, 0xc4, 0xc7, 0xb8, 0xd3, 0xd5, 0xcb, 0x00, 0x6d,
  67. 0x00, 0x69, 0x00, 0x8b } },
  68. { .addr = 0xFA, .len = 1, .data = { 0x03 } },
  69. };
  70. static struct samsung_spi_data etc_sequence[] = {
  71. { .addr = 0xF6, .len = 3, .data = { 0x00, 0x8e, 0x07 } },
  72. { .addr = 0xB3, .len = 1, .data = { 0x0C } },
  73. };
  74. static struct samsung_state_type samsung_state = { .brightness = 180 };
  75. static struct msm_panel_common_pdata *lcdc_samsung_pdata;
  76. #ifndef CONFIG_SPI_QUP
  77. static void samsung_spi_write_byte(boolean dc, u8 data)
  78. {
  79. uint32 bit;
  80. int bnum;
  81. gpio_set_value(spi_sclk, 0);
  82. gpio_set_value(spi_mosi, dc ? 1 : 0);
  83. udelay(1); /* at least 20 ns */
  84. gpio_set_value(spi_sclk, 1); /* clk high */
  85. udelay(1); /* at least 20 ns */
  86. bnum = 8; /* 8 data bits */
  87. bit = 0x80;
  88. while (bnum--) {
  89. gpio_set_value(spi_sclk, 0); /* clk low */
  90. gpio_set_value(spi_mosi, (data & bit) ? 1 : 0);
  91. udelay(1);
  92. gpio_set_value(spi_sclk, 1); /* clk high */
  93. udelay(1);
  94. bit >>= 1;
  95. }
  96. gpio_set_value(spi_mosi, 0);
  97. }
  98. static void samsung_spi_read_bytes(u8 cmd, u8 *data, int num)
  99. {
  100. int bnum;
  101. /* Chip Select - low */
  102. gpio_set_value(spi_cs, 0);
  103. udelay(2);
  104. /* command byte first */
  105. samsung_spi_write_byte(0, cmd);
  106. udelay(2);
  107. gpio_direction_input(spi_mosi);
  108. if (num > 1) {
  109. /* extra dummy clock */
  110. gpio_set_value(spi_sclk, 0);
  111. udelay(1);
  112. gpio_set_value(spi_sclk, 1);
  113. udelay(1);
  114. }
  115. /* followed by data bytes */
  116. bnum = num * 8; /* number of bits */
  117. *data = 0;
  118. while (bnum) {
  119. gpio_set_value(spi_sclk, 0); /* clk low */
  120. udelay(1);
  121. *data <<= 1;
  122. *data |= gpio_get_value(spi_mosi) ? 1 : 0;
  123. gpio_set_value(spi_sclk, 1); /* clk high */
  124. udelay(1);
  125. --bnum;
  126. if ((bnum % 8) == 0)
  127. ++data;
  128. }
  129. gpio_direction_output(spi_mosi, 0);
  130. /* Chip Select - high */
  131. udelay(2);
  132. gpio_set_value(spi_cs, 1);
  133. }
  134. #endif
  135. #ifdef DEBUG
  136. static const char *byte_to_binary(const u8 *buf, int len)
  137. {
  138. static char b[32*8+1];
  139. char *p = b;
  140. int i, z;
  141. for (i = 0; i < len; ++i) {
  142. u8 val = *buf++;
  143. for (z = 1 << 7; z > 0; z >>= 1)
  144. *p++ = (val & z) ? '1' : '0';
  145. }
  146. *p = 0;
  147. return b;
  148. }
  149. #endif
  150. #define BIT_OFFSET (bit_size % 8)
  151. #define ADD_BIT(val) do { \
  152. tx_buf[bit_size / 8] |= \
  153. (u8)((val ? 1 : 0) << (7 - BIT_OFFSET)); \
  154. ++bit_size; \
  155. } while (0)
  156. #define ADD_BYTE(data) do { \
  157. tx_buf[bit_size / 8] |= (u8)(data >> BIT_OFFSET); \
  158. bit_size += 8; \
  159. if (BIT_OFFSET != 0) \
  160. tx_buf[bit_size / 8] |= (u8)(data << (8 - BIT_OFFSET));\
  161. } while (0)
  162. static int samsung_serigo(struct samsung_spi_data data)
  163. {
  164. #ifdef CONFIG_SPI_QUP
  165. char tx_buf[32];
  166. int bit_size = 0, i, rc;
  167. struct spi_message m;
  168. struct spi_transfer t;
  169. if (!lcdc_spi_client) {
  170. pr_err("%s lcdc_spi_client is NULL\n", __func__);
  171. return -EINVAL;
  172. }
  173. memset(&t, 0, sizeof t);
  174. memset(tx_buf, 0, sizeof tx_buf);
  175. t.tx_buf = tx_buf;
  176. spi_setup(lcdc_spi_client);
  177. spi_message_init(&m);
  178. spi_message_add_tail(&t, &m);
  179. ADD_BIT(FALSE);
  180. ADD_BYTE(data.addr);
  181. for (i = 0; i < data.len; ++i) {
  182. ADD_BIT(TRUE);
  183. ADD_BYTE(data.data[i]);
  184. }
  185. /* add padding bits so we round to next byte */
  186. t.len = (bit_size+7) / 8;
  187. if (t.len <= 4)
  188. t.bits_per_word = bit_size;
  189. rc = spi_sync(lcdc_spi_client, &m);
  190. #ifdef DEBUG
  191. pr_info("%s: addr=0x%02x, #args=%d[%d] [%s], rc=%d\n",
  192. __func__, data.addr, t.len, t.bits_per_word,
  193. byte_to_binary(tx_buf, t.len), rc);
  194. #endif
  195. return rc;
  196. #else
  197. int i;
  198. /* Chip Select - low */
  199. gpio_set_value(spi_cs, 0);
  200. udelay(2);
  201. samsung_spi_write_byte(FALSE, data.addr);
  202. udelay(2);
  203. for (i = 0; i < data.len; ++i) {
  204. samsung_spi_write_byte(TRUE, data.data[i]);
  205. udelay(2);
  206. }
  207. /* Chip Select - high */
  208. gpio_set_value(spi_cs, 1);
  209. #ifdef DEBUG
  210. pr_info("%s: cmd=0x%02x, #args=%d\n", __func__, data.addr, data.len);
  211. #endif
  212. return 0;
  213. #endif
  214. }
  215. static int samsung_write_cmd(u8 cmd)
  216. {
  217. #ifdef CONFIG_SPI_QUP
  218. char tx_buf[2];
  219. int bit_size = 0, rc;
  220. struct spi_message m;
  221. struct spi_transfer t;
  222. if (!lcdc_spi_client) {
  223. pr_err("%s lcdc_spi_client is NULL\n", __func__);
  224. return -EINVAL;
  225. }
  226. memset(&t, 0, sizeof t);
  227. memset(tx_buf, 0, sizeof tx_buf);
  228. t.tx_buf = tx_buf;
  229. spi_setup(lcdc_spi_client);
  230. spi_message_init(&m);
  231. spi_message_add_tail(&t, &m);
  232. ADD_BIT(FALSE);
  233. ADD_BYTE(cmd);
  234. t.len = 2;
  235. t.bits_per_word = 9;
  236. rc = spi_sync(lcdc_spi_client, &m);
  237. #ifdef DEBUG
  238. pr_info("%s: addr=0x%02x, #args=%d[%d] [%s], rc=%d\n",
  239. __func__, cmd, t.len, t.bits_per_word,
  240. byte_to_binary(tx_buf, t.len), rc);
  241. #endif
  242. return rc;
  243. #else
  244. /* Chip Select - low */
  245. gpio_set_value(spi_cs, 0);
  246. udelay(2);
  247. samsung_spi_write_byte(FALSE, cmd);
  248. /* Chip Select - high */
  249. udelay(2);
  250. gpio_set_value(spi_cs, 1);
  251. #ifdef DEBUG
  252. pr_info("%s: cmd=0x%02x\n", __func__, cmd);
  253. #endif
  254. return 0;
  255. #endif
  256. }
  257. static int samsung_serigo_list(struct samsung_spi_data *data, int count)
  258. {
  259. int i, rc;
  260. for (i = 0; i < count; ++i, ++data) {
  261. rc = samsung_serigo(*data);
  262. if (rc)
  263. return rc;
  264. msleep(10);
  265. }
  266. return 0;
  267. }
  268. #ifndef CONFIG_SPI_QUP
  269. static void samsung_spi_init(void)
  270. {
  271. spi_sclk = *(lcdc_samsung_pdata->gpio_num);
  272. spi_cs = *(lcdc_samsung_pdata->gpio_num + 1);
  273. spi_mosi = *(lcdc_samsung_pdata->gpio_num + 2);
  274. /* Set the output so that we don't disturb the slave device */
  275. gpio_set_value(spi_sclk, 1);
  276. gpio_set_value(spi_mosi, 0);
  277. /* Set the Chip Select deasserted (active low) */
  278. gpio_set_value(spi_cs, 1);
  279. }
  280. #endif
  281. static void samsung_disp_powerup(void)
  282. {
  283. if (!samsung_state.disp_powered_up && !samsung_state.display_on)
  284. samsung_state.disp_powered_up = TRUE;
  285. }
  286. static struct work_struct disp_on_delayed_work;
  287. static void samsung_disp_on_delayed_work(struct work_struct *work_ptr)
  288. {
  289. /* 0x01: Software Reset */
  290. samsung_write_cmd(0x01);
  291. msleep(120);
  292. msleep(300);
  293. samsung_serigo_list(panel_sequence,
  294. sizeof(panel_sequence)/sizeof(*panel_sequence));
  295. samsung_serigo_list(display_sequence,
  296. sizeof(display_sequence)/sizeof(*display_sequence));
  297. switch (samsung_state.brightness) {
  298. case 300:
  299. samsung_serigo_list(gamma_sequence_300,
  300. sizeof(gamma_sequence_300)/sizeof(*gamma_sequence_300));
  301. break;
  302. case 180:
  303. default:
  304. samsung_serigo_list(gamma_sequence_180,
  305. sizeof(gamma_sequence_180)/sizeof(*gamma_sequence_180));
  306. break;
  307. case 80:
  308. samsung_serigo_list(gamma_sequence_80,
  309. sizeof(gamma_sequence_80)/sizeof(*gamma_sequence_80));
  310. break;
  311. }
  312. samsung_serigo_list(etc_sequence,
  313. sizeof(etc_sequence)/sizeof(*etc_sequence));
  314. /* 0x11: Sleep Out */
  315. samsung_write_cmd(0x11);
  316. msleep(120);
  317. /* 0x13: Normal Mode On */
  318. samsung_write_cmd(0x13);
  319. #ifndef CONFIG_SPI_QUP
  320. {
  321. u8 data;
  322. msleep(120);
  323. /* 0x0A: Read Display Power Mode */
  324. samsung_spi_read_bytes(0x0A, &data, 1);
  325. pr_info("%s: power=[%s]\n", __func__,
  326. byte_to_binary(&data, 1));
  327. msleep(120);
  328. /* 0x0C: Read Display Pixel Format */
  329. samsung_spi_read_bytes(0x0C, &data, 1);
  330. pr_info("%s: pixel-format=[%s]\n", __func__,
  331. byte_to_binary(&data, 1));
  332. }
  333. #endif
  334. msleep(120);
  335. /* 0x29: Display On */
  336. samsung_write_cmd(0x29);
  337. }
  338. static void samsung_disp_on(void)
  339. {
  340. if (samsung_state.disp_powered_up && !samsung_state.display_on) {
  341. INIT_WORK(&disp_on_delayed_work, samsung_disp_on_delayed_work);
  342. schedule_work(&disp_on_delayed_work);
  343. samsung_state.display_on = TRUE;
  344. }
  345. }
  346. static int lcdc_samsung_panel_on(struct platform_device *pdev)
  347. {
  348. pr_info("%s\n", __func__);
  349. if (!samsung_state.disp_initialized) {
  350. #ifndef CONFIG_SPI_QUP
  351. lcdc_samsung_pdata->panel_config_gpio(1);
  352. samsung_spi_init();
  353. #endif
  354. samsung_disp_powerup();
  355. samsung_disp_on();
  356. samsung_state.disp_initialized = TRUE;
  357. }
  358. return 0;
  359. }
  360. static int lcdc_samsung_panel_off(struct platform_device *pdev)
  361. {
  362. pr_info("%s\n", __func__);
  363. if (samsung_state.disp_powered_up && samsung_state.display_on) {
  364. /* 0x10: Sleep In */
  365. samsung_write_cmd(0x10);
  366. msleep(120);
  367. samsung_state.display_on = FALSE;
  368. samsung_state.disp_initialized = FALSE;
  369. }
  370. return 0;
  371. }
  372. #ifdef SYSFS_DEBUG_CMD
  373. static ssize_t samsung_rda_cmd(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. ssize_t ret = snprintf(buf, PAGE_SIZE, "n/a\n");
  377. pr_info("%s: 'n/a'\n", __func__);
  378. return ret;
  379. }
  380. static ssize_t samsung_wta_cmd(struct device *dev,
  381. struct device_attribute *attr, const char *buf, size_t count)
  382. {
  383. ssize_t ret = strnlen(buf, PAGE_SIZE);
  384. uint32 cmd;
  385. sscanf(buf, "%x", &cmd);
  386. samsung_write_cmd((u8)cmd);
  387. return ret;
  388. }
  389. static DEVICE_ATTR(cmd, S_IRUGO | S_IWUGO, samsung_rda_cmd, samsung_wta_cmd);
  390. static struct attribute *fs_attrs[] = {
  391. &dev_attr_cmd.attr,
  392. NULL,
  393. };
  394. static struct attribute_group fs_attr_group = {
  395. .attrs = fs_attrs,
  396. };
  397. #endif
  398. static struct msm_fb_panel_data samsung_panel_data = {
  399. .on = lcdc_samsung_panel_on,
  400. .off = lcdc_samsung_panel_off,
  401. };
  402. static int __devinit samsung_probe(struct platform_device *pdev)
  403. {
  404. struct msm_panel_info *pinfo;
  405. #ifdef SYSFS_DEBUG_CMD
  406. struct platform_device *fb_dev;
  407. struct msm_fb_data_type *mfd;
  408. int rc;
  409. #endif
  410. pr_info("%s: id=%d\n", __func__, pdev->id);
  411. lcdc_samsung_pdata = pdev->dev.platform_data;
  412. pinfo = &samsung_panel_data.panel_info;
  413. pinfo->xres = 480;
  414. pinfo->yres = 800;
  415. pinfo->type = LCDC_PANEL;
  416. pinfo->pdest = DISPLAY_1;
  417. pinfo->wait_cycle = 0;
  418. pinfo->bpp = 24;
  419. pinfo->fb_num = 2;
  420. pinfo->clk_rate = 25600000; /* Max 27.77MHz */
  421. pinfo->bl_max = 15;
  422. pinfo->bl_min = 1;
  423. /* AMS367PE02 Operation Manual, Page 7 */
  424. pinfo->lcdc.h_back_porch = 16-2; /* HBP-HLW */
  425. pinfo->lcdc.h_front_porch = 16;
  426. pinfo->lcdc.h_pulse_width = 2;
  427. /* AMS367PE02 Operation Manual, Page 6 */
  428. pinfo->lcdc.v_back_porch = 3-2; /* VBP-VLW */
  429. pinfo->lcdc.v_front_porch = 28;
  430. pinfo->lcdc.v_pulse_width = 2;
  431. pinfo->lcdc.border_clr = 0;
  432. pinfo->lcdc.underflow_clr = 0xff;
  433. pinfo->lcdc.hsync_skew = 0;
  434. pdev->dev.platform_data = &samsung_panel_data;
  435. #ifndef SYSFS_DEBUG_CMD
  436. msm_fb_add_device(pdev);
  437. #else
  438. fb_dev = msm_fb_add_device(pdev);
  439. mfd = platform_get_drvdata(fb_dev);
  440. rc = sysfs_create_group(&mfd->fbi->dev->kobj, &fs_attr_group);
  441. if (rc) {
  442. pr_err("%s: sysfs group creation failed, rc=%d\n", __func__,
  443. rc);
  444. return rc;
  445. }
  446. #endif
  447. return 0;
  448. }
  449. #ifdef CONFIG_SPI_QUP
  450. static int __devinit lcdc_samsung_spi_probe(struct spi_device *spi)
  451. {
  452. pr_info("%s\n", __func__);
  453. lcdc_spi_client = spi;
  454. lcdc_spi_client->bits_per_word = 32;
  455. return 0;
  456. }
  457. static int __devexit lcdc_samsung_spi_remove(struct spi_device *spi)
  458. {
  459. lcdc_spi_client = NULL;
  460. return 0;
  461. }
  462. static struct spi_driver lcdc_samsung_spi_driver = {
  463. .driver.name = LCDC_SAMSUNG_SPI_DEVICE_NAME,
  464. .driver.owner = THIS_MODULE,
  465. .probe = lcdc_samsung_spi_probe,
  466. .remove = __devexit_p(lcdc_samsung_spi_remove),
  467. };
  468. #endif
  469. static struct platform_driver this_driver = {
  470. .probe = samsung_probe,
  471. .driver.name = "lcdc_samsung_oled",
  472. };
  473. static int __init lcdc_samsung_panel_init(void)
  474. {
  475. int ret;
  476. if (msm_fb_detect_client("lcdc_samsung_oled")) {
  477. pr_err("%s: detect failed\n", __func__);
  478. return 0;
  479. }
  480. ret = platform_driver_register(&this_driver);
  481. if (ret) {
  482. pr_err("%s: driver register failed, rc=%d\n", __func__, ret);
  483. return ret;
  484. }
  485. #ifdef CONFIG_SPI_QUP
  486. ret = spi_register_driver(&lcdc_samsung_spi_driver);
  487. if (ret) {
  488. pr_err("%s: spi register failed: rc=%d\n", __func__, ret);
  489. platform_driver_unregister(&this_driver);
  490. } else
  491. pr_info("%s: SUCCESS (SPI)\n", __func__);
  492. #else
  493. pr_info("%s: SUCCESS (BitBang)\n", __func__);
  494. #endif
  495. return ret;
  496. }
  497. module_init(lcdc_samsung_panel_init);
  498. static void __exit lcdc_samsung_panel_exit(void)
  499. {
  500. pr_info("%s\n", __func__);
  501. #ifdef CONFIG_SPI_QUP
  502. spi_unregister_driver(&lcdc_samsung_spi_driver);
  503. #endif
  504. platform_driver_unregister(&this_driver);
  505. }
  506. module_exit(lcdc_samsung_panel_exit);