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