1414#include <zephyr/kernel.h>
1515
1616struct composite_config {
17- const struct device * battery_voltage ;
18- const struct device * battery_current ;
17+ const struct device * source_primary ;
18+ const struct device * source_secondary ;
1919 int32_t ocv_lookup_table [BATTERY_OCV_TABLE_LEN ];
2020 uint32_t charge_capacity_microamp_hours ;
2121 enum battery_chemistry chemistry ;
@@ -40,6 +40,19 @@ static int composite_fetch(const struct device *dev)
4040 return pm_device_runtime_put (dev );
4141}
4242
43+ static int composite_channel_get (const struct device * dev , enum sensor_channel chan ,
44+ struct sensor_value * val )
45+ {
46+ const struct composite_config * config = dev -> config ;
47+ int rc ;
48+
49+ rc = sensor_channel_get (config -> source_primary , chan , val );
50+ if ((rc == - ENOTSUP ) && config -> source_secondary ) {
51+ rc = sensor_channel_get (config -> source_secondary , chan , val );
52+ }
53+ return rc ;
54+ }
55+
4356static int composite_get_prop (const struct device * dev , fuel_gauge_prop_t prop ,
4457 union fuel_gauge_prop_val * val )
4558{
@@ -57,13 +70,13 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
5770 offsetof(union fuel_gauge_prop_val , relative_state_of_charge ));
5871 BUILD_ASSERT (sizeof (val -> current ) == sizeof (val -> avg_current ));
5972 BUILD_ASSERT (offsetof(union fuel_gauge_prop_val , current ) ==
60- offsetof(union fuel_gauge_prop_val , avg_current ));
73+ offsetof(union fuel_gauge_prop_val , avg_current ));
6174
6275 if (now >= data -> next_reading ) {
6376 /* Trigger a sample on the input devices */
64- rc = composite_fetch (config -> battery_voltage );
65- if ((rc == 0 ) && config -> battery_current ) {
66- rc = composite_fetch (config -> battery_current );
77+ rc = composite_fetch (config -> source_primary );
78+ if ((rc == 0 ) && config -> source_secondary ) {
79+ rc = composite_fetch (config -> source_secondary );
6780 }
6881 if (rc != 0 ) {
6982 return rc ;
@@ -87,7 +100,7 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
87100 val -> full_charge_capacity = config -> charge_capacity_microamp_hours / 1000 ;
88101 break ;
89102 case FUEL_GAUGE_VOLTAGE :
90- rc = sensor_channel_get ( config -> battery_voltage , SENSOR_CHAN_VOLTAGE , & sensor_val );
103+ rc = composite_channel_get ( dev , SENSOR_CHAN_VOLTAGE , & sensor_val );
91104 val -> voltage = sensor_value_to_micro (& sensor_val );
92105 break ;
93106 case FUEL_GAUGE_ABSOLUTE_STATE_OF_CHARGE :
@@ -96,7 +109,7 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
96109 return - ENOTSUP ;
97110 }
98111 /* Fetch the voltage from the sensor */
99- rc = sensor_channel_get ( config -> battery_voltage , SENSOR_CHAN_VOLTAGE , & sensor_val );
112+ rc = composite_channel_get ( dev , SENSOR_CHAN_VOLTAGE , & sensor_val );
100113 voltage = sensor_value_to_micro (& sensor_val );
101114 if (rc == 0 ) {
102115 /* Convert voltage to state of charge */
@@ -106,10 +119,7 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
106119 break ;
107120 case FUEL_GAUGE_CURRENT :
108121 case FUEL_GAUGE_AVG_CURRENT :
109- if (config -> battery_current == NULL ) {
110- return - ENOTSUP ;
111- }
112- rc = sensor_channel_get (config -> battery_current , SENSOR_CHAN_CURRENT , & sensor_val );
122+ rc = composite_channel_get (dev , SENSOR_CHAN_CURRENT , & sensor_val );
113123 val -> current = sensor_value_to_micro (& sensor_val );
114124 break ;
115125 default :
@@ -119,22 +129,36 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
119129 return rc ;
120130}
121131
132+ static int fuel_gauge_composite_init (const struct device * dev )
133+ {
134+ const struct composite_config * config = dev -> config ;
135+
136+ /* Validate sources are ready */
137+ if (!device_is_ready (config -> source_primary )) {
138+ return - ENODEV ;
139+ }
140+ if (config -> source_secondary && !device_is_ready (config -> source_secondary )) {
141+ return - ENODEV ;
142+ }
143+ return 0 ;
144+ }
145+
122146static DEVICE_API (fuel_gauge , composite_api ) = {
123147 .get_property = composite_get_prop ,
124148};
125149
126150#define COMPOSITE_INIT (inst ) \
127151 static const struct composite_config composite_##inst##_config = { \
128- .battery_voltage = DEVICE_DT_GET(DT_INST_PROP(inst, battery_voltage )), \
129- .battery_current = DEVICE_DT_GET_OR_NULL(DT_INST_PROP(inst, battery_current )), \
152+ .source_primary = DEVICE_DT_GET(DT_INST_PROP(inst, source_primary )), \
153+ .source_secondary = DEVICE_DT_GET_OR_NULL(DT_INST_PROP(inst, source_secondary )), \
130154 .ocv_lookup_table = \
131155 BATTERY_OCV_TABLE_DT_GET(DT_DRV_INST(inst), ocv_capacity_table_0), \
132156 .charge_capacity_microamp_hours = \
133157 DT_INST_PROP_OR(inst, charge_full_design_microamp_hours, 0), \
134158 .chemistry = BATTERY_CHEMISTRY_DT_GET(inst), \
135159 }; \
136160 static struct composite_data composite_##inst##_data; \
137- DEVICE_DT_INST_DEFINE(inst, NULL , NULL, &composite_##inst##_data, \
161+ DEVICE_DT_INST_DEFINE(inst, fuel_gauge_composite_init , NULL, &composite_##inst##_data, \
138162 &composite_##inst##_config, POST_KERNEL, \
139163 CONFIG_SENSOR_INIT_PRIORITY, &composite_api);
140164
0 commit comments