1 /*
2  * Copyright (c) 2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef NRFS_SWEXT_H
8 #define NRFS_SWEXT_H
9 
10 #include <internal/services/nrfs_swext.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /** @brief SWEXT control service response notification types. */
17 typedef enum __NRFS_PACKED {
18 	NRFS_SWEXT_EVT_ENABLED		= 0,	/** Output enabled. */
19 	NRFS_SWEXT_EVT_OVERCURRENT	= 1,	/** Overcurrent detected. SWEXT is powered down. */
20 	NRFS_SWEXT_EVT_REJECTED		= 2,	/** Request rejected. */
21 } nrfs_swext_evt_type_t;
22 
23 /** @brief SWEXT control service response data structure. */
24 typedef struct {
25 	nrfs_swext_evt_type_t type;
26 } nrfs_swext_evt_t;
27 
28 /** @brief SWEXT control service event handler type. */
29 typedef void (*nrfs_swext_evt_handler_t)(nrfs_swext_evt_t const *p_evt, void *context);
30 
31 /**
32  * @brief Function for initializing SWEXT (SWitch EXTernal) peripheral.
33  *
34  * @param[in] handler             Function called as a response to the request.
35  *
36  * @retval NRFS_SUCCESS           Service initialized successfully.
37  * @retval NRFS_ERR_INVALID_STATE Service was already initialized.
38  */
39 nrfs_err_t nrfs_swext_init(nrfs_swext_evt_handler_t handler);
40 
41 /**
42  * @brief Function for uninitializing SWEXT (SWitch EXTernal) peripheral.
43  *
44  * @warning Notifications from previous requests are dropped after service uninitialization.
45  */
46 void nrfs_swext_uninit(void);
47 
48 /**
49  * @brief Function for enabling SWEXT (SWitch EXTernal) peripheral.
50 
51  * @param[in] load_current Load current in raw format.
52  * @param[in] p_context Opaque user data that will be passed to registered callback.
53  *
54  * @retval NRFS_SUCCESS           Request sent successfully.
55  * @retval NRFS_ERR_INVALID_STATE Service is uninitialized.
56  * @retval NRFS_ERR_IPC           Backend returned error during request sending.
57  */
58 nrfs_err_t nrfs_swext_power_up(uint8_t load_current, void *p_context);
59 
60 /**
61  * @brief Function for disabling SWEXT (SWitch EXTernal) peripheral.
62 
63  * @param[in] pd_clamp Enable/disable power down clamp.
64  * @param[in] p_context Opaque user datathat will be passed to registered callback.
65  *
66  * @retval NRFS_SUCCESS           Request sent successfully.
67  * @retval NRFS_ERR_INVALID_STATE Service is uninitialized.
68  * @retval NRFS_ERR_IPC           Backend returned error during request sending.
69  */
70 nrfs_err_t nrfs_swext_power_down(swext_pd_clamp_t pd_clamp, void *p_context);
71 
72 /**
73  * @brief Function for converting a load current value in uA to raw data.
74  *
75  * The returned value is represented in 500uA steps rounded up.
76  *
77  * @param[in] load_current Estimated load current attached to SWEXT peripheral.
78  *
79  * @retval Load current in raw format.
80  */
81 
nrfs_swext_load_current_to_raw(uint16_t load_current)82 static inline uint8_t nrfs_swext_load_current_to_raw(uint16_t load_current)
83 {
84 	return (load_current + 499) / 500;
85 }
86 
87 #ifdef __cplusplus
88 }
89 #endif
90 
91 #endif /* NRFS_SWEXT_H */
92