1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Freescale Semiconductor, Inc.
4  * Copyright 2017-2018 NXP
5  *	Dong Aisheng <aisheng.dong@nxp.com>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/firmware/imx/sci.h>
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/pinctrl/pinctrl.h>
13 #include <linux/platform_device.h>
14 
15 #include "../core.h"
16 #include "pinctrl-imx.h"
17 
18 enum pad_func_e {
19 	IMX_SC_PAD_FUNC_SET = 15,
20 	IMX_SC_PAD_FUNC_GET = 16,
21 };
22 
23 struct imx_sc_msg_req_pad_set {
24 	struct imx_sc_rpc_msg hdr;
25 	u32 val;
26 	u16 pad;
27 } __packed __aligned(4);
28 
29 struct imx_sc_msg_req_pad_get {
30 	struct imx_sc_rpc_msg hdr;
31 	u16 pad;
32 } __packed __aligned(4);
33 
34 struct imx_sc_msg_resp_pad_get {
35 	struct imx_sc_rpc_msg hdr;
36 	u32 val;
37 } __packed;
38 
39 static struct imx_sc_ipc *pinctrl_ipc_handle;
40 
41 int imx_pinctrl_sc_ipc_init(struct platform_device *pdev)
42 {
43 	return imx_scu_get_handle(&pinctrl_ipc_handle);
44 }
45 EXPORT_SYMBOL_GPL(imx_pinctrl_sc_ipc_init);
46 
47 int imx_pinconf_get_scu(struct pinctrl_dev *pctldev, unsigned pin_id,
48 			unsigned long *config)
49 {
50 	struct imx_sc_msg_req_pad_get msg;
51 	struct imx_sc_msg_resp_pad_get *resp;
52 	struct imx_sc_rpc_msg *hdr = &msg.hdr;
53 	int ret;
54 
55 	hdr->ver = IMX_SC_RPC_VERSION;
56 	hdr->svc = IMX_SC_RPC_SVC_PAD;
57 	hdr->func = IMX_SC_PAD_FUNC_GET;
58 	hdr->size = 2;
59 
60 	msg.pad = pin_id;
61 
62 	ret = imx_scu_call_rpc(pinctrl_ipc_handle, &msg, true);
63 	if (ret)
64 		return ret;
65 
66 	resp = (struct imx_sc_msg_resp_pad_get *)&msg;
67 	*config = resp->val;
68 
69 	return 0;
70 }
71 EXPORT_SYMBOL_GPL(imx_pinconf_get_scu);
72 
73 int imx_pinconf_set_scu(struct pinctrl_dev *pctldev, unsigned pin_id,
74 			unsigned long *configs, unsigned num_configs)
75 {
76 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
77 	struct imx_sc_msg_req_pad_set msg;
78 	struct imx_sc_rpc_msg *hdr = &msg.hdr;
79 	unsigned int mux = configs[0];
80 	unsigned int conf = configs[1];
81 	unsigned int val;
82 	int ret;
83 
84 	/*
85 	 * Set mux and conf together in one IPC call
86 	 */
87 	WARN_ON(num_configs != 2);
88 
89 	val = conf | BM_PAD_CTL_IFMUX_ENABLE | BM_PAD_CTL_GP_ENABLE;
90 	val |= mux << BP_PAD_CTL_IFMUX;
91 
92 	hdr->ver = IMX_SC_RPC_VERSION;
93 	hdr->svc = IMX_SC_RPC_SVC_PAD;
94 	hdr->func = IMX_SC_PAD_FUNC_SET;
95 	hdr->size = 3;
96 
97 	msg.pad = pin_id;
98 	msg.val = val;
99 
100 	ret = imx_scu_call_rpc(pinctrl_ipc_handle, &msg, true);
101 
102 	dev_dbg(ipctl->dev, "write: pin_id %u config 0x%x val 0x%x\n",
103 		pin_id, conf, val);
104 
105 	return ret;
106 }
107 EXPORT_SYMBOL_GPL(imx_pinconf_set_scu);
108 
109 void imx_pinctrl_parse_pin_scu(struct imx_pinctrl *ipctl,
110 			       unsigned int *pin_id, struct imx_pin *pin,
111 			       const __be32 **list_p)
112 {
113 	const struct imx_pinctrl_soc_info *info = ipctl->info;
114 	struct imx_pin_scu *pin_scu = &pin->conf.scu;
115 	const __be32 *list = *list_p;
116 
117 	pin->pin = be32_to_cpu(*list++);
118 	*pin_id = pin->pin;
119 	pin_scu->mux_mode = be32_to_cpu(*list++);
120 	pin_scu->config = be32_to_cpu(*list++);
121 	*list_p = list;
122 
123 	dev_dbg(ipctl->dev, "%s: 0x%x 0x%08lx", info->pins[pin->pin].name,
124 		pin_scu->mux_mode, pin_scu->config);
125 }
126 EXPORT_SYMBOL_GPL(imx_pinctrl_parse_pin_scu);
127 
128 MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
129 MODULE_DESCRIPTION("NXP i.MX SCU common pinctrl driver");
130 MODULE_LICENSE("GPL v2");
131