From patchwork Mon Sep 9 10:15:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13796608 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 F0AB5176FDF; Mon, 9 Sep 2024 10:16:13 +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=1725876975; cv=none; b=hIx3FQScTRPTTY7jst+Ad2FG5F7l7+QFXrUe7vtofLjtkP2/1N5r9WGXbaDTrLWjbVysYmx82qaF0jZDXWEje2Wc/dIsneG87oZuyBtt5GcpLOI8sqk2tIGFXkvwcSF2hb10jVMNdo/LSkH4wl/VbO2dTAILzu+Pm5ByLDYO8aM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725876975; c=relaxed/simple; bh=G38sv4XR3xasP3+tvojck46runUd80LpWHyyBF/nHAM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NB1M14xT1tFAf6TJtuUQO/5cYBz1kMts4JXfi/wblHrmnZio/cv+RL5+D7D8m67bLDn+qwVINriY4/HrHb52dw/VClEtOZMWnqSiOI8oNlkHq/z0M1gjpUEVSvW13PIo6jGJbdUZLJ9FJFk0xrgiCPxVAuEoK8eCG8sA1R8yUzk= 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=mY3IbYYD; 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="mY3IbYYD" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-206b9455460so33555035ad.0; Mon, 09 Sep 2024 03:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725876973; x=1726481773; 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=GU1ncfdHT15Ic64HsMbCwYVyILFDii0TETAChQZx3hI=; b=mY3IbYYDsaddUd03L6ixNgt333JETcwggrMi+ZbS+uS7FTiMVOR4Z8hMiKE7BwV/ZP yM0HNLcgvP61+yJuMPS2jKnYh1Iccyn2/W3SFXz0CCK6BUNT0aSB8xH2xYWMxthq8A9v vbTH8nXoPhLY6qMFBkndXmirX7yhgqSMU/csbb65zhQdzFO5wq5ge10/4eO90m1soZc7 byUoqNDnERqgERlr1C8pNjOGmjJ8fdZT7y3r4U6H8x2OWdVQzKMklXZlLmVuya3i0V3x 3YE9xjEFhGymcoNtj/wSs8UvcdLn9qKatpVxTQdbwYFuwxRYm3gc2cFd9bJGFMLlUFqC l24w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725876973; x=1726481773; 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=GU1ncfdHT15Ic64HsMbCwYVyILFDii0TETAChQZx3hI=; b=N4NK02ViHBkE/SGkjU7kHB8eu3kodmfaAznpKgWSswEq2Im8ePGSb84tK3rxEfNrCZ fZ3p/hT3CXhaZyxIoxuE0BA83mrixdOfLitl3nUK1B3pvPXHWN2lA47RXyUqEzbKOcJs +t6h8ELIuesgNMK/YPhA3xcMztjuf1o4JATOol6VGqbFjTA2iV29ET7J1pNi2MUL6QcQ h0/sjrMrVeIH/IFcUO5bmoJnYec7nhIlUN4FLEa5yxuqUPxGKsNvVVHbq7BQVxc+YI2s MRNbqBy55AWuRA7hgdXw8nkOZG7I97C1ZgQuEXFzkPZnFgUkwUUIgsXJ8R9WysHobBL7 oJOw== X-Forwarded-Encrypted: i=1; AJvYcCX/CuVITE5ySdHzjdGcM+2OSyCu6CZQQ/190FXJfo31dTDnn9dif9T+BE7iJ0AZoSZPt4rKB5vscG4ighA=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2BSSjk/UJxXEOvrG/m+AuIrQnU2TfHWVroOz5UlwS2bEBjWfC G7e65gmWfqqeF6BeYdWp/ieI/Edf3A1JD4qovh+qLJZZtt+0TcihQAPuS+ey/B0= X-Google-Smtp-Source: AGHT+IGuDmkIAHFb/Yr5TECZLAkE8FTYj9iiAYknnv3qac0W6ciqlKSMP5WDNwYfjCu6Ri4kOnPbHg== X-Received: by 2002:a17:903:283:b0:205:656d:5f46 with SMTP id d9443c01a7336-206b8461b10mr284684385ad.28.1725876972630; Mon, 09 Sep 2024 03:16:12 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20710e33456sm31219885ad.99.2024.09.09.03.16.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:16:12 -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 v4 1/2] iio: proximity: vl53l0x-i2c: Added sensor ID check Date: Mon, 9 Sep 2024 15:45:06 +0530 Message-ID: <20240909101508.263085-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240909101508.263085-1-abhashkumarjha123@gmail.com> References: <20240909101508.263085-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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/iio/proximity/vl53l0x-i2c.c b/drivers/iio/proximity/vl53l0x-i2c.c index 8d4f3f849..3f416d3db 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,13 @@ 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 < 0) + return -EINVAL; + + if (ret != VL53L0X_MODEL_ID_VAL) + dev_info(&client->dev, "Unknown 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 +276,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 Mon Sep 9 10:15:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhash Jha X-Patchwork-Id: 13796609 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 A0592176FDF; Mon, 9 Sep 2024 10:16:18 +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=1725876980; cv=none; b=lUotMBNlO4l3sbabhEVasAimUYfywKjLKIHIIgiUhmiHhDwHFTFV6siROuQWOuye9LT2oiYOODtmorrqDp9e7RnQ0pFcrTohbCD2pPbMUfWjsSf2GH5FTwS7RFhyVRDd4SCDwH157el/cRrRtytDwWP2Z7bHURPdgr9cN+6eZrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725876980; c=relaxed/simple; bh=Ipn3p+eaJS2AHl3nFasrtBlGR7PiMT9aTTOUsDyc83A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g99OQR48PJ4/Q9LhxgbasrmnUgKryv6dcgWx0S7pH7nfxBN6d9X2WqbN21V38/+7xg4iljcP/DYGTJlpe8i+9cs0twyVZvjv8auEvC/VmI3x81TL472pG9HFQQkji6bUTXYqMggDGGoMtNdF9ybP/XIoA0XjhNzB2ucO/jHpe48= 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=nBG0rvZX; 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="nBG0rvZX" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2054feabfc3so35789085ad.1; Mon, 09 Sep 2024 03:16:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725876977; x=1726481777; 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=iQsxlrcy+BiE9bauUvB+9q1z/3ZnehgyCOhvRDBIG04=; b=nBG0rvZXLkE+jPP5hAU9vIYoE5Hza1ZiVRGJBDhy+5C37q6FZhgthj4T6OE5qkOpNr jGClpX2ccKwmSSnaEr1eq8FwjhR9irV+yEcX9gZ+4uOKZv8vv7obD8VYOHt3MGbV7NHF trjpmRV3io9LzFTzv1QEiCPT4mvcTRvW7JNWmB9W/JhK2vVK53isA+0gZ0IEwRExjSwS 9KGA4iNliNdYjyga2OqlIa7FkguA7ovFGNMfMMcG2NS7vHhlIE7VBvMn2gXDoH2jmk0C T/1XCJY3ygfQTc+AvelNNeIoctLBN4i9iu0DIBSn0KPDtwRiLSdh26rSpgZVgx9L+DZn ayrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725876977; x=1726481777; 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=iQsxlrcy+BiE9bauUvB+9q1z/3ZnehgyCOhvRDBIG04=; b=SgO/Gqf4VfByaNz1k1+U+ZeYbecsx3CojrsfUtgKY480HQeO3vcje3Y71KKW4lsKe6 atsgC97RqInIdVJvyZ6+az1zzmMaHWFx1w9kDWj5cbZu9dzdlFjR72VV2dRFYtRfmh4K ACySogKzuD1I0ZTI5ML7GPHuUv/mu/x+wc5V3EobL973LTFXjbDTLBuBfENIVMt+8GWK nDH5WJYgJx+/rHjlk+CpDTVfD/Z4oDnbW9rogYkl2eagRaDGdHmJYq6lxyZEgulib5Bu RDt21tkQCaAncKcPOqpLIJQlqLXwwdxhdE1j+JaM9dx1Uubm/RSnZdRCPnHHT3A6DG6V LCLQ== X-Forwarded-Encrypted: i=1; AJvYcCXSlEmulfEVSRHUMHucge96fGVKCRGvDDa6M3RqrhuI+GzEqpXOUZX90U1hHUu+KcLfrUsG1Xd6wZwYuhI=@vger.kernel.org X-Gm-Message-State: AOJu0YwBqGegqMRnixgbH54oG/d00a6obiZEyTTK4zM0FrXPh+4S1+jh Wbl6IZKu5p/CE7/KOU0WsIvFWqE9Yvx7pPoTiFFJ170t5rT/R3cY4kNEXvIVKB4= X-Google-Smtp-Source: AGHT+IEmQoUevVqnjJgBVQ+taceCDLeGypGu+eo6fntikBkWEszXTf0/CN97H1juOFIQoAU340NQrQ== X-Received: by 2002:a17:902:ecd2:b0:1fd:67c2:f97f with SMTP id d9443c01a7336-206f0566595mr132666295ad.28.1725876976721; Mon, 09 Sep 2024 03:16:16 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20710e33456sm31219885ad.99.2024.09.09.03.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:16:16 -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 v4 2/2] iio: proximity: vl53l0x-i2c: Added continuous mode support Date: Mon, 9 Sep 2024 15:45:07 +0530 Message-ID: <20240909101508.263085-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240909101508.263085-1-abhashkumarjha123@gmail.com> References: <20240909101508.263085-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. Added a trigger for this device to be used for continuous mode. Signed-off-by: Abhash Jha --- drivers/iio/proximity/vl53l0x-i2c.c | 161 +++++++++++++++++++++++----- 1 file changed, 135 insertions(+), 26 deletions(-) diff --git a/drivers/iio/proximity/vl53l0x-i2c.c b/drivers/iio/proximity/vl53l0x-i2c.c index 3f416d3db..cbf030869 100644 --- a/drivers/iio/proximity/vl53l0x-i2c.c +++ b/drivers/iio/proximity/vl53l0x-i2c.c @@ -22,6 +22,12 @@ #include #include +#include +#include +#include +#include + +#include #define VL_REG_SYSRANGE_START 0x00 @@ -43,20 +49,70 @@ #define VL_REG_RESULT_RANGE_STATUS_COMPLETE BIT(0) #define VL53L0X_MODEL_ID_VAL 0xEE +#define VL53L0X_CONTINUOUS_MODE 0x02 +#define VL53L0X_SINGLE_MODE 0x01 struct vl53l0x_data { struct i2c_client *client; struct completion completion; struct regulator *vdd_supply; struct gpio_desc *reset_gpio; + struct iio_trigger *trig; + + struct { + u16 chan; + s64 timestamp __aligned(8); + } scan; }; -static irqreturn_t vl53l0x_handle_irq(int irq, void *priv) +static int vl53l0x_clear_irq(struct vl53l0x_data *data) +{ int ret; + + ret = i2c_smbus_write_byte_data(data->client, + VL_REG_SYSTEM_INTERRUPT_CLEAR, 1); + if (ret < 0) { + dev_err(&data->client->dev, "failed to clear irq: %d\n", ret); + return -EINVAL; + } + + return 0; +} + +static irqreturn_t vl53l0x_trigger_handler(int irq, void *priv) +{ + struct iio_poll_func *pf = priv; + struct iio_dev *indio_dev = pf->indio_dev; + struct vl53l0x_data *data = iio_priv(indio_dev); + u8 buffer[12]; + int ret; + + 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 = get_unaligned_be16(&buffer[10]); + 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); + + return IRQ_HANDLED; +} + +static irqreturn_t vl53l0x_threaded_irq(int irq, void *priv) { struct iio_dev *indio_dev = priv; struct vl53l0x_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; } @@ -71,8 +127,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 +144,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) @@ -128,7 +165,9 @@ static int vl53l0x_read_proximity(struct vl53l0x_data *data, if (time_left == 0) return -ETIMEDOUT; - vl53l0x_clear_irq(data); + ret = vl53l0x_clear_irq(data); + if (ret < 0) + return ret; } else { do { ret = i2c_smbus_read_byte_data(client, @@ -153,7 +192,7 @@ static int vl53l0x_read_proximity(struct vl53l0x_data *data, return -EREMOTEIO; /* Values should be between 30~1200 in millimeters. */ - *val = (buffer[10] << 8) + buffer[11]; + *val = get_unaligned_be16(&buffer[10]); return 0; } @@ -163,7 +202,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(1), }; static int vl53l0x_read_raw(struct iio_dev *indio_dev, @@ -193,8 +239,16 @@ static int vl53l0x_read_raw(struct iio_dev *indio_dev, } } +static int vl53l0x_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig) +{ + struct vl53l0x_data *data = iio_priv(indio_dev); + + return data->trig == trig ? 0 : -EINVAL; +} + static const struct iio_info vl53l0x_info = { .read_raw = vl53l0x_read_raw, + .validate_trigger = vl53l0x_validate_trigger, }; static void vl53l0x_power_off(void *_data) @@ -221,6 +275,39 @@ 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); + + return i2c_smbus_write_byte_data(data->client, VL_REG_SYSRANGE_START, + VL53L0X_CONTINUOUS_MODE); +} + +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, + VL53L0X_SINGLE_MODE); + if (ret < 0) + return ret; + + /* Let the ongoing reading finish */ + reinit_completion(&data->completion); + wait_for_completion_timeout(&data->completion, HZ / 10); + return vl53l0x_clear_irq(data); +} + +static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { + .postenable = &vl53l0x_buffer_postenable, + .postdisable = &vl53l0x_buffer_postdisable, +}; + +static const struct iio_trigger_ops vl53l0x_trigger_ops = { + .validate_device = iio_trigger_validate_own_device, +}; + static int vl53l0x_probe(struct i2c_client *client) { struct vl53l0x_data *data; @@ -278,9 +365,31 @@ static int vl53l0x_probe(struct i2c_client *client) if (client->irq) { 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 = &vl53l0x_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); + ret = vl53l0x_configure_irq(client, indio_dev); if (ret) return ret; + + ret = devm_iio_triggered_buffer_setup(&client->dev, + indio_dev, + NULL, + &vl53l0x_trigger_handler, + &iio_triggered_buffer_setup_ops); + if (ret) + return ret; } return devm_iio_device_register(&client->dev, indio_dev);