Skip to content

drivers: sensor: add Garmin LIDAR-Lite v4 LED distance sensor driver #93063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drivers/sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_subdirectory(bosch)
add_subdirectory(broadcom)
add_subdirectory(espressif)
add_subdirectory(everlight)
add_subdirectory(grmn)
add_subdirectory(honeywell)
add_subdirectory(infineon)
add_subdirectory(ite)
Expand Down
1 change: 1 addition & 0 deletions drivers/sensor/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ source "drivers/sensor/bosch/Kconfig"
source "drivers/sensor/broadcom/Kconfig"
source "drivers/sensor/espressif/Kconfig"
source "drivers/sensor/everlight/Kconfig"
source "drivers/sensor/grmn/Kconfig"
source "drivers/sensor/honeywell/Kconfig"
source "drivers/sensor/infineon/Kconfig"
source "drivers/sensor/ite/Kconfig"
Expand Down
6 changes: 6 additions & 0 deletions drivers/sensor/grmn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2025 Alex Aylward
# SPDX-License-Identifier: Apache-2.0

# zephyr-keep-sorted-start
add_subdirectory_ifdef(CONFIG_LIDAR_LITE_V4 lidar_lite_v4)
# zephyr-keep-sorted-stop
6 changes: 6 additions & 0 deletions drivers/sensor/grmn/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2025 Alex Aylward
# SPDX-License-Identifier: Apache-2.0

# zephyr-keep-sorted-start
source "drivers/sensor/grmn/lidar_lite_v4/Kconfig"
# zephyr-keep-sorted-stop
5 changes: 5 additions & 0 deletions drivers/sensor/grmn/lidar_lite_v4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2025 Alex Aylward
# SPDX-License-Identifier: Apache-2.0

zephyr_library()
zephyr_library_sources(lidar_lite_v4.c)
10 changes: 10 additions & 0 deletions drivers/sensor/grmn/lidar_lite_v4/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Alex Aylward
# SPDX-License-Identifier: Apache-2.0

config LIDAR_LITE_V4
bool "Lidar Lite V4 support"
depends on DT_HAS_GRMN_LIDAR_LITE_V4_ENABLED
default y
select I2C
help
Driver for Lidar Lite V4 I2C Operation.
123 changes: 123 additions & 0 deletions drivers/sensor/grmn/lidar_lite_v4/lidar_lite_v4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2025 Alex Aylward
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT grmn_lidar_lite_v4

#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/clock.h>
#include <zephyr/sys/time_units.h>

#define MAX_TIMEOUT_MS 100

#define LIDAR_LITE_V4_REG_MEASURE 0x00
#define LIDAR_LITE_V4_REG_STATUS 0x01
#define LIDAR_LITE_V4_REG_DISTANCE_LOW 0x10

#define LIDAR_LITE_V4_CMD_MEASURE 0x04
#define LIDAR_LITE_V4_STATUS_BUSY BIT(0)

struct lidar_lite_v4_config {
struct i2c_dt_spec i2c;
};

struct lidar_lite_v4_data {
uint16_t distance;
};

int lidar_lite_v4_init(const struct device *dev)
{
const struct lidar_lite_v4_config *config = dev->config;

if (!i2c_is_ready_dt(&config->i2c)) {
return -ENODEV;
}

return 0;
}

static int lidar_lite_v4_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
struct lidar_lite_v4_data *data = dev->data;
const struct lidar_lite_v4_config *config = dev->config;
uint8_t status;
uint8_t distance_bytes[2];
int ret;

if (chan != SENSOR_CHAN_DISTANCE && chan != SENSOR_CHAN_ALL) {
return -ENOTSUP;
}

/* Write command to trigger measurement */
ret = i2c_reg_write_byte_dt(&config->i2c, LIDAR_LITE_V4_REG_MEASURE,
LIDAR_LITE_V4_CMD_MEASURE);
if (ret < 0) {
return ret;
}

/* Read status register and wait until measurement completes (timeout based on elapsed time)
*/
k_timepoint_t timeout = sys_timepoint_calc(K_MSEC(MAX_TIMEOUT_MS));
while (1) {
ret = i2c_reg_read_byte_dt(&config->i2c, LIDAR_LITE_V4_REG_STATUS, &status);
if (ret < 0) {
return ret;
}

if ((status & LIDAR_LITE_V4_STATUS_BUSY) == 0) {
break;
}

if (sys_timepoint_expired(timeout)) {
return -ETIMEDOUT;
}

k_msleep(1);
}

/* Read distance data (low byte at 0x10, high byte at 0x11) */
ret = i2c_burst_read_dt(&config->i2c, LIDAR_LITE_V4_REG_DISTANCE_LOW, distance_bytes, 2);
if (ret < 0) {
return ret;
}

/* Combine low and high bytes (little-endian) to get 16-bit distance in centimeters */
data->distance = sys_get_le16(distance_bytes);

return 0;
}

static int lidar_lite_v4_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
struct lidar_lite_v4_data *data = dev->data;

switch (chan) {
case SENSOR_CHAN_DISTANCE:
return sensor_value_from_milli(val, (uint32_t)data->distance * 10);
default:
return -ENOTSUP;
}
}

static const struct sensor_driver_api lidar_lite_v4_driver_api = {
.sample_fetch = lidar_lite_v4_sample_fetch,
.channel_get = lidar_lite_v4_channel_get,
};

#define LIDAR_LITE_V4_DEFINE(inst) \
static struct lidar_lite_v4_data lidar_lite_v4_data_##inst; \
static const struct lidar_lite_v4_config lidar_lite_v4_config_##inst = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
}; \
SENSOR_DEVICE_DT_INST_DEFINE(inst, lidar_lite_v4_init, NULL, &lidar_lite_v4_data_##inst, \
&lidar_lite_v4_config_##inst, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, &lidar_lite_v4_driver_api);

DT_INST_FOREACH_STATUS_OKAY(LIDAR_LITE_V4_DEFINE)
8 changes: 8 additions & 0 deletions dts/bindings/sensor/grmn,lidar-lite-v4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2025 Alex Aylward
# SPDX-License-Identifier: Apache-2.0

description: Garmin LIDAR-Lite v4 laser rangefinder

compatible: "grmn,lidar-lite-v4"

include: [sensor-device.yaml, i2c-device.yaml]
5 changes: 5 additions & 0 deletions tests/drivers/build_all/sensor/i2c.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -1382,3 +1382,8 @@ test_i2c_lsm9ds1_mag: lsm9ds1_mag@b9 {
compatible = "st,lsm9ds1_mag";
reg = <0xb9>;
};

test_i2c_lidar_lite_v4: lidar_lite_v4@ba {
compatible = "grmn,lidar-lite-v4";
reg = <0xba>;
};