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