1 /*
2 * Copyright (c) 2021, Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nordic_nrf_gpio
8
9 #include <nrfx_gpiote.h>
10 #include <string.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/dt-bindings/gpio/nordic-nrf-gpio.h>
13 #include <zephyr/irq.h>
14
15 #include <zephyr/drivers/gpio/gpio_utils.h>
16
17 #ifdef CONFIG_SOC_NRF54H20_GPD
18 #include <nrf/gpd.h>
19 #endif
20
21 struct gpio_nrfx_data {
22 /* gpio_driver_data needs to be first */
23 struct gpio_driver_data common;
24 sys_slist_t callbacks;
25 };
26
27 struct gpio_nrfx_cfg {
28 /* gpio_driver_config needs to be first */
29 struct gpio_driver_config common;
30 NRF_GPIO_Type *port;
31 uint32_t edge_sense;
32 uint8_t port_num;
33 nrfx_gpiote_t gpiote;
34 #ifdef CONFIG_SOC_NRF54H20_GPD
35 uint8_t pad_pd;
36 #endif
37 };
38
get_port_data(const struct device * port)39 static inline struct gpio_nrfx_data *get_port_data(const struct device *port)
40 {
41 return port->data;
42 }
43
get_port_cfg(const struct device * port)44 static inline const struct gpio_nrfx_cfg *get_port_cfg(const struct device *port)
45 {
46 return port->config;
47 }
48
has_gpiote(const struct gpio_nrfx_cfg * cfg)49 static bool has_gpiote(const struct gpio_nrfx_cfg *cfg)
50 {
51 return cfg->gpiote.p_reg != NULL;
52 }
53
get_pull(gpio_flags_t flags)54 static nrf_gpio_pin_pull_t get_pull(gpio_flags_t flags)
55 {
56 if (flags & GPIO_PULL_UP) {
57 return NRF_GPIO_PIN_PULLUP;
58 } else if (flags & GPIO_PULL_DOWN) {
59 return NRF_GPIO_PIN_PULLDOWN;
60 }
61
62 return NRF_GPIO_PIN_NOPULL;
63 }
64
gpio_nrfx_gpd_retain_set(const struct device * port,uint32_t mask,gpio_flags_t flags)65 static int gpio_nrfx_gpd_retain_set(const struct device *port, uint32_t mask, gpio_flags_t flags)
66 {
67 #ifdef CONFIG_SOC_NRF54H20_GPD
68 const struct gpio_nrfx_cfg *cfg = get_port_cfg(port);
69
70 if (cfg->pad_pd == NRF_GPD_FAST_ACTIVE1) {
71 int ret;
72
73 if (flags & GPIO_OUTPUT) {
74 nrf_gpio_port_retain_enable(cfg->port, mask);
75 }
76
77 ret = nrf_gpd_release(NRF_GPD_FAST_ACTIVE1);
78 if (ret < 0) {
79 return ret;
80 }
81 }
82 #else
83 ARG_UNUSED(port);
84 ARG_UNUSED(mask);
85 ARG_UNUSED(flags);
86 #endif
87
88 return 0;
89 }
90
gpio_nrfx_gpd_retain_clear(const struct device * port,uint32_t mask)91 static int gpio_nrfx_gpd_retain_clear(const struct device *port, uint32_t mask)
92 {
93 #ifdef CONFIG_SOC_NRF54H20_GPD
94 const struct gpio_nrfx_cfg *cfg = get_port_cfg(port);
95
96 if (cfg->pad_pd == NRF_GPD_FAST_ACTIVE1) {
97 int ret;
98
99 ret = nrf_gpd_request(NRF_GPD_FAST_ACTIVE1);
100 if (ret < 0) {
101 return ret;
102 }
103
104 nrf_gpio_port_retain_disable(cfg->port, mask);
105 }
106 #else
107 ARG_UNUSED(port);
108 ARG_UNUSED(mask);
109 #endif
110
111 return 0;
112 }
113
gpio_nrfx_pin_configure(const struct device * port,gpio_pin_t pin,gpio_flags_t flags)114 static int gpio_nrfx_pin_configure(const struct device *port, gpio_pin_t pin,
115 gpio_flags_t flags)
116 {
117 int ret = 0;
118 nrfx_err_t err = NRFX_SUCCESS;
119 uint8_t ch;
120 bool free_ch = false;
121 const struct gpio_nrfx_cfg *cfg = get_port_cfg(port);
122 nrfx_gpiote_pin_t abs_pin = NRF_GPIO_PIN_MAP(cfg->port_num, pin);
123 nrf_gpio_pin_pull_t pull = get_pull(flags);
124 nrf_gpio_pin_drive_t drive;
125
126 switch (flags & (NRF_GPIO_DRIVE_MSK | GPIO_OPEN_DRAIN)) {
127 case NRF_GPIO_DRIVE_S0S1:
128 drive = NRF_GPIO_PIN_S0S1;
129 break;
130 case NRF_GPIO_DRIVE_S0H1:
131 drive = NRF_GPIO_PIN_S0H1;
132 break;
133 case NRF_GPIO_DRIVE_H0S1:
134 drive = NRF_GPIO_PIN_H0S1;
135 break;
136 case NRF_GPIO_DRIVE_H0H1:
137 drive = NRF_GPIO_PIN_H0H1;
138 break;
139 case NRF_GPIO_DRIVE_S0 | GPIO_OPEN_DRAIN:
140 drive = NRF_GPIO_PIN_S0D1;
141 break;
142 case NRF_GPIO_DRIVE_H0 | GPIO_OPEN_DRAIN:
143 drive = NRF_GPIO_PIN_H0D1;
144 break;
145 case NRF_GPIO_DRIVE_S1 | GPIO_OPEN_SOURCE:
146 drive = NRF_GPIO_PIN_D0S1;
147 break;
148 case NRF_GPIO_DRIVE_H1 | GPIO_OPEN_SOURCE:
149 drive = NRF_GPIO_PIN_D0H1;
150 break;
151 default:
152 return -EINVAL;
153 }
154
155 ret = gpio_nrfx_gpd_retain_clear(port, BIT(pin));
156 if (ret < 0) {
157 return ret;
158 }
159
160 if (flags & GPIO_OUTPUT_INIT_HIGH) {
161 nrf_gpio_port_out_set(cfg->port, BIT(pin));
162 } else if (flags & GPIO_OUTPUT_INIT_LOW) {
163 nrf_gpio_port_out_clear(cfg->port, BIT(pin));
164 }
165
166 if (!has_gpiote(cfg)) {
167 nrf_gpio_pin_dir_t dir = (flags & GPIO_OUTPUT)
168 ? NRF_GPIO_PIN_DIR_OUTPUT
169 : NRF_GPIO_PIN_DIR_INPUT;
170 nrf_gpio_pin_input_t input = (flags & GPIO_INPUT)
171 ? NRF_GPIO_PIN_INPUT_CONNECT
172 : NRF_GPIO_PIN_INPUT_DISCONNECT;
173
174 nrf_gpio_reconfigure(abs_pin, &dir, &input, &pull, &drive, NULL);
175
176 goto end;
177 }
178
179 /* Get the GPIOTE channel associated with this pin, if any. It needs
180 * to be freed when the pin is reconfigured or disconnected.
181 */
182 if (IS_ENABLED(CONFIG_GPIO_NRFX_INTERRUPT)) {
183 err = nrfx_gpiote_channel_get(&cfg->gpiote, abs_pin, &ch);
184 free_ch = (err == NRFX_SUCCESS);
185 }
186
187 if ((flags & (GPIO_INPUT | GPIO_OUTPUT)) == GPIO_DISCONNECTED) {
188 /* Ignore the error code. The pin may not have been used. */
189 (void)nrfx_gpiote_pin_uninit(&cfg->gpiote, abs_pin);
190 } else {
191 /* Remove previously configured trigger when pin is reconfigured. */
192 if (IS_ENABLED(CONFIG_GPIO_NRFX_INTERRUPT)) {
193 nrfx_gpiote_trigger_config_t trigger_config = {
194 .trigger = NRFX_GPIOTE_TRIGGER_NONE,
195 };
196 nrfx_gpiote_input_pin_config_t input_pin_config = {
197 .p_trigger_config = &trigger_config,
198 };
199
200 err = nrfx_gpiote_input_configure(&cfg->gpiote,
201 abs_pin, &input_pin_config);
202 if (err != NRFX_SUCCESS) {
203 ret = -EINVAL;
204 goto end;
205 }
206 }
207
208 if (flags & GPIO_OUTPUT) {
209 nrfx_gpiote_output_config_t output_config = {
210 .drive = drive,
211 .input_connect = (flags & GPIO_INPUT)
212 ? NRF_GPIO_PIN_INPUT_CONNECT
213 : NRF_GPIO_PIN_INPUT_DISCONNECT,
214 .pull = pull,
215 };
216
217 err = nrfx_gpiote_output_configure(&cfg->gpiote,
218 abs_pin, &output_config, NULL);
219 } else {
220 nrfx_gpiote_input_pin_config_t input_pin_config = {
221 .p_pull_config = &pull,
222 };
223
224 err = nrfx_gpiote_input_configure(&cfg->gpiote,
225 abs_pin, &input_pin_config);
226 }
227
228 if (err != NRFX_SUCCESS) {
229 ret = -EINVAL;
230 goto end;
231 }
232 }
233
234 if (IS_ENABLED(CONFIG_GPIO_NRFX_INTERRUPT) && free_ch) {
235 err = nrfx_gpiote_channel_free(&cfg->gpiote, ch);
236 __ASSERT_NO_MSG(err == NRFX_SUCCESS);
237 }
238
239 end:
240 (void)gpio_nrfx_gpd_retain_set(port, BIT(pin), flags);
241 return ret;
242 }
243
gpio_nrfx_port_get_raw(const struct device * port,gpio_port_value_t * value)244 static int gpio_nrfx_port_get_raw(const struct device *port,
245 gpio_port_value_t *value)
246 {
247 NRF_GPIO_Type *reg = get_port_cfg(port)->port;
248
249 *value = nrf_gpio_port_in_read(reg);
250
251 return 0;
252 }
253
gpio_nrfx_port_set_masked_raw(const struct device * port,gpio_port_pins_t mask,gpio_port_value_t value)254 static int gpio_nrfx_port_set_masked_raw(const struct device *port,
255 gpio_port_pins_t mask,
256 gpio_port_value_t value)
257 {
258 NRF_GPIO_Type *reg = get_port_cfg(port)->port;
259 int ret;
260
261 const uint32_t set_mask = value & mask;
262 const uint32_t clear_mask = (~set_mask) & mask;
263
264 ret = gpio_nrfx_gpd_retain_clear(port, mask);
265 if (ret < 0) {
266 return ret;
267 }
268
269 nrf_gpio_port_out_set(reg, set_mask);
270 nrf_gpio_port_out_clear(reg, clear_mask);
271
272 return gpio_nrfx_gpd_retain_set(port, mask, GPIO_OUTPUT);
273 }
274
gpio_nrfx_port_set_bits_raw(const struct device * port,gpio_port_pins_t mask)275 static int gpio_nrfx_port_set_bits_raw(const struct device *port,
276 gpio_port_pins_t mask)
277 {
278 NRF_GPIO_Type *reg = get_port_cfg(port)->port;
279 int ret;
280
281 ret = gpio_nrfx_gpd_retain_clear(port, mask);
282 if (ret < 0) {
283 return ret;
284 }
285
286 nrf_gpio_port_out_set(reg, mask);
287
288 return gpio_nrfx_gpd_retain_set(port, mask, GPIO_OUTPUT);
289 }
290
gpio_nrfx_port_clear_bits_raw(const struct device * port,gpio_port_pins_t mask)291 static int gpio_nrfx_port_clear_bits_raw(const struct device *port,
292 gpio_port_pins_t mask)
293 {
294 NRF_GPIO_Type *reg = get_port_cfg(port)->port;
295 int ret;
296
297 ret = gpio_nrfx_gpd_retain_clear(port, mask);
298 if (ret < 0) {
299 return ret;
300 }
301
302 nrf_gpio_port_out_clear(reg, mask);
303
304 return gpio_nrfx_gpd_retain_set(port, mask, GPIO_OUTPUT);
305 }
306
gpio_nrfx_port_toggle_bits(const struct device * port,gpio_port_pins_t mask)307 static int gpio_nrfx_port_toggle_bits(const struct device *port,
308 gpio_port_pins_t mask)
309 {
310 NRF_GPIO_Type *reg = get_port_cfg(port)->port;
311 const uint32_t value = nrf_gpio_port_out_read(reg) ^ mask;
312 const uint32_t set_mask = value & mask;
313 const uint32_t clear_mask = (~value) & mask;
314 int ret;
315
316 ret = gpio_nrfx_gpd_retain_clear(port, mask);
317 if (ret < 0) {
318 return ret;
319 }
320
321 nrf_gpio_port_out_set(reg, set_mask);
322 nrf_gpio_port_out_clear(reg, clear_mask);
323
324 return gpio_nrfx_gpd_retain_set(port, mask, GPIO_OUTPUT);
325 }
326
327 #ifdef CONFIG_GPIO_NRFX_INTERRUPT
get_trigger(enum gpio_int_mode mode,enum gpio_int_trig trig)328 static nrfx_gpiote_trigger_t get_trigger(enum gpio_int_mode mode,
329 enum gpio_int_trig trig)
330 {
331 if (mode == GPIO_INT_MODE_LEVEL) {
332 return trig == GPIO_INT_TRIG_LOW ? NRFX_GPIOTE_TRIGGER_LOW :
333 NRFX_GPIOTE_TRIGGER_HIGH;
334 }
335
336 return trig == GPIO_INT_TRIG_BOTH ? NRFX_GPIOTE_TRIGGER_TOGGLE :
337 trig == GPIO_INT_TRIG_LOW ? NRFX_GPIOTE_TRIGGER_HITOLO :
338 NRFX_GPIOTE_TRIGGER_LOTOHI;
339 }
340
gpio_nrfx_pin_interrupt_configure(const struct device * port,gpio_pin_t pin,enum gpio_int_mode mode,enum gpio_int_trig trig)341 static int gpio_nrfx_pin_interrupt_configure(const struct device *port,
342 gpio_pin_t pin,
343 enum gpio_int_mode mode,
344 enum gpio_int_trig trig)
345 {
346 const struct gpio_nrfx_cfg *cfg = get_port_cfg(port);
347 uint32_t abs_pin = NRF_GPIO_PIN_MAP(cfg->port_num, pin);
348 nrfx_err_t err;
349 uint8_t ch;
350
351 if (!has_gpiote(cfg)) {
352 return -ENOTSUP;
353 }
354
355 if (mode == GPIO_INT_MODE_DISABLED) {
356 nrfx_gpiote_trigger_disable(&cfg->gpiote, abs_pin);
357
358 return 0;
359 }
360
361 nrfx_gpiote_trigger_config_t trigger_config = {
362 .trigger = get_trigger(mode, trig),
363 };
364 nrfx_gpiote_input_pin_config_t input_pin_config = {
365 .p_trigger_config = &trigger_config,
366 };
367
368 /* If edge mode is to be used and pin is not configured to use sense for
369 * edge use IN event.
370 */
371 if (!(BIT(pin) & cfg->edge_sense) &&
372 (mode == GPIO_INT_MODE_EDGE) &&
373 (nrf_gpio_pin_dir_get(abs_pin) == NRF_GPIO_PIN_DIR_INPUT)) {
374 err = nrfx_gpiote_channel_get(&cfg->gpiote, abs_pin, &ch);
375 if (err == NRFX_ERROR_INVALID_PARAM) {
376 err = nrfx_gpiote_channel_alloc(&cfg->gpiote, &ch);
377 if (err != NRFX_SUCCESS) {
378 return -ENOMEM;
379 }
380 }
381
382 trigger_config.p_in_channel = &ch;
383 }
384
385 err = nrfx_gpiote_input_configure(&cfg->gpiote, abs_pin, &input_pin_config);
386 if (err != NRFX_SUCCESS) {
387 return -EINVAL;
388 }
389
390 nrfx_gpiote_trigger_enable(&cfg->gpiote, abs_pin, true);
391
392 return 0;
393 }
394
gpio_nrfx_manage_callback(const struct device * port,struct gpio_callback * callback,bool set)395 static int gpio_nrfx_manage_callback(const struct device *port,
396 struct gpio_callback *callback,
397 bool set)
398 {
399 return gpio_manage_callback(&get_port_data(port)->callbacks,
400 callback, set);
401 }
402 #endif /* CONFIG_GPIO_NRFX_INTERRUPT */
403
404 #ifdef CONFIG_GPIO_GET_DIRECTION
gpio_nrfx_port_get_direction(const struct device * port,gpio_port_pins_t map,gpio_port_pins_t * inputs,gpio_port_pins_t * outputs)405 static int gpio_nrfx_port_get_direction(const struct device *port,
406 gpio_port_pins_t map,
407 gpio_port_pins_t *inputs,
408 gpio_port_pins_t *outputs)
409 {
410 const struct gpio_nrfx_cfg *cfg = get_port_cfg(port);
411 NRF_GPIO_Type *reg = cfg->port;
412
413 map &= cfg->common.port_pin_mask;
414
415 if (outputs != NULL) {
416 *outputs = map & nrf_gpio_port_dir_read(cfg->port);
417 }
418
419 if (inputs != NULL) {
420 *inputs = 0;
421 while (map) {
422 uint32_t pin = NRF_CTZ(map);
423 uint32_t pin_cnf = reg->PIN_CNF[pin];
424
425 /* Check if the pin has its input buffer connected. */
426 if (((pin_cnf & GPIO_PIN_CNF_INPUT_Msk) >>
427 GPIO_PIN_CNF_INPUT_Pos) ==
428 GPIO_PIN_CNF_INPUT_Connect) {
429 *inputs |= BIT(pin);
430 }
431
432 map &= ~BIT(pin);
433 }
434 }
435
436 return 0;
437 }
438 #endif /* CONFIG_GPIO_GET_DIRECTION */
439
440 #ifdef CONFIG_GPIO_NRFX_INTERRUPT
441 /* Get port device from port id. */
get_dev(uint32_t port_id)442 static const struct device *get_dev(uint32_t port_id)
443 {
444 const struct device *dev = NULL;
445
446 #define GPIO_NRF_GET_DEV(i) \
447 else if (DT_INST_PROP(i, port) == port_id) { \
448 dev = DEVICE_DT_INST_GET(i); \
449 }
450
451 if (0) {
452 } /* Followed by else if from FOREACH macro. Done to avoid return statement in macro. */
453 DT_INST_FOREACH_STATUS_OKAY(GPIO_NRF_GET_DEV)
454 #undef GPIO_NRF_GET_DEV
455
456 return dev;
457 }
458
nrfx_gpio_handler(nrfx_gpiote_pin_t abs_pin,nrfx_gpiote_trigger_t trigger,void * context)459 static void nrfx_gpio_handler(nrfx_gpiote_pin_t abs_pin,
460 nrfx_gpiote_trigger_t trigger,
461 void *context)
462 {
463 uint32_t pin = abs_pin;
464 uint32_t port_id = nrf_gpio_pin_port_number_extract(&pin);
465 const struct device *port = get_dev(port_id);
466
467 /* If given port is handled directly by nrfx driver it might not be enabled in DT. */
468 if (port == NULL) {
469 return;
470 }
471
472 struct gpio_nrfx_data *data = get_port_data(port);
473 sys_slist_t *list = &data->callbacks;
474
475 gpio_fire_callbacks(list, port, BIT(pin));
476 }
477 #endif /* CONFIG_GPIO_NRFX_INTERRUPT */
478
479 #define GPIOTE_IRQ_HANDLER_CONNECT(node_id) \
480 IRQ_CONNECT(DT_IRQN(node_id), DT_IRQ(node_id, priority), nrfx_isr, \
481 NRFX_CONCAT(nrfx_gpiote_, DT_PROP(node_id, instance), _irq_handler), 0);
482
gpio_nrfx_init(const struct device * port)483 static int gpio_nrfx_init(const struct device *port)
484 {
485 const struct gpio_nrfx_cfg *cfg = get_port_cfg(port);
486 nrfx_err_t err;
487
488 if (!has_gpiote(cfg)) {
489 return 0;
490 }
491
492 if (nrfx_gpiote_init_check(&cfg->gpiote)) {
493 return 0;
494 }
495
496 err = nrfx_gpiote_init(&cfg->gpiote, 0 /*not used*/);
497 if (err != NRFX_SUCCESS) {
498 return -EIO;
499 }
500
501 #ifdef CONFIG_GPIO_NRFX_INTERRUPT
502 nrfx_gpiote_global_callback_set(&cfg->gpiote, nrfx_gpio_handler, NULL);
503 DT_FOREACH_STATUS_OKAY(nordic_nrf_gpiote, GPIOTE_IRQ_HANDLER_CONNECT);
504 #endif /* CONFIG_GPIO_NRFX_INTERRUPT */
505
506 return 0;
507 }
508
509 static DEVICE_API(gpio, gpio_nrfx_drv_api_funcs) = {
510 .pin_configure = gpio_nrfx_pin_configure,
511 .port_get_raw = gpio_nrfx_port_get_raw,
512 .port_set_masked_raw = gpio_nrfx_port_set_masked_raw,
513 .port_set_bits_raw = gpio_nrfx_port_set_bits_raw,
514 .port_clear_bits_raw = gpio_nrfx_port_clear_bits_raw,
515 .port_toggle_bits = gpio_nrfx_port_toggle_bits,
516 #ifdef CONFIG_GPIO_NRFX_INTERRUPT
517 .pin_interrupt_configure = gpio_nrfx_pin_interrupt_configure,
518 .manage_callback = gpio_nrfx_manage_callback,
519 #endif
520 #ifdef CONFIG_GPIO_GET_DIRECTION
521 .port_get_direction = gpio_nrfx_port_get_direction,
522 #endif
523 };
524
525 #define GPIOTE_PHANDLE(id) DT_INST_PHANDLE(id, gpiote_instance)
526 #define GPIOTE_INST(id) DT_PROP(GPIOTE_PHANDLE(id), instance)
527
528 #define GPIOTE_INSTANCE(id) \
529 COND_CODE_1(DT_INST_NODE_HAS_PROP(id, gpiote_instance), \
530 (NRFX_GPIOTE_INSTANCE(GPIOTE_INST(id))), \
531 ({ .p_reg = NULL }))
532
533 /* Device instantiation is done with node labels because 'port_num' is
534 * the peripheral number by SoC numbering. We therefore cannot use
535 * DT_INST APIs here without wider changes.
536 */
537
538 #define GPIOTE_CHECK(id) \
539 COND_CODE_1(DT_INST_NODE_HAS_PROP(id, gpiote_instance), \
540 (BUILD_ASSERT(DT_NODE_HAS_STATUS_OKAY(GPIOTE_PHANDLE(id)), \
541 "Please enable GPIOTE instance for used GPIO port!")), \
542 ())
543
544 #ifdef CONFIG_SOC_NRF54H20_GPD
545 #define PAD_PD(inst) \
546 .pad_pd = DT_INST_PHA_BY_NAME_OR(inst, power_domains, pad, id, \
547 NRF_GPD_SLOW_MAIN),
548 #else
549 #define PAD_PD(inst)
550 #endif
551
552 #define GPIO_NRF_DEVICE(id) \
553 GPIOTE_CHECK(id); \
554 static const struct gpio_nrfx_cfg gpio_nrfx_p##id##_cfg = { \
555 .common = { \
556 .port_pin_mask = \
557 GPIO_PORT_PIN_MASK_FROM_DT_INST(id), \
558 }, \
559 .port = _CONCAT(NRF_P, DT_INST_PROP(id, port)), \
560 .port_num = DT_INST_PROP(id, port), \
561 .edge_sense = DT_INST_PROP_OR(id, sense_edge_mask, 0), \
562 .gpiote = GPIOTE_INSTANCE(id), \
563 PAD_PD(id) \
564 }; \
565 \
566 static struct gpio_nrfx_data gpio_nrfx_p##id##_data; \
567 \
568 DEVICE_DT_INST_DEFINE(id, gpio_nrfx_init, \
569 NULL, \
570 &gpio_nrfx_p##id##_data, \
571 &gpio_nrfx_p##id##_cfg, \
572 PRE_KERNEL_1, \
573 CONFIG_GPIO_INIT_PRIORITY, \
574 &gpio_nrfx_drv_api_funcs);
575
576 DT_INST_FOREACH_STATUS_OKAY(GPIO_NRF_DEVICE)
577