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