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_damage_helper.h>
27 #include <drm/drm_drv.h>
28 #include <drm/drm_fb_cma_helper.h>
29 #include <drm/drm_fb_helper.h>
30 #include <drm/drm_format_helper.h>
31 #include <drm/drm_gem_cma_helper.h>
32 #include <drm/drm_gem_framebuffer_helper.h>
33 #include <drm/drm_rect.h>
34 #include <drm/drm_vblank.h>
35 #include <drm/drm_simple_kms_helper.h>
36 #include <drm/tinydrm/tinydrm-helpers.h>
38 #define REPAPER_RID_G2_COG_ID 0x12
47 enum repaper_stage
{ /* Image pixel -> Display pixel */
48 REPAPER_COMPENSATE
, /* B -> W, W -> B (Current Image) */
49 REPAPER_WHITE
, /* B -> N, W -> W (Current Image) */
50 REPAPER_INVERSE
, /* B -> N, W -> B (New Image) */
51 REPAPER_NORMAL
/* B -> B, W -> W (New Image) */
54 enum repaper_epd_border_byte
{
55 REPAPER_BORDER_BYTE_NONE
,
56 REPAPER_BORDER_BYTE_ZERO
,
57 REPAPER_BORDER_BYTE_SET
,
61 struct drm_device drm
;
62 struct drm_simple_display_pipe pipe
;
63 struct spi_device
*spi
;
65 struct gpio_desc
*panel_on
;
66 struct gpio_desc
*border
;
67 struct gpio_desc
*discharge
;
68 struct gpio_desc
*reset
;
69 struct gpio_desc
*busy
;
71 struct thermal_zone_device
*thermal
;
75 unsigned int bytes_per_scan
;
76 const u8
*channel_select
;
77 unsigned int stage_time
;
78 unsigned int factored_stage_time
;
81 enum repaper_epd_border_byte border_byte
;
91 static inline struct repaper_epd
*drm_to_epd(struct drm_device
*drm
)
93 return container_of(drm
, struct repaper_epd
, drm
);
96 static int repaper_spi_transfer(struct spi_device
*spi
, u8 header
,
97 const void *tx
, void *rx
, size_t len
)
99 void *txbuf
= NULL
, *rxbuf
= NULL
;
100 struct spi_transfer tr
[2] = {};
104 headerbuf
= kmalloc(1, GFP_KERNEL
);
108 headerbuf
[0] = header
;
109 tr
[0].tx_buf
= headerbuf
;
112 /* Stack allocated tx? */
113 if (tx
&& len
<= 32) {
114 txbuf
= kmemdup(tx
, len
, GFP_KERNEL
);
122 rxbuf
= kmalloc(len
, GFP_KERNEL
);
129 tr
[1].tx_buf
= txbuf
? txbuf
: tx
;
130 tr
[1].rx_buf
= rxbuf
;
134 ret
= spi_sync_transfer(spi
, tr
, 2);
136 memcpy(rx
, rxbuf
, len
);
146 static int repaper_write_buf(struct spi_device
*spi
, u8 reg
,
147 const u8
*buf
, size_t len
)
151 ret
= repaper_spi_transfer(spi
, 0x70, ®
, NULL
, 1);
155 return repaper_spi_transfer(spi
, 0x72, buf
, NULL
, len
);
158 static int repaper_write_val(struct spi_device
*spi
, u8 reg
, u8 val
)
160 return repaper_write_buf(spi
, reg
, &val
, 1);
163 static int repaper_read_val(struct spi_device
*spi
, u8 reg
)
168 ret
= repaper_spi_transfer(spi
, 0x70, ®
, NULL
, 1);
172 ret
= repaper_spi_transfer(spi
, 0x73, NULL
, &val
, 1);
174 return ret
? ret
: val
;
177 static int repaper_read_id(struct spi_device
*spi
)
182 ret
= repaper_spi_transfer(spi
, 0x71, NULL
, &id
, 1);
184 return ret
? ret
: id
;
187 static void repaper_spi_mosi_low(struct spi_device
*spi
)
189 const u8 buf
[1] = { 0 };
191 spi_write(spi
, buf
, 1);
194 /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */
195 static void repaper_even_pixels(struct repaper_epd
*epd
, u8
**pp
,
196 const u8
*data
, u8 fixed_value
, const u8
*mask
,
197 enum repaper_stage stage
)
201 for (b
= 0; b
< (epd
->width
/ 8); b
++) {
203 u8 pixels
= data
[b
] & 0xaa;
204 u8 pixel_mask
= 0xff;
208 pixel_mask
= (mask
[b
] ^ pixels
) & 0xaa;
209 pixel_mask
|= pixel_mask
>> 1;
213 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
214 pixels
= 0xaa | ((pixels
^ 0xaa) >> 1);
216 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
217 pixels
= 0x55 + ((pixels
^ 0xaa) >> 1);
219 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
220 pixels
= 0x55 | (pixels
^ 0xaa);
222 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
223 pixels
= 0xaa | (pixels
>> 1);
227 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x55);
228 p1
= (pixels
>> 6) & 0x03;
229 p2
= (pixels
>> 4) & 0x03;
230 p3
= (pixels
>> 2) & 0x03;
231 p4
= (pixels
>> 0) & 0x03;
232 pixels
= (p1
<< 0) | (p2
<< 2) | (p3
<< 4) | (p4
<< 6);
235 *(*pp
)++ = fixed_value
;
240 /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */
241 static void repaper_odd_pixels(struct repaper_epd
*epd
, u8
**pp
,
242 const u8
*data
, u8 fixed_value
, const u8
*mask
,
243 enum repaper_stage stage
)
247 for (b
= epd
->width
/ 8; b
> 0; b
--) {
249 u8 pixels
= data
[b
- 1] & 0x55;
250 u8 pixel_mask
= 0xff;
253 pixel_mask
= (mask
[b
- 1] ^ pixels
) & 0x55;
254 pixel_mask
|= pixel_mask
<< 1;
258 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
259 pixels
= 0xaa | (pixels
^ 0x55);
261 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
262 pixels
= 0x55 + (pixels
^ 0x55);
264 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
265 pixels
= 0x55 | ((pixels
^ 0x55) << 1);
267 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
268 pixels
= 0xaa | pixels
;
272 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x55);
275 *(*pp
)++ = fixed_value
;
280 /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
281 static inline u16
repaper_interleave_bits(u16 value
)
283 value
= (value
| (value
<< 4)) & 0x0f0f;
284 value
= (value
| (value
<< 2)) & 0x3333;
285 value
= (value
| (value
<< 1)) & 0x5555;
290 /* pixels on display are numbered from 1 */
291 static void repaper_all_pixels(struct repaper_epd
*epd
, u8
**pp
,
292 const u8
*data
, u8 fixed_value
, const u8
*mask
,
293 enum repaper_stage stage
)
297 for (b
= epd
->width
/ 8; b
> 0; b
--) {
299 u16 pixels
= repaper_interleave_bits(data
[b
- 1]);
300 u16 pixel_mask
= 0xffff;
303 pixel_mask
= repaper_interleave_bits(mask
[b
- 1]);
305 pixel_mask
= (pixel_mask
^ pixels
) & 0x5555;
306 pixel_mask
|= pixel_mask
<< 1;
310 case REPAPER_COMPENSATE
: /* B -> W, W -> B (Current) */
311 pixels
= 0xaaaa | (pixels
^ 0x5555);
313 case REPAPER_WHITE
: /* B -> N, W -> W (Current) */
314 pixels
= 0x5555 + (pixels
^ 0x5555);
316 case REPAPER_INVERSE
: /* B -> N, W -> B (New) */
317 pixels
= 0x5555 | ((pixels
^ 0x5555) << 1);
319 case REPAPER_NORMAL
: /* B -> B, W -> W (New) */
320 pixels
= 0xaaaa | pixels
;
324 pixels
= (pixels
& pixel_mask
) | (~pixel_mask
& 0x5555);
325 *(*pp
)++ = pixels
>> 8;
328 *(*pp
)++ = fixed_value
;
329 *(*pp
)++ = fixed_value
;
334 /* output one line of scan and data bytes to the display */
335 static void repaper_one_line(struct repaper_epd
*epd
, unsigned int line
,
336 const u8
*data
, u8 fixed_value
, const u8
*mask
,
337 enum repaper_stage stage
)
339 u8
*p
= epd
->line_buffer
;
342 repaper_spi_mosi_low(epd
->spi
);
344 if (epd
->pre_border_byte
)
347 if (epd
->middle_scan
) {
349 repaper_odd_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
352 for (b
= epd
->bytes_per_scan
; b
> 0; b
--) {
353 if (line
/ 4 == b
- 1)
354 *p
++ = 0x03 << (2 * (line
& 0x03));
360 repaper_even_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
363 * even scan line, but as lines on display are numbered from 1,
366 for (b
= 0; b
< epd
->bytes_per_scan
; b
++) {
367 if (0 != (line
& 0x01) && line
/ 8 == b
)
368 *p
++ = 0xc0 >> (line
& 0x06);
374 repaper_all_pixels(epd
, &p
, data
, fixed_value
, mask
, stage
);
377 * odd scan line, but as lines on display are numbered from 1,
380 for (b
= epd
->bytes_per_scan
; b
> 0; b
--) {
381 if (0 == (line
& 0x01) && line
/ 8 == b
- 1)
382 *p
++ = 0x03 << (line
& 0x06);
388 switch (epd
->border_byte
) {
389 case REPAPER_BORDER_BYTE_NONE
:
392 case REPAPER_BORDER_BYTE_ZERO
:
396 case REPAPER_BORDER_BYTE_SET
:
398 case REPAPER_COMPENSATE
:
400 case REPAPER_INVERSE
:
410 repaper_write_buf(epd
->spi
, 0x0a, epd
->line_buffer
,
411 p
- epd
->line_buffer
);
413 /* Output data to panel */
414 repaper_write_val(epd
->spi
, 0x02, 0x07);
416 repaper_spi_mosi_low(epd
->spi
);
419 static void repaper_frame_fixed(struct repaper_epd
*epd
, u8 fixed_value
,
420 enum repaper_stage stage
)
424 for (line
= 0; line
< epd
->height
; line
++)
425 repaper_one_line(epd
, line
, NULL
, fixed_value
, NULL
, stage
);
428 static void repaper_frame_data(struct repaper_epd
*epd
, const u8
*image
,
429 const u8
*mask
, enum repaper_stage stage
)
434 for (line
= 0; line
< epd
->height
; line
++) {
435 repaper_one_line(epd
, line
,
436 &image
[line
* (epd
->width
/ 8)],
440 for (line
= 0; line
< epd
->height
; line
++) {
441 size_t n
= line
* epd
->width
/ 8;
443 repaper_one_line(epd
, line
, &image
[n
], 0, &mask
[n
],
449 static void repaper_frame_fixed_repeat(struct repaper_epd
*epd
, u8 fixed_value
,
450 enum repaper_stage stage
)
452 u64 start
= local_clock();
453 u64 end
= start
+ (epd
->factored_stage_time
* 1000 * 1000);
456 repaper_frame_fixed(epd
, fixed_value
, stage
);
457 } while (local_clock() < end
);
460 static void repaper_frame_data_repeat(struct repaper_epd
*epd
, const u8
*image
,
461 const u8
*mask
, enum repaper_stage stage
)
463 u64 start
= local_clock();
464 u64 end
= start
+ (epd
->factored_stage_time
* 1000 * 1000);
467 repaper_frame_data(epd
, image
, mask
, stage
);
468 } while (local_clock() < end
);
471 static void repaper_get_temperature(struct repaper_epd
*epd
)
473 int ret
, temperature
= 0;
474 unsigned int factor10x
;
479 ret
= thermal_zone_get_temp(epd
->thermal
, &temperature
);
481 DRM_DEV_ERROR(&epd
->spi
->dev
, "Failed to get temperature (%d)\n", ret
);
487 if (temperature
<= -10)
489 else if (temperature
<= -5)
491 else if (temperature
<= 5)
493 else if (temperature
<= 10)
495 else if (temperature
<= 15)
497 else if (temperature
<= 20)
499 else if (temperature
<= 40)
504 epd
->factored_stage_time
= epd
->stage_time
* factor10x
/ 10;
507 static void repaper_gray8_to_mono_reversed(u8
*buf
, u32 width
, u32 height
)
509 u8
*gray8
= buf
, *mono
= buf
;
512 for (y
= 0; y
< height
; y
++)
513 for (xb
= 0; xb
< width
/ 8; xb
++) {
516 for (i
= 0; i
< 8; i
++) {
520 if (gray8
[y
* width
+ x
] >> 7)
527 static int repaper_fb_dirty(struct drm_framebuffer
*fb
)
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 repaper_epd
*epd
= drm_to_epd(fb
->dev
);
532 struct drm_rect clip
;
539 if (!drm_dev_enter(fb
->dev
, &idx
))
542 /* repaper can't do partial updates */
546 clip
.y2
= fb
->height
;
548 repaper_get_temperature(epd
);
550 DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb
->base
.id
,
551 epd
->factored_stage_time
);
553 buf
= kmalloc_array(fb
->width
, fb
->height
, GFP_KERNEL
);
560 ret
= dma_buf_begin_cpu_access(import_attach
->dmabuf
,
566 drm_fb_xrgb8888_to_gray8(buf
, cma_obj
->vaddr
, fb
, &clip
);
569 ret
= dma_buf_end_cpu_access(import_attach
->dmabuf
,
575 repaper_gray8_to_mono_reversed(buf
, fb
->width
, fb
->height
);
578 repaper_frame_data_repeat(epd
, buf
, epd
->current_frame
,
580 } else if (epd
->cleared
) {
581 repaper_frame_data_repeat(epd
, epd
->current_frame
, NULL
,
583 repaper_frame_data_repeat(epd
, epd
->current_frame
, NULL
,
585 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_INVERSE
);
586 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_NORMAL
);
590 /* Clear display (anything -> white) */
591 repaper_frame_fixed_repeat(epd
, 0xff, REPAPER_COMPENSATE
);
592 repaper_frame_fixed_repeat(epd
, 0xff, REPAPER_WHITE
);
593 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_INVERSE
);
594 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_NORMAL
);
596 /* Assuming a clear (white) screen output an image */
597 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_COMPENSATE
);
598 repaper_frame_fixed_repeat(epd
, 0xaa, REPAPER_WHITE
);
599 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_INVERSE
);
600 repaper_frame_data_repeat(epd
, buf
, NULL
, REPAPER_NORMAL
);
606 memcpy(epd
->current_frame
, buf
, fb
->width
* fb
->height
/ 8);
609 * An extra frame write is needed if pixels are set in the bottom line,
610 * or else grey lines rises up from the pixels
612 if (epd
->pre_border_byte
) {
615 for (x
= 0; x
< (fb
->width
/ 8); x
++)
616 if (buf
[x
+ (fb
->width
* (fb
->height
- 1) / 8)]) {
617 repaper_frame_data_repeat(epd
, buf
,
632 static void power_off(struct repaper_epd
*epd
)
634 /* Turn off power and all signals */
635 gpiod_set_value_cansleep(epd
->reset
, 0);
636 gpiod_set_value_cansleep(epd
->panel_on
, 0);
638 gpiod_set_value_cansleep(epd
->border
, 0);
640 /* Ensure SPI MOSI and CLOCK are Low before CS Low */
641 repaper_spi_mosi_low(epd
->spi
);
643 /* Discharge pulse */
644 gpiod_set_value_cansleep(epd
->discharge
, 1);
646 gpiod_set_value_cansleep(epd
->discharge
, 0);
649 static void repaper_pipe_enable(struct drm_simple_display_pipe
*pipe
,
650 struct drm_crtc_state
*crtc_state
,
651 struct drm_plane_state
*plane_state
)
653 struct repaper_epd
*epd
= drm_to_epd(pipe
->crtc
.dev
);
654 struct spi_device
*spi
= epd
->spi
;
655 struct device
*dev
= &spi
->dev
;
659 if (!drm_dev_enter(pipe
->crtc
.dev
, &idx
))
662 DRM_DEBUG_DRIVER("\n");
664 /* Power up sequence */
665 gpiod_set_value_cansleep(epd
->reset
, 0);
666 gpiod_set_value_cansleep(epd
->panel_on
, 0);
667 gpiod_set_value_cansleep(epd
->discharge
, 0);
669 gpiod_set_value_cansleep(epd
->border
, 0);
670 repaper_spi_mosi_low(spi
);
671 usleep_range(5000, 10000);
673 gpiod_set_value_cansleep(epd
->panel_on
, 1);
675 * This delay comes from the repaper.org userspace driver, it's not
676 * mentioned in the datasheet.
678 usleep_range(10000, 15000);
679 gpiod_set_value_cansleep(epd
->reset
, 1);
681 gpiod_set_value_cansleep(epd
->border
, 1);
682 usleep_range(5000, 10000);
683 gpiod_set_value_cansleep(epd
->reset
, 0);
684 usleep_range(5000, 10000);
685 gpiod_set_value_cansleep(epd
->reset
, 1);
686 usleep_range(5000, 10000);
688 /* Wait for COG to become ready */
689 for (i
= 100; i
> 0; i
--) {
690 if (!gpiod_get_value_cansleep(epd
->busy
))
693 usleep_range(10, 100);
697 DRM_DEV_ERROR(dev
, "timeout waiting for panel to become ready.\n");
702 repaper_read_id(spi
);
703 ret
= repaper_read_id(spi
);
704 if (ret
!= REPAPER_RID_G2_COG_ID
) {
706 dev_err(dev
, "failed to read chip (%d)\n", ret
);
708 dev_err(dev
, "wrong COG ID 0x%02x\n", ret
);
714 repaper_write_val(spi
, 0x02, 0x40);
716 ret
= repaper_read_val(spi
, 0x0f);
717 if (ret
< 0 || !(ret
& 0x80)) {
719 DRM_DEV_ERROR(dev
, "failed to read chip (%d)\n", ret
);
721 DRM_DEV_ERROR(dev
, "panel is reported broken\n");
726 /* Power saving mode */
727 repaper_write_val(spi
, 0x0b, 0x02);
729 repaper_write_buf(spi
, 0x01, epd
->channel_select
, 8);
730 /* High power mode osc */
731 repaper_write_val(spi
, 0x07, 0xd1);
733 repaper_write_val(spi
, 0x08, 0x02);
735 repaper_write_val(spi
, 0x09, 0xc2);
737 repaper_write_val(spi
, 0x04, 0x03);
738 /* Driver latch on */
739 repaper_write_val(spi
, 0x03, 0x01);
740 /* Driver latch off */
741 repaper_write_val(spi
, 0x03, 0x00);
742 usleep_range(5000, 10000);
744 /* Start chargepump */
745 for (i
= 0; i
< 4; ++i
) {
746 /* Charge pump positive voltage on - VGH/VDL on */
747 repaper_write_val(spi
, 0x05, 0x01);
750 /* Charge pump negative voltage on - VGL/VDL on */
751 repaper_write_val(spi
, 0x05, 0x03);
754 /* Charge pump Vcom on - Vcom driver on */
755 repaper_write_val(spi
, 0x05, 0x0f);
759 ret
= repaper_read_val(spi
, 0x0f);
761 DRM_DEV_ERROR(dev
, "failed to read chip (%d)\n", ret
);
773 DRM_DEV_ERROR(dev
, "dc/dc failed\n");
779 * Output enable to disable
780 * The userspace driver sets this to 0x04, but the datasheet says 0x06
782 repaper_write_val(spi
, 0x02, 0x04);
785 epd
->partial
= false;
790 static void repaper_pipe_disable(struct drm_simple_display_pipe
*pipe
)
792 struct repaper_epd
*epd
= drm_to_epd(pipe
->crtc
.dev
);
793 struct spi_device
*spi
= epd
->spi
;
797 * This callback is not protected by drm_dev_enter/exit since we want to
798 * turn off the display on regular driver unload. It's highly unlikely
799 * that the underlying SPI controller is gone should this be called after
806 DRM_DEBUG_DRIVER("\n");
808 epd
->enabled
= false;
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 void repaper_pipe_update(struct drm_simple_display_pipe
*pipe
,
852 struct drm_plane_state
*old_state
)
854 struct drm_plane_state
*state
= pipe
->plane
.state
;
855 struct drm_crtc
*crtc
= &pipe
->crtc
;
856 struct drm_rect rect
;
858 if (drm_atomic_helper_damage_merged(old_state
, state
, &rect
))
859 repaper_fb_dirty(state
->fb
);
861 if (crtc
->state
->event
) {
862 spin_lock_irq(&crtc
->dev
->event_lock
);
863 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
864 spin_unlock_irq(&crtc
->dev
->event_lock
);
865 crtc
->state
->event
= NULL
;
869 static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs
= {
870 .enable
= repaper_pipe_enable
,
871 .disable
= repaper_pipe_disable
,
872 .update
= repaper_pipe_update
,
873 .prepare_fb
= drm_gem_fb_simple_display_pipe_prepare_fb
,
876 static const struct drm_mode_config_funcs repaper_mode_config_funcs
= {
877 .fb_create
= drm_gem_fb_create_with_dirty
,
878 .atomic_check
= drm_atomic_helper_check
,
879 .atomic_commit
= drm_atomic_helper_commit
,
882 static void repaper_release(struct drm_device
*drm
)
884 struct repaper_epd
*epd
= drm_to_epd(drm
);
886 DRM_DEBUG_DRIVER("\n");
888 drm_mode_config_cleanup(drm
);
893 static const uint32_t repaper_formats
[] = {
897 static const struct drm_display_mode repaper_e1144cs021_mode
= {
898 DRM_SIMPLE_MODE(128, 96, 29, 22),
901 static const u8 repaper_e1144cs021_cs
[] = { 0x00, 0x00, 0x00, 0x00,
902 0x00, 0x0f, 0xff, 0x00 };
904 static const struct drm_display_mode repaper_e1190cs021_mode
= {
905 DRM_SIMPLE_MODE(144, 128, 36, 32),
908 static const u8 repaper_e1190cs021_cs
[] = { 0x00, 0x00, 0x00, 0x03,
909 0xfc, 0x00, 0x00, 0xff };
911 static const struct drm_display_mode repaper_e2200cs021_mode
= {
912 DRM_SIMPLE_MODE(200, 96, 46, 22),
915 static const u8 repaper_e2200cs021_cs
[] = { 0x00, 0x00, 0x00, 0x00,
916 0x01, 0xff, 0xe0, 0x00 };
918 static const struct drm_display_mode repaper_e2271cs021_mode
= {
919 DRM_SIMPLE_MODE(264, 176, 57, 38),
922 static const u8 repaper_e2271cs021_cs
[] = { 0x00, 0x00, 0x00, 0x7f,
923 0xff, 0xfe, 0x00, 0x00 };
925 DEFINE_DRM_GEM_CMA_FOPS(repaper_fops
);
927 static struct drm_driver repaper_driver
= {
928 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
|
930 .fops
= &repaper_fops
,
931 .release
= repaper_release
,
932 DRM_GEM_CMA_VMAP_DRIVER_OPS
,
934 .desc
= "Pervasive Displays RePaper e-ink panels",
940 static const struct of_device_id repaper_of_match
[] = {
941 { .compatible
= "pervasive,e1144cs021", .data
= (void *)E1144CS021
},
942 { .compatible
= "pervasive,e1190cs021", .data
= (void *)E1190CS021
},
943 { .compatible
= "pervasive,e2200cs021", .data
= (void *)E2200CS021
},
944 { .compatible
= "pervasive,e2271cs021", .data
= (void *)E2271CS021
},
947 MODULE_DEVICE_TABLE(of
, repaper_of_match
);
949 static const struct spi_device_id repaper_id
[] = {
950 { "e1144cs021", E1144CS021
},
951 { "e1190cs021", E1190CS021
},
952 { "e2200cs021", E2200CS021
},
953 { "e2271cs021", E2271CS021
},
956 MODULE_DEVICE_TABLE(spi
, repaper_id
);
958 static int repaper_probe(struct spi_device
*spi
)
960 const struct drm_display_mode
*mode
;
961 const struct spi_device_id
*spi_id
;
962 const struct of_device_id
*match
;
963 struct device
*dev
= &spi
->dev
;
964 enum repaper_model model
;
965 const char *thermal_zone
;
966 struct repaper_epd
*epd
;
967 size_t line_buffer_size
;
968 struct drm_device
*drm
;
971 match
= of_match_device(repaper_of_match
, dev
);
973 model
= (enum repaper_model
)match
->data
;
975 spi_id
= spi_get_device_id(spi
);
976 model
= spi_id
->driver_data
;
979 /* The SPI device is used to allocate dma memory */
980 if (!dev
->coherent_dma_mask
) {
981 ret
= dma_coerce_mask_and_coherent(dev
, DMA_BIT_MASK(32));
983 dev_warn(dev
, "Failed to set dma mask %d\n", ret
);
988 epd
= kzalloc(sizeof(*epd
), GFP_KERNEL
);
994 ret
= devm_drm_dev_init(dev
, drm
, &repaper_driver
);
1000 drm_mode_config_init(drm
);
1001 drm
->mode_config
.funcs
= &repaper_mode_config_funcs
;
1005 epd
->panel_on
= devm_gpiod_get(dev
, "panel-on", GPIOD_OUT_LOW
);
1006 if (IS_ERR(epd
->panel_on
)) {
1007 ret
= PTR_ERR(epd
->panel_on
);
1008 if (ret
!= -EPROBE_DEFER
)
1009 DRM_DEV_ERROR(dev
, "Failed to get gpio 'panel-on'\n");
1013 epd
->discharge
= devm_gpiod_get(dev
, "discharge", GPIOD_OUT_LOW
);
1014 if (IS_ERR(epd
->discharge
)) {
1015 ret
= PTR_ERR(epd
->discharge
);
1016 if (ret
!= -EPROBE_DEFER
)
1017 DRM_DEV_ERROR(dev
, "Failed to get gpio 'discharge'\n");
1021 epd
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
1022 if (IS_ERR(epd
->reset
)) {
1023 ret
= PTR_ERR(epd
->reset
);
1024 if (ret
!= -EPROBE_DEFER
)
1025 DRM_DEV_ERROR(dev
, "Failed to get gpio 'reset'\n");
1029 epd
->busy
= devm_gpiod_get(dev
, "busy", GPIOD_IN
);
1030 if (IS_ERR(epd
->busy
)) {
1031 ret
= PTR_ERR(epd
->busy
);
1032 if (ret
!= -EPROBE_DEFER
)
1033 DRM_DEV_ERROR(dev
, "Failed to get gpio 'busy'\n");
1037 if (!device_property_read_string(dev
, "pervasive,thermal-zone",
1039 epd
->thermal
= thermal_zone_get_zone_by_name(thermal_zone
);
1040 if (IS_ERR(epd
->thermal
)) {
1041 DRM_DEV_ERROR(dev
, "Failed to get thermal zone: %s\n", thermal_zone
);
1042 return PTR_ERR(epd
->thermal
);
1048 mode
= &repaper_e1144cs021_mode
;
1049 epd
->channel_select
= repaper_e1144cs021_cs
;
1050 epd
->stage_time
= 480;
1051 epd
->bytes_per_scan
= 96 / 4;
1052 epd
->middle_scan
= true; /* data-scan-data */
1053 epd
->pre_border_byte
= false;
1054 epd
->border_byte
= REPAPER_BORDER_BYTE_ZERO
;
1058 mode
= &repaper_e1190cs021_mode
;
1059 epd
->channel_select
= repaper_e1190cs021_cs
;
1060 epd
->stage_time
= 480;
1061 epd
->bytes_per_scan
= 128 / 4 / 2;
1062 epd
->middle_scan
= false; /* scan-data-scan */
1063 epd
->pre_border_byte
= false;
1064 epd
->border_byte
= REPAPER_BORDER_BYTE_SET
;
1068 mode
= &repaper_e2200cs021_mode
;
1069 epd
->channel_select
= repaper_e2200cs021_cs
;
1070 epd
->stage_time
= 480;
1071 epd
->bytes_per_scan
= 96 / 4;
1072 epd
->middle_scan
= true; /* data-scan-data */
1073 epd
->pre_border_byte
= true;
1074 epd
->border_byte
= REPAPER_BORDER_BYTE_NONE
;
1078 epd
->border
= devm_gpiod_get(dev
, "border", GPIOD_OUT_LOW
);
1079 if (IS_ERR(epd
->border
)) {
1080 ret
= PTR_ERR(epd
->border
);
1081 if (ret
!= -EPROBE_DEFER
)
1082 DRM_DEV_ERROR(dev
, "Failed to get gpio 'border'\n");
1086 mode
= &repaper_e2271cs021_mode
;
1087 epd
->channel_select
= repaper_e2271cs021_cs
;
1088 epd
->stage_time
= 630;
1089 epd
->bytes_per_scan
= 176 / 4;
1090 epd
->middle_scan
= true; /* data-scan-data */
1091 epd
->pre_border_byte
= true;
1092 epd
->border_byte
= REPAPER_BORDER_BYTE_NONE
;
1099 epd
->width
= mode
->hdisplay
;
1100 epd
->height
= mode
->vdisplay
;
1101 epd
->factored_stage_time
= epd
->stage_time
;
1103 line_buffer_size
= 2 * epd
->width
/ 8 + epd
->bytes_per_scan
+ 2;
1104 epd
->line_buffer
= devm_kzalloc(dev
, line_buffer_size
, GFP_KERNEL
);
1105 if (!epd
->line_buffer
)
1108 epd
->current_frame
= devm_kzalloc(dev
, epd
->width
* epd
->height
/ 8,
1110 if (!epd
->current_frame
)
1113 ret
= tinydrm_display_pipe_init(drm
, &epd
->pipe
, &repaper_pipe_funcs
,
1114 DRM_MODE_CONNECTOR_VIRTUAL
,
1116 ARRAY_SIZE(repaper_formats
), mode
, 0);
1120 drm_mode_config_reset(drm
);
1122 ret
= drm_dev_register(drm
, 0);
1126 spi_set_drvdata(spi
, drm
);
1128 DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi
->max_speed_hz
/ 1000000);
1130 drm_fbdev_generic_setup(drm
, 0);
1135 static int repaper_remove(struct spi_device
*spi
)
1137 struct drm_device
*drm
= spi_get_drvdata(spi
);
1139 drm_dev_unplug(drm
);
1140 drm_atomic_helper_shutdown(drm
);
1145 static void repaper_shutdown(struct spi_device
*spi
)
1147 drm_atomic_helper_shutdown(spi_get_drvdata(spi
));
1150 static struct spi_driver repaper_spi_driver
= {
1153 .owner
= THIS_MODULE
,
1154 .of_match_table
= repaper_of_match
,
1156 .id_table
= repaper_id
,
1157 .probe
= repaper_probe
,
1158 .remove
= repaper_remove
,
1159 .shutdown
= repaper_shutdown
,
1161 module_spi_driver(repaper_spi_driver
);
1163 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1164 MODULE_AUTHOR("Noralf Trønnes");
1165 MODULE_LICENSE("GPL");