Linux IIO development
 help / color / mirror / Atom feed
From: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
Cc: "Atharva Tiwari" <atharvatiwarilinuxdev@gmail.com>,
	"Ihor Odainyi" <pv4k@odayny.com>,
	"Antoine Sidem" <sidemantoine@gmail.com>,
	"Aditya Garg" <aditya.garg@linux.dev>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Len Brown" <lenb@kernel.org>,
	"Linus Walleij" <linusw@kernel.org>,
	"Bartosz Golaszewski" <brgl@kernel.org>,
	"Sven Peter" <sven@kernel.org>, "Janne Grunau" <j@jannau.net>,
	"Neal Gompa" <neal@gompa.dev>,
	"Guenter Roeck" <linux@roeck-us.net>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>, "Lee Jones" <lee@kernel.org>,
	"Pavel Machek" <pavel@kernel.org>,
	"Griffin Kroah-Hartman" <griffin@kroah.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Sebastian Reichel" <sre@kernel.org>,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Wim Van Sebroeck" <wim@linux-watchdog.org>,
	"Jarkko Sakkinen" <jarkko@kernel.org>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Roberto Sassu" <roberto.sassu@huawei.com>,
	"Dmitry Kasatkin" <dmitry.kasatkin@gmail.com>,
	"Eric Snowberg" <eric.snowberg@oracle.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
	linux-acpi@vger.kernel.org, linux-gpio@vger.kernel.org,
	asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
	linux-hwmon@vger.kernel.org, linux-iio@vger.kernel.org,
	linux-leds@vger.kernel.org, mfd@lists.linux.dev,
	platform-driver-x86@vger.kernel.org, linux-pm@vger.kernel.org,
	linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org,
	linux-integrity@vger.kernel.org, keyrings@vger.kernel.org,
	linux-security-module@vger.kernel.org
Subject: [PATCH 02/14] platform/x86: add backend for macsmc in intel macs
Date: Fri,  4 Dec 2026 08:12:42 -0500	[thread overview]
Message-ID: <20261204131619.5059-3-atharvatiwarilinuxdev@gmail.com> (raw)
In-Reply-To: <20261204131619.5059-1-atharvatiwarilinuxdev@gmail.com>

Add a backend that talks to the SMC on Intel Macs over ACPI.
It supports the old I/O port interface, and also MMIO and
interrupt-driven completion, which applesmc doesn't have.
If an interrupt is missed it falls back to polling.

This is the base that lets the macsmc drivers replace
applesmc.

Tested-by: Ihor Odainyi <pv4k@odayny.com>
Tested-by: Antoine Sidem <sidemantoine@gmail.com>
Tested-by: Aditya Garg <aditya.garg@linux.dev>
Signed-off-by: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
---
 drivers/platform/x86/Kconfig       |  10 +
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/macsmc-acpi.c | 695 +++++++++++++++++++++++++++++
 3 files changed, 706 insertions(+)
 create mode 100644 drivers/platform/x86/macsmc-acpi.c

diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 4851bf451..00474f31b 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -202,6 +202,16 @@ config ADV_SWBUTTON
 	  To compile this driver as a module, choose M here. The module will
 	  be called adv_swbutton.
 
+config MACSMC_ACPI
+	tristate "Apple SMC (System Management Controller) ACPI Backend"
+	depends on MFD_MACSMC_CORE && ACPI
+	help
+	  This driver allows communication with the SMC (System Management Controller)
+	  on Intel-Macs. It is used to control fans, control battery, and more.
+
+	  To compile this driver as a module, choose M here. The module will
+	  be called macsmc_acpi.
+
 config APPLE_GMUX
 	tristate "Apple Gmux Driver"
 	depends on ACPI && PCI
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index cab20a414..933aed4a8 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -28,6 +28,7 @@ obj-y				+= amd/
 obj-$(CONFIG_ADV_SWBUTTON)	+= adv_swbutton.o
 
 # Apple
+obj-$(CONFIG_MACSMC_ACPI)	+= macsmc-acpi.o
 obj-$(CONFIG_APPLE_GMUX)	+= apple-gmux.o
 
 # ASUS
