From: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
Cc: "Atharva Tiwari" <atharvatiwarilinuxdev@gmail.com>,
"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: [RFC PATCH 05/14] iio/accel: add driver for apple SMS
Date: Thu, 29 Oct 2026 04:52:02 -0400 [thread overview]
Message-ID: <20261029085504.3547-6-atharvatiwarilinuxdev@gmail.com> (raw)
In-Reply-To: <20261029085504.3547-1-atharvatiwarilinuxdev@gmail.com>
Add a driver for the Sudden Motion Sensor accelerometer,
exposed through IIO. It reports the three axes and also
drop, orientation and shock events.
This replaces the motion sensor support in applesmc.
Signed-off-by: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
---
drivers/iio/accel/Kconfig | 10 ++
drivers/iio/accel/Makefile | 1 +
drivers/iio/accel/macsmc-accel.c | 248 +++++++++++++++++++++++++++++++
drivers/mfd/macsmc-core.c | 10 ++
4 files changed, 269 insertions(+)
create mode 100644 drivers/iio/accel/macsmc-accel.c
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 141ac9842..50bc127a4 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -532,6 +532,16 @@ config KXCJK1013
To compile this driver as a module, choose M here: the module will
be called kxcjk-1013.
+config MACSMC_ACCEL
+ tristate "Apple SMC Motion Sensor Driver"
+ depends on MACSMC_ACPI
+ help
+ Say yes here if you want support for Apple Motion Sensors, that uses
+ the System Management Controller.
+
+ To compile this driver as a module, choose M here: the module will
+ be called macsmc-accel.
+
config MC3230
tristate "mCube MC3230 Digital Accelerometer Driver"
depends on I2C
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index fa440a859..52fe661de 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -54,6 +54,7 @@ obj-$(CONFIG_KXSD9) += kxsd9.o
obj-$(CONFIG_KXSD9_SPI) += kxsd9-spi.o
obj-$(CONFIG_KXSD9_I2C) += kxsd9-i2c.o
obj-$(CONFIG_MC3230) += mc3230.o
+obj-$(CONFIG_MACSMC_ACCEL) += macsmc-accel.o
obj-$(CONFIG_MMA7455) += mma7455_core.o
obj-$(CONFIG_MMA7455_I2C) += mma7455_i2c.o
diff --git a/drivers/iio/accel/macsmc-accel.c b/drivers/iio/accel/macsmc-accel.c
new file mode 100644
index 000000000..0abf81e7b
--- /dev/null
+++ b/drivers/iio/accel/macsmc-accel.c
@@ -0,0 +1,248 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+/*
+ * Apple SMC sudden motion sensor (SMS) driver
+ *
+ * Copyright (C) 2026 Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
+ *
+ * Based on drivers/hwmon/applesmc.c:
+ * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
+ * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
+ */
+
+#include <linux/delay.h>
+#include <linux/byteorder/generic.h>
+#include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+#include <linux/mfd/macsmc.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+
+struct macsmc_accel {
+ struct apple_smc *smc;
+ struct iio_dev *indio_dev;
+ struct notifier_block nb;
+};
+
+enum {
+ SMS_DROP = 0,
+ SMS_ORIENTATION,
+ SMS_SHOCK,
+};
+
+static int macsmc_accel_read(struct macsmc_accel *drv, smc_key key, s16 *p)
+{
+ int ret;
+
+ ret = apple_smc_read_s16(drv->smc, key, p);
+ if (ret < 0)
+ return ret;
+
+ *p = (s16)be16_to_cpu(*p);
+ return 0;
+}
+
+static int macsmc_accel_dev_init(struct macsmc_accel *drv)
+{
+ int ret, total;
+ u16 buf;
+
+ for (total = 0; total < 100; total++) {
+ ret = apple_smc_read_u16(drv->smc, SMC_KEY(MOCN), &buf);
+ if (!ret && buf)
+ return 0;
+
+ buf = 0xE0;
+ apple_smc_write_u16(drv->smc, SMC_KEY(MOCN), buf);
+ msleep(50);
+ }
+
+ return -ENODEV;
+}
+
+static int macsmc_accel_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct macsmc_accel *drv = iio_priv(indio_dev);
+ int ret;
+ s16 buf;
+
+ if (mask != IIO_CHAN_INFO_RAW)
+ return -EINVAL;
+
+ switch (chan->channel2) {
+ case IIO_MOD_X:
+ ret = macsmc_accel_read(drv, SMC_KEY(MO_X), &buf);
+ if (ret < 0)
+ return ret;
+
+ *val = buf;
+ return IIO_VAL_INT;
+ case IIO_MOD_Y:
+ ret = macsmc_accel_read(drv, SMC_KEY(MO_Y), &buf);
+ if (ret < 0)
+ return ret;
+
+ *val = buf;
+ return IIO_VAL_INT;
+ case IIO_MOD_Z:
+ ret = macsmc_accel_read(drv, SMC_KEY(MO_Z), &buf);
+ if (ret < 0)
+ return ret;
+
+ *val = buf;
+ return IIO_VAL_INT;
+ }
+
+ return -EINVAL;
+}
+
+static int macsmc_accel_event(struct notifier_block *nb, unsigned long event, void *data)
+{
+ struct macsmc_accel *drv = container_of(nb, struct macsmc_accel, nb);
+ u64 code;
+
+ /* check if event is for us */
+ if (event != 0x60 && event != 0x69 && event != 0x6F)
+ return NOTIFY_DONE;
+
+ switch (event) {
+ case 0x60:
+ code = IIO_UNMOD_EVENT_CODE(IIO_ACCEL, SMS_DROP,
+ IIO_EV_TYPE_CHANGE,
+ IIO_EV_DIR_NONE);
+ break;
+ case 0x69:
+ code = IIO_UNMOD_EVENT_CODE(IIO_ACCEL, SMS_ORIENTATION,
+ IIO_EV_TYPE_CHANGE,
+ IIO_EV_DIR_NONE);
+ break;
+ case 0x6F:
+ code = IIO_UNMOD_EVENT_CODE(IIO_ACCEL, SMS_SHOCK,
+ IIO_EV_TYPE_CHANGE,
+ IIO_EV_DIR_NONE);
+ break;
+ }
+
+ iio_push_event(drv->indio_dev, code, iio_get_time_ns(drv->indio_dev));
+ return NOTIFY_OK;
+}
+
+static void macsmc_accel_unregister_notifier(void *data)
+{
+ struct macsmc_accel *drv = data;
+
+ blocking_notifier_chain_unregister(drv->smc->event_handlers,
+ &drv->nb);
+}
+
+static const struct iio_event_spec macsmc_accel_event_spec = {
+ .type = IIO_EV_TYPE_CHANGE,
+ .dir = IIO_EV_DIR_NONE,
+ .mask_separate = BIT(IIO_EV_INFO_ENABLE),
+};
+
+static const struct iio_chan_spec macsmc_accel_channels[] = {
+ {
+ .type = IIO_ACCEL,
+ .modified = 1,
+ .channel2 = IIO_MOD_X,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ },
+ {
+ .type = IIO_ACCEL,
+ .modified = 1,
+ .channel2 = IIO_MOD_Y,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ },
+ {
+ .type = IIO_ACCEL,
+ .modified = 1,
+ .channel2 = IIO_MOD_Z,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ },
+ {
+ .type = IIO_ACCEL,
+ .channel = SMS_DROP,
+ .event_spec = &macsmc_accel_event_spec,
+ .num_event_specs = 1,
+ },
+ {
+ .type = IIO_ACCEL,
+ .channel = SMS_ORIENTATION,
+ .event_spec = &macsmc_accel_event_spec,
+ .num_event_specs = 1,
+ },
+ {
+ .type = IIO_ACCEL,
+ .channel = SMS_SHOCK,
+ .event_spec = &macsmc_accel_event_spec,
+ .num_event_specs = 1,
+ },
+};
+
+static const struct iio_info macsmc_accel_info = {
+ .read_raw = macsmc_accel_read_raw
+};
+
+static int macsmc_accel_restore(struct device *dev)
+{
+ struct macsmc_accel *drv = dev_get_drvdata(dev);
+
+ return macsmc_accel_dev_init(drv);
+}
+
+static const struct dev_pm_ops macsmc_accel_pm_ops = {
+ .restore = macsmc_accel_restore,
+};
+
+static int macsmc_accel_probe(struct platform_device *pdev)
+{
+ struct macsmc_accel *drv;
+ struct iio_dev *indio_dev;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*drv));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ drv = iio_priv(indio_dev);
+ drv->smc = dev_get_drvdata(pdev->dev.parent);
+
+ ret = macsmc_accel_dev_init(drv);
+ if (ret)
+ return ret;
+
+ drv->indio_dev = indio_dev;
+ drv->nb.notifier_call = macsmc_accel_event;
+ ret = blocking_notifier_chain_register(drv->smc->event_handlers, &drv->nb);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(&pdev->dev, macsmc_accel_unregister_notifier, drv);
+ if (ret)
+ return ret;
+
+ platform_set_drvdata(pdev, drv);
+
+ indio_dev->name = "macsmc-accel";
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->info = &macsmc_accel_info;
+ indio_dev->channels = macsmc_accel_channels;
+ indio_dev->num_channels = ARRAY_SIZE(macsmc_accel_channels);
+
+ return devm_iio_device_register(&pdev->dev, indio_dev);
+}
+
+static struct platform_driver macsmc_accel_driver = {
+ .probe = macsmc_accel_probe,
+ .driver = {
+ .name = "macsmc-accel",
+ .pm = &macsmc_accel_pm_ops,
+ },
+};
+module_platform_driver(macsmc_accel_driver);
+
+MODULE_AUTHOR("Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>");
+MODULE_DESCRIPTION("Apple SMC Motion Sensor Driver");
+MODULE_LICENSE("Dual MIT/GPL");
+MODULE_ALIAS("platform:macsmc-accel");
diff --git a/drivers/mfd/macsmc-core.c b/drivers/mfd/macsmc-core.c
index 6014df4fe..6567d2f1f 100644
--- a/drivers/mfd/macsmc-core.c
+++ b/drivers/mfd/macsmc-core.c
@@ -24,6 +24,10 @@ static const struct mfd_cell apple_smc_rtkit_devs[] = {
* There isn't device tree on acpi backend, so we need to use
* apple_smc_key_exists to register mfd devs
*/
+static const struct mfd_cell apple_smc_acpi_accel[] = {
+ MFD_CELL_NAME("macsmc-accel"),
+};
+
static const struct mfd_cell apple_smc_acpi_common[] = {
MFD_CELL_NAME("macsmc-hwmon"),
MFD_CELL_NAME("macsmc-power"),
@@ -169,6 +173,12 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
return ret;
}
+ if (apple_smc_key_exists(smc, SMC_KEY(MOCN))) {
+ ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_accel,
+ ARRAY_SIZE(apple_smc_acpi_accel), NULL, 0, NULL);
+ if (ret < 0)
+ return ret;
+ }
} else
ret = devm_mfd_add_devices(dev, -1, apple_smc_rtkit_devs,
ARRAY_SIZE(apple_smc_rtkit_devs), NULL, 0, NULL);
--
2.55.0
next prev parent reply other threads:[~2026-10-29 8:58 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 8:51 [RFC PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Atharva Tiwari
2026-10-29 8:51 ` [RFC PATCH 01/14] treewide: Add backend for macsmc Atharva Tiwari
2026-10-29 9:20 ` sashiko-bot
2026-10-29 8:51 ` [RFC PATCH 02/14] platform/x86: add backend for macsmc in intel macs Atharva Tiwari
2026-10-29 9:23 ` sashiko-bot
2026-10-29 12:59 ` Ilpo Järvinen
2026-10-29 8:52 ` [RFC PATCH 03/14] watchdog: add driver for apple smc Atharva Tiwari
2026-10-29 9:17 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 04/14] iio/light: add new driver for macsmc Atharva Tiwari
2026-10-29 9:17 ` sashiko-bot
2026-10-29 8:52 ` Atharva Tiwari [this message]
2026-10-29 9:20 ` [RFC PATCH 05/14] iio/accel: add driver for apple SMS sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 06/14] leds: add support for macsmc kbl Atharva Tiwari
2026-10-29 9:16 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc Atharva Tiwari
2026-10-29 9:15 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip Atharva Tiwari
2026-10-29 8:52 ` [RFC PATCH 09/14] treewide: disable other rtc's in t2 macs Atharva Tiwari
2026-10-29 9:16 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Atharva Tiwari
2026-10-29 9:19 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 11/14] acpi: disable battery and adapter for x86 apple machines Atharva Tiwari
2026-10-29 9:13 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 12/14] hwmon/macsmc: add more types Atharva Tiwari
2026-10-29 9:18 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon Atharva Tiwari
2026-10-29 9:20 ` sashiko-bot
2026-10-29 8:52 ` [RFC PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Atharva Tiwari
2026-10-29 9:22 ` Sasha Finkelstein
2026-10-29 9:26 ` sashiko-bot
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=20261029085504.3547-6-atharvatiwarilinuxdev@gmail.com \
--to=atharvatiwarilinuxdev@gmail.com \
--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=rafael@kernel.org \
--cc=roberto.sassu@huawei.com \
--cc=serge@hallyn.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