2 * Pinctrl driver for the Wondermedia SoC's
4 * Copyright (c) 2013 Tony Prisk <linux@prisktech.co.nz>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/err.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/interrupt.h>
20 #include <linux/irq.h>
22 #include <linux/of_irq.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinconf.h>
26 #include <linux/pinctrl/pinconf-generic.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/pinmux.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
32 #include "pinctrl-wmt.h"
34 static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
39 val = readl_relaxed(data->base + reg);
41 writel_relaxed(val, data->base + reg);
44 static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
49 val = readl_relaxed(data->base + reg);
51 writel_relaxed(val, data->base + reg);
56 WMT_FSEL_GPIO_OUT = 1,
61 static const char * const wmt_functions[WMT_FSEL_COUNT] = {
62 [WMT_FSEL_GPIO_IN] = "gpio_in",
63 [WMT_FSEL_GPIO_OUT] = "gpio_out",
64 [WMT_FSEL_ALT] = "alt",
67 static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
69 return WMT_FSEL_COUNT;
72 static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
75 return wmt_functions[selector];
78 static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
80 const char * const **groups,
81 unsigned * const num_groups)
83 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
85 /* every pin does every function */
86 *groups = data->groups;
87 *num_groups = data->ngroups;
92 static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
95 u32 bank = WMT_BANK_FROM_PIN(pin);
96 u32 bit = WMT_BIT_FROM_PIN(pin);
97 u32 reg_en = data->banks[bank].reg_en;
98 u32 reg_dir = data->banks[bank].reg_dir;
100 if (reg_dir == NO_REG) {
101 dev_err(data->dev, "pin:%d no direction register defined\n",
107 * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
108 * disabled (as on VT8500) and that no alternate function is available.
111 case WMT_FSEL_GPIO_IN:
112 if (reg_en != NO_REG)
113 wmt_setbits(data, reg_en, BIT(bit));
114 wmt_clearbits(data, reg_dir, BIT(bit));
116 case WMT_FSEL_GPIO_OUT:
117 if (reg_en != NO_REG)
118 wmt_setbits(data, reg_en, BIT(bit));
119 wmt_setbits(data, reg_dir, BIT(bit));
122 if (reg_en == NO_REG) {
123 dev_err(data->dev, "pin:%d no alt function available\n",
127 wmt_clearbits(data, reg_en, BIT(bit));
133 static int wmt_pmx_set_mux(struct pinctrl_dev *pctldev,
134 unsigned func_selector,
135 unsigned group_selector)
137 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
138 u32 pinnum = data->pins[group_selector].number;
140 return wmt_set_pinmux(data, func_selector, pinnum);
143 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
144 struct pinctrl_gpio_range *range,
147 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
149 /* disable by setting GPIO_IN */
150 wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
153 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
154 struct pinctrl_gpio_range *range,
158 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
160 wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
166 static const struct pinmux_ops wmt_pinmux_ops = {
167 .get_functions_count = wmt_pmx_get_functions_count,
168 .get_function_name = wmt_pmx_get_function_name,
169 .get_function_groups = wmt_pmx_get_function_groups,
170 .set_mux = wmt_pmx_set_mux,
171 .gpio_disable_free = wmt_pmx_gpio_disable_free,
172 .gpio_set_direction = wmt_pmx_gpio_set_direction,
175 static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
177 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
179 return data->ngroups;
182 static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
185 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
187 return data->groups[selector];
190 static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
192 const unsigned **pins,
195 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
197 *pins = &data->pins[selector].number;
203 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
207 for (i = 0; i < data->npins; i++) {
208 if (data->pins[i].number == pin)
215 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
216 struct device_node *np,
218 struct pinctrl_map **maps)
221 struct pinctrl_map *map = *maps;
223 if (fnum >= ARRAY_SIZE(wmt_functions)) {
224 dev_err(data->dev, "invalid wm,function %d\n", fnum);
228 group = wmt_pctl_find_group_by_pin(data, pin);
230 dev_err(data->dev, "unable to match pin %d to group\n", pin);
234 map->type = PIN_MAP_TYPE_MUX_GROUP;
235 map->data.mux.group = data->groups[group];
236 map->data.mux.function = wmt_functions[fnum];
242 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
243 struct device_node *np,
245 struct pinctrl_map **maps)
248 unsigned long *configs;
249 struct pinctrl_map *map = *maps;
252 dev_err(data->dev, "invalid wm,pull %d\n", pull);
256 group = wmt_pctl_find_group_by_pin(data, pin);
258 dev_err(data->dev, "unable to match pin %d to group\n", pin);
262 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
268 configs[0] = PIN_CONFIG_BIAS_DISABLE;
271 configs[0] = PIN_CONFIG_BIAS_PULL_DOWN;
274 configs[0] = PIN_CONFIG_BIAS_PULL_UP;
277 configs[0] = PIN_CONFIG_BIAS_DISABLE;
278 dev_err(data->dev, "invalid pull state %d - disabling\n", pull);
281 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
282 map->data.configs.group_or_pin = data->groups[group];
283 map->data.configs.configs = configs;
284 map->data.configs.num_configs = 1;
290 static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
291 struct pinctrl_map *maps,
296 for (i = 0; i < num_maps; i++)
297 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
298 kfree(maps[i].data.configs.configs);
303 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
304 struct device_node *np,
305 struct pinctrl_map **map,
308 struct pinctrl_map *maps, *cur_map;
309 struct property *pins, *funcs, *pulls;
311 int num_pins, num_funcs, num_pulls, maps_per_pin;
313 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
315 pins = of_find_property(np, "wm,pins", NULL);
317 dev_err(data->dev, "missing wmt,pins property\n");
321 funcs = of_find_property(np, "wm,function", NULL);
322 pulls = of_find_property(np, "wm,pull", NULL);
324 if (!funcs && !pulls) {
325 dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
330 * The following lines calculate how many values are defined for each
333 num_pins = pins->length / sizeof(u32);
334 num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
335 num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
337 if (num_funcs > 1 && num_funcs != num_pins) {
338 dev_err(data->dev, "wm,function must have 1 or %d entries\n",
343 if (num_pulls > 1 && num_pulls != num_pins) {
344 dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
355 cur_map = maps = kcalloc(num_pins * maps_per_pin, sizeof(*maps),
360 for (i = 0; i < num_pins; i++) {
361 err = of_property_read_u32_index(np, "wm,pins", i, &pin);
365 if (pin >= (data->nbanks * 32)) {
366 dev_err(data->dev, "invalid wm,pins value\n");
372 err = of_property_read_u32_index(np, "wm,function",
373 (num_funcs > 1 ? i : 0), &func);
377 err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
384 err = of_property_read_u32_index(np, "wm,pull",
385 (num_pulls > 1 ? i : 0), &pull);
389 err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
396 *num_maps = num_pins * maps_per_pin;
400 * The fail path removes any maps that have been allocated. The fail path is
401 * only called from code after maps has been kzalloc'd. It is also safe to
402 * pass 'num_pins * maps_per_pin' as the map count even though we probably
403 * failed before all the mappings were read as all maps are allocated at once,
404 * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
405 * is no failpath where a config can be allocated without .type being set.
408 wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
412 static const struct pinctrl_ops wmt_pctl_ops = {
413 .get_groups_count = wmt_get_groups_count,
414 .get_group_name = wmt_get_group_name,
415 .get_group_pins = wmt_get_group_pins,
416 .dt_node_to_map = wmt_pctl_dt_node_to_map,
417 .dt_free_map = wmt_pctl_dt_free_map,
420 static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
421 unsigned long *config)
426 static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
427 unsigned long *configs, unsigned num_configs)
429 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
430 enum pin_config_param param;
432 u32 bank = WMT_BANK_FROM_PIN(pin);
433 u32 bit = WMT_BIT_FROM_PIN(pin);
434 u32 reg_pull_en = data->banks[bank].reg_pull_en;
435 u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
438 if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
439 dev_err(data->dev, "bias functions not supported on pin %d\n",
444 for (i = 0; i < num_configs; i++) {
445 param = pinconf_to_config_param(configs[i]);
446 arg = pinconf_to_config_argument(configs[i]);
448 if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
449 (param == PIN_CONFIG_BIAS_PULL_UP)) {
451 param = PIN_CONFIG_BIAS_DISABLE;
455 case PIN_CONFIG_BIAS_DISABLE:
456 wmt_clearbits(data, reg_pull_en, BIT(bit));
458 case PIN_CONFIG_BIAS_PULL_DOWN:
459 wmt_clearbits(data, reg_pull_cfg, BIT(bit));
460 wmt_setbits(data, reg_pull_en, BIT(bit));
462 case PIN_CONFIG_BIAS_PULL_UP:
463 wmt_setbits(data, reg_pull_cfg, BIT(bit));
464 wmt_setbits(data, reg_pull_en, BIT(bit));
467 dev_err(data->dev, "unknown pinconf param\n");
470 } /* for each config */
475 static const struct pinconf_ops wmt_pinconf_ops = {
476 .pin_config_get = wmt_pinconf_get,
477 .pin_config_set = wmt_pinconf_set,
480 static struct pinctrl_desc wmt_desc = {
481 .owner = THIS_MODULE,
482 .name = "pinctrl-wmt",
483 .pctlops = &wmt_pctl_ops,
484 .pmxops = &wmt_pinmux_ops,
485 .confops = &wmt_pinconf_ops,
488 static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
490 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
491 u32 bank = WMT_BANK_FROM_PIN(offset);
492 u32 bit = WMT_BIT_FROM_PIN(offset);
493 u32 reg_dir = data->banks[bank].reg_dir;
496 val = readl_relaxed(data->base + reg_dir);
498 return GPIOF_DIR_OUT;
503 static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
505 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
506 u32 bank = WMT_BANK_FROM_PIN(offset);
507 u32 bit = WMT_BIT_FROM_PIN(offset);
508 u32 reg_data_in = data->banks[bank].reg_data_in;
510 if (reg_data_in == NO_REG) {
511 dev_err(data->dev, "no data in register defined\n");
515 return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
518 static void wmt_gpio_set_value(struct gpio_chip *chip, unsigned offset,
521 struct wmt_pinctrl_data *data = gpiochip_get_data(chip);
522 u32 bank = WMT_BANK_FROM_PIN(offset);
523 u32 bit = WMT_BIT_FROM_PIN(offset);
524 u32 reg_data_out = data->banks[bank].reg_data_out;
526 if (reg_data_out == NO_REG) {
527 dev_err(data->dev, "no data out register defined\n");
532 wmt_setbits(data, reg_data_out, BIT(bit));
534 wmt_clearbits(data, reg_data_out, BIT(bit));
537 static int wmt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
539 return pinctrl_gpio_direction_input(chip->base + offset);
542 static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
545 wmt_gpio_set_value(chip, offset, value);
546 return pinctrl_gpio_direction_output(chip->base + offset);
549 static const struct gpio_chip wmt_gpio_chip = {
551 .owner = THIS_MODULE,
552 .request = gpiochip_generic_request,
553 .free = gpiochip_generic_free,
554 .get_direction = wmt_gpio_get_direction,
555 .direction_input = wmt_gpio_direction_input,
556 .direction_output = wmt_gpio_direction_output,
557 .get = wmt_gpio_get_value,
558 .set = wmt_gpio_set_value,
562 int wmt_pinctrl_probe(struct platform_device *pdev,
563 struct wmt_pinctrl_data *data)
566 struct resource *res;
568 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
569 data->base = devm_ioremap_resource(&pdev->dev, res);
570 if (IS_ERR(data->base))
571 return PTR_ERR(data->base);
573 wmt_desc.pins = data->pins;
574 wmt_desc.npins = data->npins;
576 data->gpio_chip = wmt_gpio_chip;
577 data->gpio_chip.parent = &pdev->dev;
578 data->gpio_chip.of_node = pdev->dev.of_node;
579 data->gpio_chip.ngpio = data->nbanks * 32;
581 platform_set_drvdata(pdev, data);
583 data->dev = &pdev->dev;
585 data->pctl_dev = devm_pinctrl_register(&pdev->dev, &wmt_desc, data);
586 if (IS_ERR(data->pctl_dev)) {
587 dev_err(&pdev->dev, "Failed to register pinctrl\n");
588 return PTR_ERR(data->pctl_dev);
591 err = gpiochip_add_data(&data->gpio_chip, data);
593 dev_err(&pdev->dev, "could not add GPIO chip\n");
597 err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
598 0, 0, data->nbanks * 32);
602 dev_info(&pdev->dev, "Pin controller initialized\n");
607 gpiochip_remove(&data->gpio_chip);