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