panel-sitronix-st7789v.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Free Electrons
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/module.h>
  8. #include <linux/regulator/consumer.h>
  9. #include <linux/spi/spi.h>
  10. #include <video/mipi_display.h>
  11. #include <drm/drm_device.h>
  12. #include <drm/drm_modes.h>
  13. #include <drm/drm_panel.h>
  14. #define ST7789V_COLMOD_RGB_FMT_18BITS (6 << 4)
  15. #define ST7789V_COLMOD_CTRL_FMT_18BITS (6 << 0)
  16. #define ST7789V_RAMCTRL_CMD 0xb0
  17. #define ST7789V_RAMCTRL_RM_RGB BIT(4)
  18. #define ST7789V_RAMCTRL_DM_RGB BIT(0)
  19. #define ST7789V_RAMCTRL_MAGIC (3 << 6)
  20. #define ST7789V_RAMCTRL_EPF(n) (((n) & 3) << 4)
  21. #define ST7789V_RGBCTRL_CMD 0xb1
  22. #define ST7789V_RGBCTRL_WO BIT(7)
  23. #define ST7789V_RGBCTRL_RCM(n) (((n) & 3) << 5)
  24. #define ST7789V_RGBCTRL_VSYNC_HIGH BIT(3)
  25. #define ST7789V_RGBCTRL_HSYNC_HIGH BIT(2)
  26. #define ST7789V_RGBCTRL_PCLK_HIGH BIT(1)
  27. #define ST7789V_RGBCTRL_VBP(n) ((n) & 0x7f)
  28. #define ST7789V_RGBCTRL_HBP(n) ((n) & 0x1f)
  29. #define ST7789V_PORCTRL_CMD 0xb2
  30. #define ST7789V_PORCTRL_IDLE_BP(n) (((n) & 0xf) << 4)
  31. #define ST7789V_PORCTRL_IDLE_FP(n) ((n) & 0xf)
  32. #define ST7789V_PORCTRL_PARTIAL_BP(n) (((n) & 0xf) << 4)
  33. #define ST7789V_PORCTRL_PARTIAL_FP(n) ((n) & 0xf)
  34. #define ST7789V_GCTRL_CMD 0xb7
  35. #define ST7789V_GCTRL_VGHS(n) (((n) & 7) << 4)
  36. #define ST7789V_GCTRL_VGLS(n) ((n) & 7)
  37. #define ST7789V_VCOMS_CMD 0xbb
  38. #define ST7789V_LCMCTRL_CMD 0xc0
  39. #define ST7789V_LCMCTRL_XBGR BIT(5)
  40. #define ST7789V_LCMCTRL_XMX BIT(3)
  41. #define ST7789V_LCMCTRL_XMH BIT(2)
  42. #define ST7789V_VDVVRHEN_CMD 0xc2
  43. #define ST7789V_VDVVRHEN_CMDEN BIT(0)
  44. #define ST7789V_VRHS_CMD 0xc3
  45. #define ST7789V_VDVS_CMD 0xc4
  46. #define ST7789V_FRCTRL2_CMD 0xc6
  47. #define ST7789V_PWCTRL1_CMD 0xd0
  48. #define ST7789V_PWCTRL1_MAGIC 0xa4
  49. #define ST7789V_PWCTRL1_AVDD(n) (((n) & 3) << 6)
  50. #define ST7789V_PWCTRL1_AVCL(n) (((n) & 3) << 4)
  51. #define ST7789V_PWCTRL1_VDS(n) ((n) & 3)
  52. #define ST7789V_PVGAMCTRL_CMD 0xe0
  53. #define ST7789V_PVGAMCTRL_JP0(n) (((n) & 3) << 4)
  54. #define ST7789V_PVGAMCTRL_JP1(n) (((n) & 3) << 4)
  55. #define ST7789V_PVGAMCTRL_VP0(n) ((n) & 0xf)
  56. #define ST7789V_PVGAMCTRL_VP1(n) ((n) & 0x3f)
  57. #define ST7789V_PVGAMCTRL_VP2(n) ((n) & 0x3f)
  58. #define ST7789V_PVGAMCTRL_VP4(n) ((n) & 0x1f)
  59. #define ST7789V_PVGAMCTRL_VP6(n) ((n) & 0x1f)
  60. #define ST7789V_PVGAMCTRL_VP13(n) ((n) & 0xf)
  61. #define ST7789V_PVGAMCTRL_VP20(n) ((n) & 0x7f)
  62. #define ST7789V_PVGAMCTRL_VP27(n) ((n) & 7)
  63. #define ST7789V_PVGAMCTRL_VP36(n) (((n) & 7) << 4)
  64. #define ST7789V_PVGAMCTRL_VP43(n) ((n) & 0x7f)
  65. #define ST7789V_PVGAMCTRL_VP50(n) ((n) & 0xf)
  66. #define ST7789V_PVGAMCTRL_VP57(n) ((n) & 0x1f)
  67. #define ST7789V_PVGAMCTRL_VP59(n) ((n) & 0x1f)
  68. #define ST7789V_PVGAMCTRL_VP61(n) ((n) & 0x3f)
  69. #define ST7789V_PVGAMCTRL_VP62(n) ((n) & 0x3f)
  70. #define ST7789V_PVGAMCTRL_VP63(n) (((n) & 0xf) << 4)
  71. #define ST7789V_NVGAMCTRL_CMD 0xe1
  72. #define ST7789V_NVGAMCTRL_JN0(n) (((n) & 3) << 4)
  73. #define ST7789V_NVGAMCTRL_JN1(n) (((n) & 3) << 4)
  74. #define ST7789V_NVGAMCTRL_VN0(n) ((n) & 0xf)
  75. #define ST7789V_NVGAMCTRL_VN1(n) ((n) & 0x3f)
  76. #define ST7789V_NVGAMCTRL_VN2(n) ((n) & 0x3f)
  77. #define ST7789V_NVGAMCTRL_VN4(n) ((n) & 0x1f)
  78. #define ST7789V_NVGAMCTRL_VN6(n) ((n) & 0x1f)
  79. #define ST7789V_NVGAMCTRL_VN13(n) ((n) & 0xf)
  80. #define ST7789V_NVGAMCTRL_VN20(n) ((n) & 0x7f)
  81. #define ST7789V_NVGAMCTRL_VN27(n) ((n) & 7)
  82. #define ST7789V_NVGAMCTRL_VN36(n) (((n) & 7) << 4)
  83. #define ST7789V_NVGAMCTRL_VN43(n) ((n) & 0x7f)
  84. #define ST7789V_NVGAMCTRL_VN50(n) ((n) & 0xf)
  85. #define ST7789V_NVGAMCTRL_VN57(n) ((n) & 0x1f)
  86. #define ST7789V_NVGAMCTRL_VN59(n) ((n) & 0x1f)
  87. #define ST7789V_NVGAMCTRL_VN61(n) ((n) & 0x3f)
  88. #define ST7789V_NVGAMCTRL_VN62(n) ((n) & 0x3f)
  89. #define ST7789V_NVGAMCTRL_VN63(n) (((n) & 0xf) << 4)
  90. #define ST7789V_TEST(val, func) \
  91. do { \
  92. if ((val = (func))) \
  93. return val; \
  94. } while (0)
  95. struct st7789v {
  96. struct drm_panel panel;
  97. struct spi_device *spi;
  98. struct gpio_desc *reset;
  99. struct regulator *power;
  100. };
  101. enum st7789v_prefix {
  102. ST7789V_COMMAND = 0,
  103. ST7789V_DATA = 1,
  104. };
  105. static inline struct st7789v *panel_to_st7789v(struct drm_panel *panel)
  106. {
  107. return container_of(panel, struct st7789v, panel);
  108. }
  109. static int st7789v_spi_write(struct st7789v *ctx, enum st7789v_prefix prefix,
  110. u8 data)
  111. {
  112. struct spi_transfer xfer = { };
  113. struct spi_message msg;
  114. u16 txbuf = ((prefix & 1) << 8) | data;
  115. spi_message_init(&msg);
  116. xfer.tx_buf = &txbuf;
  117. xfer.bits_per_word = 9;
  118. xfer.len = sizeof(txbuf);
  119. spi_message_add_tail(&xfer, &msg);
  120. return spi_sync(ctx->spi, &msg);
  121. }
  122. static int st7789v_write_command(struct st7789v *ctx, u8 cmd)
  123. {
  124. return st7789v_spi_write(ctx, ST7789V_COMMAND, cmd);
  125. }
  126. static int st7789v_write_data(struct st7789v *ctx, u8 cmd)
  127. {
  128. return st7789v_spi_write(ctx, ST7789V_DATA, cmd);
  129. }
  130. static const struct drm_display_mode default_mode = {
  131. .clock = 7000,
  132. .hdisplay = 240,
  133. .hsync_start = 240 + 38,
  134. .hsync_end = 240 + 38 + 10,
  135. .htotal = 240 + 38 + 10 + 10,
  136. .vdisplay = 320,
  137. .vsync_start = 320 + 8,
  138. .vsync_end = 320 + 8 + 4,
  139. .vtotal = 320 + 8 + 4 + 4,
  140. };
  141. static int st7789v_get_modes(struct drm_panel *panel,
  142. struct drm_connector *connector)
  143. {
  144. struct drm_display_mode *mode;
  145. mode = drm_mode_duplicate(connector->dev, &default_mode);
  146. if (!mode) {
  147. dev_err(panel->dev, "failed to add mode %ux%ux@%u\n",
  148. default_mode.hdisplay, default_mode.vdisplay,
  149. drm_mode_vrefresh(&default_mode));
  150. return -ENOMEM;
  151. }
  152. drm_mode_set_name(mode);
  153. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  154. drm_mode_probed_add(connector, mode);
  155. connector->display_info.width_mm = 61;
  156. connector->display_info.height_mm = 103;
  157. return 1;
  158. }
  159. static int st7789v_prepare(struct drm_panel *panel)
  160. {
  161. struct st7789v *ctx = panel_to_st7789v(panel);
  162. int ret;
  163. ret = regulator_enable(ctx->power);
  164. if (ret)
  165. return ret;
  166. gpiod_set_value(ctx->reset, 1);
  167. msleep(30);
  168. gpiod_set_value(ctx->reset, 0);
  169. msleep(120);
  170. ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_EXIT_SLEEP_MODE));
  171. /* We need to wait 120ms after a sleep out command */
  172. msleep(120);
  173. ST7789V_TEST(ret, st7789v_write_command(ctx,
  174. MIPI_DCS_SET_ADDRESS_MODE));
  175. ST7789V_TEST(ret, st7789v_write_data(ctx, 0));
  176. ST7789V_TEST(ret, st7789v_write_command(ctx,
  177. MIPI_DCS_SET_PIXEL_FORMAT));
  178. ST7789V_TEST(ret, st7789v_write_data(ctx,
  179. (MIPI_DCS_PIXEL_FMT_18BIT << 4) |
  180. (MIPI_DCS_PIXEL_FMT_18BIT)));
  181. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_PORCTRL_CMD));
  182. ST7789V_TEST(ret, st7789v_write_data(ctx, 0xc));
  183. ST7789V_TEST(ret, st7789v_write_data(ctx, 0xc));
  184. ST7789V_TEST(ret, st7789v_write_data(ctx, 0));
  185. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PORCTRL_IDLE_BP(3) |
  186. ST7789V_PORCTRL_IDLE_FP(3)));
  187. ST7789V_TEST(ret, st7789v_write_data(ctx,
  188. ST7789V_PORCTRL_PARTIAL_BP(3) |
  189. ST7789V_PORCTRL_PARTIAL_FP(3)));
  190. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_GCTRL_CMD));
  191. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_GCTRL_VGLS(5) |
  192. ST7789V_GCTRL_VGHS(3)));
  193. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VCOMS_CMD));
  194. ST7789V_TEST(ret, st7789v_write_data(ctx, 0x2b));
  195. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_LCMCTRL_CMD));
  196. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_LCMCTRL_XMH |
  197. ST7789V_LCMCTRL_XMX |
  198. ST7789V_LCMCTRL_XBGR));
  199. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VDVVRHEN_CMD));
  200. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_VDVVRHEN_CMDEN));
  201. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VRHS_CMD));
  202. ST7789V_TEST(ret, st7789v_write_data(ctx, 0xf));
  203. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_VDVS_CMD));
  204. ST7789V_TEST(ret, st7789v_write_data(ctx, 0x20));
  205. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_FRCTRL2_CMD));
  206. ST7789V_TEST(ret, st7789v_write_data(ctx, 0xf));
  207. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_PWCTRL1_CMD));
  208. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PWCTRL1_MAGIC));
  209. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PWCTRL1_AVDD(2) |
  210. ST7789V_PWCTRL1_AVCL(2) |
  211. ST7789V_PWCTRL1_VDS(1)));
  212. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_PVGAMCTRL_CMD));
  213. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP63(0xd)));
  214. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP1(0xca)));
  215. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP2(0xe)));
  216. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP4(8)));
  217. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP6(9)));
  218. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP13(7)));
  219. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP20(0x2d)));
  220. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP27(0xb) |
  221. ST7789V_PVGAMCTRL_VP36(3)));
  222. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP43(0x3d)));
  223. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_JP1(3) |
  224. ST7789V_PVGAMCTRL_VP50(4)));
  225. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP57(0xa)));
  226. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP59(0xa)));
  227. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP61(0x1b)));
  228. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_PVGAMCTRL_VP62(0x28)));
  229. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_NVGAMCTRL_CMD));
  230. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN63(0xd)));
  231. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN1(0xca)));
  232. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN2(0xf)));
  233. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN4(8)));
  234. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN6(8)));
  235. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN13(7)));
  236. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN20(0x2e)));
  237. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN27(0xc) |
  238. ST7789V_NVGAMCTRL_VN36(5)));
  239. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN43(0x40)));
  240. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_JN1(3) |
  241. ST7789V_NVGAMCTRL_VN50(4)));
  242. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN57(9)));
  243. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN59(0xb)));
  244. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN61(0x1b)));
  245. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_NVGAMCTRL_VN62(0x28)));
  246. ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_ENTER_INVERT_MODE));
  247. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_RAMCTRL_CMD));
  248. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RAMCTRL_DM_RGB |
  249. ST7789V_RAMCTRL_RM_RGB));
  250. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RAMCTRL_EPF(3) |
  251. ST7789V_RAMCTRL_MAGIC));
  252. ST7789V_TEST(ret, st7789v_write_command(ctx, ST7789V_RGBCTRL_CMD));
  253. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RGBCTRL_WO |
  254. ST7789V_RGBCTRL_RCM(2) |
  255. ST7789V_RGBCTRL_VSYNC_HIGH |
  256. ST7789V_RGBCTRL_HSYNC_HIGH |
  257. ST7789V_RGBCTRL_PCLK_HIGH));
  258. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RGBCTRL_VBP(8)));
  259. ST7789V_TEST(ret, st7789v_write_data(ctx, ST7789V_RGBCTRL_HBP(20)));
  260. return 0;
  261. }
  262. static int st7789v_enable(struct drm_panel *panel)
  263. {
  264. struct st7789v *ctx = panel_to_st7789v(panel);
  265. return st7789v_write_command(ctx, MIPI_DCS_SET_DISPLAY_ON);
  266. }
  267. static int st7789v_disable(struct drm_panel *panel)
  268. {
  269. struct st7789v *ctx = panel_to_st7789v(panel);
  270. int ret;
  271. ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_SET_DISPLAY_OFF));
  272. return 0;
  273. }
  274. static int st7789v_unprepare(struct drm_panel *panel)
  275. {
  276. struct st7789v *ctx = panel_to_st7789v(panel);
  277. int ret;
  278. ST7789V_TEST(ret, st7789v_write_command(ctx, MIPI_DCS_ENTER_SLEEP_MODE));
  279. regulator_disable(ctx->power);
  280. return 0;
  281. }
  282. static const struct drm_panel_funcs st7789v_drm_funcs = {
  283. .disable = st7789v_disable,
  284. .enable = st7789v_enable,
  285. .get_modes = st7789v_get_modes,
  286. .prepare = st7789v_prepare,
  287. .unprepare = st7789v_unprepare,
  288. };
  289. static int st7789v_probe(struct spi_device *spi)
  290. {
  291. struct st7789v *ctx;
  292. int ret;
  293. ctx = devm_kzalloc(&spi->dev, sizeof(*ctx), GFP_KERNEL);
  294. if (!ctx)
  295. return -ENOMEM;
  296. spi_set_drvdata(spi, ctx);
  297. ctx->spi = spi;
  298. drm_panel_init(&ctx->panel, &spi->dev, &st7789v_drm_funcs,
  299. DRM_MODE_CONNECTOR_DPI);
  300. ctx->power = devm_regulator_get(&spi->dev, "power");
  301. if (IS_ERR(ctx->power))
  302. return PTR_ERR(ctx->power);
  303. ctx->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_LOW);
  304. if (IS_ERR(ctx->reset)) {
  305. dev_err(&spi->dev, "Couldn't get our reset line\n");
  306. return PTR_ERR(ctx->reset);
  307. }
  308. ret = drm_panel_of_backlight(&ctx->panel);
  309. if (ret)
  310. return ret;
  311. drm_panel_add(&ctx->panel);
  312. return 0;
  313. }
  314. static int st7789v_remove(struct spi_device *spi)
  315. {
  316. struct st7789v *ctx = spi_get_drvdata(spi);
  317. drm_panel_remove(&ctx->panel);
  318. return 0;
  319. }
  320. static const struct of_device_id st7789v_of_match[] = {
  321. { .compatible = "sitronix,st7789v" },
  322. { }
  323. };
  324. MODULE_DEVICE_TABLE(of, st7789v_of_match);
  325. static struct spi_driver st7789v_driver = {
  326. .probe = st7789v_probe,
  327. .remove = st7789v_remove,
  328. .driver = {
  329. .name = "st7789v",
  330. .of_match_table = st7789v_of_match,
  331. },
  332. };
  333. module_spi_driver(st7789v_driver);
  334. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  335. MODULE_DESCRIPTION("Sitronix st7789v LCD Driver");
  336. MODULE_LICENSE("GPL v2");