From e950b48432419d7e3c15f479e571f48ae52ea689 Mon Sep 17 00:00:00 2001 From: Ethan Duckett Date: Wed, 25 Oct 2023 17:13:54 +0100 Subject: [PATCH] drivers: adc: ltc2451: Add ltc2451 driver Adds support for the Linear Technologies LTC2451 ADC. Signed-off-by: Ethan Duckett --- drivers/adc/CMakeLists.txt | 1 + drivers/adc/Kconfig | 2 + drivers/adc/Kconfig.ltc2451 | 8 ++ drivers/adc/adc_ltc2451.c | 107 ++++++++++++++++++ dts/bindings/adc/lltc,ltc2451.yaml | 19 ++++ .../build_all/adc/boards/native_posix.overlay | 7 ++ tests/drivers/build_all/adc/testcase.yaml | 1 + 7 files changed, 145 insertions(+) create mode 100644 drivers/adc/Kconfig.ltc2451 create mode 100644 drivers/adc/adc_ltc2451.c create mode 100644 dts/bindings/adc/lltc,ltc2451.yaml diff --git a/drivers/adc/CMakeLists.txt b/drivers/adc/CMakeLists.txt index 4738aaa891c922b..34536f4c4e009df 100644 --- a/drivers/adc/CMakeLists.txt +++ b/drivers/adc/CMakeLists.txt @@ -47,3 +47,4 @@ zephyr_library_sources_ifdef(CONFIG_ADC_NXP_S32_ADC_SAR adc_nxp_s32_adc_sar.c) zephyr_library_sources_ifdef(CONFIG_ADC_MAX1125X adc_max1125x.c) zephyr_library_sources_ifdef(CONFIG_ADC_MAX11102_17 adc_max11102_17.c) zephyr_library_sources_ifdef(CONFIG_ADC_AD5592 adc_ad5592.c) +zephyr_library_sources_ifdef(CONFIG_ADC_LTC2451 adc_ltc2451.c) diff --git a/drivers/adc/Kconfig b/drivers/adc/Kconfig index 14f4d89657e4a65..b52183c2b476bc5 100644 --- a/drivers/adc/Kconfig +++ b/drivers/adc/Kconfig @@ -118,4 +118,6 @@ source "drivers/adc/Kconfig.max11102_17" source "drivers/adc/Kconfig.ad5592" +source "drivers/adc/Kconfig.ltc2451" + endif # ADC diff --git a/drivers/adc/Kconfig.ltc2451 b/drivers/adc/Kconfig.ltc2451 new file mode 100644 index 000000000000000..8f3cb384cfbc724 --- /dev/null +++ b/drivers/adc/Kconfig.ltc2451 @@ -0,0 +1,8 @@ +# Copyright (c) 2023 Brill Power +# SPDX-License-Identifier: Apache-2.0 + +config ADC_LTC2451 + bool "LTC2451 driver" + default y + depends on DT_HAS_LLTC_LTC2451_ENABLED + select I2C diff --git a/drivers/adc/adc_ltc2451.c b/drivers/adc/adc_ltc2451.c new file mode 100644 index 000000000000000..479ef22cd5ab139 --- /dev/null +++ b/drivers/adc/adc_ltc2451.c @@ -0,0 +1,107 @@ +/* LLTC LTC2451 ADC + * + * Copyright (c) 2023 Brill Power Ltd. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(ltc2451, CONFIG_ADC_LOG_LEVEL); + +#define DT_DRV_COMPAT lltc_ltc2451 + +struct ltc2451_config { + struct i2c_dt_spec i2c; + uint8_t conversion_speed; +}; + +static int ltc2451_channel_setup(const struct device *dev, + const struct adc_channel_cfg *channel_cfg) +{ + (void)dev; + + if (channel_cfg->channel_id != 0) { + LOG_ERR("Invalid channel id '%d'", channel_cfg->channel_id); + return -EINVAL; + } + + return 0; +} + +static int ltc2451_set_conversion_speed(const struct device *dev, uint8_t conversion_speed) +{ + const struct ltc2451_config *config = dev->config; + uint8_t wr_buf[1]; + int err; + + if (conversion_speed == 60) { + wr_buf[0] = 0; + } else if (conversion_speed == 30) { + wr_buf[0] = 1; + } else { + LOG_ERR("Invalid conversion speed selected"); + return -EINVAL; + } + + err = i2c_write_dt(&config->i2c, wr_buf, sizeof(wr_buf)); + + if (err != 0) { + LOG_ERR("LTC write failed (err %d)", err); + } + + return err; +} + +static int ltc2451_read_latest_conversion(const struct device *dev, + const struct adc_sequence *sequence) +{ + uint8_t rd_buf[2]; + const struct ltc2451_config *config = dev->config; + uint16_t *value_buf; + + int err = i2c_read_dt(&config->i2c, rd_buf, sizeof(rd_buf)); + + if (err == 0) { + value_buf = (uint16_t *)sequence->buffer; + value_buf[0] = (rd_buf[0] << 8) | rd_buf[1]; + } else { + LOG_ERR("LTC read failed (err %d)", err); + } + + return err; +} + +static int ltc2451_init(const struct device *dev) +{ + const struct ltc2451_config *config = dev->config; + + if (!device_is_ready(config->i2c.bus)) { + LOG_ERR("I2C device not ready"); + return -ENODEV; + } + + int err = ltc2451_set_conversion_speed(dev, config->conversion_speed); + + return err; +} + +static const struct adc_driver_api ltc2451_api = { + .channel_setup = ltc2451_channel_setup, + .read = ltc2451_read_latest_conversion, +}; + +#define INIT_LTC2451_DEVICE(index) \ + static const struct ltc2451_config ltc2451_cfg_##index = { \ + .i2c = I2C_DT_SPEC_INST_GET(index), \ + .conversion_speed = DT_INST_PROP(index, conversion_speed), \ + }; \ + \ + DEVICE_DT_INST_DEFINE(index, <c2451_init, NULL, NULL, \ + <c2451_cfg_##index, POST_KERNEL, CONFIG_ADC_INIT_PRIORITY, \ + <c2451_api); + +DT_INST_FOREACH_STATUS_OKAY(INIT_LTC2451_DEVICE) diff --git a/dts/bindings/adc/lltc,ltc2451.yaml b/dts/bindings/adc/lltc,ltc2451.yaml new file mode 100644 index 000000000000000..8e1ae19eea5eb92 --- /dev/null +++ b/dts/bindings/adc/lltc,ltc2451.yaml @@ -0,0 +1,19 @@ +# Copyright (c) 2023 Brill Power Ltd. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +description: Linear Technology LTC2451 ADC + +compatible: "lltc,ltc2451" + +include: base.yaml + +properties: + reg: + required: true + + conversion-speed: + type: int + enum: + - 30 + - 60 + description: Set conversion speed in Hz diff --git a/tests/drivers/build_all/adc/boards/native_posix.overlay b/tests/drivers/build_all/adc/boards/native_posix.overlay index 7f6f64243c8c04e..c128185f949679f 100644 --- a/tests/drivers/build_all/adc/boards/native_posix.overlay +++ b/tests/drivers/build_all/adc/boards/native_posix.overlay @@ -87,6 +87,13 @@ reg = <0x7>; #io-channel-cells = <1>; }; + + test_i2c_ltc2451: ltc2451@8{ + compatible = "lltc,ltc2451"; + reg = <0x8>; + conversion-speed = <60>; + #io-channel-cells = <1>; + }; }; test_spi: spi@33334444 { diff --git a/tests/drivers/build_all/adc/testcase.yaml b/tests/drivers/build_all/adc/testcase.yaml index cde46e57a09492c..c8a6fe5e3a7786b 100644 --- a/tests/drivers/build_all/adc/testcase.yaml +++ b/tests/drivers/build_all/adc/testcase.yaml @@ -18,6 +18,7 @@ tests: - adc_ads114s08 - adc_emul - adc_max1125x + - adc_ltc2451 extra_args: "CONFIG_GPIO=y" drivers.adc.cc32xx.build: platform_allow: cc3220sf_launchxl