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_drv.h>
24 #include <drm/drm_fb_cma_helper.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_gem_cma_helper.h>
27 #include <drm/drm_gem_framebuffer_helper.h>
28 #include <drm/tinydrm/mipi-dbi.h>
29 #include <drm/tinydrm/tinydrm-helpers.h>
31 #define ILI9225_DRIVER_READ_CODE 0x00
32 #define ILI9225_DRIVER_OUTPUT_CONTROL 0x01
33 #define ILI9225_LCD_AC_DRIVING_CONTROL 0x02
34 #define ILI9225_ENTRY_MODE 0x03
35 #define ILI9225_DISPLAY_CONTROL_1 0x07
36 #define ILI9225_BLANK_PERIOD_CONTROL_1 0x08
37 #define ILI9225_FRAME_CYCLE_CONTROL 0x0b
38 #define ILI9225_INTERFACE_CONTROL 0x0c
39 #define ILI9225_OSCILLATION_CONTROL 0x0f
40 #define ILI9225_POWER_CONTROL_1 0x10
41 #define ILI9225_POWER_CONTROL_2 0x11
42 #define ILI9225_POWER_CONTROL_3 0x12
43 #define ILI9225_POWER_CONTROL_4 0x13
44 #define ILI9225_POWER_CONTROL_5 0x14
45 #define ILI9225_VCI_RECYCLING 0x15
46 #define ILI9225_RAM_ADDRESS_SET_1 0x20
47 #define ILI9225_RAM_ADDRESS_SET_2 0x21
48 #define ILI9225_WRITE_DATA_TO_GRAM 0x22
49 #define ILI9225_SOFTWARE_RESET 0x28
50 #define ILI9225_GATE_SCAN_CONTROL 0x30
51 #define ILI9225_VERTICAL_SCROLL_1 0x31
52 #define ILI9225_VERTICAL_SCROLL_2 0x32
53 #define ILI9225_VERTICAL_SCROLL_3 0x33
54 #define ILI9225_PARTIAL_DRIVING_POS_1 0x34
55 #define ILI9225_PARTIAL_DRIVING_POS_2 0x35
56 #define ILI9225_HORIZ_WINDOW_ADDR_1 0x36
57 #define ILI9225_HORIZ_WINDOW_ADDR_2 0x37
58 #define ILI9225_VERT_WINDOW_ADDR_1 0x38
59 #define ILI9225_VERT_WINDOW_ADDR_2 0x39
60 #define ILI9225_GAMMA_CONTROL_1 0x50
61 #define ILI9225_GAMMA_CONTROL_2 0x51
62 #define ILI9225_GAMMA_CONTROL_3 0x52
63 #define ILI9225_GAMMA_CONTROL_4 0x53
64 #define ILI9225_GAMMA_CONTROL_5 0x54
65 #define ILI9225_GAMMA_CONTROL_6 0x55
66 #define ILI9225_GAMMA_CONTROL_7 0x56
67 #define ILI9225_GAMMA_CONTROL_8 0x57
68 #define ILI9225_GAMMA_CONTROL_9 0x58
69 #define ILI9225_GAMMA_CONTROL_10 0x59
71 static inline int ili9225_command(struct mipi_dbi *mipi, u8 cmd, u16 data)
73 u8 par[2] = { data >> 8, data & 0xff };
75 return mipi_dbi_command_buf(mipi, cmd, par, 2);
78 static int ili9225_fb_dirty(struct drm_framebuffer *fb,
79 struct drm_file *file_priv, unsigned int flags,
80 unsigned int color, struct drm_clip_rect *clips,
81 unsigned int num_clips)
83 struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
84 struct tinydrm_device *tdev = fb->dev->dev_private;
85 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
86 bool swap = mipi->swap_bytes;
87 struct drm_clip_rect clip;
97 full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
98 fb->width, fb->height);
100 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
101 clip.x1, clip.x2, clip.y1, clip.y2);
103 if (!mipi->dc || !full || swap ||
104 fb->format->format == DRM_FORMAT_XRGB8888) {
106 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
113 switch (mipi->rotation) {
125 y1 = fb->width - clip.x2;
126 y2 = fb->width - clip.x1 - 1;
131 x1 = fb->width - clip.x2;
132 x2 = fb->width - clip.x1 - 1;
133 y1 = fb->height - clip.y2;
134 y2 = fb->height - clip.y1 - 1;
139 x1 = fb->height - clip.y2;
140 x2 = fb->height - clip.y1 - 1;
148 ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
149 ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
150 ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_1, y2);
151 ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_2, y1);
153 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, x_start);
154 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, y_start);
156 ret = mipi_dbi_command_buf(mipi, ILI9225_WRITE_DATA_TO_GRAM, tr,
157 (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
162 static const struct drm_framebuffer_funcs ili9225_fb_funcs = {
163 .destroy = drm_gem_fb_destroy,
164 .create_handle = drm_gem_fb_create_handle,
165 .dirty = tinydrm_fb_dirty,
168 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
169 struct drm_crtc_state *crtc_state,
170 struct drm_plane_state *plane_state)
172 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
173 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
174 struct device *dev = tdev->drm->dev;
180 mipi_dbi_hw_reset(mipi);
183 * There don't seem to be two example init sequences that match, so
184 * using the one from the popular Arduino library for this display.
185 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
188 ret = ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0000);
190 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
193 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0000);
194 ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x0000);
195 ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x0000);
196 ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x0000);
200 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0018);
201 ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x6121);
202 ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x006f);
203 ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x495f);
204 ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0800);
208 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
212 switch (mipi->rotation) {
226 ili9225_command(mipi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
227 ili9225_command(mipi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
228 ili9225_command(mipi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
229 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
230 ili9225_command(mipi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
231 ili9225_command(mipi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
232 ili9225_command(mipi, ILI9225_INTERFACE_CONTROL, 0x0000);
233 ili9225_command(mipi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
234 ili9225_command(mipi, ILI9225_VCI_RECYCLING, 0x0020);
235 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
236 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
238 ili9225_command(mipi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
239 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
240 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
241 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
242 ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
243 ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
245 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_1, 0x0000);
246 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_2, 0x0808);
247 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_3, 0x080a);
248 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_4, 0x000a);
249 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
250 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_6, 0x0808);
251 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_7, 0x0000);
252 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
253 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_9, 0x0710);
254 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_10, 0x0710);
256 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
260 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
262 mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
265 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
267 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
268 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
275 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
277 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
279 ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
281 mipi->enabled = false;
284 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
287 struct spi_device *spi = mipi->spi;
288 unsigned int bpw = 8;
292 gpiod_set_value_cansleep(mipi->dc, 0);
293 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
294 ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
298 if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
301 gpiod_set_value_cansleep(mipi->dc, 1);
302 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
304 return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
307 static const u32 ili9225_formats[] = {
312 static int ili9225_init(struct device *dev, struct mipi_dbi *mipi,
313 const struct drm_simple_display_pipe_funcs *pipe_funcs,
314 struct drm_driver *driver,
315 const struct drm_display_mode *mode,
316 unsigned int rotation)
318 size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
319 struct tinydrm_device *tdev = &mipi->tinydrm;
325 mutex_init(&mipi->cmdlock);
327 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
331 ret = devm_tinydrm_init(dev, tdev, &ili9225_fb_funcs, driver);
335 tdev->fb_dirty = ili9225_fb_dirty;
337 ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
338 DRM_MODE_CONNECTOR_VIRTUAL,
340 ARRAY_SIZE(ili9225_formats), mode,
345 tdev->drm->mode_config.preferred_depth = 16;
346 mipi->rotation = rotation;
348 drm_mode_config_reset(tdev->drm);
350 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
351 tdev->drm->mode_config.preferred_depth, rotation);
356 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
357 .enable = ili9225_pipe_enable,
358 .disable = ili9225_pipe_disable,
359 .update = tinydrm_display_pipe_update,
360 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
363 static const struct drm_display_mode ili9225_mode = {
364 TINYDRM_MODE(176, 220, 35, 44),
367 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
369 static struct drm_driver ili9225_driver = {
370 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
372 .fops = &ili9225_fops,
373 DRM_GEM_CMA_VMAP_DRIVER_OPS,
375 .desc = "Ilitek ILI9225",
381 static const struct of_device_id ili9225_of_match[] = {
382 { .compatible = "vot,v220hf01a-t" },
385 MODULE_DEVICE_TABLE(of, ili9225_of_match);
387 static const struct spi_device_id ili9225_id[] = {
388 { "v220hf01a-t", 0 },
391 MODULE_DEVICE_TABLE(spi, ili9225_id);
393 static int ili9225_probe(struct spi_device *spi)
395 struct device *dev = &spi->dev;
396 struct mipi_dbi *mipi;
397 struct gpio_desc *rs;
401 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
405 mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
406 if (IS_ERR(mipi->reset)) {
407 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
408 return PTR_ERR(mipi->reset);
411 rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
413 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
417 device_property_read_u32(dev, "rotation", &rotation);
419 ret = mipi_dbi_spi_init(spi, mipi, rs);
423 /* override the command function set in mipi_dbi_spi_init() */
424 mipi->command = ili9225_dbi_command;
426 ret = ili9225_init(&spi->dev, mipi, &ili9225_pipe_funcs,
427 &ili9225_driver, &ili9225_mode, rotation);
431 spi_set_drvdata(spi, mipi);
433 return devm_tinydrm_register(&mipi->tinydrm);
436 static void ili9225_shutdown(struct spi_device *spi)
438 struct mipi_dbi *mipi = spi_get_drvdata(spi);
440 tinydrm_shutdown(&mipi->tinydrm);
443 static struct spi_driver ili9225_spi_driver = {
446 .owner = THIS_MODULE,
447 .of_match_table = ili9225_of_match,
449 .id_table = ili9225_id,
450 .probe = ili9225_probe,
451 .shutdown = ili9225_shutdown,
453 module_spi_driver(ili9225_spi_driver);
455 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
456 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
457 MODULE_LICENSE("GPL");