Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Documentation/ABI/testing/sysfs-driver-apple-dcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-License-Identifier: GPL-2.0-only OR MIT

What: /sys/bus/platform/drivers/apple-dcp/*/panel_adaptation
Date: December 2026
KernelVersion: 7.3
Contact: dri-devel@lists.freedesktop.org
Description:
(RW) Present on the internal panel platform device of an Apple
T8132 (M4) firmware 26.6.2 display coprocessor (apple,t8132-dcp).
Absent on external DCPs (apple,t8132-dcpext).

Exposes a 3x3 white-point adaptation colour matrix (True Tone)
formatted as nine space-separated decimal signed S31.32 two's
complement integers in row-major order, terminated by a newline.
The identity matrix is:

4294967296 0 0 0 4294967296 0 0 0 4294967296

Each written coefficient is clamped to [-4.0, +4.0]. Nothing is
sent to the firmware until a non-identity matrix is written;
writing the identity matrix clears a previously active
adaptation once. While the panel is blanked, power-gated, or
suspended, writes update the stored matrix without issuing an
RPC and the pending matrix is applied before the first swap on
wake.
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,7 @@ M: Janne Grunau <j@jannau.net>
L: dri-devel@lists.freedesktop.org
S: Maintained
T: git git://anongit.freedesktop.org/drm/drm-misc
F: Documentation/ABI/testing/sysfs-driver-apple-dcp
F: Documentation/devicetree/bindings/display/apple,dcp-typec-routes.yaml
F: Documentation/gpu/apple-dcp-*.rst
F: drivers/gpu/drm/apple/
Expand Down
219 changes: 218 additions & 1 deletion drivers/gpu/drm/apple/iomfb_m4.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/ctype.h>
#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/io.h>
#include <linux/limits.h>
#include <linux/mutex.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
Expand Down Expand Up @@ -40,6 +42,7 @@
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/suspend.h>
#include <linux/sysfs.h>
#include <linux/soc/apple/dart.h>
#include <linux/soc/apple/dart-cio.h>
#include <linux/soc/apple/rtkit.h>
Expand Down Expand Up @@ -229,6 +232,14 @@ struct iomfb_m4 {
struct drm_framebuffer *uncertain_fb;
struct dcp_m4_frame active_frame;
u64 flips, blanks, brightness_swaps;
/*
* The internal panel's white-point adaptation (True Tone) in S31.32
* two's complement (row-major), whether it differs from identity, and
* whether set_matrix (A423) is due.
*/
struct work_struct adapt_work;
s64 adapt[9];
bool adapt_active, adapt_pending;
struct delayed_work idle_cache;
/*
* Framebuffer references a window let go of, put
Expand Down Expand Up @@ -2633,6 +2644,8 @@ static int iomfb_m4_check_frame(const struct dcp_m4_frame *frame)
}

static void iomfb_m4_backlight_work(struct work_struct *work);
static void iomfb_m4_adapt_work(struct work_struct *work);
static const struct attribute_group iomfb_m4_adapt_group;
static void iomfb_m4_idle_cache_work(struct work_struct *work);
static void iomfb_m4_ext_put_work(struct work_struct *work);

Expand Down Expand Up @@ -2737,6 +2750,7 @@ int iomfb_m4_init(struct apple_dcp *owner)
struct resource *res;
const struct dcp_m4_profile *pf = dcp_m4_machine_profile();
u32 ready = 0;
unsigned int i;
int ret;
/* An external DCP the boot loader handed over: same firmware, no panel. */
bool external = of_device_is_compatible(dev->of_node, "apple,t8132-dcpext");
Expand Down Expand Up @@ -2785,6 +2799,9 @@ int iomfb_m4_init(struct apple_dcp *owner)
spin_lock_init(&dcp->state_lock);
init_completion(&dcp->reply);
INIT_WORK(&dcp->backlight_work, iomfb_m4_backlight_work);
INIT_WORK(&dcp->adapt_work, iomfb_m4_adapt_work);
for (i = 0; i < ARRAY_SIZE(dcp->adapt); i++)
dcp->adapt[i] = (i % 4 == 0) ? (1LL << 32) : 0;
spin_lock_init(&dcp->ext_put_lock);
INIT_LIST_HEAD(&dcp->ext_puts);
INIT_WORK(&dcp->ext_put_work, iomfb_m4_ext_put_work);
Expand Down Expand Up @@ -2838,6 +2855,12 @@ int iomfb_m4_init(struct apple_dcp *owner)
owner->v26_6 = dcp;
owner->rtk = dcp->rtkit;
owner->coproc_reg = dcp->coproc;
/* Added after iomfb_m4_release(), so sysfs unwinds before dcp is freed. */
if (!external) {
ret = devm_device_add_group(dev, &iomfb_m4_adapt_group);
if (ret)
dev_warn(dev, "panel_adaptation: %d\n", ret);
}
pm_runtime_get_noresume(dev);
dev_info(dev, "firmware 26.6.2 retained DCP backend prepared\n");
return 0;
Expand Down Expand Up @@ -3627,6 +3650,187 @@ static int iomfb_m4_swap(struct iomfb_m4 *dcp, const struct dcp_m4_frame *frame)
return ret;
}

