]> Git Repo - linux.git/blob - drivers/gpu/drm/tinydrm/ili9225.c
Merge remote-tracking branch 'regmap/fix/core' into regmap-linus
[linux.git] / drivers / gpu / drm / tinydrm / ili9225.c
1 /*
2  * DRM driver for Ilitek ILI9225 panels
3  *
4  * Copyright 2017 David Lechner <[email protected]>
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_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>
27
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
67
68 static inline int ili9225_command(struct mipi_dbi *mipi, u8 cmd, u16 data)
69 {
70         u8 par[2] = { data >> 8, data & 0xff };
71
72         return mipi_dbi_command_buf(mipi, cmd, par, 2);
73 }
74
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)
79 {
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;
90
91         mutex_lock(&tdev->dirty_lock);
92
93         if (!mipi->enabled)
94                 goto out_unlock;
95
96         /* fbdev can flush even when we're not interested */
97         if (tdev->pipe.plane.fb != fb)
98                 goto out_unlock;
99
100         full = tinydrm_merge_clips(&clip, clips, num_clips, flags,
101                                    fb->width, fb->height);
102
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);
105
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;
114         }
115
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;
149         }
150
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);
155
156         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, x_start);
157         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, y_start);
158
159         ret = mipi_dbi_command_buf(mipi, ILI9225_WRITE_DATA_TO_GRAM, tr,
160                                 (clip.x2 - clip.x1) * (clip.y2 - clip.y1) * 2);
161
162 out_unlock:
163         mutex_unlock(&tdev->dirty_lock);
164
165         if (ret)
166                 dev_err_once(fb->dev->dev, "Failed to update display %d\n",
167                              ret);
168
169         return ret;
170 }
171
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,
176 };
177
178 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
179                                 struct drm_crtc_state *crtc_state)
180 {
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;
187
188         DRM_DEBUG_KMS("\n");
189
190         mipi_dbi_hw_reset(mipi);
191
192         /*
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
196          */
197
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;
202         }
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);
207
208         msleep(40);
209
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);
215
216         msleep(10);
217
218         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
219
220         msleep(50);
221
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;
235         }
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);
247
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);
254
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);
265
266         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
267
268         msleep(50);
269
270         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
271
272         mipi->enabled = true;
273
274         if (fb)
275                 fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0);
276 }
277
278 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
279 {
280         struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
281         struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
282
283         DRM_DEBUG_KMS("\n");
284
285         if (!mipi->enabled)
286                 return;
287
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);
293
294         mipi->enabled = false;
295 }
296
297 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
298                                size_t num)
299 {
300         struct spi_device *spi = mipi->spi;
301         unsigned int bpw = 8;
302         u32 speed_hz;
303         int ret;
304
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;
310
311         if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
312                 bpw = 16;
313
314         gpiod_set_value_cansleep(mipi->dc, 1);
315         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
316
317         return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
318 }
319
320 static const u32 ili9225_formats[] = {
321         DRM_FORMAT_RGB565,
322         DRM_FORMAT_XRGB8888,
323 };
324
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)
330 {
331         size_t bufsize = mode->vdisplay * mode->hdisplay * sizeof(u16);
332         struct tinydrm_device *tdev = &mipi->tinydrm;
333         int ret;
334
335         if (!mipi->command)
336                 return -EINVAL;
337
338         mutex_init(&mipi->cmdlock);
339
340         mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
341         if (!mipi->tx_buf)
342                 return -ENOMEM;
343
344         ret = devm_tinydrm_init(dev, tdev, &ili9225_fb_funcs, driver);
345         if (ret)
346                 return ret;
347
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;
355
356         tdev->drm->mode_config.preferred_depth = 16;
357         mipi->rotation = rotation;
358
359         drm_mode_config_reset(tdev->drm);
360
361         DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
362                       tdev->drm->mode_config.preferred_depth, rotation);
363
364         return 0;
365 }
366
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,
372 };
373
374 static const struct drm_display_mode ili9225_mode = {
375         TINYDRM_MODE(176, 220, 35, 44),
376 };
377
378 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
379
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,
391 };
392
393 static const struct of_device_id ili9225_of_match[] = {
394         { .compatible = "vot,v220hf01a-t" },
395         {},
396 };
397 MODULE_DEVICE_TABLE(of, ili9225_of_match);
398
399 static const struct spi_device_id ili9225_id[] = {
400         { "v220hf01a-t", 0 },
401         { },
402 };
403 MODULE_DEVICE_TABLE(spi, ili9225_id);
404
405 static int ili9225_probe(struct spi_device *spi)
406 {
407         struct device *dev = &spi->dev;
408         struct mipi_dbi *mipi;
409         struct gpio_desc *rs;
410         u32 rotation = 0;
411         int ret;
412
413         mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
414         if (!mipi)
415                 return -ENOMEM;
416
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);
421         }
422
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);
427         }
428
429         device_property_read_u32(dev, "rotation", &rotation);
430
431         ret = mipi_dbi_spi_init(spi, mipi, rs);
432         if (ret)
433                 return ret;
434
435         /* override the command function set in  mipi_dbi_spi_init() */
436         mipi->command = ili9225_dbi_command;
437
438         ret = ili9225_init(&spi->dev, mipi, &ili9225_pipe_funcs,
439                            &ili9225_driver, &ili9225_mode, rotation);
440         if (ret)
441                 return ret;
442
443         spi_set_drvdata(spi, mipi);
444
445         return devm_tinydrm_register(&mipi->tinydrm);
446 }
447
448 static void ili9225_shutdown(struct spi_device *spi)
449 {
450         struct mipi_dbi *mipi = spi_get_drvdata(spi);
451
452         tinydrm_shutdown(&mipi->tinydrm);
453 }
454
455 static struct spi_driver ili9225_spi_driver = {
456         .driver = {
457                 .name = "ili9225",
458                 .owner = THIS_MODULE,
459                 .of_match_table = ili9225_of_match,
460         },
461         .id_table = ili9225_id,
462         .probe = ili9225_probe,
463         .shutdown = ili9225_shutdown,
464 };
465 module_spi_driver(ili9225_spi_driver);
466
467 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
468 MODULE_AUTHOR("David Lechner <[email protected]>");
469 MODULE_LICENSE("GPL");
This page took 0.06045 seconds and 4 git commands to generate.