1 // SPDX-License-Identifier: GPL-2.0+
3 * MIPI-DSI Sony ACX424AKP panel driver. This is a 480x864
4 * AMOLED panel with a command-only DSI interface.
6 * Copyright (C) Linaro Ltd. 2019
7 * Author: Linus Walleij
8 * Based on code and know-how from Marcus Lorentzon
9 * Copyright (C) ST-Ericsson SA 2010
11 #include <linux/backlight.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
16 #include <linux/regulator/consumer.h>
18 #include <video/mipi_display.h>
20 #include <drm/drm_mipi_dsi.h>
21 #include <drm/drm_modes.h>
22 #include <drm/drm_panel.h>
23 #include <drm/drm_print.h>
25 #define ACX424_DCS_READ_ID1 0xDA
26 #define ACX424_DCS_READ_ID2 0xDB
27 #define ACX424_DCS_READ_ID3 0xDC
28 #define ACX424_DCS_SET_MDDI 0xAE
31 * Sony seems to use vendor ID 0x81
33 #define DISPLAY_SONY_ACX424AKP_ID1 0x811b
34 #define DISPLAY_SONY_ACX424AKP_ID2 0x811a
36 * The third ID looks like a bug, vendor IDs begin at 0x80
37 * and panel 00 ... seems like default values.
39 #define DISPLAY_SONY_ACX424AKP_ID3 0x8000
42 struct drm_panel panel
;
44 struct backlight_device
*bl
;
45 struct regulator
*supply
;
46 struct gpio_desc
*reset_gpio
;
50 static const struct drm_display_mode sony_acx424akp_vid_mode
= {
53 .hsync_start
= 480 + 15,
54 .hsync_end
= 480 + 15 + 0,
55 .htotal
= 480 + 15 + 0 + 15,
57 .vsync_start
= 864 + 14,
58 .vsync_end
= 864 + 14 + 1,
59 .vtotal
= 864 + 14 + 1 + 11,
63 .flags
= DRM_MODE_FLAG_PVSYNC
,
67 * The timings are not very helpful as the display is used in
68 * command mode using the maximum HS frequency.
70 static const struct drm_display_mode sony_acx424akp_cmd_mode
= {
73 .hsync_start
= 480 + 154,
74 .hsync_end
= 480 + 154 + 16,
75 .htotal
= 480 + 154 + 16 + 32,
77 .vsync_start
= 864 + 1,
78 .vsync_end
= 864 + 1 + 1,
79 .vtotal
= 864 + 1 + 1 + 1,
81 * Some desired refresh rate, experiments at the maximum "pixel"
82 * clock speed (HS clock 420 MHz) yields around 117Hz.
89 static inline struct acx424akp
*panel_to_acx424akp(struct drm_panel
*panel
)
91 return container_of(panel
, struct acx424akp
, panel
);
94 #define FOSC 20 /* 20Mhz */
95 #define SCALE_FACTOR_NS_DIV_MHZ 1000
97 static int acx424akp_set_brightness(struct backlight_device
*bl
)
99 struct acx424akp
*acx
= bl_get_data(bl
);
100 struct mipi_dsi_device
*dsi
= to_mipi_dsi_device(acx
->dev
);
101 int period_ns
= 1023;
102 int duty_ns
= bl
->props
.brightness
;
108 /* Calculate the PWM duty cycle in n/256's */
109 pwm_ratio
= max(((duty_ns
* 256) / period_ns
) - 1, 1);
111 ((FOSC
* period_ns
) / 256) /
112 SCALE_FACTOR_NS_DIV_MHZ
);
114 /* Set up PWM dutycycle ONE byte (differs from the standard) */
115 DRM_DEV_DEBUG(acx
->dev
, "calculated duty cycle %02x\n", pwm_ratio
);
116 ret
= mipi_dsi_dcs_write(dsi
, MIPI_DCS_SET_DISPLAY_BRIGHTNESS
,
119 DRM_DEV_ERROR(acx
->dev
,
120 "failed to set display PWM ratio (%d)\n",
126 * Sequence to write PWMDIV:
128 * 0xF3 0xAA CMD2 Unlock
129 * 0x00 0x01 Enter CMD2 page 0
130 * 0X7D 0x01 No reload MTP of CMD2 P1
132 * 0x7F 0xAA CMD2 page 1 lock
135 ret
= mipi_dsi_dcs_write(dsi
, 0xf3, &par
, 1);
137 DRM_DEV_ERROR(acx
->dev
,
138 "failed to unlock CMD 2 (%d)\n",
143 ret
= mipi_dsi_dcs_write(dsi
, 0x00, &par
, 1);
145 DRM_DEV_ERROR(acx
->dev
,
146 "failed to enter page 1 (%d)\n",
151 ret
= mipi_dsi_dcs_write(dsi
, 0x7d, &par
, 1);
153 DRM_DEV_ERROR(acx
->dev
,
154 "failed to disable MTP reload (%d)\n",
158 ret
= mipi_dsi_dcs_write(dsi
, 0x22, &pwm_div
, 1);
160 DRM_DEV_ERROR(acx
->dev
,
161 "failed to set PWM divisor (%d)\n",
166 ret
= mipi_dsi_dcs_write(dsi
, 0x7f, &par
, 1);
168 DRM_DEV_ERROR(acx
->dev
,
169 "failed to lock CMD 2 (%d)\n",
174 /* Enable backlight */
176 ret
= mipi_dsi_dcs_write(dsi
, MIPI_DCS_WRITE_CONTROL_DISPLAY
,
179 DRM_DEV_ERROR(acx
->dev
,
180 "failed to enable display backlight (%d)\n",
188 static const struct backlight_ops acx424akp_bl_ops
= {
189 .update_status
= acx424akp_set_brightness
,
192 static int acx424akp_read_id(struct acx424akp
*acx
)
194 struct mipi_dsi_device
*dsi
= to_mipi_dsi_device(acx
->dev
);
195 u8 vendor
, version
, panel
;
199 ret
= mipi_dsi_dcs_read(dsi
, ACX424_DCS_READ_ID1
, &vendor
, 1);
201 DRM_DEV_ERROR(acx
->dev
, "could not vendor ID byte\n");
204 ret
= mipi_dsi_dcs_read(dsi
, ACX424_DCS_READ_ID2
, &version
, 1);
206 DRM_DEV_ERROR(acx
->dev
, "could not read device version byte\n");
209 ret
= mipi_dsi_dcs_read(dsi
, ACX424_DCS_READ_ID3
, &panel
, 1);
211 DRM_DEV_ERROR(acx
->dev
, "could not read panel ID byte\n");
215 if (vendor
== 0x00) {
216 DRM_DEV_ERROR(acx
->dev
, "device vendor ID is zero\n");
220 val
= (vendor
<< 8) | panel
;
222 case DISPLAY_SONY_ACX424AKP_ID1
:
223 case DISPLAY_SONY_ACX424AKP_ID2
:
224 case DISPLAY_SONY_ACX424AKP_ID3
:
225 DRM_DEV_INFO(acx
->dev
,
226 "MTP vendor: %02x, version: %02x, panel: %02x\n",
227 vendor
, version
, panel
);
230 DRM_DEV_INFO(acx
->dev
,
231 "unknown vendor: %02x, version: %02x, panel: %02x\n",
232 vendor
, version
, panel
);
239 static int acx424akp_power_on(struct acx424akp
*acx
)
243 ret
= regulator_enable(acx
->supply
);
245 DRM_DEV_ERROR(acx
->dev
, "failed to enable supply (%d)\n", ret
);
250 gpiod_set_value_cansleep(acx
->reset_gpio
, 1);
252 /* De-assert RESET */
253 gpiod_set_value_cansleep(acx
->reset_gpio
, 0);
254 usleep_range(11000, 20000);
259 static void acx424akp_power_off(struct acx424akp
*acx
)
262 gpiod_set_value_cansleep(acx
->reset_gpio
, 1);
263 usleep_range(11000, 20000);
265 regulator_disable(acx
->supply
);
268 static int acx424akp_prepare(struct drm_panel
*panel
)
270 struct acx424akp
*acx
= panel_to_acx424akp(panel
);
271 struct mipi_dsi_device
*dsi
= to_mipi_dsi_device(acx
->dev
);
275 ret
= acx424akp_power_on(acx
);
279 ret
= acx424akp_read_id(acx
);
281 DRM_DEV_ERROR(acx
->dev
, "failed to read panel ID (%d)\n", ret
);
285 /* Enabe tearing mode: send TE (tearing effect) at VBLANK */
286 ret
= mipi_dsi_dcs_set_tear_on(dsi
,
287 MIPI_DSI_DCS_TEAR_MODE_VBLANK
);
289 DRM_DEV_ERROR(acx
->dev
, "failed to enable vblank TE (%d)\n",
297 * This presumably deactivates the Qualcomm MDDI interface and
298 * selects DSI, similar code is found in other drivers such as the
299 * Sharp LS043T1LE01 which makes us suspect that this panel may be
300 * using a Novatek NT35565 or similar display driver chip that shares
301 * this command. Due to the lack of documentation we cannot know for
304 ret
= mipi_dsi_dcs_write(dsi
, ACX424_DCS_SET_MDDI
,
305 &mddi
, sizeof(mddi
));
307 DRM_DEV_ERROR(acx
->dev
, "failed to set MDDI (%d)\n", ret
);
311 /* Exit sleep mode */
312 ret
= mipi_dsi_dcs_exit_sleep_mode(dsi
);
314 DRM_DEV_ERROR(acx
->dev
, "failed to exit sleep mode (%d)\n",
320 ret
= mipi_dsi_dcs_set_display_on(dsi
);
322 DRM_DEV_ERROR(acx
->dev
, "failed to turn display on (%d)\n",
326 if (acx
->video_mode
) {
327 /* In video mode turn peripheral on */
328 ret
= mipi_dsi_turn_on_peripheral(dsi
);
330 dev_err(acx
->dev
, "failed to turn on peripheral\n");
335 acx
->bl
->props
.power
= FB_BLANK_NORMAL
;
340 acx424akp_power_off(acx
);
344 static int acx424akp_unprepare(struct drm_panel
*panel
)
346 struct acx424akp
*acx
= panel_to_acx424akp(panel
);
347 struct mipi_dsi_device
*dsi
= to_mipi_dsi_device(acx
->dev
);
351 /* Disable backlight */
353 ret
= mipi_dsi_dcs_write(dsi
, MIPI_DCS_WRITE_CONTROL_DISPLAY
,
356 DRM_DEV_ERROR(acx
->dev
,
357 "failed to disable display backlight (%d)\n",
362 ret
= mipi_dsi_dcs_set_display_off(dsi
);
364 DRM_DEV_ERROR(acx
->dev
, "failed to turn display off (%d)\n",
369 /* Enter sleep mode */
370 ret
= mipi_dsi_dcs_enter_sleep_mode(dsi
);
372 DRM_DEV_ERROR(acx
->dev
, "failed to enter sleep mode (%d)\n",
378 acx424akp_power_off(acx
);
379 acx
->bl
->props
.power
= FB_BLANK_POWERDOWN
;
384 static int acx424akp_enable(struct drm_panel
*panel
)
386 struct acx424akp
*acx
= panel_to_acx424akp(panel
);
389 * The backlight is on as long as the display is on
390 * so no use to call backlight_enable() here.
392 acx
->bl
->props
.power
= FB_BLANK_UNBLANK
;
397 static int acx424akp_disable(struct drm_panel
*panel
)
399 struct acx424akp
*acx
= panel_to_acx424akp(panel
);
402 * The backlight is on as long as the display is on
403 * so no use to call backlight_disable() here.
405 acx
->bl
->props
.power
= FB_BLANK_NORMAL
;
410 static int acx424akp_get_modes(struct drm_panel
*panel
,
411 struct drm_connector
*connector
)
413 struct acx424akp
*acx
= panel_to_acx424akp(panel
);
414 struct drm_display_mode
*mode
;
417 mode
= drm_mode_duplicate(connector
->dev
,
418 &sony_acx424akp_vid_mode
);
420 mode
= drm_mode_duplicate(connector
->dev
,
421 &sony_acx424akp_cmd_mode
);
423 DRM_ERROR("bad mode or failed to add mode\n");
426 drm_mode_set_name(mode
);
427 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
429 connector
->display_info
.width_mm
= mode
->width_mm
;
430 connector
->display_info
.height_mm
= mode
->height_mm
;
432 drm_mode_probed_add(connector
, mode
);
434 return 1; /* Number of modes */
437 static const struct drm_panel_funcs acx424akp_drm_funcs
= {
438 .disable
= acx424akp_disable
,
439 .unprepare
= acx424akp_unprepare
,
440 .prepare
= acx424akp_prepare
,
441 .enable
= acx424akp_enable
,
442 .get_modes
= acx424akp_get_modes
,
445 static int acx424akp_probe(struct mipi_dsi_device
*dsi
)
447 struct device
*dev
= &dsi
->dev
;
448 struct acx424akp
*acx
;
451 acx
= devm_kzalloc(dev
, sizeof(struct acx424akp
), GFP_KERNEL
);
454 acx
->video_mode
= of_property_read_bool(dev
->of_node
,
455 "enforce-video-mode");
457 mipi_dsi_set_drvdata(dsi
, acx
);
461 dsi
->format
= MIPI_DSI_FMT_RGB888
;
463 * FIXME: these come from the ST-Ericsson vendor driver for the
464 * HREF520 and seems to reflect limitations in the PLLs on that
465 * platform, if you have the datasheet, please cross-check the
468 dsi
->lp_rate
= 19200000;
469 dsi
->hs_rate
= 420160000;
472 /* Burst mode using event for sync */
474 MIPI_DSI_MODE_VIDEO
|
475 MIPI_DSI_MODE_VIDEO_BURST
;
478 MIPI_DSI_CLOCK_NON_CONTINUOUS
|
479 MIPI_DSI_MODE_EOT_PACKET
;
481 acx
->supply
= devm_regulator_get(dev
, "vddi");
482 if (IS_ERR(acx
->supply
))
483 return PTR_ERR(acx
->supply
);
485 /* This asserts RESET by default */
486 acx
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
488 if (IS_ERR(acx
->reset_gpio
)) {
489 ret
= PTR_ERR(acx
->reset_gpio
);
490 if (ret
!= -EPROBE_DEFER
)
491 DRM_DEV_ERROR(dev
, "failed to request GPIO (%d)\n",
496 drm_panel_init(&acx
->panel
, dev
, &acx424akp_drm_funcs
,
497 DRM_MODE_CONNECTOR_DSI
);
499 acx
->bl
= devm_backlight_device_register(dev
, "acx424akp", dev
, acx
,
500 &acx424akp_bl_ops
, NULL
);
501 if (IS_ERR(acx
->bl
)) {
502 DRM_DEV_ERROR(dev
, "failed to register backlight device\n");
503 return PTR_ERR(acx
->bl
);
505 acx
->bl
->props
.max_brightness
= 1023;
506 acx
->bl
->props
.brightness
= 512;
507 acx
->bl
->props
.power
= FB_BLANK_POWERDOWN
;
509 ret
= drm_panel_add(&acx
->panel
);
513 ret
= mipi_dsi_attach(dsi
);
515 drm_panel_remove(&acx
->panel
);
522 static int acx424akp_remove(struct mipi_dsi_device
*dsi
)
524 struct acx424akp
*acx
= mipi_dsi_get_drvdata(dsi
);
526 mipi_dsi_detach(dsi
);
527 drm_panel_remove(&acx
->panel
);
532 static const struct of_device_id acx424akp_of_match
[] = {
533 { .compatible
= "sony,acx424akp" },
536 MODULE_DEVICE_TABLE(of
, acx424akp_of_match
);
538 static struct mipi_dsi_driver acx424akp_driver
= {
539 .probe
= acx424akp_probe
,
540 .remove
= acx424akp_remove
,
542 .name
= "panel-sony-acx424akp",
543 .of_match_table
= acx424akp_of_match
,
546 module_mipi_dsi_driver(acx424akp_driver
);
548 MODULE_AUTHOR("Linus Wallei <linus.walleij@linaro.org>");
549 MODULE_DESCRIPTION("MIPI-DSI Sony acx424akp Panel Driver");
550 MODULE_LICENSE("GPL v2");