1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2023, Linaro Limited
3 
4 #include <linux/backlight.h>
5 #include <linux/delay.h>
6 #include <linux/gpio/consumer.h>
7 #include <linux/regulator/consumer.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 
11 #include <drm/display/drm_dsc.h>
12 #include <drm/drm_mipi_dsi.h>
13 #include <drm/drm_modes.h>
14 #include <drm/drm_panel.h>
15 
16 #include <video/mipi_display.h>
17 
18 struct visionox_vtdr6130 {
19 	struct drm_panel panel;
20 	struct mipi_dsi_device *dsi;
21 	struct gpio_desc *reset_gpio;
22 	struct regulator_bulk_data supplies[3];
23 	bool prepared;
24 };
25 
to_visionox_vtdr6130(struct drm_panel * panel)26 static inline struct visionox_vtdr6130 *to_visionox_vtdr6130(struct drm_panel *panel)
27 {
28 	return container_of(panel, struct visionox_vtdr6130, panel);
29 }
30 
visionox_vtdr6130_reset(struct visionox_vtdr6130 * ctx)31 static void visionox_vtdr6130_reset(struct visionox_vtdr6130 *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(10000, 11000);
37 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
38 	usleep_range(10000, 11000);
39 }
40 
visionox_vtdr6130_on(struct visionox_vtdr6130 * ctx)41 static int visionox_vtdr6130_on(struct visionox_vtdr6130 *ctx)
42 {
43 	struct mipi_dsi_device *dsi = ctx->dsi;
44 	struct device *dev = &dsi->dev;
45 	int ret;
46 
47 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
48 
49 	ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
50 	if (ret)
51 		return ret;
52 
53 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20);
54 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS, 0x00, 0x00);
55 	mipi_dsi_dcs_write_seq(dsi, 0x59, 0x09);
56 	mipi_dsi_dcs_write_seq(dsi, 0x6c, 0x01);
57 	mipi_dsi_dcs_write_seq(dsi, 0x6d, 0x00);
58 	mipi_dsi_dcs_write_seq(dsi, 0x6f, 0x01);
59 	mipi_dsi_dcs_write_seq(dsi, 0x70,
60 			       0x12, 0x00, 0x00, 0xab, 0x30, 0x80, 0x09, 0x60, 0x04,
61 			       0x38, 0x00, 0x28, 0x02, 0x1c, 0x02, 0x1c, 0x02, 0x00,
62 			       0x02, 0x0e, 0x00, 0x20, 0x03, 0xdd, 0x00, 0x07, 0x00,
63 			       0x0c, 0x02, 0x77, 0x02, 0x8b, 0x18, 0x00, 0x10, 0xf0,
64 			       0x07, 0x10, 0x20, 0x00, 0x06, 0x0f, 0x0f, 0x33, 0x0e,
65 			       0x1c, 0x2a, 0x38, 0x46, 0x54, 0x62, 0x69, 0x70, 0x77,
66 			       0x79, 0x7b, 0x7d, 0x7e, 0x02, 0x02, 0x22, 0x00, 0x2a,
67 			       0x40, 0x2a, 0xbe, 0x3a, 0xfc, 0x3a, 0xfa, 0x3a, 0xf8,
68 			       0x3b, 0x38, 0x3b, 0x78, 0x3b, 0xb6, 0x4b, 0xb6, 0x4b,
69 			       0xf4, 0x4b, 0xf4, 0x6c, 0x34, 0x84, 0x74, 0x00, 0x00,
70 			       0x00, 0x00, 0x00, 0x00);
71 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xaa, 0x10);
72 	mipi_dsi_dcs_write_seq(dsi, 0xb1,
73 			       0x01, 0x38, 0x00, 0x14, 0x00, 0x1c, 0x00, 0x01, 0x66,
74 			       0x00, 0x14, 0x00, 0x14, 0x00, 0x01, 0x66, 0x00, 0x14,
75 			       0x05, 0xcc, 0x00);
76 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xaa, 0x13);
77 	mipi_dsi_dcs_write_seq(dsi, 0xce,
78 			       0x09, 0x11, 0x09, 0x11, 0x08, 0xc1, 0x07, 0xfa, 0x05,
79 			       0xa4, 0x00, 0x3c, 0x00, 0x34, 0x00, 0x24, 0x00, 0x0c,
80 			       0x00, 0x0c, 0x04, 0x00, 0x35);
81 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xaa, 0x14);
82 	mipi_dsi_dcs_write_seq(dsi, 0xb2, 0x03, 0x33);
83 	mipi_dsi_dcs_write_seq(dsi, 0xb4,
84 			       0x00, 0x33, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00,
85 			       0x3e, 0x00, 0x00);
86 	mipi_dsi_dcs_write_seq(dsi, 0xb5,
87 			       0x00, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x06, 0x01);
88 	mipi_dsi_dcs_write_seq(dsi, 0xb9, 0x00, 0x00, 0x08, 0x09, 0x09, 0x09);
89 	mipi_dsi_dcs_write_seq(dsi, 0xbc,
90 			       0x10, 0x00, 0x00, 0x06, 0x11, 0x09, 0x3b, 0x09, 0x47,
91 			       0x09, 0x47, 0x00);
92 	mipi_dsi_dcs_write_seq(dsi, 0xbe,
93 			       0x10, 0x10, 0x00, 0x08, 0x22, 0x09, 0x19, 0x09, 0x25,
94 			       0x09, 0x25, 0x00);
95 	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x5a, 0x80);
96 	mipi_dsi_dcs_write_seq(dsi, 0x65, 0x14);
97 	mipi_dsi_dcs_write_seq(dsi, 0xfa, 0x08, 0x08, 0x08);
98 	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x5a, 0x81);
99 	mipi_dsi_dcs_write_seq(dsi, 0x65, 0x05);
100 	mipi_dsi_dcs_write_seq(dsi, 0xf3, 0x0f);
101 	mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xaa, 0x00);
102 	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x5a, 0x82);
103 	mipi_dsi_dcs_write_seq(dsi, 0xf9, 0x00);
104 	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x51, 0x83);
105 	mipi_dsi_dcs_write_seq(dsi, 0x65, 0x04);
106 	mipi_dsi_dcs_write_seq(dsi, 0xf8, 0x00);
107 	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x5a, 0x00);
108 	mipi_dsi_dcs_write_seq(dsi, 0x65, 0x01);
109 	mipi_dsi_dcs_write_seq(dsi, 0xf4, 0x9a);
110 	mipi_dsi_dcs_write_seq(dsi, 0xff, 0x5a, 0x00);
111 
112 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
113 	if (ret < 0) {
114 		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
115 		return ret;
116 	}
117 	msleep(120);
118 
119 	ret = mipi_dsi_dcs_set_display_on(dsi);
120 	if (ret < 0) {
121 		dev_err(dev, "Failed to set display on: %d\n", ret);
122 		return ret;
123 	}
124 	msleep(20);
125 
126 	return 0;
127 }
128 
visionox_vtdr6130_off(struct visionox_vtdr6130 * ctx)129 static int visionox_vtdr6130_off(struct visionox_vtdr6130 *ctx)
130 {
131 	struct mipi_dsi_device *dsi = ctx->dsi;
132 	struct device *dev = &dsi->dev;
133 	int ret;
134 
135 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
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 	msleep(20);
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 
visionox_vtdr6130_prepare(struct drm_panel * panel)154 static int visionox_vtdr6130_prepare(struct drm_panel *panel)
155 {
156 	struct visionox_vtdr6130 *ctx = to_visionox_vtdr6130(panel);
157 	struct device *dev = &ctx->dsi->dev;
158 	int ret;
159 
160 	if (ctx->prepared)
161 		return 0;
162 
163 	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies),
164 				    ctx->supplies);
165 	if (ret < 0)
166 		return ret;
167 
168 	visionox_vtdr6130_reset(ctx);
169 
170 	ret = visionox_vtdr6130_on(ctx);
171 	if (ret < 0) {
172 		dev_err(dev, "Failed to initialize panel: %d\n", ret);
173 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
174 		regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
175 		return ret;
176 	}
177 
178 	ctx->prepared = true;
179 	return 0;
180 }
181 
visionox_vtdr6130_unprepare(struct drm_panel * panel)182 static int visionox_vtdr6130_unprepare(struct drm_panel *panel)
183 {
184 	struct visionox_vtdr6130 *ctx = to_visionox_vtdr6130(panel);
185 	struct device *dev = &ctx->dsi->dev;
186 	int ret;
187 
188 	if (!ctx->prepared)
189 		return 0;
190 
191 	ret = visionox_vtdr6130_off(ctx);
192 	if (ret < 0)
193 		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
194 
195 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
196 
197 	regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
198 
199 	ctx->prepared = false;
200 	return 0;
201 }
202 
203 static const struct drm_display_mode visionox_vtdr6130_mode = {
204 	.clock = (1080 + 20 + 2 + 20) * (2400 + 20 + 2 + 18) * 144 / 1000,
205 	.hdisplay = 1080,
206 	.hsync_start = 1080 + 20,
207 	.hsync_end = 1080 + 20 + 2,
208 	.htotal = 1080 + 20 + 2 + 20,
209 	.vdisplay = 2400,
210 	.vsync_start = 2400 + 20,
211 	.vsync_end = 2400 + 20 + 2,
212 	.vtotal = 2400 + 20 + 2 + 18,
213 	.width_mm = 71,
214 	.height_mm = 157,
215 };
216 
visionox_vtdr6130_get_modes(struct drm_panel * panel,struct drm_connector * connector)217 static int visionox_vtdr6130_get_modes(struct drm_panel *panel,
218 				       struct drm_connector *connector)
219 {
220 	struct drm_display_mode *mode;
221 
222 	mode = drm_mode_duplicate(connector->dev, &visionox_vtdr6130_mode);
223 	if (!mode)
224 		return -ENOMEM;
225 
226 	drm_mode_set_name(mode);
227 
228 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
229 	connector->display_info.width_mm = mode->width_mm;
230 	connector->display_info.height_mm = mode->height_mm;
231 	drm_mode_probed_add(connector, mode);
232 
233 	return 1;
234 }
235 
236 static const struct drm_panel_funcs visionox_vtdr6130_panel_funcs = {
237 	.prepare = visionox_vtdr6130_prepare,
238 	.unprepare = visionox_vtdr6130_unprepare,
239 	.get_modes = visionox_vtdr6130_get_modes,
240 };
241 
visionox_vtdr6130_bl_update_status(struct backlight_device * bl)242 static int visionox_vtdr6130_bl_update_status(struct backlight_device *bl)
243 {
244 	struct mipi_dsi_device *dsi = bl_get_data(bl);
245 	u16 brightness = backlight_get_brightness(bl);
246 
247 	return mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
248 }
249 
250 static const struct backlight_ops visionox_vtdr6130_bl_ops = {
251 	.update_status = visionox_vtdr6130_bl_update_status,
252 };
253 
254 static struct backlight_device *
visionox_vtdr6130_create_backlight(struct mipi_dsi_device * dsi)255 visionox_vtdr6130_create_backlight(struct mipi_dsi_device *dsi)
256 {
257 	struct device *dev = &dsi->dev;
258 	const struct backlight_properties props = {
259 		.type = BACKLIGHT_RAW,
260 		.brightness = 4095,
261 		.max_brightness = 4095,
262 	};
263 
264 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
265 					      &visionox_vtdr6130_bl_ops, &props);
266 }
267 
visionox_vtdr6130_probe(struct mipi_dsi_device * dsi)268 static int visionox_vtdr6130_probe(struct mipi_dsi_device *dsi)
269 {
270 	struct device *dev = &dsi->dev;
271 	struct visionox_vtdr6130 *ctx;
272 	int ret;
273 
274 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
275 	if (!ctx)
276 		return -ENOMEM;
277 
278 	ctx->supplies[0].supply = "vddio";
279 	ctx->supplies[1].supply = "vci";
280 	ctx->supplies[2].supply = "vdd";
281 
282 	ret = devm_regulator_bulk_get(&dsi->dev, ARRAY_SIZE(ctx->supplies),
283 				      ctx->supplies);
284 	if (ret < 0)
285 		return ret;
286 
287 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
288 	if (IS_ERR(ctx->reset_gpio))
289 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
290 				     "Failed to get reset-gpios\n");
291 
292 	ctx->dsi = dsi;
293 	mipi_dsi_set_drvdata(dsi, ctx);
294 
295 	dsi->lanes = 4;
296 	dsi->format = MIPI_DSI_FMT_RGB888;
297 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_NO_EOT_PACKET |
298 			  MIPI_DSI_CLOCK_NON_CONTINUOUS;
299 
300 	drm_panel_init(&ctx->panel, dev, &visionox_vtdr6130_panel_funcs,
301 		       DRM_MODE_CONNECTOR_DSI);
302 
303 	ctx->panel.backlight = visionox_vtdr6130_create_backlight(dsi);
304 	if (IS_ERR(ctx->panel.backlight))
305 		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
306 				     "Failed to create backlight\n");
307 
308 	drm_panel_add(&ctx->panel);
309 
310 	ret = mipi_dsi_attach(dsi);
311 	if (ret < 0) {
312 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
313 		drm_panel_remove(&ctx->panel);
314 		return ret;
315 	}
316 
317 	return 0;
318 }
319 
visionox_vtdr6130_remove(struct mipi_dsi_device * dsi)320 static void visionox_vtdr6130_remove(struct mipi_dsi_device *dsi)
321 {
322 	struct visionox_vtdr6130 *ctx = mipi_dsi_get_drvdata(dsi);
323 	int ret;
324 
325 	ret = mipi_dsi_detach(dsi);
326 	if (ret < 0)
327 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
328 
329 	drm_panel_remove(&ctx->panel);
330 }
331 
332 static const struct of_device_id visionox_vtdr6130_of_match[] = {
333 	{ .compatible = "visionox,vtdr6130" },
334 	{ /* sentinel */ }
335 };
336 MODULE_DEVICE_TABLE(of, visionox_vtdr6130_of_match);
337 
338 static struct mipi_dsi_driver visionox_vtdr6130_driver = {
339 	.probe = visionox_vtdr6130_probe,
340 	.remove = visionox_vtdr6130_remove,
341 	.driver = {
342 		.name = "panel-visionox-vtdr6130",
343 		.of_match_table = visionox_vtdr6130_of_match,
344 	},
345 };
346 module_mipi_dsi_driver(visionox_vtdr6130_driver);
347 
348 MODULE_AUTHOR("Neil Armstrong <neil.armstrong@linaro.org>");
349 MODULE_DESCRIPTION("Panel driver for the Visionox VTDR6130 AMOLED DSI panel");
350 MODULE_LICENSE("GPL");
351