1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree.
7 #include <linux/backlight.h>
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_graph.h>
14 #include <linux/regulator/consumer.h>
16 #include <video/mipi_display.h>
18 #include <drm/drm_mipi_dsi.h>
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
21 #include <drm/drm_probe_helper.h>
23 struct rm69380_panel {
24 struct drm_panel panel;
25 struct mipi_dsi_device *dsi[2];
26 struct regulator_bulk_data supplies[2];
27 struct gpio_desc *reset_gpio;
31 struct rm69380_panel *to_rm69380_panel(struct drm_panel *panel)
33 return container_of(panel, struct rm69380_panel, panel);
36 static void rm69380_reset(struct rm69380_panel *ctx)
38 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
39 usleep_range(15000, 16000);
40 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
41 usleep_range(10000, 11000);
42 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
46 static int rm69380_on(struct rm69380_panel *ctx)
48 struct mipi_dsi_device *dsi = ctx->dsi[0];
49 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
51 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
53 ctx->dsi[1]->mode_flags |= MIPI_DSI_MODE_LPM;
55 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xfe, 0xd4);
56 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x00, 0x80);
57 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xfe, 0xd0);
58 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x48, 0x00);
59 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xfe, 0x26);
60 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x75, 0x3f);
61 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x1d, 0x1a);
62 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xfe, 0x00);
63 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x28);
64 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xc2, 0x08);
66 mipi_dsi_dcs_set_tear_on_multi(&dsi_ctx, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
67 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
68 mipi_dsi_msleep(&dsi_ctx, 20);
70 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
71 mipi_dsi_msleep(&dsi_ctx, 36);
73 return dsi_ctx.accum_err;
76 static void rm69380_off(struct rm69380_panel *ctx)
78 struct mipi_dsi_device *dsi = ctx->dsi[0];
79 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
81 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
83 ctx->dsi[1]->mode_flags &= ~MIPI_DSI_MODE_LPM;
85 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
86 mipi_dsi_msleep(&dsi_ctx, 35);
87 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
88 mipi_dsi_msleep(&dsi_ctx, 20);
91 static int rm69380_prepare(struct drm_panel *panel)
93 struct rm69380_panel *ctx = to_rm69380_panel(panel);
96 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
102 ret = rm69380_on(ctx);
104 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
105 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
111 static int rm69380_unprepare(struct drm_panel *panel)
113 struct rm69380_panel *ctx = to_rm69380_panel(panel);
117 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
118 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
123 static const struct drm_display_mode rm69380_mode = {
124 .clock = (2560 + 32 + 12 + 38) * (1600 + 20 + 4 + 8) * 90 / 1000,
126 .hsync_start = 2560 + 32,
127 .hsync_end = 2560 + 32 + 12,
128 .htotal = 2560 + 32 + 12 + 38,
130 .vsync_start = 1600 + 20,
131 .vsync_end = 1600 + 20 + 4,
132 .vtotal = 1600 + 20 + 4 + 8,
135 .type = DRM_MODE_TYPE_DRIVER,
138 static int rm69380_get_modes(struct drm_panel *panel,
139 struct drm_connector *connector)
141 return drm_connector_helper_get_modes_fixed(connector, &rm69380_mode);
144 static const struct drm_panel_funcs rm69380_panel_funcs = {
145 .prepare = rm69380_prepare,
146 .unprepare = rm69380_unprepare,
147 .get_modes = rm69380_get_modes,
150 static int rm69380_bl_update_status(struct backlight_device *bl)
152 struct mipi_dsi_device *dsi = bl_get_data(bl);
153 u16 brightness = backlight_get_brightness(bl);
156 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
158 ret = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
162 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
167 static int rm69380_bl_get_brightness(struct backlight_device *bl)
169 struct mipi_dsi_device *dsi = bl_get_data(bl);
173 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
175 ret = mipi_dsi_dcs_get_display_brightness_large(dsi, &brightness);
179 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
184 static const struct backlight_ops rm69380_bl_ops = {
185 .update_status = rm69380_bl_update_status,
186 .get_brightness = rm69380_bl_get_brightness,
189 static struct backlight_device *
190 rm69380_create_backlight(struct mipi_dsi_device *dsi)
192 struct device *dev = &dsi->dev;
193 const struct backlight_properties props = {
194 .type = BACKLIGHT_RAW,
196 .max_brightness = 2047,
199 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
200 &rm69380_bl_ops, &props);
203 static int rm69380_probe(struct mipi_dsi_device *dsi)
205 struct mipi_dsi_host *dsi_sec_host;
206 struct rm69380_panel *ctx;
207 struct device *dev = &dsi->dev;
208 struct device_node *dsi_sec;
211 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
215 ctx->supplies[0].supply = "vddio";
216 ctx->supplies[1].supply = "avdd";
217 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
220 return dev_err_probe(dev, ret, "Failed to get regulators\n");
222 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
223 if (IS_ERR(ctx->reset_gpio))
224 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
225 "Failed to get reset-gpios\n");
227 dsi_sec = of_graph_get_remote_node(dsi->dev.of_node, 1, -1);
230 const struct mipi_dsi_device_info info = { "RM69380 DSI1", 0,
233 dsi_sec_host = of_find_mipi_dsi_host_by_node(dsi_sec);
234 of_node_put(dsi_sec);
236 return dev_err_probe(dev, -EPROBE_DEFER,
237 "Cannot get secondary DSI host\n");
240 devm_mipi_dsi_device_register_full(dev, dsi_sec_host, &info);
241 if (IS_ERR(ctx->dsi[1]))
242 return dev_err_probe(dev, PTR_ERR(ctx->dsi[1]),
243 "Cannot get secondary DSI node\n");
245 mipi_dsi_set_drvdata(ctx->dsi[1], ctx);
249 mipi_dsi_set_drvdata(dsi, ctx);
251 drm_panel_init(&ctx->panel, dev, &rm69380_panel_funcs,
252 DRM_MODE_CONNECTOR_DSI);
253 ctx->panel.prepare_prev_first = true;
255 ctx->panel.backlight = rm69380_create_backlight(dsi);
256 if (IS_ERR(ctx->panel.backlight))
257 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
258 "Failed to create backlight\n");
260 drm_panel_add(&ctx->panel);
262 for (i = 0; i < ARRAY_SIZE(ctx->dsi); i++) {
266 dev_dbg(&ctx->dsi[i]->dev, "Binding DSI %d\n", i);
268 ctx->dsi[i]->lanes = 4;
269 ctx->dsi[i]->format = MIPI_DSI_FMT_RGB888;
270 ctx->dsi[i]->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
271 MIPI_DSI_CLOCK_NON_CONTINUOUS;
273 ret = devm_mipi_dsi_attach(dev, ctx->dsi[i]);
275 drm_panel_remove(&ctx->panel);
276 return dev_err_probe(dev, ret,
277 "Failed to attach to DSI%d\n", i);
284 static void rm69380_remove(struct mipi_dsi_device *dsi)
286 struct rm69380_panel *ctx = mipi_dsi_get_drvdata(dsi);
288 drm_panel_remove(&ctx->panel);
291 static const struct of_device_id rm69380_of_match[] = {
292 { .compatible = "lenovo,j716f-edo-rm69380" },
295 MODULE_DEVICE_TABLE(of, rm69380_of_match);
297 static struct mipi_dsi_driver rm69380_panel_driver = {
298 .probe = rm69380_probe,
299 .remove = rm69380_remove,
301 .name = "panel-raydium-rm69380",
302 .of_match_table = rm69380_of_match,
305 module_mipi_dsi_driver(rm69380_panel_driver);
308 MODULE_DESCRIPTION("DRM driver for Raydium RM69380-equipped DSI panels");
309 MODULE_LICENSE("GPL");