1 // SPDX-License-Identifier: GPL-2.0-only
3 * Pinctrl driver for the Wondermedia SoC's
5 * Copyright (c) 2013 Tony Prisk <linux@prisktech.co.nz>
9 #include <linux/gpio/driver.h>
10 #include <linux/interrupt.h>
12 #include <linux/irq.h>
14 #include <linux/of_irq.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pinctrl/machine.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include "pinctrl-wmt.h"
26 static inline void wmt_setbits(struct wmt_pinctrl_data
*data
, u32 reg
,
31 val
= readl_relaxed(data
->base
+ reg
);
33 writel_relaxed(val
, data
->base
+ reg
);
36 static inline void wmt_clearbits(struct wmt_pinctrl_data
*data
, u32 reg
,
41 val
= readl_relaxed(data
->base
+ reg
);
43 writel_relaxed(val
, data
->base
+ reg
);
48 WMT_FSEL_GPIO_OUT
= 1,
53 static const char * const wmt_functions
[WMT_FSEL_COUNT
] = {
54 [WMT_FSEL_GPIO_IN
] = "gpio_in",
55 [WMT_FSEL_GPIO_OUT
] = "gpio_out",
56 [WMT_FSEL_ALT
] = "alt",
59 static int wmt_pmx_get_functions_count(struct pinctrl_dev
*pctldev
)
61 return WMT_FSEL_COUNT
;
64 static const char *wmt_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
67 return wmt_functions
[selector
];
70 static int wmt_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
72 const char * const **groups
,
73 unsigned * const num_groups
)
75 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
77 /* every pin does every function */
78 *groups
= data
->groups
;
79 *num_groups
= data
->ngroups
;
84 static int wmt_set_pinmux(struct wmt_pinctrl_data
*data
, unsigned func
,
87 u32 bank
= WMT_BANK_FROM_PIN(pin
);
88 u32 bit
= WMT_BIT_FROM_PIN(pin
);
89 u32 reg_en
= data
->banks
[bank
].reg_en
;
90 u32 reg_dir
= data
->banks
[bank
].reg_dir
;
92 if (reg_dir
== NO_REG
) {
93 dev_err(data
->dev
, "pin:%d no direction register defined\n",
99 * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
100 * disabled (as on VT8500) and that no alternate function is available.
103 case WMT_FSEL_GPIO_IN
:
104 if (reg_en
!= NO_REG
)
105 wmt_setbits(data
, reg_en
, BIT(bit
));
106 wmt_clearbits(data
, reg_dir
, BIT(bit
));
108 case WMT_FSEL_GPIO_OUT
:
109 if (reg_en
!= NO_REG
)
110 wmt_setbits(data
, reg_en
, BIT(bit
));
111 wmt_setbits(data
, reg_dir
, BIT(bit
));
114 if (reg_en
== NO_REG
) {
115 dev_err(data
->dev
, "pin:%d no alt function available\n",
119 wmt_clearbits(data
, reg_en
, BIT(bit
));
125 static int wmt_pmx_set_mux(struct pinctrl_dev
*pctldev
,
126 unsigned func_selector
,
127 unsigned group_selector
)
129 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
130 u32 pinnum
= data
->pins
[group_selector
].number
;
132 return wmt_set_pinmux(data
, func_selector
, pinnum
);
135 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev
*pctldev
,
136 struct pinctrl_gpio_range
*range
,
139 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
141 /* disable by setting GPIO_IN */
142 wmt_set_pinmux(data
, WMT_FSEL_GPIO_IN
, offset
);
145 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
146 struct pinctrl_gpio_range
*range
,
150 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
152 wmt_set_pinmux(data
, (input
? WMT_FSEL_GPIO_IN
: WMT_FSEL_GPIO_OUT
),
158 static const struct pinmux_ops wmt_pinmux_ops
= {
159 .get_functions_count
= wmt_pmx_get_functions_count
,
160 .get_function_name
= wmt_pmx_get_function_name
,
161 .get_function_groups
= wmt_pmx_get_function_groups
,
162 .set_mux
= wmt_pmx_set_mux
,
163 .gpio_disable_free
= wmt_pmx_gpio_disable_free
,
164 .gpio_set_direction
= wmt_pmx_gpio_set_direction
,
167 static int wmt_get_groups_count(struct pinctrl_dev
*pctldev
)
169 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
171 return data
->ngroups
;
174 static const char *wmt_get_group_name(struct pinctrl_dev
*pctldev
,
177 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
179 return data
->groups
[selector
];
182 static int wmt_get_group_pins(struct pinctrl_dev
*pctldev
,
184 const unsigned **pins
,
187 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
189 *pins
= &data
->pins
[selector
].number
;
195 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data
*data
, u32 pin
)
199 for (i
= 0; i
< data
->npins
; i
++) {
200 if (data
->pins
[i
].number
== pin
)
207 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data
*data
,
208 struct device_node
*np
,
210 struct pinctrl_map
**maps
)
213 struct pinctrl_map
*map
= *maps
;
215 if (fnum
>= ARRAY_SIZE(wmt_functions
)) {
216 dev_err(data
->dev
, "invalid wm,function %d\n", fnum
);
220 group
= wmt_pctl_find_group_by_pin(data
, pin
);
222 dev_err(data
->dev
, "unable to match pin %d to group\n", pin
);
226 map
->type
= PIN_MAP_TYPE_MUX_GROUP
;
227 map
->data
.mux
.group
= data
->groups
[group
];
228 map
->data
.mux
.function
= wmt_functions
[fnum
];
234 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data
*data
,
235 struct device_node
*np
,
237 struct pinctrl_map
**maps
)
240 unsigned long *configs
;
241 struct pinctrl_map
*map
= *maps
;
244 dev_err(data
->dev
, "invalid wm,pull %d\n", pull
);
248 group
= wmt_pctl_find_group_by_pin(data
, pin
);
250 dev_err(data
->dev
, "unable to match pin %d to group\n", pin
);
254 configs
= kzalloc(sizeof(*configs
), GFP_KERNEL
);
260 configs
[0] = PIN_CONFIG_BIAS_DISABLE
;
263 configs
[0] = PIN_CONFIG_BIAS_PULL_DOWN
;
266 configs
[0] = PIN_CONFIG_BIAS_PULL_UP
;
269 configs
[0] = PIN_CONFIG_BIAS_DISABLE
;
270 dev_err(data
->dev
, "invalid pull state %d - disabling\n", pull
);
273 map
->type
= PIN_MAP_TYPE_CONFIGS_PIN
;
274 map
->data
.configs
.group_or_pin
= data
->groups
[group
];
275 map
->data
.configs
.configs
= configs
;
276 map
->data
.configs
.num_configs
= 1;
282 static void wmt_pctl_dt_free_map(struct pinctrl_dev
*pctldev
,
283 struct pinctrl_map
*maps
,
288 for (i
= 0; i
< num_maps
; i
++)
289 if (maps
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
)
290 kfree(maps
[i
].data
.configs
.configs
);
295 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
296 struct device_node
*np
,
297 struct pinctrl_map
**map
,
300 struct pinctrl_map
*maps
, *cur_map
;
301 struct property
*pins
, *funcs
, *pulls
;
303 int num_pins
, num_funcs
, num_pulls
, maps_per_pin
;
305 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
307 pins
= of_find_property(np
, "wm,pins", NULL
);
309 dev_err(data
->dev
, "missing wmt,pins property\n");
313 funcs
= of_find_property(np
, "wm,function", NULL
);
314 pulls
= of_find_property(np
, "wm,pull", NULL
);
316 if (!funcs
&& !pulls
) {
317 dev_err(data
->dev
, "neither wm,function nor wm,pull specified\n");
322 * The following lines calculate how many values are defined for each
325 num_pins
= pins
->length
/ sizeof(u32
);
326 num_funcs
= funcs
? (funcs
->length
/ sizeof(u32
)) : 0;
327 num_pulls
= pulls
? (pulls
->length
/ sizeof(u32
)) : 0;
329 if (num_funcs
> 1 && num_funcs
!= num_pins
) {
330 dev_err(data
->dev
, "wm,function must have 1 or %d entries\n",
335 if (num_pulls
> 1 && num_pulls
!= num_pins
) {
336 dev_err(data
->dev
, "wm,pull must have 1 or %d entries\n",
347 cur_map
= maps
= kcalloc(num_pins
* maps_per_pin
, sizeof(*maps
),
352 for (i
= 0; i
< num_pins
; i
++) {
353 err
= of_property_read_u32_index(np
, "wm,pins", i
, &pin
);
357 if (pin
>= (data
->nbanks
* 32)) {
358 dev_err(data
->dev
, "invalid wm,pins value\n");
364 err
= of_property_read_u32_index(np
, "wm,function",
365 (num_funcs
> 1 ? i
: 0), &func
);
369 err
= wmt_pctl_dt_node_to_map_func(data
, np
, pin
, func
,
376 err
= of_property_read_u32_index(np
, "wm,pull",
377 (num_pulls
> 1 ? i
: 0), &pull
);
381 err
= wmt_pctl_dt_node_to_map_pull(data
, np
, pin
, pull
,
388 *num_maps
= num_pins
* maps_per_pin
;
392 * The fail path removes any maps that have been allocated. The fail path is
393 * only called from code after maps has been kzalloc'd. It is also safe to
394 * pass 'num_pins * maps_per_pin' as the map count even though we probably
395 * failed before all the mappings were read as all maps are allocated at once,
396 * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
397 * is no failpath where a config can be allocated without .type being set.
400 wmt_pctl_dt_free_map(pctldev
, maps
, num_pins
* maps_per_pin
);
404 static const struct pinctrl_ops wmt_pctl_ops
= {
405 .get_groups_count
= wmt_get_groups_count
,
406 .get_group_name
= wmt_get_group_name
,
407 .get_group_pins
= wmt_get_group_pins
,
408 .dt_node_to_map
= wmt_pctl_dt_node_to_map
,
409 .dt_free_map
= wmt_pctl_dt_free_map
,
412 static int wmt_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
413 unsigned long *config
)
418 static int wmt_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
419 unsigned long *configs
, unsigned num_configs
)
421 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
422 enum pin_config_param param
;
424 u32 bank
= WMT_BANK_FROM_PIN(pin
);
425 u32 bit
= WMT_BIT_FROM_PIN(pin
);
426 u32 reg_pull_en
= data
->banks
[bank
].reg_pull_en
;
427 u32 reg_pull_cfg
= data
->banks
[bank
].reg_pull_cfg
;
430 if ((reg_pull_en
== NO_REG
) || (reg_pull_cfg
== NO_REG
)) {
431 dev_err(data
->dev
, "bias functions not supported on pin %d\n",
436 for (i
= 0; i
< num_configs
; i
++) {
437 param
= pinconf_to_config_param(configs
[i
]);
438 arg
= pinconf_to_config_argument(configs
[i
]);
440 if ((param
== PIN_CONFIG_BIAS_PULL_DOWN
) ||
441 (param
== PIN_CONFIG_BIAS_PULL_UP
)) {
443 param
= PIN_CONFIG_BIAS_DISABLE
;
447 case PIN_CONFIG_BIAS_DISABLE
:
448 wmt_clearbits(data
, reg_pull_en
, BIT(bit
));
450 case PIN_CONFIG_BIAS_PULL_DOWN
:
451 wmt_clearbits(data
, reg_pull_cfg
, BIT(bit
));
452 wmt_setbits(data
, reg_pull_en
, BIT(bit
));
454 case PIN_CONFIG_BIAS_PULL_UP
:
455 wmt_setbits(data
, reg_pull_cfg
, BIT(bit
));
456 wmt_setbits(data
, reg_pull_en
, BIT(bit
));
459 dev_err(data
->dev
, "unknown pinconf param\n");
462 } /* for each config */
467 static const struct pinconf_ops wmt_pinconf_ops
= {
468 .pin_config_get
= wmt_pinconf_get
,
469 .pin_config_set
= wmt_pinconf_set
,
472 static struct pinctrl_desc wmt_desc
= {
473 .owner
= THIS_MODULE
,
474 .name
= "pinctrl-wmt",
475 .pctlops
= &wmt_pctl_ops
,
476 .pmxops
= &wmt_pinmux_ops
,
477 .confops
= &wmt_pinconf_ops
,
480 static int wmt_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
482 struct wmt_pinctrl_data
*data
= gpiochip_get_data(chip
);
483 u32 bank
= WMT_BANK_FROM_PIN(offset
);
484 u32 bit
= WMT_BIT_FROM_PIN(offset
);
485 u32 reg_dir
= data
->banks
[bank
].reg_dir
;
488 val
= readl_relaxed(data
->base
+ reg_dir
);
490 return GPIO_LINE_DIRECTION_OUT
;
492 return GPIO_LINE_DIRECTION_IN
;
495 static int wmt_gpio_get_value(struct gpio_chip
*chip
, unsigned offset
)
497 struct wmt_pinctrl_data
*data
= gpiochip_get_data(chip
);
498 u32 bank
= WMT_BANK_FROM_PIN(offset
);
499 u32 bit
= WMT_BIT_FROM_PIN(offset
);
500 u32 reg_data_in
= data
->banks
[bank
].reg_data_in
;
502 if (reg_data_in
== NO_REG
) {
503 dev_err(data
->dev
, "no data in register defined\n");
507 return !!(readl_relaxed(data
->base
+ reg_data_in
) & BIT(bit
));
510 static void wmt_gpio_set_value(struct gpio_chip
*chip
, unsigned offset
,
513 struct wmt_pinctrl_data
*data
= gpiochip_get_data(chip
);
514 u32 bank
= WMT_BANK_FROM_PIN(offset
);
515 u32 bit
= WMT_BIT_FROM_PIN(offset
);
516 u32 reg_data_out
= data
->banks
[bank
].reg_data_out
;
518 if (reg_data_out
== NO_REG
) {
519 dev_err(data
->dev
, "no data out register defined\n");
524 wmt_setbits(data
, reg_data_out
, BIT(bit
));
526 wmt_clearbits(data
, reg_data_out
, BIT(bit
));
529 static int wmt_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
531 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
534 static int wmt_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
537 wmt_gpio_set_value(chip
, offset
, value
);
538 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
541 static const struct gpio_chip wmt_gpio_chip
= {
543 .owner
= THIS_MODULE
,
544 .request
= gpiochip_generic_request
,
545 .free
= gpiochip_generic_free
,
546 .get_direction
= wmt_gpio_get_direction
,
547 .direction_input
= wmt_gpio_direction_input
,
548 .direction_output
= wmt_gpio_direction_output
,
549 .get
= wmt_gpio_get_value
,
550 .set
= wmt_gpio_set_value
,
554 int wmt_pinctrl_probe(struct platform_device
*pdev
,
555 struct wmt_pinctrl_data
*data
)
559 data
->base
= devm_platform_ioremap_resource(pdev
, 0);
560 if (IS_ERR(data
->base
))
561 return PTR_ERR(data
->base
);
563 wmt_desc
.pins
= data
->pins
;
564 wmt_desc
.npins
= data
->npins
;
566 data
->gpio_chip
= wmt_gpio_chip
;
567 data
->gpio_chip
.parent
= &pdev
->dev
;
568 data
->gpio_chip
.of_node
= pdev
->dev
.of_node
;
569 data
->gpio_chip
.ngpio
= data
->nbanks
* 32;
571 platform_set_drvdata(pdev
, data
);
573 data
->dev
= &pdev
->dev
;
575 data
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &wmt_desc
, data
);
576 if (IS_ERR(data
->pctl_dev
)) {
577 dev_err(&pdev
->dev
, "Failed to register pinctrl\n");
578 return PTR_ERR(data
->pctl_dev
);
581 err
= gpiochip_add_data(&data
->gpio_chip
, data
);
583 dev_err(&pdev
->dev
, "could not add GPIO chip\n");
587 err
= gpiochip_add_pin_range(&data
->gpio_chip
, dev_name(data
->dev
),
588 0, 0, data
->nbanks
* 32);
592 dev_info(&pdev
->dev
, "Pin controller initialized\n");
597 gpiochip_remove(&data
->gpio_chip
);