From patchwork Tue Dec 29 01:52:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11991831 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.9 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 5B702C433E0 for ; Tue, 29 Dec 2020 01:54:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1704E21D94 for ; Tue, 29 Dec 2020 01:54:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726158AbgL2Bxt (ORCPT ); Mon, 28 Dec 2020 20:53:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726014AbgL2Bxt (ORCPT ); Mon, 28 Dec 2020 20:53:49 -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 2493AC061793 for ; Mon, 28 Dec 2020 17:53:09 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id lj6so682179pjb.0 for ; Mon, 28 Dec 2020 17:53:09 -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=q/SDjd3IqDveGOdOyKRmFp4u/wbZ0g210W66vmPxVOk=; b=N3AyRN3tfiCB1sC6qXbrnNWHnmxIvCiljz1mktJIZou59E5VF6MFca1dbrItx31JFX 1n9w2vKQQXdpffEphMgbBOPNDZL/GSKSKNOG2GDz0UBmsGPPE4++hjjXt9CnGtOQQTaN Jcld/dNQXLMy3aKlJnyYfq2OKLxbZMtMAPies= 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=q/SDjd3IqDveGOdOyKRmFp4u/wbZ0g210W66vmPxVOk=; b=ppUPjMuFfNjhlqilN599WmbJStOmCmGnjjnP1Hp28GTcNEbAP+Fs4eFN5NFza4NH0h o4N5TTA6v54A5ipBtinFE4E4c/c+5rsejIAWPMRcsRv1aG/3XKC7Lpx9kTt4w52aprxf cDl8h3/lbpm562BGVs6XqOk1/xojFY+5o366xGiwyLFjRuuV8/EuIZvUy0wviBtQEQ+J HU9jyh7+q9WyfyVJ4WHSVUBaRzFgmVdlU6u5NSyhzN5GSDtn5s1LYQ8ohPJH6vRMa+0U P+HysXnDLADeYppkZsIWFA2hBEoM7M59ESWk1J1x+O3/Kc62iNORK8Y1PGg8YJNZblUh twQQ== X-Gm-Message-State: AOAM530nVyEPIsDZl/9Sco1XGW1ugHtIhmRV2wXcUYb5c8nsz8hKHfzm wdKQ7/3AxxGn2B/ipkowwT91AQ== X-Google-Smtp-Source: ABdhPJxGH4vsUeUvELLO4+oEGEmDHFGzfhYC2b/5Th379WLdDbAk9pJUc21rKnZpt3qxBm9ekDr7LA== X-Received: by 2002:a17:90a:cc0a:: with SMTP id b10mr1656545pju.59.1609206788723; Mon, 28 Dec 2020 17:53:08 -0800 (PST) Received: from localhost ([2620:15c:202:201:7220:84ff:fe09:94fe]) by smtp.gmail.com with ESMTPSA id s1sm1243740pjk.1.2020.12.28.17.53.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Dec 2020 17:53:08 -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 v6 1/3] iio: acpi_als: Add timestamp channel Date: Mon, 28 Dec 2020 17:52:58 -0800 Message-Id: <20201229015300.3037816-2-gwendal@chromium.org> X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog In-Reply-To: <20201229015300.3037816-1-gwendal@chromium.org> References: <20201229015300.3037816-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 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 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 Tue Dec 29 01:52:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11991833 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.9 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 724D3C433E6 for ; Tue, 29 Dec 2020 01:54:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3683C221F8 for ; Tue, 29 Dec 2020 01:54:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726197AbgL2Bxv (ORCPT ); Mon, 28 Dec 2020 20:53:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726014AbgL2Bxv (ORCPT ); Mon, 28 Dec 2020 20:53:51 -0500 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0684AC061796 for ; Mon, 28 Dec 2020 17:53:11 -0800 (PST) Received: by mail-pg1-x52f.google.com with SMTP id n25so8387931pgb.0 for ; Mon, 28 Dec 2020 17:53:11 -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=Wvb8Xy2SHchoCsPEpm92rrDJ+Eajer+vmen2sR0Pqng=; b=gaoe0DsFbuOwGLBPwnGNMq9ByJXDU/qBfWRD7nP68s/qsJtvcXgHfyamJT1Li7dIDq yz5MF8uvyhvhaXvy0PpGKZbRVzdVHEfsd2GAqYsZd5eHCWlzpItNaTCxyBPkmzeblBhj 8YAx8tso7KEd5wRIjPgzSWO2Lo0Z+WVeQfhYY= 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=Wvb8Xy2SHchoCsPEpm92rrDJ+Eajer+vmen2sR0Pqng=; b=s+Tl/AxKrHnn5vbqID++Ipp0M9C0VflmNeiCW9VN8p8Yt1WIBc5R19tgcaGQj1+msT 46apeugCVRyC2x+ke1g8+ORnctKlslgFmTQurNhiZqddKCSIaJNh/nqxhPDXChwjTEX1 QHXhJhb5q1UDL2cbo1pUIZEy2HBw4S9RxE/fEfdkPib9Sz+ALRPPbKiUWvywaZTVQKvN tQgIfrUJPLGmCwAtSa+2s/JAmpEO3gykq5iJyJcgOfBSSeeG5XCiYUB/wX8fJpR7zau+ 3SiE4wbcKdgGt/hpxQnTy5kY4tgtRzNw0rYuE+APm2o5Z0VrhOlJKFIS8rGL+TjUMXc3 Ag2Q== X-Gm-Message-State: AOAM530uGk0/4PRzqUKKZXK0vdEjTo3F1T4CzttErFpqKW+kPb5NFk9D H87QbyZPV2IOEh16lEFEcxddsEw7F9TiRQ== X-Google-Smtp-Source: ABdhPJwaxsRkql5+GoA8+6UJX2goUSZO5TJ7zMNK1T6o2li+8hCsXUvtmp7pwE1LWbg44XSxCWDAog== X-Received: by 2002:a05:6a00:134d:b029:1ad:b17:dbc4 with SMTP id k13-20020a056a00134db02901ad0b17dbc4mr36064470pfu.53.1609206790507; Mon, 28 Dec 2020 17:53:10 -0800 (PST) Received: from localhost ([2620:15c:202:201:7220:84ff:fe09:94fe]) by smtp.gmail.com with ESMTPSA id fv22sm749066pjb.14.2020.12.28.17.53.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Dec 2020 17:53:10 -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 v6 2/3] iio: acpi_als: Add local variable dev in probe Date: Mon, 28 Dec 2020 17:52:59 -0800 Message-Id: <20201229015300.3037816-3-gwendal@chromium.org> X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog In-Reply-To: <20201229015300.3037816-1-gwendal@chromium.org> References: <20201229015300.3037816-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 --- 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 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 Tue Dec 29 01:53:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11991835 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.9 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 80B6CC43381 for ; Tue, 29 Dec 2020 01:54:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E938207DE for ; Tue, 29 Dec 2020 01:54:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726232AbgL2Bxx (ORCPT ); Mon, 28 Dec 2020 20:53:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726014AbgL2Bxx (ORCPT ); Mon, 28 Dec 2020 20:53:53 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1888C061798 for ; Mon, 28 Dec 2020 17:53:12 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id 4so6501764plk.5 for ; Mon, 28 Dec 2020 17:53:12 -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=6a/4PXNW4RoHDdxMI3IizbFDKROwq7pJJM4nIFWHSMU=; b=N+75h+iBPh++VQYNSfVw6cIlpW14acdbXpOJUDSbdCUlH5U7ShR/hjB9zV/llYCCy7 oeBy8EMbQUVA3AsuRItie5b61zodPtnBhVVfP23r5n1Ikbje8ux9StdjtfchzE9P1fJT Xik0kukG47RoiKsPClICt+oKzpPa2/MvLXGVo= 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=6a/4PXNW4RoHDdxMI3IizbFDKROwq7pJJM4nIFWHSMU=; b=cjUqDtNrWll+NYxAsoro0hQm/Yi1D+ryZpVVEki70+ptZkrSc9uyn3Y6KTu3VXYq/H XFMhParJbUPsvUQb6guzGtp0SNEj3/TAe9KyRRiiBtxuYPr6XLcifsFA6lms8d54fIYO m6DsgO8a2zv0SMrrYh8l0qiG0P5/vXHZLSlHk19PP1IO7xG5+qjiqXL9bEzckUCckGtC QJd6jIxVi6wWfcEu2G1TeeXKPhES9Szpc74cm+RU3MOInJDYAPHMWWk9qpbgKcGh1Jfh 9AJ4+znz2Nr59vi6qN38FypV8Y/KGefVQTlWoaHoYmzx6kQz5VZX67truqnrGJPg6Jn4 QwVQ== X-Gm-Message-State: AOAM531R5LBh+oir1tIRe3WgLJk+3+wlMIXcAdygfVAZGS241yFdivPD m/EzA53edmdAVAk1L1MRkFC9OA== X-Google-Smtp-Source: ABdhPJw4fahdFvbzubnwOw7tSnQ+0cbwKXhcfhY3UiUFaRD2GiE5mBgsW//ixlE1h5pmB5QZBpV/9g== X-Received: by 2002:a17:90a:e38c:: with SMTP id b12mr1663536pjz.177.1609206792445; Mon, 28 Dec 2020 17:53:12 -0800 (PST) Received: from localhost ([2620:15c:202:201:7220:84ff:fe09:94fe]) by smtp.gmail.com with ESMTPSA id i6sm30149273pgc.58.2020.12.28.17.53.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Dec 2020 17:53:11 -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 v6 3/3] iio: acpi_als: Add trigger support Date: Mon, 28 Dec 2020 17:53:00 -0800 Message-Id: <20201229015300.3037816-4-gwendal@chromium.org> X-Mailer: git-send-email 2.29.2.729.g45daf8777d-goog In-Reply-To: <20201229015300.3037816-1-gwendal@chromium.org> References: <20201229015300.3037816-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 --- 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 | 104 ++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 31 deletions(-) diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c index fd20808d4a119..183e8604b82bf 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,12 +149,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 +203,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); }