1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019-2020 Icenowy Zheng <icenowy@aosc.io>
6 #include <linux/gpio/consumer.h>
7 #include <linux/delay.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/regulator/consumer.h>
13 #include <drm/drm_mipi_dsi.h>
14 #include <drm/drm_modes.h>
15 #include <drm/drm_panel.h>
17 #define K101_IM2BA02_INIT_CMD_LEN 2
19 static const char * const regulator_names
[] = {
26 struct drm_panel panel
;
27 struct mipi_dsi_device
*dsi
;
29 struct regulator_bulk_data supplies
[ARRAY_SIZE(regulator_names
)];
30 struct gpio_desc
*reset
;
33 static inline struct k101_im2ba02
*panel_to_k101_im2ba02(struct drm_panel
*panel
)
35 return container_of(panel
, struct k101_im2ba02
, panel
);
38 struct k101_im2ba02_init_cmd
{
39 u8 data
[K101_IM2BA02_INIT_CMD_LEN
];
42 static const struct k101_im2ba02_init_cmd k101_im2ba02_init_cmds
[] = {
43 /* Switch to page 0 */
44 { .data
= { 0xE0, 0x00 } },
46 /* Seems to be some password */
47 { .data
= { 0xE1, 0x93} },
48 { .data
= { 0xE2, 0x65 } },
49 { .data
= { 0xE3, 0xF8 } },
51 /* Lane number, 0x02 - 3 lanes, 0x03 - 4 lanes */
52 { .data
= { 0x80, 0x03 } },
54 /* Sequence control */
55 { .data
= { 0x70, 0x02 } },
56 { .data
= { 0x71, 0x23 } },
57 { .data
= { 0x72, 0x06 } },
59 /* Switch to page 1 */
60 { .data
= { 0xE0, 0x01 } },
63 { .data
= { 0x00, 0x00 } },
64 { .data
= { 0x01, 0x66 } },
65 /* Set VCOM_Reverse */
66 { .data
= { 0x03, 0x00 } },
67 { .data
= { 0x04, 0x25 } },
69 /* Set Gamma Power, VG[MS][PN] */
70 { .data
= { 0x17, 0x00 } },
71 { .data
= { 0x18, 0x6D } },
72 { .data
= { 0x19, 0x00 } },
73 { .data
= { 0x1A, 0x00 } },
74 { .data
= { 0x1B, 0xBF } }, /* VGMN = -4.5V */
75 { .data
= { 0x1C, 0x00 } },
78 { .data
= { 0x1F, 0x3E } }, /* VGH_R = 15V */
79 { .data
= { 0x20, 0x28 } }, /* VGL_R = -11V */
80 { .data
= { 0x21, 0x28 } }, /* VGL_R2 = -11V */
81 { .data
= { 0x22, 0x0E } }, /* PA[6:4] = 0, PA[0] = 0 */
84 { .data
= { 0x37, 0x09 } }, /* SS = 1, BGR = 1 */
87 { .data
= { 0x38, 0x04 } }, /* JDT = 100 column inversion */
88 { .data
= { 0x39, 0x08 } }, /* RGB_N_EQ1 */
89 { .data
= { 0x3A, 0x12 } }, /* RGB_N_EQ2 */
90 { .data
= { 0x3C, 0x78 } }, /* set EQ3 for TE_H */
91 { .data
= { 0x3D, 0xFF } }, /* set CHGEN_ON */
92 { .data
= { 0x3E, 0xFF } }, /* set CHGEN_OFF */
93 { .data
= { 0x3F, 0x7F } }, /* set CHGEN_OFF2 */
95 /* Set TCON parameter */
96 { .data
= { 0x40, 0x06 } }, /* RSO = 800 points */
97 { .data
= { 0x41, 0xA0 } }, /* LN = 1280 lines */
99 /* Set power voltage */
100 { .data
= { 0x55, 0x0F } }, /* DCDCM */
101 { .data
= { 0x56, 0x01 } },
102 { .data
= { 0x57, 0x69 } },
103 { .data
= { 0x58, 0x0A } },
104 { .data
= { 0x59, 0x0A } },
105 { .data
= { 0x5A, 0x45 } },
106 { .data
= { 0x5B, 0x15 } },
109 { .data
= { 0x5D, 0x7C } },
110 { .data
= { 0x5E, 0x65 } },
111 { .data
= { 0x5F, 0x55 } },
112 { .data
= { 0x60, 0x49 } },
113 { .data
= { 0x61, 0x44 } },
114 { .data
= { 0x62, 0x35 } },
115 { .data
= { 0x63, 0x3A } },
116 { .data
= { 0x64, 0x23 } },
117 { .data
= { 0x65, 0x3D } },
118 { .data
= { 0x66, 0x3C } },
119 { .data
= { 0x67, 0x3D } },
120 { .data
= { 0x68, 0x5D } },
121 { .data
= { 0x69, 0x4D } },
122 { .data
= { 0x6A, 0x56 } },
123 { .data
= { 0x6B, 0x48 } },
124 { .data
= { 0x6C, 0x45 } },
125 { .data
= { 0x6D, 0x38 } },
126 { .data
= { 0x6E, 0x25 } },
127 { .data
= { 0x6F, 0x00 } },
128 { .data
= { 0x70, 0x7C } },
129 { .data
= { 0x71, 0x65 } },
130 { .data
= { 0x72, 0x55 } },
131 { .data
= { 0x73, 0x49 } },
132 { .data
= { 0x74, 0x44 } },
133 { .data
= { 0x75, 0x35 } },
134 { .data
= { 0x76, 0x3A } },
135 { .data
= { 0x77, 0x23 } },
136 { .data
= { 0x78, 0x3D } },
137 { .data
= { 0x79, 0x3C } },
138 { .data
= { 0x7A, 0x3D } },
139 { .data
= { 0x7B, 0x5D } },
140 { .data
= { 0x7C, 0x4D } },
141 { .data
= { 0x7D, 0x56 } },
142 { .data
= { 0x7E, 0x48 } },
143 { .data
= { 0x7F, 0x45 } },
144 { .data
= { 0x80, 0x38 } },
145 { .data
= { 0x81, 0x25 } },
146 { .data
= { 0x82, 0x00 } },
148 /* Switch to page 2, for GIP */
149 { .data
= { 0xE0, 0x02 } },
151 { .data
= { 0x00, 0x1E } },
152 { .data
= { 0x01, 0x1E } },
153 { .data
= { 0x02, 0x41 } },
154 { .data
= { 0x03, 0x41 } },
155 { .data
= { 0x04, 0x43 } },
156 { .data
= { 0x05, 0x43 } },
157 { .data
= { 0x06, 0x1F } },
158 { .data
= { 0x07, 0x1F } },
159 { .data
= { 0x08, 0x1F } },
160 { .data
= { 0x09, 0x1F } },
161 { .data
= { 0x0A, 0x1E } },
162 { .data
= { 0x0B, 0x1E } },
163 { .data
= { 0x0C, 0x1F } },
164 { .data
= { 0x0D, 0x47 } },
165 { .data
= { 0x0E, 0x47 } },
166 { .data
= { 0x0F, 0x45 } },
167 { .data
= { 0x10, 0x45 } },
168 { .data
= { 0x11, 0x4B } },
169 { .data
= { 0x12, 0x4B } },
170 { .data
= { 0x13, 0x49 } },
171 { .data
= { 0x14, 0x49 } },
172 { .data
= { 0x15, 0x1F } },
174 { .data
= { 0x16, 0x1E } },
175 { .data
= { 0x17, 0x1E } },
176 { .data
= { 0x18, 0x40 } },
177 { .data
= { 0x19, 0x40 } },
178 { .data
= { 0x1A, 0x42 } },
179 { .data
= { 0x1B, 0x42 } },
180 { .data
= { 0x1C, 0x1F } },
181 { .data
= { 0x1D, 0x1F } },
182 { .data
= { 0x1E, 0x1F } },
183 { .data
= { 0x1F, 0x1f } },
184 { .data
= { 0x20, 0x1E } },
185 { .data
= { 0x21, 0x1E } },
186 { .data
= { 0x22, 0x1f } },
187 { .data
= { 0x23, 0x46 } },
188 { .data
= { 0x24, 0x46 } },
189 { .data
= { 0x25, 0x44 } },
190 { .data
= { 0x26, 0x44 } },
191 { .data
= { 0x27, 0x4A } },
192 { .data
= { 0x28, 0x4A } },
193 { .data
= { 0x29, 0x48 } },
194 { .data
= { 0x2A, 0x48 } },
195 { .data
= { 0x2B, 0x1f } },
197 { .data
= { 0x2C, 0x1F } },
198 { .data
= { 0x2D, 0x1F } },
199 { .data
= { 0x2E, 0x42 } },
200 { .data
= { 0x2F, 0x42 } },
201 { .data
= { 0x30, 0x40 } },
202 { .data
= { 0x31, 0x40 } },
203 { .data
= { 0x32, 0x1E } },
204 { .data
= { 0x33, 0x1E } },
205 { .data
= { 0x34, 0x1F } },
206 { .data
= { 0x35, 0x1F } },
207 { .data
= { 0x36, 0x1E } },
208 { .data
= { 0x37, 0x1E } },
209 { .data
= { 0x38, 0x1F } },
210 { .data
= { 0x39, 0x48 } },
211 { .data
= { 0x3A, 0x48 } },
212 { .data
= { 0x3B, 0x4A } },
213 { .data
= { 0x3C, 0x4A } },
214 { .data
= { 0x3D, 0x44 } },
215 { .data
= { 0x3E, 0x44 } },
216 { .data
= { 0x3F, 0x46 } },
217 { .data
= { 0x40, 0x46 } },
218 { .data
= { 0x41, 0x1F } },
220 { .data
= { 0x42, 0x1F } },
221 { .data
= { 0x43, 0x1F } },
222 { .data
= { 0x44, 0x43 } },
223 { .data
= { 0x45, 0x43 } },
224 { .data
= { 0x46, 0x41 } },
225 { .data
= { 0x47, 0x41 } },
226 { .data
= { 0x48, 0x1E } },
227 { .data
= { 0x49, 0x1E } },
228 { .data
= { 0x4A, 0x1E } },
229 { .data
= { 0x4B, 0x1F } },
230 { .data
= { 0x4C, 0x1E } },
231 { .data
= { 0x4D, 0x1E } },
232 { .data
= { 0x4E, 0x1F } },
233 { .data
= { 0x4F, 0x49 } },
234 { .data
= { 0x50, 0x49 } },
235 { .data
= { 0x51, 0x4B } },
236 { .data
= { 0x52, 0x4B } },
237 { .data
= { 0x53, 0x45 } },
238 { .data
= { 0x54, 0x45 } },
239 { .data
= { 0x55, 0x47 } },
240 { .data
= { 0x56, 0x47 } },
241 { .data
= { 0x57, 0x1F } },
243 { .data
= { 0x58, 0x10 } },
244 { .data
= { 0x59, 0x00 } },
245 { .data
= { 0x5A, 0x00 } },
246 { .data
= { 0x5B, 0x30 } },
247 { .data
= { 0x5C, 0x02 } },
248 { .data
= { 0x5D, 0x40 } },
249 { .data
= { 0x5E, 0x01 } },
250 { .data
= { 0x5F, 0x02 } },
251 { .data
= { 0x60, 0x30 } },
252 { .data
= { 0x61, 0x01 } },
253 { .data
= { 0x62, 0x02 } },
254 { .data
= { 0x63, 0x6A } },
255 { .data
= { 0x64, 0x6A } },
256 { .data
= { 0x65, 0x05 } },
257 { .data
= { 0x66, 0x12 } },
258 { .data
= { 0x67, 0x74 } },
259 { .data
= { 0x68, 0x04 } },
260 { .data
= { 0x69, 0x6A } },
261 { .data
= { 0x6A, 0x6A } },
262 { .data
= { 0x6B, 0x08 } },
264 { .data
= { 0x6C, 0x00 } },
265 { .data
= { 0x6D, 0x04 } },
266 { .data
= { 0x6E, 0x04 } },
267 { .data
= { 0x6F, 0x88 } },
268 { .data
= { 0x70, 0x00 } },
269 { .data
= { 0x71, 0x00 } },
270 { .data
= { 0x72, 0x06 } },
271 { .data
= { 0x73, 0x7B } },
272 { .data
= { 0x74, 0x00 } },
273 { .data
= { 0x75, 0x07 } },
274 { .data
= { 0x76, 0x00 } },
275 { .data
= { 0x77, 0x5D } },
276 { .data
= { 0x78, 0x17 } },
277 { .data
= { 0x79, 0x1F } },
278 { .data
= { 0x7A, 0x00 } },
279 { .data
= { 0x7B, 0x00 } },
280 { .data
= { 0x7C, 0x00 } },
281 { .data
= { 0x7D, 0x03 } },
282 { .data
= { 0x7E, 0x7B } },
284 { .data
= { 0xE0, 0x04 } },
285 { .data
= { 0x2B, 0x2B } },
286 { .data
= { 0x2E, 0x44 } },
288 { .data
= { 0xE0, 0x01 } },
289 { .data
= { 0x0E, 0x01 } },
291 { .data
= { 0xE0, 0x03 } },
292 { .data
= { 0x98, 0x2F } },
294 { .data
= { 0xE0, 0x00 } },
295 { .data
= { 0xE6, 0x02 } },
296 { .data
= { 0xE7, 0x02 } },
298 { .data
= { 0x11, 0x00 } },
301 static const struct k101_im2ba02_init_cmd timed_cmds
[] = {
302 { .data
= { 0x29, 0x00 } },
303 { .data
= { 0x35, 0x00 } },
306 static int k101_im2ba02_prepare(struct drm_panel
*panel
)
308 struct k101_im2ba02
*ctx
= panel_to_k101_im2ba02(panel
);
309 struct mipi_dsi_device
*dsi
= ctx
->dsi
;
313 ret
= regulator_bulk_enable(ARRAY_SIZE(ctx
->supplies
), ctx
->supplies
);
319 gpiod_set_value(ctx
->reset
, 1);
322 gpiod_set_value(ctx
->reset
, 0);
325 gpiod_set_value(ctx
->reset
, 1);
328 for (i
= 0; i
< ARRAY_SIZE(k101_im2ba02_init_cmds
); i
++) {
329 const struct k101_im2ba02_init_cmd
*cmd
= &k101_im2ba02_init_cmds
[i
];
331 ret
= mipi_dsi_dcs_write_buffer(dsi
, cmd
->data
, K101_IM2BA02_INIT_CMD_LEN
);
339 gpiod_set_value(ctx
->reset
, 0);
342 return regulator_bulk_disable(ARRAY_SIZE(ctx
->supplies
), ctx
->supplies
);
345 static int k101_im2ba02_enable(struct drm_panel
*panel
)
347 struct k101_im2ba02
*ctx
= panel_to_k101_im2ba02(panel
);
348 const struct k101_im2ba02_init_cmd
*cmd
= &timed_cmds
[1];
353 ret
= mipi_dsi_dcs_set_display_on(ctx
->dsi
);
359 return mipi_dsi_dcs_write_buffer(ctx
->dsi
, cmd
->data
, K101_IM2BA02_INIT_CMD_LEN
);
362 static int k101_im2ba02_disable(struct drm_panel
*panel
)
364 struct k101_im2ba02
*ctx
= panel_to_k101_im2ba02(panel
);
366 return mipi_dsi_dcs_set_display_off(ctx
->dsi
);
369 static int k101_im2ba02_unprepare(struct drm_panel
*panel
)
371 struct k101_im2ba02
*ctx
= panel_to_k101_im2ba02(panel
);
374 ret
= mipi_dsi_dcs_set_display_off(ctx
->dsi
);
376 dev_err(panel
->dev
, "failed to set display off: %d\n", ret
);
378 ret
= mipi_dsi_dcs_enter_sleep_mode(ctx
->dsi
);
380 dev_err(panel
->dev
, "failed to enter sleep mode: %d\n", ret
);
384 gpiod_set_value(ctx
->reset
, 0);
387 return regulator_bulk_disable(ARRAY_SIZE(ctx
->supplies
), ctx
->supplies
);
390 static const struct drm_display_mode k101_im2ba02_default_mode
= {
394 .hsync_start
= 800 + 20,
395 .hsync_end
= 800 + 20 + 20,
396 .htotal
= 800 + 20 + 20 + 20,
399 .vsync_start
= 1280 + 16,
400 .vsync_end
= 1280 + 16 + 4,
401 .vtotal
= 1280 + 16 + 4 + 4,
403 .type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
,
408 static int k101_im2ba02_get_modes(struct drm_panel
*panel
,
409 struct drm_connector
*connector
)
411 struct k101_im2ba02
*ctx
= panel_to_k101_im2ba02(panel
);
412 struct drm_display_mode
*mode
;
414 mode
= drm_mode_duplicate(connector
->dev
, &k101_im2ba02_default_mode
);
416 dev_err(&ctx
->dsi
->dev
, "failed to add mode %ux%u@%u\n",
417 k101_im2ba02_default_mode
.hdisplay
,
418 k101_im2ba02_default_mode
.vdisplay
,
419 drm_mode_vrefresh(&k101_im2ba02_default_mode
));
423 drm_mode_set_name(mode
);
425 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
426 connector
->display_info
.width_mm
= mode
->width_mm
;
427 connector
->display_info
.height_mm
= mode
->height_mm
;
428 drm_mode_probed_add(connector
, mode
);
433 static const struct drm_panel_funcs k101_im2ba02_funcs
= {
434 .disable
= k101_im2ba02_disable
,
435 .unprepare
= k101_im2ba02_unprepare
,
436 .prepare
= k101_im2ba02_prepare
,
437 .enable
= k101_im2ba02_enable
,
438 .get_modes
= k101_im2ba02_get_modes
,
441 static int k101_im2ba02_dsi_probe(struct mipi_dsi_device
*dsi
)
443 struct k101_im2ba02
*ctx
;
447 ctx
= devm_kzalloc(&dsi
->dev
, sizeof(*ctx
), GFP_KERNEL
);
451 mipi_dsi_set_drvdata(dsi
, ctx
);
454 for (i
= 0; i
< ARRAY_SIZE(ctx
->supplies
); i
++)
455 ctx
->supplies
[i
].supply
= regulator_names
[i
];
457 ret
= devm_regulator_bulk_get(&dsi
->dev
, ARRAY_SIZE(ctx
->supplies
),
460 dev_err(&dsi
->dev
, "Couldn't get regulators\n");
464 ctx
->reset
= devm_gpiod_get(&dsi
->dev
, "reset", GPIOD_OUT_LOW
);
465 if (IS_ERR(ctx
->reset
)) {
466 dev_err(&dsi
->dev
, "Couldn't get our reset GPIO\n");
467 return PTR_ERR(ctx
->reset
);
470 drm_panel_init(&ctx
->panel
, &dsi
->dev
, &k101_im2ba02_funcs
,
471 DRM_MODE_CONNECTOR_DSI
);
473 ret
= drm_panel_of_backlight(&ctx
->panel
);
477 drm_panel_add(&ctx
->panel
);
479 dsi
->mode_flags
= MIPI_DSI_MODE_VIDEO
;
480 dsi
->format
= MIPI_DSI_FMT_RGB888
;
483 ret
= mipi_dsi_attach(dsi
);
485 drm_panel_remove(&ctx
->panel
);
492 static int k101_im2ba02_dsi_remove(struct mipi_dsi_device
*dsi
)
494 struct k101_im2ba02
*ctx
= mipi_dsi_get_drvdata(dsi
);
496 mipi_dsi_detach(dsi
);
497 drm_panel_remove(&ctx
->panel
);
502 static const struct of_device_id k101_im2ba02_of_match
[] = {
503 { .compatible
= "feixin,k101-im2ba02", },
506 MODULE_DEVICE_TABLE(of
, k101_im2ba02_of_match
);
508 static struct mipi_dsi_driver k101_im2ba02_driver
= {
509 .probe
= k101_im2ba02_dsi_probe
,
510 .remove
= k101_im2ba02_dsi_remove
,
512 .name
= "feixin-k101-im2ba02",
513 .of_match_table
= k101_im2ba02_of_match
,
516 module_mipi_dsi_driver(k101_im2ba02_driver
);
518 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
519 MODULE_DESCRIPTION("Feixin K101 IM2BA02 MIPI-DSI LCD panel");
520 MODULE_LICENSE("GPL");