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