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>
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_set_mux(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_gpio_disable_free(struct pinctrl_dev
*pctldev
,
145 struct pinctrl_gpio_range
*range
,
148 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
150 /* disable by setting GPIO_IN */
151 wmt_set_pinmux(data
, WMT_FSEL_GPIO_IN
, offset
);
154 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
155 struct pinctrl_gpio_range
*range
,
159 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
161 wmt_set_pinmux(data
, (input
? WMT_FSEL_GPIO_IN
: WMT_FSEL_GPIO_OUT
),
167 static struct pinmux_ops wmt_pinmux_ops
= {
168 .get_functions_count
= wmt_pmx_get_functions_count
,
169 .get_function_name
= wmt_pmx_get_function_name
,
170 .get_function_groups
= wmt_pmx_get_function_groups
,
171 .set_mux
= wmt_pmx_set_mux
,
172 .gpio_disable_free
= wmt_pmx_gpio_disable_free
,
173 .gpio_set_direction
= wmt_pmx_gpio_set_direction
,
176 static int wmt_get_groups_count(struct pinctrl_dev
*pctldev
)
178 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
180 return data
->ngroups
;
183 static const char *wmt_get_group_name(struct pinctrl_dev
*pctldev
,
186 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
188 return data
->groups
[selector
];
191 static int wmt_get_group_pins(struct pinctrl_dev
*pctldev
,
193 const unsigned **pins
,
196 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
198 *pins
= &data
->pins
[selector
].number
;
204 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data
*data
, u32 pin
)
208 for (i
= 0; i
< data
->npins
; i
++) {
209 if (data
->pins
[i
].number
== pin
)
216 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data
*data
,
217 struct device_node
*np
,
219 struct pinctrl_map
**maps
)
222 struct pinctrl_map
*map
= *maps
;
224 if (fnum
>= ARRAY_SIZE(wmt_functions
)) {
225 dev_err(data
->dev
, "invalid wm,function %d\n", fnum
);
229 group
= wmt_pctl_find_group_by_pin(data
, pin
);
231 dev_err(data
->dev
, "unable to match pin %d to group\n", pin
);
235 map
->type
= PIN_MAP_TYPE_MUX_GROUP
;
236 map
->data
.mux
.group
= data
->groups
[group
];
237 map
->data
.mux
.function
= wmt_functions
[fnum
];
243 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data
*data
,
244 struct device_node
*np
,
246 struct pinctrl_map
**maps
)
249 unsigned long *configs
;
250 struct pinctrl_map
*map
= *maps
;
253 dev_err(data
->dev
, "invalid wm,pull %d\n", pull
);
257 group
= wmt_pctl_find_group_by_pin(data
, pin
);
259 dev_err(data
->dev
, "unable to match pin %d to group\n", pin
);
263 configs
= kzalloc(sizeof(*configs
), GFP_KERNEL
);
269 configs
[0] = PIN_CONFIG_BIAS_DISABLE
;
272 configs
[0] = PIN_CONFIG_BIAS_PULL_DOWN
;
275 configs
[0] = PIN_CONFIG_BIAS_PULL_UP
;
278 configs
[0] = PIN_CONFIG_BIAS_DISABLE
;
279 dev_err(data
->dev
, "invalid pull state %d - disabling\n", pull
);
282 map
->type
= PIN_MAP_TYPE_CONFIGS_PIN
;
283 map
->data
.configs
.group_or_pin
= data
->groups
[group
];
284 map
->data
.configs
.configs
= configs
;
285 map
->data
.configs
.num_configs
= 1;
291 static void wmt_pctl_dt_free_map(struct pinctrl_dev
*pctldev
,
292 struct pinctrl_map
*maps
,
297 for (i
= 0; i
< num_maps
; i
++)
298 if (maps
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
)
299 kfree(maps
[i
].data
.configs
.configs
);
304 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
305 struct device_node
*np
,
306 struct pinctrl_map
**map
,
309 struct pinctrl_map
*maps
, *cur_map
;
310 struct property
*pins
, *funcs
, *pulls
;
312 int num_pins
, num_funcs
, num_pulls
, maps_per_pin
;
314 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
316 pins
= of_find_property(np
, "wm,pins", NULL
);
318 dev_err(data
->dev
, "missing wmt,pins property\n");
322 funcs
= of_find_property(np
, "wm,function", NULL
);
323 pulls
= of_find_property(np
, "wm,pull", NULL
);
325 if (!funcs
&& !pulls
) {
326 dev_err(data
->dev
, "neither wm,function nor wm,pull specified\n");
331 * The following lines calculate how many values are defined for each
334 num_pins
= pins
->length
/ sizeof(u32
);
335 num_funcs
= funcs
? (funcs
->length
/ sizeof(u32
)) : 0;
336 num_pulls
= pulls
? (pulls
->length
/ sizeof(u32
)) : 0;
338 if (num_funcs
> 1 && num_funcs
!= num_pins
) {
339 dev_err(data
->dev
, "wm,function must have 1 or %d entries\n",
344 if (num_pulls
> 1 && num_pulls
!= num_pins
) {
345 dev_err(data
->dev
, "wm,pull must have 1 or %d entries\n",
356 cur_map
= maps
= kzalloc(num_pins
* maps_per_pin
* sizeof(*maps
),
361 for (i
= 0; i
< num_pins
; i
++) {
362 err
= of_property_read_u32_index(np
, "wm,pins", i
, &pin
);
366 if (pin
>= (data
->nbanks
* 32)) {
367 dev_err(data
->dev
, "invalid wm,pins value\n");
373 err
= of_property_read_u32_index(np
, "wm,function",
374 (num_funcs
> 1 ? i
: 0), &func
);
378 err
= wmt_pctl_dt_node_to_map_func(data
, np
, pin
, func
,
385 err
= of_property_read_u32_index(np
, "wm,pull",
386 (num_pulls
> 1 ? i
: 0), &pull
);
390 err
= wmt_pctl_dt_node_to_map_pull(data
, np
, pin
, pull
,
397 *num_maps
= num_pins
* maps_per_pin
;
401 * The fail path removes any maps that have been allocated. The fail path is
402 * only called from code after maps has been kzalloc'd. It is also safe to
403 * pass 'num_pins * maps_per_pin' as the map count even though we probably
404 * failed before all the mappings were read as all maps are allocated at once,
405 * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
406 * is no failpath where a config can be allocated without .type being set.
409 wmt_pctl_dt_free_map(pctldev
, maps
, num_pins
* maps_per_pin
);
413 static struct pinctrl_ops wmt_pctl_ops
= {
414 .get_groups_count
= wmt_get_groups_count
,
415 .get_group_name
= wmt_get_group_name
,
416 .get_group_pins
= wmt_get_group_pins
,
417 .dt_node_to_map
= wmt_pctl_dt_node_to_map
,
418 .dt_free_map
= wmt_pctl_dt_free_map
,
421 static int wmt_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
422 unsigned long *config
)
427 static int wmt_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
428 unsigned long *configs
, unsigned num_configs
)
430 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
431 enum pin_config_param param
;
433 u32 bank
= WMT_BANK_FROM_PIN(pin
);
434 u32 bit
= WMT_BIT_FROM_PIN(pin
);
435 u32 reg_pull_en
= data
->banks
[bank
].reg_pull_en
;
436 u32 reg_pull_cfg
= data
->banks
[bank
].reg_pull_cfg
;
439 if ((reg_pull_en
== NO_REG
) || (reg_pull_cfg
== NO_REG
)) {
440 dev_err(data
->dev
, "bias functions not supported on pin %d\n",
445 for (i
= 0; i
< num_configs
; i
++) {
446 param
= pinconf_to_config_param(configs
[i
]);
447 arg
= pinconf_to_config_argument(configs
[i
]);
449 if ((param
== PIN_CONFIG_BIAS_PULL_DOWN
) ||
450 (param
== PIN_CONFIG_BIAS_PULL_UP
)) {
452 param
= PIN_CONFIG_BIAS_DISABLE
;
456 case PIN_CONFIG_BIAS_DISABLE
:
457 wmt_clearbits(data
, reg_pull_en
, BIT(bit
));
459 case PIN_CONFIG_BIAS_PULL_DOWN
:
460 wmt_clearbits(data
, reg_pull_cfg
, BIT(bit
));
461 wmt_setbits(data
, reg_pull_en
, BIT(bit
));
463 case PIN_CONFIG_BIAS_PULL_UP
:
464 wmt_setbits(data
, reg_pull_cfg
, BIT(bit
));
465 wmt_setbits(data
, reg_pull_en
, BIT(bit
));
468 dev_err(data
->dev
, "unknown pinconf param\n");
471 } /* for each config */
476 static struct pinconf_ops wmt_pinconf_ops
= {
477 .pin_config_get
= wmt_pinconf_get
,
478 .pin_config_set
= wmt_pinconf_set
,
481 static struct pinctrl_desc wmt_desc
= {
482 .owner
= THIS_MODULE
,
483 .name
= "pinctrl-wmt",
484 .pctlops
= &wmt_pctl_ops
,
485 .pmxops
= &wmt_pinmux_ops
,
486 .confops
= &wmt_pinconf_ops
,
489 static int wmt_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
491 struct wmt_pinctrl_data
*data
= gpiochip_get_data(chip
);
492 u32 bank
= WMT_BANK_FROM_PIN(offset
);
493 u32 bit
= WMT_BIT_FROM_PIN(offset
);
494 u32 reg_dir
= data
->banks
[bank
].reg_dir
;
497 val
= readl_relaxed(data
->base
+ reg_dir
);
499 return GPIOF_DIR_OUT
;
504 static int wmt_gpio_get_value(struct gpio_chip
*chip
, unsigned offset
)
506 struct wmt_pinctrl_data
*data
= gpiochip_get_data(chip
);
507 u32 bank
= WMT_BANK_FROM_PIN(offset
);
508 u32 bit
= WMT_BIT_FROM_PIN(offset
);
509 u32 reg_data_in
= data
->banks
[bank
].reg_data_in
;
511 if (reg_data_in
== NO_REG
) {
512 dev_err(data
->dev
, "no data in register defined\n");
516 return !!(readl_relaxed(data
->base
+ reg_data_in
) & BIT(bit
));
519 static void wmt_gpio_set_value(struct gpio_chip
*chip
, unsigned offset
,
522 struct wmt_pinctrl_data
*data
= gpiochip_get_data(chip
);
523 u32 bank
= WMT_BANK_FROM_PIN(offset
);
524 u32 bit
= WMT_BIT_FROM_PIN(offset
);
525 u32 reg_data_out
= data
->banks
[bank
].reg_data_out
;
527 if (reg_data_out
== NO_REG
) {
528 dev_err(data
->dev
, "no data out register defined\n");
533 wmt_setbits(data
, reg_data_out
, BIT(bit
));
535 wmt_clearbits(data
, reg_data_out
, BIT(bit
));
538 static int wmt_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
540 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
543 static int wmt_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
546 wmt_gpio_set_value(chip
, offset
, value
);
547 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
550 static struct gpio_chip wmt_gpio_chip
= {
552 .owner
= THIS_MODULE
,
553 .request
= gpiochip_generic_request
,
554 .free
= gpiochip_generic_free
,
555 .get_direction
= wmt_gpio_get_direction
,
556 .direction_input
= wmt_gpio_direction_input
,
557 .direction_output
= wmt_gpio_direction_output
,
558 .get
= wmt_gpio_get_value
,
559 .set
= wmt_gpio_set_value
,
563 int wmt_pinctrl_probe(struct platform_device
*pdev
,
564 struct wmt_pinctrl_data
*data
)
567 struct resource
*res
;
569 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
570 data
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
571 if (IS_ERR(data
->base
))
572 return PTR_ERR(data
->base
);
574 wmt_desc
.pins
= data
->pins
;
575 wmt_desc
.npins
= data
->npins
;
577 data
->gpio_chip
= wmt_gpio_chip
;
578 data
->gpio_chip
.parent
= &pdev
->dev
;
579 data
->gpio_chip
.of_node
= pdev
->dev
.of_node
;
580 data
->gpio_chip
.ngpio
= data
->nbanks
* 32;
582 platform_set_drvdata(pdev
, data
);
584 data
->dev
= &pdev
->dev
;
586 data
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &wmt_desc
, data
);
587 if (IS_ERR(data
->pctl_dev
)) {
588 dev_err(&pdev
->dev
, "Failed to register pinctrl\n");
589 return PTR_ERR(data
->pctl_dev
);
592 err
= gpiochip_add_data(&data
->gpio_chip
, data
);
594 dev_err(&pdev
->dev
, "could not add GPIO chip\n");
598 err
= gpiochip_add_pin_range(&data
->gpio_chip
, dev_name(data
->dev
),
599 0, 0, data
->nbanks
* 32);
603 dev_info(&pdev
->dev
, "Pin controller initialized\n");
608 gpiochip_remove(&data
->gpio_chip
);
612 int wmt_pinctrl_remove(struct platform_device
*pdev
)
614 struct wmt_pinctrl_data
*data
= platform_get_drvdata(pdev
);
616 gpiochip_remove(&data
->gpio_chip
);