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/regulator/consumer.h>
23 #include <video/mipi_display.h>
25 #include <drm/drm_mipi_dsi.h>
26 #include <drm/drm_modes.h>
27 #include <drm/drm_panel.h>
29 #define NT35560_DCS_READ_ID1 0xDA
30 #define NT35560_DCS_READ_ID2 0xDB
31 #define NT35560_DCS_READ_ID3 0xDC
32 #define NT35560_DCS_SET_MDDI 0xAE
35 * Sony seems to use vendor ID 0x81
37 #define DISPLAY_SONY_ACX424AKP_ID1 0x8103
38 #define DISPLAY_SONY_ACX424AKP_ID2 0x811a
39 #define DISPLAY_SONY_ACX424AKP_ID3 0x811b
41 * The fourth ID looks like a bug, vendor IDs begin at 0x80
42 * and panel 00 ... seems like default values.
44 #define DISPLAY_SONY_ACX424AKP_ID4 0x8000
46 struct nt35560_config {
47 const struct drm_display_mode *vid_mode;
48 const struct drm_display_mode *cmd_mode;
52 const struct nt35560_config *conf;
53 struct drm_panel panel;
55 struct regulator *supply;
56 struct gpio_desc *reset_gpio;
60 static const struct drm_display_mode sony_acx424akp_vid_mode = {
63 .hsync_start = 480 + 15,
64 .hsync_end = 480 + 15 + 0,
65 .htotal = 480 + 15 + 0 + 15,
67 .vsync_start = 864 + 14,
68 .vsync_end = 864 + 14 + 1,
69 .vtotal = 864 + 14 + 1 + 11,
72 .flags = DRM_MODE_FLAG_PVSYNC,
76 * The timings are not very helpful as the display is used in
77 * command mode using the maximum HS frequency.
79 static const struct drm_display_mode sony_acx424akp_cmd_mode = {
82 .hsync_start = 480 + 154,
83 .hsync_end = 480 + 154 + 16,
84 .htotal = 480 + 154 + 16 + 32,
86 .vsync_start = 864 + 1,
87 .vsync_end = 864 + 1 + 1,
88 .vtotal = 864 + 1 + 1 + 1,
90 * Some desired refresh rate, experiments at the maximum "pixel"
91 * clock speed (HS clock 420 MHz) yields around 117Hz.
97 static const struct nt35560_config sony_acx424akp_data = {
98 .vid_mode = &sony_acx424akp_vid_mode,
99 .cmd_mode = &sony_acx424akp_cmd_mode,
102 static const struct drm_display_mode sony_acx424akm_vid_mode = {
105 .hsync_start = 480 + 15,
106 .hsync_end = 480 + 15 + 0,
107 .htotal = 480 + 15 + 0 + 15,
109 .vsync_start = 854 + 14,
110 .vsync_end = 854 + 14 + 1,
111 .vtotal = 854 + 14 + 1 + 11,
114 .flags = DRM_MODE_FLAG_PVSYNC,
118 * The timings are not very helpful as the display is used in
119 * command mode using the maximum HS frequency.
121 static const struct drm_display_mode sony_acx424akm_cmd_mode = {
124 .hsync_start = 480 + 154,
125 .hsync_end = 480 + 154 + 16,
126 .htotal = 480 + 154 + 16 + 32,
128 .vsync_start = 854 + 1,
129 .vsync_end = 854 + 1 + 1,
130 .vtotal = 854 + 1 + 1 + 1,
135 static const struct nt35560_config sony_acx424akm_data = {
136 .vid_mode = &sony_acx424akm_vid_mode,
137 .cmd_mode = &sony_acx424akm_cmd_mode,
140 static inline struct nt35560 *panel_to_nt35560(struct drm_panel *panel)
142 return container_of(panel, struct nt35560, panel);
145 #define FOSC 20 /* 20Mhz */
146 #define SCALE_FACTOR_NS_DIV_MHZ 1000
148 static int nt35560_set_brightness(struct backlight_device *bl)
150 struct nt35560 *nt = bl_get_data(bl);
151 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
152 int period_ns = 1023;
153 int duty_ns = bl->props.brightness;
159 if (backlight_is_blank(bl)) {
160 /* Disable backlight */
162 ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
165 dev_err(nt->dev, "failed to disable display backlight (%d)\n", ret);
171 /* Calculate the PWM duty cycle in n/256's */
172 pwm_ratio = max(((duty_ns * 256) / period_ns) - 1, 1);
174 ((FOSC * period_ns) / 256) /
175 SCALE_FACTOR_NS_DIV_MHZ);
177 /* Set up PWM dutycycle ONE byte (differs from the standard) */
178 dev_dbg(nt->dev, "calculated duty cycle %02x\n", pwm_ratio);
179 ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
182 dev_err(nt->dev, "failed to set display PWM ratio (%d)\n", ret);
187 * Sequence to write PWMDIV:
189 * 0xF3 0xAA CMD2 Unlock
190 * 0x00 0x01 Enter CMD2 page 0
191 * 0X7D 0x01 No reload MTP of CMD2 P1
193 * 0x7F 0xAA CMD2 page 1 lock
196 ret = mipi_dsi_dcs_write(dsi, 0xf3, &par, 1);
198 dev_err(nt->dev, "failed to unlock CMD 2 (%d)\n", ret);
202 ret = mipi_dsi_dcs_write(dsi, 0x00, &par, 1);
204 dev_err(nt->dev, "failed to enter page 1 (%d)\n", ret);
208 ret = mipi_dsi_dcs_write(dsi, 0x7d, &par, 1);
210 dev_err(nt->dev, "failed to disable MTP reload (%d)\n", ret);
213 ret = mipi_dsi_dcs_write(dsi, 0x22, &pwm_div, 1);
215 dev_err(nt->dev, "failed to set PWM divisor (%d)\n", ret);
219 ret = mipi_dsi_dcs_write(dsi, 0x7f, &par, 1);
221 dev_err(nt->dev, "failed to lock CMD 2 (%d)\n", ret);
225 /* Enable backlight */
227 ret = mipi_dsi_dcs_write(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY,
230 dev_err(nt->dev, "failed to enable display backlight (%d)\n", ret);
237 static const struct backlight_ops nt35560_bl_ops = {
238 .update_status = nt35560_set_brightness,
241 static const struct backlight_properties nt35560_bl_props = {
242 .type = BACKLIGHT_RAW,
244 .max_brightness = 1023,
247 static int nt35560_read_id(struct nt35560 *nt)
249 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
250 u8 vendor, version, panel;
254 ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID1, &vendor, 1);
256 dev_err(nt->dev, "could not vendor ID byte\n");
259 ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID2, &version, 1);
261 dev_err(nt->dev, "could not read device version byte\n");
264 ret = mipi_dsi_dcs_read(dsi, NT35560_DCS_READ_ID3, &panel, 1);
266 dev_err(nt->dev, "could not read panel ID byte\n");
270 if (vendor == 0x00) {
271 dev_err(nt->dev, "device vendor ID is zero\n");
275 val = (vendor << 8) | panel;
277 case DISPLAY_SONY_ACX424AKP_ID1:
278 case DISPLAY_SONY_ACX424AKP_ID2:
279 case DISPLAY_SONY_ACX424AKP_ID3:
280 case DISPLAY_SONY_ACX424AKP_ID4:
281 dev_info(nt->dev, "MTP vendor: %02x, version: %02x, panel: %02x\n",
282 vendor, version, panel);
285 dev_info(nt->dev, "unknown vendor: %02x, version: %02x, panel: %02x\n",
286 vendor, version, panel);
293 static int nt35560_power_on(struct nt35560 *nt)
297 ret = regulator_enable(nt->supply);
299 dev_err(nt->dev, "failed to enable supply (%d)\n", ret);
304 gpiod_set_value_cansleep(nt->reset_gpio, 1);
306 /* De-assert RESET */
307 gpiod_set_value_cansleep(nt->reset_gpio, 0);
308 usleep_range(11000, 20000);
313 static void nt35560_power_off(struct nt35560 *nt)
316 gpiod_set_value_cansleep(nt->reset_gpio, 1);
317 usleep_range(11000, 20000);
319 regulator_disable(nt->supply);
322 static int nt35560_prepare(struct drm_panel *panel)
324 struct nt35560 *nt = panel_to_nt35560(panel);
325 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
329 ret = nt35560_power_on(nt);
333 ret = nt35560_read_id(nt);
335 dev_err(nt->dev, "failed to read panel ID (%d)\n", ret);
339 /* Enabe tearing mode: send TE (tearing effect) at VBLANK */
340 ret = mipi_dsi_dcs_set_tear_on(dsi,
341 MIPI_DSI_DCS_TEAR_MODE_VBLANK);
343 dev_err(nt->dev, "failed to enable vblank TE (%d)\n", ret);
350 * This presumably deactivates the Qualcomm MDDI interface and
351 * selects DSI, similar code is found in other drivers such as the
352 * Sharp LS043T1LE01 which makes us suspect that this panel may be
353 * using a Novatek NT35565 or similar display driver chip that shares
354 * this command. Due to the lack of documentation we cannot know for
357 ret = mipi_dsi_dcs_write(dsi, NT35560_DCS_SET_MDDI,
358 &mddi, sizeof(mddi));
360 dev_err(nt->dev, "failed to set MDDI (%d)\n", ret);
364 /* Exit sleep mode */
365 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
367 dev_err(nt->dev, "failed to exit sleep mode (%d)\n", ret);
372 ret = mipi_dsi_dcs_set_display_on(dsi);
374 dev_err(nt->dev, "failed to turn display on (%d)\n", ret);
377 if (nt->video_mode) {
378 /* In video mode turn peripheral on */
379 ret = mipi_dsi_turn_on_peripheral(dsi);
381 dev_err(nt->dev, "failed to turn on peripheral\n");
389 nt35560_power_off(nt);
393 static int nt35560_unprepare(struct drm_panel *panel)
395 struct nt35560 *nt = panel_to_nt35560(panel);
396 struct mipi_dsi_device *dsi = to_mipi_dsi_device(nt->dev);
399 ret = mipi_dsi_dcs_set_display_off(dsi);
401 dev_err(nt->dev, "failed to turn display off (%d)\n", ret);
405 /* Enter sleep mode */
406 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
408 dev_err(nt->dev, "failed to enter sleep mode (%d)\n", ret);
413 nt35560_power_off(nt);
419 static int nt35560_get_modes(struct drm_panel *panel,
420 struct drm_connector *connector)
422 struct nt35560 *nt = panel_to_nt35560(panel);
423 const struct nt35560_config *conf = nt->conf;
424 struct drm_display_mode *mode;
427 mode = drm_mode_duplicate(connector->dev,
430 mode = drm_mode_duplicate(connector->dev,
433 dev_err(panel->dev, "bad mode or failed to add mode\n");
436 drm_mode_set_name(mode);
437 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
439 connector->display_info.width_mm = mode->width_mm;
440 connector->display_info.height_mm = mode->height_mm;
442 drm_mode_probed_add(connector, mode);
444 return 1; /* Number of modes */
447 static const struct drm_panel_funcs nt35560_drm_funcs = {
448 .unprepare = nt35560_unprepare,
449 .prepare = nt35560_prepare,
450 .get_modes = nt35560_get_modes,
453 static int nt35560_probe(struct mipi_dsi_device *dsi)
455 struct device *dev = &dsi->dev;
459 nt = devm_kzalloc(dev, sizeof(struct nt35560), GFP_KERNEL);
462 nt->video_mode = of_property_read_bool(dev->of_node,
463 "enforce-video-mode");
465 mipi_dsi_set_drvdata(dsi, nt);
468 nt->conf = of_device_get_match_data(dev);
470 dev_err(dev, "missing device configuration\n");
475 dsi->format = MIPI_DSI_FMT_RGB888;
477 * FIXME: these come from the ST-Ericsson vendor driver for the
478 * HREF520 and seems to reflect limitations in the PLLs on that
479 * platform, if you have the datasheet, please cross-check the
482 dsi->lp_rate = 19200000;
483 dsi->hs_rate = 420160000;
486 /* Burst mode using event for sync */
488 MIPI_DSI_MODE_VIDEO |
489 MIPI_DSI_MODE_VIDEO_BURST;
492 MIPI_DSI_CLOCK_NON_CONTINUOUS;
494 nt->supply = devm_regulator_get(dev, "vddi");
495 if (IS_ERR(nt->supply))
496 return PTR_ERR(nt->supply);
498 /* This asserts RESET by default */
499 nt->reset_gpio = devm_gpiod_get_optional(dev, "reset",
501 if (IS_ERR(nt->reset_gpio))
502 return dev_err_probe(dev, PTR_ERR(nt->reset_gpio),
503 "failed to request GPIO\n");
505 drm_panel_init(&nt->panel, dev, &nt35560_drm_funcs,
506 DRM_MODE_CONNECTOR_DSI);
508 nt->panel.backlight = devm_backlight_device_register(dev, "nt35560", dev, nt,
509 &nt35560_bl_ops, &nt35560_bl_props);
510 if (IS_ERR(nt->panel.backlight))
511 return dev_err_probe(dev, PTR_ERR(nt->panel.backlight),
512 "failed to register backlight device\n");
514 drm_panel_add(&nt->panel);
516 ret = mipi_dsi_attach(dsi);
518 drm_panel_remove(&nt->panel);
525 static void nt35560_remove(struct mipi_dsi_device *dsi)
527 struct nt35560 *nt = mipi_dsi_get_drvdata(dsi);
529 mipi_dsi_detach(dsi);
530 drm_panel_remove(&nt->panel);
533 static const struct of_device_id nt35560_of_match[] = {
535 .compatible = "sony,acx424akp",
536 .data = &sony_acx424akp_data,
539 .compatible = "sony,acx424akm",
540 .data = &sony_acx424akm_data,
544 MODULE_DEVICE_TABLE(of, nt35560_of_match);
546 static struct mipi_dsi_driver nt35560_driver = {
547 .probe = nt35560_probe,
548 .remove = nt35560_remove,
550 .name = "panel-novatek-nt35560",
551 .of_match_table = nt35560_of_match,
554 module_mipi_dsi_driver(nt35560_driver);
556 MODULE_AUTHOR("Linus Wallei <linus.walleij@linaro.org>");
557 MODULE_DESCRIPTION("MIPI-DSI Novatek NT35560 Panel Driver");
558 MODULE_LICENSE("GPL v2");