hx8357.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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/gpio/consumer.h>
  9. #include <linux/lcd.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/property.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. struct gpio_descs *im_pins;
  82. struct gpio_desc *reset;
  83. struct spi_device *spi;
  84. int state;
  85. };
  86. static u8 hx8357_seq_power[] = {
  87. HX8357_SET_POWER, 0x44, 0x41, 0x06,
  88. };
  89. static u8 hx8357_seq_vcom[] = {
  90. HX8357_SET_VCOM, 0x40, 0x10,
  91. };
  92. static u8 hx8357_seq_power_normal[] = {
  93. HX8357_SET_POWER_NORMAL, 0x05, 0x12,
  94. };
  95. static u8 hx8357_seq_panel_driving[] = {
  96. HX8357_SET_PANEL_DRIVING, 0x14, 0x3b, 0x00, 0x02, 0x11,
  97. };
  98. static u8 hx8357_seq_display_frame[] = {
  99. HX8357_SET_DISPLAY_FRAME, 0x0c,
  100. };
  101. static u8 hx8357_seq_panel_related[] = {
  102. HX8357_SET_PANEL_RELATED, 0x01,
  103. };
  104. static u8 hx8357_seq_undefined1[] = {
  105. 0xea, 0x03, 0x00, 0x00,
  106. };
  107. static u8 hx8357_seq_undefined2[] = {
  108. 0xeb, 0x40, 0x54, 0x26, 0xdb,
  109. };
  110. static u8 hx8357_seq_gamma[] = {
  111. HX8357_SET_GAMMA, 0x00, 0x15, 0x00, 0x22, 0x00,
  112. 0x08, 0x77, 0x26, 0x77, 0x22, 0x04, 0x00,
  113. };
  114. static u8 hx8357_seq_address_mode[] = {
  115. HX8357_SET_ADDRESS_MODE, 0xc0,
  116. };
  117. static u8 hx8357_seq_pixel_format[] = {
  118. HX8357_SET_PIXEL_FORMAT,
  119. HX8357_SET_PIXEL_FORMAT_DPI_18BIT |
  120. HX8357_SET_PIXEL_FORMAT_DBI_18BIT,
  121. };
  122. static u8 hx8357_seq_column_address[] = {
  123. HX8357_SET_COLUMN_ADDRESS, 0x00, 0x00, 0x01, 0x3f,
  124. };
  125. static u8 hx8357_seq_page_address[] = {
  126. HX8357_SET_PAGE_ADDRESS, 0x00, 0x00, 0x01, 0xdf,
  127. };
  128. static u8 hx8357_seq_rgb[] = {
  129. HX8357_SET_RGB, 0x02,
  130. };
  131. static u8 hx8357_seq_display_mode[] = {
  132. HX8357_SET_DISPLAY_MODE,
  133. HX8357_SET_DISPLAY_MODE_RGB_THROUGH |
  134. HX8357_SET_DISPLAY_MODE_RGB_INTERFACE,
  135. };
  136. static u8 hx8369_seq_write_CABC_min_brightness[] = {
  137. HX8369_WRITE_CABC_MIN_BRIGHTNESS, 0x00,
  138. };
  139. static u8 hx8369_seq_write_CABC_control[] = {
  140. HX8369_WRITE_CABC_DISPLAY_VALUE, 0x24,
  141. };
  142. static u8 hx8369_seq_set_display_brightness[] = {
  143. HX8369_SET_DISPLAY_BRIGHTNESS, 0xFF,
  144. };
  145. static u8 hx8369_seq_write_CABC_control_setting[] = {
  146. HX8369_WRITE_CABC_BRIGHT_CTRL, 0x02,
  147. };
  148. static u8 hx8369_seq_extension_command[] = {
  149. HX8369_SET_EXTENSION_COMMAND, 0xff, 0x83, 0x69,
  150. };
  151. static u8 hx8369_seq_display_related[] = {
  152. HX8369_SET_DISPLAY_MODE, 0x00, 0x2b, 0x03, 0x03, 0x70, 0x00,
  153. 0xff, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x01,
  154. };
  155. static u8 hx8369_seq_panel_waveform_cycle[] = {
  156. HX8369_SET_DISPLAY_WAVEFORM_CYC, 0x0a, 0x1d, 0x80, 0x06, 0x02,
  157. };
  158. static u8 hx8369_seq_set_address_mode[] = {
  159. HX8357_SET_ADDRESS_MODE, 0x00,
  160. };
  161. static u8 hx8369_seq_vcom[] = {
  162. HX8369_SET_VCOM, 0x3e, 0x3e,
  163. };
  164. static u8 hx8369_seq_gip[] = {
  165. HX8369_SET_GIP, 0x00, 0x01, 0x03, 0x25, 0x01, 0x02, 0x28, 0x70,
  166. 0x11, 0x13, 0x00, 0x00, 0x40, 0x26, 0x51, 0x37, 0x00, 0x00, 0x71,
  167. 0x35, 0x60, 0x24, 0x07, 0x0f, 0x04, 0x04,
  168. };
  169. static u8 hx8369_seq_power[] = {
  170. HX8369_SET_POWER, 0x01, 0x00, 0x34, 0x03, 0x00, 0x11, 0x11, 0x32,
  171. 0x2f, 0x3f, 0x3f, 0x01, 0x3a, 0x01, 0xe6, 0xe6, 0xe6, 0xe6, 0xe6,
  172. };
  173. static u8 hx8369_seq_gamma_curve_related[] = {
  174. HX8369_SET_GAMMA_CURVE_RELATED, 0x00, 0x0d, 0x19, 0x2f, 0x3b, 0x3d,
  175. 0x2e, 0x4a, 0x08, 0x0e, 0x0f, 0x14, 0x16, 0x14, 0x14, 0x14, 0x1e,
  176. 0x00, 0x0d, 0x19, 0x2f, 0x3b, 0x3d, 0x2e, 0x4a, 0x08, 0x0e, 0x0f,
  177. 0x14, 0x16, 0x14, 0x14, 0x14, 0x1e,
  178. };
  179. static int hx8357_spi_write_then_read(struct lcd_device *lcdev,
  180. u8 *txbuf, u16 txlen,
  181. u8 *rxbuf, u16 rxlen)
  182. {
  183. struct hx8357_data *lcd = lcd_get_data(lcdev);
  184. struct spi_message msg;
  185. struct spi_transfer xfer[2];
  186. u16 *local_txbuf = NULL;
  187. int ret = 0;
  188. memset(xfer, 0, sizeof(xfer));
  189. spi_message_init(&msg);
  190. if (txlen) {
  191. int i;
  192. local_txbuf = kcalloc(txlen, sizeof(*local_txbuf), GFP_KERNEL);
  193. if (!local_txbuf)
  194. return -ENOMEM;
  195. for (i = 0; i < txlen; i++) {
  196. local_txbuf[i] = txbuf[i];
  197. if (i > 0)
  198. local_txbuf[i] |= 1 << 8;
  199. }
  200. xfer[0].len = 2 * txlen;
  201. xfer[0].bits_per_word = 9;
  202. xfer[0].tx_buf = local_txbuf;
  203. spi_message_add_tail(&xfer[0], &msg);
  204. }
  205. if (rxlen) {
  206. xfer[1].len = rxlen;
  207. xfer[1].bits_per_word = 8;
  208. xfer[1].rx_buf = rxbuf;
  209. spi_message_add_tail(&xfer[1], &msg);
  210. }
  211. ret = spi_sync(lcd->spi, &msg);
  212. if (ret < 0)
  213. dev_err(&lcdev->dev, "Couldn't send SPI data\n");
  214. if (txlen)
  215. kfree(local_txbuf);
  216. return ret;
  217. }
  218. static inline int hx8357_spi_write_array(struct lcd_device *lcdev,
  219. u8 *value, u8 len)
  220. {
  221. return hx8357_spi_write_then_read(lcdev, value, len, NULL, 0);
  222. }
  223. static inline int hx8357_spi_write_byte(struct lcd_device *lcdev,
  224. u8 value)
  225. {
  226. return hx8357_spi_write_then_read(lcdev, &value, 1, NULL, 0);
  227. }
  228. static int hx8357_enter_standby(struct lcd_device *lcdev)
  229. {
  230. int ret;
  231. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_OFF);
  232. if (ret < 0)
  233. return ret;
  234. usleep_range(10000, 12000);
  235. ret = hx8357_spi_write_byte(lcdev, HX8357_ENTER_SLEEP_MODE);
  236. if (ret < 0)
  237. return ret;
  238. /*
  239. * The controller needs 120ms when entering in sleep mode before we can
  240. * send the command to go off sleep mode
  241. */
  242. msleep(120);
  243. return 0;
  244. }
  245. static int hx8357_exit_standby(struct lcd_device *lcdev)
  246. {
  247. int ret;
  248. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  249. if (ret < 0)
  250. return ret;
  251. /*
  252. * The controller needs 120ms when exiting from sleep mode before we
  253. * can send the command to enter in sleep mode
  254. */
  255. msleep(120);
  256. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
  257. if (ret < 0)
  258. return ret;
  259. return 0;
  260. }
  261. static void hx8357_lcd_reset(struct lcd_device *lcdev)
  262. {
  263. struct hx8357_data *lcd = lcd_get_data(lcdev);
  264. /* Reset the screen */
  265. gpiod_set_value(lcd->reset, 0);
  266. usleep_range(10000, 12000);
  267. gpiod_set_value(lcd->reset, 1);
  268. usleep_range(10000, 12000);
  269. gpiod_set_value(lcd->reset, 0);
  270. /* The controller needs 120ms to recover from reset */
  271. msleep(120);
  272. }
  273. static int hx8357_lcd_init(struct lcd_device *lcdev)
  274. {
  275. struct hx8357_data *lcd = lcd_get_data(lcdev);
  276. int ret;
  277. /*
  278. * Set the interface selection pins to SPI mode, with three
  279. * wires
  280. */
  281. if (lcd->im_pins) {
  282. gpiod_set_value_cansleep(lcd->im_pins->desc[0], 1);
  283. gpiod_set_value_cansleep(lcd->im_pins->desc[1], 0);
  284. gpiod_set_value_cansleep(lcd->im_pins->desc[2], 1);
  285. }
  286. ret = hx8357_spi_write_array(lcdev, hx8357_seq_power,
  287. ARRAY_SIZE(hx8357_seq_power));
  288. if (ret < 0)
  289. return ret;
  290. ret = hx8357_spi_write_array(lcdev, hx8357_seq_vcom,
  291. ARRAY_SIZE(hx8357_seq_vcom));
  292. if (ret < 0)
  293. return ret;
  294. ret = hx8357_spi_write_array(lcdev, hx8357_seq_power_normal,
  295. ARRAY_SIZE(hx8357_seq_power_normal));
  296. if (ret < 0)
  297. return ret;
  298. ret = hx8357_spi_write_array(lcdev, hx8357_seq_panel_driving,
  299. ARRAY_SIZE(hx8357_seq_panel_driving));
  300. if (ret < 0)
  301. return ret;
  302. ret = hx8357_spi_write_array(lcdev, hx8357_seq_display_frame,
  303. ARRAY_SIZE(hx8357_seq_display_frame));
  304. if (ret < 0)
  305. return ret;
  306. ret = hx8357_spi_write_array(lcdev, hx8357_seq_panel_related,
  307. ARRAY_SIZE(hx8357_seq_panel_related));
  308. if (ret < 0)
  309. return ret;
  310. ret = hx8357_spi_write_array(lcdev, hx8357_seq_undefined1,
  311. ARRAY_SIZE(hx8357_seq_undefined1));
  312. if (ret < 0)
  313. return ret;
  314. ret = hx8357_spi_write_array(lcdev, hx8357_seq_undefined2,
  315. ARRAY_SIZE(hx8357_seq_undefined2));
  316. if (ret < 0)
  317. return ret;
  318. ret = hx8357_spi_write_array(lcdev, hx8357_seq_gamma,
  319. ARRAY_SIZE(hx8357_seq_gamma));
  320. if (ret < 0)
  321. return ret;
  322. ret = hx8357_spi_write_array(lcdev, hx8357_seq_address_mode,
  323. ARRAY_SIZE(hx8357_seq_address_mode));
  324. if (ret < 0)
  325. return ret;
  326. ret = hx8357_spi_write_array(lcdev, hx8357_seq_pixel_format,
  327. ARRAY_SIZE(hx8357_seq_pixel_format));
  328. if (ret < 0)
  329. return ret;
  330. ret = hx8357_spi_write_array(lcdev, hx8357_seq_column_address,
  331. ARRAY_SIZE(hx8357_seq_column_address));
  332. if (ret < 0)
  333. return ret;
  334. ret = hx8357_spi_write_array(lcdev, hx8357_seq_page_address,
  335. ARRAY_SIZE(hx8357_seq_page_address));
  336. if (ret < 0)
  337. return ret;
  338. ret = hx8357_spi_write_array(lcdev, hx8357_seq_rgb,
  339. ARRAY_SIZE(hx8357_seq_rgb));
  340. if (ret < 0)
  341. return ret;
  342. ret = hx8357_spi_write_array(lcdev, hx8357_seq_display_mode,
  343. ARRAY_SIZE(hx8357_seq_display_mode));
  344. if (ret < 0)
  345. return ret;
  346. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  347. if (ret < 0)
  348. return ret;
  349. /*
  350. * The controller needs 120ms to fully recover from exiting sleep mode
  351. */
  352. msleep(120);
  353. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
  354. if (ret < 0)
  355. return ret;
  356. usleep_range(5000, 7000);
  357. ret = hx8357_spi_write_byte(lcdev, HX8357_WRITE_MEMORY_START);
  358. if (ret < 0)
  359. return ret;
  360. return 0;
  361. }
  362. static int hx8369_lcd_init(struct lcd_device *lcdev)
  363. {
  364. int ret;
  365. ret = hx8357_spi_write_array(lcdev, hx8369_seq_extension_command,
  366. ARRAY_SIZE(hx8369_seq_extension_command));
  367. if (ret < 0)
  368. return ret;
  369. usleep_range(10000, 12000);
  370. ret = hx8357_spi_write_array(lcdev, hx8369_seq_display_related,
  371. ARRAY_SIZE(hx8369_seq_display_related));
  372. if (ret < 0)
  373. return ret;
  374. ret = hx8357_spi_write_array(lcdev, hx8369_seq_panel_waveform_cycle,
  375. ARRAY_SIZE(hx8369_seq_panel_waveform_cycle));
  376. if (ret < 0)
  377. return ret;
  378. ret = hx8357_spi_write_array(lcdev, hx8369_seq_set_address_mode,
  379. ARRAY_SIZE(hx8369_seq_set_address_mode));
  380. if (ret < 0)
  381. return ret;
  382. ret = hx8357_spi_write_array(lcdev, hx8369_seq_vcom,
  383. ARRAY_SIZE(hx8369_seq_vcom));
  384. if (ret < 0)
  385. return ret;
  386. ret = hx8357_spi_write_array(lcdev, hx8369_seq_gip,
  387. ARRAY_SIZE(hx8369_seq_gip));
  388. if (ret < 0)
  389. return ret;
  390. ret = hx8357_spi_write_array(lcdev, hx8369_seq_power,
  391. ARRAY_SIZE(hx8369_seq_power));
  392. if (ret < 0)
  393. return ret;
  394. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  395. if (ret < 0)
  396. return ret;
  397. /*
  398. * The controller needs 120ms to fully recover from exiting sleep mode
  399. */
  400. msleep(120);
  401. ret = hx8357_spi_write_array(lcdev, hx8369_seq_gamma_curve_related,
  402. ARRAY_SIZE(hx8369_seq_gamma_curve_related));
  403. if (ret < 0)
  404. return ret;
  405. ret = hx8357_spi_write_byte(lcdev, HX8357_EXIT_SLEEP_MODE);
  406. if (ret < 0)
  407. return ret;
  408. usleep_range(1000, 1200);
  409. ret = hx8357_spi_write_array(lcdev, hx8369_seq_write_CABC_control,
  410. ARRAY_SIZE(hx8369_seq_write_CABC_control));
  411. if (ret < 0)
  412. return ret;
  413. usleep_range(10000, 12000);
  414. ret = hx8357_spi_write_array(lcdev,
  415. hx8369_seq_write_CABC_control_setting,
  416. ARRAY_SIZE(hx8369_seq_write_CABC_control_setting));
  417. if (ret < 0)
  418. return ret;
  419. ret = hx8357_spi_write_array(lcdev,
  420. hx8369_seq_write_CABC_min_brightness,
  421. ARRAY_SIZE(hx8369_seq_write_CABC_min_brightness));
  422. if (ret < 0)
  423. return ret;
  424. usleep_range(10000, 12000);
  425. ret = hx8357_spi_write_array(lcdev, hx8369_seq_set_display_brightness,
  426. ARRAY_SIZE(hx8369_seq_set_display_brightness));
  427. if (ret < 0)
  428. return ret;
  429. ret = hx8357_spi_write_byte(lcdev, HX8357_SET_DISPLAY_ON);
  430. if (ret < 0)
  431. return ret;
  432. return 0;
  433. }
  434. #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL)
  435. static int hx8357_set_power(struct lcd_device *lcdev, int power)
  436. {
  437. struct hx8357_data *lcd = lcd_get_data(lcdev);
  438. int ret = 0;
  439. if (POWER_IS_ON(power) && !POWER_IS_ON(lcd->state))
  440. ret = hx8357_exit_standby(lcdev);
  441. else if (!POWER_IS_ON(power) && POWER_IS_ON(lcd->state))
  442. ret = hx8357_enter_standby(lcdev);
  443. if (ret == 0)
  444. lcd->state = power;
  445. else
  446. dev_warn(&lcdev->dev, "failed to set power mode %d\n", power);
  447. return ret;
  448. }
  449. static int hx8357_get_power(struct lcd_device *lcdev)
  450. {
  451. struct hx8357_data *lcd = lcd_get_data(lcdev);
  452. return lcd->state;
  453. }
  454. static const struct lcd_ops hx8357_ops = {
  455. .set_power = hx8357_set_power,
  456. .get_power = hx8357_get_power,
  457. };
  458. typedef int (*hx8357_init_fn)(struct lcd_device *);
  459. static int hx8357_probe(struct spi_device *spi)
  460. {
  461. struct device *dev = &spi->dev;
  462. struct lcd_device *lcdev;
  463. struct hx8357_data *lcd;
  464. hx8357_init_fn init_fn;
  465. int i, ret;
  466. lcd = devm_kzalloc(dev, sizeof(*lcd), GFP_KERNEL);
  467. if (!lcd)
  468. return -ENOMEM;
  469. ret = spi_setup(spi);
  470. if (ret < 0)
  471. return dev_err_probe(dev, ret, "SPI setup failed.\n");
  472. lcd->spi = spi;
  473. init_fn = device_get_match_data(dev);
  474. if (!init_fn)
  475. return -EINVAL;
  476. lcd->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  477. if (IS_ERR(lcd->reset))
  478. return dev_err_probe(dev, PTR_ERR(lcd->reset), "failed to request reset GPIO\n");
  479. gpiod_set_consumer_name(lcd->reset, "hx8357-reset");
  480. lcd->im_pins = devm_gpiod_get_array_optional(dev, "im", GPIOD_OUT_LOW);
  481. if (IS_ERR(lcd->im_pins))
  482. return dev_err_probe(dev, PTR_ERR(lcd->im_pins), "failed to request im GPIOs\n");
  483. if (lcd->im_pins) {
  484. if (lcd->im_pins->ndescs < HX8357_NUM_IM_PINS)
  485. return dev_err_probe(dev, -EINVAL, "not enough im GPIOs\n");
  486. for (i = 0; i < HX8357_NUM_IM_PINS; i++)
  487. gpiod_set_consumer_name(lcd->im_pins->desc[i], "im_pins");
  488. }
  489. lcdev = devm_lcd_device_register(dev, "mxsfb", dev, lcd, &hx8357_ops);
  490. if (IS_ERR(lcdev)) {
  491. ret = PTR_ERR(lcdev);
  492. return ret;
  493. }
  494. spi_set_drvdata(spi, lcdev);
  495. hx8357_lcd_reset(lcdev);
  496. ret = init_fn(lcdev);
  497. if (ret)
  498. return dev_err_probe(dev, ret, "Couldn't initialize panel\n");
  499. dev_info(dev, "Panel probed\n");
  500. return 0;
  501. }
  502. static const struct of_device_id hx8357_dt_ids[] = {
  503. {
  504. .compatible = "himax,hx8357",
  505. .data = hx8357_lcd_init,
  506. },
  507. {
  508. .compatible = "himax,hx8369",
  509. .data = hx8369_lcd_init,
  510. },
  511. {}
  512. };
  513. MODULE_DEVICE_TABLE(of, hx8357_dt_ids);
  514. static struct spi_driver hx8357_driver = {
  515. .probe = hx8357_probe,
  516. .driver = {
  517. .name = "hx8357",
  518. .of_match_table = hx8357_dt_ids,
  519. },
  520. };
  521. module_spi_driver(hx8357_driver);
  522. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  523. MODULE_DESCRIPTION("Himax HX-8357 LCD Driver");
  524. MODULE_LICENSE("GPL");