]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/tinydrm/ili9225.c
drm/tinydrm: Use drm_dev_enter/exit()
[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 idx, ret = 0;
93         bool full;
94         void *tr;
95
96         if (!mipi->enabled)
97                 return;
98
99         if (!drm_dev_enter(fb->dev, &idx))
100                 return;
101
102         full = width == fb->width && height == fb->height;
103
104         DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
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, rect, swap);
110                 if (ret)
111                         goto err_msg;
112         } else {
113                 tr = cma_obj->vaddr;
114         }
115
116         switch (mipi->rotation) {
117         default:
118                 x1 = rect->x1;
119                 x2 = rect->x2 - 1;
120                 y1 = rect->y1;
121                 y2 = rect->y2 - 1;
122                 x_start = x1;
123                 y_start = y1;
124                 break;
125         case 90:
126                 x1 = rect->y1;
127                 x2 = rect->y2 - 1;
128                 y1 = fb->width - rect->x2;
129                 y2 = fb->width - rect->x1 - 1;
130                 x_start = x1;
131                 y_start = y2;
132                 break;
133         case 180:
134                 x1 = fb->width - rect->x2;
135                 x2 = fb->width - rect->x1 - 1;
136                 y1 = fb->height - rect->y2;
137                 y2 = fb->height - rect->y1 - 1;
138                 x_start = x2;
139                 y_start = y2;
140                 break;
141         case 270:
142                 x1 = fb->height - rect->y2;
143                 x2 = fb->height - rect->y1 - 1;
144                 y1 = rect->x1;
145                 y2 = rect->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                                    width * height * 2);
161 err_msg:
162         if (ret)
163                 dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
164
165         drm_dev_exit(idx);
166 }
167
168 static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe,
169                                 struct drm_plane_state *old_state)
170 {
171         struct drm_plane_state *state = pipe->plane.state;
172         struct drm_crtc *crtc = &pipe->crtc;
173         struct drm_rect rect;
174
175         if (drm_atomic_helper_damage_merged(old_state, state, &rect))
176                 ili9225_fb_dirty(state->fb, &rect);
177
178         if (crtc->state->event) {
179                 spin_lock_irq(&crtc->dev->event_lock);
180                 drm_crtc_send_vblank_event(crtc, crtc->state->event);
181                 spin_unlock_irq(&crtc->dev->event_lock);
182                 crtc->state->event = NULL;
183         }
184 }
185
186 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe,
187                                 struct drm_crtc_state *crtc_state,
188                                 struct drm_plane_state *plane_state)
189 {
190         struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
191         struct drm_framebuffer *fb = plane_state->fb;
192         struct device *dev = pipe->crtc.dev->dev;
193         struct drm_rect rect = {
194                 .x1 = 0,
195                 .x2 = fb->width,
196                 .y1 = 0,
197                 .y2 = fb->height,
198         };
199         int ret, idx;
200         u8 am_id;
201
202         if (!drm_dev_enter(pipe->crtc.dev, &idx))
203                 return;
204
205         DRM_DEBUG_KMS("\n");
206
207         mipi_dbi_hw_reset(mipi);
208
209         /*
210          * There don't seem to be two example init sequences that match, so
211          * using the one from the popular Arduino library for this display.
212          * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp
213          */
214
215         ret = ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0000);
216         if (ret) {
217                 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret);
218                 goto out_exit;
219         }
220         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0000);
221         ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x0000);
222         ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x0000);
223         ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x0000);
224
225         msleep(40);
226
227         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0018);
228         ili9225_command(mipi, ILI9225_POWER_CONTROL_3, 0x6121);
229         ili9225_command(mipi, ILI9225_POWER_CONTROL_4, 0x006f);
230         ili9225_command(mipi, ILI9225_POWER_CONTROL_5, 0x495f);
231         ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0800);
232
233         msleep(10);
234
235         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x103b);
236
237         msleep(50);
238
239         switch (mipi->rotation) {
240         default:
241                 am_id = 0x30;
242                 break;
243         case 90:
244                 am_id = 0x18;
245                 break;
246         case 180:
247                 am_id = 0x00;
248                 break;
249         case 270:
250                 am_id = 0x28;
251                 break;
252         }
253         ili9225_command(mipi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c);
254         ili9225_command(mipi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100);
255         ili9225_command(mipi, ILI9225_ENTRY_MODE, 0x1000 | am_id);
256         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
257         ili9225_command(mipi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808);
258         ili9225_command(mipi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100);
259         ili9225_command(mipi, ILI9225_INTERFACE_CONTROL, 0x0000);
260         ili9225_command(mipi, ILI9225_OSCILLATION_CONTROL, 0x0d01);
261         ili9225_command(mipi, ILI9225_VCI_RECYCLING, 0x0020);
262         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_1, 0x0000);
263         ili9225_command(mipi, ILI9225_RAM_ADDRESS_SET_2, 0x0000);
264
265         ili9225_command(mipi, ILI9225_GATE_SCAN_CONTROL, 0x0000);
266         ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_1, 0x00db);
267         ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_2, 0x0000);
268         ili9225_command(mipi, ILI9225_VERTICAL_SCROLL_3, 0x0000);
269         ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db);
270         ili9225_command(mipi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000);
271
272         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_1, 0x0000);
273         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_2, 0x0808);
274         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_3, 0x080a);
275         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_4, 0x000a);
276         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_5, 0x0a08);
277         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_6, 0x0808);
278         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_7, 0x0000);
279         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_8, 0x0a00);
280         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_9, 0x0710);
281         ili9225_command(mipi, ILI9225_GAMMA_CONTROL_10, 0x0710);
282
283         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0012);
284
285         msleep(50);
286
287         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x1017);
288
289         mipi->enabled = true;
290         ili9225_fb_dirty(fb, &rect);
291 out_exit:
292         drm_dev_exit(idx);
293 }
294
295 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe)
296 {
297         struct mipi_dbi *mipi = drm_to_mipi_dbi(pipe->crtc.dev);
298
299         DRM_DEBUG_KMS("\n");
300
301         /*
302          * This callback is not protected by drm_dev_enter/exit since we want to
303          * turn off the display on regular driver unload. It's highly unlikely
304          * that the underlying SPI controller is gone should this be called after
305          * unplug.
306          */
307
308         if (!mipi->enabled)
309                 return;
310
311         ili9225_command(mipi, ILI9225_DISPLAY_CONTROL_1, 0x0000);
312         msleep(50);
313         ili9225_command(mipi, ILI9225_POWER_CONTROL_2, 0x0007);
314         msleep(50);
315         ili9225_command(mipi, ILI9225_POWER_CONTROL_1, 0x0a02);
316
317         mipi->enabled = false;
318 }
319
320 static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 cmd, u8 *par,
321                                size_t num)
322 {
323         struct spi_device *spi = mipi->spi;
324         unsigned int bpw = 8;
325         u32 speed_hz;
326         int ret;
327
328         gpiod_set_value_cansleep(mipi->dc, 0);
329         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
330         ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, &cmd, 1);
331         if (ret || !num)
332                 return ret;
333
334         if (cmd == ILI9225_WRITE_DATA_TO_GRAM && !mipi->swap_bytes)
335                 bpw = 16;
336
337         gpiod_set_value_cansleep(mipi->dc, 1);
338         speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
339
340         return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num);
341 }
342
343 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
344         .enable         = ili9225_pipe_enable,
345         .disable        = ili9225_pipe_disable,
346         .update         = ili9225_pipe_update,
347         .prepare_fb     = drm_gem_fb_simple_display_pipe_prepare_fb,
348 };
349
350 static const struct drm_display_mode ili9225_mode = {
351         DRM_SIMPLE_MODE(176, 220, 35, 44),
352 };
353
354 DEFINE_DRM_GEM_CMA_FOPS(ili9225_fops);
355
356 static struct drm_driver ili9225_driver = {
357         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
358                                   DRIVER_ATOMIC,
359         .fops                   = &ili9225_fops,
360         .release                = mipi_dbi_release,
361         DRM_GEM_CMA_VMAP_DRIVER_OPS,
362         .name                   = "ili9225",
363         .desc                   = "Ilitek ILI9225",
364         .date                   = "20171106",
365         .major                  = 1,
366         .minor                  = 0,
367 };
368
369 static const struct of_device_id ili9225_of_match[] = {
370         { .compatible = "vot,v220hf01a-t" },
371         {},
372 };
373 MODULE_DEVICE_TABLE(of, ili9225_of_match);
374
375 static const struct spi_device_id ili9225_id[] = {
376         { "v220hf01a-t", 0 },
377         { },
378 };
379 MODULE_DEVICE_TABLE(spi, ili9225_id);
380
381 static int ili9225_probe(struct spi_device *spi)
382 {
383         struct device *dev = &spi->dev;
384         struct drm_device *drm;
385         struct mipi_dbi *mipi;
386         struct gpio_desc *rs;
387         u32 rotation = 0;
388         int ret;
389
390         mipi = kzalloc(sizeof(*mipi), GFP_KERNEL);
391         if (!mipi)
392                 return -ENOMEM;
393
394         drm = &mipi->drm;
395         ret = devm_drm_dev_init(dev, drm, &ili9225_driver);
396         if (ret) {
397                 kfree(mipi);
398                 return ret;
399         }
400
401         drm_mode_config_init(drm);
402
403         mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
404         if (IS_ERR(mipi->reset)) {
405                 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
406                 return PTR_ERR(mipi->reset);
407         }
408
409         rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW);
410         if (IS_ERR(rs)) {
411                 DRM_DEV_ERROR(dev, "Failed to get gpio 'rs'\n");
412                 return PTR_ERR(rs);
413         }
414
415         device_property_read_u32(dev, "rotation", &rotation);
416
417         ret = mipi_dbi_spi_init(spi, mipi, rs);
418         if (ret)
419                 return ret;
420
421         /* override the command function set in  mipi_dbi_spi_init() */
422         mipi->command = ili9225_dbi_command;
423
424         ret = mipi_dbi_init(mipi, &ili9225_pipe_funcs, &ili9225_mode, rotation);
425         if (ret)
426                 return ret;
427
428         drm_mode_config_reset(drm);
429
430         ret = drm_dev_register(drm, 0);
431         if (ret)
432                 return ret;
433
434         spi_set_drvdata(spi, drm);
435
436         drm_fbdev_generic_setup(drm, 32);
437
438         return 0;
439 }
440
441 static int ili9225_remove(struct spi_device *spi)
442 {
443         struct drm_device *drm = spi_get_drvdata(spi);
444
445         drm_dev_unplug(drm);
446         drm_atomic_helper_shutdown(drm);
447
448         return 0;
449 }
450
451 static void ili9225_shutdown(struct spi_device *spi)
452 {
453         drm_atomic_helper_shutdown(spi_get_drvdata(spi));
454 }
455
456 static struct spi_driver ili9225_spi_driver = {
457         .driver = {
458                 .name = "ili9225",
459                 .owner = THIS_MODULE,
460                 .of_match_table = ili9225_of_match,
461         },
462         .id_table = ili9225_id,
463         .probe = ili9225_probe,
464         .remove = ili9225_remove,
465         .shutdown = ili9225_shutdown,
466 };
467 module_spi_driver(ili9225_spi_driver);
468
469 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver");
470 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
471 MODULE_LICENSE("GPL");