xref: /openbmc/linux/drivers/gpu/drm/drm_mipi_dbi.c (revision c4a11bf4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * MIPI Display Bus Interface (DBI) LCD controller support
4  *
5  * Copyright 2016 Noralf Trønnes
6  */
7 
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
14 
15 #include <drm/drm_connector.h>
16 #include <drm/drm_damage_helper.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_gem_cma_helper.h>
19 #include <drm/drm_format_helper.h>
20 #include <drm/drm_fourcc.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_mipi_dbi.h>
23 #include <drm/drm_modes.h>
24 #include <drm/drm_probe_helper.h>
25 #include <drm/drm_rect.h>
26 #include <video/mipi_display.h>
27 
28 #define MIPI_DBI_MAX_SPI_READ_SPEED 2000000 /* 2MHz */
29 
30 #define DCS_POWER_MODE_DISPLAY			BIT(2)
31 #define DCS_POWER_MODE_DISPLAY_NORMAL_MODE	BIT(3)
32 #define DCS_POWER_MODE_SLEEP_MODE		BIT(4)
33 #define DCS_POWER_MODE_PARTIAL_MODE		BIT(5)
34 #define DCS_POWER_MODE_IDLE_MODE		BIT(6)
35 #define DCS_POWER_MODE_RESERVED_MASK		(BIT(0) | BIT(1) | BIT(7))
36 
37 /**
38  * DOC: overview
39  *
40  * This library provides helpers for MIPI Display Bus Interface (DBI)
41  * compatible display controllers.
42  *
43  * Many controllers for tiny lcd displays are MIPI compliant and can use this
44  * library. If a controller uses registers 0x2A and 0x2B to set the area to
45  * update and uses register 0x2C to write to frame memory, it is most likely
46  * MIPI compliant.
47  *
48  * Only MIPI Type 1 displays are supported since a full frame memory is needed.
49  *
50  * There are 3 MIPI DBI implementation types:
51  *
52  * A. Motorola 6800 type parallel bus
53  *
54  * B. Intel 8080 type parallel bus
55  *
56  * C. SPI type with 3 options:
57  *
58  *    1. 9-bit with the Data/Command signal as the ninth bit
59  *    2. Same as above except it's sent as 16 bits
60  *    3. 8-bit with the Data/Command signal as a separate D/CX pin
61  *
62  * Currently mipi_dbi only supports Type C options 1 and 3 with
63  * mipi_dbi_spi_init().
64  */
65 
66 #define MIPI_DBI_DEBUG_COMMAND(cmd, data, len) \
67 ({ \
68 	if (!len) \
69 		DRM_DEBUG_DRIVER("cmd=%02x\n", cmd); \
70 	else if (len <= 32) \
71 		DRM_DEBUG_DRIVER("cmd=%02x, par=%*ph\n", cmd, (int)len, data);\
72 	else \
73 		DRM_DEBUG_DRIVER("cmd=%02x, len=%zu\n", cmd, len); \
74 })
75 
76 static const u8 mipi_dbi_dcs_read_commands[] = {
77 	MIPI_DCS_GET_DISPLAY_ID,
78 	MIPI_DCS_GET_RED_CHANNEL,
79 	MIPI_DCS_GET_GREEN_CHANNEL,
80 	MIPI_DCS_GET_BLUE_CHANNEL,
81 	MIPI_DCS_GET_DISPLAY_STATUS,
82 	MIPI_DCS_GET_POWER_MODE,
83 	MIPI_DCS_GET_ADDRESS_MODE,
84 	MIPI_DCS_GET_PIXEL_FORMAT,
85 	MIPI_DCS_GET_DISPLAY_MODE,
86 	MIPI_DCS_GET_SIGNAL_MODE,
87 	MIPI_DCS_GET_DIAGNOSTIC_RESULT,
88 	MIPI_DCS_READ_MEMORY_START,
89 	MIPI_DCS_READ_MEMORY_CONTINUE,
90 	MIPI_DCS_GET_SCANLINE,
91 	MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
92 	MIPI_DCS_GET_CONTROL_DISPLAY,
93 	MIPI_DCS_GET_POWER_SAVE,
94 	MIPI_DCS_GET_CABC_MIN_BRIGHTNESS,
95 	MIPI_DCS_READ_DDB_START,
96 	MIPI_DCS_READ_DDB_CONTINUE,
97 	0, /* sentinel */
98 };
99 
100 static bool mipi_dbi_command_is_read(struct mipi_dbi *dbi, u8 cmd)
101 {
102 	unsigned int i;
103 
104 	if (!dbi->read_commands)
105 		return false;
106 
107 	for (i = 0; i < 0xff; i++) {
108 		if (!dbi->read_commands[i])
109 			return false;
110 		if (cmd == dbi->read_commands[i])
111 			return true;
112 	}
113 
114 	return false;
115 }
116 
117 /**
118  * mipi_dbi_command_read - MIPI DCS read command
119  * @dbi: MIPI DBI structure
120  * @cmd: Command
121  * @val: Value read
122  *
123  * Send MIPI DCS read command to the controller.
124  *
125  * Returns:
126  * Zero on success, negative error code on failure.
127  */
128 int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val)
129 {
130 	if (!dbi->read_commands)
131 		return -EACCES;
132 
133 	if (!mipi_dbi_command_is_read(dbi, cmd))
134 		return -EINVAL;
135 
136 	return mipi_dbi_command_buf(dbi, cmd, val, 1);
137 }
138 EXPORT_SYMBOL(mipi_dbi_command_read);
139 
140 /**
141  * mipi_dbi_command_buf - MIPI DCS command with parameter(s) in an array
142  * @dbi: MIPI DBI structure
143  * @cmd: Command
144  * @data: Parameter buffer
145  * @len: Buffer length
146  *
147  * Returns:
148  * Zero on success, negative error code on failure.
149  */
150 int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len)
151 {
152 	u8 *cmdbuf;
153 	int ret;
154 
155 	/* SPI requires dma-safe buffers */
156 	cmdbuf = kmemdup(&cmd, 1, GFP_KERNEL);
157 	if (!cmdbuf)
158 		return -ENOMEM;
159 
160 	mutex_lock(&dbi->cmdlock);
161 	ret = dbi->command(dbi, cmdbuf, data, len);
162 	mutex_unlock(&dbi->cmdlock);
163 
164 	kfree(cmdbuf);
165 
166 	return ret;
167 }
168 EXPORT_SYMBOL(mipi_dbi_command_buf);
169 
170 /* This should only be used by mipi_dbi_command() */
171 int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
172 			      size_t len)
173 {
174 	u8 *buf;
175 	int ret;
176 
177 	buf = kmemdup(data, len, GFP_KERNEL);
178 	if (!buf)
179 		return -ENOMEM;
180 
181 	ret = mipi_dbi_command_buf(dbi, cmd, buf, len);
182 
183 	kfree(buf);
184 
185 	return ret;
186 }
187 EXPORT_SYMBOL(mipi_dbi_command_stackbuf);
188 
189 /**
190  * mipi_dbi_buf_copy - Copy a framebuffer, transforming it if necessary
191  * @dst: The destination buffer
192  * @fb: The source framebuffer
193  * @clip: Clipping rectangle of the area to be copied
194  * @swap: When true, swap MSB/LSB of 16-bit values
195  *
196  * Returns:
197  * Zero on success, negative error code on failure.
198  */
199 int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
200 		      struct drm_rect *clip, bool swap)
201 {
202 	struct drm_gem_object *gem = drm_gem_fb_get_obj(fb, 0);
203 	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(gem);
204 	void *src = cma_obj->vaddr;
205 	int ret;
206 
207 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
208 	if (ret)
209 		return ret;
210 
211 	switch (fb->format->format) {
212 	case DRM_FORMAT_RGB565:
213 		if (swap)
214 			drm_fb_swab(dst, src, fb, clip, !gem->import_attach);
215 		else
216 			drm_fb_memcpy(dst, src, fb, clip);
217 		break;
218 	case DRM_FORMAT_XRGB8888:
219 		drm_fb_xrgb8888_to_rgb565(dst, src, fb, clip, swap);
220 		break;
221 	default:
222 		drm_err_once(fb->dev, "Format is not supported: %p4cc\n",
223 			     &fb->format->format);
224 		return -EINVAL;
225 	}
226 
227 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
228 
229 	return ret;
230 }
231 EXPORT_SYMBOL(mipi_dbi_buf_copy);
232 
233 static void mipi_dbi_set_window_address(struct mipi_dbi_dev *dbidev,
234 					unsigned int xs, unsigned int xe,
235 					unsigned int ys, unsigned int ye)
236 {
237 	struct mipi_dbi *dbi = &dbidev->dbi;
238 
239 	xs += dbidev->left_offset;
240 	xe += dbidev->left_offset;
241 	ys += dbidev->top_offset;
242 	ye += dbidev->top_offset;
243 
244 	mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS, (xs >> 8) & 0xff,
245 			 xs & 0xff, (xe >> 8) & 0xff, xe & 0xff);
246 	mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS, (ys >> 8) & 0xff,
247 			 ys & 0xff, (ye >> 8) & 0xff, ye & 0xff);
248 }
249 
250 static void mipi_dbi_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
251 {
252 	struct drm_gem_object *gem = drm_gem_fb_get_obj(fb, 0);
253 	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(gem);
254 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
255 	unsigned int height = rect->y2 - rect->y1;
256 	unsigned int width = rect->x2 - rect->x1;
257 	struct mipi_dbi *dbi = &dbidev->dbi;
258 	bool swap = dbi->swap_bytes;
259 	int idx, ret = 0;
260 	bool full;
261 	void *tr;
262 
263 	if (WARN_ON(!fb))
264 		return;
265 
266 	if (!drm_dev_enter(fb->dev, &idx))
267 		return;
268 
269 	full = width == fb->width && height == fb->height;
270 
271 	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
272 
273 	if (!dbi->dc || !full || swap ||
274 	    fb->format->format == DRM_FORMAT_XRGB8888) {
275 		tr = dbidev->tx_buf;
276 		ret = mipi_dbi_buf_copy(dbidev->tx_buf, fb, rect, swap);
277 		if (ret)
278 			goto err_msg;
279 	} else {
280 		tr = cma_obj->vaddr;
281 	}
282 
283 	mipi_dbi_set_window_address(dbidev, rect->x1, rect->x2 - 1, rect->y1,
284 				    rect->y2 - 1);
285 
286 	ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START, tr,
287 				   width * height * 2);
288 err_msg:
289 	if (ret)
290 		drm_err_once(fb->dev, "Failed to update display %d\n", ret);
291 
292 	drm_dev_exit(idx);
293 }
294 
295 /**
296  * mipi_dbi_pipe_update - Display pipe update helper
297  * @pipe: Simple display pipe
298  * @old_state: Old plane state
299  *
300  * This function handles framebuffer flushing and vblank events. Drivers can use
301  * this as their &drm_simple_display_pipe_funcs->update callback.
302  */
303 void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
304 			  struct drm_plane_state *old_state)
305 {
306 	struct drm_plane_state *state = pipe->plane.state;
307 	struct drm_rect rect;
308 
309 	if (!pipe->crtc.state->active)
310 		return;
311 
312 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
313 		mipi_dbi_fb_dirty(state->fb, &rect);
314 }
315 EXPORT_SYMBOL(mipi_dbi_pipe_update);
316 
317 /**
318  * mipi_dbi_enable_flush - MIPI DBI enable helper
319  * @dbidev: MIPI DBI device structure
320  * @crtc_state: CRTC state
321  * @plane_state: Plane state
322  *
323  * Flushes the whole framebuffer and enables the backlight. Drivers can use this
324  * in their &drm_simple_display_pipe_funcs->enable callback.
325  *
326  * Note: Drivers which don't use mipi_dbi_pipe_update() because they have custom
327  * framebuffer flushing, can't use this function since they both use the same
328  * flushing code.
329  */
330 void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
331 			   struct drm_crtc_state *crtc_state,
332 			   struct drm_plane_state *plane_state)
333 {
334 	struct drm_framebuffer *fb = plane_state->fb;
335 	struct drm_rect rect = {
336 		.x1 = 0,
337 		.x2 = fb->width,
338 		.y1 = 0,
339 		.y2 = fb->height,
340 	};
341 	int idx;
342 
343 	if (!drm_dev_enter(&dbidev->drm, &idx))
344 		return;
345 
346 	mipi_dbi_fb_dirty(fb, &rect);
347 	backlight_enable(dbidev->backlight);
348 
349 	drm_dev_exit(idx);
350 }
351 EXPORT_SYMBOL(mipi_dbi_enable_flush);
352 
353 static void mipi_dbi_blank(struct mipi_dbi_dev *dbidev)
354 {
355 	struct drm_device *drm = &dbidev->drm;
356 	u16 height = drm->mode_config.min_height;
357 	u16 width = drm->mode_config.min_width;
358 	struct mipi_dbi *dbi = &dbidev->dbi;
359 	size_t len = width * height * 2;
360 	int idx;
361 
362 	if (!drm_dev_enter(drm, &idx))
363 		return;
364 
365 	memset(dbidev->tx_buf, 0, len);
366 
367 	mipi_dbi_set_window_address(dbidev, 0, width - 1, 0, height - 1);
368 	mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START,
369 			     (u8 *)dbidev->tx_buf, len);
370 
371 	drm_dev_exit(idx);
372 }
373 
374 /**
375  * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper
376  * @pipe: Display pipe
377  *
378  * This function disables backlight if present, if not the display memory is
379  * blanked. The regulator is disabled if in use. Drivers can use this as their
380  * &drm_simple_display_pipe_funcs->disable callback.
381  */
382 void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe)
383 {
384 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
385 
386 	DRM_DEBUG_KMS("\n");
387 
388 	if (dbidev->backlight)
389 		backlight_disable(dbidev->backlight);
390 	else
391 		mipi_dbi_blank(dbidev);
392 
393 	if (dbidev->regulator)
394 		regulator_disable(dbidev->regulator);
395 }
396 EXPORT_SYMBOL(mipi_dbi_pipe_disable);
397 
398 static int mipi_dbi_connector_get_modes(struct drm_connector *connector)
399 {
400 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(connector->dev);
401 	struct drm_display_mode *mode;
402 
403 	mode = drm_mode_duplicate(connector->dev, &dbidev->mode);
404 	if (!mode) {
405 		DRM_ERROR("Failed to duplicate mode\n");
406 		return 0;
407 	}
408 
409 	if (mode->name[0] == '\0')
410 		drm_mode_set_name(mode);
411 
412 	mode->type |= DRM_MODE_TYPE_PREFERRED;
413 	drm_mode_probed_add(connector, mode);
414 
415 	if (mode->width_mm) {
416 		connector->display_info.width_mm = mode->width_mm;
417 		connector->display_info.height_mm = mode->height_mm;
418 	}
419 
420 	return 1;
421 }
422 
423 static const struct drm_connector_helper_funcs mipi_dbi_connector_hfuncs = {
424 	.get_modes = mipi_dbi_connector_get_modes,
425 };
426 
427 static const struct drm_connector_funcs mipi_dbi_connector_funcs = {
428 	.reset = drm_atomic_helper_connector_reset,
429 	.fill_modes = drm_helper_probe_single_connector_modes,
430 	.destroy = drm_connector_cleanup,
431 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
432 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
433 };
434 
435 static int mipi_dbi_rotate_mode(struct drm_display_mode *mode,
436 				unsigned int rotation)
437 {
438 	if (rotation == 0 || rotation == 180) {
439 		return 0;
440 	} else if (rotation == 90 || rotation == 270) {
441 		swap(mode->hdisplay, mode->vdisplay);
442 		swap(mode->hsync_start, mode->vsync_start);
443 		swap(mode->hsync_end, mode->vsync_end);
444 		swap(mode->htotal, mode->vtotal);
445 		swap(mode->width_mm, mode->height_mm);
446 		return 0;
447 	} else {
448 		return -EINVAL;
449 	}
450 }
451 
452 static const struct drm_mode_config_funcs mipi_dbi_mode_config_funcs = {
453 	.fb_create = drm_gem_fb_create_with_dirty,
454 	.atomic_check = drm_atomic_helper_check,
455 	.atomic_commit = drm_atomic_helper_commit,
456 };
457 
458 static const uint32_t mipi_dbi_formats[] = {
459 	DRM_FORMAT_RGB565,
460 	DRM_FORMAT_XRGB8888,
461 };
462 
463 /**
464  * mipi_dbi_dev_init_with_formats - MIPI DBI device initialization with custom formats
465  * @dbidev: MIPI DBI device structure to initialize
466  * @funcs: Display pipe functions
467  * @formats: Array of supported formats (DRM_FORMAT\_\*).
468  * @format_count: Number of elements in @formats
469  * @mode: Display mode
470  * @rotation: Initial rotation in degrees Counter Clock Wise
471  * @tx_buf_size: Allocate a transmit buffer of this size.
472  *
473  * This function sets up a &drm_simple_display_pipe with a &drm_connector that
474  * has one fixed &drm_display_mode which is rotated according to @rotation.
475  * This mode is used to set the mode config min/max width/height properties.
476  *
477  * Use mipi_dbi_dev_init() if you don't need custom formats.
478  *
479  * Note:
480  * Some of the helper functions expects RGB565 to be the default format and the
481  * transmit buffer sized to fit that.
482  *
483  * Returns:
484  * Zero on success, negative error code on failure.
485  */
486 int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
487 				   const struct drm_simple_display_pipe_funcs *funcs,
488 				   const uint32_t *formats, unsigned int format_count,
489 				   const struct drm_display_mode *mode,
490 				   unsigned int rotation, size_t tx_buf_size)
491 {
492 	static const uint64_t modifiers[] = {
493 		DRM_FORMAT_MOD_LINEAR,
494 		DRM_FORMAT_MOD_INVALID
495 	};
496 	struct drm_device *drm = &dbidev->drm;
497 	int ret;
498 
499 	if (!dbidev->dbi.command)
500 		return -EINVAL;
501 
502 	ret = drmm_mode_config_init(drm);
503 	if (ret)
504 		return ret;
505 
506 	dbidev->tx_buf = devm_kmalloc(drm->dev, tx_buf_size, GFP_KERNEL);
507 	if (!dbidev->tx_buf)
508 		return -ENOMEM;
509 
510 	drm_mode_copy(&dbidev->mode, mode);
511 	ret = mipi_dbi_rotate_mode(&dbidev->mode, rotation);
512 	if (ret) {
513 		DRM_ERROR("Illegal rotation value %u\n", rotation);
514 		return -EINVAL;
515 	}
516 
517 	drm_connector_helper_add(&dbidev->connector, &mipi_dbi_connector_hfuncs);
518 	ret = drm_connector_init(drm, &dbidev->connector, &mipi_dbi_connector_funcs,
519 				 DRM_MODE_CONNECTOR_SPI);
520 	if (ret)
521 		return ret;
522 
523 	ret = drm_simple_display_pipe_init(drm, &dbidev->pipe, funcs, formats, format_count,
524 					   modifiers, &dbidev->connector);
525 	if (ret)
526 		return ret;
527 
528 	drm_plane_enable_fb_damage_clips(&dbidev->pipe.plane);
529 
530 	drm->mode_config.funcs = &mipi_dbi_mode_config_funcs;
531 	drm->mode_config.min_width = dbidev->mode.hdisplay;
532 	drm->mode_config.max_width = dbidev->mode.hdisplay;
533 	drm->mode_config.min_height = dbidev->mode.vdisplay;
534 	drm->mode_config.max_height = dbidev->mode.vdisplay;
535 	dbidev->rotation = rotation;
536 
537 	DRM_DEBUG_KMS("rotation = %u\n", rotation);
538 
539 	return 0;
540 }
541 EXPORT_SYMBOL(mipi_dbi_dev_init_with_formats);
542 
543 /**
544  * mipi_dbi_dev_init - MIPI DBI device initialization
545  * @dbidev: MIPI DBI device structure to initialize
546  * @funcs: Display pipe functions
547  * @mode: Display mode
548  * @rotation: Initial rotation in degrees Counter Clock Wise
549  *
550  * This function sets up a &drm_simple_display_pipe with a &drm_connector that
551  * has one fixed &drm_display_mode which is rotated according to @rotation.
552  * This mode is used to set the mode config min/max width/height properties.
553  * Additionally &mipi_dbi.tx_buf is allocated.
554  *
555  * Supported formats: Native RGB565 and emulated XRGB8888.
556  *
557  * Returns:
558  * Zero on success, negative error code on failure.
559  */
560 int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
561 		      const struct drm_simple_display_pipe_funcs *funcs,
562 		      const struct drm_display_mode *mode, unsigned int rotation)
563 {
564 	size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
565 
566 	dbidev->drm.mode_config.preferred_depth = 16;
567 
568 	return mipi_dbi_dev_init_with_formats(dbidev, funcs, mipi_dbi_formats,
569 					      ARRAY_SIZE(mipi_dbi_formats), mode,
570 					      rotation, bufsize);
571 }
572 EXPORT_SYMBOL(mipi_dbi_dev_init);
573 
574 /**
575  * mipi_dbi_hw_reset - Hardware reset of controller
576  * @dbi: MIPI DBI structure
577  *
578  * Reset controller if the &mipi_dbi->reset gpio is set.
579  */
580 void mipi_dbi_hw_reset(struct mipi_dbi *dbi)
581 {
582 	if (!dbi->reset)
583 		return;
584 
585 	gpiod_set_value_cansleep(dbi->reset, 0);
586 	usleep_range(20, 1000);
587 	gpiod_set_value_cansleep(dbi->reset, 1);
588 	msleep(120);
589 }
590 EXPORT_SYMBOL(mipi_dbi_hw_reset);
591 
592 /**
593  * mipi_dbi_display_is_on - Check if display is on
594  * @dbi: MIPI DBI structure
595  *
596  * This function checks the Power Mode register (if readable) to see if
597  * display output is turned on. This can be used to see if the bootloader
598  * has already turned on the display avoiding flicker when the pipeline is
599  * enabled.
600  *
601  * Returns:
602  * true if the display can be verified to be on, false otherwise.
603  */
604 bool mipi_dbi_display_is_on(struct mipi_dbi *dbi)
605 {
606 	u8 val;
607 
608 	if (mipi_dbi_command_read(dbi, MIPI_DCS_GET_POWER_MODE, &val))
609 		return false;
610 
611 	val &= ~DCS_POWER_MODE_RESERVED_MASK;
612 
613 	/* The poweron/reset value is 08h DCS_POWER_MODE_DISPLAY_NORMAL_MODE */
614 	if (val != (DCS_POWER_MODE_DISPLAY |
615 	    DCS_POWER_MODE_DISPLAY_NORMAL_MODE | DCS_POWER_MODE_SLEEP_MODE))
616 		return false;
617 
618 	DRM_DEBUG_DRIVER("Display is ON\n");
619 
620 	return true;
621 }
622 EXPORT_SYMBOL(mipi_dbi_display_is_on);
623 
624 static int mipi_dbi_poweron_reset_conditional(struct mipi_dbi_dev *dbidev, bool cond)
625 {
626 	struct device *dev = dbidev->drm.dev;
627 	struct mipi_dbi *dbi = &dbidev->dbi;
628 	int ret;
629 
630 	if (dbidev->regulator) {
631 		ret = regulator_enable(dbidev->regulator);
632 		if (ret) {
633 			DRM_DEV_ERROR(dev, "Failed to enable regulator (%d)\n", ret);
634 			return ret;
635 		}
636 	}
637 
638 	if (cond && mipi_dbi_display_is_on(dbi))
639 		return 1;
640 
641 	mipi_dbi_hw_reset(dbi);
642 	ret = mipi_dbi_command(dbi, MIPI_DCS_SOFT_RESET);
643 	if (ret) {
644 		DRM_DEV_ERROR(dev, "Failed to send reset command (%d)\n", ret);
645 		if (dbidev->regulator)
646 			regulator_disable(dbidev->regulator);
647 		return ret;
648 	}
649 
650 	/*
651 	 * If we did a hw reset, we know the controller is in Sleep mode and
652 	 * per MIPI DSC spec should wait 5ms after soft reset. If we didn't,
653 	 * we assume worst case and wait 120ms.
654 	 */
655 	if (dbi->reset)
656 		usleep_range(5000, 20000);
657 	else
658 		msleep(120);
659 
660 	return 0;
661 }
662 
663 /**
664  * mipi_dbi_poweron_reset - MIPI DBI poweron and reset
665  * @dbidev: MIPI DBI device structure
666  *
667  * This function enables the regulator if used and does a hardware and software
668  * reset.
669  *
670  * Returns:
671  * Zero on success, or a negative error code.
672  */
673 int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev)
674 {
675 	return mipi_dbi_poweron_reset_conditional(dbidev, false);
676 }
677 EXPORT_SYMBOL(mipi_dbi_poweron_reset);
678 
679 /**
680  * mipi_dbi_poweron_conditional_reset - MIPI DBI poweron and conditional reset
681  * @dbidev: MIPI DBI device structure
682  *
683  * This function enables the regulator if used and if the display is off, it
684  * does a hardware and software reset. If mipi_dbi_display_is_on() determines
685  * that the display is on, no reset is performed.
686  *
687  * Returns:
688  * Zero if the controller was reset, 1 if the display was already on, or a
689  * negative error code.
690  */
691 int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev)
692 {
693 	return mipi_dbi_poweron_reset_conditional(dbidev, true);
694 }
695 EXPORT_SYMBOL(mipi_dbi_poweron_conditional_reset);
696 
697 #if IS_ENABLED(CONFIG_SPI)
698 
699 /**
700  * mipi_dbi_spi_cmd_max_speed - get the maximum SPI bus speed
701  * @spi: SPI device
702  * @len: The transfer buffer length.
703  *
704  * Many controllers have a max speed of 10MHz, but can be pushed way beyond
705  * that. Increase reliability by running pixel data at max speed and the rest
706  * at 10MHz, preventing transfer glitches from messing up the init settings.
707  */
708 u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len)
709 {
710 	if (len > 64)
711 		return 0; /* use default */
712 
713 	return min_t(u32, 10000000, spi->max_speed_hz);
714 }
715 EXPORT_SYMBOL(mipi_dbi_spi_cmd_max_speed);
716 
717 static bool mipi_dbi_machine_little_endian(void)
718 {
719 #if defined(__LITTLE_ENDIAN)
720 	return true;
721 #else
722 	return false;
723 #endif
724 }
725 
726 /*
727  * MIPI DBI Type C Option 1
728  *
729  * If the SPI controller doesn't have 9 bits per word support,
730  * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer.
731  * Pad partial blocks with MIPI_DCS_NOP (zero).
732  * This is how the D/C bit (x) is added:
733  *     x7654321
734  *     0x765432
735  *     10x76543
736  *     210x7654
737  *     3210x765
738  *     43210x76
739  *     543210x7
740  *     6543210x
741  *     76543210
742  */
743 
744 static int mipi_dbi_spi1e_transfer(struct mipi_dbi *dbi, int dc,
745 				   const void *buf, size_t len,
746 				   unsigned int bpw)
747 {
748 	bool swap_bytes = (bpw == 16 && mipi_dbi_machine_little_endian());
749 	size_t chunk, max_chunk = dbi->tx_buf9_len;
750 	struct spi_device *spi = dbi->spi;
751 	struct spi_transfer tr = {
752 		.tx_buf = dbi->tx_buf9,
753 		.bits_per_word = 8,
754 	};
755 	struct spi_message m;
756 	const u8 *src = buf;
757 	int i, ret;
758 	u8 *dst;
759 
760 	if (drm_debug_enabled(DRM_UT_DRIVER))
761 		pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
762 			 __func__, dc, max_chunk);
763 
764 	tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
765 	spi_message_init_with_transfers(&m, &tr, 1);
766 
767 	if (!dc) {
768 		if (WARN_ON_ONCE(len != 1))
769 			return -EINVAL;
770 
771 		/* Command: pad no-op's (zeroes) at beginning of block */
772 		dst = dbi->tx_buf9;
773 		memset(dst, 0, 9);
774 		dst[8] = *src;
775 		tr.len = 9;
776 
777 		return spi_sync(spi, &m);
778 	}
779 
780 	/* max with room for adding one bit per byte */
781 	max_chunk = max_chunk / 9 * 8;
782 	/* but no bigger than len */
783 	max_chunk = min(max_chunk, len);
784 	/* 8 byte blocks */
785 	max_chunk = max_t(size_t, 8, max_chunk & ~0x7);
786 
787 	while (len) {
788 		size_t added = 0;
789 
790 		chunk = min(len, max_chunk);
791 		len -= chunk;
792 		dst = dbi->tx_buf9;
793 
794 		if (chunk < 8) {
795 			u8 val, carry = 0;
796 
797 			/* Data: pad no-op's (zeroes) at end of block */
798 			memset(dst, 0, 9);
799 
800 			if (swap_bytes) {
801 				for (i = 1; i < (chunk + 1); i++) {
802 					val = src[1];
803 					*dst++ = carry | BIT(8 - i) | (val >> i);
804 					carry = val << (8 - i);
805 					i++;
806 					val = src[0];
807 					*dst++ = carry | BIT(8 - i) | (val >> i);
808 					carry = val << (8 - i);
809 					src += 2;
810 				}
811 				*dst++ = carry;
812 			} else {
813 				for (i = 1; i < (chunk + 1); i++) {
814 					val = *src++;
815 					*dst++ = carry | BIT(8 - i) | (val >> i);
816 					carry = val << (8 - i);
817 				}
818 				*dst++ = carry;
819 			}
820 
821 			chunk = 8;
822 			added = 1;
823 		} else {
824 			for (i = 0; i < chunk; i += 8) {
825 				if (swap_bytes) {
826 					*dst++ =                 BIT(7) | (src[1] >> 1);
827 					*dst++ = (src[1] << 7) | BIT(6) | (src[0] >> 2);
828 					*dst++ = (src[0] << 6) | BIT(5) | (src[3] >> 3);
829 					*dst++ = (src[3] << 5) | BIT(4) | (src[2] >> 4);
830 					*dst++ = (src[2] << 4) | BIT(3) | (src[5] >> 5);
831 					*dst++ = (src[5] << 3) | BIT(2) | (src[4] >> 6);
832 					*dst++ = (src[4] << 2) | BIT(1) | (src[7] >> 7);
833 					*dst++ = (src[7] << 1) | BIT(0);
834 					*dst++ = src[6];
835 				} else {
836 					*dst++ =                 BIT(7) | (src[0] >> 1);
837 					*dst++ = (src[0] << 7) | BIT(6) | (src[1] >> 2);
838 					*dst++ = (src[1] << 6) | BIT(5) | (src[2] >> 3);
839 					*dst++ = (src[2] << 5) | BIT(4) | (src[3] >> 4);
840 					*dst++ = (src[3] << 4) | BIT(3) | (src[4] >> 5);
841 					*dst++ = (src[4] << 3) | BIT(2) | (src[5] >> 6);
842 					*dst++ = (src[5] << 2) | BIT(1) | (src[6] >> 7);
843 					*dst++ = (src[6] << 1) | BIT(0);
844 					*dst++ = src[7];
845 				}
846 
847 				src += 8;
848 				added++;
849 			}
850 		}
851 
852 		tr.len = chunk + added;
853 
854 		ret = spi_sync(spi, &m);
855 		if (ret)
856 			return ret;
857 	}
858 
859 	return 0;
860 }
861 
862 static int mipi_dbi_spi1_transfer(struct mipi_dbi *dbi, int dc,
863 				  const void *buf, size_t len,
864 				  unsigned int bpw)
865 {
866 	struct spi_device *spi = dbi->spi;
867 	struct spi_transfer tr = {
868 		.bits_per_word = 9,
869 	};
870 	const u16 *src16 = buf;
871 	const u8 *src8 = buf;
872 	struct spi_message m;
873 	size_t max_chunk;
874 	u16 *dst16;
875 	int ret;
876 
877 	if (!spi_is_bpw_supported(spi, 9))
878 		return mipi_dbi_spi1e_transfer(dbi, dc, buf, len, bpw);
879 
880 	tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
881 	max_chunk = dbi->tx_buf9_len;
882 	dst16 = dbi->tx_buf9;
883 
884 	if (drm_debug_enabled(DRM_UT_DRIVER))
885 		pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
886 			 __func__, dc, max_chunk);
887 
888 	max_chunk = min(max_chunk / 2, len);
889 
890 	spi_message_init_with_transfers(&m, &tr, 1);
891 	tr.tx_buf = dst16;
892 
893 	while (len) {
894 		size_t chunk = min(len, max_chunk);
895 		unsigned int i;
896 
897 		if (bpw == 16 && mipi_dbi_machine_little_endian()) {
898 			for (i = 0; i < (chunk * 2); i += 2) {
899 				dst16[i]     = *src16 >> 8;
900 				dst16[i + 1] = *src16++ & 0xFF;
901 				if (dc) {
902 					dst16[i]     |= 0x0100;
903 					dst16[i + 1] |= 0x0100;
904 				}
905 			}
906 		} else {
907 			for (i = 0; i < chunk; i++) {
908 				dst16[i] = *src8++;
909 				if (dc)
910 					dst16[i] |= 0x0100;
911 			}
912 		}
913 
914 		tr.len = chunk * 2;
915 		len -= chunk;
916 
917 		ret = spi_sync(spi, &m);
918 		if (ret)
919 			return ret;
920 	}
921 
922 	return 0;
923 }
924 
925 static int mipi_dbi_typec1_command_read(struct mipi_dbi *dbi, u8 *cmd,
926 					u8 *data, size_t len)
927 {
928 	struct spi_device *spi = dbi->spi;
929 	u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
930 			     spi->max_speed_hz / 2);
931 	struct spi_transfer tr[2] = {
932 		{
933 			.speed_hz = speed_hz,
934 			.bits_per_word = 9,
935 			.tx_buf = dbi->tx_buf9,
936 			.len = 2,
937 		}, {
938 			.speed_hz = speed_hz,
939 			.bits_per_word = 8,
940 			.len = len,
941 			.rx_buf = data,
942 		},
943 	};
944 	struct spi_message m;
945 	u16 *dst16;
946 	int ret;
947 
948 	if (!len)
949 		return -EINVAL;
950 
951 	if (!spi_is_bpw_supported(spi, 9)) {
952 		/*
953 		 * FIXME: implement something like mipi_dbi_spi1e_transfer() but
954 		 * for reads using emulation.
955 		 */
956 		dev_err(&spi->dev,
957 			"reading on host not supporting 9 bpw not yet implemented\n");
958 		return -EOPNOTSUPP;
959 	}
960 
961 	/*
962 	 * Turn the 8bit command into a 16bit version of the command in the
963 	 * buffer. Only 9 bits of this will be used when executing the actual
964 	 * transfer.
965 	 */
966 	dst16 = dbi->tx_buf9;
967 	dst16[0] = *cmd;
968 
969 	spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
970 	ret = spi_sync(spi, &m);
971 
972 	if (!ret)
973 		MIPI_DBI_DEBUG_COMMAND(*cmd, data, len);
974 
975 	return ret;
976 }
977 
978 static int mipi_dbi_typec1_command(struct mipi_dbi *dbi, u8 *cmd,
979 				   u8 *parameters, size_t num)
980 {
981 	unsigned int bpw = (*cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
982 	int ret;
983 
984 	if (mipi_dbi_command_is_read(dbi, *cmd))
985 		return mipi_dbi_typec1_command_read(dbi, cmd, parameters, num);
986 
987 	MIPI_DBI_DEBUG_COMMAND(*cmd, parameters, num);
988 
989 	ret = mipi_dbi_spi1_transfer(dbi, 0, cmd, 1, 8);
990 	if (ret || !num)
991 		return ret;
992 
993 	return mipi_dbi_spi1_transfer(dbi, 1, parameters, num, bpw);
994 }
995 
996 /* MIPI DBI Type C Option 3 */
997 
998 static int mipi_dbi_typec3_command_read(struct mipi_dbi *dbi, u8 *cmd,
999 					u8 *data, size_t len)
1000 {
1001 	struct spi_device *spi = dbi->spi;
1002 	u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
1003 			     spi->max_speed_hz / 2);
1004 	struct spi_transfer tr[2] = {
1005 		{
1006 			.speed_hz = speed_hz,
1007 			.tx_buf = cmd,
1008 			.len = 1,
1009 		}, {
1010 			.speed_hz = speed_hz,
1011 			.len = len,
1012 		},
1013 	};
1014 	struct spi_message m;
1015 	u8 *buf;
1016 	int ret;
1017 
1018 	if (!len)
1019 		return -EINVAL;
1020 
1021 	/*
1022 	 * Support non-standard 24-bit and 32-bit Nokia read commands which
1023 	 * start with a dummy clock, so we need to read an extra byte.
1024 	 */
1025 	if (*cmd == MIPI_DCS_GET_DISPLAY_ID ||
1026 	    *cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
1027 		if (!(len == 3 || len == 4))
1028 			return -EINVAL;
1029 
1030 		tr[1].len = len + 1;
1031 	}
1032 
1033 	buf = kmalloc(tr[1].len, GFP_KERNEL);
1034 	if (!buf)
1035 		return -ENOMEM;
1036 
1037 	tr[1].rx_buf = buf;
1038 	gpiod_set_value_cansleep(dbi->dc, 0);
1039 
1040 	spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
1041 	ret = spi_sync(spi, &m);
1042 	if (ret)
1043 		goto err_free;
1044 
1045 	if (tr[1].len == len) {
1046 		memcpy(data, buf, len);
1047 	} else {
1048 		unsigned int i;
1049 
1050 		for (i = 0; i < len; i++)
1051 			data[i] = (buf[i] << 1) | (buf[i + 1] >> 7);
1052 	}
1053 
1054 	MIPI_DBI_DEBUG_COMMAND(*cmd, data, len);
1055 
1056 err_free:
1057 	kfree(buf);
1058 
1059 	return ret;
1060 }
1061 
1062 static int mipi_dbi_typec3_command(struct mipi_dbi *dbi, u8 *cmd,
1063 				   u8 *par, size_t num)
1064 {
1065 	struct spi_device *spi = dbi->spi;
1066 	unsigned int bpw = 8;
1067 	u32 speed_hz;
1068 	int ret;
1069 
1070 	if (mipi_dbi_command_is_read(dbi, *cmd))
1071 		return mipi_dbi_typec3_command_read(dbi, cmd, par, num);
1072 
1073 	MIPI_DBI_DEBUG_COMMAND(*cmd, par, num);
1074 
1075 	gpiod_set_value_cansleep(dbi->dc, 0);
1076 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
1077 	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
1078 	if (ret || !num)
1079 		return ret;
1080 
1081 	if (*cmd == MIPI_DCS_WRITE_MEMORY_START && !dbi->swap_bytes)
1082 		bpw = 16;
1083 
1084 	gpiod_set_value_cansleep(dbi->dc, 1);
1085 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
1086 
1087 	return mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
1088 }
1089 
1090 /**
1091  * mipi_dbi_spi_init - Initialize MIPI DBI SPI interface
1092  * @spi: SPI device
1093  * @dbi: MIPI DBI structure to initialize
1094  * @dc: D/C gpio (optional)
1095  *
1096  * This function sets &mipi_dbi->command, enables &mipi_dbi->read_commands for the
1097  * usual read commands. It should be followed by a call to mipi_dbi_dev_init() or
1098  * a driver-specific init.
1099  *
1100  * If @dc is set, a Type C Option 3 interface is assumed, if not
1101  * Type C Option 1.
1102  *
1103  * If the SPI master driver doesn't support the necessary bits per word,
1104  * the following transformation is used:
1105  *
1106  * - 9-bit: reorder buffer as 9x 8-bit words, padded with no-op command.
1107  * - 16-bit: if big endian send as 8-bit, if little endian swap bytes
1108  *
1109  * Returns:
1110  * Zero on success, negative error code on failure.
1111  */
1112 int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
1113 		      struct gpio_desc *dc)
1114 {
1115 	struct device *dev = &spi->dev;
1116 	int ret;
1117 
1118 	/*
1119 	 * Even though it's not the SPI device that does DMA (the master does),
1120 	 * the dma mask is necessary for the dma_alloc_wc() in
1121 	 * drm_gem_cma_create(). The dma_addr returned will be a physical
1122 	 * address which might be different from the bus address, but this is
1123 	 * not a problem since the address will not be used.
1124 	 * The virtual address is used in the transfer and the SPI core
1125 	 * re-maps it on the SPI master device using the DMA streaming API
1126 	 * (spi_map_buf()).
1127 	 */
1128 	if (!dev->coherent_dma_mask) {
1129 		ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1130 		if (ret) {
1131 			dev_warn(dev, "Failed to set dma mask %d\n", ret);
1132 			return ret;
1133 		}
1134 	}
1135 
1136 	dbi->spi = spi;
1137 	dbi->read_commands = mipi_dbi_dcs_read_commands;
1138 
1139 	if (dc) {
1140 		dbi->command = mipi_dbi_typec3_command;
1141 		dbi->dc = dc;
1142 		if (mipi_dbi_machine_little_endian() && !spi_is_bpw_supported(spi, 16))
1143 			dbi->swap_bytes = true;
1144 	} else {
1145 		dbi->command = mipi_dbi_typec1_command;
1146 		dbi->tx_buf9_len = SZ_16K;
1147 		dbi->tx_buf9 = devm_kmalloc(dev, dbi->tx_buf9_len, GFP_KERNEL);
1148 		if (!dbi->tx_buf9)
1149 			return -ENOMEM;
1150 	}
1151 
1152 	mutex_init(&dbi->cmdlock);
1153 
1154 	DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
1155 
1156 	return 0;
1157 }
1158 EXPORT_SYMBOL(mipi_dbi_spi_init);
1159 
1160 /**
1161  * mipi_dbi_spi_transfer - SPI transfer helper
1162  * @spi: SPI device
1163  * @speed_hz: Override speed (optional)
1164  * @bpw: Bits per word
1165  * @buf: Buffer to transfer
1166  * @len: Buffer length
1167  *
1168  * This SPI transfer helper breaks up the transfer of @buf into chunks which
1169  * the SPI controller driver can handle.
1170  *
1171  * Returns:
1172  * Zero on success, negative error code on failure.
1173  */
1174 int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
1175 			  u8 bpw, const void *buf, size_t len)
1176 {
1177 	size_t max_chunk = spi_max_transfer_size(spi);
1178 	struct spi_transfer tr = {
1179 		.bits_per_word = bpw,
1180 		.speed_hz = speed_hz,
1181 	};
1182 	struct spi_message m;
1183 	size_t chunk;
1184 	int ret;
1185 
1186 	spi_message_init_with_transfers(&m, &tr, 1);
1187 
1188 	while (len) {
1189 		chunk = min(len, max_chunk);
1190 
1191 		tr.tx_buf = buf;
1192 		tr.len = chunk;
1193 		buf += chunk;
1194 		len -= chunk;
1195 
1196 		ret = spi_sync(spi, &m);
1197 		if (ret)
1198 			return ret;
1199 	}
1200 
1201 	return 0;
1202 }
1203 EXPORT_SYMBOL(mipi_dbi_spi_transfer);
1204 
1205 #endif /* CONFIG_SPI */
1206 
1207 #ifdef CONFIG_DEBUG_FS
1208 
1209 static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
1210 					      const char __user *ubuf,
1211 					      size_t count, loff_t *ppos)
1212 {
1213 	struct seq_file *m = file->private_data;
1214 	struct mipi_dbi_dev *dbidev = m->private;
1215 	u8 val, cmd = 0, parameters[64];
1216 	char *buf, *pos, *token;
1217 	int i, ret, idx;
1218 
1219 	if (!drm_dev_enter(&dbidev->drm, &idx))
1220 		return -ENODEV;
1221 
1222 	buf = memdup_user_nul(ubuf, count);
1223 	if (IS_ERR(buf)) {
1224 		ret = PTR_ERR(buf);
1225 		goto err_exit;
1226 	}
1227 
1228 	/* strip trailing whitespace */
1229 	for (i = count - 1; i > 0; i--)
1230 		if (isspace(buf[i]))
1231 			buf[i] = '\0';
1232 		else
1233 			break;
1234 	i = 0;
1235 	pos = buf;
1236 	while (pos) {
1237 		token = strsep(&pos, " ");
1238 		if (!token) {
1239 			ret = -EINVAL;
1240 			goto err_free;
1241 		}
1242 
1243 		ret = kstrtou8(token, 16, &val);
1244 		if (ret < 0)
1245 			goto err_free;
1246 
1247 		if (token == buf)
1248 			cmd = val;
1249 		else
1250 			parameters[i++] = val;
1251 
1252 		if (i == 64) {
1253 			ret = -E2BIG;
1254 			goto err_free;
1255 		}
1256 	}
1257 
1258 	ret = mipi_dbi_command_buf(&dbidev->dbi, cmd, parameters, i);
1259 
1260 err_free:
1261 	kfree(buf);
1262 err_exit:
1263 	drm_dev_exit(idx);
1264 
1265 	return ret < 0 ? ret : count;
1266 }
1267 
1268 static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused)
1269 {
1270 	struct mipi_dbi_dev *dbidev = m->private;
1271 	struct mipi_dbi *dbi = &dbidev->dbi;
1272 	u8 cmd, val[4];
1273 	int ret, idx;
1274 	size_t len;
1275 
1276 	if (!drm_dev_enter(&dbidev->drm, &idx))
1277 		return -ENODEV;
1278 
1279 	for (cmd = 0; cmd < 255; cmd++) {
1280 		if (!mipi_dbi_command_is_read(dbi, cmd))
1281 			continue;
1282 
1283 		switch (cmd) {
1284 		case MIPI_DCS_READ_MEMORY_START:
1285 		case MIPI_DCS_READ_MEMORY_CONTINUE:
1286 			len = 2;
1287 			break;
1288 		case MIPI_DCS_GET_DISPLAY_ID:
1289 			len = 3;
1290 			break;
1291 		case MIPI_DCS_GET_DISPLAY_STATUS:
1292 			len = 4;
1293 			break;
1294 		default:
1295 			len = 1;
1296 			break;
1297 		}
1298 
1299 		seq_printf(m, "%02x: ", cmd);
1300 		ret = mipi_dbi_command_buf(dbi, cmd, val, len);
1301 		if (ret) {
1302 			seq_puts(m, "XX\n");
1303 			continue;
1304 		}
1305 		seq_printf(m, "%*phN\n", (int)len, val);
1306 	}
1307 
1308 	drm_dev_exit(idx);
1309 
1310 	return 0;
1311 }
1312 
1313 static int mipi_dbi_debugfs_command_open(struct inode *inode,
1314 					 struct file *file)
1315 {
1316 	return single_open(file, mipi_dbi_debugfs_command_show,
1317 			   inode->i_private);
1318 }
1319 
1320 static const struct file_operations mipi_dbi_debugfs_command_fops = {
1321 	.owner = THIS_MODULE,
1322 	.open = mipi_dbi_debugfs_command_open,
1323 	.read = seq_read,
1324 	.llseek = seq_lseek,
1325 	.release = single_release,
1326 	.write = mipi_dbi_debugfs_command_write,
1327 };
1328 
1329 /**
1330  * mipi_dbi_debugfs_init - Create debugfs entries
1331  * @minor: DRM minor
1332  *
1333  * This function creates a 'command' debugfs file for sending commands to the
1334  * controller or getting the read command values.
1335  * Drivers can use this as their &drm_driver->debugfs_init callback.
1336  *
1337  */
1338 void mipi_dbi_debugfs_init(struct drm_minor *minor)
1339 {
1340 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(minor->dev);
1341 	umode_t mode = S_IFREG | S_IWUSR;
1342 
1343 	if (dbidev->dbi.read_commands)
1344 		mode |= S_IRUGO;
1345 	debugfs_create_file("command", mode, minor->debugfs_root, dbidev,
1346 			    &mipi_dbi_debugfs_command_fops);
1347 }
1348 EXPORT_SYMBOL(mipi_dbi_debugfs_init);
1349 
1350 #endif
1351 
1352 MODULE_LICENSE("GPL");
1353