drm/tinydrm: Use struct drm_rect
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / tinydrm / ili9225.c
1 /*
2  * DRM driver for Ilitek ILI9225 panels
3  *
4  * Copyright 2017 David Lechner <david@lechnology.com>
5  *
6  * Some code copied from mipi-dbi.c
7  * Copyright 2016 Noralf Trønnes
8  *
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.
13  */
14
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>
22
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/drm_rect.h>
29 #include <drm/tinydrm/mipi-dbi.h>
30 #include <drm/tinydrm/tinydrm-helpers.h>
31
32 #define ILI9225_DRIVER_READ_CODE        0x00
33 #define ILI9225_DRIVER_OUTPUT_CONTROL   0x01
34 #define ILI9225_LCD_AC_DRIVING_CONTROL  0x02
35 #define ILI9225_ENTRY_MODE              0x03
36 #define ILI9225_DISPLAY_CONTROL_1       0x07
37 #define ILI9225_BLANK_PERIOD_CONTROL_1  0x08
38 #define ILI9225_FRAME_CYCLE_CONTROL     0x0b
39 #define ILI9225_INTERFACE_CONTROL       0x0c
40 #define ILI9225_OSCILLATION_CONTROL     0x0f
41 #define ILI9225_POWER_CONTROL_1         0x10
42 #define ILI9225_POWER_CONTROL_2         0x11
43 #define ILI9225_POWER_CONTROL_3         0x12
44 #define ILI9225_POWER_CONTROL_4         0x13
45 #define ILI9225_POWER_CONTROL_5         0x14
46 #define ILI9225_VCI_RECYCLING           0x15
47 #define ILI9225_RAM_ADDRESS_SET_1       0x20
48 #define ILI9225_RAM_ADDRESS_SET_2       0x21
49 #define ILI9225_WRITE_DATA_TO_GRAM      0x22
50 #define ILI9225_SOFTWARE_RESET          0x28
51 #define ILI9225_GATE_SCAN_CONTROL       0x30
52 #define ILI9225_VERTICAL_SCROLL_1       0x31
53 #define ILI9225_VERTICAL_SCROLL_2       0x32
54 #define ILI9225_VERTICAL_SCROLL_3       0x33
55 #define ILI9225_PARTIAL_DRIVING_POS_1   0x34
56 #define ILI9225_PARTIAL_DRIVING_POS_2   0x35
57 #define ILI9225_HORIZ_WINDOW_ADDR_1     0x36
58 #define ILI9225_HORIZ_WINDOW_ADDR_2     0x37
59 #define ILI9225_VERT_WINDOW_ADDR_1      0x38
60 #define ILI9225_VERT_WINDOW_ADDR_2      0x39
61 #define ILI9225_GAMMA_CONTROL_1         0x50
62 #define ILI9225_GAMMA_CONTROL_2         0x51
63 #define ILI9225_GAMMA_CONTROL_3         0x52
64 #define ILI9225_GAMMA_CONTROL_4         0x53
65 #define ILI9225_GAMMA_CONTROL_5         0x54
66 #define ILI9225_GAMMA_CONTROL_6         0x55
67 #define ILI9225_GAMMA_CONTROL_7         0x56
68 #define ILI9225_GAMMA_CONTROL_8         0x57
69 #define ILI9225_GAMMA_CONTROL_9         0x58
70 #define ILI9225_GAMMA_CONTROL_10        0x59
71
72 static inline int ili9225_command(struct mipi_dbi *mipi, u8 cmd, u16 data)
73 {
74         u8 par[2] = { data >> 8, data & 0xff };
75
76         return mipi_dbi_command_buf(mipi, cmd, par, 2);
77 }
78
79 static int ili9225_fb_dirty(struct drm_framebuffer *fb,
80                             struct drm_file *file_priv, unsigned int flags,
81                             unsigned int color, struct drm_clip_rect *clips,
82                             unsigned int num_clips)
83 {
84         struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
85         struct tinydrm_device *tdev = fb->dev->dev_private;
86         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
87         bool swap = mipi->swap_bytes;
88         struct drm_rect clip;
89         struct drm_rect *rect = &clip;
90         u16 x_start, y_start;
91         u16 x1, x2, y1, y2;
92         int ret = 0;
93         bool full;
94         void *tr;
95
96         if (!mipi->enabled)
97                 return 0;
98
99         full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
100                                    fb->width, fb->height);
101
102         DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
103
104         if (!mipi->dc || !full || swap ||
105             fb->format->format == DRM_FORMAT_XRGB8888) {
106                 tr = mipi->tx_buf;
107                 ret = mipi_dbi_buf_copy(mipi->tx_buf, fb, rect, swap);
108                 if (ret)
109                         return ret;
110         } else {
111                 tr = cma_obj->vaddr;
112         }
113
114         switch (mipi->rotation) {
115         default:
116                 x1 = rect->x1;
117                 x2 = rect->x2 - 1;
118                 y1 = rect->y1;
119                 y2 = rect->y2 - 1;
120                 x_start = x1;
121                 y_start = y1;
122                 break;
123         case 90:
124                 x1 = rect->y1;
125                 x2 = rect->y2 - 1;
126                 y1 = fb->width - rect->x2;
127                 y2 = fb->width - rect->x1 - 1;
128                 x_start = x1;
129                 y_start = y2;
130                 break;
131         case 180:
132                 x1 = fb->width - rect->x2;
133                 x2 = fb->width - rect->x1 - 1;
134                 y1 = fb->height - rect->y2;
135                 y2 = fb->height - rect->y1 - 1;
136                 x_start = x2;
137                 y_start = y2;
138                 break;
139         case 270:
140                 x1 = fb->height - rect->y2;
141                 x2 = fb->height - rect->y1 - 1;
142                 y1 = rect->x1;
143                 y2 = rect->x2 - 1;
144                 x_start = x2;
145                 y_start = y1;
146                 break;
147         }
148
149         ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_1, x2);
150         ili9225_command(mipi, ILI9225_HORIZ_WINDOW_ADDR_2, x1);
151         ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_1, y2);
152         ili9225_command(mipi, ILI9225_VERT_WINDOW_ADDR_2, y1);
153
154         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, x_start);
155         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, y_start);
156
157         ret = mipi_dbi_command_buf(mipi, ILI9225_WRITE_DATA_TO_GRAM, tr,
158                                    (rect->x2 - rect->x1) * (rect->y2 - rect->y1) * 2);
159
160         return ret;
161 }
162
163 static const struct drm_framebuffer_funcs ili9225_fb_funcs = {
164         .destroy        = drm_gem_fb_destroy,
165         .create_handle  = drm_gem_fb_create_handle,
166         .dirty          = tinydrm_fb_dirty,
167 };
168
169 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
170                                 struct drm_crtc_state *crtc_state,
171                                 struct drm_plane_state *plane_state)
172 {
173         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
174         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
175         struct device *dev = tdev->drm->dev;
176         int ret;
177         u8 am_id;
178
179         DRM_DEBUG_KMS("\n");
180
181         mipi_dbi_hw_reset(mipi);
182
183         /*
184          * There don't seem to be two example init sequences that match, so
185          * using the one from the popular Arduino library for this display.
186          * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
187          */
188
189         ret = ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0000);
190         if (ret) {
191                 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
192                 return;
193         }
194         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0000);
195         ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x0000);
196         ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x0000);
197         ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x0000);
198
199         msleep(40);
200
201         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0018);
202         ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x6121);
203         ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x006f);
204         ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x495f);
205         ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0800);
206
207         msleep(10);
208
209         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
210
211         msleep(50);
212
213         switch (mipi->rotation) {
214         default:
215                 am_id = 0x30;
216                 break;
217         case 90:
218                 am_id = 0x18;
219                 break;
220         case 180:
221                 am_id = 0x00;
222                 break;
223         case 270:
224                 am_id = 0x28;
225                 break;
226         }
227         ili9225_command(mipi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
228         ili9225_command(mipi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
229         ili9225_command(mipi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
230         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
231         ili9225_command(mipi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
232         ili9225_command(mipi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
233         ili9225_command(mipi, ILI9225_INTERFACE_CONTROL, 0x0000);
234         ili9225_command(mipi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
235         ili9225_command(mipi, ILI9225_VCI_RECYCLING, 0x0020);
236         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
237         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
238
239         ili9225_command(mipi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
240         ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
241         ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
242         ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
243         ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
244         ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
245
246         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_1, 0x0000);
247         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_2, 0x0808);
248         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_3, 0x080a);
249         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_4, 0x000a);
250         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
251         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_6, 0x0808);
252         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_7, 0x0000);
253         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
254         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_9, 0x0710);
255         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_10, 0x0710);
256
257         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
258
259         msleep(50);
260
261         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
262
263         mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
264 }
265
266 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
267 {
268         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
269         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
270
271         DRM_DEBUG_KMS("\n");
272
273         if (!mipi->enabled)
274                 return;
275
276         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
277         msleep(50);
278         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
279         msleep(50);
280         ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
281
282         mipi->enabled = false;
283 }
284
285 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
286                                size_t num)
287 {
288         struct spi_device *spi = mipi->spi;
289         unsigned int bpw = 8;
290         u32 speed_hz;
291         int ret;
292
293         gpiod_set_value_cansleep(mipi->dc, 0);
294         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
295         ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
296         if (ret || !num)
297                 return ret;
298
299         if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
300                 bpw = 16;
301
302         gpiod_set_value_cansleep(mipi->dc, 1);
303         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
304
305         return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
306 }
307
308 static const u32 ili9225_formats[] = {
309         DRM_FORMAT_RGB565,
310         DRM_FORMAT_XRGB8888,
311 };
312
313 static int ili9225_init(struct device *dev, struct mipi_dbi *mipi,
314                         const struct drm_simple_display_pipe_funcs *pipe_funcs,
315                         struct drm_driver *driver,
316                         const struct drm_display_mode *mode,
317                         unsigned int rotation)
318 {
319         size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
320         struct tinydrm_device *tdev = &mipi->tinydrm;
321         int ret;
322
323         if (!mipi->command)
324                 return -EINVAL;
325
326         mutex_init(&mipi->cmdlock);
327
328         mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
329         if (!mipi->tx_buf)
330                 return -ENOMEM;
331
332         ret = devm_tinydrm_init(dev, tdev, &ili9225_fb_funcs, driver);
333         if (ret)
334                 return ret;
335
336         tdev->fb_dirty = ili9225_fb_dirty;
337
338         ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
339                                         DRM_MODE_CONNECTOR_VIRTUAL,
340                                         ili9225_formats,
341                                         ARRAY_SIZE(ili9225_formats), mode,
342                                         rotation);
343         if (ret)
344                 return ret;
345
346         tdev->drm->mode_config.preferred_depth = 16;
347         mipi->rotation = rotation;
348
349         drm_mode_config_reset(tdev->drm);
350
351         DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
352                       tdev->drm->mode_config.preferred_depth, rotation);
353
354         return 0;
355 }
356
357 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
358         .enable         = ili9225_pipe_enable,
359         .disable        = ili9225_pipe_disable,
360         .update         = tinydrm_display_pipe_update,
361         .prepare_fb     = drm_gem_fb_simple_display_pipe_prepare_fb,
362 };
363
364 static const struct drm_display_mode ili9225_mode = {
365         TINYDRM_MODE(176, 220, 35, 44),
366 };
367
368 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
369
370 static struct drm_driver ili9225_driver = {
371         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
372                                   DRIVER_ATOMIC,
373         .fops                   = &ili9225_fops,
374         DRM_GEM_CMA_VMAP_DRIVER_OPS,
375         .name                   = "ili9225",
376         .desc                   = "Ilitek ILI9225",
377         .date                   = "20171106",
378         .major                  = 1,
379         .minor                  = 0,
380 };
381
382 static const struct of_device_id ili9225_of_match[] = {
383         { .compatible = "vot,v220hf01a-t" },
384         {},
385 };
386 MODULE_DEVICE_TABLE(of, ili9225_of_match);
387
388 static const struct spi_device_id ili9225_id[] = {
389         { "v220hf01a-t", 0 },
390         { },
391 };
392 MODULE_DEVICE_TABLE(spi, ili9225_id);
393
394 static int ili9225_probe(struct spi_device *spi)
395 {
396         struct device *dev = &spi->dev;
397         struct mipi_dbi *mipi;
398         struct gpio_desc *rs;
399         u32 rotation = 0;
400         int ret;
401
402         mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
403         if (!mipi)
404                 return -ENOMEM;
405
406         mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
407         if (IS_ERR(mipi->reset)) {
408                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
409                 return PTR_ERR(mipi->reset);
410         }
411
412         rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
413         if (IS_ERR(rs)) {
414                 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
415                 return PTR_ERR(rs);
416         }
417
418         device_property_read_u32(dev, "rotation", &rotation);
419
420         ret = mipi_dbi_spi_init(spi, mipi, rs);
421         if (ret)
422                 return ret;
423
424         /* override the command function set in  mipi_dbi_spi_init() */
425         mipi->command = ili9225_dbi_command;
426
427         ret = ili9225_init(&spi->dev, mipi, &ili9225_pipe_funcs,
428                            &ili9225_driver, &ili9225_mode, rotation);
429         if (ret)
430                 return ret;
431
432         spi_set_drvdata(spi, mipi);
433
434         return devm_tinydrm_register(&mipi->tinydrm);
435 }
436
437 static void ili9225_shutdown(struct spi_device *spi)
438 {
439         struct mipi_dbi *mipi = spi_get_drvdata(spi);
440
441         tinydrm_shutdown(&mipi->tinydrm);
442 }
443
444 static struct spi_driver ili9225_spi_driver = {
445         .driver = {
446                 .name = "ili9225",
447                 .owner = THIS_MODULE,
448                 .of_match_table = ili9225_of_match,
449         },
450         .id_table = ili9225_id,
451         .probe = ili9225_probe,
452         .shutdown = ili9225_shutdown,
453 };
454 module_spi_driver(ili9225_spi_driver);
455
456 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
457 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
458 MODULE_LICENSE("GPL");