1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2018, Bootlin
6 #include <linux/delay.h>
7 #include <linux/device.h>
9 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/regulator/consumer.h>
17 #include <drm/drm_mipi_dsi.h>
18 #include <drm/drm_modes.h>
19 #include <drm/drm_panel.h>
21 #include <video/mipi_display.h>
24 struct drm_panel panel
;
25 struct mipi_dsi_device
*dsi
;
27 struct regulator
*power
;
28 struct gpio_desc
*reset
;
36 struct ili9881c_instr
{
48 #define ILI9881C_SWITCH_PAGE_INSTR(_page) \
50 .op = ILI9881C_SWITCH_PAGE, \
56 #define ILI9881C_COMMAND_INSTR(_cmd, _data) \
58 .op = ILI9881C_COMMAND, \
67 static const struct ili9881c_instr ili9881c_init
[] = {
68 ILI9881C_SWITCH_PAGE_INSTR(3),
69 ILI9881C_COMMAND_INSTR(0x01, 0x00),
70 ILI9881C_COMMAND_INSTR(0x02, 0x00),
71 ILI9881C_COMMAND_INSTR(0x03, 0x73),
72 ILI9881C_COMMAND_INSTR(0x04, 0x03),
73 ILI9881C_COMMAND_INSTR(0x05, 0x00),
74 ILI9881C_COMMAND_INSTR(0x06, 0x06),
75 ILI9881C_COMMAND_INSTR(0x07, 0x06),
76 ILI9881C_COMMAND_INSTR(0x08, 0x00),
77 ILI9881C_COMMAND_INSTR(0x09, 0x18),
78 ILI9881C_COMMAND_INSTR(0x0a, 0x04),
79 ILI9881C_COMMAND_INSTR(0x0b, 0x00),
80 ILI9881C_COMMAND_INSTR(0x0c, 0x02),
81 ILI9881C_COMMAND_INSTR(0x0d, 0x03),
82 ILI9881C_COMMAND_INSTR(0x0e, 0x00),
83 ILI9881C_COMMAND_INSTR(0x0f, 0x25),
84 ILI9881C_COMMAND_INSTR(0x10, 0x25),
85 ILI9881C_COMMAND_INSTR(0x11, 0x00),
86 ILI9881C_COMMAND_INSTR(0x12, 0x00),
87 ILI9881C_COMMAND_INSTR(0x13, 0x00),
88 ILI9881C_COMMAND_INSTR(0x14, 0x00),
89 ILI9881C_COMMAND_INSTR(0x15, 0x00),
90 ILI9881C_COMMAND_INSTR(0x16, 0x0C),
91 ILI9881C_COMMAND_INSTR(0x17, 0x00),
92 ILI9881C_COMMAND_INSTR(0x18, 0x00),
93 ILI9881C_COMMAND_INSTR(0x19, 0x00),
94 ILI9881C_COMMAND_INSTR(0x1a, 0x00),
95 ILI9881C_COMMAND_INSTR(0x1b, 0x00),
96 ILI9881C_COMMAND_INSTR(0x1c, 0x00),
97 ILI9881C_COMMAND_INSTR(0x1d, 0x00),
98 ILI9881C_COMMAND_INSTR(0x1e, 0xC0),
99 ILI9881C_COMMAND_INSTR(0x1f, 0x80),
100 ILI9881C_COMMAND_INSTR(0x20, 0x04),
101 ILI9881C_COMMAND_INSTR(0x21, 0x01),
102 ILI9881C_COMMAND_INSTR(0x22, 0x00),
103 ILI9881C_COMMAND_INSTR(0x23, 0x00),
104 ILI9881C_COMMAND_INSTR(0x24, 0x00),
105 ILI9881C_COMMAND_INSTR(0x25, 0x00),
106 ILI9881C_COMMAND_INSTR(0x26, 0x00),
107 ILI9881C_COMMAND_INSTR(0x27, 0x00),
108 ILI9881C_COMMAND_INSTR(0x28, 0x33),
109 ILI9881C_COMMAND_INSTR(0x29, 0x03),
110 ILI9881C_COMMAND_INSTR(0x2a, 0x00),
111 ILI9881C_COMMAND_INSTR(0x2b, 0x00),
112 ILI9881C_COMMAND_INSTR(0x2c, 0x00),
113 ILI9881C_COMMAND_INSTR(0x2d, 0x00),
114 ILI9881C_COMMAND_INSTR(0x2e, 0x00),
115 ILI9881C_COMMAND_INSTR(0x2f, 0x00),
116 ILI9881C_COMMAND_INSTR(0x30, 0x00),
117 ILI9881C_COMMAND_INSTR(0x31, 0x00),
118 ILI9881C_COMMAND_INSTR(0x32, 0x00),
119 ILI9881C_COMMAND_INSTR(0x33, 0x00),
120 ILI9881C_COMMAND_INSTR(0x34, 0x04),
121 ILI9881C_COMMAND_INSTR(0x35, 0x00),
122 ILI9881C_COMMAND_INSTR(0x36, 0x00),
123 ILI9881C_COMMAND_INSTR(0x37, 0x00),
124 ILI9881C_COMMAND_INSTR(0x38, 0x3C),
125 ILI9881C_COMMAND_INSTR(0x39, 0x00),
126 ILI9881C_COMMAND_INSTR(0x3a, 0x00),
127 ILI9881C_COMMAND_INSTR(0x3b, 0x00),
128 ILI9881C_COMMAND_INSTR(0x3c, 0x00),
129 ILI9881C_COMMAND_INSTR(0x3d, 0x00),
130 ILI9881C_COMMAND_INSTR(0x3e, 0x00),
131 ILI9881C_COMMAND_INSTR(0x3f, 0x00),
132 ILI9881C_COMMAND_INSTR(0x40, 0x00),
133 ILI9881C_COMMAND_INSTR(0x41, 0x00),
134 ILI9881C_COMMAND_INSTR(0x42, 0x00),
135 ILI9881C_COMMAND_INSTR(0x43, 0x00),
136 ILI9881C_COMMAND_INSTR(0x44, 0x00),
137 ILI9881C_COMMAND_INSTR(0x50, 0x01),
138 ILI9881C_COMMAND_INSTR(0x51, 0x23),
139 ILI9881C_COMMAND_INSTR(0x52, 0x45),
140 ILI9881C_COMMAND_INSTR(0x53, 0x67),
141 ILI9881C_COMMAND_INSTR(0x54, 0x89),
142 ILI9881C_COMMAND_INSTR(0x55, 0xab),
143 ILI9881C_COMMAND_INSTR(0x56, 0x01),
144 ILI9881C_COMMAND_INSTR(0x57, 0x23),
145 ILI9881C_COMMAND_INSTR(0x58, 0x45),
146 ILI9881C_COMMAND_INSTR(0x59, 0x67),
147 ILI9881C_COMMAND_INSTR(0x5a, 0x89),
148 ILI9881C_COMMAND_INSTR(0x5b, 0xab),
149 ILI9881C_COMMAND_INSTR(0x5c, 0xcd),
150 ILI9881C_COMMAND_INSTR(0x5d, 0xef),
151 ILI9881C_COMMAND_INSTR(0x5e, 0x11),
152 ILI9881C_COMMAND_INSTR(0x5f, 0x02),
153 ILI9881C_COMMAND_INSTR(0x60, 0x02),
154 ILI9881C_COMMAND_INSTR(0x61, 0x02),
155 ILI9881C_COMMAND_INSTR(0x62, 0x02),
156 ILI9881C_COMMAND_INSTR(0x63, 0x02),
157 ILI9881C_COMMAND_INSTR(0x64, 0x02),
158 ILI9881C_COMMAND_INSTR(0x65, 0x02),
159 ILI9881C_COMMAND_INSTR(0x66, 0x02),
160 ILI9881C_COMMAND_INSTR(0x67, 0x02),
161 ILI9881C_COMMAND_INSTR(0x68, 0x02),
162 ILI9881C_COMMAND_INSTR(0x69, 0x02),
163 ILI9881C_COMMAND_INSTR(0x6a, 0x0C),
164 ILI9881C_COMMAND_INSTR(0x6b, 0x02),
165 ILI9881C_COMMAND_INSTR(0x6c, 0x0F),
166 ILI9881C_COMMAND_INSTR(0x6d, 0x0E),
167 ILI9881C_COMMAND_INSTR(0x6e, 0x0D),
168 ILI9881C_COMMAND_INSTR(0x6f, 0x06),
169 ILI9881C_COMMAND_INSTR(0x70, 0x07),
170 ILI9881C_COMMAND_INSTR(0x71, 0x02),
171 ILI9881C_COMMAND_INSTR(0x72, 0x02),
172 ILI9881C_COMMAND_INSTR(0x73, 0x02),
173 ILI9881C_COMMAND_INSTR(0x74, 0x02),
174 ILI9881C_COMMAND_INSTR(0x75, 0x02),
175 ILI9881C_COMMAND_INSTR(0x76, 0x02),
176 ILI9881C_COMMAND_INSTR(0x77, 0x02),
177 ILI9881C_COMMAND_INSTR(0x78, 0x02),
178 ILI9881C_COMMAND_INSTR(0x79, 0x02),
179 ILI9881C_COMMAND_INSTR(0x7a, 0x02),
180 ILI9881C_COMMAND_INSTR(0x7b, 0x02),
181 ILI9881C_COMMAND_INSTR(0x7c, 0x02),
182 ILI9881C_COMMAND_INSTR(0x7d, 0x02),
183 ILI9881C_COMMAND_INSTR(0x7e, 0x02),
184 ILI9881C_COMMAND_INSTR(0x7f, 0x02),
185 ILI9881C_COMMAND_INSTR(0x80, 0x0C),
186 ILI9881C_COMMAND_INSTR(0x81, 0x02),
187 ILI9881C_COMMAND_INSTR(0x82, 0x0F),
188 ILI9881C_COMMAND_INSTR(0x83, 0x0E),
189 ILI9881C_COMMAND_INSTR(0x84, 0x0D),
190 ILI9881C_COMMAND_INSTR(0x85, 0x06),
191 ILI9881C_COMMAND_INSTR(0x86, 0x07),
192 ILI9881C_COMMAND_INSTR(0x87, 0x02),
193 ILI9881C_COMMAND_INSTR(0x88, 0x02),
194 ILI9881C_COMMAND_INSTR(0x89, 0x02),
195 ILI9881C_COMMAND_INSTR(0x8A, 0x02),
196 ILI9881C_SWITCH_PAGE_INSTR(4),
197 ILI9881C_COMMAND_INSTR(0x6C, 0x15),
198 ILI9881C_COMMAND_INSTR(0x6E, 0x22),
199 ILI9881C_COMMAND_INSTR(0x6F, 0x33),
200 ILI9881C_COMMAND_INSTR(0x3A, 0xA4),
201 ILI9881C_COMMAND_INSTR(0x8D, 0x0D),
202 ILI9881C_COMMAND_INSTR(0x87, 0xBA),
203 ILI9881C_COMMAND_INSTR(0x26, 0x76),
204 ILI9881C_COMMAND_INSTR(0xB2, 0xD1),
205 ILI9881C_SWITCH_PAGE_INSTR(1),
206 ILI9881C_COMMAND_INSTR(0x22, 0x0A),
207 ILI9881C_COMMAND_INSTR(0x53, 0xDC),
208 ILI9881C_COMMAND_INSTR(0x55, 0xA7),
209 ILI9881C_COMMAND_INSTR(0x50, 0x78),
210 ILI9881C_COMMAND_INSTR(0x51, 0x78),
211 ILI9881C_COMMAND_INSTR(0x31, 0x02),
212 ILI9881C_COMMAND_INSTR(0x60, 0x14),
213 ILI9881C_COMMAND_INSTR(0xA0, 0x2A),
214 ILI9881C_COMMAND_INSTR(0xA1, 0x39),
215 ILI9881C_COMMAND_INSTR(0xA2, 0x46),
216 ILI9881C_COMMAND_INSTR(0xA3, 0x0e),
217 ILI9881C_COMMAND_INSTR(0xA4, 0x12),
218 ILI9881C_COMMAND_INSTR(0xA5, 0x25),
219 ILI9881C_COMMAND_INSTR(0xA6, 0x19),
220 ILI9881C_COMMAND_INSTR(0xA7, 0x1d),
221 ILI9881C_COMMAND_INSTR(0xA8, 0xa6),
222 ILI9881C_COMMAND_INSTR(0xA9, 0x1C),
223 ILI9881C_COMMAND_INSTR(0xAA, 0x29),
224 ILI9881C_COMMAND_INSTR(0xAB, 0x85),
225 ILI9881C_COMMAND_INSTR(0xAC, 0x1C),
226 ILI9881C_COMMAND_INSTR(0xAD, 0x1B),
227 ILI9881C_COMMAND_INSTR(0xAE, 0x51),
228 ILI9881C_COMMAND_INSTR(0xAF, 0x22),
229 ILI9881C_COMMAND_INSTR(0xB0, 0x2d),
230 ILI9881C_COMMAND_INSTR(0xB1, 0x4f),
231 ILI9881C_COMMAND_INSTR(0xB2, 0x59),
232 ILI9881C_COMMAND_INSTR(0xB3, 0x3F),
233 ILI9881C_COMMAND_INSTR(0xC0, 0x2A),
234 ILI9881C_COMMAND_INSTR(0xC1, 0x3a),
235 ILI9881C_COMMAND_INSTR(0xC2, 0x45),
236 ILI9881C_COMMAND_INSTR(0xC3, 0x0e),
237 ILI9881C_COMMAND_INSTR(0xC4, 0x11),
238 ILI9881C_COMMAND_INSTR(0xC5, 0x24),
239 ILI9881C_COMMAND_INSTR(0xC6, 0x1a),
240 ILI9881C_COMMAND_INSTR(0xC7, 0x1c),
241 ILI9881C_COMMAND_INSTR(0xC8, 0xaa),
242 ILI9881C_COMMAND_INSTR(0xC9, 0x1C),
243 ILI9881C_COMMAND_INSTR(0xCA, 0x29),
244 ILI9881C_COMMAND_INSTR(0xCB, 0x96),
245 ILI9881C_COMMAND_INSTR(0xCC, 0x1C),
246 ILI9881C_COMMAND_INSTR(0xCD, 0x1B),
247 ILI9881C_COMMAND_INSTR(0xCE, 0x51),
248 ILI9881C_COMMAND_INSTR(0xCF, 0x22),
249 ILI9881C_COMMAND_INSTR(0xD0, 0x2b),
250 ILI9881C_COMMAND_INSTR(0xD1, 0x4b),
251 ILI9881C_COMMAND_INSTR(0xD2, 0x59),
252 ILI9881C_COMMAND_INSTR(0xD3, 0x3F),
255 static inline struct ili9881c
*panel_to_ili9881c(struct drm_panel
*panel
)
257 return container_of(panel
, struct ili9881c
, panel
);
261 * The panel seems to accept some private DCS commands that map
262 * directly to registers.
264 * It is organised by page, with each page having its own set of
265 * registers, and the first page looks like it's holding the standard
268 * So before any attempt at sending a command or data, we have to be
269 * sure if we're in the right page or not.
271 static int ili9881c_switch_page(struct ili9881c
*ctx
, u8 page
)
273 u8 buf
[4] = { 0xff, 0x98, 0x81, page
};
276 ret
= mipi_dsi_dcs_write_buffer(ctx
->dsi
, buf
, sizeof(buf
));
283 static int ili9881c_send_cmd_data(struct ili9881c
*ctx
, u8 cmd
, u8 data
)
285 u8 buf
[2] = { cmd
, data
};
288 ret
= mipi_dsi_dcs_write_buffer(ctx
->dsi
, buf
, sizeof(buf
));
295 static int ili9881c_prepare(struct drm_panel
*panel
)
297 struct ili9881c
*ctx
= panel_to_ili9881c(panel
);
301 /* Power the panel */
302 ret
= regulator_enable(ctx
->power
);
308 gpiod_set_value(ctx
->reset
, 1);
311 gpiod_set_value(ctx
->reset
, 0);
314 for (i
= 0; i
< ARRAY_SIZE(ili9881c_init
); i
++) {
315 const struct ili9881c_instr
*instr
= &ili9881c_init
[i
];
317 if (instr
->op
== ILI9881C_SWITCH_PAGE
)
318 ret
= ili9881c_switch_page(ctx
, instr
->arg
.page
);
319 else if (instr
->op
== ILI9881C_COMMAND
)
320 ret
= ili9881c_send_cmd_data(ctx
, instr
->arg
.cmd
.cmd
,
321 instr
->arg
.cmd
.data
);
327 ret
= ili9881c_switch_page(ctx
, 0);
331 ret
= mipi_dsi_dcs_set_tear_on(ctx
->dsi
, MIPI_DSI_DCS_TEAR_MODE_VBLANK
);
335 ret
= mipi_dsi_dcs_exit_sleep_mode(ctx
->dsi
);
342 static int ili9881c_enable(struct drm_panel
*panel
)
344 struct ili9881c
*ctx
= panel_to_ili9881c(panel
);
348 mipi_dsi_dcs_set_display_on(ctx
->dsi
);
353 static int ili9881c_disable(struct drm_panel
*panel
)
355 struct ili9881c
*ctx
= panel_to_ili9881c(panel
);
357 return mipi_dsi_dcs_set_display_off(ctx
->dsi
);
360 static int ili9881c_unprepare(struct drm_panel
*panel
)
362 struct ili9881c
*ctx
= panel_to_ili9881c(panel
);
364 mipi_dsi_dcs_enter_sleep_mode(ctx
->dsi
);
365 regulator_disable(ctx
->power
);
366 gpiod_set_value(ctx
->reset
, 1);
371 static const struct drm_display_mode bananapi_default_mode
= {
376 .hsync_start
= 720 + 10,
377 .hsync_end
= 720 + 10 + 20,
378 .htotal
= 720 + 10 + 20 + 30,
381 .vsync_start
= 1280 + 10,
382 .vsync_end
= 1280 + 10 + 10,
383 .vtotal
= 1280 + 10 + 10 + 20,
386 static int ili9881c_get_modes(struct drm_panel
*panel
,
387 struct drm_connector
*connector
)
389 struct ili9881c
*ctx
= panel_to_ili9881c(panel
);
390 struct drm_display_mode
*mode
;
392 mode
= drm_mode_duplicate(connector
->dev
, &bananapi_default_mode
);
394 dev_err(&ctx
->dsi
->dev
, "failed to add mode %ux%ux@%u\n",
395 bananapi_default_mode
.hdisplay
,
396 bananapi_default_mode
.vdisplay
,
397 bananapi_default_mode
.vrefresh
);
401 drm_mode_set_name(mode
);
403 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
404 drm_mode_probed_add(connector
, mode
);
406 connector
->display_info
.width_mm
= 62;
407 connector
->display_info
.height_mm
= 110;
412 static const struct drm_panel_funcs ili9881c_funcs
= {
413 .prepare
= ili9881c_prepare
,
414 .unprepare
= ili9881c_unprepare
,
415 .enable
= ili9881c_enable
,
416 .disable
= ili9881c_disable
,
417 .get_modes
= ili9881c_get_modes
,
420 static int ili9881c_dsi_probe(struct mipi_dsi_device
*dsi
)
422 struct ili9881c
*ctx
;
425 ctx
= devm_kzalloc(&dsi
->dev
, sizeof(*ctx
), GFP_KERNEL
);
428 mipi_dsi_set_drvdata(dsi
, ctx
);
431 drm_panel_init(&ctx
->panel
, &dsi
->dev
, &ili9881c_funcs
,
432 DRM_MODE_CONNECTOR_DSI
);
434 ctx
->power
= devm_regulator_get(&dsi
->dev
, "power");
435 if (IS_ERR(ctx
->power
)) {
436 dev_err(&dsi
->dev
, "Couldn't get our power regulator\n");
437 return PTR_ERR(ctx
->power
);
440 ctx
->reset
= devm_gpiod_get(&dsi
->dev
, "reset", GPIOD_OUT_LOW
);
441 if (IS_ERR(ctx
->reset
)) {
442 dev_err(&dsi
->dev
, "Couldn't get our reset GPIO\n");
443 return PTR_ERR(ctx
->reset
);
446 ret
= drm_panel_of_backlight(&ctx
->panel
);
450 ret
= drm_panel_add(&ctx
->panel
);
454 dsi
->mode_flags
= MIPI_DSI_MODE_VIDEO_SYNC_PULSE
;
455 dsi
->format
= MIPI_DSI_FMT_RGB888
;
458 return mipi_dsi_attach(dsi
);
461 static int ili9881c_dsi_remove(struct mipi_dsi_device
*dsi
)
463 struct ili9881c
*ctx
= mipi_dsi_get_drvdata(dsi
);
465 mipi_dsi_detach(dsi
);
466 drm_panel_remove(&ctx
->panel
);
471 static const struct of_device_id ili9881c_of_match
[] = {
472 { .compatible
= "bananapi,lhr050h41" },
475 MODULE_DEVICE_TABLE(of
, ili9881c_of_match
);
477 static struct mipi_dsi_driver ili9881c_dsi_driver
= {
478 .probe
= ili9881c_dsi_probe
,
479 .remove
= ili9881c_dsi_remove
,
481 .name
= "ili9881c-dsi",
482 .of_match_table
= ili9881c_of_match
,
485 module_mipi_dsi_driver(ili9881c_dsi_driver
);
487 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
488 MODULE_DESCRIPTION("Ilitek ILI9881C Controller Driver");
489 MODULE_LICENSE("GPL v2");