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_fb_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/tinydrm/mipi-dbi.h>
26 #include <drm/tinydrm/tinydrm-helpers.h>
28 #define ILI9225_DRIVER_READ_CODE 0x00
29 #define ILI9225_DRIVER_OUTPUT_CONTROL 0x01
30 #define ILI9225_LCD_AC_DRIVING_CONTROL 0x02
31 #define ILI9225_ENTRY_MODE 0x03
32 #define ILI9225_DISPLAY_CONTROL_1 0x07
33 #define ILI9225_BLANK_PERIOD_CONTROL_1 0x08
34 #define ILI9225_FRAME_CYCLE_CONTROL 0x0b
35 #define ILI9225_INTERFACE_CONTROL 0x0c
36 #define ILI9225_OSCILLATION_CONTROL 0x0f
37 #define ILI9225_POWER_CONTROL_1 0x10
38 #define ILI9225_POWER_CONTROL_2 0x11
39 #define ILI9225_POWER_CONTROL_3 0x12
40 #define ILI9225_POWER_CONTROL_4 0x13
41 #define ILI9225_POWER_CONTROL_5 0x14
42 #define ILI9225_VCI_RECYCLING 0x15
43 #define ILI9225_RAM_ADDRESS_SET_1 0x20
44 #define ILI9225_RAM_ADDRESS_SET_2 0x21
45 #define ILI9225_WRITE_DATA_TO_GRAM 0x22
46 #define ILI9225_SOFTWARE_RESET 0x28
47 #define ILI9225_GATE_SCAN_CONTROL 0x30
48 #define ILI9225_VERTICAL_SCROLL_1 0x31
49 #define ILI9225_VERTICAL_SCROLL_2 0x32
50 #define ILI9225_VERTICAL_SCROLL_3 0x33
51 #define ILI9225_PARTIAL_DRIVING_POS_1 0x34
52 #define ILI9225_PARTIAL_DRIVING_POS_2 0x35
53 #define ILI9225_HORIZ_WINDOW_ADDR_1 0x36
54 #define ILI9225_HORIZ_WINDOW_ADDR_2 0x37
55 #define ILI9225_VERT_WINDOW_ADDR_1 0x38
56 #define ILI9225_VERT_WINDOW_ADDR_2 0x39
57 #define ILI9225_GAMMA_CONTROL_1 0x50
58 #define ILI9225_GAMMA_CONTROL_2 0x51
59 #define ILI9225_GAMMA_CONTROL_3 0x52
60 #define ILI9225_GAMMA_CONTROL_4 0x53
61 #define ILI9225_GAMMA_CONTROL_5 0x54
62 #define ILI9225_GAMMA_CONTROL_6 0x55
63 #define ILI9225_GAMMA_CONTROL_7 0x56
64 #define ILI9225_GAMMA_CONTROL_8 0x57
65 #define ILI9225_GAMMA_CONTROL_9 0x58
66 #define ILI9225_GAMMA_CONTROL_10 0x59
68 static inline int ili9225_command(struct mipi_dbi
*mipi
, u8 cmd
, u16 data
)
70 u8 par
[2] = { data
>> 8, data
& 0xff };
72 return mipi_dbi_command_buf(mipi
, cmd
, par
, 2);
75 static int ili9225_fb_dirty(struct drm_framebuffer
*fb
,
76 struct drm_file
*file_priv
, unsigned int flags
,
77 unsigned int color
, struct drm_clip_rect
*clips
,
78 unsigned int num_clips
)
80 struct drm_gem_cma_object
*cma_obj
= drm_fb_cma_get_gem_obj(fb
, 0);
81 struct tinydrm_device
*tdev
= fb
->dev
->dev_private
;
82 struct mipi_dbi
*mipi
= mipi_dbi_from_tinydrm(tdev
);
83 bool swap
= mipi
->swap_bytes
;
84 struct drm_clip_rect clip
;
94 full
= tinydrm_merge_clips(&clip
, clips
, num_clips
, flags
,
95 fb
->width
, fb
->height
);
97 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb
->base
.id
,
98 clip
.x1
, clip
.x2
, clip
.y1
, clip
.y2
);
100 if (!mipi
->dc
|| !full
|| swap
||
101 fb
->format
->format
== DRM_FORMAT_XRGB8888
) {
103 ret
= mipi_dbi_buf_copy(mipi
->tx_buf
, fb
, &clip
, swap
);
110 switch (mipi
->rotation
) {
122 y1
= fb
->width
- clip
.x2
;
123 y2
= fb
->width
- clip
.x1
- 1;
128 x1
= fb
->width
- clip
.x2
;
129 x2
= fb
->width
- clip
.x1
- 1;
130 y1
= fb
->height
- clip
.y2
;
131 y2
= fb
->height
- clip
.y1
- 1;
136 x1
= fb
->height
- clip
.y2
;
137 x2
= fb
->height
- clip
.y1
- 1;
145 ili9225_command(mipi
, ILI9225_HORIZ_WINDOW_ADDR_1
, x2
);
146 ili9225_command(mipi
, ILI9225_HORIZ_WINDOW_ADDR_2
, x1
);
147 ili9225_command(mipi
, ILI9225_VERT_WINDOW_ADDR_1
, y2
);
148 ili9225_command(mipi
, ILI9225_VERT_WINDOW_ADDR_2
, y1
);
150 ili9225_command(mipi
, ILI9225_RAM_ADDRESS_SET_1
, x_start
);
151 ili9225_command(mipi
, ILI9225_RAM_ADDRESS_SET_2
, y_start
);
153 ret
= mipi_dbi_command_buf(mipi
, ILI9225_WRITE_DATA_TO_GRAM
, tr
,
154 (clip
.x2
- clip
.x1
) * (clip
.y2
- clip
.y1
) * 2);
159 static const struct drm_framebuffer_funcs ili9225_fb_funcs
= {
160 .destroy
= drm_gem_fb_destroy
,
161 .create_handle
= drm_gem_fb_create_handle
,
162 .dirty
= tinydrm_fb_dirty
,
165 static void ili9225_pipe_enable(struct drm_simple_display_pipe
*pipe
,
166 struct drm_crtc_state
*crtc_state
,
167 struct drm_plane_state
*plane_state
)
169 struct tinydrm_device
*tdev
= pipe_to_tinydrm(pipe
);
170 struct mipi_dbi
*mipi
= mipi_dbi_from_tinydrm(tdev
);
171 struct device
*dev
= tdev
->drm
->dev
;
177 mipi_dbi_hw_reset(mipi
);
180 * There don't seem to be two example init sequences that match, so
181 * using the one from the popular Arduino library for this display.
182 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
185 ret
= ili9225_command(mipi
, ILI9225_POWER_CONTROL_1
, 0x0000);
187 DRM_DEV_ERROR(dev
, "Error sending command %d\n", ret
);
190 ili9225_command(mipi
, ILI9225_POWER_CONTROL_2
, 0x0000);
191 ili9225_command(mipi
, ILI9225_POWER_CONTROL_3
, 0x0000);
192 ili9225_command(mipi
, ILI9225_POWER_CONTROL_4
, 0x0000);
193 ili9225_command(mipi
, ILI9225_POWER_CONTROL_5
, 0x0000);
197 ili9225_command(mipi
, ILI9225_POWER_CONTROL_2
, 0x0018);
198 ili9225_command(mipi
, ILI9225_POWER_CONTROL_3
, 0x6121);
199 ili9225_command(mipi
, ILI9225_POWER_CONTROL_4
, 0x006f);
200 ili9225_command(mipi
, ILI9225_POWER_CONTROL_5
, 0x495f);
201 ili9225_command(mipi
, ILI9225_POWER_CONTROL_1
, 0x0800);
205 ili9225_command(mipi
, ILI9225_POWER_CONTROL_2
, 0x103b);
209 switch (mipi
->rotation
) {
223 ili9225_command(mipi
, ILI9225_DRIVER_OUTPUT_CONTROL
, 0x011c);
224 ili9225_command(mipi
, ILI9225_LCD_AC_DRIVING_CONTROL
, 0x0100);
225 ili9225_command(mipi
, ILI9225_ENTRY_MODE
, 0x1000 | am_id
);
226 ili9225_command(mipi
, ILI9225_DISPLAY_CONTROL_1
, 0x0000);
227 ili9225_command(mipi
, ILI9225_BLANK_PERIOD_CONTROL_1
, 0x0808);
228 ili9225_command(mipi
, ILI9225_FRAME_CYCLE_CONTROL
, 0x1100);
229 ili9225_command(mipi
, ILI9225_INTERFACE_CONTROL
, 0x0000);
230 ili9225_command(mipi
, ILI9225_OSCILLATION_CONTROL
, 0x0d01);
231 ili9225_command(mipi
, ILI9225_VCI_RECYCLING
, 0x0020);
232 ili9225_command(mipi
, ILI9225_RAM_ADDRESS_SET_1
, 0x0000);
233 ili9225_command(mipi
, ILI9225_RAM_ADDRESS_SET_2
, 0x0000);
235 ili9225_command(mipi
, ILI9225_GATE_SCAN_CONTROL
, 0x0000);
236 ili9225_command(mipi
, ILI9225_VERTICAL_SCROLL_1
, 0x00db);
237 ili9225_command(mipi
, ILI9225_VERTICAL_SCROLL_2
, 0x0000);
238 ili9225_command(mipi
, ILI9225_VERTICAL_SCROLL_3
, 0x0000);
239 ili9225_command(mipi
, ILI9225_PARTIAL_DRIVING_POS_1
, 0x00db);
240 ili9225_command(mipi
, ILI9225_PARTIAL_DRIVING_POS_2
, 0x0000);
242 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_1
, 0x0000);
243 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_2
, 0x0808);
244 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_3
, 0x080a);
245 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_4
, 0x000a);
246 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_5
, 0x0a08);
247 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_6
, 0x0808);
248 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_7
, 0x0000);
249 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_8
, 0x0a00);
250 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_9
, 0x0710);
251 ili9225_command(mipi
, ILI9225_GAMMA_CONTROL_10
, 0x0710);
253 ili9225_command(mipi
, ILI9225_DISPLAY_CONTROL_1
, 0x0012);
257 ili9225_command(mipi
, ILI9225_DISPLAY_CONTROL_1
, 0x1017);
259 mipi_dbi_enable_flush(mipi
, crtc_state
, plane_state
);
262 static void ili9225_pipe_disable(struct drm_simple_display_pipe
*pipe
)
264 struct tinydrm_device
*tdev
= pipe_to_tinydrm(pipe
);
265 struct mipi_dbi
*mipi
= mipi_dbi_from_tinydrm(tdev
);
272 ili9225_command(mipi
, ILI9225_DISPLAY_CONTROL_1
, 0x0000);
274 ili9225_command(mipi
, ILI9225_POWER_CONTROL_2
, 0x0007);
276 ili9225_command(mipi
, ILI9225_POWER_CONTROL_1
, 0x0a02);
278 mipi
->enabled
= false;
281 static int ili9225_dbi_command(struct mipi_dbi
*mipi
, u8 cmd
, u8
*par
,
284 struct spi_device
*spi
= mipi
->spi
;
285 unsigned int bpw
= 8;
289 gpiod_set_value_cansleep(mipi
->dc
, 0);
290 speed_hz
= mipi_dbi_spi_cmd_max_speed(spi
, 1);
291 ret
= tinydrm_spi_transfer(spi
, speed_hz
, NULL
, 8, &cmd
, 1);
295 if (cmd
== ILI9225_WRITE_DATA_TO_GRAM
&& !mipi
->swap_bytes
)
298 gpiod_set_value_cansleep(mipi
->dc
, 1);
299 speed_hz
= mipi_dbi_spi_cmd_max_speed(spi
, num
);
301 return tinydrm_spi_transfer(spi
, speed_hz
, NULL
, bpw
, par
, num
);
304 static const u32 ili9225_formats
[] = {
309 static int ili9225_init(struct device
*dev
, struct mipi_dbi
*mipi
,
310 const struct drm_simple_display_pipe_funcs
*pipe_funcs
,
311 struct drm_driver
*driver
,
312 const struct drm_display_mode
*mode
,
313 unsigned int rotation
)
315 size_t bufsize
= mode
->vdisplay
* mode
->hdisplay
* sizeof(u16
);
316 struct tinydrm_device
*tdev
= &mipi
->tinydrm
;
322 mutex_init(&mipi
->cmdlock
);
324 mipi
->tx_buf
= devm_kmalloc(dev
, bufsize
, GFP_KERNEL
);
328 ret
= devm_tinydrm_init(dev
, tdev
, &ili9225_fb_funcs
, driver
);
332 tdev
->fb_dirty
= ili9225_fb_dirty
;
334 ret
= tinydrm_display_pipe_init(tdev
, pipe_funcs
,
335 DRM_MODE_CONNECTOR_VIRTUAL
,
337 ARRAY_SIZE(ili9225_formats
), mode
,
342 tdev
->drm
->mode_config
.preferred_depth
= 16;
343 mipi
->rotation
= rotation
;
345 drm_mode_config_reset(tdev
->drm
);
347 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
348 tdev
->drm
->mode_config
.preferred_depth
, rotation
);
353 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs
= {
354 .enable
= ili9225_pipe_enable
,
355 .disable
= ili9225_pipe_disable
,
356 .update
= tinydrm_display_pipe_update
,
357 .prepare_fb
= drm_gem_fb_simple_display_pipe_prepare_fb
,
360 static const struct drm_display_mode ili9225_mode
= {
361 TINYDRM_MODE(176, 220, 35, 44),
364 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops
);
366 static struct drm_driver ili9225_driver
= {
367 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
|
369 .fops
= &ili9225_fops
,
370 TINYDRM_GEM_DRIVER_OPS
,
372 .desc
= "Ilitek ILI9225",
378 static const struct of_device_id ili9225_of_match
[] = {
379 { .compatible
= "vot,v220hf01a-t" },
382 MODULE_DEVICE_TABLE(of
, ili9225_of_match
);
384 static const struct spi_device_id ili9225_id
[] = {
385 { "v220hf01a-t", 0 },
388 MODULE_DEVICE_TABLE(spi
, ili9225_id
);
390 static int ili9225_probe(struct spi_device
*spi
)
392 struct device
*dev
= &spi
->dev
;
393 struct mipi_dbi
*mipi
;
394 struct gpio_desc
*rs
;
398 mipi
= devm_kzalloc(dev
, sizeof(*mipi
), GFP_KERNEL
);
402 mipi
->reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
403 if (IS_ERR(mipi
->reset
)) {
404 DRM_DEV_ERROR(dev
, "Failed to get gpio 'reset'\n");
405 return PTR_ERR(mipi
->reset
);
408 rs
= devm_gpiod_get(dev
, "rs", GPIOD_OUT_LOW
);
410 DRM_DEV_ERROR(dev
, "Failed to get gpio 'rs'\n");
414 device_property_read_u32(dev
, "rotation", &rotation
);
416 ret
= mipi_dbi_spi_init(spi
, mipi
, rs
);
420 /* override the command function set in mipi_dbi_spi_init() */
421 mipi
->command
= ili9225_dbi_command
;
423 ret
= ili9225_init(&spi
->dev
, mipi
, &ili9225_pipe_funcs
,
424 &ili9225_driver
, &ili9225_mode
, rotation
);
428 spi_set_drvdata(spi
, mipi
);
430 return devm_tinydrm_register(&mipi
->tinydrm
);
433 static void ili9225_shutdown(struct spi_device
*spi
)
435 struct mipi_dbi
*mipi
= spi_get_drvdata(spi
);
437 tinydrm_shutdown(&mipi
->tinydrm
);
440 static struct spi_driver ili9225_spi_driver
= {
443 .owner
= THIS_MODULE
,
444 .of_match_table
= ili9225_of_match
,
446 .id_table
= ili9225_id
,
447 .probe
= ili9225_probe
,
448 .shutdown
= ili9225_shutdown
,
450 module_spi_driver(ili9225_spi_driver
);
452 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
453 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
454 MODULE_LICENSE("GPL");