From patchwork Tue Dec 24 10:59:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13919938 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F1FA192D76; Tue, 24 Dec 2024 10:59:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735037999; cv=none; b=fyw+6OlJN191fbJYJv4Gm8NLw+erPMAu08tWmjRNiDA3+vexE8prP5H2DCLr19BnWZMXsQ/w1fm7UElG2XnjvedHy9aKU8suYrVpLyPRr4iBffswpLzIMLRdUVp3yzkzcYQF6WsqV6MDo8irwf/wdeVJAILPMjQL9HhjkPSqN/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735037999; c=relaxed/simple; bh=NzK4cjcUACTSCFQVbojeKefz34qKcANt5reQRhyKDJw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LpOy1+jIicMpuw6wnBzUTQWygUrBrACkLRPOWA89SEKB/YSmYj9cCDNbGE69gdCqDuatsMAplLTfpZBvWTknVRXNJ2tpzILuKEG7UgI5Gy5CFOJINetxtKEklDU8cRB4E7xbF6Z9gYoPXOeGTREGmKWqOf1wRWAhpKPeCCZDD80= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WVOFwfrj; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WVOFwfrj" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-385de9f789cso3837994f8f.2; Tue, 24 Dec 2024 02:59:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735037996; x=1735642796; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LShQ3cZdx7UGTQMZnEPJlH8RBGekAXkVTNISJd8oxLQ=; b=WVOFwfrjzChFK5lX4FJRVopgrIRVG23S5TSZHDv+WQKcAt6Q9bBgXM0Bd61RZyHcQ5 9OiLVj1Vvr0bzzOQ+8Ot/7C23z2GhPqTqWfvwhEd79+ohGYle7zC9Y75FuOtjGY2UqGw Pw4h2MeSUWwS5LpIFRBVPG+cHo1jFi6ZkmkViaGUxauiUXsFzJpLpUn6O9UJw+YtGCWV sXoEnzw21Gj6VsNDOfWlW2jmuJgYhHamTU54yVtxyCSRfM23/72+dYqSK+shVZgSypnD gqcyahvGaTrz2ZFTWaqbwzrt3008S6HWApFDlcb0OtamdiY0rCoqo5usdgpS3Mcvl4Yj jLgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735037996; x=1735642796; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LShQ3cZdx7UGTQMZnEPJlH8RBGekAXkVTNISJd8oxLQ=; b=X2IfRGkIRZeFkHUoSmc61nGQR939lTlrsPcETPzXSBSGQ185filmrcgyX22/ljGaZt yWZ+70vCMwnoX1DlOeopGOBDk0Tm7P0ePmcPpipyvFTf5C3Br2xVg3ZFXlinSNYoVtQq En3ueoruB4sGNz6gIpLP1o7xUvmkuASF7ka54gHabbs8v118pk2M6UwHfZd1XkGsrxDg x0cwq2+3fh+lOAceUATY5pbWkcwBjQs7Yl7fNmCoIZ4fRT6V9NRuiLW9Nh4Bbhirzg2s ZuLZ9FygRw+pq06CaS6ze4rB+G0hvWl7+esAAdyanz082ZxOmVw8apy65jYFxGm8/Omx GITw== X-Forwarded-Encrypted: i=1; AJvYcCWZNliB8bXqQeZ963UnQI2v7j1GpKGMqAMFycYaPNcvzDvxONQz+MyeT1p3w+ObUJpkxagGWEGl3VsCU1c=@vger.kernel.org X-Gm-Message-State: AOJu0YyzjOYx7o8zrjhC/LjFjtk+LwBgimIl31t32QwiW0w5G4iSpoRG GpN4pbsHzR1GnB5ZPqS5KNeQDSvW6bdJpy2pFIlVrz4D0F6ecZHi X-Gm-Gg: ASbGncuytRsSyM+U9n78kmoltPPglg2ENy8r8hXMksSE5ezMpdnPkDXgxJZPYNnJZzT IE9s1zfIqzfs/p322xso6XfTMlSwuxqLuuVrhN3G5uReNCwco36VNyDH+oJRvrksTHwGLxNfO5D csI2AmcjLVr7CuPrOa3s783n2eaqY/b9GfFTgGInwl5NqGD8wIkPJqv/83V3XQYzx7W9IMCqIP6 uX+zqCE3z7fD1A2x8u+CggG6MG92EMoeEbQqotsGQXciEhpT9MjJ08ph6NcC3frl+3An3eChTB3 IJiZ/6s+nzNVOIk+EweP8tvIeW4hbjreMoOJnalDWQWtpSTf4V38rH/87Z0MsP+bmvhKL3FWfXM tXw== X-Google-Smtp-Source: AGHT+IE7s3pkOcQqkBsgIM+4dkbj2Rc6ZfsT8DKtU5Id1KqujTE4gNkAPCop8nxwYzF9VAdiqUZ8eQ== X-Received: by 2002:adf:a443:0:b0:38a:3732:4462 with SMTP id ffacd0b85a97d-38a37324578mr1887743f8f.48.1735037995767; Tue, 24 Dec 2024 02:59:55 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1a49-0b63-5213-952a.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1a49:b63:5213:952a]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8acb85sm13699427f8f.103.2024.12.24.02.59.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 02:59:55 -0800 (PST) From: Javier Carrasco Date: Tue, 24 Dec 2024 11:59:00 +0100 Subject: [PATCH v2 1/4] iio: gts-helper: add helpers to ease searches of gain_sel and new_gain Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-veml3235_scale-v2-1-2e1286846c77@gmail.com> References: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> In-Reply-To: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> To: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , Javier Carrasco X-Mailer: b4 0.15-dev-1b0d6 X-Developer-Signature: v=1; a=ed25519-sha256; t=1735037993; l=5803; i=javier.carrasco.cruz@gmail.com; s=20240312; h=from:subject:message-id; bh=NzK4cjcUACTSCFQVbojeKefz34qKcANt5reQRhyKDJw=; b=PCnEm7HPYJiMzQXoFUVeuyMjzJc5I7pLRV4N3Sn4yRc/2fC/1Y1BhP96xWJkVyDLpkPGnE2GV 1rOI+lPtXzzAWEgPD2bmAziqqyr4FVtteFL1QxgghU6yjGHcZxtWcB7 X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=lzSIvIzMz0JhJrzLXI0HAdPwsNPSSmEn6RbS+PTS9aQ= This helper functions reduce the burden in the drivers that want to fetch a gain selector in all available times or a new optimal gain. The former is currently achieved by calling iio_gts_find_gain_sel_for_scale_using_time() for the current time selector, and then iterating over the rest of time selectors if the gain selector was not found. The latter requires a combination of multiple iio-gts helpers to find the new gain, look for an optimal gain if there was no exact match, and set a minimum gain if the optimal gain is not in the range of available gains. Provide simpler workflows by means of functions that address common patterns in the users of the iio-gts helpers. Signed-off-by: Javier Carrasco --- drivers/iio/industrialio-gts-helper.c | 76 +++++++++++++++++++++++++++++++++++ include/linux/iio/iio-gts-helper.h | 5 +++ 2 files changed, 81 insertions(+) diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index 3b5a998150623ba43c2f015c2c5d8a757743b893..7a96dbec45848781008f72338d63d7693abb9076 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -915,6 +915,40 @@ int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel } EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_sel_for_scale_using_time, "IIO_GTS_HELPER"); +/** + * iio_gts_find_gain_sel_in_times - Fetch gain selector in the available times. + * @gts: Gain time scale descriptor + * @scale_int: Integral part of the scale (typically val1) + * @scale_nano: Fractional part of the scale (nano or ppb) + * @gain_sel: Pointer to value where gain selector is stored. + * @time_sel: Pointer to value where time selector is stored. + * + * Wrapper around iio_gts_find_gain_for_scale_using_time() to fetch the + * gain selector for all supported integration times. + * + * Return: 0 on success and -EINVAL on error. + */ +int iio_gts_find_gain_sel_in_times(struct iio_gts *gts, int scale_int, + int scale_nano, int *gain_sel, int *time_sel) +{ + int i, ret; + + for (i = 0; i < gts->num_itime; i++) { + *time_sel = gts->itime_table[i].sel; + ret = iio_gts_find_gain_sel_for_scale_using_time(gts, *time_sel, + scale_int, + scale_nano, + gain_sel); + if (ret) + continue; + + return 0; + } + + return -EINVAL; +} +EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_sel_in_times, "IIO_GTS_HELPER"); + static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time) { const struct iio_itime_sel_mul *itime; @@ -1086,6 +1120,48 @@ int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain, } EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_old_gain_time, "IIO_GTS_HELPER"); +/** + * iio_gts_find_new_gain_by_gain_time_min - compensate for time change + * @gts: Gain time scale descriptor + * @old_gain: Previously set gain + * @old_time: Selector corresponding previously set time + * @new_time: Selector corresponding new time to be set + * @new_gain: Pointer to value where new gain is to be written + * @in_range: Indicate if the @new_gain was in the range of + * supported gains. + * + * Wrapper around iio_gts_find_new_gain_by_old_gain_time() that tries to + * set an optimal value if no exact match was found, defaulting to the + * minimum gain to avoid saturations if the optimal value is not in the + * range of supported gains. + * + * Return: 0 on success and a negative value if no gain was found. + */ +int iio_gts_find_new_gain_by_gain_time_min(struct iio_gts *gts, int old_gain, + int old_time, int new_time, + int *new_gain, bool *in_range) +{ + int ret; + + *in_range = true; + ret = iio_gts_find_new_gain_by_old_gain_time(gts, old_gain, old_time, + new_time, new_gain); + if (*new_gain < 0) + return -EINVAL; + + if (ret) { + *new_gain = iio_find_closest_gain_low(gts, *new_gain, in_range); + if (*new_gain < 0) { + *new_gain = iio_gts_get_min_gain(gts); + if (*new_gain < 0) + return -EINVAL; + } + } + + return 0; +} +EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_gain_time_min, "IIO_GTS_HELPER"); + MODULE_LICENSE("GPL"); MODULE_AUTHOR("Matti Vaittinen "); MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers"); diff --git a/include/linux/iio/iio-gts-helper.h b/include/linux/iio/iio-gts-helper.h index 9cb6c80dea716cb80b69bee5277230bafe1c9a95..ae91ad008cc8cbd9d674402f3d4e50385c78e5d1 100644 --- a/include/linux/iio/iio-gts-helper.h +++ b/include/linux/iio/iio-gts-helper.h @@ -188,6 +188,8 @@ int iio_gts_total_gain_to_scale(struct iio_gts *gts, int total_gain, int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel, int scale_int, int scale_nano, int *gain_sel); +int iio_gts_find_gain_sel_in_times(struct iio_gts *gts, int scale_int, + int scale_nano, int *gain_sel, int *time_sel); int iio_gts_get_scale(struct iio_gts *gts, int gain, int time, int *scale_int, int *scale_nano); int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts, @@ -196,6 +198,9 @@ int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts, int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain, int old_time, int new_time, int *new_gain); +int iio_gts_find_new_gain_by_gain_time_min(struct iio_gts *gts, int old_gain, + int old_time, int new_time, + int *new_gain, bool *in_range); int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type, int *length); int iio_gts_all_avail_scales(struct iio_gts *gts, const int **vals, int *type, From patchwork Tue Dec 24 10:59:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13919939 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26DD81B4F02; Tue, 24 Dec 2024 10:59:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735038000; cv=none; b=MuWRnbNY+70U1s9syj5mP3jYSXLXJBz2FCsvNXxZnn3reJrgSMdA/GH9kz6T95iB3wRsfDiT2ZOP9p7HiS+M00Dokqyy2ZgqGRDmidLIo2JsMxcnSNIVYQndge8qSkLjhIzisn+kZF8qXVzUqtUez7+hfP42Frl1hlk7uapED6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735038000; c=relaxed/simple; bh=2Xw81VnH7Pi3oW32cYAtoCHyGZacAncEkYKwYPX+v9s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sxIUaQGum/Z0Gzv9+9Oe8AuW84eHbhA2jJRWl26OlmcKb7FoIEz3u3xjO5Jw6SJGjNSRlPCU4iM62E3JF++3I8D8wGOX+OVf/ZjJHWkUguAiLNmlfCSjwIuf/U8kyJemJ9qmSUV13VEv3zdz+4kkNmH0ctJNxgqH6Hi/SjjsN3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Mio64nP1; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Mio64nP1" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4363ae65100so53478275e9.0; Tue, 24 Dec 2024 02:59:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735037997; x=1735642797; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=YMV9SEazsPxA7ks5Ucr2JrZZrfBykigFdMLCjyvHqjQ=; b=Mio64nP1E0vbAq71beUqxrgnk6C8qeOfiZ23RZSA9k+xbnb34VFAXT2GoDM0sh9MKz IBspv8c2uPaB1XKsB0fBCpArg1kvONhqcUKcixnulz01H/HlOLFflmc9PcgeQFHXAFDq 3WnBtFpa/tRYQX6CO5R8FIowbS5yJNypR/uPfnhtKTym50jI9aORdLJGMaVRJvRYgRLj ivxZR5yArIkJWGQTfjkius0e/s4BpNA8KscivAXCWKsPgOTxdvlkrWLcFfBP8Oif37Vo IQ4rj5PnqQScoAQ2JnEQBU9bAXz9xmC7UJiujfpXVnQvLIut3gzVE/kWb3kn5+K96t4J n/tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735037997; x=1735642797; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YMV9SEazsPxA7ks5Ucr2JrZZrfBykigFdMLCjyvHqjQ=; b=jSjx9VtAvPAi94W+8EXOzw+I++CVMZdqd7g88cwqE6+C2grthUX6uizcMzfDUEFO4A xyXQZnpvgHaiRS8kPUCaMFTluAsVccbSm4hgMDlE3s0+602bWjens5pBrINgV/zPE8Lq xS1zDhfgIftuInF7nJtwN1rVnJCFof/oPuz4bSQWC6c/51O227TdFH0NIGHsfpXdFdWK hkWxbD5Mm6O6A96y9l9qr1csA8coL8qO0NasLDYj3YLDaIFSJGzOE941PAfJA83+ut12 gEavri6YfuQ1FwoC4Sk5ublQx5HngnYM8ACDcWIZpPmIczziTNBGab4h/8QsANMagPAV vxqw== X-Forwarded-Encrypted: i=1; AJvYcCXW9/ii/iV/WinPwKJOUihq5ZsFavS9X5IAM1HvamapTNIOFLAhSKYp/Sdlehq8QFe15viyJUbzeXTBwj8=@vger.kernel.org X-Gm-Message-State: AOJu0YwAqnwogF2JTKPYxEvaDzCllssQkt6G9MbzVLWjUo7RhPKhOtP/ YBc28tKAZwlD84RumT3nI8ZFARSfgteEW1qJqnm/pJSOa8etKsSg1/Pf3Q== X-Gm-Gg: ASbGnctzYxC6j5q3xQtmcTTckqjfPhiSl+Z229BJwfjtN6KM5P37Jnst1+HpCB2MGES 5OmOokQsJykambWNuDT6Meop6koISBdG/xCKKGPD1tQJzq4Fo7whhvubQiu3hdh//wwDnYxx4Qw lM4BTGgh2ZXJCL5z/gWFKyi0J6bYGAUvI02TZAUOloN41GYjRvt8uUL8dA7FRjjRENQWkM634T+ AVATRsjxxmvt2Melr2o2Rl4660z6Q7dc9CU+d7XR+Vto2oWv179zOGkkr8y2TyNEl94URHktPai zgcD1LbSOI6xI7jYTDu+d7Ry/oxJDfZN9xXn2y+Suv+5QHI6fiZypuQgyh60n+t3J76HKzbc85W dRg== X-Google-Smtp-Source: AGHT+IExOQ+qcuxs9yGFpM5l4EtC0wK1SDOUgQMVGzUP4kqM+QAfqqQmTjaoV43pPUIhKx9xa/WfSg== X-Received: by 2002:a05:6000:186b:b0:385:e877:c037 with SMTP id ffacd0b85a97d-38a223f82f4mr13609587f8f.42.1735037997443; Tue, 24 Dec 2024 02:59:57 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1a49-0b63-5213-952a.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1a49:b63:5213:952a]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8acb85sm13699427f8f.103.2024.12.24.02.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 02:59:56 -0800 (PST) From: Javier Carrasco Date: Tue, 24 Dec 2024 11:59:01 +0100 Subject: [PATCH v2 2/4] iio: light: veml3235: fix code style Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-veml3235_scale-v2-2-2e1286846c77@gmail.com> References: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> In-Reply-To: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> To: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , Javier Carrasco X-Mailer: b4 0.15-dev-1b0d6 X-Developer-Signature: v=1; a=ed25519-sha256; t=1735037993; l=1057; i=javier.carrasco.cruz@gmail.com; s=20240312; h=from:subject:message-id; bh=2Xw81VnH7Pi3oW32cYAtoCHyGZacAncEkYKwYPX+v9s=; b=y8BE6ROBvnqeHuQmptIxRBatLmyQou5rZNeDYKHoo9RrcoJfzC4N/+Poive6O7JX3RqxGiziM vmMq+OTBI8PDo4WTTkWQBpA5nW5ZdVddG1FkPftE4Yrv3g4SUHArZrg X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=lzSIvIzMz0JhJrzLXI0HAdPwsNPSSmEn6RbS+PTS9aQ= Trivial fixes to drop double spacings. Signed-off-by: Javier Carrasco --- drivers/iio/light/veml3235.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/light/veml3235.c b/drivers/iio/light/veml3235.c index 66361c3012a3d9f30a79630d51f329dacfb141bc..fa5c7e7dfbfaec1b96428612b1dcba91ea51603f 100644 --- a/drivers/iio/light/veml3235.c +++ b/drivers/iio/light/veml3235.c @@ -321,7 +321,7 @@ static void veml3235_read_id(struct veml3235_data *data) { int ret, reg; - ret = regmap_field_read(data->rf.id, ®); + ret = regmap_field_read(data->rf.id, ®); if (ret) { dev_info(data->dev, "failed to read ID\n"); return; @@ -389,8 +389,8 @@ static int veml3235_hw_init(struct iio_dev *indio_dev) } static const struct iio_info veml3235_info = { - .read_raw = veml3235_read_raw, - .read_avail = veml3235_read_avail, + .read_raw = veml3235_read_raw, + .read_avail = veml3235_read_avail, .write_raw = veml3235_write_raw, }; From patchwork Tue Dec 24 10:59:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13919940 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 178EC1B87F5; Tue, 24 Dec 2024 11:00:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735038002; cv=none; b=V+XHNqO2Yf4cIVQ0wraLjISYNOX62aOL9ysVe5QJ5+0iZ4qQN7rLJtwM/frfpA1MsAmFRraH1kqCqw0HipDCw+SJ5AmIdVXBKeMvP8WEoZ6PUnphB2mYjian+IgLCHlppglHSq+Vc1ObeKMvtDmDomrPxbqyc1WsfgiTSYBZdeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735038002; c=relaxed/simple; bh=sPLOEksW/AM8olcpcDqn2YYtDWmfEDxZzIdwUjGngkA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hhrmkp80zkwutuxQQ/KGy20g+9MJbHJsR7dCccKq1PUvT4csP+Lb2eSntMTlkh9Axv68/6aGMPoWyhmdfrHUGXVVJ7cM6NHcIX1UwBQwJ8QsEMs5o0tEqb3nfCz6IL6bEP8pG2PxmAiK4xQSAaWyjnV/tubZ1HcWPohnVDkhe3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l4mb2UGj; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l4mb2UGj" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-436202dd730so36435735e9.2; Tue, 24 Dec 2024 03:00:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735037999; x=1735642799; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1SS4x6h64jxZLgUFCVk1KhAJGzDKZ2HMe1hx7S4Ppb0=; b=l4mb2UGjSSLxoZC5IAYtq5Ahl2fG6BjRCRlQlKQkASXK4sRzaIr/ubi66pp06fw5f2 mphpcNSnoh1TbxE8xPYxLH7Iwvb4JVJ4PfrunIiO6EldxzlusOKKh715HvE+V/eMv7FO iBboRyhPeOzNk8fOf2o91dseImu6new37zs9AtHcIOe/SG2tSy5owtw0yX/2jL4Cc0jX Wn8fClXAh5P3A+pQxDzRJuIbEjvC9d9HMl/ZHw9yKS/u4H+QHfs4+yEV+7ESMhfYgnja bjMVAixaQ1JS+r5Z6t3j7A/MiqscFxtJ9e3mxYa1hkv7z7Z1GkVeoxrESFG6w327GQMC s62A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735037999; x=1735642799; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1SS4x6h64jxZLgUFCVk1KhAJGzDKZ2HMe1hx7S4Ppb0=; b=WbYOLGV7aq6t4X2Prmq1bAZCus2dxDpUQ2NEpIMIdItoZisPlAgSotbaTnXiJmUuzf SxRFN2KUwXxLELnt7EPGjBzx6RW4hdN5kKN0ZDcANw1GaAZLSCCOnHagnNNF4DebTlRj 9gWS3EwiElyxr3irMzSDKoIB3upDACGzRQPKgjIm2zSav9PWlPAsCDdKBy72pWxp0NOY w1P6uDasfzayvuYs5Q96WxsKx4hqUHpZARwxaESzSZyvqrSGqPlqtJTeLVx+HfqVxAJb hkILUwfOakPFwZh3NlTnGFHJvONqoeqbQsHDHFTEtP4L1Ku2rAVJVYmROBac+s1x2VUH KTKA== X-Forwarded-Encrypted: i=1; AJvYcCXEeTAdy5l6paZR0Xe5oD56tpszQ8YmyhCt5GoBOzW4XwZCgKwluPvnGUHZPd2/RuUvS6ySfxqj72RumZw=@vger.kernel.org X-Gm-Message-State: AOJu0YxdyhPJDWMiTq/I63jV4/HTSxAvU2eLdxdjz23DVTpIrI3dwzvZ xfEbowj6pqtF5WZQ8n/vFp3vPXPNsTye2y63fKcwJ+oCc7PyfXKD X-Gm-Gg: ASbGncuq6mzOn15RrS0YTLv3ZPLFiM1LfUQrKcg0DixhmIPj6KWsiUCiMHKPqVp9Dzn vsDUutKHcyB0izuphfCITEchHf/lIBfy6jfuXNCULwltTM/GHrDtqSwKjfNON2WIzOhd9oUE/U0 iherPzS/YEMVMfcoBPeSc3TSRW0wwzu/671pgXGzoM5lpgGwZYZOL8z1HNE/7l0ShXbJ/04JXLm CItr+PdAS2M9L1Mp0I2eBHEAmx0ehsXC7nQLA3bNKuL/dF8POnE1sxxeYI38q2dCHFPNTJyT3tK iIEip3NOFs60jf2SxSDi7NqTxXE24eLtpptyi2gJ/ItjaneMTh739EDJ2WxN4dQO4Ao4tBSTUAs jCA== X-Google-Smtp-Source: AGHT+IEOzvExCHJ7boMrOt1ykGMR+oFnx1PyauQX0twRNSmwegBWqiQXP8s9B6uXLExMxiY0bqb2RQ== X-Received: by 2002:a05:600c:350c:b0:434:a91e:c709 with SMTP id 5b1f17b1804b1-43668b78c11mr108381225e9.28.1735037998986; Tue, 24 Dec 2024 02:59:58 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1a49-0b63-5213-952a.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1a49:b63:5213:952a]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8acb85sm13699427f8f.103.2024.12.24.02.59.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 02:59:58 -0800 (PST) From: Javier Carrasco Date: Tue, 24 Dec 2024 11:59:02 +0100 Subject: [PATCH v2 3/4] iio: light: veml3235: extend regmap to add cache Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-veml3235_scale-v2-3-2e1286846c77@gmail.com> References: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> In-Reply-To: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> To: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , Javier Carrasco X-Mailer: b4 0.15-dev-1b0d6 X-Developer-Signature: v=1; a=ed25519-sha256; t=1735037993; l=2136; i=javier.carrasco.cruz@gmail.com; s=20240312; h=from:subject:message-id; bh=sPLOEksW/AM8olcpcDqn2YYtDWmfEDxZzIdwUjGngkA=; b=exK3Xy2BHvv8Bk682aOGsh4yGrCUrI6eV3/2A2FsaCgJf0znOmiw855uhoIUOA3wWKiZ9iveI 3oeixOuNfg6AFSoBGKVyNQgLjAzkdR4S/qaljhN9VJCjc6Ul/SkdAeC X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=lzSIvIzMz0JhJrzLXI0HAdPwsNPSSmEn6RbS+PTS9aQ= The configuration and ID registers are not volatile and are not affected by read operations (i.e. not precious), making them suitable to be cached in order to reduce the number of accesses to the device. Signed-off-by: Javier Carrasco --- drivers/iio/light/veml3235.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/drivers/iio/light/veml3235.c b/drivers/iio/light/veml3235.c index fa5c7e7dfbfaec1b96428612b1dcba91ea51603f..f754980ea156a6e128ff159b816e09099197c5c7 100644 --- a/drivers/iio/light/veml3235.c +++ b/drivers/iio/light/veml3235.c @@ -101,12 +101,43 @@ static const struct iio_chan_spec veml3235_channels[] = { }, }; +static const struct regmap_range veml3235_readable_ranges[] = { + regmap_reg_range(VEML3235_REG_CONF, VEML3235_REG_ID), +}; + +static const struct regmap_access_table veml3235_readable_table = { + .yes_ranges = veml3235_readable_ranges, + .n_yes_ranges = ARRAY_SIZE(veml3235_readable_ranges), +}; + +static const struct regmap_range veml3235_writable_ranges[] = { + regmap_reg_range(VEML3235_REG_CONF, VEML3235_REG_CONF), +}; + +static const struct regmap_access_table veml3235_writable_table = { + .yes_ranges = veml3235_writable_ranges, + .n_yes_ranges = ARRAY_SIZE(veml3235_writable_ranges), +}; + +static const struct regmap_range veml3235_volatile_ranges[] = { + regmap_reg_range(VEML3235_REG_WH_DATA, VEML3235_REG_ALS_DATA), +}; + +static const struct regmap_access_table veml3235_volatile_table = { + .yes_ranges = veml3235_volatile_ranges, + .n_yes_ranges = ARRAY_SIZE(veml3235_volatile_ranges), +}; + static const struct regmap_config veml3235_regmap_config = { .name = "veml3235_regmap", .reg_bits = 8, .val_bits = 16, .max_register = VEML3235_REG_ID, .val_format_endian = REGMAP_ENDIAN_LITTLE, + .rd_table = &veml3235_readable_table, + .wr_table = &veml3235_writable_table, + .volatile_table = &veml3235_volatile_table, + .cache_type = REGCACHE_RBTREE, }; static int veml3235_get_it(struct veml3235_data *data, int *val, int *val2) From patchwork Tue Dec 24 10:59:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13919941 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A8A11BE251; Tue, 24 Dec 2024 11:00:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735038004; cv=none; b=ntXPM9/zrnkpz0vrpCck8laz/Pl/LDaMJ5YDF2XavXFYNGo34WqutOz/ANiSjI+BKCv18gBjMQvWcOCpibuuR5o5eCr+eESSwRFkAtGMFbGK1JzpCh3xOEAasesxQZwnB+QMIAQQOY/r0BtggpPT2EMk4UjJyv1X2oSJ8hB3vAY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735038004; c=relaxed/simple; bh=3L4AkJMuOiqvoy831vC+216muqu8ivWuslJJlT0OtXo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Xr91oy+mzBuhKayDMYChe5xlDpLB75z96yPbOZWnu3Qxve+dfIYrEYj0nwumuNL/LZ6ftmF9BXWeD8gMSe9fTIXeTbQk3vFgODFdArRFJC+WJEs3sm0mBeQmo/WoCiwHv9J4EXVSTGdMIXXlTKYTgkX6v8ZRzGtw5W2PHXuNNoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iRG2cwab; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iRG2cwab" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3862b40a6e0so2861908f8f.0; Tue, 24 Dec 2024 03:00:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735038000; x=1735642800; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=rd7GQFui/s6a+z+46G9C342q/zChKZOmrRYi99jNoGc=; b=iRG2cwabktouHS8vcai1z32kwgbNWK6Ab8yuzXwc1oKKypEYU+LNKRD+smUnOlL0Lk iKovdc6RAwGiJ+ORjr35jfsN89jYtPRBkxisOhsNYxRbmtEBJKYwksxEyJI/TBHRXNcH w5YDGXniCd9slimrvO3JaGZPJ4rh24aM4U6K0b8ik/6T5SCtL/Gf6saDRldHgTGe/Xdv ufckJwixdX1+nOqhJ0MBitzyKHD8YXbzXIRIBmhKfamIleXEhGYnrz8OpltMyhOtdffo tRL4P9QJE/ioKe65DyqdoFZOiuZfX5BBHhwbadI/Srx79Gbsukf1ViT4mME7WbIBSJMf dFIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735038000; x=1735642800; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rd7GQFui/s6a+z+46G9C342q/zChKZOmrRYi99jNoGc=; b=VUhqruwvHjvtx+w+hYQwn2oExrzxxhhuVG9c7k4RauBJXeT4oJwTa2SWA/o3d7kOPL 8isMZLwVJ3O6sVRvaSB6sKRsK+Zxmj4rAbwdNlMeRhQ11biCAmPOrDVFsHSTkt0A28ZO I4UarJLoyg8WzPuN/XfiLE0y0ki/+kdyQDqHkBkg5iyOhrETxfnnj97XZvwjPU0ragJr yNi79xoNzAgHCGkGkkFCFzPQnv/oXjZ4rKLm8ftIhnjhpkiLo6Xx0CjSRxFhnjZVFVlH KbFwKH8sUMtMyYkKUnnqXPO+LxLB0TgbdwoKscq9U+cPkX5F/iJpnltJkewPX1RRaT42 QvvA== X-Forwarded-Encrypted: i=1; AJvYcCX7N/gntC6iyeyn3qP7loc2lBjaL49fJqPK6bouS/PIl/BcObbkOXIHz3M5r7vAFx2AOhVozsmI/Wb7Lr8=@vger.kernel.org X-Gm-Message-State: AOJu0YxjwcVHtvOhPd8ViHAg5fEh5eYILNvySjD3jWe9sXlxmrE/P5hz GmVG9hBKBreoNNm0AxKV5fqFPKbQ8HKwMyPm6ybQv/UUwQXSLpUD X-Gm-Gg: ASbGnctcuzDLnEIbuS0zDkGlVorTmll7cAHwWWQLWG6TezYFREcBcRGNago0RxZht+t biVLzvGteZT2RAHKZRENUQnc65gfMgop3icWwEHP1s07n69pPJCZCUiQkqwJM1G/I5A1lhAjukw /GcOlGm5ZITYoiXq2FDHWUAMv/i6MVTUFc/dZeMZh/xHcfTJQMYDpP5rtBq1JlIliZL3biaIYhw 8+hHKrz1xdAepoomjAXQ95/KevuDMhqHT/i7OJYaDE4a91WY/dNWhpxvVB05aJhVRpOsetTRrJF Nhh1sOuxE0T5PwXwS/Y0QD6vQDLdu+8/8xYNQRlIQ3ZomTvD0dAbQG7ehUbdkC3am2/6UthntW+ Hog== X-Google-Smtp-Source: AGHT+IHKgW0sfccPnvD5RD/iH1AA80M4gLAKLu6GQz/3JgEanzWfRXz73rsdbN3UQ68v42tEeMv+UQ== X-Received: by 2002:a5d:64e3:0:b0:388:c7c2:5bdb with SMTP id ffacd0b85a97d-38a221e1533mr14637370f8f.2.1735038000352; Tue, 24 Dec 2024 03:00:00 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1a49-0b63-5213-952a.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1a49:b63:5213:952a]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8acb85sm13699427f8f.103.2024.12.24.02.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 02:59:59 -0800 (PST) From: Javier Carrasco Date: Tue, 24 Dec 2024 11:59:03 +0100 Subject: [PATCH v2 4/4] iio: veml3235: fix scale to conform to ABI Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-veml3235_scale-v2-4-2e1286846c77@gmail.com> References: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> In-Reply-To: <20241224-veml3235_scale-v2-0-2e1286846c77@gmail.com> To: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , Javier Carrasco X-Mailer: b4 0.15-dev-1b0d6 X-Developer-Signature: v=1; a=ed25519-sha256; t=1735037993; l=10625; i=javier.carrasco.cruz@gmail.com; s=20240312; h=from:subject:message-id; bh=3L4AkJMuOiqvoy831vC+216muqu8ivWuslJJlT0OtXo=; b=d1l16gRjz9vbtc6jYEQkj+hPkni8Mcg7mIxMb8L+go9fU3gNJVugszsiLdUzpgvlg7p2tacsg bayGJmRXll/DILpd4Mttyfv/bJliH4UA7KjDNeFMp9G4M5Jak0EVIzr X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=lzSIvIzMz0JhJrzLXI0HAdPwsNPSSmEn6RbS+PTS9aQ= The current scale is not ABI-compliant as it is just the sensor gain instead of the value that acts as a multiplier to be applied to the raw value (there is no offset). Use the iio-gts helpers to obtain the proper scale values according to the gain and integration time to match the resolution tables from the datasheet. When at it, use 'scale' instead of 'gain' consistently for the get/set functions to avoid misunderstandings. Fixes: c5a23f80c164 ("iio: light: add support for veml3235") Signed-off-by: Javier Carrasco Reviewed-by: Matti Vaittinen --- drivers/iio/light/Kconfig | 1 + drivers/iio/light/veml3235.c | 237 +++++++++++++++++++++++-------------------- 2 files changed, 130 insertions(+), 108 deletions(-) diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index 0cf9cf2a3f944d9f9967b0e02dcb114c116803a4..12864691a7ffa008df5b3384c9caca7ec234577f 100644 --- a/drivers/iio/light/Kconfig +++ b/drivers/iio/light/Kconfig @@ -656,6 +656,7 @@ config VCNL4035 config VEML3235 tristate "VEML3235 ambient light sensor" select REGMAP_I2C + select IIO_GTS_HELPER depends on I2C help Say Y here if you want to build a driver for the Vishay VEML3235 diff --git a/drivers/iio/light/veml3235.c b/drivers/iio/light/veml3235.c index f754980ea156a6e128ff159b816e09099197c5c7..56e98bf421841bf0082245f69274c7a00c1d5c8a 100644 --- a/drivers/iio/light/veml3235.c +++ b/drivers/iio/light/veml3235.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -35,17 +36,33 @@ struct veml3235_data { struct device *dev; struct regmap *regmap; struct veml3235_rf rf; + struct iio_gts gts; }; -static const int veml3235_it_times[][2] = { - { 0, 50000 }, - { 0, 100000 }, - { 0, 200000 }, - { 0, 400000 }, - { 0, 800000 }, +static const struct iio_itime_sel_mul veml3235_it_sel[] = { + GAIN_SCALE_ITIME_US(50000, 0, 1), + GAIN_SCALE_ITIME_US(100000, 1, 2), + GAIN_SCALE_ITIME_US(200000, 2, 4), + GAIN_SCALE_ITIME_US(400000, 3, 8), + GAIN_SCALE_ITIME_US(800000, 4, 16), }; -static const int veml3235_scale_vals[] = { 1, 2, 4, 8 }; +/* + * The MSB (DG) doubles the value of the rest of the field, which leads to + * two possible combinations to obtain gain = 2 and gain = 4. The gain + * handling can be simplified by restricting DG = 1 to the only gain that + * really requires it, gain = 8. Note that "X10" is a reserved value. + */ +#define VEML3235_SEL_GAIN_X1 0 +#define VEML3235_SEL_GAIN_X2 1 +#define VEML3235_SEL_GAIN_X4 3 +#define VEML3235_SEL_GAIN_X8 7 +static const struct iio_gain_sel_pair veml3235_gain_sel[] = { + GAIN_SCALE_GAIN(1, VEML3235_SEL_GAIN_X1), + GAIN_SCALE_GAIN(2, VEML3235_SEL_GAIN_X2), + GAIN_SCALE_GAIN(4, VEML3235_SEL_GAIN_X4), + GAIN_SCALE_GAIN(8, VEML3235_SEL_GAIN_X8), +}; static int veml3235_power_on(struct veml3235_data *data) { @@ -142,32 +159,17 @@ static const struct regmap_config veml3235_regmap_config = { static int veml3235_get_it(struct veml3235_data *data, int *val, int *val2) { - int ret, reg; + int ret, it_idx; - ret = regmap_field_read(data->rf.it, ®); + ret = regmap_field_read(data->rf.it, &it_idx); if (ret) return ret; - switch (reg) { - case 0: - *val2 = 50000; - break; - case 1: - *val2 = 100000; - break; - case 2: - *val2 = 200000; - break; - case 3: - *val2 = 400000; - break; - case 4: - *val2 = 800000; - break; - default: - return -EINVAL; - } + ret = iio_gts_find_int_time_by_sel(&data->gts, it_idx); + if (ret < 0) + return ret; + *val2 = ret; *val = 0; return IIO_VAL_INT_PLUS_MICRO; @@ -176,78 +178,78 @@ static int veml3235_get_it(struct veml3235_data *data, int *val, int *val2) static int veml3235_set_it(struct iio_dev *indio_dev, int val, int val2) { struct veml3235_data *data = iio_priv(indio_dev); - int ret, new_it; + int ret, gain_idx, it_idx, new_gain, prev_gain, prev_it; + bool in_range; - if (val) + if (val || !iio_gts_valid_time(&data->gts, val2)) return -EINVAL; - switch (val2) { - case 50000: - new_it = 0x00; - break; - case 100000: - new_it = 0x01; - break; - case 200000: - new_it = 0x02; - break; - case 400000: - new_it = 0x03; - break; - case 800000: - new_it = 0x04; - break; - default: - return -EINVAL; - } + ret = regmap_field_read(data->rf.it, &it_idx); + if (ret) + return ret; - ret = regmap_field_write(data->rf.it, new_it); - if (ret) { - dev_err(data->dev, - "failed to update integration time: %d\n", ret); + ret = regmap_field_read(data->rf.gain, &gain_idx); + if (ret) return ret; - } - return 0; + prev_it = iio_gts_find_int_time_by_sel(&data->gts, it_idx); + if (prev_it < 0) + return prev_it; + + if (prev_it == val2) + return 0; + + prev_gain = iio_gts_find_gain_by_sel(&data->gts, gain_idx); + if (prev_gain < 0) + return prev_gain; + + ret = iio_gts_find_new_gain_by_gain_time_min(&data->gts, prev_gain, prev_it, + val2, &new_gain, &in_range); + if (ret) + return ret; + + if (!in_range) + dev_dbg(data->dev, "Optimal gain out of range\n"); + + ret = iio_gts_find_sel_by_int_time(&data->gts, val2); + if (ret < 0) + return ret; + + ret = regmap_field_write(data->rf.it, ret); + if (ret) + return ret; + + ret = iio_gts_find_sel_by_gain(&data->gts, new_gain); + if (ret < 0) + return ret; + + return regmap_field_write(data->rf.gain, ret); } -static int veml3235_set_gain(struct iio_dev *indio_dev, int val, int val2) +static int veml3235_set_scale(struct iio_dev *indio_dev, int val, int val2) { struct veml3235_data *data = iio_priv(indio_dev); - int ret, new_gain; + int ret, it_idx, gain_sel, time_sel; - if (val2 != 0) - return -EINVAL; + ret = regmap_field_read(data->rf.it, &it_idx); + if (ret) + return ret; - switch (val) { - case 1: - new_gain = 0x00; - break; - case 2: - new_gain = 0x01; - break; - case 4: - new_gain = 0x03; - break; - case 8: - new_gain = 0x07; - break; - default: - return -EINVAL; - } + ret = iio_gts_find_gain_sel_in_times(&data->gts, val, val2, &gain_sel, + &time_sel); + if (ret) + return ret; - ret = regmap_field_write(data->rf.gain, new_gain); - if (ret) { - dev_err(data->dev, "failed to set gain: %d\n", ret); + ret = regmap_field_write(data->rf.it, time_sel); + if (ret) return ret; - } - return 0; + return regmap_field_write(data->rf.gain, gain_sel); } -static int veml3235_get_gain(struct veml3235_data *data, int *val) +static int veml3235_get_scale(struct veml3235_data *data, int *val, int *val2) { - int ret, reg; + int gain, it, reg, ret; ret = regmap_field_read(data->rf.gain, ®); if (ret) { @@ -255,25 +257,25 @@ static int veml3235_get_gain(struct veml3235_data *data, int *val) return ret; } - switch (reg & 0x03) { - case 0: - *val = 1; - break; - case 1: - *val = 2; - break; - case 3: - *val = 4; - break; - default: - return -EINVAL; + gain = iio_gts_find_gain_by_sel(&data->gts, reg); + if (gain < 0) + return gain; + + ret = regmap_field_read(data->rf.it, ®); + if (ret) { + dev_err(data->dev, "failed to read integration time %d\n", ret); + return ret; } - /* Double gain */ - if (reg & 0x04) - *val *= 2; + it = iio_gts_find_int_time_by_sel(&data->gts, reg); + if (it < 0) + return it; - return IIO_VAL_INT; + ret = iio_gts_get_scale(&data->gts, gain, it, val, val2); + if (ret) + return ret; + + return IIO_VAL_INT_PLUS_NANO; } static int veml3235_read_raw(struct iio_dev *indio_dev, @@ -307,7 +309,7 @@ static int veml3235_read_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_INT_TIME: return veml3235_get_it(data, val, val2); case IIO_CHAN_INFO_SCALE: - return veml3235_get_gain(data, val); + return veml3235_get_scale(data, val, val2); default: return -EINVAL; } @@ -318,17 +320,27 @@ static int veml3235_read_avail(struct iio_dev *indio_dev, const int **vals, int *type, int *length, long mask) { + struct veml3235_data *data = iio_priv(indio_dev); + switch (mask) { case IIO_CHAN_INFO_INT_TIME: - *vals = (int *)&veml3235_it_times; - *length = 2 * ARRAY_SIZE(veml3235_it_times); - *type = IIO_VAL_INT_PLUS_MICRO; - return IIO_AVAIL_LIST; + return iio_gts_avail_times(&data->gts, vals, type, length); case IIO_CHAN_INFO_SCALE: - *vals = (int *)&veml3235_scale_vals; - *length = ARRAY_SIZE(veml3235_scale_vals); - *type = IIO_VAL_INT; - return IIO_AVAIL_LIST; + return iio_gts_all_avail_scales(&data->gts, vals, type, length); + default: + return -EINVAL; + } +} + +static int veml3235_write_raw_get_fmt(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_SCALE: + return IIO_VAL_INT_PLUS_NANO; + case IIO_CHAN_INFO_INT_TIME: + return IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; } @@ -342,7 +354,7 @@ static int veml3235_write_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_INT_TIME: return veml3235_set_it(indio_dev, val, val2); case IIO_CHAN_INFO_SCALE: - return veml3235_set_gain(indio_dev, val, val2); + return veml3235_set_scale(indio_dev, val, val2); } return -EINVAL; @@ -402,6 +414,13 @@ static int veml3235_hw_init(struct iio_dev *indio_dev) struct device *dev = data->dev; int ret; + ret = devm_iio_init_iio_gts(data->dev, 0, 272640000, + veml3235_gain_sel, ARRAY_SIZE(veml3235_gain_sel), + veml3235_it_sel, ARRAY_SIZE(veml3235_it_sel), + &data->gts); + if (ret) + return dev_err_probe(data->dev, ret, "failed to init iio gts\n"); + /* Set gain to 1 and integration time to 100 ms */ ret = regmap_field_write(data->rf.gain, 0x00); if (ret) @@ -423,6 +442,7 @@ static const struct iio_info veml3235_info = { .read_raw = veml3235_read_raw, .read_avail = veml3235_read_avail, .write_raw = veml3235_write_raw, + .write_raw_get_fmt = veml3235_write_raw_get_fmt, }; static int veml3235_probe(struct i2c_client *client) @@ -524,3 +544,4 @@ module_i2c_driver(veml3235_driver); MODULE_AUTHOR("Javier Carrasco "); MODULE_DESCRIPTION("VEML3235 Ambient Light Sensor"); MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("IIO_GTS_HELPER");