Skip to content

Commit 3602342

Browse files
jurenatnashif
authored andcommitted
drivers: sensor: ti: ina230: Add support for INA236
This commit adds support for INA236 into the existing INA230 driver. These two chips are similar enough to share most of the code. The device can be defined the same way as INA230 and we only have the extra option to select the high-precision mode. ``` ina236: ina236@40 { status = "okay"; compatible = "ti,ina236", "ti,ina230"; reg = <0x40>; adc-mode = "Bus and shunt voltage continuous"; vbus-conversion-time-us = <1100>; vshunt-conversion-time-us = <1100>; avg-count = <1>; current-lsb-microamps = <1000>; rshunt-micro-ohms = <15000>; alert-gpios = <&gpiod 0 GPIO_ACTIVE_LOW>; high-precision; }; ``` Signed-off-by: Tomáš Juřena <[email protected]>
1 parent 8f4cf7f commit 3602342

File tree

5 files changed

+67
-34
lines changed

5 files changed

+67
-34
lines changed

drivers/sensor/ti/ina23x/Kconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
config INA23X
66
bool "INA23X Current and Power Monitor"
77
default y
8-
depends on DT_HAS_TI_INA230_ENABLED || DT_HAS_TI_INA237_ENABLED
8+
depends on DT_HAS_TI_INA230_ENABLED || DT_HAS_TI_INA236_ENABLED || DT_HAS_TI_INA237_ENABLED
99
select I2C
1010
help
1111
Enable driver for INA23X Current and Power Monitor.
@@ -15,9 +15,9 @@ if INA23X
1515
config INA230
1616
bool "INA230"
1717
default y
18-
depends on DT_HAS_TI_INA230_ENABLED
18+
depends on DT_HAS_TI_INA230_ENABLED || DT_HAS_TI_INA236_ENABLED
1919
help
20-
Enable driver for INA230/INA231.
20+
Enable driver for INA230/INA231/INA236.
2121

2222
config INA237
2323
bool "INA237"

drivers/sensor/ti/ina23x/ina230.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8-
#define DT_DRV_COMPAT ti_ina230
9-
108
#include "ina230.h"
119
#include "ina23x_common.h"
1210

@@ -20,9 +18,11 @@ LOG_MODULE_REGISTER(INA230, CONFIG_SENSOR_LOG_LEVEL);
2018

2119
/** @brief The LSB value for the bus voltage register, in microvolts/LSB. */
2220
#define INA230_BUS_VOLTAGE_UV_LSB 1250U
21+
#define INA236_BUS_VOLTAGE_UV_LSB 1600U
2322

2423
/** @brief The scaling for the power register. */
2524
#define INA230_POWER_SCALING 25
25+
#define INA236_POWER_SCALING 32
2626

2727
static int ina230_channel_get(const struct device *dev, enum sensor_channel chan,
2828
struct sensor_value *val)
@@ -34,7 +34,7 @@ static int ina230_channel_get(const struct device *dev, enum sensor_channel chan
3434

3535
switch (chan) {
3636
case SENSOR_CHAN_VOLTAGE:
37-
bus_uv = data->bus_voltage * INA230_BUS_VOLTAGE_UV_LSB;
37+
bus_uv = data->bus_voltage * config->uv_lsb;
3838

3939
/* convert to fractional volts (units for voltage channel) */
4040
val->val1 = bus_uv / 1000000U;
@@ -51,7 +51,7 @@ static int ina230_channel_get(const struct device *dev, enum sensor_channel chan
5151
break;
5252

5353
case SENSOR_CHAN_POWER:
54-
power_uw = data->power * INA230_POWER_SCALING * config->current_lsb;
54+
power_uw = data->power * config->power_scale * config->current_lsb;
5555

5656
/* convert to fractional watts */
5757
val->val1 = (int32_t)(power_uw / 1000000U);
@@ -248,23 +248,32 @@ static const struct sensor_driver_api ina230_driver_api = {
248248
#define INA230_CFG_IRQ(inst)
249249
#endif /* CONFIG_INA230_TRIGGER */
250250

251-
#define INA230_DRIVER_INIT(inst) \
251+
#define INA230_DRIVER_INIT(inst, type) \
252252
static struct ina230_data drv_data_##inst; \
253-
static const struct ina230_config drv_config_##inst = { \
253+
static const struct ina230_config drv_config_##type##inst = { \
254254
.bus = I2C_DT_SPEC_INST_GET(inst), \
255-
.config = DT_INST_PROP(inst, config) | \
256-
(DT_INST_ENUM_IDX(inst, avg_count) << 9) | \
257-
(DT_INST_ENUM_IDX(inst, vbus_conversion_time_us) << 6) | \
258-
(DT_INST_ENUM_IDX(inst, vshunt_conversion_time_us) << 3) | \
259-
DT_INST_ENUM_IDX(inst, adc_mode), \
255+
.config = (DT_INST_PROP_OR(inst, high_precision, 0) << 12) | \
256+
DT_INST_PROP(inst, config) | (DT_INST_ENUM_IDX(inst, avg_count) << 9) | \
257+
(DT_INST_ENUM_IDX(inst, vbus_conversion_time_us) << 6) | \
258+
(DT_INST_ENUM_IDX(inst, vshunt_conversion_time_us) << 3) | \
259+
DT_INST_ENUM_IDX(inst, adc_mode), \
260260
.current_lsb = DT_INST_PROP(inst, current_lsb_microamps), \
261-
.cal = (uint16_t)((INA230_CAL_SCALING * 10000000ULL) / \
262-
((uint64_t)DT_INST_PROP(inst, current_lsb_microamps) * \
263-
DT_INST_PROP(inst, rshunt_micro_ohms))), \
261+
.uv_lsb = type##_BUS_VOLTAGE_UV_LSB, \
262+
.power_scale = type##_POWER_SCALING, \
263+
.cal = (uint16_t)(((INA230_CAL_SCALING * 10000000ULL) / \
264+
((uint64_t)DT_INST_PROP(inst, current_lsb_microamps) * \
265+
DT_INST_PROP(inst, rshunt_micro_ohms))) >> \
266+
(DT_INST_PROP_OR(inst, high_precision, 0) << 1)), \
264267
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, alert_gpios), (INA230_CFG_IRQ(inst)), \
265-
())}; \
268+
())}; \
266269
SENSOR_DEVICE_DT_INST_DEFINE(inst, &ina230_init, NULL, &drv_data_##inst, \
267-
&drv_config_##inst, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
268-
&ina230_driver_api);
270+
&drv_config_##type##inst, POST_KERNEL, \
271+
CONFIG_SENSOR_INIT_PRIORITY, &ina230_driver_api);
272+
273+
#undef DT_DRV_COMPAT
274+
#define DT_DRV_COMPAT ti_ina230
275+
DT_INST_FOREACH_STATUS_OKAY_VARGS(INA230_DRIVER_INIT, INA230)
269276

