1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ACPI Ambient Light Sensor Driver
6 * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com>
8 * Rework for IIO subsystem:
9 * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com>
11 * Final cleanup and debugging:
12 * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de>
13 * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com>
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
32 ACPI_MODULE_NAME("acpi-als");
35 * So far, there's only one channel in here, but the specification for
36 * ACPI0008 says there can be more to what the block can report. Like
37 * chromaticity and such. We are ready for incoming additions!
39 static const struct iio_chan_spec acpi_als_channels[] = {
47 /* _RAW is here for backward ABI compatibility */
48 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
49 BIT(IIO_CHAN_INFO_PROCESSED),
51 IIO_CHAN_SOFT_TIMESTAMP(1),
55 * The event buffer contains timestamp and all the data from
56 * the ACPI0008 block. There are multiple, but so far we only
57 * support _ALI (illuminance): One channel, padding and timestamp.
59 #define ACPI_ALS_EVT_BUFFER_SIZE \
60 (sizeof(s32) + sizeof(s32) + sizeof(s64))
63 struct acpi_device *device;
65 struct iio_trigger *trig;
67 s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8);
71 * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
72 * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
75 * The _ALR property returns tables that can be used to fine-tune the values
76 * reported by the other props based on the particular hardware type and it's
77 * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
79 * So far, we support only ALI (illuminance).
81 #define ACPI_ALS_ILLUMINANCE "_ALI"
82 #define ACPI_ALS_CHROMATICITY "_ALC"
83 #define ACPI_ALS_COLOR_TEMP "_ALT"
84 #define ACPI_ALS_POLLING "_ALP"
85 #define ACPI_ALS_TABLES "_ALR"
87 static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
89 unsigned long long temp_val;
92 status = acpi_evaluate_integer(als->device->handle, prop, NULL,
95 if (ACPI_FAILURE(status)) {
96 ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
105 static void acpi_als_notify(struct acpi_device *device, u32 event)
107 struct iio_dev *indio_dev = acpi_driver_data(device);
108 struct acpi_als *als = iio_priv(indio_dev);
110 if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) {
112 case ACPI_ALS_NOTIFY_ILLUMINANCE:
113 iio_trigger_poll_chained(als->trig);
116 /* Unhandled event */
117 dev_dbg(&device->dev,
118 "Unhandled ACPI ALS event (%08x)!\n",
124 static int acpi_als_read_raw(struct iio_dev *indio_dev,
125 struct iio_chan_spec const *chan, int *val,
126 int *val2, long mask)
128 struct acpi_als *als = iio_priv(indio_dev);
132 if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
135 /* we support only illumination (_ALI) so far. */
136 if (chan->type != IIO_LIGHT)
139 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
148 static const struct iio_info acpi_als_info = {
149 .read_raw = acpi_als_read_raw,
152 static irqreturn_t acpi_als_trigger_handler(int irq, void *p)
154 struct iio_poll_func *pf = p;
155 struct iio_dev *indio_dev = pf->indio_dev;
156 struct acpi_als *als = iio_priv(indio_dev);
157 s32 *buffer = als->evt_buffer;
161 mutex_lock(&als->lock);
163 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
169 * When coming from own trigger via polls, set polling function
170 * timestamp here. Given ACPI notifier is already in a thread and call
171 * function directly, there is no need to set the timestamp in the
174 * If the timestamp was actually 0, the timestamp is set one more time.
177 pf->timestamp = iio_get_time_ns(indio_dev);
179 iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
181 mutex_unlock(&als->lock);
182 iio_trigger_notify_done(indio_dev->trig);
187 static int acpi_als_add(struct acpi_device *device)
189 struct device *dev = &device->dev;
190 struct iio_dev *indio_dev;
191 struct acpi_als *als;
194 indio_dev = devm_iio_device_alloc(dev, sizeof(*als));
198 als = iio_priv(indio_dev);
200 device->driver_data = indio_dev;
201 als->device = device;
202 mutex_init(&als->lock);
204 indio_dev->name = ACPI_ALS_DEVICE_NAME;
205 indio_dev->info = &acpi_als_info;
206 indio_dev->channels = acpi_als_channels;
207 indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
209 als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name, indio_dev->id);
213 ret = devm_iio_trigger_register(dev, als->trig);
217 * Set hardware trigger by default to let events flow when
218 * BIOS support notification.
220 indio_dev->trig = iio_trigger_get(als->trig);
222 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
223 iio_pollfunc_store_time,
224 acpi_als_trigger_handler,
229 return devm_iio_device_register(dev, indio_dev);
232 static const struct acpi_device_id acpi_als_device_ids[] = {
237 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
239 static struct acpi_driver acpi_als_driver = {
241 .class = ACPI_ALS_CLASS,
242 .ids = acpi_als_device_ids,
245 .notify = acpi_als_notify,
249 module_acpi_driver(acpi_als_driver);
251 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
252 MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
253 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
254 MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
255 MODULE_LICENSE("GPL");