1 /*
2  * Copyright (c) 2020 Nuvoton Technology Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _NUVOTON_NPCX_SOC_MIWU_H_
8 #define _NUVOTON_NPCX_SOC_MIWU_H_
9 
10 #include <stdint.h>
11 
12 #include <zephyr/device.h>
13 #include <zephyr/drivers/gpio.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 enum miwu_table {
20 	NPCX_MIWU_TABLE_0,
21 	NPCX_MIWU_TABLE_1,
22 	NPCX_MIWU_TABLE_2,
23 	NPCX_MIWU_TABLE_COUNT
24 };
25 
26 enum miwu_group {
27 	NPCX_MIWU_GROUP_1,
28 	NPCX_MIWU_GROUP_2,
29 	NPCX_MIWU_GROUP_3,
30 	NPCX_MIWU_GROUP_4,
31 	NPCX_MIWU_GROUP_5,
32 	NPCX_MIWU_GROUP_6,
33 	NPCX_MIWU_GROUP_7,
34 	NPCX_MIWU_GROUP_8,
35 	NPCX_MIWU_GROUP_COUNT
36 };
37 
38 #define NPCX_MIWU_TABLE_NONE NPCX_MIWU_TABLE_COUNT
39 
40 /* Interrupt modes supported by npcx miwu modules */
41 enum miwu_int_mode {
42 	NPCX_MIWU_MODE_LEVEL,
43 	NPCX_MIWU_MODE_EDGE,
44 };
45 
46 /* Interrupt trigger modes supported by npcx miwu modules */
47 enum miwu_int_trig {
48 	NPCX_MIWU_TRIG_LOW,  /** Edge failing or active low detection */
49 	NPCX_MIWU_TRIG_HIGH, /** Edge rising or active high detection */
50 	NPCX_MIWU_TRIG_BOTH, /** Both edge rising and failing detection */
51 };
52 
53 /* NPCX miwu driver callback type */
54 enum {
55 	NPCX_MIWU_CALLBACK_GPIO,
56 	NPCX_MIWU_CALLBACK_DEV,
57 };
58 
59 /**
60  * @brief NPCX wake-up input source structure
61  *
62  * Used to indicate a Wake-Up Input source (WUI) belongs to which group and bit
63  * of Multi-Input Wake-Up Unit (MIWU) modules.
64  */
65 struct npcx_wui {
66 	uint8_t table:2; /** A source belongs to which MIWU table. */
67 	uint8_t group:3; /** A source belongs to which group of MIWU table. */
68 	uint8_t bit:3; /** A source belongs to which bit of MIWU group. */
69 };
70 
71 /**
72  * Define npcx miwu driver callback handler signature for wake-up input source
73  * of generic hardware. Its parameters contain the device issued interrupt
74  * and corresponding WUI source.
75  */
76 typedef void (*miwu_dev_callback_handler_t)(const struct device *source,
77 							struct npcx_wui *wui);
78 
79 /**
80  * @brief MIWU/GPIO information structure
81  *
82  * It contains both GPIO and MIWU information which is stored in unused field
83  * of struct gpio_port_pins_t since a interested mask of pins is only 8 bits.
84  * Beware the size of such structure must equal struct gpio_port_pins_t.
85  */
86 struct miwu_io_params {
87 	uint8_t pin_mask; /** A mask of pins the callback is interested in. */
88 	uint8_t gpio_port; /** GPIO device index */
89 	uint8_t cb_type; /** Callback type */
90 	struct npcx_wui wui; /** Wake-up input source of GPIO */
91 };
92 
93 /**
94  * @brief MIWU/generic device information structure
95  *
96  * It contains the information used for MIWU generic device event. Please notice
97  * the offset of cb_type must be the same as cb_type in struct miwu_io_params.
98  */
99 struct miwu_dev_params {
100 	uint8_t reserve1;
101 	uint8_t reserve2;
102 	uint8_t cb_type; /** Callback type */
103 	struct npcx_wui wui; /** Device instance register callback function */
104 	const struct device *source; /** Wake-up input source */
105 };
106 
107 /**
108  * @brief MIWU callback structure for a gpio or device input
109  *
110  * Used to register a generic gpio/device callback in the driver instance
111  * callback list. Beware such structure should not be allocated on stack.
112  *
113  * Note: To help setting it, see npcx_miwu_init_dev_callback() and
114  *       npcx_miwu_manage_callback() below
115  */
116 struct miwu_callback {
117 	/** Node of single-linked list */
118 	sys_snode_t node;
119 	union {
120 		struct {
121 			/** Callback function being called when GPIO event occurred */
122 			gpio_callback_handler_t handler;
123 			struct miwu_io_params params;
124 		} io_cb;
125 
126 		struct {
127 			/** Callback function being called when device event occurred */
128 			miwu_dev_callback_handler_t handler;
129 			struct miwu_dev_params params;
130 		} dev_cb;
131 	};
132 };
133 
134 /**
135  * @brief Enable interrupt of the wake-up input source
136  *
137  * @param A pointer on wake-up input source
138  */
139 void npcx_miwu_irq_enable(const struct npcx_wui *wui);
140 
141 /**
142  * @brief Disable interrupt of the wake-up input source
143  *
144  * @param wui A pointer on wake-up input source
145  */
146 void npcx_miwu_irq_disable(const struct npcx_wui *wui);
147 
148 /**
149  * @brief Connect io to the wake-up input source
150  *
151  * @param wui A pointer on wake-up input source
152  */
153 void npcx_miwu_io_enable(const struct npcx_wui *wui);
154 
155 /**
156  * @brief Disconnect io to the wake-up input source
157  *
158  * @param wui A pointer on wake-up input source
159  */
160 void npcx_miwu_io_disable(const struct npcx_wui *wui);
161 
162 /**
163  * @brief Get interrupt state of the wake-up input source
164  *
165  * @param wui A pointer on wake-up input source
166  *
167  * @retval 0 if interrupt is disabled, otherwise interrupt is enabled
168  */
169 bool npcx_miwu_irq_get_state(const struct npcx_wui *wui);
170 
171 /**
172  * @brief Get & clear interrupt pending bit of the wake-up input source
173  *
174  * @param wui A pointer on wake-up input source
175  *
176  * @retval 1 if interrupt is pending
177  */
178 bool npcx_miwu_irq_get_and_clear_pending(const struct npcx_wui *wui);
179 
180 /**
181  * @brief Configure interrupt type of the wake-up input source
182  *
183  * @param wui Pointer to wake-up input source for configuring
184  * @param mode Interrupt mode supported by NPCX MIWU
185  * @param trig Interrupt trigger mode supported by NPCX MIWU
186  *
187  * @retval 0 If successful
188  * @retval -EINVAL Invalid parameters
189  */
190 int npcx_miwu_interrupt_configure(const struct npcx_wui *wui,
191 		enum miwu_int_mode mode, enum miwu_int_trig trig);
192 
193 /**
194  * @brief Function to initialize a struct miwu_callback with gpio properly
195  *
196  * @param callback Pointer to io callback structure for initialization
197  * @param io_wui Pointer to wake-up input IO source
198  * @param port GPIO port issued a callback function
199  */
200 void npcx_miwu_init_gpio_callback(struct miwu_callback *callback,
201 				const struct npcx_wui *io_wui, int port);
202 
203 /**
204  * @brief Function to initialize a struct miwu_callback with device properly
205  *
206  * @param callback Pointer to device callback structure for initialization
207  * @param dev_wui Pointer to wake-up input device source
208  * @param handler A function called when its device input event issued
209  * @param source Pointer to device instance issued a callback function
210  */
211 void npcx_miwu_init_dev_callback(struct miwu_callback *callback,
212 				const struct npcx_wui *dev_wui,
213 				miwu_dev_callback_handler_t handler,
214 				const struct device *source);
215 
216 /**
217  * @brief Function to insert or remove a miwu callback from a callback list
218  *
219  * @param callback Pointer to miwu callback structure
220  * @param set A boolean indicating insertion or removal of the callback
221  *
222  * @retval 0 If successful.
223  * @retval -EINVAL Invalid parameters
224  */
225 int npcx_miwu_manage_callback(struct miwu_callback *cb, bool set);
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 #endif /* _NUVOTON_NPCX_SOC_MIWU_H_ */
231