1 // SPDX-License-Identifier: GPL-2.0+
3 * MIPI-DSI Novatek NT35560-based panel controller.
5 * Supported panels include:
6 * Sony ACX424AKM - a 480x854 AMOLED DSI panel
7 * Sony ACX424AKP - a 480x864 AMOLED DSI panel
9 * Copyright (C) Linaro Ltd. 2019-2021
10 * Author: Linus Walleij
11 * Based on code and know-how from Marcus Lorentzon
12 * Copyright (C) ST-Ericsson SA 2010
13 * Based on code and know-how from Johan Olson and Joakim Wesslen
14 * Copyright (C) Sony Ericsson Mobile Communications 2010
16 #include <linux/backlight.h>
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/regulator/consumer.h>
24 #include <video/mipi_display.h>
26 #include <drm/drm_mipi_dsi.h>
27 #include <drm/drm_modes.h>
28 #include <drm/drm_panel.h>
30 #define NT35560_DCS_READ_ID1 0xDA
31 #define NT35560_DCS_READ_ID2 0xDB
32 #define NT35560_DCS_READ_ID3 0xDC
33 #define NT35560_DCS_SET_MDDI 0xAE
36 * Sony seems to use vendor ID 0x81
38 #define DISPLAY_SONY_ACX424AKP_ID1 0x8103
39 #define DISPLAY_SONY_ACX424AKP_ID2 0x811a
40 #define DISPLAY_SONY_ACX424AKP_ID3 0x811b
42 * The fourth ID looks like a bug, vendor IDs begin at 0x80
43 * and panel 00 ... seems like default values.
45 #define DISPLAY_SONY_ACX424AKP_ID4 0x8000
47 struct nt35560_config {
48 const struct drm_display_mode *vid_mode;
49 const struct drm_display_mode *cmd_mode;
53 const struct nt35560_config *conf;
54 struct drm_panel panel;
56 struct regulator *supply;
57 struct gpio_desc *reset_gpio;
61 static const struct drm_display_mode sony_acx424akp_vid_mode = {
64 .hsync_start = 480 + 15,
65 .hsync_end = 480 + 15 + 0,
66 .htotal = 480 + 15 + 0 + 15,
68 .vsync_start = 864 + 14,
69 .vsync_end = 864 + 14 + 1,
70 .vtotal = 864 + 14 + 1 + 11,
73 .flags = DRM_MODE_FLAG_PVSYNC,
77 * The timings are not very helpful as the display is used in
78 * command mode using the maximum HS frequency.
80 static const struct drm_display_mode sony_acx424akp_cmd_mode = {
83 .hsync_start = 480 + 154,
84 .hsync_end = 480 + 154 + 16,
85 .htotal = 480 + 154 + 16 + 32,
87 .vsync_start = 864 + 1,
88 .vsync_end = 864 + 1 + 1,
89 .vtotal = 864 + 1 + 1 + 1,
91 * Some desired refresh rate, experiments at the maximum "pixel"
92 * clock speed (HS clock 420 MHz) yields around 117Hz.
98 static const struct nt35560_config sony_acx424akp_data = {
99 .vid_mode = &sony_acx424akp_vid_mode,
100 .cmd_mode = &sony_acx424akp_cmd_mode,
103 static const struct drm_display_mode sony_acx424akm_vid_mode = {
106 .hsync_start = 480 + 15,
107 .hsync_end = 480 + 15 + 0,
108 .htotal = 480 + 15 + 0 + 15,
110 .vsync_start = 854 + 14,
111 .vsync_end = 854 + 14 + 1,
112 .vtotal = 854 + 14 + 1 + 11,
115 .flags = DRM_MODE_FLAG_PVSYNC,
119 * The timings are not very helpful as the display is used in
120 * command mode using the maximum HS frequency.
122 static const struct drm_display_mode sony_acx424akm_cmd_mode = {
125 .hsync_start = 480 + 154,
126 .hsync_end = 480 + 154 + 16,
127 .htotal = 480 + 154 + 16 + 32,
129 .vsync_start = 854 + 1,
130 .vsync_end = 854 + 1 + 1,
131 .vtotal = 854 + 1 + 1 + 1,
136 static const struct nt35560_config sony_acx424akm_data = {
137 .vid_mode = &sony_acx424akm_vid_mode,
138 .cmd_mode = &sony_acx424akm_cmd_mode,
141 static inline struct nt35560 *panel_to_nt35560(struct drm_panel *panel)
143 return container_of(panel, struct nt35560, panel);
146 #define FOSC 20 /* 20Mhz */
147 #define SCALE_FACTOR_NS_DIV_MHZ 1000
149 static int nt35560_set_brightness(struct backlight_device *bl)
151 struct nt35560 *nt = bl_get_data(bl);
152 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
153 int period_ns = 1023;
154 int duty_ns = bl->props.brightness;
160 if (backlight_is_blank(bl)) {
161 /* Disable backlight */
163 ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
166 dev_err(nt->dev, "failed to disable display backlight (%d)\n", ret);
172 /* Calculate the PWM duty cycle in n/256's */
173 pwm_ratio = max(((duty_ns * 256) / period_ns) - 1, 1);
175 ((FOSC * period_ns) / 256) /
176 SCALE_FACTOR_NS_DIV_MHZ);
178 /* Set up PWM dutycycle ONE byte (differs from the standard) */
179 dev_dbg(nt->dev, "calculated duty cycle %02x\n", pwm_ratio);
180 ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
183 dev_err(nt->dev, "failed to set display PWM ratio (%d)\n", ret);
188 * Sequence to write PWMDIV:
190 * 0xF3 0xAA CMD2 Unlock
191 * 0x00 0x01 Enter CMD2 page 0
192 * 0X7D 0x01 No reload MTP of CMD2 P1
194 * 0x7F 0xAA CMD2 page 1 lock
197 ret = mipi_dsi_dcs_write(dsi, 0xf3, &par, 1);
199 dev_err(nt->dev, "failed to unlock CMD 2 (%d)\n", ret);
203 ret = mipi_dsi_dcs_write(dsi, 0x00, &par, 1);
205 dev_err(nt->dev, "failed to enter page 1 (%d)\n", ret);
209 ret = mipi_dsi_dcs_write(dsi, 0x7d, &par, 1);
211 dev_err(nt->dev, "failed to disable MTP reload (%d)\n", ret);
214 ret = mipi_dsi_dcs_write(dsi, 0x22, &pwm_div, 1);
216 dev_err(nt->dev, "failed to set PWM divisor (%d)\n", ret);
220 ret = mipi_dsi_dcs_write(dsi, 0x7f, &par, 1);
222 dev_err(nt->dev, "failed to lock CMD 2 (%d)\n", ret);
226 /* Enable backlight */
228 ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
231 dev_err(nt->dev, "failed to enable display backlight (%d)\n", ret);
238 static const struct backlight_ops nt35560_bl_ops = {
239 .update_status = nt35560_set_brightness,
242 static const struct backlight_properties nt35560_bl_props = {
243 .type = BACKLIGHT_RAW,
245 .max_brightness = 1023,
248 static int nt35560_read_id(struct nt35560 *nt)
250 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
251 u8 vendor, version, panel;
255 ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID1, &vendor, 1);
257 dev_err(nt->dev, "could not vendor ID byte\n");
260 ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID2, &version, 1);
262 dev_err(nt->dev, "could not read device version byte\n");
265 ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID3, &panel, 1);
267 dev_err(nt->dev, "could not read panel ID byte\n");
271 if (vendor == 0x00) {
272 dev_err(nt->dev, "device vendor ID is zero\n");
276 val = (vendor << 8) | panel;
278 case DISPLAY_SONY_ACX424AKP_ID1:
279 case DISPLAY_SONY_ACX424AKP_ID2:
280 case DISPLAY_SONY_ACX424AKP_ID3:
281 case DISPLAY_SONY_ACX424AKP_ID4:
282 dev_info(nt->dev, "MTP vendor: %02x, version: %02x, panel: %02x\n",
283 vendor, version, panel);
286 dev_info(nt->dev, "unknown vendor: %02x, version: %02x, panel: %02x\n",
287 vendor, version, panel);
294 static int nt35560_power_on(struct nt35560 *nt)
298 ret = regulator_enable(nt->supply);
300 dev_err(nt->dev, "failed to enable supply (%d)\n", ret);
305 gpiod_set_value_cansleep(nt->reset_gpio, 1);
307 /* De-assert RESET */
308 gpiod_set_value_cansleep(nt->reset_gpio, 0);
309 usleep_range(11000, 20000);
314 static void nt35560_power_off(struct nt35560 *nt)
317 gpiod_set_value_cansleep(nt->reset_gpio, 1);
318 usleep_range(11000, 20000);
320 regulator_disable(nt->supply);
323 static int nt35560_prepare(struct drm_panel *panel)
325 struct nt35560 *nt = panel_to_nt35560(panel);
326 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
330 ret = nt35560_power_on(nt);
334 ret = nt35560_read_id(nt);
336 dev_err(nt->dev, "failed to read panel ID (%d)\n", ret);
340 /* Enabe tearing mode: send TE (tearing effect) at VBLANK */
341 ret = mipi_dsi_dcs_set_tear_on(dsi,
342 MIPI_DSI_DCS_TEAR_MODE_VBLANK);
344 dev_err(nt->dev, "failed to enable vblank TE (%d)\n", ret);
351 * This presumably deactivates the Qualcomm MDDI interface and
352 * selects DSI, similar code is found in other drivers such as the
353 * Sharp LS043T1LE01 which makes us suspect that this panel may be
354 * using a Novatek NT35565 or similar display driver chip that shares
355 * this command. Due to the lack of documentation we cannot know for
358 ret = mipi_dsi_dcs_write(dsi, NT35560_DCS_SET_MDDI,
359 &mddi, sizeof(mddi));
361 dev_err(nt->dev, "failed to set MDDI (%d)\n", ret);
365 /* Exit sleep mode */
366 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
368 dev_err(nt->dev, "failed to exit sleep mode (%d)\n", ret);
373 ret = mipi_dsi_dcs_set_display_on(dsi);
375 dev_err(nt->dev, "failed to turn display on (%d)\n", ret);
378 if (nt->video_mode) {
379 /* In video mode turn peripheral on */
380 ret = mipi_dsi_turn_on_peripheral(dsi);
382 dev_err(nt->dev, "failed to turn on peripheral\n");
390 nt35560_power_off(nt);
394 static int nt35560_unprepare(struct drm_panel *panel)
396 struct nt35560 *nt = panel_to_nt35560(panel);
397 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
400 ret = mipi_dsi_dcs_set_display_off(dsi);
402 dev_err(nt->dev, "failed to turn display off (%d)\n", ret);
406 /* Enter sleep mode */
407 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
409 dev_err(nt->dev, "failed to enter sleep mode (%d)\n", ret);
414 nt35560_power_off(nt);
420 static int nt35560_get_modes(struct drm_panel *panel,
421 struct drm_connector *connector)
423 struct nt35560 *nt = panel_to_nt35560(panel);
424 const struct nt35560_config *conf = nt->conf;
425 struct drm_display_mode *mode;
428 mode = drm_mode_duplicate(connector->dev,
431 mode = drm_mode_duplicate(connector->dev,
434 dev_err(panel->dev, "bad mode or failed to add mode\n");
437 drm_mode_set_name(mode);
438 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
440 connector->display_info.width_mm = mode->width_mm;
441 connector->display_info.height_mm = mode->height_mm;
443 drm_mode_probed_add(connector, mode);
445 return 1; /* Number of modes */
448 static const struct drm_panel_funcs nt35560_drm_funcs = {
449 .unprepare = nt35560_unprepare,
450 .prepare = nt35560_prepare,
451 .get_modes = nt35560_get_modes,
454 static int nt35560_probe(struct mipi_dsi_device *dsi)
456 struct device *dev = &dsi->dev;
460 nt = devm_kzalloc(dev, sizeof(struct nt35560), GFP_KERNEL);
463 nt->video_mode = of_property_read_bool(dev->of_node,
464 "enforce-video-mode");
466 mipi_dsi_set_drvdata(dsi, nt);
469 nt->conf = of_device_get_match_data(dev);
471 dev_err(dev, "missing device configuration\n");
476 dsi->format = MIPI_DSI_FMT_RGB888;
478 * FIXME: these come from the ST-Ericsson vendor driver for the
479 * HREF520 and seems to reflect limitations in the PLLs on that
480 * platform, if you have the datasheet, please cross-check the
483 dsi->lp_rate = 19200000;
484 dsi->hs_rate = 420160000;
487 /* Burst mode using event for sync */
489 MIPI_DSI_MODE_VIDEO |
490 MIPI_DSI_MODE_VIDEO_BURST;
493 MIPI_DSI_CLOCK_NON_CONTINUOUS;
495 nt->supply = devm_regulator_get(dev, "vddi");
496 if (IS_ERR(nt->supply))
497 return PTR_ERR(nt->supply);
499 /* This asserts RESET by default */
500 nt->reset_gpio = devm_gpiod_get_optional(dev, "reset",
502 if (IS_ERR(nt->reset_gpio))
503 return dev_err_probe(dev, PTR_ERR(nt->reset_gpio),
504 "failed to request GPIO\n");
506 drm_panel_init(&nt->panel, dev, &nt35560_drm_funcs,
507 DRM_MODE_CONNECTOR_DSI);
509 nt->panel.backlight = devm_backlight_device_register(dev, "nt35560", dev, nt,
510 &nt35560_bl_ops, &nt35560_bl_props);
511 if (IS_ERR(nt->panel.backlight))
512 return dev_err_probe(dev, PTR_ERR(nt->panel.backlight),
513 "failed to register backlight device\n");
515 drm_panel_add(&nt->panel);
517 ret = mipi_dsi_attach(dsi);
519 drm_panel_remove(&nt->panel);
526 static int nt35560_remove(struct mipi_dsi_device *dsi)
528 struct nt35560 *nt = mipi_dsi_get_drvdata(dsi);
530 mipi_dsi_detach(dsi);
531 drm_panel_remove(&nt->panel);
536 static const struct of_device_id nt35560_of_match[] = {
538 .compatible = "sony,acx424akp",
539 .data = &sony_acx424akp_data,
542 .compatible = "sony,acx424akm",
543 .data = &sony_acx424akm_data,
547 MODULE_DEVICE_TABLE(of, nt35560_of_match);
549 static struct mipi_dsi_driver nt35560_driver = {
550 .probe = nt35560_probe,
551 .remove = nt35560_remove,
553 .name = "panel-novatek-nt35560",
554 .of_match_table = nt35560_of_match,
557 module_mipi_dsi_driver(nt35560_driver);
559 MODULE_AUTHOR("Linus Wallei <linus.walleij@linaro.org>");
560 MODULE_DESCRIPTION("MIPI-DSI Novatek NT35560 Panel Driver");
561 MODULE_LICENSE("GPL v2");