hx8357.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Driver for the Himax HX-8357 LCD Controller
  3. *
  4. * Copyright 2012 Free Electrons
  5. *
  6. * Licensed under the GPLv2 or later.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/lcd.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/spi/spi.h>
  15. #define HX8357_NUM_IM_PINS 3
  16. #define HX8357_SWRESET 0x01
  17. #define HX8357_GET_RED_CHANNEL 0x06
  18. #define HX8357_GET_GREEN_CHANNEL 0x07
  19. #define HX8357_GET_BLUE_CHANNEL 0x08
  20. #define HX8357_GET_POWER_MODE 0x0a
  21. #define HX8357_GET_MADCTL 0x0b
  22. #define HX8357_GET_PIXEL_FORMAT 0x0c
  23. #define HX8357_GET_DISPLAY_MODE 0x0d
  24. #define HX8357_GET_SIGNAL_MODE 0x0e
  25. #define HX8357_GET_DIAGNOSTIC_RESULT 0x0f
  26. #define HX8357_ENTER_SLEEP_MODE 0x10
  27. #define HX8357_EXIT_SLEEP_MODE 0x11
  28. #define HX8357_ENTER_PARTIAL_MODE 0x12
  29. #define HX8357_ENTER_NORMAL_MODE 0x13
  30. #define HX8357_EXIT_INVERSION_MODE 0x20
  31. #define HX8357_ENTER_INVERSION_MODE 0x21
  32. #define HX8357_SET_DISPLAY_OFF 0x28
  33. #define HX8357_SET_DISPLAY_ON 0x29
  34. #define HX8357_SET_COLUMN_ADDRESS 0x2a
  35. #define HX8357_SET_PAGE_ADDRESS 0x2b
  36. #define HX8357_WRITE_MEMORY_START 0x2c
  37. #define HX8357_READ_MEMORY_START 0x2e
  38. #define HX8357_SET_PARTIAL_AREA 0x30
  39. #define HX8357_SET_SCROLL_AREA 0x33
  40. #define HX8357_SET_TEAR_OFF 0x34
  41. #define HX8357_SET_TEAR_ON 0x35
  42. #define HX8357_SET_ADDRESS_MODE 0x36
  43. #define HX8357_SET_SCROLL_START 0x37
  44. #define HX8357_EXIT_IDLE_MODE 0x38
  45. #define HX8357_ENTER_IDLE_MODE 0x39
  46. #define HX8357_SET_PIXEL_FORMAT 0x3a
  47. #define HX8357_SET_PIXEL_FORMAT_DBI_3BIT (0x1)
  48. #define HX8357_SET_PIXEL_FORMAT_DBI_16BIT (0x5)
  49. #define HX8357_SET_PIXEL_FORMAT_DBI_18BIT (0x6)
  50. #define HX8357_SET_PIXEL_FORMAT_DPI_3BIT (0x1 << 4)
  51. #define HX8357_SET_PIXEL_FORMAT_DPI_16BIT (0x5 << 4)
  52. #define HX8357_SET_PIXEL_FORMAT_DPI_18BIT (0x6 << 4)
  53. #define HX8357_WRITE_MEMORY_CONTINUE 0x3c
  54. #define HX8357_READ_MEMORY_CONTINUE 0x3e
  55. #define HX8357_SET_TEAR_SCAN_LINES 0x44
  56. #define HX8357_GET_SCAN_LINES 0x45
  57. #define HX8357_READ_DDB_START 0xa1
  58. #define HX8357_SET_DISPLAY_MODE 0xb4
  59. #define HX8357_SET_DISPLAY_MODE_RGB_THROUGH (0x3)
  60. #define HX8357_SET_DISPLAY_MODE_RGB_INTERFACE (1 << 4)
  61. #define HX8357_SET_PANEL_DRIVING 0xc0
  62. #define HX8357_SET_DISPLAY_FRAME 0xc5
  63. #define HX8357_SET_RGB 0xc6
  64. #define HX8357_SET_RGB_ENABLE_HIGH (1 << 1)
  65. #define HX8357_SET_GAMMA 0xc8
  66. #define HX8357_SET_POWER 0xd0
  67. #define HX8357_SET_VCOM 0xd1
  68. #define HX8357_SET_POWER_NORMAL 0xd2
  69. #define HX8357_SET_PANEL_RELATED 0xe9
  70. #define HX8369_SET_DISPLAY_BRIGHTNESS 0x51
  71. #define HX8369_WRITE_CABC_DISPLAY_VALUE 0x53
  72. #define HX8369_WRITE_CABC_BRIGHT_CTRL 0x55
  73. #define HX8369_WRITE_CABC_MIN_BRIGHTNESS 0x5e
  74. #define HX8369_SET_POWER 0xb1
  75. #define HX8369_SET_DISPLAY_MODE 0xb2
  76. #define HX8369_SET_DISPLAY_WAVEFORM_CYC 0xb4
  77. #define HX8369_SET_VCOM 0xb6
  78. #define HX8369_SET_EXTENSION_COMMAND 0xb9
  79. #define HX8369_SET_GIP 0xd5
  80. #define HX8369_SET_GAMMA_CURVE_RELATED 0xe0
  81. struct hx8357_data {
  82. unsigned im_pins[HX8357_NUM_IM_PINS];
  83. unsigned reset;
  84. struct spi_device *spi;
  85. int state;
  86. bool use_im_pins;
  87. };
  88. static u8 hx8357_seq_power[] = {
  89. HX8357_SET_POWER, 0x44, 0x41, 0x06,
  90. };
  91. static u8 hx8357_seq_vcom[] = {
  92. HX8357_SET_VCOM, 0x40, 0x10,
  93. };
  94. static u8 hx8357_seq_power_normal[] = {
  95. HX8357_SET_POWER_NORMAL, 0x05, 0x12,
  96. };
  97. static u8 hx8357_seq_panel_driving[] = {
  98. HX8357_SET_PANEL_DRIVING, 0x14, 0x3b, 0x00, 0x02, 0x11,
  99. };
  100. static u8 hx8357_seq_display_frame[] = {
  101. HX8357_SET_DISPLAY_FRAME, 0x0c,
  102. };
  103. static u8 hx8357_seq_panel_related[] = {
  104. HX8357_SET_PANEL_RELATED, 0x01,
  105. };
  106. static u8 hx8357_seq_undefined1[] = {
  107. 0xea, 0x03, 0x00, 0x00,
  108. };
  109. static u8 hx8357_seq_undefined2[] = {
  110. 0xeb, 0x40, 0x54, 0x26, 0xdb,
  111. };
  112. static u8 hx8357_seq_gamma[] = {
  113. HX8357_SET_GAMMA, 0x00, 0x15, 0x00, 0x22, 0x00,
  114. 0x08, 0x77, 0x26, 0x77, 0x22, 0x04, 0x00,
  115. };
  116. static u8 hx8357_seq_address_mode[] = {
  117. HX8357_SET_ADDRESS_MODE, 0xc0,
  118. };
  119. static u8 hx8357_seq_pixel_format[] = {
  120. HX8357_SET_PIXEL_FORMAT,
  121. HX8357_SET_PIXEL_FORMAT_DPI_18BIT |
  122. HX8357_SET_PIXEL_FORMAT_DBI_18BIT,
  123. };
  124. static u8 hx8357_seq_column_address[] = {
  125. HX8357_SET_COLUMN_ADDRESS, 0x00, 0x00, 0x01, 0x3f,
  126. };
  127. static u8 hx8357_seq_page_address[] = {
  128. HX8357_SET_PAGE_ADDRESS, 0x00, 0x00, 0x01, 0xdf,
  129. };
  130. static u8 hx8357_seq_rgb[] = {
  131. HX8357_SET_RGB, 0x02,
  132. };
  133. static u8 hx8357_seq_display_mode[] = {
  134. HX8357_SET_DISPLAY_MODE,
  135. HX8357_SET_DISPLAY_MODE_RGB_THROUGH |
  136. HX8357_SET_DISPLAY_MODE_RGB_INTERFACE,
  137. };
  138. static u8 hx8369_seq_write_CABC_min_brightness[] = {
  139. HX8369_WRITE_CABC_MIN_BRIGHTNESS, 0x00,
  140. };
  141. static u8 hx8369_seq_write_CABC_control[] = {
  142. HX8369_WRITE_CABC_DISPLAY_VALUE, 0x24,
  143. };
  144. static u8 hx8369_seq_set_display_brightness[] = {
  145. HX8369_SET_DISPLAY_BRIGHTNESS, 0xFF,
  146. };
  147. static u8 hx8369_seq_write_CABC_control_setting[] = {
  148. HX8369_WRITE_CABC_BRIGHT_CTRL, 0x02,
  149. };
  150. static u8 hx8369_seq_extension_command[] = {
  151. HX8369_SET_EXTENSION_COMMAND, 0xff, 0x83, 0x69,
  152. };
  153. static u8 hx8369_seq_display_related[] = {
  154. HX8369_SET_DISPLAY_MODE, 0x00, 0x2b, 0x03, 0x03, 0x70, 0x00,
  155. 0xff, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x01,
  156. };
  157. static u8 hx8369_seq_panel_waveform_cycle[] = {
  158. HX8369_SET_DISPLAY_WAVEFORM_CYC, 0x0a, 0x1d, 0x80, 0x06, 0x02,
  159. };
  160. static u8 hx8369_seq_set_address_mode[] = {
  161. HX8357_SET_ADDRESS_MODE, 0x00,
  162. };
  163. static u8 hx8369_seq_vcom[] = {
  164. HX8369_SET_VCOM, 0x3e, 0x3e,
  165. };
  166. static u8 hx8369_seq_gip[] = {
  167. HX8369_SET_GIP, 0x00, 0x01, 0x03, 0x25, 0x01, 0x02, 0x28, 0x70,
  168. 0x11, 0x13, 0x00, 0x00, 0x40, 0x26, 0x51, 0x37, 0x00, 0x00, 0x71,
  169. 0x35, 0x60, 0x24, 0x07, 0x0f, 0x04, 0x04,
  170. };
  171. static u8 hx8369_seq_power[] = {
  172. HX8369_SET_POWER, 0x01, 0x00, 0x34, 0x03, 0x00, 0x11, 0x11, 0x32,
  173. 0x2f, 0x3f, 0x3f, 0x01, 0x3a, 0x01, 0xe6, 0xe6, 0xe6, 0xe6, 0xe6,
  174. };
  175. static u8 hx8369_seq_gamma_curve_related[] = {
  176. HX8369_SET_GAMMA_CURVE_RELATED, 0x00, 0x0d, 0x19, 0x2f, 0x3b, 0x3d,
  177. 0x2e, 0x4a, 0x08, 0x0e, 0x0f, 0x14, 0x16, 0x14, 0x14, 0x14, 0x1e,
  178. 0x00, 0x0d, 0x19, 0x2f, 0x3b, 0x3d, 0x2e, 0x4a, 0x08, 0x0e, 0x0f,
  179. 0x14, 0x16, 0x14, 0x14, 0x14, 0x1e,
  180. };
  181. static int hx8357_spi_write_then_read(struct lcd_device *lcdev,
  182. u8 *txbuf, u16 txlen,
  183. u8 *rxbuf, u16 rxlen)
  184. {
  185. struct hx8357_data *lcd = lcd_get_data(lcdev);
  186. struct spi_message msg;
  187. struct spi_transfer xfer[2];
  188. u16 *local_txbuf = NULL;
  189. int ret = 0;
  190. memset(xfer, 0, sizeof(xfer));
  191. spi_message_init(&msg);
  192. if (txlen) {
  193. int i;
  194. local_txbuf = kcalloc(txlen, sizeof(*local_txbuf), GFP_KERNEL);
  195. if (!local_txbuf)
  196. return -ENOMEM;
  197. for (i = 0; i < txlen; i++) {
  198. local_txbuf[i] = txbuf[i];
  199. if (i > 0)
  200. local_txbuf[i] |= 1 << 8;
  201. }
  202. xfer[0].len = 2 * txlen;
  203. xfer[0].bits_per_word = 9;
  204. xfer[0].tx_buf = local_txbuf;
  205. spi_message_add_tail(&xfer[0], &msg);
  206. }
  207. if (rxlen) {
  208. xfer[1].len = rxlen;
  209. xfer[1].bits_per_word = 8;
  210. xfer[1].rx_buf = rxbuf;
  211. spi_message_add_tail(&xfer[1], &msg);
  212. }
  213. ret = spi_sync(lcd->spi, &msg);
  214. if (ret < 0)
  215. dev_err(&lcdev->dev, "Couldn't send SPI data\n");
  216. if (txlen)
  217. kfree(local_txbuf);
  218. return ret;
  219. }
  220. static inline int hx8357_spi_write_array(struct lcd_device *lcdev,
  221. u8 *value, u8 len)
  222. {
  223. return hx8357_spi_write_then_read(lcdev, value, len, NULL, 0);
  224. }
  225. static inline int hx8357_spi_write_byte(struct lcd_device *lcdev,
  226. u8 value)
  227. {
  228. return hx8357_spi_write_then_read(lcdev, &value, 1, NULL, 0);
  229. }
  230. static int hx8357_enter_standby(struct lcd_device *lcdev)
  231. {
  232. int ret;
  233. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_OFF);
  234. if (ret < 0)
  235. return ret;
  236. usleep_range(10000, 12000);
  237. ret = hx8357_spi_write_byte(lcdev, HX8357_ENTER_SLEEP_MODE);
  238. if (ret < 0)
  239. return ret;
  240. /*
  241. * The controller needs 120ms when entering in sleep mode before we can
  242. * send the command to go off sleep mode
  243. */
  244. msleep(120);
  245. return 0;
  246. }
  247. static int hx8357_exit_standby(struct lcd_device *lcdev)
  248. {
  249. int ret;
  250. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  251. if (ret < 0)
  252. return ret;
  253. /*
  254. * The controller needs 120ms when exiting from sleep mode before we
  255. * can send the command to enter in sleep mode
  256. */
  257. msleep(120);
  258. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
  259. if (ret < 0)
  260. return ret;
  261. return 0;
  262. }
  263. static void hx8357_lcd_reset(struct lcd_device *lcdev)
  264. {
  265. struct hx8357_data *lcd = lcd_get_data(lcdev);
  266. /* Reset the screen */
  267. gpio_set_value(lcd->reset, 1);
  268. usleep_range(10000, 12000);
  269. gpio_set_value(lcd->reset, 0);
  270. usleep_range(10000, 12000);
  271. gpio_set_value(lcd->reset, 1);
  272. /* The controller needs 120ms to recover from reset */
  273. msleep(120);
  274. }
  275. static int hx8357_lcd_init(struct lcd_device *lcdev)
  276. {
  277. struct hx8357_data *lcd = lcd_get_data(lcdev);
  278. int ret;
  279. /*
  280. * Set the interface selection pins to SPI mode, with three
  281. * wires
  282. */
  283. if (lcd->use_im_pins) {
  284. gpio_set_value_cansleep(lcd->im_pins[0], 1);
  285. gpio_set_value_cansleep(lcd->im_pins[1], 0);
  286. gpio_set_value_cansleep(lcd->im_pins[2], 1);
  287. }
  288. ret = hx8357_spi_write_array(lcdev, hx8357_seq_power,
  289. ARRAY_SIZE(hx8357_seq_power));
  290. if (ret < 0)
  291. return ret;
  292. ret = hx8357_spi_write_array(lcdev, hx8357_seq_vcom,
  293. ARRAY_SIZE(hx8357_seq_vcom));
  294. if (ret < 0)
  295. return ret;
  296. ret = hx8357_spi_write_array(lcdev, hx8357_seq_power_normal,
  297. ARRAY_SIZE(hx8357_seq_power_normal));
  298. if (ret < 0)
  299. return ret;
  300. ret = hx8357_spi_write_array(lcdev, hx8357_seq_panel_driving,
  301. ARRAY_SIZE(hx8357_seq_panel_driving));
  302. if (ret < 0)
  303. return ret;
  304. ret = hx8357_spi_write_array(lcdev, hx8357_seq_display_frame,
  305. ARRAY_SIZE(hx8357_seq_display_frame));
  306. if (ret < 0)
  307. return ret;
  308. ret = hx8357_spi_write_array(lcdev, hx8357_seq_panel_related,
  309. ARRAY_SIZE(hx8357_seq_panel_related));
  310. if (ret < 0)
  311. return ret;
  312. ret = hx8357_spi_write_array(lcdev, hx8357_seq_undefined1,
  313. ARRAY_SIZE(hx8357_seq_undefined1));
  314. if (ret < 0)
  315. return ret;
  316. ret = hx8357_spi_write_array(lcdev, hx8357_seq_undefined2,
  317. ARRAY_SIZE(hx8357_seq_undefined2));
  318. if (ret < 0)
  319. return ret;
  320. ret = hx8357_spi_write_array(lcdev, hx8357_seq_gamma,
  321. ARRAY_SIZE(hx8357_seq_gamma));
  322. if (ret < 0)
  323. return ret;
  324. ret = hx8357_spi_write_array(lcdev, hx8357_seq_address_mode,
  325. ARRAY_SIZE(hx8357_seq_address_mode));
  326. if (ret < 0)
  327. return ret;
  328. ret = hx8357_spi_write_array(lcdev, hx8357_seq_pixel_format,
  329. ARRAY_SIZE(hx8357_seq_pixel_format));
  330. if (ret < 0)
  331. return ret;
  332. ret = hx8357_spi_write_array(lcdev, hx8357_seq_column_address,
  333. ARRAY_SIZE(hx8357_seq_column_address));
  334. if (ret < 0)
  335. return ret;
  336. ret = hx8357_spi_write_array(lcdev, hx8357_seq_page_address,
  337. ARRAY_SIZE(hx8357_seq_page_address));
  338. if (ret < 0)
  339. return ret;
  340. ret = hx8357_spi_write_array(lcdev, hx8357_seq_rgb,
  341. ARRAY_SIZE(hx8357_seq_rgb));
  342. if (ret < 0)
  343. return ret;
  344. ret = hx8357_spi_write_array(lcdev, hx8357_seq_display_mode,
  345. ARRAY_SIZE(hx8357_seq_display_mode));
  346. if (ret < 0)
  347. return ret;
  348. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  349. if (ret < 0)
  350. return ret;
  351. /*
  352. * The controller needs 120ms to fully recover from exiting sleep mode
  353. */
  354. msleep(120);
  355. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
  356. if (ret < 0)
  357. return ret;
  358. usleep_range(5000, 7000);
  359. ret = hx8357_spi_write_byte(lcdev, HX8357_WRITE_MEMORY_START);
  360. if (ret < 0)
  361. return ret;
  362. return 0;
  363. }
  364. static int hx8369_lcd_init(struct lcd_device *lcdev)
  365. {
  366. int ret;
  367. ret = hx8357_spi_write_array(lcdev, hx8369_seq_extension_command,
  368. ARRAY_SIZE(hx8369_seq_extension_command));
  369. if (ret < 0)
  370. return ret;
  371. usleep_range(10000, 12000);
  372. ret = hx8357_spi_write_array(lcdev, hx8369_seq_display_related,
  373. ARRAY_SIZE(hx8369_seq_display_related));
  374. if (ret < 0)
  375. return ret;
  376. ret = hx8357_spi_write_array(lcdev, hx8369_seq_panel_waveform_cycle,
  377. ARRAY_SIZE(hx8369_seq_panel_waveform_cycle));
  378. if (ret < 0)
  379. return ret;
  380. ret = hx8357_spi_write_array(lcdev, hx8369_seq_set_address_mode,
  381. ARRAY_SIZE(hx8369_seq_set_address_mode));
  382. if (ret < 0)
  383. return ret;
  384. ret = hx8357_spi_write_array(lcdev, hx8369_seq_vcom,
  385. ARRAY_SIZE(hx8369_seq_vcom));
  386. if (ret < 0)
  387. return ret;
  388. ret = hx8357_spi_write_array(lcdev, hx8369_seq_gip,
  389. ARRAY_SIZE(hx8369_seq_gip));
  390. if (ret < 0)
  391. return ret;
  392. ret = hx8357_spi_write_array(lcdev, hx8369_seq_power,
  393. ARRAY_SIZE(hx8369_seq_power));
  394. if (ret < 0)
  395. return ret;
  396. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  397. if (ret < 0)
  398. return ret;
  399. /*
  400. * The controller needs 120ms to fully recover from exiting sleep mode
  401. */
  402. msleep(120);
  403. ret = hx8357_spi_write_array(lcdev, hx8369_seq_gamma_curve_related,
  404. ARRAY_SIZE(hx8369_seq_gamma_curve_related));
  405. if (ret < 0)
  406. return ret;
  407. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  408. if (ret < 0)
  409. return ret;
  410. usleep_range(1000, 1200);
  411. ret = hx8357_spi_write_array(lcdev, hx8369_seq_write_CABC_control,
  412. ARRAY_SIZE(hx8369_seq_write_CABC_control));
  413. if (ret < 0)
  414. return ret;
  415. usleep_range(10000, 12000);
  416. ret = hx8357_spi_write_array(lcdev,
  417. hx8369_seq_write_CABC_control_setting,
  418. ARRAY_SIZE(hx8369_seq_write_CABC_control_setting));
  419. if (ret < 0)
  420. return ret;
  421. ret = hx8357_spi_write_array(lcdev,
  422. hx8369_seq_write_CABC_min_brightness,
  423. ARRAY_SIZE(hx8369_seq_write_CABC_min_brightness));
  424. if (ret < 0)
  425. return ret;
  426. usleep_range(10000, 12000);
  427. ret = hx8357_spi_write_array(lcdev, hx8369_seq_set_display_brightness,
  428. ARRAY_SIZE(hx8369_seq_set_display_brightness));
  429. if (ret < 0)
  430. return ret;
  431. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
  432. if (ret < 0)
  433. return ret;
  434. return 0;
  435. }
  436. #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
  437. static int hx8357_set_power(struct lcd_device *lcdev, int power)
  438. {
  439. struct hx8357_data *lcd = lcd_get_data(lcdev);
  440. int ret = 0;
  441. if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->state))
  442. ret = hx8357_exit_standby(lcdev);
  443. else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->state))
  444. ret = hx8357_enter_standby(lcdev);
  445. if (ret == 0)
  446. lcd->state = power;
  447. else
  448. dev_warn(&lcdev->dev, "failed to set power mode %d\n", power);
  449. return ret;
  450. }
  451. static int hx8357_get_power(struct lcd_device *lcdev)
  452. {
  453. struct hx8357_data *lcd = lcd_get_data(lcdev);
  454. return lcd->state;
  455. }
  456. static struct lcd_ops hx8357_ops = {
  457. .set_power = hx8357_set_power,
  458. .get_power = hx8357_get_power,
  459. };
  460. static const struct of_device_id hx8357_dt_ids[] = {
  461. {
  462. .compatible = "himax,hx8357",
  463. .data = hx8357_lcd_init,
  464. },
  465. {
  466. .compatible = "himax,hx8369",
  467. .data = hx8369_lcd_init,
  468. },
  469. {},
  470. };
  471. MODULE_DEVICE_TABLE(of, hx8357_dt_ids);
  472. static int hx8357_probe(struct spi_device *spi)
  473. {
  474. struct lcd_device *lcdev;
  475. struct hx8357_data *lcd;
  476. const struct of_device_id *match;
  477. int i, ret;
  478. lcd = devm_kzalloc(&spi->dev, sizeof(*lcd), GFP_KERNEL);
  479. if (!lcd)
  480. return -ENOMEM;
  481. ret = spi_setup(spi);
  482. if (ret < 0) {
  483. dev_err(&spi->dev, "SPI setup failed.\n");
  484. return ret;
  485. }
  486. lcd->spi = spi;
  487. match = of_match_device(hx8357_dt_ids, &spi->dev);
  488. if (!match || !match->data)
  489. return -EINVAL;
  490. lcd->reset = of_get_named_gpio(spi->dev.of_node, "gpios-reset", 0);
  491. if (!gpio_is_valid(lcd->reset)) {
  492. dev_err(&spi->dev, "Missing dt property: gpios-reset\n");
  493. return -EINVAL;
  494. }
  495. ret = devm_gpio_request_one(&spi->dev, lcd->reset,
  496. GPIOF_OUT_INIT_HIGH,
  497. "hx8357-reset");
  498. if (ret) {
  499. dev_err(&spi->dev,
  500. "failed to request gpio %d: %d\n",
  501. lcd->reset, ret);
  502. return -EINVAL;
  503. }
  504. if (of_find_property(spi->dev.of_node, "im-gpios", NULL)) {
  505. lcd->use_im_pins = 1;
  506. for (i = 0; i < HX8357_NUM_IM_PINS; i++) {
  507. lcd->im_pins[i] = of_get_named_gpio(spi->dev.of_node,
  508. "im-gpios", i);
  509. if (lcd->im_pins[i] == -EPROBE_DEFER) {
  510. dev_info(&spi->dev, "GPIO requested is not here yet, deferring the probe\n");
  511. return -EPROBE_DEFER;
  512. }
  513. if (!gpio_is_valid(lcd->im_pins[i])) {
  514. dev_err(&spi->dev, "Missing dt property: im-gpios\n");
  515. return -EINVAL;
  516. }
  517. ret = devm_gpio_request_one(&spi->dev, lcd->im_pins[i],
  518. GPIOF_OUT_INIT_LOW,
  519. "im_pins");
  520. if (ret) {
  521. dev_err(&spi->dev, "failed to request gpio %d: %d\n",
  522. lcd->im_pins[i], ret);
  523. return -EINVAL;
  524. }
  525. }
  526. } else {
  527. lcd->use_im_pins = 0;
  528. }
  529. lcdev = devm_lcd_device_register(&spi->dev, "mxsfb", &spi->dev, lcd,
  530. &hx8357_ops);
  531. if (IS_ERR(lcdev)) {
  532. ret = PTR_ERR(lcdev);
  533. return ret;
  534. }
  535. spi_set_drvdata(spi, lcdev);
  536. hx8357_lcd_reset(lcdev);
  537. ret = ((int (*)(struct lcd_device *))match->data)(lcdev);
  538. if (ret) {
  539. dev_err(&spi->dev, "Couldn't initialize panel\n");
  540. return ret;
  541. }
  542. dev_info(&spi->dev, "Panel probed\n");
  543. return 0;
  544. }
  545. static struct spi_driver hx8357_driver = {
  546. .probe = hx8357_probe,
  547. .driver = {
  548. .name = "hx8357",
  549. .of_match_table = hx8357_dt_ids,
  550. },
  551. };
  552. module_spi_driver(hx8357_driver);
  553. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  554. MODULE_DESCRIPTION("Himax HX-8357 LCD Driver");
  555. MODULE_LICENSE("GPL");