From patchwork Wed Mar 17 07:40:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12145041 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=-19.0 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 05F32C43381 for ; Wed, 17 Mar 2021 07:41:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C30F064FAB for ; Wed, 17 Mar 2021 07:41:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230021AbhCQHk3 (ORCPT ); Wed, 17 Mar 2021 03:40:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230036AbhCQHkT (ORCPT ); Wed, 17 Mar 2021 03:40:19 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1533C06174A for ; Wed, 17 Mar 2021 00:40:19 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id t18so629285pjs.3 for ; Wed, 17 Mar 2021 00:40:19 -0700 (PDT) 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=jRlbJMWwDuUDN4es0UT89P59wZTiBTlsknrAonJ49yw=; b=TIM4v4ipqsuq/UFj9hvQ45/C+kZgjazEiF9M6ubwAR7L+IhNDCVo6BIE2MZvahz/et dGEd8TBi1Vq3oxsw2o5jk0g2Y4UiWY6dOEUe/bALNFB+u11aQGkdzpKREPNSfA6ZMC7e thasKerk4lOD6uEtND7iFbS0wvUliqCgCoYjg= 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=jRlbJMWwDuUDN4es0UT89P59wZTiBTlsknrAonJ49yw=; b=Ezrjhd6/WjdMpIRfRBrLElMvjvJMMPQ6bq/cXDxdURlXxndzl58bpzZduT+PZbb+3K JKlPtUlQUSEL+wp+DVm3SnCeP6/eSViAgR7jGE+Xf4jSCIHpxRK+b2sADOdcLTmntqdC k4aSlAzdFD+aH049HrbuurnYENeEDeisqI+dTHNT/BH+OfkPMSAEgnGgB64AS3/L4Vwu VAJkrkM938npcA5PFWmGPJN7lm1X3Kwm+4pAeZu+vkQRvP9Nl7BccU9xnBgd3wY8A0Sf C6d/qrfpbGJmC8lAiU/qwvBz91vfUy2qj+bbMD+NTGJWRPo54FTUiAkCC0R3dMTSz4kw p/ew== X-Gm-Message-State: AOAM531BA4i8GCRjrzdVmwAzBbErltmoP8TCkNR1ptvn9V3U6lFzkwxJ z8cwro3qc+4HxIcTpAX1Af6Q9g== X-Google-Smtp-Source: ABdhPJwWBeL9HeOBhPJm+25EkMR6PwTCWkApMExspAKAi0TGiMtpH+dtlOE+HZanlt6dmcTG3eLI8g== X-Received: by 2002:a17:90a:5d09:: with SMTP id s9mr3117651pji.172.1615966819358; Wed, 17 Mar 2021 00:40:19 -0700 (PDT) Received: from localhost ([2620:15c:202:201:bdd3:3391:be74:f7be]) by smtp.gmail.com with UTF8SMTPSA id n24sm18870637pgl.27.2021.03.17.00.40.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Mar 2021 00:40:18 -0700 (PDT) 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 v7 1/3] iio: acpi_als: Add timestamp channel Date: Wed, 17 Mar 2021 00:40:10 -0700 Message-Id: <20210317074012.2336454-2-gwendal@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210317074012.2336454-1-gwendal@chromium.org> References: <20210317074012.2336454-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 --- No changes in v7 No changes in v6 Changes in v5: Fill comment line. Changes in v4: Fix spelling. 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 f8e547fd35e7a..784868aa8fbf7 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 Mar 17 07:40:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12145045 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=-19.0 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 7A85EC43333 for ; Wed, 17 Mar 2021 07:41:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5FF9964F8B for ; Wed, 17 Mar 2021 07:41:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230036AbhCQHka (ORCPT ); Wed, 17 Mar 2021 03:40:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230037AbhCQHkW (ORCPT ); Wed, 17 Mar 2021 03:40:22 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8249C06174A for ; Wed, 17 Mar 2021 00:40:21 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id j25so525868pfe.2 for ; Wed, 17 Mar 2021 00:40:21 -0700 (PDT) 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=9XltDF7+T4WZ/nMImUrKTMbpGKBG0fZHmhETi35m2h8=; b=dHf2BUOZ1i11kp1dxwSkwKv6uRI4GMK1fhMwD87J17rsZ3KNySssqWBlidD+J6Rlln U724Wgi234KZE4TD7/YK6NSOxLHv9Cjsz/MhevpoIIJL/Vn4fO9JGqQ1Bxr/35vLD7aq Ik0BV8HYcstUHi4PW/c4ruAlCmVI6Q1a7oLTY= 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=9XltDF7+T4WZ/nMImUrKTMbpGKBG0fZHmhETi35m2h8=; b=QRER2yVM5P9KJhkNKue9s0aGKafYHqO0HlsVbPPgYpCtKmdtO1zYvCxmC90Wr5IJFG oYFgSWXHiZFllA5m0vRNctVWnoJyUCsaueQFn9UNzBMGpqfcLRwB0gxspwUbBjE65LW5 6JZH67noEAlfvQjOTxvLYrWCnT/IFv3w//VOFJFn2T+G45YsPtnrp9xoRz6LAmeGoHC4 uRZ9W3bIZxWA7UOleQwnmiAJvwXviMhTWR77f9MUz/wz/JsnSo7vG6YEElKPZ5KvRt2O 8GLb6CNbSl46hRpD5zuFvUkK36xb2p35GF++BzF2Edn0Ls+Wh7zJnNsr2BdHiG/B9TeC t4pQ== X-Gm-Message-State: AOAM532RXsDLKROQgiXxXoTX24OsbEjrsK/mXKrsS8BuXg3k9jmO/WQd +vu8EO0A666e3eipYVAr88dyiA== X-Google-Smtp-Source: ABdhPJyfkzsBaCb+diIxKx/TyWgpPENckQPVuwatoLQPzcuFVzkB94Zvhqn0fAanhrBXd1dzJUGVpA== X-Received: by 2002:a63:a50f:: with SMTP id n15mr1532139pgf.348.1615966821566; Wed, 17 Mar 2021 00:40:21 -0700 (PDT) Received: from localhost ([2620:15c:202:201:bdd3:3391:be74:f7be]) by smtp.gmail.com with UTF8SMTPSA id e21sm16808458pgv.74.2021.03.17.00.40.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Mar 2021 00:40:20 -0700 (PDT) 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 v7 2/3] iio: acpi_als: Add local variable dev in probe Date: Wed, 17 Mar 2021 00:40:11 -0700 Message-Id: <20210317074012.2336454-3-gwendal@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210317074012.2336454-1-gwendal@chromium.org> References: <20210317074012.2336454-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. Signed-off-by: Gwendal Grignou Reviewed-by: Andy Shevchenko --- Changes in v7: Rebased, replace &device->dev at new locations. No changes in v6. Changes in v5: Follow inverted xmas tree declaration convention. Added in v4. 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 784868aa8fbf7..a2107945cff93 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 acpi_als *als; int ret; - indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als)); + indio_dev = devm_iio_device_alloc(dev, sizeof(*als)); if (!indio_dev) return -ENOMEM; @@ -180,12 +181,12 @@ static int acpi_als_add(struct acpi_device *device) indio_dev->channels = acpi_als_channels; indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels); - ret = devm_iio_kfifo_buffer_setup(&device->dev, indio_dev, + ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, INDIO_BUFFER_SOFTWARE, NULL); if (ret) return ret; - 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 Mar 17 07:40:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12145043 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=-19.0 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 70866C43332 for ; Wed, 17 Mar 2021 07:41:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4B88764FA8 for ; Wed, 17 Mar 2021 07:41:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230129AbhCQHka (ORCPT ); Wed, 17 Mar 2021 03:40:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230041AbhCQHkX (ORCPT ); Wed, 17 Mar 2021 03:40:23 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5809C06174A for ; Wed, 17 Mar 2021 00:40:23 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id 11so526114pfn.9 for ; Wed, 17 Mar 2021 00:40:23 -0700 (PDT) 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=6DTuOrWEqvYr+bsiUwGD57v7ZjK35N0cW4IiI0jP3JI=; b=DZ7RN+cOQM5nK5U7VC2n+ADuIH7uL4B/m1a5e39DqE6tI6V/WtJVFUKtmuHBGtqRuA DdchV7qLSWsplHCkzp3MChSyA87Qh6fYGi9JNFE06N/qTQWg8PsM8SiusEsyZPqgz/Hp ECZ5vwLnS1z6dGOeykZbOV5+3zxjbPqnwsf3g= 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=6DTuOrWEqvYr+bsiUwGD57v7ZjK35N0cW4IiI0jP3JI=; b=pCYXUaUbTEjDk+ryifwI/kDKP/6nOa5HBgPBC8xKVmTj+jDJCzRyZik8ZYgbOq+Jkz hrbPV96XjJ+ZOCGEJcDjPeZwowCXrC38kICpV6HoIgb5Yb1I8YnZjqRDtie6A/wjff2j ydR2SwzSUbKzBpn5JRCEFT+mkKedPn7a9pCP1hr2YnWwTyXtpHtUfKQEMKwv5bavdXAw Mb9U2TMLsAU+QY2giTN9sxv5PQhUBY+YDnf5gP+WvDWLKejKIqRdItlul+LlD7agrujS ycX0DcDUP9lQPCHJPHQyI8tyYOMqd8F0eJe7uIy9JjUKUkLGO2eK7NbY8USLBNRoLlfK FE0w== X-Gm-Message-State: AOAM532wUNnoKA9vEXo4kd77IKXiG+URXhxbPOgujXFpQVDdnTTXCF/B XZQhKq8Z9908zcLEVrf9einLSQ== X-Google-Smtp-Source: ABdhPJyX3v53E3sGR+NqYk8VdcZ+9tMqaCcf1U1UYcqEVLFkZ0AH6wfZMSgjugIGqnkDuXaYBkZNhA== X-Received: by 2002:aa7:8ad2:0:b029:1ee:16f0:7c70 with SMTP id b18-20020aa78ad20000b02901ee16f07c70mr3005656pfd.58.1615966823285; Wed, 17 Mar 2021 00:40:23 -0700 (PDT) Received: from localhost ([2620:15c:202:201:bdd3:3391:be74:f7be]) by smtp.gmail.com with UTF8SMTPSA id q34sm17949527pgl.92.2021.03.17.00.40.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Mar 2021 00:40:22 -0700 (PDT) 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 v7 3/3] iio: acpi_als: Add trigger support Date: Wed, 17 Mar 2021 00:40:12 -0700 Message-Id: <20210317074012.2336454-4-gwendal@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210317074012.2336454-1-gwendal@chromium.org> References: <20210317074012.2336454-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 v7: Rebased : no need to set indio_dev->modes directly anymore. Changes in v6: Inverted logic in acpi_als_notify() Put timestamp comments in the same paragraph. Changes in v5: Improve commit message readability, add note about backward compatibility. Remove unneeded include file. Changes in v4: Added comments when pf->timestamp is 0 and valid. Fix spelling. drivers/iio/light/acpi-als.c | 96 +++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 28 deletions(-) diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c index a2107945cff93..30393f08e0825 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,19 @@ 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); - - switch (event) { - case ACPI_ALS_NOTIFY_ILLUMINANCE: - ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val); - if (ret < 0) - goto out; - *buffer++ = val; - break; - default: - /* Unhandled event */ - dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n", - event); - goto out; + if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) { + switch (event) { + case ACPI_ALS_NOTIFY_ILLUMINANCE: + iio_trigger_poll_chained(als->trig); + break; + default: + /* Unhandled event */ + dev_dbg(&device->dev, + "Unhandled ACPI ALS event (%08x)!\n", + event); + } } - - 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,6 +149,41 @@ 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; @@ -181,8 +206,23 @@ static int acpi_als_add(struct acpi_device *device) indio_dev->channels = acpi_als_channels; indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels); - ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, - INDIO_BUFFER_SOFTWARE, NULL); + als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name, indio_dev->id); + if (!als->trig) + return -ENOMEM; + + 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;