panel-visionox-rm69299.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/module.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <video/mipi_display.h>
  11. #include <drm/drm_mipi_dsi.h>
  12. #include <drm/drm_modes.h>
  13. #include <drm/drm_panel.h>
  14. struct visionox_rm69299 {
  15. struct drm_panel panel;
  16. struct regulator_bulk_data supplies[2];
  17. struct gpio_desc *reset_gpio;
  18. struct mipi_dsi_device *dsi;
  19. };
  20. static inline struct visionox_rm69299 *panel_to_ctx(struct drm_panel *panel)
  21. {
  22. return container_of(panel, struct visionox_rm69299, panel);
  23. }
  24. static int visionox_rm69299_power_on(struct visionox_rm69299 *ctx)
  25. {
  26. int ret;
  27. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  28. if (ret < 0)
  29. return ret;
  30. /*
  31. * Reset sequence of visionox panel requires the panel to be
  32. * out of reset for 10ms, followed by being held in reset
  33. * for 10ms and then out again
  34. */
  35. gpiod_set_value(ctx->reset_gpio, 1);
  36. usleep_range(10000, 20000);
  37. gpiod_set_value(ctx->reset_gpio, 0);
  38. usleep_range(10000, 20000);
  39. gpiod_set_value(ctx->reset_gpio, 1);
  40. usleep_range(10000, 20000);
  41. return 0;
  42. }
  43. static int visionox_rm69299_power_off(struct visionox_rm69299 *ctx)
  44. {
  45. gpiod_set_value(ctx->reset_gpio, 0);
  46. return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  47. }
  48. static int visionox_rm69299_unprepare(struct drm_panel *panel)
  49. {
  50. struct visionox_rm69299 *ctx = panel_to_ctx(panel);
  51. int ret;
  52. ctx->dsi->mode_flags = 0;
  53. ret = mipi_dsi_dcs_write(ctx->dsi, MIPI_DCS_SET_DISPLAY_OFF, NULL, 0);
  54. if (ret < 0)
  55. dev_err(ctx->panel.dev, "set_display_off cmd failed ret = %d\n", ret);
  56. /* 120ms delay required here as per DCS spec */
  57. msleep(120);
  58. ret = mipi_dsi_dcs_write(ctx->dsi, MIPI_DCS_ENTER_SLEEP_MODE, NULL, 0);
  59. if (ret < 0) {
  60. dev_err(ctx->panel.dev, "enter_sleep cmd failed ret = %d\n", ret);
  61. }
  62. ret = visionox_rm69299_power_off(ctx);
  63. return ret;
  64. }
  65. static int visionox_rm69299_prepare(struct drm_panel *panel)
  66. {
  67. struct visionox_rm69299 *ctx = panel_to_ctx(panel);
  68. int ret;
  69. ret = visionox_rm69299_power_on(ctx);
  70. if (ret < 0)
  71. return ret;
  72. ctx->dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  73. ret = mipi_dsi_dcs_write_buffer(ctx->dsi, (u8[]) { 0xfe, 0x00 }, 2);
  74. if (ret < 0) {
  75. dev_err(ctx->panel.dev, "cmd set tx 0 failed, ret = %d\n", ret);
  76. goto power_off;
  77. }
  78. ret = mipi_dsi_dcs_write_buffer(ctx->dsi, (u8[]) { 0xc2, 0x08 }, 2);
  79. if (ret < 0) {
  80. dev_err(ctx->panel.dev, "cmd set tx 1 failed, ret = %d\n", ret);
  81. goto power_off;
  82. }
  83. ret = mipi_dsi_dcs_write_buffer(ctx->dsi, (u8[]) { 0x35, 0x00 }, 2);
  84. if (ret < 0) {
  85. dev_err(ctx->panel.dev, "cmd set tx 2 failed, ret = %d\n", ret);
  86. goto power_off;
  87. }
  88. ret = mipi_dsi_dcs_write_buffer(ctx->dsi, (u8[]) { 0x51, 0xff }, 2);
  89. if (ret < 0) {
  90. dev_err(ctx->panel.dev, "cmd set tx 3 failed, ret = %d\n", ret);
  91. goto power_off;
  92. }
  93. ret = mipi_dsi_dcs_write(ctx->dsi, MIPI_DCS_EXIT_SLEEP_MODE, NULL, 0);
  94. if (ret < 0) {
  95. dev_err(ctx->panel.dev, "exit_sleep_mode cmd failed ret = %d\n", ret);
  96. goto power_off;
  97. }
  98. /* Per DSI spec wait 120ms after sending exit sleep DCS command */
  99. msleep(120);
  100. ret = mipi_dsi_dcs_write(ctx->dsi, MIPI_DCS_SET_DISPLAY_ON, NULL, 0);
  101. if (ret < 0) {
  102. dev_err(ctx->panel.dev, "set_display_on cmd failed ret = %d\n", ret);
  103. goto power_off;
  104. }
  105. /* Per DSI spec wait 120ms after sending set_display_on DCS command */
  106. msleep(120);
  107. return 0;
  108. power_off:
  109. return ret;
  110. }
  111. static const struct drm_display_mode visionox_rm69299_1080x2248_60hz = {
  112. .name = "1080x2248",
  113. .clock = 158695,
  114. .hdisplay = 1080,
  115. .hsync_start = 1080 + 26,
  116. .hsync_end = 1080 + 26 + 2,
  117. .htotal = 1080 + 26 + 2 + 36,
  118. .vdisplay = 2248,
  119. .vsync_start = 2248 + 56,
  120. .vsync_end = 2248 + 56 + 4,
  121. .vtotal = 2248 + 56 + 4 + 4,
  122. .flags = 0,
  123. };
  124. static int visionox_rm69299_get_modes(struct drm_panel *panel,
  125. struct drm_connector *connector)
  126. {
  127. struct visionox_rm69299 *ctx = panel_to_ctx(panel);
  128. struct drm_display_mode *mode;
  129. mode = drm_mode_duplicate(connector->dev,
  130. &visionox_rm69299_1080x2248_60hz);
  131. if (!mode) {
  132. dev_err(ctx->panel.dev, "failed to create a new display mode\n");
  133. return 0;
  134. }
  135. connector->display_info.width_mm = 74;
  136. connector->display_info.height_mm = 131;
  137. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  138. drm_mode_probed_add(connector, mode);
  139. return 1;
  140. }
  141. static const struct drm_panel_funcs visionox_rm69299_drm_funcs = {
  142. .unprepare = visionox_rm69299_unprepare,
  143. .prepare = visionox_rm69299_prepare,
  144. .get_modes = visionox_rm69299_get_modes,
  145. };
  146. static int visionox_rm69299_probe(struct mipi_dsi_device *dsi)
  147. {
  148. struct device *dev = &dsi->dev;
  149. struct visionox_rm69299 *ctx;
  150. int ret;
  151. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  152. if (!ctx)
  153. return -ENOMEM;
  154. mipi_dsi_set_drvdata(dsi, ctx);
  155. ctx->panel.dev = dev;
  156. ctx->dsi = dsi;
  157. ctx->supplies[0].supply = "vdda";
  158. ctx->supplies[0].init_load_uA = 32000;
  159. ctx->supplies[1].supply = "vdd3p3";
  160. ctx->supplies[1].init_load_uA = 13200;
  161. ret = devm_regulator_bulk_get(ctx->panel.dev, ARRAY_SIZE(ctx->supplies),
  162. ctx->supplies);
  163. if (ret < 0)
  164. return ret;
  165. ctx->reset_gpio = devm_gpiod_get(ctx->panel.dev,
  166. "reset", GPIOD_OUT_LOW);
  167. if (IS_ERR(ctx->reset_gpio)) {
  168. dev_err(dev, "cannot get reset gpio %ld\n", PTR_ERR(ctx->reset_gpio));
  169. return PTR_ERR(ctx->reset_gpio);
  170. }
  171. drm_panel_init(&ctx->panel, dev, &visionox_rm69299_drm_funcs,
  172. DRM_MODE_CONNECTOR_DSI);
  173. ctx->panel.dev = dev;
  174. ctx->panel.funcs = &visionox_rm69299_drm_funcs;
  175. drm_panel_add(&ctx->panel);
  176. dsi->lanes = 4;
  177. dsi->format = MIPI_DSI_FMT_RGB888;
  178. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_LPM |
  179. MIPI_DSI_CLOCK_NON_CONTINUOUS;
  180. ret = mipi_dsi_attach(dsi);
  181. if (ret < 0) {
  182. dev_err(dev, "dsi attach failed ret = %d\n", ret);
  183. goto err_dsi_attach;
  184. }
  185. return 0;
  186. err_dsi_attach:
  187. drm_panel_remove(&ctx->panel);
  188. return ret;
  189. }
  190. static void visionox_rm69299_remove(struct mipi_dsi_device *dsi)
  191. {
  192. struct visionox_rm69299 *ctx = mipi_dsi_get_drvdata(dsi);
  193. mipi_dsi_detach(ctx->dsi);
  194. drm_panel_remove(&ctx->panel);
  195. }
  196. static const struct of_device_id visionox_rm69299_of_match[] = {
  197. { .compatible = "visionox,rm69299-1080p-display", },
  198. { /* sentinel */ }
  199. };
  200. MODULE_DEVICE_TABLE(of, visionox_rm69299_of_match);
  201. static struct mipi_dsi_driver visionox_rm69299_driver = {
  202. .driver = {
  203. .name = "panel-visionox-rm69299",
  204. .of_match_table = visionox_rm69299_of_match,
  205. },
  206. .probe = visionox_rm69299_probe,
  207. .remove = visionox_rm69299_remove,
  208. };
  209. module_mipi_dsi_driver(visionox_rm69299_driver);
  210. MODULE_DESCRIPTION("Visionox RM69299 DSI Panel Driver");
  211. MODULE_LICENSE("GPL v2");