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.h>
18 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
23 #include <linux/of_irq.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/pinctrl/machine.h>
26 #include <linux/pinctrl/pinconf.h>
27 #include <linux/pinctrl/pinconf-generic.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 #include "pinctrl-wmt.h"
35 static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
40 val = readl_relaxed(data->base + reg);
42 writel_relaxed(val, data->base + reg);
45 static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
50 val = readl_relaxed(data->base + reg);
52 writel_relaxed(val, data->base + reg);
57 WMT_FSEL_GPIO_OUT = 1,
62 static const char * const wmt_functions[WMT_FSEL_COUNT] = {
63 [WMT_FSEL_GPIO_IN] = "gpio_in",
64 [WMT_FSEL_GPIO_OUT] = "gpio_out",
65 [WMT_FSEL_ALT] = "alt",
68 static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
70 return WMT_FSEL_COUNT;
73 static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
76 return wmt_functions[selector];
79 static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
81 const char * const **groups,
82 unsigned * const num_groups)
84 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
86 /* every pin does every function */
87 *groups = data->groups;
88 *num_groups = data->ngroups;
93 static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
96 u32 bank = WMT_BANK_FROM_PIN(pin);
97 u32 bit = WMT_BIT_FROM_PIN(pin);
98 u32 reg_en = data->banks[bank].reg_en;
99 u32 reg_dir = data->banks[bank].reg_dir;
101 if (reg_dir == NO_REG) {
102 dev_err(data->dev, "pin:%d no direction register defined\n",
108 * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
109 * disabled (as on VT8500) and that no alternate function is available.
112 case WMT_FSEL_GPIO_IN:
113 if (reg_en != NO_REG)
114 wmt_setbits(data, reg_en, BIT(bit));
115 wmt_clearbits(data, reg_dir, BIT(bit));
117 case WMT_FSEL_GPIO_OUT:
118 if (reg_en != NO_REG)
119 wmt_setbits(data, reg_en, BIT(bit));
120 wmt_setbits(data, reg_dir, BIT(bit));
123 if (reg_en == NO_REG) {
124 dev_err(data->dev, "pin:%d no alt function available\n",
128 wmt_clearbits(data, reg_en, BIT(bit));
134 static int wmt_pmx_enable(struct pinctrl_dev *pctldev,
135 unsigned func_selector,
136 unsigned group_selector)
138 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
139 u32 pinnum = data->pins[group_selector].number;
141 return wmt_set_pinmux(data, func_selector, pinnum);
144 static void wmt_pmx_disable(struct pinctrl_dev *pctldev,
145 unsigned func_selector,
146 unsigned group_selector)
148 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
149 u32 pinnum = data->pins[group_selector].number;
151 /* disable by setting GPIO_IN */
152 wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, pinnum);
155 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
156 struct pinctrl_gpio_range *range,
159 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
161 /* disable by setting GPIO_IN */
162 wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
165 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
166 struct pinctrl_gpio_range *range,
170 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
172 wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
178 static struct pinmux_ops wmt_pinmux_ops = {
179 .get_functions_count = wmt_pmx_get_functions_count,
180 .get_function_name = wmt_pmx_get_function_name,
181 .get_function_groups = wmt_pmx_get_function_groups,
182 .enable = wmt_pmx_enable,
183 .disable = wmt_pmx_disable,
184 .gpio_disable_free = wmt_pmx_gpio_disable_free,
185 .gpio_set_direction = wmt_pmx_gpio_set_direction,
188 static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
190 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
192 return data->ngroups;
195 static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
198 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
200 return data->groups[selector];
203 static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
205 const unsigned **pins,
208 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
210 *pins = &data->pins[selector].number;
216 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
220 for (i = 0; i < data->npins; i++) {
221 if (data->pins[i].number == pin)
228 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
229 struct device_node *np,
231 struct pinctrl_map **maps)
234 struct pinctrl_map *map = *maps;
236 if (fnum >= ARRAY_SIZE(wmt_functions)) {
237 dev_err(data->dev, "invalid wm,function %d\n", fnum);
241 group = wmt_pctl_find_group_by_pin(data, pin);
243 dev_err(data->dev, "unable to match pin %d to group\n", pin);
247 map->type = PIN_MAP_TYPE_MUX_GROUP;
248 map->data.mux.group = data->groups[group];
249 map->data.mux.function = wmt_functions[fnum];
255 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
256 struct device_node *np,
258 struct pinctrl_map **maps)
261 unsigned long *configs;
262 struct pinctrl_map *map = *maps;
265 dev_err(data->dev, "invalid wm,pull %d\n", pull);
269 group = wmt_pctl_find_group_by_pin(data, pin);
271 dev_err(data->dev, "unable to match pin %d to group\n", pin);
275 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
281 configs[0] = PIN_CONFIG_BIAS_DISABLE;
284 configs[0] = PIN_CONFIG_BIAS_PULL_DOWN;
287 configs[0] = PIN_CONFIG_BIAS_PULL_UP;
290 configs[0] = PIN_CONFIG_BIAS_DISABLE;
291 dev_err(data->dev, "invalid pull state %d - disabling\n", pull);
294 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
295 map->data.configs.group_or_pin = data->groups[group];
296 map->data.configs.configs = configs;
297 map->data.configs.num_configs = 1;
303 static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
304 struct pinctrl_map *maps,
309 for (i = 0; i < num_maps; i++)
310 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
311 kfree(maps[i].data.configs.configs);
316 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
317 struct device_node *np,
318 struct pinctrl_map **map,
321 struct pinctrl_map *maps, *cur_map;
322 struct property *pins, *funcs, *pulls;
324 int num_pins, num_funcs, num_pulls, maps_per_pin;
326 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
328 pins = of_find_property(np, "wm,pins", NULL);
330 dev_err(data->dev, "missing wmt,pins property\n");
334 funcs = of_find_property(np, "wm,function", NULL);
335 pulls = of_find_property(np, "wm,pull", NULL);
337 if (!funcs && !pulls) {
338 dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
343 * The following lines calculate how many values are defined for each
346 num_pins = pins->length / sizeof(u32);
347 num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
348 num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
350 if (num_funcs > 1 && num_funcs != num_pins) {
351 dev_err(data->dev, "wm,function must have 1 or %d entries\n",
356 if (num_pulls > 1 && num_pulls != num_pins) {
357 dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
368 cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
373 for (i = 0; i < num_pins; i++) {
374 err = of_property_read_u32_index(np, "wm,pins", i, &pin);
378 if (pin >= (data->nbanks * 32)) {
379 dev_err(data->dev, "invalid wm,pins value\n");
385 err = of_property_read_u32_index(np, "wm,function",
386 (num_funcs > 1 ? i : 0), &func);
390 err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
397 err = of_property_read_u32_index(np, "wm,pull",
398 (num_pulls > 1 ? i : 0), &pull);
402 err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
409 *num_maps = num_pins * maps_per_pin;
413 * The fail path removes any maps that have been allocated. The fail path is
414 * only called from code after maps has been kzalloc'd. It is also safe to
415 * pass 'num_pins * maps_per_pin' as the map count even though we probably
416 * failed before all the mappings were read as all maps are allocated at once,
417 * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
418 * is no failpath where a config can be allocated without .type being set.
421 wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
425 static struct pinctrl_ops wmt_pctl_ops = {
426 .get_groups_count = wmt_get_groups_count,
427 .get_group_name = wmt_get_group_name,
428 .get_group_pins = wmt_get_group_pins,
429 .dt_node_to_map = wmt_pctl_dt_node_to_map,
430 .dt_free_map = wmt_pctl_dt_free_map,
433 static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
434 unsigned long *config)
439 static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
440 unsigned long *configs, unsigned num_configs)
442 struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
443 enum pin_config_param param;
445 u32 bank = WMT_BANK_FROM_PIN(pin);
446 u32 bit = WMT_BIT_FROM_PIN(pin);
447 u32 reg_pull_en = data->banks[bank].reg_pull_en;
448 u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
451 if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
452 dev_err(data->dev, "bias functions not supported on pin %d\n",
457 for (i = 0; i < num_configs; i++) {
458 param = pinconf_to_config_param(configs[i]);
459 arg = pinconf_to_config_argument(configs[i]);
461 if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
462 (param == PIN_CONFIG_BIAS_PULL_UP)) {
464 param = PIN_CONFIG_BIAS_DISABLE;
468 case PIN_CONFIG_BIAS_DISABLE:
469 wmt_clearbits(data, reg_pull_en, BIT(bit));
471 case PIN_CONFIG_BIAS_PULL_DOWN:
472 wmt_clearbits(data, reg_pull_cfg, BIT(bit));
473 wmt_setbits(data, reg_pull_en, BIT(bit));
475 case PIN_CONFIG_BIAS_PULL_UP:
476 wmt_setbits(data, reg_pull_cfg, BIT(bit));
477 wmt_setbits(data, reg_pull_en, BIT(bit));
480 dev_err(data->dev, "unknown pinconf param\n");
483 } /* for each config */
488 static struct pinconf_ops wmt_pinconf_ops = {
489 .pin_config_get = wmt_pinconf_get,
490 .pin_config_set = wmt_pinconf_set,
493 static struct pinctrl_desc wmt_desc = {
494 .owner = THIS_MODULE,
495 .name = "pinctrl-wmt",
496 .pctlops = &wmt_pctl_ops,
497 .pmxops = &wmt_pinmux_ops,
498 .confops = &wmt_pinconf_ops,
501 static int wmt_gpio_request(struct gpio_chip *chip, unsigned offset)
503 return pinctrl_request_gpio(chip->base + offset);
506 static void wmt_gpio_free(struct gpio_chip *chip, unsigned offset)
508 pinctrl_free_gpio(chip->base + offset);
511 static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
513 struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
514 u32 bank = WMT_BANK_FROM_PIN(offset);
515 u32 bit = WMT_BIT_FROM_PIN(offset);
516 u32 reg_dir = data->banks[bank].reg_dir;
519 val = readl_relaxed(data->base + reg_dir);
521 return GPIOF_DIR_OUT;
526 static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
528 struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
529 u32 bank = WMT_BANK_FROM_PIN(offset);
530 u32 bit = WMT_BIT_FROM_PIN(offset);
531 u32 reg_data_in = data->banks[bank].reg_data_in;
533 if (reg_data_in == NO_REG) {
534 dev_err(data->dev, "no data in register defined\n");
538 return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
541 static void wmt_gpio_set_value(struct gpio_chip *chip, unsigned offset,
544 struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
545 u32 bank = WMT_BANK_FROM_PIN(offset);
546 u32 bit = WMT_BIT_FROM_PIN(offset);
547 u32 reg_data_out = data->banks[bank].reg_data_out;
549 if (reg_data_out == NO_REG) {
550 dev_err(data->dev, "no data out register defined\n");
555 wmt_setbits(data, reg_data_out, BIT(bit));
557 wmt_clearbits(data, reg_data_out, BIT(bit));
560 static int wmt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
562 return pinctrl_gpio_direction_input(chip->base + offset);
565 static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
568 wmt_gpio_set_value(chip, offset, value);
569 return pinctrl_gpio_direction_output(chip->base + offset);
572 static struct gpio_chip wmt_gpio_chip = {
574 .owner = THIS_MODULE,
575 .request = wmt_gpio_request,
576 .free = wmt_gpio_free,
577 .get_direction = wmt_gpio_get_direction,
578 .direction_input = wmt_gpio_direction_input,
579 .direction_output = wmt_gpio_direction_output,
580 .get = wmt_gpio_get_value,
581 .set = wmt_gpio_set_value,
585 int wmt_pinctrl_probe(struct platform_device *pdev,
586 struct wmt_pinctrl_data *data)
589 struct resource *res;
591 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
592 data->base = devm_ioremap_resource(&pdev->dev, res);
593 if (IS_ERR(data->base))
594 return PTR_ERR(data->base);
596 wmt_desc.pins = data->pins;
597 wmt_desc.npins = data->npins;
599 data->gpio_chip = wmt_gpio_chip;
600 data->gpio_chip.dev = &pdev->dev;
601 data->gpio_chip.of_node = pdev->dev.of_node;
602 data->gpio_chip.ngpio = data->nbanks * 32;
604 platform_set_drvdata(pdev, data);
606 data->dev = &pdev->dev;
608 data->pctl_dev = pinctrl_register(&wmt_desc, &pdev->dev, data);
609 if (!data->pctl_dev) {
610 dev_err(&pdev->dev, "Failed to register pinctrl\n");
614 err = gpiochip_add(&data->gpio_chip);
616 dev_err(&pdev->dev, "could not add GPIO chip\n");
620 err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
621 0, 0, data->nbanks * 32);
625 dev_info(&pdev->dev, "Pin controller initialized\n");
630 if (gpiochip_remove(&data->gpio_chip))
631 dev_err(&pdev->dev, "failed to remove gpio chip\n");
633 pinctrl_unregister(data->pctl_dev);
637 int wmt_pinctrl_remove(struct platform_device *pdev)
639 struct wmt_pinctrl_data *data = platform_get_drvdata(pdev);
642 err = gpiochip_remove(&data->gpio_chip);
644 dev_err(&pdev->dev, "failed to remove gpio chip\n");
646 pinctrl_unregister(data->pctl_dev);