1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/firmware/scmi/pinctrl.h>
8 #include <zephyr/kernel.h>
9 
10 DT_SCMI_PROTOCOL_DEFINE_NODEV(DT_INST(0, arm_scmi_pinctrl), NULL);
11 
scmi_pinctrl_settings_configure(struct scmi_pinctrl_settings * settings)12 int scmi_pinctrl_settings_configure(struct scmi_pinctrl_settings *settings)
13 {
14 	struct scmi_protocol *proto;
15 	struct scmi_message msg, reply;
16 	uint32_t config_num;
17 	int32_t status, ret;
18 	bool use_polling;
19 
20 	proto = &SCMI_PROTOCOL_NAME(SCMI_PROTOCOL_PINCTRL);
21 
22 	/* sanity checks */
23 	if (!settings) {
24 		return -EINVAL;
25 	}
26 
27 	if (!proto) {
28 		return -EINVAL;
29 	}
30 
31 	if (proto->id != SCMI_PROTOCOL_PINCTRL) {
32 		return -EINVAL;
33 	}
34 
35 	config_num = SCMI_PINCTRL_ATTRIBUTES_CONFIG_NUM(settings->attributes);
36 
37 	if (!config_num) {
38 		return -EINVAL;
39 	}
40 
41 	if ((config_num * 2) > ARM_SCMI_PINCTRL_MAX_CONFIG_SIZE) {
42 		return -EINVAL;
43 	}
44 
45 	msg.hdr = SCMI_MESSAGE_HDR_MAKE(SCMI_PINCTRL_MSG_PINCTRL_SETTINGS_CONFIGURE,
46 					SCMI_COMMAND, proto->id, 0x0);
47 	msg.len = sizeof(*settings) -
48 		(ARM_SCMI_PINCTRL_MAX_CONFIG_SIZE - config_num * 2) * 4;
49 	msg.content = settings;
50 
51 	reply.hdr = msg.hdr;
52 	reply.len = sizeof(status);
53 	reply.content = &status;
54 
55 	use_polling = k_is_pre_kernel();
56 
57 	ret = scmi_send_message(proto, &msg, &reply, use_polling);
58 	if (ret < 0) {
59 		return ret;
60 	}
61 
62 	if (status != SCMI_SUCCESS) {
63 		return scmi_status_to_errno(status);
64 	}
65 
66 	return 0;
67 }
68