drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / tinydrm / ili9225.c
blob455fefe012f5912e825b4296e8c96b0129b814ea
1 /*
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;
85 u16 x_start, y_start;
86 u16 x1, x2, y1, y2;
87 int ret = 0;
88 bool full;
89 void *tr;
91 if (!mipi->enabled)
92 return 0;
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) {
102 tr = mipi->tx_buf;
103 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
104 if (ret)
105 return ret;
106 } else {
107 tr = cma_obj->vaddr;
110 switch (mipi->rotation) {
111 default:
112 x1 = clip.x1;
113 x2 = clip.x2 - 1;
114 y1 = clip.y1;
115 y2 = clip.y2 - 1;
116 x_start = x1;
117 y_start = y1;
118 break;
119 case 90:
120 x1 = clip.y1;
121 x2 = clip.y2 - 1;
122 y1 = fb->width - clip.x2;
123 y2 = fb->width - clip.x1 - 1;
124 x_start = x1;
125 y_start = y2;
126 break;
127 case 180:
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;
132 x_start = x2;
133 y_start = y2;
134 break;
135 case 270:
136 x1 = fb->height - clip.y2;
137 x2 = fb->height - clip.y1 - 1;
138 y1 = clip.x1;
139 y2 = clip.x2 - 1;
140 x_start = x2;
141 y_start = y1;
142 break;
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);
156 return ret;
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;
172 int ret;
173 u8 am_id;
175 DRM_DEBUG_KMS("\n");
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);
186 if (ret) {
187 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
188 return;
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);
195 msleep(40);
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);
203 msleep(10);
205 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
207 msleep(50);
209 switch (mipi->rotation) {
210 default:
211 am_id = 0x30;
212 break;
213 case 90:
214 am_id = 0x18;
215 break;
216 case 180:
217 am_id = 0x00;
218 break;
219 case 270:
220 am_id = 0x28;
221 break;
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);
255 msleep(50);
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);
267 DRM_DEBUG_KMS("\n");
269 if (!mipi->enabled)
270 return;
272 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
273 msleep(50);
274 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
275 msleep(50);
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,
282 size_t num)
284 struct spi_device *spi = mipi->spi;
285 unsigned int bpw = 8;
286 u32 speed_hz;
287 int ret;
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);
292 if (ret || !num)
293 return ret;
295 if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
296 bpw = 16;
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[] = {
305 DRM_FORMAT_RGB565,
306 DRM_FORMAT_XRGB8888,
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;
317 int ret;
319 if (!mipi->command)
320 return -EINVAL;
322 mutex_init(&mipi->cmdlock);
324 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
325 if (!mipi->tx_buf)
326 return -ENOMEM;
328 ret = devm_tinydrm_init(dev, tdev, &ili9225_fb_funcs, driver);
329 if (ret)
330 return ret;
332 tdev->fb_dirty = ili9225_fb_dirty;
334 ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
335 DRM_MODE_CONNECTOR_VIRTUAL,
336 ili9225_formats,
337 ARRAY_SIZE(ili9225_formats), mode,
338 rotation);
339 if (ret)
340 return ret;
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);
350 return 0;
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 |
368 DRIVER_ATOMIC,
369 .fops = &ili9225_fops,
370 TINYDRM_GEM_DRIVER_OPS,
371 .name = "ili9225",
372 .desc = "Ilitek ILI9225",
373 .date = "20171106",
374 .major = 1,
375 .minor = 0,
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 },
386 { },
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;
395 u32 rotation = 0;
396 int ret;
398 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
399 if (!mipi)
400 return -ENOMEM;
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);
409 if (IS_ERR(rs)) {
410 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
411 return PTR_ERR(rs);
414 device_property_read_u32(dev, "rotation", &rotation);
416 ret = mipi_dbi_spi_init(spi, mipi, rs);
417 if (ret)
418 return ret;
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);
425 if (ret)
426 return ret;
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 = {
441 .driver = {
442 .name = "ili9225",
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");