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

---
 drivers/usb/typec/tipd/core.c | 83 ++++++++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c
index 0b53e13d29cf..72e3051b3a1d 100644
--- a/drivers/usb/typec/tipd/core.c
+++ b/drivers/usb/typec/tipd/core.c
@@ -25,6 +25,13 @@
 #include <linux/sysfs.h>
 #include <linux/string.h>
 
+#ifdef CONFIG_TYPEC_TPS6598X_SPMI_APPLE
+#include <linux/spmi.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
+#include "spmi.h"
+#endif
+
 #include "tps6598x.h"
 #include "trace.h"
 
@@ -243,8 +250,13 @@ static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
 static struct tps6598x *tps6598x_from_device(struct device *dev)
 {
 	struct i2c_client *client = i2c_verify_client(dev);
-	struct tps6598x *tps = i2c_get_clientdata(client);
-	return tps;
+#ifdef CONFIG_TYPEC_TPS6598X_SPMI_APPLE
+	if (!client) {
+		struct spmi_device *device = to_spmi_device(dev);
+		return spmi_device_get_drvdata(device);
+	}
+#endif
+	return i2c_get_clientdata(client);
 }
 
 static int
@@ -2118,10 +2130,62 @@ static void tps6598x_remove_i2c(struct i2c_client *client)
 	tps6598x_remove(tps);
 }
 
+#ifdef CONFIG_TYPEC_TPS6598X_SPMI_APPLE
+static const struct of_device_id tps6598x_of_match[];
+
+static int tps6598x_probe_spmi(struct spmi_device *device)
+{
+	const struct of_device_id *match;
+	const struct tipd_data *data;
+	struct tps6598x *tps;
+	int ret;
+
+	match = of_match_device(tps6598x_of_match, &device->dev);
+	if (!match)
+		return -EINVAL;
+	data = match->data;
+
+	tps = devm_kzalloc(&device->dev, data->tps_struct_size, GFP_KERNEL);
+	if (!tps)
+		return -ENOMEM;
+
+	mutex_init(&tps->lock);
+	tps->dev = &device->dev;
+	tps->data = data;
+
+	tps->irq = of_irq_get(device->dev.of_node, 0);
+	if (tps->irq < 0)
+		return tps->irq;
+
+	tps->regmap = devm_regmap_init_spmi_tps6598x(device, &tps6598x_regmap_config);
+	if (IS_ERR(tps->regmap))
+		return PTR_ERR(tps->regmap);
+
+	spmi_command_wakeup(device);
+	mdelay(10); /* TODO: polling loop? */
+
+	ret = tps6598x_probe(tps);
+
+	if (ret == 0)
+		spmi_device_set_drvdata(device, tps);
+
+	return ret;
+}
+
+
+static void tps6598x_remove_spmi(struct spmi_device *device)
+{
+	struct tps6598x *tps = spmi_device_get_drvdata(device);
+	tps6598x_remove(tps);
+}
+#endif
+
 static int __maybe_unused tps6598x_suspend(struct device *dev)
 {
 	struct tps6598x *tps = tps6598x_from_device(dev);
 
+	// TODO: spmi sleep
+
 	if (tps->wakeup) {
 		disable_irq(tps->irq);
 		enable_irq_wake(tps->irq);
@@ -2140,6 +2204,8 @@ static int __maybe_unused tps6598x_resume(struct device *dev)
 	struct tps6598x *tps = tps6598x_from_device(dev);
 	int ret;
 
+	// TODO: spmi wakeup
+
 	ret = tps6598x_check_mode(tps);
 	if (ret < 0)
 		return ret;
@@ -2250,6 +2316,19 @@ static struct i2c_driver tps6598x_i2c_driver = {
 };
 module_i2c_driver(tps6598x_i2c_driver);
 
+#ifdef CONFIG_TYPEC_TPS6598X_SPMI_APPLE
+static struct spmi_driver tps6598x_spmi_driver = {
+        .driver = {
+                .name = "tps6598x",
+                .pm = &tps6598x_pm_ops,
+                .of_match_table = tps6598x_of_match,
+        },
+        .probe = tps6598x_probe_spmi,
+        .remove = tps6598x_remove_spmi,
+};
+module_spmi_driver(tps6598x_spmi_driver);
+#endif
+
 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
 MODULE_LICENSE("GPL v2");
 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
-- 
2.52.0

