From patchwork Mon Sep 9 10:36:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13796613 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (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 BA7C65733A; Mon, 9 Sep 2024 10:37:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878242; cv=none; b=gX0RLVHVvqBC5IDUKLG+PjAqNFn+oLJPok/6XnqCd2mIu5uAeTh/D4/gyApGyazsQIShXwBC4MsSKg1TJieNPwHKUovXggpKetsUnLummFiPbcV59dUUbVLNzKh7OwChU4aSffgWXFmRZVVSC3WYBZWaw8PuMzl4aa4TOY93pvk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878242; c=relaxed/simple; bh=skbp0YrM2RADqkbI/Z5Qxcn6D38PFT221Jr/tgf3w/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DxRtom8SFZdhjl3CKav7uXw7KgNnM24HPTDgJpGczwpxlJjX86cW4nP3xPjDkVtv5B1kMusR9gbITfNnPY/8SD+274yBGhIKldaV0LYz0okKM1jzpqSEJI72ai6SWqJEPLkRbK75j2Hp5nmRLrY8UK9oZz//VoabBUFK0EadQvo= 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=UKN809dq; arc=none smtp.client-ip=209.85.210.49 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="UKN809dq" Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-710da168ea0so1251972a34.0; Mon, 09 Sep 2024 03:37:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725878239; x=1726483039; 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=UKN809dqLDejSjyAWzsbrK2pxvzbN2E8gvZv6O80QoH+pcNxdWLH3JBy8lxVxblda8 CRpWkYKwqeGCQhR+UtHB3etJe9CpVma7CDDFvqi4SvePMGMD8SY39009FB7qDAkgBXvY LSYxMjCdywVYHWOo60BJFw3ztrKWTGpoYCv/xYMR008Ic7+eHsd5ZFxXfTDqRy1P1iok mJlj7g0v7PQrKUz78vOkWcINHdZoIzkY4eUVmwaOBTwvhT8i1lA50qFC57iBfPIHDxzH qIqzGfKHNwHNok7xUoWEpkuJmHCKVmvxv60XhBlcDrktqVizXUNam1Ge6UgZAfWKbnHs GHoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725878239; x=1726483039; 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=hnAK+TusQVlMDnIpEQAVPfDCa/eDJwT3MNghYlEDV8fF+tUNWmoNCvI8l20RujFL4D SJkkwrDFtGyd1rRlQ7hxkoQ1+ezX4sQU90lyaGCExIhLoDBht55A2VY1PmGUN8Chb2FO x0518aId4tySPfB7bfPuXttR0lBecYJ+9Z6NXcQ0XJQoDxHk9Hwfovxw07xFol7qPuqP H92KK1xwukmBRdME4raEzSV3rAVbqzpsXbz9Q6vF/itt5moGxevA5eUWXHQgQSfz3Nic BII25FEyCpDfcEERHpX8jfZC6cHFvQB/Oi1Ag0Np0aeBLNrMif46ZQeMVIm6fYToIWfx FfLg== X-Forwarded-Encrypted: i=1; AJvYcCUalMKCWSGXiSAkeYx1y4mivaWpa6YnGdBOYhDdmrmmJnkJIiUGCgwMuXXBZTr9T4ivoLlPFmHb0c0o5P0=@vger.kernel.org X-Gm-Message-State: AOJu0YxSdoswKmQfo447749FkSk92rcMIop8qUOdx++uwORSYQpI0/jC rM7wc41Sq1rLe65gg1pCx1pCxpOvFyEizPOhovti1ind/xiF9b2G4hyZUY4RhEw= X-Google-Smtp-Source: AGHT+IG0oQmaGBjgLMpWGuse9NYxwzolnohkDmZnYJsa6vb5JeaSj1RlMwm7IIjUMUbSVAS0OboOUw== X-Received: by 2002:a05:6830:3148:b0:703:783a:dad5 with SMTP id 46e09a7af769-710cc26e969mr9072989a34.27.1725878238708; Mon, 09 Sep 2024 03:37:18 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-718e5897b3fsm3258032b3a.25.2024.09.09.03.37.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:37:17 -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 1/4] iio: light: ltr390: Added configurable sampling frequency support Date: Mon, 9 Sep 2024 16:06:20 +0530 Message-ID: <20240909103623.264113-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240909103623.264113-1-abhashkumarjha123@gmail.com> References: <20240909103623.264113-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 Mon Sep 9 10:36:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13796614 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (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 253451B531E; Mon, 9 Sep 2024 10:37:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878246; cv=none; b=jMrxiw1GaAzd3WamS8hpx27fFBvDdOVGLEdtq/8kb+S9nU0P8wSeoUruBH6H6twlB3bDaCJjKJ6wIk8m2EZ7S0qnqnoIfEvjwzljo3+vngYIezFpzS3RUNsXNCVbXn522xDyOXf8cPWKwrXtWmGwl5H+NJGiud8nbTrntsTSX3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878246; c=relaxed/simple; bh=ZY51vahykKTtxNfMnqXqfEr+7j9AXVcEUZUVgG8ugUw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ulcJyqDOUQOb/Bkn1PUkSUE9UWLMu8kVeUQ3fK4wu4m0Alpzxd6D2BB2AWxI2eWHc22crhNjf85+Pevmo6zfkQ2TOxAvEGlrDir00ZowUm/S76tAIRtjKSbhJC0BcWBRKY2NzW/pEIn4hNTB+ALHf7SDN7R09Injp5yc4L830e0= 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=VqJA9smc; arc=none smtp.client-ip=209.85.160.49 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="VqJA9smc" Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-2689e7a941fso2644959fac.3; Mon, 09 Sep 2024 03:37:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725878243; x=1726483043; 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=VqJA9smcwC/jTdaVOXFLkBJNQRXL3J63TujEKx3/hQn1dVFhKKp8w4Uf0OiowCLPLI iv43dfQfUakBTuRgKnPAhJCbuXWqebwPomYIgOdwg/fhlOrhpNBb6KBSAKMI13sVLnsJ NLbCVY5Rq5PbAtOqdg928bSwZq58cNU6lqEOrmulja9Lqb0X9LH6xbRLVe0gSoS/kYX7 J8W5r6Cuv5x7z6JBFL+lL8DBZODzIIfkz5FSiTQn8S9hoz3ToGqPEli2g7Rhhaj7wQUD WAZ88HLkCdzEXCjVrBhcEqIaPpvGH/0S4MTWQAs/XvevNAi0uk12oZn+h9vRpH6Wo5e8 0fdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725878243; x=1726483043; 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=Sn/r4oZLkrQBHdFO2mOWU6wJEVmM0Ijsmyp7qHOsvTcs8kw9ZHOC1ouhMNCxdEWIZy wxpBPvdAEYaPn1LcwSC9ZhsuUloLQa8S8Chz+4IniPiyPONpUaF/F5wnTnzRGwLa3coW ICjD+mSC+bwHzkIVTQR1rkLM+h+rxumeL3NqQQHqs1igsdojdPv2WVKr1i659DIW1rBK 7mrCPUPWBE94Xp/CIch+gBv2PHlWldEyLQmR5smQe2BPAUzJzEZBcFrlViXhebDaN3Sc QR5HQgMs1bhofV/RButN5PlYSx5duLJSbOAivyEC8a9FojuW8kgL3BrydFBX3R1E/kKf bF8w== X-Forwarded-Encrypted: i=1; AJvYcCUlwRwsuxhR2sTgVOm2PLFAS8qjTncf/HhqXVLbpRbWAGeMgEjPtd/tkG7IyfBzcVIfuGvsXsr4pXe1tqA=@vger.kernel.org X-Gm-Message-State: AOJu0Yyny4oL6O4E91F6b9Xes51rwUjZIpcXp8hGBgz8bnrF7018YxJf zeaC7XEHXQXcVfVyXx6IcEGzspXQSguWLdDXj1El8g6Q5UaKaV9DkIH4awol8X4= X-Google-Smtp-Source: AGHT+IEwTsgkU2Lka+SuMUBV2K2e8TUqEQbAfxHJ+/+tGd3hOlmM1ZXFd31VKISc0Iew1g6pg0iK9Q== X-Received: by 2002:a05:6870:1c8:b0:270:1850:f898 with SMTP id 586e51a60fabf-27b82e1203amr12552164fac.12.1725878243192; Mon, 09 Sep 2024 03:37:23 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-718e5897b3fsm3258032b3a.25.2024.09.09.03.37.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:37:22 -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 2/4] iio: light: ltr390: Suspend and Resume support Date: Mon, 9 Sep 2024 16:06:21 +0530 Message-ID: <20240909103623.264113-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240909103623.264113-1-abhashkumarjha123@gmail.com> References: <20240909103623.264113-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 Mon Sep 9 10:36:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13796615 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 7210C1B580A; Mon, 9 Sep 2024 10:37:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878251; cv=none; b=Jf9DaN4WrcFa0HcqQFsInvygDK4QWSPrxv5m6Z2XsV90UZXuwzndH+IMi1lasmO4jF69oZID6iu9CqNne2p0s5ld91HeH7tChhwoMd2f1Hbx3WWl8CvUA2q6dRD2T5sbiCWg/O3OvA3XVDcoFIaNDqIwwWBNzZLQ2Zr0uf6MUz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878251; c=relaxed/simple; bh=VNG7rvvZia/wCUOSgxtTR2Ji8uix+Z+Sqo/HnwqlhRw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GIgw+1UNvVH+KGEAK9GnitCNnE09Q0/0Oz2nAgmJHcACF2H3KvfmD5fL4eHgNYnzgx4tBt24fwAMSYNKuoXuE+HeoY6XNaJXogtfQOzMHAhY9H7nn5XVPOfMRxXT6bkZNgQN+t0lytNoCp1VcNc4iTiqWWiZIqL1dnlK4ZVGfOo= 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=OD204rO8; arc=none smtp.client-ip=209.85.210.179 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="OD204rO8" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-718a3b8a2dcso2265697b3a.2; Mon, 09 Sep 2024 03:37:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725878248; x=1726483048; 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=cINpB1QE7f8M6u2vyABHpUJsKL9FmJXbrSnf5SAefL4=; b=OD204rO86j67p/XZuXkj8GV2jR7hcNVvG8OkcTb8SDuIy+6VEaKmMAsVsXII04XZs9 aWhbAbDpdZ8H6IWA46libOzv5ZTDdgqARIezpL3HDDqgdq1BglQiljuRBpoIoN9vqlba 5BA0EP6Yks8zivolawY77ML+Pk+ow0tiTLVYOxRt2fCP1XhHhnI1DhWQ5GwtkiUmKJLZ S3nmo4qor7oS1h+8RQj5BoR6eRsE20bVMos6r8zuwoVcg9Y3NyfytRQgC3i2E7El0uec 7GYXhazvnFB6hqGCRW0Cba9J24wujM0V/jUoz3jtGd13GS+4DzDM3+C9uYRVGLxpG989 Nw1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725878248; x=1726483048; 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=cINpB1QE7f8M6u2vyABHpUJsKL9FmJXbrSnf5SAefL4=; b=neIRoixMJ083WJ7BPtGVsYAtMUj+NYwjW0+ryR+yyao3cr9CaJhQOpey6I8axJxTUO P+jJD/eAczTjRqfz8fMEb6wkrTj1EHa7BXnE02F/1X+73zpQzBPjzWNriDiKzttzQL4F TV6Z1nulElBfA5WY7/lYN2IF+Rs+92/YE/fLpnImpz2U7xUACPhSWLHTD/WhbMdj8jj0 h4gyTez2vXqVdL0Y/sErts5RPlpEaQZzRZKXC5jFYrvoBpbsLYaqnXg+6fYZvm4Om9HJ W4VFxz83GPtJk31W1dWRrzr12HBHChntOFTztTvbG4izE/1O2o5wCktx3ckcDBYl4mcx 4ZCQ== X-Forwarded-Encrypted: i=1; AJvYcCVf/Fff8wEh5SvkPJnSPE1VUv3wlCmXlbDAvb/TFg9sOd0SfEqECQY2wAEEH1mScGND2L2JrFndHS5HM6s=@vger.kernel.org X-Gm-Message-State: AOJu0YyecpKg8fvADmep4TIIEhXbCWGim3J2nlCa/sBPM98EY3a1fuBB o8egU3DSBZYEBLn0ODdspbYJHKq1dtUOlcfS2Eox8vJf9n6RLBHWy37joZIRTV0= X-Google-Smtp-Source: AGHT+IFPO4w7m2Z2BarivpeU4GdfUU+wB2NzdzppQzrTv6AH443WYXXZuWXSeRgyV96rXsewRbQTFw== X-Received: by 2002:a05:6a00:190d:b0:718:d94b:4b with SMTP id d2e1a72fcca58-718d94b0149mr9276003b3a.6.1725878247635; Mon, 09 Sep 2024 03:37:27 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-718e5897b3fsm3258032b3a.25.2024.09.09.03.37.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:37:26 -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 3/4] iio: light: ltr390: Interrupts and threshold event support Date: Mon, 9 Sep 2024 16:06:22 +0530 Message-ID: <20240909103623.264113-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240909103623.264113-1-abhashkumarjha123@gmail.com> References: <20240909103623.264113-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 | 220 ++++++++++++++++++++++++++++++++++++- 1 file changed, 218 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index c4ff26d68..c707a4871 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -26,6 +26,7 @@ #include #include +#include #include @@ -33,9 +34,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 +50,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 +236,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 +260,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 +271,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 +396,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 +629,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 Mon Sep 9 10:36:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13796616 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 ED3191B4C4A; Mon, 9 Sep 2024 10:37:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878254; cv=none; b=h9PQ2dWZ6fdVWPCuTFR8fOiqYhUxGemv/1Thl+PcvtAEqiuHSuwMThcqMZS1TClMFi8pw5110oZQvukYbS+UnyyBTVgT0zJEmIKaX6wF+HSKmOzod465oMDyLjIdXCruXKurh0MMaGi+yMdk6uPDCdfAPLWcz29Eb92Kv/BWpb4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725878254; c=relaxed/simple; bh=dpIzHbvRu28bi9o3PmHvgdJ8jCcqO43pFnJDruwQtpM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PuAEZ2zfaDgBRnkZp6jpKsxAtY3eOwUC874Iy20arj/tGtucxC2y4L9NNNuZIGcKcZuFr/d08lAY01JBpxbvBFtDfAt03a2EPyLp5nvGWX/mIz2GoiuptRN1nsikPotPgBSDJGvukNnAB9yolz0P9gIq2eUpXq8cRbjWiFXz/cM= 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=CRhbe3u+; arc=none smtp.client-ip=209.85.210.181 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="CRhbe3u+" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-718e9c8bd83so823974b3a.1; Mon, 09 Sep 2024 03:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725878252; x=1726483052; 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=dA77S68U5yH+ARFT6rAvuB0lv7jhji9buh4OnSYCZQ0=; b=CRhbe3u+1cCmqhjB/QhBz+inSro0CLeT/NRfZBF8vCgiCJpNoJR/UpHZ9uy3tVb1qa R+iq4QxcRXkFrvzJtWXrjLkYcCRXjsf79wnXzbrPHWsS+irPoTDAfZ9nxvOZduC+jb8g TkF0Nf2EjJ8D0mC60aytXge3iRVPTcPC/8TuuyA/KeCyj2fTS1j7nFOw6NZf/2p9zon9 S0sLdfeyCpFukghDtGBQYbio1Gj9+jRcH2kWuQWLS1go5eRjw22VOkSOoxw26KHLmK5B 4VDuqvJmPji0O/Vfc97xsMlx8TbOzdmtVTUF9LpFpvVYUvbPhOEXiR5loPOqaLWTAf1i GFeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725878252; x=1726483052; 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=dA77S68U5yH+ARFT6rAvuB0lv7jhji9buh4OnSYCZQ0=; b=gkRIUp6lekjFN85WXxO4WA3nXIryqTjlPnDAV6tq36tFRxh+fJlHsuJ1v2j99epRTu VXXFNkFvaPBTJAmGHcCcooMRBrIR9gUzaarjKXzgw9ZFqrWQ5seXMXiI7dU4tOM7UfVI UtLu3xdcUrVYJQc8fNrIQnMZ5+kSo68RPPQrhWn92kHuX4hH30ctwqWbL1VIMbHNa+iB wYWSJxBeBfZFYlgLSRMl+yl5tZ0tdhVcxxlbXsl7jGSeSIHbNYlvPFECJwGBZr89kfhG sMHx1cTTBImfRp4VvvB2GKc2kSFToDa5FCwU07GjUBMtAiUHa1CpMbnam1QvQTmm0Tuy Nq2A== X-Forwarded-Encrypted: i=1; AJvYcCUuKAjL7oojh3SPLewkTdqDYjtTbSW047n5J9ZJ9TCSU9uaU0xaCZznTFAp5MZzR4aJA4YdcpWI6ZekelU=@vger.kernel.org X-Gm-Message-State: AOJu0YzoQ+ggLKx6wom7YRL+yTPjCjgaR0mllH4Sl9p2IjHTzKMXRfHi 3eNtoPmk+B/xBmN3lyAYupgMapKjxBhxdA8qZ85UJx3nCI+BHZQprJrf/W8/ja0= X-Google-Smtp-Source: AGHT+IGD2UjNRug5F2H/UZOpU/gKqZ3pgXTuldQheukNRNfxEhbVqxTgsjheiGqXLJZme2Og3p5v9Q== X-Received: by 2002:a05:6a20:2d0e:b0:1cf:1228:c175 with SMTP id adf61e73a8af0-1cf1bfa1533mr16390196637.8.1725878251637; Mon, 09 Sep 2024 03:37:31 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-718e5897b3fsm3258032b3a.25.2024.09.09.03.37.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:37:30 -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 4/4] iio: light: ltr390: Add interrupt persistance support Date: Mon, 9 Sep 2024 16:06:23 +0530 Message-ID: <20240909103623.264113-5-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240909103623.264113-1-abhashkumarjha123@gmail.com> References: <20240909103623.264113-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 c707a4871..87195f001 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -38,6 +38,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 @@ -46,6 +47,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) @@ -77,6 +80,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; @@ -154,7 +162,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; @@ -163,7 +171,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, @@ -223,7 +231,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: @@ -248,7 +256,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), } }; @@ -396,6 +405,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) @@ -456,6 +503,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; } @@ -474,6 +525,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; }