1 // SPDX-License-Identifier: GPL-2.0+
8 #include <dm/device_compat.h>
9 #include <dm/pinctrl.h>
10 #include <linux/libfdt.h>
13 DECLARE_GLOBAL_DATA_PTR;
16 fdt_addr_t base; /* first configuration register */
17 int offset; /* index of last configuration register */
18 u32 mask; /* configuration-value mask bits */
19 int width; /* configuration register bit width */
23 struct single_fdt_pin_cfg {
24 fdt32_t reg; /* configuration register offset */
25 fdt32_t val; /* configuration register value */
28 struct single_fdt_bits_cfg {
29 fdt32_t reg; /* configuration register offset */
30 fdt32_t val; /* configuration register value */
31 fdt32_t mask; /* configuration register mask */
35 * single_configure_pins() - Configure pins based on FDT data
37 * @dev: Pointer to single pin configuration device which is the parent of
38 * the pins node holding the pin configuration data.
39 * @pins: Pointer to the first element of an array of register/value pairs
40 * of type 'struct single_fdt_pin_cfg'. Each such pair describes the
41 * the pin to be configured and the value to be used for configuration.
42 * This pointer points to a 'pinctrl-single,pins' property in the
44 * @size: Size of the 'pins' array in bytes.
45 * The number of register/value pairs in the 'pins' array therefore
46 * equals to 'size / sizeof(struct single_fdt_pin_cfg)'.
48 static int single_configure_pins(struct udevice *dev,
49 const struct single_fdt_pin_cfg *pins,
52 struct single_pdata *pdata = dev->platdata;
53 int count = size / sizeof(struct single_fdt_pin_cfg);
57 for (n = 0; n < count; n++, pins++) {
58 reg = fdt32_to_cpu(pins->reg);
59 if ((reg < 0) || (reg > pdata->offset)) {
60 dev_dbg(dev, " invalid register offset 0x%pa\n", ®);
64 val = fdt32_to_cpu(pins->val) & pdata->mask;
65 switch (pdata->width) {
67 writew((readw(reg) & ~pdata->mask) | val, reg);
70 writel((readl(reg) & ~pdata->mask) | val, reg);
73 dev_warn(dev, "unsupported register width %i\n",
77 dev_dbg(dev, " reg/val 0x%pa/0x%08x\n", ®, val);
82 static int single_configure_bits(struct udevice *dev,
83 const struct single_fdt_bits_cfg *pins,
86 struct single_pdata *pdata = dev->platdata;
87 int count = size / sizeof(struct single_fdt_bits_cfg);
91 for (n = 0; n < count; n++, pins++) {
92 reg = fdt32_to_cpu(pins->reg);
93 if ((reg < 0) || (reg > pdata->offset)) {
94 dev_dbg(dev, " invalid register offset 0x%pa\n", ®);
99 mask = fdt32_to_cpu(pins->mask);
100 val = fdt32_to_cpu(pins->val) & mask;
102 switch (pdata->width) {
104 writew((readw(reg) & ~mask) | val, reg);
107 writel((readl(reg) & ~mask) | val, reg);
110 dev_warn(dev, "unsupported register width %i\n",
114 dev_dbg(dev, " reg/val 0x%pa/0x%08x\n", ®, val);
118 static int single_set_state(struct udevice *dev,
119 struct udevice *config)
121 const void *fdt = gd->fdt_blob;
122 const struct single_fdt_pin_cfg *prop;
123 const struct single_fdt_bits_cfg *prop_bits;
126 prop = fdt_getprop(fdt, dev_of_offset(config), "pinctrl-single,pins",
130 dev_dbg(dev, "configuring pins for %s\n", config->name);
131 if (len % sizeof(struct single_fdt_pin_cfg)) {
132 dev_dbg(dev, " invalid pin configuration in fdt\n");
133 return -FDT_ERR_BADSTRUCTURE;
135 single_configure_pins(dev, prop, len);
139 /* pinctrl-single,pins not found so check for pinctrl-single,bits */
140 prop_bits = fdt_getprop(fdt, dev_of_offset(config),
141 "pinctrl-single,bits",
144 dev_dbg(dev, "configuring pins for %s\n", config->name);
145 if (len % sizeof(struct single_fdt_bits_cfg)) {
146 dev_dbg(dev, " invalid bits configuration in fdt\n");
147 return -FDT_ERR_BADSTRUCTURE;
149 single_configure_bits(dev, prop_bits, len);
153 /* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */
157 static int single_ofdata_to_platdata(struct udevice *dev)
162 struct single_pdata *pdata = dev->platdata;
164 pdata->width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
165 "pinctrl-single,register-width", 0);
167 res = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
171 pdata->offset = of_reg[1] - pdata->width / 8;
173 addr = devfdt_get_addr(dev);
174 if (addr == FDT_ADDR_T_NONE) {
175 dev_dbg(dev, "no valid base register address\n");
180 pdata->mask = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
181 "pinctrl-single,function-mask",
183 pdata->bits_per_mux = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
184 "pinctrl-single,bit-per-mux");
189 const struct pinctrl_ops single_pinctrl_ops = {
190 .set_state = single_set_state,
193 static const struct udevice_id single_pinctrl_match[] = {
194 { .compatible = "pinctrl-single" },
198 U_BOOT_DRIVER(single_pinctrl) = {
199 .name = "single-pinctrl",
200 .id = UCLASS_PINCTRL,
201 .of_match = single_pinctrl_match,
202 .ops = &single_pinctrl_ops,
203 .platdata_auto_alloc_size = sizeof(struct single_pdata),
204 .ofdata_to_platdata = single_ofdata_to_platdata,