xref: /OK3568_Linux_fs/u-boot/drivers/pinctrl/pinctrl-uclass.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <linux/libfdt.h>
9 #include <linux/err.h>
10 #include <linux/list.h>
11 #include <dm.h>
12 #include <dm/lists.h>
13 #include <dm/pinctrl.h>
14 #include <dm/util.h>
15 #include <dm/of_access.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
pinctrl_decode_pin_config(const void * blob,int node)19 int pinctrl_decode_pin_config(const void *blob, int node)
20 {
21 	int flags = 0;
22 
23 	if (fdtdec_get_bool(blob, node, "bias-pull-up"))
24 		flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
25 	else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
26 		flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
27 
28 	return flags;
29 }
30 
31 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
32 /**
33  * pinctrl_config_one() - apply pinctrl settings for a single node
34  *
35  * @config: pin configuration node
36  * @return: 0 on success, or negative error code on failure
37  */
pinctrl_config_one(struct udevice * config)38 static int pinctrl_config_one(struct udevice *config)
39 {
40 	struct udevice *pctldev;
41 	const struct pinctrl_ops *ops;
42 
43 	pctldev = config;
44 	for (;;) {
45 		pctldev = dev_get_parent(pctldev);
46 		if (!pctldev) {
47 			dev_err(config, "could not find pctldev\n");
48 			return -EINVAL;
49 		}
50 		if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
51 			break;
52 	}
53 
54 	ops = pinctrl_get_ops(pctldev);
55 	return ops->set_state(pctldev, config);
56 }
57 
58 /**
59  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60  *
61  * @dev: peripheral device
62  * @statename: state name, like "default"
63  * @return: 0 on success, or negative error code on failure
64  */
pinctrl_select_state_full(struct udevice * dev,const char * statename)65 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66 {
67 	char propname[32]; /* long enough */
68 	const fdt32_t *list;
69 	uint32_t phandle;
70 	struct udevice *config;
71 	int state, size, i, ret;
72 
73 	state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
74 	if (state < 0) {
75 		char *end;
76 		/*
77 		 * If statename is not found in "pinctrl-names",
78 		 * assume statename is just the integer state ID.
79 		 */
80 		state = simple_strtoul(statename, &end, 10);
81 		if (*end)
82 			return -EINVAL;
83 	}
84 
85 	snprintf(propname, sizeof(propname), "pinctrl-%d", state);
86 	list = dev_read_prop(dev, propname, &size);
87 	if (!list)
88 		return -EINVAL;
89 
90 	size /= sizeof(*list);
91 	for (i = 0; i < size; i++) {
92 		phandle = fdt32_to_cpu(*list++);
93 		ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
94 						      &config);
95 		if (ret)
96 			return ret;
97 
98 		ret = pinctrl_config_one(config);
99 		if (ret)
100 			return ret;
101 	}
102 
103 	return 0;
104 }
105 
106 /**
107  * pinconfig_post_bind() - post binding for PINCONFIG uclass
108  * Recursively bind its children as pinconfig devices.
109  *
110  * @dev: pinconfig device
111  * @return: 0 on success, or negative error code on failure
112  */
pinconfig_post_bind(struct udevice * dev)113 static int pinconfig_post_bind(struct udevice *dev)
114 {
115 	bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
116 	const char *name;
117 	ofnode node;
118 	int ret;
119 
120 	dev_for_each_subnode(node, dev) {
121 		if (pre_reloc_only &&
122 		    !ofnode_pre_reloc(node))
123 			continue;
124 		/*
125 		 * If this node has "compatible" property, this is not
126 		 * a pin configuration node, but a normal device. skip.
127 		 */
128 		ofnode_get_property(node, "compatible", &ret);
129 		if (ret >= 0)
130 			continue;
131 
132 		if (ret != -FDT_ERR_NOTFOUND)
133 			return ret;
134 
135 		name = ofnode_get_name(node);
136 		if (!name)
137 			return -EINVAL;
138 		ret = device_bind_driver_to_node(dev, "pinconfig", name,
139 						 node, NULL);
140 		if (ret)
141 			return ret;
142 	}
143 
144 	return 0;
145 }
146 
147 UCLASS_DRIVER(pinconfig) = {
148 	.id = UCLASS_PINCONFIG,
149 	.post_bind = pinconfig_post_bind,
150 	.name = "pinconfig",
151 };
152 
153 U_BOOT_DRIVER(pinconfig_generic) = {
154 	.name = "pinconfig",
155 	.id = UCLASS_PINCONFIG,
156 };
157 
158 #else
pinctrl_select_state_full(struct udevice * dev,const char * statename)159 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
160 {
161 	return -ENODEV;
162 }
163 
pinconfig_post_bind(struct udevice * dev)164 static int pinconfig_post_bind(struct udevice *dev)
165 {
166 	return 0;
167 }
168 #endif
169 
170 /**
171  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
172  *
173  * @dev: peripheral device
174  * @return: 0 on success, or negative error code on failure
175  */
pinctrl_select_state_simple(struct udevice * dev)176 static int pinctrl_select_state_simple(struct udevice *dev)
177 {
178 	struct udevice *pctldev;
179 	struct pinctrl_ops *ops;
180 	int ret;
181 
182 	/*
183 	 * For most system, there is only one pincontroller device. But in
184 	 * case of multiple pincontroller devices, probe the one with sequence
185 	 * number 0 (defined by alias) to avoid race condition.
186 	 */
187 	ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
188 	if (ret)
189 		/* if not found, get the first one */
190 		ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
191 	if (ret)
192 		return ret;
193 
194 	ops = pinctrl_get_ops(pctldev);
195 	if (!ops->set_state_simple) {
196 		dev_dbg(dev, "set_state_simple op missing\n");
197 		return -ENOSYS;
198 	}
199 
200 	return ops->set_state_simple(pctldev, dev);
201 }
202 
pinctrl_select_state(struct udevice * dev,const char * statename)203 int pinctrl_select_state(struct udevice *dev, const char *statename)
204 {
205 	/*
206 	 * Try full-implemented pinctrl first.
207 	 * If it fails or is not implemented, try simple one.
208 	 */
209 	if (pinctrl_select_state_full(dev, statename))
210 		return pinctrl_select_state_simple(dev);
211 
212 	return 0;
213 }
214 
pinctrl_request(struct udevice * dev,int func,int flags)215 int pinctrl_request(struct udevice *dev, int func, int flags)
216 {
217 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
218 
219 	if (!ops->request)
220 		return -ENOSYS;
221 
222 	return ops->request(dev, func, flags);
223 }
224 
pinctrl_request_noflags(struct udevice * dev,int func)225 int pinctrl_request_noflags(struct udevice *dev, int func)
226 {
227 	return pinctrl_request(dev, func, 0);
228 }
229 
pinctrl_get_periph_id(struct udevice * dev,struct udevice * periph)230 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
231 {
232 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
233 
234 	if (!ops->get_periph_id)
235 		return -ENOSYS;
236 
237 	return ops->get_periph_id(dev, periph);
238 }
239 
pinctrl_get_gpio_mux(struct udevice * dev,int banknum,int index)240 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
241 {
242 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
243 
244 	if (!ops->get_gpio_mux)
245 		return -ENOSYS;
246 
247 	return ops->get_gpio_mux(dev, banknum, index);
248 }
249 
pinctrl_get_pins_count(struct udevice * dev)250 int pinctrl_get_pins_count(struct udevice *dev)
251 {
252 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
253 
254 	if (!ops->get_pins_count)
255 		return -ENOSYS;
256 
257 	return ops->get_pins_count(dev);
258 }
259 
260 /**
261  * pinconfig_post_bind() - post binding for PINCTRL uclass
262  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
263  *
264  * @dev: pinctrl device
265  * @return: 0 on success, or negative error code on failure
266  */
pinctrl_post_bind(struct udevice * dev)267 static int pinctrl_post_bind(struct udevice *dev)
268 {
269 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
270 
271 	if (!ops) {
272 		dev_dbg(dev, "ops is not set.  Do not bind.\n");
273 		return -EINVAL;
274 	}
275 
276 	/*
277 	 * If set_state callback is set, we assume this pinctrl driver is the
278 	 * full implementation.  In this case, its child nodes should be bound
279 	 * so that peripheral devices can easily search in parent devices
280 	 * during later DT-parsing.
281 	 */
282 	if (ops->set_state)
283 		return pinconfig_post_bind(dev);
284 
285 	return 0;
286 }
287 
288 UCLASS_DRIVER(pinctrl) = {
289 	.id = UCLASS_PINCTRL,
290 	.post_bind = pinctrl_post_bind,
291 	.flags = DM_UC_FLAG_SEQ_ALIAS,
292 	.name = "pinctrl",
293 };
294