1 /*
2 * Copyright (c) 2024 SILA Embedded Solutions GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT rohm_bd8lb600fs_diagnostics
8
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/drivers/mfd/bd8lb600fs.h>
11 #include <zephyr/drivers/sensor/bd8lb600fs.h>
12 #include <zephyr/logging/log.h>
13
14 #include "bd8lb600fs_diagnostics.h"
15
16 LOG_MODULE_REGISTER(BD8LB600FS_DIAGNOSTICS, CONFIG_SENSOR_LOG_LEVEL);
17
bd8lb600fs_diagnostics_sample_fetch(const struct device * dev,enum sensor_channel chan)18 static int bd8lb600fs_diagnostics_sample_fetch(const struct device *dev, enum sensor_channel chan)
19 {
20 const struct bd8lb600fs_diagnostics_config *config = dev->config;
21 struct bd8lb600fs_diagnostics_data *data = dev->data;
22
23 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
24 return mfd_bd8lb600fs_get_output_diagnostics(config->parent_dev, &data->old,
25 &data->ocp_or_tsd);
26 }
27
bd8lb600fs_diagnostics_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)28 static int bd8lb600fs_diagnostics_channel_get(const struct device *dev, enum sensor_channel chan,
29 struct sensor_value *val)
30 {
31 struct bd8lb600fs_diagnostics_data *data = dev->data;
32
33 switch (chan) {
34 case SENSOR_CHAN_BD8LB600FS_OPEN_LOAD:
35 val->val1 = data->old;
36 val->val2 = 0;
37 return 0;
38 case SENSOR_CHAN_BD8LB600FS_OVER_CURRENT_OR_THERMAL_SHUTDOWN:
39 val->val1 = data->ocp_or_tsd;
40 val->val2 = 0;
41 return 0;
42 default:
43 LOG_ERR("%s: requesting unsupported channel %i", dev->name, chan);
44 return -ENOTSUP;
45 }
46 }
47
48 static DEVICE_API(sensor, bd8lb600fs_diagnostics_driver_api) = {
49 .sample_fetch = bd8lb600fs_diagnostics_sample_fetch,
50 .channel_get = bd8lb600fs_diagnostics_channel_get,
51 };
52
bd8lb600fs_diagnostics_init(const struct device * dev)53 static int bd8lb600fs_diagnostics_init(const struct device *dev)
54 {
55 const struct bd8lb600fs_diagnostics_config *config = dev->config;
56
57 if (!device_is_ready(config->parent_dev)) {
58 LOG_ERR("%s: parent device is not ready", dev->name);
59 return -ENODEV;
60 }
61
62 return 0;
63 }
64
65 #define BD8LB600FS_DIAGNOSTICS_DEFINE(inst) \
66 static struct bd8lb600fs_diagnostics_data bd8lb600fs_diagnostics_data_##inst; \
67 \
68 static const struct bd8lb600fs_diagnostics_config bd8lb600fs_diagnostics_config_##inst = { \
69 .parent_dev = DEVICE_DT_GET(DT_PARENT(DT_DRV_INST(inst))), \
70 }; \
71 \
72 SENSOR_DEVICE_DT_INST_DEFINE( \
73 inst, bd8lb600fs_diagnostics_init, NULL, &bd8lb600fs_diagnostics_data_##inst, \
74 &bd8lb600fs_diagnostics_config_##inst, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
75 &bd8lb600fs_diagnostics_driver_api);
76
77 DT_INST_FOREACH_STATUS_OKAY(BD8LB600FS_DIAGNOSTICS_DEFINE)
78