xref: /openbmc/linux/drivers/gpu/drm/tiny/repaper.c (revision 14474950)
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 enabled;
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 (!epd->enabled)
542 		return 0;
543 
544 	if (!drm_dev_enter(fb->dev, &idx))
545 		return -ENODEV;
546 
547 	/* repaper can't do partial updates */
548 	clip.x1 = 0;
549 	clip.x2 = fb->width;
550 	clip.y1 = 0;
551 	clip.y2 = fb->height;
552 
553 	repaper_get_temperature(epd);
554 
555 	DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
556 		  epd->factored_stage_time);
557 
558 	buf = kmalloc_array(fb->width, fb->height, GFP_KERNEL);
559 	if (!buf) {
560 		ret = -ENOMEM;
561 		goto out_exit;
562 	}
563 
564 	if (import_attach) {
565 		ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
566 					       DMA_FROM_DEVICE);
567 		if (ret)
568 			goto out_free;
569 	}
570 
571 	drm_fb_xrgb8888_to_gray8(buf, cma_obj->vaddr, fb, &clip);
572 
573 	if (import_attach) {
574 		ret = dma_buf_end_cpu_access(import_attach->dmabuf,
575 					     DMA_FROM_DEVICE);
576 		if (ret)
577 			goto out_free;
578 	}
579 
580 	repaper_gray8_to_mono_reversed(buf, fb->width, fb->height);
581 
582 	if (epd->partial) {
583 		repaper_frame_data_repeat(epd, buf, epd->current_frame,
584 					  REPAPER_NORMAL);
585 	} else if (epd->cleared) {
586 		repaper_frame_data_repeat(epd, epd->current_frame, NULL,
587 					  REPAPER_COMPENSATE);
588 		repaper_frame_data_repeat(epd, epd->current_frame, NULL,
589 					  REPAPER_WHITE);
590 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
591 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
592 
593 		epd->partial = true;
594 	} else {
595 		/* Clear display (anything -> white) */
596 		repaper_frame_fixed_repeat(epd, 0xff, REPAPER_COMPENSATE);
597 		repaper_frame_fixed_repeat(epd, 0xff, REPAPER_WHITE);
598 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_INVERSE);
599 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_NORMAL);
600 
601 		/* Assuming a clear (white) screen output an image */
602 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_COMPENSATE);
603 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_WHITE);
604 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
605 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
606 
607 		epd->cleared = true;
608 		epd->partial = true;
609 	}
610 
611 	memcpy(epd->current_frame, buf, fb->width * fb->height / 8);
612 
613 	/*
614 	 * An extra frame write is needed if pixels are set in the bottom line,
615 	 * or else grey lines rises up from the pixels
616 	 */
617 	if (epd->pre_border_byte) {
618 		unsigned int x;
619 
620 		for (x = 0; x < (fb->width / 8); x++)
621 			if (buf[x + (fb->width * (fb->height - 1) / 8)]) {
622 				repaper_frame_data_repeat(epd, buf,
623 							  epd->current_frame,
624 							  REPAPER_NORMAL);
625 				break;
626 			}
627 	}
628 
629 out_free:
630 	kfree(buf);
631 out_exit:
632 	drm_dev_exit(idx);
633 
634 	return ret;
635 }
636 
637 static void power_off(struct repaper_epd *epd)
638 {
639 	/* Turn off power and all signals */
640 	gpiod_set_value_cansleep(epd->reset, 0);
641 	gpiod_set_value_cansleep(epd->panel_on, 0);
642 	if (epd->border)
643 		gpiod_set_value_cansleep(epd->border, 0);
644 
645 	/* Ensure SPI MOSI and CLOCK are Low before CS Low */
646 	repaper_spi_mosi_low(epd->spi);
647 
648 	/* Discharge pulse */
649 	gpiod_set_value_cansleep(epd->discharge, 1);
650 	msleep(150);
651 	gpiod_set_value_cansleep(epd->discharge, 0);
652 }
653 
654 static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
655 				struct drm_crtc_state *crtc_state,
656 				struct drm_plane_state *plane_state)
657 {
658 	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
659 	struct spi_device *spi = epd->spi;
660 	struct device *dev = &spi->dev;
661 	bool dc_ok = false;
662 	int i, ret, idx;
663 
664 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
665 		return;
666 
667 	DRM_DEBUG_DRIVER("\n");
668 
669 	/* Power up sequence */
670 	gpiod_set_value_cansleep(epd->reset, 0);
671 	gpiod_set_value_cansleep(epd->panel_on, 0);
672 	gpiod_set_value_cansleep(epd->discharge, 0);
673 	if (epd->border)
674 		gpiod_set_value_cansleep(epd->border, 0);
675 	repaper_spi_mosi_low(spi);
676 	usleep_range(5000, 10000);
677 
678 	gpiod_set_value_cansleep(epd->panel_on, 1);
679 	/*
680 	 * This delay comes from the repaper.org userspace driver, it's not
681 	 * mentioned in the datasheet.
682 	 */
683 	usleep_range(10000, 15000);
684 	gpiod_set_value_cansleep(epd->reset, 1);
685 	if (epd->border)
686 		gpiod_set_value_cansleep(epd->border, 1);
687 	usleep_range(5000, 10000);
688 	gpiod_set_value_cansleep(epd->reset, 0);
689 	usleep_range(5000, 10000);
690 	gpiod_set_value_cansleep(epd->reset, 1);
691 	usleep_range(5000, 10000);
692 
693 	/* Wait for COG to become ready */
694 	for (i = 100; i > 0; i--) {
695 		if (!gpiod_get_value_cansleep(epd->busy))
696 			break;
697 
698 		usleep_range(10, 100);
699 	}
700 
701 	if (!i) {
702 		DRM_DEV_ERROR(dev, "timeout waiting for panel to become ready.\n");
703 		power_off(epd);
704 		goto out_exit;
705 	}
706 
707 	repaper_read_id(spi);
708 	ret = repaper_read_id(spi);
709 	if (ret != REPAPER_RID_G2_COG_ID) {
710 		if (ret < 0)
711 			dev_err(dev, "failed to read chip (%d)\n", ret);
712 		else
713 			dev_err(dev, "wrong COG ID 0x%02x\n", ret);
714 		power_off(epd);
715 		goto out_exit;
716 	}
717 
718 	/* Disable OE */
719 	repaper_write_val(spi, 0x02, 0x40);
720 
721 	ret = repaper_read_val(spi, 0x0f);
722 	if (ret < 0 || !(ret & 0x80)) {
723 		if (ret < 0)
724 			DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
725 		else
726 			DRM_DEV_ERROR(dev, "panel is reported broken\n");
727 		power_off(epd);
728 		goto out_exit;
729 	}
730 
731 	/* Power saving mode */
732 	repaper_write_val(spi, 0x0b, 0x02);
733 	/* Channel select */
734 	repaper_write_buf(spi, 0x01, epd->channel_select, 8);
735 	/* High power mode osc */
736 	repaper_write_val(spi, 0x07, 0xd1);
737 	/* Power setting */
738 	repaper_write_val(spi, 0x08, 0x02);
739 	/* Vcom level */
740 	repaper_write_val(spi, 0x09, 0xc2);
741 	/* Power setting */
742 	repaper_write_val(spi, 0x04, 0x03);
743 	/* Driver latch on */
744 	repaper_write_val(spi, 0x03, 0x01);
745 	/* Driver latch off */
746 	repaper_write_val(spi, 0x03, 0x00);
747 	usleep_range(5000, 10000);
748 
749 	/* Start chargepump */
750 	for (i = 0; i < 4; ++i) {
751 		/* Charge pump positive voltage on - VGH/VDL on */
752 		repaper_write_val(spi, 0x05, 0x01);
753 		msleep(240);
754 
755 		/* Charge pump negative voltage on - VGL/VDL on */
756 		repaper_write_val(spi, 0x05, 0x03);
757 		msleep(40);
758 
759 		/* Charge pump Vcom on - Vcom driver on */
760 		repaper_write_val(spi, 0x05, 0x0f);
761 		msleep(40);
762 
763 		/* check DC/DC */
764 		ret = repaper_read_val(spi, 0x0f);
765 		if (ret < 0) {
766 			DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
767 			power_off(epd);
768 			goto out_exit;
769 		}
770 
771 		if (ret & 0x40) {
772 			dc_ok = true;
773 			break;
774 		}
775 	}
776 
777 	if (!dc_ok) {
778 		DRM_DEV_ERROR(dev, "dc/dc failed\n");
779 		power_off(epd);
780 		goto out_exit;
781 	}
782 
783 	/*
784 	 * Output enable to disable
785 	 * The userspace driver sets this to 0x04, but the datasheet says 0x06
786 	 */
787 	repaper_write_val(spi, 0x02, 0x04);
788 
789 	epd->enabled = true;
790 	epd->partial = false;
791 out_exit:
792 	drm_dev_exit(idx);
793 }
794 
795 static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
796 {
797 	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
798 	struct spi_device *spi = epd->spi;
799 	unsigned int line;
800 
801 	/*
802 	 * This callback is not protected by drm_dev_enter/exit since we want to
803 	 * turn off the display on regular driver unload. It's highly unlikely
804 	 * that the underlying SPI controller is gone should this be called after
805 	 * unplug.
806 	 */
807 
808 	if (!epd->enabled)
809 		return;
810 
811 	DRM_DEBUG_DRIVER("\n");
812 
813 	epd->enabled = false;
814 
815 	/* Nothing frame */
816 	for (line = 0; line < epd->height; line++)
817 		repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
818 				 REPAPER_COMPENSATE);
819 
820 	/* 2.7" */
821 	if (epd->border) {
822 		/* Dummy line */
823 		repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
824 				 REPAPER_COMPENSATE);
825 		msleep(25);
826 		gpiod_set_value_cansleep(epd->border, 0);
827 		msleep(200);
828 		gpiod_set_value_cansleep(epd->border, 1);
829 	} else {
830 		/* Border dummy line */
831 		repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
832 				 REPAPER_NORMAL);
833 		msleep(200);
834 	}
835 
836 	/* not described in datasheet */
837 	repaper_write_val(spi, 0x0b, 0x00);
838 	/* Latch reset turn on */
839 	repaper_write_val(spi, 0x03, 0x01);
840 	/* Power off charge pump Vcom */
841 	repaper_write_val(spi, 0x05, 0x03);
842 	/* Power off charge pump neg voltage */
843 	repaper_write_val(spi, 0x05, 0x01);
844 	msleep(120);
845 	/* Discharge internal */
846 	repaper_write_val(spi, 0x04, 0x80);
847 	/* turn off all charge pumps */
848 	repaper_write_val(spi, 0x05, 0x00);
849 	/* Turn off osc */
850 	repaper_write_val(spi, 0x07, 0x01);
851 	msleep(50);
852 
853 	power_off(epd);
854 }
855 
856 static void repaper_pipe_update(struct drm_simple_display_pipe *pipe,
857 				struct drm_plane_state *old_state)
858 {
859 	struct drm_plane_state *state = pipe->plane.state;
860 	struct drm_rect rect;
861 
862 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
863 		repaper_fb_dirty(state->fb);
864 }
865 
866 static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
867 	.enable = repaper_pipe_enable,
868 	.disable = repaper_pipe_disable,
869 	.update = repaper_pipe_update,
870 	.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
871 };
872 
873 static int repaper_connector_get_modes(struct drm_connector *connector)
874 {
875 	struct repaper_epd *epd = drm_to_epd(connector->dev);
876 	struct drm_display_mode *mode;
877 
878 	mode = drm_mode_duplicate(connector->dev, epd->mode);
879 	if (!mode) {
880 		DRM_ERROR("Failed to duplicate mode\n");
881 		return 0;
882 	}
883 
884 	drm_mode_set_name(mode);
885 	mode->type |= DRM_MODE_TYPE_PREFERRED;
886 	drm_mode_probed_add(connector, mode);
887 
888 	connector->display_info.width_mm = mode->width_mm;
889 	connector->display_info.height_mm = mode->height_mm;
890 
891 	return 1;
892 }
893 
894 static const struct drm_connector_helper_funcs repaper_connector_hfuncs = {
895 	.get_modes = repaper_connector_get_modes,
896 };
897 
898 static const struct drm_connector_funcs repaper_connector_funcs = {
899 	.reset = drm_atomic_helper_connector_reset,
900 	.fill_modes = drm_helper_probe_single_connector_modes,
901 	.destroy = drm_connector_cleanup,
902 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
903 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
904 };
905 
906 static const struct drm_mode_config_funcs repaper_mode_config_funcs = {
907 	.fb_create = drm_gem_fb_create_with_dirty,
908 	.atomic_check = drm_atomic_helper_check,
909 	.atomic_commit = drm_atomic_helper_commit,
910 };
911 
912 static const uint32_t repaper_formats[] = {
913 	DRM_FORMAT_XRGB8888,
914 };
915 
916 static const struct drm_display_mode repaper_e1144cs021_mode = {
917 	DRM_SIMPLE_MODE(128, 96, 29, 22),
918 };
919 
920 static const u8 repaper_e1144cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
921 					    0x00, 0x0f, 0xff, 0x00 };
922 
923 static const struct drm_display_mode repaper_e1190cs021_mode = {
924 	DRM_SIMPLE_MODE(144, 128, 36, 32),
925 };
926 
927 static const u8 repaper_e1190cs021_cs[] = { 0x00, 0x00, 0x00, 0x03,
928 					    0xfc, 0x00, 0x00, 0xff };
929 
930 static const struct drm_display_mode repaper_e2200cs021_mode = {
931 	DRM_SIMPLE_MODE(200, 96, 46, 22),
932 };
933 
934 static const u8 repaper_e2200cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
935 					    0x01, 0xff, 0xe0, 0x00 };
936 
937 static const struct drm_display_mode repaper_e2271cs021_mode = {
938 	DRM_SIMPLE_MODE(264, 176, 57, 38),
939 };
940 
941 static const u8 repaper_e2271cs021_cs[] = { 0x00, 0x00, 0x00, 0x7f,
942 					    0xff, 0xfe, 0x00, 0x00 };
943 
944 DEFINE_DRM_GEM_CMA_FOPS(repaper_fops);
945 
946 static struct drm_driver repaper_driver = {
947 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
948 	.fops			= &repaper_fops,
949 	DRM_GEM_CMA_VMAP_DRIVER_OPS,
950 	.name			= "repaper",
951 	.desc			= "Pervasive Displays RePaper e-ink panels",
952 	.date			= "20170405",
953 	.major			= 1,
954 	.minor			= 0,
955 };
956 
957 static const struct of_device_id repaper_of_match[] = {
958 	{ .compatible = "pervasive,e1144cs021", .data = (void *)E1144CS021 },
959 	{ .compatible = "pervasive,e1190cs021", .data = (void *)E1190CS021 },
960 	{ .compatible = "pervasive,e2200cs021", .data = (void *)E2200CS021 },
961 	{ .compatible = "pervasive,e2271cs021", .data = (void *)E2271CS021 },
962 	{},
963 };
964 MODULE_DEVICE_TABLE(of, repaper_of_match);
965 
966 static const struct spi_device_id repaper_id[] = {
967 	{ "e1144cs021", E1144CS021 },
968 	{ "e1190cs021", E1190CS021 },
969 	{ "e2200cs021", E2200CS021 },
970 	{ "e2271cs021", E2271CS021 },
971 	{ },
972 };
973 MODULE_DEVICE_TABLE(spi, repaper_id);
974 
975 static int repaper_probe(struct spi_device *spi)
976 {
977 	const struct drm_display_mode *mode;
978 	const struct spi_device_id *spi_id;
979 	struct device *dev = &spi->dev;
980 	enum repaper_model model;
981 	const char *thermal_zone;
982 	struct repaper_epd *epd;
983 	size_t line_buffer_size;
984 	struct drm_device *drm;
985 	const void *match;
986 	int ret;
987 
988 	match = device_get_match_data(dev);
989 	if (match) {
990 		model = (enum repaper_model)match;
991 	} else {
992 		spi_id = spi_get_device_id(spi);
993 		model = (enum repaper_model)spi_id->driver_data;
994 	}
995 
996 	/* The SPI device is used to allocate dma memory */
997 	if (!dev->coherent_dma_mask) {
998 		ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
999 		if (ret) {
1000 			dev_warn(dev, "Failed to set dma mask %d\n", ret);
1001 			return ret;
1002 		}
1003 	}
1004 
1005 	epd = devm_drm_dev_alloc(dev, &repaper_driver,
1006 				 struct repaper_epd, drm);
1007 	if (IS_ERR(epd))
1008 		return PTR_ERR(epd);
1009 
1010 	drm = &epd->drm;
1011 
1012 	ret = drmm_mode_config_init(drm);
1013 	if (ret)
1014 		return ret;
1015 	drm->mode_config.funcs = &repaper_mode_config_funcs;
1016 
1017 	epd->spi = spi;
1018 
1019 	epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
1020 	if (IS_ERR(epd->panel_on)) {
1021 		ret = PTR_ERR(epd->panel_on);
1022 		if (ret != -EPROBE_DEFER)
1023 			DRM_DEV_ERROR(dev, "Failed to get gpio 'panel-on'\n");
1024 		return ret;
1025 	}
1026 
1027 	epd->discharge = devm_gpiod_get(dev, "discharge", GPIOD_OUT_LOW);
1028 	if (IS_ERR(epd->discharge)) {
1029 		ret = PTR_ERR(epd->discharge);
1030 		if (ret != -EPROBE_DEFER)
1031 			DRM_DEV_ERROR(dev, "Failed to get gpio 'discharge'\n");
1032 		return ret;
1033 	}
1034 
1035 	epd->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1036 	if (IS_ERR(epd->reset)) {
1037 		ret = PTR_ERR(epd->reset);
1038 		if (ret != -EPROBE_DEFER)
1039 			DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
1040 		return ret;
1041 	}
1042 
1043 	epd->busy = devm_gpiod_get(dev, "busy", GPIOD_IN);
1044 	if (IS_ERR(epd->busy)) {
1045 		ret = PTR_ERR(epd->busy);
1046 		if (ret != -EPROBE_DEFER)
1047 			DRM_DEV_ERROR(dev, "Failed to get gpio 'busy'\n");
1048 		return ret;
1049 	}
1050 
1051 	if (!device_property_read_string(dev, "pervasive,thermal-zone",
1052 					 &thermal_zone)) {
1053 		epd->thermal = thermal_zone_get_zone_by_name(thermal_zone);
1054 		if (IS_ERR(epd->thermal)) {
1055 			DRM_DEV_ERROR(dev, "Failed to get thermal zone: %s\n", thermal_zone);
1056 			return PTR_ERR(epd->thermal);
1057 		}
1058 	}
1059 
1060 	switch (model) {
1061 	case E1144CS021:
1062 		mode = &repaper_e1144cs021_mode;
1063 		epd->channel_select = repaper_e1144cs021_cs;
1064 		epd->stage_time = 480;
1065 		epd->bytes_per_scan = 96 / 4;
1066 		epd->middle_scan = true; /* data-scan-data */
1067 		epd->pre_border_byte = false;
1068 		epd->border_byte = REPAPER_BORDER_BYTE_ZERO;
1069 		break;
1070 
1071 	case E1190CS021:
1072 		mode = &repaper_e1190cs021_mode;
1073 		epd->channel_select = repaper_e1190cs021_cs;
1074 		epd->stage_time = 480;
1075 		epd->bytes_per_scan = 128 / 4 / 2;
1076 		epd->middle_scan = false; /* scan-data-scan */
1077 		epd->pre_border_byte = false;
1078 		epd->border_byte = REPAPER_BORDER_BYTE_SET;
1079 		break;
1080 
1081 	case E2200CS021:
1082 		mode = &repaper_e2200cs021_mode;
1083 		epd->channel_select = repaper_e2200cs021_cs;
1084 		epd->stage_time = 480;
1085 		epd->bytes_per_scan = 96 / 4;
1086 		epd->middle_scan = true; /* data-scan-data */
1087 		epd->pre_border_byte = true;
1088 		epd->border_byte = REPAPER_BORDER_BYTE_NONE;
1089 		break;
1090 
1091 	case E2271CS021:
1092 		epd->border = devm_gpiod_get(dev, "border", GPIOD_OUT_LOW);
1093 		if (IS_ERR(epd->border)) {
1094 			ret = PTR_ERR(epd->border);
1095 			if (ret != -EPROBE_DEFER)
1096 				DRM_DEV_ERROR(dev, "Failed to get gpio 'border'\n");
1097 			return ret;
1098 		}
1099 
1100 		mode = &repaper_e2271cs021_mode;
1101 		epd->channel_select = repaper_e2271cs021_cs;
1102 		epd->stage_time = 630;
1103 		epd->bytes_per_scan = 176 / 4;
1104 		epd->middle_scan = true; /* data-scan-data */
1105 		epd->pre_border_byte = true;
1106 		epd->border_byte = REPAPER_BORDER_BYTE_NONE;
1107 		break;
1108 
1109 	default:
1110 		return -ENODEV;
1111 	}
1112 
1113 	epd->mode = mode;
1114 	epd->width = mode->hdisplay;
1115 	epd->height = mode->vdisplay;
1116 	epd->factored_stage_time = epd->stage_time;
1117 
1118 	line_buffer_size = 2 * epd->width / 8 + epd->bytes_per_scan + 2;
1119 	epd->line_buffer = devm_kzalloc(dev, line_buffer_size, GFP_KERNEL);
1120 	if (!epd->line_buffer)
1121 		return -ENOMEM;
1122 
1123 	epd->current_frame = devm_kzalloc(dev, epd->width * epd->height / 8,
1124 					  GFP_KERNEL);
1125 	if (!epd->current_frame)
1126 		return -ENOMEM;
1127 
1128 	drm->mode_config.min_width = mode->hdisplay;
1129 	drm->mode_config.max_width = mode->hdisplay;
1130 	drm->mode_config.min_height = mode->vdisplay;
1131 	drm->mode_config.max_height = mode->vdisplay;
1132 
1133 	drm_connector_helper_add(&epd->connector, &repaper_connector_hfuncs);
1134 	ret = drm_connector_init(drm, &epd->connector, &repaper_connector_funcs,
1135 				 DRM_MODE_CONNECTOR_SPI);
1136 	if (ret)
1137 		return ret;
1138 
1139 	ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
1140 					   repaper_formats, ARRAY_SIZE(repaper_formats),
1141 					   NULL, &epd->connector);
1142 	if (ret)
1143 		return ret;
1144 
1145 	drm_mode_config_reset(drm);
1146 
1147 	ret = drm_dev_register(drm, 0);
1148 	if (ret)
1149 		return ret;
1150 
1151 	spi_set_drvdata(spi, drm);
1152 
1153 	DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
1154 
1155 	drm_fbdev_generic_setup(drm, 0);
1156 
1157 	return 0;
1158 }
1159 
1160 static int repaper_remove(struct spi_device *spi)
1161 {
1162 	struct drm_device *drm = spi_get_drvdata(spi);
1163 
1164 	drm_dev_unplug(drm);
1165 	drm_atomic_helper_shutdown(drm);
1166 
1167 	return 0;
1168 }
1169 
1170 static void repaper_shutdown(struct spi_device *spi)
1171 {
1172 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
1173 }
1174 
1175 static struct spi_driver repaper_spi_driver = {
1176 	.driver = {
1177 		.name = "repaper",
1178 		.of_match_table = repaper_of_match,
1179 	},
1180 	.id_table = repaper_id,
1181 	.probe = repaper_probe,
1182 	.remove = repaper_remove,
1183 	.shutdown = repaper_shutdown,
1184 };
1185 module_spi_driver(repaper_spi_driver);
1186 
1187 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1188 MODULE_AUTHOR("Noralf Trønnes");
1189 MODULE_LICENSE("GPL");
1190