270-
DT_INST_FOREACH_STATUS_OKAY(INA230_DRIVER_INIT)
277+
#undef DT_DRV_COMPAT
278+
#define DT_DRV_COMPAT ti_ina236
279+
DT_INST_FOREACH_STATUS_OKAY_VARGS(INA230_DRIVER_INIT, INA236)

drivers/sensor/ti/ina23x/ina230.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
#include <zephyr/kernel.h>
2424
#endif
2525

26-
#define INA230_REG_CONFIG 0x00
27-
#define INA230_REG_SHUNT_VOLT 0x01
28-
#define INA230_REG_BUS_VOLT 0x02
29-
#define INA230_REG_POWER 0x03
30-
#define INA230_REG_CURRENT 0x04
31-
#define INA230_REG_CALIB 0x05
32-
#define INA230_REG_MASK 0x06
33-
#define INA230_REG_ALERT 0x07
26+
#define INA230_REG_CONFIG 0x00
27+
#define INA230_REG_SHUNT_VOLT 0x01
28+
#define INA230_REG_BUS_VOLT 0x02
29+
#define INA230_REG_POWER 0x03
30+
#define INA230_REG_CURRENT 0x04
31+
#define INA230_REG_CALIB 0x05
32+
#define INA230_REG_MASK 0x06
33+
#define INA230_REG_ALERT 0x07
34+
#define INA236_REG_MANUFACTURER_ID 0x3E
35+
#define INA236_REG_DEVICE_ID 0x3F
3436

3537
struct ina230_data {
3638
const struct device *dev;
@@ -43,25 +45,26 @@ struct ina230_data {
4345
struct k_work work;
4446
sensor_trigger_handler_t handler_alert;
4547
const struct sensor_trigger *trig_alert;
46-
#endif /* CONFIG_INA230_TRIGGER */
48+
#endif /* CONFIG_INA230_TRIGGER */
4749
};
4850

4951
struct ina230_config {
5052
struct i2c_dt_spec bus;
5153
uint16_t config;
5254
uint32_t current_lsb;
5355
uint16_t cal;
56+
uint8_t power_scale;
57+
uint32_t uv_lsb;
5458
#ifdef CONFIG_INA230_TRIGGER
5559
bool trig_enabled;
5660
uint16_t mask;
5761
const struct gpio_dt_spec alert_gpio;
5862
uint16_t alert_limit;
59-
#endif /* CONFIG_INA230_TRIGGER */
63+
#endif /* CONFIG_INA230_TRIGGER */
6064
};
6165

6266
int ina230_trigger_mode_init(const struct device *dev);
63-
int ina230_trigger_set(const struct device *dev,
64-
const struct sensor_trigger *trig,
67+
int ina230_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
6568
sensor_trigger_handler_t handler);
6669

6770
#endif /* ZEPHYR_DRIVERS_SENSOR_INA23X_INA230_H_ */

dts/bindings/sensor/ti,ina230.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77

88
description: |
9-
TI INA230 and INA231 Bidirectional Current and Power Monitor.
9+
TI INA230, INA231 and INA236 Bidirectional Current and Power Monitor.
1010
The <zephyr/dt-bindings/sensor/ina230.h> file should be included in the
1111
DeviceTree and it provides macro that can be used for initializing the
1212
configuration register.

dts/bindings/sensor/ti,ina236.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright 2024 Tomas Jurena
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
description: |
8+
TI INA236 Bidirectional Current and Power Monitor.
9+
The <zephyr/dt-bindings/sensor/ina230.h> file should be included in the
10+
DeviceTree and it provides macro that can be used for initializing the
11+
configuration register.
12+
13+
compatible: "ti,ina236"
14+
15+
include: ti,ina230.yaml
16+
17+
properties:
18+
high-precision:
19+
type: boolean
20+
description: |
21+
Enable high precision mode (4x the resolution).

0 commit comments

Comments
 (0)