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