panel-raydium-rm69380.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree.
  4. * Copyright (c) 2024 David Wronek <david@mainlining.org>
  5. */
  6. #include <linux/backlight.h>
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_graph.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. #include <drm/drm_probe_helper.h>
  19. struct rm69380_panel {
  20. struct drm_panel panel;
  21. struct mipi_dsi_device *dsi[2];
  22. struct regulator_bulk_data supplies[2];
  23. struct gpio_desc *reset_gpio;
  24. };
  25. static inline
  26. struct rm69380_panel *to_rm69380_panel(struct drm_panel *panel)
  27. {
  28. return container_of(panel, struct rm69380_panel, panel);
  29. }
  30. static void rm69380_reset(struct rm69380_panel *ctx)
  31. {
  32. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  33. usleep_range(15000, 16000);
  34. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  35. usleep_range(10000, 11000);
  36. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  37. msleep(30);
  38. }
  39. static int rm69380_on(struct rm69380_panel *ctx)
  40. {
  41. struct mipi_dsi_device *dsi = ctx->dsi[0];
  42. struct device *dev = &dsi->dev;
  43. int ret;
  44. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  45. if (ctx->dsi[1])
  46. ctx->dsi[1]->mode_flags |= MIPI_DSI_MODE_LPM;
  47. mipi_dsi_dcs_write_seq(dsi, 0xfe, 0xd4);
  48. mipi_dsi_dcs_write_seq(dsi, 0x00, 0x80);
  49. mipi_dsi_dcs_write_seq(dsi, 0xfe, 0xd0);
  50. mipi_dsi_dcs_write_seq(dsi, 0x48, 0x00);
  51. mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x26);
  52. mipi_dsi_dcs_write_seq(dsi, 0x75, 0x3f);
  53. mipi_dsi_dcs_write_seq(dsi, 0x1d, 0x1a);
  54. mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00);
  55. mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x28);
  56. mipi_dsi_dcs_write_seq(dsi, 0xc2, 0x08);
  57. ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
  58. if (ret < 0) {
  59. dev_err(dev, "Failed to set tear on: %d\n", ret);
  60. return ret;
  61. }
  62. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  63. if (ret < 0) {
  64. dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
  65. return ret;
  66. }
  67. msleep(20);
  68. ret = mipi_dsi_dcs_set_display_on(dsi);
  69. if (ret < 0) {
  70. dev_err(dev, "Failed to set display on: %d\n", ret);
  71. return ret;
  72. }
  73. msleep(36);
  74. return 0;
  75. }
  76. static int rm69380_off(struct rm69380_panel *ctx)
  77. {
  78. struct mipi_dsi_device *dsi = ctx->dsi[0];
  79. struct device *dev = &dsi->dev;
  80. int ret;
  81. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  82. if (ctx->dsi[1])
  83. ctx->dsi[1]->mode_flags &= ~MIPI_DSI_MODE_LPM;
  84. ret = mipi_dsi_dcs_set_display_off(dsi);
  85. if (ret < 0) {
  86. dev_err(dev, "Failed to set display off: %d\n", ret);
  87. return ret;
  88. }
  89. msleep(35);
  90. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  91. if (ret < 0) {
  92. dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
  93. return ret;
  94. }
  95. msleep(20);
  96. return 0;
  97. }
  98. static int rm69380_prepare(struct drm_panel *panel)
  99. {
  100. struct rm69380_panel *ctx = to_rm69380_panel(panel);
  101. struct device *dev = &ctx->dsi[0]->dev;
  102. int ret;
  103. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  104. if (ret < 0) {
  105. dev_err(dev, "Failed to enable regulators: %d\n", ret);
  106. return ret;
  107. }
  108. rm69380_reset(ctx);
  109. ret = rm69380_on(ctx);
  110. if (ret < 0) {
  111. dev_err(dev, "Failed to initialize panel: %d\n", ret);
  112. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  113. regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. static int rm69380_unprepare(struct drm_panel *panel)
  119. {
  120. struct rm69380_panel *ctx = to_rm69380_panel(panel);
  121. struct device *dev = &ctx->dsi[0]->dev;
  122. int ret;
  123. ret = rm69380_off(ctx);
  124. if (ret < 0)
  125. dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
  126. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  127. regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  128. return 0;
  129. }
  130. static const struct drm_display_mode rm69380_mode = {
  131. .clock = (2560 + 32 + 12 + 38) * (1600 + 20 + 4 + 8) * 90 / 1000,
  132. .hdisplay = 2560,
  133. .hsync_start = 2560 + 32,
  134. .hsync_end = 2560 + 32 + 12,
  135. .htotal = 2560 + 32 + 12 + 38,
  136. .vdisplay = 1600,
  137. .vsync_start = 1600 + 20,
  138. .vsync_end = 1600 + 20 + 4,
  139. .vtotal = 1600 + 20 + 4 + 8,
  140. .width_mm = 248,
  141. .height_mm = 155,
  142. .type = DRM_MODE_TYPE_DRIVER,
  143. };
  144. static int rm69380_get_modes(struct drm_panel *panel,
  145. struct drm_connector *connector)
  146. {
  147. return drm_connector_helper_get_modes_fixed(connector, &rm69380_mode);
  148. }
  149. static const struct drm_panel_funcs rm69380_panel_funcs = {
  150. .prepare = rm69380_prepare,
  151. .unprepare = rm69380_unprepare,
  152. .get_modes = rm69380_get_modes,
  153. };
  154. static int rm69380_bl_update_status(struct backlight_device *bl)
  155. {
  156. struct mipi_dsi_device *dsi = bl_get_data(bl);
  157. u16 brightness = backlight_get_brightness(bl);
  158. int ret;
  159. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  160. ret = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
  161. if (ret < 0)
  162. return ret;
  163. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  164. return 0;
  165. }
  166. static int rm69380_bl_get_brightness(struct backlight_device *bl)
  167. {
  168. struct mipi_dsi_device *dsi = bl_get_data(bl);
  169. u16 brightness;
  170. int ret;
  171. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  172. ret = mipi_dsi_dcs_get_display_brightness_large(dsi, &brightness);
  173. if (ret < 0)
  174. return ret;
  175. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  176. return brightness;
  177. }
  178. static const struct backlight_ops rm69380_bl_ops = {
  179. .update_status = rm69380_bl_update_status,
  180. .get_brightness = rm69380_bl_get_brightness,
  181. };
  182. static struct backlight_device *
  183. rm69380_create_backlight(struct mipi_dsi_device *dsi)
  184. {
  185. struct device *dev = &dsi->dev;
  186. const struct backlight_properties props = {
  187. .type = BACKLIGHT_RAW,
  188. .brightness = 511,
  189. .max_brightness = 2047,
  190. };
  191. return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
  192. &rm69380_bl_ops, &props);
  193. }
  194. static int rm69380_probe(struct mipi_dsi_device *dsi)
  195. {
  196. struct mipi_dsi_host *dsi_sec_host;
  197. struct rm69380_panel *ctx;
  198. struct device *dev = &dsi->dev;
  199. struct device_node *dsi_sec;
  200. int ret, i;
  201. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  202. if (!ctx)
  203. return -ENOMEM;
  204. ctx->supplies[0].supply = "vddio";
  205. ctx->supplies[1].supply = "avdd";
  206. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
  207. ctx->supplies);
  208. if (ret < 0)
  209. return dev_err_probe(dev, ret, "Failed to get regulators\n");
  210. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  211. if (IS_ERR(ctx->reset_gpio))
  212. return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
  213. "Failed to get reset-gpios\n");
  214. dsi_sec = of_graph_get_remote_node(dsi->dev.of_node, 1, -1);
  215. if (dsi_sec) {
  216. const struct mipi_dsi_device_info info = { "RM69380 DSI1", 0,
  217. dsi_sec };
  218. dsi_sec_host = of_find_mipi_dsi_host_by_node(dsi_sec);
  219. of_node_put(dsi_sec);
  220. if (!dsi_sec_host)
  221. return dev_err_probe(dev, -EPROBE_DEFER,
  222. "Cannot get secondary DSI host\n");
  223. ctx->dsi[1] =
  224. devm_mipi_dsi_device_register_full(dev, dsi_sec_host, &info);
  225. if (IS_ERR(ctx->dsi[1]))
  226. return dev_err_probe(dev, PTR_ERR(ctx->dsi[1]),
  227. "Cannot get secondary DSI node\n");
  228. mipi_dsi_set_drvdata(ctx->dsi[1], ctx);
  229. }
  230. ctx->dsi[0] = dsi;
  231. mipi_dsi_set_drvdata(dsi, ctx);
  232. drm_panel_init(&ctx->panel, dev, &rm69380_panel_funcs,
  233. DRM_MODE_CONNECTOR_DSI);
  234. ctx->panel.prepare_prev_first = true;
  235. ctx->panel.backlight = rm69380_create_backlight(dsi);
  236. if (IS_ERR(ctx->panel.backlight))
  237. return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
  238. "Failed to create backlight\n");
  239. drm_panel_add(&ctx->panel);
  240. for (i = 0; i < ARRAY_SIZE(ctx->dsi); i++) {
  241. if (!ctx->dsi[i])
  242. continue;
  243. dev_dbg(&ctx->dsi[i]->dev, "Binding DSI %d\n", i);
  244. ctx->dsi[i]->lanes = 4;
  245. ctx->dsi[i]->format = MIPI_DSI_FMT_RGB888;
  246. ctx->dsi[i]->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
  247. MIPI_DSI_CLOCK_NON_CONTINUOUS;
  248. ret = devm_mipi_dsi_attach(dev, ctx->dsi[i]);
  249. if (ret < 0) {
  250. drm_panel_remove(&ctx->panel);
  251. return dev_err_probe(dev, ret,
  252. "Failed to attach to DSI%d\n", i);
  253. }
  254. }
  255. return 0;
  256. }
  257. static void rm69380_remove(struct mipi_dsi_device *dsi)
  258. {
  259. struct rm69380_panel *ctx = mipi_dsi_get_drvdata(dsi);
  260. drm_panel_remove(&ctx->panel);
  261. }
  262. static const struct of_device_id rm69380_of_match[] = {
  263. { .compatible = "lenovo,j716f-edo-rm69380" },
  264. { /* sentinel */ }
  265. };
  266. MODULE_DEVICE_TABLE(of, rm69380_of_match);
  267. static struct mipi_dsi_driver rm69380_panel_driver = {
  268. .probe = rm69380_probe,
  269. .remove = rm69380_remove,
  270. .driver = {
  271. .name = "panel-raydium-rm69380",
  272. .of_match_table = rm69380_of_match,
  273. },
  274. };
  275. module_mipi_dsi_driver(rm69380_panel_driver);
  276. MODULE_AUTHOR("David Wronek <david@mainlining.org");
  277. MODULE_DESCRIPTION("DRM driver for Raydium RM69380-equipped DSI panels");
  278. MODULE_LICENSE("GPL");