1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DRM driver for MIPI DBI compatible display panels
4  *
5  * Copyright 2022 Noralf Trønnes
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/firmware.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/property.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/spi/spi.h>
16 
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_atomic_helper.h>
21 #include <drm/drm_gem_dma_helper.h>
22 #include <drm/drm_managed.h>
23 #include <drm/drm_mipi_dbi.h>
24 #include <drm/drm_modes.h>
25 #include <drm/drm_modeset_helper.h>
26 
27 #include <video/mipi_display.h>
28 
29 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
30 					     0, 0, 0, 0, 0, 0, 0 };
31 
32 /*
33  * The display controller configuration is stored in a firmware file.
34  * The Device Tree 'compatible' property value with a '.bin' suffix is passed
35  * to request_firmware() to fetch this file.
36  */
37 struct panel_mipi_dbi_config {
38 	/* Magic string: panel_mipi_dbi_magic */
39 	u8 magic[15];
40 
41 	/* Config file format version */
42 	u8 file_format_version;
43 
44 	/*
45 	 * MIPI commands to execute when the display pipeline is enabled.
46 	 * This is used to configure the display controller.
47 	 *
48 	 * The commands are stored in a byte array with the format:
49 	 *     command, num_parameters, [ parameter, ...], command, ...
50 	 *
51 	 * Some commands require a pause before the next command can be received.
52 	 * Inserting a delay in the command sequence is done by using the NOP command with one
53 	 * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
54 	 * Command Set where it has no parameters).
55 	 *
56 	 * Example:
57 	 *     command 0x11
58 	 *     sleep 120ms
59 	 *     command 0xb1 parameters 0x01, 0x2c, 0x2d
60 	 *     command 0x29
61 	 *
62 	 * Byte sequence:
63 	 *     0x11 0x00
64 	 *     0x00 0x01 0x78
65 	 *     0xb1 0x03 0x01 0x2c 0x2d
66 	 *     0x29 0x00
67 	 */
68 	u8 commands[];
69 };
70 
71 struct panel_mipi_dbi_commands {
72 	const u8 *buf;
73 	size_t len;
74 };
75 
76 static struct panel_mipi_dbi_commands *
77 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
78 {
79 	const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
80 	struct panel_mipi_dbi_commands *commands;
81 	size_t size = fw->size, commands_len;
82 	unsigned int i = 0;
83 
84 	if (size < sizeof(*config) + 2) { /* At least 1 command */
85 		dev_err(dev, "config: file size=%zu is too small\n", size);
86 		return ERR_PTR(-EINVAL);
87 	}
88 
89 	if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
90 		dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
91 		return ERR_PTR(-EINVAL);
92 	}
93 
94 	if (config->file_format_version != 1) {
95 		dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
96 		return ERR_PTR(-EINVAL);
97 	}
98 
99 	drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
100 
101 	commands_len = size - sizeof(*config);
102 
103 	while ((i + 1) < commands_len) {
104 		u8 command = config->commands[i++];
105 		u8 num_parameters = config->commands[i++];
106 		const u8 *parameters = &config->commands[i];
107 
108 		i += num_parameters;
109 		if (i > commands_len) {
110 			dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
111 				command, num_parameters);
112 			return ERR_PTR(-EINVAL);
113 		}
114 
115 		if (command == 0x00 && num_parameters == 1)
116 			drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
117 		else
118 			drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
119 				    command, num_parameters, parameters);
120 	}
121 
122 	if (i != commands_len) {
123 		dev_err(dev, "config: malformed command array\n");
124 		return ERR_PTR(-EINVAL);
125 	}
126 
127 	commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
128 	if (!commands)
129 		return ERR_PTR(-ENOMEM);
130 
131 	commands->len = commands_len;
132 	commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
133 	if (!commands->buf)
134 		return ERR_PTR(-ENOMEM);
135 
136 	return commands;
137 }
138 
139 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
140 {
141 	struct panel_mipi_dbi_commands *commands;
142 	const struct firmware *fw;
143 	const char *compatible;
144 	char fw_name[40];
145 	int ret;
146 
147 	ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
148 	if (ret)
149 		return ERR_PTR(ret);
150 
151 	snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
152 	ret = request_firmware(&fw, fw_name, dev);
153 	if (ret) {
154 		dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
155 			compatible, ret);
156 
157 		return ERR_PTR(ret);
158 	}
159 
160 	commands = panel_mipi_dbi_check_commands(dev, fw);
161 	release_firmware(fw);
162 
163 	return commands;
164 }
165 
166 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
167 					    struct panel_mipi_dbi_commands *commands)
168 {
169 	unsigned int i = 0;
170 
171 	if (!commands)
172 		return;
173 
174 	while (i < commands->len) {
175 		u8 command = commands->buf[i++];
176 		u8 num_parameters = commands->buf[i++];
177 		const u8 *parameters = &commands->buf[i];
178 
179 		if (command == 0x00 && num_parameters == 1)
180 			msleep(parameters[0]);
181 		else if (num_parameters)
182 			mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
183 		else
184 			mipi_dbi_command(dbi, command);
185 
186 		i += num_parameters;
187 	}
188 }
189 
190 static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
191 				  struct drm_crtc_state *crtc_state,
192 				  struct drm_plane_state *plane_state)
193 {
194 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
195 	struct mipi_dbi *dbi = &dbidev->dbi;
196 	int ret, idx;
197 
198 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
199 		return;
200 
201 	drm_dbg(pipe->crtc.dev, "\n");
202 
203 	ret = mipi_dbi_poweron_conditional_reset(dbidev);
204 	if (ret < 0)
205 		goto out_exit;
206 	if (!ret)
207 		panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
208 
209 	mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
210 out_exit:
211 	drm_dev_exit(idx);
212 }
213 
214 static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
215 	.mode_valid = mipi_dbi_pipe_mode_valid,
216 	.enable = panel_mipi_dbi_enable,
217 	.disable = mipi_dbi_pipe_disable,
218 	.update = mipi_dbi_pipe_update,
219 };
220 
221 DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops);
222 
223 static const struct drm_driver panel_mipi_dbi_driver = {
224 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
225 	.fops			= &panel_mipi_dbi_fops,
226 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
227 	.debugfs_init		= mipi_dbi_debugfs_init,
228 	.name			= "panel-mipi-dbi",
229 	.desc			= "MIPI DBI compatible display panel",
230 	.date			= "20220103",
231 	.major			= 1,
232 	.minor			= 0,
233 };
234 
235 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
236 {
237 	struct device *dev = dbidev->drm.dev;
238 	u16 hback_porch, vback_porch;
239 	int ret;
240 
241 	ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
242 	if (ret) {
243 		dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
244 		return ret;
245 	}
246 
247 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
248 
249 	hback_porch = mode->htotal - mode->hsync_end;
250 	vback_porch = mode->vtotal - mode->vsync_end;
251 
252 	/*
253 	 * Make sure width and height are set and that only back porch and
254 	 * pixelclock are set in the other timing values. Also check that
255 	 * width and height don't exceed the 16-bit value specified by MIPI DCS.
256 	 */
257 	if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
258 	    mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
259 	    mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
260 		dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
261 		return -EINVAL;
262 	}
263 
264 	/* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
265 	if (!mode->clock)
266 		mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
267 
268 	dbidev->top_offset = vback_porch;
269 	dbidev->left_offset = hback_porch;
270 
271 	return 0;
272 }
273 
274 static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
275 {
276 	struct device *dev = &spi->dev;
277 	struct drm_display_mode mode;
278 	struct mipi_dbi_dev *dbidev;
279 	struct drm_device *drm;
280 	struct mipi_dbi *dbi;
281 	struct gpio_desc *dc;
282 	int ret;
283 
284 	dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
285 	if (IS_ERR(dbidev))
286 		return PTR_ERR(dbidev);
287 
288 	dbi = &dbidev->dbi;
289 	drm = &dbidev->drm;
290 
291 	ret = panel_mipi_dbi_get_mode(dbidev, &mode);
292 	if (ret)
293 		return ret;
294 
295 	dbidev->regulator = devm_regulator_get(dev, "power");
296 	if (IS_ERR(dbidev->regulator))
297 		return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
298 				     "Failed to get regulator 'power'\n");
299 
300 	dbidev->backlight = devm_of_find_backlight(dev);
301 	if (IS_ERR(dbidev->backlight))
302 		return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
303 
304 	dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
305 	if (IS_ERR(dbi->reset))
306 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
307 
308 	dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
309 	if (IS_ERR(dc))
310 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
311 
312 	ret = mipi_dbi_spi_init(spi, dbi, dc);
313 	if (ret)
314 		return ret;
315 
316 	if (device_property_present(dev, "write-only"))
317 		dbi->read_commands = NULL;
318 
319 	dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
320 	if (IS_ERR(dbidev->driver_private))
321 		return PTR_ERR(dbidev->driver_private);
322 
323 	ret = mipi_dbi_dev_init(dbidev, &panel_mipi_dbi_pipe_funcs, &mode, 0);
324 	if (ret)
325 		return ret;
326 
327 	drm_mode_config_reset(drm);
328 
329 	ret = drm_dev_register(drm, 0);
330 	if (ret)
331 		return ret;
332 
333 	spi_set_drvdata(spi, drm);
334 
335 	drm_fbdev_generic_setup(drm, 0);
336 
337 	return 0;
338 }
339 
340 static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
341 {
342 	struct drm_device *drm = spi_get_drvdata(spi);
343 
344 	drm_dev_unplug(drm);
345 	drm_atomic_helper_shutdown(drm);
346 }
347 
348 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
349 {
350 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
351 }
352 
353 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
354 {
355 	return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
356 }
357 
358 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
359 {
360 	drm_mode_config_helper_resume(dev_get_drvdata(dev));
361 
362 	return 0;
363 }
364 
365 static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
366 	SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
367 };
368 
369 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
370 	{ .compatible = "panel-mipi-dbi-spi" },
371 	{},
372 };
373 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
374 
375 static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
376 	{ "panel-mipi-dbi-spi", 0 },
377 	{ },
378 };
379 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
380 
381 static struct spi_driver panel_mipi_dbi_spi_driver = {
382 	.driver = {
383 		.name = "panel-mipi-dbi-spi",
384 		.owner = THIS_MODULE,
385 		.of_match_table = panel_mipi_dbi_spi_of_match,
386 		.pm = &panel_mipi_dbi_pm_ops,
387 	},
388 	.id_table = panel_mipi_dbi_spi_id,
389 	.probe = panel_mipi_dbi_spi_probe,
390 	.remove = panel_mipi_dbi_spi_remove,
391 	.shutdown = panel_mipi_dbi_spi_shutdown,
392 };
393 module_spi_driver(panel_mipi_dbi_spi_driver);
394 
395 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
396 MODULE_AUTHOR("Noralf Trønnes");
397 MODULE_LICENSE("GPL");
398