1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DRM driver for Pervasive Displays RePaper branded e-ink panels
5 * Copyright 2013-2017 Pervasive Displays, Inc.
6 * Copyright 2017 Noralf Trønnes
9 * Material Film: Aurora Mb (V231)
12 * The controller code was taken from the userspace driver:
13 * https://github.com/repaper/gratis
16 #include <linux/delay.h>
17 #include <linux/dma-buf.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/sched/clock.h>
22 #include <linux/spi/spi.h>
23 #include <linux/thermal.h>
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_connector.h>
27 #include <drm/drm_damage_helper.h>
28 #include <drm/drm_drv.h>
29 #include <drm/drm_fb_cma_helper.h>
30 #include <drm/drm_fb_helper.h>
31 #include <drm/drm_format_helper.h>
32 #include <drm/drm_gem_cma_helper.h>
33 #include <drm/drm_gem_framebuffer_helper.h>
34 #include <drm/drm_modes.h>
35 #include <drm/drm_rect.h>
36 #include <drm/drm_vblank.h>
37 #include <drm/drm_probe_helper.h>
38 #include <drm/drm_simple_kms_helper.h>
40 #define REPAPER_RID_G2_COG_ID 0x12
49 enum repaper_stage
{ /* Image pixel -> Display pixel */
50 REPAPER_COMPENSATE
, /* B -> W, W -> B (Current Image) */
51 REPAPER_WHITE
, /* B -> N, W -> W (Current Image) */
52 REPAPER_INVERSE
, /* B -> N, W -> B (New Image) */
53 REPAPER_NORMAL
/* B -> B, W -> W (New Image) */
56 enum repaper_epd_border_byte
{
57 REPAPER_BORDER_BYTE_NONE
,
58 REPAPER_BORDER_BYTE_ZERO
,
59 REPAPER_BORDER_BYTE_SET
,
63 struct drm_device drm
;
64 struct drm_simple_display_pipe pipe
;
65 const struct drm_display_mode
*mode
;
66 struct drm_connector connector
;
67 struct spi_device
*spi
;
69 struct gpio_desc
*panel_on
;
70 struct gpio_desc
*border
;
71 struct gpio_desc
*discharge
;
72 struct gpio_desc
*reset
;
73 struct gpio_desc
*busy
;
75 struct thermal_zone_device
*thermal
;
79 unsigned int bytes_per_scan
;
80 const u8
*channel_select
;
81 unsigned int stage_time
;
82 unsigned int factored_stage_time
;
85 enum repaper_epd_border_byte border_byte
;
95 static inline struct repaper_epd
*drm_to_epd(struct drm_device
*drm
)
97 return container_of(drm
, struct repaper_epd
, drm
);
100 static int repaper_spi_transfer(struct spi_device
*spi
, u8 header
,
101 const void *tx
, void *rx
, size_t len
)
103 void *txbuf
= NULL
, *rxbuf
= NULL
;
104 struct spi_transfer tr
[2] = {};
108 headerbuf
= kmalloc(1, GFP_KERNEL
);
112 headerbuf
[0] = header
;
113 tr
[0].tx_buf
= headerbuf
;
116 /* Stack allocated tx? */
117 if (tx
&& len
<= 32) {
118 txbuf
= kmemdup(tx
, len
, GFP_KERNEL
);
126 rxbuf
= kmalloc(len
, GFP_KERNEL
);
133 tr
[1].tx_buf
= txbuf
? txbuf
: tx
;
134 tr
[1].rx_buf
= rxbuf
;
138 ret
= spi_sync_transfer(spi
, tr
, 2);
140 memcpy(rx
, rxbuf
, len
);
150 static int repaper_write_buf(struct spi_device
*spi
, u8 reg
,
151 const u8
*buf
, size_t len
)
155 ret
= repaper_spi_transfer(spi
, 0x70, ®
, NULL
, 1);
159 return repaper_spi_transfer(spi
, 0x72, buf
, NULL
, len
);
162 static int repaper_write_val(struct spi_device
*spi
, u8 reg
, u8 val
)
164 return repaper_write_buf(spi
, reg
, &val
, 1);
167 static int repaper_read_val(struct spi_device
*spi
, u8 reg
)
172 ret
= repaper_spi_transfer(spi
, 0x70, ®
, NULL
, 1);
176 ret
= repaper_spi_transfer(spi
, 0x73, NULL
, &val
, 1);
178 return ret
? ret
: val
;
181 static int repaper_read_id(struct spi_device
*spi
)
186 ret
= repaper_spi_transfer(spi
, 0x71, NULL
, &id
, 1);
188 return ret
? ret
: id
;
191 static void repaper_spi_mosi_low(struct spi_device
*spi
)
193 const u8 buf
[1] = { 0 };
195 spi_write(spi
, buf
, 1);
198 /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */
199 static void repaper_even_pixels(struct repaper_epd
*epd
, u8
**pp
,
200 const u8
*data
, u8 fixed_value
, const u8
*mask
,
201 enum repaper_stage stage
)
205 for (b
= 0; b
< (epd
->width
/ 8); b
++) {
207 u8 pixels
= data
[b
] & 0xaa;
208 u8 pixel_mask
= 0xff;
212 pixel_mask
= (mask
[b
] ^ pixels
) & 0xaa;
213 pixel_mask
|= pixel_mask
>> 1;
217 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
218 pixels
= 0xaa | ((pixels
^ 0xaa) >> 1);
220 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
221 pixels
= 0x55 + ((pixels
^ 0xaa) >> 1);
223 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
224 pixels
= 0x55 | (pixels
^ 0xaa);
226 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
227 pixels
= 0xaa | (pixels
>> 1);
231 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x55);
232 p1
= (pixels
>> 6) & 0x03;
233 p2
= (pixels
>> 4) & 0x03;
234 p3
= (pixels
>> 2) & 0x03;
235 p4
= (pixels
>> 0) & 0x03;
236 pixels
= (p1
<< 0) | (p2
<< 2) | (p3
<< 4) | (p4
<< 6);
239 *(*pp
)++ = fixed_value
;
244 /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */
245 static void repaper_odd_pixels(struct repaper_epd
*epd
, u8
**pp
,
246 const u8
*data
, u8 fixed_value
, const u8
*mask
,
247 enum repaper_stage stage
)
251 for (b
= epd
->width
/ 8; b
> 0; b
--) {
253 u8 pixels
= data
[b
- 1] & 0x55;
254 u8 pixel_mask
= 0xff;
257 pixel_mask
= (mask
[b
- 1] ^ pixels
) & 0x55;
258 pixel_mask
|= pixel_mask
<< 1;
262 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
263 pixels
= 0xaa | (pixels
^ 0x55);
265 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
266 pixels
= 0x55 + (pixels
^ 0x55);
268 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
269 pixels
= 0x55 | ((pixels
^ 0x55) << 1);
271 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
272 pixels
= 0xaa | pixels
;
276 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x55);
279 *(*pp
)++ = fixed_value
;
284 /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
285 static inline u16
repaper_interleave_bits(u16 value
)
287 value
= (value
| (value
<< 4)) & 0x0f0f;
288 value
= (value
| (value
<< 2)) & 0x3333;
289 value
= (value
| (value
<< 1)) & 0x5555;
294 /* pixels on display are numbered from 1 */
295 static void repaper_all_pixels(struct repaper_epd
*epd
, u8
**pp
,
296 const u8
*data
, u8 fixed_value
, const u8
*mask
,
297 enum repaper_stage stage
)
301 for (b
= epd
->width
/ 8; b
> 0; b
--) {
303 u16 pixels
= repaper_interleave_bits(data
[b
- 1]);
304 u16 pixel_mask
= 0xffff;
307 pixel_mask
= repaper_interleave_bits(mask
[b
- 1]);
309 pixel_mask
= (pixel_mask
^ pixels
) & 0x5555;
310 pixel_mask
|= pixel_mask
<< 1;
314 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
315 pixels
= 0xaaaa | (pixels
^ 0x5555);
317 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
318 pixels
= 0x5555 + (pixels
^ 0x5555);
320 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
321 pixels
= 0x5555 | ((pixels
^ 0x5555) << 1);
323 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
324 pixels
= 0xaaaa | pixels
;
328 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x5555);
329 *(*pp
)++ = pixels
>> 8;
332 *(*pp
)++ = fixed_value
;
333 *(*pp
)++ = fixed_value
;
338 /* output one line of scan and data bytes to the display */
339 static void repaper_one_line(struct repaper_epd
*epd
, unsigned int line
,
340 const u8
*data
, u8 fixed_value
, const u8
*mask
,
341 enum repaper_stage stage
)
343 u8
*p
= epd
->line_buffer
;
346 repaper_spi_mosi_low(epd
->spi
);
348 if (epd
->pre_border_byte
)
351 if (epd
->middle_scan
) {
353 repaper_odd_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
356 for (b
= epd
->bytes_per_scan
; b
> 0; b
--) {
357 if (line
/ 4 == b
- 1)
358 *p
++ = 0x03 << (2 * (line
& 0x03));
364 repaper_even_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
367 * even scan line, but as lines on display are numbered from 1,
370 for (b
= 0; b
< epd
->bytes_per_scan
; b
++) {
371 if (0 != (line
& 0x01) && line
/ 8 == b
)
372 *p
++ = 0xc0 >> (line
& 0x06);
378 repaper_all_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
381 * odd scan line, but as lines on display are numbered from 1,
384 for (b
= epd
->bytes_per_scan
; b
> 0; b
--) {
385 if (0 == (line
& 0x01) && line
/ 8 == b
- 1)
386 *p
++ = 0x03 << (line
& 0x06);
392 switch (epd
->border_byte
) {
393 case REPAPER_BORDER_BYTE_NONE
:
396 case REPAPER_BORDER_BYTE_ZERO
:
400 case REPAPER_BORDER_BYTE_SET
:
402 case REPAPER_COMPENSATE
:
404 case REPAPER_INVERSE
:
414 repaper_write_buf(epd
->spi
, 0x0a, epd
->line_buffer
,
415 p
- epd
->line_buffer
);
417 /* Output data to panel */
418 repaper_write_val(epd
->spi
, 0x02, 0x07);
420 repaper_spi_mosi_low(epd
->spi
);
423 static void repaper_frame_fixed(struct repaper_epd
*epd
, u8 fixed_value
,
424 enum repaper_stage stage
)
428 for (line
= 0; line
< epd
->height
; line
++)
429 repaper_one_line(epd
, line
, NULL
, fixed_value
, NULL
, stage
);
432 static void repaper_frame_data(struct repaper_epd
*epd
, const u8
*image
,
433 const u8
*mask
, enum repaper_stage stage
)
438 for (line
= 0; line
< epd
->height
; line
++) {
439 repaper_one_line(epd
, line
,
440 &image
[line
* (epd
->width
/ 8)],
444 for (line
= 0; line
< epd
->height
; line
++) {
445 size_t n
= line
* epd
->width
/ 8;
447 repaper_one_line(epd
, line
, &image
[n
], 0, &mask
[n
],
453 static void repaper_frame_fixed_repeat(struct repaper_epd
*epd
, u8 fixed_value
,
454 enum repaper_stage stage
)
456 u64 start
= local_clock();
457 u64 end
= start
+ (epd
->factored_stage_time
* 1000 * 1000);
460 repaper_frame_fixed(epd
, fixed_value
, stage
);
461 } while (local_clock() < end
);
464 static void repaper_frame_data_repeat(struct repaper_epd
*epd
, const u8
*image
,
465 const u8
*mask
, enum repaper_stage stage
)
467 u64 start
= local_clock();
468 u64 end
= start
+ (epd
->factored_stage_time
* 1000 * 1000);
471 repaper_frame_data(epd
, image
, mask
, stage
);
472 } while (local_clock() < end
);
475 static void repaper_get_temperature(struct repaper_epd
*epd
)
477 int ret
, temperature
= 0;
478 unsigned int factor10x
;
483 ret
= thermal_zone_get_temp(epd
->thermal
, &temperature
);
485 DRM_DEV_ERROR(&epd
->spi
->dev
, "Failed to get temperature (%d)\n", ret
);
491 if (temperature
<= -10)
493 else if (temperature
<= -5)
495 else if (temperature
<= 5)
497 else if (temperature
<= 10)
499 else if (temperature
<= 15)
501 else if (temperature
<= 20)
503 else if (temperature
<= 40)
508 epd
->factored_stage_time
= epd
->stage_time
* factor10x
/ 10;
511 static void repaper_gray8_to_mono_reversed(u8
*buf
, u32 width
, u32 height
)
513 u8
*gray8
= buf
, *mono
= buf
;
516 for (y
= 0; y
< height
; y
++)
517 for (xb
= 0; xb
< width
/ 8; xb
++) {
520 for (i
= 0; i
< 8; i
++) {
524 if (gray8
[y
* width
+ x
] >> 7)
531 static int repaper_fb_dirty(struct drm_framebuffer
*fb
)
533 struct drm_gem_cma_object
*cma_obj
= drm_fb_cma_get_gem_obj(fb
, 0);
534 struct dma_buf_attachment
*import_attach
= cma_obj
->base
.import_attach
;
535 struct repaper_epd
*epd
= drm_to_epd(fb
->dev
);
536 struct drm_rect clip
;
543 if (!drm_dev_enter(fb
->dev
, &idx
))
546 /* repaper can't do partial updates */
550 clip
.y2
= fb
->height
;
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_array(fb
->width
, fb
->height
, GFP_KERNEL
);
564 ret
= dma_buf_begin_cpu_access(import_attach
->dmabuf
,
570 drm_fb_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
,
636 static void power_off(struct repaper_epd
*epd
)
638 /* Turn off power and all signals */
639 gpiod_set_value_cansleep(epd
->reset
, 0);
640 gpiod_set_value_cansleep(epd
->panel_on
, 0);
642 gpiod_set_value_cansleep(epd
->border
, 0);
644 /* Ensure SPI MOSI and CLOCK are Low before CS Low */
645 repaper_spi_mosi_low(epd
->spi
);
647 /* Discharge pulse */
648 gpiod_set_value_cansleep(epd
->discharge
, 1);
650 gpiod_set_value_cansleep(epd
->discharge
, 0);
653 static void repaper_pipe_enable(struct drm_simple_display_pipe
*pipe
,
654 struct drm_crtc_state
*crtc_state
,
655 struct drm_plane_state
*plane_state
)
657 struct repaper_epd
*epd
= drm_to_epd(pipe
->crtc
.dev
);
658 struct spi_device
*spi
= epd
->spi
;
659 struct device
*dev
= &spi
->dev
;
663 if (!drm_dev_enter(pipe
->crtc
.dev
, &idx
))
666 DRM_DEBUG_DRIVER("\n");
668 /* Power up sequence */
669 gpiod_set_value_cansleep(epd
->reset
, 0);
670 gpiod_set_value_cansleep(epd
->panel_on
, 0);
671 gpiod_set_value_cansleep(epd
->discharge
, 0);
673 gpiod_set_value_cansleep(epd
->border
, 0);
674 repaper_spi_mosi_low(spi
);
675 usleep_range(5000, 10000);
677 gpiod_set_value_cansleep(epd
->panel_on
, 1);
679 * This delay comes from the repaper.org userspace driver, it's not
680 * mentioned in the datasheet.
682 usleep_range(10000, 15000);
683 gpiod_set_value_cansleep(epd
->reset
, 1);
685 gpiod_set_value_cansleep(epd
->border
, 1);
686 usleep_range(5000, 10000);
687 gpiod_set_value_cansleep(epd
->reset
, 0);
688 usleep_range(5000, 10000);
689 gpiod_set_value_cansleep(epd
->reset
, 1);
690 usleep_range(5000, 10000);
692 /* Wait for COG to become ready */
693 for (i
= 100; i
> 0; i
--) {
694 if (!gpiod_get_value_cansleep(epd
->busy
))
697 usleep_range(10, 100);
701 DRM_DEV_ERROR(dev
, "timeout waiting for panel to become ready.\n");
706 repaper_read_id(spi
);
707 ret
= repaper_read_id(spi
);
708 if (ret
!= REPAPER_RID_G2_COG_ID
) {
710 dev_err(dev
, "failed to read chip (%d)\n", ret
);
712 dev_err(dev
, "wrong COG ID 0x%02x\n", ret
);
718 repaper_write_val(spi
, 0x02, 0x40);
720 ret
= repaper_read_val(spi
, 0x0f);
721 if (ret
< 0 || !(ret
& 0x80)) {
723 DRM_DEV_ERROR(dev
, "failed to read chip (%d)\n", ret
);
725 DRM_DEV_ERROR(dev
, "panel is reported broken\n");
730 /* Power saving mode */
731 repaper_write_val(spi
, 0x0b, 0x02);
733 repaper_write_buf(spi
, 0x01, epd
->channel_select
, 8);
734 /* High power mode osc */
735 repaper_write_val(spi
, 0x07, 0xd1);
737 repaper_write_val(spi
, 0x08, 0x02);
739 repaper_write_val(spi
, 0x09, 0xc2);
741 repaper_write_val(spi
, 0x04, 0x03);
742 /* Driver latch on */
743 repaper_write_val(spi
, 0x03, 0x01);
744 /* Driver latch off */
745 repaper_write_val(spi
, 0x03, 0x00);
746 usleep_range(5000, 10000);
748 /* Start chargepump */
749 for (i
= 0; i
< 4; ++i
) {
750 /* Charge pump positive voltage on - VGH/VDL on */
751 repaper_write_val(spi
, 0x05, 0x01);
754 /* Charge pump negative voltage on - VGL/VDL on */
755 repaper_write_val(spi
, 0x05, 0x03);
758 /* Charge pump Vcom on - Vcom driver on */
759 repaper_write_val(spi
, 0x05, 0x0f);
763 ret
= repaper_read_val(spi
, 0x0f);
765 DRM_DEV_ERROR(dev
, "failed to read chip (%d)\n", ret
);
777 DRM_DEV_ERROR(dev
, "dc/dc failed\n");
783 * Output enable to disable
784 * The userspace driver sets this to 0x04, but the datasheet says 0x06
786 repaper_write_val(spi
, 0x02, 0x04);
789 epd
->partial
= false;
794 static void repaper_pipe_disable(struct drm_simple_display_pipe
*pipe
)
796 struct repaper_epd
*epd
= drm_to_epd(pipe
->crtc
.dev
);
797 struct spi_device
*spi
= epd
->spi
;
801 * This callback is not protected by drm_dev_enter/exit since we want to
802 * turn off the display on regular driver unload. It's highly unlikely
803 * that the underlying SPI controller is gone should this be called after
810 DRM_DEBUG_DRIVER("\n");
812 epd
->enabled
= false;
815 for (line
= 0; line
< epd
->height
; line
++)
816 repaper_one_line(epd
, 0x7fffu
, NULL
, 0x00, NULL
,
822 repaper_one_line(epd
, 0x7fffu
, NULL
, 0x00, NULL
,
825 gpiod_set_value_cansleep(epd
->border
, 0);
827 gpiod_set_value_cansleep(epd
->border
, 1);
829 /* Border dummy line */
830 repaper_one_line(epd
, 0x7fffu
, NULL
, 0x00, NULL
,
835 /* not described in datasheet */
836 repaper_write_val(spi
, 0x0b, 0x00);
837 /* Latch reset turn on */
838 repaper_write_val(spi
, 0x03, 0x01);
839 /* Power off charge pump Vcom */
840 repaper_write_val(spi
, 0x05, 0x03);
841 /* Power off charge pump neg voltage */
842 repaper_write_val(spi
, 0x05, 0x01);
844 /* Discharge internal */
845 repaper_write_val(spi
, 0x04, 0x80);
846 /* turn off all charge pumps */
847 repaper_write_val(spi
, 0x05, 0x00);
849 repaper_write_val(spi
, 0x07, 0x01);
855 static void repaper_pipe_update(struct drm_simple_display_pipe
*pipe
,
856 struct drm_plane_state
*old_state
)
858 struct drm_plane_state
*state
= pipe
->plane
.state
;
859 struct drm_crtc
*crtc
= &pipe
->crtc
;
860 struct drm_rect rect
;
862 if (drm_atomic_helper_damage_merged(old_state
, state
, &rect
))
863 repaper_fb_dirty(state
->fb
);
865 if (crtc
->state
->event
) {
866 spin_lock_irq(&crtc
->dev
->event_lock
);
867 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
868 spin_unlock_irq(&crtc
->dev
->event_lock
);
869 crtc
->state
->event
= NULL
;
873 static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs
= {
874 .enable
= repaper_pipe_enable
,
875 .disable
= repaper_pipe_disable
,
876 .update
= repaper_pipe_update
,
877 .prepare_fb
= drm_gem_fb_simple_display_pipe_prepare_fb
,
880 static int repaper_connector_get_modes(struct drm_connector
*connector
)
882 struct repaper_epd
*epd
= drm_to_epd(connector
->dev
);
883 struct drm_display_mode
*mode
;
885 mode
= drm_mode_duplicate(connector
->dev
, epd
->mode
);
887 DRM_ERROR("Failed to duplicate mode\n");
891 drm_mode_set_name(mode
);
892 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
893 drm_mode_probed_add(connector
, mode
);
895 connector
->display_info
.width_mm
= mode
->width_mm
;
896 connector
->display_info
.height_mm
= mode
->height_mm
;
901 static const struct drm_connector_helper_funcs repaper_connector_hfuncs
= {
902 .get_modes
= repaper_connector_get_modes
,
905 static const struct drm_connector_funcs repaper_connector_funcs
= {
906 .reset
= drm_atomic_helper_connector_reset
,
907 .fill_modes
= drm_helper_probe_single_connector_modes
,
908 .destroy
= drm_connector_cleanup
,
909 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
910 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
913 static const struct drm_mode_config_funcs repaper_mode_config_funcs
= {
914 .fb_create
= drm_gem_fb_create_with_dirty
,
915 .atomic_check
= drm_atomic_helper_check
,
916 .atomic_commit
= drm_atomic_helper_commit
,
919 static void repaper_release(struct drm_device
*drm
)
921 struct repaper_epd
*epd
= drm_to_epd(drm
);
923 DRM_DEBUG_DRIVER("\n");
925 drm_mode_config_cleanup(drm
);
930 static const uint32_t repaper_formats
[] = {
934 static const struct drm_display_mode repaper_e1144cs021_mode
= {
935 DRM_SIMPLE_MODE(128, 96, 29, 22),
938 static const u8 repaper_e1144cs021_cs
[] = { 0x00, 0x00, 0x00, 0x00,
939 0x00, 0x0f, 0xff, 0x00 };
941 static const struct drm_display_mode repaper_e1190cs021_mode
= {
942 DRM_SIMPLE_MODE(144, 128, 36, 32),
945 static const u8 repaper_e1190cs021_cs
[] = { 0x00, 0x00, 0x00, 0x03,
946 0xfc, 0x00, 0x00, 0xff };
948 static const struct drm_display_mode repaper_e2200cs021_mode
= {
949 DRM_SIMPLE_MODE(200, 96, 46, 22),
952 static const u8 repaper_e2200cs021_cs
[] = { 0x00, 0x00, 0x00, 0x00,
953 0x01, 0xff, 0xe0, 0x00 };
955 static const struct drm_display_mode repaper_e2271cs021_mode
= {
956 DRM_SIMPLE_MODE(264, 176, 57, 38),
959 static const u8 repaper_e2271cs021_cs
[] = { 0x00, 0x00, 0x00, 0x7f,
960 0xff, 0xfe, 0x00, 0x00 };
962 DEFINE_DRM_GEM_CMA_FOPS(repaper_fops
);
964 static struct drm_driver repaper_driver
= {
965 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_ATOMIC
,
966 .fops
= &repaper_fops
,
967 .release
= repaper_release
,
968 DRM_GEM_CMA_VMAP_DRIVER_OPS
,
970 .desc
= "Pervasive Displays RePaper e-ink panels",
976 static const struct of_device_id repaper_of_match
[] = {
977 { .compatible
= "pervasive,e1144cs021", .data
= (void *)E1144CS021
},
978 { .compatible
= "pervasive,e1190cs021", .data
= (void *)E1190CS021
},
979 { .compatible
= "pervasive,e2200cs021", .data
= (void *)E2200CS021
},
980 { .compatible
= "pervasive,e2271cs021", .data
= (void *)E2271CS021
},
983 MODULE_DEVICE_TABLE(of
, repaper_of_match
);
985 static const struct spi_device_id repaper_id
[] = {
986 { "e1144cs021", E1144CS021
},
987 { "e1190cs021", E1190CS021
},
988 { "e2200cs021", E2200CS021
},
989 { "e2271cs021", E2271CS021
},
992 MODULE_DEVICE_TABLE(spi
, repaper_id
);
994 static int repaper_probe(struct spi_device
*spi
)
996 const struct drm_display_mode
*mode
;
997 const struct spi_device_id
*spi_id
;
998 const struct of_device_id
*match
;
999 struct device
*dev
= &spi
->dev
;
1000 enum repaper_model model
;
1001 const char *thermal_zone
;
1002 struct repaper_epd
*epd
;
1003 size_t line_buffer_size
;
1004 struct drm_device
*drm
;
1007 match
= of_match_device(repaper_of_match
, dev
);
1009 model
= (enum repaper_model
)match
->data
;
1011 spi_id
= spi_get_device_id(spi
);
1012 model
= spi_id
->driver_data
;
1015 /* The SPI device is used to allocate dma memory */
1016 if (!dev
->coherent_dma_mask
) {
1017 ret
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(32));
1019 dev_warn(dev
, "Failed to set dma mask %d\n", ret
);
1024 epd
= kzalloc(sizeof(*epd
), GFP_KERNEL
);
1030 ret
= devm_drm_dev_init(dev
, drm
, &repaper_driver
);
1036 drm_mode_config_init(drm
);
1037 drm
->mode_config
.funcs
= &repaper_mode_config_funcs
;
1041 epd
->panel_on
= devm_gpiod_get(dev
, "panel-on", GPIOD_OUT_LOW
);
1042 if (IS_ERR(epd
->panel_on
)) {
1043 ret
= PTR_ERR(epd
->panel_on
);
1044 if (ret
!= -EPROBE_DEFER
)
1045 DRM_DEV_ERROR(dev
, "Failed to get gpio 'panel-on'\n");
1049 epd
->discharge
= devm_gpiod_get(dev
, "discharge", GPIOD_OUT_LOW
);
1050 if (IS_ERR(epd
->discharge
)) {
1051 ret
= PTR_ERR(epd
->discharge
);
1052 if (ret
!= -EPROBE_DEFER
)
1053 DRM_DEV_ERROR(dev
, "Failed to get gpio 'discharge'\n");
1057 epd
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
1058 if (IS_ERR(epd
->reset
)) {
1059 ret
= PTR_ERR(epd
->reset
);
1060 if (ret
!= -EPROBE_DEFER
)
1061 DRM_DEV_ERROR(dev
, "Failed to get gpio 'reset'\n");
1065 epd
->busy
= devm_gpiod_get(dev
, "busy", GPIOD_IN
);
1066 if (IS_ERR(epd
->busy
)) {
1067 ret
= PTR_ERR(epd
->busy
);
1068 if (ret
!= -EPROBE_DEFER
)
1069 DRM_DEV_ERROR(dev
, "Failed to get gpio 'busy'\n");
1073 if (!device_property_read_string(dev
, "pervasive,thermal-zone",
1075 epd
->thermal
= thermal_zone_get_zone_by_name(thermal_zone
);
1076 if (IS_ERR(epd
->thermal
)) {
1077 DRM_DEV_ERROR(dev
, "Failed to get thermal zone: %s\n", thermal_zone
);
1078 return PTR_ERR(epd
->thermal
);
1084 mode
= &repaper_e1144cs021_mode
;
1085 epd
->channel_select
= repaper_e1144cs021_cs
;
1086 epd
->stage_time
= 480;
1087 epd
->bytes_per_scan
= 96 / 4;
1088 epd
->middle_scan
= true; /* data-scan-data */
1089 epd
->pre_border_byte
= false;
1090 epd
->border_byte
= REPAPER_BORDER_BYTE_ZERO
;
1094 mode
= &repaper_e1190cs021_mode
;
1095 epd
->channel_select
= repaper_e1190cs021_cs
;
1096 epd
->stage_time
= 480;
1097 epd
->bytes_per_scan
= 128 / 4 / 2;
1098 epd
->middle_scan
= false; /* scan-data-scan */
1099 epd
->pre_border_byte
= false;
1100 epd
->border_byte
= REPAPER_BORDER_BYTE_SET
;
1104 mode
= &repaper_e2200cs021_mode
;
1105 epd
->channel_select
= repaper_e2200cs021_cs
;
1106 epd
->stage_time
= 480;
1107 epd
->bytes_per_scan
= 96 / 4;
1108 epd
->middle_scan
= true; /* data-scan-data */
1109 epd
->pre_border_byte
= true;
1110 epd
->border_byte
= REPAPER_BORDER_BYTE_NONE
;
1114 epd
->border
= devm_gpiod_get(dev
, "border", GPIOD_OUT_LOW
);
1115 if (IS_ERR(epd
->border
)) {
1116 ret
= PTR_ERR(epd
->border
);
1117 if (ret
!= -EPROBE_DEFER
)
1118 DRM_DEV_ERROR(dev
, "Failed to get gpio 'border'\n");
1122 mode
= &repaper_e2271cs021_mode
;
1123 epd
->channel_select
= repaper_e2271cs021_cs
;
1124 epd
->stage_time
= 630;
1125 epd
->bytes_per_scan
= 176 / 4;
1126 epd
->middle_scan
= true; /* data-scan-data */
1127 epd
->pre_border_byte
= true;
1128 epd
->border_byte
= REPAPER_BORDER_BYTE_NONE
;
1136 epd
->width
= mode
->hdisplay
;
1137 epd
->height
= mode
->vdisplay
;
1138 epd
->factored_stage_time
= epd
->stage_time
;
1140 line_buffer_size
= 2 * epd
->width
/ 8 + epd
->bytes_per_scan
+ 2;
1141 epd
->line_buffer
= devm_kzalloc(dev
, line_buffer_size
, GFP_KERNEL
);
1142 if (!epd
->line_buffer
)
1145 epd
->current_frame
= devm_kzalloc(dev
, epd
->width
* epd
->height
/ 8,
1147 if (!epd
->current_frame
)
1150 drm
->mode_config
.min_width
= mode
->hdisplay
;
1151 drm
->mode_config
.max_width
= mode
->hdisplay
;
1152 drm
->mode_config
.min_height
= mode
->vdisplay
;
1153 drm
->mode_config
.max_height
= mode
->vdisplay
;
1155 drm_connector_helper_add(&epd
->connector
, &repaper_connector_hfuncs
);
1156 ret
= drm_connector_init(drm
, &epd
->connector
, &repaper_connector_funcs
,
1157 DRM_MODE_CONNECTOR_SPI
);
1161 ret
= drm_simple_display_pipe_init(drm
, &epd
->pipe
, &repaper_pipe_funcs
,
1162 repaper_formats
, ARRAY_SIZE(repaper_formats
),
1163 NULL
, &epd
->connector
);
1167 drm_mode_config_reset(drm
);
1169 ret
= drm_dev_register(drm
, 0);
1173 spi_set_drvdata(spi
, drm
);
1175 DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi
->max_speed_hz
/ 1000000);
1177 drm_fbdev_generic_setup(drm
, 0);
1182 static int repaper_remove(struct spi_device
*spi
)
1184 struct drm_device
*drm
= spi_get_drvdata(spi
);
1186 drm_dev_unplug(drm
);
1187 drm_atomic_helper_shutdown(drm
);
1192 static void repaper_shutdown(struct spi_device
*spi
)
1194 drm_atomic_helper_shutdown(spi_get_drvdata(spi
));
1197 static struct spi_driver repaper_spi_driver
= {
1200 .owner
= THIS_MODULE
,
1201 .of_match_table
= repaper_of_match
,
1203 .id_table
= repaper_id
,
1204 .probe
= repaper_probe
,
1205 .remove
= repaper_remove
,
1206 .shutdown
= repaper_shutdown
,
1208 module_spi_driver(repaper_spi_driver
);
1210 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1211 MODULE_AUTHOR("Noralf Trønnes");
1212 MODULE_LICENSE("GPL");