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