Merge tag 'amdtee-fixes-for-v5.13' of git://git.linaro.org/people/jens.wiklander...
[linux-2.6-microblaze.git] / drivers / iio / light / acpi-als.c
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/irq.h>
20 #include <linux/mutex.h>
21
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>
27
28 #define ACPI_ALS_CLASS                  "als"
29 #define ACPI_ALS_DEVICE_NAME            "acpi-als"
30 #define ACPI_ALS_NOTIFY_ILLUMINANCE     0x80
31
32 ACPI_MODULE_NAME("acpi-als");
33
34 /*
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!
38  */
39 static const struct iio_chan_spec acpi_als_channels[] = {
40         {
41                 .type           = IIO_LIGHT,
42                 .scan_type      = {
43                         .sign           = 's',
44                         .realbits       = 32,
45                         .storagebits    = 32,
46                 },
47                 /* _RAW is here for backward ABI compatibility */
48                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
49                                           BIT(IIO_CHAN_INFO_PROCESSED),
50         },
51         IIO_CHAN_SOFT_TIMESTAMP(1),
52 };
53
54 /*
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.
58  */
59 #define ACPI_ALS_EVT_BUFFER_SIZE                \
60         (sizeof(s32) + sizeof(s32) + sizeof(s64))
61
62 struct acpi_als {
63         struct acpi_device      *device;
64         struct mutex            lock;
65         struct iio_trigger      *trig;
66
67         s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)]  __aligned(8);
68 };
69
70 /*
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
73  * special.
74  *
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.).
78  *
79  * So far, we support only ALI (illuminance).
80  */
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"
86
87 static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
88 {
89         unsigned long long temp_val;
90         acpi_status status;
91
92         status = acpi_evaluate_integer(als->device->handle, prop, NULL,
93                                        &temp_val);
94
95         if (ACPI_FAILURE(status)) {
96                 ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
97                 return -EIO;
98         }
99
100         *val = temp_val;
101
102         return 0;
103 }
104
105 static void acpi_als_notify(struct acpi_device *device, u32 event)
106 {
107         struct iio_dev *indio_dev = acpi_driver_data(device);
108         struct acpi_als *als = iio_priv(indio_dev);
109
110         if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) {
111                 switch (event) {
112                 case ACPI_ALS_NOTIFY_ILLUMINANCE:
113                         iio_trigger_poll_chained(als->trig);
114                         break;
115                 default:
116                         /* Unhandled event */
117                         dev_dbg(&device->dev,
118                                 "Unhandled ACPI ALS event (%08x)!\n",
119                                 event);
120                 }
121         }
122 }
123
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)
127 {
128         struct acpi_als *als = iio_priv(indio_dev);
129         s32 temp_val;
130         int ret;
131
132         if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
133                 return -EINVAL;
134
135         /* we support only illumination (_ALI) so far. */
136         if (chan->type != IIO_LIGHT)
137                 return -EINVAL;
138
139         ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
140         if (ret < 0)
141                 return ret;
142
143         *val = temp_val;
144
145         return IIO_VAL_INT;
146 }
147
148 static const struct iio_info acpi_als_info = {
149         .read_raw               = acpi_als_read_raw,
150 };
151
152 static irqreturn_t acpi_als_trigger_handler(int irq, void *p)
153 {
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;
158         s32 val;
159         int ret;
160
161         mutex_lock(&als->lock);
162
163         ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
164         if (ret < 0)
165                 goto out;
166         *buffer = val;
167
168         /*
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
172          * notify function.
173          *
174          * If the timestamp was actually 0, the timestamp is set one more time.
175          */
176         if (!pf->timestamp)
177                 pf->timestamp = iio_get_time_ns(indio_dev);
178
179         iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
180 out:
181         mutex_unlock(&als->lock);
182         iio_trigger_notify_done(indio_dev->trig);
183
184         return IRQ_HANDLED;
185 }
186
187 static int acpi_als_add(struct acpi_device *device)
188 {
189         struct device *dev = &device->dev;
190         struct iio_dev *indio_dev;
191         struct acpi_als *als;
192         int ret;
193
194         indio_dev = devm_iio_device_alloc(dev, sizeof(*als));
195         if (!indio_dev)
196                 return -ENOMEM;
197
198         als = iio_priv(indio_dev);
199
200         device->driver_data = indio_dev;
201         als->device = device;
202         mutex_init(&als->lock);
203
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);
208
209         als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name, indio_dev->id);
210         if (!als->trig)
211                 return -ENOMEM;
212
213         ret = devm_iio_trigger_register(dev, als->trig);
214         if (ret)
215                 return ret;
216         /*
217          * Set hardware trigger by default to let events flow when
218          * BIOS support notification.
219          */
220         indio_dev->trig = iio_trigger_get(als->trig);
221
222         ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
223                                               iio_pollfunc_store_time,
224                                               acpi_als_trigger_handler,
225                                               NULL);
226         if (ret)
227                 return ret;
228
229         return devm_iio_device_register(dev, indio_dev);
230 }
231
232 static const struct acpi_device_id acpi_als_device_ids[] = {
233         {"ACPI0008", 0},
234         {},
235 };
236
237 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
238
239 static struct acpi_driver acpi_als_driver = {
240         .name   = "acpi_als",
241         .class  = ACPI_ALS_CLASS,
242         .ids    = acpi_als_device_ids,
243         .ops = {
244                 .add    = acpi_als_add,
245                 .notify = acpi_als_notify,
246         },
247 };
248
249 module_acpi_driver(acpi_als_driver);
250
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");