diff --git a/drivers/platform/x86/macsmc-acpi.c b/drivers/platform/x86/macsmc-acpi.c
new file mode 100644
index 000000000..8a93245c9
--- /dev/null
+++ b/drivers/platform/x86/macsmc-acpi.c
@@ -0,0 +1,695 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+/*
+ * Apple SMC ACPI backend
+ * Copyright (C) 2026 Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
+ *
+ * PIO code taken from drivers/hwmon/applesmc.c:
+ * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
+ * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
+ */
+
+#include <linux/acpi.h>
+#include <linux/bits.h>
+#include <linux/byteorder/generic.h>
+#include <linux/cleanup.h>
+#include <linux/compiler.h>
+#include <linux/completion.h>
+#include <linux/device.h>
+#include <linux/devm-helpers.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/iopoll.h>
+#include <linux/kfifo.h>
+#include <linux/mfd/macsmc.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/reboot.h>
+#include <linux/spinlock.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+#include <linux/workqueue.h>
+
+#define SMC_IO_DATA_PORT	0
+#define SMC_IO_CMD_PORT		4
+#define SMC_IO_NR_PORTS		32
+
+#define SMC_IO_STATUS_AWAITING_DATA	BIT(0)
+#define SMC_IO_STATUS_IB_CLOSED		BIT(1)
+#define SMC_IO_STATUS_BUSY		BIT(2)
+#define SMC_IO_STATUS_IRQ_PENDING	BIT(6)
+#define SMC_IO_INT_PLIMIT		0x10
+#define SMC_IO_INT_PLIMIT_GPU		0x14
+#define SMC_IO_INT_ID_PORT		0x1F
+
+#define SMC_MMIO_KEY_DATA		0
+#define SMC_MMIO_KEY_INFO_SIZE		(SMC_MMIO_KEY_DATA + 5)
+#define SMC_MMIO_KEY_INFO_FLAGS		(SMC_MMIO_KEY_DATA + 6)
+#define SMC_MMIO_KEY_INTERRUPT_STATUS	0x4000
+#define SMC_MMIO_KEY_STATUS		0x4005
+#define SMC_MMIO_KEY_NAME		0x78
+#define SMC_MMIO_KEY_DATA_LEN		0x7D
+#define SMC_MMIO_KEY_SMC_ID		0x7E
+#define SMC_MMIO_KEY_CMD		0x7F
+#define SMC_MMIO_MIN_SIZE		0x4006
+#define SMC_MMIO_STATUS_DONE		BIT(5)
+#define SMC_MMIO_IRQ_PENDING		BIT(5)
+
+#define SMC_TIMEOUT_MS			1000
+
+struct apple_smc_acpi {
+	struct device *dev;
+
+	u8 __iomem *iomem_base;
+	u32 iomem_base_addr, iomem_base_size;
+	bool is_mmio;
+
+	u16 port_base;
+	bool has_port;
+
+	int irq;
+	bool has_interrupt;
+	bool interrupt_enabled;
+	struct completion cmd_done;
+
+	struct blocking_notifier_head event_handlers;
+	struct work_struct shutdown_work;
+	struct work_struct notifier_work;
+
+	DECLARE_KFIFO(event_fifo, u8, 64);
+	/* Protects event_fifo, shared between the IRQ handler and notifier_work */
+	spinlock_t event_lock;
+};
+
+
+static int apple_smc_acpi_port_wait(struct apple_smc_acpi *smc, u8 val, u8 mask)
+{
+	u8 status;
+	int ret;
+
+	if (smc->interrupt_enabled) {
+		reinit_completion(&smc->cmd_done);
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}
+
+	ret = read_poll_timeout(inb, status, (status & mask) == val,
+				USEC_PER_MSEC, SMC_TIMEOUT_MS * USEC_PER_MSEC,
+				false, smc->port_base + SMC_IO_CMD_PORT);
+	if (ret)
+		dev_dbg(smc->dev, "port/wait: timeout value:%u, mask:%u\n", val, mask);
+
+	return ret;
+}
+
+static int apple_smc_acpi_port_write_byte(struct apple_smc_acpi *smc, u8 buf)
+{
+	int ret;
+
+	ret = apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_IB_CLOSED);
+	if (ret)
+		return ret;
+
+	ret = apple_smc_acpi_port_wait(smc, SMC_IO_STATUS_BUSY, SMC_IO_STATUS_BUSY);
+	if (ret)
+		return ret;
+
+	outb(buf, smc->port_base + SMC_IO_DATA_PORT);
+	return 0;
+}
+
+static int apple_smc_acpi_port_write_cmd(struct apple_smc_acpi *smc, u8 buf)
+{
+	int ret;
+
+	ret = apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_IB_CLOSED);
+	if (ret)
+		return ret;
+
+	outb(buf, smc->port_base + SMC_IO_CMD_PORT);
+	return 0;
+}
+
+static int apple_smc_acpi_port_write_key(struct apple_smc_acpi *smc, smc_key key)
+{
+	int i;
+
+	for (i = 3; i >= 0; i--) {
+		if (apple_smc_acpi_port_write_byte(smc, (key >> (i * 8)) & 0xff))
+			return -EIO;
+	}
+	return 0;
+}
+
+static int apple_smc_acpi_port_valid(struct apple_smc_acpi *smc)
+{
+	int ret;
+
+	ret = apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+	if (!ret)
+		return ret;
+
+	ret = apple_smc_acpi_port_write_cmd(smc, SMC_MSG_READ_KEY);
+	if (ret)
+		return ret;
+
+	return apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+}
+
+static int apple_smc_acpi_port_read(struct apple_smc_acpi *smc, u8 cmd, smc_key key,
+				u8 *buffer, size_t len)
+{
+	int ret;
+
+	ret = apple_smc_acpi_port_valid(smc);
+	if (ret)
+		return ret;
+
+	if (apple_smc_acpi_port_write_cmd(smc, cmd) || apple_smc_acpi_port_write_key(smc, key)) {
+		dev_dbg(smc->dev, "port/read: read argument failed, key: 0x%X, cmd: 0x%X\n",
+				key, cmd);
+		return -EIO;
+	}
+
+	if (apple_smc_acpi_port_write_byte(smc, len)) {
+		dev_dbg(smc->dev, "port/read: read len failed, key: 0x%X, len: 0x%zX\n", key, len);
+		return -EIO;
+	}
+
+	for (ret = 0; ret < len; ret++) {
+		if (apple_smc_acpi_port_wait(smc,
+				SMC_IO_STATUS_AWAITING_DATA | SMC_IO_STATUS_BUSY,
+				SMC_IO_STATUS_AWAITING_DATA | SMC_IO_STATUS_BUSY)) {
+			dev_dbg(smc->dev, "port/read: read failed, key: 0x%X\n", key);
+			return -EIO;
+		}
+		buffer[ret] = inb(smc->port_base + SMC_IO_DATA_PORT);
+	}
+
+	return apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+}
+
+static int apple_smc_acpi_port_write(struct apple_smc_acpi *smc, smc_key key, const u8 *buffer,
+		size_t len)
+{
+	int ret;
+
+	ret = apple_smc_acpi_port_valid(smc);
+	if (ret)
+		return ret;
+
+	if (apple_smc_acpi_port_write_cmd(smc, SMC_MSG_WRITE_KEY) ||
+			apple_smc_acpi_port_write_key(smc, key)) {
+		dev_dbg(smc->dev, "port/write: write argument failed, key: 0x%X\n", key);
+		return -EIO;
+	}
+
+	if (apple_smc_acpi_port_write_byte(smc, len)) {
+		dev_dbg(smc->dev, "port/write: write len failed, key: 0x%X, len: 0x%zX\n",
+				key, len);
+		return -EIO;
+	}
+
+	for (ret = 0; ret < len; ret++) {
+		if (apple_smc_acpi_port_write_byte(smc, buffer[ret])) {
+			dev_dbg(smc->dev, "port/write: write failed, key: 0x%X\n", key);
+			return -EIO;
+		}
+	}
+	return apple_smc_acpi_port_wait(smc, 0, SMC_IO_STATUS_BUSY);
+}
+
+static int apple_smc_acpi_port_get_key_info(struct apple_smc_acpi *smc,
+			smc_key key, struct apple_smc_key_info *info)
+{
+	u8 buf[6];
+
+	if (apple_smc_acpi_port_read(smc, SMC_MSG_GET_KEY_INFO, key, buf, sizeof(buf)))
+		return -EIO;
+
+	if (!info)
+		return 0;
+
+	info->size = buf[0];
+	info->type_code = get_unaligned_be32(&buf[1]);
+	info->flags = buf[5];
+
+	return 0;
+}
+
+static void apple_smc_acpi_mmio_clear(struct apple_smc_acpi *smc)
+{
+	if (ioread8(smc->iomem_base + SMC_MMIO_KEY_STATUS))
+		return iowrite8(0, smc->iomem_base + SMC_MMIO_KEY_STATUS);
+}
+
+static int apple_smc_acpi_mmio_wait(struct apple_smc_acpi *smc)
+{
+	u8 status;
+	int ret;
+
+	if (smc->interrupt_enabled) {
+		if (wait_for_completion_timeout(&smc->cmd_done,
+					msecs_to_jiffies(SMC_TIMEOUT_MS)) > 0)
+			return 0;
+
+		smc->interrupt_enabled = 0;
+		dev_dbg(smc->dev, "mmio/wait: interrupt missed, reverting to polling\n");
+	}
+
+	ret = read_poll_timeout(ioread8, status, status & SMC_MMIO_STATUS_DONE,
+				USEC_PER_MSEC, SMC_TIMEOUT_MS * USEC_PER_MSEC,
+				false, smc->iomem_base + SMC_MMIO_KEY_STATUS);
+	if (ret)
+		dev_dbg(smc->dev, "mmio/wait: timeout");
+
+	return ret;
+}
+
+static int apple_smc_acpi_mmio_result(struct apple_smc_acpi *smc, smc_key key)
+{
+	u8 status;
+
+	if (apple_smc_acpi_mmio_wait(smc))
+		return -EIO;
+
+	status = ioread8(smc->iomem_base + SMC_MMIO_KEY_CMD);
+	if (status) {
+		dev_dbg(smc->dev, "mmio: command failed, status: 0x%X, key: 0x%X\n",
+			status, key);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int apple_smc_acpi_mmio_read(struct apple_smc_acpi *smc, u8 cmd, smc_key key,
+		void *buffer, size_t len)
+{
+	int ret;
+
+	reinit_completion(&smc->cmd_done);
+	apple_smc_acpi_mmio_clear(smc);
+	iowrite32be(key, smc->iomem_base + SMC_MMIO_KEY_NAME);
+	iowrite8(0, smc->iomem_base + SMC_MMIO_KEY_SMC_ID);
+	iowrite8(cmd, smc->iomem_base + SMC_MMIO_KEY_CMD);
+
+	ret = apple_smc_acpi_mmio_result(smc, key);
+	if (ret)
+		return ret;
+
+	memcpy_fromio(buffer, smc->iomem_base + SMC_MMIO_KEY_DATA, len);
+	return 0;
+}
+
+static int apple_smc_acpi_mmio_write(struct apple_smc_acpi *smc, smc_key key, const void *buffer,
+		size_t len)
+{
+	reinit_completion(&smc->cmd_done);
+	apple_smc_acpi_mmio_clear(smc);
+	iowrite32be(key, smc->iomem_base + SMC_MMIO_KEY_NAME);
+	memcpy_toio(smc->iomem_base + SMC_MMIO_KEY_DATA, buffer, len);
+	iowrite8(len, smc->iomem_base + SMC_MMIO_KEY_DATA_LEN);
+	iowrite8(0, smc->iomem_base + SMC_MMIO_KEY_SMC_ID);
+	iowrite8(SMC_MSG_WRITE_KEY, smc->iomem_base + SMC_MMIO_KEY_CMD);
+
+	return apple_smc_acpi_mmio_result(smc, key);
+}
+
+static int apple_smc_acpi_mmio_get_key_info(struct apple_smc_acpi *smc, smc_key key,
+					struct apple_smc_key_info *info)
+{
+	int ret;
+
+	reinit_completion(&smc->cmd_done);
+	apple_smc_acpi_mmio_clear(smc);
+	iowrite32be(key, smc->iomem_base + SMC_MMIO_KEY_NAME);
+	iowrite8(0, smc->iomem_base + SMC_MMIO_KEY_SMC_ID);
+	iowrite8(SMC_MSG_GET_KEY_INFO, smc->iomem_base + SMC_MMIO_KEY_CMD);
+	ret = apple_smc_acpi_mmio_result(smc, key);
+	if (ret)
+		return ret;
+
+	if (!info)
+		return 0;
+
+	info->type_code = cpu_to_be32(ioread32(smc->iomem_base + SMC_MMIO_KEY_DATA));
+	info->size = ioread8(smc->iomem_base + SMC_MMIO_KEY_INFO_SIZE);
+	info->flags = ioread8(smc->iomem_base + SMC_MMIO_KEY_INFO_FLAGS);
+
+	return 0;
+}
+
+static void apple_smc_acpi_shutdown_work(struct work_struct *work)
+{
+	orderly_poweroff(true);
+}
+
+static void apple_smc_acpi_notifier_work(struct work_struct *work)
+{
+	struct apple_smc_acpi *smc =
+		container_of(work, struct apple_smc_acpi, notifier_work);
+
+	u8 event;
+
+	for (;;) {
+		scoped_guard(spinlock_irqsave, &smc->event_lock) {
+			if (!kfifo_get(&smc->event_fifo, &event))
+				return;
+		}
+
+		blocking_notifier_call_chain(&smc->event_handlers, event, NULL);
+	}
+}
+
+static irqreturn_t apple_smc_acpi_irq(int irq, void *data)
+{
+	struct apple_smc_acpi *smc = data;
+	u8 status, int_id;
+
+	if (smc->is_mmio) {
+		status = ioread8(smc->iomem_base + SMC_MMIO_KEY_INTERRUPT_STATUS);
+		if (!(status & SMC_MMIO_IRQ_PENDING) && status)
+			return IRQ_NONE;
+	} else {
+		status = inb(smc->port_base + SMC_IO_CMD_PORT);
+		if (!(status & SMC_IO_STATUS_IRQ_PENDING) && status)
+			return IRQ_NONE;
+	}
+
+	int_id = inb(smc->port_base + SMC_IO_INT_ID_PORT);
+	switch (int_id) {
+	case SMC_INT_CMD_DONE:
+		complete(&smc->cmd_done);
+		break;
+	case SMC_INT_SHUTDOWN:
+		schedule_work(&smc->shutdown_work);
+		break;
+	case SMC_INT_BRIDGEOS_CRASH:
+		dev_crit(smc->dev, "BridgeOS Crashed!!!\n");
+		break;
+	case SMC_INT_ALS_CHANGE:
+	case SMC_INT_THERMAL_LEVEL:
+	case SMC_INT_PLATFORM_FUNCTION:
+	case SMC_INT_SMS_DROP:
+	case SMC_INT_SMS_ORIENTATION:
+	case SMC_INT_SMS_SHOCK:
+	case SMC_INT_PLIMIT_CHANGED:
+		scoped_guard(spinlock_irqsave, &smc->event_lock) {
+			if (!kfifo_put(&smc->event_fifo, int_id))
+				dev_warn_ratelimited(smc->dev, "Notification lost: %u\n",
+						     int_id);
+		}
+
+		schedule_work(&smc->notifier_work);
+		break;
+	}
+
+	return IRQ_HANDLED;
+}
+
+static acpi_status apple_smc_acpi_walk_resources(struct acpi_resource *res, void *data)
+{
+	struct apple_smc_acpi *smc = data;
+
+	switch (res->type) {
+	case ACPI_RESOURCE_TYPE_IO:
+		if (!smc->has_port) {
+			if (res->data.io.address_length < SMC_IO_NR_PORTS)
+				return AE_ERROR;
+			smc->port_base = res->data.io.minimum;
+			smc->has_port = true;
+		}
+		return AE_OK;
+
+	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
+		if (!smc->is_mmio) {
+			if (res->data.fixed_memory32.address_length < SMC_MMIO_MIN_SIZE) {
+				dev_dbg(smc->dev, "found mmio support but its too small: %u\n",
+					res->data.fixed_memory32.address_length);
+				return AE_OK;
+			}
+			smc->iomem_base_addr = res->data.fixed_memory32.address;
+			smc->iomem_base_size = res->data.fixed_memory32.address_length;
+			smc->is_mmio = true;
+		}
+		return AE_OK;
+
+	case ACPI_RESOURCE_TYPE_IRQ:
+		smc->irq = res->data.irq.interrupts[0];
+		return AE_OK;
+
+	case ACPI_RESOURCE_TYPE_END_TAG:
+		if (!smc->has_port)
+			return AE_NOT_FOUND;
+
+		return AE_OK;
+
+	default:
+		return AE_OK;
+	}
+}
+
+static void apple_smc_acpi_try_enable_mmio(struct apple_smc_acpi *smc)
+{
+	u8 ldkn;
+
+	if (!smc->is_mmio)
+		return;
+
+	smc->iomem_base = devm_ioremap(smc->dev, smc->iomem_base_addr,
+					smc->iomem_base_size);
+	if (!smc->iomem_base)
+		goto out;
+
+	if (ioread8(smc->iomem_base + SMC_MMIO_KEY_STATUS) == 0xFF) {
+		dev_dbg(smc->dev, "mmio enable failed: init status is 0xff\n");
+		goto out_iomem;
+	}
+
+	if (apple_smc_acpi_mmio_read(smc, SMC_MSG_READ_KEY, SMC_KEY(LDKN), &ldkn,
+				     sizeof(ldkn))) {
+		dev_dbg(smc->dev, "mmio enable failed: ldkn read failed\n");
+		goto out_iomem;
+	}
+
+	if (ldkn < 2) {
+		dev_dbg(smc->dev, "mmio enable failed: ldkn version less than 2\n");
+		goto out_iomem;
+	}
+	return;
+
+out_iomem:
+	devm_iounmap(smc->dev, smc->iomem_base);
+	smc->iomem_base = NULL;
+out:
+	smc->is_mmio = false;
+}
+
+static void apple_smc_acpi_try_enable_interrupts(struct apple_smc_acpi *smc)
+{
+	const u8 enable = 1;
+	int ret;
+
+	if (!smc->has_interrupt)
+		return;
+
+	if (smc->is_mmio)
+		ret = apple_smc_acpi_mmio_write(smc, SMC_KEY(NTOK), &enable, sizeof(enable));
+	else
+		ret = apple_smc_acpi_port_write(smc, SMC_KEY(NTOK), &enable, sizeof(enable));
+
+	if (ret < 0) {
+		dev_dbg(smc->dev, "Unable to enable interrupts\n");
+		smc->interrupt_enabled = 0;
+		return;
+	}
+
+	smc->interrupt_enabled = 1;
+}
+
+static int apple_smc_acpi_read(void *cookie, smc_key key, void *buf, size_t size)
+{
+	struct apple_smc_acpi *smc = cookie;
+	int ret;
+
+	if (smc->is_mmio)
+		ret = apple_smc_acpi_mmio_read(smc, SMC_MSG_READ_KEY,
+			key, buf, size);
+	else
+		ret = apple_smc_acpi_port_read(smc, SMC_MSG_READ_KEY,
+			key, buf, size);
+
+	if (ret < 0)
+		return ret;
+
+	return size;
+
+}
+
+static int apple_smc_acpi_write(void *cookie, smc_key key, const void *buf, size_t size)
+{
+	struct apple_smc_acpi *smc = cookie;
+	int ret;
+
+	if (smc->is_mmio)
+		ret = apple_smc_acpi_mmio_write(smc, key, buf, size);
+	else
+		ret = apple_smc_acpi_port_write(smc, key, buf, size);
+
+	if (ret < 0)
+		return ret;
+
+	return size;
+}
+
+static int apple_smc_acpi_get_key_by_index(void *cookie, int index, smc_key *key)
+{
+	struct apple_smc_acpi *smc = cookie;
+
+	if (smc->is_mmio)
+		return apple_smc_acpi_mmio_read(smc, SMC_MSG_GET_KEY_BY_INDEX, (smc_key) index,
+				key, 4);
+	else
+		return apple_smc_acpi_port_read(smc, SMC_MSG_GET_KEY_BY_INDEX, (smc_key) index,
+				(u8 *)key, 4);
+
+}
+
+static int apple_smc_acpi_get_key_info(void *cookie, smc_key key,
+		struct apple_smc_key_info *info)
+{
+	struct apple_smc_acpi *smc = cookie;
+
+	if (smc->is_mmio)
+		return apple_smc_acpi_mmio_get_key_info(smc, key, info);
+	else
+		return apple_smc_acpi_port_get_key_info(smc, key, info);
+
+}
+
+static void apple_smc_acpi_notifications(void *cookie)
+{
+	const u8 buf = 0;
+
+	apple_smc_acpi_write(cookie, SMC_KEY(NTOK), &buf, sizeof(buf));
+}
+
+static const struct apple_smc_backend_ops apple_smc_acpi_be_ops = {
+	.read = apple_smc_acpi_read,
+	.write = apple_smc_acpi_write,
+	.get_key_by_index = apple_smc_acpi_get_key_by_index,
+	.get_key_info = apple_smc_acpi_get_key_info,
+	.disable_notifications = apple_smc_acpi_notifications,
+};
+
+static ssize_t plimit_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct apple_smc *smc = dev_get_drvdata(dev);
+	struct apple_smc_acpi *smc_acpi = smc->be_cookie;
+
+	return sysfs_emit(buf, "%08x %02x\n",
+			inl(smc_acpi->port_base + SMC_IO_INT_PLIMIT),
+			inb(smc_acpi->port_base + SMC_IO_INT_PLIMIT_GPU));
+}
+static DEVICE_ATTR_RO(plimit);
+
+static struct attribute *apple_smc_acpi_attrs[] = {
+	&dev_attr_plimit.attr,
+	NULL
+};
+ATTRIBUTE_GROUPS(apple_smc_acpi);
+
+static int apple_smc_acpi_probe(struct platform_device *pdev)
+{
+	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
+	struct apple_smc_acpi *smc;
+	acpi_status status;
+	int ret;
+
+	smc = devm_kzalloc(&pdev->dev, sizeof(*smc), GFP_KERNEL);
+	if (!smc)
+		return -ENOMEM;
+
+	smc->dev = &pdev->dev;
+	init_completion(&smc->cmd_done);
+	status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
+				apple_smc_acpi_walk_resources, smc);
+
+	if (ACPI_FAILURE(status))
+		return -ENODEV;
+
+	if (!devm_request_region(smc->dev, smc->port_base, SMC_IO_NR_PORTS, "macsmc-acpi"))
+		return -EBUSY;
+
+	apple_smc_acpi_try_enable_mmio(smc);
+	BLOCKING_INIT_NOTIFIER_HEAD(&smc->event_handlers);
+	spin_lock_init(&smc->event_lock);
+	INIT_KFIFO(smc->event_fifo);
+
+	ret = devm_work_autocancel(smc->dev, &smc->shutdown_work,
+				   apple_smc_acpi_shutdown_work);
+	if (ret)
+		return ret;
+
+	ret = devm_work_autocancel(smc->dev, &smc->notifier_work,
+				   apple_smc_acpi_notifier_work);
+	if (ret)
+		return ret;
+
+	if (smc->irq) {
+		ret = devm_request_irq(smc->dev, smc->irq, apple_smc_acpi_irq,
+				       0, "macsmc-acpi", smc);
+		if (ret < 0)
+			dev_dbg(smc->dev, "Unable to register interrupts\n");
+		smc->has_interrupt = !ret;
+	}
+	apple_smc_acpi_try_enable_interrupts(smc);
+
+	return apple_smc_probe(smc->dev, &apple_smc_acpi_be_ops, smc, &smc->event_handlers, true);
+}
+
+static int apple_smc_acpi_resume(struct device *dev)
+{
+	struct apple_smc *smc = dev_get_drvdata(dev);
+	struct apple_smc_acpi *smc_acpi = smc->be_cookie;
+
+	/* NTOK may be set to 0 on resume */
+	if (smc_acpi->interrupt_enabled) {
+		mutex_lock(&smc->mutex);
+		smc_acpi->interrupt_enabled = 0;
+		apple_smc_acpi_try_enable_interrupts(smc_acpi);
+		mutex_unlock(&smc->mutex);
+	}
+	return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(apple_smc_acpi_ops, NULL, apple_smc_acpi_resume);
+
+static const struct acpi_device_id apple_smc_acpi_ids[] = {
+	{"APP0001", 0},
+	{}
+};
+MODULE_DEVICE_TABLE(acpi, apple_smc_acpi_ids);
+
+static struct platform_driver apple_smc_acpi_driver = {
+	.probe = apple_smc_acpi_probe,
+	.driver = {
+		.name = "macsmc-acpi",
+		.acpi_match_table = apple_smc_acpi_ids,
+		.pm = pm_sleep_ptr(&apple_smc_acpi_ops),
+		.dev_groups = apple_smc_acpi_groups,
+	},
+};
+module_platform_driver(apple_smc_acpi_driver);
+
+MODULE_AUTHOR("Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>");
+MODULE_LICENSE("Dual MIT/GPL");
+MODULE_DESCRIPTION("Apple SMC ACPI backend");
-- 
2.55.0


  parent reply	other threads:[~2026-12-04 13:19 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 13:12 [PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Atharva Tiwari
2026-12-04 13:12 ` [PATCH 01/14] treewide: Add backend for macsmc Atharva Tiwari
2026-12-04 14:07   ` Andy Shevchenko
2026-12-04 16:45   ` Sven Peter
2026-12-04 13:12 ` Atharva Tiwari [this message]
2026-12-04 14:19   ` [PATCH 02/14] platform/x86: add backend for macsmc in intel macs Andy Shevchenko
2026-12-04 14:52     ` Aditya Garg
2026-12-04 17:23       ` Andy Shevchenko
2026-12-04 18:34         ` Aditya Garg
2026-12-04 19:57     ` Atharva Tiwari
2026-12-04 13:12 ` [PATCH 03/14] watchdog: add driver for apple smc Atharva Tiwari
2026-12-04 13:12 ` [PATCH 04/14] iio/light: add new driver for macsmc Atharva Tiwari
2026-12-04 13:12 ` [PATCH 05/14] iio/accel: add driver for apple SMS Atharva Tiwari
2026-12-04 13:12 ` [PATCH 06/14] leds: add support for macsmc kbl Atharva Tiwari
2026-12-04 13:12 ` [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc Atharva Tiwari
2026-12-04 14:12   ` Andy Shevchenko
2026-12-04 14:48     ` Aditya Garg
2026-12-04 17:06       ` Andy Shevchenko
2026-12-04 17:10         ` Aditya Garg
2026-12-04 17:18           ` Andy Shevchenko
2026-12-04 17:26       ` Sven Peter
2026-12-04 13:12 ` [PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip Atharva Tiwari
2026-12-04 13:12 ` [PATCH 09/14] treewide: disable other rtc's in t2 macs Atharva Tiwari
2026-12-04 13:12 ` [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Atharva Tiwari
2026-12-04 13:12 ` [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines Atharva Tiwari
2026-12-04 14:08   ` Andy Shevchenko
2026-12-04 14:56     ` Aditya Garg
2026-12-04 17:14       ` Andy Shevchenko
2026-12-04 18:51         ` Aditya Garg
2026-12-04 20:02           ` Andy Shevchenko
2026-12-04 13:12 ` [PATCH 12/14] hwmon/macsmc: add more types Atharva Tiwari
2026-12-04 13:12 ` [PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon Atharva Tiwari
2026-12-04 13:12 ` [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Atharva Tiwari
2026-12-04 14:20 ` [PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204131619.5059-3-atharvatiwarilinuxdev@gmail.com \
    --to=atharvatiwarilinuxdev@gmail.com \
    --cc=aditya.garg@linux.dev \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andy@kernel.org \
    --cc=asahi@lists.linux.dev \
    --cc=bhelgaas@google.com \
    --cc=bp@alien8.de \
    --cc=brgl@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=dlechner@baylibre.com \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=eric.snowberg@oracle.com \
    --cc=griffin@kroah.com \
    --cc=hansg@kernel.org \
    --cc=hpa@zytor.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=j@jannau.net \
    --cc=jarkko@kernel.org \
    --cc=jic23@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=lee@kernel.org \
    --cc=lenb@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mfd@lists.linux.dev \
    --cc=mingo@redhat.com \
    --cc=neal@gompa.dev \
    --cc=nuno.sa@analog.com \
    --cc=paul@paul-moore.com \
    --cc=pavel@kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=pv4k@odayny.com \
    --cc=rafael@kernel.org \
    --cc=roberto.sassu@huawei.com \
    --cc=serge@hallyn.com \
    --cc=sidemantoine@gmail.com \
    --cc=sre@kernel.org \
    --cc=sven@kernel.org \
    --cc=tglx@kernel.org \
    --cc=wim@linux-watchdog.org \
    --cc=x86@kernel.org \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox