]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/tinydrm/ili9225.c
drm/tinydrm: Remove tinydrm_device
[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_atomic_helper.h>
24 #include <drm/drm_damage_helper.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_fb_cma_helper.h>
27 #include <drm/drm_fb_helper.h>
28 #include <drm/drm_fourcc.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_rect.h>
32 #include <drm/drm_vblank.h>
33 #include <drm/tinydrm/mipi-dbi.h>
34 #include <drm/tinydrm/tinydrm-helpers.h>
35
36 #define ILI9225_DRIVER_READ_CODE        0x00
37 #define ILI9225_DRIVER_OUTPUT_CONTROL   0x01
38 #define ILI9225_LCD_AC_DRIVING_CONTROL  0x02
39 #define ILI9225_ENTRY_MODE              0x03
40 #define ILI9225_DISPLAY_CONTROL_1       0x07
41 #define ILI9225_BLANK_PERIOD_CONTROL_1  0x08
42 #define ILI9225_FRAME_CYCLE_CONTROL     0x0b
43 #define ILI9225_INTERFACE_CONTROL       0x0c
44 #define ILI9225_OSCILLATION_CONTROL     0x0f
45 #define ILI9225_POWER_CONTROL_1         0x10
46 #define ILI9225_POWER_CONTROL_2         0x11
47 #define ILI9225_POWER_CONTROL_3         0x12
48 #define ILI9225_POWER_CONTROL_4         0x13
49 #define ILI9225_POWER_CONTROL_5         0x14
50 #define ILI9225_VCI_RECYCLING           0x15
51 #define ILI9225_RAM_ADDRESS_SET_1       0x20
52 #define ILI9225_RAM_ADDRESS_SET_2       0x21
53 #define ILI9225_WRITE_DATA_TO_GRAM      0x22
54 #define ILI9225_SOFTWARE_RESET          0x28
55 #define ILI9225_GATE_SCAN_CONTROL       0x30
56 #define ILI9225_VERTICAL_SCROLL_1       0x31
57 #define ILI9225_VERTICAL_SCROLL_2       0x32
58 #define ILI9225_VERTICAL_SCROLL_3       0x33
59 #define ILI9225_PARTIAL_DRIVING_POS_1   0x34
60 #define ILI9225_PARTIAL_DRIVING_POS_2   0x35
61 #define ILI9225_HORIZ_WINDOW_ADDR_1     0x36
62 #define ILI9225_HORIZ_WINDOW_ADDR_2     0x37
63 #define ILI9225_VERT_WINDOW_ADDR_1      0x38
64 #define ILI9225_VERT_WINDOW_ADDR_2      0x39
65 #define ILI9225_GAMMA_CONTROL_1         0x50
66 #define ILI9225_GAMMA_CONTROL_2         0x51
67 #define ILI9225_GAMMA_CONTROL_3         0x52
68 #define ILI9225_GAMMA_CONTROL_4         0x53
69 #define ILI9225_GAMMA_CONTROL_5         0x54
70 #define ILI9225_GAMMA_CONTROL_6         0x55
71 #define ILI9225_GAMMA_CONTROL_7         0x56
72 #define ILI9225_GAMMA_CONTROL_8         0x57
73 #define ILI9225_GAMMA_CONTROL_9         0x58
74 #define ILI9225_GAMMA_CONTROL_10        0x59
75
76 static inline int ili9225_command(struct mipi_dbi *mipi, u8 cmd, u16 data)
77 {
78         u8 par[2] = { data >> 8, data & 0xff };
79
80         return mipi_dbi_command_buf(mipi, cmd, par, 2);
81 }
82
83 static void ili9225_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect)
84 {
85         struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
86         struct mipi_dbi *mipi = drm_to_mipi_dbi(fb->dev);
87         unsigned int height = rect->y2 - rect->y1;
88         unsigned int width = rect->x2 - rect->x1;
89         bool swap = mipi->swap_bytes;
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;
98
99         full = width == fb->width && height == fb->height;
100
101         DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
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, rect, swap);
107                 if (ret)
108                         goto err_msg;
109         } else {
110                 tr = cma_obj->vaddr;
111         }
112
113         switch (mipi->rotation) {
114         default:
115                 x1 = rect->x1;
116                 x2 = rect->x2 - 1;
117                 y1 = rect->y1;
118                 y2 = rect->y2 - 1;
119                 x_start = x1;
120                 y_start = y1;
121                 break;
122         case 90:
123                 x1 = rect->y1;
124                 x2 = rect->y2 - 1;
125                 y1 = fb->width - rect->x2;
126                 y2 = fb->width - rect->x1 - 1;
127                 x_start = x1;
128                 y_start = y2;
129                 break;
130         case 180:
131                 x1 = fb->width - rect->x2;
132                 x2 = fb->width - rect->x1 - 1;
133                 y1 = fb->height - rect->y2;
134                 y2 = fb->height - rect->y1 - 1;
135                 x_start = x2;
136                 y_start = y2;
137                 break;
138         case 270:
139                 x1 = fb->height - rect->y2;
140                 x2 = fb->height - rect->y1 - 1;
141                 y1 = rect->x1;
142                 y2 = rect->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                                    width * height * 2);
158 err_msg:
159         if (ret)
160                 dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
161 }
162
163 static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
164                                 struct drm_plane_state *old_state)
165 {
166         struct drm_plane_state *state = pipe->plane.state;
167         struct drm_crtc *crtc = &pipe->crtc;
168         struct drm_rect rect;
169
170         if (drm_atomic_helper_damage_merged(old_state, state, &rect))
171                 ili9225_fb_dirty(state->fb, &rect);
172
173         if (crtc->state->event) {
174                 spin_lock_irq(&crtc->dev->event_lock);
175                 drm_crtc_send_vblank_event(crtc, crtc->state->event);
176                 spin_unlock_irq(&crtc->dev->event_lock);
177                 crtc->state->event = NULL;
178         }
179 }
180
181 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
182                                 struct drm_crtc_state *crtc_state,
183                                 struct drm_plane_state *plane_state)
184 {
185         struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
186         struct drm_framebuffer *fb = plane_state->fb;
187         struct device *dev = pipe->crtc.dev->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 mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
288
289         DRM_DEBUG_KMS("\n");
290
291         if (!mipi->enabled)
292                 return;
293
294         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
295         msleep(50);
296         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
297         msleep(50);
298         ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
299
300         mipi->enabled = false;
301 }
302
303 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
304                                size_t num)
305 {
306         struct spi_device *spi = mipi->spi;
307         unsigned int bpw = 8;
308         u32 speed_hz;
309         int ret;
310
311         gpiod_set_value_cansleep(mipi->dc, 0);
312         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
313         ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
314         if (ret || !num)
315                 return ret;
316
317         if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
318                 bpw = 16;
319
320         gpiod_set_value_cansleep(mipi->dc, 1);
321         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
322
323         return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
324 }
325
326 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
327         .enable         = ili9225_pipe_enable,
328         .disable        = ili9225_pipe_disable,
329         .update         = ili9225_pipe_update,
330         .prepare_fb     = drm_gem_fb_simple_display_pipe_prepare_fb,
331 };
332
333 static const struct drm_display_mode ili9225_mode = {
334         DRM_SIMPLE_MODE(176, 220, 35, 44),
335 };
336
337 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
338
339 static struct drm_driver ili9225_driver = {
340         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
341                                   DRIVER_ATOMIC,
342         .fops                   = &ili9225_fops,
343         .release                = mipi_dbi_release,
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 drm_device *drm;
368         struct mipi_dbi *mipi;
369         struct gpio_desc *rs;
370         u32 rotation = 0;
371         int ret;
372
373         mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
374         if (!mipi)
375                 return -ENOMEM;
376
377         drm = &mipi->drm;
378         ret = devm_drm_dev_init(dev, drm, &ili9225_driver);
379         if (ret) {
380                 kfree(mipi);
381                 return ret;
382         }
383
384         drm_mode_config_init(drm);
385
386         mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
387         if (IS_ERR(mipi->reset)) {
388                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
389                 return PTR_ERR(mipi->reset);
390         }
391
392         rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
393         if (IS_ERR(rs)) {
394                 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
395                 return PTR_ERR(rs);
396         }
397
398         device_property_read_u32(dev, "rotation", &rotation);
399
400         ret = mipi_dbi_spi_init(spi, mipi, rs);
401         if (ret)
402                 return ret;
403
404         /* override the command function set in  mipi_dbi_spi_init() */
405         mipi->command = ili9225_dbi_command;
406
407         ret = mipi_dbi_init(mipi, &ili9225_pipe_funcs, &ili9225_mode, rotation);
408         if (ret)
409                 return ret;
410
411         drm_mode_config_reset(drm);
412
413         ret = drm_dev_register(drm, 0);
414         if (ret)
415                 return ret;
416
417         spi_set_drvdata(spi, drm);
418
419         drm_fbdev_generic_setup(drm, 32);
420
421         return 0;
422 }
423
424 static int ili9225_remove(struct spi_device *spi)
425 {
426         struct drm_device *drm = spi_get_drvdata(spi);
427
428         drm_dev_unplug(drm);
429         drm_atomic_helper_shutdown(drm);
430
431         return 0;
432 }
433
434 static void ili9225_shutdown(struct spi_device *spi)
435 {
436         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
437 }
438
439 static struct spi_driver ili9225_spi_driver = {
440         .driver = {
441                 .name = "ili9225",
442                 .owner = THIS_MODULE,
443                 .of_match_table = ili9225_of_match,
444         },
445         .id_table = ili9225_id,
446         .probe = ili9225_probe,
447         .remove = ili9225_remove,
448         .shutdown = ili9225_shutdown,
449 };
450 module_spi_driver(ili9225_spi_driver);
451
452 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
453 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
454 MODULE_LICENSE("GPL");