1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ACPI Ambient Light Sensor Driver
8 * Rework for IIO subsystem:
11 * Final cleanup and debugging:
16 #include <linux/module.h>
17 #include <linux/acpi.h>
18 #include <linux/err.h>
19 #include <linux/irq.h>
20 #include <linux/mutex.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/trigger_consumer.h>
28 #define ACPI_ALS_CLASS "als"
29 #define ACPI_ALS_DEVICE_NAME "acpi-als"
30 #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80
33 * So far, there's only one channel in here, but the specification for
34 * ACPI0008 says there can be more to what the block can report. Like
35 * chromaticity and such. We are ready for incoming additions!
37 static const struct iio_chan_spec acpi_als_channels[] = {
45 /* _RAW is here for backward ABI compatibility */
46 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
47 BIT(IIO_CHAN_INFO_PROCESSED),
49 IIO_CHAN_SOFT_TIMESTAMP(1),
53 * The event buffer contains timestamp and all the data from
54 * the ACPI0008 block. There are multiple, but so far we only
55 * support _ALI (illuminance): One channel, padding and timestamp.
57 #define ACPI_ALS_EVT_BUFFER_SIZE \
58 (sizeof(s32) + sizeof(s32) + sizeof(s64))
61 struct acpi_device *device;
63 struct iio_trigger *trig;
65 s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8);
69 * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
70 * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
73 * The _ALR property returns tables that can be used to fine-tune the values
74 * reported by the other props based on the particular hardware type and it's
75 * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
77 * So far, we support only ALI (illuminance).
79 #define ACPI_ALS_ILLUMINANCE "_ALI"
80 #define ACPI_ALS_CHROMATICITY "_ALC"
81 #define ACPI_ALS_COLOR_TEMP "_ALT"
82 #define ACPI_ALS_POLLING "_ALP"
83 #define ACPI_ALS_TABLES "_ALR"
85 static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
87 unsigned long long temp_val;
90 status = acpi_evaluate_integer(als->device->handle, prop, NULL,
93 if (ACPI_FAILURE(status)) {
94 acpi_evaluation_failure_warn(als->device->handle, prop, status);
103 static void acpi_als_notify(struct acpi_device *device, u32 event)
105 struct iio_dev *indio_dev = acpi_driver_data(device);
106 struct acpi_als *als = iio_priv(indio_dev);
108 if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) {
110 case ACPI_ALS_NOTIFY_ILLUMINANCE:
111 iio_trigger_poll_nested(als->trig);
114 /* Unhandled event */
115 dev_dbg(&device->dev,
116 "Unhandled ACPI ALS event (%08x)!\n",
122 static int acpi_als_read_raw(struct iio_dev *indio_dev,
123 struct iio_chan_spec const *chan, int *val,
124 int *val2, long mask)
126 struct acpi_als *als = iio_priv(indio_dev);
130 if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
133 /* we support only illumination (_ALI) so far. */
134 if (chan->type != IIO_LIGHT)
137 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
146 static const struct iio_info acpi_als_info = {
147 .read_raw = acpi_als_read_raw,
150 static irqreturn_t acpi_als_trigger_handler(int irq, void *p)
152 struct iio_poll_func *pf = p;
153 struct iio_dev *indio_dev = pf->indio_dev;
154 struct acpi_als *als = iio_priv(indio_dev);
155 s32 *buffer = als->evt_buffer;
159 mutex_lock(&als->lock);
161 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
167 * When coming from own trigger via polls, set polling function
168 * timestamp here. Given ACPI notifier is already in a thread and call
169 * function directly, there is no need to set the timestamp in the
172 * If the timestamp was actually 0, the timestamp is set one more time.
175 pf->timestamp = iio_get_time_ns(indio_dev);
177 iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
179 mutex_unlock(&als->lock);
180 iio_trigger_notify_done(indio_dev->trig);
185 static int acpi_als_add(struct acpi_device *device)
187 struct device *dev = &device->dev;
188 struct iio_dev *indio_dev;
189 struct acpi_als *als;
192 indio_dev = devm_iio_device_alloc(dev, sizeof(*als));
196 als = iio_priv(indio_dev);
198 device->driver_data = indio_dev;
199 als->device = device;
200 mutex_init(&als->lock);
202 indio_dev->name = ACPI_ALS_DEVICE_NAME;
203 indio_dev->info = &acpi_als_info;
204 indio_dev->channels = acpi_als_channels;
205 indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
207 als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
208 iio_device_id(indio_dev));
212 ret = devm_iio_trigger_register(dev, als->trig);
216 * Set hardware trigger by default to let events flow when
217 * BIOS support notification.
219 indio_dev->trig = iio_trigger_get(als->trig);
221 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
222 iio_pollfunc_store_time,
223 acpi_als_trigger_handler,
228 return devm_iio_device_register(dev, indio_dev);
231 static const struct acpi_device_id acpi_als_device_ids[] = {
236 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
238 static struct acpi_driver acpi_als_driver = {
240 .class = ACPI_ALS_CLASS,
241 .ids = acpi_als_device_ids,
244 .notify = acpi_als_notify,
248 module_acpi_driver(acpi_als_driver);
253 MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
254 MODULE_LICENSE("GPL");