static s64 iomfb_m4_adapt_clamp(s64 v)
{
return clamp_t(s64, v, -(4LL << 32), 4LL << 32);
}

/*
* Send the pending panel white-point adaptation matrix: set_matrix (A423) at
* location 9, as iomfb_v26_6.c does. A refused matrix leaves the previous one
* in place and frames go on; only a transport failure, which has failed the
* session, is returned.
*/
static int iomfb_m4_send_adaptation(struct iomfb_m4 *dcp)
{
struct iomfb_set_matrix_req req = { .location = 9 };
struct iomfb_set_matrix_resp resp = {};
unsigned int i;
int ret;

lockdep_assert_held(&dcp->lock);
if (!dcp->adapt_pending)
return 0;
for (i = 0; i < ARRAY_SIZE(req.matrix); i++)
req.matrix[i] = (u64)dcp->adapt[i];
ret = dcp_m4_runtime_call(dcp->runtime, 423, false, &req, sizeof(req),
&resp, sizeof(resp));
if (ret)
return ret;
dcp->adapt_pending = false;
if (resp.ret)
dev_warn_ratelimited(dcp->dev,
"panel adaptation matrix not applied (status %u)\n",
resp.ret);
return 0;
}

static void iomfb_m4_adapt_work(struct work_struct *work)
{
struct iomfb_m4 *dcp = container_of(work, struct iomfb_m4, adapt_work);
int ret;

mutex_lock(&dcp->lock);
/*
* Never send A423 while blanked, power-gated, or suspended: the matrix
* stays pending and is sent before the first swap on wake.
*/
if (READ_ONCE(dcp->owner->quiescing) || !dcp->runtime || dcp->external ||
dcp->powered_off || dcp->gated || dcp->display_off ||
!dcp->adapt_pending)
goto out;
ret = dcp_m4_runtime_live(dcp->runtime);
if (!ret)
iomfb_m4_send_adaptation(dcp);
out:
mutex_unlock(&dcp->lock);
}

/*
* panel_adaptation: the internal panel's white-point adaptation (True Tone),
* nine S31.32 fixed-point values in decimal, two's complement, row-major;
* identity (4294967296 0 0 0 4294967296 0 0 0 4294967296) clears it.
* Each coefficient is clamped to [-4.0, +4.0]. Nothing is sent until a
* non-identity matrix is written; identity is sent once to clear it.
*/
static ssize_t panel_adaptation_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct apple_dcp *owner = dev_get_drvdata(dev);
struct iomfb_m4 *dcp = owner ? owner->v26_6 : NULL;
s64 m[9];

if (!dcp || dcp->external)
return -ENODEV;
mutex_lock(&dcp->lock);
memcpy(m, dcp->adapt, sizeof(m));
mutex_unlock(&dcp->lock);
return sysfs_emit(buf, "%lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
(long long)m[0], (long long)m[1], (long long)m[2],
(long long)m[3], (long long)m[4], (long long)m[5],
(long long)m[6], (long long)m[7], (long long)m[8]);
}

static int iomfb_m4_parse_adaptation(const char *buf, size_t count, s64 out[9])
{
const char *p = buf;
const char *end = buf + count;
unsigned int i;

if (memchr(buf, '\0', count))
return -EINVAL;

for (i = 0; i < 9; i++) {
const char *digits;
bool neg = false;
u64 mag = 0;
u64 limit;

if (i == 0) {
while (p < end && isspace((unsigned char)*p))
p++;
} else {
if (p >= end || !isspace((unsigned char)*p))
return -EINVAL;
while (p < end && isspace((unsigned char)*p))
p++;
}
if (p >= end)
return -EINVAL;
if (*p == '-') {
neg = true;
p++;
} else if (*p == '+') {
p++;
}
digits = p;
limit = neg ? (u64)S64_MAX + 1ULL : (u64)S64_MAX;
while (p < end && isdigit((unsigned char)*p)) {
u64 d = *p - '0';

if (mag > (limit - d) / 10ULL)
return -EINVAL;
mag = mag * 10ULL + d;
p++;
}
if (p == digits)
return -EINVAL;
out[i] = neg ? (s64)(0ULL - mag) : (s64)mag;
}

while (p < end && isspace((unsigned char)*p))
p++;
if (p != end)
return -EINVAL;

return 0;
}

