1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2021 Raffaele Tranquillini <raffaele.tranquillini@gmail.com>
4 *
5 * Generated using linux-mdss-dsi-panel-driver-generator from Lineage OS device tree:
6 * https://github.com/LineageOS/android_kernel_xiaomi_msm8996/blob/lineage-18.1/arch/arm/boot/dts/qcom/a1-msm8996-mtp.dtsi
7 */
8
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13
14 #include <video/mipi_display.h>
15
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_panel.h>
19
20 struct jdi_fhd_r63452 {
21 struct drm_panel panel;
22 struct mipi_dsi_device *dsi;
23 struct gpio_desc *reset_gpio;
24 };
25
to_jdi_fhd_r63452(struct drm_panel * panel)26 static inline struct jdi_fhd_r63452 *to_jdi_fhd_r63452(struct drm_panel *panel)
27 {
28 return container_of(panel, struct jdi_fhd_r63452, panel);
29 }
30
jdi_fhd_r63452_reset(struct jdi_fhd_r63452 * ctx)31 static void jdi_fhd_r63452_reset(struct jdi_fhd_r63452 *ctx)
32 {
33 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
34 usleep_range(10000, 11000);
35 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
36 usleep_range(1000, 2000);
37 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
38 usleep_range(10000, 11000);
39 }
40
jdi_fhd_r63452_on(struct jdi_fhd_r63452 * ctx)41 static int jdi_fhd_r63452_on(struct jdi_fhd_r63452 *ctx)
42 {
43 struct mipi_dsi_device *dsi = ctx->dsi;
44 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
45
46 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
47
48 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xb0, 0x00);
49 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xd6, 0x01);
50 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xec,
51 0x64, 0xdc, 0xec, 0x3b, 0x52, 0x00, 0x0b, 0x0b,
52 0x13, 0x15, 0x68, 0x0b, 0xb5);
53 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xb0, 0x03);
54
55 mipi_dsi_dcs_set_tear_on_multi(&dsi_ctx, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
56
57 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
58
59 mipi_dsi_dcs_set_pixel_format_multi(&dsi_ctx, 0x77);
60 mipi_dsi_dcs_set_column_address_multi(&dsi_ctx, 0x0000, 0x0437);
61 mipi_dsi_dcs_set_page_address_multi(&dsi_ctx, 0x0000, 0x077f);
62 mipi_dsi_dcs_set_tear_scanline_multi(&dsi_ctx, 0x0000);
63 mipi_dsi_dcs_set_display_brightness_multi(&dsi_ctx, 0x00ff);
64
65 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
66 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
67 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_SET_CABC_MIN_BRIGHTNESS, 0x00);
68 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x84, 0x00);
69
70 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
71 mipi_dsi_msleep(&dsi_ctx, 20);
72 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
73 mipi_dsi_msleep(&dsi_ctx, 80);
74
75 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xb0, 0x04);
76 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0x84, 0x00);
77 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xc8, 0x11);
78 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xb0, 0x03);
79
80 return dsi_ctx.accum_err;
81 }
82
jdi_fhd_r63452_off(struct jdi_fhd_r63452 * ctx)83 static void jdi_fhd_r63452_off(struct jdi_fhd_r63452 *ctx)
84 {
85 struct mipi_dsi_device *dsi = ctx->dsi;
86 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
87
88 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
89
90 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xb0, 0x00);
91 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xd6, 0x01);
92 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xec,
93 0x64, 0xdc, 0xec, 0x3b, 0x52, 0x00, 0x0b, 0x0b,
94 0x13, 0x15, 0x68, 0x0b, 0x95);
95 mipi_dsi_generic_write_seq_multi(&dsi_ctx, 0xb0, 0x03);
96
97 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
98 mipi_dsi_usleep_range(&dsi_ctx, 2000, 3000);
99 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
100 mipi_dsi_msleep(&dsi_ctx, 120);
101 }
102
jdi_fhd_r63452_prepare(struct drm_panel * panel)103 static int jdi_fhd_r63452_prepare(struct drm_panel *panel)
104 {
105 struct jdi_fhd_r63452 *ctx = to_jdi_fhd_r63452(panel);
106 int ret;
107
108 jdi_fhd_r63452_reset(ctx);
109
110 ret = jdi_fhd_r63452_on(ctx);
111 if (ret < 0)
112 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
113
114 return ret;
115 }
116
jdi_fhd_r63452_unprepare(struct drm_panel * panel)117 static int jdi_fhd_r63452_unprepare(struct drm_panel *panel)
118 {
119 struct jdi_fhd_r63452 *ctx = to_jdi_fhd_r63452(panel);
120
121 /*
122 * NOTE: We don't return an error here as while the panel won't have
123 * been cleanly turned off at least we've asserted the reset signal
124 * so it should be safe to power it back on again later
125 */
126 jdi_fhd_r63452_off(ctx);
127
128 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
129
130 return 0;
131 }
132
133 static const struct drm_display_mode jdi_fhd_r63452_mode = {
134 .clock = (1080 + 120 + 16 + 40) * (1920 + 4 + 2 + 4) * 60 / 1000,
135 .hdisplay = 1080,
136 .hsync_start = 1080 + 120,
137 .hsync_end = 1080 + 120 + 16,
138 .htotal = 1080 + 120 + 16 + 40,
139 .vdisplay = 1920,
140 .vsync_start = 1920 + 4,
141 .vsync_end = 1920 + 4 + 2,
142 .vtotal = 1920 + 4 + 2 + 4,
143 .width_mm = 64,
144 .height_mm = 114,
145 };
146
jdi_fhd_r63452_get_modes(struct drm_panel * panel,struct drm_connector * connector)147 static int jdi_fhd_r63452_get_modes(struct drm_panel *panel,
148 struct drm_connector *connector)
149 {
150 struct drm_display_mode *mode;
151
152 mode = drm_mode_duplicate(connector->dev, &jdi_fhd_r63452_mode);
153 if (!mode)
154 return -ENOMEM;
155
156 drm_mode_set_name(mode);
157
158 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
159 connector->display_info.width_mm = mode->width_mm;
160 connector->display_info.height_mm = mode->height_mm;
161 drm_mode_probed_add(connector, mode);
162
163 return 1;
164 }
165
166 static const struct drm_panel_funcs jdi_fhd_r63452_panel_funcs = {
167 .prepare = jdi_fhd_r63452_prepare,
168 .unprepare = jdi_fhd_r63452_unprepare,
169 .get_modes = jdi_fhd_r63452_get_modes,
170 };
171
jdi_fhd_r63452_probe(struct mipi_dsi_device * dsi)172 static int jdi_fhd_r63452_probe(struct mipi_dsi_device *dsi)
173 {
174 struct device *dev = &dsi->dev;
175 struct jdi_fhd_r63452 *ctx;
176 int ret;
177
178 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
179 if (!ctx)
180 return -ENOMEM;
181
182 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
183 if (IS_ERR(ctx->reset_gpio))
184 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
185 "Failed to get reset-gpios\n");
186
187 ctx->dsi = dsi;
188 mipi_dsi_set_drvdata(dsi, ctx);
189
190 dsi->lanes = 4;
191 dsi->format = MIPI_DSI_FMT_RGB888;
192 dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
193 MIPI_DSI_CLOCK_NON_CONTINUOUS;
194
195 drm_panel_init(&ctx->panel, dev, &jdi_fhd_r63452_panel_funcs,
196 DRM_MODE_CONNECTOR_DSI);
197 ctx->panel.prepare_prev_first = true;
198
199 ret = drm_panel_of_backlight(&ctx->panel);
200 if (ret)
201 return dev_err_probe(dev, ret, "Failed to get backlight\n");
202
203 drm_panel_add(&ctx->panel);
204
205 ret = mipi_dsi_attach(dsi);
206 if (ret < 0) {
207 dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
208 return ret;
209 }
210
211 return 0;
212 }
213
jdi_fhd_r63452_remove(struct mipi_dsi_device * dsi)214 static void jdi_fhd_r63452_remove(struct mipi_dsi_device *dsi)
215 {
216 struct jdi_fhd_r63452 *ctx = mipi_dsi_get_drvdata(dsi);
217 int ret;
218
219 ret = mipi_dsi_detach(dsi);
220 if (ret < 0)
221 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
222
223 drm_panel_remove(&ctx->panel);
224 }
225
226 static const struct of_device_id jdi_fhd_r63452_of_match[] = {
227 { .compatible = "jdi,fhd-r63452" },
228 { /* sentinel */ }
229 };
230 MODULE_DEVICE_TABLE(of, jdi_fhd_r63452_of_match);
231
232 static struct mipi_dsi_driver jdi_fhd_r63452_driver = {
233 .probe = jdi_fhd_r63452_probe,
234 .remove = jdi_fhd_r63452_remove,
235 .driver = {
236 .name = "panel-jdi-fhd-r63452",
237 .of_match_table = jdi_fhd_r63452_of_match,
238 },
239 };
240 module_mipi_dsi_driver(jdi_fhd_r63452_driver);
241
242 MODULE_AUTHOR("Raffaele Tranquillini <raffaele.tranquillini@gmail.com>");
243 MODULE_DESCRIPTION("DRM driver for JDI FHD R63452 DSI panel, command mode");
244 MODULE_LICENSE("GPL v2");
245