Linux Hardware Monitor 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>,
	"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 04/14] iio/light: add new driver for macsmc
Date: Fri,  4 Dec 2026 08:12:44 -0500	[thread overview]
Message-ID: <20261204131619.5059-5-atharvatiwarilinuxdev@gmail.com> (raw)
In-Reply-To: <20261204131619.5059-1-atharvatiwarilinuxdev@gmail.com>

Add a driver for the ambient light sensors, exposed through
the standard IIO interface with left and right channels.

This replaces the light sensor support in applesmc.

Tested-by: Ihor Odainyi <pv4k@odayny.com>
Tested-by: Antoine Sidem <sidemantoine@gmail.com>
Signed-off-by: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
---
 drivers/iio/light/Kconfig        |  11 ++
 drivers/iio/light/Makefile       |   1 +
 drivers/iio/light/macsmc-light.c | 209 +++++++++++++++++++++++++++++++
 drivers/mfd/macsmc-core.c        |  11 ++
 4 files changed, 232 insertions(+)
 create mode 100644 drivers/iio/light/macsmc-light.c

diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 462fff1dd..f84707edc 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -457,6 +457,17 @@ config LV0104CS
 	  To compile this driver as a module, choose M here:
 	  the module will be called lv0104cs.
 
+config MACSMC_LIGHT
+	tristate "Apple SMC Light Sensor"
+	depends on MACSMC_ACPI
+	help
+	  Say Y here if you have a Intel-based Mac. and you
+	  want support for light sensors.
+
+	  To compile this driver as a module, choose M here:
+	  the module will be called macsmc_light
+
+
 config MAX44000
 	tristate "MAX44000 Ambient and Infrared Proximity Sensor"
 	depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 6672ed01a..2ef4c054d 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_LTR390)		+= ltr390.o
 obj-$(CONFIG_LTR501)		+= ltr501.o
 obj-$(CONFIG_LTRF216A)		+= ltrf216a.o
 obj-$(CONFIG_LV0104CS)		+= lv0104cs.o
+obj-$(CONFIG_MACSMC_LIGHT)	+= macsmc-light.o
 obj-$(CONFIG_MAX44000)		+= max44000.o
 obj-$(CONFIG_MAX44009)		+= max44009.o
 obj-$(CONFIG_NOA1305)		+= noa1305.o
diff --git a/drivers/iio/light/macsmc-light.c b/drivers/iio/light/macsmc-light.c
new file mode 100644
index 000000000..12d88035a
--- /dev/null
+++ b/drivers/iio/light/macsmc-light.c
@@ -0,0 +1,209 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+/*
+ * Apple SMC Light Sensor Driver
+ *
+ * Copyright (C) 2026 Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
+ *
+ * Based on drivers/hwmom/applesmc.c:
+ * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
+ * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
+ */
+
+#include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/container_of.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/mfd/macsmc.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/platform_device.h>
+#include <linux/sysfs.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+
+#include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+
+enum {
+	LEFT_LIGHT = 0,
+	RIGHT_LIGHT,
+};
+
+struct macsmc_light {
+	struct apple_smc *smc;
+	struct notifier_block nb;
+	struct iio_dev *indio_dev;
+	u8 size;		/* length of the ALV0 key, ALV1 has the same */
+	bool single_sensor;	/* one combined 10-bit value instead of ALV0 + ALV1 */
+};
+
+static int macsmc_light_read_raw(struct iio_dev *indio_dev,
+		const struct iio_chan_spec *chan,
+		int *val, int *val2, long mask)
+
+{
+	struct macsmc_light *drv = iio_priv(indio_dev);
+	u8 buffer[10];
+	int ret;
+
+	if (mask != IIO_CHAN_INFO_RAW)
+		return -EINVAL;
+
+	switch (chan->channel) {
+	case LEFT_LIGHT:
+		ret = apple_smc_read(drv->smc, SMC_KEY(ALV0), buffer, drv->size);
+		if (ret < 0)
+			return ret;
+
+		if (drv->single_sensor)
+			*val = get_unaligned_be16(buffer + 6) >> 2;
+		else
+			*val = buffer[2];
+
+		break;
+	case RIGHT_LIGHT:
+		if (drv->single_sensor) {
+			*val = 0;
+			break;
+		}
+		ret = apple_smc_read(drv->smc, SMC_KEY(ALV1), buffer, drv->size);
+		if (ret < 0)
+			return ret;
+
+		*val = buffer[2];
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return IIO_VAL_INT;
+}
+
+static int macsmc_light_event(struct notifier_block *nb, unsigned long event, void *data)
+{
+	struct macsmc_light *drv = container_of(nb, struct macsmc_light, nb);
+	struct iio_dev *indio_dev = drv->indio_dev;
+	s64 timestamp = iio_get_time_ns(indio_dev);
+
+	/* check if event is for us */
+	if (event != SMC_INT_ALS_CHANGE)
+		return NOTIFY_DONE;
+
+	iio_push_event(indio_dev,
+		       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, LEFT_LIGHT,
+					    IIO_EV_TYPE_CHANGE,
+					    IIO_EV_DIR_EITHER),
+		       timestamp);
+
+	if (!drv->single_sensor) {
+		iio_push_event(indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, RIGHT_LIGHT,
+						    IIO_EV_TYPE_CHANGE,
+						    IIO_EV_DIR_EITHER),
+			       timestamp);
+	}
+
+	return NOTIFY_OK;
+}
+
+static void macsmc_light_unregister_notifier(void *data)
+{
+	struct macsmc_light *drv = data;
+
+	blocking_notifier_chain_unregister(drv->smc->event_handlers,
+					   &drv->nb);
+}
+
+static const struct iio_event_spec macsmc_light_events[] = {
+	{
+		.type = IIO_EV_TYPE_CHANGE,
+		.dir = IIO_EV_DIR_EITHER,
+	},
+};
+
+static const struct iio_chan_spec macsmc_light_channels[] = {
+	{
+		.type = IIO_LIGHT,
+		.channel = LEFT_LIGHT,
+		.indexed = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.event_spec = macsmc_light_events,
+		.num_event_specs = ARRAY_SIZE(macsmc_light_events),
+	},
+	{
+		.type = IIO_LIGHT,
+		.channel = RIGHT_LIGHT,
+		.indexed = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.event_spec = macsmc_light_events,
+		.num_event_specs = ARRAY_SIZE(macsmc_light_events),
+	},
+};
+
+static int macsmc_light_read_label(struct iio_dev *indio_dev,
+				   const struct iio_chan_spec *chan, char *label)
+{
+	return sysfs_emit(label, "%s\n", chan->channel == LEFT_LIGHT ? "left" : "right");
+}
+
+static const struct iio_info macsmc_light_info = {
+	.read_raw = macsmc_light_read_raw,
+	.read_label = macsmc_light_read_label,
+};
+
+static int macsmc_light_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct iio_dev *indio_dev;
+	struct macsmc_light *drv;
+	struct apple_smc_key_info info;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*drv));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	drv = iio_priv(indio_dev);
+	drv->smc = dev_get_drvdata(dev->parent);
+
+	ret = apple_smc_get_key_info(drv->smc, SMC_KEY(ALV0), &info);
+	if (ret < 0)
+		return -ENODEV;
+
+	if (info.size < 3 || info.size > 10)
+		return -ENODEV;
+
+	drv->size = info.size;
+	drv->single_sensor = info.size == 10;
+	drv->indio_dev = indio_dev;
+	drv->nb.notifier_call = macsmc_light_event;
+	ret = blocking_notifier_chain_register(drv->smc->event_handlers, &drv->nb);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, macsmc_light_unregister_notifier, drv);
+	if (ret)
+		return ret;
+
+	indio_dev->name = "macsmc-light";
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &macsmc_light_info;
+	indio_dev->channels = macsmc_light_channels;
+	indio_dev->num_channels = ARRAY_SIZE(macsmc_light_channels);
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
+static struct platform_driver macsmc_light_driver = {
+	.probe = macsmc_light_probe,
+	.driver = {
+		.name = "macsmc-light",
+	},
+};
+module_platform_driver(macsmc_light_driver);
+
+MODULE_AUTHOR("Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>");
+MODULE_LICENSE("Dual MIT/GPL");
+MODULE_DESCRIPTION("Apple SMC Light Sensor Driver");
+MODULE_ALIAS("platform:macsmc-light");
diff --git a/drivers/mfd/macsmc-core.c b/drivers/mfd/macsmc-core.c
index 1c1469828..6014df4fe 100644
--- a/drivers/mfd/macsmc-core.c
+++ b/drivers/mfd/macsmc-core.c
@@ -29,6 +29,10 @@ static const struct mfd_cell apple_smc_acpi_common[] = {
 	MFD_CELL_NAME("macsmc-power"),
 };
 
