From patchwork Fri Aug 30 20:16:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13785517 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.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 CF7A91BD503; Fri, 30 Aug 2024 20:17:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049033; cv=none; b=TDik8rUCBrlkyZDK2Rmeq+mFCVwmMzzX+BTjBZzn0BroOnz58UmTW1zhLUvQyqogqTZKr/SmFePOBFcGbDN+gdJuAR1MaiAkgjZKUWGJ1zXuWtW3aIau9ZodSo47Z3OL3dTkCq35XljhB4801oKiZyNbPrUdjXoyOEViUr+zwwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049033; c=relaxed/simple; bh=FtQB4eRlURsdLorLAN11Um1AdZ7GI+yphW6uG1KZCRs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hVG3nE5lpYJfD/sLaeIBQdyWm1CIdlM8XPvKG+hAxuRm4DeJWujXfPaUl07DhRTCsIDK/OcUctDSMbI4LPMlULmRnPBxHQHmWSy5PVMHtUjPQ7Z/qhCV/5Pp+OsEs7MXsKL4Y73S5C4wJVTJU4433jw4yus8b591XhgZtAkRzck= 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=Z6YuqOc5; arc=none smtp.client-ip=209.85.210.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="Z6YuqOc5" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-714226888dfso1859783b3a.1; Fri, 30 Aug 2024 13:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725049030; x=1725653830; 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=mhoIbh0jbk7zzRX7G0VaVBoQVyOigzSeO5Rn8EjIAQY=; b=Z6YuqOc51xZLSxYXOaZZclj/ekF/ZlupN9ACzIM9u/ejGFlC4JD3WS1xU961e5xoZS 0B0NJN+4QDHp5eQe7A4JICzIX9AD0nn3sVDlIt4PA4hACca5mTKTc8427NiT/U36MY5q PYcL6I/LK9TVrBrFzNbaNZea/wUZiOiaTvBMSsVXj+3pNFSf0942wnv8g4scFSicihOh +ahMXACbvZZI6hVRrnzlXdCu+XlL14hkPC5R3K/ApXYw4weptI5Cx5LdntCCUCAMw+EJ 6aTTnfMgMQoomNbOtqPzK99MdVMYAAvTWGQJImwogEPebNif1tmbjob/Jj5Gj5ke4X6E EKbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725049030; x=1725653830; 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=mhoIbh0jbk7zzRX7G0VaVBoQVyOigzSeO5Rn8EjIAQY=; b=wPYn8TpTSVqEdLKqJxdr3PN5XmZJIpXd63/zY1pBPDdem6Y9diJCgBjcHERj8riv0/ byFRwEXyzrFD0btJxgXjaltnFp2Ow+RLtCr3mwexf+DFPHEflkH8ShhgNBQPEXvBQlVt gPW+MjApWs0e9E87J1xTjqp/SJX1Qbf/R3vzZ+A5vgm0RYylkJrhhtTkSfA9zllSGtDE o8z84h0c7SrLgwW2IelR4XdFROsLlBZuiS8Bp1zaIrvcH8vrSDWHTBkxJq2HHQp1jjoa IL79r9JefzziDcdFMlMaZtXk5lT7dI/IVLGtWGDUaNvlN2KtDee4ZIv9IdXTN4pZmExB 55Ug== X-Forwarded-Encrypted: i=1; AJvYcCUr+AGl0P7ixn1gne+gyqaAC9CyCj/HXJ4yfl82oxyCTQvUETuV2DEEuGGXM5rcM4WEH8lXxzGzxtt1lIY=@vger.kernel.org X-Gm-Message-State: AOJu0YwMd52LtHhvi0ailY9kRgBwbLtU4Y0vxfFkydV3Lfgbg7DjqfRu 9acOUzwPDYzfjGvybkbxGiVzqs7qO5fZEF66C0Sk0vpjW0C48vS2dC6aHq// X-Google-Smtp-Source: AGHT+IF6YrWJ0OeuN8M/KkQiFn44MDFRMKfb1t24dErWx+/7RrTPEFKDaSspDdeLI5oIaSdlkGqWJA== X-Received: by 2002:a05:6a21:78b:b0:1c4:7d53:bf76 with SMTP id adf61e73a8af0-1cce10aafe2mr7423902637.38.1725049030464; Fri, 30 Aug 2024 13:17:10 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([2401:4900:608e:fa44:845a:20a9:791f:c32b]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-715e56e5c23sm3257476b3a.172.2024.08.30.13.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 13:17:10 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: songqiang1304521@gmail.com, jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH 1/2] iio: proximity: vl53l0x-i2c: Added sensor ID check Date: Sat, 31 Aug 2024 01:46:25 +0530 Message-ID: <20240830201627.298264-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830201627.298264-1-abhashkumarjha123@gmail.com> References: <20240830201627.298264-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 commit adds a check for the sensor's model ID. We read the model identification register (0xC0) and expect a value of 0xEE. Signed-off-by: Abhash Jha --- drivers/iio/proximity/vl53l0x-i2c.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/iio/proximity/vl53l0x-i2c.c b/drivers/iio/proximity/vl53l0x-i2c.c index 8d4f3f849..2b3dd18be 100644 --- a/drivers/iio/proximity/vl53l0x-i2c.c +++ b/drivers/iio/proximity/vl53l0x-i2c.c @@ -39,8 +39,11 @@ #define VL_REG_RESULT_INT_STATUS 0x13 #define VL_REG_RESULT_RANGE_STATUS 0x14 +#define VL_REG_IDENTIFICATION_MODEL_ID 0xC0 #define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0) +#define VL53L0X_MODEL_ID_VAL 0xEE + struct vl53l0x_data { struct i2c_client *client; struct completion completion; @@ -223,6 +226,7 @@ static int vl53l0x_probe(struct i2c_client *client) struct vl53l0x_data *data; struct iio_dev *indio_dev; int error; + int ret; indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); if (!indio_dev) @@ -237,6 +241,11 @@ static int vl53l0x_probe(struct i2c_client *client) I2C_FUNC_SMBUS_BYTE_DATA)) return -EOPNOTSUPP; + ret = i2c_smbus_read_byte_data(data->client, VL_REG_IDENTIFICATION_MODEL_ID); + if (ret != VL53L0X_MODEL_ID_VAL) + return dev_err_probe(&client->dev, ret, + "Received invalid model id: 0x%x", ret); + data->vdd_supply = devm_regulator_get(&client->dev, "vdd"); if (IS_ERR(data->vdd_supply)) return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply), @@ -265,8 +274,6 @@ static int vl53l0x_probe(struct i2c_client *client) /* usage of interrupt is optional */ if (client->irq) { - int ret; - init_completion(&data->completion); ret = vl53l0x_configure_irq(client, indio_dev); From patchwork Fri Aug 30 20:16:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13785518 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 F00AB1BF31D; Fri, 30 Aug 2024 20:17:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049043; cv=none; b=o37vPXu6QTNVLrQUYuHwzeHUygQKhPvDqDdJycrTwv7Tm+fzqWgd4tXH5WrFxxBBFrzdXZUr9a+Pgc2Rcq8AgpmXnAud3Hgred05K5u2qR6Vx3uXkYo1uXSum6SWk+WkZ53+srF+lYfYXaRLbzkyceSMqA149zW44dBKq6BHdFU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725049043; c=relaxed/simple; bh=kuGb3WJ3qlPjwbQZqwSoMTeTEWH8fLqpvs4rAg86zuc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rMXacisPWCsciu98c3nGNK5NpjKXRB3UtLAkxKArQ5HJE5Spq8+MAGey7ELRchqXShN/bckC+lwg0qiSay2M1Gy7r+Q4DZpTkm8wvO/EvyH33FD5X8Q0KbRXCSODlfmirP0m1OhAjksboA+m/ti/A3Tis7SR2UH7FZSnH8zwY6E= 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=a3XTdsU8; arc=none smtp.client-ip=209.85.210.174 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="a3XTdsU8" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-714287e4083so2028582b3a.2; Fri, 30 Aug 2024 13:17:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725049041; x=1725653841; 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=mi+Say77cH1ylZ4XneSFtnKiT60ynjvcD6h/n+L53uA=; b=a3XTdsU8KX4Dj/5dzJiZO5YAw5BIT0U6TnmJfFoP0kploYGfRpMQcRV2lkLlJQBqdC YlSIEtCv13l6sVf40yvSJeiFWDRZN/kguuQ/wpQa0ufVxeYxbW5xAVDB3gc/5TMd5GOu LGZGYTVhcxjGSKKOC8orpBbK2sRLa924/WB8WeAv2uKZ1NLW5NTq3JTAhsHruEpi0yeA YGqr06wJ8ibD90VaSHmAiPoYDdF52OEIIW8nk1buPg2eDKhMxMr/ezmrr1ox61CI2QZ9 LAsRwvNnuVSDbl0vmYtzwpLjlLonTA39i8D3wsCGNak352x08UDddhBK53mOmyPRolkR EeVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725049041; x=1725653841; 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=mi+Say77cH1ylZ4XneSFtnKiT60ynjvcD6h/n+L53uA=; b=jCYeoMsSTyBrJoRg87l/FNZ/G14vaFdrDVM2RvdwMVijF3jdyaq31l3D0kFRAR0FBz HO7RQ5YQjhX2D5bsqe8cBH6HIpu4bN8NZfHgIoxUr7L9m9Igoz0+hnSHX5yYrtS9HowU mYxBCpndVs8y54BPZb8ovQeO63jL8TtuCe4mwv42pR4XHYg2nLe6c0d+1FFwJGKIKOvj 3ZMcrEn+iXQCXNDoVyhp0ligdLjLoFZg6EMLFqL/6QbdBOO9gyc8a8OTpKrDVi7Jf/xT WXiokn1k5n3/JlulP+c+kfEyEGUrCsxwmC/LC+oGDqJWotzU/JBxsHHs4X621oLNYTpR 37Rg== X-Forwarded-Encrypted: i=1; AJvYcCUCVhLAjqbOUBs1O8JXkvOA5abC9+djjh2SLPtVv5c0zcJbHpY6AhuM7ARDh6evGL9mDGFN3zvzVxW/MSM=@vger.kernel.org X-Gm-Message-State: AOJu0YxTcImFXk3NOI4riHUInzXJXTrT0FffyR8eOveYI/Unh8KLdQhA H+s1cfuRtS4oFTXVa/MIStrMyLIqKu7EBYmQNeSO0Vf6hzXyyWJvynIWkO/h X-Google-Smtp-Source: AGHT+IH77ZkU6rLkTqxj259tg7vHn96jqdsZUiM0N5nxtf3ldYpsu6QUlittVbx6fPS/DmNHvWjkHQ== X-Received: by 2002:a05:6a00:2388:b0:70d:2693:d215 with SMTP id d2e1a72fcca58-7173b60ca4emr1196059b3a.16.1725049040592; Fri, 30 Aug 2024 13:17:20 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([2401:4900:608e:fa44:845a:20a9:791f:c32b]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-715e56e5c23sm3257476b3a.172.2024.08.30.13.17.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 13:17:20 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: songqiang1304521@gmail.com, jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH 2/2] iio: proximity: vl53l0x-i2c: Added continuous mode support Date: Sat, 31 Aug 2024 01:46:26 +0530 Message-ID: <20240830201627.298264-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240830201627.298264-1-abhashkumarjha123@gmail.com> References: <20240830201627.298264-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 continuous mode of the sensor is enabled in the buffer_postenable. Replaced the original irq handler with a threaded irq handler to perform i2c reads during continuous mode. The continuous mode is disabled by disabling the buffer. Signed-off-by: Abhash Jha --- drivers/iio/proximity/vl53l0x-i2c.c | 125 ++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 24 deletions(-) diff --git a/drivers/iio/proximity/vl53l0x-i2c.c b/drivers/iio/proximity/vl53l0x-i2c.c index 2b3dd18be..b0c947586 100644 --- a/drivers/iio/proximity/vl53l0x-i2c.c +++ b/drivers/iio/proximity/vl53l0x-i2c.c @@ -22,6 +22,10 @@ #include #include +#include +#include +#include +#include #define VL_REG_SYSRANGE_START 0x00 @@ -49,14 +53,58 @@ struct vl53l0x_data { struct completion completion; struct regulator *vdd_supply; struct gpio_desc *reset_gpio; + + struct { + u16 chan; + s64 timestamp __aligned(8); + } scan; }; -static irqreturn_t vl53l0x_handle_irq(int irq, void *priv) +static void vl53l0x_clear_irq(struct vl53l0x_data *data) +{ + struct device *dev = &data->client->dev; + int ret; + + ret = i2c_smbus_write_byte_data(data->client, + VL_REG_SYSTEM_INTERRUPT_CLEAR, 1); + if (ret < 0) + dev_err(dev, "failed to clear error irq: %d\n", ret); + + ret = i2c_smbus_write_byte_data(data->client, + VL_REG_SYSTEM_INTERRUPT_CLEAR, 0); + if (ret < 0) + dev_err(dev, "failed to clear range irq: %d\n", ret); + + ret = i2c_smbus_read_byte_data(data->client, VL_REG_RESULT_INT_STATUS); + if (ret < 0 || ret & 0x07) + dev_err(dev, "failed to clear irq: %d\n", ret); +} + +static irqreturn_t vl53l0x_threaded_irq(int irq, void *priv) { struct iio_dev *indio_dev = priv; struct vl53l0x_data *data = iio_priv(indio_dev); + u8 buffer[12]; + int ret; - complete(&data->completion); + if (iio_buffer_enabled(indio_dev)) { + ret = i2c_smbus_read_i2c_block_data(data->client, + VL_REG_RESULT_RANGE_STATUS, + sizeof(buffer), buffer); + if (ret < 0) + return ret; + else if (ret != 12) + return -EREMOTEIO; + + data->scan.chan = (buffer[10] << 8) + buffer[11]; + iio_push_to_buffers_with_timestamp(indio_dev, + &data->scan, + iio_get_time_ns(indio_dev)); + + iio_trigger_notify_done(indio_dev->trig); + vl53l0x_clear_irq(data); + } else + complete(&data->completion); return IRQ_HANDLED; } @@ -71,8 +119,9 @@ static int vl53l0x_configure_irq(struct i2c_client *client, if (!irq_flags) irq_flags = IRQF_TRIGGER_FALLING; - ret = devm_request_irq(&client->dev, client->irq, vl53l0x_handle_irq, - irq_flags, indio_dev->name, indio_dev); + ret = devm_request_threaded_irq(&client->dev, client->irq, + NULL, vl53l0x_threaded_irq, + irq_flags | IRQF_ONESHOT, indio_dev->name, indio_dev); if (ret) { dev_err(&client->dev, "devm_request_irq error: %d\n", ret); return ret; @@ -87,26 +136,6 @@ static int vl53l0x_configure_irq(struct i2c_client *client, return ret; } -static void vl53l0x_clear_irq(struct vl53l0x_data *data) -{ - struct device *dev = &data->client->dev; - int ret; - - ret = i2c_smbus_write_byte_data(data->client, - VL_REG_SYSTEM_INTERRUPT_CLEAR, 1); - if (ret < 0) - dev_err(dev, "failed to clear error irq: %d\n", ret); - - ret = i2c_smbus_write_byte_data(data->client, - VL_REG_SYSTEM_INTERRUPT_CLEAR, 0); - if (ret < 0) - dev_err(dev, "failed to clear range irq: %d\n", ret); - - ret = i2c_smbus_read_byte_data(data->client, VL_REG_RESULT_INT_STATUS); - if (ret < 0 || ret & 0x07) - dev_err(dev, "failed to clear irq: %d\n", ret); -} - static int vl53l0x_read_proximity(struct vl53l0x_data *data, const struct iio_chan_spec *chan, int *val) @@ -163,7 +192,14 @@ static const struct iio_chan_spec vl53l0x_channels[] = { .type = IIO_DISTANCE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .scan_index = 0, + .scan_type = { + .sign = 'u', + .realbits = 12, + .storagebits = 16, + }, }, + IIO_CHAN_SOFT_TIMESTAMP(32), }; static int vl53l0x_read_raw(struct iio_dev *indio_dev, @@ -221,6 +257,40 @@ static int vl53l0x_power_on(struct vl53l0x_data *data) return 0; } +static int vl53l0x_buffer_postenable(struct iio_dev *indio_dev) +{ + struct vl53l0x_data *data = iio_priv(indio_dev); + int ret; + + ret = i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START, 0x02); + if (ret < 0) + return ret; + + return 0; +} + +static int vl53l0x_buffer_postdisable(struct iio_dev *indio_dev) +{ + struct vl53l0x_data *data = iio_priv(indio_dev); + int ret; + + ret = i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START, 0x01); + if (ret < 0) + return ret; + + /* Let the ongoing reading finish */ + reinit_completion(&data->completion); + wait_for_completion_timeout(&data->completion, HZ/10); + vl53l0x_clear_irq(data); + + return 0; +} + +static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { + .postenable = &vl53l0x_buffer_postenable, + .postdisable = &vl53l0x_buffer_postdisable, +}; + static int vl53l0x_probe(struct i2c_client *client) { struct vl53l0x_data *data; @@ -279,6 +349,13 @@ static int vl53l0x_probe(struct i2c_client *client) ret = vl53l0x_configure_irq(client, indio_dev); if (ret) return ret; + + ret = devm_iio_triggered_buffer_setup(&client->dev, + indio_dev, + &iio_pollfunc_store_time, + NULL, &iio_triggered_buffer_setup_ops); + if (ret) + return ret; } return devm_iio_device_register(&client->dev, indio_dev);