panel-visionox-rm69299.c 7.1 KB

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