2 * ACPI Ambient Light Sensor Driver
7 * Rework for IIO subsystem:
10 * Final cleanup and debugging:
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <linux/module.h>
30 #include <linux/acpi.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/buffer.h>
36 #include <linux/iio/kfifo_buf.h>
38 #define ACPI_ALS_CLASS "als"
39 #define ACPI_ALS_DEVICE_NAME "acpi-als"
40 #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80
42 ACPI_MODULE_NAME("acpi-als");
45 * So far, there's only one channel in here, but the specification for
46 * ACPI0008 says there can be more to what the block can report. Like
47 * chromaticity and such. We are ready for incoming additions!
49 static const struct iio_chan_spec acpi_als_channels[] = {
57 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
62 * The event buffer contains timestamp and all the data from
63 * the ACPI0008 block. There are multiple, but so far we only
64 * support _ALI (illuminance). Once someone adds new channels
65 * to acpi_als_channels[], the evt_buffer below will grow
68 #define EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels)
69 #define EVT_BUFFER_SIZE \
70 (sizeof(s64) + (EVT_NR_SOURCES * sizeof(s32)))
73 struct acpi_device *device;
76 s32 evt_buffer[EVT_BUFFER_SIZE];
80 * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
81 * and ALP can all be handled by als_read_value() below, while the ALR is
84 * The _ALR property returns tables that can be used to fine-tune the values
85 * reported by the other props based on the particular hardware type and it's
86 * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
88 * So far, we support only ALI (illuminance).
90 #define ACPI_ALS_ILLUMINANCE "_ALI"
91 #define ACPI_ALS_CHROMATICITY "_ALC"
92 #define ACPI_ALS_COLOR_TEMP "_ALT"
93 #define ACPI_ALS_POLLING "_ALP"
94 #define ACPI_ALS_TABLES "_ALR"
96 static int als_read_value(struct acpi_als *als, char *prop, s32 *val)
98 unsigned long long temp_val;
101 status = acpi_evaluate_integer(als->device->handle, prop, NULL,
104 if (ACPI_FAILURE(status)) {
105 ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
114 static void acpi_als_notify(struct acpi_device *device, u32 event)
116 struct iio_dev *indio_dev = acpi_driver_data(device);
117 struct acpi_als *als = iio_priv(indio_dev);
118 s32 *buffer = als->evt_buffer;
119 s64 time_ns = iio_get_time_ns();
123 mutex_lock(&als->lock);
125 memset(buffer, 0, EVT_BUFFER_SIZE);
128 case ACPI_ALS_NOTIFY_ILLUMINANCE:
129 ret = als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
135 /* Unhandled event */
136 dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n",
141 iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns);
144 mutex_unlock(&als->lock);
147 static int acpi_als_read_raw(struct iio_dev *indio_dev,
148 struct iio_chan_spec const *chan, int *val,
149 int *val2, long mask)
151 struct acpi_als *als = iio_priv(indio_dev);
155 if (mask != IIO_CHAN_INFO_RAW)
158 /* we support only illumination (_ALI) so far. */
159 if (chan->type != IIO_LIGHT)
162 ret = als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
171 static const struct iio_info acpi_als_info = {
172 .driver_module = THIS_MODULE,
173 .read_raw = acpi_als_read_raw,
176 static int acpi_als_add(struct acpi_device *device)
178 struct acpi_als *als;
179 struct iio_dev *indio_dev;
180 struct iio_buffer *buffer;
182 indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als));
186 als = iio_priv(indio_dev);
188 device->driver_data = indio_dev;
189 als->device = device;
190 mutex_init(&als->lock);
192 indio_dev->name = ACPI_ALS_DEVICE_NAME;
193 indio_dev->dev.parent = &device->dev;
194 indio_dev->info = &acpi_als_info;
195 indio_dev->modes = INDIO_BUFFER_SOFTWARE;
196 indio_dev->channels = acpi_als_channels;
197 indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
199 buffer = devm_iio_kfifo_allocate(&device->dev);
203 iio_device_attach_buffer(indio_dev, buffer);
205 return devm_iio_device_register(&device->dev, indio_dev);
208 static const struct acpi_device_id acpi_als_device_ids[] = {
213 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
215 static struct acpi_driver acpi_als_driver = {
217 .class = ACPI_ALS_CLASS,
218 .ids = acpi_als_device_ids,
221 .notify = acpi_als_notify,
225 module_acpi_driver(acpi_als_driver);
230 MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
231 MODULE_LICENSE("GPL");