lg4573.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * LCD: LG4573, TFT 4.3", 480x800, RGB24
  4. * LCD initialization via SPI
  5. *
  6. */
  7. #include <common.h>
  8. #include <backlight.h>
  9. #include <display.h>
  10. #include <dm.h>
  11. #include <dm/read.h>
  12. #include <dm/uclass-internal.h>
  13. #include <errno.h>
  14. #include <spi.h>
  15. #include <asm/gpio.h>
  16. #define PWR_ON_DELAY_MSECS 120
  17. static int lb043wv_spi_write_u16(struct spi_slave *slave, u16 val)
  18. {
  19. unsigned short buf16 = htons(val);
  20. int ret = 0;
  21. ret = spi_xfer(slave, 16, &buf16, NULL,
  22. SPI_XFER_BEGIN | SPI_XFER_END);
  23. if (ret)
  24. debug("%s: Failed to send: %d\n", __func__, ret);
  25. return ret;
  26. }
  27. static void lb043wv_spi_write_u16_array(struct spi_slave *slave, u16 *buff,
  28. int size)
  29. {
  30. int i;
  31. for (i = 0; i < size; i++)
  32. lb043wv_spi_write_u16(slave, buff[i]);
  33. }
  34. static void lb043wv_display_mode_settings(struct spi_slave *slave)
  35. {
  36. static u16 display_mode_settings[] = {
  37. 0x703A,
  38. 0x7270,
  39. 0x70B1,
  40. 0x7208,
  41. 0x723B,
  42. 0x720F,
  43. 0x70B2,
  44. 0x7200,
  45. 0x72C8,
  46. 0x70B3,
  47. 0x7200,
  48. 0x70B4,
  49. 0x7200,
  50. 0x70B5,
  51. 0x7242,
  52. 0x7210,
  53. 0x7210,
  54. 0x7200,
  55. 0x7220,
  56. 0x70B6,
  57. 0x720B,
  58. 0x720F,
  59. 0x723C,
  60. 0x7213,
  61. 0x7213,
  62. 0x72E8,
  63. 0x70B7,
  64. 0x7246,
  65. 0x7206,
  66. 0x720C,
  67. 0x7200,
  68. 0x7200,
  69. };
  70. debug("transfer display mode settings\n");
  71. lb043wv_spi_write_u16_array(slave, display_mode_settings,
  72. ARRAY_SIZE(display_mode_settings));
  73. }
  74. static void lb043wv_power_settings(struct spi_slave *slave)
  75. {
  76. static u16 power_settings[] = {
  77. 0x70C0,
  78. 0x7201,
  79. 0x7211,
  80. 0x70C3,
  81. 0x7207,
  82. 0x7203,
  83. 0x7204,
  84. 0x7204,
  85. 0x7204,
  86. 0x70C4,
  87. 0x7212,
  88. 0x7224,
  89. 0x7218,
  90. 0x7218,
  91. 0x7202,
  92. 0x7249,
  93. 0x70C5,
  94. 0x726F,
  95. 0x70C6,
  96. 0x7241,
  97. 0x7263,
  98. };
  99. debug("transfer power settings\n");
  100. lb043wv_spi_write_u16_array(slave, power_settings,
  101. ARRAY_SIZE(power_settings));
  102. }
  103. static void lb043wv_gamma_settings(struct spi_slave *slave)
  104. {
  105. static u16 gamma_settings[] = {
  106. 0x70D0,
  107. 0x7203,
  108. 0x7207,
  109. 0x7273,
  110. 0x7235,
  111. 0x7200,
  112. 0x7201,
  113. 0x7220,
  114. 0x7200,
  115. 0x7203,
  116. 0x70D1,
  117. 0x7203,
  118. 0x7207,
  119. 0x7273,
  120. 0x7235,
  121. 0x7200,
  122. 0x7201,
  123. 0x7220,
  124. 0x7200,
  125. 0x7203,
  126. 0x70D2,
  127. 0x7203,
  128. 0x7207,
  129. 0x7273,
  130. 0x7235,
  131. 0x7200,
  132. 0x7201,
  133. 0x7220,
  134. 0x7200,
  135. 0x7203,
  136. 0x70D3,
  137. 0x7203,
  138. 0x7207,
  139. 0x7273,
  140. 0x7235,
  141. 0x7200,
  142. 0x7201,
  143. 0x7220,
  144. 0x7200,
  145. 0x7203,
  146. 0x70D4,
  147. 0x7203,
  148. 0x7207,
  149. 0x7273,
  150. 0x7235,
  151. 0x7200,
  152. 0x7201,
  153. 0x7220,
  154. 0x7200,
  155. 0x7203,
  156. 0x70D5,
  157. 0x7203,
  158. 0x7207,
  159. 0x7273,
  160. 0x7235,
  161. 0x7200,
  162. 0x7201,
  163. 0x7220,
  164. 0x7200,
  165. 0x7203,
  166. };
  167. debug("transfer gamma settings\n");
  168. lb043wv_spi_write_u16_array(slave, gamma_settings,
  169. ARRAY_SIZE(gamma_settings));
  170. }
  171. static void lb043wv_display_on(struct spi_slave *slave)
  172. {
  173. static u16 sleep_out = 0x7011;
  174. static u16 display_on = 0x7029;
  175. lb043wv_spi_write_u16(slave, sleep_out);
  176. mdelay(PWR_ON_DELAY_MSECS);
  177. lb043wv_spi_write_u16(slave, display_on);
  178. }
  179. static int lg4573_spi_startup(struct spi_slave *slave)
  180. {
  181. int ret;
  182. ret = spi_claim_bus(slave);
  183. if (ret)
  184. return ret;
  185. lb043wv_display_mode_settings(slave);
  186. lb043wv_power_settings(slave);
  187. lb043wv_gamma_settings(slave);
  188. lb043wv_display_on(slave);
  189. spi_release_bus(slave);
  190. return 0;
  191. }
  192. static int do_lgset(cmd_tbl_t *cmdtp, int flag, int argc,
  193. char * const argv[])
  194. {
  195. struct spi_slave *slave;
  196. struct udevice *dev;
  197. int ret;
  198. ret = uclass_get_device_by_driver(UCLASS_DISPLAY,
  199. DM_GET_DRIVER(lg4573_lcd), &dev);
  200. if (ret) {
  201. printf("%s: Could not get lg4573 device\n", __func__);
  202. return ret;
  203. }
  204. slave = dev_get_parent_priv(dev);
  205. if (!slave) {
  206. printf("%s: No slave data\n", __func__);
  207. return -ENODEV;
  208. }
  209. lg4573_spi_startup(slave);
  210. return 0;
  211. }
  212. U_BOOT_CMD(
  213. lgset, 2, 1, do_lgset,
  214. "set lgdisplay",
  215. ""
  216. );
  217. static int lg4573_bind(struct udevice *dev)
  218. {
  219. return 0;
  220. }
  221. static int lg4573_probe(struct udevice *dev)
  222. {
  223. return 0;
  224. }
  225. static const struct udevice_id lg4573_ids[] = {
  226. { .compatible = "lg,lg4573" },
  227. { }
  228. };
  229. struct lg4573_lcd_priv {
  230. struct display_timing timing;
  231. struct udevice *backlight;
  232. struct gpio_desc enable;
  233. int panel_bpp;
  234. u32 power_on_delay;
  235. };
  236. static int lg4573_lcd_read_timing(struct udevice *dev,
  237. struct display_timing *timing)
  238. {
  239. struct lg4573_lcd_priv *priv = dev_get_priv(dev);
  240. memcpy(timing, &priv->timing, sizeof(struct display_timing));
  241. return 0;
  242. }
  243. static int lg4573_lcd_enable(struct udevice *dev, int bpp,
  244. const struct display_timing *edid)
  245. {
  246. struct spi_slave *slave = dev_get_parent_priv(dev);
  247. struct lg4573_lcd_priv *priv = dev_get_priv(dev);
  248. int ret = 0;
  249. dm_gpio_set_value(&priv->enable, 1);
  250. ret = backlight_enable(priv->backlight);
  251. mdelay(priv->power_on_delay);
  252. lg4573_spi_startup(slave);
  253. return ret;
  254. };
  255. static const struct dm_display_ops lg4573_lcd_ops = {
  256. .read_timing = lg4573_lcd_read_timing,
  257. .enable = lg4573_lcd_enable,
  258. };
  259. static int lg4573_ofdata_to_platdata(struct udevice *dev)
  260. {
  261. struct lg4573_lcd_priv *priv = dev_get_priv(dev);
  262. int ret;
  263. ret = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev,
  264. "backlight", &priv->backlight);
  265. if (ret) {
  266. debug("%s: Cannot get backlight: ret=%d\n", __func__, ret);
  267. return log_ret(ret);
  268. }
  269. ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
  270. GPIOD_IS_OUT);
  271. if (ret) {
  272. debug("%s: Warning: cannot get enable GPIO: ret=%d\n",
  273. __func__, ret);
  274. if (ret != -ENOENT)
  275. return log_ret(ret);
  276. }
  277. priv->power_on_delay = dev_read_u32_default(dev, "power-on-delay", 10);
  278. return 0;
  279. }
  280. U_BOOT_DRIVER(lg4573_lcd) = {
  281. .name = "lg4573",
  282. .id = UCLASS_DISPLAY,
  283. .ops = &lg4573_lcd_ops,
  284. .ofdata_to_platdata = lg4573_ofdata_to_platdata,
  285. .of_match = lg4573_ids,
  286. .bind = lg4573_bind,
  287. .probe = lg4573_probe,
  288. .priv_auto_alloc_size = sizeof(struct lg4573_lcd_priv),
  289. };