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