-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drivers: adc: ltc2451: Add ltc2451 driver
Adds support for the Linear Technologies LTC2451 ADC. Signed-off-by: Ethan Duckett <[email protected]>
- Loading branch information
1 parent
4df0722
commit 7c3e540
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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 | ||
|
||
module = ADC_LTC2451 | ||
module-str = ADC_LTC2451 | ||
source "subsys/logging/Kconfig.template.log_config" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* LLTC LTC2451 ADC | ||
* | ||
* Copyright (c) 2023 Brill Power Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/device.h> | ||
#include <zephyr/drivers/adc.h> | ||
#include <zephyr/drivers/i2c.h> | ||
#include <zephyr/logging/log.h> | ||
|
||
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; | ||
(void)channel_cfg; | ||
return 0; /* there is only 1 channel which requires no real setup */ | ||
} | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters