panel-mantix-mlaf057we51.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_device.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. #define dsi_generic_write_seq(dsi, seq...) do { \
  38. static const u8 d[] = { seq }; \
  39. int ret; \
  40. ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d)); \
  41. if (ret < 0) \
  42. return ret; \
  43. } while (0)
  44. static int mantix_init_sequence(struct mantix *ctx)
  45. {
  46. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  47. struct device *dev = ctx->dev;
  48. /*
  49. * Init sequence was supplied by the panel vendor.
  50. */
  51. dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A);
  52. dsi_generic_write_seq(dsi, MANTIX_CMD_INT_CANCEL, 0x03);
  53. dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A, 0x03);
  54. dsi_generic_write_seq(dsi, 0x80, 0xA9, 0x00);
  55. dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5A, 0x09);
  56. dsi_generic_write_seq(dsi, 0x80, 0x64, 0x00, 0x64, 0x00, 0x00);
  57. msleep(20);
  58. dsi_generic_write_seq(dsi, MANTIX_CMD_SPI_FINISH, 0xA5);
  59. dsi_generic_write_seq(dsi, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x00, 0x2F);
  60. msleep(20);
  61. dev_dbg(dev, "Panel init sequence done\n");
  62. return 0;
  63. }
  64. static int mantix_enable(struct drm_panel *panel)
  65. {
  66. struct mantix *ctx = panel_to_mantix(panel);
  67. struct device *dev = ctx->dev;
  68. struct mipi_dsi_device *dsi = to_mipi_dsi_device(dev);
  69. int ret;
  70. ret = mantix_init_sequence(ctx);
  71. if (ret < 0) {
  72. dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
  73. return ret;
  74. }
  75. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  76. if (ret < 0) {
  77. dev_err(dev, "Failed to exit sleep mode\n");
  78. return ret;
  79. }
  80. msleep(20);
  81. ret = mipi_dsi_dcs_set_display_on(dsi);
  82. if (ret)
  83. return ret;
  84. usleep_range(10000, 12000);
  85. ret = mipi_dsi_turn_on_peripheral(dsi);
  86. if (ret < 0) {
  87. dev_err(dev, "Failed to turn on peripheral\n");
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. static int mantix_disable(struct drm_panel *panel)
  93. {
  94. struct mantix *ctx = panel_to_mantix(panel);
  95. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  96. int ret;
  97. ret = mipi_dsi_dcs_set_display_off(dsi);
  98. if (ret < 0)
  99. dev_err(ctx->dev, "Failed to turn off the display: %d\n", ret);
  100. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  101. if (ret < 0)
  102. dev_err(ctx->dev, "Failed to enter sleep mode: %d\n", ret);
  103. return 0;
  104. }
  105. static int mantix_unprepare(struct drm_panel *panel)
  106. {
  107. struct mantix *ctx = panel_to_mantix(panel);
  108. gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 1);
  109. usleep_range(5000, 6000);
  110. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  111. regulator_disable(ctx->avee);
  112. regulator_disable(ctx->avdd);
  113. /* T11 */
  114. usleep_range(5000, 6000);
  115. regulator_disable(ctx->vddi);
  116. /* T14 */
  117. msleep(50);
  118. return 0;
  119. }
  120. static int mantix_prepare(struct drm_panel *panel)
  121. {
  122. struct mantix *ctx = panel_to_mantix(panel);
  123. int ret;
  124. /* Focaltech FT8006P, section 7.3.1 and 7.3.4 */
  125. dev_dbg(ctx->dev, "Resetting the panel\n");
  126. ret = regulator_enable(ctx->vddi);
  127. if (ret < 0) {
  128. dev_err(ctx->dev, "Failed to enable vddi supply: %d\n", ret);
  129. return ret;
  130. }
  131. /* T1 + T2 */
  132. usleep_range(8000, 10000);
  133. ret = regulator_enable(ctx->avdd);
  134. if (ret < 0) {
  135. dev_err(ctx->dev, "Failed to enable avdd supply: %d\n", ret);
  136. return ret;
  137. }
  138. /* T2d */
  139. usleep_range(3500, 4000);
  140. ret = regulator_enable(ctx->avee);
  141. if (ret < 0) {
  142. dev_err(ctx->dev, "Failed to enable avee supply: %d\n", ret);
  143. return ret;
  144. }
  145. /* T3 + T4 + time for voltage to become stable: */
  146. usleep_range(6000, 7000);
  147. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  148. gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 0);
  149. /* T6 */
  150. msleep(50);
  151. return 0;
  152. }
  153. static const struct drm_display_mode default_mode_mantix = {
  154. .hdisplay = 720,
  155. .hsync_start = 720 + 45,
  156. .hsync_end = 720 + 45 + 14,
  157. .htotal = 720 + 45 + 14 + 25,
  158. .vdisplay = 1440,
  159. .vsync_start = 1440 + 130,
  160. .vsync_end = 1440 + 130 + 8,
  161. .vtotal = 1440 + 130 + 8 + 106,
  162. .clock = 85298,
  163. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  164. .width_mm = 65,
  165. .height_mm = 130,
  166. };
  167. static const struct drm_display_mode default_mode_ys = {
  168. .hdisplay = 720,
  169. .hsync_start = 720 + 45,
  170. .hsync_end = 720 + 45 + 14,
  171. .htotal = 720 + 45 + 14 + 25,
  172. .vdisplay = 1440,
  173. .vsync_start = 1440 + 175,
  174. .vsync_end = 1440 + 175 + 8,
  175. .vtotal = 1440 + 175 + 8 + 50,
  176. .clock = 85298,
  177. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  178. .width_mm = 65,
  179. .height_mm = 130,
  180. };
  181. static const u32 mantix_bus_formats[] = {
  182. MEDIA_BUS_FMT_RGB888_1X24,
  183. };
  184. static int mantix_get_modes(struct drm_panel *panel,
  185. struct drm_connector *connector)
  186. {
  187. struct mantix *ctx = panel_to_mantix(panel);
  188. struct drm_display_mode *mode;
  189. mode = drm_mode_duplicate(connector->dev, ctx->default_mode);
  190. if (!mode) {
  191. dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
  192. ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
  193. drm_mode_vrefresh(ctx->default_mode));
  194. return -ENOMEM;
  195. }
  196. drm_mode_set_name(mode);
  197. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  198. connector->display_info.width_mm = mode->width_mm;
  199. connector->display_info.height_mm = mode->height_mm;
  200. drm_mode_probed_add(connector, mode);
  201. drm_display_info_set_bus_formats(&connector->display_info,
  202. mantix_bus_formats,
  203. ARRAY_SIZE(mantix_bus_formats));
  204. return 1;
  205. }
  206. static const struct drm_panel_funcs mantix_drm_funcs = {
  207. .disable = mantix_disable,
  208. .unprepare = mantix_unprepare,
  209. .prepare = mantix_prepare,
  210. .enable = mantix_enable,
  211. .get_modes = mantix_get_modes,
  212. };
  213. static int mantix_probe(struct mipi_dsi_device *dsi)
  214. {
  215. struct device *dev = &dsi->dev;
  216. struct mantix *ctx;
  217. int ret;
  218. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  219. if (!ctx)
  220. return -ENOMEM;
  221. ctx->default_mode = of_device_get_match_data(dev);
  222. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  223. if (IS_ERR(ctx->reset_gpio)) {
  224. dev_err(dev, "cannot get reset gpio\n");
  225. return PTR_ERR(ctx->reset_gpio);
  226. }
  227. ctx->tp_rstn_gpio = devm_gpiod_get(dev, "mantix,tp-rstn", GPIOD_OUT_HIGH);
  228. if (IS_ERR(ctx->tp_rstn_gpio)) {
  229. dev_err(dev, "cannot get tp-rstn gpio\n");
  230. return PTR_ERR(ctx->tp_rstn_gpio);
  231. }
  232. mipi_dsi_set_drvdata(dsi, ctx);
  233. ctx->dev = dev;
  234. dsi->lanes = 4;
  235. dsi->format = MIPI_DSI_FMT_RGB888;
  236. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  237. MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
  238. ctx->avdd = devm_regulator_get(dev, "avdd");
  239. if (IS_ERR(ctx->avdd))
  240. return dev_err_probe(dev, PTR_ERR(ctx->avdd), "Failed to request avdd regulator\n");
  241. ctx->avee = devm_regulator_get(dev, "avee");
  242. if (IS_ERR(ctx->avee))
  243. return dev_err_probe(dev, PTR_ERR(ctx->avee), "Failed to request avee regulator\n");
  244. ctx->vddi = devm_regulator_get(dev, "vddi");
  245. if (IS_ERR(ctx->vddi))
  246. return dev_err_probe(dev, PTR_ERR(ctx->vddi), "Failed to request vddi regulator\n");
  247. drm_panel_init(&ctx->panel, dev, &mantix_drm_funcs,
  248. DRM_MODE_CONNECTOR_DSI);
  249. ret = drm_panel_of_backlight(&ctx->panel);
  250. if (ret)
  251. return ret;
  252. drm_panel_add(&ctx->panel);
  253. ret = mipi_dsi_attach(dsi);
  254. if (ret < 0) {
  255. dev_err(dev, "mipi_dsi_attach failed (%d). Is host ready?\n", ret);
  256. drm_panel_remove(&ctx->panel);
  257. return ret;
  258. }
  259. dev_info(dev, "%ux%u@%u %ubpp dsi %udl - ready\n",
  260. ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
  261. drm_mode_vrefresh(ctx->default_mode),
  262. mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
  263. return 0;
  264. }
  265. static void mantix_shutdown(struct mipi_dsi_device *dsi)
  266. {
  267. struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
  268. drm_panel_unprepare(&ctx->panel);
  269. drm_panel_disable(&ctx->panel);
  270. }
  271. static void mantix_remove(struct mipi_dsi_device *dsi)
  272. {
  273. struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
  274. mantix_shutdown(dsi);
  275. mipi_dsi_detach(dsi);
  276. drm_panel_remove(&ctx->panel);
  277. }
  278. static const struct of_device_id mantix_of_match[] = {
  279. { .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
  280. { .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
  281. { /* sentinel */ }
  282. };
  283. MODULE_DEVICE_TABLE(of, mantix_of_match);
  284. static struct mipi_dsi_driver mantix_driver = {
  285. .probe = mantix_probe,
  286. .remove = mantix_remove,
  287. .shutdown = mantix_shutdown,
  288. .driver = {
  289. .name = DRV_NAME,
  290. .of_match_table = mantix_of_match,
  291. },
  292. };
  293. module_mipi_dsi_driver(mantix_driver);
  294. MODULE_AUTHOR("Guido Günther <[email protected]>");
  295. MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
  296. MODULE_LICENSE("GPL v2");