1 /*
2  * Copyright 2021 The Chromium OS Authors
3  * Copyright (c) 2021 Grinn
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_DRIVERS_SENSOR_INA23X_INA230_H_
9 #define ZEPHYR_DRIVERS_SENSOR_INA23X_INA230_H_
10 
11 #ifdef CONFIG_INA230_TRIGGER
12 #include <stdbool.h>
13 #endif
14 #include <stdint.h>
15 
16 #include <zephyr/device.h>
17 #ifdef CONFIG_INA230_TRIGGER
18 #include <zephyr/drivers/gpio.h>
19 #endif
20 #include <zephyr/drivers/i2c.h>
21 #include <zephyr/drivers/sensor.h>
22 #ifdef CONFIG_INA230_TRIGGER
23 #include <zephyr/kernel.h>
24 #endif
25 
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 
35 struct ina230_data {
36 	const struct device *dev;
37 	int16_t current;
38 	uint16_t bus_voltage;
39 	uint16_t power;
40 #ifdef CONFIG_INA230_TRIGGER
41 	const struct device *gpio;
42 	struct gpio_callback gpio_cb;
43 	struct k_work work;
44 	sensor_trigger_handler_t handler_alert;
45 	const struct sensor_trigger *trig_alert;
46 #endif  /* CONFIG_INA230_TRIGGER */
47 };
48 
49 struct ina230_config {
50 	struct i2c_dt_spec bus;
51 	uint16_t config;
52 	uint32_t current_lsb;
53 	uint16_t cal;
54 #ifdef CONFIG_INA230_TRIGGER
55 	bool trig_enabled;
56 	uint16_t mask;
57 	const struct gpio_dt_spec alert_gpio;
58 	uint16_t alert_limit;
59 #endif  /* CONFIG_INA230_TRIGGER */
60 };
61 
62 int ina230_trigger_mode_init(const struct device *dev);
63 int ina230_trigger_set(const struct device *dev,
64 		       const struct sensor_trigger *trig,
65 		       sensor_trigger_handler_t handler);
66 
67 #endif /* ZEPHYR_DRIVERS_SENSOR_INA23X_INA230_H_ */
68