1 /*
2 * DRM driver for Pervasive Displays RePaper branded e-ink panels
3 *
4 * Copyright 2013-2017 Pervasive Displays, Inc.
5 * Copyright 2017 Noralf Trønnes
6 *
7 * The driver supports:
8 * Material Film: Aurora Mb (V231)
9 * Driver IC: G2 (eTC)
10 *
11 * The controller code was taken from the userspace driver:
12 * https://github.com/repaper/gratis
13 *
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.
18 */
19
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>
28
29 #include <drm/drm_gem_framebuffer_helper.h>
30 #include <drm/tinydrm/tinydrm.h>
31 #include <drm/tinydrm/tinydrm-helpers.h>
32
33 #define REPAPER_RID_G2_COG_ID 0x12
34
35 enum repaper_model {
36 E1144CS021 = 1,
37 E1190CS021,
38 E2200CS021,
39 E2271CS021,
40 };
41
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) */
47 };
48
49 enum repaper_epd_border_byte {
50 REPAPER_BORDER_BYTE_NONE,
51 REPAPER_BORDER_BYTE_ZERO,
52 REPAPER_BORDER_BYTE_SET,
53 };
54
55 struct repaper_epd {
56 struct tinydrm_device tinydrm;
57 struct spi_device *spi;
58
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;
64
65 struct thermal_zone_device *thermal;
66
67 unsigned int height;
68 unsigned int width;
69 unsigned int bytes_per_scan;
70 const u8 *channel_select;
71 unsigned int stage_time;
72 unsigned int factored_stage_time;
73 bool middle_scan;
74 bool pre_border_byte;
75 enum repaper_epd_border_byte border_byte;
76
77 u8 *line_buffer;
78 void *current_frame;
79
80 bool enabled;
81 bool cleared;
82 bool partial;
83 };
84
85 static inline struct repaper_epd *
epd_from_tinydrm(struct tinydrm_device * tdev)86 epd_from_tinydrm(struct tinydrm_device *tdev)
87 {
88 return container_of(tdev, struct repaper_epd, tinydrm);
89 }
90
repaper_spi_transfer(struct spi_device * spi,u8 header,const void * tx,void * rx,size_t len)91 static int repaper_spi_transfer(struct spi_device *spi, u8 header,
92 const void *tx, void *rx, size_t len)
93 {
94 void *txbuf = NULL, *rxbuf = NULL;
95 struct spi_transfer tr[2] = {};
96 u8 *headerbuf;
97 int ret;
98
99 headerbuf = kmalloc(1, GFP_KERNEL);
100 if (!headerbuf)
101 return -ENOMEM;
102
103 headerbuf[0] = header;
104 tr[0].tx_buf = headerbuf;
105 tr[0].len = 1;
106
107 /* Stack allocated tx? */
108 if (tx && len <= 32) {
109 txbuf = kmalloc(len, GFP_KERNEL);
110 if (!txbuf) {
111 ret = -ENOMEM;
112 goto out_free;
113 }
114 memcpy(txbuf, tx, len);
115 }
116
117 if (rx) {
118 rxbuf = kmalloc(len, GFP_KERNEL);
119 if (!rxbuf) {
120 ret = -ENOMEM;
121 goto out_free;
122 }
123 }
124
125 tr[1].tx_buf = txbuf ? txbuf : tx;
126 tr[1].rx_buf = rxbuf;
127 tr[1].len = len;
128
129 ndelay(80);
130 ret = spi_sync_transfer(spi, tr, 2);
131 if (rx && !ret)
132 memcpy(rx, rxbuf, len);
133
134 out_free:
135 kfree(headerbuf);
136 kfree(txbuf);
137 kfree(rxbuf);
138
139 return ret;
140 }
141
repaper_write_buf(struct spi_device * spi,u8 reg,const u8 * buf,size_t len)142 static int repaper_write_buf(struct spi_device *spi, u8 reg,
143 const u8 *buf, size_t len)
144 {
145 int ret;
146
147 ret = repaper_spi_transfer(spi, 0x70, ®, NULL, 1);
148 if (ret)
149 return ret;
150
151 return repaper_spi_transfer(spi, 0x72, buf, NULL, len);
152 }
153
repaper_write_val(struct spi_device * spi,u8 reg,u8 val)154 static int repaper_write_val(struct spi_device *spi, u8 reg, u8 val)
155 {
156 return repaper_write_buf(spi, reg, &val, 1);
157 }
158
repaper_read_val(struct spi_device * spi,u8 reg)159 static int repaper_read_val(struct spi_device *spi, u8 reg)
160 {
161 int ret;
162 u8 val;
163
164 ret = repaper_spi_transfer(spi, 0x70, ®, NULL, 1);
165 if (ret)
166 return ret;
167
168 ret = repaper_spi_transfer(spi, 0x73, NULL, &val, 1);
169
170 return ret ? ret : val;
171 }
172
repaper_read_id(struct spi_device * spi)173 static int repaper_read_id(struct spi_device *spi)
174 {
175 int ret;
176 u8 id;
177
178 ret = repaper_spi_transfer(spi, 0x71, NULL, &id, 1);
179
180 return ret ? ret : id;
181 }
182
repaper_spi_mosi_low(struct spi_device * spi)183 static void repaper_spi_mosi_low(struct spi_device *spi)
184 {
185 const u8 buf[1] = { 0 };
186
187 spi_write(spi, buf, 1);
188 }
189
190 /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */
repaper_even_pixels(struct repaper_epd * epd,u8 ** pp,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)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)
194 {
195 unsigned int b;
196
197 for (b = 0; b < (epd->width / 8); b++) {
198 if (data) {
199 u8 pixels = data[b] & 0xaa;
200 u8 pixel_mask = 0xff;
201 u8 p1, p2, p3, p4;
202
203 if (mask) {
204 pixel_mask = (mask[b] ^ pixels) & 0xaa;
205 pixel_mask |= pixel_mask >> 1;
206 }
207
208 switch (stage) {
209 case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
210 pixels = 0xaa | ((pixels ^ 0xaa) >> 1);
211 break;
212 case REPAPER_WHITE: /* B -> N, W -> W (Current) */
213 pixels = 0x55 + ((pixels ^ 0xaa) >> 1);
214 break;
215 case REPAPER_INVERSE: /* B -> N, W -> B (New) */
216 pixels = 0x55 | (pixels ^ 0xaa);
217 break;
218 case REPAPER_NORMAL: /* B -> B, W -> W (New) */
219 pixels = 0xaa | (pixels >> 1);
220 break;
221 }
222
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);
229 *(*pp)++ = pixels;
230 } else {
231 *(*pp)++ = fixed_value;
232 }
233 }
234 }
235
236 /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */
repaper_odd_pixels(struct repaper_epd * epd,u8 ** pp,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)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)
240 {
241 unsigned int b;
242
243 for (b = epd->width / 8; b > 0; b--) {
244 if (data) {
245 u8 pixels = data[b - 1] & 0x55;
246 u8 pixel_mask = 0xff;
247
248 if (mask) {
249 pixel_mask = (mask[b - 1] ^ pixels) & 0x55;
250 pixel_mask |= pixel_mask << 1;
251 }
252
253 switch (stage) {
254 case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
255 pixels = 0xaa | (pixels ^ 0x55);
256 break;
257 case REPAPER_WHITE: /* B -> N, W -> W (Current) */
258 pixels = 0x55 + (pixels ^ 0x55);
259 break;
260 case REPAPER_INVERSE: /* B -> N, W -> B (New) */
261 pixels = 0x55 | ((pixels ^ 0x55) << 1);
262 break;
263 case REPAPER_NORMAL: /* B -> B, W -> W (New) */
264 pixels = 0xaa | pixels;
265 break;
266 }
267
268 pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55);
269 *(*pp)++ = pixels;
270 } else {
271 *(*pp)++ = fixed_value;
272 }
273 }
274 }
275
276 /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
repaper_interleave_bits(u16 value)277 static inline u16 repaper_interleave_bits(u16 value)
278 {
279 value = (value | (value << 4)) & 0x0f0f;
280 value = (value | (value << 2)) & 0x3333;
281 value = (value | (value << 1)) & 0x5555;
282
283 return value;
284 }
285
286 /* pixels on display are numbered from 1 */
repaper_all_pixels(struct repaper_epd * epd,u8 ** pp,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)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)
290 {
291 unsigned int b;
292
293 for (b = epd->width / 8; b > 0; b--) {
294 if (data) {
295 u16 pixels = repaper_interleave_bits(data[b - 1]);
296 u16 pixel_mask = 0xffff;
297
298 if (mask) {
299 pixel_mask = repaper_interleave_bits(mask[b - 1]);
300
301 pixel_mask = (pixel_mask ^ pixels) & 0x5555;
302 pixel_mask |= pixel_mask << 1;
303 }
304
305 switch (stage) {
306 case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
307 pixels = 0xaaaa | (pixels ^ 0x5555);
308 break;
309 case REPAPER_WHITE: /* B -> N, W -> W (Current) */
310 pixels = 0x5555 + (pixels ^ 0x5555);
311 break;
312 case REPAPER_INVERSE: /* B -> N, W -> B (New) */
313 pixels = 0x5555 | ((pixels ^ 0x5555) << 1);
314 break;
315 case REPAPER_NORMAL: /* B -> B, W -> W (New) */
316 pixels = 0xaaaa | pixels;
317 break;
318 }
319
320 pixels = (pixels & pixel_mask) | (~pixel_mask & 0x5555);
321 *(*pp)++ = pixels >> 8;
322 *(*pp)++ = pixels;
323 } else {
324 *(*pp)++ = fixed_value;
325 *(*pp)++ = fixed_value;
326 }
327 }
328 }
329
330 /* output one line of scan and data bytes to the display */
repaper_one_line(struct repaper_epd * epd,unsigned int line,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)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)
334 {
335 u8 *p = epd->line_buffer;
336 unsigned int b;
337
338 repaper_spi_mosi_low(epd->spi);
339
340 if (epd->pre_border_byte)
341 *p++ = 0x00;
342
343 if (epd->middle_scan) {
344 /* data bytes */
345 repaper_odd_pixels(epd, &p, data, fixed_value, mask, stage);
346
347 /* scan line */
348 for (b = epd->bytes_per_scan; b > 0; b--) {
349 if (line / 4 == b - 1)
350 *p++ = 0x03 << (2 * (line & 0x03));
351 else
352 *p++ = 0x00;
353 }
354
355 /* data bytes */
356 repaper_even_pixels(epd, &p, data, fixed_value, mask, stage);
357 } else {
358 /*
359 * even scan line, but as lines on display are numbered from 1,
360 * line: 1,3,5,...
361 */
362 for (b = 0; b < epd->bytes_per_scan; b++) {
363 if (0 != (line & 0x01) && line / 8 == b)
364 *p++ = 0xc0 >> (line & 0x06);
365 else
366 *p++ = 0x00;
367 }
368
369 /* data bytes */
370 repaper_all_pixels(epd, &p, data, fixed_value, mask, stage);
371
372 /*
373 * odd scan line, but as lines on display are numbered from 1,
374 * line: 0,2,4,6,...
375 */
376 for (b = epd->bytes_per_scan; b > 0; b--) {
377 if (0 == (line & 0x01) && line / 8 == b - 1)
378 *p++ = 0x03 << (line & 0x06);
379 else
380 *p++ = 0x00;
381 }
382 }
383
384 switch (epd->border_byte) {
385 case REPAPER_BORDER_BYTE_NONE:
386 break;
387
388 case REPAPER_BORDER_BYTE_ZERO:
389 *p++ = 0x00;
390 break;
391
392 case REPAPER_BORDER_BYTE_SET:
393 switch (stage) {
394 case REPAPER_COMPENSATE:
395 case REPAPER_WHITE:
396 case REPAPER_INVERSE:
397 *p++ = 0x00;
398 break;
399 case REPAPER_NORMAL:
400 *p++ = 0xaa;
401 break;
402 }
403 break;
404 }
405
406 repaper_write_buf(epd->spi, 0x0a, epd->line_buffer,
407 p - epd->line_buffer);
408
409 /* Output data to panel */
410 repaper_write_val(epd->spi, 0x02, 0x07);
411
412 repaper_spi_mosi_low(epd->spi);
413 }
414
repaper_frame_fixed(struct repaper_epd * epd,u8 fixed_value,enum repaper_stage stage)415 static void repaper_frame_fixed(struct repaper_epd *epd, u8 fixed_value,
416 enum repaper_stage stage)
417 {
418 unsigned int line;
419
420 for (line = 0; line < epd->height; line++)
421 repaper_one_line(epd, line, NULL, fixed_value, NULL, stage);
422 }
423
repaper_frame_data(struct repaper_epd * epd,const u8 * image,const u8 * mask,enum repaper_stage stage)424 static void repaper_frame_data(struct repaper_epd *epd, const u8 *image,
425 const u8 *mask, enum repaper_stage stage)
426 {
427 unsigned int line;
428
429 if (!mask) {
430 for (line = 0; line < epd->height; line++) {
431 repaper_one_line(epd, line,
432 &image[line * (epd->width / 8)],
433 0, NULL, stage);
434 }
435 } else {
436 for (line = 0; line < epd->height; line++) {
437 size_t n = line * epd->width / 8;
438
439 repaper_one_line(epd, line, &image[n], 0, &mask[n],
440 stage);
441 }
442 }
443 }
444
repaper_frame_fixed_repeat(struct repaper_epd * epd,u8 fixed_value,enum repaper_stage stage)445 static void repaper_frame_fixed_repeat(struct repaper_epd *epd, u8 fixed_value,
446 enum repaper_stage stage)
447 {
448 u64 start = local_clock();
449 u64 end = start + (epd->factored_stage_time * 1000 * 1000);
450
451 do {
452 repaper_frame_fixed(epd, fixed_value, stage);
453 } while (local_clock() < end);
454 }
455
repaper_frame_data_repeat(struct repaper_epd * epd,const u8 * image,const u8 * mask,enum repaper_stage stage)456 static void repaper_frame_data_repeat(struct repaper_epd *epd, const u8 *image,
457 const u8 *mask, enum repaper_stage stage)
458 {
459 u64 start = local_clock();
460 u64 end = start + (epd->factored_stage_time * 1000 * 1000);
461
462 do {
463 repaper_frame_data(epd, image, mask, stage);
464 } while (local_clock() < end);
465 }
466
repaper_get_temperature(struct repaper_epd * epd)467 static void repaper_get_temperature(struct repaper_epd *epd)
468 {
469 int ret, temperature = 0;
470 unsigned int factor10x;
471
472 if (!epd->thermal)
473 return;
474
475 ret = thermal_zone_get_temp(epd->thermal, &temperature);
476 if (ret) {
477 DRM_DEV_ERROR(&epd->spi->dev, "Failed to get temperature (%d)\n", ret);
478 return;
479 }
480
481 temperature /= 1000;
482
483 if (temperature <= -10)
484 factor10x = 170;
485 else if (temperature <= -5)
486 factor10x = 120;
487 else if (temperature <= 5)
488 factor10x = 80;
489 else if (temperature <= 10)
490 factor10x = 40;
491 else if (temperature <= 15)
492 factor10x = 30;
493 else if (temperature <= 20)
494 factor10x = 20;
495 else if (temperature <= 40)
496 factor10x = 10;
497 else
498 factor10x = 7;
499
500 epd->factored_stage_time = epd->stage_time * factor10x / 10;
501 }
502
repaper_gray8_to_mono_reversed(u8 * buf,u32 width,u32 height)503 static void repaper_gray8_to_mono_reversed(u8 *buf, u32 width, u32 height)
504 {
505 u8 *gray8 = buf, *mono = buf;
506 int y, xb, i;
507
508 for (y = 0; y < height; y++)
509 for (xb = 0; xb < width / 8; xb++) {
510 u8 byte = 0x00;
511
512 for (i = 0; i < 8; i++) {
513 int x = xb * 8 + i;
514
515 byte >>= 1;
516 if (gray8[y * width + x] >> 7)
517 byte |= BIT(7);
518 }
519 *mono++ = byte;
520 }
521 }
522
repaper_fb_dirty(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips)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)
528 {
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;
534 u8 *buf = NULL;
535 int ret = 0;
536
537 /* repaper can't do partial updates */
538 clip.x1 = 0;
539 clip.x2 = fb->width;
540 clip.y1 = 0;
541 clip.y2 = fb->height;
542
543 if (!epd->enabled)
544 return 0;
545
546 repaper_get_temperature(epd);
547
548 DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
549 epd->factored_stage_time);
550
551 buf = kmalloc_array(fb->width, fb->height, GFP_KERNEL);
552 if (!buf)
553 return -ENOMEM;
554
555 if (import_attach) {
556 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
557 DMA_FROM_DEVICE);
558 if (ret)
559 goto out_free;
560 }
561
562 tinydrm_xrgb8888_to_gray8(buf, cma_obj->vaddr, fb, &clip);
563
564 if (import_attach) {
565 ret = dma_buf_end_cpu_access(import_attach->dmabuf,
566 DMA_FROM_DEVICE);
567 if (ret)
568 goto out_free;
569 }
570
571 repaper_gray8_to_mono_reversed(buf, fb->width, fb->height);
572
573 if (epd->partial) {
574 repaper_frame_data_repeat(epd, buf, epd->current_frame,
575 REPAPER_NORMAL);
576 } else if (epd->cleared) {
577 repaper_frame_data_repeat(epd, epd->current_frame, NULL,
578 REPAPER_COMPENSATE);
579 repaper_frame_data_repeat(epd, epd->current_frame, NULL,
580 REPAPER_WHITE);
581 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
582 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
583
584 epd->partial = true;
585 } else {
586 /* Clear display (anything -> white) */
587 repaper_frame_fixed_repeat(epd, 0xff, REPAPER_COMPENSATE);
588 repaper_frame_fixed_repeat(epd, 0xff, REPAPER_WHITE);
589 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_INVERSE);
590 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_NORMAL);
591
592 /* Assuming a clear (white) screen output an image */
593 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_COMPENSATE);
594 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_WHITE);
595 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
596 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
597
598 epd->cleared = true;
599 epd->partial = true;
600 }
601
602 memcpy(epd->current_frame, buf, fb->width * fb->height / 8);
603
604 /*
605 * An extra frame write is needed if pixels are set in the bottom line,
606 * or else grey lines rises up from the pixels
607 */
608 if (epd->pre_border_byte) {
609 unsigned int x;
610
611 for (x = 0; x < (fb->width / 8); x++)
612 if (buf[x + (fb->width * (fb->height - 1) / 8)]) {
613 repaper_frame_data_repeat(epd, buf,
614 epd->current_frame,
615 REPAPER_NORMAL);
616 break;
617 }
618 }
619
620 out_free:
621 kfree(buf);
622
623 return ret;
624 }
625
626 static const struct drm_framebuffer_funcs repaper_fb_funcs = {
627 .destroy = drm_gem_fb_destroy,
628 .create_handle = drm_gem_fb_create_handle,
629 .dirty = tinydrm_fb_dirty,
630 };
631
power_off(struct repaper_epd * epd)632 static void power_off(struct repaper_epd *epd)
633 {
634 /* Turn off power and all signals */
635 gpiod_set_value_cansleep(epd->reset, 0);
636 gpiod_set_value_cansleep(epd->panel_on, 0);
637 if (epd->border)
638 gpiod_set_value_cansleep(epd->border, 0);
639
640 /* Ensure SPI MOSI and CLOCK are Low before CS Low */
641 repaper_spi_mosi_low(epd->spi);
642
643 /* Discharge pulse */
644 gpiod_set_value_cansleep(epd->discharge, 1);
645 msleep(150);
646 gpiod_set_value_cansleep(epd->discharge, 0);
647 }
648
repaper_pipe_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)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)
652 {
653 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
654 struct repaper_epd *epd = epd_from_tinydrm(tdev);
655 struct spi_device *spi = epd->spi;
656 struct device *dev = &spi->dev;
657 bool dc_ok = false;
658 int i, ret;
659
660 DRM_DEBUG_DRIVER("\n");
661
662 /* Power up sequence */
663 gpiod_set_value_cansleep(epd->reset, 0);
664 gpiod_set_value_cansleep(epd->panel_on, 0);
665 gpiod_set_value_cansleep(epd->discharge, 0);
666 if (epd->border)
667 gpiod_set_value_cansleep(epd->border, 0);
668 repaper_spi_mosi_low(spi);
669 usleep_range(5000, 10000);
670
671 gpiod_set_value_cansleep(epd->panel_on, 1);
672 /*
673 * This delay comes from the repaper.org userspace driver, it's not
674 * mentioned in the datasheet.
675 */
676 usleep_range(10000, 15000);
677 gpiod_set_value_cansleep(epd->reset, 1);
678 if (epd->border)
679 gpiod_set_value_cansleep(epd->border, 1);
680 usleep_range(5000, 10000);
681 gpiod_set_value_cansleep(epd->reset, 0);
682 usleep_range(5000, 10000);
683 gpiod_set_value_cansleep(epd->reset, 1);
684 usleep_range(5000, 10000);
685
686 /* Wait for COG to become ready */
687 for (i = 100; i > 0; i--) {
688 if (!gpiod_get_value_cansleep(epd->busy))
689 break;
690
691 usleep_range(10, 100);
692 }
693
694 if (!i) {
695 DRM_DEV_ERROR(dev, "timeout waiting for panel to become ready.\n");
696 power_off(epd);
697 return;
698 }
699
700 repaper_read_id(spi);
701 ret = repaper_read_id(spi);
702 if (ret != REPAPER_RID_G2_COG_ID) {
703 if (ret < 0)
704 dev_err(dev, "failed to read chip (%d)\n", ret);
705 else
706 dev_err(dev, "wrong COG ID 0x%02x\n", ret);
707 power_off(epd);
708 return;
709 }
710
711 /* Disable OE */
712 repaper_write_val(spi, 0x02, 0x40);
713
714 ret = repaper_read_val(spi, 0x0f);
715 if (ret < 0 || !(ret & 0x80)) {
716 if (ret < 0)
717 DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
718 else
719 DRM_DEV_ERROR(dev, "panel is reported broken\n");
720 power_off(epd);
721 return;
722 }
723
724 /* Power saving mode */
725 repaper_write_val(spi, 0x0b, 0x02);
726 /* Channel select */
727 repaper_write_buf(spi, 0x01, epd->channel_select, 8);
728 /* High power mode osc */
729 repaper_write_val(spi, 0x07, 0xd1);
730 /* Power setting */
731 repaper_write_val(spi, 0x08, 0x02);
732 /* Vcom level */
733 repaper_write_val(spi, 0x09, 0xc2);
734 /* Power setting */
735 repaper_write_val(spi, 0x04, 0x03);
736 /* Driver latch on */
737 repaper_write_val(spi, 0x03, 0x01);
738 /* Driver latch off */
739 repaper_write_val(spi, 0x03, 0x00);
740 usleep_range(5000, 10000);
741
742 /* Start chargepump */
743 for (i = 0; i < 4; ++i) {
744 /* Charge pump positive voltage on - VGH/VDL on */
745 repaper_write_val(spi, 0x05, 0x01);
746 msleep(240);
747
748 /* Charge pump negative voltage on - VGL/VDL on */
749 repaper_write_val(spi, 0x05, 0x03);
750 msleep(40);
751
752 /* Charge pump Vcom on - Vcom driver on */
753 repaper_write_val(spi, 0x05, 0x0f);
754 msleep(40);
755
756 /* check DC/DC */
757 ret = repaper_read_val(spi, 0x0f);
758 if (ret < 0) {
759 DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
760 power_off(epd);
761 return;
762 }
763
764 if (ret & 0x40) {
765 dc_ok = true;
766 break;
767 }
768 }
769
770 if (!dc_ok) {
771 DRM_DEV_ERROR(dev, "dc/dc failed\n");
772 power_off(epd);
773 return;
774 }
775
776 /*
777 * Output enable to disable
778 * The userspace driver sets this to 0x04, but the datasheet says 0x06
779 */
780 repaper_write_val(spi, 0x02, 0x04);
781
782 epd->enabled = true;
783 epd->partial = false;
784 }
785
repaper_pipe_disable(struct drm_simple_display_pipe * pipe)786 static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
787 {
788 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
789 struct repaper_epd *epd = epd_from_tinydrm(tdev);
790 struct spi_device *spi = epd->spi;
791 unsigned int line;
792
793 DRM_DEBUG_DRIVER("\n");
794
795 mutex_lock(&tdev->dirty_lock);
796 epd->enabled = false;
797 mutex_unlock(&tdev->dirty_lock);
798
799 /* Nothing frame */
800 for (line = 0; line < epd->height; line++)
801 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
802 REPAPER_COMPENSATE);
803
804 /* 2.7" */
805 if (epd->border) {
806 /* Dummy line */
807 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
808 REPAPER_COMPENSATE);
809 msleep(25);
810 gpiod_set_value_cansleep(epd->border, 0);
811 msleep(200);
812 gpiod_set_value_cansleep(epd->border, 1);
813 } else {
814 /* Border dummy line */
815 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
816 REPAPER_NORMAL);
817 msleep(200);
818 }
819
820 /* not described in datasheet */
821 repaper_write_val(spi, 0x0b, 0x00);
822 /* Latch reset turn on */
823 repaper_write_val(spi, 0x03, 0x01);
824 /* Power off charge pump Vcom */
825 repaper_write_val(spi, 0x05, 0x03);
826 /* Power off charge pump neg voltage */
827 repaper_write_val(spi, 0x05, 0x01);
828 msleep(120);
829 /* Discharge internal */
830 repaper_write_val(spi, 0x04, 0x80);
831 /* turn off all charge pumps */
832 repaper_write_val(spi, 0x05, 0x00);
833 /* Turn off osc */
834 repaper_write_val(spi, 0x07, 0x01);
835 msleep(50);
836
837 power_off(epd);
838 }
839
840 static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
841 .enable = repaper_pipe_enable,
842 .disable = repaper_pipe_disable,
843 .update = tinydrm_display_pipe_update,
844 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
845 };
846
847 static const uint32_t repaper_formats[] = {
848 DRM_FORMAT_XRGB8888,
849 };
850
851 static const struct drm_display_mode repaper_e1144cs021_mode = {
852 TINYDRM_MODE(128, 96, 29, 22),
853 };
854
855 static const u8 repaper_e1144cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
856 0x00, 0x0f, 0xff, 0x00 };
857
858 static const struct drm_display_mode repaper_e1190cs021_mode = {
859 TINYDRM_MODE(144, 128, 36, 32),
860 };
861
862 static const u8 repaper_e1190cs021_cs[] = { 0x00, 0x00, 0x00, 0x03,
863 0xfc, 0x00, 0x00, 0xff };
864
865 static const struct drm_display_mode repaper_e2200cs021_mode = {
866 TINYDRM_MODE(200, 96, 46, 22),
867 };
868
869 static const u8 repaper_e2200cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
870 0x01, 0xff, 0xe0, 0x00 };
871
872 static const struct drm_display_mode repaper_e2271cs021_mode = {
873 TINYDRM_MODE(264, 176, 57, 38),
874 };
875
876 static const u8 repaper_e2271cs021_cs[] = { 0x00, 0x00, 0x00, 0x7f,
877 0xff, 0xfe, 0x00, 0x00 };
878
879 DEFINE_DRM_GEM_CMA_FOPS(repaper_fops);
880
881 static struct drm_driver repaper_driver = {
882 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
883 DRIVER_ATOMIC,
884 .fops = &repaper_fops,
885 TINYDRM_GEM_DRIVER_OPS,
886 .name = "repaper",
887 .desc = "Pervasive Displays RePaper e-ink panels",
888 .date = "20170405",
889 .major = 1,
890 .minor = 0,
891 };
892
893 static const struct of_device_id repaper_of_match[] = {
894 { .compatible = "pervasive,e1144cs021", .data = (void *)E1144CS021 },
895 { .compatible = "pervasive,e1190cs021", .data = (void *)E1190CS021 },
896 { .compatible = "pervasive,e2200cs021", .data = (void *)E2200CS021 },
897 { .compatible = "pervasive,e2271cs021", .data = (void *)E2271CS021 },
898 {},
899 };
900 MODULE_DEVICE_TABLE(of, repaper_of_match);
901
902 static const struct spi_device_id repaper_id[] = {
903 { "e1144cs021", E1144CS021 },
904 { "e1190cs021", E1190CS021 },
905 { "e2200cs021", E2200CS021 },
906 { "e2271cs021", E2271CS021 },
907 { },
908 };
909 MODULE_DEVICE_TABLE(spi, repaper_id);
910
repaper_probe(struct spi_device * spi)911 static int repaper_probe(struct spi_device *spi)
912 {
913 const struct drm_display_mode *mode;
914 const struct spi_device_id *spi_id;
915 const struct of_device_id *match;
916 struct device *dev = &spi->dev;
917 struct tinydrm_device *tdev;
918 enum repaper_model model;
919 const char *thermal_zone;
920 struct repaper_epd *epd;
921 size_t line_buffer_size;
922 int ret;
923
924 match = of_match_device(repaper_of_match, dev);
925 if (match) {
926 model = (enum repaper_model)match->data;
927 } else {
928 spi_id = spi_get_device_id(spi);
929 model = spi_id->driver_data;
930 }
931
932 /* The SPI device is used to allocate dma memory */
933 if (!dev->coherent_dma_mask) {
934 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
935 if (ret) {
936 dev_warn(dev, "Failed to set dma mask %d\n", ret);
937 return ret;
938 }
939 }
940
941 epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL);
942 if (!epd)
943 return -ENOMEM;
944
945 epd->spi = spi;
946
947 epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
948 if (IS_ERR(epd->panel_on)) {
949 ret = PTR_ERR(epd->panel_on);
950 if (ret != -EPROBE_DEFER)
951 DRM_DEV_ERROR(dev, "Failed to get gpio 'panel-on'\n");
952 return ret;
953 }
954
955 epd->discharge = devm_gpiod_get(dev, "discharge", GPIOD_OUT_LOW);
956 if (IS_ERR(epd->discharge)) {
957 ret = PTR_ERR(epd->discharge);
958 if (ret != -EPROBE_DEFER)
959 DRM_DEV_ERROR(dev, "Failed to get gpio 'discharge'\n");
960 return ret;
961 }
962
963 epd->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
964 if (IS_ERR(epd->reset)) {
965 ret = PTR_ERR(epd->reset);
966 if (ret != -EPROBE_DEFER)
967 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
968 return ret;
969 }
970
971 epd->busy = devm_gpiod_get(dev, "busy", GPIOD_IN);
972 if (IS_ERR(epd->busy)) {
973 ret = PTR_ERR(epd->busy);
974 if (ret != -EPROBE_DEFER)
975 DRM_DEV_ERROR(dev, "Failed to get gpio 'busy'\n");
976 return ret;
977 }
978
979 if (!device_property_read_string(dev, "pervasive,thermal-zone",
980 &thermal_zone)) {
981 epd->thermal = thermal_zone_get_zone_by_name(thermal_zone);
982 if (IS_ERR(epd->thermal)) {
983 DRM_DEV_ERROR(dev, "Failed to get thermal zone: %s\n", thermal_zone);
984 return PTR_ERR(epd->thermal);
985 }
986 }
987
988 switch (model) {
989 case E1144CS021:
990 mode = &repaper_e1144cs021_mode;
991 epd->channel_select = repaper_e1144cs021_cs;
992 epd->stage_time = 480;
993 epd->bytes_per_scan = 96 / 4;
994 epd->middle_scan = true; /* data-scan-data */
995 epd->pre_border_byte = false;
996 epd->border_byte = REPAPER_BORDER_BYTE_ZERO;
997 break;
998
999 case E1190CS021:
1000 mode = &repaper_e1190cs021_mode;
1001 epd->channel_select = repaper_e1190cs021_cs;
1002 epd->stage_time = 480;
1003 epd->bytes_per_scan = 128 / 4 / 2;
1004 epd->middle_scan = false; /* scan-data-scan */
1005 epd->pre_border_byte = false;
1006 epd->border_byte = REPAPER_BORDER_BYTE_SET;
1007 break;
1008
1009 case E2200CS021:
1010 mode = &repaper_e2200cs021_mode;
1011 epd->channel_select = repaper_e2200cs021_cs;
1012 epd->stage_time = 480;
1013 epd->bytes_per_scan = 96 / 4;
1014 epd->middle_scan = true; /* data-scan-data */
1015 epd->pre_border_byte = true;
1016 epd->border_byte = REPAPER_BORDER_BYTE_NONE;
1017 break;
1018
1019 case E2271CS021:
1020 epd->border = devm_gpiod_get(dev, "border", GPIOD_OUT_LOW);
1021 if (IS_ERR(epd->border)) {
1022 ret = PTR_ERR(epd->border);
1023 if (ret != -EPROBE_DEFER)
1024 DRM_DEV_ERROR(dev, "Failed to get gpio 'border'\n");
1025 return ret;
1026 }
1027
1028 mode = &repaper_e2271cs021_mode;
1029 epd->channel_select = repaper_e2271cs021_cs;
1030 epd->stage_time = 630;
1031 epd->bytes_per_scan = 176 / 4;
1032 epd->middle_scan = true; /* data-scan-data */
1033 epd->pre_border_byte = true;
1034 epd->border_byte = REPAPER_BORDER_BYTE_NONE;
1035 break;
1036
1037 default:
1038 return -ENODEV;
1039 }
1040
1041 epd->width = mode->hdisplay;
1042 epd->height = mode->vdisplay;
1043 epd->factored_stage_time = epd->stage_time;
1044
1045 line_buffer_size = 2 * epd->width / 8 + epd->bytes_per_scan + 2;
1046 epd->line_buffer = devm_kzalloc(dev, line_buffer_size, GFP_KERNEL);
1047 if (!epd->line_buffer)
1048 return -ENOMEM;
1049
1050 epd->current_frame = devm_kzalloc(dev, epd->width * epd->height / 8,
1051 GFP_KERNEL);
1052 if (!epd->current_frame)
1053 return -ENOMEM;
1054
1055 tdev = &epd->tinydrm;
1056
1057 ret = devm_tinydrm_init(dev, tdev, &repaper_fb_funcs, &repaper_driver);
1058 if (ret)
1059 return ret;
1060
1061 tdev->fb_dirty = repaper_fb_dirty;
1062
1063 ret = tinydrm_display_pipe_init(tdev, &repaper_pipe_funcs,
1064 DRM_MODE_CONNECTOR_VIRTUAL,
1065 repaper_formats,
1066 ARRAY_SIZE(repaper_formats), mode, 0);
1067 if (ret)
1068 return ret;
1069
1070 drm_mode_config_reset(tdev->drm);
1071 spi_set_drvdata(spi, tdev);
1072
1073 DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
1074
1075 return devm_tinydrm_register(tdev);
1076 }
1077
repaper_shutdown(struct spi_device * spi)1078 static void repaper_shutdown(struct spi_device *spi)
1079 {
1080 struct tinydrm_device *tdev = spi_get_drvdata(spi);
1081
1082 tinydrm_shutdown(tdev);
1083 }
1084
1085 static struct spi_driver repaper_spi_driver = {
1086 .driver = {
1087 .name = "repaper",
1088 .owner = THIS_MODULE,
1089 .of_match_table = repaper_of_match,
1090 },
1091 .id_table = repaper_id,
1092 .probe = repaper_probe,
1093 .shutdown = repaper_shutdown,
1094 };
1095 module_spi_driver(repaper_spi_driver);
1096
1097 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1098 MODULE_AUTHOR("Noralf Trønnes");
1099 MODULE_LICENSE("GPL");
1100