From patchwork Sat Sep 14 18:12:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13804523 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.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 74DE71D1F6B; Sat, 14 Sep 2024 18:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337617; cv=none; b=HJag80gG5otIwDvRLVaJcWQQhep1BH0tExr0Sdp9/36vaWkiiq+9nCjvLLtlYhkpGho9Q+MF8081JR9LthfZ64Moqn4yea13PsfmzTRFyo4qNxmVZb6bQYKTuEG0IOzXRQfLf1yF+q5k5Rn8mJBgCUg2QZMLmO8zpxVAdzEEubM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337617; c=relaxed/simple; bh=pKJJJ8SCewiCk5MYqqk3UIB1n5D/Y4NL2MIcr71rX2U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Gf0dJv6lkGQay36Wp2yxxEshpYbxDwojJKrNyIa+BN5HTVMjDyVsZS9W/5HGGRZmVJouTwnKxTsMRooQ4l+bzcXXWVsiZ+FlZ1dnfPWf1sC4wiw1BA45rmrZgym6fk855LrfXJ0NJa5yoeyhlpAHfo4OZ135ZcwZCEsYaZ3ZnSU= 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=TsK8NbAi; arc=none smtp.client-ip=209.85.216.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="TsK8NbAi" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2d8881850d9so2628636a91.3; Sat, 14 Sep 2024 11:13:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726337615; x=1726942415; 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=hQ/Jft3meKc/ILM6QOHiIcM/QHQgFekSHb0KcpsMFbY=; b=TsK8NbAiocmCecSUlmd83MApXygGYRrOyeqHE0EpNazZW5VJLNt3EeZhdwlQrjjgXJ yezaPNd8HQmSM/JZet3zRvu2L01N2BfOsgngGAhXMAvCk7pF6VRgyxSMPPZgDYFuWo49 UEByLva4PNnKitd9OKqXDfZ8p6zeFN5NDDDN5h4A8UqAkrbwEgvSWJ2JzDX42VZy4DE9 X/OFuu86h471uzWzDeOJXZ+L/I3wJRBROK2vl/tOwWDgHNou1cyRfhwoNHFaj90ICao5 gjGmKesJFSJFUzHP4TicqcjZP8PkXTWIEXkdtYRk5C3JioQt2zpRt2l/UjbexeC1Unx2 wQIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726337615; x=1726942415; 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=hQ/Jft3meKc/ILM6QOHiIcM/QHQgFekSHb0KcpsMFbY=; b=j8Jg+MX6re1D9N8q2PeyGX5Y1yLPfAJ9Z4xgdTDwbmcFUxSXbS0ZftuQTx0+T0iIYr 8ZShutrZTcD91UaHphcsv16QkJGpDUSNXi3wHa/YSLtgrg7GJB+FDK5B87GhH/CmZUuS 1c1qMr4OLp1fbB9pxuLtK0mhMAzXGEEMS8zMOfd1527f2pPvWs+9RvpjErJPtqNNGhd5 FTPQYOMFPUntJF4f/+xXnIeVFdhf6Qu8jbpnt/ROUdSPIFt1ddtZ+SHIuOmKdNOv9f9E BfilCfNaXmaSf66rPPuLsWrG7EFzGtnIMSumacQohjFgNlW13VG3T2Fava5yvVQMNW5x Enag== X-Forwarded-Encrypted: i=1; AJvYcCUJxUUu78xNLKp/Tm5kOqVUos2q54KcHZZaLx8vIxT/w49O0L16iBDcIUUpP8F2Q0xittO2tHst0aq2AtI=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0zBrpLMXBXEsOwJELzqw1ATITB05fEHHiQEXZwku5J2FEojLc F3mbd9fP4KaQVb1eiwfu3qT6Zdxl3tEj9qtdPn4jdLDuVsuGdV5UiQwPU7ccYvk= X-Google-Smtp-Source: AGHT+IEKNsEHD8CZGFoCwyQezivIYUotjp9OkV09spmCwPOl5zZIVr3cNOYmEObeFblCn+Q8/fEP9w== X-Received: by 2002:a17:90b:3686:b0:2d8:82da:2627 with SMTP id 98e67ed59e1d1-2dba0061bc6mr11662235a91.27.1726337614589; Sat, 14 Sep 2024 11:13:34 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2dbcfd26f2csm1830870a91.31.2024.09.14.11.13.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 11:13:34 -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 v3 1/4] iio: light: ltr390: Added configurable sampling frequency support Date: Sat, 14 Sep 2024 23:42:43 +0530 Message-ID: <20240914181246.504450-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240914181246.504450-1-abhashkumarjha123@gmail.com> References: <20240914181246.504450-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Provided 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 | 70 +++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index 7e58b50f3..c50ea31e2 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 GENMASK(2, 0) #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 = FIELD_GET(LTR390_ALS_UVS_MEAS_RATE_MASK, value); + + 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,20 @@ 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_available = BIT(IIO_CHAN_INFO_INT_TIME) | 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), }, /* 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_available = BIT(IIO_CHAN_INFO_INT_TIME) | 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), }, }; @@ -264,6 +298,23 @@ 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 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); + return regmap_update_bits(data->regmap, + LTR390_ALS_UVS_MEAS_RATE, + LTR390_ALS_UVS_MEAS_RATE_MASK, idx); + } + + 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 +329,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 +357,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 Sat Sep 14 18:12:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13804524 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 B98C91D12FF; Sat, 14 Sep 2024 18:13:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337622; cv=none; b=mysh2Du3ILmQc9Cw8Y3KDSnpYYSm9eJgtbktzIietXjI085v98rgdb9JfBd99x3zVf5bIVtcWKMTp8qsiZjfjatXz/tJMCvbUBXJ8e1zsXc49n5mjWwXw8p/HaU/KEeu5TG+NMkY06mdCHiimIx2hyDu39nXneXw+o01yJNGzhM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337622; c=relaxed/simple; bh=s/LoBMZv5gHJkDYqSlQPWXy35bnh/xfj+rbEh52Ptfg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PopYM0lfAxcP1JAtJokDs8AWRq+cuQXur5f4+N7h0TUPUSHL3FSKsfVJG0e1YBVQdGHnh2OAHkeOuOv8gSb1Fm1dlGVBfY6HaqC/Wkb6VztGTZkDs+fRLIaUwrpWyZUoeYaw9tGlyaDugI4GdfUO6NDzTMcauOGay9uDBN30anA= 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=V5S1dfzP; arc=none smtp.client-ip=209.85.214.173 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="V5S1dfzP" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2053f6b8201so29966175ad.2; Sat, 14 Sep 2024 11:13:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726337619; x=1726942419; 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=VOs1UIlM5LJYEJGNmyVNKJGScq5MVA7FTKWexwbjRs8=; b=V5S1dfzPoyNVous0+hvEz6FPo4u7KgMxtkA1uuw2N4cOpmtC5pZ7R2LZD2oiim6i8G fJc0G8/fXWXPtiAN4zTBAXdryoeuh3hh1MCaLTmqR33ZW5K47tsnLJkj2O0jn6uk6gUt LPUGOa73y5VCGW9fU8UFrfose9QqVxUQdtMkjqGWWMIZ33Joc82X/sco6W72m6AuwoSE PsvVuAQ74MDP/9ymXSL0qw+9dIxeSrSPyQg9Jeclv3rjSOithgMaRUihKZryBByq/TQV PorBVocK5tKIwdFdSwlSFd5HhkFzPE9JmZtjEeeU2cI9udlZjhSyoWANGxuzDl95bptv /G9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726337619; x=1726942419; 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=VOs1UIlM5LJYEJGNmyVNKJGScq5MVA7FTKWexwbjRs8=; b=C53ArbTyttrBTGUcMb9I2w8Uyz1o/va4oqy0kNPV5bezvrnz2sphY/BAuyLcMw90ck NW3+M2kFFI/xS35Wx+TJgGRjD7MU86CI75c7VL5Ct/lvw6EAp6LYTv2m9bISYurmt/mP SdNF1d7t21kVdw1TqgK7efg03sKY4gOaFgoprl24Qe26EeecGv8ewbdHob9bTbSt+epP EtYZhBndWnT+bRZcUvl1LyyQGOUAhQLP931ebt7JBJY4uyUb+qdis9XHwweVpu58ig/C iWTk0JPGlGCzIVNDtRfWj/iuEolIbyuuCQ/FKE45Bdac7q9BoajReiq/NMtiSwYZmsSr t+hA== X-Forwarded-Encrypted: i=1; AJvYcCVCvljLac7QrVdpSFckZ+7iVo5yKbyV6XlnjbGTymlmabf5pueTqpzP/Bd8YHUJQaGiXX866y2q6dlbSuM=@vger.kernel.org X-Gm-Message-State: AOJu0YymYs4YxCiXWEwUiYB+mnSxFJZpTpp9Xa5iBulClMQoPIG1/sYs G2VHQb4Ou9etMuSvTUlRAlnjBApypxnR7V2UH8/pJNqYi977wP+uKGFmPUrKEIc= X-Google-Smtp-Source: AGHT+IHw2Nfj8ssbvnePhgO0iXjdhze1JIm25qEsE1YQBbKcOq4E5OjDf2F82RtFlFyLZYL+5+n1mA== X-Received: by 2002:a17:903:41cc:b0:206:d6ac:85e1 with SMTP id d9443c01a7336-2076e308699mr151995085ad.2.1726337619052; Sat, 14 Sep 2024 11:13:39 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2dbcfd26f2csm1830870a91.31.2024.09.14.11.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 11:13:38 -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 v3 2/4] iio: light: ltr390: Suspend and Resume support Date: Sat, 14 Sep 2024 23:42:44 +0530 Message-ID: <20240914181246.504450-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240914181246.504450-1-abhashkumarjha123@gmail.com> References: <20240914181246.504450-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 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index c50ea31e2..e152009b5 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -430,6 +431,26 @@ static int ltr390_probe(struct i2c_client *client) return devm_iio_device_register(dev, indio_dev); } +static int ltr390_suspend(struct device *dev) +{ + struct iio_dev *indio_dev = dev_get_drvdata(dev); + struct ltr390_data *data = iio_priv(indio_dev); + + return regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL, + LTR390_SENSOR_ENABLE); +} + +static int ltr390_resume(struct device *dev) +{ + struct iio_dev *indio_dev = dev_get_drvdata(dev); + struct ltr390_data *data = iio_priv(indio_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 */ } @@ -446,6 +467,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 Sat Sep 14 18:12:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13804525 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.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 57B411D278F; Sat, 14 Sep 2024 18:13:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337625; cv=none; b=Pu7C6QLTrjwdjx15TLFm96MqrhnIFO6YYGVDZhOdM3QtNRWbg2xzdBpe5KWchZKoB2MbnkrIx+EjG98x2jljKhaRnBLfb+QJWJhTdxp5ujTIMoIyhYai6csZlYCHQjkIEmb7fvyk9UVeSA+hEPMPuBVmD1amD8z9N/H8cuV3YPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337625; c=relaxed/simple; bh=/zrDPSUSW+KCgQasuMdEtHhQ2cHGxxYjaaXishYNjVY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XEe5K7Yi3F5uwHcFfaLBGntWxG5gd1EqGqylN5Gp+Pq8GQLGKI0veGIxJZz0/tKfwbwMadQWaGtWLNudM7Wmw0XVqyOX2g2GQFPZCLn9RomeQddhL234Aomugyg3zr9vAruIOwIlRddtV88AWauCFCz1zYoJnrQPDhzqO+KrjDM= 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=mLDyxYcP; arc=none smtp.client-ip=209.85.214.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="mLDyxYcP" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-206bd1c6ccdso17819795ad.3; Sat, 14 Sep 2024 11:13:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726337622; x=1726942422; 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=JgOgFn1PixKBms2a98A4BTpMQlTcE2wOvdP5Mou9Jro=; b=mLDyxYcP9shg1uu6VApUUn9dB/N8JuXIsTgq9un3K82GEm+tjLxDV78CWl/7hHpUcL yvgdoF98LQ0kFt3tYZT0R8VUNGV156yrqs9cPml+g4eRyLcYNwZJxmbvzU6DlOXczl2z ydnXvHZUa0/f2jLG3gkSOZ0T/S7FZm4PH454jk95liQG4NwH2lHHcuGwl3TOh55Eh6Qg ukQEa+2vSVxXdozmA1ZoQSascDyjgcQPrvSEaGc0AU5leLUi0vZG9Ev3SrqAH73uG3ki p/4Lo89xgaRhIezJdCrmoRkdzS6qdcCD6LU8B00K5PwLONY0LPPZF1b5DWstBL03XEsY FlMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726337623; x=1726942423; 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=JgOgFn1PixKBms2a98A4BTpMQlTcE2wOvdP5Mou9Jro=; b=s28tbPnziTOhmKFcOVMQ6HE4+vD6VCWqKdwf9MiCu8m/txuKsXw5UG8xOCxkeMcOMD u5SjMw2U8UisdoS/BIGN+fs5BamqIcFnXJQLw2KL0Axpu7v++OWRYvwu26Q0Wm8Hzgla kWs0i+bHM6ik6sMhOCqYOm9LlYu3HxtkiBFtyoZpLPk4K4WW7TYi8Eo5ActWcIxXWBgr pAAVCb/gXwtQ3TLl+3mrWxWoNqi3K3QFM7s1mOCosSHEpnPefk20eJ2ypdVq9yzFvzwl OHympxs1NW1lLsyBgndxiJZ59qC+SjVf3gbvChz+xxvAKaL/Kar64BkxfX2A/H6XOfuH tD8g== X-Forwarded-Encrypted: i=1; AJvYcCXTrWjoWj7p/65pP0vhVa9hCeYRuOyrij2iL65pDT/EPkoWyFYKGiPI3wlsxALPch7lSWTM8LTCTqtbric=@vger.kernel.org X-Gm-Message-State: AOJu0YyNcTxz557FiDyMqA/xtg+5ZBSygh+Bs9MVcUv6daXFY58yd3yS FIdELJ+AgigtdI+smS9Q58WRte+qmeMhYu1LQQhQbBPCK59F5V33sWZTAHzmetU= X-Google-Smtp-Source: AGHT+IFv2pj+4hgeJM5D9yizxIZ3hpbcpCIWgKB3qpXsjyn8RAVikmg0iASj79WXpE+dxzWK/0/J8w== X-Received: by 2002:a17:90b:3758:b0:2da:88b3:d001 with SMTP id 98e67ed59e1d1-2dbb9e1d271mr8534753a91.18.1726337622410; Sat, 14 Sep 2024 11:13:42 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2dbcfd26f2csm1830870a91.31.2024.09.14.11.13.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 11:13:41 -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 v3 3/4] iio: light: ltr390: Interrupts and threshold event support Date: Sat, 14 Sep 2024 23:42:45 +0530 Message-ID: <20240914181246.504450-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240914181246.504450-1-abhashkumarjha123@gmail.com> References: <20240914181246.504450-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 | 212 +++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index e152009b5..57bf48595 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -25,8 +25,11 @@ #include #include #include +#include +#include #include +#include #include @@ -34,9 +37,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 @@ -47,6 +53,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 @@ -231,6 +239,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 */ { @@ -241,6 +265,8 @@ static const struct iio_chan_spec ltr390_channels[] = { .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ), + .event_spec = ltr390_event_spec, + .num_event_specs = ARRAY_SIZE(ltr390_event_spec), }, /* ALS sensor */ { @@ -251,6 +277,8 @@ static const struct iio_chan_spec ltr390_channels[] = { .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ), + .event_spec = ltr390_event_spec, + .num_event_specs = ARRAY_SIZE(ltr390_event_spec), }, }; @@ -369,12 +397,183 @@ static int ltr390_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec cons } } +static int ltr390_read_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; + + 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); + + guard(mutex)(&data->lock); + switch (dir) { + case IIO_EV_DIR_RISING: + return regmap_bulk_write(data->regmap, LTR390_THRESH_UP, &val, 3); + + case IIO_EV_DIR_FALLING: + return regmap_bulk_write(data->regmap, LTR390_THRESH_LOW, &val, 3); + + 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 FIELD_GET(LTR390_LS_INT_EN, status); +} + +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; @@ -428,6 +627,19 @@ 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) { + ret = devm_request_threaded_irq(&client->dev, client->irq, + NULL, ltr390_interrupt_handler, + IRQF_TRIGGER_FALLING | + 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 Sat Sep 14 18:12:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13804526 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 8D3431D3196; Sat, 14 Sep 2024 18:13:47 +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=1726337629; cv=none; b=lwB9ChvGYIvgHC7skWPEw4a5334IR+oqEn4VSURVlATIeJMCS7vIiZp1w9rnEHEWd9W4AhrODTp5Dbc7Fi6YNJt6zRkJ0Ofr6ocdu70wKFvCaxrnvJsJP/Eof91owtD3eDw5214uBw9LUyXddjbCn1CGvVqe2O4y81bCFhbaSnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726337629; c=relaxed/simple; bh=Qw6txWVPdZ7FgDvn4pS7WGFdIcv31Wh2+xKLWxH8g3o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RoduutVATFGaKNy6F/R84tAWUUdFLiXE9rami6W5sP5zZMPwonrdllAh6YmScA8rxj4mvspNjZFNxUKRGcW8eMTgvgViWpQrIcYlYHXh5vu8kaupH9XysdV4dYrtmWy8OzMkI4o2fhHDKdUMxRKNsdpAjEY4k8p5VgAsbcxGCSo= 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=SIkwuz2T; 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="SIkwuz2T" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20573eb852aso28071015ad.1; Sat, 14 Sep 2024 11:13:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726337626; x=1726942426; 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=028JaGJ1ahArc8EOD+3vvYERpoxcW3WW4Or8S3HILb4=; b=SIkwuz2TH0kp5RMPptdxst/x+UkFQlOcQqIHDloQqDRYKSUyRbtUh+FFY3ZX6fwEYR m1qOJecOfQqWpcGyThmiSQT+PG1OemrA+LmC/pp1pNHOi/6pyerDqY7o7r4iswn65GvR 5Y37CcM1G+3nVn2vGe3N25syj/ccshmKC6fQqiyM8aa1mjtl/VMAzWn+VtFnM1eC8oWS 40Jz7qcVkWil600DPBZE1F/qzQWAPujcNc07qm/ShM5p1UbAhsVwMhbrCV8IKpkDNJBr 5cuHsL1vFTuRXQlMpOgoIedDrMl2JFsU1zNXElheMapZe3xQbfSNV4U9rm1O1O2s+t7Z LA7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726337626; x=1726942426; 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=028JaGJ1ahArc8EOD+3vvYERpoxcW3WW4Or8S3HILb4=; b=c7Ya3T/seDopYVFJUT9c4GyBy/wZyl2hbSfcGVyszDcnSKCMhvy56kP7GqBLD8YLVl ZE69csnxnZAZIULt46v0duKDIA7NXmpF6JrvXLlzCU/N7XAamBfApYHIY2cBv0tOXbSm SqOGBctmIdP537l83hhuYv2JwPcGpagt3AhWRGyvYxfG4ZakP3V0iS/dBXR3O/MIumlr 4EB01HxCtZuDpnAtpBLgfifZvQA3v9MEZNfMfwGk5lr7SBfQ3kqYS9f/FRij7e76oA+t n8W2Rw6oRsUpUW6uxg6nlF2zSUPyd3ksT6Cbkn5FH56PbC3ltbeBovRErgbIacWEzO4K 2/pA== X-Forwarded-Encrypted: i=1; AJvYcCWfF/yEehsTwMRYyfAZVswLn3xBK+vkvStG0gJY6Mz/9/75BSFD8Pr/uubY9bD+g8vhw5nveZcjjBsPIms=@vger.kernel.org X-Gm-Message-State: AOJu0YzwcBRUg+UM8sqgEZbv4I9VzVQwp6TwiDGJ1lTKdWNVq5JwJTyc WyDBNBPL4OjOxvaRp3zOPeq3VmS3R+xzFVW0uUgRqbZrsHdW7lvZ3gxmIstivdM= X-Google-Smtp-Source: AGHT+IF7DPpO9UMOZdwoydrcnG4dGInQk2kuK5Z6EZkb0Su3KwkpSd/qTrIfDI56nTkKN0A+j6XXgA== X-Received: by 2002:a17:90a:62ca:b0:2d8:9fbe:6727 with SMTP id 98e67ed59e1d1-2db9fc1adabmr15562659a91.4.1726337625752; Sat, 14 Sep 2024 11:13:45 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2dbcfd26f2csm1830870a91.31.2024.09.14.11.13.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 11:13:45 -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 v3 4/4] iio: light: ltr390: Add interrupt persistance support Date: Sat, 14 Sep 2024 23:42:46 +0530 Message-ID: <20240914181246.504450-5-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240914181246.504450-1-abhashkumarjha123@gmail.com> References: <20240914181246.504450-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 | 65 +++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c index 57bf48595..a51ad6704 100644 --- a/drivers/iio/light/ltr390.c +++ b/drivers/iio/light/ltr390.c @@ -41,6 +41,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 @@ -49,6 +50,8 @@ #define LTR390_ALS_UVS_MEAS_RATE_MASK GENMASK(2, 0) #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 GENMASK(7, 4) +#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) @@ -80,6 +83,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; @@ -157,7 +165,8 @@ 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; @@ -166,7 +175,7 @@ static int ltr390_get_samp_freq(struct ltr390_data *data) return ret; value = FIELD_GET(LTR390_ALS_UVS_MEAS_RATE_MASK, value); - return ltr390_samp_freq_table[value][0]; + return ltr390_samp_freq_table[value][option]; } static int ltr390_read_raw(struct iio_dev *iio_device, @@ -226,7 +235,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: @@ -251,7 +260,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), } }; @@ -397,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(struct iio_dev *indio_dev, enum iio_event_direction dir, int *val, int *val2) @@ -453,6 +501,9 @@ static int ltr390_read_event_value(struct iio_dev *indio_dev, 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; } @@ -472,6 +523,12 @@ static int ltr390_write_event_value(struct iio_dev *indio_dev, 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; }