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/mutex.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/kfifo_buf.h>
25 #define ACPI_ALS_CLASS "als"
26 #define ACPI_ALS_DEVICE_NAME "acpi-als"
27 #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80
29 ACPI_MODULE_NAME("acpi-als");
32 * So far, there's only one channel in here, but the specification for
33 * ACPI0008 says there can be more to what the block can report. Like
34 * chromaticity and such. We are ready for incoming additions!
36 static const struct iio_chan_spec acpi_als_channels
[] = {
44 /* _RAW is here for backward ABI compatibility */
45 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
46 BIT(IIO_CHAN_INFO_PROCESSED
),
51 * The event buffer contains timestamp and all the data from
52 * the ACPI0008 block. There are multiple, but so far we only
53 * support _ALI (illuminance). Once someone adds new channels
54 * to acpi_als_channels[], the evt_buffer below will grow
57 #define ACPI_ALS_EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels)
58 #define ACPI_ALS_EVT_BUFFER_SIZE \
59 (sizeof(s64) + (ACPI_ALS_EVT_NR_SOURCES * sizeof(s32)))
62 struct acpi_device
*device
;
65 s32 evt_buffer
[ACPI_ALS_EVT_BUFFER_SIZE
];
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_EXCEPTION((AE_INFO
, status
, "Error reading ALS %s", prop
));
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
);
107 s32
*buffer
= als
->evt_buffer
;
108 s64 time_ns
= iio_get_time_ns(indio_dev
);
112 mutex_lock(&als
->lock
);
114 memset(buffer
, 0, ACPI_ALS_EVT_BUFFER_SIZE
);
117 case ACPI_ALS_NOTIFY_ILLUMINANCE
:
118 ret
= acpi_als_read_value(als
, ACPI_ALS_ILLUMINANCE
, &val
);
124 /* Unhandled event */
125 dev_dbg(&device
->dev
, "Unhandled ACPI ALS event (%08x)!\n",
130 iio_push_to_buffers_with_timestamp(indio_dev
, als
->evt_buffer
, time_ns
);
133 mutex_unlock(&als
->lock
);
136 static int acpi_als_read_raw(struct iio_dev
*indio_dev
,
137 struct iio_chan_spec
const *chan
, int *val
,
138 int *val2
, long mask
)
140 struct acpi_als
*als
= iio_priv(indio_dev
);
144 if ((mask
!= IIO_CHAN_INFO_PROCESSED
) && (mask
!= IIO_CHAN_INFO_RAW
))
147 /* we support only illumination (_ALI) so far. */
148 if (chan
->type
!= IIO_LIGHT
)
151 ret
= acpi_als_read_value(als
, ACPI_ALS_ILLUMINANCE
, &temp_val
);
160 static const struct iio_info acpi_als_info
= {
161 .read_raw
= acpi_als_read_raw
,
164 static int acpi_als_add(struct acpi_device
*device
)
166 struct acpi_als
*als
;
167 struct iio_dev
*indio_dev
;
168 struct iio_buffer
*buffer
;
170 indio_dev
= devm_iio_device_alloc(&device
->dev
, sizeof(*als
));
174 als
= iio_priv(indio_dev
);
176 device
->driver_data
= indio_dev
;
177 als
->device
= device
;
178 mutex_init(&als
->lock
);
180 indio_dev
->name
= ACPI_ALS_DEVICE_NAME
;
181 indio_dev
->dev
.parent
= &device
->dev
;
182 indio_dev
->info
= &acpi_als_info
;
183 indio_dev
->modes
= INDIO_BUFFER_SOFTWARE
;
184 indio_dev
->channels
= acpi_als_channels
;
185 indio_dev
->num_channels
= ARRAY_SIZE(acpi_als_channels
);
187 buffer
= devm_iio_kfifo_allocate(&device
->dev
);
191 iio_device_attach_buffer(indio_dev
, buffer
);
193 return devm_iio_device_register(&device
->dev
, indio_dev
);
196 static const struct acpi_device_id acpi_als_device_ids
[] = {
201 MODULE_DEVICE_TABLE(acpi
, acpi_als_device_ids
);
203 static struct acpi_driver acpi_als_driver
= {
205 .class = ACPI_ALS_CLASS
,
206 .ids
= acpi_als_device_ids
,
209 .notify
= acpi_als_notify
,
213 module_acpi_driver(acpi_als_driver
);
215 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
216 MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
217 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
218 MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
219 MODULE_LICENSE("GPL");