From patchwork Tue Sep 10 04:50:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13797944 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 1B4451684A6; Tue, 10 Sep 2024 04:51:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943900; cv=none; b=GwcuXwax2r8F/HQ1jpAVKmXd0oh78KkeBhMkxORkuez9zbqKEKujOUz9Tl8xB1u8SxvN8z5Kt11nJchrGVx1qa+zXkDrbVe2ytTH7DnOY6E3r/t5Dhc1Q4VmXdjMV9X3z4F4dyilB5+dOWT1d9VAgA0H3/8CSrFATWV7SvKKHW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943900; c=relaxed/simple; bh=skbp0YrM2RADqkbI/Z5Qxcn6D38PFT221Jr/tgf3w/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oXeDza0NLoc2TQ4M/4AXu6eRCpkAxUIomQDKb3LyZ3OkoapzJ9+v+qFacR4M0o0khmCy97xIzI/fkCejeuRBTrFcSpUY4zJRZ7Abeb4mIhLUEVi/SCitKTBIdtMiIiVqq6Eu+UuxnYjTpWrhUDJLBRuuS5Hy+v+z1AEmnLQwmFs= 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=TxpLCBs2; arc=none smtp.client-ip=209.85.214.169 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="TxpLCBs2" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2059112f0a7so46848035ad.3; Mon, 09 Sep 2024 21:51:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725943898; x=1726548698; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JJtcIGVTfhzjKM3GKhKbnrLKBv2BYLTrCHHS5t8BPkM=; b=TxpLCBs20l276nOwpdxSq+lCZtha16EYSERnu4+XhPfUYKixPJpminwj3jhI8lKypo WWxDDb4BFxcvNCw9X2ayep45sNfa9D5wMLiamUkfhEugOxJt7qeKJvAf03ChzqAw6T+0 rxN2mxAuxsgHz47KIWc37Qmve1ugAarlAV6Xr/fTeDrPWS64kZFnZhMetg4rkkRX1SbS BqYCY7kiZQaJAClZ+KtDNkDcSEfQcCVZ4kCq+XRvuyTIhcgd7/PRzn19F/V9evQ5lThz PNs2b9mY9D8h465DslAAt0dDR8tziQxRB68JNBVo3eXj1Dl7YuImhkATfgfRYeIuJBSc sWrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725943898; x=1726548698; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JJtcIGVTfhzjKM3GKhKbnrLKBv2BYLTrCHHS5t8BPkM=; b=R7ecXprkPDkKyHL4tf+r5GqaY9vlMlwZj+clFo6WXLAhuHmUCEgZBtw9n4UrttoB9y ooPT8KDNwBJ8uabHajmNAAIynluTioXv7rZPgrDxsFbxxpl1UO7WQn6VGOzvd6gAzUgZ wtP+LWWOV1FfXPJdiPcgCy2TXm8a+0i4raNL7gyBajKjPNlCcVPyggACo8L51z6tjM+D hKy5dTbukKf3Dus3KP6RwB9cWqd369EhdNwkTfvMZ0r3wCY0yoeTUiMdY77zwNnJi/aQ 8KebhQMe6S/fuu62fCww0JmhhUHbOiyYm83XYE/AnKUQhTuHn0uozxepQ59HrM8e1C5M 7Tmw== X-Forwarded-Encrypted: i=1; AJvYcCWdL6JSsf82BYgRjiuSLG7eHQVXC66E9PcRFv3tG4yF7FRzYVNo6Tkwfi4Ronm9IV8oMDEipoPAT8hOALI=@vger.kernel.org X-Gm-Message-State: AOJu0Yyr1LuvXqB/yIIamF6zX8Tj6zs96ffvpgisan6Rrbr+L43kQ9yx OzpK0AiNC8pr5Mvm+CILtDnfX3s4a4itiHNNqYd9/I6xxsNx6cpj4t20AsVVngk= X-Google-Smtp-Source: AGHT+IFMeIirR37eFjiaSRYN3Bz3jdqtWYTJT/Mk36/BPEUU25Jqo9V2Gy2AfqlJNfyeC1oTrAtcCg== X-Received: by 2002:a17:903:22d2:b0:205:5427:2231 with SMTP id d9443c01a7336-20706ffde4cmr134009155ad.47.1725943897747; Mon, 09 Sep 2024 21:51:37 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20710ee70a1sm40882965ad.121.2024.09.09.21.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 21:51:36 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: anshulusr@gmail.com, jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 1/4] iio: light: ltr390: Added configurable sampling frequency support Date: Tue, 10 Sep 2024 10:20:26 +0530 Message-ID: <20240910045030.266946-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240910045030.266946-1-abhashkumarjha123@gmail.com> References: <20240910045030.266946-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Provied configurable sampling frequency(Measurement rate) support. Also exposed the available sampling frequency values using read_avail callback. Signed-off-by: Abhash Jha --- drivers/iio/light/ltr390.c | 68 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index 7e58b50f3..73ef4a5a0 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -39,6 +39,7 @@ #define LTR390_PART_NUMBER_ID 0xb #define LTR390_ALS_UVS_GAIN_MASK 0x07 +#define LTR390_ALS_UVS_MEAS_RATE_MASK 0x07 #define LTR390_ALS_UVS_INT_TIME_MASK 0x70 #define LTR390_ALS_UVS_INT_TIME(x) FIELD_PREP(LTR390_ALS_UVS_INT_TIME_MASK, (x)) @@ -87,6 +88,18 @@ static const struct regmap_config ltr390_regmap_config = { .val_bits = 8, }; +/* Sampling frequency is in mili Hz and mili Seconds */ +static const int ltr390_samp_freq_table[][2] = { + [0] = {40000, 25}, + [1] = {20000, 50}, + [2] = {10000, 100}, + [3] = {5000, 200}, + [4] = {2000, 500}, + [5] = {1000, 1000}, + [6] = {500, 2000}, + [7] = {500, 2000} +}; + static int ltr390_register_read(struct ltr390_data *data, u8 register_address) { struct device *dev = &data->client->dev; @@ -135,6 +148,18 @@ static int ltr390_counts_per_uvi(struct ltr390_data *data) return DIV_ROUND_CLOSEST(23 * data->gain * data->int_time_us, 10 * orig_gain * orig_int_time); } +static int ltr390_get_samp_freq(struct ltr390_data *data) +{ + int ret, value; + + ret = regmap_read(data->regmap, LTR390_ALS_UVS_MEAS_RATE, &value); + if (ret < 0) + return ret; + value &= LTR390_ALS_UVS_MEAS_RATE_MASK; + + return ltr390_samp_freq_table[value][0]; +} + static int ltr390_read_raw(struct iio_dev *iio_device, struct iio_chan_spec const *chan, int *val, int *val2, long mask) @@ -191,6 +216,10 @@ static int ltr390_read_raw(struct iio_dev *iio_device, *val = data->int_time_us; return IIO_VAL_INT; + case IIO_CHAN_INFO_SAMP_FREQ: + *val = ltr390_get_samp_freq(data); + return IIO_VAL_INT; + default: return -EINVAL; } @@ -199,6 +228,7 @@ static int ltr390_read_raw(struct iio_dev *iio_device, /* integration time in us */ static const int ltr390_int_time_map_us[] = { 400000, 200000, 100000, 50000, 25000, 12500 }; static const int ltr390_gain_map[] = { 1, 3, 6, 9, 18 }; +static const int ltr390_freq_map[] = { 40000, 20000, 10000, 5000, 2000, 1000, 500, 500 }; static const struct iio_chan_spec ltr390_channels[] = { /* UV sensor */ @@ -206,16 +236,18 @@ static const struct iio_chan_spec ltr390_channels[] = { .type = IIO_UVINDEX, .scan_index = 0, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), - .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SAMP_FREQ), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) + | BIT(IIO_CHAN_INFO_SAMP_FREQ) }, /* ALS sensor */ { .type = IIO_LIGHT, .scan_index = 1, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), - .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SAMP_FREQ), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) + | BIT(IIO_CHAN_INFO_SAMP_FREQ) }, }; @@ -264,6 +296,27 @@ static int ltr390_set_int_time(struct ltr390_data *data, int val) return -EINVAL; } +static int ltr390_set_samp_freq(struct ltr390_data *data, int val) +{ + int ret, idx; + + for (idx = 0; idx < ARRAY_SIZE(ltr390_samp_freq_table); idx++) { + if (ltr390_samp_freq_table[idx][0] != val) + continue; + + guard(mutex)(&data->lock); + ret = regmap_update_bits(data->regmap, + LTR390_ALS_UVS_MEAS_RATE, + LTR390_ALS_UVS_MEAS_RATE_MASK, idx); + if (ret) + return ret; + + return 0; + } + + return -EINVAL; +} + static int ltr390_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, const int **vals, int *type, int *length, long mask) { @@ -278,6 +331,11 @@ static int ltr390_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec con *type = IIO_VAL_INT; *vals = ltr390_int_time_map_us; return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_SAMP_FREQ: + *length = ARRAY_SIZE(ltr390_freq_map); + *type = IIO_VAL_INT; + *vals = ltr390_freq_map; + return IIO_AVAIL_LIST; default: return -EINVAL; } @@ -301,6 +359,12 @@ static int ltr390_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec cons return ltr390_set_int_time(data, val); + case IIO_CHAN_INFO_SAMP_FREQ: + if (val2 != 0) + return -EINVAL; + + return ltr390_set_samp_freq(data, val); + default: return -EINVAL; } From patchwork Tue Sep 10 04:50:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13797945 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 67CA0170A18; Tue, 10 Sep 2024 04:51:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943903; cv=none; b=b2Bd/+ZMnm49ADG8lsMXnUbvtvF53B/W92hOEAJPC42DJs3jaukGewEL3zhst9c9xlZAdYTmA6YkcsgUL+ncGXNNYUK6I1zDAK+I78KD8HNPbj3VZkpf32gk5cdG8nskYe8ppVDLwIbToznLvrK4l6IPzGcBIrb1+GaDSiqgWmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943903; c=relaxed/simple; bh=ZY51vahykKTtxNfMnqXqfEr+7j9AXVcEUZUVgG8ugUw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FC/oP7BzICXZhyF0Dh5BhuiR6aA9Ik1FC0t9HdlB9IaDVp4FWNbuzG9/e3R3mWaEWyW2vtJVWMrkHTjo+P9bIvoo9QJel/5tfSvOGhThdcI8wc2s520XSAcLkz3tNL8PMzCYz/eowEllnvowPMK+tUoCUBDwZz/3VMEqINZzU+0= 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=jeDn4jKP; arc=none smtp.client-ip=209.85.214.172 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="jeDn4jKP" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-207397d1000so10768905ad.0; Mon, 09 Sep 2024 21:51:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725943901; x=1726548701; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kq1aRor6Vaho4Co3OEdp8Mb4+CwARpFfzv0gXPyBQ1k=; b=jeDn4jKP9HWYva4/lA0ilQj/HjRNLvj6Z0QRMArKHA2Mqp6RJmOzzjjdKxiAXdouP3 7fd9L31o9OSDWbiXYqc5RHkI5Fa2aq2323nEllgQKCzCcHUgQWjUbygqj+Lmji5db/GC p2h0UjpzTzUsLuy2HYTvX9sEJ4JL7FuFRDw2pR/Y1ZysZ/ry/zqL1bJE8ru55b4aHjnT rW4PV1gR4hlmYgHy3MiwxpcskRvODzCiKx59FFWcstF/nuEwb8zBS6OjoR59RlNnyBTs oJkccVRsFycFlrke20uUiOyKbKQlvmNLuA0tN67KaZVvmw1nninL1HVHvpmgSLUmeyC2 5YvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725943901; x=1726548701; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kq1aRor6Vaho4Co3OEdp8Mb4+CwARpFfzv0gXPyBQ1k=; b=gF8Q+ZjMGCBb1Cwt/ZrnTG4oYLnq4CdbRNUicVZNnfBh4ChAL1JGjS3ZusZSfEiE3B 5y2lVDV/LYJW6rIzYa1XHrPgfLGjyF6XJDOsUIJe+wNrvg5mHRIKgy/dYH7PSmJel9Xn qF46n86vEkTGCiEmw3kd8ECu6IhQjk9vIpffsgBryW/h5xvp/ZWBB1VZpjAFwejkVdmi ZdAj2bA3Bx9a4nJwS+hs254VuvXj5wuLy0YlsfAK57E/TlaosIbpzlodL9jxtTWV3QoK 3xAEYOc+MWMBAIQ8Tv6DxkFJaGTuV5m7Chbtvzoc5gZ+OXnwditwTQ+zDxEOzdWjrOqX j6LA== X-Forwarded-Encrypted: i=1; AJvYcCVSMoWiYa5okSGxqReSPwXAr+vb3n3CXylLlakI6VcqdOyX8hiMbff9w8LPXS3bG4VPlu/Kv2Qfb/XzFEw=@vger.kernel.org X-Gm-Message-State: AOJu0YxqrdLb7XVzbuaha6AHdNiiDaYmjagIPR+BZ7Rqfsgbp+3tbYBD Vhbz/7QN+7KANe52P7KSrpGuj5xI+XQOJgpRu3i2xIi4mUo/449Ud85strrhIN4= X-Google-Smtp-Source: AGHT+IEZFwA12SIFm4/ZJZb50Z3cGWmZmxx61tP6cOq6CPke2qTl4zvd13Cb4Nu06pNhKGdVa2HUWw== X-Received: by 2002:a17:902:ea01:b0:207:1845:bc48 with SMTP id d9443c01a7336-20743c55a5dmr24473455ad.30.1725943901018; Mon, 09 Sep 2024 21:51:41 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20710ee70a1sm40882965ad.121.2024.09.09.21.51.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 21:51:40 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: anshulusr@gmail.com, jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 2/4] iio: light: ltr390: Suspend and Resume support Date: Tue, 10 Sep 2024 10:20:27 +0530 Message-ID: <20240910045030.266946-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240910045030.266946-1-abhashkumarjha123@gmail.com> References: <20240910045030.266946-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Added support for suspend and resume PM ops. We suspend the sensor by clearing the ALS_UVS_EN bit in the MAIN CONTROL register. And we resume it by setting that bit. Signed-off-by: Abhash Jha --- drivers/iio/light/ltr390.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index 73ef4a5a0..c4ff26d68 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -432,6 +432,24 @@ static int ltr390_probe(struct i2c_client *client) return devm_iio_device_register(dev, indio_dev); } +static int ltr390_suspend(struct device *dev) +{ + struct ltr390_data *data = iio_priv(i2c_get_clientdata( + to_i2c_client(dev))); + + return regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_SENSOR_ENABLE); +} + +static int ltr390_resume(struct device *dev) +{ + struct ltr390_data *data = iio_priv(i2c_get_clientdata( + to_i2c_client(dev))); + + return regmap_set_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_SENSOR_ENABLE); +} + +static DEFINE_SIMPLE_DEV_PM_OPS(ltr390_pm_ops, ltr390_suspend, ltr390_resume); + static const struct i2c_device_id ltr390_id[] = { { "ltr390" }, { /* Sentinel */ } @@ -448,6 +466,7 @@ static struct i2c_driver ltr390_driver = { .driver = { .name = "ltr390", .of_match_table = ltr390_of_table, + .pm = pm_sleep_ptr(<r390_pm_ops), }, .probe = ltr390_probe, .id_table = ltr390_id, From patchwork Tue Sep 10 04:50:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13797946 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 B040F17ADF1; Tue, 10 Sep 2024 04:51:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943907; cv=none; b=ElYuvBG8627uR0JsmGve9ihjTjhEwHlu395rt4XuqZGKvfZXVu3P/zd9AOCPr4VgJm4SHcn0wcUiT1EPGhURAdXouwpoIYAIdf0ceL+V6073IBNm4T274LJJpW82qoUACBiRNqlJuNA+pJ2Q3P15W5eP4AR53IYbH9TOvTCE+SQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943907; c=relaxed/simple; bh=v37FDpFE36W7mV9epj6fFeV3Ik5HTRhfwDrvQQ8bovg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jsWGSUTY313BfXNph81mWSdpvBZQEB6MdDqpxUiUfj4xS0Jf1oDFJxqkiNXu3nVEpkSEGZmSGHQJVcsalxmi0JA/ZfgfwjwC0PSb3UHjoTqhkCzKA5Ph1cjLBUrkFmPRBAceugzCaLdQnK5L2c0Zjng43n+s07QmNTJB3K5BIYA= 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=njBlvBz9; arc=none smtp.client-ip=209.85.214.171 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="njBlvBz9" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-205722ba00cso43262005ad.0; Mon, 09 Sep 2024 21:51:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725943904; x=1726548704; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=83ljQ5eji9mYeH6V/mqjH2Y9avWtITn03WqxwrNIYP0=; b=njBlvBz9FnGU//cFCbIeUm2RYLS9Ww4cEXYCZBAuQIb3IctHSnL/lMxfg5Q3FrazWP DlA+QCFZ8kyQ8LOlsmyK0xUiTbp7+QOCoSOSfVBMBEPp7MdgesRFquk67FY+lJF/CAmm jRXM2XWulpGrVVxCS2GtcBA7EYCn9JEzSk1UJfvmPLHaWeFfaosScvA0xaIeyNMCL6mD UoAg/Pxm3bhtMod4BHtoMcZVtuOTxMbudQWDge5fcpFyuRq+ZapXymavH4olUQmPWnvp ihprzYVMYlCTg5QoUyIanBtkO0E+Rs4RqWDbmDqx0wa/CFr+QF6CSRD37oH7OaH1pLER fFzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725943904; x=1726548704; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=83ljQ5eji9mYeH6V/mqjH2Y9avWtITn03WqxwrNIYP0=; b=SfvLaRmoi8iNYteWu1t9js3zRndKfb5GKMDYOogayuzS1cHbf05+FSE93dCEHwaIiO 7Xl1YMGYAIulgKeGd346Xkw8G1QENP5jPr9sTT3vdo+uJg/0C3QXiSvBydL9UOfYUaI5 /1+3hMXKoRVg8lAcJjX1ZdOJGE1e3Et3GeiDbVHqQZ/MmRVWuqeD0T1hC1Kow85/y0pf HEzs75894Nv34HwA5OBy/5NDMMTWwQsFoTPyK361l88Zc6ITtfjAED59gvJnZHqU2WNj 9vDvgsFCgjzkqmwMjs0vJJcG5GNq7M+PIO/978cSoR8qz5IKeZtalKc82yofi8FYbQoo IBOA== X-Forwarded-Encrypted: i=1; AJvYcCX0lut9CCjGurvMf+MDJ0PNjSV53hqs8pha+IijwPZ+EM9d2saQcJ9WVzhVUdeLEfv0jRxU5QJZvZUtIho=@vger.kernel.org X-Gm-Message-State: AOJu0YywxQ/hWb/RKX675KpMVzodJG096XOIFnnvE9+GcPd1eqjChSVQ 4U1lS9Z9bQxWB9As1kNF5yTyoNeGyGA67kStI2QNfjtmqOQ4t3cKyIR8KmlT7DY= X-Google-Smtp-Source: AGHT+IHeuf2Mxslu+aWTb4XZuPj5vK22w7vFS//6vTu/v0+wLh1htz/3bWFdvQpR43bXi6SdVZQIaw== X-Received: by 2002:a17:902:fc43:b0:205:42cf:2226 with SMTP id d9443c01a7336-206f064fdfbmr158818685ad.55.1725943904239; Mon, 09 Sep 2024 21:51:44 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20710ee70a1sm40882965ad.121.2024.09.09.21.51.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 21:51:43 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: anshulusr@gmail.com, jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 3/4] iio: light: ltr390: Interrupts and threshold event support Date: Tue, 10 Sep 2024 10:20:28 +0530 Message-ID: <20240910045030.266946-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240910045030.266946-1-abhashkumarjha123@gmail.com> References: <20240910045030.266946-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Added support for threshold events for both the ALS and UVI channels. The events are reported when the threshold interrupt is triggered. Both rising and falling threshold types are supported. Signed-off-by: Abhash Jha --- drivers/iio/light/ltr390.c | 222 ++++++++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index c4ff26d68..8a8a118ca 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -24,8 +24,11 @@ #include #include #include +#include +#include #include +#include #include @@ -33,9 +36,12 @@ #define LTR390_ALS_UVS_MEAS_RATE 0x04 #define LTR390_ALS_UVS_GAIN 0x05 #define LTR390_PART_ID 0x06 +#define LTR390_MAIN_STATUS 0x07 #define LTR390_ALS_DATA 0x0D #define LTR390_UVS_DATA 0x10 #define LTR390_INT_CFG 0x19 +#define LTR390_THRESH_UP 0x21 +#define LTR390_THRESH_LOW 0x24 #define LTR390_PART_NUMBER_ID 0xb #define LTR390_ALS_UVS_GAIN_MASK 0x07 @@ -46,6 +52,8 @@ #define LTR390_SW_RESET BIT(4) #define LTR390_UVS_MODE BIT(3) #define LTR390_SENSOR_ENABLE BIT(1) +#define LTR390_LS_INT_EN BIT(2) +#define LTR390_LS_INT_SEL_UVS BIT(5) #define LTR390_FRACTIONAL_PRECISION 100 @@ -230,6 +238,22 @@ static const int ltr390_int_time_map_us[] = { 400000, 200000, 100000, 50000, 250 static const int ltr390_gain_map[] = { 1, 3, 6, 9, 18 }; static const int ltr390_freq_map[] = { 40000, 20000, 10000, 5000, 2000, 1000, 500, 500 }; +static const struct iio_event_spec ltr390_event_spec[] = { + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_RISING, + .mask_separate = BIT(IIO_EV_INFO_VALUE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_FALLING, + .mask_separate = BIT(IIO_EV_INFO_VALUE), + }, { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_separate = BIT(IIO_EV_INFO_ENABLE), + } +}; + static const struct iio_chan_spec ltr390_channels[] = { /* UV sensor */ { @@ -238,7 +262,9 @@ static const struct iio_chan_spec ltr390_channels[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SAMP_FREQ), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) - | BIT(IIO_CHAN_INFO_SAMP_FREQ) + | BIT(IIO_CHAN_INFO_SAMP_FREQ), + .event_spec = ltr390_event_spec, + .num_event_specs = ARRAY_SIZE(ltr390_event_spec), }, /* ALS sensor */ { @@ -247,7 +273,9 @@ static const struct iio_chan_spec ltr390_channels[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SAMP_FREQ), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) - | BIT(IIO_CHAN_INFO_SAMP_FREQ) + | BIT(IIO_CHAN_INFO_SAMP_FREQ), + .event_spec = ltr390_event_spec, + .num_event_specs = ARRAY_SIZE(ltr390_event_spec), }, }; @@ -370,12 +398,186 @@ static int ltr390_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec cons } } +static int ltr390_read_threshold(const struct iio_dev *indio_dev, + enum iio_event_direction dir, + int *val, int *val2) +{ + struct ltr390_data *data = iio_priv(indio_dev); + int ret; + + switch (dir) { + case IIO_EV_DIR_RISING: + ret = ltr390_register_read(data, LTR390_THRESH_UP); + if (ret < 0) + return ret; + *val = ret; + return IIO_VAL_INT; + + case IIO_EV_DIR_FALLING: + ret = ltr390_register_read(data, LTR390_THRESH_LOW); + if (ret < 0) + return ret; + *val = ret; + return IIO_VAL_INT; + default: + return -EINVAL; + } +} + +static int ltr390_write_threshold(struct iio_dev *indio_dev, + enum iio_event_direction dir, + int val, int val2) +{ + struct ltr390_data *data = iio_priv(indio_dev); + int ret; + + guard(mutex)(&data->lock); + switch (dir) { + case IIO_EV_DIR_RISING: + ret = regmap_bulk_write(data->regmap, + LTR390_THRESH_UP, + &val, 3); + return ret; + case IIO_EV_DIR_FALLING: + ret = regmap_bulk_write(data->regmap, + LTR390_THRESH_LOW, + &val, 3); + return ret; + default: + return -EINVAL; + } +} + +static int ltr390_read_event_value(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int *val, int *val2) +{ + switch (info) { + case IIO_EV_INFO_VALUE: + return ltr390_read_threshold(indio_dev, dir, val, val2); + default: + return -EINVAL; + } +} + +static int ltr390_write_event_value(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int val, int val2) +{ + switch (info) { + case IIO_EV_INFO_VALUE: + if (val2 != 0) + return -EINVAL; + + return ltr390_write_threshold(indio_dev, dir, val, val2); + default: + return -EINVAL; + } +} + +static int ltr390_read_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir) +{ + struct ltr390_data *data = iio_priv(indio_dev); + int ret, status; + + ret = regmap_read(data->regmap, LTR390_INT_CFG, &status); + if (ret < 0) + return ret; + + return status & LTR390_LS_INT_EN; +} + +static int ltr390_write_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + int state) +{ + struct ltr390_data *data = iio_priv(indio_dev); + int ret; + + if (state != 1 && state != 0) + return -EINVAL; + + if (state == 0) + return regmap_clear_bits(data->regmap, LTR390_INT_CFG, LTR390_LS_INT_EN); + + guard(mutex)(&data->lock); + ret = regmap_set_bits(data->regmap, LTR390_INT_CFG, LTR390_LS_INT_EN); + if (ret < 0) + return ret; + + switch (chan->type) { + case IIO_LIGHT: + ret = ltr390_set_mode(data, LTR390_SET_ALS_MODE); + if (ret < 0) + return ret; + + return regmap_clear_bits(data->regmap, LTR390_INT_CFG, LTR390_LS_INT_SEL_UVS); + + case IIO_UVINDEX: + ret = ltr390_set_mode(data, LTR390_SET_UVS_MODE); + if (ret < 0) + return ret; + + return regmap_set_bits(data->regmap, LTR390_INT_CFG, LTR390_LS_INT_SEL_UVS); + + default: + return -EINVAL; + } +} + static const struct iio_info ltr390_info = { .read_raw = ltr390_read_raw, .write_raw = ltr390_write_raw, .read_avail = ltr390_read_avail, + .read_event_value = ltr390_read_event_value, + .read_event_config = ltr390_read_event_config, + .write_event_value = ltr390_write_event_value, + .write_event_config = ltr390_write_event_config, }; +static irqreturn_t ltr390_interrupt_handler(int irq, void *private) +{ + struct iio_dev *indio_dev = private; + struct ltr390_data *data = iio_priv(indio_dev); + int ret, status; + + /* Reading the status register to clear the interrupt flag, Datasheet pg: 17*/ + ret = regmap_read(data->regmap, LTR390_MAIN_STATUS, &status); + if (ret < 0) + return ret; + + switch (data->mode) { + case LTR390_SET_ALS_MODE: + iio_push_event(indio_dev, + IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0, + IIO_EV_TYPE_THRESH, + IIO_EV_DIR_EITHER), + iio_get_time_ns(indio_dev)); + break; + + case LTR390_SET_UVS_MODE: + iio_push_event(indio_dev, + IIO_UNMOD_EVENT_CODE(IIO_UVINDEX, 0, + IIO_EV_TYPE_THRESH, + IIO_EV_DIR_EITHER), + iio_get_time_ns(indio_dev)); + break; + } + + return IRQ_HANDLED; +} + static int ltr390_probe(struct i2c_client *client) { struct ltr390_data *data; @@ -429,6 +631,22 @@ static int ltr390_probe(struct i2c_client *client) if (ret) return dev_err_probe(dev, ret, "failed to enable the sensor\n"); + if (client->irq) { + int irq_flags = irq_get_trigger_type(client->irq); + + if (!irq_flags) + irq_flags = IRQF_TRIGGER_FALLING; + + ret = devm_request_threaded_irq(&client->dev, client->irq, + NULL, ltr390_interrupt_handler, + irq_flags | IRQF_ONESHOT, + "ltr390_thresh_event", indio_dev); + if (ret) { + dev_err(&client->dev, "request irq (%d) failed\n", client->irq); + return ret; + } + } + return devm_iio_device_register(dev, indio_dev); } From patchwork Tue Sep 10 04:50:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13797947 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 6709917DE15; Tue, 10 Sep 2024 04:51:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943910; cv=none; b=Kz0NFahtz4mJsxz+EoqVALn05ChsxbZqbpJPA54YAdCqumSKp6IZ4W2E+46mWMf4G3rEMc3SMeevUPZrBNLV91r6hHrVXcDRi51Sh9cK6ll4vqjF8BalRaOrmPZVZ02aleNwVYpCj1u1LIR8ZTVC8UD2v4yLMKTu0X3pkfncRrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725943910; c=relaxed/simple; bh=VOuv3djESEvw7P505CzZyiQP2j1nSX/lht7pwBok8vk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HE7agSd+yar1fCKa24w7xK6R7ls+UKa5pTxNbf5WcpjgPHWZJkQv6W1MjLq8S1SN9/XG0GjkueLcnWrPF0HHBR14BQopoGibWH4ip4T1iTFnEJNQbGANMfAmpWk+2yQPL7MidVgcOOMbH3nDXa9TlnJZlqN6vVBH54vRMz0Ylbw= 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=NQZ8NwgX; arc=none smtp.client-ip=209.85.214.182 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="NQZ8NwgX" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2055f630934so2961005ad.1; Mon, 09 Sep 2024 21:51:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725943908; x=1726548708; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wlOPJ/ylC24BWfeRWAwVgQ++41LRYuaxkLK4GT7ljqY=; b=NQZ8NwgXHAAYPncNCxTC/97wwg7HPJc3UBMCcUP5R8TvwRoSCujM6xBgSMgXneSbLj acxNxVaOF147vAyJut1WuCG8aVYOXQv0TDQkFlQ9n9O9r6qG40HDTuohbvbgvKNfC3KD BEgFwG17HxPgT+hnp3zGDMMD8ZZBg5pN6zDeUUz6e+NDMe/WCqhFGRgKmbmz06X3gC13 opkuCVtZ1gGFN8POPRWjYIqFu7c5ajmUV4nHDTfnwf3/3sYyT865izQvo67irlwZp8t3 JXugpzCA31tNEouz4rdsvzOqBTziD6zi0ZT0ECtYgnBjpZord5lJEZ6ya4Dr/zfpEbfF BGBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725943908; x=1726548708; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wlOPJ/ylC24BWfeRWAwVgQ++41LRYuaxkLK4GT7ljqY=; b=oVxqW7XOysDkE8rmTO6n2OSQavN03boPJjW1mHexj8GCgV7c5GAeWn8USm0mWef/bC 6sKhLXHWwCu4HnDq4lLbMmSeNEdlohyHyyDIE6S9ceZc3908g83Wi8NjAkSNqZ3ccUvo i5KeIgCMnV48EdMoghnXEXYY8p0ciGMIZ64GMKlJOUArKixaU+mUWI7SqDp03SKuvix2 cbqzcpvengoCP69eaXehOc+wBVE4wQxakm+bYp5CoVuFrmAMiUgauwIsUolMdg1F95Nd vdo/PLqvho1XJEn8839GnN6YmIurha4kER1/rMoMSMh0DuXXAZgaTBoJBvcKaU7RDZB6 f0xQ== X-Forwarded-Encrypted: i=1; AJvYcCUaXqitris6FglxDBOFt7qOvJs1bZCbedtkdTzuxkrcOV80DEPXB0uAFdzi9bnIoZLfOvU/sOwnZ3ICWaE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6/AQJws1i3Acy+cds693OZXMZ7zcHszTmi9yf2gEv2gZzM00a XIKZ0t2ohLwKsfcHDFqQ6TkEifGKGSM6Slbjf6m+7TGBQUCU/dG2aQW8rAkIjZE= X-Google-Smtp-Source: AGHT+IHhKFspCEdi70+8WnP9gh8IIk4ATkEdd+pWTzRJ2UaZXQXSM8vtl9goJ154SuhXqDa1telKmQ== X-Received: by 2002:a17:902:d4cd:b0:205:826e:6a13 with SMTP id d9443c01a7336-2070a816a6bmr114706035ad.54.1725943907470; Mon, 09 Sep 2024 21:51:47 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20710ee70a1sm40882965ad.121.2024.09.09.21.51.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 21:51:46 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: anshulusr@gmail.com, jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 4/4] iio: light: ltr390: Add interrupt persistance support Date: Tue, 10 Sep 2024 10:20:29 +0530 Message-ID: <20240910045030.266946-5-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240910045030.266946-1-abhashkumarjha123@gmail.com> References: <20240910045030.266946-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Added support to configure the threshold interrupt persistance value by providing IIO_EV_INFO_PERIOD attribute. The value written to the attribute should be in miliseconds and should be greater than the sampling rate of the sensor. Signed-off-by: Abhash Jha --- drivers/iio/light/ltr390.c | 66 +++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index 8a8a118ca..9706105c6 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -40,6 +40,7 @@ #define LTR390_ALS_DATA 0x0D #define LTR390_UVS_DATA 0x10 #define LTR390_INT_CFG 0x19 +#define LTR390_INT_PST 0x1A #define LTR390_THRESH_UP 0x21 #define LTR390_THRESH_LOW 0x24 @@ -48,6 +49,8 @@ #define LTR390_ALS_UVS_MEAS_RATE_MASK 0x07 #define LTR390_ALS_UVS_INT_TIME_MASK 0x70 #define LTR390_ALS_UVS_INT_TIME(x) FIELD_PREP(LTR390_ALS_UVS_INT_TIME_MASK, (x)) +#define LTR390_INT_PST_MASK 0xF0 +#define LTR390_INT_PST_VAL(x) FIELD_PREP(LTR390_INT_PST_MASK, (x)) #define LTR390_SW_RESET BIT(4) #define LTR390_UVS_MODE BIT(3) @@ -79,6 +82,11 @@ enum ltr390_mode { LTR390_SET_UVS_MODE, }; +enum ltr390_meas_rate { + LTR390_GET_FREQ, + LTR390_GET_PERIOD, +}; + struct ltr390_data { struct regmap *regmap; struct i2c_client *client; @@ -156,7 +164,7 @@ static int ltr390_counts_per_uvi(struct ltr390_data *data) return DIV_ROUND_CLOSEST(23 * data->gain * data->int_time_us, 10 * orig_gain * orig_int_time); } -static int ltr390_get_samp_freq(struct ltr390_data *data) +static int ltr390_get_samp_freq_or_period(struct ltr390_data *data, enum ltr390_meas_rate option) { int ret, value; @@ -165,7 +173,7 @@ static int ltr390_get_samp_freq(struct ltr390_data *data) return ret; value &= LTR390_ALS_UVS_MEAS_RATE_MASK; - return ltr390_samp_freq_table[value][0]; + return ltr390_samp_freq_table[value][option]; } static int ltr390_read_raw(struct iio_dev *iio_device, @@ -225,7 +233,7 @@ static int ltr390_read_raw(struct iio_dev *iio_device, return IIO_VAL_INT; case IIO_CHAN_INFO_SAMP_FREQ: - *val = ltr390_get_samp_freq(data); + *val = ltr390_get_samp_freq_or_period(data, LTR390_GET_FREQ); return IIO_VAL_INT; default: @@ -250,7 +258,8 @@ static const struct iio_event_spec ltr390_event_spec[] = { }, { .type = IIO_EV_TYPE_THRESH, .dir = IIO_EV_DIR_EITHER, - .mask_separate = BIT(IIO_EV_INFO_ENABLE), + .mask_separate = BIT(IIO_EV_INFO_ENABLE) | + BIT(IIO_EV_INFO_PERIOD), } }; @@ -398,6 +407,44 @@ static int ltr390_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec cons } } +static int ltr390_read_intr_prst(struct ltr390_data *data, int *val) +{ + int ret, prst, samp_period; + + samp_period = ltr390_get_samp_freq_or_period(data, LTR390_GET_PERIOD); + ret = regmap_read(data->regmap, LTR390_INT_PST, &prst); + if (ret < 0) + return ret; + *val = prst * samp_period; + + return IIO_VAL_INT; +} + +static int ltr390_write_intr_prst(struct ltr390_data *data, int val) +{ + int ret, samp_period, new_val; + + samp_period = ltr390_get_samp_freq_or_period(data, LTR390_GET_PERIOD); + + /* persist period should be greater than or equal to samp period */ + if (val < samp_period) + return -EINVAL; + + new_val = DIV_ROUND_UP(val, samp_period); + if (new_val < 0 || new_val > 0x0f) + return -EINVAL; + + guard(mutex)(&data->lock); + ret = regmap_update_bits(data->regmap, + LTR390_INT_PST, + LTR390_INT_PST_MASK, + LTR390_INT_PST_VAL(new_val)); + if (ret) + return ret; + + return 0; +} + static int ltr390_read_threshold(const struct iio_dev *indio_dev, enum iio_event_direction dir, int *val, int *val2) @@ -458,6 +505,10 @@ static int ltr390_read_event_value(struct iio_dev *indio_dev, switch (info) { case IIO_EV_INFO_VALUE: return ltr390_read_threshold(indio_dev, dir, val, val2); + + case IIO_EV_INFO_PERIOD: + return ltr390_read_intr_prst(iio_priv(indio_dev), val); + default: return -EINVAL; } @@ -476,6 +527,13 @@ static int ltr390_write_event_value(struct iio_dev *indio_dev, return -EINVAL; return ltr390_write_threshold(indio_dev, dir, val, val2); + + case IIO_EV_INFO_PERIOD: + if (val2 != 0) + return -EINVAL; + + return ltr390_write_intr_prst(iio_priv(indio_dev), val); + default: return -EINVAL; }