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_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
238 if (!ctx)
239 return -ENOMEM;
240 ctx->default_mode = of_device_get_match_data(dev);
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 ctx->default_mode->hdisplay, ctx->default_mode->vdisplay,
292 drm_mode_vrefresh(ctx->default_mode),
293 mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes);
294
295 return 0;
296 }
297
mantix_shutdown(struct mipi_dsi_device * dsi)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
mantix_remove(struct mipi_dsi_device * dsi)306 static void 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
316 static const struct of_device_id mantix_of_match[] = {
317 { .compatible = "mantix,mlaf057we51-x", .data = &default_mode_mantix },
318 { .compatible = "ys,ys57pss36bh5gq", .data = &default_mode_ys },
319 { /* sentinel */ }
320 };
321 MODULE_DEVICE_TABLE(of, mantix_of_match);
322
323 static struct mipi_dsi_driver mantix_driver = {
324 .probe = mantix_probe,
325 .remove = mantix_remove,
326 .shutdown = mantix_shutdown,
327 .driver = {
328 .name = DRV_NAME,
329 .of_match_table = mantix_of_match,
330 },
331 };
332 module_mipi_dsi_driver(mantix_driver);
333
334 MODULE_AUTHOR("Guido Günther <agx@sigxcpu.org>");
335 MODULE_DESCRIPTION("DRM driver for Mantix MLAF057WE51-X MIPI DSI panel");
336 MODULE_LICENSE("GPL v2");
337