hx8357.c 16 KB

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