+static const struct mfd_cell apple_smc_acpi_light[] = {
+	MFD_CELL_NAME("macsmc-light"),
+};
+
 static const struct mfd_cell apple_smc_acpi_rtc[] = {
 	MFD_CELL_NAME("macsmc-rtc"),
 };
@@ -158,6 +162,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
 				return ret;
 		}
 
+		if (apple_smc_key_exists(smc, SMC_KEY(ALV0))) {
+			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_light,
+				ARRAY_SIZE(apple_smc_acpi_light), 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


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

Thread overview: 55+ 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 13:56   ` sashiko-bot
2026-12-04 14:07   ` Andy Shevchenko
2026-12-04 16:45   ` Sven Peter
2026-12-04 13:12 ` [PATCH 02/14] platform/x86: add backend for macsmc in intel macs Atharva Tiwari
2026-12-04 13:58   ` sashiko-bot
2026-12-04 14:19   ` 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 20:23       ` Andy Shevchenko
2026-12-04 13:12 ` [PATCH 03/14] watchdog: add driver for apple smc Atharva Tiwari
2026-12-04 13:48   ` sashiko-bot
2026-12-04 13:12 ` Atharva Tiwari [this message]
2026-12-04 13:47   ` [PATCH 04/14] iio/light: add new driver for macsmc sashiko-bot
2026-12-04 13:12 ` [PATCH 05/14] iio/accel: add driver for apple SMS Atharva Tiwari
2026-12-04 14:11   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 06/14] leds: add support for macsmc kbl Atharva Tiwari
2026-12-04 14:07   ` sashiko-bot
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 14:16   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip Atharva Tiwari
2026-12-04 14:11   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 09/14] treewide: disable other rtc's in t2 macs Atharva Tiwari
2026-12-04 14:27   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Atharva Tiwari
2026-12-04 14:39   ` sashiko-bot
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 20:13             ` Andy Shevchenko
2026-12-04 14:25   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 12/14] hwmon/macsmc: add more types Atharva Tiwari
2026-12-04 14:37   ` sashiko-bot
2026-12-04 18:44     ` Guenter Roeck
2026-12-04 13:12 ` [PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon Atharva Tiwari
2026-12-04 14:48   ` sashiko-bot
2026-12-04 18:45     ` Guenter Roeck
2026-12-04 20:13       ` Jarkko Sakkinen
2026-12-04 13:12 ` [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Atharva Tiwari
2026-12-04 13:40   ` Sasha Finkelstein
2026-12-04 19:43     ` Atharva Tiwari
2026-12-04 14:48   ` sashiko-bot
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-5-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=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