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
151 changes: 148 additions & 3 deletions drivers/gpu/drm/google/gs-dsim-cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* registers a MIPI DSI host on DSIM0, so the panel driver can bind and send
* DCS commands (brightness, display on/off) through the DSIM packet header
* and payload FIFOs, which the hardware interleaves with frame transfers.
* It also reads from the panel (its ID, say): the request goes out with a
* bus turnaround, and the response is polled for and taken from the RX FIFO.
*
* Nothing is sent until the "allow_tx" module parameter is set, so the
* driver can first be loaded to read the hardware state only.
Expand All @@ -35,22 +37,28 @@

#include <drm/drm_mipi_dsi.h>
#include <drm/drm_panel.h>
#include <video/mipi_display.h>

#include "gs-dsim.h"

#define DSIM_VERSION 0x0000
#define DSIM_LINK_STATUS1 0x000c
#define DSIM_DPHY_STATUS 0x001c
#define DSIM_CLK_CTRL 0x0020
#define DSIM_TIMEOUT 0x0028
#define DSIM_ESCMODE 0x002c
#define DSIM_ESCMODE_CMD_LPDT BIT(7)
#define DSIM_UNDERRUN_CTRL 0x0034
#define DSIM_CONFIG 0x004c
#define DSIM_INTSRC 0x0050
#define DSIM_INTSRC_RX_DATA_DONE BIT(18)
#define DSIM_INTMSK 0x0054
#define DSIM_PKTHDR 0x0058
#define DSIM_PKTHDR_BTA BIT(24)
#define DSIM_PAYLOAD 0x005c
#define DSIM_RXFIFO 0x0060
#define DSIM_FIFOCTRL 0x0068
#define DSIM_FIFOCTRL_EMPTY_RX BIT(12)
#define DSIM_FIFOCTRL_FULL_PH_SFR BIT(11)
#define DSIM_FIFOCTRL_EMPTY_PH_SFR BIT(10)
#define DSIM_FIFOCTRL_FULL_PL_SFR BIT(9)
Expand All @@ -64,6 +72,15 @@
#define DSIM_MAX_PL_FIFO 2048
/* the vendor driver's MIPI_WR_TIMEOUT */
#define DSIM_TX_TIMEOUT_US 80000
/* the vendor driver's MIPI_RD_TIMEOUT */
#define DSIM_RX_TIMEOUT_US 100000
/*
* The longest read this driver takes, a limit of its own: panel IDs and
* registers are a few bytes. It is not the RX FIFO's size (64 words), and
* the vendor driver allows up to 252 bytes.
*/
#define DSIM_MAX_RX_LEN 64
#define DSIM_RX_FIFO_WORDS 64

static bool allow_tx;
module_param(allow_tx, bool, 0644);
Expand Down Expand Up @@ -211,6 +228,127 @@ static int gs_dsim_detach(struct mipi_dsi_host *host,
return 0;
}

static int gs_dsim_send_header(struct gs_dsim *dsim, u8 h0, u8 h1, u8 h2, bool bta)
{
int ret = gs_dsim_wait_fifo(dsim, DSIM_FIFOCTRL_FULL_PH_SFR, false);

if (ret)
return ret;
writel(h0 | h1 << 8 | h2 << 16 | (bta ? DSIM_PKTHDR_BTA : 0),
dsim->regs + DSIM_PKTHDR);
return gs_dsim_wait_fifo(dsim, DSIM_FIFOCTRL_EMPTY_PH_SFR, true);
}

/* Empty the RX FIFO, so that nothing left in it is taken for a response. */
static void gs_dsim_drain_rx(struct gs_dsim *dsim)
{
int i;

for (i = 0; i < DSIM_RX_FIFO_WORDS &&
!(readl(dsim->regs + DSIM_FIFOCTRL) & DSIM_FIFOCTRL_EMPTY_RX); i++)
readl(dsim->regs + DSIM_RXFIFO);
}

