Add AMOLED brightness control ("smart dimming") for the Samsung
AMS452EF01 panel with S6E88A0 controller:

- On first prepare(), read panel revision ID3 (DCh) and the 33-byte
  factory MTP calibration (C8h) under level-2 test key, then precompute
  the 62-level gamma table (5..360 cd/m2) for revision A or C panels
  using fixed-point math matching the vendor algorithm. If calibration
  read fails, fall back gracefully to the fixed max-brightness gamma.
- Register a raw backlight device (0..255) on ctx->panel.backlight so
  drm_panel_enable()/disable() drive it automatically.
- In update_status(), map brightness 0..255 to the 62 hardware levels
  (skipping redundant writes when the level is unchanged or the panel is
  disabled/blanked) and send test-key-on (F0h), AOR (B2h), ELVSS (B6h),
  gamma (CAh), latch (F7h), and test-key-off (F0h) atomically via
  mipi_dsi_dcs_write_batch() in HS mode so the active video stream is
  not disturbed and updates complete in a single VBLANK (~35 ms on
  MSM8916 instead of ~200 ms with 6 individual transfers).
- Add `depends on BACKLIGHT_CLASS_DEVICE` to
  DRM_PANEL_SAMSUNG_S6E88A0_AMS452EF01 in Kconfig.

Tested on Samsung Galaxy A3 (SM-A300F, rev C panel) with smooth
brightness transitions across the full 0..255 range.

Signed-off-by: Vladimir Bely <[email protected]>
---
 drivers/gpu/drm/panel/Kconfig                 |   1 +
 .../panel/panel-samsung-s6e88a0-ams452ef01.c  | 816 ++++++++++++++++++
 2 files changed, 817 insertions(+)

diff --git a/drivers/gpu/drm/panel/Kconfig b/drivers/gpu/drm/panel/Kconfig
index 26ffa2378..39bad7f6c 100644
--- a/drivers/gpu/drm/panel/Kconfig
+++ b/drivers/gpu/drm/panel/Kconfig
@@ -1035,6 +1035,7 @@ config DRM_PANEL_SAMSUNG_S6E88A0_AMS427AP24
 config DRM_PANEL_SAMSUNG_S6E88A0_AMS452EF01
        tristate "Samsung AMS452EF01 panel with S6E88A0 DSI video mode 
controller"
        depends on OF
+       depends on BACKLIGHT_CLASS_DEVICE
        select DRM_MIPI_DSI
        select VIDEOMODE_HELPERS
 
diff --git a/drivers/gpu/drm/panel/panel-samsung-s6e88a0-ams452ef01.c 
b/drivers/gpu/drm/panel/panel-samsung-s6e88a0-ams452ef01.c
index 43502a3ea..faa5e7d35 100644
--- a/drivers/gpu/drm/panel/panel-samsung-s6e88a0-ams452ef01.c
+++ b/drivers/gpu/drm/panel/panel-samsung-s6e88a0-ams452ef01.c
@@ -1,11 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0-only
 // Copyright (C) 2019, Michael Srba
 
+#include <linux/backlight.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/regulator/consumer.h>
+#include <linux/slab.h>
 
 #include <video/mipi_display.h>
 
@@ -15,11 +19,697 @@
 
 #include "panel-samsung-dsi.h"
 
