14
14
#include <zephyr/kernel.h>
15
15
16
16
struct 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 ;
19
19
int32_t ocv_lookup_table [BATTERY_OCV_TABLE_LEN ];
20
20
uint32_t charge_capacity_microamp_hours ;
21
21
enum battery_chemistry chemistry ;
@@ -40,6 +40,19 @@ static int composite_fetch(const struct device *dev)
40
40
return pm_device_runtime_put (dev );
41
41
}
42
42
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
+
43
56
static int composite_get_prop (const struct device * dev , fuel_gauge_prop_t prop ,
44
57
union fuel_gauge_prop_val * val )
45
58
{
@@ -61,9 +74,9 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
61
74
62
75
if (now >= data -> next_reading ) {
63
76
/* 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 );
67
80
}
68
81
if (rc != 0 ) {
69
82
return rc ;
@@ -87,7 +100,7 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
87
100
val -> full_charge_capacity = config -> charge_capacity_microamp_hours / 1000 ;
88
101
break ;
89
102
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 );
91
104
val -> voltage = sensor_value_to_micro (& sensor_val );
92
105
break ;
93
106
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,
96
109
return - ENOTSUP ;
97
110
}
98
111
/* 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 );
100
113
voltage = sensor_value_to_micro (& sensor_val );
101
114
if (rc == 0 ) {
102
115
/* Convert voltage to state of charge */
@@ -106,10 +119,7 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
106
119
break ;
107
120
case FUEL_GAUGE_CURRENT :
108
121
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 );
113
123
val -> current = sensor_value_to_micro (& sensor_val );
114
124
break ;
115
125
default :
@@ -119,22 +129,36 @@ static int composite_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
119
129
return rc ;
120
130
}
121
131
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
+
122
146
static DEVICE_API (fuel_gauge , composite_api ) = {
123
147
.get_property = composite_get_prop ,
124
148
};
125
149
126
150
#define COMPOSITE_INIT (inst ) \
127
151
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 )), \
130
154
.ocv_lookup_table = \
131
155
BATTERY_OCV_TABLE_DT_GET(DT_DRV_INST(inst), ocv_capacity_table_0), \
132
156
.charge_capacity_microamp_hours = \
133
157
DT_INST_PROP_OR(inst, charge_full_design_microamp_hours, 0), \
134
158
.chemistry = BATTERY_CHEMISTRY_DT_GET(inst), \
135
159
}; \
136
160
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, \
138
162
&composite_##inst##_config, POST_KERNEL, \
139
163
CONFIG_SENSOR_INIT_PRIORITY, &composite_api);
140
164
0 commit comments