static ssize_t panel_adaptation_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct apple_dcp *owner = dev_get_drvdata(dev);
struct iomfb_m4 *dcp = owner ? owner->v26_6 : NULL;
s64 raw[9];
s64 m[9];
unsigned int i;
bool active = false;

if (!dcp || dcp->external)
return -ENODEV;
if (iomfb_m4_parse_adaptation(buf, count, raw))
return -EINVAL;
for (i = 0; i < ARRAY_SIZE(m); i++) {
m[i] = iomfb_m4_adapt_clamp(raw[i]);
active |= m[i] != ((i % 4 == 0) ? (1LL << 32) : 0);
}
mutex_lock(&dcp->lock);
if (!memcmp(m, dcp->adapt, sizeof(m)))
goto out;
memcpy(dcp->adapt, m, sizeof(m));
if (active || dcp->adapt_active)
dcp->adapt_pending = true;
dcp->adapt_active = active;
if (dcp->adapt_pending && dcp->runtime && !dcp->powered_off &&
!dcp->gated && !dcp->display_off &&
!READ_ONCE(dcp->owner->quiescing))
queue_work(dcp->runtime->wq, &dcp->adapt_work);
out:
mutex_unlock(&dcp->lock);
return count;
}
static DEVICE_ATTR_RW(panel_adaptation);

static struct attribute *iomfb_m4_adapt_attrs[] = {
&dev_attr_panel_adaptation.attr,
NULL
};

static const struct attribute_group iomfb_m4_adapt_group = {
.attrs = iomfb_m4_adapt_attrs,
};

/* As iomfb_m4_present(), @dst_y panel rows down: a notch-free frame. */
int iomfb_m4_present_at(struct apple_dcp *owner, struct drm_framebuffer *fb,
dma_addr_t dva, size_t mapped, u32 dst_y)
Expand Down Expand Up @@ -3668,6 +3872,10 @@ int iomfb_m4_present_at(struct apple_dcp *owner, struct drm_framebuffer *fb,
dcp->display_on_us_max);
}
}
if (ret)
goto out;
/* A pending panel adaptation matrix goes out before the frame it shows with. */
ret = iomfb_m4_send_adaptation(dcp);
if (ret)
goto out;
/* Keep the source before the first work descriptor can become visible. */
Expand Down Expand Up @@ -3780,6 +3988,9 @@ static int dcp_m4_set_power_state(struct iomfb_m4 *dcp, bool on)
ret = -EIO;
dcp_m4_failed(dcp, ret);
}
/* The firmware may lose the adaptation matrix with the display off. */
if (!ret && !on && dcp->adapt_active)
dcp->adapt_pending = true;
/* A display powered back up starts outside the idle cache. */
if (!ret && on) {
dcp->runtime->idle_state = 0;
Expand Down Expand Up @@ -3966,6 +4177,8 @@ static int iomfb_m4_power_ungate(struct iomfb_m4 *dcp)
if (ret)
goto fail;
WRITE_ONCE(dcp->gated, false);
if (dcp->adapt_active)
dcp->adapt_pending = true;

step = "A414";
ret = dcp_m4_runtime_call(r, 414, false, prepare, sizeof(prepare),
Expand Down Expand Up @@ -6548,8 +6761,11 @@ int iomfb_m4_resume(struct apple_dcp *owner)
ret = 0;
} else if (dcp->powered_off) {
ret = dcp_m4_set_power_state(dcp, true);
if (!ret)
if (!ret) {
dcp->powered_off = false;
if (dcp->adapt_active)
dcp->adapt_pending = true;
}
} else {
ret = dcp_m4_runtime_health(dcp->runtime);
if (ret)
Expand Down Expand Up @@ -6610,6 +6826,7 @@ void iomfb_m4_quiesce(struct apple_dcp *owner)
/* Wake any RPC waiter before waiting for workers; never recycle its DMA. */
dcp_m4_failed(dcp, -ENODEV);
cancel_work_sync(&dcp->backlight_work);
cancel_work_sync(&dcp->adapt_work);
if (dcp->runtime)
flush_workqueue(dcp->runtime->wq);
/* After the flush, as drained callbacks can queue it. */
Expand Down
Loading
Loading