Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / gpu / drm / tinydrm / repaper.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DRM driver for Pervasive Displays RePaper branded e-ink panels
4  *
5  * Copyright 2013-2017 Pervasive Displays, Inc.
6  * Copyright 2017 Noralf Trønnes
7  *
8  * The driver supports:
9  * Material Film: Aurora Mb (V231)
10  * Driver IC: G2 (eTC)
11  *
12  * The controller code was taken from the userspace driver:
13  * https://github.com/repaper/gratis
14  */
15
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>
24
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>
37
38 #define REPAPER_RID_G2_COG_ID   0x12
39
40 enum repaper_model {
41         E1144CS021 = 1,
42         E1190CS021,
43         E2200CS021,
44         E2271CS021,
45 };
46
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) */
52 };
53
54 enum repaper_epd_border_byte {
55         REPAPER_BORDER_BYTE_NONE,
56         REPAPER_BORDER_BYTE_ZERO,
57         REPAPER_BORDER_BYTE_SET,
58 };
59
60 struct repaper_epd {
61         struct drm_device drm;
62         struct drm_simple_display_pipe pipe;
63         struct spi_device *spi;
64
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;
70
71         struct thermal_zone_device *thermal;
72
73         unsigned int height;
74         unsigned int width;
75         unsigned int bytes_per_scan;
76         const u8 *channel_select;
77         unsigned int stage_time;
78         unsigned int factored_stage_time;
79         bool middle_scan;
80         bool pre_border_byte;
81         enum repaper_epd_border_byte border_byte;
82
83         u8 *line_buffer;
84         void *current_frame;
85
86         bool enabled;
87         bool cleared;
88         bool partial;
89 };
90
91 static inline struct repaper_epd *drm_to_epd(struct drm_device *drm)
92 {
93         return container_of(drm, struct repaper_epd, drm);
94 }
95
96 static int repaper_spi_transfer(struct spi_device *spi, u8 header,
97                                 const void *tx, void *rx, size_t len)
98 {
99         void *txbuf = NULL, *rxbuf = NULL;
100         struct spi_transfer tr[2] = {};
101         u8 *headerbuf;
102         int ret;
103
104         headerbuf = kmalloc(1, GFP_KERNEL);
105         if (!headerbuf)
106                 return -ENOMEM;
107
108         headerbuf[0] = header;
109         tr[0].tx_buf = headerbuf;
110         tr[0].len = 1;
111
112         /* Stack allocated tx? */
113         if (tx && len <= 32) {
114                 txbuf = kmemdup(tx, len, GFP_KERNEL);
115                 if (!txbuf) {
116                         ret = -ENOMEM;
117                         goto out_free;
118                 }
119         }
120
121         if (rx) {
122                 rxbuf = kmalloc(len, GFP_KERNEL);
123                 if (!rxbuf) {
124                         ret = -ENOMEM;
125                         goto out_free;
126                 }
127         }
128
129         tr[1].tx_buf = txbuf ? txbuf : tx;
130         tr[1].rx_buf = rxbuf;
131         tr[1].len = len;
132
133         ndelay(80);
134         ret = spi_sync_transfer(spi, tr, 2);
135         if (rx && !ret)
136                 memcpy(rx, rxbuf, len);
137
138 out_free:
139         kfree(headerbuf);
140         kfree(txbuf);
141         kfree(rxbuf);
142
143         return ret;
144 }
145
146 static int repaper_write_buf(struct spi_device *spi, u8 reg,
147                              const u8 *buf, size_t len)
148 {
149         int ret;
150
151         ret = repaper_spi_transfer(spi, 0x70, &reg, NULL, 1);
152         if (ret)
153                 return ret;
154
155         return repaper_spi_transfer(spi, 0x72, buf, NULL, len);
156 }
157
158 static int repaper_write_val(struct spi_device *spi, u8 reg, u8 val)
159 {
160         return repaper_write_buf(spi, reg, &val, 1);
161 }
162
163 static int repaper_read_val(struct spi_device *spi, u8 reg)
164 {
165         int ret;
166         u8 val;
167
168         ret = repaper_spi_transfer(spi, 0x70, &reg, NULL, 1);
169         if (ret)
170                 return ret;
171
172         ret = repaper_spi_transfer(spi, 0x73, NULL, &val, 1);
173
174         return ret ? ret : val;
175 }
176
177 static int repaper_read_id(struct spi_device *spi)
178 {
179         int ret;
180         u8 id;
181
182         ret = repaper_spi_transfer(spi, 0x71, NULL, &id, 1);
183
184         return ret ? ret : id;
185 }
186
187 static void repaper_spi_mosi_low(struct spi_device *spi)
188 {
189         const u8 buf[1] = { 0 };
190
191         spi_write(spi, buf, 1);
192 }
193
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)
198 {
199         unsigned int b;
200
201         for (b = 0; b < (epd->width / 8); b++) {
202                 if (data) {
203                         u8 pixels = data[b] & 0xaa;
204                         u8 pixel_mask = 0xff;
205                         u8 p1, p2, p3, p4;
206
207                         if (mask) {
208                                 pixel_mask = (mask[b] ^ pixels) & 0xaa;
209                                 pixel_mask |= pixel_mask >> 1;
210                         }
211
212                         switch (stage) {
213                         case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
214                                 pixels = 0xaa | ((pixels ^ 0xaa) >> 1);
215                                 break;
216                         case REPAPER_WHITE:      /* B -> N, W -> W (Current) */
217                                 pixels = 0x55 + ((pixels ^ 0xaa) >> 1);
218                                 break;
219                         case REPAPER_INVERSE:    /* B -> N, W -> B (New) */
220                                 pixels = 0x55 | (pixels ^ 0xaa);
221                                 break;
222                         case REPAPER_NORMAL:     /* B -> B, W -> W (New) */
223                                 pixels = 0xaa | (pixels >> 1);
224                                 break;
225                         }
226
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);
233                         *(*pp)++ = pixels;
234                 } else {
235                         *(*pp)++ = fixed_value;
236                 }
237         }
238 }
239
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)
244 {
245         unsigned int b;
246
247         for (b = epd->width / 8; b > 0; b--) {
248                 if (data) {
249                         u8 pixels = data[b - 1] & 0x55;
250                         u8 pixel_mask = 0xff;
251
252                         if (mask) {
253                                 pixel_mask = (mask[b - 1] ^ pixels) & 0x55;
254                                 pixel_mask |= pixel_mask << 1;
255                         }
256
257                         switch (stage) {
258                         case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
259                                 pixels = 0xaa | (pixels ^ 0x55);
260                                 break;
261                         case REPAPER_WHITE:      /* B -> N, W -> W (Current) */
262                                 pixels = 0x55 + (pixels ^ 0x55);
263                                 break;
264                         case REPAPER_INVERSE:    /* B -> N, W -> B (New) */
265                                 pixels = 0x55 | ((pixels ^ 0x55) << 1);
266                                 break;
267                         case REPAPER_NORMAL:     /* B -> B, W -> W (New) */
268                                 pixels = 0xaa | pixels;
269                                 break;
270                         }
271
272                         pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55);
273                         *(*pp)++ = pixels;
274                 } else {
275                         *(*pp)++ = fixed_value;
276                 }
277         }
278 }
279
280 /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
281 static inline u16 repaper_interleave_bits(u16 value)
282 {
283         value = (value | (value << 4)) & 0x0f0f;
284         value = (value | (value << 2)) & 0x3333;
285         value = (value | (value << 1)) & 0x5555;
286
287         return value;
288 }
289
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)
294 {
295         unsigned int b;
296
297         for (b = epd->width / 8; b > 0; b--) {
298                 if (data) {
299                         u16 pixels = repaper_interleave_bits(data[b - 1]);
300                         u16 pixel_mask = 0xffff;
301
302                         if (mask) {
303                                 pixel_mask = repaper_interleave_bits(mask[b - 1]);
304
305                                 pixel_mask = (pixel_mask ^ pixels) & 0x5555;
306                                 pixel_mask |= pixel_mask << 1;
307                         }
308
309                         switch (stage) {
310                         case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
311                                 pixels = 0xaaaa | (pixels ^ 0x5555);
312                                 break;
313                         case REPAPER_WHITE:      /* B -> N, W -> W (Current) */
314                                 pixels = 0x5555 + (pixels ^ 0x5555);
315                                 break;
316                         case REPAPER_INVERSE:    /* B -> N, W -> B (New) */
317                                 pixels = 0x5555 | ((pixels ^ 0x5555) << 1);
318                                 break;
319                         case REPAPER_NORMAL:     /* B -> B, W -> W (New) */
320                                 pixels = 0xaaaa | pixels;
321                                 break;
322                         }
323
324                         pixels = (pixels & pixel_mask) | (~pixel_mask & 0x5555);
325                         *(*pp)++ = pixels >> 8;
326                         *(*pp)++ = pixels;
327                 } else {
328                         *(*pp)++ = fixed_value;
329                         *(*pp)++ = fixed_value;
330                 }
331         }
332 }
333
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)
338 {
339         u8 *p = epd->line_buffer;
340         unsigned int b;
341
342         repaper_spi_mosi_low(epd->spi);
343
344         if (epd->pre_border_byte)
345                 *p++ = 0x00;
346
347         if (epd->middle_scan) {
348                 /* data bytes */
349                 repaper_odd_pixels(epd, &p, data, fixed_value, mask, stage);
350
351                 /* scan line */
352                 for (b = epd->bytes_per_scan; b > 0; b--) {
353                         if (line / 4 == b - 1)
354                                 *p++ = 0x03 << (2 * (line & 0x03));
355                         else
356                                 *p++ = 0x00;
357                 }
358
359                 /* data bytes */
360                 repaper_even_pixels(epd, &p, data, fixed_value, mask, stage);
361         } else {
362                 /*
363                  * even scan line, but as lines on display are numbered from 1,
364                  * line: 1,3,5,...
365                  */
366                 for (b = 0; b < epd->bytes_per_scan; b++) {
367                         if (0 != (line & 0x01) && line / 8 == b)
368                                 *p++ = 0xc0 >> (line & 0x06);
369                         else
370                                 *p++ = 0x00;
371                 }
372
373                 /* data bytes */
374                 repaper_all_pixels(epd, &p, data, fixed_value, mask, stage);
375
376                 /*
377                  * odd scan line, but as lines on display are numbered from 1,
378                  * line: 0,2,4,6,...
379                  */
380                 for (b = epd->bytes_per_scan; b > 0; b--) {
381                         if (0 == (line & 0x01) && line / 8 == b - 1)
382                                 *p++ = 0x03 << (line & 0x06);
383                         else
384                                 *p++ = 0x00;
385                 }
386         }
387
388         switch (epd->border_byte) {
389         case REPAPER_BORDER_BYTE_NONE:
390                 break;
391
392         case REPAPER_BORDER_BYTE_ZERO:
393                 *p++ = 0x00;
394                 break;
395
396         case REPAPER_BORDER_BYTE_SET:
397                 switch (stage) {
398                 case REPAPER_COMPENSATE:
399                 case REPAPER_WHITE:
400                 case REPAPER_INVERSE:
401                         *p++ = 0x00;
402                         break;
403                 case REPAPER_NORMAL:
404                         *p++ = 0xaa;
405                         break;
406                 }
407                 break;
408         }
409
410         repaper_write_buf(epd->spi, 0x0a, epd->line_buffer,
411                           p - epd->line_buffer);
412
413         /* Output data to panel */
414         repaper_write_val(epd->spi, 0x02, 0x07);
415
416         repaper_spi_mosi_low(epd->spi);
417 }
418
419 static void repaper_frame_fixed(struct repaper_epd *epd, u8 fixed_value,
420                                 enum repaper_stage stage)
421 {
422         unsigned int line;
423
424         for (line = 0; line < epd->height; line++)
425                 repaper_one_line(epd, line, NULL, fixed_value, NULL, stage);
426 }
427
428 static void repaper_frame_data(struct repaper_epd *epd, const u8 *image,
429                                const u8 *mask, enum repaper_stage stage)
430 {
431         unsigned int line;
432
433         if (!mask) {
434                 for (line = 0; line < epd->height; line++) {
435                         repaper_one_line(epd, line,
436                                          &image[line * (epd->width / 8)],
437                                          0, NULL, stage);
438                 }
439         } else {
440                 for (line = 0; line < epd->height; line++) {
441                         size_t n = line * epd->width / 8;
442
443                         repaper_one_line(epd, line, &image[n], 0, &mask[n],
444                                          stage);
445                 }
446         }
447 }
448
449 static void repaper_frame_fixed_repeat(struct repaper_epd *epd, u8 fixed_value,
450                                        enum repaper_stage stage)
451 {
452         u64 start = local_clock();
453         u64 end = start + (epd->factored_stage_time * 1000 * 1000);
454
455         do {
456                 repaper_frame_fixed(epd, fixed_value, stage);
457         } while (local_clock() < end);
458 }
459
460 static void repaper_frame_data_repeat(struct repaper_epd *epd, const u8 *image,
461                                       const u8 *mask, enum repaper_stage stage)
462 {
463         u64 start = local_clock();
464         u64 end = start + (epd->factored_stage_time * 1000 * 1000);
465
466         do {
467                 repaper_frame_data(epd, image, mask, stage);
468         } while (local_clock() < end);
469 }
470
471 static void repaper_get_temperature(struct repaper_epd *epd)
472 {
473         int ret, temperature = 0;
474         unsigned int factor10x;
475
476         if (!epd->thermal)
477                 return;
478
479         ret = thermal_zone_get_temp(epd->thermal, &temperature);
480         if (ret) {
481                 DRM_DEV_ERROR(&epd->spi->dev, "Failed to get temperature (%d)\n", ret);
482                 return;
483         }
484
485         temperature /= 1000;
486
487         if (temperature <= -10)
488                 factor10x = 170;
489         else if (temperature <= -5)
490                 factor10x = 120;
491         else if (temperature <= 5)
492                 factor10x = 80;
493         else if (temperature <= 10)
494                 factor10x = 40;
495         else if (temperature <= 15)
496                 factor10x = 30;
497         else if (temperature <= 20)
498                 factor10x = 20;
499         else if (temperature <= 40)
500                 factor10x = 10;
501         else
502                 factor10x = 7;
503
504         epd->factored_stage_time = epd->stage_time * factor10x / 10;
505 }
506
507 static void repaper_gray8_to_mono_reversed(u8 *buf, u32 width, u32 height)
508 {
509         u8 *gray8 = buf, *mono = buf;
510         int y, xb, i;
511
512         for (y = 0; y < height; y++)
513                 for (xb = 0; xb < width / 8; xb++) {
514                         u8 byte = 0x00;
515
516                         for (i = 0; i < 8; i++) {
517                                 int x = xb * 8 + i;
518
519                                 byte >>= 1;
520                                 if (gray8[y * width + x] >> 7)
521                                         byte |= BIT(7);
522                         }
523                         *mono++ = byte;
524                 }
525 }
526
527 static int repaper_fb_dirty(struct drm_framebuffer *fb)
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 repaper_epd *epd = drm_to_epd(fb->dev);
532         struct drm_rect clip;
533         int idx, ret = 0;
534         u8 *buf = NULL;
535
536         if (!epd->enabled)
537                 return 0;
538
539         if (!drm_dev_enter(fb->dev, &idx))
540                 return -ENODEV;
541
542         /* repaper can't do partial updates */
543         clip.x1 = 0;
544         clip.x2 = fb->width;
545         clip.y1 = 0;
546         clip.y2 = fb->height;
547
548         repaper_get_temperature(epd);
549
550         DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
551                   epd->factored_stage_time);
552
553         buf = kmalloc_array(fb->width, fb->height, GFP_KERNEL);
554         if (!buf) {
555                 ret = -ENOMEM;
556                 goto out_exit;
557         }
558
559         if (import_attach) {
560                 ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
561                                                DMA_FROM_DEVICE);
562                 if (ret)
563                         goto out_free;
564         }
565
566         drm_fb_xrgb8888_to_gray8(buf, cma_obj->vaddr, fb, &clip);
567
568         if (import_attach) {
569                 ret = dma_buf_end_cpu_access(import_attach->dmabuf,
570                                              DMA_FROM_DEVICE);
571                 if (ret)
572                         goto out_free;
573         }
574
575         repaper_gray8_to_mono_reversed(buf, fb->width, fb->height);
576
577         if (epd->partial) {
578                 repaper_frame_data_repeat(epd, buf, epd->current_frame,
579                                           REPAPER_NORMAL);
580         } else if (epd->cleared) {
581                 repaper_frame_data_repeat(epd, epd->current_frame, NULL,
582                                           REPAPER_COMPENSATE);
583                 repaper_frame_data_repeat(epd, epd->current_frame, NULL,
584                                           REPAPER_WHITE);
585                 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
586                 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
587
588                 epd->partial = true;
589         } else {
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);
595
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);
601
602                 epd->cleared = true;
603                 epd->partial = true;
604         }
605
606         memcpy(epd->current_frame, buf, fb->width * fb->height / 8);
607
608         /*
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
611          */
612         if (epd->pre_border_byte) {
613                 unsigned int x;
614
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,
618                                                           epd->current_frame,
619                                                           REPAPER_NORMAL);
620                                 break;
621                         }
622         }
623
624 out_free:
625         kfree(buf);
626 out_exit:
627         drm_dev_exit(idx);
628
629         return ret;
630 }
631
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
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 repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
654         struct spi_device *spi = epd->spi;
655         struct device *dev = &spi->dev;
656         bool dc_ok = false;
657         int i, ret, idx;
658
659         if (!drm_dev_enter(pipe->crtc.dev, &idx))
660                 return;
661
662         DRM_DEBUG_DRIVER("\n");
663
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);
668         if (epd->border)
669                 gpiod_set_value_cansleep(epd->border, 0);
670         repaper_spi_mosi_low(spi);
671         usleep_range(5000, 10000);
672
673         gpiod_set_value_cansleep(epd->panel_on, 1);
674         /*
675          * This delay comes from the repaper.org userspace driver, it's not
676          * mentioned in the datasheet.
677          */
678         usleep_range(10000, 15000);
679         gpiod_set_value_cansleep(epd->reset, 1);
680         if (epd->border)
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);
687
688         /* Wait for COG to become ready */
689         for (i = 100; i > 0; i--) {
690                 if (!gpiod_get_value_cansleep(epd->busy))
691                         break;
692
693                 usleep_range(10, 100);
694         }
695
696         if (!i) {
697                 DRM_DEV_ERROR(dev, "timeout waiting for panel to become ready.\n");
698                 power_off(epd);
699                 goto out_exit;
700         }
701
702         repaper_read_id(spi);
703         ret = repaper_read_id(spi);
704         if (ret != REPAPER_RID_G2_COG_ID) {
705                 if (ret < 0)
706                         dev_err(dev, "failed to read chip (%d)\n", ret);
707                 else
708                         dev_err(dev, "wrong COG ID 0x%02x\n", ret);
709                 power_off(epd);
710                 goto out_exit;
711         }
712
713         /* Disable OE */
714         repaper_write_val(spi, 0x02, 0x40);
715
716         ret = repaper_read_val(spi, 0x0f);
717         if (ret < 0 || !(ret & 0x80)) {
718                 if (ret < 0)
719                         DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
720                 else
721                         DRM_DEV_ERROR(dev, "panel is reported broken\n");
722                 power_off(epd);
723                 goto out_exit;
724         }
725
726         /* Power saving mode */
727         repaper_write_val(spi, 0x0b, 0x02);
728         /* Channel select */
729         repaper_write_buf(spi, 0x01, epd->channel_select, 8);
730         /* High power mode osc */
731         repaper_write_val(spi, 0x07, 0xd1);
732         /* Power setting */
733         repaper_write_val(spi, 0x08, 0x02);
734         /* Vcom level */
735         repaper_write_val(spi, 0x09, 0xc2);
736         /* Power setting */
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);
743
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);
748                 msleep(240);
749
750                 /* Charge pump negative voltage on - VGL/VDL on */
751                 repaper_write_val(spi, 0x05, 0x03);
752                 msleep(40);
753
754                 /* Charge pump Vcom on - Vcom driver on */
755                 repaper_write_val(spi, 0x05, 0x0f);
756                 msleep(40);
757
758                 /* check DC/DC */
759                 ret = repaper_read_val(spi, 0x0f);
760                 if (ret < 0) {
761                         DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
762                         power_off(epd);
763                         goto out_exit;
764                 }
765
766                 if (ret & 0x40) {
767                         dc_ok = true;
768                         break;
769                 }
770         }
771
772         if (!dc_ok) {
773                 DRM_DEV_ERROR(dev, "dc/dc failed\n");
774                 power_off(epd);
775                 goto out_exit;
776         }
777
778         /*
779          * Output enable to disable
780          * The userspace driver sets this to 0x04, but the datasheet says 0x06
781          */
782         repaper_write_val(spi, 0x02, 0x04);
783
784         epd->enabled = true;
785         epd->partial = false;
786 out_exit:
787         drm_dev_exit(idx);
788 }
789
790 static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
791 {
792         struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
793         struct spi_device *spi = epd->spi;
794         unsigned int line;
795
796         /*
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
800          * unplug.
801          */
802
803         if (!epd->enabled)
804                 return;
805
806         DRM_DEBUG_DRIVER("\n");
807
808         epd->enabled = false;
809
810         /* Nothing frame */
811         for (line = 0; line < epd->height; line++)
812                 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
813                                  REPAPER_COMPENSATE);
814
815         /* 2.7" */
816         if (epd->border) {
817                 /* Dummy line */
818                 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
819                                  REPAPER_COMPENSATE);
820                 msleep(25);
821                 gpiod_set_value_cansleep(epd->border, 0);
822                 msleep(200);
823                 gpiod_set_value_cansleep(epd->border, 1);
824         } else {
825                 /* Border dummy line */
826                 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
827                                  REPAPER_NORMAL);
828                 msleep(200);
829         }
830
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);
839         msleep(120);
840         /* Discharge internal */
841         repaper_write_val(spi, 0x04, 0x80);
842         /* turn off all charge pumps */
843         repaper_write_val(spi, 0x05, 0x00);
844         /* Turn off osc */
845         repaper_write_val(spi, 0x07, 0x01);
846         msleep(50);
847
848         power_off(epd);
849 }
850
851 static void repaper_pipe_update(struct drm_simple_display_pipe *pipe,
852                                 struct drm_plane_state *old_state)
853 {
854         struct drm_plane_state *state = pipe->plane.state;
855         struct drm_crtc *crtc = &pipe->crtc;
856         struct drm_rect rect;
857
858         if (drm_atomic_helper_damage_merged(old_state, state, &rect))
859                 repaper_fb_dirty(state->fb);
860
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;
866         }
867 }
868
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,
874 };
875
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,
880 };
881
882 static void repaper_release(struct drm_device *drm)
883 {
884         struct repaper_epd *epd = drm_to_epd(drm);
885
886         DRM_DEBUG_DRIVER("\n");
887
888         drm_mode_config_cleanup(drm);
889         drm_dev_fini(drm);
890         kfree(epd);
891 }
892
893 static const uint32_t repaper_formats[] = {
894         DRM_FORMAT_XRGB8888,
895 };
896
897 static const struct drm_display_mode repaper_e1144cs021_mode = {
898         DRM_SIMPLE_MODE(128, 96, 29, 22),
899 };
900
901 static const u8 repaper_e1144cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
902                                             0x00, 0x0f, 0xff, 0x00 };
903
904 static const struct drm_display_mode repaper_e1190cs021_mode = {
905         DRM_SIMPLE_MODE(144, 128, 36, 32),
906 };
907
908 static const u8 repaper_e1190cs021_cs[] = { 0x00, 0x00, 0x00, 0x03,
909                                             0xfc, 0x00, 0x00, 0xff };
910
911 static const struct drm_display_mode repaper_e2200cs021_mode = {
912         DRM_SIMPLE_MODE(200, 96, 46, 22),
913 };
914
915 static const u8 repaper_e2200cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
916                                             0x01, 0xff, 0xe0, 0x00 };
917
918 static const struct drm_display_mode repaper_e2271cs021_mode = {
919         DRM_SIMPLE_MODE(264, 176, 57, 38),
920 };
921
922 static const u8 repaper_e2271cs021_cs[] = { 0x00, 0x00, 0x00, 0x7f,
923                                             0xff, 0xfe, 0x00, 0x00 };
924
925 DEFINE_DRM_GEM_CMA_FOPS(repaper_fops);
926
927 static struct drm_driver repaper_driver = {
928         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
929                                   DRIVER_ATOMIC,
930         .fops                   = &repaper_fops,
931         .release                = repaper_release,
932         DRM_GEM_CMA_VMAP_DRIVER_OPS,
933         .name                   = "repaper",
934         .desc                   = "Pervasive Displays RePaper e-ink panels",
935         .date                   = "20170405",
936         .major                  = 1,
937         .minor                  = 0,
938 };
939
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 },
945         {},
946 };
947 MODULE_DEVICE_TABLE(of, repaper_of_match);
948
949 static const struct spi_device_id repaper_id[] = {
950         { "e1144cs021", E1144CS021 },
951         { "e1190cs021", E1190CS021 },
952         { "e2200cs021", E2200CS021 },
953         { "e2271cs021", E2271CS021 },
954         { },
955 };
956 MODULE_DEVICE_TABLE(spi, repaper_id);
957
958 static int repaper_probe(struct spi_device *spi)
959 {
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;
969         int ret;
970
971         match = of_match_device(repaper_of_match, dev);
972         if (match) {
973                 model = (enum repaper_model)match->data;
974         } else {
975                 spi_id = spi_get_device_id(spi);
976                 model = spi_id->driver_data;
977         }
978
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));
982                 if (ret) {
983                         dev_warn(dev, "Failed to set dma mask %d\n", ret);
984                         return ret;
985                 }
986         }
987
988         epd = kzalloc(sizeof(*epd), GFP_KERNEL);
989         if (!epd)
990                 return -ENOMEM;
991
992         drm = &epd->drm;
993
994         ret = devm_drm_dev_init(dev, drm, &repaper_driver);
995         if (ret) {
996                 kfree(epd);
997                 return ret;
998         }
999
1000         drm_mode_config_init(drm);
1001         drm->mode_config.funcs = &repaper_mode_config_funcs;
1002
1003         epd->spi = spi;
1004
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");
1010                 return ret;
1011         }
1012
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");
1018                 return ret;
1019         }
1020
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");
1026                 return ret;
1027         }
1028
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");
1034                 return ret;
1035         }
1036
1037         if (!device_property_read_string(dev, "pervasive,thermal-zone",
1038                                          &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);
1043                 }
1044         }
1045
1046         switch (model) {
1047         case E1144CS021:
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;
1055                 break;
1056
1057         case E1190CS021:
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;
1065                 break;
1066
1067         case E2200CS021:
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;
1075                 break;
1076
1077         case E2271CS021:
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");
1083                         return ret;
1084                 }
1085
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;
1093                 break;
1094
1095         default:
1096                 return -ENODEV;
1097         }
1098
1099         epd->width = mode->hdisplay;
1100         epd->height = mode->vdisplay;
1101         epd->factored_stage_time = epd->stage_time;
1102
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)
1106                 return -ENOMEM;
1107
1108         epd->current_frame = devm_kzalloc(dev, epd->width * epd->height / 8,
1109                                           GFP_KERNEL);
1110         if (!epd->current_frame)
1111                 return -ENOMEM;
1112
1113         ret = tinydrm_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
1114                                         DRM_MODE_CONNECTOR_VIRTUAL,
1115                                         repaper_formats,
1116                                         ARRAY_SIZE(repaper_formats), mode, 0);
1117         if (ret)
1118                 return ret;
1119
1120         drm_mode_config_reset(drm);
1121
1122         ret = drm_dev_register(drm, 0);
1123         if (ret)
1124                 return ret;
1125
1126         spi_set_drvdata(spi, drm);
1127
1128         DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
1129
1130         drm_fbdev_generic_setup(drm, 0);
1131
1132         return 0;
1133 }
1134
1135 static int repaper_remove(struct spi_device *spi)
1136 {
1137         struct drm_device *drm = spi_get_drvdata(spi);
1138
1139         drm_dev_unplug(drm);
1140         drm_atomic_helper_shutdown(drm);
1141
1142         return 0;
1143 }
1144
1145 static void repaper_shutdown(struct spi_device *spi)
1146 {
1147         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
1148 }
1149
1150 static struct spi_driver repaper_spi_driver = {
1151         .driver = {
1152                 .name = "repaper",
1153                 .owner = THIS_MODULE,
1154                 .of_match_table = repaper_of_match,
1155         },
1156         .id_table = repaper_id,
1157         .probe = repaper_probe,
1158         .remove = repaper_remove,
1159         .shutdown = repaper_shutdown,
1160 };
1161 module_spi_driver(repaper_spi_driver);
1162
1163 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1164 MODULE_AUTHOR("Noralf Trønnes");
1165 MODULE_LICENSE("GPL");