lg4573.c 5.8 KB

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