xref: /openbmc/linux/drivers/gpu/drm/tiny/panel-mipi-dbi.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
10e65e2e6SNoralf Trønnes // SPDX-License-Identifier: GPL-2.0
20e65e2e6SNoralf Trønnes /*
30e65e2e6SNoralf Trønnes  * DRM driver for MIPI DBI compatible display panels
40e65e2e6SNoralf Trønnes  *
50e65e2e6SNoralf Trønnes  * Copyright 2022 Noralf Trønnes
60e65e2e6SNoralf Trønnes  */
70e65e2e6SNoralf Trønnes 
80e65e2e6SNoralf Trønnes #include <linux/backlight.h>
90e65e2e6SNoralf Trønnes #include <linux/delay.h>
100e65e2e6SNoralf Trønnes #include <linux/firmware.h>
110e65e2e6SNoralf Trønnes #include <linux/gpio/consumer.h>
120e65e2e6SNoralf Trønnes #include <linux/module.h>
130e65e2e6SNoralf Trønnes #include <linux/property.h>
140e65e2e6SNoralf Trønnes #include <linux/regulator/consumer.h>
150e65e2e6SNoralf Trønnes #include <linux/spi/spi.h>
160e65e2e6SNoralf Trønnes 
170e65e2e6SNoralf Trønnes #include <drm/drm_atomic_helper.h>
180e65e2e6SNoralf Trønnes #include <drm/drm_drv.h>
198ab59da2SThomas Zimmermann #include <drm/drm_fbdev_generic.h>
200e65e2e6SNoralf Trønnes #include <drm/drm_gem_atomic_helper.h>
214a83c26aSDanilo Krummrich #include <drm/drm_gem_dma_helper.h>
220e65e2e6SNoralf Trønnes #include <drm/drm_managed.h>
230e65e2e6SNoralf Trønnes #include <drm/drm_mipi_dbi.h>
240e65e2e6SNoralf Trønnes #include <drm/drm_modes.h>
250e65e2e6SNoralf Trønnes #include <drm/drm_modeset_helper.h>
260e65e2e6SNoralf Trønnes 
270e65e2e6SNoralf Trønnes #include <video/mipi_display.h>
280e65e2e6SNoralf Trønnes 
290e65e2e6SNoralf Trønnes static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
300e65e2e6SNoralf Trønnes 					     0, 0, 0, 0, 0, 0, 0 };
310e65e2e6SNoralf Trønnes 
320e65e2e6SNoralf Trønnes /*
330e65e2e6SNoralf Trønnes  * The display controller configuration is stored in a firmware file.
340e65e2e6SNoralf Trønnes  * The Device Tree 'compatible' property value with a '.bin' suffix is passed
350e65e2e6SNoralf Trønnes  * to request_firmware() to fetch this file.
360e65e2e6SNoralf Trønnes  */
370e65e2e6SNoralf Trønnes struct panel_mipi_dbi_config {
380e65e2e6SNoralf Trønnes 	/* Magic string: panel_mipi_dbi_magic */
390e65e2e6SNoralf Trønnes 	u8 magic[15];
400e65e2e6SNoralf Trønnes 
410e65e2e6SNoralf Trønnes 	/* Config file format version */
420e65e2e6SNoralf Trønnes 	u8 file_format_version;
430e65e2e6SNoralf Trønnes 
440e65e2e6SNoralf Trønnes 	/*
450e65e2e6SNoralf Trønnes 	 * MIPI commands to execute when the display pipeline is enabled.
460e65e2e6SNoralf Trønnes 	 * This is used to configure the display controller.
470e65e2e6SNoralf Trønnes 	 *
480e65e2e6SNoralf Trønnes 	 * The commands are stored in a byte array with the format:
490e65e2e6SNoralf Trønnes 	 *     command, num_parameters, [ parameter, ...], command, ...
500e65e2e6SNoralf Trønnes 	 *
510e65e2e6SNoralf Trønnes 	 * Some commands require a pause before the next command can be received.
520e65e2e6SNoralf Trønnes 	 * Inserting a delay in the command sequence is done by using the NOP command with one
530e65e2e6SNoralf Trønnes 	 * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
540e65e2e6SNoralf Trønnes 	 * Command Set where it has no parameters).
550e65e2e6SNoralf Trønnes 	 *
560e65e2e6SNoralf Trønnes 	 * Example:
570e65e2e6SNoralf Trønnes 	 *     command 0x11
580e65e2e6SNoralf Trønnes 	 *     sleep 120ms
590e65e2e6SNoralf Trønnes 	 *     command 0xb1 parameters 0x01, 0x2c, 0x2d
600e65e2e6SNoralf Trønnes 	 *     command 0x29
610e65e2e6SNoralf Trønnes 	 *
620e65e2e6SNoralf Trønnes 	 * Byte sequence:
630e65e2e6SNoralf Trønnes 	 *     0x11 0x00
640e65e2e6SNoralf Trønnes 	 *     0x00 0x01 0x78
650e65e2e6SNoralf Trønnes 	 *     0xb1 0x03 0x01 0x2c 0x2d
660e65e2e6SNoralf Trønnes 	 *     0x29 0x00
670e65e2e6SNoralf Trønnes 	 */
680e65e2e6SNoralf Trønnes 	u8 commands[];
690e65e2e6SNoralf Trønnes };
700e65e2e6SNoralf Trønnes 
710e65e2e6SNoralf Trønnes struct panel_mipi_dbi_commands {
720e65e2e6SNoralf Trønnes 	const u8 *buf;
730e65e2e6SNoralf Trønnes 	size_t len;
740e65e2e6SNoralf Trønnes };
750e65e2e6SNoralf Trønnes 
760e65e2e6SNoralf Trønnes static struct panel_mipi_dbi_commands *
panel_mipi_dbi_check_commands(struct device * dev,const struct firmware * fw)770e65e2e6SNoralf Trønnes panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
780e65e2e6SNoralf Trønnes {
790e65e2e6SNoralf Trønnes 	const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
800e65e2e6SNoralf Trønnes 	struct panel_mipi_dbi_commands *commands;
810e65e2e6SNoralf Trønnes 	size_t size = fw->size, commands_len;
820e65e2e6SNoralf Trønnes 	unsigned int i = 0;
830e65e2e6SNoralf Trønnes 
840e65e2e6SNoralf Trønnes 	if (size < sizeof(*config) + 2) { /* At least 1 command */
850e65e2e6SNoralf Trønnes 		dev_err(dev, "config: file size=%zu is too small\n", size);
860e65e2e6SNoralf Trønnes 		return ERR_PTR(-EINVAL);
870e65e2e6SNoralf Trønnes 	}
880e65e2e6SNoralf Trønnes 
890e65e2e6SNoralf Trønnes 	if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
900e65e2e6SNoralf Trønnes 		dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
910e65e2e6SNoralf Trønnes 		return ERR_PTR(-EINVAL);
920e65e2e6SNoralf Trønnes 	}
930e65e2e6SNoralf Trønnes 
940e65e2e6SNoralf Trønnes 	if (config->file_format_version != 1) {
950e65e2e6SNoralf Trønnes 		dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
960e65e2e6SNoralf Trønnes 		return ERR_PTR(-EINVAL);
970e65e2e6SNoralf Trønnes 	}
980e65e2e6SNoralf Trønnes 
990e65e2e6SNoralf Trønnes 	drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
1000e65e2e6SNoralf Trønnes 
1010e65e2e6SNoralf Trønnes 	commands_len = size - sizeof(*config);
1020e65e2e6SNoralf Trønnes 
1030e65e2e6SNoralf Trønnes 	while ((i + 1) < commands_len) {
1040e65e2e6SNoralf Trønnes 		u8 command = config->commands[i++];
1050e65e2e6SNoralf Trønnes 		u8 num_parameters = config->commands[i++];
1060e65e2e6SNoralf Trønnes 		const u8 *parameters = &config->commands[i];
1070e65e2e6SNoralf Trønnes 
1080e65e2e6SNoralf Trønnes 		i += num_parameters;
1090e65e2e6SNoralf Trønnes 		if (i > commands_len) {
1100e65e2e6SNoralf Trønnes 			dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
1110e65e2e6SNoralf Trønnes 				command, num_parameters);
1120e65e2e6SNoralf Trønnes 			return ERR_PTR(-EINVAL);
1130e65e2e6SNoralf Trønnes 		}
1140e65e2e6SNoralf Trønnes 
1150e65e2e6SNoralf Trønnes 		if (command == 0x00 && num_parameters == 1)
1160e65e2e6SNoralf Trønnes 			drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
1170e65e2e6SNoralf Trønnes 		else
1180e65e2e6SNoralf Trønnes 			drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
1190e65e2e6SNoralf Trønnes 				    command, num_parameters, parameters);
1200e65e2e6SNoralf Trønnes 	}
1210e65e2e6SNoralf Trønnes 
1220e65e2e6SNoralf Trønnes 	if (i != commands_len) {
1230e65e2e6SNoralf Trønnes 		dev_err(dev, "config: malformed command array\n");
1240e65e2e6SNoralf Trønnes 		return ERR_PTR(-EINVAL);
1250e65e2e6SNoralf Trønnes 	}
1260e65e2e6SNoralf Trønnes 
1270e65e2e6SNoralf Trønnes 	commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
1280e65e2e6SNoralf Trønnes 	if (!commands)
1290e65e2e6SNoralf Trønnes 		return ERR_PTR(-ENOMEM);
1300e65e2e6SNoralf Trønnes 
1310e65e2e6SNoralf Trønnes 	commands->len = commands_len;
1320e65e2e6SNoralf Trønnes 	commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
1330e65e2e6SNoralf Trønnes 	if (!commands->buf)
1340e65e2e6SNoralf Trønnes 		return ERR_PTR(-ENOMEM);
1350e65e2e6SNoralf Trønnes 
1360e65e2e6SNoralf Trønnes 	return commands;
1370e65e2e6SNoralf Trønnes }
1380e65e2e6SNoralf Trønnes 
panel_mipi_dbi_commands_from_fw(struct device * dev)1390e65e2e6SNoralf Trønnes static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
1400e65e2e6SNoralf Trønnes {
1410e65e2e6SNoralf Trønnes 	struct panel_mipi_dbi_commands *commands;
1420e65e2e6SNoralf Trønnes 	const struct firmware *fw;
1430e65e2e6SNoralf Trønnes 	const char *compatible;
1440e65e2e6SNoralf Trønnes 	char fw_name[40];
1450e65e2e6SNoralf Trønnes 	int ret;
1460e65e2e6SNoralf Trønnes 
1470e65e2e6SNoralf Trønnes 	ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
1480e65e2e6SNoralf Trønnes 	if (ret)
1490e65e2e6SNoralf Trønnes 		return ERR_PTR(ret);
1500e65e2e6SNoralf Trønnes 
1510e65e2e6SNoralf Trønnes 	snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
1520e65e2e6SNoralf Trønnes 	ret = request_firmware(&fw, fw_name, dev);
1530e65e2e6SNoralf Trønnes 	if (ret) {
1540e65e2e6SNoralf Trønnes 		dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
1550e65e2e6SNoralf Trønnes 			compatible, ret);
1560e65e2e6SNoralf Trønnes 
1570e65e2e6SNoralf Trønnes 		return ERR_PTR(ret);
1580e65e2e6SNoralf Trønnes 	}
1590e65e2e6SNoralf Trønnes 
1600e65e2e6SNoralf Trønnes 	commands = panel_mipi_dbi_check_commands(dev, fw);
1610e65e2e6SNoralf Trønnes 	release_firmware(fw);
1620e65e2e6SNoralf Trønnes 
1630e65e2e6SNoralf Trønnes 	return commands;
1640e65e2e6SNoralf Trønnes }
1650e65e2e6SNoralf Trønnes 
panel_mipi_dbi_commands_execute(struct mipi_dbi * dbi,struct panel_mipi_dbi_commands * commands)1660e65e2e6SNoralf Trønnes static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
1670e65e2e6SNoralf Trønnes 					    struct panel_mipi_dbi_commands *commands)
1680e65e2e6SNoralf Trønnes {
1690e65e2e6SNoralf Trønnes 	unsigned int i = 0;
1700e65e2e6SNoralf Trønnes 
1710e65e2e6SNoralf Trønnes 	if (!commands)
1720e65e2e6SNoralf Trønnes 		return;
1730e65e2e6SNoralf Trønnes 
1740e65e2e6SNoralf Trønnes 	while (i < commands->len) {
1750e65e2e6SNoralf Trønnes 		u8 command = commands->buf[i++];
1760e65e2e6SNoralf Trønnes 		u8 num_parameters = commands->buf[i++];
1770e65e2e6SNoralf Trønnes 		const u8 *parameters = &commands->buf[i];
1780e65e2e6SNoralf Trønnes 
1790e65e2e6SNoralf Trønnes 		if (command == 0x00 && num_parameters == 1)
1800e65e2e6SNoralf Trønnes 			msleep(parameters[0]);
1810e65e2e6SNoralf Trønnes 		else if (num_parameters)
1820e65e2e6SNoralf Trønnes 			mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
1830e65e2e6SNoralf Trønnes 		else
1840e65e2e6SNoralf Trønnes 			mipi_dbi_command(dbi, command);
1850e65e2e6SNoralf Trønnes 
1860e65e2e6SNoralf Trønnes 		i += num_parameters;
1870e65e2e6SNoralf Trønnes 	}
1880e65e2e6SNoralf Trønnes }
1890e65e2e6SNoralf Trønnes 
panel_mipi_dbi_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)1900e65e2e6SNoralf Trønnes static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
1910e65e2e6SNoralf Trønnes 				  struct drm_crtc_state *crtc_state,
1920e65e2e6SNoralf Trønnes 				  struct drm_plane_state *plane_state)
1930e65e2e6SNoralf Trønnes {
1940e65e2e6SNoralf Trønnes 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
1950e65e2e6SNoralf Trønnes 	struct mipi_dbi *dbi = &dbidev->dbi;
1960e65e2e6SNoralf Trønnes 	int ret, idx;
1970e65e2e6SNoralf Trønnes 
1980e65e2e6SNoralf Trønnes 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
1990e65e2e6SNoralf Trønnes 		return;
2000e65e2e6SNoralf Trønnes 
2010e65e2e6SNoralf Trønnes 	drm_dbg(pipe->crtc.dev, "\n");
2020e65e2e6SNoralf Trønnes 
2030e65e2e6SNoralf Trønnes 	ret = mipi_dbi_poweron_conditional_reset(dbidev);
2040e65e2e6SNoralf Trønnes 	if (ret < 0)
2050e65e2e6SNoralf Trønnes 		goto out_exit;
2060e65e2e6SNoralf Trønnes 	if (!ret)
2070e65e2e6SNoralf Trønnes 		panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
2080e65e2e6SNoralf Trønnes 
2090e65e2e6SNoralf Trønnes 	mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
2100e65e2e6SNoralf Trønnes out_exit:
2110e65e2e6SNoralf Trønnes 	drm_dev_exit(idx);
2120e65e2e6SNoralf Trønnes }
2130e65e2e6SNoralf Trønnes 
2140e65e2e6SNoralf Trønnes static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
21563aa5ec6SThomas Zimmermann 	DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(panel_mipi_dbi_enable),
2160e65e2e6SNoralf Trønnes };
2170e65e2e6SNoralf Trønnes 
2184a83c26aSDanilo Krummrich DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops);
2190e65e2e6SNoralf Trønnes 
2200e65e2e6SNoralf Trønnes static const struct drm_driver panel_mipi_dbi_driver = {
2210e65e2e6SNoralf Trønnes 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
2220e65e2e6SNoralf Trønnes 	.fops			= &panel_mipi_dbi_fops,
2234a83c26aSDanilo Krummrich 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
2240e65e2e6SNoralf Trønnes 	.debugfs_init		= mipi_dbi_debugfs_init,
2250e65e2e6SNoralf Trønnes 	.name			= "panel-mipi-dbi",
2260e65e2e6SNoralf Trønnes 	.desc			= "MIPI DBI compatible display panel",
2270e65e2e6SNoralf Trønnes 	.date			= "20220103",
2280e65e2e6SNoralf Trønnes 	.major			= 1,
2290e65e2e6SNoralf Trønnes 	.minor			= 0,
2300e65e2e6SNoralf Trønnes };
2310e65e2e6SNoralf Trønnes 
panel_mipi_dbi_get_mode(struct mipi_dbi_dev * dbidev,struct drm_display_mode * mode)2320e65e2e6SNoralf Trønnes static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
2330e65e2e6SNoralf Trønnes {
2340e65e2e6SNoralf Trønnes 	struct device *dev = dbidev->drm.dev;
2350e65e2e6SNoralf Trønnes 	u16 hback_porch, vback_porch;
2360e65e2e6SNoralf Trønnes 	int ret;
2370e65e2e6SNoralf Trønnes 
2380e65e2e6SNoralf Trønnes 	ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
2390e65e2e6SNoralf Trønnes 	if (ret) {
2400e65e2e6SNoralf Trønnes 		dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
2410e65e2e6SNoralf Trønnes 		return ret;
2420e65e2e6SNoralf Trønnes 	}
2430e65e2e6SNoralf Trønnes 
2440e65e2e6SNoralf Trønnes 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
2450e65e2e6SNoralf Trønnes 
2460e65e2e6SNoralf Trønnes 	hback_porch = mode->htotal - mode->hsync_end;
2470e65e2e6SNoralf Trønnes 	vback_porch = mode->vtotal - mode->vsync_end;
2480e65e2e6SNoralf Trønnes 
2490e65e2e6SNoralf Trønnes 	/*
2500e65e2e6SNoralf Trønnes 	 * Make sure width and height are set and that only back porch and
2510e65e2e6SNoralf Trønnes 	 * pixelclock are set in the other timing values. Also check that
2520e65e2e6SNoralf Trønnes 	 * width and height don't exceed the 16-bit value specified by MIPI DCS.
2530e65e2e6SNoralf Trønnes 	 */
2540e65e2e6SNoralf Trønnes 	if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
2550e65e2e6SNoralf Trønnes 	    mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
2560e65e2e6SNoralf Trønnes 	    mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
2570e65e2e6SNoralf Trønnes 		dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
2580e65e2e6SNoralf Trønnes 		return -EINVAL;
2590e65e2e6SNoralf Trønnes 	}
2600e65e2e6SNoralf Trønnes 
2610e65e2e6SNoralf Trønnes 	/* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
2620e65e2e6SNoralf Trønnes 	if (!mode->clock)
2630e65e2e6SNoralf Trønnes 		mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
2640e65e2e6SNoralf Trønnes 
2650e65e2e6SNoralf Trønnes 	dbidev->top_offset = vback_porch;
2660e65e2e6SNoralf Trønnes 	dbidev->left_offset = hback_porch;
2670e65e2e6SNoralf Trønnes 
2680e65e2e6SNoralf Trønnes 	return 0;
2690e65e2e6SNoralf Trønnes }
2700e65e2e6SNoralf Trønnes 
panel_mipi_dbi_spi_probe(struct spi_device * spi)2710e65e2e6SNoralf Trønnes static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
2720e65e2e6SNoralf Trønnes {
2730e65e2e6SNoralf Trønnes 	struct device *dev = &spi->dev;
2740e65e2e6SNoralf Trønnes 	struct drm_display_mode mode;
2750e65e2e6SNoralf Trønnes 	struct mipi_dbi_dev *dbidev;
2760e65e2e6SNoralf Trønnes 	struct drm_device *drm;
2770e65e2e6SNoralf Trønnes 	struct mipi_dbi *dbi;
2780e65e2e6SNoralf Trønnes 	struct gpio_desc *dc;
2790e65e2e6SNoralf Trønnes 	int ret;
2800e65e2e6SNoralf Trønnes 
2810e65e2e6SNoralf Trønnes 	dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
2820e65e2e6SNoralf Trønnes 	if (IS_ERR(dbidev))
2830e65e2e6SNoralf Trønnes 		return PTR_ERR(dbidev);
2840e65e2e6SNoralf Trønnes 
2850e65e2e6SNoralf Trønnes 	dbi = &dbidev->dbi;
2860e65e2e6SNoralf Trønnes 	drm = &dbidev->drm;
2870e65e2e6SNoralf Trønnes 
2880e65e2e6SNoralf Trønnes 	ret = panel_mipi_dbi_get_mode(dbidev, &mode);
2890e65e2e6SNoralf Trønnes 	if (ret)
2900e65e2e6SNoralf Trønnes 		return ret;
2910e65e2e6SNoralf Trønnes 
2920e65e2e6SNoralf Trønnes 	dbidev->regulator = devm_regulator_get(dev, "power");
2930e65e2e6SNoralf Trønnes 	if (IS_ERR(dbidev->regulator))
2940e65e2e6SNoralf Trønnes 		return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
2950e65e2e6SNoralf Trønnes 				     "Failed to get regulator 'power'\n");
2960e65e2e6SNoralf Trønnes 
2973dd26888SOtto Pflüger 	dbidev->io_regulator = devm_regulator_get(dev, "io");
2983dd26888SOtto Pflüger 	if (IS_ERR(dbidev->io_regulator))
2993dd26888SOtto Pflüger 		return dev_err_probe(dev, PTR_ERR(dbidev->io_regulator),
3003dd26888SOtto Pflüger 				     "Failed to get regulator 'io'\n");
3013dd26888SOtto Pflüger 
3020e65e2e6SNoralf Trønnes 	dbidev->backlight = devm_of_find_backlight(dev);
3030e65e2e6SNoralf Trønnes 	if (IS_ERR(dbidev->backlight))
3040e65e2e6SNoralf Trønnes 		return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
3050e65e2e6SNoralf Trønnes 
3060e65e2e6SNoralf Trønnes 	dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
3070e65e2e6SNoralf Trønnes 	if (IS_ERR(dbi->reset))
3080e65e2e6SNoralf Trønnes 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
3090e65e2e6SNoralf Trønnes 
310*3a63ef6eSOtto Pflüger 	/* Multiple panels can share the "dc" GPIO, but only if they are on the same SPI bus! */
311*3a63ef6eSOtto Pflüger 	dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
3120e65e2e6SNoralf Trønnes 	if (IS_ERR(dc))
3130e65e2e6SNoralf Trønnes 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
3140e65e2e6SNoralf Trønnes 
3150e65e2e6SNoralf Trønnes 	ret = mipi_dbi_spi_init(spi, dbi, dc);
3160e65e2e6SNoralf Trønnes 	if (ret)
3170e65e2e6SNoralf Trønnes 		return ret;
3180e65e2e6SNoralf Trønnes 
3190e65e2e6SNoralf Trønnes 	if (device_property_present(dev, "write-only"))
3200e65e2e6SNoralf Trønnes 		dbi->read_commands = NULL;
3210e65e2e6SNoralf Trønnes 
3220e65e2e6SNoralf Trønnes 	dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
3230e65e2e6SNoralf Trønnes 	if (IS_ERR(dbidev->driver_private))
3240e65e2e6SNoralf Trønnes 		return PTR_ERR(dbidev->driver_private);
3250e65e2e6SNoralf Trønnes 
3260e65e2e6SNoralf Trønnes 	ret = mipi_dbi_dev_init(dbidev, &panel_mipi_dbi_pipe_funcs, &mode, 0);
3270e65e2e6SNoralf Trønnes 	if (ret)
3280e65e2e6SNoralf Trønnes 		return ret;
3290e65e2e6SNoralf Trønnes 
3300e65e2e6SNoralf Trønnes 	drm_mode_config_reset(drm);
3310e65e2e6SNoralf Trønnes 
3320e65e2e6SNoralf Trønnes 	ret = drm_dev_register(drm, 0);
3330e65e2e6SNoralf Trønnes 	if (ret)
3340e65e2e6SNoralf Trønnes 		return ret;
3350e65e2e6SNoralf Trønnes 
3360e65e2e6SNoralf Trønnes 	spi_set_drvdata(spi, drm);
3370e65e2e6SNoralf Trønnes 
3380e65e2e6SNoralf Trønnes 	drm_fbdev_generic_setup(drm, 0);
3390e65e2e6SNoralf Trønnes 
3400e65e2e6SNoralf Trønnes 	return 0;
3410e65e2e6SNoralf Trønnes }
3420e65e2e6SNoralf Trønnes 
panel_mipi_dbi_spi_remove(struct spi_device * spi)343b14ffae3SLinus Torvalds static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
3440e65e2e6SNoralf Trønnes {
3450e65e2e6SNoralf Trønnes 	struct drm_device *drm = spi_get_drvdata(spi);
3460e65e2e6SNoralf Trønnes 
3470e65e2e6SNoralf Trønnes 	drm_dev_unplug(drm);
3480e65e2e6SNoralf Trønnes 	drm_atomic_helper_shutdown(drm);
3490e65e2e6SNoralf Trønnes }
3500e65e2e6SNoralf Trønnes 
panel_mipi_dbi_spi_shutdown(struct spi_device * spi)3510e65e2e6SNoralf Trønnes static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
3520e65e2e6SNoralf Trønnes {
3530e65e2e6SNoralf Trønnes 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
3540e65e2e6SNoralf Trønnes }
3550e65e2e6SNoralf Trønnes 
panel_mipi_dbi_pm_suspend(struct device * dev)3560e65e2e6SNoralf Trønnes static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
3570e65e2e6SNoralf Trønnes {
3580e65e2e6SNoralf Trønnes 	return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
3590e65e2e6SNoralf Trønnes }
3600e65e2e6SNoralf Trønnes 
panel_mipi_dbi_pm_resume(struct device * dev)3610e65e2e6SNoralf Trønnes static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
3620e65e2e6SNoralf Trønnes {
3630e65e2e6SNoralf Trønnes 	drm_mode_config_helper_resume(dev_get_drvdata(dev));
3640e65e2e6SNoralf Trønnes 
3650e65e2e6SNoralf Trønnes 	return 0;
3660e65e2e6SNoralf Trønnes }
3670e65e2e6SNoralf Trønnes 
3680e65e2e6SNoralf Trønnes static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
3690e65e2e6SNoralf Trønnes 	SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
3700e65e2e6SNoralf Trønnes };
3710e65e2e6SNoralf Trønnes 
3720e65e2e6SNoralf Trønnes static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
3730e65e2e6SNoralf Trønnes 	{ .compatible = "panel-mipi-dbi-spi" },
3740e65e2e6SNoralf Trønnes 	{},
3750e65e2e6SNoralf Trønnes };
3760e65e2e6SNoralf Trønnes MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
3770e65e2e6SNoralf Trønnes 
3780e65e2e6SNoralf Trønnes static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
3790e65e2e6SNoralf Trønnes 	{ "panel-mipi-dbi-spi", 0 },
3800e65e2e6SNoralf Trønnes 	{ },
3810e65e2e6SNoralf Trønnes };
3820e65e2e6SNoralf Trønnes MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
3830e65e2e6SNoralf Trønnes 
3840e65e2e6SNoralf Trønnes static struct spi_driver panel_mipi_dbi_spi_driver = {
3850e65e2e6SNoralf Trønnes 	.driver = {
3860e65e2e6SNoralf Trønnes 		.name = "panel-mipi-dbi-spi",
3870e65e2e6SNoralf Trønnes 		.owner = THIS_MODULE,
3880e65e2e6SNoralf Trønnes 		.of_match_table = panel_mipi_dbi_spi_of_match,
3890e65e2e6SNoralf Trønnes 		.pm = &panel_mipi_dbi_pm_ops,
3900e65e2e6SNoralf Trønnes 	},
3910e65e2e6SNoralf Trønnes 	.id_table = panel_mipi_dbi_spi_id,
3920e65e2e6SNoralf Trønnes 	.probe = panel_mipi_dbi_spi_probe,
3930e65e2e6SNoralf Trønnes 	.remove = panel_mipi_dbi_spi_remove,
3940e65e2e6SNoralf Trønnes 	.shutdown = panel_mipi_dbi_spi_shutdown,
3950e65e2e6SNoralf Trønnes };
3960e65e2e6SNoralf Trønnes module_spi_driver(panel_mipi_dbi_spi_driver);
3970e65e2e6SNoralf Trønnes 
3980e65e2e6SNoralf Trønnes MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
3990e65e2e6SNoralf Trønnes MODULE_AUTHOR("Noralf Trønnes");
4000e65e2e6SNoralf Trønnes MODULE_LICENSE("GPL");
401