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