xref: /linux-6.15/drivers/gpu/drm/tiny/repaper.c (revision 4d098000)
1043386a0SNoralf Trønnes // SPDX-License-Identifier: GPL-2.0-or-later
2043386a0SNoralf Trønnes /*
3043386a0SNoralf Trønnes  * DRM driver for Pervasive Displays RePaper branded e-ink panels
4043386a0SNoralf Trønnes  *
5043386a0SNoralf Trønnes  * Copyright 2013-2017 Pervasive Displays, Inc.
6043386a0SNoralf Trønnes  * Copyright 2017 Noralf Trønnes
7043386a0SNoralf Trønnes  *
8043386a0SNoralf Trønnes  * The driver supports:
9043386a0SNoralf Trønnes  * Material Film: Aurora Mb (V231)
10043386a0SNoralf Trønnes  * Driver IC: G2 (eTC)
11043386a0SNoralf Trønnes  *
12043386a0SNoralf Trønnes  * The controller code was taken from the userspace driver:
13043386a0SNoralf Trønnes  * https://github.com/repaper/gratis
14043386a0SNoralf Trønnes  */
15043386a0SNoralf Trønnes 
16043386a0SNoralf Trønnes #include <linux/delay.h>
17043386a0SNoralf Trønnes #include <linux/gpio/consumer.h>
18043386a0SNoralf Trønnes #include <linux/module.h>
19ffb42e64SAndy Shevchenko #include <linux/property.h>
20043386a0SNoralf Trønnes #include <linux/sched/clock.h>
21043386a0SNoralf Trønnes #include <linux/spi/spi.h>
22043386a0SNoralf Trønnes #include <linux/thermal.h>
23043386a0SNoralf Trønnes 
24b86711c6SThomas Zimmermann #include <drm/clients/drm_client_setup.h>
25043386a0SNoralf Trønnes #include <drm/drm_atomic_helper.h>
26043386a0SNoralf Trønnes #include <drm/drm_connector.h>
27043386a0SNoralf Trønnes #include <drm/drm_damage_helper.h>
28043386a0SNoralf Trønnes #include <drm/drm_drv.h>
296bcfe8eaSDanilo Krummrich #include <drm/drm_fb_dma_helper.h>
3001849b42SThomas Zimmermann #include <drm/drm_fbdev_dma.h>
31043386a0SNoralf Trønnes #include <drm/drm_format_helper.h>
32720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
33820c1707SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
344a83c26aSDanilo Krummrich #include <drm/drm_gem_dma_helper.h>
35043386a0SNoralf Trønnes #include <drm/drm_gem_framebuffer_helper.h>
36144a29fdSDaniel Vetter #include <drm/drm_managed.h>
37043386a0SNoralf Trønnes #include <drm/drm_modes.h>
38043386a0SNoralf Trønnes #include <drm/drm_rect.h>
39043386a0SNoralf Trønnes #include <drm/drm_probe_helper.h>
40043386a0SNoralf Trønnes #include <drm/drm_simple_kms_helper.h>
41043386a0SNoralf Trønnes 
42043386a0SNoralf Trønnes #define REPAPER_RID_G2_COG_ID	0x12
43043386a0SNoralf Trønnes 
44043386a0SNoralf Trønnes enum repaper_model {
45ffb42e64SAndy Shevchenko 	/* 0 is reserved to avoid clashing with NULL */
46043386a0SNoralf Trønnes 	E1144CS021 = 1,
47043386a0SNoralf Trønnes 	E1190CS021,
48043386a0SNoralf Trønnes 	E2200CS021,
49043386a0SNoralf Trønnes 	E2271CS021,
50043386a0SNoralf Trønnes };
51043386a0SNoralf Trønnes 
52043386a0SNoralf Trønnes enum repaper_stage {         /* Image pixel -> Display pixel */
53043386a0SNoralf Trønnes 	REPAPER_COMPENSATE,  /* B -> W, W -> B (Current Image) */
54043386a0SNoralf Trønnes 	REPAPER_WHITE,       /* B -> N, W -> W (Current Image) */
55043386a0SNoralf Trønnes 	REPAPER_INVERSE,     /* B -> N, W -> B (New Image) */
56043386a0SNoralf Trønnes 	REPAPER_NORMAL       /* B -> B, W -> W (New Image) */
57043386a0SNoralf Trønnes };
58043386a0SNoralf Trønnes 
59043386a0SNoralf Trønnes enum repaper_epd_border_byte {
60043386a0SNoralf Trønnes 	REPAPER_BORDER_BYTE_NONE,
61043386a0SNoralf Trønnes 	REPAPER_BORDER_BYTE_ZERO,
62043386a0SNoralf Trønnes 	REPAPER_BORDER_BYTE_SET,
63043386a0SNoralf Trønnes };
64043386a0SNoralf Trønnes 
65043386a0SNoralf Trønnes struct repaper_epd {
66043386a0SNoralf Trønnes 	struct drm_device drm;
67043386a0SNoralf Trønnes 	struct drm_simple_display_pipe pipe;
68043386a0SNoralf Trønnes 	const struct drm_display_mode *mode;
69043386a0SNoralf Trønnes 	struct drm_connector connector;
70043386a0SNoralf Trønnes 	struct spi_device *spi;
71043386a0SNoralf Trønnes 
72043386a0SNoralf Trønnes 	struct gpio_desc *panel_on;
73043386a0SNoralf Trønnes 	struct gpio_desc *border;
74043386a0SNoralf Trønnes 	struct gpio_desc *discharge;
75043386a0SNoralf Trønnes 	struct gpio_desc *reset;
76043386a0SNoralf Trønnes 	struct gpio_desc *busy;
77043386a0SNoralf Trønnes 
78043386a0SNoralf Trønnes 	struct thermal_zone_device *thermal;
79043386a0SNoralf Trønnes 
80043386a0SNoralf Trønnes 	unsigned int height;
81043386a0SNoralf Trønnes 	unsigned int width;
82043386a0SNoralf Trønnes 	unsigned int bytes_per_scan;
83043386a0SNoralf Trønnes 	const u8 *channel_select;
84043386a0SNoralf Trønnes 	unsigned int stage_time;
85043386a0SNoralf Trønnes 	unsigned int factored_stage_time;
86043386a0SNoralf Trønnes 	bool middle_scan;
87043386a0SNoralf Trønnes 	bool pre_border_byte;
88043386a0SNoralf Trønnes 	enum repaper_epd_border_byte border_byte;
89043386a0SNoralf Trønnes 
90043386a0SNoralf Trønnes 	u8 *line_buffer;
91043386a0SNoralf Trønnes 	void *current_frame;
92043386a0SNoralf Trønnes 
93043386a0SNoralf Trønnes 	bool cleared;
94043386a0SNoralf Trønnes 	bool partial;
95043386a0SNoralf Trønnes };
96043386a0SNoralf Trønnes 
drm_to_epd(struct drm_device * drm)97043386a0SNoralf Trønnes static inline struct repaper_epd *drm_to_epd(struct drm_device *drm)
98043386a0SNoralf Trønnes {
99043386a0SNoralf Trønnes 	return container_of(drm, struct repaper_epd, drm);
100043386a0SNoralf Trønnes }
101043386a0SNoralf Trønnes 
repaper_spi_transfer(struct spi_device * spi,u8 header,const void * tx,void * rx,size_t len)102043386a0SNoralf Trønnes static int repaper_spi_transfer(struct spi_device *spi, u8 header,
103043386a0SNoralf Trønnes 				const void *tx, void *rx, size_t len)
104043386a0SNoralf Trønnes {
105043386a0SNoralf Trønnes 	void *txbuf = NULL, *rxbuf = NULL;
106043386a0SNoralf Trønnes 	struct spi_transfer tr[2] = {};
107043386a0SNoralf Trønnes 	u8 *headerbuf;
108043386a0SNoralf Trønnes 	int ret;
109043386a0SNoralf Trønnes 
110043386a0SNoralf Trønnes 	headerbuf = kmalloc(1, GFP_KERNEL);
111043386a0SNoralf Trønnes 	if (!headerbuf)
112043386a0SNoralf Trønnes 		return -ENOMEM;
113043386a0SNoralf Trønnes 
114043386a0SNoralf Trønnes 	headerbuf[0] = header;
115043386a0SNoralf Trønnes 	tr[0].tx_buf = headerbuf;
116043386a0SNoralf Trønnes 	tr[0].len = 1;
117043386a0SNoralf Trønnes 
118043386a0SNoralf Trønnes 	/* Stack allocated tx? */
119043386a0SNoralf Trønnes 	if (tx && len <= 32) {
120043386a0SNoralf Trønnes 		txbuf = kmemdup(tx, len, GFP_KERNEL);
121043386a0SNoralf Trønnes 		if (!txbuf) {
122043386a0SNoralf Trønnes 			ret = -ENOMEM;
123043386a0SNoralf Trønnes 			goto out_free;
124043386a0SNoralf Trønnes 		}
125043386a0SNoralf Trønnes 	}
126043386a0SNoralf Trønnes 
127043386a0SNoralf Trønnes 	if (rx) {
128043386a0SNoralf Trønnes 		rxbuf = kmalloc(len, GFP_KERNEL);
129043386a0SNoralf Trønnes 		if (!rxbuf) {
130043386a0SNoralf Trønnes 			ret = -ENOMEM;
131043386a0SNoralf Trønnes 			goto out_free;
132043386a0SNoralf Trønnes 		}
133043386a0SNoralf Trønnes 	}
134043386a0SNoralf Trønnes 
135043386a0SNoralf Trønnes 	tr[1].tx_buf = txbuf ? txbuf : tx;
136043386a0SNoralf Trønnes 	tr[1].rx_buf = rxbuf;
137043386a0SNoralf Trønnes 	tr[1].len = len;
138043386a0SNoralf Trønnes 
139043386a0SNoralf Trønnes 	ndelay(80);
140043386a0SNoralf Trønnes 	ret = spi_sync_transfer(spi, tr, 2);
141043386a0SNoralf Trønnes 	if (rx && !ret)
142043386a0SNoralf Trønnes 		memcpy(rx, rxbuf, len);
143043386a0SNoralf Trønnes 
144043386a0SNoralf Trønnes out_free:
145043386a0SNoralf Trønnes 	kfree(headerbuf);
146043386a0SNoralf Trønnes 	kfree(txbuf);
147043386a0SNoralf Trønnes 	kfree(rxbuf);
148043386a0SNoralf Trønnes 
149043386a0SNoralf Trønnes 	return ret;
150043386a0SNoralf Trønnes }
151043386a0SNoralf Trønnes 
repaper_write_buf(struct spi_device * spi,u8 reg,const u8 * buf,size_t len)152043386a0SNoralf Trønnes static int repaper_write_buf(struct spi_device *spi, u8 reg,
153043386a0SNoralf Trønnes 			     const u8 *buf, size_t len)
154043386a0SNoralf Trønnes {
155043386a0SNoralf Trønnes 	int ret;
156043386a0SNoralf Trønnes 
157043386a0SNoralf Trønnes 	ret = repaper_spi_transfer(spi, 0x70, &reg, NULL, 1);
158043386a0SNoralf Trønnes 	if (ret)
159043386a0SNoralf Trønnes 		return ret;
160043386a0SNoralf Trønnes 
161043386a0SNoralf Trønnes 	return repaper_spi_transfer(spi, 0x72, buf, NULL, len);
162043386a0SNoralf Trønnes }
163043386a0SNoralf Trønnes 
repaper_write_val(struct spi_device * spi,u8 reg,u8 val)164043386a0SNoralf Trønnes static int repaper_write_val(struct spi_device *spi, u8 reg, u8 val)
165043386a0SNoralf Trønnes {
166043386a0SNoralf Trønnes 	return repaper_write_buf(spi, reg, &val, 1);
167043386a0SNoralf Trønnes }
168043386a0SNoralf Trønnes 
repaper_read_val(struct spi_device * spi,u8 reg)169043386a0SNoralf Trønnes static int repaper_read_val(struct spi_device *spi, u8 reg)
170043386a0SNoralf Trønnes {
171043386a0SNoralf Trønnes 	int ret;
172043386a0SNoralf Trønnes 	u8 val;
173043386a0SNoralf Trønnes 
174043386a0SNoralf Trønnes 	ret = repaper_spi_transfer(spi, 0x70, &reg, NULL, 1);
175043386a0SNoralf Trønnes 	if (ret)
176043386a0SNoralf Trønnes 		return ret;
177043386a0SNoralf Trønnes 
178043386a0SNoralf Trønnes 	ret = repaper_spi_transfer(spi, 0x73, NULL, &val, 1);
179043386a0SNoralf Trønnes 
180043386a0SNoralf Trønnes 	return ret ? ret : val;
181043386a0SNoralf Trønnes }
182043386a0SNoralf Trønnes 
repaper_read_id(struct spi_device * spi)183043386a0SNoralf Trønnes static int repaper_read_id(struct spi_device *spi)
184043386a0SNoralf Trønnes {
185043386a0SNoralf Trønnes 	int ret;
186043386a0SNoralf Trønnes 	u8 id;
187043386a0SNoralf Trønnes 
188043386a0SNoralf Trønnes 	ret = repaper_spi_transfer(spi, 0x71, NULL, &id, 1);
189043386a0SNoralf Trønnes 
190043386a0SNoralf Trønnes 	return ret ? ret : id;
191043386a0SNoralf Trønnes }
192043386a0SNoralf Trønnes 
repaper_spi_mosi_low(struct spi_device * spi)193043386a0SNoralf Trønnes static void repaper_spi_mosi_low(struct spi_device *spi)
194043386a0SNoralf Trønnes {
195043386a0SNoralf Trønnes 	const u8 buf[1] = { 0 };
196043386a0SNoralf Trønnes 
197043386a0SNoralf Trønnes 	spi_write(spi, buf, 1);
198043386a0SNoralf Trønnes }
199043386a0SNoralf Trønnes 
200043386a0SNoralf Trønnes /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */
repaper_even_pixels(struct repaper_epd * epd,u8 ** pp,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)201043386a0SNoralf Trønnes static void repaper_even_pixels(struct repaper_epd *epd, u8 **pp,
202043386a0SNoralf Trønnes 				const u8 *data, u8 fixed_value, const u8 *mask,
203043386a0SNoralf Trønnes 				enum repaper_stage stage)
204043386a0SNoralf Trønnes {
205043386a0SNoralf Trønnes 	unsigned int b;
206043386a0SNoralf Trønnes 
207043386a0SNoralf Trønnes 	for (b = 0; b < (epd->width / 8); b++) {
208043386a0SNoralf Trønnes 		if (data) {
209043386a0SNoralf Trønnes 			u8 pixels = data[b] & 0xaa;
210043386a0SNoralf Trønnes 			u8 pixel_mask = 0xff;
211043386a0SNoralf Trønnes 			u8 p1, p2, p3, p4;
212043386a0SNoralf Trønnes 
213043386a0SNoralf Trønnes 			if (mask) {
214043386a0SNoralf Trønnes 				pixel_mask = (mask[b] ^ pixels) & 0xaa;
215043386a0SNoralf Trønnes 				pixel_mask |= pixel_mask >> 1;
216043386a0SNoralf Trønnes 			}
217043386a0SNoralf Trønnes 
218043386a0SNoralf Trønnes 			switch (stage) {
219043386a0SNoralf Trønnes 			case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
220043386a0SNoralf Trønnes 				pixels = 0xaa | ((pixels ^ 0xaa) >> 1);
221043386a0SNoralf Trønnes 				break;
222043386a0SNoralf Trønnes 			case REPAPER_WHITE:      /* B -> N, W -> W (Current) */
223043386a0SNoralf Trønnes 				pixels = 0x55 + ((pixels ^ 0xaa) >> 1);
224043386a0SNoralf Trønnes 				break;
225043386a0SNoralf Trønnes 			case REPAPER_INVERSE:    /* B -> N, W -> B (New) */
226043386a0SNoralf Trønnes 				pixels = 0x55 | (pixels ^ 0xaa);
227043386a0SNoralf Trønnes 				break;
228043386a0SNoralf Trønnes 			case REPAPER_NORMAL:     /* B -> B, W -> W (New) */
229043386a0SNoralf Trønnes 				pixels = 0xaa | (pixels >> 1);
230043386a0SNoralf Trønnes 				break;
231043386a0SNoralf Trønnes 			}
232043386a0SNoralf Trønnes 
233043386a0SNoralf Trønnes 			pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55);
234043386a0SNoralf Trønnes 			p1 = (pixels >> 6) & 0x03;
235043386a0SNoralf Trønnes 			p2 = (pixels >> 4) & 0x03;
236043386a0SNoralf Trønnes 			p3 = (pixels >> 2) & 0x03;
237043386a0SNoralf Trønnes 			p4 = (pixels >> 0) & 0x03;
238043386a0SNoralf Trønnes 			pixels = (p1 << 0) | (p2 << 2) | (p3 << 4) | (p4 << 6);
239043386a0SNoralf Trønnes 			*(*pp)++ = pixels;
240043386a0SNoralf Trønnes 		} else {
241043386a0SNoralf Trønnes 			*(*pp)++ = fixed_value;
242043386a0SNoralf Trønnes 		}
243043386a0SNoralf Trønnes 	}
244043386a0SNoralf Trønnes }
245043386a0SNoralf Trønnes 
246043386a0SNoralf Trønnes /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */
repaper_odd_pixels(struct repaper_epd * epd,u8 ** pp,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)247043386a0SNoralf Trønnes static void repaper_odd_pixels(struct repaper_epd *epd, u8 **pp,
248043386a0SNoralf Trønnes 			       const u8 *data, u8 fixed_value, const u8 *mask,
249043386a0SNoralf Trønnes 			       enum repaper_stage stage)
250043386a0SNoralf Trønnes {
251043386a0SNoralf Trønnes 	unsigned int b;
252043386a0SNoralf Trønnes 
253043386a0SNoralf Trønnes 	for (b = epd->width / 8; b > 0; b--) {
254043386a0SNoralf Trønnes 		if (data) {
255043386a0SNoralf Trønnes 			u8 pixels = data[b - 1] & 0x55;
256043386a0SNoralf Trønnes 			u8 pixel_mask = 0xff;
257043386a0SNoralf Trønnes 
258043386a0SNoralf Trønnes 			if (mask) {
259043386a0SNoralf Trønnes 				pixel_mask = (mask[b - 1] ^ pixels) & 0x55;
260043386a0SNoralf Trønnes 				pixel_mask |= pixel_mask << 1;
261043386a0SNoralf Trønnes 			}
262043386a0SNoralf Trønnes 
263043386a0SNoralf Trønnes 			switch (stage) {
264043386a0SNoralf Trønnes 			case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
265043386a0SNoralf Trønnes 				pixels = 0xaa | (pixels ^ 0x55);
266043386a0SNoralf Trønnes 				break;
267043386a0SNoralf Trønnes 			case REPAPER_WHITE:      /* B -> N, W -> W (Current) */
268043386a0SNoralf Trønnes 				pixels = 0x55 + (pixels ^ 0x55);
269043386a0SNoralf Trønnes 				break;
270043386a0SNoralf Trønnes 			case REPAPER_INVERSE:    /* B -> N, W -> B (New) */
271043386a0SNoralf Trønnes 				pixels = 0x55 | ((pixels ^ 0x55) << 1);
272043386a0SNoralf Trønnes 				break;
273043386a0SNoralf Trønnes 			case REPAPER_NORMAL:     /* B -> B, W -> W (New) */
274043386a0SNoralf Trønnes 				pixels = 0xaa | pixels;
275043386a0SNoralf Trønnes 				break;
276043386a0SNoralf Trønnes 			}
277043386a0SNoralf Trønnes 
278043386a0SNoralf Trønnes 			pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55);
279043386a0SNoralf Trønnes 			*(*pp)++ = pixels;
280043386a0SNoralf Trønnes 		} else {
281043386a0SNoralf Trønnes 			*(*pp)++ = fixed_value;
282043386a0SNoralf Trønnes 		}
283043386a0SNoralf Trønnes 	}
284043386a0SNoralf Trønnes }
285043386a0SNoralf Trønnes 
286043386a0SNoralf Trønnes /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
repaper_interleave_bits(u16 value)287043386a0SNoralf Trønnes static inline u16 repaper_interleave_bits(u16 value)
288043386a0SNoralf Trønnes {
289043386a0SNoralf Trønnes 	value = (value | (value << 4)) & 0x0f0f;
290043386a0SNoralf Trønnes 	value = (value | (value << 2)) & 0x3333;
291043386a0SNoralf Trønnes 	value = (value | (value << 1)) & 0x5555;
292043386a0SNoralf Trønnes 
293043386a0SNoralf Trønnes 	return value;
294043386a0SNoralf Trønnes }
295043386a0SNoralf Trønnes 
296043386a0SNoralf Trønnes /* pixels on display are numbered from 1 */
repaper_all_pixels(struct repaper_epd * epd,u8 ** pp,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)297043386a0SNoralf Trønnes static void repaper_all_pixels(struct repaper_epd *epd, u8 **pp,
298043386a0SNoralf Trønnes 			       const u8 *data, u8 fixed_value, const u8 *mask,
299043386a0SNoralf Trønnes 			       enum repaper_stage stage)
300043386a0SNoralf Trønnes {
301043386a0SNoralf Trønnes 	unsigned int b;
302043386a0SNoralf Trønnes 
303043386a0SNoralf Trønnes 	for (b = epd->width / 8; b > 0; b--) {
304043386a0SNoralf Trønnes 		if (data) {
305043386a0SNoralf Trønnes 			u16 pixels = repaper_interleave_bits(data[b - 1]);
306043386a0SNoralf Trønnes 			u16 pixel_mask = 0xffff;
307043386a0SNoralf Trønnes 
308043386a0SNoralf Trønnes 			if (mask) {
309043386a0SNoralf Trønnes 				pixel_mask = repaper_interleave_bits(mask[b - 1]);
310043386a0SNoralf Trønnes 
311043386a0SNoralf Trønnes 				pixel_mask = (pixel_mask ^ pixels) & 0x5555;
312043386a0SNoralf Trønnes 				pixel_mask |= pixel_mask << 1;
313043386a0SNoralf Trønnes 			}
314043386a0SNoralf Trønnes 
315043386a0SNoralf Trønnes 			switch (stage) {
316043386a0SNoralf Trønnes 			case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
317043386a0SNoralf Trønnes 				pixels = 0xaaaa | (pixels ^ 0x5555);
318043386a0SNoralf Trønnes 				break;
319043386a0SNoralf Trønnes 			case REPAPER_WHITE:      /* B -> N, W -> W (Current) */
320043386a0SNoralf Trønnes 				pixels = 0x5555 + (pixels ^ 0x5555);
321043386a0SNoralf Trønnes 				break;
322043386a0SNoralf Trønnes 			case REPAPER_INVERSE:    /* B -> N, W -> B (New) */
323043386a0SNoralf Trønnes 				pixels = 0x5555 | ((pixels ^ 0x5555) << 1);
324043386a0SNoralf Trønnes 				break;
325043386a0SNoralf Trønnes 			case REPAPER_NORMAL:     /* B -> B, W -> W (New) */
326043386a0SNoralf Trønnes 				pixels = 0xaaaa | pixels;
327043386a0SNoralf Trønnes 				break;
328043386a0SNoralf Trønnes 			}
329043386a0SNoralf Trønnes 
330043386a0SNoralf Trønnes 			pixels = (pixels & pixel_mask) | (~pixel_mask & 0x5555);
331043386a0SNoralf Trønnes 			*(*pp)++ = pixels >> 8;
332043386a0SNoralf Trønnes 			*(*pp)++ = pixels;
333043386a0SNoralf Trønnes 		} else {
334043386a0SNoralf Trønnes 			*(*pp)++ = fixed_value;
335043386a0SNoralf Trønnes 			*(*pp)++ = fixed_value;
336043386a0SNoralf Trønnes 		}
337043386a0SNoralf Trønnes 	}
338043386a0SNoralf Trønnes }
339043386a0SNoralf Trønnes 
340043386a0SNoralf Trønnes /* output one line of scan and data bytes to the display */
repaper_one_line(struct repaper_epd * epd,unsigned int line,const u8 * data,u8 fixed_value,const u8 * mask,enum repaper_stage stage)341043386a0SNoralf Trønnes static void repaper_one_line(struct repaper_epd *epd, unsigned int line,
342043386a0SNoralf Trønnes 			     const u8 *data, u8 fixed_value, const u8 *mask,
343043386a0SNoralf Trønnes 			     enum repaper_stage stage)
344043386a0SNoralf Trønnes {
345043386a0SNoralf Trønnes 	u8 *p = epd->line_buffer;
346043386a0SNoralf Trønnes 	unsigned int b;
347043386a0SNoralf Trønnes 
348043386a0SNoralf Trønnes 	repaper_spi_mosi_low(epd->spi);
349043386a0SNoralf Trønnes 
350043386a0SNoralf Trønnes 	if (epd->pre_border_byte)
351043386a0SNoralf Trønnes 		*p++ = 0x00;
352043386a0SNoralf Trønnes 
353043386a0SNoralf Trønnes 	if (epd->middle_scan) {
354043386a0SNoralf Trønnes 		/* data bytes */
355043386a0SNoralf Trønnes 		repaper_odd_pixels(epd, &p, data, fixed_value, mask, stage);
356043386a0SNoralf Trønnes 
357043386a0SNoralf Trønnes 		/* scan line */
358043386a0SNoralf Trønnes 		for (b = epd->bytes_per_scan; b > 0; b--) {
359043386a0SNoralf Trønnes 			if (line / 4 == b - 1)
360043386a0SNoralf Trønnes 				*p++ = 0x03 << (2 * (line & 0x03));
361043386a0SNoralf Trønnes 			else
362043386a0SNoralf Trønnes 				*p++ = 0x00;
363043386a0SNoralf Trønnes 		}
364043386a0SNoralf Trønnes 
365043386a0SNoralf Trønnes 		/* data bytes */
366043386a0SNoralf Trønnes 		repaper_even_pixels(epd, &p, data, fixed_value, mask, stage);
367043386a0SNoralf Trønnes 	} else {
368043386a0SNoralf Trønnes 		/*
369043386a0SNoralf Trønnes 		 * even scan line, but as lines on display are numbered from 1,
370043386a0SNoralf Trønnes 		 * line: 1,3,5,...
371043386a0SNoralf Trønnes 		 */
372043386a0SNoralf Trønnes 		for (b = 0; b < epd->bytes_per_scan; b++) {
373043386a0SNoralf Trønnes 			if (0 != (line & 0x01) && line / 8 == b)
374043386a0SNoralf Trønnes 				*p++ = 0xc0 >> (line & 0x06);
375043386a0SNoralf Trønnes 			else
376043386a0SNoralf Trønnes 				*p++ = 0x00;
377043386a0SNoralf Trønnes 		}
378043386a0SNoralf Trønnes 
379043386a0SNoralf Trønnes 		/* data bytes */
380043386a0SNoralf Trønnes 		repaper_all_pixels(epd, &p, data, fixed_value, mask, stage);
381043386a0SNoralf Trønnes 
382043386a0SNoralf Trønnes 		/*
383043386a0SNoralf Trønnes 		 * odd scan line, but as lines on display are numbered from 1,
384043386a0SNoralf Trønnes 		 * line: 0,2,4,6,...
385043386a0SNoralf Trønnes 		 */
386043386a0SNoralf Trønnes 		for (b = epd->bytes_per_scan; b > 0; b--) {
387043386a0SNoralf Trønnes 			if (0 == (line & 0x01) && line / 8 == b - 1)
388043386a0SNoralf Trønnes 				*p++ = 0x03 << (line & 0x06);
389043386a0SNoralf Trønnes 			else
390043386a0SNoralf Trønnes 				*p++ = 0x00;
391043386a0SNoralf Trønnes 		}
392043386a0SNoralf Trønnes 	}
393043386a0SNoralf Trønnes 
394043386a0SNoralf Trønnes 	switch (epd->border_byte) {
395043386a0SNoralf Trønnes 	case REPAPER_BORDER_BYTE_NONE:
396043386a0SNoralf Trønnes 		break;
397043386a0SNoralf Trønnes 
398043386a0SNoralf Trønnes 	case REPAPER_BORDER_BYTE_ZERO:
399043386a0SNoralf Trønnes 		*p++ = 0x00;
400043386a0SNoralf Trønnes 		break;
401043386a0SNoralf Trønnes 
402043386a0SNoralf Trønnes 	case REPAPER_BORDER_BYTE_SET:
403043386a0SNoralf Trønnes 		switch (stage) {
404043386a0SNoralf Trønnes 		case REPAPER_COMPENSATE:
405043386a0SNoralf Trønnes 		case REPAPER_WHITE:
406043386a0SNoralf Trønnes 		case REPAPER_INVERSE:
407043386a0SNoralf Trønnes 			*p++ = 0x00;
408043386a0SNoralf Trønnes 			break;
409043386a0SNoralf Trønnes 		case REPAPER_NORMAL:
410043386a0SNoralf Trønnes 			*p++ = 0xaa;
411043386a0SNoralf Trønnes 			break;
412043386a0SNoralf Trønnes 		}
413043386a0SNoralf Trønnes 		break;
414043386a0SNoralf Trønnes 	}
415043386a0SNoralf Trønnes 
416043386a0SNoralf Trønnes 	repaper_write_buf(epd->spi, 0x0a, epd->line_buffer,
417043386a0SNoralf Trønnes 			  p - epd->line_buffer);
418043386a0SNoralf Trønnes 
419043386a0SNoralf Trønnes 	/* Output data to panel */
420043386a0SNoralf Trønnes 	repaper_write_val(epd->spi, 0x02, 0x07);
421043386a0SNoralf Trønnes 
422043386a0SNoralf Trønnes 	repaper_spi_mosi_low(epd->spi);
423043386a0SNoralf Trønnes }
424043386a0SNoralf Trønnes 
repaper_frame_fixed(struct repaper_epd * epd,u8 fixed_value,enum repaper_stage stage)425043386a0SNoralf Trønnes static void repaper_frame_fixed(struct repaper_epd *epd, u8 fixed_value,
426043386a0SNoralf Trønnes 				enum repaper_stage stage)
427043386a0SNoralf Trønnes {
428043386a0SNoralf Trønnes 	unsigned int line;
429043386a0SNoralf Trønnes 
430043386a0SNoralf Trønnes 	for (line = 0; line < epd->height; line++)
431043386a0SNoralf Trønnes 		repaper_one_line(epd, line, NULL, fixed_value, NULL, stage);
432043386a0SNoralf Trønnes }
433043386a0SNoralf Trønnes 
repaper_frame_data(struct repaper_epd * epd,const u8 * image,const u8 * mask,enum repaper_stage stage)434043386a0SNoralf Trønnes static void repaper_frame_data(struct repaper_epd *epd, const u8 *image,
435043386a0SNoralf Trønnes 			       const u8 *mask, enum repaper_stage stage)
436043386a0SNoralf Trønnes {
437043386a0SNoralf Trønnes 	unsigned int line;
438043386a0SNoralf Trønnes 
439043386a0SNoralf Trønnes 	if (!mask) {
440043386a0SNoralf Trønnes 		for (line = 0; line < epd->height; line++) {
441043386a0SNoralf Trønnes 			repaper_one_line(epd, line,
442043386a0SNoralf Trønnes 					 &image[line * (epd->width / 8)],
443043386a0SNoralf Trønnes 					 0, NULL, stage);
444043386a0SNoralf Trønnes 		}
445043386a0SNoralf Trønnes 	} else {
446043386a0SNoralf Trønnes 		for (line = 0; line < epd->height; line++) {
447043386a0SNoralf Trønnes 			size_t n = line * epd->width / 8;
448043386a0SNoralf Trønnes 
449043386a0SNoralf Trønnes 			repaper_one_line(epd, line, &image[n], 0, &mask[n],
450043386a0SNoralf Trønnes 					 stage);
451043386a0SNoralf Trønnes 		}
452043386a0SNoralf Trønnes 	}
453043386a0SNoralf Trønnes }
454043386a0SNoralf Trønnes 
repaper_frame_fixed_repeat(struct repaper_epd * epd,u8 fixed_value,enum repaper_stage stage)455043386a0SNoralf Trønnes static void repaper_frame_fixed_repeat(struct repaper_epd *epd, u8 fixed_value,
456043386a0SNoralf Trønnes 				       enum repaper_stage stage)
457043386a0SNoralf Trønnes {
458043386a0SNoralf Trønnes 	u64 start = local_clock();
459*4d098000SNikita Zhandarovich 	u64 end = start + ((u64)epd->factored_stage_time * 1000 * 1000);
460043386a0SNoralf Trønnes 
461043386a0SNoralf Trønnes 	do {
462043386a0SNoralf Trønnes 		repaper_frame_fixed(epd, fixed_value, stage);
463043386a0SNoralf Trønnes 	} while (local_clock() < end);
464043386a0SNoralf Trønnes }
465043386a0SNoralf Trønnes 
repaper_frame_data_repeat(struct repaper_epd * epd,const u8 * image,const u8 * mask,enum repaper_stage stage)466043386a0SNoralf Trønnes static void repaper_frame_data_repeat(struct repaper_epd *epd, const u8 *image,
467043386a0SNoralf Trønnes 				      const u8 *mask, enum repaper_stage stage)
468043386a0SNoralf Trønnes {
469043386a0SNoralf Trønnes 	u64 start = local_clock();
470*4d098000SNikita Zhandarovich 	u64 end = start + ((u64)epd->factored_stage_time * 1000 * 1000);
471043386a0SNoralf Trønnes 
472043386a0SNoralf Trønnes 	do {
473043386a0SNoralf Trønnes 		repaper_frame_data(epd, image, mask, stage);
474043386a0SNoralf Trønnes 	} while (local_clock() < end);
475043386a0SNoralf Trønnes }
476043386a0SNoralf Trønnes 
repaper_get_temperature(struct repaper_epd * epd)477043386a0SNoralf Trønnes static void repaper_get_temperature(struct repaper_epd *epd)
478043386a0SNoralf Trønnes {
479043386a0SNoralf Trønnes 	int ret, temperature = 0;
480043386a0SNoralf Trønnes 	unsigned int factor10x;
481043386a0SNoralf Trønnes 
482043386a0SNoralf Trønnes 	if (!epd->thermal)
483043386a0SNoralf Trønnes 		return;
484043386a0SNoralf Trønnes 
485043386a0SNoralf Trønnes 	ret = thermal_zone_get_temp(epd->thermal, &temperature);
486043386a0SNoralf Trønnes 	if (ret) {
487043386a0SNoralf Trønnes 		DRM_DEV_ERROR(&epd->spi->dev, "Failed to get temperature (%d)\n", ret);
488043386a0SNoralf Trønnes 		return;
489043386a0SNoralf Trønnes 	}
490043386a0SNoralf Trønnes 
491043386a0SNoralf Trønnes 	temperature /= 1000;
492043386a0SNoralf Trønnes 
493043386a0SNoralf Trønnes 	if (temperature <= -10)
494043386a0SNoralf Trønnes 		factor10x = 170;
495043386a0SNoralf Trønnes 	else if (temperature <= -5)
496043386a0SNoralf Trønnes 		factor10x = 120;
497043386a0SNoralf Trønnes 	else if (temperature <= 5)
498043386a0SNoralf Trønnes 		factor10x = 80;
499043386a0SNoralf Trønnes 	else if (temperature <= 10)
500043386a0SNoralf Trønnes 		factor10x = 40;
501043386a0SNoralf Trønnes 	else if (temperature <= 15)
502043386a0SNoralf Trønnes 		factor10x = 30;
503043386a0SNoralf Trønnes 	else if (temperature <= 20)
504043386a0SNoralf Trønnes 		factor10x = 20;
505043386a0SNoralf Trønnes 	else if (temperature <= 40)
506043386a0SNoralf Trønnes 		factor10x = 10;
507043386a0SNoralf Trønnes 	else
508043386a0SNoralf Trønnes 		factor10x = 7;
509043386a0SNoralf Trønnes 
510043386a0SNoralf Trønnes 	epd->factored_stage_time = epd->stage_time * factor10x / 10;
511043386a0SNoralf Trønnes }
512043386a0SNoralf Trønnes 
repaper_fb_dirty(struct drm_framebuffer * fb,struct drm_format_conv_state * fmtcnv_state)5134cd24d4bSThomas Zimmermann static int repaper_fb_dirty(struct drm_framebuffer *fb,
5144cd24d4bSThomas Zimmermann 			    struct drm_format_conv_state *fmtcnv_state)
515043386a0SNoralf Trønnes {
5164a83c26aSDanilo Krummrich 	struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
517043386a0SNoralf Trønnes 	struct repaper_epd *epd = drm_to_epd(fb->dev);
518b3aca563SThomas Zimmermann 	unsigned int dst_pitch = 0;
519b3aca563SThomas Zimmermann 	struct iosys_map dst, vmap;
520043386a0SNoralf Trønnes 	struct drm_rect clip;
521043386a0SNoralf Trønnes 	int idx, ret = 0;
522043386a0SNoralf Trønnes 	u8 *buf = NULL;
523043386a0SNoralf Trønnes 
524043386a0SNoralf Trønnes 	if (!drm_dev_enter(fb->dev, &idx))
525043386a0SNoralf Trønnes 		return -ENODEV;
526043386a0SNoralf Trønnes 
527043386a0SNoralf Trønnes 	/* repaper can't do partial updates */
528043386a0SNoralf Trønnes 	clip.x1 = 0;
529043386a0SNoralf Trønnes 	clip.x2 = fb->width;
530043386a0SNoralf Trønnes 	clip.y1 = 0;
531043386a0SNoralf Trønnes 	clip.y2 = fb->height;
532043386a0SNoralf Trønnes 
533043386a0SNoralf Trønnes 	repaper_get_temperature(epd);
534043386a0SNoralf Trønnes 
535043386a0SNoralf Trønnes 	DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
536043386a0SNoralf Trønnes 		  epd->factored_stage_time);
537043386a0SNoralf Trønnes 
538fedf429eSGeert Uytterhoeven 	buf = kmalloc(fb->width * fb->height / 8, GFP_KERNEL);
539043386a0SNoralf Trønnes 	if (!buf) {
540043386a0SNoralf Trønnes 		ret = -ENOMEM;
541043386a0SNoralf Trønnes 		goto out_exit;
542043386a0SNoralf Trønnes 	}
543043386a0SNoralf Trønnes 
544baf6c24bSThomas Zimmermann 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
545043386a0SNoralf Trønnes 	if (ret)
546043386a0SNoralf Trønnes 		goto out_free;
547043386a0SNoralf Trønnes 
548b3aca563SThomas Zimmermann 	iosys_map_set_vaddr(&dst, buf);
549b3aca563SThomas Zimmermann 	iosys_map_set_vaddr(&vmap, dma_obj->vaddr);
5504cd24d4bSThomas Zimmermann 	drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, &vmap, fb, &clip, fmtcnv_state);
551043386a0SNoralf Trønnes 
552baf6c24bSThomas Zimmermann 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
553043386a0SNoralf Trønnes 
554043386a0SNoralf Trønnes 	if (epd->partial) {
555043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, buf, epd->current_frame,
556043386a0SNoralf Trønnes 					  REPAPER_NORMAL);
557043386a0SNoralf Trønnes 	} else if (epd->cleared) {
558043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, epd->current_frame, NULL,
559043386a0SNoralf Trønnes 					  REPAPER_COMPENSATE);
560043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, epd->current_frame, NULL,
561043386a0SNoralf Trønnes 					  REPAPER_WHITE);
562043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
563043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
564043386a0SNoralf Trønnes 
565043386a0SNoralf Trønnes 		epd->partial = true;
566043386a0SNoralf Trønnes 	} else {
567043386a0SNoralf Trønnes 		/* Clear display (anything -> white) */
568043386a0SNoralf Trønnes 		repaper_frame_fixed_repeat(epd, 0xff, REPAPER_COMPENSATE);
569043386a0SNoralf Trønnes 		repaper_frame_fixed_repeat(epd, 0xff, REPAPER_WHITE);
570043386a0SNoralf Trønnes 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_INVERSE);
571043386a0SNoralf Trønnes 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_NORMAL);
572043386a0SNoralf Trønnes 
573043386a0SNoralf Trønnes 		/* Assuming a clear (white) screen output an image */
574043386a0SNoralf Trønnes 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_COMPENSATE);
575043386a0SNoralf Trønnes 		repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_WHITE);
576043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
577043386a0SNoralf Trønnes 		repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
578043386a0SNoralf Trønnes 
579043386a0SNoralf Trønnes 		epd->cleared = true;
580043386a0SNoralf Trønnes 		epd->partial = true;
581043386a0SNoralf Trønnes 	}
582043386a0SNoralf Trønnes 
583043386a0SNoralf Trønnes 	memcpy(epd->current_frame, buf, fb->width * fb->height / 8);
584043386a0SNoralf Trønnes 
585043386a0SNoralf Trønnes 	/*
586043386a0SNoralf Trønnes 	 * An extra frame write is needed if pixels are set in the bottom line,
587043386a0SNoralf Trønnes 	 * or else grey lines rises up from the pixels
588043386a0SNoralf Trønnes 	 */
589043386a0SNoralf Trønnes 	if (epd->pre_border_byte) {
590043386a0SNoralf Trønnes 		unsigned int x;
591043386a0SNoralf Trønnes 
592043386a0SNoralf Trønnes 		for (x = 0; x < (fb->width / 8); x++)
593043386a0SNoralf Trønnes 			if (buf[x + (fb->width * (fb->height - 1) / 8)]) {
594043386a0SNoralf Trønnes 				repaper_frame_data_repeat(epd, buf,
595043386a0SNoralf Trønnes 							  epd->current_frame,
596043386a0SNoralf Trønnes 							  REPAPER_NORMAL);
597043386a0SNoralf Trønnes 				break;
598043386a0SNoralf Trønnes 			}
599043386a0SNoralf Trønnes 	}
600043386a0SNoralf Trønnes 
601043386a0SNoralf Trønnes out_free:
602043386a0SNoralf Trønnes 	kfree(buf);
603043386a0SNoralf Trønnes out_exit:
604043386a0SNoralf Trønnes 	drm_dev_exit(idx);
605043386a0SNoralf Trønnes 
606043386a0SNoralf Trønnes 	return ret;
607043386a0SNoralf Trønnes }
608043386a0SNoralf Trønnes 
power_off(struct repaper_epd * epd)609043386a0SNoralf Trønnes static void power_off(struct repaper_epd *epd)
610043386a0SNoralf Trønnes {
611043386a0SNoralf Trønnes 	/* Turn off power and all signals */
612043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->reset, 0);
613043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->panel_on, 0);
614043386a0SNoralf Trønnes 	if (epd->border)
615043386a0SNoralf Trønnes 		gpiod_set_value_cansleep(epd->border, 0);
616043386a0SNoralf Trønnes 
617043386a0SNoralf Trønnes 	/* Ensure SPI MOSI and CLOCK are Low before CS Low */
618043386a0SNoralf Trønnes 	repaper_spi_mosi_low(epd->spi);
619043386a0SNoralf Trønnes 
620043386a0SNoralf Trønnes 	/* Discharge pulse */
621043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->discharge, 1);
622043386a0SNoralf Trønnes 	msleep(150);
623043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->discharge, 0);
624043386a0SNoralf Trønnes }
625043386a0SNoralf Trønnes 
repaper_pipe_mode_valid(struct drm_simple_display_pipe * pipe,const struct drm_display_mode * mode)626216b9bbaSThomas Zimmermann static enum drm_mode_status repaper_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
627216b9bbaSThomas Zimmermann 						    const struct drm_display_mode *mode)
628216b9bbaSThomas Zimmermann {
629216b9bbaSThomas Zimmermann 	struct drm_crtc *crtc = &pipe->crtc;
630216b9bbaSThomas Zimmermann 	struct repaper_epd *epd = drm_to_epd(crtc->dev);
631216b9bbaSThomas Zimmermann 
632216b9bbaSThomas Zimmermann 	return drm_crtc_helper_mode_valid_fixed(crtc, mode, epd->mode);
633216b9bbaSThomas Zimmermann }
634216b9bbaSThomas Zimmermann 
repaper_pipe_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)635043386a0SNoralf Trønnes static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
636043386a0SNoralf Trønnes 				struct drm_crtc_state *crtc_state,
637043386a0SNoralf Trønnes 				struct drm_plane_state *plane_state)
638043386a0SNoralf Trønnes {
639043386a0SNoralf Trønnes 	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
640043386a0SNoralf Trønnes 	struct spi_device *spi = epd->spi;
641043386a0SNoralf Trønnes 	struct device *dev = &spi->dev;
642043386a0SNoralf Trønnes 	bool dc_ok = false;
643043386a0SNoralf Trønnes 	int i, ret, idx;
644043386a0SNoralf Trønnes 
645043386a0SNoralf Trønnes 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
646043386a0SNoralf Trønnes 		return;
647043386a0SNoralf Trønnes 
648043386a0SNoralf Trønnes 	DRM_DEBUG_DRIVER("\n");
649043386a0SNoralf Trønnes 
650043386a0SNoralf Trønnes 	/* Power up sequence */
651043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->reset, 0);
652043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->panel_on, 0);
653043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->discharge, 0);
654043386a0SNoralf Trønnes 	if (epd->border)
655043386a0SNoralf Trønnes 		gpiod_set_value_cansleep(epd->border, 0);
656043386a0SNoralf Trønnes 	repaper_spi_mosi_low(spi);
657043386a0SNoralf Trønnes 	usleep_range(5000, 10000);
658043386a0SNoralf Trønnes 
659043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->panel_on, 1);
660043386a0SNoralf Trønnes 	/*
661043386a0SNoralf Trønnes 	 * This delay comes from the repaper.org userspace driver, it's not
662043386a0SNoralf Trønnes 	 * mentioned in the datasheet.
663043386a0SNoralf Trønnes 	 */
664043386a0SNoralf Trønnes 	usleep_range(10000, 15000);
665043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->reset, 1);
666043386a0SNoralf Trønnes 	if (epd->border)
667043386a0SNoralf Trønnes 		gpiod_set_value_cansleep(epd->border, 1);
668043386a0SNoralf Trønnes 	usleep_range(5000, 10000);
669043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->reset, 0);
670043386a0SNoralf Trønnes 	usleep_range(5000, 10000);
671043386a0SNoralf Trønnes 	gpiod_set_value_cansleep(epd->reset, 1);
672043386a0SNoralf Trønnes 	usleep_range(5000, 10000);
673043386a0SNoralf Trønnes 
674043386a0SNoralf Trønnes 	/* Wait for COG to become ready */
675043386a0SNoralf Trønnes 	for (i = 100; i > 0; i--) {
676043386a0SNoralf Trønnes 		if (!gpiod_get_value_cansleep(epd->busy))
677043386a0SNoralf Trønnes 			break;
678043386a0SNoralf Trønnes 
679043386a0SNoralf Trønnes 		usleep_range(10, 100);
680043386a0SNoralf Trønnes 	}
681043386a0SNoralf Trønnes 
682043386a0SNoralf Trønnes 	if (!i) {
683043386a0SNoralf Trønnes 		DRM_DEV_ERROR(dev, "timeout waiting for panel to become ready.\n");
684043386a0SNoralf Trønnes 		power_off(epd);
685043386a0SNoralf Trønnes 		goto out_exit;
686043386a0SNoralf Trønnes 	}
687043386a0SNoralf Trønnes 
688043386a0SNoralf Trønnes 	repaper_read_id(spi);
689043386a0SNoralf Trønnes 	ret = repaper_read_id(spi);
690043386a0SNoralf Trønnes 	if (ret != REPAPER_RID_G2_COG_ID) {
691043386a0SNoralf Trønnes 		if (ret < 0)
692043386a0SNoralf Trønnes 			dev_err(dev, "failed to read chip (%d)\n", ret);
693043386a0SNoralf Trønnes 		else
694043386a0SNoralf Trønnes 			dev_err(dev, "wrong COG ID 0x%02x\n", ret);
695043386a0SNoralf Trønnes 		power_off(epd);
696043386a0SNoralf Trønnes 		goto out_exit;
697043386a0SNoralf Trønnes 	}
698043386a0SNoralf Trønnes 
699043386a0SNoralf Trønnes 	/* Disable OE */
700043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x02, 0x40);
701043386a0SNoralf Trønnes 
702043386a0SNoralf Trønnes 	ret = repaper_read_val(spi, 0x0f);
703043386a0SNoralf Trønnes 	if (ret < 0 || !(ret & 0x80)) {
704043386a0SNoralf Trønnes 		if (ret < 0)
705043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
706043386a0SNoralf Trønnes 		else
707043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "panel is reported broken\n");
708043386a0SNoralf Trønnes 		power_off(epd);
709043386a0SNoralf Trønnes 		goto out_exit;
710043386a0SNoralf Trønnes 	}
711043386a0SNoralf Trønnes 
712043386a0SNoralf Trønnes 	/* Power saving mode */
713043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x0b, 0x02);
714043386a0SNoralf Trønnes 	/* Channel select */
715043386a0SNoralf Trønnes 	repaper_write_buf(spi, 0x01, epd->channel_select, 8);
716043386a0SNoralf Trønnes 	/* High power mode osc */
717043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x07, 0xd1);
718043386a0SNoralf Trønnes 	/* Power setting */
719043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x08, 0x02);
720043386a0SNoralf Trønnes 	/* Vcom level */
721043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x09, 0xc2);
722043386a0SNoralf Trønnes 	/* Power setting */
723043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x04, 0x03);
724043386a0SNoralf Trønnes 	/* Driver latch on */
725043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x03, 0x01);
726043386a0SNoralf Trønnes 	/* Driver latch off */
727043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x03, 0x00);
728043386a0SNoralf Trønnes 	usleep_range(5000, 10000);
729043386a0SNoralf Trønnes 
730043386a0SNoralf Trønnes 	/* Start chargepump */
731043386a0SNoralf Trønnes 	for (i = 0; i < 4; ++i) {
732043386a0SNoralf Trønnes 		/* Charge pump positive voltage on - VGH/VDL on */
733043386a0SNoralf Trønnes 		repaper_write_val(spi, 0x05, 0x01);
734043386a0SNoralf Trønnes 		msleep(240);
735043386a0SNoralf Trønnes 
736043386a0SNoralf Trønnes 		/* Charge pump negative voltage on - VGL/VDL on */
737043386a0SNoralf Trønnes 		repaper_write_val(spi, 0x05, 0x03);
738043386a0SNoralf Trønnes 		msleep(40);
739043386a0SNoralf Trønnes 
740043386a0SNoralf Trønnes 		/* Charge pump Vcom on - Vcom driver on */
741043386a0SNoralf Trønnes 		repaper_write_val(spi, 0x05, 0x0f);
742043386a0SNoralf Trønnes 		msleep(40);
743043386a0SNoralf Trønnes 
744043386a0SNoralf Trønnes 		/* check DC/DC */
745043386a0SNoralf Trønnes 		ret = repaper_read_val(spi, 0x0f);
746043386a0SNoralf Trønnes 		if (ret < 0) {
747043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
748043386a0SNoralf Trønnes 			power_off(epd);
749043386a0SNoralf Trønnes 			goto out_exit;
750043386a0SNoralf Trønnes 		}
751043386a0SNoralf Trønnes 
752043386a0SNoralf Trønnes 		if (ret & 0x40) {
753043386a0SNoralf Trønnes 			dc_ok = true;
754043386a0SNoralf Trønnes 			break;
755043386a0SNoralf Trønnes 		}
756043386a0SNoralf Trønnes 	}
757043386a0SNoralf Trønnes 
758043386a0SNoralf Trønnes 	if (!dc_ok) {
759043386a0SNoralf Trønnes 		DRM_DEV_ERROR(dev, "dc/dc failed\n");
760043386a0SNoralf Trønnes 		power_off(epd);
761043386a0SNoralf Trønnes 		goto out_exit;
762043386a0SNoralf Trønnes 	}
763043386a0SNoralf Trønnes 
764043386a0SNoralf Trønnes 	/*
765043386a0SNoralf Trønnes 	 * Output enable to disable
766043386a0SNoralf Trønnes 	 * The userspace driver sets this to 0x04, but the datasheet says 0x06
767043386a0SNoralf Trønnes 	 */
768043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x02, 0x04);
769043386a0SNoralf Trønnes 
770043386a0SNoralf Trønnes 	epd->partial = false;
771043386a0SNoralf Trønnes out_exit:
772043386a0SNoralf Trønnes 	drm_dev_exit(idx);
773043386a0SNoralf Trønnes }
774043386a0SNoralf Trønnes 
repaper_pipe_disable(struct drm_simple_display_pipe * pipe)775043386a0SNoralf Trønnes static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
776043386a0SNoralf Trønnes {
777043386a0SNoralf Trønnes 	struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev);
778043386a0SNoralf Trønnes 	struct spi_device *spi = epd->spi;
779043386a0SNoralf Trønnes 	unsigned int line;
780043386a0SNoralf Trønnes 
781043386a0SNoralf Trønnes 	/*
782043386a0SNoralf Trønnes 	 * This callback is not protected by drm_dev_enter/exit since we want to
783043386a0SNoralf Trønnes 	 * turn off the display on regular driver unload. It's highly unlikely
784043386a0SNoralf Trønnes 	 * that the underlying SPI controller is gone should this be called after
785043386a0SNoralf Trønnes 	 * unplug.
786043386a0SNoralf Trønnes 	 */
787043386a0SNoralf Trønnes 
788043386a0SNoralf Trønnes 	DRM_DEBUG_DRIVER("\n");
789043386a0SNoralf Trønnes 
790043386a0SNoralf Trønnes 	/* Nothing frame */
791043386a0SNoralf Trønnes 	for (line = 0; line < epd->height; line++)
792043386a0SNoralf Trønnes 		repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
793043386a0SNoralf Trønnes 				 REPAPER_COMPENSATE);
794043386a0SNoralf Trønnes 
795043386a0SNoralf Trønnes 	/* 2.7" */
796043386a0SNoralf Trønnes 	if (epd->border) {
797043386a0SNoralf Trønnes 		/* Dummy line */
798043386a0SNoralf Trønnes 		repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
799043386a0SNoralf Trønnes 				 REPAPER_COMPENSATE);
800043386a0SNoralf Trønnes 		msleep(25);
801043386a0SNoralf Trønnes 		gpiod_set_value_cansleep(epd->border, 0);
802043386a0SNoralf Trønnes 		msleep(200);
803043386a0SNoralf Trønnes 		gpiod_set_value_cansleep(epd->border, 1);
804043386a0SNoralf Trønnes 	} else {
805043386a0SNoralf Trønnes 		/* Border dummy line */
806043386a0SNoralf Trønnes 		repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
807043386a0SNoralf Trønnes 				 REPAPER_NORMAL);
808043386a0SNoralf Trønnes 		msleep(200);
809043386a0SNoralf Trønnes 	}
810043386a0SNoralf Trønnes 
811043386a0SNoralf Trønnes 	/* not described in datasheet */
812043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x0b, 0x00);
813043386a0SNoralf Trønnes 	/* Latch reset turn on */
814043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x03, 0x01);
815043386a0SNoralf Trønnes 	/* Power off charge pump Vcom */
816043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x05, 0x03);
817043386a0SNoralf Trønnes 	/* Power off charge pump neg voltage */
818043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x05, 0x01);
819043386a0SNoralf Trønnes 	msleep(120);
820043386a0SNoralf Trønnes 	/* Discharge internal */
821043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x04, 0x80);
822043386a0SNoralf Trønnes 	/* turn off all charge pumps */
823043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x05, 0x00);
824043386a0SNoralf Trønnes 	/* Turn off osc */
825043386a0SNoralf Trønnes 	repaper_write_val(spi, 0x07, 0x01);
826043386a0SNoralf Trønnes 	msleep(50);
827043386a0SNoralf Trønnes 
828043386a0SNoralf Trønnes 	power_off(epd);
829043386a0SNoralf Trønnes }
830043386a0SNoralf Trønnes 
repaper_pipe_update(struct drm_simple_display_pipe * pipe,struct drm_plane_state * old_state)831043386a0SNoralf Trønnes static void repaper_pipe_update(struct drm_simple_display_pipe *pipe,
832043386a0SNoralf Trønnes 				struct drm_plane_state *old_state)
833043386a0SNoralf Trønnes {
834043386a0SNoralf Trønnes 	struct drm_plane_state *state = pipe->plane.state;
8354cd24d4bSThomas Zimmermann 	struct drm_format_conv_state fmtcnv_state = DRM_FORMAT_CONV_STATE_INIT;
836043386a0SNoralf Trønnes 	struct drm_rect rect;
837043386a0SNoralf Trønnes 
838e8411b7eSDaniel Vetter 	if (!pipe->crtc.state->active)
839e8411b7eSDaniel Vetter 		return;
840e8411b7eSDaniel Vetter 
841043386a0SNoralf Trønnes 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
8424cd24d4bSThomas Zimmermann 		repaper_fb_dirty(state->fb, &fmtcnv_state);
8434cd24d4bSThomas Zimmermann 
8444cd24d4bSThomas Zimmermann 	drm_format_conv_state_release(&fmtcnv_state);
845043386a0SNoralf Trønnes }
846043386a0SNoralf Trønnes 
847043386a0SNoralf Trønnes static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
848216b9bbaSThomas Zimmermann 	.mode_valid = repaper_pipe_mode_valid,
849043386a0SNoralf Trønnes 	.enable = repaper_pipe_enable,
850043386a0SNoralf Trønnes 	.disable = repaper_pipe_disable,
851043386a0SNoralf Trønnes 	.update = repaper_pipe_update,
852043386a0SNoralf Trønnes };
853043386a0SNoralf Trønnes 
repaper_connector_get_modes(struct drm_connector * connector)854043386a0SNoralf Trønnes static int repaper_connector_get_modes(struct drm_connector *connector)
855043386a0SNoralf Trønnes {
856043386a0SNoralf Trønnes 	struct repaper_epd *epd = drm_to_epd(connector->dev);
857043386a0SNoralf Trønnes 
858d25654b3SThomas Zimmermann 	return drm_connector_helper_get_modes_fixed(connector, epd->mode);
859043386a0SNoralf Trønnes }
860043386a0SNoralf Trønnes 
861043386a0SNoralf Trønnes static const struct drm_connector_helper_funcs repaper_connector_hfuncs = {
862043386a0SNoralf Trønnes 	.get_modes = repaper_connector_get_modes,
863043386a0SNoralf Trønnes };
864043386a0SNoralf Trønnes 
865043386a0SNoralf Trønnes static const struct drm_connector_funcs repaper_connector_funcs = {
866043386a0SNoralf Trønnes 	.reset = drm_atomic_helper_connector_reset,
867043386a0SNoralf Trønnes 	.fill_modes = drm_helper_probe_single_connector_modes,
868043386a0SNoralf Trønnes 	.destroy = drm_connector_cleanup,
869043386a0SNoralf Trønnes 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
870043386a0SNoralf Trønnes 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
871043386a0SNoralf Trønnes };
872043386a0SNoralf Trønnes 
873043386a0SNoralf Trønnes static const struct drm_mode_config_funcs repaper_mode_config_funcs = {
874043386a0SNoralf Trønnes 	.fb_create = drm_gem_fb_create_with_dirty,
875043386a0SNoralf Trønnes 	.atomic_check = drm_atomic_helper_check,
876043386a0SNoralf Trønnes 	.atomic_commit = drm_atomic_helper_commit,
877043386a0SNoralf Trønnes };
878043386a0SNoralf Trønnes 
879043386a0SNoralf Trønnes static const uint32_t repaper_formats[] = {
880043386a0SNoralf Trønnes 	DRM_FORMAT_XRGB8888,
881043386a0SNoralf Trønnes };
882043386a0SNoralf Trønnes 
883043386a0SNoralf Trønnes static const struct drm_display_mode repaper_e1144cs021_mode = {
884043386a0SNoralf Trønnes 	DRM_SIMPLE_MODE(128, 96, 29, 22),
885043386a0SNoralf Trønnes };
886043386a0SNoralf Trønnes 
887043386a0SNoralf Trønnes static const u8 repaper_e1144cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
888043386a0SNoralf Trønnes 					    0x00, 0x0f, 0xff, 0x00 };
889043386a0SNoralf Trønnes 
890043386a0SNoralf Trønnes static const struct drm_display_mode repaper_e1190cs021_mode = {
891043386a0SNoralf Trønnes 	DRM_SIMPLE_MODE(144, 128, 36, 32),
892043386a0SNoralf Trønnes };
893043386a0SNoralf Trønnes 
894043386a0SNoralf Trønnes static const u8 repaper_e1190cs021_cs[] = { 0x00, 0x00, 0x00, 0x03,
895043386a0SNoralf Trønnes 					    0xfc, 0x00, 0x00, 0xff };
896043386a0SNoralf Trønnes 
897043386a0SNoralf Trønnes static const struct drm_display_mode repaper_e2200cs021_mode = {
898043386a0SNoralf Trønnes 	DRM_SIMPLE_MODE(200, 96, 46, 22),
899043386a0SNoralf Trønnes };
900043386a0SNoralf Trønnes 
901043386a0SNoralf Trønnes static const u8 repaper_e2200cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
902043386a0SNoralf Trønnes 					    0x01, 0xff, 0xe0, 0x00 };
903043386a0SNoralf Trønnes 
904043386a0SNoralf Trønnes static const struct drm_display_mode repaper_e2271cs021_mode = {
905043386a0SNoralf Trønnes 	DRM_SIMPLE_MODE(264, 176, 57, 38),
906043386a0SNoralf Trønnes };
907043386a0SNoralf Trønnes 
908043386a0SNoralf Trønnes static const u8 repaper_e2271cs021_cs[] = { 0x00, 0x00, 0x00, 0x7f,
909043386a0SNoralf Trønnes 					    0xff, 0xfe, 0x00, 0x00 };
910043386a0SNoralf Trønnes 
9114a83c26aSDanilo Krummrich DEFINE_DRM_GEM_DMA_FOPS(repaper_fops);
912043386a0SNoralf Trønnes 
91370a59dd8SDaniel Vetter static const struct drm_driver repaper_driver = {
914043386a0SNoralf Trønnes 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
915043386a0SNoralf Trønnes 	.fops			= &repaper_fops,
9164a83c26aSDanilo Krummrich 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
9171622e611SThomas Zimmermann 	DRM_FBDEV_DMA_DRIVER_OPS,
918043386a0SNoralf Trønnes 	.name			= "repaper",
919043386a0SNoralf Trønnes 	.desc			= "Pervasive Displays RePaper e-ink panels",
920043386a0SNoralf Trønnes 	.major			= 1,
921043386a0SNoralf Trønnes 	.minor			= 0,
922043386a0SNoralf Trønnes };
923043386a0SNoralf Trønnes 
924043386a0SNoralf Trønnes static const struct of_device_id repaper_of_match[] = {
925043386a0SNoralf Trønnes 	{ .compatible = "pervasive,e1144cs021", .data = (void *)E1144CS021 },
926043386a0SNoralf Trønnes 	{ .compatible = "pervasive,e1190cs021", .data = (void *)E1190CS021 },
927043386a0SNoralf Trønnes 	{ .compatible = "pervasive,e2200cs021", .data = (void *)E2200CS021 },
928043386a0SNoralf Trønnes 	{ .compatible = "pervasive,e2271cs021", .data = (void *)E2271CS021 },
929043386a0SNoralf Trønnes 	{},
930043386a0SNoralf Trønnes };
931043386a0SNoralf Trønnes MODULE_DEVICE_TABLE(of, repaper_of_match);
932043386a0SNoralf Trønnes 
933043386a0SNoralf Trønnes static const struct spi_device_id repaper_id[] = {
934043386a0SNoralf Trønnes 	{ "e1144cs021", E1144CS021 },
935043386a0SNoralf Trønnes 	{ "e1190cs021", E1190CS021 },
936043386a0SNoralf Trønnes 	{ "e2200cs021", E2200CS021 },
937043386a0SNoralf Trønnes 	{ "e2271cs021", E2271CS021 },
938043386a0SNoralf Trønnes 	{ },
939043386a0SNoralf Trønnes };
940043386a0SNoralf Trønnes MODULE_DEVICE_TABLE(spi, repaper_id);
941043386a0SNoralf Trønnes 
repaper_probe(struct spi_device * spi)942043386a0SNoralf Trønnes static int repaper_probe(struct spi_device *spi)
943043386a0SNoralf Trønnes {
944043386a0SNoralf Trønnes 	const struct drm_display_mode *mode;
945043386a0SNoralf Trønnes 	const struct spi_device_id *spi_id;
946043386a0SNoralf Trønnes 	struct device *dev = &spi->dev;
947043386a0SNoralf Trønnes 	enum repaper_model model;
948043386a0SNoralf Trønnes 	const char *thermal_zone;
949043386a0SNoralf Trønnes 	struct repaper_epd *epd;
950043386a0SNoralf Trønnes 	size_t line_buffer_size;
951043386a0SNoralf Trønnes 	struct drm_device *drm;
952ffb42e64SAndy Shevchenko 	const void *match;
953043386a0SNoralf Trønnes 	int ret;
954043386a0SNoralf Trønnes 
955ffb42e64SAndy Shevchenko 	match = device_get_match_data(dev);
956043386a0SNoralf Trønnes 	if (match) {
95724883eb2SJustin Stitt 		model = (enum repaper_model)(uintptr_t)match;
958043386a0SNoralf Trønnes 	} else {
959043386a0SNoralf Trønnes 		spi_id = spi_get_device_id(spi);
960ffb42e64SAndy Shevchenko 		model = (enum repaper_model)spi_id->driver_data;
961043386a0SNoralf Trønnes 	}
962043386a0SNoralf Trønnes 
963043386a0SNoralf Trønnes 	/* The SPI device is used to allocate dma memory */
964043386a0SNoralf Trønnes 	if (!dev->coherent_dma_mask) {
965043386a0SNoralf Trønnes 		ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
966043386a0SNoralf Trønnes 		if (ret) {
967043386a0SNoralf Trønnes 			dev_warn(dev, "Failed to set dma mask %d\n", ret);
968043386a0SNoralf Trønnes 			return ret;
969043386a0SNoralf Trønnes 		}
970043386a0SNoralf Trønnes 	}
971043386a0SNoralf Trønnes 
97298904f3dSDaniel Vetter 	epd = devm_drm_dev_alloc(dev, &repaper_driver,
97398904f3dSDaniel Vetter 				 struct repaper_epd, drm);
97498904f3dSDaniel Vetter 	if (IS_ERR(epd))
97598904f3dSDaniel Vetter 		return PTR_ERR(epd);
976043386a0SNoralf Trønnes 
977043386a0SNoralf Trønnes 	drm = &epd->drm;
978043386a0SNoralf Trønnes 
979be9f2b03SDaniel Vetter 	ret = drmm_mode_config_init(drm);
980be9f2b03SDaniel Vetter 	if (ret)
981be9f2b03SDaniel Vetter 		return ret;
982043386a0SNoralf Trønnes 	drm->mode_config.funcs = &repaper_mode_config_funcs;
983043386a0SNoralf Trønnes 
984043386a0SNoralf Trønnes 	epd->spi = spi;
985043386a0SNoralf Trønnes 
986043386a0SNoralf Trønnes 	epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
987043386a0SNoralf Trønnes 	if (IS_ERR(epd->panel_on)) {
988043386a0SNoralf Trønnes 		ret = PTR_ERR(epd->panel_on);
989043386a0SNoralf Trønnes 		if (ret != -EPROBE_DEFER)
990043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "Failed to get gpio 'panel-on'\n");
991043386a0SNoralf Trønnes 		return ret;
992043386a0SNoralf Trønnes 	}
993043386a0SNoralf Trønnes 
994043386a0SNoralf Trønnes 	epd->discharge = devm_gpiod_get(dev, "discharge", GPIOD_OUT_LOW);
995043386a0SNoralf Trønnes 	if (IS_ERR(epd->discharge)) {
996043386a0SNoralf Trønnes 		ret = PTR_ERR(epd->discharge);
997043386a0SNoralf Trønnes 		if (ret != -EPROBE_DEFER)
998043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "Failed to get gpio 'discharge'\n");
999043386a0SNoralf Trønnes 		return ret;
1000043386a0SNoralf Trønnes 	}
1001043386a0SNoralf Trønnes 
1002043386a0SNoralf Trønnes 	epd->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1003043386a0SNoralf Trønnes 	if (IS_ERR(epd->reset)) {
1004043386a0SNoralf Trønnes 		ret = PTR_ERR(epd->reset);
1005043386a0SNoralf Trønnes 		if (ret != -EPROBE_DEFER)
1006043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
1007043386a0SNoralf Trønnes 		return ret;
1008043386a0SNoralf Trønnes 	}
1009043386a0SNoralf Trønnes 
1010043386a0SNoralf Trønnes 	epd->busy = devm_gpiod_get(dev, "busy", GPIOD_IN);
1011043386a0SNoralf Trønnes 	if (IS_ERR(epd->busy)) {
1012043386a0SNoralf Trønnes 		ret = PTR_ERR(epd->busy);
1013043386a0SNoralf Trønnes 		if (ret != -EPROBE_DEFER)
1014043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "Failed to get gpio 'busy'\n");
1015043386a0SNoralf Trønnes 		return ret;
1016043386a0SNoralf Trønnes 	}
1017043386a0SNoralf Trønnes 
1018043386a0SNoralf Trønnes 	if (!device_property_read_string(dev, "pervasive,thermal-zone",
1019043386a0SNoralf Trønnes 					 &thermal_zone)) {
1020043386a0SNoralf Trønnes 		epd->thermal = thermal_zone_get_zone_by_name(thermal_zone);
1021043386a0SNoralf Trønnes 		if (IS_ERR(epd->thermal)) {
1022043386a0SNoralf Trønnes 			DRM_DEV_ERROR(dev, "Failed to get thermal zone: %s\n", thermal_zone);
1023043386a0SNoralf Trønnes 			return PTR_ERR(epd->thermal);
1024043386a0SNoralf Trønnes 		}
1025043386a0SNoralf Trønnes 	}
1026043386a0SNoralf Trønnes 
1027043386a0SNoralf Trønnes 	switch (model) {
1028043386a0SNoralf Trønnes 	case E1144CS021:
1029043386a0SNoralf Trønnes 		mode = &repaper_e1144cs021_mode;
1030043386a0SNoralf Trønnes 		epd->channel_select = repaper_e1144cs021_cs;
1031043386a0SNoralf Trønnes 		epd->stage_time = 480;
1032043386a0SNoralf Trønnes 		epd->bytes_per_scan = 96 / 4;
1033043386a0SNoralf Trønnes 		epd->middle_scan = true; /* data-scan-data */
1034043386a0SNoralf Trønnes 		epd->pre_border_byte = false;
1035043386a0SNoralf Trønnes 		epd->border_byte = REPAPER_BORDER_BYTE_ZERO;
1036043386a0SNoralf Trønnes 		break;
1037043386a0SNoralf Trønnes 
1038043386a0SNoralf Trønnes 	case E1190CS021:
1039043386a0SNoralf Trønnes 		mode = &repaper_e1190cs021_mode;
1040043386a0SNoralf Trønnes 		epd->channel_select = repaper_e1190cs021_cs;
1041043386a0SNoralf Trønnes 		epd->stage_time = 480;
1042043386a0SNoralf Trønnes 		epd->bytes_per_scan = 128 / 4 / 2;
1043043386a0SNoralf Trønnes 		epd->middle_scan = false; /* scan-data-scan */
1044043386a0SNoralf Trønnes 		epd->pre_border_byte = false;
1045043386a0SNoralf Trønnes 		epd->border_byte = REPAPER_BORDER_BYTE_SET;
1046043386a0SNoralf Trønnes 		break;
1047043386a0SNoralf Trønnes 
1048043386a0SNoralf Trønnes 	case E2200CS021:
1049043386a0SNoralf Trønnes 		mode = &repaper_e2200cs021_mode;
1050043386a0SNoralf Trønnes 		epd->channel_select = repaper_e2200cs021_cs;
1051043386a0SNoralf Trønnes 		epd->stage_time = 480;
1052043386a0SNoralf Trønnes 		epd->bytes_per_scan = 96 / 4;
1053043386a0SNoralf Trønnes 		epd->middle_scan = true; /* data-scan-data */
1054043386a0SNoralf Trønnes 		epd->pre_border_byte = true;
1055043386a0SNoralf Trønnes 		epd->border_byte = REPAPER_BORDER_BYTE_NONE;
1056043386a0SNoralf Trønnes 		break;
1057043386a0SNoralf Trønnes 
1058043386a0SNoralf Trønnes 	case E2271CS021:
1059043386a0SNoralf Trønnes 		epd->border = devm_gpiod_get(dev, "border", GPIOD_OUT_LOW);
1060043386a0SNoralf Trønnes 		if (IS_ERR(epd->border)) {
1061043386a0SNoralf Trønnes 			ret = PTR_ERR(epd->border);
1062043386a0SNoralf Trønnes 			if (ret != -EPROBE_DEFER)
1063043386a0SNoralf Trønnes 				DRM_DEV_ERROR(dev, "Failed to get gpio 'border'\n");
1064043386a0SNoralf Trønnes 			return ret;
1065043386a0SNoralf Trønnes 		}
1066043386a0SNoralf Trønnes 
1067043386a0SNoralf Trønnes 		mode = &repaper_e2271cs021_mode;
1068043386a0SNoralf Trønnes 		epd->channel_select = repaper_e2271cs021_cs;
1069043386a0SNoralf Trønnes 		epd->stage_time = 630;
1070043386a0SNoralf Trønnes 		epd->bytes_per_scan = 176 / 4;
1071043386a0SNoralf Trønnes 		epd->middle_scan = true; /* data-scan-data */
1072043386a0SNoralf Trønnes 		epd->pre_border_byte = true;
1073043386a0SNoralf Trønnes 		epd->border_byte = REPAPER_BORDER_BYTE_NONE;
1074043386a0SNoralf Trønnes 		break;
1075043386a0SNoralf Trønnes 
1076043386a0SNoralf Trønnes 	default:
1077043386a0SNoralf Trønnes 		return -ENODEV;
1078043386a0SNoralf Trønnes 	}
1079043386a0SNoralf Trønnes 
1080043386a0SNoralf Trønnes 	epd->mode = mode;
1081043386a0SNoralf Trønnes 	epd->width = mode->hdisplay;
1082043386a0SNoralf Trønnes 	epd->height = mode->vdisplay;
1083043386a0SNoralf Trønnes 	epd->factored_stage_time = epd->stage_time;
1084043386a0SNoralf Trønnes 
1085043386a0SNoralf Trønnes 	line_buffer_size = 2 * epd->width / 8 + epd->bytes_per_scan + 2;
1086043386a0SNoralf Trønnes 	epd->line_buffer = devm_kzalloc(dev, line_buffer_size, GFP_KERNEL);
1087043386a0SNoralf Trønnes 	if (!epd->line_buffer)
1088043386a0SNoralf Trønnes 		return -ENOMEM;
1089043386a0SNoralf Trønnes 
1090043386a0SNoralf Trønnes 	epd->current_frame = devm_kzalloc(dev, epd->width * epd->height / 8,
1091043386a0SNoralf Trønnes 					  GFP_KERNEL);
1092043386a0SNoralf Trønnes 	if (!epd->current_frame)
1093043386a0SNoralf Trønnes 		return -ENOMEM;
1094043386a0SNoralf Trønnes 
1095043386a0SNoralf Trønnes 	drm->mode_config.min_width = mode->hdisplay;
1096043386a0SNoralf Trønnes 	drm->mode_config.max_width = mode->hdisplay;
1097043386a0SNoralf Trønnes 	drm->mode_config.min_height = mode->vdisplay;
1098043386a0SNoralf Trønnes 	drm->mode_config.max_height = mode->vdisplay;
1099043386a0SNoralf Trønnes 
1100043386a0SNoralf Trønnes 	drm_connector_helper_add(&epd->connector, &repaper_connector_hfuncs);
1101043386a0SNoralf Trønnes 	ret = drm_connector_init(drm, &epd->connector, &repaper_connector_funcs,
1102043386a0SNoralf Trønnes 				 DRM_MODE_CONNECTOR_SPI);
1103043386a0SNoralf Trønnes 	if (ret)
1104043386a0SNoralf Trønnes 		return ret;
1105043386a0SNoralf Trønnes 
1106043386a0SNoralf Trønnes 	ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs,
1107043386a0SNoralf Trønnes 					   repaper_formats, ARRAY_SIZE(repaper_formats),
1108043386a0SNoralf Trønnes 					   NULL, &epd->connector);
1109043386a0SNoralf Trønnes 	if (ret)
1110043386a0SNoralf Trønnes 		return ret;
1111043386a0SNoralf Trønnes 
1112043386a0SNoralf Trønnes 	drm_mode_config_reset(drm);
1113043386a0SNoralf Trønnes 
1114043386a0SNoralf Trønnes 	ret = drm_dev_register(drm, 0);
1115043386a0SNoralf Trønnes 	if (ret)
1116043386a0SNoralf Trønnes 		return ret;
1117043386a0SNoralf Trønnes 
1118043386a0SNoralf Trønnes 	spi_set_drvdata(spi, drm);
1119043386a0SNoralf Trønnes 
1120043386a0SNoralf Trønnes 	DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
1121043386a0SNoralf Trønnes 
11221622e611SThomas Zimmermann 	drm_client_setup(drm, NULL);
1123043386a0SNoralf Trønnes 
1124043386a0SNoralf Trønnes 	return 0;
1125043386a0SNoralf Trønnes }
1126043386a0SNoralf Trønnes 
repaper_remove(struct spi_device * spi)1127a0386bbaSUwe Kleine-König static void repaper_remove(struct spi_device *spi)
1128043386a0SNoralf Trønnes {
1129043386a0SNoralf Trønnes 	struct drm_device *drm = spi_get_drvdata(spi);
1130043386a0SNoralf Trønnes 
1131043386a0SNoralf Trønnes 	drm_dev_unplug(drm);
1132043386a0SNoralf Trønnes 	drm_atomic_helper_shutdown(drm);
1133043386a0SNoralf Trønnes }
1134043386a0SNoralf Trønnes 
repaper_shutdown(struct spi_device * spi)1135043386a0SNoralf Trønnes static void repaper_shutdown(struct spi_device *spi)
1136043386a0SNoralf Trønnes {
1137043386a0SNoralf Trønnes 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
1138043386a0SNoralf Trønnes }
1139043386a0SNoralf Trønnes 
1140043386a0SNoralf Trønnes static struct spi_driver repaper_spi_driver = {
1141043386a0SNoralf Trønnes 	.driver = {
1142043386a0SNoralf Trønnes 		.name = "repaper",
1143043386a0SNoralf Trønnes 		.of_match_table = repaper_of_match,
1144043386a0SNoralf Trønnes 	},
1145043386a0SNoralf Trønnes 	.id_table = repaper_id,
1146043386a0SNoralf Trønnes 	.probe = repaper_probe,
1147043386a0SNoralf Trønnes 	.remove = repaper_remove,
1148043386a0SNoralf Trønnes 	.shutdown = repaper_shutdown,
1149043386a0SNoralf Trønnes };
1150043386a0SNoralf Trønnes module_spi_driver(repaper_spi_driver);
1151043386a0SNoralf Trønnes 
1152043386a0SNoralf Trønnes MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
1153043386a0SNoralf Trønnes MODULE_AUTHOR("Noralf Trønnes");
1154043386a0SNoralf Trønnes MODULE_LICENSE("GPL");
1155