From patchwork Wed Dec 16 21:41:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11978621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BD53C4361B for ; Wed, 16 Dec 2020 21:42:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CCE3F23602 for ; Wed, 16 Dec 2020 21:42:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726636AbgLPVmA (ORCPT ); Wed, 16 Dec 2020 16:42:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725601AbgLPVmA (ORCPT ); Wed, 16 Dec 2020 16:42:00 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1249FC06179C for ; Wed, 16 Dec 2020 13:41:20 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id f14so2232604pju.4 for ; Wed, 16 Dec 2020 13:41:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/1cSEzuqKCGj+bWrSPcCXCtJInhUSBBWBc/FVwh5wvU=; b=NRIQ/mWL4FGjDaJEZ+poLkSq7E3nvro8ya5vv5lqlkMYxqvcKdDLn3zyQNBuzhO945 LMk5Upw4+r0bAYpe/uFxrfCbjzZSn6akZXy2JFjT9CNynWQeJfTqyRxpX2bJKBLRLXJw 65QEWo2/v80ENdPapMrLI9agIuvbCAzx0phbA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/1cSEzuqKCGj+bWrSPcCXCtJInhUSBBWBc/FVwh5wvU=; b=rLlwPPC9d2Pg0aNma8ieBSdAIKKo1FvufTiTtV+RIIx8jOhcogP48KeimdNddWeEZ/ S7YGSw6qe1m8SLzyalVxpY+Nks7s67YAgLJH/getcJXU8VOE3/Xn8LHtyspfNAINnStI rbz0gEpLGZCl+MSAlZLdZQdNNdDvECetZTYAvpPuwAfVeqMh4tHhOWhOlCaDetIut+kP e41NlEUZEPCbUi9qdYausYL7RuMc9lIMX0X3ktcF5YpSXKeECYKuqaCKpgM32cuVTwXt nZWUb0W8Bdz1VM39lv/nDuk0BtYxwHKobxfykhYdSnJ07m45b5MJ+/dGs7ZLYlc1lohD ojdQ== X-Gm-Message-State: AOAM530v3RQMHnGKhJZleYUjyoKTnWOccmywnmzt09oWcy2q5hv1BjiQ Kdg1Tuf1/ua1axcbtLAxrn8m8O3dVVu40Q== X-Google-Smtp-Source: ABdhPJwLTxUpQagmWzFx2lMwLBwGXaAdXxB2A3P9lkDJcMUWOAF/F9/vfPlAO69GRWmlsfVMQCSing== X-Received: by 2002:a17:90b:1886:: with SMTP id mn6mr4781104pjb.31.1608154879642; Wed, 16 Dec 2020 13:41:19 -0800 (PST) Received: from localhost ([2620:15c:202:201:7220:84ff:fe09:94fe]) by smtp.gmail.com with ESMTPSA id c199sm3910931pfb.108.2020.12.16.13.41.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Dec 2020 13:41:18 -0800 (PST) From: Gwendal Grignou To: gabriele.mzt@gmail.com, jic23@kernel.org, lars@metafoo.de, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH v5 1/3] iio: acpi_als: Add timestamp channel Date: Wed, 16 Dec 2020 13:41:05 -0800 Message-Id: <20201216214107.774969-2-gwendal@chromium.org> X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog In-Reply-To: <20201216214107.774969-1-gwendal@chromium.org> References: <20201216214107.774969-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add timestamp channel in list of channel, to allow retrieving timestamps when events are produced. Signed-off-by: Gwendal Grignou Reviewed-by: Andy Shevchenko --- Changes in v4: Fix spelling. Changes in v5: Fill comment line. drivers/iio/light/acpi-als.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c index 2be7180e2cbfa..6dbaeeebe2041 100644 --- a/drivers/iio/light/acpi-als.c +++ b/drivers/iio/light/acpi-als.c @@ -45,24 +45,22 @@ static const struct iio_chan_spec acpi_als_channels[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED), }, + IIO_CHAN_SOFT_TIMESTAMP(1), }; /* * The event buffer contains timestamp and all the data from * the ACPI0008 block. There are multiple, but so far we only - * support _ALI (illuminance). Once someone adds new channels - * to acpi_als_channels[], the evt_buffer below will grow - * automatically. + * support _ALI (illuminance): One channel, padding and timestamp. */ -#define ACPI_ALS_EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels) #define ACPI_ALS_EVT_BUFFER_SIZE \ - (sizeof(s64) + (ACPI_ALS_EVT_NR_SOURCES * sizeof(s32))) + (sizeof(s32) + sizeof(s32) + sizeof(s64)) struct acpi_als { struct acpi_device *device; struct mutex lock; - s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE]; + s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8); }; /* From patchwork Wed Dec 16 21:41:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11978623 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C021C4361B for ; Wed, 16 Dec 2020 21:42:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1617123603 for ; Wed, 16 Dec 2020 21:42:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726638AbgLPVmC (ORCPT ); Wed, 16 Dec 2020 16:42:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725601AbgLPVmC (ORCPT ); Wed, 16 Dec 2020 16:42:02 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57575C0617A6 for ; Wed, 16 Dec 2020 13:41:22 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id lb18so2223876pjb.5 for ; Wed, 16 Dec 2020 13:41:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6sw+x1vGelhN3zr0/rCrAVkAh2yb7e09rAh3MAQCnjQ=; b=KZTmdL4tFJ0PeShevhbBOpNX60/Zp2m/XClZ6slBFYE/a8qTRMxNde7/3+m0cs1INK P+4nKRXo2FXyYFlE1tLTpkY1MKy/AxsG9nlnn308WqJRbQrQ8MtMIJk6ruhWeDGel5Nl shAPCfXEjIUuKfTdUTzVW1WdCvwNL3IMvNHdM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6sw+x1vGelhN3zr0/rCrAVkAh2yb7e09rAh3MAQCnjQ=; b=ntPq+Xl55KtH+m55LechVjUw1YRCohln01rxQ+/tJawbsyBb/CUxZZqY9VJ2BIHQoN oWV3wWwY67r6KUUvz7DAp8X55GYjWiQ1QxdBTzfTSKjvcH9rbcVl2F2stDw8T0wQwWiK Nxeb/REvou3zYYSqfUMww0usGE7Ecl9sOFOM5OcC5zAKwaXShobXm4yJ6LB2xIu30H9t 7aSLLIIxhIsg565Sc1dcjaJqd34toVNCrwc1qVUQ4s1uRskhHoLRdRrMS47gMKpEjwUB j/dZg9n0Fla56/IPvkXi9cUUL99POWVSTEWi0bjwawAEoVMQxnxaLzXQvDxQaAsvLMHs KH2Q== X-Gm-Message-State: AOAM533nWiAbqqm8Ji8pQP/KfwyNZXeH2mJ2/m+XGUbYaae41irAMTih IRPTvL4cyz0y4BzZ7EVT/MRHkg== X-Google-Smtp-Source: ABdhPJytWYHdt9fITPm9aeH6R+ZnG3PeuPUAsTo5BPVm8iu4f2vKtQAbOjhD+Trgw/gQ2Sa00a/zgQ== X-Received: by 2002:a17:902:82c7:b029:da:cb88:38f8 with SMTP id u7-20020a17090282c7b02900dacb8838f8mr26130799plz.49.1608154881922; Wed, 16 Dec 2020 13:41:21 -0800 (PST) Received: from localhost ([2620:15c:202:201:7220:84ff:fe09:94fe]) by smtp.gmail.com with ESMTPSA id w24sm3441478pfn.100.2020.12.16.13.41.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Dec 2020 13:41:20 -0800 (PST) From: Gwendal Grignou To: gabriele.mzt@gmail.com, jic23@kernel.org, lars@metafoo.de, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH v5 2/3] iio: acpi_als: Add local variable dev in probe Date: Wed, 16 Dec 2020 13:41:06 -0800 Message-Id: <20201216214107.774969-3-gwendal@chromium.org> X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog In-Reply-To: <20201216214107.774969-1-gwendal@chromium.org> References: <20201216214107.774969-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Use dev = &device->dev in probe routine for clarity. Remove setting parent of iio device, already done in iio_device_alloc(). Signed-off-by: Gwendal Grignou Reviewed-by: Andy Shevchenko --- Added in v4. Changes in v5: Follow inverted xmas tree declaration convention. drivers/iio/light/acpi-als.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c index 6dbaeeebe2041..fd20808d4a119 100644 --- a/drivers/iio/light/acpi-als.c +++ b/drivers/iio/light/acpi-als.c @@ -161,11 +161,12 @@ static const struct iio_info acpi_als_info = { static int acpi_als_add(struct acpi_device *device) { - struct acpi_als *als; + struct device *dev = &device->dev; struct iio_dev *indio_dev; struct iio_buffer *buffer; + struct acpi_als *als; - indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als)); + indio_dev = devm_iio_device_alloc(dev, sizeof(*als)); if (!indio_dev) return -ENOMEM; @@ -181,13 +182,13 @@ static int acpi_als_add(struct acpi_device *device) indio_dev->channels = acpi_als_channels; indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels); - buffer = devm_iio_kfifo_allocate(&device->dev); + buffer = devm_iio_kfifo_allocate(dev); if (!buffer) return -ENOMEM; iio_device_attach_buffer(indio_dev, buffer); - return devm_iio_device_register(&device->dev, indio_dev); + return devm_iio_device_register(dev, indio_dev); } static const struct acpi_device_id acpi_als_device_ids[] = { From patchwork Wed Dec 16 21:41:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11978625 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31630C0018C for ; Wed, 16 Dec 2020 21:42:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 06E06235E4 for ; Wed, 16 Dec 2020 21:42:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726672AbgLPVmF (ORCPT ); Wed, 16 Dec 2020 16:42:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725601AbgLPVmE (ORCPT ); Wed, 16 Dec 2020 16:42:04 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90FD6C0617B0 for ; Wed, 16 Dec 2020 13:41:24 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id q4so7983506plr.7 for ; Wed, 16 Dec 2020 13:41:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9EjDNwU05bgJKT6WsaqEUOCspqDMMYWE67z/jFF2GQE=; b=Qsjqw46y05FO0LmUuJ+u4Od90NjL0Z4azAQtM1qhOmGIz25wbh4acgTBTugi6Y/QM/ FAYAAtg0/J6qfIuwlaiZJApkhiT14roxtDza/QW0SmG8c8UQnCVVA66EZijeq3jWNASq fNozCjdMhQFpCISLzvuR87AXg767nchB57ATU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9EjDNwU05bgJKT6WsaqEUOCspqDMMYWE67z/jFF2GQE=; b=TyreHf+fASg0UXLPbyvApoxVxSGhrKvGnTL3Y5G+RDfymYTjTPC1/qqw1njyc2JRnD ctacBOdywHIj3yedhs7aTL1r7NBlEIbMMssUQRULA4TC48cQrkCgkhtff9K0Bvi9eC9h X+WEochf7zChuApqLipM1hENT8sBgXf+4LfHtkIPb8W3EK2UkIFU8oCOB5i+3eFZVzTK EmtNcqZO73yljuR58EhqWtnT0I7AbrfmOWK8rIjGXQQ8exq57VyN87ByuPwmE8KLrY8K DGYjX99IqGLPU1dxsAGWuC2g6RFVT5em5ipWN9D57QmyDnB4lb9aj0NCMpqCy8DYqPJV 5ZFQ== X-Gm-Message-State: AOAM531sjOlyb/HYNqZ13YlAwonmsrC8K0R36rDWC3Pm0XYwUwRLb8eK V2m4HBddWuGNK7pmOdCl2BP5HA== X-Google-Smtp-Source: ABdhPJwLZM1N5mG/U2OpZRZTv9zPpr2F/wRZ3151naSM6gNyatphqM96wguN+sedz1mYI716h2Q92A== X-Received: by 2002:a17:90a:d145:: with SMTP id t5mr4840480pjw.104.1608154884120; Wed, 16 Dec 2020 13:41:24 -0800 (PST) Received: from localhost ([2620:15c:202:201:7220:84ff:fe09:94fe]) by smtp.gmail.com with ESMTPSA id u24sm2880742pjx.56.2020.12.16.13.41.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Dec 2020 13:41:23 -0800 (PST) From: Gwendal Grignou To: gabriele.mzt@gmail.com, jic23@kernel.org, lars@metafoo.de, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH v5 3/3] iio: acpi_als: Add trigger support Date: Wed, 16 Dec 2020 13:41:07 -0800 Message-Id: <20201216214107.774969-4-gwendal@chromium.org> X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog In-Reply-To: <20201216214107.774969-1-gwendal@chromium.org> References: <20201216214107.774969-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org As some firmware does not notify on illuminance changes, add a trigger to be able to query light via software (sysfs-trigger or hrtrigger). Add a hardware trigger set as the default trigger to maintain backward compatibility. Check iio_info reports the sensor as buffer capable: iio:device0: acpi-als (buffer capable) To test, check we can get data on demand on an Intel based chromebook: IIO_DEV="iio:device0" echo 1 > iio_sysfs_trigger/add_trigger cat trigger2/name > ${IIO_DEV}/trigger/current_trigger for i in ${IIO_DEV}/scan_elements/*_en ${IIO_DEV}/buffer/enable ; do echo 1 > $i done od -x /dev/${IIO_DEV} & echo 1 > trigger2/trigger_now Signed-off-by: Gwendal Grignou Reviewed-by: Andy Shevchenko --- Changes in v4: Added comments when pf->timestamp is 0 and valid. Fix spelling. Changes in v5: Improve commit message readability, add note about backward compatibility. Remove unneeded include file. drivers/iio/light/acpi-als.c | 89 ++++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c index fd20808d4a119..80fe0da51fad3 100644 --- a/drivers/iio/light/acpi-als.c +++ b/drivers/iio/light/acpi-als.c @@ -16,11 +16,14 @@ #include #include #include +#include #include #include #include -#include +#include +#include +#include #define ACPI_ALS_CLASS "als" #define ACPI_ALS_DEVICE_NAME "acpi-als" @@ -59,6 +62,7 @@ static const struct iio_chan_spec acpi_als_channels[] = { struct acpi_als { struct acpi_device *device; struct mutex lock; + struct iio_trigger *trig; s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8); }; @@ -102,33 +106,20 @@ static void acpi_als_notify(struct acpi_device *device, u32 event) { struct iio_dev *indio_dev = acpi_driver_data(device); struct acpi_als *als = iio_priv(indio_dev); - s32 *buffer = als->evt_buffer; - s64 time_ns = iio_get_time_ns(indio_dev); - s32 val; - int ret; - mutex_lock(&als->lock); - - memset(buffer, 0, ACPI_ALS_EVT_BUFFER_SIZE); + if (!iio_buffer_enabled(indio_dev) || + !iio_trigger_using_own(indio_dev)) + return; switch (event) { case ACPI_ALS_NOTIFY_ILLUMINANCE: - ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val); - if (ret < 0) - goto out; - *buffer++ = val; + iio_trigger_poll_chained(als->trig); break; default: /* Unhandled event */ dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n", event); - goto out; } - - iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns); - -out: - mutex_unlock(&als->lock); } static int acpi_als_read_raw(struct iio_dev *indio_dev, @@ -159,12 +150,47 @@ static const struct iio_info acpi_als_info = { .read_raw = acpi_als_read_raw, }; +static irqreturn_t acpi_als_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct acpi_als *als = iio_priv(indio_dev); + s32 *buffer = als->evt_buffer; + s32 val; + int ret; + + mutex_lock(&als->lock); + + ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val); + if (ret < 0) + goto out; + *buffer = val; + + /* + * When coming from own trigger via polls, set polling function timestamp + * here. + * Given ACPI notifier is already in a thread and call function directly, + * there is no need to set the timestamp in the notify function. + * + * If the timestamp was actually 0, the timestamp is set one more time. + */ + if (!pf->timestamp) + pf->timestamp = iio_get_time_ns(indio_dev); + + iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp); +out: + mutex_unlock(&als->lock); + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static int acpi_als_add(struct acpi_device *device) { struct device *dev = &device->dev; struct iio_dev *indio_dev; - struct iio_buffer *buffer; struct acpi_als *als; + int ret; indio_dev = devm_iio_device_alloc(dev, sizeof(*als)); if (!indio_dev) @@ -178,15 +204,32 @@ static int acpi_als_add(struct acpi_device *device) indio_dev->name = ACPI_ALS_DEVICE_NAME; indio_dev->info = &acpi_als_info; - indio_dev->modes = INDIO_BUFFER_SOFTWARE; + indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->channels = acpi_als_channels; indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels); - buffer = devm_iio_kfifo_allocate(dev); - if (!buffer) + als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", + indio_dev->name, + indio_dev->id); + if (!als->trig) return -ENOMEM; - iio_device_attach_buffer(indio_dev, buffer); + iio_trigger_set_drvdata(als->trig, indio_dev); + ret = devm_iio_trigger_register(dev, als->trig); + if (ret) + return ret; + /* + * Set hardware trigger by default to let events flow when + * BIOS support notification. + */ + indio_dev->trig = iio_trigger_get(als->trig); + + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, + iio_pollfunc_store_time, + acpi_als_trigger_handler, + NULL); + if (ret) + return ret; return devm_iio_device_register(dev, indio_dev); }