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