[v2,06/15] drm/bridge: tc358767: Simplify AUX data read

Submitted by Andrey Smirnov on March 22, 2019, 3:28 a.m.

Details

Message ID 20190322032901.12045-7-andrew.smirnov@gmail.com
State Superseded
Headers show
Series "tc358767 driver improvements" ( rev: 2 ) in DRI devel

Not browsing as part of any series.

Commit Message

Andrey Smirnov March 22, 2019, 3:28 a.m.
Simplify AUX data read by removing index arithmetic and shifting with
a helper functions that does three things:

    1. Fetch data from up to 4 32-bit registers from the chip
    2. Optionally fix data endianness (not needed on LE hosts)
    3. Copy read data into user provided array.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 drivers/gpu/drm/bridge/tc358767.c | 39 ++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 13 deletions(-)

Patch hide | download patch | download mbox

diff --git a/drivers/gpu/drm/bridge/tc358767.c b/drivers/gpu/drm/bridge/tc358767.c
index 060e4b05095a..81c10a5d8106 100644
--- a/drivers/gpu/drm/bridge/tc358767.c
+++ b/drivers/gpu/drm/bridge/tc358767.c
@@ -307,6 +307,28 @@  static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
 	return 0;
 }
 
+static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size)
+{
+	u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)];
+	int ret, i, count = DIV_ROUND_UP(size, 4);
+
+	for (i = 0; i < count; i++) {
+		ret = regmap_read(tc->regmap, DP0_AUXRDATA(i), &auxrdata[i]);
+		if (ret)
+			return ret;
+		/*
+		 * Our regmap is configured as LE for register data,
+		 * so we need undo any byte swapping that might have
+		 * happened to preserve original byte order.
+		 */
+		le32_to_cpus(&auxrdata[i]);
+	}
+
+	memcpy(data, auxrdata, size);
+
+	return size;
+}
+
 static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
 			       struct drm_dp_aux_msg *msg)
 {
@@ -365,19 +387,10 @@  static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
 	if (ret)
 		return ret;
 
-	if (request == DP_AUX_I2C_READ || request == DP_AUX_NATIVE_READ) {
-		/* Read data */
-		while (i < size) {
-			if ((i % 4) == 0) {
-				ret = regmap_read(tc->regmap,
-						  DP0_AUXRDATA(i >> 2), &tmp);
-				if (ret)
-					return ret;
-			}
-			buf[i] = tmp & 0xff;
-			tmp = tmp >> 8;
-			i++;
-		}
+	switch (request) {
+	case DP_AUX_NATIVE_READ:
+	case DP_AUX_I2C_READ:
+		return tc_aux_read_data(tc, msg->buffer, size);
 	}
 
 	return size;