+/*
+ * Brightness levels and the tables of the vendor driver. Each level has an
+ * AOR and ELVSS setting, and a gamma generated from the panel calibration.
+ */
+/*
+ * Luminance of the brightness levels, in cd/m2:
+ * 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21
+ * 22, 24, 25, 27, 29, 30, 32, 34, 37, 39, 41, 44, 47, 50, 53, 56
+ * 60, 64, 68, 72, 77, 82, 87, 93, 98, 105, 111, 119, 126, 134, 143, 152
+ * 162, 172, 183, 195, 207, 220, 234, 249, 265, 282, 300, 316, 333, 360
+ */
+
+/* Highest backlight brightness (0-255) mapped to each level */
+static const u8 s6e88a0_ams452ef01_brightness_max[] = {
+       5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+       15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+       25, 26, 27, 28, 29, 30, 32, 34, 36, 38,
+       40, 43, 46, 49, 52, 56, 59, 63, 67, 71,
+       76, 80, 86, 91, 97, 104, 110, 118, 125, 133,
+       142, 150, 160, 170, 181, 193, 205, 218, 230, 242,
+       254, 255,
+};
+
+/* AMOLED Off Ratio (B2h) of each level */
+static const u16 s6e88a0_ams452ef01_aor[] = {
+       0x3ac, 0x3a5, 0x39b, 0x393, 0x389, 0x380, 0x377, 0x36f,
+       0x365, 0x35c, 0x352, 0x34a, 0x341, 0x32f, 0x325, 0x31d,
+       0x312, 0x301, 0x2f7, 0x2e5, 0x2d3, 0x2c8, 0x2b7, 0x2a2,
+       0x287, 0x274, 0x25f, 0x242, 0x222, 0x205, 0x1e5, 0x1c7,
+       0x19e, 0x178, 0x178, 0x178, 0x178, 0x178, 0x178, 0x178,
+       0x178, 0x178, 0x178, 0x178, 0x178, 0x178, 0x178, 0x178,
+       0x178, 0x14f, 0x122, 0x0ef, 0x0bd, 0x085, 0x049, 0x00a,
+       0x00a, 0x00a, 0x00a, 0x00a, 0x00a, 0x00a,
+};
+
+/* ELVSS (B6h) of each level */
+static const u8 s6e88a0_ams452ef01_elvss[] = {
+       0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
+       0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
+       0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
+       0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
+       0x17, 0x17, 0x17, 0x17, 0x17, 0x16, 0x16, 0x15,
+       0x15, 0x15, 0x14, 0x14, 0x13, 0x13, 0x12, 0x12,
+       0x11, 0x11, 0x10, 0x10, 0x10, 0x10, 0x0f, 0x0f,
+       0x0f, 0x0e, 0x0d, 0x0c, 0x0c, 0x0b,
+};
+
+/* Panel revision A: luminance the gamma of each level is based on (AOR does 
the rest) */
+static const u16 s6e88a0_ams452ef01_base_luminance_reva[] = {
+       112, 112, 112, 112, 112, 112, 112, 112, 112, 112,
+       112, 112, 112, 112, 112, 112, 112, 112, 112, 112,
+       112, 112, 112, 112, 112, 112, 112, 112, 112, 112,
+       112, 112, 112, 112, 112, 120, 129, 134, 144, 153,
+       162, 173, 182, 196, 206, 218, 232, 245, 257, 257,
+       257, 257, 257, 257, 257, 257, 276, 289, 303, 321,
+       336, 360,
+};
+
+/* Panel revision A: gray index offsets of V255 V203 V151 V87 V51 V35 V23 V11 
V3 */
+static const s8 s6e88a0_ams452ef01_gradation_offset_reva[][9] = {
+       { 0, 6, 10, 20, 29, 34, 38, -1, 0 },
+       { 0, 5, 9, 19, 26, 30, 35, -1, -2 },
+       { 0, 5, 7, 16, 22, 26, 30, 3, 1 },
+       { 0, 4, 7, 14, 20, 24, 27, 29, 4 },
+       { 0, 4, 6, 13, 19, 22, 26, 28, 14 },
+       { 0, 4, 6, 12, 17, 20, 24, 27, 22 },
+       { 0, 4, 6, 11, 16, 19, 23, 25, 22 },
+       { 0, 3, 5, 10, 14, 17, 21, 24, 28 },
+       { 0, 3, 5, 10, 14, 16, 20, 23, 24 },
+       { 0, 3, 5, 9, 13, 15, 19, 22, 22 },
+       { 0, 3, 4, 8, 12, 14, 18, 21, 22 },
+       { 0, 3, 4, 8, 11, 13, 17, 20, 24 },
+       { 0, 3, 4, 7, 10, 12, 16, 19, 25 },
+       { 0, 3, 4, 6, 9, 11, 15, 18, 19 },
+       { 0, 3, 3, 6, 9, 10, 14, 17, 19 },
+       { 0, 3, 3, 6, 9, 10, 14, 17, 18 },
+       { 0, 3, 3, 6, 8, 10, 14, 16, 18 },
+       { 0, 3, 3, 5, 7, 9, 13, 15, 18 },
+       { 0, 3, 3, 5, 7, 9, 12, 15, 18 },
+       { 0, 3, 3, 5, 6, 8, 11, 14, 18 },
+       { 0, 3, 3, 4, 6, 7, 10, 13, 20 },
+       { 0, 3, 3, 4, 5, 7, 10, 13, 17 },
+       { 0, 3, 3, 4, 5, 7, 9, 12, 16 },
+       { 0, 2, 3, 4, 5, 6, 9, 12, 12 },
+       { 0, 2, 2, 3, 4, 5, 8, 11, 13 },
+       { 0, 2, 2, 3, 4, 5, 8, 11, 10 },
+       { 0, 2, 2, 3, 4, 5, 7, 10, 12 },
+       { 0, 2, 2, 3, 4, 4, 7, 10, 10 },
+       { 0, 2, 2, 3, 3, 4, 6, 9, 8 },
+       { 0, 2, 2, 2, 3, 3, 6, 8, 9 },
+       { 0, 2, 2, 2, 3, 3, 6, 8, 7 },
+       { 0, 2, 2, 2, 2, 3, 5, 7, 10 },
+       { 0, 2, 2, 2, 2, 2, 5, 6, 8 },
+       { 0, 2, 2, 2, 2, 2, 4, 6, 4 },
+       { 0, 2, 2, 2, 2, 2, 4, 5, 6 },
+       { 0, 1, 2, 2, 2, 3, 3, 5, 8 },
+       { 0, 2, 2, 2, 2, 3, 4, 6, 6 },
+       { 0, 2, 2, 2, 1, 3, 4, 5, 2 },
+       { 0, 1, 2, 2, 1, 2, 3, 5, 5 },
+       { 0, 1, 1, 2, 1, 2, 3, 5, 3 },
+       { 0, 1, 2, 2, 1, 3, 3, 5, 5 },
+       { 0, 1, 1, 2, 1, 2, 3, 5, 5 },
+       { 0, 0, 2, 2, 1, 2, 2, 4, 5 },
+       { 0, -1, 1, 1, 1, 1, 3, 4, 6 },
+       { 0, 1, 1, 2, 2, 2, 3, 5, 3 },
+       { 0, 1, 1, 1, 1, 2, 3, 5, 0 },
+       { 0, 1, 2, 2, 2, 2, 2, 4, 0 },
+       { 0, 2, 2, 2, 2, 2, 2, 4, 1 },
+       { 0, 2, 3, 2, 2, 2, 2, 4, 3 },
+       { 0, 2, 2, 2, 2, 2, 2, 4, 0 },
+       { 0, 2, 2, 2, 1, 1, 1, 3, 0 },
+       { 0, 2, 2, 2, 1, 1, 1, 2, 3 },
+       { 0, 2, 2, 2, 1, 1, 1, 2, 0 },
+       { 0, 1, 2, 2, 1, 1, 1, 1, 3 },
+       { 0, 1, 1, 2, 1, 1, 1, 1, 1 },
+       { 0, 1, 1, 2, 1, 1, 1, 1, 0 },
+       { 0, 1, 2, 1, 0, 1, 0, 1, 2 },
+       { 0, 2, 2, 1, 1, 1, 1, 1, 0 },
+       { 0, 1, 1, 1, 0, 0, 0, 0, 1 },
+       { 0, 2, 1, 0, 0, 0, 0, 1, 0 },
+       { 0, 2, 1, 0, 0, 0, -1, 0, 3 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+};
+
+/* Panel revision A: R/G/B corrections of V255 V203 V151 V87 V51 V35 V23 V11 */
+static const s8 s6e88a0_ams452ef01_rgb_offset_reva[][24] = {
+       { -3, 0, -3, -1, 0, -2, -4, 0, -4, -7, 4, -8, -7, 4, -10, -4, 3, -7, 
-1, 1, -3, -2, 3, -8 },
+       { -2, 0, -2, -2, 0, -2, -3, 0, -4, -7, 3, -8, -7, 4, -10, -4, 2, -5, 0, 
1, -2, -2, 4, -10 },
+       { -2, 0, -2, -1, 0, -1, -2, 0, -3, -7, 3, -8, -7, 4, -9, -2, 2, -4, 0, 
1, -4, -2, 4, -9 },
+       { -1, 0, -1, -2, 0, -2, -1, 0, -2, -6, 3, -7, -7, 3, -8, -1, 2, -4, -1, 
2, -6, 0, 1, -3 },
+       { -1, 0, -1, -1, 0, -1, -2, 0, -3, -5, 2, -6, -7, 4, -9, -4, 2, -6, -1, 
1, -4, 2, 1, -2 },
+       { -1, 0, -1, -1, 0, -1, -1, 0, -2, -4, 2, -5, -6, 4, -8, -2, 2, -6, -3, 
2, -6, 0, 2, -4 },
+       { -1, 0, -1, 0, 0, -1, -2, 0, -2, -4, 2, -5, -7, 3, -8, -2, 2, -6, -3, 
2, -5, 0, 2, -4 },
+       { -1, 0, -1, 0, 0, 0, -1, 0, -2, -4, 2, -4, -6, 3, -8, -3, 2, -5, -2, 
1, -4, 0, 3, -7 },
+       { 0, 0, 0, -1, 0, -1, -2, 0, -2, -4, 1, -4, -3, 3, -6, -5, 2, -6, -2, 
2, -5, -1, 2, -5 },
+       { 0, 0, 0, -1, 0, -1, -1, 0, -1, -3, 1, -4, -4, 3, -7, -3, 2, -5, -2, 
2, -6, -1, 3, -8 },
+       { 0, 0, 0, -1, 0, -1, -1, 0, -1, -4, 1, -4, -3, 3, -7, -3, 2, -5, -2, 
2, -6, 0, 3, -6 },
+       { 0, 0, 0, -1, 0, -1, -1, 0, -1, -3, 1, -3, -4, 3, -8, -3, 2, -5, -1, 
3, -6, -2, 2, -6 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -2, -3, 1, -3, -3, 3, -6, -3, 2, -5, -2, 2, 
-6, -4, 3, -7 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -3, 1, -4, -4, 3, -7, -3, 1, -4, -2, 3, 
-7, -2, 3, -6 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -2, 1, -3, -2, 3, -6, -5, 2, -6, -3, 3, 
-7, -2, 3, -7 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -2, 1, -3, -3, 2, -6, -5, 2, -6, -2, 3, 
-6, -3, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -2, 1, -3, -3, 3, -6, -3, 1, -4, -2, 2, 
-6, -3, 5, -10 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -1, 0, -2, -2, 3, -6, -4, 1, -4, -3, 2, 
-6, -1, 4, -9 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -1, 0, -2, -2, 2, -6, -3, 2, -4, -6, 3, 
-8, -2, 4, -8 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, -1, -1, 0, -2, -2, 2, -6, -2, 1, -3, -4, 3, 
-7, -3, 4, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -2, -1, 2, -6, -4, 2, -4, -2, 2, 
-6, -4, 4, -9 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -2, -2, 2, -6, -4, 1, -4, -3, 2, 
-6, -3, 5, -10 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -2, -2, 2, -6, -2, 1, -2, -5, 4, 
-8, -1, 4, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -2, -1, 2, -5, -4, 1, -4, -3, 2, 
-6, -1, 4, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 2, -5, -4, 1, -3, -2, 2, -6, 
-4, 5, -10 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 2, -5, -3, 1, -2, -2, 2, -6, 
-4, 4, -10 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 2, -4, -4, 1, -2, -3, 2, -6, 
-2, 4, -9 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 2, -4, -3, 0, -2, -3, 2, -6, 
-3, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -4, -1, 0, -2, -3, 2, -6, 
-2, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -4, -3, 0, -2, 0, 1, -4, 
-3, 4, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -4, -3, 0, -2, -1, 1, -4, 
-2, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -4, -2, 0, -2, -1, 2, -5, 
-2, 4, -10 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -4, -1, 0, -1, 0, 1, -3, 
-2, 4, -9 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, -3, -1, 0, -1, -2, 1, -4, 
-1, 4, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, -3, -1, 0, 0, -1, 1, -4, 0, 
4, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -3, 0, 0, 0, -1, 1, -4, 0, 
4, -8 },
+       { 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, -1, 0, 1, -4, -2, 0, 0, -1, 1, -4, 
-2, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, -2, -1, 0, 0, 0, 1, -3, -3, 
3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, -2, -1, 0, 0, -1, 1, -4, 
-3, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, -2, 0, 0, 0, -2, 1, -4, -2, 
3, -7 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -3, -1, 0, 0, -2, 1, -3, 
-2, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -3, 0, 0, 0, -1, 1, -2, -2, 
3, -8 },
+       { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, -2, 0, 0, 0, 0, 1, -3, -2, 
3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, -2, 0, 0, 0, 0, 0, -2, -2, 
3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -2, -1, 0, -2, -2, 0, -2, 
-2, 3, -8 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, -1, -3, 0, -2, 
0, 3, -6 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, -1, -1, 0, -1, 0, 0, -1, 0, 1, -2, 0, 0, 0, 
0, 3, -6 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 1, -2, 0, 0, -1, 
-1, 2, -6 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, -2, -2, 0, -1, 
0, 3, -6 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, -2, -1, 0, 0, 
-1, 2, -6 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 1, -2, -1, 0, 0, 0, 
2, -4 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 0, 0, 1, -1, 
2, -6 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1, 0, 
2, -5 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, -2, 0, 0, 1, 0, 1, 
-4 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, -2, 0, 0, 2, 0, 1, 
-4 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+};
+
+/* Panel revision C: luminance the gamma of each level is based on (AOR does 
the rest) */
+static const u16 s6e88a0_ams452ef01_base_luminance_revc[] = {
+       111, 111, 111, 111, 111, 111, 111, 111, 111, 111,
+       111, 111, 111, 111, 111, 111, 111, 111, 111, 111,
+       111, 111, 111, 111, 111, 111, 111, 111, 111, 111,
+       111, 111, 111, 111, 117, 123, 131, 140, 148, 157,
+       165, 176, 184, 196, 204, 217, 229, 243, 255, 255,
+       255, 255, 255, 255, 255, 255, 272, 283, 300, 318,
+       333, 360,
+};
+
+/* Panel revision C: gray index offsets of V255 V203 V151 V87 V51 V35 V23 V11 
V3 */
+static const s8 s6e88a0_ams452ef01_gradation_offset_revc[][9] = {
+       { 0, 6, 12, 20, 27, 31, 36, 39, 40 },
+       { 0, 6, 11, 17, 24, 28, 31, 36, 39 },
+       { 0, 4, 8, 15, 20, 24, 28, 31, 32 },
+       { 0, 3, 5, 11, 16, 20, 24, 26, 29 },
+       { 0, 2, 4, 9, 15, 18, 22, 24, 26 },
+       { 0, 4, 7, 11, 14, 19, 22, 24, 25 },
+       { 0, 2, 4, 7, 12, 15, 19, 21, 23 },
+       { 0, 1, 3, 7, 11, 14, 17, 20, 21 },
+       { 0, 1, 2, 6, 10, 13, 16, 19, 20 },
+       { 0, 1, 2, 6, 10, 13, 16, 18, 19 },
+       { 0, 1, 2, 5, 9, 12, 14, 17, 18 },
+       { 0, 1, 2, 4, 8, 11, 14, 16, 17 },
+       { 0, 1, 1, 4, 8, 10, 13, 15, 16 },
+       { 0, 1, 1, 3, 7, 9, 12, 14, 15 },
+       { 0, 1, 1, 3, 6, 9, 11, 14, 14 },
+       { 0, 1, 1, 3, 6, 8, 10, 13, 14 },
+       { 0, 1, 1, 2, 6, 8, 10, 13, 13 },
+       { 0, 1, 1, 2, 5, 7, 9, 12, 13 },
+       { 0, 1, 1, 2, 5, 7, 9, 11, 12 },
+       { 0, 1, 1, 2, 4, 6, 8, 11, 11 },
+       { 0, 1, 0, 2, 4, 6, 8, 10, 11 },
+       { 0, 1, 0, 2, 4, 6, 8, 10, 10 },
+       { 0, 1, 0, 1, 4, 5, 7, 10, 10 },
+       { 0, 1, 0, 1, 4, 5, 7, 9, 10 },
+       { 0, 1, 0, 1, 3, 5, 6, 8, 9 },
+       { 0, 1, 0, 1, 3, 4, 6, 8, 8 },
+       { 0, 1, 0, 1, 3, 4, 6, 7, 8 },
+       { 0, 1, 0, 1, 3, 4, 5, 7, 7 },
+       { 0, 1, 0, 1, 3, 3, 5, 7, 7 },
+       { 0, 1, 0, 0, 2, 3, 4, 6, 7 },
+       { 0, 1, 0, 0, 2, 3, 4, 6, 6 },
+       { 0, 1, 0, 0, 2, 3, 4, 5, 6 },
+       { 0, 1, 0, 0, 2, 2, 3, 5, 5 },
+       { 0, 1, 0, 0, 2, 2, 3, 4, 5 },
+       { 0, 2, 1, 0, 1, 2, 3, 4, 4 },
+       { 0, 2, 2, 1, 2, 2, 3, 4, 4 },
+       { 0, 1, 1, 1, 2, 2, 3, 4, 4 },
+       { 0, 2, 3, 1, 2, 2, 3, 4, 4 },
+       { 0, 2, 3, 1, 2, 3, 3, 4, 4 },
+       { 0, 2, 3, 2, 2, 2, 3, 4, 4 },
+       { 0, 3, 4, 2, 2, 2, 3, 4, 4 },
+       { 0, 3, 4, 2, 2, 3, 2, 4, 4 },
+       { 0, 3, 3, 2, 2, 2, 3, 4, 4 },
+       { 0, 2, 3, 2, 2, 2, 3, 4, 4 },
+       { 0, 3, 4, 2, 2, 2, 2, 4, 4 },
+       { 0, 3, 5, 2, 2, 2, 3, 4, 4 },
+       { 0, 3, 5, 2, 2, 2, 2, 3, 3 },
+       { 0, 2, 4, 2, 2, 1, 2, 3, 3 },
+       { 0, 3, 4, 2, 1, 1, 1, 3, 3 },
+       { 0, 3, 4, 2, 1, 1, 1, 2, 3 },
+       { 0, 3, 4, 2, 1, 1, 1, 2, 2 },
+       { 0, 2, 3, 2, 1, 1, 1, 2, 2 },
+       { 0, 2, 3, 1, 1, 1, 1, 1, 2 },
+       { 0, 2, 3, 1, 1, 1, 1, 1, 1 },
+       { 0, 2, 3, 1, 1, 1, 0, 1, 1 },
+       { 0, 2, 3, 1, 1, 1, 1, 1, 1 },
+       { 0, 1, 2, 2, 0, 1, 1, 1, 1 },
+       { 0, 2, 4, 1, 1, 0, 0, 0, 1 },
+       { 0, 1, 3, 1, 0, 0, 1, 0, 1 },
+       { 0, 1, 4, 1, 0, 0, 0, 0, 1 },
+       { 0, 0, 3, 0, 0, 0, 0, 0, 1 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+};
+
+/* Panel revision C: R/G/B corrections of V255 V203 V151 V87 V51 V35 V23 V11 */
+static const s8 s6e88a0_ams452ef01_rgb_offset_revc[][24] = {
+       { -5, 0, -4, -3, 0, -3, -6, 1, -4, -16, 4, -13,
+         -19, 6, -16, -13, 5, -10, -4, 3, -6, -6, 8, -8 },
+       { -3, 0, -3, -3, 0, -3, -5, 1, -4, -13, 4, -10,
+         -15, 5, -13, -15, 4, -12, -10, 5, -13, -6, 4, -8 },
+       { -4, -1, -4, -3, 0, -2, -4, 1, -3, -14, 4, -10,
+         -18, 5, -15, -13, 3, -11, -8, 4, -10, -10, 5, -11 },
+       { -2, 0, -2, -3, 0, -3, -6, 0, -4, -14, 3, -11,
+         -20, 6, -16, -13, 6, -15, -7, 4, -8, -8, 6, -13 },
+       { -2, 0, -2, -2, 0, -2, -5, 0, -5, -13, 3, -9,
+         -18, 6, -14, -13, 6, -14, -7, 4, -11, -11, 6, -14 },
+       { -2, 0, -2, -2, -1, -2, -5, 0, -4, -10, 3, -8,
+         -20, 6, -14, -8, 5, -7, -9, 5, -13, -11, 5, -12 },
+       { 0, 0, 0, -1, 0, -1, -4, 0, -2, -9, 2, -6,
+         -10, 5, -10, -12, 5, -12, -7, 4, -8, -8, 6, -14 },
+       { 0, 0, 0, -1, 0, -1, -3, 0, -2, -8, 3, -6,
+         -10, 5, -10, -9, 5, -11, -6, 5, -10, -8, 5, -11 },
+       { 0, 0, 0, 0, 0, 0, -4, 0, -3, -7, 2, -5, -12, 5, -11, -9, 5, -11, -9, 
6, -12, -6, 5, -12 },
+       { 0, 0, 0, 0, 0, 0, -4, 0, -3, -7, 2, -5,
+         -11, 4, -10, -8, 4, -10, -10, 6, -12, -7, 6, -13 },
+       { 0, 0, 0, 0, 0, 0, -3, 0, -2, -7, 2, -5,
+         -9, 4, -10, -8, 5, -10, -10, 5, -12, -10, 7, -16 },
+       { 0, 0, 0, 0, 0, 0, -2, 0, -2, -7, 1, -4, -9, 4, -10, -10, 5, -11, -8, 
4, -10, -7, 7, -14 },
+       { 0, 0, 0, 0, 0, 0, -2, 0, -1, -6, 1, -4, -8, 4, -10, -12, 5, -11, -7, 
4, -9, -6, 7, -15 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -5, 1, -4, -8, 4, -9, -10, 4, -9, -9, 5, 
-12, -7, 7, -15 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -5, 1, -4, -7, 4, -9, -9, 3, -8, -9, 6, 
-14, -6, 6, -12 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -4, 1, -3, -7, 4, -9, -8, 4, -9, -8, 6, 
-12, -6, 6, -13 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -4, 1, -3, -5, 4, -8, -9, 3, -8, -8, 6, 
-12, -6, 6, -12 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -3, 1, -2, -5, 4, -8, -9, 3, -7, -8, 5, 
-12, -9, 6, -14 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -3, 1, -2, -5, 3, -8, -8, 3, -6, -7, 5, 
-11, -8, 7, -16 },
+       { 0, 0, 0, 0, 0, 0, -1, 0, 0, -3, 1, -2, -4, 4, -8, -8, 2, -6, -6, 5, 
-11, -7, 6, -12 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 1, -2, -3, 4, -8, -7, 2, -5, -7, 4, 
-10, -9, 8, -16 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 1, -2, -3, 3, -8, -7, 2, -5, -7, 4, 
-10, -9, 7, -16 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, -2, -3, 3, -8, -8, 3, -6, -6, 5, 
-10, -5, 6, -12 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, -2, -2, 3, -7, -7, 2, -5, -6, 4, 
-10, -7, 7, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 0, -2, -3, 2, -6, -6, 2, -4, -5, 5, 
-10, -7, 7, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, -2, 2, -6, -7, 2, -5, -4, 4, 
-8, -8, 6, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, -2, 2, -6, -5, 2, -4, -4, 3, 
-8, -7, 8, -16 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, 0, 2, -5, -6, 1, -4, -5, 4, 
-10, -7, 7, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, 0, 2, -5, -6, 2, -4, -3, 3, -7, 
-5, 6, -12 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0, 2, -5, -6, 1, -4, -4, 3, -8, 
-5, 7, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, 0, 2, -5, -4, 1, -3, -3, 3, -7, 
-8, 6, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 2, -4, -4, 1, -3, -3, 2, -6, 
-7, 7, -16 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -5, -2, 1, -2, -3, 3, -6, 
-6, 6, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, -4, -2, 1, -2, -3, 2, -6, 
-6, 6, -14 },
+       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -4, -2, 0, -2, -2, 2, -4, 
-6, 7, -16 },
+       { 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 2, -4, -3, 1, -2, -1, 2, -4, 
-6, 6, -13 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -4, -2, 0, -1, -3, 2, -6, 
-4, 6, -13 },
+       { 0, 0, -1, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 2, -4, -2, 0, -1, -1, 2, -5, 
-6, 6, -14 },
+       { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, -4, -3, 0, -1, -2, 2, -6, 
-7, 6, -14 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -4, -1, 0, -1, -1, 2, -5, 
-6, 6, -13 },
+       { 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, -4, -2, 0, 0, -3, 2, -6, 
-6, 5, -12 },
+       { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, -3, -1, 0, -1, -3, 2, -5, 
-5, 5, -12 },
+       { 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, -3, -2, 0, -1, -2, 1, -4, 
-5, 5, -11 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, -2, 0, 0, 0, -2, 2, -4, 
-4, 5, -10 },
+       { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, -3, -2, 0, -2, -2, 1, -4, 
-5, 4, -10 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -2, 0, 0, -2, -3, 1, -4, 
-3, 5, -11 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, -2, -2, 1, -3, 
-3, 4, -10 },
+       { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, -2, -2, 1, -2, 
-3, 4, -9 },
+       { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, -2, -2, 1, -2, 
-3, 3, -8 },
+       { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, -2, -2, 0, -2, 
-4, 4, -9 },
+       { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, -2, 0, 0, -1, -4, 
3, -8 },
+       { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, -2, 0, 0, -1, -2, 
3, -6 },
+       { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -2, 1, 0, -1, 0, 0, -1, -1, 
3, -6 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, -2, 0, 0, 0, 0, 
2, -6 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 2, 0, -1, 1, 0, 0, 2, 
2, -4 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0 },
+};
+
+/* (i / 255)^2.15 << 22 */
+static const u32 s6e88a0_ams452ef01_coeff_2p15[] = {
+       0, 28, 125, 298, 553, 894, 1323, 1843,
+       2456, 3164, 3968, 4871, 5873, 6976, 8180, 9488,
+       10901, 12418, 14042, 15773, 17612, 19560, 21618, 23786,
+       26065, 28456, 30959, 33576, 36307, 39152, 42112, 45188,
+       48381, 51690, 55116, 58660, 62323, 66105, 70006, 74027,
+       78168, 82430, 86813, 91318, 95945, 100694, 105567, 110563,
+       115682, 120926, 126294, 131788, 137406, 143150, 149020, 155017,
+       161140, 167390, 173768, 180273, 186906, 193668, 200558, 207578,
+       214726, 222005, 229413, 236951, 244620, 252420, 260351, 268413,
+       276607, 284933, 293391, 301981, 310705, 319561, 328550, 337673,
+       346930, 356321, 365846, 375506, 385300, 395229, 405294, 415494,
+       425830, 436302, 446910, 457654, 468535, 479553, 490708, 502001,
+       513431, 524998, 536704, 548547, 560530, 572650, 584910, 597308,
+       609846, 622523, 635340, 648296, 661393, 674630, 688007, 701525,
+       715183, 728983, 742923, 757005, 771229, 785594, 800101, 814750,
+       829542, 844476, 859552, 874771, 890133, 905639, 921288, 937080,
+       953015, 969095, 985319, 1001686, 1018199, 1034855, 1051656, 1068602,
+       1085693, 1102929, 1120311, 1137838, 1155510, 1173328, 1191293, 1209403,
+       1227659, 1246062, 1264612, 1283308, 1302151, 1321140, 1340277, 1359562,
+       1378994, 1398573, 1418300, 1438175, 1458198, 1478369, 1498688, 1519156,
+       1539772, 1560537, 1581451, 1602514, 1623726, 1645087, 1666598, 1688258,
+       1710068, 1732028, 1754138, 1776398, 1798807, 1821368, 1844078, 1866940,
+       1889952, 1913115, 1936429, 1959894, 1983510, 2007278, 2031197, 2055268,
+       2079490, 2103864, 2128391, 2153069, 2177900, 2202883, 2228018, 2253306,
+       2278747, 2304341, 2330088, 2355987, 2382040, 2408246, 2434606, 2461119,
+       2487786, 2514607, 2541581, 2568710, 2595992, 2623429, 2651020, 2678766,
+       2706666, 2734721, 2762931, 2791295, 2819815, 2848490, 2877320, 2906305,
+       2935446, 2964742, 2994194, 3023802, 3053566, 3083485, 3113561, 3143793,
+       3174181, 3204726, 3235427, 3266285, 3297300, 3328471, 3359800, 3391285,
+       3422927, 3454727, 3486684, 3518799, 3551071, 3583500, 3616088, 3648833,
+       3681736, 3714798, 3748017, 3781395, 3814931, 3848625, 3882478, 3916489,
+       3950660, 3984989, 4019477, 4054124, 4088930, 4123895, 4159020, 4194304,
+};
+
+/* 360 * (i / 255)^2.2 << 22 */
+static const u32 s6e88a0_ams452ef01_curve_2p2_360[] = {
+       0, 7666, 35224, 85949, 161847, 264428,
+       394918, 554357, 743656, 963624, 1214994, 1498435,
+       1814566, 2163961, 2547157, 2964659, 3416945, 3904466,
+       4427652, 4986915, 5582645, 6215219, 6884999, 7592333,
+       8337556, 9120991, 9942953, 10803744, 11703659, 12642984,
+       13621997, 14640966, 15700156, 16799822, 17940214, 19121577,
+       20344148, 21608161, 22913842, 24261416, 25651100, 27083107,
+       28557649, 30074929, 31635150, 33238509, 34885202, 36575418,
+       38309345, 40087169, 41909069, 43775226, 45685814, 47641006,
+       49640972, 51685881, 53775898, 55911184, 58091902, 60318208,
+       62590261, 64908212, 67272215, 69682419, 72138972, 74642022,
+       77191711, 79788183, 82431579, 85122037, 87859696, 90644692,
+       93477158, 96357229, 99285035, 102260706, 105284372, 108356159,
+       111476194, 114644602, 117861504, 121127025, 124441284, 127804402,
+       131216496, 134677686, 138188086, 141747812, 145356979, 149015699,
+       152724084, 156482246, 160290295, 164148340, 168056489, 172014850,
+       176023528, 180082630, 184192259, 188352521, 192563517, 196825349,
+       201138120, 205501929, 209916876, 214383060, 218900580, 223469532,
+       228090013, 232762120, 237485947, 242261589, 247089140, 251968693,
+       256900341, 261884176, 266920288, 272008769, 277149707, 282343194,
+       287589317, 292888165, 298239825, 303644384, 309101929, 314612546,
+       320176319, 325793334, 331463675, 337187426, 342964669, 348795488,
+       354679965, 360618181, 366610218, 372656155, 378756075, 384910055,
+       391118176, 397380516, 403697153, 410068166, 416493632, 422973628,
+       429508231, 436097516, 442741560, 449440438, 456194224, 463002994,
+       469866820, 476785778, 483759940, 490789380, 497874169, 505014380,
+       512210085, 519461355, 526768261, 534130874, 541549264, 549023502,
+       556553657, 564139797, 571781993, 579480313, 587234824, 595045596,
+       602912695, 610836188, 618816144, 626852629, 634945708, 643095448,
+       651301914, 659565173, 667885289, 676262327, 684696351, 693187426,
+       701735616, 710340985, 719003595, 727723510, 736500793, 745335507,
+       754227713, 763177473, 772184850, 781249905, 790372699, 799553293,
+       808791748, 818088124, 827442482, 836854881, 846325381, 855854042,
+       865440922, 875086082, 884789579, 894551472, 904371820, 914250680,
+       924188110, 934184168, 944238911, 954352397, 964524681, 974755822,
+       985045875, 995394897, 1005802944, 1016270071, 1026796335, 1037381790,
+       1048026492, 1058730496, 1069493857, 1080316629, 1091198867, 1102140624,
+       1113141956, 1124202915, 1135323556, 1146503931, 1157744095, 1169044100,
+       1180403998, 1191823843, 1203303688, 1214843583, 1226443583, 1238103738,
+       1249824100, 1261604721, 1273445652, 1285346945, 1297308650, 1309330819,
+       1321413502, 1333556750, 1345760613, 1358025142, 1370350386, 1382736395,
+       1395183219, 1407690908, 1420259511, 1432889077, 1445579655, 1458331295,
+       1471144045, 1484017953, 1496953069, 1509949440,
+};
+
+#define S6E88A0_NUM_LEVELS     
((int)ARRAY_SIZE(s6e88a0_ams452ef01_brightness_max))
+#define S6E88A0_GAMMA_LEN      33      /* CAh payload, also the C8h MTP layout 
*/
+#define S6E88A0_SHIFT          22      /* fixed point fraction bits */
+
+/* Gray level of each gamma point: V0 V1 V3 V11 V23 V35 V51 V87 V151 V203 V255 
*/
+static const u8 s6e88a0_ams452ef01_point_gray[] = {
+       0, 1, 3, 11, 23, 35, 51, 87, 151, 203, 255,
+};
+
+#define POINT_V0       0
+#define POINT_V1       1
+#define POINT_V3       2
+#define POINT_V11      3
+#define POINT_V255     10
+#define NUM_POINTS     ((int)ARRAY_SIZE(s6e88a0_ams452ef01_point_gray))
+
+/* Gamma register of the 360 cd/m2 level, which the MTP offsets are relative 
to */
+static const u8 s6e88a0_ams452ef01_gamma_max[S6E88A0_GAMMA_LEN] = {
+       0x01, 0x00, 0x01, 0x00, 0x01, 0x00,     /* V255 R, G, B */
+       0x80, 0x80, 0x80,                       /* V203 R, G, B */
+       0x80, 0x80, 0x80,                       /* V151 */
+       0x80, 0x80, 0x80,                       /* V87 */
+       0x80, 0x80, 0x80,                       /* V51 */
+       0x80, 0x80, 0x80,                       /* V35 */
+       0x80, 0x80, 0x80,                       /* V23 */
+       0x80, 0x80, 0x80,                       /* V11 */
+       0x6b, 0x68, 0x71,                       /* V3 */
+       0x00, 0x00, 0x00,                       /* VT */
+};
+
+static const u8 s6e88a0_ams452ef01_vt_coeff[] = {
+       0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 138, 148, 158, 168, 178, 186,
+};
+
+/* VREG0 output, 5.8 V on revision A panels and 5.6 V on others */
+#define S6E88A0_VREG_REV_A     ((58 << S6E88A0_SHIFT) / 10)
+#define S6E88A0_VREG_REV_C     ((56 << S6E88A0_SHIFT) / 10)
+
+struct s6e88a0_ams452ef01_dimming {
+       bool rev_a;
+       s32 vreg;
+       s32 vt[3];
+       /* MTP offsets: V255, then V203 V151 V87 V51 V35 V23 V11 V3, then VT */
+       s32 mtp_v255[3];
+       s32 mtp[8][3];
+       s32 mtp_vt[3];
+       s32 point[3][NUM_POINTS];       /* voltage of each gamma point */
+       s32 gray[3][256];               /* voltage of each gray level */
+};
+
+/* Offsets are sign and magnitude, bit 7 being the sign */
+static s32 s6e88a0_ams452ef01_signed(u8 v)
+{
+       return v & 0x80 ? -(v & 0x7f) : v;
+}
+
+/* Voltage of a gamma point from its register value and the point above */
+static u32 s6e88a0_ams452ef01_point_volt(s32 top, s32 above, s32 reg, u32 
coeff)
+{
+       /* top - (top - above) * (coeff + reg) / (coeff * 5) */
+       u64 span = (u64)(s64)(top - above);
+       u64 ratio = div_u64((u64)(s64)(coeff + reg) << S6E88A0_SHIFT, coeff * 
5);
+
+       return top - (u32)((span * ratio) >> S6E88A0_SHIFT);
+}
+
+static void s6e88a0_ams452ef01_parse_mtp(struct s6e88a0_ams452ef01_dimming 
*dim,
+                                        const u8 *mtp)
+{
+       int ch, k;
+
+       for (ch = 0; ch < 3; ch++) {
+               dim->mtp_v255[ch] = mtp[2 * ch] ? -mtp[2 * ch + 1] : mtp[2 * ch 
+ 1];
+               for (k = 0; k < 8; k++)
+                       dim->mtp[k][ch] = s6e88a0_ams452ef01_signed(mtp[6 + 3 * 
k + ch]);
+               dim->mtp_vt[ch] = s6e88a0_ams452ef01_signed(mtp[30 + ch]);
+       }
+}
+
+/* Voltages of the gamma points and of every gray level, from the MTP */
+static void s6e88a0_ams452ef01_gen_gray(struct s6e88a0_ams452ef01_dimming *dim)
+{
+       const u8 *ref = s6e88a0_ams452ef01_gamma_max;
+       s32 vreg = dim->vreg;
+       int ch, p, g;
+
+       for (ch = 0; ch < 3; ch++) {
+               s32 *point = dim->point[ch];
+               u32 vt_idx = clamp(dim->mtp_vt[ch], 0, 15);
+               s32 v255 = (ref[2 * ch] << 8 | ref[2 * ch + 1]) + 
dim->mtp_v255[ch];
+               u64 ratio;
+
+               /* V255 and VT are relative to VREG0 */
+               ratio = div_u64((u64)(s64)(72 + v255) << S6E88A0_SHIFT, 860);
+               point[POINT_V255] = vreg - (u32)(((u64)vreg * ratio) >> 
S6E88A0_SHIFT);
+               point[POINT_V0] = vreg;
+               point[POINT_V1] = 0;    /* unused, V0-V3 is interpolated */
+
+               ratio = div_u64((u64)s6e88a0_ams452ef01_vt_coeff[vt_idx] << 
S6E88A0_SHIFT, 860);
+               dim->vt[ch] = vreg - (u32)(((u64)vreg * ratio) >> 
S6E88A0_SHIFT);
+
+               /* V203 down to V11 are relative to VT and the point above */
+               for (p = POINT_V255 - 1, g = 0; p >= POINT_V11; p--, g++) {
+                       s32 mtp_reg = dim->mtp[g][ch] + ref[6 + 3 * g + ch];
+
+                       point[p] = s6e88a0_ams452ef01_point_volt(dim->vt[ch], 
point[p + 1],
+                                                                mtp_reg, 64);
+               }
+
+               /* V3 is relative to VREG0 and V11 */
+               point[POINT_V3] = s6e88a0_ams452ef01_point_volt(vreg, 
point[POINT_V11],
+                                                               dim->mtp[7][ch] 
+ ref[27 + ch], 64);
+
+               /* Interpolate the gray levels between the points */
+               for (p = 0; p < NUM_POINTS; p++)
+                       dim->gray[ch][s6e88a0_ams452ef01_point_gray[p]] = 
point[p];
+
+               for (p = POINT_V0; p < POINT_V255; p++) {
+                       int lo = s6e88a0_ams452ef01_point_gray[p];
+                       int hi, span;
+
+                       if (p == POINT_V1)
+                               continue;       /* V0-V3 is a single segment */
+                       hi = s6e88a0_ams452ef01_point_gray[p == POINT_V0 ? 
POINT_V3 : p + 1];
+                       span = hi - lo;
+
+                       for (g = lo + 1; g < hi; g++) {
+                               u64 v = (u64)(s64)(dim->gray[ch][lo] - 
dim->gray[ch][hi]) *
+                                       (span - (g - lo)) << S6E88A0_SHIFT;
+
+                               dim->gray[ch][g] = dim->gray[ch][hi] +
+                                       (u32)(div_u64(v, span) >> 
S6E88A0_SHIFT);
+                       }
+               }
+       }
+}
+
+/* Gray level whose luminance on the 360 cd/m2 2.2 gamma curve is closest */
+static int s6e88a0_ams452ef01_find_gray(s64 lum)
+{
+       int g;
+
+       for (g = 0; g < 255; g++) {
+               s64 d1 = lum - s6e88a0_ams452ef01_curve_2p2_360[g];
+               s64 d2 = lum - s6e88a0_ams452ef01_curve_2p2_360[g + 1];
+
+               if (d2 < 0)
+                       return d1 + d2 <= 0 ? g : g + 1;
+               if (d1 == 0)
+                       return g;
+               if (d2 == 0)
+                       return g + 1;
+       }
+
+       return 255;
+}
+
+/* Register value of a gamma point from the voltages of its gray levels */
+static u8 s6e88a0_ams452ef01_point_reg(s32 top, s32 volt, s32 above, u32 coeff)
+{
+       u64 v;
+
+       if (top <= above || top <= volt)
+               return 0;
+
+       v = (u64)(s64)(top - volt) * (coeff * 5);
+
+       return (div_u64(v, (u32)(u64)(s64)(top - above)) - coeff) & 0xff;
+}
+
+static void s6e88a0_ams452ef01_gen_level(const struct 
s6e88a0_ams452ef01_dimming *dim,
+                                        int level, u8 *out)
+{
+       const u16 *base = dim->rev_a ? s6e88a0_ams452ef01_base_luminance_reva :
+                                      s6e88a0_ams452ef01_base_luminance_revc;
+       const s8 *grad = dim->rev_a ? 
s6e88a0_ams452ef01_gradation_offset_reva[level] :
+                                     
s6e88a0_ams452ef01_gradation_offset_revc[level];
+       const s8 *rgb = dim->rev_a ? s6e88a0_ams452ef01_rgb_offset_reva[level] :
+                                    s6e88a0_ams452ef01_rgb_offset_revc[level];
+       int gray[NUM_POINTS];
+       int reg[S6E88A0_GAMMA_LEN];
+       int ch, p, k;
+
+       /* Target gray level of each point (V1 to V255) for this luminance */
+       for (p = POINT_V1; p < NUM_POINTS; p++) {
+               u8 point_gray = s6e88a0_ams452ef01_point_gray[p];
+               s64 lum = (s64)s6e88a0_ams452ef01_coeff_2p15[point_gray] * 
base[level];
+
+               gray[p] = s6e88a0_ams452ef01_find_gray(lum);
+       }
+       for (p = POINT_V255, k = 0; p > POINT_V1; p--, k++)
+               gray[p] = clamp(gray[p] + grad[k], 1, 255);
+
+       for (ch = 0; ch < 3; ch++) {
+               const s32 *v = dim->gray[ch];
+               u64 v255 = div_u64((u64)(s64)(dim->vreg - v[gray[POINT_V255]]) 
* 860,
+                                  dim->vreg) - 72;
+
+               /* V255 is 9 bits wide, the others are 8 bits */
+               reg[2 * ch] = (v255 >> 8) & 0xff;
+               reg[2 * ch + 1] = v255 & 0xff;
+               for (p = POINT_V255 - 1, k = 0; p >= POINT_V11; p--, k++)
+                       reg[6 + 3 * k + ch] =
+                               s6e88a0_ams452ef01_point_reg(dim->vt[ch], 
v[gray[p]],
+                                                            v[gray[p + 1]], 
64);
+               reg[27 + ch] = s6e88a0_ams452ef01_point_reg(dim->vreg, 
v[gray[POINT_V3]],
+                                                           v[gray[POINT_V11]], 
64);
+               reg[30 + ch] = s6e88a0_ams452ef01_gamma_max[30 + ch];
+       }
+
+       /* Per level colour correction */
+       for (ch = 0; ch < 3; ch++) {
+               int v255 = (reg[2 * ch] << 8 | reg[2 * ch + 1]) + rgb[ch];
+
+               reg[2 * ch] = (v255 / 256) & 0xff;
+               reg[2 * ch + 1] = v255 & 0xff;
+       }
+       for (k = 3; k < 24; k++)
+               reg[k + 3] += rgb[k];
+
+       /* The panel adds its MTP offsets back */
+       for (ch = 0; ch < 3; ch++) {
+               int v255 = (reg[2 * ch] << 8 | reg[2 * ch + 1]) - 
dim->mtp_v255[ch];
+
+               reg[2 * ch] = (v255 / 256) & 0xff;
+               reg[2 * ch + 1] = v255 & 0xff;
+               for (k = 0; k < 8; k++)
+                       reg[6 + 3 * k + ch] = clamp(reg[6 + 3 * k + ch] - 
dim->mtp[k][ch], 0, 255);
+       }
+
+       for (k = 0; k < S6E88A0_GAMMA_LEN; k++)
+               out[k] = reg[k];
+}
+
+/*
+ * Generate the gamma of every brightness level from the panel calibration
+ * (MTP), like the vendor "smart dimming": the AOR table gives the luminance,
+ * this keeps the 2.2 gamma curve and the white point at each level.
+ */
+static void s6e88a0_ams452ef01_gen_gamma(struct s6e88a0_ams452ef01_dimming 
*dim,
+                                        const u8 *mtp, u8 id3,
+                                        u8 (*gamma)[S6E88A0_GAMMA_LEN])
+{
+       int level;
+
+       dim->rev_a = id3 == 0x00;
+       dim->vreg = dim->rev_a ? S6E88A0_VREG_REV_A : S6E88A0_VREG_REV_C;
+       s6e88a0_ams452ef01_parse_mtp(dim, mtp);
+       s6e88a0_ams452ef01_gen_gray(dim);
+
+       for (level = 0; level < S6E88A0_NUM_LEVELS - 1; level++)
+               s6e88a0_ams452ef01_gen_level(dim, level, gamma[level]);
+       /* The top level is the reference itself */
+       memcpy(gamma[S6E88A0_NUM_LEVELS - 1], s6e88a0_ams452ef01_gamma_max,
+              S6E88A0_GAMMA_LEN);
+}
+
 struct s6e88a0_ams452ef01 {
        struct drm_panel panel;
        struct mipi_dsi_device *dsi;
        struct regulator_bulk_data supplies[2];
        struct gpio_desc *reset_gpio;
+       bool gamma_ready;
+       int level;      /* brightness level last written, -1 for none */
+       u8 gamma[S6E88A0_NUM_LEVELS][S6E88A0_GAMMA_LEN];
 };
 
 static inline struct
@@ -86,9 +776,48 @@ static void s6e88a0_ams452ef01_off(struct 
s6e88a0_ams452ef01 *ctx)
        mipi_dsi_msleep(&dsi_ctx, 120);
 }
 
+/* Read the panel revision and calibration, then generate the gamma tables */
+static int s6e88a0_ams452ef01_init_dimming(struct s6e88a0_ams452ef01 *ctx)
+{
+       struct mipi_dsi_device *dsi = ctx->dsi;
+       struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
+       struct s6e88a0_ams452ef01_dimming *dim;
+       u8 mtp[S6E88A0_GAMMA_LEN];
+       u8 id3;
+       int ret;
+
+       samsung_dsi_test_key_on_lvl2(&dsi_ctx);
+       if (dsi_ctx.accum_err)
+               return dsi_ctx.accum_err;
+
+       /* Short reads are checked too, a partial MTP would give a wrong gamma 
*/
+       ret = mipi_dsi_dcs_read(dsi, 0xdc, &id3, sizeof(id3));
+       if (ret == sizeof(id3))
+               ret = mipi_dsi_dcs_read(dsi, 0xc8, mtp, sizeof(mtp));
+
+       samsung_dsi_test_key_off_lvl2(&dsi_ctx);
+
+       if (ret < 0)
+               return ret;
+       if (ret != sizeof(mtp))
+               return -EIO;
+
+       dim = kzalloc_obj(*dim);
+       if (!dim)
+               return -ENOMEM;
+
+       s6e88a0_ams452ef01_gen_gamma(dim, mtp, id3, ctx->gamma);
+       kfree(dim);
+
+       dev_dbg(&dsi->dev, "ID3 %#04x, MTP %*ph\n", id3, (int)sizeof(mtp), mtp);
+
+       return 0;
+}
+
 static int s6e88a0_ams452ef01_prepare(struct drm_panel *panel)
 {
        struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
+       struct device *dev = &ctx->dsi->dev;
        int ret;
 
        ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
@@ -104,6 +833,17 @@ static int s6e88a0_ams452ef01_prepare(struct drm_panel 
*panel)
                                       ctx->supplies);
                return ret;
        }
