treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / light / acpi-als.c
blob1eafd0b24e18234bf2a28323fb598d530d90586f
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ACPI Ambient Light Sensor Driver
5 * Based on ALS 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[] = {
38 .type = IIO_LIGHT,
39 .scan_type = {
40 .sign = 's',
41 .realbits = 32,
42 .storagebits = 32,
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
55 * automatically.
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)))
61 struct acpi_als {
62 struct acpi_device *device;
63 struct mutex lock;
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
71 * special.
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;
88 acpi_status status;
90 status = acpi_evaluate_integer(als->device->handle, prop, NULL,
91 &temp_val);
93 if (ACPI_FAILURE(status)) {
94 ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
95 return -EIO;
98 *val = temp_val;
100 return 0;
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);
109 s32 val;
110 int ret;
112 mutex_lock(&als->lock);
114 memset(buffer, 0, ACPI_ALS_EVT_BUFFER_SIZE);
116 switch (event) {
117 case ACPI_ALS_NOTIFY_ILLUMINANCE:
118 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
119 if (ret < 0)
120 goto out;
121 *buffer++ = val;
122 break;
123 default:
124 /* Unhandled event */
125 dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n",
126 event);
127 goto out;
130 iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns);
132 out:
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);
141 s32 temp_val;
142 int ret;
144 if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
145 return -EINVAL;
147 /* we support only illumination (_ALI) so far. */
148 if (chan->type != IIO_LIGHT)
149 return -EINVAL;
151 ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
152 if (ret < 0)
153 return ret;
155 *val = temp_val;
157 return IIO_VAL_INT;
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));
171 if (!indio_dev)
172 return -ENOMEM;
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);
188 if (!buffer)
189 return -ENOMEM;
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[] = {
197 {"ACPI0008", 0},
201 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
203 static struct acpi_driver acpi_als_driver = {
204 .name = "acpi_als",
205 .class = ACPI_ALS_CLASS,
206 .ids = acpi_als_device_ids,
207 .ops = {
208 .add = acpi_als_add,
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");