0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/err.h>
0009 #include <linux/gpio/driver.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/io.h>
0012 #include <linux/irq.h>
0013 #include <linux/of.h>
0014 #include <linux/of_irq.h>
0015 #include <linux/pinctrl/consumer.h>
0016 #include <linux/pinctrl/machine.h>
0017 #include <linux/pinctrl/pinconf.h>
0018 #include <linux/pinctrl/pinconf-generic.h>
0019 #include <linux/pinctrl/pinctrl.h>
0020 #include <linux/pinctrl/pinmux.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/slab.h>
0023
0024 #include "pinctrl-wmt.h"
0025
0026 static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
0027 u32 mask)
0028 {
0029 u32 val;
0030
0031 val = readl_relaxed(data->base + reg);
0032 val |= mask;
0033 writel_relaxed(val, data->base + reg);
0034 }
0035
0036 static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
0037 u32 mask)
0038 {
0039 u32 val;
0040
0041 val = readl_relaxed(data->base + reg);
0042 val &= ~mask;
0043 writel_relaxed(val, data->base + reg);
0044 }
0045
0046 enum wmt_func_sel {
0047 WMT_FSEL_GPIO_IN = 0,
0048 WMT_FSEL_GPIO_OUT = 1,
0049 WMT_FSEL_ALT = 2,
0050 WMT_FSEL_COUNT = 3,
0051 };
0052
0053 static const char * const wmt_functions[WMT_FSEL_COUNT] = {
0054 [WMT_FSEL_GPIO_IN] = "gpio_in",
0055 [WMT_FSEL_GPIO_OUT] = "gpio_out",
0056 [WMT_FSEL_ALT] = "alt",
0057 };
0058
0059 static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
0060 {
0061 return WMT_FSEL_COUNT;
0062 }
0063
0064 static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
0065 unsigned selector)
0066 {
0067 return wmt_functions[selector];
0068 }
0069
0070 static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
0071 unsigned selector,
0072 const char * const **groups,
0073 unsigned * const num_groups)
0074 {
0075 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0076
0077
0078 *groups = data->groups;
0079 *num_groups = data->ngroups;
0080
0081 return 0;
0082 }
0083
0084 static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
0085 unsigned pin)
0086 {
0087 u32 bank = WMT_BANK_FROM_PIN(pin);
0088 u32 bit = WMT_BIT_FROM_PIN(pin);
0089 u32 reg_en = data->banks[bank].reg_en;
0090 u32 reg_dir = data->banks[bank].reg_dir;
0091
0092 if (reg_dir == NO_REG) {
0093 dev_err(data->dev, "pin:%d no direction register defined\n",
0094 pin);
0095 return -EINVAL;
0096 }
0097
0098
0099
0100
0101
0102 switch (func) {
0103 case WMT_FSEL_GPIO_IN:
0104 if (reg_en != NO_REG)
0105 wmt_setbits(data, reg_en, BIT(bit));
0106 wmt_clearbits(data, reg_dir, BIT(bit));
0107 break;
0108 case WMT_FSEL_GPIO_OUT:
0109 if (reg_en != NO_REG)
0110 wmt_setbits(data, reg_en, BIT(bit));
0111 wmt_setbits(data, reg_dir, BIT(bit));
0112 break;
0113 case WMT_FSEL_ALT:
0114 if (reg_en == NO_REG) {
0115 dev_err(data->dev, "pin:%d no alt function available\n",
0116 pin);
0117 return -EINVAL;
0118 }
0119 wmt_clearbits(data, reg_en, BIT(bit));
0120 }
0121
0122 return 0;
0123 }
0124
0125 static int wmt_pmx_set_mux(struct pinctrl_dev *pctldev,
0126 unsigned func_selector,
0127 unsigned group_selector)
0128 {
0129 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0130 u32 pinnum = data->pins[group_selector].number;
0131
0132 return wmt_set_pinmux(data, func_selector, pinnum);
0133 }
0134
0135 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
0136 struct pinctrl_gpio_range *range,
0137 unsigned offset)
0138 {
0139 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0140
0141
0142 wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
0143 }
0144
0145 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
0146 struct pinctrl_gpio_range *range,
0147 unsigned offset,
0148 bool input)
0149 {
0150 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0151
0152 wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
0153 offset);
0154
0155 return 0;
0156 }
0157
0158 static const struct pinmux_ops wmt_pinmux_ops = {
0159 .get_functions_count = wmt_pmx_get_functions_count,
0160 .get_function_name = wmt_pmx_get_function_name,
0161 .get_function_groups = wmt_pmx_get_function_groups,
0162 .set_mux = wmt_pmx_set_mux,
0163 .gpio_disable_free = wmt_pmx_gpio_disable_free,
0164 .gpio_set_direction = wmt_pmx_gpio_set_direction,
0165 };
0166
0167 static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
0168 {
0169 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0170
0171 return data->ngroups;
0172 }
0173
0174 static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
0175 unsigned selector)
0176 {
0177 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0178
0179 return data->groups[selector];
0180 }
0181
0182 static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
0183 unsigned selector,
0184 const unsigned **pins,
0185 unsigned *num_pins)
0186 {
0187 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0188
0189 *pins = &data->pins[selector].number;
0190 *num_pins = 1;
0191
0192 return 0;
0193 }
0194
0195 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
0196 {
0197 int i;
0198
0199 for (i = 0; i < data->npins; i++) {
0200 if (data->pins[i].number == pin)
0201 return i;
0202 }
0203
0204 return -EINVAL;
0205 }
0206
0207 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
0208 struct device_node *np,
0209 u32 pin, u32 fnum,
0210 struct pinctrl_map **maps)
0211 {
0212 int group;
0213 struct pinctrl_map *map = *maps;
0214
0215 if (fnum >= ARRAY_SIZE(wmt_functions)) {
0216 dev_err(data->dev, "invalid wm,function %d\n", fnum);
0217 return -EINVAL;
0218 }
0219
0220 group = wmt_pctl_find_group_by_pin(data, pin);
0221 if (group < 0) {
0222 dev_err(data->dev, "unable to match pin %d to group\n", pin);
0223 return group;
0224 }
0225
0226 map->type = PIN_MAP_TYPE_MUX_GROUP;
0227 map->data.mux.group = data->groups[group];
0228 map->data.mux.function = wmt_functions[fnum];
0229 (*maps)++;
0230
0231 return 0;
0232 }
0233
0234 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
0235 struct device_node *np,
0236 u32 pin, u32 pull,
0237 struct pinctrl_map **maps)
0238 {
0239 int group;
0240 unsigned long *configs;
0241 struct pinctrl_map *map = *maps;
0242
0243 if (pull > 2) {
0244 dev_err(data->dev, "invalid wm,pull %d\n", pull);
0245 return -EINVAL;
0246 }
0247
0248 group = wmt_pctl_find_group_by_pin(data, pin);
0249 if (group < 0) {
0250 dev_err(data->dev, "unable to match pin %d to group\n", pin);
0251 return group;
0252 }
0253
0254 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
0255 if (!configs)
0256 return -ENOMEM;
0257
0258 switch (pull) {
0259 case 0:
0260 configs[0] = PIN_CONFIG_BIAS_DISABLE;
0261 break;
0262 case 1:
0263 configs[0] = PIN_CONFIG_BIAS_PULL_DOWN;
0264 break;
0265 case 2:
0266 configs[0] = PIN_CONFIG_BIAS_PULL_UP;
0267 break;
0268 default:
0269 configs[0] = PIN_CONFIG_BIAS_DISABLE;
0270 dev_err(data->dev, "invalid pull state %d - disabling\n", pull);
0271 }
0272
0273 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
0274 map->data.configs.group_or_pin = data->groups[group];
0275 map->data.configs.configs = configs;
0276 map->data.configs.num_configs = 1;
0277 (*maps)++;
0278
0279 return 0;
0280 }
0281
0282 static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
0283 struct pinctrl_map *maps,
0284 unsigned num_maps)
0285 {
0286 int i;
0287
0288 for (i = 0; i < num_maps; i++)
0289 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
0290 kfree(maps[i].data.configs.configs);
0291
0292 kfree(maps);
0293 }
0294
0295 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
0296 struct device_node *np,
0297 struct pinctrl_map **map,
0298 unsigned *num_maps)
0299 {
0300 struct pinctrl_map *maps, *cur_map;
0301 struct property *pins, *funcs, *pulls;
0302 u32 pin, func, pull;
0303 int num_pins, num_funcs, num_pulls, maps_per_pin;
0304 int i, err;
0305 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0306
0307 pins = of_find_property(np, "wm,pins", NULL);
0308 if (!pins) {
0309 dev_err(data->dev, "missing wmt,pins property\n");
0310 return -EINVAL;
0311 }
0312
0313 funcs = of_find_property(np, "wm,function", NULL);
0314 pulls = of_find_property(np, "wm,pull", NULL);
0315
0316 if (!funcs && !pulls) {
0317 dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
0318 return -EINVAL;
0319 }
0320
0321
0322
0323
0324
0325 num_pins = pins->length / sizeof(u32);
0326 num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
0327 num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
0328
0329 if (num_funcs > 1 && num_funcs != num_pins) {
0330 dev_err(data->dev, "wm,function must have 1 or %d entries\n",
0331 num_pins);
0332 return -EINVAL;
0333 }
0334
0335 if (num_pulls > 1 && num_pulls != num_pins) {
0336 dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
0337 num_pins);
0338 return -EINVAL;
0339 }
0340
0341 maps_per_pin = 0;
0342 if (num_funcs)
0343 maps_per_pin++;
0344 if (num_pulls)
0345 maps_per_pin++;
0346
0347 cur_map = maps = kcalloc(num_pins * maps_per_pin, sizeof(*maps),
0348 GFP_KERNEL);
0349 if (!maps)
0350 return -ENOMEM;
0351
0352 for (i = 0; i < num_pins; i++) {
0353 err = of_property_read_u32_index(np, "wm,pins", i, &pin);
0354 if (err)
0355 goto fail;
0356
0357 if (pin >= (data->nbanks * 32)) {
0358 dev_err(data->dev, "invalid wm,pins value\n");
0359 err = -EINVAL;
0360 goto fail;
0361 }
0362
0363 if (num_funcs) {
0364 err = of_property_read_u32_index(np, "wm,function",
0365 (num_funcs > 1 ? i : 0), &func);
0366 if (err)
0367 goto fail;
0368
0369 err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
0370 &cur_map);
0371 if (err)
0372 goto fail;
0373 }
0374
0375 if (num_pulls) {
0376 err = of_property_read_u32_index(np, "wm,pull",
0377 (num_pulls > 1 ? i : 0), &pull);
0378 if (err)
0379 goto fail;
0380
0381 err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
0382 &cur_map);
0383 if (err)
0384 goto fail;
0385 }
0386 }
0387 *map = maps;
0388 *num_maps = num_pins * maps_per_pin;
0389 return 0;
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399 fail:
0400 wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
0401 return err;
0402 }
0403
0404 static const struct pinctrl_ops wmt_pctl_ops = {
0405 .get_groups_count = wmt_get_groups_count,
0406 .get_group_name = wmt_get_group_name,
0407 .get_group_pins = wmt_get_group_pins,
0408 .dt_node_to_map = wmt_pctl_dt_node_to_map,
0409 .dt_free_map = wmt_pctl_dt_free_map,
0410 };
0411
0412 static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
0413 unsigned long *config)
0414 {
0415 return -ENOTSUPP;
0416 }
0417
0418 static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
0419 unsigned long *configs, unsigned num_configs)
0420 {
0421 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
0422 enum pin_config_param param;
0423 u32 arg;
0424 u32 bank = WMT_BANK_FROM_PIN(pin);
0425 u32 bit = WMT_BIT_FROM_PIN(pin);
0426 u32 reg_pull_en = data->banks[bank].reg_pull_en;
0427 u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
0428 int i;
0429
0430 if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
0431 dev_err(data->dev, "bias functions not supported on pin %d\n",
0432 pin);
0433 return -EINVAL;
0434 }
0435
0436 for (i = 0; i < num_configs; i++) {
0437 param = pinconf_to_config_param(configs[i]);
0438 arg = pinconf_to_config_argument(configs[i]);
0439
0440 if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
0441 (param == PIN_CONFIG_BIAS_PULL_UP)) {
0442 if (arg == 0)
0443 param = PIN_CONFIG_BIAS_DISABLE;
0444 }
0445
0446 switch (param) {
0447 case PIN_CONFIG_BIAS_DISABLE:
0448 wmt_clearbits(data, reg_pull_en, BIT(bit));
0449 break;
0450 case PIN_CONFIG_BIAS_PULL_DOWN:
0451 wmt_clearbits(data, reg_pull_cfg, BIT(bit));
0452 wmt_setbits(data, reg_pull_en, BIT(bit));
0453 break;
0454 case PIN_CONFIG_BIAS_PULL_UP:
0455 wmt_setbits(data, reg_pull_cfg, BIT(bit));
0456 wmt_setbits(data, reg_pull_en, BIT(bit));
0457 break;
0458 default:
0459 dev_err(data->dev, "unknown pinconf param\n");
0460 return -EINVAL;
0461 }
0462 }
0463
0464 return 0;
0465 }
0466
0467 static const struct pinconf_ops wmt_pinconf_ops = {
0468 .pin_config_get = wmt_pinconf_get,
0469 .pin_config_set = wmt_pinconf_set,
0470 };
0471
0472 static struct pinctrl_desc wmt_desc = {
0473 .owner = THIS_MODULE,
0474 .name = "pinctrl-wmt",
0475 .pctlops = &wmt_pctl_ops,
0476 .pmxops = &wmt_pinmux_ops,
0477 .confops = &wmt_pinconf_ops,
0478 };
0479
0480 static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
0481 {
0482 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
0483 u32 bank = WMT_BANK_FROM_PIN(offset);
0484 u32 bit = WMT_BIT_FROM_PIN(offset);
0485 u32 reg_dir = data->banks[bank].reg_dir;
0486 u32 val;
0487
0488 val = readl_relaxed(data->base + reg_dir);
0489 if (val & BIT(bit))
0490 return GPIO_LINE_DIRECTION_OUT;
0491
0492 return GPIO_LINE_DIRECTION_IN;
0493 }
0494
0495 static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
0496 {
0497 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
0498 u32 bank = WMT_BANK_FROM_PIN(offset);
0499 u32 bit = WMT_BIT_FROM_PIN(offset);
0500 u32 reg_data_in = data->banks[bank].reg_data_in;
0501
0502 if (reg_data_in == NO_REG) {
0503 dev_err(data->dev, "no data in register defined\n");
0504 return -EINVAL;
0505 }
0506
0507 return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
0508 }
0509
0510 static void wmt_gpio_set_value(struct gpio_chip *chip, unsigned offset,
0511 int val)
0512 {
0513 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
0514 u32 bank = WMT_BANK_FROM_PIN(offset);
0515 u32 bit = WMT_BIT_FROM_PIN(offset);
0516 u32 reg_data_out = data->banks[bank].reg_data_out;
0517
0518 if (reg_data_out == NO_REG) {
0519 dev_err(data->dev, "no data out register defined\n");
0520 return;
0521 }
0522
0523 if (val)
0524 wmt_setbits(data, reg_data_out, BIT(bit));
0525 else
0526 wmt_clearbits(data, reg_data_out, BIT(bit));
0527 }
0528
0529 static int wmt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
0530 {
0531 return pinctrl_gpio_direction_input(chip->base + offset);
0532 }
0533
0534 static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
0535 int value)
0536 {
0537 wmt_gpio_set_value(chip, offset, value);
0538 return pinctrl_gpio_direction_output(chip->base + offset);
0539 }
0540
0541 static const struct gpio_chip wmt_gpio_chip = {
0542 .label = "gpio-wmt",
0543 .owner = THIS_MODULE,
0544 .request = gpiochip_generic_request,
0545 .free = gpiochip_generic_free,
0546 .get_direction = wmt_gpio_get_direction,
0547 .direction_input = wmt_gpio_direction_input,
0548 .direction_output = wmt_gpio_direction_output,
0549 .get = wmt_gpio_get_value,
0550 .set = wmt_gpio_set_value,
0551 .can_sleep = false,
0552 };
0553
0554 int wmt_pinctrl_probe(struct platform_device *pdev,
0555 struct wmt_pinctrl_data *data)
0556 {
0557 int err;
0558
0559 data->base = devm_platform_ioremap_resource(pdev, 0);
0560 if (IS_ERR(data->base))
0561 return PTR_ERR(data->base);
0562
0563 wmt_desc.pins = data->pins;
0564 wmt_desc.npins = data->npins;
0565
0566 data->gpio_chip = wmt_gpio_chip;
0567 data->gpio_chip.parent = &pdev->dev;
0568 data->gpio_chip.ngpio = data->nbanks * 32;
0569
0570 platform_set_drvdata(pdev, data);
0571
0572 data->dev = &pdev->dev;
0573
0574 data->pctl_dev = devm_pinctrl_register(&pdev->dev, &wmt_desc, data);
0575 if (IS_ERR(data->pctl_dev)) {
0576 dev_err(&pdev->dev, "Failed to register pinctrl\n");
0577 return PTR_ERR(data->pctl_dev);
0578 }
0579
0580 err = gpiochip_add_data(&data->gpio_chip, data);
0581 if (err) {
0582 dev_err(&pdev->dev, "could not add GPIO chip\n");
0583 return err;
0584 }
0585
0586 err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
0587 0, 0, data->nbanks * 32);
0588 if (err)
0589 goto fail_range;
0590
0591 dev_info(&pdev->dev, "Pin controller initialized\n");
0592
0593 return 0;
0594
0595 fail_range:
0596 gpiochip_remove(&data->gpio_chip);
0597 return err;
0598 }