/*
* A read, as the vendor driver's dsim_read_data(): set the maximum return
* packet size, send the read request, wait for RX_DATA_DONE (polled here),
* then parse the response from the RX FIFO. Lock held.
*/
static ssize_t gs_dsim_read(struct gs_dsim *dsim, const struct mipi_dsi_msg *msg)
{
struct mipi_dsi_packet packet;
u8 *rx_buf = msg->rx_buf;
u32 rx, val, intmsk;
size_t rx_size = 0, i = 0;
int ret;

if (msg->rx_len > DSIM_MAX_RX_LEN)
return -EINVAL;
ret = mipi_dsi_create_packet(&packet, msg);
if (ret)
return ret;

/* the vendor's BTA and LP RX timeouts, if the bootloader left none */
if (!readl(dsim->regs + DSIM_TIMEOUT))
writel(0xff << 16 | 0xffff, dsim->regs + DSIM_TIMEOUT);

/* a response that came after an earlier read timed out */
gs_dsim_drain_rx(dsim);

/* let RX_DATA_DONE latch in INTSRC while the read runs (no handler: polled) */
intmsk = readl(dsim->regs + DSIM_INTMSK);
writel(intmsk & ~DSIM_INTSRC_RX_DATA_DONE, dsim->regs + DSIM_INTMSK);
writel(DSIM_INTSRC_RX_DATA_DONE, dsim->regs + DSIM_INTSRC);
ret = gs_dsim_send_header(dsim, MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
msg->rx_len & 0xff, (msg->rx_len >> 8) & 0xff, false) ?:
gs_dsim_send_header(dsim, packet.header[0], packet.header[1],
packet.header[2], true);
if (ret) {
writel(intmsk, dsim->regs + DSIM_INTMSK);
return ret;
}

ret = readl_poll_timeout(dsim->regs + DSIM_INTSRC, val,
val & DSIM_INTSRC_RX_DATA_DONE, 10, DSIM_RX_TIMEOUT_US);
if (ret) {
dev_err(dsim->dev, "read 0x%02x timed out: INTSRC 0x%08x FIFOCTRL 0x%08x CMD_STATUS 0x%08x LINK_STATUS1 0x%08x ESCMODE 0x%08x\n",
packet.header[1], readl(dsim->regs + DSIM_INTSRC),
readl(dsim->regs + DSIM_FIFOCTRL), readl(dsim->regs + DSIM_CMD_STATUS),
readl(dsim->regs + DSIM_LINK_STATUS1), readl(dsim->regs + DSIM_ESCMODE));
writel(intmsk, dsim->regs + DSIM_INTMSK);
return ret;
}
writel(intmsk, dsim->regs + DSIM_INTMSK);
writel(DSIM_INTSRC_RX_DATA_DONE, dsim->regs + DSIM_INTSRC);

rx = readl(dsim->regs + DSIM_RXFIFO);
switch (rx & 0xff) {
case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
rx_buf[0] = (rx >> 8) & 0xff;
rx_size = 1;
break;
case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
rx_buf[0] = (rx >> 8) & 0xff;
if (msg->rx_len > 1)
rx_buf[1] = (rx >> 16) & 0xff;
rx_size = min_t(size_t, msg->rx_len, 2);
break;
case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
rx_size = min_t(size_t, (rx >> 8) & 0xffff, msg->rx_len);
while (i < rx_size) {
size_t max = min_t(size_t, rx_size, i + 4);

rx = readl(dsim->regs + DSIM_RXFIFO);
for (; i < max; i++, rx >>= 8)
rx_buf[i] = rx & 0xff;
}
break;
/*
* Stricter than the vendor driver: its dsim_reg_rx_err_handler() takes
* an error report with no error bits set as a read of zero bytes, and
* it accepts an EoTp packet. Here both fail the read, so a caller never
* takes a read that returned no data for one that did.
*/
case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
dev_warn(dsim->dev, "read 0x%02x: error report 0x%04x\n",
packet.header[1], (rx >> 8) & 0xffff);
ret = -EIO;
break;
default:
dev_warn(dsim->dev, "read 0x%02x: unexpected response 0x%08x\n",
packet.header[1], rx);
ret = -EIO;
}

/* leave the RX FIFO empty for the next read */
gs_dsim_drain_rx(dsim);

return ret ?: rx_size;
}

