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