2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/list.h>
11 #include <dm/device.h>
13 #include <dm/pinctrl.h>
14 #include <dm/uclass.h>
16 DECLARE_GLOBAL_DATA_PTR
;
18 int pinctrl_decode_pin_config(const void *blob
, int node
)
22 if (fdtdec_get_bool(blob
, node
, "bias-pull-up"))
23 flags
|= 1 << PIN_CONFIG_BIAS_PULL_UP
;
24 else if (fdtdec_get_bool(blob
, node
, "bias-pull-down"))
25 flags
|= 1 << PIN_CONFIG_BIAS_PULL_DOWN
;
30 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
32 * pinctrl_config_one() - apply pinctrl settings for a single node
34 * @config: pin configuration node
35 * @return: 0 on success, or negative error code on failure
37 static int pinctrl_config_one(struct udevice
*config
)
39 struct udevice
*pctldev
;
40 const struct pinctrl_ops
*ops
;
44 pctldev
= dev_get_parent(pctldev
);
46 dev_err(config
, "could not find pctldev\n");
49 if (pctldev
->uclass
->uc_drv
->id
== UCLASS_PINCTRL
)
53 ops
= pinctrl_get_ops(pctldev
);
54 return ops
->set_state(pctldev
, config
);
58 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60 * @dev: peripheral device
61 * @statename: state name, like "default"
62 * @return: 0 on success, or negative error code on failure
64 static int pinctrl_select_state_full(struct udevice
*dev
, const char *statename
)
66 const void *fdt
= gd
->fdt_blob
;
67 int node
= dev
->of_offset
;
68 char propname
[32]; /* long enough */
72 struct udevice
*config
;
73 int state
, size
, i
, ret
;
75 state
= fdt_find_string(fdt
, node
, "pinctrl-names", statename
);
79 * If statename is not found in "pinctrl-names",
80 * assume statename is just the integer state ID.
82 state
= simple_strtoul(statename
, &end
, 10);
87 snprintf(propname
, sizeof(propname
), "pinctrl-%d", state
);
88 list
= fdt_getprop(fdt
, node
, propname
, &size
);
92 size
/= sizeof(*list
);
93 for (i
= 0; i
< size
; i
++) {
94 phandle
= fdt32_to_cpu(*list
++);
96 config_node
= fdt_node_offset_by_phandle(fdt
, phandle
);
97 if (config_node
< 0) {
98 dev_err(dev
, "prop %s index %d invalid phandle\n",
102 ret
= uclass_get_device_by_of_offset(UCLASS_PINCONFIG
,
103 config_node
, &config
);
107 ret
= pinctrl_config_one(config
);
116 * pinconfig_post-bind() - post binding for PINCONFIG uclass
117 * Recursively bind its children as pinconfig devices.
119 * @dev: pinconfig device
120 * @return: 0 on success, or negative error code on failure
122 static int pinconfig_post_bind(struct udevice
*dev
)
124 const void *fdt
= gd
->fdt_blob
;
125 int offset
= dev
->of_offset
;
126 bool pre_reloc_only
= !(gd
->flags
& GD_FLG_RELOC
);
130 for (offset
= fdt_first_subnode(fdt
, offset
);
132 offset
= fdt_next_subnode(fdt
, offset
)) {
133 if (pre_reloc_only
&&
134 !fdt_getprop(fdt
, offset
, "u-boot,dm-pre-reloc", NULL
))
137 * If this node has "compatible" property, this is not
138 * a pin configuration node, but a normal device. skip.
140 fdt_get_property(fdt
, offset
, "compatible", &ret
);
144 if (ret
!= -FDT_ERR_NOTFOUND
)
147 name
= fdt_get_name(fdt
, offset
, NULL
);
150 ret
= device_bind_driver_to_node(dev
, "pinconfig", name
,
159 UCLASS_DRIVER(pinconfig
) = {
160 .id
= UCLASS_PINCONFIG
,
161 .post_bind
= pinconfig_post_bind
,
165 U_BOOT_DRIVER(pinconfig_generic
) = {
167 .id
= UCLASS_PINCONFIG
,
171 static int pinctrl_select_state_full(struct udevice
*dev
, const char *statename
)
176 static int pinconfig_post_bind(struct udevice
*dev
)
183 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
185 * @dev: peripheral device
186 * @return: 0 on success, or negative error code on failure
188 static int pinctrl_select_state_simple(struct udevice
*dev
)
190 struct udevice
*pctldev
;
191 struct pinctrl_ops
*ops
;
195 * For simplicity, assume the first device of PINCTRL uclass
196 * is the correct one. This is most likely OK as there is
197 * usually only one pinctrl device on the system.
199 ret
= uclass_get_device(UCLASS_PINCTRL
, 0, &pctldev
);
203 ops
= pinctrl_get_ops(pctldev
);
204 if (!ops
->set_state_simple
) {
205 dev_dbg(dev
, "set_state_simple op missing\n");
209 return ops
->set_state_simple(pctldev
, dev
);
212 int pinctrl_select_state(struct udevice
*dev
, const char *statename
)
215 * Try full-implemented pinctrl first.
216 * If it fails or is not implemented, try simple one.
218 if (pinctrl_select_state_full(dev
, statename
))
219 return pinctrl_select_state_simple(dev
);
224 int pinctrl_request(struct udevice
*dev
, int func
, int flags
)
226 struct pinctrl_ops
*ops
= pinctrl_get_ops(dev
);
231 return ops
->request(dev
, func
, flags
);
234 int pinctrl_request_noflags(struct udevice
*dev
, int func
)
236 return pinctrl_request(dev
, func
, 0);
239 int pinctrl_get_periph_id(struct udevice
*dev
, struct udevice
*periph
)
241 struct pinctrl_ops
*ops
= pinctrl_get_ops(dev
);
243 if (!ops
->get_periph_id
)
246 return ops
->get_periph_id(dev
, periph
);
249 int pinctrl_get_gpio_mux(struct udevice
*dev
, int banknum
, int index
)
251 struct pinctrl_ops
*ops
= pinctrl_get_ops(dev
);
253 if (!ops
->get_gpio_mux
)
256 return ops
->get_gpio_mux(dev
, banknum
, index
);
260 * pinconfig_post-bind() - post binding for PINCTRL uclass
261 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
263 * @dev: pinctrl device
264 * @return: 0 on success, or negative error code on failure
266 static int pinctrl_post_bind(struct udevice
*dev
)
268 const struct pinctrl_ops
*ops
= pinctrl_get_ops(dev
);
271 dev_dbg(dev
, "ops is not set. Do not bind.\n");
276 * If set_state callback is set, we assume this pinctrl driver is the
277 * full implementation. In this case, its child nodes should be bound
278 * so that peripheral devices can easily search in parent devices
279 * during later DT-parsing.
282 return pinconfig_post_bind(dev
);
287 UCLASS_DRIVER(pinctrl
) = {
288 .id
= UCLASS_PINCTRL
,
289 .post_bind
= pinctrl_post_bind
,