hx8357d.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DRM driver for the HX8357D LCD controller
  4. *
  5. * Copyright 2018 Broadcom
  6. * Copyright 2018 David Lechner <david@lechnology.com>
  7. * Copyright 2016 Noralf Trønnes
  8. * Copyright (C) 2015 Adafruit Industries
  9. * Copyright (C) 2013 Christian Vogelgsang
  10. */
  11. #include <linux/backlight.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/module.h>
  15. #include <linux/property.h>
  16. #include <linux/spi/spi.h>
  17. #include <drm/clients/drm_client_setup.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_drv.h>
  20. #include <drm/drm_fbdev_dma.h>
  21. #include <drm/drm_gem_atomic_helper.h>
  22. #include <drm/drm_gem_dma_helper.h>
  23. #include <drm/drm_managed.h>
  24. #include <drm/drm_mipi_dbi.h>
  25. #include <drm/drm_modeset_helper.h>
  26. #include <drm/drm_print.h>
  27. #include <video/mipi_display.h>
  28. #define HX8357D_SETOSC 0xb0
  29. #define HX8357D_SETPOWER 0xb1
  30. #define HX8357D_SETRGB 0xb3
  31. #define HX8357D_SETCYC 0xb3
  32. #define HX8357D_SETCOM 0xb6
  33. #define HX8357D_SETEXTC 0xb9
  34. #define HX8357D_SETSTBA 0xc0
  35. #define HX8357D_SETPANEL 0xcc
  36. #define HX8357D_SETGAMMA 0xe0
  37. #define HX8357D_MADCTL_MY 0x80
  38. #define HX8357D_MADCTL_MX 0x40
  39. #define HX8357D_MADCTL_MV 0x20
  40. #define HX8357D_MADCTL_ML 0x10
  41. #define HX8357D_MADCTL_RGB 0x00
  42. #define HX8357D_MADCTL_BGR 0x08
  43. #define HX8357D_MADCTL_MH 0x04
  44. static void yx240qv29_enable(struct drm_simple_display_pipe *pipe,
  45. struct drm_crtc_state *crtc_state,
  46. struct drm_plane_state *plane_state)
  47. {
  48. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  49. struct mipi_dbi *dbi = &dbidev->dbi;
  50. u8 addr_mode;
  51. int ret, idx;
  52. if (!drm_dev_enter(pipe->crtc.dev, &idx))
  53. return;
  54. DRM_DEBUG_KMS("\n");
  55. ret = mipi_dbi_poweron_conditional_reset(dbidev);
  56. if (ret < 0)
  57. goto out_exit;
  58. if (ret == 1)
  59. goto out_enable;
  60. /* setextc */
  61. mipi_dbi_command(dbi, HX8357D_SETEXTC, 0xFF, 0x83, 0x57);
  62. msleep(150);
  63. /* setRGB which also enables SDO */
  64. mipi_dbi_command(dbi, HX8357D_SETRGB, 0x00, 0x00, 0x06, 0x06);
  65. /* -1.52V */
  66. mipi_dbi_command(dbi, HX8357D_SETCOM, 0x25);
  67. /* Normal mode 70Hz, Idle mode 55 Hz */
  68. mipi_dbi_command(dbi, HX8357D_SETOSC, 0x68);
  69. /* Set Panel - BGR, Gate direction swapped */
  70. mipi_dbi_command(dbi, HX8357D_SETPANEL, 0x05);
  71. mipi_dbi_command(dbi, HX8357D_SETPOWER,
  72. 0x00, /* Not deep standby */
  73. 0x15, /* BT */
  74. 0x1C, /* VSPR */
  75. 0x1C, /* VSNR */
  76. 0x83, /* AP */
  77. 0xAA); /* FS */
  78. mipi_dbi_command(dbi, HX8357D_SETSTBA,
  79. 0x50, /* OPON normal */
  80. 0x50, /* OPON idle */
  81. 0x01, /* STBA */
  82. 0x3C, /* STBA */
  83. 0x1E, /* STBA */
  84. 0x08); /* GEN */
  85. mipi_dbi_command(dbi, HX8357D_SETCYC,
  86. 0x02, /* NW 0x02 */
  87. 0x40, /* RTN */
  88. 0x00, /* DIV */
  89. 0x2A, /* DUM */
  90. 0x2A, /* DUM */
  91. 0x0D, /* GDON */
  92. 0x78); /* GDOFF */
  93. mipi_dbi_command(dbi, HX8357D_SETGAMMA,
  94. 0x02,
  95. 0x0A,
  96. 0x11,
  97. 0x1d,
  98. 0x23,
  99. 0x35,
  100. 0x41,
  101. 0x4b,
  102. 0x4b,
  103. 0x42,
  104. 0x3A,
  105. 0x27,
  106. 0x1B,
  107. 0x08,
  108. 0x09,
  109. 0x03,
  110. 0x02,
  111. 0x0A,
  112. 0x11,
  113. 0x1d,
  114. 0x23,
  115. 0x35,
  116. 0x41,
  117. 0x4b,
  118. 0x4b,
  119. 0x42,
  120. 0x3A,
  121. 0x27,
  122. 0x1B,
  123. 0x08,
  124. 0x09,
  125. 0x03,
  126. 0x00,
  127. 0x01);
  128. /* 16 bit */
  129. mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT,
  130. MIPI_DCS_PIXEL_FMT_16BIT);
  131. /* TE off */
  132. mipi_dbi_command(dbi, MIPI_DCS_SET_TEAR_ON, 0x00);
  133. /* tear line */
  134. mipi_dbi_command(dbi, MIPI_DCS_SET_TEAR_SCANLINE, 0x00, 0x02);
  135. /* Exit Sleep */
  136. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  137. msleep(150);
  138. /* display on */
  139. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
  140. usleep_range(5000, 7000);
  141. out_enable:
  142. switch (dbidev->rotation) {
  143. default:
  144. addr_mode = HX8357D_MADCTL_MX | HX8357D_MADCTL_MY;
  145. break;
  146. case 90:
  147. addr_mode = HX8357D_MADCTL_MV | HX8357D_MADCTL_MY;
  148. break;
  149. case 180:
  150. addr_mode = 0;
  151. break;
  152. case 270:
  153. addr_mode = HX8357D_MADCTL_MV | HX8357D_MADCTL_MX;
  154. break;
  155. }
  156. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  157. mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
  158. out_exit:
  159. drm_dev_exit(idx);
  160. }
  161. static const struct drm_simple_display_pipe_funcs hx8357d_pipe_funcs = {
  162. DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(yx240qv29_enable),
  163. };
  164. static const struct drm_display_mode yx350hv15_mode = {
  165. DRM_SIMPLE_MODE(320, 480, 60, 75),
  166. };
  167. DEFINE_DRM_GEM_DMA_FOPS(hx8357d_fops);
  168. static const struct drm_driver hx8357d_driver = {
  169. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  170. .fops = &hx8357d_fops,
  171. DRM_GEM_DMA_DRIVER_OPS_VMAP,
  172. DRM_FBDEV_DMA_DRIVER_OPS,
  173. .debugfs_init = mipi_dbi_debugfs_init,
  174. .name = "hx8357d",
  175. .desc = "HX8357D",
  176. .major = 1,
  177. .minor = 0,
  178. };
  179. static const struct of_device_id hx8357d_of_match[] = {
  180. { .compatible = "adafruit,yx350hv15" },
  181. { }
  182. };
  183. MODULE_DEVICE_TABLE(of, hx8357d_of_match);
  184. static const struct spi_device_id hx8357d_id[] = {
  185. { "yx350hv15", 0 },
  186. { }
  187. };
  188. MODULE_DEVICE_TABLE(spi, hx8357d_id);
  189. static int hx8357d_probe(struct spi_device *spi)
  190. {
  191. struct device *dev = &spi->dev;
  192. struct mipi_dbi_dev *dbidev;
  193. struct drm_device *drm;
  194. struct gpio_desc *dc;
  195. u32 rotation = 0;
  196. int ret;
  197. dbidev = devm_drm_dev_alloc(dev, &hx8357d_driver,
  198. struct mipi_dbi_dev, drm);
  199. if (IS_ERR(dbidev))
  200. return PTR_ERR(dbidev);
  201. drm = &dbidev->drm;
  202. dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
  203. if (IS_ERR(dc))
  204. return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
  205. dbidev->backlight = devm_of_find_backlight(dev);
  206. if (IS_ERR(dbidev->backlight))
  207. return PTR_ERR(dbidev->backlight);
  208. device_property_read_u32(dev, "rotation", &rotation);
  209. ret = mipi_dbi_spi_init(spi, &dbidev->dbi, dc);
  210. if (ret)
  211. return ret;
  212. ret = mipi_dbi_dev_init(dbidev, &hx8357d_pipe_funcs, &yx350hv15_mode, rotation);
  213. if (ret)
  214. return ret;
  215. drm_mode_config_reset(drm);
  216. ret = drm_dev_register(drm, 0);
  217. if (ret)
  218. return ret;
  219. spi_set_drvdata(spi, drm);
  220. drm_client_setup(drm, NULL);
  221. return 0;
  222. }
  223. static void hx8357d_remove(struct spi_device *spi)
  224. {
  225. struct drm_device *drm = spi_get_drvdata(spi);
  226. drm_dev_unplug(drm);
  227. drm_atomic_helper_shutdown(drm);
  228. }
  229. static void hx8357d_shutdown(struct spi_device *spi)
  230. {
  231. drm_atomic_helper_shutdown(spi_get_drvdata(spi));
  232. }
  233. static struct spi_driver hx8357d_spi_driver = {
  234. .driver = {
  235. .name = "hx8357d",
  236. .of_match_table = hx8357d_of_match,
  237. },
  238. .id_table = hx8357d_id,
  239. .probe = hx8357d_probe,
  240. .remove = hx8357d_remove,
  241. .shutdown = hx8357d_shutdown,
  242. };
  243. module_spi_driver(hx8357d_spi_driver);
  244. MODULE_DESCRIPTION("HX8357D DRM driver");
  245. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  246. MODULE_LICENSE("GPL");