2 * DRM driver for Pervasive Displays RePaper branded e-ink panels
4 * Copyright 2013-2017 Pervasive Displays, Inc.
5 * Copyright 2017 Noralf Trønnes
8 * Material Film: Aurora Mb (V231)
11 * The controller code was taken from the userspace driver:
12 * https://github.com/repaper/gratis
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
20 #include <linux/delay.h>
21 #include <linux/dma-buf.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/sched/clock.h>
26 #include <linux/spi/spi.h>
27 #include <linux/thermal.h>
29 #include <drm/drm_gem_framebuffer_helper.h>
30 #include <drm/tinydrm/tinydrm.h>
31 #include <drm/tinydrm/tinydrm-helpers.h>
33 #define REPAPER_RID_G2_COG_ID 0x12
42 enum repaper_stage
{ /* Image pixel -> Display pixel */
43 REPAPER_COMPENSATE
, /* B -> W, W -> B (Current Image) */
44 REPAPER_WHITE
, /* B -> N, W -> W (Current Image) */
45 REPAPER_INVERSE
, /* B -> N, W -> B (New Image) */
46 REPAPER_NORMAL
/* B -> B, W -> W (New Image) */
49 enum repaper_epd_border_byte
{
50 REPAPER_BORDER_BYTE_NONE
,
51 REPAPER_BORDER_BYTE_ZERO
,
52 REPAPER_BORDER_BYTE_SET
,
56 struct tinydrm_device tinydrm
;
57 struct spi_device
*spi
;
59 struct gpio_desc
*panel_on
;
60 struct gpio_desc
*border
;
61 struct gpio_desc
*discharge
;
62 struct gpio_desc
*reset
;
63 struct gpio_desc
*busy
;
65 struct thermal_zone_device
*thermal
;
69 unsigned int bytes_per_scan
;
70 const u8
*channel_select
;
71 unsigned int stage_time
;
72 unsigned int factored_stage_time
;
75 enum repaper_epd_border_byte border_byte
;
85 static inline struct repaper_epd
*
86 epd_from_tinydrm(struct tinydrm_device
*tdev
)
88 return container_of(tdev
, struct repaper_epd
, tinydrm
);
91 static int repaper_spi_transfer(struct spi_device
*spi
, u8 header
,
92 const void *tx
, void *rx
, size_t len
)
94 void *txbuf
= NULL
, *rxbuf
= NULL
;
95 struct spi_transfer tr
[2] = {};
99 headerbuf
= kmalloc(1, GFP_KERNEL
);
103 headerbuf
[0] = header
;
104 tr
[0].tx_buf
= headerbuf
;
107 /* Stack allocated tx? */
108 if (tx
&& len
<= 32) {
109 txbuf
= kmalloc(len
, GFP_KERNEL
);
114 memcpy(txbuf
, tx
, len
);
118 rxbuf
= kmalloc(len
, GFP_KERNEL
);
125 tr
[1].tx_buf
= txbuf
? txbuf
: tx
;
126 tr
[1].rx_buf
= rxbuf
;
130 ret
= spi_sync_transfer(spi
, tr
, 2);
132 memcpy(rx
, rxbuf
, len
);
142 static int repaper_write_buf(struct spi_device
*spi
, u8 reg
,
143 const u8
*buf
, size_t len
)
147 ret
= repaper_spi_transfer(spi
, 0x70, ®
, NULL
, 1);
151 return repaper_spi_transfer(spi
, 0x72, buf
, NULL
, len
);
154 static int repaper_write_val(struct spi_device
*spi
, u8 reg
, u8 val
)
156 return repaper_write_buf(spi
, reg
, &val
, 1);
159 static int repaper_read_val(struct spi_device
*spi
, u8 reg
)
164 ret
= repaper_spi_transfer(spi
, 0x70, ®
, NULL
, 1);
168 ret
= repaper_spi_transfer(spi
, 0x73, NULL
, &val
, 1);
170 return ret
? ret
: val
;
173 static int repaper_read_id(struct spi_device
*spi
)
178 ret
= repaper_spi_transfer(spi
, 0x71, NULL
, &id
, 1);
180 return ret
? ret
: id
;
183 static void repaper_spi_mosi_low(struct spi_device
*spi
)
185 const u8 buf
[1] = { 0 };
187 spi_write(spi
, buf
, 1);
190 /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */
191 static void repaper_even_pixels(struct repaper_epd
*epd
, u8
**pp
,
192 const u8
*data
, u8 fixed_value
, const u8
*mask
,
193 enum repaper_stage stage
)
197 for (b
= 0; b
< (epd
->width
/ 8); b
++) {
199 u8 pixels
= data
[b
] & 0xaa;
200 u8 pixel_mask
= 0xff;
204 pixel_mask
= (mask
[b
] ^ pixels
) & 0xaa;
205 pixel_mask
|= pixel_mask
>> 1;
209 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
210 pixels
= 0xaa | ((pixels
^ 0xaa) >> 1);
212 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
213 pixels
= 0x55 + ((pixels
^ 0xaa) >> 1);
215 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
216 pixels
= 0x55 | (pixels
^ 0xaa);
218 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
219 pixels
= 0xaa | (pixels
>> 1);
223 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x55);
224 p1
= (pixels
>> 6) & 0x03;
225 p2
= (pixels
>> 4) & 0x03;
226 p3
= (pixels
>> 2) & 0x03;
227 p4
= (pixels
>> 0) & 0x03;
228 pixels
= (p1
<< 0) | (p2
<< 2) | (p3
<< 4) | (p4
<< 6);
231 *(*pp
)++ = fixed_value
;
236 /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */
237 static void repaper_odd_pixels(struct repaper_epd
*epd
, u8
**pp
,
238 const u8
*data
, u8 fixed_value
, const u8
*mask
,
239 enum repaper_stage stage
)
243 for (b
= epd
->width
/ 8; b
> 0; b
--) {
245 u8 pixels
= data
[b
- 1] & 0x55;
246 u8 pixel_mask
= 0xff;
249 pixel_mask
= (mask
[b
- 1] ^ pixels
) & 0x55;
250 pixel_mask
|= pixel_mask
<< 1;
254 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
255 pixels
= 0xaa | (pixels
^ 0x55);
257 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
258 pixels
= 0x55 + (pixels
^ 0x55);
260 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
261 pixels
= 0x55 | ((pixels
^ 0x55) << 1);
263 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
264 pixels
= 0xaa | pixels
;
268 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x55);
271 *(*pp
)++ = fixed_value
;
276 /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
277 static inline u16
repaper_interleave_bits(u16 value
)
279 value
= (value
| (value
<< 4)) & 0x0f0f;
280 value
= (value
| (value
<< 2)) & 0x3333;
281 value
= (value
| (value
<< 1)) & 0x5555;
286 /* pixels on display are numbered from 1 */
287 static void repaper_all_pixels(struct repaper_epd
*epd
, u8
**pp
,
288 const u8
*data
, u8 fixed_value
, const u8
*mask
,
289 enum repaper_stage stage
)
293 for (b
= epd
->width
/ 8; b
> 0; b
--) {
295 u16 pixels
= repaper_interleave_bits(data
[b
- 1]);
296 u16 pixel_mask
= 0xffff;
299 pixel_mask
= repaper_interleave_bits(mask
[b
- 1]);
301 pixel_mask
= (pixel_mask
^ pixels
) & 0x5555;
302 pixel_mask
|= pixel_mask
<< 1;
306 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
307 pixels
= 0xaaaa | (pixels
^ 0x5555);
309 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
310 pixels
= 0x5555 + (pixels
^ 0x5555);
312 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
313 pixels
= 0x5555 | ((pixels
^ 0x5555) << 1);
315 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
316 pixels
= 0xaaaa | pixels
;
320 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x5555);
321 *(*pp
)++ = pixels
>> 8;
324 *(*pp
)++ = fixed_value
;
325 *(*pp
)++ = fixed_value
;
330 /* output one line of scan and data bytes to the display */
331 static void repaper_one_line(struct repaper_epd
*epd
, unsigned int line
,
332 const u8
*data
, u8 fixed_value
, const u8
*mask
,
333 enum repaper_stage stage
)
335 u8
*p
= epd
->line_buffer
;
338 repaper_spi_mosi_low(epd
->spi
);
340 if (epd
->pre_border_byte
)
343 if (epd
->middle_scan
) {
345 repaper_odd_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
348 for (b
= epd
->bytes_per_scan
; b
> 0; b
--) {
349 if (line
/ 4 == b
- 1)
350 *p
++ = 0x03 << (2 * (line
& 0x03));
356 repaper_even_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
359 * even scan line, but as lines on display are numbered from 1,
362 for (b
= 0; b
< epd
->bytes_per_scan
; b
++) {
363 if (0 != (line
& 0x01) && line
/ 8 == b
)
364 *p
++ = 0xc0 >> (line
& 0x06);
370 repaper_all_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
373 * odd scan line, but as lines on display are numbered from 1,
376 for (b
= epd
->bytes_per_scan
; b
> 0; b
--) {
377 if (0 == (line
& 0x01) && line
/ 8 == b
- 1)
378 *p
++ = 0x03 << (line
& 0x06);
384 switch (epd
->border_byte
) {
385 case REPAPER_BORDER_BYTE_NONE
:
388 case REPAPER_BORDER_BYTE_ZERO
:
392 case REPAPER_BORDER_BYTE_SET
:
394 case REPAPER_COMPENSATE
:
396 case REPAPER_INVERSE
:
406 repaper_write_buf(epd
->spi
, 0x0a, epd
->line_buffer
,
407 p
- epd
->line_buffer
);
409 /* Output data to panel */
410 repaper_write_val(epd
->spi
, 0x02, 0x07);
412 repaper_spi_mosi_low(epd
->spi
);
415 static void repaper_frame_fixed(struct repaper_epd
*epd
, u8 fixed_value
,
416 enum repaper_stage stage
)
420 for (line
= 0; line
< epd
->height
; line
++)
421 repaper_one_line(epd
, line
, NULL
, fixed_value
, NULL
, stage
);
424 static void repaper_frame_data(struct repaper_epd
*epd
, const u8
*image
,
425 const u8
*mask
, enum repaper_stage stage
)
430 for (line
= 0; line
< epd
->height
; line
++) {
431 repaper_one_line(epd
, line
,
432 &image
[line
* (epd
->width
/ 8)],
436 for (line
= 0; line
< epd
->height
; line
++) {
437 size_t n
= line
* epd
->width
/ 8;
439 repaper_one_line(epd
, line
, &image
[n
], 0, &mask
[n
],
445 static void repaper_frame_fixed_repeat(struct repaper_epd
*epd
, u8 fixed_value
,
446 enum repaper_stage stage
)
448 u64 start
= local_clock();
449 u64 end
= start
+ (epd
->factored_stage_time
* 1000 * 1000);
452 repaper_frame_fixed(epd
, fixed_value
, stage
);
453 } while (local_clock() < end
);
456 static void repaper_frame_data_repeat(struct repaper_epd
*epd
, const u8
*image
,
457 const u8
*mask
, enum repaper_stage stage
)
459 u64 start
= local_clock();
460 u64 end
= start
+ (epd
->factored_stage_time
* 1000 * 1000);
463 repaper_frame_data(epd
, image
, mask
, stage
);
464 } while (local_clock() < end
);
467 static void repaper_get_temperature(struct repaper_epd
*epd
)
469 int ret
, temperature
= 0;
470 unsigned int factor10x
;
475 ret
= thermal_zone_get_temp(epd
->thermal
, &temperature
);
477 DRM_DEV_ERROR(&epd
->spi
->dev
, "Failed to get temperature (%d)\n", ret
);
483 if (temperature
<= -10)
485 else if (temperature
<= -5)
487 else if (temperature
<= 5)
489 else if (temperature
<= 10)
491 else if (temperature
<= 15)
493 else if (temperature
<= 20)
495 else if (temperature
<= 40)
500 epd
->factored_stage_time
= epd
->stage_time
* factor10x
/ 10;
503 static void repaper_gray8_to_mono_reversed(u8
*buf
, u32 width
, u32 height
)
505 u8
*gray8
= buf
, *mono
= buf
;
508 for (y
= 0; y
< height
; y
++)
509 for (xb
= 0; xb
< width
/ 8; xb
++) {
512 for (i
= 0; i
< 8; i
++) {
516 if (gray8
[y
* width
+ x
] >> 7)
523 static int repaper_fb_dirty(struct drm_framebuffer
*fb
,
524 struct drm_file
*file_priv
,
525 unsigned int flags
, unsigned int color
,
526 struct drm_clip_rect
*clips
,
527 unsigned int num_clips
)
529 struct drm_gem_cma_object
*cma_obj
= drm_fb_cma_get_gem_obj(fb
, 0);
530 struct dma_buf_attachment
*import_attach
= cma_obj
->base
.import_attach
;
531 struct tinydrm_device
*tdev
= fb
->dev
->dev_private
;
532 struct repaper_epd
*epd
= epd_from_tinydrm(tdev
);
533 struct drm_clip_rect clip
;
537 /* repaper can't do partial updates */
541 clip
.y2
= fb
->height
;
543 mutex_lock(&tdev
->dirty_lock
);
548 /* fbdev can flush even when we're not interested */
549 if (tdev
->pipe
.plane
.fb
!= fb
)
552 repaper_get_temperature(epd
);
554 DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb
->base
.id
,
555 epd
->factored_stage_time
);
557 buf
= kmalloc(fb
->width
* fb
->height
, GFP_KERNEL
);
564 ret
= dma_buf_begin_cpu_access(import_attach
->dmabuf
,
570 tinydrm_xrgb8888_to_gray8(buf
, cma_obj
->vaddr
, fb
, &clip
);
573 ret
= dma_buf_end_cpu_access(import_attach
->dmabuf
,
579 repaper_gray8_to_mono_reversed(buf
, fb
->width
, fb
->height
);
582 repaper_frame_data_repeat(epd
, buf
, epd
->current_frame
,
584 } else if (epd
->cleared
) {
585 repaper_frame_data_repeat(epd
, epd
->current_frame
, NULL
,
587 repaper_frame_data_repeat(epd
, epd
->current_frame
, NULL
,
589 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_INVERSE
);
590 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_NORMAL
);
594 /* Clear display (anything -> white) */
595 repaper_frame_fixed_repeat(epd
, 0xff, REPAPER_COMPENSATE
);
596 repaper_frame_fixed_repeat(epd
, 0xff, REPAPER_WHITE
);
597 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_INVERSE
);
598 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_NORMAL
);
600 /* Assuming a clear (white) screen output an image */
601 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_COMPENSATE
);
602 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_WHITE
);
603 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_INVERSE
);
604 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_NORMAL
);
610 memcpy(epd
->current_frame
, buf
, fb
->width
* fb
->height
/ 8);
613 * An extra frame write is needed if pixels are set in the bottom line,
614 * or else grey lines rises up from the pixels
616 if (epd
->pre_border_byte
) {
619 for (x
= 0; x
< (fb
->width
/ 8); x
++)
620 if (buf
[x
+ (fb
->width
* (fb
->height
- 1) / 8)]) {
621 repaper_frame_data_repeat(epd
, buf
,
629 mutex_unlock(&tdev
->dirty_lock
);
632 DRM_DEV_ERROR(fb
->dev
->dev
, "Failed to update display (%d)\n", ret
);
638 static const struct drm_framebuffer_funcs repaper_fb_funcs
= {
639 .destroy
= drm_gem_fb_destroy
,
640 .create_handle
= drm_gem_fb_create_handle
,
641 .dirty
= repaper_fb_dirty
,
644 static void power_off(struct repaper_epd
*epd
)
646 /* Turn off power and all signals */
647 gpiod_set_value_cansleep(epd
->reset
, 0);
648 gpiod_set_value_cansleep(epd
->panel_on
, 0);
650 gpiod_set_value_cansleep(epd
->border
, 0);
652 /* Ensure SPI MOSI and CLOCK are Low before CS Low */
653 repaper_spi_mosi_low(epd
->spi
);
655 /* Discharge pulse */
656 gpiod_set_value_cansleep(epd
->discharge
, 1);
658 gpiod_set_value_cansleep(epd
->discharge
, 0);
661 static void repaper_pipe_enable(struct drm_simple_display_pipe
*pipe
,
662 struct drm_crtc_state
*crtc_state
)
664 struct tinydrm_device
*tdev
= pipe_to_tinydrm(pipe
);
665 struct repaper_epd
*epd
= epd_from_tinydrm(tdev
);
666 struct spi_device
*spi
= epd
->spi
;
667 struct device
*dev
= &spi
->dev
;
671 DRM_DEBUG_DRIVER("\n");
673 /* Power up sequence */
674 gpiod_set_value_cansleep(epd
->reset
, 0);
675 gpiod_set_value_cansleep(epd
->panel_on
, 0);
676 gpiod_set_value_cansleep(epd
->discharge
, 0);
678 gpiod_set_value_cansleep(epd
->border
, 0);
679 repaper_spi_mosi_low(spi
);
680 usleep_range(5000, 10000);
682 gpiod_set_value_cansleep(epd
->panel_on
, 1);
684 * This delay comes from the repaper.org userspace driver, it's not
685 * mentioned in the datasheet.
687 usleep_range(10000, 15000);
688 gpiod_set_value_cansleep(epd
->reset
, 1);
690 gpiod_set_value_cansleep(epd
->border
, 1);
691 usleep_range(5000, 10000);
692 gpiod_set_value_cansleep(epd
->reset
, 0);
693 usleep_range(5000, 10000);
694 gpiod_set_value_cansleep(epd
->reset
, 1);
695 usleep_range(5000, 10000);
697 /* Wait for COG to become ready */
698 for (i
= 100; i
> 0; i
--) {
699 if (!gpiod_get_value_cansleep(epd
->busy
))
702 usleep_range(10, 100);
706 DRM_DEV_ERROR(dev
, "timeout waiting for panel to become ready.\n");
711 repaper_read_id(spi
);
712 ret
= repaper_read_id(spi
);
713 if (ret
!= REPAPER_RID_G2_COG_ID
) {
715 dev_err(dev
, "failed to read chip (%d)\n", ret
);
717 dev_err(dev
, "wrong COG ID 0x%02x\n", ret
);
723 repaper_write_val(spi
, 0x02, 0x40);
725 ret
= repaper_read_val(spi
, 0x0f);
726 if (ret
< 0 || !(ret
& 0x80)) {
728 DRM_DEV_ERROR(dev
, "failed to read chip (%d)\n", ret
);
730 DRM_DEV_ERROR(dev
, "panel is reported broken\n");
735 /* Power saving mode */
736 repaper_write_val(spi
, 0x0b, 0x02);
738 repaper_write_buf(spi
, 0x01, epd
->channel_select
, 8);
739 /* High power mode osc */
740 repaper_write_val(spi
, 0x07, 0xd1);
742 repaper_write_val(spi
, 0x08, 0x02);
744 repaper_write_val(spi
, 0x09, 0xc2);
746 repaper_write_val(spi
, 0x04, 0x03);
747 /* Driver latch on */
748 repaper_write_val(spi
, 0x03, 0x01);
749 /* Driver latch off */
750 repaper_write_val(spi
, 0x03, 0x00);
751 usleep_range(5000, 10000);
753 /* Start chargepump */
754 for (i
= 0; i
< 4; ++i
) {
755 /* Charge pump positive voltage on - VGH/VDL on */
756 repaper_write_val(spi
, 0x05, 0x01);
759 /* Charge pump negative voltage on - VGL/VDL on */
760 repaper_write_val(spi
, 0x05, 0x03);
763 /* Charge pump Vcom on - Vcom driver on */
764 repaper_write_val(spi
, 0x05, 0x0f);
768 ret
= repaper_read_val(spi
, 0x0f);
770 DRM_DEV_ERROR(dev
, "failed to read chip (%d)\n", ret
);
782 DRM_DEV_ERROR(dev
, "dc/dc failed\n");
788 * Output enable to disable
789 * The userspace driver sets this to 0x04, but the datasheet says 0x06
791 repaper_write_val(spi
, 0x02, 0x04);
794 epd
->partial
= false;
797 static void repaper_pipe_disable(struct drm_simple_display_pipe
*pipe
)
799 struct tinydrm_device
*tdev
= pipe_to_tinydrm(pipe
);
800 struct repaper_epd
*epd
= epd_from_tinydrm(tdev
);
801 struct spi_device
*spi
= epd
->spi
;
804 DRM_DEBUG_DRIVER("\n");
806 mutex_lock(&tdev
->dirty_lock
);
807 epd
->enabled
= false;
808 mutex_unlock(&tdev
->dirty_lock
);
811 for (line
= 0; line
< epd
->height
; line
++)
812 repaper_one_line(epd
, 0x7fffu
, NULL
, 0x00, NULL
,
818 repaper_one_line(epd
, 0x7fffu
, NULL
, 0x00, NULL
,
821 gpiod_set_value_cansleep(epd
->border
, 0);
823 gpiod_set_value_cansleep(epd
->border
, 1);
825 /* Border dummy line */
826 repaper_one_line(epd
, 0x7fffu
, NULL
, 0x00, NULL
,
831 /* not described in datasheet */
832 repaper_write_val(spi
, 0x0b, 0x00);
833 /* Latch reset turn on */
834 repaper_write_val(spi
, 0x03, 0x01);
835 /* Power off charge pump Vcom */
836 repaper_write_val(spi
, 0x05, 0x03);
837 /* Power off charge pump neg voltage */
838 repaper_write_val(spi
, 0x05, 0x01);
840 /* Discharge internal */
841 repaper_write_val(spi
, 0x04, 0x80);
842 /* turn off all charge pumps */
843 repaper_write_val(spi
, 0x05, 0x00);
845 repaper_write_val(spi
, 0x07, 0x01);
851 static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs
= {
852 .enable
= repaper_pipe_enable
,
853 .disable
= repaper_pipe_disable
,
854 .update
= tinydrm_display_pipe_update
,
855 .prepare_fb
= tinydrm_display_pipe_prepare_fb
,
858 static const uint32_t repaper_formats
[] = {
862 static const struct drm_display_mode repaper_e1144cs021_mode
= {
863 TINYDRM_MODE(128, 96, 29, 22),
866 static const u8 repaper_e1144cs021_cs
[] = { 0x00, 0x00, 0x00, 0x00,
867 0x00, 0x0f, 0xff, 0x00 };
869 static const struct drm_display_mode repaper_e1190cs021_mode
= {
870 TINYDRM_MODE(144, 128, 36, 32),
873 static const u8 repaper_e1190cs021_cs
[] = { 0x00, 0x00, 0x00, 0x03,
874 0xfc, 0x00, 0x00, 0xff };
876 static const struct drm_display_mode repaper_e2200cs021_mode
= {
877 TINYDRM_MODE(200, 96, 46, 22),
880 static const u8 repaper_e2200cs021_cs
[] = { 0x00, 0x00, 0x00, 0x00,
881 0x01, 0xff, 0xe0, 0x00 };
883 static const struct drm_display_mode repaper_e2271cs021_mode
= {
884 TINYDRM_MODE(264, 176, 57, 38),
887 static const u8 repaper_e2271cs021_cs
[] = { 0x00, 0x00, 0x00, 0x7f,
888 0xff, 0xfe, 0x00, 0x00 };
890 DEFINE_DRM_GEM_CMA_FOPS(repaper_fops
);
892 static struct drm_driver repaper_driver
= {
893 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
|
895 .fops
= &repaper_fops
,
896 TINYDRM_GEM_DRIVER_OPS
,
898 .desc
= "Pervasive Displays RePaper e-ink panels",
904 static const struct of_device_id repaper_of_match
[] = {
905 { .compatible
= "pervasive,e1144cs021", .data
= (void *)E1144CS021
},
906 { .compatible
= "pervasive,e1190cs021", .data
= (void *)E1190CS021
},
907 { .compatible
= "pervasive,e2200cs021", .data
= (void *)E2200CS021
},
908 { .compatible
= "pervasive,e2271cs021", .data
= (void *)E2271CS021
},
911 MODULE_DEVICE_TABLE(of
, repaper_of_match
);
913 static const struct spi_device_id repaper_id
[] = {
914 { "e1144cs021", E1144CS021
},
915 { "e1190cs021", E1190CS021
},
916 { "e2200cs021", E2200CS021
},
917 { "e2271cs021", E2271CS021
},
920 MODULE_DEVICE_TABLE(spi
, repaper_id
);
922 static int repaper_probe(struct spi_device
*spi
)
924 const struct drm_display_mode
*mode
;
925 const struct spi_device_id
*spi_id
;
926 const struct of_device_id
*match
;
927 struct device
*dev
= &spi
->dev
;
928 struct tinydrm_device
*tdev
;
929 enum repaper_model model
;
930 const char *thermal_zone
;
931 struct repaper_epd
*epd
;
932 size_t line_buffer_size
;
935 match
= of_match_device(repaper_of_match
, dev
);
937 model
= (enum repaper_model
)match
->data
;
939 spi_id
= spi_get_device_id(spi
);
940 model
= spi_id
->driver_data
;
943 /* The SPI device is used to allocate dma memory */
944 if (!dev
->coherent_dma_mask
) {
945 ret
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(32));
947 dev_warn(dev
, "Failed to set dma mask %d\n", ret
);
952 epd
= devm_kzalloc(dev
, sizeof(*epd
), GFP_KERNEL
);
958 epd
->panel_on
= devm_gpiod_get(dev
, "panel-on", GPIOD_OUT_LOW
);
959 if (IS_ERR(epd
->panel_on
)) {
960 ret
= PTR_ERR(epd
->panel_on
);
961 if (ret
!= -EPROBE_DEFER
)
962 DRM_DEV_ERROR(dev
, "Failed to get gpio 'panel-on'\n");
966 epd
->discharge
= devm_gpiod_get(dev
, "discharge", GPIOD_OUT_LOW
);
967 if (IS_ERR(epd
->discharge
)) {
968 ret
= PTR_ERR(epd
->discharge
);
969 if (ret
!= -EPROBE_DEFER
)
970 DRM_DEV_ERROR(dev
, "Failed to get gpio 'discharge'\n");
974 epd
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
975 if (IS_ERR(epd
->reset
)) {
976 ret
= PTR_ERR(epd
->reset
);
977 if (ret
!= -EPROBE_DEFER
)
978 DRM_DEV_ERROR(dev
, "Failed to get gpio 'reset'\n");
982 epd
->busy
= devm_gpiod_get(dev
, "busy", GPIOD_IN
);
983 if (IS_ERR(epd
->busy
)) {
984 ret
= PTR_ERR(epd
->busy
);
985 if (ret
!= -EPROBE_DEFER
)
986 DRM_DEV_ERROR(dev
, "Failed to get gpio 'busy'\n");
990 if (!device_property_read_string(dev
, "pervasive,thermal-zone",
992 epd
->thermal
= thermal_zone_get_zone_by_name(thermal_zone
);
993 if (IS_ERR(epd
->thermal
)) {
994 DRM_DEV_ERROR(dev
, "Failed to get thermal zone: %s\n", thermal_zone
);
995 return PTR_ERR(epd
->thermal
);
1001 mode
= &repaper_e1144cs021_mode
;
1002 epd
->channel_select
= repaper_e1144cs021_cs
;
1003 epd
->stage_time
= 480;
1004 epd
->bytes_per_scan
= 96 / 4;
1005 epd
->middle_scan
= true; /* data-scan-data */
1006 epd
->pre_border_byte
= false;
1007 epd
->border_byte
= REPAPER_BORDER_BYTE_ZERO
;
1011 mode
= &repaper_e1190cs021_mode
;
1012 epd
->channel_select
= repaper_e1190cs021_cs
;
1013 epd
->stage_time
= 480;
1014 epd
->bytes_per_scan
= 128 / 4 / 2;
1015 epd
->middle_scan
= false; /* scan-data-scan */
1016 epd
->pre_border_byte
= false;
1017 epd
->border_byte
= REPAPER_BORDER_BYTE_SET
;
1021 mode
= &repaper_e2200cs021_mode
;
1022 epd
->channel_select
= repaper_e2200cs021_cs
;
1023 epd
->stage_time
= 480;
1024 epd
->bytes_per_scan
= 96 / 4;
1025 epd
->middle_scan
= true; /* data-scan-data */
1026 epd
->pre_border_byte
= true;
1027 epd
->border_byte
= REPAPER_BORDER_BYTE_NONE
;
1031 epd
->border
= devm_gpiod_get(dev
, "border", GPIOD_OUT_LOW
);
1032 if (IS_ERR(epd
->border
)) {
1033 ret
= PTR_ERR(epd
->border
);
1034 if (ret
!= -EPROBE_DEFER
)
1035 DRM_DEV_ERROR(dev
, "Failed to get gpio 'border'\n");
1039 mode
= &repaper_e2271cs021_mode
;
1040 epd
->channel_select
= repaper_e2271cs021_cs
;
1041 epd
->stage_time
= 630;
1042 epd
->bytes_per_scan
= 176 / 4;
1043 epd
->middle_scan
= true; /* data-scan-data */
1044 epd
->pre_border_byte
= true;
1045 epd
->border_byte
= REPAPER_BORDER_BYTE_NONE
;
1052 epd
->width
= mode
->hdisplay
;
1053 epd
->height
= mode
->vdisplay
;
1054 epd
->factored_stage_time
= epd
->stage_time
;
1056 line_buffer_size
= 2 * epd
->width
/ 8 + epd
->bytes_per_scan
+ 2;
1057 epd
->line_buffer
= devm_kzalloc(dev
, line_buffer_size
, GFP_KERNEL
);
1058 if (!epd
->line_buffer
)
1061 epd
->current_frame
= devm_kzalloc(dev
, epd
->width
* epd
->height
/ 8,
1063 if (!epd
->current_frame
)
1066 tdev
= &epd
->tinydrm
;
1068 ret
= devm_tinydrm_init(dev
, tdev
, &repaper_fb_funcs
, &repaper_driver
);
1072 ret
= tinydrm_display_pipe_init(tdev
, &repaper_pipe_funcs
,
1073 DRM_MODE_CONNECTOR_VIRTUAL
,
1075 ARRAY_SIZE(repaper_formats
), mode
, 0);
1079 drm_mode_config_reset(tdev
->drm
);
1080 spi_set_drvdata(spi
, tdev
);
1082 DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi
->max_speed_hz
/ 1000000);
1084 return devm_tinydrm_register(tdev
);
1087 static void repaper_shutdown(struct spi_device
*spi
)
1089 struct tinydrm_device
*tdev
= spi_get_drvdata(spi
);
1091 tinydrm_shutdown(tdev
);
1094 static struct spi_driver repaper_spi_driver
= {
1097 .owner
= THIS_MODULE
,
1098 .of_match_table
= repaper_of_match
,
1100 .id_table
= repaper_id
,
1101 .probe
= repaper_probe
,
1102 .shutdown
= repaper_shutdown
,
1104 module_spi_driver(repaper_spi_driver
);
1106 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1107 MODULE_AUTHOR("Noralf Trønnes");
1108 MODULE_LICENSE("GPL");