Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / tinydrm / ili9225.c
blobc0cf498493023d10f71f6b05970a0ddc1aa5e03f
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 mutex_lock(&tdev->dirty_lock);
93 if (!mipi->enabled)
94 goto out_unlock;
96 /* fbdev can flush even when we're not interested */
97 if (tdev->pipe.plane.fb != fb)
98 goto out_unlock;
100 full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
101 fb->width, fb->height);
103 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
104 clip.x1, clip.x2, clip.y1, clip.y2);
106 if (!mipi->dc || !full || swap ||
107 fb->format->format == DRM_FORMAT_XRGB8888) {
108 tr = mipi->tx_buf;
109 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, &clip, swap);
110 if (ret)
111 goto out_unlock;
112 } else {
113 tr = cma_obj->vaddr;
116 switch (mipi->rotation) {
117 default:
118 x1 = clip.x1;
119 x2 = clip.x2 - 1;
120 y1 = clip.y1;
121 y2 = clip.y2 - 1;
122 x_start = x1;
123 y_start = y1;
124 break;
125 case 90:
126 x1 = clip.y1;
127 x2 = clip.y2 - 1;
128 y1 = fb->width - clip.x2;
129 y2 = fb->width - clip.x1 - 1;
130 x_start = x1;
131 y_start = y2;
132 break;
133 case 180:
134 x1 = fb->width - clip.x2;
135 x2 = fb->width - clip.x1 - 1;
136 y1 = fb->height - clip.y2;
137 y2 = fb->height - clip.y1 - 1;
138 x_start = x2;
139 y_start = y2;
140 break;
141 case 270:
142 x1 = fb->height - clip.y2;
143 x2 = fb->height - clip.y1 - 1;
144 y1 = clip.x1;
145 y2 = clip.x2 - 1;
146 x_start = x2;
147 y_start = y1;
148 break;
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,
160 (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
162 out_unlock:
163 mutex_unlock(&tdev->dirty_lock);
165 if (ret)
166 dev_err_once(fb->dev->dev, "Failed to update display %d\n",
167 ret);
169 return ret;
172 static const struct drm_framebuffer_funcs ili9225_fb_funcs = {
173 .destroy = drm_gem_fb_destroy,
174 .create_handle = drm_gem_fb_create_handle,
175 .dirty = ili9225_fb_dirty,
178 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
179 struct drm_crtc_state *crtc_state)
181 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
182 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
183 struct drm_framebuffer *fb = pipe->plane.fb;
184 struct device *dev = tdev->drm->dev;
185 int ret;
186 u8 am_id;
188 DRM_DEBUG_KMS("\n");
190 mipi_dbi_hw_reset(mipi);
193 * There don't seem to be two example init sequences that match, so
194 * using the one from the popular Arduino library for this display.
195 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
198 ret = ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0000);
199 if (ret) {
200 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
201 return;
203 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0000);
204 ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x0000);
205 ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x0000);
206 ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x0000);
208 msleep(40);
210 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0018);
211 ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x6121);
212 ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x006f);
213 ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x495f);
214 ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0800);
216 msleep(10);
218 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
220 msleep(50);
222 switch (mipi->rotation) {
223 default:
224 am_id = 0x30;
225 break;
226 case 90:
227 am_id = 0x18;
228 break;
229 case 180:
230 am_id = 0x00;
231 break;
232 case 270:
233 am_id = 0x28;
234 break;
236 ili9225_command(mipi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
237 ili9225_command(mipi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
238 ili9225_command(mipi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
239 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
240 ili9225_command(mipi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
241 ili9225_command(mipi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
242 ili9225_command(mipi, ILI9225_INTERFACE_CONTROL, 0x0000);
243 ili9225_command(mipi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
244 ili9225_command(mipi, ILI9225_VCI_RECYCLING, 0x0020);
245 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
246 ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
248 ili9225_command(mipi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
249 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
250 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
251 ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
252 ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
253 ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
255 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_1, 0x0000);
256 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_2, 0x0808);
257 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_3, 0x080a);
258 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_4, 0x000a);
259 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
260 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_6, 0x0808);
261 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_7, 0x0000);
262 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
263 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_9, 0x0710);
264 ili9225_command(mipi, ILI9225_GAMMA_CONTROL_10, 0x0710);
266 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
268 msleep(50);
270 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
272 mipi->enabled = true;
274 if (fb)
275 fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0);
278 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
280 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
281 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
283 DRM_DEBUG_KMS("\n");
285 if (!mipi->enabled)
286 return;
288 ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
289 msleep(50);
290 ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
291 msleep(50);
292 ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
294 mipi->enabled = false;
297 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
298 size_t num)
300 struct spi_device *spi = mipi->spi;
301 unsigned int bpw = 8;
302 u32 speed_hz;
303 int ret;
305 gpiod_set_value_cansleep(mipi->dc, 0);
306 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
307 ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
308 if (ret || !num)
309 return ret;
311 if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
312 bpw = 16;
314 gpiod_set_value_cansleep(mipi->dc, 1);
315 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
317 return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
320 static const u32 ili9225_formats[] = {
321 DRM_FORMAT_RGB565,
322 DRM_FORMAT_XRGB8888,
325 static int ili9225_init(struct device *dev, struct mipi_dbi *mipi,
326 const struct drm_simple_display_pipe_funcs *pipe_funcs,
327 struct drm_driver *driver,
328 const struct drm_display_mode *mode,
329 unsigned int rotation)
331 size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
332 struct tinydrm_device *tdev = &mipi->tinydrm;
333 int ret;
335 if (!mipi->command)
336 return -EINVAL;
338 mutex_init(&mipi->cmdlock);
340 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
341 if (!mipi->tx_buf)
342 return -ENOMEM;
344 ret = devm_tinydrm_init(dev, tdev, &ili9225_fb_funcs, driver);
345 if (ret)
346 return ret;
348 ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
349 DRM_MODE_CONNECTOR_VIRTUAL,
350 ili9225_formats,
351 ARRAY_SIZE(ili9225_formats), mode,
352 rotation);
353 if (ret)
354 return ret;
356 tdev->drm->mode_config.preferred_depth = 16;
357 mipi->rotation = rotation;
359 drm_mode_config_reset(tdev->drm);
361 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
362 tdev->drm->mode_config.preferred_depth, rotation);
364 return 0;
367 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
368 .enable = ili9225_pipe_enable,
369 .disable = ili9225_pipe_disable,
370 .update = tinydrm_display_pipe_update,
371 .prepare_fb = tinydrm_display_pipe_prepare_fb,
374 static const struct drm_display_mode ili9225_mode = {
375 TINYDRM_MODE(176, 220, 35, 44),
378 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
380 static struct drm_driver ili9225_driver = {
381 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
382 DRIVER_ATOMIC,
383 .fops = &ili9225_fops,
384 TINYDRM_GEM_DRIVER_OPS,
385 .lastclose = drm_fb_helper_lastclose,
386 .name = "ili9225",
387 .desc = "Ilitek ILI9225",
388 .date = "20171106",
389 .major = 1,
390 .minor = 0,
393 static const struct of_device_id ili9225_of_match[] = {
394 { .compatible = "vot,v220hf01a-t" },
397 MODULE_DEVICE_TABLE(of, ili9225_of_match);
399 static const struct spi_device_id ili9225_id[] = {
400 { "v220hf01a-t", 0 },
401 { },
403 MODULE_DEVICE_TABLE(spi, ili9225_id);
405 static int ili9225_probe(struct spi_device *spi)
407 struct device *dev = &spi->dev;
408 struct mipi_dbi *mipi;
409 struct gpio_desc *rs;
410 u32 rotation = 0;
411 int ret;
413 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
414 if (!mipi)
415 return -ENOMEM;
417 mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
418 if (IS_ERR(mipi->reset)) {
419 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
420 return PTR_ERR(mipi->reset);
423 rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
424 if (IS_ERR(rs)) {
425 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
426 return PTR_ERR(rs);
429 device_property_read_u32(dev, "rotation", &rotation);
431 ret = mipi_dbi_spi_init(spi, mipi, rs);
432 if (ret)
433 return ret;
435 /* override the command function set in mipi_dbi_spi_init() */
436 mipi->command = ili9225_dbi_command;
438 ret = ili9225_init(&spi->dev, mipi, &ili9225_pipe_funcs,
439 &ili9225_driver, &ili9225_mode, rotation);
440 if (ret)
441 return ret;
443 spi_set_drvdata(spi, mipi);
445 return devm_tinydrm_register(&mipi->tinydrm);
448 static void ili9225_shutdown(struct spi_device *spi)
450 struct mipi_dbi *mipi = spi_get_drvdata(spi);
452 tinydrm_shutdown(&mipi->tinydrm);
455 static struct spi_driver ili9225_spi_driver = {
456 .driver = {
457 .name = "ili9225",
458 .owner = THIS_MODULE,
459 .of_match_table = ili9225_of_match,
461 .id_table = ili9225_id,
462 .probe = ili9225_probe,
463 .shutdown = ili9225_shutdown,
465 module_spi_driver(ili9225_spi_driver);
467 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
468 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
469 MODULE_LICENSE("GPL");