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