1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ACPI Ambient Light Sensor Driver 4 * 5 * Based on ALS driver: 6 * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com> 7 * 8 * Rework for IIO subsystem: 9 * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com> 10 * 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> 14 */ 15 16 #include <linux/module.h> 17 #include <linux/acpi.h> 18 #include <linux/err.h> 19 #include <linux/mutex.h> 20 21 #include <linux/iio/iio.h> 22 #include <linux/iio/buffer.h> 23 #include <linux/iio/kfifo_buf.h> 24 25 #define ACPI_ALS_CLASS "als" 26 #define ACPI_ALS_DEVICE_NAME "acpi-als" 27 #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80 28 29 /* 30 * So far, there's only one channel in here, but the specification for 31 * ACPI0008 says there can be more to what the block can report. Like 32 * chromaticity and such. We are ready for incoming additions! 33 */ 34 static const struct iio_chan_spec acpi_als_channels[] = { 35 { 36 .type = IIO_LIGHT, 37 .scan_type = { 38 .sign = 's', 39 .realbits = 32, 40 .storagebits = 32, 41 }, 42 /* _RAW is here for backward ABI compatibility */ 43 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 44 BIT(IIO_CHAN_INFO_PROCESSED), 45 }, 46 }; 47 48 /* 49 * The event buffer contains timestamp and all the data from 50 * the ACPI0008 block. There are multiple, but so far we only 51 * support _ALI (illuminance). Once someone adds new channels 52 * to acpi_als_channels[], the evt_buffer below will grow 53 * automatically. 54 */ 55 #define ACPI_ALS_EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels) 56 #define ACPI_ALS_EVT_BUFFER_SIZE \ 57 (sizeof(s64) + (ACPI_ALS_EVT_NR_SOURCES * sizeof(s32))) 58 59 struct acpi_als { 60 struct acpi_device *device; 61 struct mutex lock; 62 63 s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE]; 64 }; 65 66 /* 67 * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT 68 * and ALP can all be handled by acpi_als_read_value() below, while the ALR is 69 * special. 70 * 71 * The _ALR property returns tables that can be used to fine-tune the values 72 * reported by the other props based on the particular hardware type and it's 73 * location (it contains tables for "rainy", "bright inhouse lighting" etc.). 74 * 75 * So far, we support only ALI (illuminance). 76 */ 77 #define ACPI_ALS_ILLUMINANCE "_ALI" 78 #define ACPI_ALS_CHROMATICITY "_ALC" 79 #define ACPI_ALS_COLOR_TEMP "_ALT" 80 #define ACPI_ALS_POLLING "_ALP" 81 #define ACPI_ALS_TABLES "_ALR" 82 83 static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val) 84 { 85 unsigned long long temp_val; 86 acpi_status status; 87 88 status = acpi_evaluate_integer(als->device->handle, prop, NULL, 89 &temp_val); 90 91 if (ACPI_FAILURE(status)) { 92 acpi_evaluation_failure_warn(als->device->handle, prop, status); 93 return -EIO; 94 } 95 96 *val = temp_val; 97 98 return 0; 99 } 100 101 static void acpi_als_notify(struct acpi_device *device, u32 event) 102 { 103 struct iio_dev *indio_dev = acpi_driver_data(device); 104 struct acpi_als *als = iio_priv(indio_dev); 105 s32 *buffer = als->evt_buffer; 106 s64 time_ns = iio_get_time_ns(indio_dev); 107 s32 val; 108 int ret; 109 110 mutex_lock(&als->lock); 111 112 memset(buffer, 0, ACPI_ALS_EVT_BUFFER_SIZE); 113 114 switch (event) { 115 case ACPI_ALS_NOTIFY_ILLUMINANCE: 116 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val); 117 if (ret < 0) 118 goto out; 119 *buffer++ = val; 120 break; 121 default: 122 /* Unhandled event */ 123 dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n", 124 event); 125 goto out; 126 } 127 128 iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns); 129 130 out: 131 mutex_unlock(&als->lock); 132 } 133 134 static int acpi_als_read_raw(struct iio_dev *indio_dev, 135 struct iio_chan_spec const *chan, int *val, 136 int *val2, long mask) 137 { 138 struct acpi_als *als = iio_priv(indio_dev); 139 s32 temp_val; 140 int ret; 141 142 if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW)) 143 return -EINVAL; 144 145 /* we support only illumination (_ALI) so far. */ 146 if (chan->type != IIO_LIGHT) 147 return -EINVAL; 148 149 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val); 150 if (ret < 0) 151 return ret; 152 153 *val = temp_val; 154 155 return IIO_VAL_INT; 156 } 157 158 static const struct iio_info acpi_als_info = { 159 .read_raw = acpi_als_read_raw, 160 }; 161 162 static int acpi_als_add(struct acpi_device *device) 163 { 164 struct acpi_als *als; 165 struct iio_dev *indio_dev; 166 struct iio_buffer *buffer; 167 168 indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als)); 169 if (!indio_dev) 170 return -ENOMEM; 171 172 als = iio_priv(indio_dev); 173 174 device->driver_data = indio_dev; 175 als->device = device; 176 mutex_init(&als->lock); 177 178 indio_dev->name = ACPI_ALS_DEVICE_NAME; 179 indio_dev->info = &acpi_als_info; 180 indio_dev->modes = INDIO_BUFFER_SOFTWARE; 181 indio_dev->channels = acpi_als_channels; 182 indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels); 183 184 buffer = devm_iio_kfifo_allocate(&device->dev); 185 if (!buffer) 186 return -ENOMEM; 187 188 iio_device_attach_buffer(indio_dev, buffer); 189 190 return devm_iio_device_register(&device->dev, indio_dev); 191 } 192 193 static const struct acpi_device_id acpi_als_device_ids[] = { 194 {"ACPI0008", 0}, 195 {}, 196 }; 197 198 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids); 199 200 static struct acpi_driver acpi_als_driver = { 201 .name = "acpi_als", 202 .class = ACPI_ALS_CLASS, 203 .ids = acpi_als_device_ids, 204 .ops = { 205 .add = acpi_als_add, 206 .notify = acpi_als_notify, 207 }, 208 }; 209 210 module_acpi_driver(acpi_als_driver); 211 212 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>"); 213 MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>"); 214 MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); 215 MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver"); 216 MODULE_LICENSE("GPL"); 217