2 * DRM driver for Ilitek ILI9225 panels
4 * Copyright 2017 David Lechner <david@lechnology.com>
6 * Some code copied from mipi-dbi.c
7 * Copyright 2016 Noralf Trønnes
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/delay.h>
16 #include <linux/dma-buf.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/module.h>
19 #include <linux/property.h>
20 #include <linux/spi/spi.h>
21 #include <video/mipi_display.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_damage_helper.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_fb_cma_helper.h>
27 #include <drm/drm_fb_helper.h>
28 #include <drm/drm_fourcc.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_rect.h>
32 #include <drm/drm_vblank.h>
33 #include <drm/tinydrm/mipi-dbi.h>
34 #include <drm/tinydrm/tinydrm-helpers.h>
36 #define ILI9225_DRIVER_READ_CODE 0x00
37 #define ILI9225_DRIVER_OUTPUT_CONTROL 0x01
38 #define ILI9225_LCD_AC_DRIVING_CONTROL 0x02
39 #define ILI9225_ENTRY_MODE 0x03
40 #define ILI9225_DISPLAY_CONTROL_1 0x07
41 #define ILI9225_BLANK_PERIOD_CONTROL_1 0x08
42 #define ILI9225_FRAME_CYCLE_CONTROL 0x0b
43 #define ILI9225_INTERFACE_CONTROL 0x0c
44 #define ILI9225_OSCILLATION_CONTROL 0x0f
45 #define ILI9225_POWER_CONTROL_1 0x10
46 #define ILI9225_POWER_CONTROL_2 0x11
47 #define ILI9225_POWER_CONTROL_3 0x12
48 #define ILI9225_POWER_CONTROL_4 0x13
49 #define ILI9225_POWER_CONTROL_5 0x14
50 #define ILI9225_VCI_RECYCLING 0x15
51 #define ILI9225_RAM_ADDRESS_SET_1 0x20
52 #define ILI9225_RAM_ADDRESS_SET_2 0x21
53 #define ILI9225_WRITE_DATA_TO_GRAM 0x22
54 #define ILI9225_SOFTWARE_RESET 0x28
55 #define ILI9225_GATE_SCAN_CONTROL 0x30
56 #define ILI9225_VERTICAL_SCROLL_1 0x31
57 #define ILI9225_VERTICAL_SCROLL_2 0x32
58 #define ILI9225_VERTICAL_SCROLL_3 0x33
59 #define ILI9225_PARTIAL_DRIVING_POS_1 0x34
60 #define ILI9225_PARTIAL_DRIVING_POS_2 0x35
61 #define ILI9225_HORIZ_WINDOW_ADDR_1 0x36
62 #define ILI9225_HORIZ_WINDOW_ADDR_2 0x37
63 #define ILI9225_VERT_WINDOW_ADDR_1 0x38
64 #define ILI9225_VERT_WINDOW_ADDR_2 0x39
65 #define ILI9225_GAMMA_CONTROL_1 0x50
66 #define ILI9225_GAMMA_CONTROL_2 0x51
67 #define ILI9225_GAMMA_CONTROL_3 0x52
68 #define ILI9225_GAMMA_CONTROL_4 0x53
69 #define ILI9225_GAMMA_CONTROL_5 0x54
70 #define ILI9225_GAMMA_CONTROL_6 0x55
71 #define ILI9225_GAMMA_CONTROL_7 0x56
72 #define ILI9225_GAMMA_CONTROL_8 0x57
73 #define ILI9225_GAMMA_CONTROL_9 0x58
74 #define ILI9225_GAMMA_CONTROL_10 0x59
76 static inline int ili9225_command(struct mipi_dbi *mipi, u8 cmd, u16 data)
78 u8 par[2] = { data >> 8, data & 0xff };
80 return mipi_dbi_command_buf(mipi, cmd, par, 2);
83 static void ili9225_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
85 struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
86 struct mipi_dbi *mipi = drm_to_mipi_dbi(fb->dev);
87 unsigned int height = rect->y2 - rect->y1;
88 unsigned int width = rect->x2 - rect->x1;
89 bool swap = mipi->swap_bytes;
99 if (!drm_dev_enter(fb->dev, &idx))
102 full = width == fb->width && height == fb->height;
104 DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
106 if (!mipi->dc || !full || swap ||
107 fb->format->format == DRM_FORMAT_XRGB8888) {
109 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, rect, swap);
116 switch (mipi->rotation) {
128 y1 = fb->width - rect->x2;
129 y2 = fb->width - rect->x1 - 1;
134 x1 = fb->width - rect->x2;
135 x2 = fb->width - rect->x1 - 1;
136 y1 = fb->height - rect->y2;
137 y2 = fb->height - rect->y1 - 1;
142 x1 = fb->height - rect->y2;
143 x2 = fb->height - rect->y1 - 1;
151 ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
152 ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
153 ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_1, y2);
154 ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_2, y1);
156 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, x_start);
157 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, y_start);
159 ret = mipi_dbi_command_buf(mipi, ILI9225_WRITE_DATA_TO_GRAM, tr,
163 dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
168 static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
169 struct drm_plane_state *old_state)
171 struct drm_plane_state *state = pipe->plane.state;
172 struct drm_crtc *crtc = &pipe->crtc;
173 struct drm_rect rect;
175 if (drm_atomic_helper_damage_merged(old_state, state, &rect))
176 ili9225_fb_dirty(state->fb, &rect);
178 if (crtc->state->event) {
179 spin_lock_irq(&crtc->dev->event_lock);
180 drm_crtc_send_vblank_event(crtc, crtc->state->event);
181 spin_unlock_irq(&crtc->dev->event_lock);
182 crtc->state->event = NULL;
186 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
187 struct drm_crtc_state *crtc_state,
188 struct drm_plane_state *plane_state)
190 struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
191 struct drm_framebuffer *fb = plane_state->fb;
192 struct device *dev = pipe->crtc.dev->dev;
193 struct drm_rect rect = {
202 if (!drm_dev_enter(pipe->crtc.dev, &idx))
207 mipi_dbi_hw_reset(mipi);
210 * There don't seem to be two example init sequences that match, so
211 * using the one from the popular Arduino library for this display.
212 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
215 ret = ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0000);
217 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
220 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0000);
221 ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x0000);
222 ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x0000);
223 ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x0000);
227 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0018);
228 ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x6121);
229 ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x006f);
230 ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x495f);
231 ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0800);
235 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
239 switch (mipi->rotation) {
253 ili9225_command(mipi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
254 ili9225_command(mipi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
255 ili9225_command(mipi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
256 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
257 ili9225_command(mipi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
258 ili9225_command(mipi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
259 ili9225_command(mipi, ILI9225_INTERFACE_CONTROL, 0x0000);
260 ili9225_command(mipi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
261 ili9225_command(mipi, ILI9225_VCI_RECYCLING, 0x0020);
262 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
263 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
265 ili9225_command(mipi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
266 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
267 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
268 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
269 ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
270 ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
272 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_1, 0x0000);
273 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_2, 0x0808);
274 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_3, 0x080a);
275 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_4, 0x000a);
276 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
277 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_6, 0x0808);
278 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_7, 0x0000);
279 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
280 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_9, 0x0710);
281 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_10, 0x0710);
283 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
287 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
289 mipi->enabled = true;
290 ili9225_fb_dirty(fb, &rect);
295 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
297 struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
302 * This callback is not protected by drm_dev_enter/exit since we want to
303 * turn off the display on regular driver unload. It's highly unlikely
304 * that the underlying SPI controller is gone should this be called after
311 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
313 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
315 ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
317 mipi->enabled = false;
320 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
323 struct spi_device *spi = mipi->spi;
324 unsigned int bpw = 8;
328 gpiod_set_value_cansleep(mipi->dc, 0);
329 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
330 ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
334 if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
337 gpiod_set_value_cansleep(mipi->dc, 1);
338 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
340 return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
343 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
344 .enable = ili9225_pipe_enable,
345 .disable = ili9225_pipe_disable,
346 .update = ili9225_pipe_update,
347 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
350 static const struct drm_display_mode ili9225_mode = {
351 DRM_SIMPLE_MODE(176, 220, 35, 44),
354 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
356 static struct drm_driver ili9225_driver = {
357 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
359 .fops = &ili9225_fops,
360 .release = mipi_dbi_release,
361 DRM_GEM_CMA_VMAP_DRIVER_OPS,
363 .desc = "Ilitek ILI9225",
369 static const struct of_device_id ili9225_of_match[] = {
370 { .compatible = "vot,v220hf01a-t" },
373 MODULE_DEVICE_TABLE(of, ili9225_of_match);
375 static const struct spi_device_id ili9225_id[] = {
376 { "v220hf01a-t", 0 },
379 MODULE_DEVICE_TABLE(spi, ili9225_id);
381 static int ili9225_probe(struct spi_device *spi)
383 struct device *dev = &spi->dev;
384 struct drm_device *drm;
385 struct mipi_dbi *mipi;
386 struct gpio_desc *rs;
390 mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
395 ret = devm_drm_dev_init(dev, drm, &ili9225_driver);
401 drm_mode_config_init(drm);
403 mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
404 if (IS_ERR(mipi->reset)) {
405 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
406 return PTR_ERR(mipi->reset);
409 rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
411 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
415 device_property_read_u32(dev, "rotation", &rotation);
417 ret = mipi_dbi_spi_init(spi, mipi, rs);
421 /* override the command function set in mipi_dbi_spi_init() */
422 mipi->command = ili9225_dbi_command;
424 ret = mipi_dbi_init(mipi, &ili9225_pipe_funcs, &ili9225_mode, rotation);
428 drm_mode_config_reset(drm);
430 ret = drm_dev_register(drm, 0);
434 spi_set_drvdata(spi, drm);
436 drm_fbdev_generic_setup(drm, 32);
441 static int ili9225_remove(struct spi_device *spi)
443 struct drm_device *drm = spi_get_drvdata(spi);
446 drm_atomic_helper_shutdown(drm);
451 static void ili9225_shutdown(struct spi_device *spi)
453 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
456 static struct spi_driver ili9225_spi_driver = {
459 .owner = THIS_MODULE,
460 .of_match_table = ili9225_of_match,
462 .id_table = ili9225_id,
463 .probe = ili9225_probe,
464 .remove = ili9225_remove,
465 .shutdown = ili9225_shutdown,
467 module_spi_driver(ili9225_spi_driver);
469 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
470 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
471 MODULE_LICENSE("GPL");