From patchwork Mon Oct 7 15:22: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: 13824908 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 793A71D86DC; Mon, 7 Oct 2024 15:22:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314559; cv=none; b=IVcM3sW6WGHtKViHhlX8+eyGMYC14CsYv9m1sB4Z1ojqdiUNZsZga/8217u3W7daSIM911Rmj9uUFcPmvfyKoQv1HegLacWlAbEw4r2eza13kbateVRpUNYaxc6ruOefXMYPqJiPcrbWCOhLQc1TjBlSwmI2zrXrUKq07Hfpx0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314559; c=relaxed/simple; bh=T+QYja7LUJzkV7RCgdEgVqVceSPVINzxqOOsbiHlAtM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RKG9Fscqlb9lh1+gVbtszCRNi8HopT5gdXpPq1OT45FeGYTZKKZJAJXShlpXtMRCMjtk0kWcpApl7zQJPGGKH8rOm8eHMcwUfajDobc0w+W034KAvc2NHVGJZ505vGvB/E/pNDLnv/FVlos51EeS2tcGL21MnBKZS52bHxXj8go= 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=eTQ7uTkU; arc=none smtp.client-ip=209.85.215.175 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="eTQ7uTkU" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7e6ed072cdaso2773301a12.0; Mon, 07 Oct 2024 08:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728314557; x=1728919357; 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=yUspM/VMPEO1Kn2QXTO9UIPl/J9+OPnNBwBdWZtkysA=; b=eTQ7uTkUoyAb9kodJrJUyR3J6l58/8eLICEzEeN0BLYADx5jfQ2iXrIl7Cib5IniC5 vY8V9DVYqiAasLCNsDTFW+5UXIjuG6TBFVwa3CwVYgNkqR3WaGiUi5+AVghMQ9QJWLv4 l2zsq9Vs1QFFlmEIWsqcohbpfcqpymU6AtJb5ASmcoiycFUusKMufOqfaGyuoouRyrCK UrwieCP8ahFSY/rCB8hw3+e2XlBWHmBLo4GOutMxWxAu8r1qIBRAus2ebj2m1HcnYqRh j+eywaxDAG9BB9Xo1ABFYdKOPpiKOT54XEQpKLPnQP3NYWF2kMAGc9rHOpyqzmthX7Sl kYsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728314557; x=1728919357; 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=yUspM/VMPEO1Kn2QXTO9UIPl/J9+OPnNBwBdWZtkysA=; b=JTHqvmkpZlmTpMGh1GfK14kWt1yoITeZXkc2ZJ/l5I2g141lYwXZ1K8Js409k7MErV DgUvCcjQBG/4sgs8pg0bIIpZ6eU/6g0rS9AheoNMjOvOeipbJoiQcssZ/0aSN/uE0Uu9 sm+KKvo80ll4KbX3rtFN5GivxNtsqCMSELunV1BRRzS9hfmBi+lfbfG2Bck2RqnGaLUu h2tqvNln/9M+HxCAVc4vetRnPBOB/HRCn0fjSiL4UC3vnXnn8KtLPF6zv6TMdoGsNFcg 8cBf7wTnV+496cTcaaENLm1vfrj/DJ1RuWcruDawMTS77qcic99FB4M+nypACvT3sZ/V fpnA== X-Forwarded-Encrypted: i=1; AJvYcCW7johFox95zu2xIj+OfOnWdLmOBicNFpBvhsA/meLSjhTDqf21vy8rMkVsAvTCjkjLaJJax0nNVV9o8iE=@vger.kernel.org X-Gm-Message-State: AOJu0YxTE1p7YFlI/6/eX3BvH7+LCsC+PEID3NEfjrsZi7NgIAon6gda XFnKlfAFHuC96+VV3Vs3wsPjKqCuWKnBlJElRa3x3fz/v5/pW8uSqoMWUJlmwME= X-Google-Smtp-Source: AGHT+IEo33uhP8Qe+gkII6XmDg9VWsgUkTeBSKRjSDsxhzvkAU617wQwO8pfQSWqTxffdaE06eaDeA== X-Received: by 2002:a17:90a:983:b0:2d3:caeb:a9ad with SMTP id 98e67ed59e1d1-2e1e6321935mr14441440a91.31.1728314556959; Mon, 07 Oct 2024 08:22:36 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2e20ae7169esm5573934a91.10.2024.10.07.08.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:22:36 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v3 1/3] iio: light: vl6180: Add configurable inter-measurement period support Date: Mon, 7 Oct 2024 20:52:21 +0530 Message-ID: <20241007152223.59008-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241007152223.59008-1-abhashkumarjha123@gmail.com> References: <20241007152223.59008-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Expose the IIO_CHAN_INFO_SAMP_FREQ attribute as a way to configure the inter-measurement period for both the IIO_DISTANCE and IIO_LIGHT channels. The inter-measurement period must be given in milihertz. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 70 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index a1b2b3c0b..67aa2f101 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -38,7 +38,9 @@ #define VL6180_OUT_OF_RESET 0x016 #define VL6180_HOLD 0x017 #define VL6180_RANGE_START 0x018 +#define VL6180_RANGE_INTER_MEAS_TIME 0x01b #define VL6180_ALS_START 0x038 +#define VL6180_ALS_INTER_MEAS_TIME 0x03e #define VL6180_ALS_GAIN 0x03f #define VL6180_ALS_IT 0x040 @@ -86,6 +88,8 @@ struct vl6180_data { struct mutex lock; unsigned int als_gain_milli; unsigned int als_it_ms; + unsigned int als_meas_rate; + unsigned int range_meas_rate; }; enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; @@ -261,12 +265,14 @@ static const struct iio_chan_spec vl6180_channels[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | - BIT(IIO_CHAN_INFO_HARDWAREGAIN), + BIT(IIO_CHAN_INFO_HARDWAREGAIN) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type = IIO_DISTANCE, .address = VL6180_RANGE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type = IIO_PROXIMITY, .address = VL6180_PROX, @@ -333,6 +339,18 @@ static int vl6180_read_raw(struct iio_dev *indio_dev, return IIO_VAL_FRACTIONAL; + case IIO_CHAN_INFO_SAMP_FREQ: + switch (chan->type) { + case IIO_DISTANCE: + *val = data->range_meas_rate; + return IIO_VAL_INT; + case IIO_LIGHT: + *val = data->als_meas_rate; + return IIO_VAL_INT; + default: + return -EINVAL; + } + default: return -EINVAL; } @@ -412,11 +430,23 @@ static int vl6180_set_it(struct vl6180_data *data, int val, int val2) return ret; } +static int vl6180_meas_reg_val_from_mhz(unsigned int mhz) +{ + unsigned int period = DIV_ROUND_CLOSEST(1000 * 1000, mhz); + unsigned int reg_val = 0; + + if (period > 10) + reg_val = period < 2550 ? (DIV_ROUND_CLOSEST(period, 10) - 1) : 254; + + return reg_val; +} + static int vl6180_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) { struct vl6180_data *data = iio_priv(indio_dev); + unsigned int reg_val; switch (mask) { case IIO_CHAN_INFO_INT_TIME: @@ -427,6 +457,28 @@ static int vl6180_write_raw(struct iio_dev *indio_dev, return -EINVAL; return vl6180_set_als_gain(data, val, val2); + + case IIO_CHAN_INFO_SAMP_FREQ: + { + guard(mutex)(&data->lock); + switch (chan->type) { + case IIO_DISTANCE: + data->range_meas_rate = val; + reg_val = vl6180_meas_reg_val_from_mhz(val); + return vl6180_write_byte(data->client, + VL6180_RANGE_INTER_MEAS_TIME, reg_val); + + case IIO_LIGHT: + data->als_meas_rate = val; + reg_val = vl6180_meas_reg_val_from_mhz(val); + return vl6180_write_byte(data->client, + VL6180_ALS_INTER_MEAS_TIME, reg_val); + + default: + return -EINVAL; + } + } + default: return -EINVAL; } @@ -473,6 +525,20 @@ static int vl6180_init(struct vl6180_data *data) if (ret < 0) return ret; + /* Default Range inter-measurement time: 50ms or 20000 mHz */ + ret = vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME, + vl6180_meas_reg_val_from_mhz(20000)); + if (ret < 0) + return ret; + data->range_meas_rate = 20000; + + /* Default ALS inter-measurement time: 10ms or 100000 mHz */ + ret = vl6180_write_byte(client, VL6180_ALS_INTER_MEAS_TIME, + vl6180_meas_reg_val_from_mhz(100000)); + if (ret < 0) + return ret; + data->als_meas_rate = 100000; + /* ALS integration time: 100ms */ data->als_it_ms = 100; ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100); From patchwork Mon Oct 7 15:22: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: 13824909 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.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 A5E861D88CD; Mon, 7 Oct 2024 15:22:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314563; cv=none; b=dHP/hLF5fdZDI6TsAqOnDKEQUaVt6SPc3TpIn3wOkMf2QIvbNzS4TsIAHDcEaIU8H0rPaRzcPECIoXcegcj3qOkJh6h/djxbFPJTDYuDYzucGw9vq2ABTV6t6jKz6onpgQOTGv2fGgl2P7XzQTb4G0takXRVflkn3ythBtGf49I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314563; c=relaxed/simple; bh=y8fpYG9T60OwQP0aXJxzsXaodmkYPpmnD99lrwUmUdo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VSXdgaAHmyF++qN1Ua0zQH+54zDiSAGxBXN7C9ojnm186HxTMgwww1bw76ZkYp2v51/TSa4VomBpxfrXwSFHS4UHQhfbfQm2vlmwclgBh7nQiCVsFYJ86BdUpTdxDERrSQTIOIJB5OjvR0ctNentnvmmgudjhKLKMzRU/jm7CCo= 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=NXedQg/T; arc=none smtp.client-ip=209.85.215.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="NXedQg/T" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7e9f998e1e4so1645462a12.1; Mon, 07 Oct 2024 08:22:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728314560; x=1728919360; 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=i6Q3pN7n5lzASa1iAB1h9SmIQ5Uey3Y3s1sa+1tGPWk=; b=NXedQg/TYx0tqyphiJbvyDoI0td2qT8voesamABCJvag1/+MvgIEVw/CLik9v7h/Xx Y5bA+xRIG9KYQk8cArE766gM3iUz7fK2gbukGvahYGYOYhXTh1U/Lm/TqhrMxdNwoTQJ bzSttaqe1/rzWJWBj7ptC947OwjS2YCx0x9HfOLgcsot8dadLt/6+AdHiIVOcEACB8j+ e8Seq7y/q646CeJ0tj7gSLgBggbnxnByKm0QXk97zhEQfYdFLAfFYZkfVDRGHC4hmW44 DWUTajz21UK4nEmKXSRmgha1SwDcydW+FEqaU5fQZnAmfCHxD2AYW7c1MN0hnh4HhU+A dvAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728314560; x=1728919360; 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=i6Q3pN7n5lzASa1iAB1h9SmIQ5Uey3Y3s1sa+1tGPWk=; b=SlTmERBKtkk3qsMylGb1fs4TxawoeZdFZM5DI0qRO8sZNRcimIWhIz48mB11PBcixp 9iiStsDH4GRrS26g3mCZNfN2oCHHCceyIjFlFHqDSYC/h1TkfOJXLDOxpDBuFBPce+qT u72pRfSyslt1uicrtmzrMROZqYvs3Hizvo+FqTVvk2NSAxmnWtEOCmUJ0FZ/cMaR6hnF iGn0CmrQwis8WfwsYYof34OSEblA2uY1nXWsJfiV44xaA9CcEeMWhoCtUj4cghj4TmNh G3Dg5T4wiINTppKrps8Idd9IsZfY2fIrDwY5NIR78wbqkeDS5CtvORfesoVv1E6yK5Fq IIxA== X-Forwarded-Encrypted: i=1; AJvYcCWR6S4hwHhslwdzG9zx3B4XpujcUoBOFQjZHyps+/WKY7z2kmtSMvM11NOPilBx3VfwKXC3fjeQMWrFG1E=@vger.kernel.org X-Gm-Message-State: AOJu0YwopVmx6dtisseCGv1uuSY2khNcTKqCacw1kwQTfPsHllPLnfJt 7qjL/qpxvYwRX83vXIPOrM7w6PG90AavyXfQ22Uqzv/qTOSAkix37tCYeHWu/6s= X-Google-Smtp-Source: AGHT+IGYgT9NDueQQjaefzAb7BVfyVAY1NDMIe8j7rhjYWE2AZ0lC9DdA3RTQrhsBA+X3+xTghmFjQ== X-Received: by 2002:a17:90b:118c:b0:2d8:8d34:5b8 with SMTP id 98e67ed59e1d1-2e1e621da2dmr15358543a91.10.1728314560313; Mon, 07 Oct 2024 08:22:40 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2e20ae7169esm5573934a91.10.2024.10.07.08.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:22:39 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v3 2/3] iio: light: vl6180: Added Interrupt support for single shot access Date: Mon, 7 Oct 2024 20:52:22 +0530 Message-ID: <20241007152223.59008-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241007152223.59008-1-abhashkumarjha123@gmail.com> References: <20241007152223.59008-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The interrupts are serviced in the `vl6180_measure` function when the irq_handler signals that the reading is complete. We now can read asynchronously if `client->irq` is set. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 54 +++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index 67aa2f101..a747501b0 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -86,6 +86,7 @@ struct vl6180_data { struct i2c_client *client; struct mutex lock; + struct completion completion; unsigned int als_gain_milli; unsigned int als_it_ms; unsigned int als_meas_rate; @@ -211,29 +212,38 @@ static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val) static int vl6180_measure(struct vl6180_data *data, int addr) { struct i2c_client *client = data->client; + unsigned long time_left; int tries = 20, ret; u16 value; mutex_lock(&data->lock); + reinit_completion(&data->completion); + /* Start single shot measurement */ ret = vl6180_write_byte(client, vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP); if (ret < 0) goto fail; - while (tries--) { - ret = vl6180_read_byte(client, VL6180_INTR_STATUS); - if (ret < 0) - goto fail; - - if (ret & vl6180_chan_regs_table[addr].drdy_mask) - break; - msleep(20); - } + if (client->irq) { + time_left = wait_for_completion_timeout(&data->completion, HZ / 10); + if (time_left == 0) + return -ETIMEDOUT; + } else { + while (tries--) { + ret = vl6180_read_byte(client, VL6180_INTR_STATUS); + if (ret < 0) + goto fail; + + if (ret & vl6180_chan_regs_table[addr].drdy_mask) + break; + msleep(20); + } - if (tries < 0) { - ret = -EIO; - goto fail; + if (tries < 0) { + ret = -EIO; + goto fail; + } } /* Read result value from appropriate registers */ @@ -484,6 +494,15 @@ static int vl6180_write_raw(struct iio_dev *indio_dev, } } +static irqreturn_t vl6180_threaded_irq(int irq, void *priv) +{ + struct iio_dev *indio_dev = priv; + struct vl6180_data *data = iio_priv(indio_dev); + + complete(&data->completion); + return IRQ_HANDLED; +} + static const struct iio_info vl6180_info = { .read_raw = vl6180_read_raw, .write_raw = vl6180_write_raw, @@ -583,6 +602,17 @@ static int vl6180_probe(struct i2c_client *client) if (ret < 0) return ret; + if (client->irq) { + ret = devm_request_threaded_irq(&client->dev, client->irq, + NULL, vl6180_threaded_irq, + IRQF_ONESHOT, + indio_dev->name, indio_dev); + if (ret) + return dev_err_probe(&client->dev, ret, "devm_request_irq error \n"); + + init_completion(&data->completion); + } + return devm_iio_device_register(&client->dev, indio_dev); } From patchwork Mon Oct 7 15:22: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: 13824910 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.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 5508B1D935A; Mon, 7 Oct 2024 15:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314567; cv=none; b=VSlYTJLQi84Q9D79a5vlAhqSEKx3E5nlUlv1KTVFWY6juz+oBZNAFRm1KaI6Z2MSmgWNcVrECMooyQtnj5aXGumUFO4zS/YGLDA6hqpGdJtIBCndStGoNHYJtg3/BDaY+K6FOQbaHY0DDVXQ1ejwk8tQVBUKNHXBsfT70IWtvo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314567; c=relaxed/simple; bh=kahuhEbWhljSN1XdX+Glr547jKdvkKJ3VPKL2ysqdQE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fmeQgZBPgjMFCLqqvg0MHcgBOF6RVl6Y7dIbb/sB78JGCjcsaH2kvhdREic0zo0zkqRN2TqtLL+9Q3vEb9AxZw5iM5hEitbaQaOCS/al+dHiCIY9OzKAaAvd+Y3BRZawq+mqPGfBBV9pQnURuzv6tYwJhCKaBfOJZJAhCp7HBQM= 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=m5PZvXh+; arc=none smtp.client-ip=209.85.215.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="m5PZvXh+" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7ea03ecf191so1247701a12.0; Mon, 07 Oct 2024 08:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728314564; x=1728919364; 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=NGXYLWa9PfVlmMvYF4HK1fhAmnNokMXHI7JdaUOAE80=; b=m5PZvXh+2ksgnzjTt2OIzG+fCLmFuSXT+H9VhKBm6xJsrabwDuMBIMhkmMCF97zzTT irpgmnnCp2/OzESudFAsg7Bt+M4eSdbERHfkwrdX8Kie8mjwwXFbK/sjUCs1cQC7hRl6 KHHqC4oX53x3EXE9nBF/f4NnfShga/VId3FFCB/f4Cpa3/pXAUxqakB5i1BU8rMjuqBr +x+DIf2L69YfC/qZIBdWX0OjQy4vwjws3Lyy4sgaxKdgjtNWDrIWQcRiK6aPFl63LUZr ySy/cJQ5bHr6YWGzwdf9jWwDU7jp1GbAX+ciNKezfbIEQzLHULad0P7CvTr8aBF2fvXy Ug9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728314564; x=1728919364; 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=NGXYLWa9PfVlmMvYF4HK1fhAmnNokMXHI7JdaUOAE80=; b=ku71deYx63MPnvgCMjgwlylT1TvXS0WVsD5o2iyLBDAE+vEAlLu63ajGYVZeafyAZY iJphpA7MyYwQx9wleG3ruU+IXf9wIMaZIegWAbAayegzOEXsCzA7+2YuVr9uDXgvBGhX qVetwJYjCBxiBoQzOeapITN/BurKx8mnmL9EYWffD1z9DxJVU12sM1tjj2Su041VWBva 7XO6vm5YLAGa/gtd7vCiFGnGn6WGs4MSW+7Gj2F3t4WkGvHUp1Pooj/I4BAtYot/ojZK In6G2SA1doxFaduqXcUiy+CsdD8G/+pZZvMfarU8UPXfEWHkEoRtBFl8eUY60X4calyM zGOw== X-Forwarded-Encrypted: i=1; AJvYcCWILr6LiltpAI2clAKY+ZSr4qLEHfRGDGF7XRrA0v/AT93E2wlrkPysKtP9W7ubPml4A9rDl9OmkSfuKJY=@vger.kernel.org X-Gm-Message-State: AOJu0YwSHkXYnWmeKJ9gX/9h/pYEVfrYvs0FjelyHSI4RdUlQMbk7Klt Io9NmciWTAx2AqGLu87kM/PdwYffEYbT9r0Y2cAwxqi47po2XGhFOz+Azw1BrYE= X-Google-Smtp-Source: AGHT+IGg4fDZTfwSodiIxvKKC0awHpg5iBCyYkbp+o+1V4s+lZyTXsCLdYL3Vf8ED/UMff0pP5QO8g== X-Received: by 2002:a17:90b:3795:b0:2d8:9255:396d with SMTP id 98e67ed59e1d1-2e1e5dc6041mr12735437a91.0.1728314564309; Mon, 07 Oct 2024 08:22:44 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2e20ae7169esm5573934a91.10.2024.10.07.08.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:22:43 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v3 3/3] iio: light: vl6180: Add support for Continuous Mode Date: Mon, 7 Oct 2024 20:52:23 +0530 Message-ID: <20241007152223.59008-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241007152223.59008-1-abhashkumarjha123@gmail.com> References: <20241007152223.59008-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 getting continuous readings from vl6180 using triggered buffer approach. The continuous mode can be enabled by enabling the buffer. Also added a trigger and appropriate checks to see that it is used with this device. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 133 +++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index a747501b0..f0c7a13a0 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -25,6 +25,10 @@ #include #include +#include +#include +#include +#include #define VL6180_DRV_NAME "vl6180" @@ -87,10 +91,16 @@ struct vl6180_data { struct i2c_client *client; struct mutex lock; struct completion completion; + struct iio_trigger *trig; unsigned int als_gain_milli; unsigned int als_it_ms; unsigned int als_meas_rate; unsigned int range_meas_rate; + + struct { + u16 chan[2]; + aligned_s64 timestamp; + } scan; }; enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; @@ -272,6 +282,12 @@ static const struct iio_chan_spec vl6180_channels[] = { { .type = IIO_LIGHT, .address = VL6180_ALS, + .scan_index = VL6180_ALS, + .scan_type = { + .sign = 'u', + .realbits = 16, + .storagebits = 16, + }, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | @@ -280,14 +296,27 @@ static const struct iio_chan_spec vl6180_channels[] = { }, { .type = IIO_DISTANCE, .address = VL6180_RANGE, + .scan_index = VL6180_RANGE, + .scan_type = { + .sign = 'u', + .realbits = 8, + .storagebits = 8, + }, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type = IIO_PROXIMITY, .address = VL6180_PROX, + .scan_index = VL6180_PROX, + .scan_type = { + .sign = 'u', + .realbits = 16, + .storagebits = 16, + }, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), - } + }, + IIO_CHAN_SOFT_TIMESTAMP(3), }; /* @@ -499,7 +528,48 @@ static irqreturn_t vl6180_threaded_irq(int irq, void *priv) struct iio_dev *indio_dev = priv; struct vl6180_data *data = iio_priv(indio_dev); - complete(&data->completion); + if (iio_buffer_enabled(indio_dev)) + iio_trigger_poll_nested(indio_dev->trig); + else + complete(&data->completion); + + return IRQ_HANDLED; +} + +static irqreturn_t vl6180_trigger_handler(int irq, void *priv) +{ + struct iio_poll_func *pf = priv; + struct iio_dev *indio_dev = pf->indio_dev; + struct vl6180_data *data = iio_priv(indio_dev); + s64 time_ns = iio_get_time_ns(indio_dev); + int ret, bit, i = 0; + + iio_for_each_active_channel(indio_dev, bit) { + if (vl6180_chan_regs_table[bit].word) + ret = vl6180_read_word(data->client, + vl6180_chan_regs_table[bit].value_reg); + else + ret = vl6180_read_byte(data->client, + vl6180_chan_regs_table[bit].value_reg); + + if (ret < 0) { + dev_err(&data->client->dev, + "failed to read from value regs: %d\n", ret); + return IRQ_HANDLED; + } + + data->scan.chan[i++] = ret; + } + + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, time_ns); + iio_trigger_notify_done(indio_dev->trig); + + /* Clear the interrupt flag after data read */ + ret = vl6180_write_byte(data->client, VL6180_INTR_CLEAR, + VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); + if (ret < 0) + dev_err(&data->client->dev, "failed to clear irq: %d\n", ret); + return IRQ_HANDLED; } @@ -507,9 +577,45 @@ static const struct iio_info vl6180_info = { .read_raw = vl6180_read_raw, .write_raw = vl6180_write_raw, .attrs = &vl6180_attribute_group, + .validate_trigger = iio_validate_own_trigger, }; -static int vl6180_init(struct vl6180_data *data) +static int vl6180_buffer_postenable(struct iio_dev *indio_dev) +{ + struct vl6180_data *data = iio_priv(indio_dev); + int bit; + + iio_for_each_active_channel(indio_dev, bit) + return vl6180_write_byte(data->client, + vl6180_chan_regs_table[bit].start_reg, + VL6180_MODE_CONT | VL6180_STARTSTOP); + + return -EINVAL; +} + +static int vl6180_buffer_postdisable(struct iio_dev *indio_dev) +{ + struct vl6180_data *data = iio_priv(indio_dev); + int bit; + + iio_for_each_active_channel(indio_dev, bit) + return vl6180_write_byte(data->client, + vl6180_chan_regs_table[bit].start_reg, + VL6180_STARTSTOP); + + return -EINVAL; +} + +static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { + .postenable = &vl6180_buffer_postenable, + .postdisable = &vl6180_buffer_postdisable, +}; + +static const struct iio_trigger_ops vl6180_trigger_ops = { + .validate_device = iio_trigger_validate_own_device, +}; + +static int vl6180_init(struct vl6180_data *data, struct iio_dev *indio_dev) { struct i2c_client *client = data->client; int ret; @@ -544,6 +650,12 @@ static int vl6180_init(struct vl6180_data *data) if (ret < 0) return ret; + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, + &vl6180_trigger_handler, + &iio_triggered_buffer_setup_ops); + if (ret) + return ret; + /* Default Range inter-measurement time: 50ms or 20000 mHz */ ret = vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME, vl6180_meas_reg_val_from_mhz(20000)); @@ -598,7 +710,7 @@ static int vl6180_probe(struct i2c_client *client) indio_dev->name = VL6180_DRV_NAME; indio_dev->modes = INDIO_DIRECT_MODE; - ret = vl6180_init(data); + ret = vl6180_init(data, indio_dev); if (ret < 0) return ret; @@ -611,6 +723,19 @@ static int vl6180_probe(struct i2c_client *client) return dev_err_probe(&client->dev, ret, "devm_request_irq error \n"); init_completion(&data->completion); + + data->trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", + indio_dev->name, iio_device_id(indio_dev)); + if (!data->trig) + return -ENOMEM; + + data->trig->ops = &vl6180_trigger_ops; + iio_trigger_set_drvdata(data->trig, indio_dev); + ret = devm_iio_trigger_register(&client->dev, data->trig); + if (ret) + return ret; + + indio_dev->trig = iio_trigger_get(data->trig); } return devm_iio_device_register(&client->dev, indio_dev);