Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ACPI Ambient Light Sensor Driver
0004  *
0005  * Based on ALS driver:
0006  * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com>
0007  *
0008  * Rework for IIO subsystem:
0009  * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com>
0010  *
0011  * Final cleanup and debugging:
0012  * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de>
0013  * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com>
0014  */
0015 
0016 #include <linux/module.h>
0017 #include <linux/acpi.h>
0018 #include <linux/err.h>
0019 #include <linux/irq.h>
0020 #include <linux/mutex.h>
0021 
0022 #include <linux/iio/iio.h>
0023 #include <linux/iio/buffer.h>
0024 #include <linux/iio/trigger.h>
0025 #include <linux/iio/triggered_buffer.h>
0026 #include <linux/iio/trigger_consumer.h>
0027 
0028 #define ACPI_ALS_CLASS          "als"
0029 #define ACPI_ALS_DEVICE_NAME        "acpi-als"
0030 #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80
0031 
0032 /*
0033  * So far, there's only one channel in here, but the specification for
0034  * ACPI0008 says there can be more to what the block can report. Like
0035  * chromaticity and such. We are ready for incoming additions!
0036  */
0037 static const struct iio_chan_spec acpi_als_channels[] = {
0038     {
0039         .type       = IIO_LIGHT,
0040         .scan_type  = {
0041             .sign       = 's',
0042             .realbits   = 32,
0043             .storagebits    = 32,
0044         },
0045         /* _RAW is here for backward ABI compatibility */
0046         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0047                       BIT(IIO_CHAN_INFO_PROCESSED),
0048     },
0049     IIO_CHAN_SOFT_TIMESTAMP(1),
0050 };
0051 
0052 /*
0053  * The event buffer contains timestamp and all the data from
0054  * the ACPI0008 block. There are multiple, but so far we only
0055  * support _ALI (illuminance): One channel, padding and timestamp.
0056  */
0057 #define ACPI_ALS_EVT_BUFFER_SIZE        \
0058     (sizeof(s32) + sizeof(s32) + sizeof(s64))
0059 
0060 struct acpi_als {
0061     struct acpi_device  *device;
0062     struct mutex        lock;
0063     struct iio_trigger  *trig;
0064 
0065     s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)]  __aligned(8);
0066 };
0067 
0068 /*
0069  * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
0070  * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
0071  * special.
0072  *
0073  * The _ALR property returns tables that can be used to fine-tune the values
0074  * reported by the other props based on the particular hardware type and it's
0075  * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
0076  *
0077  * So far, we support only ALI (illuminance).
0078  */
0079 #define ACPI_ALS_ILLUMINANCE    "_ALI"
0080 #define ACPI_ALS_CHROMATICITY   "_ALC"
0081 #define ACPI_ALS_COLOR_TEMP "_ALT"
0082 #define ACPI_ALS_POLLING    "_ALP"
0083 #define ACPI_ALS_TABLES     "_ALR"
0084 
0085 static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
0086 {
0087     unsigned long long temp_val;
0088     acpi_status status;
0089 
0090     status = acpi_evaluate_integer(als->device->handle, prop, NULL,
0091                        &temp_val);
0092 
0093     if (ACPI_FAILURE(status)) {
0094         acpi_evaluation_failure_warn(als->device->handle, prop, status);
0095         return -EIO;
0096     }
0097 
0098     *val = temp_val;
0099 
0100     return 0;
0101 }
0102 
0103 static void acpi_als_notify(struct acpi_device *device, u32 event)
0104 {
0105     struct iio_dev *indio_dev = acpi_driver_data(device);
0106     struct acpi_als *als = iio_priv(indio_dev);
0107 
0108     if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) {
0109         switch (event) {
0110         case ACPI_ALS_NOTIFY_ILLUMINANCE:
0111             iio_trigger_poll_chained(als->trig);
0112             break;
0113         default:
0114             /* Unhandled event */
0115             dev_dbg(&device->dev,
0116                 "Unhandled ACPI ALS event (%08x)!\n",
0117                 event);
0118         }
0119     }
0120 }
0121 
0122 static int acpi_als_read_raw(struct iio_dev *indio_dev,
0123                  struct iio_chan_spec const *chan, int *val,
0124                  int *val2, long mask)
0125 {
0126     struct acpi_als *als = iio_priv(indio_dev);
0127     s32 temp_val;
0128     int ret;
0129 
0130     if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
0131         return -EINVAL;
0132 
0133     /* we support only illumination (_ALI) so far. */
0134     if (chan->type != IIO_LIGHT)
0135         return -EINVAL;
0136 
0137     ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
0138     if (ret < 0)
0139         return ret;
0140 
0141     *val = temp_val;
0142 
0143     return IIO_VAL_INT;
0144 }
0145 
0146 static const struct iio_info acpi_als_info = {
0147     .read_raw       = acpi_als_read_raw,
0148 };
0149 
0150 static irqreturn_t acpi_als_trigger_handler(int irq, void *p)
0151 {
0152     struct iio_poll_func *pf = p;
0153     struct iio_dev *indio_dev = pf->indio_dev;
0154     struct acpi_als *als = iio_priv(indio_dev);
0155     s32 *buffer = als->evt_buffer;
0156     s32 val;
0157     int ret;
0158 
0159     mutex_lock(&als->lock);
0160 
0161     ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
0162     if (ret < 0)
0163         goto out;
0164     *buffer = val;
0165 
0166     /*
0167      * When coming from own trigger via polls, set polling function
0168      * timestamp here. Given ACPI notifier is already in a thread and call
0169      * function directly, there is no need to set the timestamp in the
0170      * notify function.
0171      *
0172      * If the timestamp was actually 0, the timestamp is set one more time.
0173      */
0174     if (!pf->timestamp)
0175         pf->timestamp = iio_get_time_ns(indio_dev);
0176 
0177     iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
0178 out:
0179     mutex_unlock(&als->lock);
0180     iio_trigger_notify_done(indio_dev->trig);
0181 
0182     return IRQ_HANDLED;
0183 }
0184 
0185 static int acpi_als_add(struct acpi_device *device)
0186 {
0187     struct device *dev = &device->dev;
0188     struct iio_dev *indio_dev;
0189     struct acpi_als *als;
0190     int ret;
0191 
0192     indio_dev = devm_iio_device_alloc(dev, sizeof(*als));
0193     if (!indio_dev)
0194         return -ENOMEM;
0195 
0196     als = iio_priv(indio_dev);
0197 
0198     device->driver_data = indio_dev;
0199     als->device = device;
0200     mutex_init(&als->lock);
0201 
0202     indio_dev->name = ACPI_ALS_DEVICE_NAME;
0203     indio_dev->info = &acpi_als_info;
0204     indio_dev->channels = acpi_als_channels;
0205     indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
0206 
0207     als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
0208                        iio_device_id(indio_dev));
0209     if (!als->trig)
0210         return -ENOMEM;
0211 
0212     ret = devm_iio_trigger_register(dev, als->trig);
0213     if (ret)
0214         return ret;
0215     /*
0216      * Set hardware trigger by default to let events flow when
0217      * BIOS support notification.
0218      */
0219     indio_dev->trig = iio_trigger_get(als->trig);
0220 
0221     ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
0222                           iio_pollfunc_store_time,
0223                           acpi_als_trigger_handler,
0224                           NULL);
0225     if (ret)
0226         return ret;
0227 
0228     return devm_iio_device_register(dev, indio_dev);
0229 }
0230 
0231 static const struct acpi_device_id acpi_als_device_ids[] = {
0232     {"ACPI0008", 0},
0233     {},
0234 };
0235 
0236 MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
0237 
0238 static struct acpi_driver acpi_als_driver = {
0239     .name   = "acpi_als",
0240     .class  = ACPI_ALS_CLASS,
0241     .ids    = acpi_als_device_ids,
0242     .ops = {
0243         .add    = acpi_als_add,
0244         .notify = acpi_als_notify,
0245     },
0246 };
0247 
0248 module_acpi_driver(acpi_als_driver);
0249 
0250 MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
0251 MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
0252 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
0253 MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
0254 MODULE_LICENSE("GPL");