1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 - Beniamino Galvani <b.galvani@gmail.com>
9 #include <dm/device-internal.h>
10 #include <dm/device_compat.h>
12 #include <dm/pinctrl.h>
13 #include <fdt_support.h>
14 #include <linux/err.h>
16 #include <linux/libfdt.h>
17 #include <linux/sizes.h>
20 #include "pinctrl-meson.h"
22 DECLARE_GLOBAL_DATA_PTR;
24 static const char *meson_pinctrl_dummy_name = "_dummy";
26 static char pin_name[PINNAME_SIZE];
28 int meson_pinctrl_get_groups_count(struct udevice *dev)
30 struct meson_pinctrl *priv = dev_get_priv(dev);
32 return priv->data->num_groups;
35 const char *meson_pinctrl_get_group_name(struct udevice *dev,
36 unsigned int selector)
38 struct meson_pinctrl *priv = dev_get_priv(dev);
40 if (!priv->data->groups[selector].name)
41 return meson_pinctrl_dummy_name;
43 return priv->data->groups[selector].name;
46 int meson_pinctrl_get_pins_count(struct udevice *dev)
48 struct meson_pinctrl *priv = dev_get_priv(dev);
50 return priv->data->num_pins;
53 const char *meson_pinctrl_get_pin_name(struct udevice *dev,
54 unsigned int selector)
56 struct meson_pinctrl *priv = dev_get_priv(dev);
58 if (selector > priv->data->num_pins ||
59 selector > priv->data->funcs[0].num_groups)
60 snprintf(pin_name, PINNAME_SIZE, "Error");
62 snprintf(pin_name, PINNAME_SIZE, "%s",
63 priv->data->funcs[0].groups[selector]);
68 int meson_pinmux_get_functions_count(struct udevice *dev)
70 struct meson_pinctrl *priv = dev_get_priv(dev);
72 return priv->data->num_funcs;
75 const char *meson_pinmux_get_function_name(struct udevice *dev,
76 unsigned int selector)
78 struct meson_pinctrl *priv = dev_get_priv(dev);
80 return priv->data->funcs[selector].name;
83 static int meson_gpio_calc_reg_and_bit(struct udevice *dev, unsigned int offset,
84 enum meson_reg_type reg_type,
85 unsigned int *reg, unsigned int *bit)
87 struct meson_pinctrl *priv = dev_get_priv(dev);
88 struct meson_bank *bank = NULL;
89 struct meson_reg_desc *desc;
93 pin = priv->data->pin_base + offset;
95 for (i = 0; i < priv->data->num_banks; i++) {
96 if (pin >= priv->data->banks[i].first &&
97 pin <= priv->data->banks[i].last) {
98 bank = &priv->data->banks[i];
106 desc = &bank->regs[reg_type];
107 *reg = desc->reg * 4;
108 *bit = desc->bit + pin - bank->first;
113 int meson_gpio_get(struct udevice *dev, unsigned int offset)
115 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
116 unsigned int reg, bit;
119 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_IN, ®,
124 return !!(readl(priv->reg_gpio + reg) & BIT(bit));
127 int meson_gpio_set(struct udevice *dev, unsigned int offset, int value)
129 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
130 unsigned int reg, bit;
133 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_OUT, ®,
138 clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0);
143 int meson_gpio_get_direction(struct udevice *dev, unsigned int offset)
145 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
146 unsigned int reg, bit, val;
149 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_DIR, ®,
154 val = readl(priv->reg_gpio + reg);
156 return (val & BIT(bit)) ? GPIOF_INPUT : GPIOF_OUTPUT;
159 int meson_gpio_direction_input(struct udevice *dev, unsigned int offset)
161 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
162 unsigned int reg, bit;
165 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_DIR, ®,
170 setbits_le32(priv->reg_gpio + reg, BIT(bit));
175 int meson_gpio_direction_output(struct udevice *dev,
176 unsigned int offset, int value)
178 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
179 unsigned int reg, bit;
182 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_DIR, ®,
187 clrbits_le32(priv->reg_gpio + reg, BIT(bit));
189 ret = meson_gpio_calc_reg_and_bit(dev->parent, offset, REG_OUT, ®,
194 clrsetbits_le32(priv->reg_gpio + reg, BIT(bit), value ? BIT(bit) : 0);
199 static int meson_pinconf_bias_set(struct udevice *dev, unsigned int pin,
202 struct meson_pinctrl *priv = dev_get_priv(dev);
203 unsigned int offset = pin - priv->data->pin_base;
204 unsigned int reg, bit;
207 ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_PULLEN, ®, &bit);
211 if (param == PIN_CONFIG_BIAS_DISABLE) {
212 clrsetbits_le32(priv->reg_pullen + reg, BIT(bit), 0);
216 /* othewise, enable the bias and select level */
217 clrsetbits_le32(priv->reg_pullen + reg, BIT(bit), 1);
218 ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_PULL, ®, &bit);
222 clrsetbits_le32(priv->reg_pull + reg, BIT(bit),
223 param == PIN_CONFIG_BIAS_PULL_UP);
228 static int meson_pinconf_drive_strength_set(struct udevice *dev,
230 unsigned int drive_strength_ua)
232 struct meson_pinctrl *priv = dev_get_priv(dev);
233 unsigned int offset = pin - priv->data->pin_base;
234 unsigned int reg, bit;
239 dev_err(dev, "drive-strength-microamp not supported\n");
243 ret = meson_gpio_calc_reg_and_bit(dev, offset, REG_DS, ®, &bit);
249 if (drive_strength_ua <= 500) {
250 ds_val = MESON_PINCONF_DRV_500UA;
251 } else if (drive_strength_ua <= 2500) {
252 ds_val = MESON_PINCONF_DRV_2500UA;
253 } else if (drive_strength_ua <= 3000) {
254 ds_val = MESON_PINCONF_DRV_3000UA;
255 } else if (drive_strength_ua <= 4000) {
256 ds_val = MESON_PINCONF_DRV_4000UA;
259 "pin %u: invalid drive-strength-microamp : %d , default to 4mA\n",
260 pin, drive_strength_ua);
261 ds_val = MESON_PINCONF_DRV_4000UA;
264 clrsetbits_le32(priv->reg_ds + reg, 0x3 << bit, ds_val << bit);
269 int meson_pinconf_set(struct udevice *dev, unsigned int pin,
270 unsigned int param, unsigned int arg)
275 case PIN_CONFIG_BIAS_DISABLE:
276 case PIN_CONFIG_BIAS_PULL_UP:
277 case PIN_CONFIG_BIAS_PULL_DOWN:
278 ret = meson_pinconf_bias_set(dev, pin, param);
280 case PIN_CONFIG_DRIVE_STRENGTH_UA:
281 ret = meson_pinconf_drive_strength_set(dev, pin, arg);
284 dev_err(dev, "unsupported configuration parameter %u\n", param);
291 int meson_pinconf_group_set(struct udevice *dev,
292 unsigned int group_selector,
293 unsigned int param, unsigned int arg)
295 struct meson_pinctrl *priv = dev_get_priv(dev);
296 struct meson_pmx_group *grp = &priv->data->groups[group_selector];
299 for (i = 0; i < grp->num_pins; i++) {
300 ret = meson_pinconf_set(dev, grp->pins[i], param, arg);
308 int meson_gpio_probe(struct udevice *dev)
310 struct meson_pinctrl *priv = dev_get_priv(dev->parent);
311 struct gpio_dev_priv *uc_priv;
313 uc_priv = dev_get_uclass_priv(dev);
314 uc_priv->bank_name = priv->data->name;
315 uc_priv->gpio_count = priv->data->num_pins;
320 static fdt_addr_t parse_address(int offset, const char *name, int na, int ns)
325 index = fdt_stringlist_search(gd->fdt_blob, offset, "reg-names", name);
327 return FDT_ADDR_T_NONE;
329 reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
330 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns))))
331 return FDT_ADDR_T_NONE;
333 reg += index * (na + ns);
335 return fdt_translate_address((void *)gd->fdt_blob, offset, reg);
338 int meson_pinctrl_probe(struct udevice *dev)
340 struct meson_pinctrl *priv = dev_get_priv(dev);
341 struct uclass_driver *drv;
342 struct udevice *gpio_dev;
344 int node, gpio = -1, len;
348 na = fdt_address_cells(gd->fdt_blob, dev_of_offset(dev->parent));
350 debug("bad #address-cells\n");
354 ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
356 debug("bad #size-cells\n");
360 fdt_for_each_subnode(node, gd->fdt_blob, dev_of_offset(dev)) {
361 if (fdt_getprop(gd->fdt_blob, node, "gpio-controller", &len)) {
368 debug("gpio node not found\n");
372 addr = parse_address(gpio, "mux", na, ns);
373 if (addr == FDT_ADDR_T_NONE) {
374 debug("mux address not found\n");
377 priv->reg_mux = (void __iomem *)addr;
379 addr = parse_address(gpio, "gpio", na, ns);
380 if (addr == FDT_ADDR_T_NONE) {
381 debug("gpio address not found\n");
384 priv->reg_gpio = (void __iomem *)addr;
386 addr = parse_address(gpio, "pull", na, ns);
387 /* Use gpio region if pull one is not present */
388 if (addr == FDT_ADDR_T_NONE)
389 priv->reg_pull = priv->reg_gpio;
391 priv->reg_pull = (void __iomem *)addr;
393 addr = parse_address(gpio, "pull-enable", na, ns);
394 /* Use pull region if pull-enable one is not present */
395 if (addr == FDT_ADDR_T_NONE)
396 priv->reg_pullen = priv->reg_pull;
398 priv->reg_pullen = (void __iomem *)addr;
400 addr = parse_address(gpio, "ds", na, ns);
401 /* Drive strength region is optional */
402 if (addr == FDT_ADDR_T_NONE)
405 priv->reg_ds = (void __iomem *)addr;
407 priv->data = (struct meson_pinctrl_data *)dev_get_driver_data(dev);
409 /* Lookup GPIO driver */
410 drv = lists_uclass_lookup(UCLASS_GPIO);
412 puts("Cannot find GPIO driver\n");
416 name = calloc(1, 32);
417 sprintf(name, "meson-gpio");
419 /* Create child device UCLASS_GPIO and bind it */
420 device_bind(dev, priv->data->gpio_driver, name, NULL, gpio, &gpio_dev);
421 dev_set_of_offset(gpio_dev, gpio);