]> Git Repo - linux.git/blob - drivers/gpu/drm/tinydrm/mipi-dbi.c
Merge tag 'nios2-v4.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/lftan...
[linux.git] / drivers / gpu / drm / tinydrm / mipi-dbi.c
1 /*
2  * MIPI Display Bus Interface (DBI) LCD controller support
3  *
4  * Copyright 2016 Noralf Trønnes
5  *
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.
10  */
11
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>
22
23 #define MIPI_DBI_MAX_SPI_READ_SPEED 2000000 /* 2MHz */
24
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))
31
32 /**
33  * DOC: overview
34  *
35  * This library provides helpers for MIPI Display Bus Interface (DBI)
36  * compatible display controllers.
37  *
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.
42  *
43  * Only MIPI Type 1 displays are supported since a full frame memory is needed.
44  *
45  * There are 3 MIPI DBI implementation types:
46  *
47  * A. Motorola 6800 type parallel bus
48  *
49  * B. Intel 8080 type parallel bus
50  *
51  * C. SPI type with 3 options:
52  *
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
56  *
57  * Currently mipi_dbi only supports Type C options 1 and 3 with
58  * mipi_dbi_spi_init().
59  */
60
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); \
69 })
70
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 */
93 };
94
95 static bool mipi_dbi_command_is_read(struct mipi_dbi *mipi, u8 cmd)
96 {
97         unsigned int i;
98
99         if (!mipi->read_commands)
100                 return false;
101
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;
107         }
108
109         return false;
110 }
111
112 /**
113  * mipi_dbi_command_read - MIPI DCS read command
114  * @mipi: MIPI structure
115  * @cmd: Command
116  * @val: Value read
117  *
118  * Send MIPI DCS read command to the controller.
119  *
120  * Returns:
121  * Zero on success, negative error code on failure.
122  */
123 int mipi_dbi_command_read(struct mipi_dbi *mipi, u8 cmd, u8 *val)
124 {
125         if (!mipi->read_commands)
126                 return -EACCES;
127
128         if (!mipi_dbi_command_is_read(mipi, cmd))
129                 return -EINVAL;
130
131         return mipi_dbi_command_buf(mipi, cmd, val, 1);
132 }
133 EXPORT_SYMBOL(mipi_dbi_command_read);
134
135 /**
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
141  *
142  * Returns:
143  * Zero on success, negative error code on failure.
144  */
145 int mipi_dbi_command_buf(struct mipi_dbi *mipi, u8 cmd, u8 *data, size_t len)
146 {
147         int ret;
148
149         mutex_lock(&mipi->cmdlock);
150         ret = mipi->command(mipi, cmd, data, len);
151         mutex_unlock(&mipi->cmdlock);
152
153         return ret;
154 }
155 EXPORT_SYMBOL(mipi_dbi_command_buf);
156
157 /**
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
163  *
164  * Returns:
165  * Zero on success, negative error code on failure.
166  */
167 int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
168                       struct drm_clip_rect *clip, bool swap)
169 {
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;
175
176         if (import_attach) {
177                 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
178                                                DMA_FROM_DEVICE);
179                 if (ret)
180                         return ret;
181         }
182
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;
198         }
199
200         if (import_attach)
201                 ret = dma_buf_end_cpu_access(import_attach->dmabuf,
202                                              DMA_FROM_DEVICE);
203         return ret;
204 }
205 EXPORT_SYMBOL(mipi_dbi_buf_copy);
206
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)
212 {
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;
221
222         mutex_lock(&tdev->dirty_lock);
223
224         if (!mipi->enabled)
225                 goto out_unlock;
226
227         /* fbdev can flush even when we're not interested */
228         if (tdev->pipe.plane.fb != fb)
229                 goto out_unlock;
230
231         full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
232                                    fb->width, fb->height);
233
234         DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
235                   clip.x1, clip.x2, clip.y1, clip.y2);
236
237         if (!mipi->dc || !full || swap ||
238             fb->format->format == DRM_FORMAT_XRGB8888) {
239                 tr = mipi->tx_buf;
240                 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
241                 if (ret)
242                         goto out_unlock;
243         } else {
244                 tr = cma_obj->vaddr;
245         }
246
247         mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
248                          (clip.x1 >> 8) & 0xFF, clip.x1 & 0xFF,
249                          (clip.x2 >> 8) & 0xFF, (clip.x2 - 1) & 0xFF);
250         mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
251                          (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
252                          (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
253
254         ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START, tr,
255                                 (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
256
257 out_unlock:
258         mutex_unlock(&tdev->dirty_lock);
259
260         if (ret)
261                 dev_err_once(fb->dev->dev, "Failed to update display %d\n",
262                              ret);
263
264         return ret;
265 }
266
267 static const struct drm_framebuffer_funcs mipi_dbi_fb_funcs = {
268         .destroy        = drm_gem_fb_destroy,
269         .create_handle  = drm_gem_fb_create_handle,
270         .dirty          = mipi_dbi_fb_dirty,
271 };
272
273 /**
274  * mipi_dbi_pipe_enable - MIPI DBI pipe enable helper
275  * @pipe: Display pipe
276  * @crtc_state: CRTC state
277  *
278  * This function enables backlight. Drivers can use this as their
279  * &drm_simple_display_pipe_funcs->enable callback.
280  */
281 void mipi_dbi_pipe_enable(struct drm_simple_display_pipe *pipe,
282                           struct drm_crtc_state *crtc_state)
283 {
284         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
285         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
286         struct drm_framebuffer *fb = pipe->plane.fb;
287
288         DRM_DEBUG_KMS("\n");
289
290         mipi->enabled = true;
291         if (fb)
292                 fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0);
293
294         tinydrm_enable_backlight(mipi->backlight);
295 }
296 EXPORT_SYMBOL(mipi_dbi_pipe_enable);
297
298 static void mipi_dbi_blank(struct mipi_dbi *mipi)
299 {
300         struct drm_device *drm = mipi->tinydrm.drm;
301         u16 height = drm->mode_config.min_height;
302         u16 width = drm->mode_config.min_width;
303         size_t len = width * height * 2;
304
305         memset(mipi->tx_buf, 0, len);
306
307         mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0,
308                          (width >> 8) & 0xFF, (width - 1) & 0xFF);
309         mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0,
310                          (height >> 8) & 0xFF, (height - 1) & 0xFF);
311         mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
312                              (u8 *)mipi->tx_buf, len);
313 }
314
315 /**
316  * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper
317  * @pipe: Display pipe
318  *
319  * This function disables backlight if present or if not the
320  * display memory is blanked. Drivers can use this as their
321  * &drm_simple_display_pipe_funcs->disable callback.
322  */
323 void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe)
324 {
325         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
326         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
327
328         DRM_DEBUG_KMS("\n");
329
330         mipi->enabled = false;
331
332         if (mipi->backlight)
333                 tinydrm_disable_backlight(mipi->backlight);
334         else
335                 mipi_dbi_blank(mipi);
336 }
337 EXPORT_SYMBOL(mipi_dbi_pipe_disable);
338
339 static const uint32_t mipi_dbi_formats[] = {
340         DRM_FORMAT_RGB565,
341         DRM_FORMAT_XRGB8888,
342 };
343
344 /**
345  * mipi_dbi_init - MIPI DBI initialization
346  * @dev: Parent device
347  * @mipi: &mipi_dbi structure to initialize
348  * @pipe_funcs: Display pipe functions
349  * @driver: DRM driver
350  * @mode: Display mode
351  * @rotation: Initial rotation in degrees Counter Clock Wise
352  *
353  * This function initializes a &mipi_dbi structure and it's underlying
354  * @tinydrm_device. It also sets up the display pipeline.
355  *
356  * Supported formats: Native RGB565 and emulated XRGB8888.
357  *
358  * Objects created by this function will be automatically freed on driver
359  * detach (devres).
360  *
361  * Returns:
362  * Zero on success, negative error code on failure.
363  */
364 int mipi_dbi_init(struct device *dev, struct mipi_dbi *mipi,
365                   const struct drm_simple_display_pipe_funcs *pipe_funcs,
366                   struct drm_driver *driver,
367                   const struct drm_display_mode *mode, unsigned int rotation)
368 {
369         size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
370         struct tinydrm_device *tdev = &mipi->tinydrm;
371         int ret;
372
373         if (!mipi->command)
374                 return -EINVAL;
375
376         mutex_init(&mipi->cmdlock);
377
378         mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
379         if (!mipi->tx_buf)
380                 return -ENOMEM;
381
382         ret = devm_tinydrm_init(dev, tdev, &mipi_dbi_fb_funcs, driver);
383         if (ret)
384                 return ret;
385
386         /* TODO: Maybe add DRM_MODE_CONNECTOR_SPI */
387         ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
388                                         DRM_MODE_CONNECTOR_VIRTUAL,
389                                         mipi_dbi_formats,
390                                         ARRAY_SIZE(mipi_dbi_formats), mode,
391                                         rotation);
392         if (ret)
393                 return ret;
394
395         tdev->drm->mode_config.preferred_depth = 16;
396         mipi->rotation = rotation;
397
398         drm_mode_config_reset(tdev->drm);
399
400         DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
401                       tdev->drm->mode_config.preferred_depth, rotation);
402
403         return 0;
404 }
405 EXPORT_SYMBOL(mipi_dbi_init);
406
407 /**
408  * mipi_dbi_hw_reset - Hardware reset of controller
409  * @mipi: MIPI DBI structure
410  *
411  * Reset controller if the &mipi_dbi->reset gpio is set.
412  */
413 void mipi_dbi_hw_reset(struct mipi_dbi *mipi)
414 {
415         if (!mipi->reset)
416                 return;
417
418         gpiod_set_value_cansleep(mipi->reset, 0);
419         msleep(20);
420         gpiod_set_value_cansleep(mipi->reset, 1);
421         msleep(120);
422 }
423 EXPORT_SYMBOL(mipi_dbi_hw_reset);
424
425 /**
426  * mipi_dbi_display_is_on - Check if display is on
427  * @mipi: MIPI DBI structure
428  *
429  * This function checks the Power Mode register (if readable) to see if
430  * display output is turned on. This can be used to see if the bootloader
431  * has already turned on the display avoiding flicker when the pipeline is
432  * enabled.
433  *
434  * Returns:
435  * true if the display can be verified to be on, false otherwise.
436  */
437 bool mipi_dbi_display_is_on(struct mipi_dbi *mipi)
438 {
439         u8 val;
440
441         if (mipi_dbi_command_read(mipi, MIPI_DCS_GET_POWER_MODE, &val))
442                 return false;
443
444         val &= ~DCS_POWER_MODE_RESERVED_MASK;
445
446         if (val != (DCS_POWER_MODE_DISPLAY |
447             DCS_POWER_MODE_DISPLAY_NORMAL_MODE | DCS_POWER_MODE_SLEEP_MODE))
448                 return false;
449
450         DRM_DEBUG_DRIVER("Display is ON\n");
451
452         return true;
453 }
454 EXPORT_SYMBOL(mipi_dbi_display_is_on);
455
456 #if IS_ENABLED(CONFIG_SPI)
457
458 /**
459  * mipi_dbi_spi_cmd_max_speed - get the maximum SPI bus speed
460  * @spi: SPI device
461  * @len: The transfer buffer length.
462  *
463  * Many controllers have a max speed of 10MHz, but can be pushed way beyond
464  * that. Increase reliability by running pixel data at max speed and the rest
465  * at 10MHz, preventing transfer glitches from messing up the init settings.
466  */
467 u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len)
468 {
469         if (len > 64)
470                 return 0; /* use default */
471
472         return min_t(u32, 10000000, spi->max_speed_hz);
473 }
474 EXPORT_SYMBOL(mipi_dbi_spi_cmd_max_speed);
475
476 /*
477  * MIPI DBI Type C Option 1
478  *
479  * If the SPI controller doesn't have 9 bits per word support,
480  * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer.
481  * Pad partial blocks with MIPI_DCS_NOP (zero).
482  * This is how the D/C bit (x) is added:
483  *     x7654321
484  *     0x765432
485  *     10x76543
486  *     210x7654
487  *     3210x765
488  *     43210x76
489  *     543210x7
490  *     6543210x
491  *     76543210
492  */
493
494 static int mipi_dbi_spi1e_transfer(struct mipi_dbi *mipi, int dc,
495                                    const void *buf, size_t len,
496                                    unsigned int bpw)
497 {
498         bool swap_bytes = (bpw == 16 && tinydrm_machine_little_endian());
499         size_t chunk, max_chunk = mipi->tx_buf9_len;
500         struct spi_device *spi = mipi->spi;
501         struct spi_transfer tr = {
502                 .tx_buf = mipi->tx_buf9,
503                 .bits_per_word = 8,
504         };
505         struct spi_message m;
506         const u8 *src = buf;
507         int i, ret;
508         u8 *dst;
509
510         if (drm_debug & DRM_UT_DRIVER)
511                 pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
512                          __func__, dc, max_chunk);
513
514         tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
515         spi_message_init_with_transfers(&m, &tr, 1);
516
517         if (!dc) {
518                 if (WARN_ON_ONCE(len != 1))
519                         return -EINVAL;
520
521                 /* Command: pad no-op's (zeroes) at beginning of block */
522                 dst = mipi->tx_buf9;
523                 memset(dst, 0, 9);
524                 dst[8] = *src;
525                 tr.len = 9;
526
527                 tinydrm_dbg_spi_message(spi, &m);
528
529                 return spi_sync(spi, &m);
530         }
531
532         /* max with room for adding one bit per byte */
533         max_chunk = max_chunk / 9 * 8;
534         /* but no bigger than len */
535         max_chunk = min(max_chunk, len);
536         /* 8 byte blocks */
537         max_chunk = max_t(size_t, 8, max_chunk & ~0x7);
538
539         while (len) {
540                 size_t added = 0;
541
542                 chunk = min(len, max_chunk);
543                 len -= chunk;
544                 dst = mipi->tx_buf9;
545
546                 if (chunk < 8) {
547                         u8 val, carry = 0;
548
549                         /* Data: pad no-op's (zeroes) at end of block */
550                         memset(dst, 0, 9);
551
552                         if (swap_bytes) {
553                                 for (i = 1; i < (chunk + 1); i++) {
554                                         val = src[1];
555                                         *dst++ = carry | BIT(8 - i) | (val >> i);
556                                         carry = val << (8 - i);
557                                         i++;
558                                         val = src[0];
559                                         *dst++ = carry | BIT(8 - i) | (val >> i);
560                                         carry = val << (8 - i);
561                                         src += 2;
562                                 }
563                                 *dst++ = carry;
564                         } else {
565                                 for (i = 1; i < (chunk + 1); i++) {
566                                         val = *src++;
567                                         *dst++ = carry | BIT(8 - i) | (val >> i);
568                                         carry = val << (8 - i);
569                                 }
570                                 *dst++ = carry;
571                         }
572
573                         chunk = 8;
574                         added = 1;
575                 } else {
576                         for (i = 0; i < chunk; i += 8) {
577                                 if (swap_bytes) {
578                                         *dst++ =                 BIT(7) | (src[1] >> 1);
579                                         *dst++ = (src[1] << 7) | BIT(6) | (src[0] >> 2);
580                                         *dst++ = (src[0] << 6) | BIT(5) | (src[3] >> 3);
581                                         *dst++ = (src[3] << 5) | BIT(4) | (src[2] >> 4);
582                                         *dst++ = (src[2] << 4) | BIT(3) | (src[5] >> 5);
583                                         *dst++ = (src[5] << 3) | BIT(2) | (src[4] >> 6);
584                                         *dst++ = (src[4] << 2) | BIT(1) | (src[7] >> 7);
585                                         *dst++ = (src[7] << 1) | BIT(0);
586                                         *dst++ = src[6];
587                                 } else {
588                                         *dst++ =                 BIT(7) | (src[0] >> 1);
589                                         *dst++ = (src[0] << 7) | BIT(6) | (src[1] >> 2);
590                                         *dst++ = (src[1] << 6) | BIT(5) | (src[2] >> 3);
591                                         *dst++ = (src[2] << 5) | BIT(4) | (src[3] >> 4);
592                                         *dst++ = (src[3] << 4) | BIT(3) | (src[4] >> 5);
593                                         *dst++ = (src[4] << 3) | BIT(2) | (src[5] >> 6);
594                                         *dst++ = (src[5] << 2) | BIT(1) | (src[6] >> 7);
595                                         *dst++ = (src[6] << 1) | BIT(0);
596                                         *dst++ = src[7];
597                                 }
598
599                                 src += 8;
600                                 added++;
601                         }
602                 }
603
604                 tr.len = chunk + added;
605
606                 tinydrm_dbg_spi_message(spi, &m);
607                 ret = spi_sync(spi, &m);
608                 if (ret)
609                         return ret;
610         }
611
612         return 0;
613 }
614
615 static int mipi_dbi_spi1_transfer(struct mipi_dbi *mipi, int dc,
616                                   const void *buf, size_t len,
617                                   unsigned int bpw)
618 {
619         struct spi_device *spi = mipi->spi;
620         struct spi_transfer tr = {
621                 .bits_per_word = 9,
622         };
623         const u16 *src16 = buf;
624         const u8 *src8 = buf;
625         struct spi_message m;
626         size_t max_chunk;
627         u16 *dst16;
628         int ret;
629
630         if (!tinydrm_spi_bpw_supported(spi, 9))
631                 return mipi_dbi_spi1e_transfer(mipi, dc, buf, len, bpw);
632
633         tr.speed_hz = mipi_dbi_spi_cmd_max_speed(spi, len);
634         max_chunk = mipi->tx_buf9_len;
635         dst16 = mipi->tx_buf9;
636
637         if (drm_debug & DRM_UT_DRIVER)
638                 pr_debug("[drm:%s] dc=%d, max_chunk=%zu, transfers:\n",
639                          __func__, dc, max_chunk);
640
641         max_chunk = min(max_chunk / 2, len);
642
643         spi_message_init_with_transfers(&m, &tr, 1);
644         tr.tx_buf = dst16;
645
646         while (len) {
647                 size_t chunk = min(len, max_chunk);
648                 unsigned int i;
649
650                 if (bpw == 16 && tinydrm_machine_little_endian()) {
651                         for (i = 0; i < (chunk * 2); i += 2) {
652                                 dst16[i]     = *src16 >> 8;
653                                 dst16[i + 1] = *src16++ & 0xFF;
654                                 if (dc) {
655                                         dst16[i]     |= 0x0100;
656                                         dst16[i + 1] |= 0x0100;
657                                 }
658                         }
659                 } else {
660                         for (i = 0; i < chunk; i++) {
661                                 dst16[i] = *src8++;
662                                 if (dc)
663                                         dst16[i] |= 0x0100;
664                         }
665                 }
666
667                 tr.len = chunk;
668                 len -= chunk;
669
670                 tinydrm_dbg_spi_message(spi, &m);
671                 ret = spi_sync(spi, &m);
672                 if (ret)
673                         return ret;
674         }
675
676         return 0;
677 }
678
679 static int mipi_dbi_typec1_command(struct mipi_dbi *mipi, u8 cmd,
680                                    u8 *parameters, size_t num)
681 {
682         unsigned int bpw = (cmd == MIPI_DCS_WRITE_MEMORY_START) ? 16 : 8;
683         int ret;
684
685         if (mipi_dbi_command_is_read(mipi, cmd))
686                 return -ENOTSUPP;
687
688         MIPI_DBI_DEBUG_COMMAND(cmd, parameters, num);
689
690         ret = mipi_dbi_spi1_transfer(mipi, 0, &cmd, 1, 8);
691         if (ret || !num)
692                 return ret;
693
694         return mipi_dbi_spi1_transfer(mipi, 1, parameters, num, bpw);
695 }
696
697 /* MIPI DBI Type C Option 3 */
698
699 static int mipi_dbi_typec3_command_read(struct mipi_dbi *mipi, u8 cmd,
700                                         u8 *data, size_t len)
701 {
702         struct spi_device *spi = mipi->spi;
703         u32 speed_hz = min_t(u32, MIPI_DBI_MAX_SPI_READ_SPEED,
704                              spi->max_speed_hz / 2);
705         struct spi_transfer tr[2] = {
706                 {
707                         .speed_hz = speed_hz,
708                         .tx_buf = &cmd,
709                         .len = 1,
710                 }, {
711                         .speed_hz = speed_hz,
712                         .len = len,
713                 },
714         };
715         struct spi_message m;
716         u8 *buf;
717         int ret;
718
719         if (!len)
720                 return -EINVAL;
721
722         /*
723          * Support non-standard 24-bit and 32-bit Nokia read commands which
724          * start with a dummy clock, so we need to read an extra byte.
725          */
726         if (cmd == MIPI_DCS_GET_DISPLAY_ID ||
727             cmd == MIPI_DCS_GET_DISPLAY_STATUS) {
728                 if (!(len == 3 || len == 4))
729                         return -EINVAL;
730
731                 tr[1].len = len + 1;
732         }
733
734         buf = kmalloc(tr[1].len, GFP_KERNEL);
735         if (!buf)
736                 return -ENOMEM;
737
738         tr[1].rx_buf = buf;
739         gpiod_set_value_cansleep(mipi->dc, 0);
740
741         spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
742         ret = spi_sync(spi, &m);
743         if (ret)
744                 goto err_free;
745
746         tinydrm_dbg_spi_message(spi, &m);
747
748         if (tr[1].len == len) {
749                 memcpy(data, buf, len);
750         } else {
751                 unsigned int i;
752
753                 for (i = 0; i < len; i++)
754                         data[i] = (buf[i] << 1) | !!(buf[i + 1] & BIT(7));
755         }
756
757         MIPI_DBI_DEBUG_COMMAND(cmd, data, len);
758
759 err_free:
760         kfree(buf);
761
762         return ret;
763 }
764
765 static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 cmd,
766                                    u8 *par, size_t num)
767 {
768         struct spi_device *spi = mipi->spi;
769         unsigned int bpw = 8;
770         u32 speed_hz;
771         int ret;
772
773         if (mipi_dbi_command_is_read(mipi, cmd))
774                 return mipi_dbi_typec3_command_read(mipi, cmd, par, num);
775
776         MIPI_DBI_DEBUG_COMMAND(cmd, par, num);
777
778         gpiod_set_value_cansleep(mipi->dc, 0);
779         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
780         ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
781         if (ret || !num)
782                 return ret;
783
784         if (cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
785                 bpw = 16;
786
787         gpiod_set_value_cansleep(mipi->dc, 1);
788         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
789
790         return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
791 }
792
793 /**
794  * mipi_dbi_spi_init - Initialize MIPI DBI SPI interfaced controller
795  * @spi: SPI device
796  * @mipi: &mipi_dbi structure to initialize
797  * @dc: D/C gpio (optional)
798  *
799  * This function sets &mipi_dbi->command, enables &mipi->read_commands for the
800  * usual read commands. It should be followed by a call to mipi_dbi_init() or
801  * a driver-specific init.
802  *
803  * If @dc is set, a Type C Option 3 interface is assumed, if not
804  * Type C Option 1.
805  *
806  * If the SPI master driver doesn't support the necessary bits per word,
807  * the following transformation is used:
808  *
809  * - 9-bit: reorder buffer as 9x 8-bit words, padded with no-op command.
810  * - 16-bit: if big endian send as 8-bit, if little endian swap bytes
811  *
812  * Returns:
813  * Zero on success, negative error code on failure.
814  */
815 int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *mipi,
816                       struct gpio_desc *dc)
817 {
818         size_t tx_size = tinydrm_spi_max_transfer_size(spi, 0);
819         struct device *dev = &spi->dev;
820         int ret;
821
822         if (tx_size < 16) {
823                 DRM_ERROR("SPI transmit buffer too small: %zu\n", tx_size);
824                 return -EINVAL;
825         }
826
827         /*
828          * Even though it's not the SPI device that does DMA (the master does),
829          * the dma mask is necessary for the dma_alloc_wc() in
830          * drm_gem_cma_create(). The dma_addr returned will be a physical
831          * adddress which might be different from the bus address, but this is
832          * not a problem since the address will not be used.
833          * The virtual address is used in the transfer and the SPI core
834          * re-maps it on the SPI master device using the DMA streaming API
835          * (spi_map_buf()).
836          */
837         if (!dev->coherent_dma_mask) {
838                 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
839                 if (ret) {
840                         dev_warn(dev, "Failed to set dma mask %d\n", ret);
841                         return ret;
842                 }
843         }
844
845         mipi->spi = spi;
846         mipi->read_commands = mipi_dbi_dcs_read_commands;
847
848         if (dc) {
849                 mipi->command = mipi_dbi_typec3_command;
850                 mipi->dc = dc;
851                 if (tinydrm_machine_little_endian() &&
852                     !tinydrm_spi_bpw_supported(spi, 16))
853                         mipi->swap_bytes = true;
854         } else {
855                 mipi->command = mipi_dbi_typec1_command;
856                 mipi->tx_buf9_len = tx_size;
857                 mipi->tx_buf9 = devm_kmalloc(dev, tx_size, GFP_KERNEL);
858                 if (!mipi->tx_buf9)
859                         return -ENOMEM;
860         }
861
862         DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
863
864         return 0;
865 }
866 EXPORT_SYMBOL(mipi_dbi_spi_init);
867
868 #endif /* CONFIG_SPI */
869
870 #ifdef CONFIG_DEBUG_FS
871
872 static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
873                                               const char __user *ubuf,
874                                               size_t count, loff_t *ppos)
875 {
876         struct seq_file *m = file->private_data;
877         struct mipi_dbi *mipi = m->private;
878         u8 val, cmd = 0, parameters[64];
879         char *buf, *pos, *token;
880         unsigned int i;
881         int ret;
882
883         buf = memdup_user_nul(ubuf, count);
884         if (IS_ERR(buf))
885                 return PTR_ERR(buf);
886
887         /* strip trailing whitespace */
888         for (i = count - 1; i > 0; i--)
889                 if (isspace(buf[i]))
890                         buf[i] = '\0';
891                 else
892                         break;
893         i = 0;
894         pos = buf;
895         while (pos) {
896                 token = strsep(&pos, " ");
897                 if (!token) {
898                         ret = -EINVAL;
899                         goto err_free;
900                 }
901
902                 ret = kstrtou8(token, 16, &val);
903                 if (ret < 0)
904                         goto err_free;
905
906                 if (token == buf)
907                         cmd = val;
908                 else
909                         parameters[i++] = val;
910
911                 if (i == 64) {
912                         ret = -E2BIG;
913                         goto err_free;
914                 }
915         }
916
917         ret = mipi_dbi_command_buf(mipi, cmd, parameters, i);
918
919 err_free:
920         kfree(buf);
921
922         return ret < 0 ? ret : count;
923 }
924
925 static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused)
926 {
927         struct mipi_dbi *mipi = m->private;
928         u8 cmd, val[4];
929         size_t len;
930         int ret;
931
932         for (cmd = 0; cmd < 255; cmd++) {
933                 if (!mipi_dbi_command_is_read(mipi, cmd))
934                         continue;
935
936                 switch (cmd) {
937                 case MIPI_DCS_READ_MEMORY_START:
938                 case MIPI_DCS_READ_MEMORY_CONTINUE:
939                         len = 2;
940                         break;
941                 case MIPI_DCS_GET_DISPLAY_ID:
942                         len = 3;
943                         break;
944                 case MIPI_DCS_GET_DISPLAY_STATUS:
945                         len = 4;
946                         break;
947                 default:
948                         len = 1;
949                         break;
950                 }
951
952                 seq_printf(m, "%02x: ", cmd);
953                 ret = mipi_dbi_command_buf(mipi, cmd, val, len);
954                 if (ret) {
955                         seq_puts(m, "XX\n");
956                         continue;
957                 }
958                 seq_printf(m, "%*phN\n", (int)len, val);
959         }
960
961         return 0;
962 }
963
964 static int mipi_dbi_debugfs_command_open(struct inode *inode,
965                                          struct file *file)
966 {
967         return single_open(file, mipi_dbi_debugfs_command_show,
968                            inode->i_private);
969 }
970
971 static const struct file_operations mipi_dbi_debugfs_command_fops = {
972         .owner = THIS_MODULE,
973         .open = mipi_dbi_debugfs_command_open,
974         .read = seq_read,
975         .llseek = seq_lseek,
976         .release = single_release,
977         .write = mipi_dbi_debugfs_command_write,
978 };
979
980 /**
981  * mipi_dbi_debugfs_init - Create debugfs entries
982  * @minor: DRM minor
983  *
984  * This function creates a 'command' debugfs file for sending commands to the
985  * controller or getting the read command values.
986  * Drivers can use this as their &drm_driver->debugfs_init callback.
987  *
988  * Returns:
989  * Zero on success, negative error code on failure.
990  */
991 int mipi_dbi_debugfs_init(struct drm_minor *minor)
992 {
993         struct tinydrm_device *tdev = minor->dev->dev_private;
994         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
995         umode_t mode = S_IFREG | S_IWUSR;
996
997         if (mipi->read_commands)
998                 mode |= S_IRUGO;
999         debugfs_create_file("command", mode, minor->debugfs_root, mipi,
1000                             &mipi_dbi_debugfs_command_fops);
1001
1002         return 0;
1003 }
1004 EXPORT_SYMBOL(mipi_dbi_debugfs_init);
1005
1006 #endif
1007
1008 MODULE_LICENSE("GPL");
This page took 0.089854 seconds and 4 git commands to generate.