panel-mantix-mlaf057we51.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Mantix MLAF057WE51 5.7" MIPI-DSI panel driver
  4. *
  5. * Copyright (C) Purism SPC 2020
  6. */
  7. #include <linux/backlight.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/media-bus-format.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <video/mipi_display.h>
  15. #include <drm/drm_mipi_dsi.h>
  16. #include <drm/drm_modes.h>
  17. #include <drm/drm_panel.h>
  18. #define DRV_NAME "panel-mantix-mlaf057we51"
  19. /* Manufacturer specific Commands send via DSI */
  20. #define MANTIX_CMD_OTP_STOP_RELOAD_MIPI 0x41
  21. #define MANTIX_CMD_INT_CANCEL 0x4c
  22. #define MANTIX_CMD_SPI_FINISH 0x90
  23. struct mantix {
  24. struct device *dev;
  25. struct drm_panel panel;
  26. struct gpio_desc *reset_gpio;
  27. struct gpio_desc *tp_rstn_gpio;
  28. struct regulator *avdd;
  29. struct regulator *avee;
  30. struct regulator *vddi;
  31. const struct drm_display_mode *default_mode;
  32. };
  33. static inline struct mantix *panel_to_mantix(struct drm_panel *panel)
  34. {
  35. return container_of(panel, struct mantix, panel);
  36. }
  37. static void mantix_init_sequence(struct mipi_dsi_multi_context *dsi_ctx)
  38. {
  39. /*
  40. * Init sequence was supplied by the panel vendor.
  41. */
  42. mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a);
  43. mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_INT_CANCEL, 0x03);
  44. mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a, 0x03);
  45. mipi_dsi_generic_write_seq_multi(dsi_ctx, 0x80, 0xa9, 0x00);
  46. mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a, 0x09);
  47. mipi_dsi_generic_write_seq_multi(dsi_ctx, 0x80, 0x64, 0x00, 0x64, 0x00, 0x00);
  48. mipi_dsi_msleep(dsi_ctx, 20);
  49. mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_SPI_FINISH, 0xa5);
  50. mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x00, 0x2f);
  51. mipi_dsi_msleep(dsi_ctx, 20);
  52. }
  53. static int mantix_enable(struct drm_panel *panel)
  54. {
  55. struct mantix *ctx = panel_to_mantix(panel);
  56. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  57. struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
  58. mantix_init_sequence(&dsi_ctx);
  59. if (!dsi_ctx.accum_err)
  60. dev_dbg(ctx->dev, "Panel init sequence done\n");
  61. mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
  62. mipi_dsi_msleep(&dsi_ctx, 20);
  63. mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
  64. mipi_dsi_usleep_range(&dsi_ctx, 10000, 12000);
  65. mipi_dsi_turn_on_peripheral_multi(&dsi_ctx);
  66. return dsi_ctx.accum_err;
  67. }
  68. static int mantix_disable(struct drm_panel *panel)
  69. {
  70. struct mantix *ctx = panel_to_mantix(panel);
  71. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  72. struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
  73. mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
  74. mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
  75. return dsi_ctx.accum_err;
  76. }
  77. static int mantix_unprepare(struct drm_panel *panel)
  78. {
  79. struct mantix *ctx = panel_to_mantix(panel);
  80. gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 1);
  81. usleep_range(5000, 6000);
  82. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  83. regulator_disable(ctx->avee);
  84. regulator_disable(ctx->avdd);
  85. /* T11 */
  86. usleep_range(5000, 6000);
  87. regulator_disable(ctx->vddi);
  88. /* T14 */
  89. msleep(50);
  90. return 0;
  91. }
  92. static int mantix_prepare(struct drm_panel *panel)
  93. {
  94. struct mantix *ctx = panel_to_mantix(panel);
  95. int ret;
  96. /* Focaltech FT8006P, section 7.3.1 and 7.3.4 */
  97. dev_dbg(ctx->dev, "Resetting the panel\n");
  98. ret = regulator_enable(ctx->vddi);
  99. if (ret < 0) {
  100. dev_err(ctx->dev, "Failed to enable vddi supply: %d\n", ret);
  101. return ret;
  102. }
  103. /* T1 + T2 */
  104. usleep_range(8000, 10000);
  105. ret = regulator_enable(ctx->avdd);
  106. if (ret < 0) {
  107. dev_err(ctx->dev, "Failed to enable avdd supply: %d\n", ret);
  108. return ret;
  109. }
  110. /* T2d */
  111. usleep_range(3500, 4000);
  112. ret = regulator_enable(ctx->avee);
  113. if (ret < 0) {
  114. dev_err(ctx->dev, "Failed to enable avee supply: %d\n", ret);
  115. return ret;
  116. }
  117. /* T3 + T4 + time for voltage to become stable: */
  118. usleep_range(6000, 7000);
  119. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  120. gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 0);
  121. /* T6 */
  122. msleep(50);
  123. return 0;
  124. }
  125. static const struct drm_display_mode default_mode_mantix = {
  126. .hdisplay = 720,
  127. .hsync_start = 720 + 45,
  128. .hsync_end = 720 + 45 + 14,
  129. .htotal = 720 + 45 + 14 + 25,
  130. .vdisplay = 1440,
  131. .vsync_start = 1440 + 130,
  132. .vsync_end = 1440 + 130 + 8,
  133. .vtotal = 1440 + 130 + 8 + 106,
  134. .clock = 85298,
  135. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  136. .width_mm = 65,
  137. .height_mm = 130,
  138. };
  139. static const struct drm_display_mode default_mode_ys = {
  140. .hdisplay = 720,
  141. .hsync_start = 720 + 45,
  142. .hsync_end = 720 + 45 + 14,
  143. .htotal = 720 + 45 + 14 + 25,
  144. .vdisplay = 1440,
  145. .vsync_start = 1440 + 175,
  146. .vsync_end = 1440 + 175 + 8,
  147. .vtotal = 1440 + 175 + 8 + 50,
  148. .clock = 85298,
  149. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  150. .width_mm = 65,
  151. .height_mm = 130,
  152. };
  153. static const u32 mantix_bus_formats[] = {
  154. MEDIA_BUS_FMT_RGB888_1X24,
  155. };
  156. static int mantix_get_modes(struct drm_panel *panel,
  157. struct drm_connector *connector)
  158. {
  159. struct mantix *ctx = panel_to_mantix(panel);
  160. struct drm_display_mode *mode;
  161. mode = drm_mode_duplicate(connector->dev, ctx->default_mode);
  162. if (!mode) {
  163. dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
  164. ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
  165. drm_mode_vrefresh(ctx->default_mode));
  166. return -ENOMEM;
  167. }
  168. drm_mode_set_name(mode);
  169. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  170. connector->display_info.width_mm = mode->width_mm;
  171. connector->display_info.height_mm = mode->height_mm;
  172. drm_mode_probed_add(connector, mode);
  173. drm_display_info_set_bus_formats(&connector->display_info,
  174. mantix_bus_formats,
  175. ARRAY_SIZE(mantix_bus_formats));
  176. return 1;
  177. }
  178. static const struct drm_panel_funcs mantix_drm_funcs = {
  179. .disable = mantix_disable,
  180. .unprepare = mantix_unprepare,
  181. .prepare = mantix_prepare,
  182. .enable = mantix_enable,
  183. .get_modes = mantix_get_modes,
  184. };
  185. static int mantix_probe(struct mipi_dsi_device *dsi)
  186. {
  187. struct device *dev = &dsi->dev;
  188. struct mantix *ctx;
  189. int ret;
  190. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  191. if (!ctx)
  192. return -ENOMEM;
  193. ctx->default_mode = of_device_get_match_data(dev);
  194. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  195. if (IS_ERR(ctx->reset_gpio)) {
  196. dev_err(dev, "cannot get reset gpio\n");
  197. return PTR_ERR(ctx->reset_gpio);
  198. }
  199. ctx->tp_rstn_gpio = devm_gpiod_get(dev, "mantix,tp-rstn", GPIOD_OUT_HIGH);
  200. if (IS_ERR(ctx->tp_rstn_gpio)) {
  201. dev_err(dev, "cannot get tp-rstn gpio\n");
  202. return PTR_ERR(ctx->tp_rstn_gpio);
  203. }
  204. mipi_dsi_set_drvdata(dsi, ctx);
  205. ctx->dev = dev;
  206. dsi->lanes = 4;
  207. dsi->format = MIPI_DSI_FMT_RGB888;
  208. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  209. MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
  210. ctx->avdd = devm_regulator_get(dev, "avdd");
  211. if (IS_ERR(ctx->avdd))
  212. return dev_err_probe(dev, PTR_ERR(ctx->avdd), "Failed to request avdd regulator\n");
  213. ctx->avee = devm_regulator_get(dev, "avee");
  214. if (IS_ERR(ctx->avee))
  215. return dev_err_probe(dev, PTR_ERR(ctx->avee), "Failed to request avee regulator\n");
  216. ctx->vddi = devm_regulator_get(dev, "vddi");
  217. if (IS_ERR(ctx->vddi))
  218. return dev_err_probe(dev, PTR_ERR(ctx->vddi), "Failed to request vddi regulator\n");
  219. drm_panel_init(&ctx->panel, dev, &mantix_drm_funcs,
  220. DRM_MODE_CONNECTOR_DSI);
  221. ret = drm_panel_of_backlight(&ctx->panel);
  222. if (ret)
  223. return ret;
  224. drm_panel_add(&ctx->panel);
  225. ret = mipi_dsi_attach(dsi);
  226. if (ret < 0) {
  227. dev_err(dev, "mipi_dsi_attach failed (%d). Is host ready?\n", ret);
  228. drm_panel_remove(&ctx->panel);
  229. return ret;
  230. }
  231. dev_info(dev, "%ux%u@%u %ubpp dsi %udl - ready\n",
  232. ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
  233. drm_mode_vrefresh(ctx->default_mode),
  234. mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
  235. return 0;
  236. }
  237. static void mantix_shutdown(struct mipi_dsi_device *dsi)
  238. {
  239. struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
  240. drm_panel_unprepare(&ctx->panel);
  241. drm_panel_disable(&ctx->panel);
  242. }
  243. static void mantix_remove(struct mipi_dsi_device *dsi)
  244. {
  245. struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
  246. mantix_shutdown(dsi);
  247. mipi_dsi_detach(dsi);
  248. drm_panel_remove(&ctx->panel);
  249. }
  250. static const struct of_device_id mantix_of_match[] = {
  251. { .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
  252. { .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
  253. { /* sentinel */ }
  254. };
  255. MODULE_DEVICE_TABLE(of, mantix_of_match);
  256. static struct mipi_dsi_driver mantix_driver = {
  257. .probe = mantix_probe,
  258. .remove = mantix_remove,
  259. .shutdown = mantix_shutdown,
  260. .driver = {
  261. .name = DRV_NAME,
  262. .of_match_table = mantix_of_match,
  263. },
  264. };
  265. module_mipi_dsi_driver(mantix_driver);
  266. MODULE_AUTHOR("Guido Günther <agx@sigxcpu.org>");
  267. MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
  268. MODULE_LICENSE("GPL v2");