xref: /linux/drivers/gpu/drm/tiny/ili9225.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DRM driver for Ilitek ILI9225 panels
4  *
5  * Copyright 2017 David Lechner <david@lechnology.com>
6  *
7  * Some code copied from mipi-dbi.c
8  * Copyright 2016 Noralf Trønnes
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/dma-buf.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/spi/spi.h>
17 #include <video/mipi_display.h>
18 
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_damage_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_dma_helper.h>
23 #include <drm/drm_fbdev_generic.h>
24 #include <drm/drm_fourcc.h>
25 #include <drm/drm_framebuffer.h>
26 #include <drm/drm_gem_atomic_helper.h>
27 #include <drm/drm_gem_dma_helper.h>
28 #include <drm/drm_gem_framebuffer_helper.h>
29 #include <drm/drm_managed.h>
30 #include <drm/drm_mipi_dbi.h>
31 #include <drm/drm_rect.h>
32 
33 #define ILI9225_DRIVER_READ_CODE	0x00
34 #define ILI9225_DRIVER_OUTPUT_CONTROL	0x01
35 #define ILI9225_LCD_AC_DRIVING_CONTROL	0x02
36 #define ILI9225_ENTRY_MODE		0x03
37 #define ILI9225_DISPLAY_CONTROL_1	0x07
38 #define ILI9225_BLANK_PERIOD_CONTROL_1	0x08
39 #define ILI9225_FRAME_CYCLE_CONTROL	0x0b
40 #define ILI9225_INTERFACE_CONTROL	0x0c
41 #define ILI9225_OSCILLATION_CONTROL	0x0f
42 #define ILI9225_POWER_CONTROL_1		0x10
43 #define ILI9225_POWER_CONTROL_2		0x11
44 #define ILI9225_POWER_CONTROL_3		0x12
45 #define ILI9225_POWER_CONTROL_4		0x13
46 #define ILI9225_POWER_CONTROL_5		0x14
47 #define ILI9225_VCI_RECYCLING		0x15
48 #define ILI9225_RAM_ADDRESS_SET_1	0x20
49 #define ILI9225_RAM_ADDRESS_SET_2	0x21
50 #define ILI9225_WRITE_DATA_TO_GRAM	0x22
51 #define ILI9225_SOFTWARE_RESET		0x28
52 #define ILI9225_GATE_SCAN_CONTROL	0x30
53 #define ILI9225_VERTICAL_SCROLL_1	0x31
54 #define ILI9225_VERTICAL_SCROLL_2	0x32
55 #define ILI9225_VERTICAL_SCROLL_3	0x33
56 #define ILI9225_PARTIAL_DRIVING_POS_1	0x34
57 #define ILI9225_PARTIAL_DRIVING_POS_2	0x35
58 #define ILI9225_HORIZ_WINDOW_ADDR_1	0x36
59 #define ILI9225_HORIZ_WINDOW_ADDR_2	0x37
60 #define ILI9225_VERT_WINDOW_ADDR_1	0x38
61 #define ILI9225_VERT_WINDOW_ADDR_2	0x39
62 #define ILI9225_GAMMA_CONTROL_1		0x50
63 #define ILI9225_GAMMA_CONTROL_2		0x51
64 #define ILI9225_GAMMA_CONTROL_3		0x52
65 #define ILI9225_GAMMA_CONTROL_4		0x53
66 #define ILI9225_GAMMA_CONTROL_5		0x54
67 #define ILI9225_GAMMA_CONTROL_6		0x55
68 #define ILI9225_GAMMA_CONTROL_7		0x56
69 #define ILI9225_GAMMA_CONTROL_8		0x57
70 #define ILI9225_GAMMA_CONTROL_9		0x58
71 #define ILI9225_GAMMA_CONTROL_10	0x59
72 
73 static inline int ili9225_command(struct mipi_dbi *dbi, u8 cmd, u16 data)
74 {
75 	u8 par[2] = { data >> 8, data & 0xff };
76 
77 	return mipi_dbi_command_buf(dbi, cmd, par, 2);
78 }
79 
80 static void ili9225_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
81 			     struct drm_rect *rect)
82 {
83 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
84 	unsigned int height = rect->y2 - rect->y1;
85 	unsigned int width = rect->x2 - rect->x1;
86 	struct mipi_dbi *dbi = &dbidev->dbi;
87 	bool swap = dbi->swap_bytes;
88 	u16 x_start, y_start;
89 	u16 x1, x2, y1, y2;
90 	int ret = 0;
91 	bool full;
92 	void *tr;
93 
94 	full = width == fb->width && height == fb->height;
95 
96 	DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
97 
98 	if (!dbi->dc || !full || swap ||
99 	    fb->format->format == DRM_FORMAT_XRGB8888) {
100 		tr = dbidev->tx_buf;
101 		ret = mipi_dbi_buf_copy(tr, src, fb, rect, swap);
102 		if (ret)
103 			goto err_msg;
104 	} else {
105 		tr = src->vaddr; /* TODO: Use mapping abstraction properly */
106 	}
107 
108 	switch (dbidev->rotation) {
109 	default:
110 		x1 = rect->x1;
111 		x2 = rect->x2 - 1;
112 		y1 = rect->y1;
113 		y2 = rect->y2 - 1;
114 		x_start = x1;
115 		y_start = y1;
116 		break;
117 	case 90:
118 		x1 = rect->y1;
119 		x2 = rect->y2 - 1;
120 		y1 = fb->width - rect->x2;
121 		y2 = fb->width - rect->x1 - 1;
122 		x_start = x1;
123 		y_start = y2;
124 		break;
125 	case 180:
126 		x1 = fb->width - rect->x2;
127 		x2 = fb->width - rect->x1 - 1;
128 		y1 = fb->height - rect->y2;
129 		y2 = fb->height - rect->y1 - 1;
130 		x_start = x2;
131 		y_start = y2;
132 		break;
133 	case 270:
134 		x1 = fb->height - rect->y2;
135 		x2 = fb->height - rect->y1 - 1;
136 		y1 = rect->x1;
137 		y2 = rect->x2 - 1;
138 		x_start = x2;
139 		y_start = y1;
140 		break;
141 	}
142 
143 	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
144 	ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
145 	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_1, y2);
146 	ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_2, y1);
147 
148 	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, x_start);
149 	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, y_start);
150 
151 	ret = mipi_dbi_command_buf(dbi, ILI9225_WRITE_DATA_TO_GRAM, tr,
152 				   width * height * 2);
153 err_msg:
154 	if (ret)
155 		dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
156 }
157 
158 static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
159 				struct drm_plane_state *old_state)
160 {
161 	struct drm_plane_state *state = pipe->plane.state;
162 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
163 	struct drm_framebuffer *fb = state->fb;
164 	struct drm_rect rect;
165 	int idx;
166 
167 	if (!pipe->crtc.state->active)
168 		return;
169 
170 	if (!drm_dev_enter(fb->dev, &idx))
171 		return;
172 
173 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
174 		ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect);
175 
176 	drm_dev_exit(idx);
177 }
178 
179 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
180 				struct drm_crtc_state *crtc_state,
181 				struct drm_plane_state *plane_state)
182 {
183 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
184 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
185 	struct drm_framebuffer *fb = plane_state->fb;
186 	struct device *dev = pipe->crtc.dev->dev;
187 	struct mipi_dbi *dbi = &dbidev->dbi;
188 	struct drm_rect rect = {
189 		.x1 = 0,
190 		.x2 = fb->width,
191 		.y1 = 0,
192 		.y2 = fb->height,
193 	};
194 	int ret, idx;
195 	u8 am_id;
196 
197 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
198 		return;
199 
200 	DRM_DEBUG_KMS("\n");
201 
202 	mipi_dbi_hw_reset(dbi);
203 
204 	/*
205 	 * There don't seem to be two example init sequences that match, so
206 	 * using the one from the popular Arduino library for this display.
207 	 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
208 	 */
209 
210 	ret = ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0000);
211 	if (ret) {
212 		DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
213 		goto out_exit;
214 	}
215 	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0000);
216 	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x0000);
217 	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x0000);
218 	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x0000);
219 
220 	msleep(40);
221 
222 	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0018);
223 	ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x6121);
224 	ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x006f);
225 	ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x495f);
226 	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0800);
227 
228 	msleep(10);
229 
230 	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x103b);
231 
232 	msleep(50);
233 
234 	switch (dbidev->rotation) {
235 	default:
236 		am_id = 0x30;
237 		break;
238 	case 90:
239 		am_id = 0x18;
240 		break;
241 	case 180:
242 		am_id = 0x00;
243 		break;
244 	case 270:
245 		am_id = 0x28;
246 		break;
247 	}
248 	ili9225_command(dbi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
249 	ili9225_command(dbi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
250 	ili9225_command(dbi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
251 	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
252 	ili9225_command(dbi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
253 	ili9225_command(dbi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
254 	ili9225_command(dbi, ILI9225_INTERFACE_CONTROL, 0x0000);
255 	ili9225_command(dbi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
256 	ili9225_command(dbi, ILI9225_VCI_RECYCLING, 0x0020);
257 	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
258 	ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
259 
260 	ili9225_command(dbi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
261 	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
262 	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
263 	ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
264 	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
265 	ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
266 
267 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_1, 0x0000);
268 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_2, 0x0808);
269 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_3, 0x080a);
270 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_4, 0x000a);
271 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
272 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_6, 0x0808);
273 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_7, 0x0000);
274 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
275 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_9, 0x0710);
276 	ili9225_command(dbi, ILI9225_GAMMA_CONTROL_10, 0x0710);
277 
278 	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
279 
280 	msleep(50);
281 
282 	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
283 
284 	ili9225_fb_dirty(&shadow_plane_state->data[0], fb, &rect);
285 
286 out_exit:
287 	drm_dev_exit(idx);
288 }
289 
290 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
291 {
292 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
293 	struct mipi_dbi *dbi = &dbidev->dbi;
294 
295 	DRM_DEBUG_KMS("\n");
296 
297 	/*
298 	 * This callback is not protected by drm_dev_enter/exit since we want to
299 	 * turn off the display on regular driver unload. It's highly unlikely
300 	 * that the underlying SPI controller is gone should this be called after
301 	 * unplug.
302 	 */
303 
304 	ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
305 	msleep(50);
306 	ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0007);
307 	msleep(50);
308 	ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0a02);
309 }
310 
311 static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par,
312 			       size_t num)
313 {
314 	struct spi_device *spi = dbi->spi;
315 	unsigned int bpw = 8;
316 	u32 speed_hz;
317 	int ret;
318 
319 	gpiod_set_value_cansleep(dbi->dc, 0);
320 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
321 	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
322 	if (ret || !num)
323 		return ret;
324 
325 	if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes)
326 		bpw = 16;
327 
328 	gpiod_set_value_cansleep(dbi->dc, 1);
329 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
330 
331 	return mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
332 }
333 
334 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
335 	.mode_valid	= mipi_dbi_pipe_mode_valid,
336 	.enable		= ili9225_pipe_enable,
337 	.disable	= ili9225_pipe_disable,
338 	.update		= ili9225_pipe_update,
339 	.begin_fb_access = mipi_dbi_pipe_begin_fb_access,
340 	.end_fb_access	= mipi_dbi_pipe_end_fb_access,
341 	.reset_plane	= mipi_dbi_pipe_reset_plane,
342 	.duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state,
343 	.destroy_plane_state = mipi_dbi_pipe_destroy_plane_state,
344 };
345 
346 static const struct drm_display_mode ili9225_mode = {
347 	DRM_SIMPLE_MODE(176, 220, 35, 44),
348 };
349 
350 DEFINE_DRM_GEM_DMA_FOPS(ili9225_fops);
351 
352 static const struct drm_driver ili9225_driver = {
353 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
354 	.fops			= &ili9225_fops,
355 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
356 	.name			= "ili9225",
357 	.desc			= "Ilitek ILI9225",
358 	.date			= "20171106",
359 	.major			= 1,
360 	.minor			= 0,
361 };
362 
363 static const struct of_device_id ili9225_of_match[] = {
364 	{ .compatible = "vot,v220hf01a-t" },
365 	{},
366 };
367 MODULE_DEVICE_TABLE(of, ili9225_of_match);
368 
369 static const struct spi_device_id ili9225_id[] = {
370 	{ "v220hf01a-t", 0 },
371 	{ },
372 };
373 MODULE_DEVICE_TABLE(spi, ili9225_id);
374 
375 static int ili9225_probe(struct spi_device *spi)
376 {
377 	struct device *dev = &spi->dev;
378 	struct mipi_dbi_dev *dbidev;
379 	struct drm_device *drm;
380 	struct mipi_dbi *dbi;
381 	struct gpio_desc *rs;
382 	u32 rotation = 0;
383 	int ret;
384 
385 	dbidev = devm_drm_dev_alloc(dev, &ili9225_driver,
386 				    struct mipi_dbi_dev, drm);
387 	if (IS_ERR(dbidev))
388 		return PTR_ERR(dbidev);
389 
390 	dbi = &dbidev->dbi;
391 	drm = &dbidev->drm;
392 
393 	dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
394 	if (IS_ERR(dbi->reset))
395 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
396 
397 	rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
398 	if (IS_ERR(rs))
399 		return dev_err_probe(dev, PTR_ERR(rs), "Failed to get GPIO 'rs'\n");
400 
401 	device_property_read_u32(dev, "rotation", &rotation);
402 
403 	ret = mipi_dbi_spi_init(spi, dbi, rs);
404 	if (ret)
405 		return ret;
406 
407 	/* override the command function set in  mipi_dbi_spi_init() */
408 	dbi->command = ili9225_dbi_command;
409 
410 	ret = mipi_dbi_dev_init(dbidev, &ili9225_pipe_funcs, &ili9225_mode, rotation);
411 	if (ret)
412 		return ret;
413 
414 	drm_mode_config_reset(drm);
415 
416 	ret = drm_dev_register(drm, 0);
417 	if (ret)
418 		return ret;
419 
420 	spi_set_drvdata(spi, drm);
421 
422 	drm_fbdev_generic_setup(drm, 0);
423 
424 	return 0;
425 }
426 
427 static void ili9225_remove(struct spi_device *spi)
428 {
429 	struct drm_device *drm = spi_get_drvdata(spi);
430 
431 	drm_dev_unplug(drm);
432 	drm_atomic_helper_shutdown(drm);
433 }
434 
435 static void ili9225_shutdown(struct spi_device *spi)
436 {
437 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
438 }
439 
440 static struct spi_driver ili9225_spi_driver = {
441 	.driver = {
442 		.name = "ili9225",
443 		.owner = THIS_MODULE,
444 		.of_match_table = ili9225_of_match,
445 	},
446 	.id_table = ili9225_id,
447 	.probe = ili9225_probe,
448 	.remove = ili9225_remove,
449 	.shutdown = ili9225_shutdown,
450 };
451 module_spi_driver(ili9225_spi_driver);
452 
453 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
454 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
455 MODULE_LICENSE("GPL");
456