panel-jdi-fhd-r63452.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021 Raffaele Tranquillini <[email protected]>
  4. *
  5. * Generated using linux-mdss-dsi-panel-driver-generator from Lineage OS device tree:
  6. * https://github.com/LineageOS/android_kernel_xiaomi_msm8996/blob/lineage-18.1/arch/arm/boot/dts/qcom/a1-msm8996-mtp.dtsi
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <video/mipi_display.h>
  13. #include <drm/drm_mipi_dsi.h>
  14. #include <drm/drm_modes.h>
  15. #include <drm/drm_panel.h>
  16. struct jdi_fhd_r63452 {
  17. struct drm_panel panel;
  18. struct mipi_dsi_device *dsi;
  19. struct gpio_desc *reset_gpio;
  20. bool prepared;
  21. };
  22. static inline struct jdi_fhd_r63452 *to_jdi_fhd_r63452(struct drm_panel *panel)
  23. {
  24. return container_of(panel, struct jdi_fhd_r63452, panel);
  25. }
  26. #define dsi_generic_write_seq(dsi, seq...) do { \
  27. static const u8 d[] = { seq }; \
  28. int ret; \
  29. ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d)); \
  30. if (ret < 0) \
  31. return ret; \
  32. } while (0)
  33. #define dsi_dcs_write_seq(dsi, seq...) do { \
  34. static const u8 d[] = { seq }; \
  35. int ret; \
  36. ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
  37. if (ret < 0) \
  38. return ret; \
  39. } while (0)
  40. static void jdi_fhd_r63452_reset(struct jdi_fhd_r63452 *ctx)
  41. {
  42. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  43. usleep_range(10000, 11000);
  44. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  45. usleep_range(1000, 2000);
  46. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  47. usleep_range(10000, 11000);
  48. }
  49. static int jdi_fhd_r63452_on(struct jdi_fhd_r63452 *ctx)
  50. {
  51. struct mipi_dsi_device *dsi = ctx->dsi;
  52. struct device *dev = &dsi->dev;
  53. int ret;
  54. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  55. dsi_generic_write_seq(dsi, 0xb0, 0x00);
  56. dsi_generic_write_seq(dsi, 0xd6, 0x01);
  57. dsi_generic_write_seq(dsi, 0xec,
  58. 0x64, 0xdc, 0xec, 0x3b, 0x52, 0x00, 0x0b, 0x0b,
  59. 0x13, 0x15, 0x68, 0x0b, 0xb5);
  60. dsi_generic_write_seq(dsi, 0xb0, 0x03);
  61. ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
  62. if (ret < 0) {
  63. dev_err(dev, "Failed to set tear on: %d\n", ret);
  64. return ret;
  65. }
  66. dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
  67. ret = mipi_dsi_dcs_set_pixel_format(dsi, 0x77);
  68. if (ret < 0) {
  69. dev_err(dev, "Failed to set pixel format: %d\n", ret);
  70. return ret;
  71. }
  72. ret = mipi_dsi_dcs_set_column_address(dsi, 0x0000, 0x0437);
  73. if (ret < 0) {
  74. dev_err(dev, "Failed to set column address: %d\n", ret);
  75. return ret;
  76. }
  77. ret = mipi_dsi_dcs_set_page_address(dsi, 0x0000, 0x077f);
  78. if (ret < 0) {
  79. dev_err(dev, "Failed to set page address: %d\n", ret);
  80. return ret;
  81. }
  82. ret = mipi_dsi_dcs_set_tear_scanline(dsi, 0x0000);
  83. if (ret < 0) {
  84. dev_err(dev, "Failed to set tear scanline: %d\n", ret);
  85. return ret;
  86. }
  87. ret = mipi_dsi_dcs_set_display_brightness(dsi, 0x00ff);
  88. if (ret < 0) {
  89. dev_err(dev, "Failed to set display brightness: %d\n", ret);
  90. return ret;
  91. }
  92. dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
  93. dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
  94. dsi_dcs_write_seq(dsi, MIPI_DCS_SET_CABC_MIN_BRIGHTNESS, 0x00);
  95. dsi_dcs_write_seq(dsi, 0x84, 0x00);
  96. ret = mipi_dsi_dcs_set_display_on(dsi);
  97. if (ret < 0) {
  98. dev_err(dev, "Failed to set display on: %d\n", ret);
  99. return ret;
  100. }
  101. msleep(20);
  102. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  103. if (ret < 0) {
  104. dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
  105. return ret;
  106. }
  107. msleep(80);
  108. dsi_generic_write_seq(dsi, 0xb0, 0x04);
  109. dsi_dcs_write_seq(dsi, 0x84, 0x00);
  110. dsi_generic_write_seq(dsi, 0xc8, 0x11);
  111. dsi_generic_write_seq(dsi, 0xb0, 0x03);
  112. return 0;
  113. }
  114. static int jdi_fhd_r63452_off(struct jdi_fhd_r63452 *ctx)
  115. {
  116. struct mipi_dsi_device *dsi = ctx->dsi;
  117. struct device *dev = &dsi->dev;
  118. int ret;
  119. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  120. dsi_generic_write_seq(dsi, 0xb0, 0x00);
  121. dsi_generic_write_seq(dsi, 0xd6, 0x01);
  122. dsi_generic_write_seq(dsi, 0xec,
  123. 0x64, 0xdc, 0xec, 0x3b, 0x52, 0x00, 0x0b, 0x0b,
  124. 0x13, 0x15, 0x68, 0x0b, 0x95);
  125. dsi_generic_write_seq(dsi, 0xb0, 0x03);
  126. ret = mipi_dsi_dcs_set_display_off(dsi);
  127. if (ret < 0) {
  128. dev_err(dev, "Failed to set display off: %d\n", ret);
  129. return ret;
  130. }
  131. usleep_range(2000, 3000);
  132. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  133. if (ret < 0) {
  134. dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
  135. return ret;
  136. }
  137. msleep(120);
  138. return 0;
  139. }
  140. static int jdi_fhd_r63452_prepare(struct drm_panel *panel)
  141. {
  142. struct jdi_fhd_r63452 *ctx = to_jdi_fhd_r63452(panel);
  143. struct device *dev = &ctx->dsi->dev;
  144. int ret;
  145. if (ctx->prepared)
  146. return 0;
  147. jdi_fhd_r63452_reset(ctx);
  148. ret = jdi_fhd_r63452_on(ctx);
  149. if (ret < 0) {
  150. dev_err(dev, "Failed to initialize panel: %d\n", ret);
  151. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  152. return ret;
  153. }
  154. ctx->prepared = true;
  155. return 0;
  156. }
  157. static int jdi_fhd_r63452_unprepare(struct drm_panel *panel)
  158. {
  159. struct jdi_fhd_r63452 *ctx = to_jdi_fhd_r63452(panel);
  160. struct device *dev = &ctx->dsi->dev;
  161. int ret;
  162. if (!ctx->prepared)
  163. return 0;
  164. ret = jdi_fhd_r63452_off(ctx);
  165. if (ret < 0)
  166. dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
  167. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  168. ctx->prepared = false;
  169. return 0;
  170. }
  171. static const struct drm_display_mode jdi_fhd_r63452_mode = {
  172. .clock = (1080 + 120 + 16 + 40) * (1920 + 4 + 2 + 4) * 60 / 1000,
  173. .hdisplay = 1080,
  174. .hsync_start = 1080 + 120,
  175. .hsync_end = 1080 + 120 + 16,
  176. .htotal = 1080 + 120 + 16 + 40,
  177. .vdisplay = 1920,
  178. .vsync_start = 1920 + 4,
  179. .vsync_end = 1920 + 4 + 2,
  180. .vtotal = 1920 + 4 + 2 + 4,
  181. .width_mm = 64,
  182. .height_mm = 114,
  183. };
  184. static int jdi_fhd_r63452_get_modes(struct drm_panel *panel,
  185. struct drm_connector *connector)
  186. {
  187. struct drm_display_mode *mode;
  188. mode = drm_mode_duplicate(connector->dev, &jdi_fhd_r63452_mode);
  189. if (!mode)
  190. return -ENOMEM;
  191. drm_mode_set_name(mode);
  192. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  193. connector->display_info.width_mm = mode->width_mm;
  194. connector->display_info.height_mm = mode->height_mm;
  195. drm_mode_probed_add(connector, mode);
  196. return 1;
  197. }
  198. static const struct drm_panel_funcs jdi_fhd_r63452_panel_funcs = {
  199. .prepare = jdi_fhd_r63452_prepare,
  200. .unprepare = jdi_fhd_r63452_unprepare,
  201. .get_modes = jdi_fhd_r63452_get_modes,
  202. };
  203. static int jdi_fhd_r63452_probe(struct mipi_dsi_device *dsi)
  204. {
  205. struct device *dev = &dsi->dev;
  206. struct jdi_fhd_r63452 *ctx;
  207. int ret;
  208. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  209. if (!ctx)
  210. return -ENOMEM;
  211. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  212. if (IS_ERR(ctx->reset_gpio))
  213. return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
  214. "Failed to get reset-gpios\n");
  215. ctx->dsi = dsi;
  216. mipi_dsi_set_drvdata(dsi, ctx);
  217. dsi->lanes = 4;
  218. dsi->format = MIPI_DSI_FMT_RGB888;
  219. dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
  220. MIPI_DSI_CLOCK_NON_CONTINUOUS;
  221. drm_panel_init(&ctx->panel, dev, &jdi_fhd_r63452_panel_funcs,
  222. DRM_MODE_CONNECTOR_DSI);
  223. ret = drm_panel_of_backlight(&ctx->panel);
  224. if (ret)
  225. return dev_err_probe(dev, ret, "Failed to get backlight\n");
  226. drm_panel_add(&ctx->panel);
  227. ret = mipi_dsi_attach(dsi);
  228. if (ret < 0) {
  229. dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. static void jdi_fhd_r63452_remove(struct mipi_dsi_device *dsi)
  235. {
  236. struct jdi_fhd_r63452 *ctx = mipi_dsi_get_drvdata(dsi);
  237. int ret;
  238. ret = mipi_dsi_detach(dsi);
  239. if (ret < 0)
  240. dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
  241. drm_panel_remove(&ctx->panel);
  242. }
  243. static const struct of_device_id jdi_fhd_r63452_of_match[] = {
  244. { .compatible = "jdi,fhd-r63452" },
  245. { /* sentinel */ }
  246. };
  247. MODULE_DEVICE_TABLE(of, jdi_fhd_r63452_of_match);
  248. static struct mipi_dsi_driver jdi_fhd_r63452_driver = {
  249. .probe = jdi_fhd_r63452_probe,
  250. .remove = jdi_fhd_r63452_remove,
  251. .driver = {
  252. .name = "panel-jdi-fhd-r63452",
  253. .of_match_table = jdi_fhd_r63452_of_match,
  254. },
  255. };
  256. module_mipi_dsi_driver(jdi_fhd_r63452_driver);
  257. MODULE_AUTHOR("Raffaele Tranquillini <[email protected]>");
  258. MODULE_DESCRIPTION("DRM driver for JDI FHD R63452 DSI panel, command mode");
  259. MODULE_LICENSE("GPL v2");