static ssize_t gs_dsim_transfer(struct mipi_dsi_host *host,
const struct mipi_dsi_msg *msg)
{
Expand All @@ -227,9 +365,16 @@ static ssize_t gs_dsim_transfer(struct mipi_dsi_host *host,
return -EPERM;
}

/* Reads need BTA and the RX FIFO, which are not wired up yet. */
if (msg->rx_len)
return -EOPNOTSUPP;
if (msg->rx_len) {
mutex_lock(&dsim->lock);
/* as for writes below: with packet go on, nothing goes out */
if (readl(dsim->regs + DSIM_CMD_CONFIG) & DSIM_CMD_CONFIG_PKT_GO_EN)
ret = -EBUSY;
else
ret = gs_dsim_read(dsim, msg);
mutex_unlock(&dsim->lock);
return ret;
}

ret = mipi_dsi_create_packet(&packet, msg);
if (ret)
Expand Down
88 changes: 87 additions & 1 deletion drivers/gpu/drm/panel/panel-google-tg4c.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* the panel keeps its registers), switches it between 60 and 120 Hz and
* sets its brightness. A panel without the bootloader's initialisation is
* not handled.
*
* The Pixel 9a comes with one of three panels, so the driver reads the
* panel's ID before its first command, and a panel that is not a TG4C
* gets no commands.
*/

