From patchwork Mon Mar 6 09:15:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13160751 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8E06C61DA4 for ; Mon, 6 Mar 2023 09:15:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229915AbjCFJPb (ORCPT ); Mon, 6 Mar 2023 04:15:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229885AbjCFJP3 (ORCPT ); Mon, 6 Mar 2023 04:15:29 -0500 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D0801633E; Mon, 6 Mar 2023 01:15:27 -0800 (PST) Received: by mail-lf1-x133.google.com with SMTP id s20so11737186lfb.11; Mon, 06 Mar 2023 01:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678094125; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=utv40n6eEz/XKwDsmLYKb5n25NOl1vGAjxXohGnDUr0=; b=Czwt6VqNG5WXW1nUEMckbHhLYk0vsBTAbILTFlZoMtDsaxW9zUDsm6m0oTh5BzF9q7 aRmKYETxD4I5XKVOBgLtVElOn6AoiMAAglyQ5W0XZVnBdma69hZLofsGZGKDXQgXHH+T kE5qwQm4B2GXzduPow9G7T05OXINYmKsqa0NeKqb26e7PodP3j+BBoW3gTo10UHBg9xa Kr4gCw9vVVeoFeHbjPA1R8lxDznPBMln3FamabvXE63g+yIBrtDByopaKHQ0zJW69gVi NC8DHCzIWD4KVm5IZBopAphNwhQ6e3VhZmbrNmJZ8umrbRfDP8atsyZYXqoXSOxfBXsx GmRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678094125; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=utv40n6eEz/XKwDsmLYKb5n25NOl1vGAjxXohGnDUr0=; b=tk8xNjHMVouHDwk7/1tOm4ttxMaqjDL0PshNOARZjk2bVbq0nBXj9qrcdErJLrHPk5 WQPXttmwHF/nkVRRpYtH+8FAaOYxadjaNdKlS0RBLrlOu86S6yWvtxHpD9wMYh5SUzHz 4Wmy44TcB4PjiE7ZxnoDAOjzrg4Yd7FV3nQ7ouvbnHxhRarV/cC7Ah7YaSfWzKyBLaG0 x4Yt+kmN5ku9mG+mxuqcOQzAlYlzhomYs36ARHGaldbgEOJ9al3JdI0vEUDxaqyBRLUO q4nQZW1WD1pqhX5sdgymiXBjkJbmuxpMhd7yiHqhayAv8Vm/YSlcU4SALCMxSbA+xA2K MOUg== X-Gm-Message-State: AO0yUKXH9MDYlSM91kYcMLJULsZj9E7OyNwP/Qa9SZXYJOOM76UIzlpN k0GU+6S79hMk8IlV3gvrfMk= X-Google-Smtp-Source: AK7set+MjJo2BckoazcHHHmEfLy2D+ph003Z0MeVus/VKW9Ab5YmP6Li2FU39pT/JyH6+ekCw1fGUg== X-Received: by 2002:ac2:5a4c:0:b0:4dc:7ff4:83f9 with SMTP id r12-20020ac25a4c000000b004dc7ff483f9mr2973791lfn.16.1678094125211; Mon, 06 Mar 2023 01:15:25 -0800 (PST) Received: from dc75zzyyyyyyyyyyyyydy-3.rev.dnainternet.fi (dc75zzyyyyyyyyyyyyydy-3.rev.dnainternet.fi. [2001:14ba:16f3:4a00::6]) by smtp.gmail.com with ESMTPSA id e27-20020ac2547b000000b004cafa01ebbfsm1559418lfn.101.2023.03.06.01.15.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 01:15:24 -0800 (PST) Date: Mon, 6 Mar 2023 11:15:10 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Masahiro Yamada , Randy Dunlap , Matti Vaittinen , Shreeya Patel , Krzysztof Kozlowski , Jonathan Cameron , devicetree@vger.kernel.org, Zhigang Shi , Lars-Peter Clausen , Paul Gazzillo , Rob Herring , Dmitry Osipenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Liam Beguin , Andy Shevchenko Subject: [PATCH v3 0/6] Support ROHM BU27034 ALS sensor Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Support ROHM BU27034 ALS sensor This series adds support for ROHM BU27034 Ambient Light Sensor. The BU27034 has configurable gain and measurement (integration) time settings. Both of these have inversely proportional relation to the sensor's intensity channel scale. Many users only set the scale, which means that many drivers attempt to 'guess' the best gain+time combination to meet the scale. Usually this is the biggest integration time which allows setting the requested scale. Typically, increasing the integration time has better accuracy than increasing the gain, which often amplifies the noise as well as the real signal. However, there may be cases where more responsive sensors are needed. So, in some cases the longest integration times may not be what the user prefers. The driver has no way of knowing this. Hence, the approach taken by this series is to allow user to set both the scale and the integration time with following logic: 1. When scale is set, the existing integration time is tried to be maintained as a first priority. 1a) If the requested scale can't be met by current time, then also other time + gain combinations are searched. If scale can be met by some other integration time, then the new time may be applied. If the time setting is common for all channels, then also other channels must be able to maintain their scale with this new time (by changing their gain). The new times are scanned in the order of preference (typically the longest times first). 1b) If the requested scale can be met using current time, then only the gain for the channel is changed. 2. When the integration time change - scale is tried to be maintained. When integration time change is requested also gain for all impacted channels is adjusted so that the scale is not changed, or is chaned as little as possible. This is different from the RFCv1 where the request was rejected if suitable gain couldn't be found for some channel(s). This logic is simple. When total gain (either caused by time or hw-gain) is doubled, the scale gets halved. Also, the supported times are given a 'multiplier' value which tells how much they increase the total gain. However, when I wrote this logic in bu27034 driver, I made quite a few errors on the way - and driver got pretty big. As I am writing drivers for two other sensors (RGB C/IR + flicker BU27010 and RGB C/IR BU27008) with similar gain-time-scale logic I thought that adding common helpers for these computations might be wise. I hope this way all the bugs will be concentrated in one place and not in every individual driver ;) Hence, this series also intriduces IIO gain-time-scale helpers (abbreviated as gts-helpers) + a couple of KUnit tests for the most hairy parts. I can't help thinking that there should've been simpler way of computing the gain-time-scale conversions. Also, pretty good speed improvements might be available if some of the do_div()s could be replaced by >>. This, however, is not a priority for my light-sensor use-case where speed demands are not that big. Finally, these added helpers do provide some value also for drivers which only: a) allow gain change or b) allow changing both the time and gain but so that the time-change is not reflected in register values. For a) we provide the gain - selector (register value) table format + selector to gain look-ups, gain <-> scale conversions and the available scales helpers. For latter case we also provide the time-tables, and actually all the APIs should be usable by setting the time multiplier to 1. (not testeted thoroughly though). Revision history: v2 => v3: (Mostly fixes to review comments from Andy and Jonathan) - dt-binding and maintainer patches unchanged. - iio-gts-helper tests: Use namespaces - iio-gts-helpers + bu27034 plenty of changes. See more comprehensive changelog in individual patches. RFCv1 => v2: dt-bindings: - Fix binding file name and id by using comma instead of a hyphen to separate the vendor and part names. gts-helpers: - fix include guardian - Improve kernel doc for iio_init_iio_gts. - Add iio_gts_scale_to_total_gain - Add iio_gts_total_gain_to_scale - Fix review comments from Jonathan - add documentation to few functions - replace 0xffffffffffffffffLLU by U64_MAX - some styling fixes - drop unnecessary NULL checks - order function arguments by in / out purpose - drop GAIN_SCALE_ITIME_MS() - Add helpers for available scales and times - Rename to iio-gts-helpers gts-tests: - add tests for available scales/times helpers - adapt to renamed iio-gts-helpers.h header bu27034-driver: - (really) protect read-only registers - fix get and set gain - buffered mode - Protect the whole sequences including meas_en/meas_dis to avoid messing up the enable / disable order - typofixes / doc improvements - change dropped GAIN_SCALE_ITIME_MS() to GAIN_SCALE_ITIME_US() - use more accurate scale for lux channel (milli lux) - provide available scales / integration times (using helpers). - adapt to renamed iio-gts-helpers.h file - bu27034 - longer lines in Kconfig - Drop bu27034_meas_en and bu27034_meas_dis wrappers. - Change device-name from bu27034-als to bu27034 MAINTAINERS: - Add iio-list --- Matti Vaittinen (6): dt-bindings: iio: light: Support ROHM BU27034 iio: light: Add gain-time-scale helpers iio: test: test gain-time-scale helpers MAINTAINERS: Add IIO gain-time-scale helpers iio: light: ROHM BU27034 Ambient Light Sensor MAINTAINERS: Add ROHM BU27034 .../bindings/iio/light/rohm,bu27034.yaml | 46 + MAINTAINERS | 14 + drivers/iio/light/Kconfig | 17 + drivers/iio/light/Makefile | 2 + drivers/iio/light/iio-gts-helper.c | 1158 +++++++++++++ drivers/iio/light/iio-gts-helper.h | 134 ++ drivers/iio/light/rohm-bu27034.c | 1501 +++++++++++++++++ drivers/iio/test/Kconfig | 15 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-gts.c | 538 ++++++ 10 files changed, 3426 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/rohm,bu27034.yaml create mode 100644 drivers/iio/light/iio-gts-helper.c create mode 100644 drivers/iio/light/iio-gts-helper.h create mode 100644 drivers/iio/light/rohm-bu27034.c create mode 100644 drivers/iio/test/iio-test-gts.c base-commit: c9c3395d5e3dcc6daee66c6908354d47bf98cb0c