+       ctx->level = -1;        /* on() set the default gamma */
+
+       /* The calibration does not change, read it on the first power on */
+       if (!ctx->gamma_ready) {
+               ret = s6e88a0_ams452ef01_init_dimming(ctx);
+               if (ret < 0)
+                       dev_warn(dev, "Failed to read calibration, keeping a 
fixed gamma: %d\n",
+                                ret);
+               else
+                       ctx->gamma_ready = true;
+       }
 
        return 0;
 }
@@ -173,6 +913,70 @@ static const struct drm_panel_funcs 
s6e88a0_ams452ef01_panel_funcs = {
        .get_modes = s6e88a0_ams452ef01_get_modes,
 };
 
+static int s6e88a0_ams452ef01_bl_update_status(struct backlight_device *bl)
+{
+       static const u8 key_on[] = { 0xf0, 0x5a, 0x5a };
+       static const u8 update[] = { 0xf7, 0x03 };
+       static const u8 key_off[] = { 0xf0, 0xa5, 0xa5 };
+       struct s6e88a0_ams452ef01 *ctx = bl_get_data(bl);
+       struct mipi_dsi_device *dsi = ctx->dsi;
+       int brightness = backlight_get_brightness(bl);
+       int level, ret;
+       u16 aor;
+       u8 b2[6];
+       u8 b6[3];
+       u8 ca[1 + S6E88A0_GAMMA_LEN];
+       const struct mipi_dsi_dcs_cmd cmds[] = {
+               { key_on, sizeof(key_on) },
+               { b2, sizeof(b2) },
+               { b6, sizeof(b6) },
+               { ca, sizeof(ca) },
+               { update, sizeof(update) },
+               { key_off, sizeof(key_off) },
+       };
+
+       /*
+        * drm_panel_enable() applies the brightness when the panel turns on,
+        * nothing to do while it is off or turning off.
+        */
+       if (!ctx->panel.enabled || backlight_is_blank(bl))
+               return 0;
+
+       for (level = 0; level < S6E88A0_NUM_LEVELS - 1; level++)
+               if (brightness <= s6e88a0_ams452ef01_brightness_max[level])
+                       break;
+       if (level == ctx->level)
+               return 0;
+
+       aor = s6e88a0_ams452ef01_aor[level];
+       b2[0] = 0xb2;
+       b2[1] = 0x40;
+       b2[2] = 0x0a;
+       b2[3] = 0x17;
+       b2[4] = aor >> 8;
+       b2[5] = aor & 0xff;
+       b6[0] = 0xb6;
+       b6[1] = 0x2c;
+       b6[2] = s6e88a0_ams452ef01_elvss[level];
+       ca[0] = 0xca;
+       memcpy(&ca[1], ctx->gamma_ready ? ctx->gamma[level] :
+                      s6e88a0_ams452ef01_gamma_max, S6E88A0_GAMMA_LEN);
+
+       /* Low power transfers would disturb the video stream */
+       dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
+       ret = mipi_dsi_dcs_write_batch(dsi, cmds, ARRAY_SIZE(cmds));
+       dsi->mode_flags |= MIPI_DSI_MODE_LPM;
+
+       if (!ret)
+               ctx->level = level;
+
+       return ret;
+}
+
+static const struct backlight_ops s6e88a0_ams452ef01_bl_ops = {
+       .update_status = s6e88a0_ams452ef01_bl_update_status,
+};
+
 static int s6e88a0_ams452ef01_probe(struct mipi_dsi_device *dsi)
 {
        struct device *dev = &dsi->dev;
@@ -209,6 +1013,18 @@ static int s6e88a0_ams452ef01_probe(struct 
mipi_dsi_device *dsi)
        dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST;
        ctx->panel.prepare_prev_first = true;
 
+       ctx->panel.backlight =
+               devm_backlight_device_register(dev, dev_name(dev), dev, ctx,
+                                              &s6e88a0_ams452ef01_bl_ops,
+                                              &(const struct 
backlight_properties) {
+                                                      .type = BACKLIGHT_RAW,
+                                                      .brightness = 255,
+                                                      .max_brightness = 255,
+                                              });
+       if (IS_ERR(ctx->panel.backlight))
+               return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
+                                    "Failed to register backlight\n");
+
        ret = devm_drm_panel_add(dev, &ctx->panel);
        if (ret)
                return ret;
-- 
2.43.0

Reply via email to