1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Raydium RM67191 MIPI-DSI panel driver
4 *
5 * Copyright 2019 NXP
6 */
7
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/media-bus-format.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/regulator/consumer.h>
15
16 #include <video/mipi_display.h>
17 #include <video/of_videomode.h>
18 #include <video/videomode.h>
19
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_mipi_dsi.h>
22 #include <drm/drm_panel.h>
23
24 /* Panel specific color-format bits */
25 #define COL_FMT_16BPP 0x55
26 #define COL_FMT_18BPP 0x66
27 #define COL_FMT_24BPP 0x77
28
29 /* Write Manufacture Command Set Control */
30 #define WRMAUCCTR 0xFE
31
32 /* Manufacturer Command Set pages (CMD2) */
33 struct cmd_set_entry {
34 u8 cmd;
35 u8 param;
36 };
37
38 /*
39 * There is no description in the Reference Manual about these commands.
40 * We received them from vendor, so just use them as is.
41 */
42 static const struct cmd_set_entry manufacturer_cmd_set[] = {
43 {0xFE, 0x0B},
44 {0x28, 0x40},
45 {0x29, 0x4F},
46 {0xFE, 0x0E},
47 {0x4B, 0x00},
48 {0x4C, 0x0F},
49 {0x4D, 0x20},
50 {0x4E, 0x40},
51 {0x4F, 0x60},
52 {0x50, 0xA0},
53 {0x51, 0xC0},
54 {0x52, 0xE0},
55 {0x53, 0xFF},
56 {0xFE, 0x0D},
57 {0x18, 0x08},
58 {0x42, 0x00},
59 {0x08, 0x41},
60 {0x46, 0x02},
61 {0x72, 0x09},
62 {0xFE, 0x0A},
63 {0x24, 0x17},
64 {0x04, 0x07},
65 {0x1A, 0x0C},
66 {0x0F, 0x44},
67 {0xFE, 0x04},
68 {0x00, 0x0C},
69 {0x05, 0x08},
70 {0x06, 0x08},
71 {0x08, 0x08},
72 {0x09, 0x08},
73 {0x0A, 0xE6},
74 {0x0B, 0x8C},
75 {0x1A, 0x12},
76 {0x1E, 0xE0},
77 {0x29, 0x93},
78 {0x2A, 0x93},
79 {0x2F, 0x02},
80 {0x31, 0x02},
81 {0x33, 0x05},
82 {0x37, 0x2D},
83 {0x38, 0x2D},
84 {0x3A, 0x1E},
85 {0x3B, 0x1E},
86 {0x3D, 0x27},
87 {0x3F, 0x80},
88 {0x40, 0x40},
89 {0x41, 0xE0},
90 {0x4F, 0x2F},
91 {0x50, 0x1E},
92 {0xFE, 0x06},
93 {0x00, 0xCC},
94 {0x05, 0x05},
95 {0x07, 0xA2},
96 {0x08, 0xCC},
97 {0x0D, 0x03},
98 {0x0F, 0xA2},
99 {0x32, 0xCC},
100 {0x37, 0x05},
101 {0x39, 0x83},
102 {0x3A, 0xCC},
103 {0x41, 0x04},
104 {0x43, 0x83},
105 {0x44, 0xCC},
106 {0x49, 0x05},
107 {0x4B, 0xA2},
108 {0x4C, 0xCC},
109 {0x51, 0x03},
110 {0x53, 0xA2},
111 {0x75, 0xCC},
112 {0x7A, 0x03},
113 {0x7C, 0x83},
114 {0x7D, 0xCC},
115 {0x82, 0x02},
116 {0x84, 0x83},
117 {0x85, 0xEC},
118 {0x86, 0x0F},
119 {0x87, 0xFF},
120 {0x88, 0x00},
121 {0x8A, 0x02},
122 {0x8C, 0xA2},
123 {0x8D, 0xEA},
124 {0x8E, 0x01},
125 {0x8F, 0xE8},
126 {0xFE, 0x06},
127 {0x90, 0x0A},
128 {0x92, 0x06},
129 {0x93, 0xA0},
130 {0x94, 0xA8},
131 {0x95, 0xEC},
132 {0x96, 0x0F},
133 {0x97, 0xFF},
134 {0x98, 0x00},
135 {0x9A, 0x02},
136 {0x9C, 0xA2},
137 {0xAC, 0x04},
138 {0xFE, 0x06},
139 {0xB1, 0x12},
140 {0xB2, 0x17},
141 {0xB3, 0x17},
142 {0xB4, 0x17},
143 {0xB5, 0x17},
144 {0xB6, 0x11},
145 {0xB7, 0x08},
146 {0xB8, 0x09},
147 {0xB9, 0x06},
148 {0xBA, 0x07},
149 {0xBB, 0x17},
150 {0xBC, 0x17},
151 {0xBD, 0x17},
152 {0xBE, 0x17},
153 {0xBF, 0x17},
154 {0xC0, 0x17},
155 {0xC1, 0x17},
156 {0xC2, 0x17},
157 {0xC3, 0x17},
158 {0xC4, 0x0F},
159 {0xC5, 0x0E},
160 {0xC6, 0x00},
161 {0xC7, 0x01},
162 {0xC8, 0x10},
163 {0xFE, 0x06},
164 {0x95, 0xEC},
165 {0x8D, 0xEE},
166 {0x44, 0xEC},
167 {0x4C, 0xEC},
168 {0x32, 0xEC},
169 {0x3A, 0xEC},
170 {0x7D, 0xEC},
171 {0x75, 0xEC},
172 {0x00, 0xEC},
173 {0x08, 0xEC},
174 {0x85, 0xEC},
175 {0xA6, 0x21},
176 {0xA7, 0x05},
177 {0xA9, 0x06},
178 {0x82, 0x06},
179 {0x41, 0x06},
180 {0x7A, 0x07},
181 {0x37, 0x07},
182 {0x05, 0x06},
183 {0x49, 0x06},
184 {0x0D, 0x04},
185 {0x51, 0x04},
186 };
187
188 static const u32 rad_bus_formats[] = {
189 MEDIA_BUS_FMT_RGB888_1X24,
190 MEDIA_BUS_FMT_RGB666_1X18,
191 MEDIA_BUS_FMT_RGB565_1X16,
192 };
193
194 static const u32 rad_bus_flags = DRM_BUS_FLAG_DE_LOW |
195 DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE;
196
197 struct rad_panel {
198 struct drm_panel panel;
199 struct mipi_dsi_device *dsi;
200
201 struct gpio_desc *reset;
202 struct backlight_device *backlight;
203
204 struct regulator_bulk_data *supplies;
205 unsigned int num_supplies;
206
207 bool prepared;
208 };
209
210 static const struct drm_display_mode default_mode = {
211 .clock = 132000,
212 .hdisplay = 1080,
213 .hsync_start = 1080 + 20,
214 .hsync_end = 1080 + 20 + 2,
215 .htotal = 1080 + 20 + 2 + 34,
216 .vdisplay = 1920,
217 .vsync_start = 1920 + 10,
218 .vsync_end = 1920 + 10 + 2,
219 .vtotal = 1920 + 10 + 2 + 4,
220 .width_mm = 68,
221 .height_mm = 121,
222 .flags = DRM_MODE_FLAG_NHSYNC |
223 DRM_MODE_FLAG_NVSYNC,
224 };
225
to_rad_panel(struct drm_panel * panel)226 static inline struct rad_panel *to_rad_panel(struct drm_panel *panel)
227 {
228 return container_of(panel, struct rad_panel, panel);
229 }
230
rad_panel_push_cmd_list(struct mipi_dsi_device * dsi)231 static int rad_panel_push_cmd_list(struct mipi_dsi_device *dsi)
232 {
233 size_t i;
234 size_t count = ARRAY_SIZE(manufacturer_cmd_set);
235 int ret = 0;
236
237 for (i = 0; i < count; i++) {
238 const struct cmd_set_entry *entry = &manufacturer_cmd_set[i];
239 u8 buffer[2] = { entry->cmd, entry->param };
240
241 ret = mipi_dsi_generic_write(dsi, &buffer, sizeof(buffer));
242 if (ret < 0)
243 return ret;
244 }
245
246 return ret;
247 };
248
color_format_from_dsi_format(enum mipi_dsi_pixel_format format)249 static int color_format_from_dsi_format(enum mipi_dsi_pixel_format format)
250 {
251 switch (format) {
252 case MIPI_DSI_FMT_RGB565:
253 return COL_FMT_16BPP;
254 case MIPI_DSI_FMT_RGB666:
255 case MIPI_DSI_FMT_RGB666_PACKED:
256 return COL_FMT_18BPP;
257 case MIPI_DSI_FMT_RGB888:
258 return COL_FMT_24BPP;
259 default:
260 return COL_FMT_24BPP; /* for backward compatibility */
261 }
262 };
263
rad_panel_prepare(struct drm_panel * panel)264 static int rad_panel_prepare(struct drm_panel *panel)
265 {
266 struct rad_panel *rad = to_rad_panel(panel);
267 int ret;
268
269 ret = regulator_bulk_enable(rad->num_supplies, rad->supplies);
270 if (ret)
271 return ret;
272
273 if (rad->reset) {
274 gpiod_set_value_cansleep(rad->reset, 1);
275 usleep_range(3000, 5000);
276 gpiod_set_value_cansleep(rad->reset, 0);
277 usleep_range(18000, 20000);
278 }
279
280 rad->prepared = true;
281
282 return 0;
283 }
284
rad_panel_unprepare(struct drm_panel * panel)285 static int rad_panel_unprepare(struct drm_panel *panel)
286 {
287 struct rad_panel *rad = to_rad_panel(panel);
288 int ret;
289
290 /*
291 * Right after asserting the reset, we need to release it, so that the
292 * touch driver can have an active connection with the touch controller
293 * even after the display is turned off.
294 */
295 if (rad->reset) {
296 gpiod_set_value_cansleep(rad->reset, 1);
297 usleep_range(15000, 17000);
298 gpiod_set_value_cansleep(rad->reset, 0);
299 }
300
301 ret = regulator_bulk_disable(rad->num_supplies, rad->supplies);
302 if (ret)
303 return ret;
304
305 rad->prepared = false;
306
307 return 0;
308 }
309
rad_panel_enable(struct drm_panel * panel)310 static int rad_panel_enable(struct drm_panel *panel)
311 {
312 struct rad_panel *rad = to_rad_panel(panel);
313 struct mipi_dsi_device *dsi = rad->dsi;
314 struct device *dev = &dsi->dev;
315 int color_format = color_format_from_dsi_format(dsi->format);
316 int ret;
317
318 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
319
320 ret = rad_panel_push_cmd_list(dsi);
321 if (ret < 0) {
322 dev_err(dev, "Failed to send MCS (%d)\n", ret);
323 goto fail;
324 }
325
326 /* Select User Command Set table (CMD1) */
327 ret = mipi_dsi_generic_write(dsi, (u8[]){ WRMAUCCTR, 0x00 }, 2);
328 if (ret < 0)
329 goto fail;
330
331 /* Software reset */
332 ret = mipi_dsi_dcs_soft_reset(dsi);
333 if (ret < 0) {
334 dev_err(dev, "Failed to do Software Reset (%d)\n", ret);
335 goto fail;
336 }
337
338 usleep_range(15000, 17000);
339
340 /* Set DSI mode */
341 ret = mipi_dsi_generic_write(dsi, (u8[]){ 0xC2, 0x0B }, 2);
342 if (ret < 0) {
343 dev_err(dev, "Failed to set DSI mode (%d)\n", ret);
344 goto fail;
345 }
346 /* Set tear ON */
347 ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
348 if (ret < 0) {
349 dev_err(dev, "Failed to set tear ON (%d)\n", ret);
350 goto fail;
351 }
352 /* Set tear scanline */
353 ret = mipi_dsi_dcs_set_tear_scanline(dsi, 0x380);
354 if (ret < 0) {
355 dev_err(dev, "Failed to set tear scanline (%d)\n", ret);
356 goto fail;
357 }
358 /* Set pixel format */
359 ret = mipi_dsi_dcs_set_pixel_format(dsi, color_format);
360 dev_dbg(dev, "Interface color format set to 0x%x\n", color_format);
361 if (ret < 0) {
362 dev_err(dev, "Failed to set pixel format (%d)\n", ret);
363 goto fail;
364 }
365 /* Exit sleep mode */
366 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
367 if (ret < 0) {
368 dev_err(dev, "Failed to exit sleep mode (%d)\n", ret);
369 goto fail;
370 }
371
372 usleep_range(5000, 7000);
373
374 ret = mipi_dsi_dcs_set_display_on(dsi);
375 if (ret < 0) {
376 dev_err(dev, "Failed to set display ON (%d)\n", ret);
377 goto fail;
378 }
379
380 backlight_enable(rad->backlight);
381
382 return 0;
383
384 fail:
385 gpiod_set_value_cansleep(rad->reset, 1);
386
387 return ret;
388 }
389
rad_panel_disable(struct drm_panel * panel)390 static int rad_panel_disable(struct drm_panel *panel)
391 {
392 struct rad_panel *rad = to_rad_panel(panel);
393 struct mipi_dsi_device *dsi = rad->dsi;
394 struct device *dev = &dsi->dev;
395 int ret;
396
397 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
398
399 backlight_disable(rad->backlight);
400
401 usleep_range(10000, 12000);
402
403 ret = mipi_dsi_dcs_set_display_off(dsi);
404 if (ret < 0) {
405 dev_err(dev, "Failed to set display OFF (%d)\n", ret);
406 return ret;
407 }
408
409 usleep_range(5000, 10000);
410
411 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
412 if (ret < 0) {
413 dev_err(dev, "Failed to enter sleep mode (%d)\n", ret);
414 return ret;
415 }
416
417 return 0;
418 }
419
rad_panel_get_modes(struct drm_panel * panel,struct drm_connector * connector)420 static int rad_panel_get_modes(struct drm_panel *panel,
421 struct drm_connector *connector)
422 {
423 struct drm_display_mode *mode;
424
425 mode = drm_mode_duplicate(connector->dev, &default_mode);
426 if (!mode) {
427 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
428 default_mode.hdisplay, default_mode.vdisplay,
429 drm_mode_vrefresh(&default_mode));
430 return -ENOMEM;
431 }
432
433 drm_mode_set_name(mode);
434 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
435 drm_mode_probed_add(connector, mode);
436
437 connector->display_info.width_mm = mode->width_mm;
438 connector->display_info.height_mm = mode->height_mm;
439 connector->display_info.bus_flags = rad_bus_flags;
440
441 drm_display_info_set_bus_formats(&connector->display_info,
442 rad_bus_formats,
443 ARRAY_SIZE(rad_bus_formats));
444 return 1;
445 }
446
rad_bl_get_brightness(struct backlight_device * bl)447 static int rad_bl_get_brightness(struct backlight_device *bl)
448 {
449 struct mipi_dsi_device *dsi = bl_get_data(bl);
450 struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
451 u16 brightness;
452 int ret;
453
454 if (!rad->prepared)
455 return 0;
456
457 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
458
459 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
460 if (ret < 0)
461 return ret;
462
463 bl->props.brightness = brightness;
464
465 return brightness & 0xff;
466 }
467
rad_bl_update_status(struct backlight_device * bl)468 static int rad_bl_update_status(struct backlight_device *bl)
469 {
470 struct mipi_dsi_device *dsi = bl_get_data(bl);
471 struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
472 int ret = 0;
473
474 if (!rad->prepared)
475 return 0;
476
477 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
478
479 ret = mipi_dsi_dcs_set_display_brightness(dsi, bl->props.brightness);
480 if (ret < 0)
481 return ret;
482
483 return 0;
484 }
485
486 static const struct backlight_ops rad_bl_ops = {
487 .update_status = rad_bl_update_status,
488 .get_brightness = rad_bl_get_brightness,
489 };
490
491 static const struct drm_panel_funcs rad_panel_funcs = {
492 .prepare = rad_panel_prepare,
493 .unprepare = rad_panel_unprepare,
494 .enable = rad_panel_enable,
495 .disable = rad_panel_disable,
496 .get_modes = rad_panel_get_modes,
497 };
498
499 static const char * const rad_supply_names[] = {
500 "v3p3",
501 "v1p8",
502 };
503
rad_init_regulators(struct rad_panel * rad)504 static int rad_init_regulators(struct rad_panel *rad)
505 {
506 struct device *dev = &rad->dsi->dev;
507 int i;
508
509 rad->num_supplies = ARRAY_SIZE(rad_supply_names);
510 rad->supplies = devm_kcalloc(dev, rad->num_supplies,
511 sizeof(*rad->supplies), GFP_KERNEL);
512 if (!rad->supplies)
513 return -ENOMEM;
514
515 for (i = 0; i < rad->num_supplies; i++)
516 rad->supplies[i].supply = rad_supply_names[i];
517
518 return devm_regulator_bulk_get(dev, rad->num_supplies, rad->supplies);
519 };
520
rad_panel_probe(struct mipi_dsi_device * dsi)521 static int rad_panel_probe(struct mipi_dsi_device *dsi)
522 {
523 struct device *dev = &dsi->dev;
524 struct device_node *np = dev->of_node;
525 struct rad_panel *panel;
526 struct backlight_properties bl_props;
527 int ret;
528 u32 video_mode;
529
530 panel = devm_kzalloc(&dsi->dev, sizeof(*panel), GFP_KERNEL);
531 if (!panel)
532 return -ENOMEM;
533
534 mipi_dsi_set_drvdata(dsi, panel);
535
536 panel->dsi = dsi;
537
538 dsi->format = MIPI_DSI_FMT_RGB888;
539 dsi->mode_flags = MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_VIDEO;
540
541 ret = of_property_read_u32(np, "video-mode", &video_mode);
542 if (!ret) {
543 switch (video_mode) {
544 case 0:
545 /* burst mode */
546 dsi->mode_flags |= MIPI_DSI_MODE_VIDEO_BURST;
547 break;
548 case 1:
549 /* non-burst mode with sync event */
550 break;
551 case 2:
552 /* non-burst mode with sync pulse */
553 dsi->mode_flags |= MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
554 break;
555 default:
556 dev_warn(dev, "invalid video mode %d\n", video_mode);
557 break;
558 }
559 }
560
561 ret = of_property_read_u32(np, "dsi-lanes", &dsi->lanes);
562 if (ret) {
563 dev_err(dev, "Failed to get dsi-lanes property (%d)\n", ret);
564 return ret;
565 }
566
567 panel->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
568 if (IS_ERR(panel->reset))
569 return PTR_ERR(panel->reset);
570
571 memset(&bl_props, 0, sizeof(bl_props));
572 bl_props.type = BACKLIGHT_RAW;
573 bl_props.brightness = 255;
574 bl_props.max_brightness = 255;
575
576 panel->backlight = devm_backlight_device_register(dev, dev_name(dev),
577 dev, dsi, &rad_bl_ops,
578 &bl_props);
579 if (IS_ERR(panel->backlight)) {
580 ret = PTR_ERR(panel->backlight);
581 dev_err(dev, "Failed to register backlight (%d)\n", ret);
582 return ret;
583 }
584
585 ret = rad_init_regulators(panel);
586 if (ret)
587 return ret;
588
589 drm_panel_init(&panel->panel, dev, &rad_panel_funcs,
590 DRM_MODE_CONNECTOR_DSI);
591 dev_set_drvdata(dev, panel);
592
593 drm_panel_add(&panel->panel);
594
595 ret = mipi_dsi_attach(dsi);
596 if (ret)
597 drm_panel_remove(&panel->panel);
598
599 return ret;
600 }
601
rad_panel_remove(struct mipi_dsi_device * dsi)602 static void rad_panel_remove(struct mipi_dsi_device *dsi)
603 {
604 struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
605 struct device *dev = &dsi->dev;
606 int ret;
607
608 ret = mipi_dsi_detach(dsi);
609 if (ret)
610 dev_err(dev, "Failed to detach from host (%d)\n", ret);
611
612 drm_panel_remove(&rad->panel);
613 }
614
615 static const struct of_device_id rad_of_match[] = {
616 { .compatible = "raydium,rm67191", },
617 { /* sentinel */ }
618 };
619 MODULE_DEVICE_TABLE(of, rad_of_match);
620
621 static struct mipi_dsi_driver rad_panel_driver = {
622 .driver = {
623 .name = "panel-raydium-rm67191",
624 .of_match_table = rad_of_match,
625 },
626 .probe = rad_panel_probe,
627 .remove = rad_panel_remove,
628 };
629 module_mipi_dsi_driver(rad_panel_driver);
630
631 MODULE_AUTHOR("Robert Chiras <robert.chiras@nxp.com>");
632 MODULE_DESCRIPTION("DRM Driver for Raydium RM67191 MIPI DSI panel");
633 MODULE_LICENSE("GPL v2");
634