xref: /linux/drivers/gpu/drm/panel/panel-jdi-fhd-r63452.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
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 	bool prepared;
25 };
26 
27 static inline struct jdi_fhd_r63452 *to_jdi_fhd_r63452(struct drm_panel *panel)
28 {
29 	return container_of(panel, struct jdi_fhd_r63452, panel);
30 }
31 
32 static void jdi_fhd_r63452_reset(struct jdi_fhd_r63452 *ctx)
33 {
34 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
35 	usleep_range(10000, 11000);
36 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
37 	usleep_range(1000, 2000);
38 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
39 	usleep_range(10000, 11000);
40 }
41 
42 static int jdi_fhd_r63452_on(struct jdi_fhd_r63452 *ctx)
43 {
44 	struct mipi_dsi_device *dsi = ctx->dsi;
45 	struct device *dev = &dsi->dev;
46 	int ret;
47 
48 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
49 
50 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x00);
51 	mipi_dsi_generic_write_seq(dsi, 0xd6, 0x01);
52 	mipi_dsi_generic_write_seq(dsi, 0xec,
53 				   0x64, 0xdc, 0xec, 0x3b, 0x52, 0x00, 0x0b, 0x0b,
54 				   0x13, 0x15, 0x68, 0x0b, 0xb5);
55 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x03);
56 
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 
63 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
64 
65 	ret = mipi_dsi_dcs_set_pixel_format(dsi, 0x77);
66 	if (ret < 0) {
67 		dev_err(dev, "Failed to set pixel format: %d\n", ret);
68 		return ret;
69 	}
70 
71 	ret = mipi_dsi_dcs_set_column_address(dsi, 0x0000, 0x0437);
72 	if (ret < 0) {
73 		dev_err(dev, "Failed to set column address: %d\n", ret);
74 		return ret;
75 	}
76 
77 	ret = mipi_dsi_dcs_set_page_address(dsi, 0x0000, 0x077f);
78 	if (ret < 0) {
79 		dev_err(dev, "Failed to set page address: %d\n", ret);
80 		return ret;
81 	}
82 
83 	ret = mipi_dsi_dcs_set_tear_scanline(dsi, 0x0000);
84 	if (ret < 0) {
85 		dev_err(dev, "Failed to set tear scanline: %d\n", ret);
86 		return ret;
87 	}
88 
89 	ret = mipi_dsi_dcs_set_display_brightness(dsi, 0x00ff);
90 	if (ret < 0) {
91 		dev_err(dev, "Failed to set display brightness: %d\n", ret);
92 		return ret;
93 	}
94 
95 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
96 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
97 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_CABC_MIN_BRIGHTNESS, 0x00);
98 	mipi_dsi_dcs_write_seq(dsi, 0x84, 0x00);
99 
100 	ret = mipi_dsi_dcs_set_display_on(dsi);
101 	if (ret < 0) {
102 		dev_err(dev, "Failed to set display on: %d\n", ret);
103 		return ret;
104 	}
105 	msleep(20);
106 
107 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
108 	if (ret < 0) {
109 		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
110 		return ret;
111 	}
112 	msleep(80);
113 
114 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x04);
115 	mipi_dsi_dcs_write_seq(dsi, 0x84, 0x00);
116 	mipi_dsi_generic_write_seq(dsi, 0xc8, 0x11);
117 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x03);
118 
119 	return 0;
120 }
121 
122 static int jdi_fhd_r63452_off(struct jdi_fhd_r63452 *ctx)
123 {
124 	struct mipi_dsi_device *dsi = ctx->dsi;
125 	struct device *dev = &dsi->dev;
126 	int ret;
127 
128 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
129 
130 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x00);
131 	mipi_dsi_generic_write_seq(dsi, 0xd6, 0x01);
132 	mipi_dsi_generic_write_seq(dsi, 0xec,
133 				   0x64, 0xdc, 0xec, 0x3b, 0x52, 0x00, 0x0b, 0x0b,
134 				   0x13, 0x15, 0x68, 0x0b, 0x95);
135 	mipi_dsi_generic_write_seq(dsi, 0xb0, 0x03);
136 
137 	ret = mipi_dsi_dcs_set_display_off(dsi);
138 	if (ret < 0) {
139 		dev_err(dev, "Failed to set display off: %d\n", ret);
140 		return ret;
141 	}
142 	usleep_range(2000, 3000);
143 
144 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
145 	if (ret < 0) {
146 		dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
147 		return ret;
148 	}
149 	msleep(120);
150 
151 	return 0;
152 }
153 
154 static int jdi_fhd_r63452_prepare(struct drm_panel *panel)
155 {
156 	struct jdi_fhd_r63452 *ctx = to_jdi_fhd_r63452(panel);
157 	struct device *dev = &ctx->dsi->dev;
158 	int ret;
159 
160 	if (ctx->prepared)
161 		return 0;
162 
163 	jdi_fhd_r63452_reset(ctx);
164 
165 	ret = jdi_fhd_r63452_on(ctx);
166 	if (ret < 0) {
167 		dev_err(dev, "Failed to initialize panel: %d\n", ret);
168 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
169 		return ret;
170 	}
171 
172 	ctx->prepared = true;
173 	return 0;
174 }
175 
176 static int jdi_fhd_r63452_unprepare(struct drm_panel *panel)
177 {
178 	struct jdi_fhd_r63452 *ctx = to_jdi_fhd_r63452(panel);
179 	struct device *dev = &ctx->dsi->dev;
180 	int ret;
181 
182 	if (!ctx->prepared)
183 		return 0;
184 
185 	ret = jdi_fhd_r63452_off(ctx);
186 	if (ret < 0)
187 		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
188 
189 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
190 
191 	ctx->prepared = false;
192 	return 0;
193 }
194 
195 static const struct drm_display_mode jdi_fhd_r63452_mode = {
196 	.clock = (1080 + 120 + 16 + 40) * (1920 + 4 + 2 + 4) * 60 / 1000,
197 	.hdisplay = 1080,
198 	.hsync_start = 1080 + 120,
199 	.hsync_end = 1080 + 120 + 16,
200 	.htotal = 1080 + 120 + 16 + 40,
201 	.vdisplay = 1920,
202 	.vsync_start = 1920 + 4,
203 	.vsync_end = 1920 + 4 + 2,
204 	.vtotal = 1920 + 4 + 2 + 4,
205 	.width_mm = 64,
206 	.height_mm = 114,
207 };
208 
209 static int jdi_fhd_r63452_get_modes(struct drm_panel *panel,
210 				    struct drm_connector *connector)
211 {
212 	struct drm_display_mode *mode;
213 
214 	mode = drm_mode_duplicate(connector->dev, &jdi_fhd_r63452_mode);
215 	if (!mode)
216 		return -ENOMEM;
217 
218 	drm_mode_set_name(mode);
219 
220 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
221 	connector->display_info.width_mm = mode->width_mm;
222 	connector->display_info.height_mm = mode->height_mm;
223 	drm_mode_probed_add(connector, mode);
224 
225 	return 1;
226 }
227 
228 static const struct drm_panel_funcs jdi_fhd_r63452_panel_funcs = {
229 	.prepare = jdi_fhd_r63452_prepare,
230 	.unprepare = jdi_fhd_r63452_unprepare,
231 	.get_modes = jdi_fhd_r63452_get_modes,
232 };
233 
234 static int jdi_fhd_r63452_probe(struct mipi_dsi_device *dsi)
235 {
236 	struct device *dev = &dsi->dev;
237 	struct jdi_fhd_r63452 *ctx;
238 	int ret;
239 
240 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
241 	if (!ctx)
242 		return -ENOMEM;
243 
244 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
245 	if (IS_ERR(ctx->reset_gpio))
246 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
247 				     "Failed to get reset-gpios\n");
248 
249 	ctx->dsi = dsi;
250 	mipi_dsi_set_drvdata(dsi, ctx);
251 
252 	dsi->lanes = 4;
253 	dsi->format = MIPI_DSI_FMT_RGB888;
254 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
255 			  MIPI_DSI_CLOCK_NON_CONTINUOUS;
256 
257 	drm_panel_init(&ctx->panel, dev, &jdi_fhd_r63452_panel_funcs,
258 		       DRM_MODE_CONNECTOR_DSI);
259 
260 	ret = drm_panel_of_backlight(&ctx->panel);
261 	if (ret)
262 		return dev_err_probe(dev, ret, "Failed to get backlight\n");
263 
264 	drm_panel_add(&ctx->panel);
265 
266 	ret = mipi_dsi_attach(dsi);
267 	if (ret < 0) {
268 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
269 		return ret;
270 	}
271 
272 	return 0;
273 }
274 
275 static void jdi_fhd_r63452_remove(struct mipi_dsi_device *dsi)
276 {
277 	struct jdi_fhd_r63452 *ctx = mipi_dsi_get_drvdata(dsi);
278 	int ret;
279 
280 	ret = mipi_dsi_detach(dsi);
281 	if (ret < 0)
282 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
283 
284 	drm_panel_remove(&ctx->panel);
285 }
286 
287 static const struct of_device_id jdi_fhd_r63452_of_match[] = {
288 	{ .compatible = "jdi,fhd-r63452" },
289 	{ /* sentinel */ }
290 };
291 MODULE_DEVICE_TABLE(of, jdi_fhd_r63452_of_match);
292 
293 static struct mipi_dsi_driver jdi_fhd_r63452_driver = {
294 	.probe = jdi_fhd_r63452_probe,
295 	.remove = jdi_fhd_r63452_remove,
296 	.driver = {
297 		.name = "panel-jdi-fhd-r63452",
298 		.of_match_table = jdi_fhd_r63452_of_match,
299 	},
300 };
301 module_mipi_dsi_driver(jdi_fhd_r63452_driver);
302 
303 MODULE_AUTHOR("Raffaele Tranquillini <raffaele.tranquillini@gmail.com>");
304 MODULE_DESCRIPTION("DRM driver for JDI FHD R63452 DSI panel, command mode");
305 MODULE_LICENSE("GPL v2");
306