xref: /linux/drivers/gpu/drm/panel/panel-mantix-mlaf057we51.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Mantix MLAF057WE51 5.7" MIPI-DSI panel driver
4  *
5  * Copyright (C) Purism SPC 2020
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/media-bus-format.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <video/mipi_display.h>
17 
18 #include <drm/drm_mipi_dsi.h>
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
21 
22 #define DRV_NAME "panel-mantix-mlaf057we51"
23 
24 /* Manufacturer specific Commands send via DSI */
25 #define MANTIX_CMD_OTP_STOP_RELOAD_MIPI 0x41
26 #define MANTIX_CMD_INT_CANCEL           0x4c
27 #define MANTIX_CMD_SPI_FINISH           0x90
28 
29 struct mantix {
30 	struct device *dev;
31 	struct drm_panel panel;
32 
33 	struct gpio_desc *reset_gpio;
34 	struct gpio_desc *tp_rstn_gpio;
35 
36 	struct regulator *avdd;
37 	struct regulator *avee;
38 	struct regulator *vddi;
39 
40 	const struct drm_display_mode *default_mode;
41 };
42 
panel_to_mantix(struct drm_panel * panel)43 static inline struct mantix *panel_to_mantix(struct drm_panel *panel)
44 {
45 	return container_of(panel, struct mantix, panel);
46 }
47 
mantix_init_sequence(struct mipi_dsi_multi_context * dsi_ctx)48 static void mantix_init_sequence(struct mipi_dsi_multi_context *dsi_ctx)
49 {
50 	/*
51 	 * Init sequence was supplied by the panel vendor.
52 	 */
53 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a);
54 
55 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_INT_CANCEL, 0x03);
56 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a, 0x03);
57 	mipi_dsi_generic_write_seq_multi(dsi_ctx, 0x80, 0xa9, 0x00);
58 
59 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x5a, 0x09);
60 	mipi_dsi_generic_write_seq_multi(dsi_ctx, 0x80, 0x64, 0x00, 0x64, 0x00, 0x00);
61 	mipi_dsi_msleep(dsi_ctx, 20);
62 
63 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_SPI_FINISH, 0xa5);
64 	mipi_dsi_generic_write_seq_multi(dsi_ctx, MANTIX_CMD_OTP_STOP_RELOAD_MIPI, 0x00, 0x2f);
65 	mipi_dsi_msleep(dsi_ctx, 20);
66 }
67 
mantix_enable(struct drm_panel * panel)68 static int mantix_enable(struct drm_panel *panel)
69 {
70 	struct mantix *ctx = panel_to_mantix(panel);
71 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
72 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
73 
74 	mantix_init_sequence(&dsi_ctx);
75 	if (!dsi_ctx.accum_err)
76 		dev_dbg(ctx->dev, "Panel init sequence done\n");
77 
78 	mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
79 	mipi_dsi_msleep(&dsi_ctx, 20);
80 
81 	mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
82 	mipi_dsi_usleep_range(&dsi_ctx, 10000, 12000);
83 
84 	mipi_dsi_turn_on_peripheral_multi(&dsi_ctx);
85 
86 	return dsi_ctx.accum_err;
87 }
88 
mantix_disable(struct drm_panel * panel)89 static int mantix_disable(struct drm_panel *panel)
90 {
91 	struct mantix *ctx = panel_to_mantix(panel);
92 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
93 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
94 
95 	mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
96 	mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
97 
98 	return dsi_ctx.accum_err;
99 }
100 
mantix_unprepare(struct drm_panel * panel)101 static int mantix_unprepare(struct drm_panel *panel)
102 {
103 	struct mantix *ctx = panel_to_mantix(panel);
104 
105 	gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 1);
106 	usleep_range(5000, 6000);
107 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
108 
109 	regulator_disable(ctx->avee);
110 	regulator_disable(ctx->avdd);
111 	/* T11 */
112 	usleep_range(5000, 6000);
113 	regulator_disable(ctx->vddi);
114 	/* T14 */
115 	msleep(50);
116 
117 	return 0;
118 }
119 
mantix_prepare(struct drm_panel * panel)120 static int mantix_prepare(struct drm_panel *panel)
121 {
122 	struct mantix *ctx = panel_to_mantix(panel);
123 	int ret;
124 
125 	/* Focaltech FT8006P, section 7.3.1 and 7.3.4 */
126 	dev_dbg(ctx->dev, "Resetting the panel\n");
127 	ret = regulator_enable(ctx->vddi);
128 	if (ret < 0) {
129 		dev_err(ctx->dev, "Failed to enable vddi supply: %d\n", ret);
130 		return ret;
131 	}
132 
133 	/* T1 + T2 */
134 	usleep_range(8000, 10000);
135 
136 	ret = regulator_enable(ctx->avdd);
137 	if (ret < 0) {
138 		dev_err(ctx->dev, "Failed to enable avdd supply: %d\n", ret);
139 		return ret;
140 	}
141 
142 	/* T2d */
143 	usleep_range(3500, 4000);
144 	ret = regulator_enable(ctx->avee);
145 	if (ret < 0) {
146 		dev_err(ctx->dev, "Failed to enable avee supply: %d\n", ret);
147 		return ret;
148 	}
149 
150 	/* T3 + T4 + time for voltage to become stable: */
151 	usleep_range(6000, 7000);
152 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
153 	gpiod_set_value_cansleep(ctx->tp_rstn_gpio, 0);
154 
155 	/* T6 */
156 	msleep(50);
157 
158 	return 0;
159 }
160 
161 static const struct drm_display_mode default_mode_mantix = {
162 	.hdisplay    = 720,
163 	.hsync_start = 720 + 45,
164 	.hsync_end   = 720 + 45 + 14,
165 	.htotal	     = 720 + 45 + 14 + 25,
166 	.vdisplay    = 1440,
167 	.vsync_start = 1440 + 130,
168 	.vsync_end   = 1440 + 130 + 8,
169 	.vtotal	     = 1440 + 130 + 8 + 106,
170 	.clock	     = 85298,
171 	.flags	     = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
172 	.width_mm    = 65,
173 	.height_mm   = 130,
174 };
175 
176 static const struct drm_display_mode default_mode_ys = {
177 	.hdisplay    = 720,
178 	.hsync_start = 720 + 45,
179 	.hsync_end   = 720 + 45 + 14,
180 	.htotal	     = 720 + 45 + 14 + 25,
181 	.vdisplay    = 1440,
182 	.vsync_start = 1440 + 175,
183 	.vsync_end   = 1440 + 175 + 8,
184 	.vtotal	     = 1440 + 175 + 8 + 50,
185 	.clock	     = 85298,
186 	.flags	     = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
187 	.width_mm    = 65,
188 	.height_mm   = 130,
189 };
190 
191 static const u32 mantix_bus_formats[] = {
192 	MEDIA_BUS_FMT_RGB888_1X24,
193 };
194 
mantix_get_modes(struct drm_panel * panel,struct drm_connector * connector)195 static int mantix_get_modes(struct drm_panel *panel,
196 			    struct drm_connector *connector)
197 {
198 	struct mantix *ctx = panel_to_mantix(panel);
199 	struct drm_display_mode *mode;
200 
201 	mode = drm_mode_duplicate(connector->dev, ctx->default_mode);
202 	if (!mode) {
203 		dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
204 			ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
205 			drm_mode_vrefresh(ctx->default_mode));
206 		return -ENOMEM;
207 	}
208 
209 	drm_mode_set_name(mode);
210 
211 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
212 	connector->display_info.width_mm = mode->width_mm;
213 	connector->display_info.height_mm = mode->height_mm;
214 	drm_mode_probed_add(connector, mode);
215 
216 	drm_display_info_set_bus_formats(&connector->display_info,
217 					 mantix_bus_formats,
218 					 ARRAY_SIZE(mantix_bus_formats));
219 
220 	return 1;
221 }
222 
223 static const struct drm_panel_funcs mantix_drm_funcs = {
224 	.disable   = mantix_disable,
225 	.unprepare = mantix_unprepare,
226 	.prepare   = mantix_prepare,
227 	.enable	   = mantix_enable,
228 	.get_modes = mantix_get_modes,
229 };
230 
mantix_probe(struct mipi_dsi_device * dsi)231 static int mantix_probe(struct mipi_dsi_device *dsi)
232 {
233 	struct device *dev = &dsi->dev;
234 	struct mantix *ctx;
235 	int ret;
236 
237 	ctx = devm_drm_panel_alloc(dev, struct mantix, panel, &mantix_drm_funcs,
238 				   DRM_MODE_CONNECTOR_DSI);
239 	if (IS_ERR(ctx))
240 		return PTR_ERR(ctx);
241 
242 	ctx->default_mode = of_device_get_match_data(dev);
243 
244 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
245 	if (IS_ERR(ctx->reset_gpio)) {
246 		dev_err(dev, "cannot get reset gpio\n");
247 		return PTR_ERR(ctx->reset_gpio);
248 	}
249 
250 	ctx->tp_rstn_gpio = devm_gpiod_get(dev, "mantix,tp-rstn", GPIOD_OUT_HIGH);
251 	if (IS_ERR(ctx->tp_rstn_gpio)) {
252 		dev_err(dev, "cannot get tp-rstn gpio\n");
253 		return PTR_ERR(ctx->tp_rstn_gpio);
254 	}
255 
256 	mipi_dsi_set_drvdata(dsi, ctx);
257 	ctx->dev = dev;
258 
259 	dsi->lanes = 4;
260 	dsi->format = MIPI_DSI_FMT_RGB888;
261 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
262 		MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
263 
264 	ctx->avdd = devm_regulator_get(dev, "avdd");
265 	if (IS_ERR(ctx->avdd))
266 		return dev_err_probe(dev, PTR_ERR(ctx->avdd), "Failed to request avdd regulator\n");
267 
268 	ctx->avee = devm_regulator_get(dev, "avee");
269 	if (IS_ERR(ctx->avee))
270 		return dev_err_probe(dev, PTR_ERR(ctx->avee), "Failed to request avee regulator\n");
271 
272 	ctx->vddi = devm_regulator_get(dev, "vddi");
273 	if (IS_ERR(ctx->vddi))
274 		return dev_err_probe(dev, PTR_ERR(ctx->vddi), "Failed to request vddi regulator\n");
275 
276 	ret = drm_panel_of_backlight(&ctx->panel);
277 	if (ret)
278 		return ret;
279 
280 	drm_panel_add(&ctx->panel);
281 
282 	ret = mipi_dsi_attach(dsi);
283 	if (ret < 0) {
284 		dev_err(dev, "mipi_dsi_attach failed (%d). Is host ready?\n", ret);
285 		drm_panel_remove(&ctx->panel);
286 		return ret;
287 	}
288 
289 	dev_info(dev, "%ux%u@%u %ubpp dsi %udl - ready\n",
290 		 ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
291 		 drm_mode_vrefresh(ctx->default_mode),
292 		 mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
293 
294 	return 0;
295 }
296 
mantix_shutdown(struct mipi_dsi_device * dsi)297 static void mantix_shutdown(struct mipi_dsi_device *dsi)
298 {
299 	struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
300 
301 	drm_panel_unprepare(&ctx->panel);
302 	drm_panel_disable(&ctx->panel);
303 }
304 
mantix_remove(struct mipi_dsi_device * dsi)305 static void mantix_remove(struct mipi_dsi_device *dsi)
306 {
307 	struct mantix *ctx = mipi_dsi_get_drvdata(dsi);
308 
309 	mantix_shutdown(dsi);
310 
311 	mipi_dsi_detach(dsi);
312 	drm_panel_remove(&ctx->panel);
313 }
314 
315 static const struct of_device_id mantix_of_match[] = {
316 	{ .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
317 	{ .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
318 	{ /* sentinel */ }
319 };
320 MODULE_DEVICE_TABLE(of, mantix_of_match);
321 
322 static struct mipi_dsi_driver mantix_driver = {
323 	.probe	= mantix_probe,
324 	.remove = mantix_remove,
325 	.shutdown = mantix_shutdown,
326 	.driver = {
327 		.name = DRV_NAME,
328 		.of_match_table = mantix_of_match,
329 	},
330 };
331 module_mipi_dsi_driver(mantix_driver);
332 
333 MODULE_AUTHOR("Guido Günther <agx@sigxcpu.org>");
334 MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
335 MODULE_LICENSE("GPL v2");
336