1 /* LLTC LTC2451 ADC
2  *
3  * Copyright (c) 2023 Brill Power Ltd.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/adc.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/sys/byteorder.h>
13 
14 LOG_MODULE_REGISTER(ltc2451, CONFIG_ADC_LOG_LEVEL);
15 
16 #define DT_DRV_COMPAT lltc_ltc2451
17 
18 struct ltc2451_config {
19 	struct i2c_dt_spec i2c;
20 	uint8_t conversion_speed;
21 };
22 
ltc2451_channel_setup(const struct device * dev,const struct adc_channel_cfg * channel_cfg)23 static int ltc2451_channel_setup(const struct device *dev,
24 				const struct adc_channel_cfg *channel_cfg)
25 {
26 	ARG_UNUSED(dev);
27 
28 	if (channel_cfg->channel_id != 0) {
29 		LOG_ERR("Invalid channel id '%d'", channel_cfg->channel_id);
30 		return -EINVAL;
31 	}
32 
33 	return 0;
34 }
35 
ltc2451_set_conversion_speed(const struct device * dev,uint8_t conversion_speed)36 static int ltc2451_set_conversion_speed(const struct device *dev, uint8_t conversion_speed)
37 {
38 	const struct ltc2451_config *config = dev->config;
39 	uint8_t wr_buf[1];
40 	int err;
41 
42 	if (conversion_speed == 60) {
43 		wr_buf[0] = 0;
44 	} else if (conversion_speed == 30) {
45 		wr_buf[0] = 1;
46 	} else {
47 		LOG_ERR("Invalid conversion speed selected");
48 		return -EINVAL;
49 	}
50 
51 	err = i2c_write_dt(&config->i2c, wr_buf, sizeof(wr_buf));
52 
53 	if (err != 0) {
54 		LOG_ERR("LTC write failed (err %d)", err);
55 	}
56 
57 	return err;
58 }
59 
ltc2451_read_latest_conversion(const struct device * dev,const struct adc_sequence * sequence)60 static int ltc2451_read_latest_conversion(const struct device *dev,
61 					  const struct adc_sequence *sequence)
62 {
63 	const struct ltc2451_config *config = dev->config;
64 	uint8_t rd_buf[2];
65 	uint16_t *value_buf;
66 	int err = i2c_read_dt(&config->i2c, rd_buf, sizeof(rd_buf));
67 
68 	if (err == 0) {
69 		value_buf = (uint16_t *)sequence->buffer;
70 		value_buf[0] = sys_get_be16(rd_buf);
71 	} else {
72 		LOG_ERR("LTC read failed (err %d)", err);
73 	}
74 
75 	return err;
76 }
77 
ltc2451_init(const struct device * dev)78 static int ltc2451_init(const struct device *dev)
79 {
80 	const struct ltc2451_config *config = dev->config;
81 
82 	if (!device_is_ready(config->i2c.bus)) {
83 		LOG_ERR("I2C device not ready");
84 		return -ENODEV;
85 	}
86 
87 	return ltc2451_set_conversion_speed(dev, config->conversion_speed);
88 }
89 
90 static const struct adc_driver_api ltc2451_api = {
91 	.channel_setup = ltc2451_channel_setup,
92 	.read = ltc2451_read_latest_conversion,
93 };
94 
95 #define LTC2451_DEFINE(index) \
96 	static const struct ltc2451_config ltc2451_cfg_##index = { \
97 		.i2c = I2C_DT_SPEC_INST_GET(index), \
98 		.conversion_speed = DT_INST_PROP(index, conversion_speed), \
99 	}; \
100  \
101 	DEVICE_DT_INST_DEFINE(index, &ltc2451_init, NULL, NULL, \
102 			      &ltc2451_cfg_##index, POST_KERNEL, CONFIG_ADC_INIT_PRIORITY, \
103 			      &ltc2451_api);
104 
105 DT_INST_FOREACH_STATUS_OKAY(LTC2451_DEFINE)
106