drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / tinydrm / mipi-dbi.c
blobcb3441e51d5f03f4c3e1f6b82cb091ef64af7637
1 /*
2 * MIPI Display Bus Interface (DBI) LCD controller support
4 * Copyright 2016 Noralf Trønnes
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <drm/drm_gem_framebuffer_helper.h>
13 #include <drm/tinydrm/mipi-dbi.h>
14 #include <drm/tinydrm/tinydrm-helpers.h>
15 #include <linux/debugfs.h>
16 #include <linux/dma-buf.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/module.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spi/spi.h>
21 #include <video/mipi_display.h>
23 #define MIPI_DBI_MAX_SPI_READ_SPEED 2000000 /* 2MHz */
25 #define DCS_POWER_MODE_DISPLAY BIT(2)
26 #define DCS_POWER_MODE_DISPLAY_NORMAL_MODE BIT(3)
27 #define DCS_POWER_MODE_SLEEP_MODE BIT(4)
28 #define DCS_POWER_MODE_PARTIAL_MODE BIT(5)
29 #define DCS_POWER_MODE_IDLE_MODE BIT(6)
30 #define DCS_POWER_MODE_RESERVED_MASK (BIT(0) | BIT(1) | BIT(7))
32 /**
33 * DOC: overview
35 * This library provides helpers for MIPI Display Bus Interface (DBI)
36 * compatible display controllers.
38 * Many controllers for tiny lcd displays are MIPI compliant and can use this
39 * library. If a controller uses registers 0x2A and 0x2B to set the area to
40 * update and uses register 0x2C to write to frame memory, it is most likely
41 * MIPI compliant.
43 * Only MIPI Type 1 displays are supported since a full frame memory is needed.
45 * There are 3 MIPI DBI implementation types:
47 * A. Motorola 6800 type parallel bus
49 * B. Intel 8080 type parallel bus
51 * C. SPI type with 3 options:
53 * 1. 9-bit with the Data/Command signal as the ninth bit
54 * 2. Same as above except it's sent as 16 bits
55 * 3. 8-bit with the Data/Command signal as a separate D/CX pin
57 * Currently mipi_dbi only supports Type C options 1 and 3 with
58 * mipi_dbi_spi_init().
61 #define MIPI_DBI_DEBUG_COMMAND(cmd, data, len) \
62 ({ \
63 if (!len) \
64 DRM_DEBUG_DRIVER("cmd=%02x\n", cmd); \
65 else if (len <= 32) \
66 DRM_DEBUG_DRIVER("cmd=%02x, par=%*ph\n", cmd, (int)len, data);\
67 else \
68 DRM_DEBUG_DRIVER("cmd=%02x, len=%zu\n", cmd, len); \
71 static const u8 mipi_dbi_dcs_read_commands[] = {
72 MIPI_DCS_GET_DISPLAY_ID,
73 MIPI_DCS_GET_RED_CHANNEL,
74 MIPI_DCS_GET_GREEN_CHANNEL,
75 MIPI_DCS_GET_BLUE_CHANNEL,
76 MIPI_DCS_GET_DISPLAY_STATUS,
77 MIPI_DCS_GET_POWER_MODE,
78 MIPI_DCS_GET_ADDRESS_MODE,
79 MIPI_DCS_GET_PIXEL_FORMAT,
80 MIPI_DCS_GET_DISPLAY_MODE,
81 MIPI_DCS_GET_SIGNAL_MODE,
82 MIPI_DCS_GET_DIAGNOSTIC_RESULT,
83 MIPI_DCS_READ_MEMORY_START,
84 MIPI_DCS_READ_MEMORY_CONTINUE,
85 MIPI_DCS_GET_SCANLINE,
86 MIPI_DCS_GET_DISPLAY_BRIGHTNESS,
87 MIPI_DCS_GET_CONTROL_DISPLAY,
88 MIPI_DCS_GET_POWER_SAVE,
89 MIPI_DCS_GET_CABC_MIN_BRIGHTNESS,
90 MIPI_DCS_READ_DDB_START,
91 MIPI_DCS_READ_DDB_CONTINUE,
92 0, /* sentinel */
95 static bool mipi_dbi_command_is_read(struct mipi_dbi *mipi, u8 cmd)
97 unsigned int i;
99 if (!mipi->read_commands)
100 return false;
102 for (i = 0; i < 0xff; i++) {
103 if (!mipi->read_commands[i])
104 return false;
105 if (cmd == mipi->read_commands[i])
106 return true;
109 return false;
113 * mipi_dbi_command_read - MIPI DCS read command
114 * @mipi: MIPI structure
115 * @cmd: Command
116 * @val: Value read
118 * Send MIPI DCS read command to the controller.
120 * Returns:
121 * Zero on success, negative error code on failure.
123 int mipi_dbi_command_read(struct mipi_dbi *mipi, u8 cmd, u8 *val)
125 if (!mipi->read_commands)
126 return -EACCES;
128 if (!mipi_dbi_command_is_read(mipi, cmd))
129 return -EINVAL;
131 return mipi_dbi_command_buf(mipi, cmd, val, 1);
133 EXPORT_SYMBOL(mipi_dbi_command_read);
136 * mipi_dbi_command_buf - MIPI DCS command with parameter(s) in an array
137 * @mipi: MIPI structure
138 * @cmd: Command
139 * @data: Parameter buffer
140 * @len: Buffer length
142 * Returns:
143 * Zero on success, negative error code on failure.
145 int mipi_dbi_command_buf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len)
147 int ret;
149 mutex_lock(&mipi->cmdlock);
150 ret = mipi->command(mipi, cmd, data, len);
151 mutex_unlock(&mipi->cmdlock);
153 return ret;
155 EXPORT_SYMBOL(mipi_dbi_command_buf);
158 * mipi_dbi_buf_copy - Copy a framebuffer, transforming it if necessary
159 * @dst: The destination buffer
160 * @fb: The source framebuffer
161 * @clip: Clipping rectangle of the area to be copied
162 * @swap: When true, swap MSB/LSB of 16-bit values
164 * Returns:
165 * Zero on success, negative error code on failure.
167 int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
168 struct drm_clip_rect *clip, bool swap)
170 struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
171 struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
172 struct drm_format_name_buf format_name;
173 void *src = cma_obj->vaddr;
174 int ret = 0;
176 if (import_attach) {
177 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
178 DMA_FROM_DEVICE);
179 if (ret)
180 return ret;
183 switch (fb->format->format) {
184 case DRM_FORMAT_RGB565:
185 if (swap)
186 tinydrm_swab16(dst, src, fb, clip);
187 else
188 tinydrm_memcpy(dst, src, fb, clip);
189 break;
190 case DRM_FORMAT_XRGB8888:
191 tinydrm_xrgb8888_to_rgb565(dst, src, fb, clip, swap);
192 break;
193 default:
194 dev_err_once(fb->dev->dev, "Format is not supported: %s\n",
195 drm_get_format_name(fb->format->format,
196 &format_name));
197 return -EINVAL;
200 if (import_attach)
201 ret = dma_buf_end_cpu_access(import_attach->dmabuf,
202 DMA_FROM_DEVICE);
203 return ret;
205 EXPORT_SYMBOL(mipi_dbi_buf_copy);
207 static int mipi_dbi_fb_dirty(struct drm_framebuffer *fb,
208 struct drm_file *file_priv,
209 unsigned int flags, unsigned int color,
210 struct drm_clip_rect *clips,
211 unsigned int num_clips)
213 struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
214 struct tinydrm_device *tdev = fb->dev->dev_private;
215 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
216 bool swap = mipi->swap_bytes;
217 struct drm_clip_rect clip;
218 int ret = 0;
219 bool full;
220 void *tr;
222 if (!mipi->enabled)
223 return 0;
225 full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
226 fb->width, fb->height);
228 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
229 clip.x1, clip.x2, clip.y1, clip.y2);
231 if (!mipi->dc || !full || swap ||
232 fb->format->format == DRM_FORMAT_XRGB8888) {
233 tr = mipi->tx_buf;
234 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
235 if (ret)
236 return ret;
237 } else {
238 tr = cma_obj->vaddr;
241 mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
242 (clip.x1 >> 8) & 0xFF, clip.x1 & 0xFF,
243 (clip.x2 >> 8) & 0xFF, (clip.x2 - 1) & 0xFF);
244 mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
245 (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
246 (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
248 ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START, tr,
249 (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
251 return ret;
254 static const struct drm_framebuffer_funcs mipi_dbi_fb_funcs = {
255 .destroy = drm_gem_fb_destroy,
256 .create_handle = drm_gem_fb_create_handle,
257 .dirty = tinydrm_fb_dirty,
261 * mipi_dbi_enable_flush - MIPI DBI enable helper
262 * @mipi: MIPI DBI structure
263 * @crtc_state: CRTC state
264 * @plane_state: Plane state
266 * This function sets &mipi_dbi->enabled, flushes the whole framebuffer and
267 * enables the backlight. Drivers can use this in their
268 * &drm_simple_display_pipe_funcs->enable callback.
270 void mipi_dbi_enable_flush(struct mipi_dbi *mipi,
271 struct drm_crtc_state *crtc_state,
272 struct drm_plane_state *plane_state)
274 struct tinydrm_device *tdev = &mipi->tinydrm;
275 struct drm_framebuffer *fb = plane_state->fb;
277 mipi->enabled = true;
278 if (fb)
279 tdev->fb_dirty(fb, NULL, 0, 0, NULL, 0);
281 backlight_enable(mipi->backlight);
283 EXPORT_SYMBOL(mipi_dbi_enable_flush);
285 static void mipi_dbi_blank(struct mipi_dbi *mipi)
287 struct drm_device *drm = mipi->tinydrm.drm;
288 u16 height = drm->mode_config.min_height;
289 u16 width = drm->mode_config.min_width;
290 size_t len = width * height * 2;
292 memset(mipi->tx_buf, 0, len);
294 mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0,
295 (width >> 8) & 0xFF, (width - 1) & 0xFF);
296 mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0,
297 (height >> 8) & 0xFF, (height - 1) & 0xFF);
298 mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
299 (u8 *)mipi->tx_buf, len);
303 * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper
304 * @pipe: Display pipe
306 * This function disables backlight if present, if not the display memory is
307 * blanked. The regulator is disabled if in use. Drivers can use this as their
308 * &drm_simple_display_pipe_funcs->disable callback.
310 void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe)
312 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
313 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
315 DRM_DEBUG_KMS("\n");
317 mipi->enabled = false;
319 if (mipi->backlight)
320 backlight_disable(mipi->backlight);
321 else
322 mipi_dbi_blank(mipi);
324 if (mipi->regulator)
325 regulator_disable(mipi->regulator);
327 EXPORT_SYMBOL(mipi_dbi_pipe_disable);
329 static const uint32_t mipi_dbi_formats[] = {
330 DRM_FORMAT_RGB565,
331 DRM_FORMAT_XRGB8888,
335 * mipi_dbi_init - MIPI DBI initialization
336 * @dev: Parent device
337 * @mipi: &mipi_dbi structure to initialize
338 * @pipe_funcs: Display pipe functions
339 * @driver: DRM driver
340 * @mode: Display mode
341 * @rotation: Initial rotation in degrees Counter Clock Wise
343 * This function initializes a &mipi_dbi structure and it's underlying
344 * @tinydrm_device. It also sets up the display pipeline.
346 * Supported formats: Native RGB565 and emulated XRGB8888.
348 * Objects created by this function will be automatically freed on driver
349 * detach (devres).
351 * Returns:
352 * Zero on success, negative error code on failure.
354 int mipi_dbi_init(struct device *dev, struct mipi_dbi *mipi,
355 const struct drm_simple_display_pipe_funcs *pipe_funcs,
356 struct drm_driver *driver,
357 const struct drm_display_mode *mode, unsigned int rotation)
359 size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
360 struct tinydrm_device *tdev = &mipi->tinydrm;
361 int ret;
363 if (!mipi->command)
364 return -EINVAL;
366 mutex_init(&mipi->cmdlock);
368 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
369 if (!mipi->tx_buf)
370 return -ENOMEM;
372 ret = devm_tinydrm_init(dev, tdev, &mipi_dbi_fb_funcs, driver);
373 if (ret)
374 return ret;
376 tdev->fb_dirty = mipi_dbi_fb_dirty;
378 /* TODO: Maybe add DRM_MODE_CONNECTOR_SPI */
379 ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
380 DRM_MODE_CONNECTOR_VIRTUAL,
381 mipi_dbi_formats,
382 ARRAY_SIZE(mipi_dbi_formats), mode,
383 rotation);
384 if (ret)
385 return ret;
387 tdev->drm->mode_config.preferred_depth = 16;
388 mipi->rotation = rotation;
390 drm_mode_config_reset(tdev->drm);
392 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
393 tdev->drm->mode_config.preferred_depth, rotation);
395 return 0;
397 EXPORT_SYMBOL(mipi_dbi_init);
400 * mipi_dbi_hw_reset - Hardware reset of controller
401 * @mipi: MIPI DBI structure
403 * Reset controller if the &mipi_dbi->reset gpio is set.
405 void mipi_dbi_hw_reset(struct mipi_dbi *mipi)
407 if (!mipi->reset)
408 return;
410 gpiod_set_value_cansleep(mipi->reset, 0);
411 usleep_range(20, 1000);
412 gpiod_set_value_cansleep(mipi->reset, 1);
413 msleep(120);
415 EXPORT_SYMBOL(mipi_dbi_hw_reset);
418 * mipi_dbi_display_is_on - Check if display is on
419 * @mipi: MIPI DBI structure
421 * This function checks the Power Mode register (if readable) to see if
422 * display output is turned on. This can be used to see if the bootloader
423 * has already turned on the display avoiding flicker when the pipeline is
424 * enabled.
426 * Returns:
427 * true if the display can be verified to be on, false otherwise.
429 bool mipi_dbi_display_is_on(struct mipi_dbi *mipi)
431 u8 val;
433 if (mipi_dbi_command_read(mipi, MIPI_DCS_GET_POWER_MODE, &val))
434 return false;
436 val &= ~DCS_POWER_MODE_RESERVED_MASK;
438 /* The poweron/reset value is 08h DCS_POWER_MODE_DISPLAY_NORMAL_MODE */
439 if (val != (DCS_POWER_MODE_DISPLAY |
440 DCS_POWER_MODE_DISPLAY_NORMAL_MODE | DCS_POWER_MODE_SLEEP_MODE))
441 return false;
443 DRM_DEBUG_DRIVER("Display is ON\n");
445 return true;
447 EXPORT_SYMBOL(mipi_dbi_display_is_on);
449 static int mipi_dbi_poweron_reset_conditional(struct mipi_dbi *mipi, bool cond)
451 struct device *dev = mipi->tinydrm.drm->dev;
452 int ret;
454 if (mipi->regulator) {
455 ret = regulator_enable(mipi->regulator);
456 if (ret) {
457 DRM_DEV_ERROR(dev, "Failed to enable regulator (%d)\n", ret);
458 return ret;
462 if (cond && mipi_dbi_display_is_on(mipi))
463 return 1;
465 mipi_dbi_hw_reset(mipi);
466 ret = mipi_dbi_command(mipi, MIPI_DCS_SOFT_RESET);
467 if (ret) {
468 DRM_DEV_ERROR(dev, "Failed to send reset command (%d)\n", ret);
469 if (mipi->regulator)
470 regulator_disable(mipi->regulator);
471 return ret;
475 * If we did a hw reset, we know the controller is in Sleep mode and
476 * per MIPI DSC spec should wait 5ms after soft reset. If we didn't,
477 * we assume worst case and wait 120ms.
479 if (mipi->reset)
480 usleep_range(5000, 20000);
481 else
482 msleep(120);
484 return 0;
488 * mipi_dbi_poweron_reset - MIPI DBI poweron and reset
489 * @mipi: MIPI DBI structure
491 * This function enables the regulator if used and does a hardware and software
492 * reset.
494 * Returns:
495 * Zero on success, or a negative error code.
497 int mipi_dbi_poweron_reset(struct mipi_dbi *mipi)
499 return mipi_dbi_poweron_reset_conditional(mipi, false);
501 EXPORT_SYMBOL(mipi_dbi_poweron_reset);
504 * mipi_dbi_poweron_conditional_reset - MIPI DBI poweron and conditional reset
505 * @mipi: MIPI DBI structure
507 * This function enables the regulator if used and if the display is off, it
508 * does a hardware and software reset. If mipi_dbi_display_is_on() determines
509 * that the display is on, no reset is performed.
511 * Returns:
512 * Zero if the controller was reset, 1 if the display was already on, or a
513 * negative error code.
515 int mipi_dbi_poweron_conditional_reset(struct mipi_dbi *mipi)
517 return mipi_dbi_poweron_reset_conditional(mipi, true);
519 EXPORT_SYMBOL(mipi_dbi_poweron_conditional_reset);
521 #if IS_ENABLED(CONFIG_SPI)
524 * mipi_dbi_spi_cmd_max_speed - get the maximum SPI bus speed
525 * @spi: SPI device
526 * @len: The transfer buffer length.
528 * Many controllers have a max speed of 10MHz, but can be pushed way beyond
529 * that. Increase reliability by running pixel data at max speed and the rest
530 * at 10MHz, preventing transfer glitches from messing up the init settings.
532 u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len)
534 if (len > 64)
535 return 0; /* use default */
537 return min_t(u32, 10000000, spi->max_speed_hz);
539 EXPORT_SYMBOL(mipi_dbi_spi_cmd_max_speed);
542 * MIPI DBI Type C Option 1
544 * If the SPI controller doesn't have 9 bits per word support,
545 * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer.
546 * Pad partial blocks with MIPI_DCS_NOP (zero).
547 * This is how the D/C bit (x) is added:
548 * x7654321
549 * 0x765432
550 * 10x76543
551 * 210x7654
552 * 3210x765
553 * 43210x76
554 * 543210x7
555 * 6543210x
556 * 76543210
559 static int mipi_dbi_spi1e_transfer(struct mipi_dbi *mipi, int dc,
560 const void *buf, size_t len,
561 unsigned int bpw)
563 bool swap_bytes = (bpw == 16 && tinydrm_machine_little_endian());
564 size_t chunk, max_chunk = mipi->tx_buf9_len;
565 struct spi_device *spi = mipi->spi;
566 struct spi_transfer tr = {
567 .tx_buf = mipi->tx_buf9,
568 .bits_per_word = 8,
570 struct spi_message m;
571 const u8 *src = buf;
572 int i, ret;
573 u8 *dst;
575 if (drm_debug & DRM_UT_DRIVER)
576 pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
577 __func__, dc, max_chunk);
579 tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
580 spi_message_init_with_transfers(&m, &tr, 1);
582 if (!dc) {
583 if (WARN_ON_ONCE(len != 1))
584 return -EINVAL;
586 /* Command: pad no-op's (zeroes) at beginning of block */
587 dst = mipi->tx_buf9;
588 memset(dst, 0, 9);
589 dst[8] = *src;
590 tr.len = 9;
592 tinydrm_dbg_spi_message(spi, &m);
594 return spi_sync(spi, &m);
597 /* max with room for adding one bit per byte */
598 max_chunk = max_chunk / 9 * 8;
599 /* but no bigger than len */
600 max_chunk = min(max_chunk, len);
601 /* 8 byte blocks */
602 max_chunk = max_t(size_t, 8, max_chunk & ~0x7);
604 while (len) {
605 size_t added = 0;
607 chunk = min(len, max_chunk);
608 len -= chunk;
609 dst = mipi->tx_buf9;
611 if (chunk < 8) {
612 u8 val, carry = 0;
614 /* Data: pad no-op's (zeroes) at end of block */
615 memset(dst, 0, 9);
617 if (swap_bytes) {
618 for (i = 1; i < (chunk + 1); i++) {
619 val = src[1];
620 *dst++ = carry | BIT(8 - i) | (val >> i);
621 carry = val << (8 - i);
622 i++;
623 val = src[0];
624 *dst++ = carry | BIT(8 - i) | (val >> i);
625 carry = val << (8 - i);
626 src += 2;
628 *dst++ = carry;
629 } else {
630 for (i = 1; i < (chunk + 1); i++) {
631 val = *src++;
632 *dst++ = carry | BIT(8 - i) | (val >> i);
633 carry = val << (8 - i);
635 *dst++ = carry;
638 chunk = 8;
639 added = 1;
640 } else {
641 for (i = 0; i < chunk; i += 8) {
642 if (swap_bytes) {
643 *dst++ = BIT(7) | (src[1] >> 1);
644 *dst++ = (src[1] << 7) | BIT(6) | (src[0] >> 2);
645 *dst++ = (src[0] << 6) | BIT(5) | (src[3] >> 3);
646 *dst++ = (src[3] << 5) | BIT(4) | (src[2] >> 4);
647 *dst++ = (src[2] << 4) | BIT(3) | (src[5] >> 5);
648 *dst++ = (src[5] << 3) | BIT(2) | (src[4] >> 6);
649 *dst++ = (src[4] << 2) | BIT(1) | (src[7] >> 7);
650 *dst++ = (src[7] << 1) | BIT(0);
651 *dst++ = src[6];
652 } else {
653 *dst++ = BIT(7) | (src[0] >> 1);
654 *dst++ = (src[0] << 7) | BIT(6) | (src[1] >> 2);
655 *dst++ = (src[1] << 6) | BIT(5) | (src[2] >> 3);
656 *dst++ = (src[2] << 5) | BIT(4) | (src[3] >> 4);
657 *dst++ = (src[3] << 4) | BIT(3) | (src[4] >> 5);
658 *dst++ = (src[4] << 3) | BIT(2) | (src[5] >> 6);
659 *dst++ = (src[5] << 2) | BIT(1) | (src[6] >> 7);
660 *dst++ = (src[6] << 1) | BIT(0);
661 *dst++ = src[7];
664 src += 8;
665 added++;
669 tr.len = chunk + added;
671 tinydrm_dbg_spi_message(spi, &m);
672 ret = spi_sync(spi, &m);
673 if (ret)
674 return ret;
677 return 0;
680 static int mipi_dbi_spi1_transfer(struct mipi_dbi *mipi, int dc,
681 const void *buf, size_t len,
682 unsigned int bpw)
684 struct spi_device *spi = mipi->spi;
685 struct spi_transfer tr = {
686 .bits_per_word = 9,
688 const u16 *src16 = buf;
689 const u8 *src8 = buf;
690 struct spi_message m;
691 size_t max_chunk;
692 u16 *dst16;
693 int ret;
695 if (!tinydrm_spi_bpw_supported(spi, 9))
696 return mipi_dbi_spi1e_transfer(mipi, dc, buf, len, bpw);
698 tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
699 max_chunk = mipi->tx_buf9_len;
700 dst16 = mipi->tx_buf9;
702 if (drm_debug & DRM_UT_DRIVER)
703 pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
704 __func__, dc, max_chunk);
706 max_chunk = min(max_chunk / 2, len);
708 spi_message_init_with_transfers(&m, &tr, 1);
709 tr.tx_buf = dst16;
711 while (len) {
712 size_t chunk = min(len, max_chunk);
713 unsigned int i;
715 if (bpw == 16 && tinydrm_machine_little_endian()) {
716 for (i = 0; i < (chunk * 2); i += 2) {
717 dst16[i] = *src16 >> 8;
718 dst16[i + 1] = *src16++ & 0xFF;
719 if (dc) {
720 dst16[i] |= 0x0100;
721 dst16[i + 1] |= 0x0100;
724 } else {
725 for (i = 0; i < chunk; i++) {
726 dst16[i] = *src8++;
727 if (dc)
728 dst16[i] |= 0x0100;
732 tr.len = chunk;
733 len -= chunk;
735 tinydrm_dbg_spi_message(spi, &m);
736 ret = spi_sync(spi, &m);
737 if (ret)
738 return ret;
741 return 0;
744 static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 cmd,
745 u8 *parameters, size_t num)
747 unsigned int bpw = (cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
748 int ret;
750 if (mipi_dbi_command_is_read(mipi, cmd))
751 return -ENOTSUPP;
753 MIPI_DBI_DEBUG_COMMAND(cmd, parameters, num);
755 ret = mipi_dbi_spi1_transfer(mipi, 0, &cmd, 1, 8);
756 if (ret || !num)
757 return ret;
759 return mipi_dbi_spi1_transfer(mipi, 1, parameters, num, bpw);
762 /* MIPI DBI Type C Option 3 */
764 static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
765 u8 *data, size_t len)
767 struct spi_device *spi = mipi->spi;
768 u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
769 spi->max_speed_hz / 2);
770 struct spi_transfer tr[2] = {
772 .speed_hz = speed_hz,
773 .tx_buf = &cmd,
774 .len = 1,
775 }, {
776 .speed_hz = speed_hz,
777 .len = len,
780 struct spi_message m;
781 u8 *buf;
782 int ret;
784 if (!len)
785 return -EINVAL;
788 * Support non-standard 24-bit and 32-bit Nokia read commands which
789 * start with a dummy clock, so we need to read an extra byte.
791 if (cmd == MIPI_DCS_GET_DISPLAY_ID ||
792 cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
793 if (!(len == 3 || len == 4))
794 return -EINVAL;
796 tr[1].len = len + 1;
799 buf = kmalloc(tr[1].len, GFP_KERNEL);
800 if (!buf)
801 return -ENOMEM;
803 tr[1].rx_buf = buf;
804 gpiod_set_value_cansleep(mipi->dc, 0);
806 spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
807 ret = spi_sync(spi, &m);
808 if (ret)
809 goto err_free;
811 tinydrm_dbg_spi_message(spi, &m);
813 if (tr[1].len == len) {
814 memcpy(data, buf, len);
815 } else {
816 unsigned int i;
818 for (i = 0; i < len; i++)
819 data[i] = (buf[i] << 1) | !!(buf[i + 1] & BIT(7));
822 MIPI_DBI_DEBUG_COMMAND(cmd, data, len);
824 err_free:
825 kfree(buf);
827 return ret;
830 static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 cmd,
831 u8 *par, size_t num)
833 struct spi_device *spi = mipi->spi;
834 unsigned int bpw = 8;
835 u32 speed_hz;
836 int ret;
838 if (mipi_dbi_command_is_read(mipi, cmd))
839 return mipi_dbi_typec3_command_read(mipi, cmd, par, num);
841 MIPI_DBI_DEBUG_COMMAND(cmd, par, num);
843 gpiod_set_value_cansleep(mipi->dc, 0);
844 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
845 ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
846 if (ret || !num)
847 return ret;
849 if (cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
850 bpw = 16;
852 gpiod_set_value_cansleep(mipi->dc, 1);
853 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
855 return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
859 * mipi_dbi_spi_init - Initialize MIPI DBI SPI interfaced controller
860 * @spi: SPI device
861 * @mipi: &mipi_dbi structure to initialize
862 * @dc: D/C gpio (optional)
864 * This function sets &mipi_dbi->command, enables &mipi->read_commands for the
865 * usual read commands. It should be followed by a call to mipi_dbi_init() or
866 * a driver-specific init.
868 * If @dc is set, a Type C Option 3 interface is assumed, if not
869 * Type C Option 1.
871 * If the SPI master driver doesn't support the necessary bits per word,
872 * the following transformation is used:
874 * - 9-bit: reorder buffer as 9x 8-bit words, padded with no-op command.
875 * - 16-bit: if big endian send as 8-bit, if little endian swap bytes
877 * Returns:
878 * Zero on success, negative error code on failure.
880 int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *mipi,
881 struct gpio_desc *dc)
883 size_t tx_size = tinydrm_spi_max_transfer_size(spi, 0);
884 struct device *dev = &spi->dev;
885 int ret;
887 if (tx_size < 16) {
888 DRM_ERROR("SPI transmit buffer too small: %zu\n", tx_size);
889 return -EINVAL;
893 * Even though it's not the SPI device that does DMA (the master does),
894 * the dma mask is necessary for the dma_alloc_wc() in
895 * drm_gem_cma_create(). The dma_addr returned will be a physical
896 * adddress which might be different from the bus address, but this is
897 * not a problem since the address will not be used.
898 * The virtual address is used in the transfer and the SPI core
899 * re-maps it on the SPI master device using the DMA streaming API
900 * (spi_map_buf()).
902 if (!dev->coherent_dma_mask) {
903 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
904 if (ret) {
905 dev_warn(dev, "Failed to set dma mask %d\n", ret);
906 return ret;
910 mipi->spi = spi;
911 mipi->read_commands = mipi_dbi_dcs_read_commands;
913 if (dc) {
914 mipi->command = mipi_dbi_typec3_command;
915 mipi->dc = dc;
916 if (tinydrm_machine_little_endian() &&
917 !tinydrm_spi_bpw_supported(spi, 16))
918 mipi->swap_bytes = true;
919 } else {
920 mipi->command = mipi_dbi_typec1_command;
921 mipi->tx_buf9_len = tx_size;
922 mipi->tx_buf9 = devm_kmalloc(dev, tx_size, GFP_KERNEL);
923 if (!mipi->tx_buf9)
924 return -ENOMEM;
927 DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
929 return 0;
931 EXPORT_SYMBOL(mipi_dbi_spi_init);
933 #endif /* CONFIG_SPI */
935 #ifdef CONFIG_DEBUG_FS
937 static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
938 const char __user *ubuf,
939 size_t count, loff_t *ppos)
941 struct seq_file *m = file->private_data;
942 struct mipi_dbi *mipi = m->private;
943 u8 val, cmd = 0, parameters[64];
944 char *buf, *pos, *token;
945 unsigned int i;
946 int ret;
948 buf = memdup_user_nul(ubuf, count);
949 if (IS_ERR(buf))
950 return PTR_ERR(buf);
952 /* strip trailing whitespace */
953 for (i = count - 1; i > 0; i--)
954 if (isspace(buf[i]))
955 buf[i] = '\0';
956 else
957 break;
958 i = 0;
959 pos = buf;
960 while (pos) {
961 token = strsep(&pos, " ");
962 if (!token) {
963 ret = -EINVAL;
964 goto err_free;
967 ret = kstrtou8(token, 16, &val);
968 if (ret < 0)
969 goto err_free;
971 if (token == buf)
972 cmd = val;
973 else
974 parameters[i++] = val;
976 if (i == 64) {
977 ret = -E2BIG;
978 goto err_free;
982 ret = mipi_dbi_command_buf(mipi, cmd, parameters, i);
984 err_free:
985 kfree(buf);
987 return ret < 0 ? ret : count;
990 static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused)
992 struct mipi_dbi *mipi = m->private;
993 u8 cmd, val[4];
994 size_t len;
995 int ret;
997 for (cmd = 0; cmd < 255; cmd++) {
998 if (!mipi_dbi_command_is_read(mipi, cmd))
999 continue;
1001 switch (cmd) {
1002 case MIPI_DCS_READ_MEMORY_START:
1003 case MIPI_DCS_READ_MEMORY_CONTINUE:
1004 len = 2;
1005 break;
1006 case MIPI_DCS_GET_DISPLAY_ID:
1007 len = 3;
1008 break;
1009 case MIPI_DCS_GET_DISPLAY_STATUS:
1010 len = 4;
1011 break;
1012 default:
1013 len = 1;
1014 break;
1017 seq_printf(m, "%02x: ", cmd);
1018 ret = mipi_dbi_command_buf(mipi, cmd, val, len);
1019 if (ret) {
1020 seq_puts(m, "XX\n");
1021 continue;
1023 seq_printf(m, "%*phN\n", (int)len, val);
1026 return 0;
1029 static int mipi_dbi_debugfs_command_open(struct inode *inode,
1030 struct file *file)
1032 return single_open(file, mipi_dbi_debugfs_command_show,
1033 inode->i_private);
1036 static const struct file_operations mipi_dbi_debugfs_command_fops = {
1037 .owner = THIS_MODULE,
1038 .open = mipi_dbi_debugfs_command_open,
1039 .read = seq_read,
1040 .llseek = seq_lseek,
1041 .release = single_release,
1042 .write = mipi_dbi_debugfs_command_write,
1046 * mipi_dbi_debugfs_init - Create debugfs entries
1047 * @minor: DRM minor
1049 * This function creates a 'command' debugfs file for sending commands to the
1050 * controller or getting the read command values.
1051 * Drivers can use this as their &drm_driver->debugfs_init callback.
1053 * Returns:
1054 * Zero on success, negative error code on failure.
1056 int mipi_dbi_debugfs_init(struct drm_minor *minor)
1058 struct tinydrm_device *tdev = minor->dev->dev_private;
1059 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
1060 umode_t mode = S_IFREG | S_IWUSR;
1062 if (mipi->read_commands)
1063 mode |= S_IRUGO;
1064 debugfs_create_file("command", mode, minor->debugfs_root, mipi,
1065 &mipi_dbi_debugfs_command_fops);
1067 return 0;
1069 EXPORT_SYMBOL(mipi_dbi_debugfs_init);
1071 #endif
1073 MODULE_LICENSE("GPL");