1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef ZEPHYR_INCLUDE_DRIVERS_MFD_NPM1300_H_
7 #define ZEPHYR_INCLUDE_DRIVERS_MFD_NPM1300_H_
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /**
14  * @defgroup mdf_interface_npm1300 MFD NPM1300 Interface
15  * @ingroup mfd_interfaces
16  * @{
17  */
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include <zephyr/device.h>
23 #include <zephyr/drivers/gpio.h>
24 
25 enum mfd_npm1300_event_t {
26 	NPM1300_EVENT_CHG_COMPLETED,
27 	NPM1300_EVENT_CHG_ERROR,
28 	NPM1300_EVENT_BATTERY_DETECTED,
29 	NPM1300_EVENT_BATTERY_REMOVED,
30 	NPM1300_EVENT_SHIPHOLD_PRESS,
31 	NPM1300_EVENT_SHIPHOLD_RELEASE,
32 	NPM1300_EVENT_WATCHDOG_WARN,
33 	NPM1300_EVENT_VBUS_DETECTED,
34 	NPM1300_EVENT_VBUS_REMOVED,
35 	NPM1300_EVENT_GPIO0_EDGE,
36 	NPM1300_EVENT_GPIO1_EDGE,
37 	NPM1300_EVENT_GPIO2_EDGE,
38 	NPM1300_EVENT_GPIO3_EDGE,
39 	NPM1300_EVENT_GPIO4_EDGE,
40 	NPM1300_EVENT_MAX
41 };
42 
43 /**
44  * @brief Read multiple registers from npm1300
45  *
46  * @param dev npm1300 mfd device
47  * @param base Register base address (bits 15..8 of 16-bit address)
48  * @param offset Register offset address (bits 7..0 of 16-bit address)
49  * @param data Pointer to buffer for received data
50  * @param len Number of bytes to read
51  * @retval 0 If successful
52  * @retval -errno In case of any bus error (see i2c_write_read_dt())
53  */
54 int mfd_npm1300_reg_read_burst(const struct device *dev, uint8_t base, uint8_t offset, void *data,
55 			       size_t len);
56 
57 /**
58  * @brief Read single register from npm1300
59  *
60  * @param dev npm1300 mfd device
61  * @param base Register base address (bits 15..8 of 16-bit address)
62  * @param offset Register offset address (bits 7..0 of 16-bit address)
63  * @param data Pointer to buffer for received data
64  * @retval 0 If successful
65  * @retval -errno In case of any bus error (see i2c_write_read_dt())
66  */
67 int mfd_npm1300_reg_read(const struct device *dev, uint8_t base, uint8_t offset, uint8_t *data);
68 
69 /**
70  * @brief Write single register to npm1300
71  *
72  * @param dev npm1300 mfd device
73  * @param base Register base address (bits 15..8 of 16-bit address)
74  * @param offset Register offset address (bits 7..0 of 16-bit address)
75  * @param data data to write
76  * @retval 0 If successful
77  * @retval -errno In case of any bus error (see i2c_write_dt())
78  */
79 int mfd_npm1300_reg_write(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data);
80 
81 /**
82  * @brief Write two registers to npm1300
83  *
84  * @param dev npm1300 mfd device
85  * @param base Register base address (bits 15..8 of 16-bit address)
86  * @param offset Register offset address (bits 7..0 of 16-bit address)
87  * @param data1 first byte of data to write
88  * @param data2 second byte of data to write
89  * @retval 0 If successful
90  * @retval -errno In case of any bus error (see i2c_write_dt())
91  */
92 int mfd_npm1300_reg_write2(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data1,
93 			   uint8_t data2);
94 
95 /**
96  * @brief Update selected bits in npm1300 register
97  *
98  * @param dev npm1300 mfd device
99  * @param base Register base address (bits 15..8 of 16-bit address)
100  * @param offset Register offset address (bits 7..0 of 16-bit address)
101  * @param data data to write
102  * @param mask mask of bits to be modified
103  * @retval 0 If successful
104  * @retval -errno In case of any bus error (see i2c_write_read_dt(), i2c_write_dt())
105  */
106 int mfd_npm1300_reg_update(const struct device *dev, uint8_t base, uint8_t offset, uint8_t data,
107 			   uint8_t mask);
108 
109 /**
110  * @brief Write npm1300 timer register
111  *
112  * @param dev npm1300 mfd device
113  * @param time_ms timer value in ms
114  * @retval 0 If successful
115  * @retval -EINVAL if time value is too large
116  * @retval -errno In case of any bus error (see i2c_write_dt())
117  */
118 int mfd_npm1300_set_timer(const struct device *dev, uint32_t time_ms);
119 
120 /**
121  * @brief npm1300 full power reset
122  *
123  * @param dev npm1300 mfd device
124  * @retval 0 If successful
125  * @retval -errno In case of any bus error (see i2c_write_dt())
126  */
127 int mfd_npm1300_reset(const struct device *dev);
128 
129 /**
130  * @brief npm1300 hibernate
131  *
132  * Enters low power state, and wakes after specified time
133  *
134  * @param dev npm1300 mfd device
135  * @param time_ms timer value in ms
136  * @retval 0 If successful
137  * @retval -EINVAL if time value is too large
138  * @retval -errno In case of any bus error (see i2c_write_dt())
139  */
140 int mfd_npm1300_hibernate(const struct device *dev, uint32_t time_ms);
141 
142 /**
143  * @brief Add npm1300 event callback
144  *
145  * @param dev npm1300 mfd device
146  * @param callback callback
147  * @return 0 on success, -errno on failure
148  */
149 int mfd_npm1300_add_callback(const struct device *dev, struct gpio_callback *callback);
150 
151 /**
152  * @brief Remove npm1300 event callback
153  *
154  * @param dev npm1300 mfd device
155  * @param callback callback
156  * @return 0 on success, -errno on failure
157  */
158 int mfd_npm1300_remove_callback(const struct device *dev, struct gpio_callback *callback);
159 
160 /** @} */
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif /* ZEPHYR_INCLUDE_DRIVERS_MFD_NPM1300_H_ */
167