1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/pinctrl.h>
8 
pinctrl_lookup_state(const struct pinctrl_dev_config * config,uint8_t id,const struct pinctrl_state ** state)9 int pinctrl_lookup_state(const struct pinctrl_dev_config *config, uint8_t id,
10 			 const struct pinctrl_state **state)
11 {
12 	*state = &config->states[0];
13 	while (*state < &config->states[config->state_cnt]) {
14 		if (id == (*state)->id) {
15 			return 0;
16 		}
17 
18 		(*state)++;
19 	}
20 
21 	return -ENOENT;
22 }
23 
24 #ifdef CONFIG_PINCTRL_DYNAMIC
pinctrl_update_states(struct pinctrl_dev_config * config,const struct pinctrl_state * states,uint8_t state_cnt)25 int pinctrl_update_states(struct pinctrl_dev_config *config,
26 			  const struct pinctrl_state *states,
27 			  uint8_t state_cnt)
28 {
29 	uint8_t equal = 0U;
30 
31 	/* check we are inserting same number of states */
32 	if (config->state_cnt != state_cnt) {
33 		return -EINVAL;
34 	}
35 
36 	/* check we have the same states */
37 	for (uint8_t i = 0U; i < state_cnt; i++) {
38 		for (uint8_t j = 0U; j < config->state_cnt; j++) {
39 			if (states[i].id == config->states[j].id) {
40 				equal++;
41 				break;
42 			}
43 		}
44 	}
45 
46 	if (equal != state_cnt) {
47 		return -EINVAL;
48 	}
49 
50 	/* replace current states */
51 	config->states = states;
52 
53 	return 0;
54 }
55 #endif /* CONFIG_PINCTRL_DYNAMIC */
56