#include <linux/backlight.h>
Expand Down Expand Up @@ -50,6 +54,23 @@ MODULE_PARM_DESC(allow_lhbm, "Allow local high brightness mode (the fingerprint
*/
static const u8 tg4c_cmd2_page2[] = { 0xF0, 0x55, 0xAA, 0x52, 0x08, 0x02 };

/*
* The panel's ID as Google's gs_panel reads it: MIPI DCS ID1 (the module's
* manufacturer), ID2 (its version), ID3 (the module/driver ID), and the first
* byte of the device descriptor block. The bootloader appends the same four
* bytes, last first, to exynos_drm.panel_name: google-tg4c.2d008004 on the
* Pixel 9a's TG4C, so ID1 0x04 and ID3 0x00.
*/
static const u8 tg4c_id_regs[] = { 0xda, 0xdb, 0xdc, MIPI_DCS_READ_DDB_START };
#define TG4C_ID1 0x04
#define TG4C_ID3 0x00

enum tg4c_id {
TG4C_ID_UNREAD,
TG4C_ID_TG4C,
TG4C_ID_OTHER,
};

struct tg4c {
struct drm_panel panel;
struct mipi_dsi_device *dsi;
Expand All @@ -62,13 +83,62 @@ struct tg4c {
bool lhbm;
struct delayed_work lhbm_off;
u16 brightness;
enum tg4c_id id;
};

static inline struct tg4c *to_tg4c(struct drm_panel *panel)
{
return container_of(panel, struct tg4c, panel);
}

/*
* Lock held. Google's tegu device tree lists three panels for the Pixel 9a
* (TG4A, TG4B and TG4C), whose commands differ: TG4B's rate switch, for
* one, sends more than TG4C's. A panel whose manufacturer or module ID is
* not the TG4C's is another panel. ID2 changes with the panel's build and
* is not checked.
*
* The ID is read before the first command rather than at probe, because
* gs-dsim-cmd refuses reads as well as writes (-EPERM) until its allow_tx
* parameter is set, which can come after the panel has probed. Until then
* nothing is sent anyway; once the read fails otherwise, the panel is
* driven as before the ID was read.
*
* Return: false if the panel is another panel.
*/
static bool tg4c_check_id(struct tg4c *ctx)
{
struct device *dev = &ctx->dsi->dev;
u8 id[ARRAY_SIZE(tg4c_id_regs)];
unsigned int i;
ssize_t ret;

if (ctx->id != TG4C_ID_UNREAD)
return ctx->id == TG4C_ID_TG4C;

for (i = 0; i < ARRAY_SIZE(tg4c_id_regs); i++) {
ret = mipi_dsi_dcs_read(ctx->dsi, tg4c_id_regs[i], &id[i], 1);
if (ret == -EPERM)
return true;
if (ret != 1) {
dev_warn(dev, "panel ID not read (%zd): taken for a TG4C\n", ret);
ctx->id = TG4C_ID_TG4C;
return true;
}
}

if (id[0] != TG4C_ID1 || id[2] != TG4C_ID3) {
dev_err(dev, "panel ID %*ph is not a TG4C's: sending it no commands\n",
(int)sizeof(id), id);
ctx->id = TG4C_ID_OTHER;
return false;
}

dev_info(dev, "panel ID %*ph\n", (int)sizeof(id), id);
ctx->id = TG4C_ID_TG4C;
return true;
}

static void tg4c_write_rate(struct mipi_dsi_multi_context *ctx, unsigned int hz)
{
if (hz == 120)
Expand Down Expand Up @@ -140,6 +210,8 @@ static int tg4c_prepare(struct drm_panel *panel)
guard(mutex)(&ctx->lock);
if (ctx->awake)
return 0;
if (!tg4c_check_id(ctx))
return -ENODEV;
mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
mipi_dsi_msleep(&dsi_ctx, 120);
/* the DSI host wakes the link at 60 Hz */
Expand All @@ -158,6 +230,8 @@ static int tg4c_enable(struct drm_panel *panel)
struct mipi_dsi_multi_context dsi_ctx = { .dsi = ctx->dsi };

guard(mutex)(&ctx->lock);
if (!tg4c_check_id(ctx))
return -ENODEV;
mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
if (!dsi_ctx.accum_err)
ctx->display_on = true;
Expand All @@ -171,6 +245,8 @@ static int tg4c_disable(struct drm_panel *panel)
struct mipi_dsi_multi_context dsi_ctx = { .dsi = ctx->dsi };

guard(mutex)(&ctx->lock);
if (!tg4c_check_id(ctx))
return -ENODEV;
tg4c_lhbm_set(ctx, false);
mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
mipi_dsi_msleep(&dsi_ctx, 100);
Expand All @@ -186,6 +262,8 @@ static int tg4c_unprepare(struct drm_panel *panel)
struct mipi_dsi_multi_context dsi_ctx = { .dsi = ctx->dsi };

guard(mutex)(&ctx->lock);
if (!tg4c_check_id(ctx))
return -ENODEV;
ctx->lhbm = false; /* sleep ends it */
mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
mipi_dsi_msleep(&dsi_ctx, 120);
Expand All @@ -204,7 +282,7 @@ static int tg4c_set_refresh_rate(struct drm_panel *panel, unsigned int hz)
return -EINVAL;

guard(mutex)(&ctx->lock);
if (!ctx->awake)
if (!ctx->awake || !tg4c_check_id(ctx))
return -ENODEV;
/* LHBM fails away from 120 Hz: end it first (Google's tg4c does too) */
if (hz != 120)
Expand Down Expand Up @@ -273,6 +351,8 @@ static int tg4c_bl_update_status(struct backlight_device *bl)
/* asleep: the level is kept and sent with the next update */
if (!ctx->awake)
return 0;
if (!tg4c_check_id(ctx))
return -ENODEV;

return mipi_dsi_dcs_set_display_brightness_large(ctx->dsi, brightness);
}
Expand Down Expand Up @@ -304,6 +384,8 @@ static ssize_t local_hbm_mode_store(struct device *dev, struct device_attribute
return -EPERM;

guard(mutex)(&ctx->lock);
if (!tg4c_check_id(ctx))
return -ENODEV;
ret = tg4c_lhbm_set(ctx, on);
return ret ?: count;
}
Expand Down Expand Up @@ -363,6 +445,10 @@ static int tg4c_probe(struct mipi_dsi_device *dsi)
ctx->panel.enabled = true;
ctx->panel.prepare_prev_first = true;

/* the ID now, before any command, if the host allows reads yet */
scoped_guard(mutex, &ctx->lock)
tg4c_check_id(ctx);

bl = devm_backlight_device_register(dev, dev_name(dev), dev, ctx,
&tg4c_bl_ops, &props);
if (IS_ERR(bl))
Expand Down