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