From e6ee76a2e42c052707e571c6a738c906278ec76e Mon Sep 17 00:00:00 2001
From: Alyssa Milburn <amilburn@zall.org>
Date: Sun, 8 Feb 2026 22:30:26 +0100
Subject: [PATCH 2/3] usb: typec: tipd: Add Apple SPMI regmap

---
 drivers/usb/typec/tipd/Kconfig  |   8 +++
 drivers/usb/typec/tipd/Makefile |   9 +--
 drivers/usb/typec/tipd/spmi.c   | 121 ++++++++++++++++++++++++++++++++
 drivers/usb/typec/tipd/spmi.h   |  15 ++++
 4 files changed, 149 insertions(+), 4 deletions(-)
 create mode 100644 drivers/usb/typec/tipd/spmi.c
 create mode 100644 drivers/usb/typec/tipd/spmi.h

diff --git a/drivers/usb/typec/tipd/Kconfig b/drivers/usb/typec/tipd/Kconfig
index b82715293072..767ee9f371e3 100644
--- a/drivers/usb/typec/tipd/Kconfig
+++ b/drivers/usb/typec/tipd/Kconfig
@@ -10,3 +10,11 @@ config TYPEC_TPS6598X
 
 	  If you choose to build this driver as a dynamically linked module, the
 	  module will be called tps6598x.ko.
+
+config TYPEC_TPS6598X_SPMI_APPLE
+	bool "TI TPS6598x Apple SPMI support"
+	depends on TYPEC_TPS6598X
+	select REGMAP_SPMI
+	help
+	  Say Y here to enable support for TPS6598x controllers on a SPMI bus
+	  on Apple Silicon.
diff --git a/drivers/usb/typec/tipd/Makefile b/drivers/usb/typec/tipd/Makefile
index aa439f80a889..fd27219cb5d2 100644
--- a/drivers/usb/typec/tipd/Makefile
+++ b/drivers/usb/typec/tipd/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
-CFLAGS_trace.o			:= -I$(src)
+CFLAGS_trace.o					:= -I$(src)
 
-obj-$(CONFIG_TYPEC_TPS6598X)	+= tps6598x.o
-tps6598x-y			:= core.o
-tps6598x-$(CONFIG_TRACING)	+= trace.o
+obj-$(CONFIG_TYPEC_TPS6598X)			+= tps6598x.o
+tps6598x-y					:= core.o
+tps6598x-$(CONFIG_TYPEC_TPS6598X_SPMI_APPLE)	+= spmi.o
+tps6598x-$(CONFIG_TRACING)			+= trace.o
diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c
new file mode 100644
index 000000000000..4c64d7abbf80
--- /dev/null
+++ b/drivers/usb/typec/tipd/spmi.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Regmap for Apple SPMI bridge to TPS6598x
+ *
+ * Based on regmap-spmi.c:
+ * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
+ * Based on regmap-i2c.c:
+ * Copyright 2011 Wolfson Microelectronics plc
+ * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
+ */
+
+#include <linux/regmap.h>
+#include <linux/spmi.h>
+#include <linux/module.h>
+#include <linux/init.h>
+
+#include "spmi.h"
+
+static int regmap_spmi_tps6598x_select_reg(void *context, u8 reg)
+{
+	int err;
+	u8 val;
+
+	err = spmi_register_zero_write(context, reg);
+	if (err)
+		return err;
+
+	while (1) {
+		err = spmi_register_read(context, 0, &val);
+		if (err)
+			return err;
+		if (val == (reg | 0x80))
+			continue;
+		if (val == reg)
+			break;
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int regmap_spmi_tps6598x_read(void *context,
+				 const void *reg, size_t reg_size,
+				 void *val, size_t val_size)
+{
+	u8 addr = *(u8 *)reg;
+	int err;
+	unsigned offset = 0x20;
+	size_t len;
+
+	BUG_ON(reg_size != 1);
+	BUG_ON(val_size > 0x40);
+
+	err = regmap_spmi_tps6598x_select_reg(context, addr);
+	if (err)
+		return err;
+
+	while (val_size) {
+		len = min_t(size_t, val_size, 16);
+		err = spmi_ext_register_read(context, offset, val, val_size);
+		if (err)
+			return err;
+		offset += len;
+		val += len;
+		val_size -= len;
+	}
+
+	return 0;
+}
+
+static int regmap_spmi_tps6598x_write(void *context, const void *data,
+				  size_t count)
+{
+	BUG_ON(count < 1);
+
+	u8 addr = *(u8 *)data;
+	int err = 0;
+	unsigned offset = 0xa0;
+	size_t len;
+
+	data += 1;
+	count -= 1;
+
+	BUG_ON(count > 0x40);
+
+	err = regmap_spmi_tps6598x_select_reg(context, addr);
+	if (err)
+		return err;
+
+	while (count) {
+		len = min_t(size_t, count, 16);
+		err = spmi_ext_register_write(context, offset, data, count);
+		if (err)
+			return err;
+		offset += len;
+		data += len;
+		count -= len;
+	}
+
+	return err;
+}
+
+static const struct regmap_bus regmap_spmi_tps6598x = {
+	.read				= regmap_spmi_tps6598x_read,
+	.write				= regmap_spmi_tps6598x_write,
+	.reg_format_endian_default	= REGMAP_ENDIAN_NATIVE,
+	.val_format_endian_default	= REGMAP_ENDIAN_NATIVE,
+};
+
+struct regmap *__devm_regmap_init_spmi_tps6598x(struct spmi_device *sdev,
+					    const struct regmap_config *config,
+					    struct lock_class_key *lock_key,
+					    const char *lock_name)
+{
+	return __devm_regmap_init(&sdev->dev, &regmap_spmi_tps6598x, sdev, config,
+				  lock_key, lock_name);
+}
+EXPORT_SYMBOL_GPL(__devm_regmap_init_spmi_tps6598x);
+
+MODULE_DESCRIPTION("Regmap for Apple SPMI bridge to TPS6598x");
+MODULE_LICENSE("GPL");
diff --git a/drivers/usb/typec/tipd/spmi.h b/drivers/usb/typec/tipd/spmi.h
new file mode 100644
index 000000000000..562c56a3ba6f
--- /dev/null
+++ b/drivers/usb/typec/tipd/spmi.h
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Regmap for Apple SPMI bridge to TPS6598x
+ */
+
+#include <linux/regmap.h>
+#include <linux/spmi.h>
+
+struct regmap *__devm_regmap_init_spmi_tps6598x(struct spmi_device *dev,
+					    const struct regmap_config *config,
+					    struct lock_class_key *lock_key,
+					    const char *lock_name);
+#define devm_regmap_init_spmi_tps6598x(dev, config)				\
+	__regmap_lockdep_wrapper(__devm_regmap_init_spmi_tps6598x, #config,	\
+				dev, config)
-- 
2.52.0

