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 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
= kzalloc(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 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 config
)
429 struct wmt_pinctrl_data
*data
= pinctrl_dev_get_drvdata(pctldev
);
430 enum pin_config_param param
= pinconf_to_config_param(config
);
431 u16 arg
= pinconf_to_config_argument(config
);
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
;
437 if ((reg_pull_en
== NO_REG
) || (reg_pull_cfg
== NO_REG
)) {
438 dev_err(data
->dev
, "bias functions not supported on pin %d\n",
443 if ((param
== PIN_CONFIG_BIAS_PULL_DOWN
) ||
444 (param
== PIN_CONFIG_BIAS_PULL_UP
)) {
446 param
= PIN_CONFIG_BIAS_DISABLE
;
450 case PIN_CONFIG_BIAS_DISABLE
:
451 wmt_clearbits(data
, reg_pull_en
, BIT(bit
));
453 case PIN_CONFIG_BIAS_PULL_DOWN
:
454 wmt_clearbits(data
, reg_pull_cfg
, BIT(bit
));
455 wmt_setbits(data
, reg_pull_en
, BIT(bit
));
457 case PIN_CONFIG_BIAS_PULL_UP
:
458 wmt_setbits(data
, reg_pull_cfg
, BIT(bit
));
459 wmt_setbits(data
, reg_pull_en
, BIT(bit
));
462 dev_err(data
->dev
, "unknown pinconf param\n");
469 static struct pinconf_ops wmt_pinconf_ops
= {
470 .pin_config_get
= wmt_pinconf_get
,
471 .pin_config_set
= wmt_pinconf_set
,
474 static struct pinctrl_desc wmt_desc
= {
475 .owner
= THIS_MODULE
,
476 .name
= "pinctrl-wmt",
477 .pctlops
= &wmt_pctl_ops
,
478 .pmxops
= &wmt_pinmux_ops
,
479 .confops
= &wmt_pinconf_ops
,
482 static int wmt_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
484 return pinctrl_request_gpio(chip
->base
+ offset
);
487 static void wmt_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
489 pinctrl_free_gpio(chip
->base
+ offset
);
492 static int wmt_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
494 struct wmt_pinctrl_data
*data
= dev_get_drvdata(chip
->dev
);
495 u32 bank
= WMT_BANK_FROM_PIN(offset
);
496 u32 bit
= WMT_BIT_FROM_PIN(offset
);
497 u32 reg_dir
= data
->banks
[bank
].reg_dir
;
500 val
= readl_relaxed(data
->base
+ reg_dir
);
502 return GPIOF_DIR_OUT
;
507 static int wmt_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
509 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
512 static int wmt_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
515 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
518 static int wmt_gpio_get_value(struct gpio_chip
*chip
, unsigned offset
)
520 struct wmt_pinctrl_data
*data
= dev_get_drvdata(chip
->dev
);
521 u32 bank
= WMT_BANK_FROM_PIN(offset
);
522 u32 bit
= WMT_BIT_FROM_PIN(offset
);
523 u32 reg_data_in
= data
->banks
[bank
].reg_data_in
;
525 if (reg_data_in
== NO_REG
) {
526 dev_err(data
->dev
, "no data in register defined\n");
530 return !!(readl_relaxed(data
->base
+ reg_data_in
) & BIT(bit
));
533 static void wmt_gpio_set_value(struct gpio_chip
*chip
, unsigned offset
,
536 struct wmt_pinctrl_data
*data
= dev_get_drvdata(chip
->dev
);
537 u32 bank
= WMT_BANK_FROM_PIN(offset
);
538 u32 bit
= WMT_BIT_FROM_PIN(offset
);
539 u32 reg_data_out
= data
->banks
[bank
].reg_data_out
;
541 if (reg_data_out
== NO_REG
) {
542 dev_err(data
->dev
, "no data out register defined\n");
547 wmt_setbits(data
, reg_data_out
, BIT(bit
));
549 wmt_clearbits(data
, reg_data_out
, BIT(bit
));
552 static struct gpio_chip wmt_gpio_chip
= {
554 .owner
= THIS_MODULE
,
555 .request
= wmt_gpio_request
,
556 .free
= wmt_gpio_free
,
557 .get_direction
= wmt_gpio_get_direction
,
558 .direction_input
= wmt_gpio_direction_input
,
559 .direction_output
= wmt_gpio_direction_output
,
560 .get
= wmt_gpio_get_value
,
561 .set
= wmt_gpio_set_value
,
565 int wmt_pinctrl_probe(struct platform_device
*pdev
,
566 struct wmt_pinctrl_data
*data
)
569 struct resource
*res
;
571 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
572 data
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
573 if (IS_ERR(data
->base
))
574 return PTR_ERR(data
->base
);
576 wmt_desc
.pins
= data
->pins
;
577 wmt_desc
.npins
= data
->npins
;
579 data
->gpio_chip
= wmt_gpio_chip
;
580 data
->gpio_chip
.dev
= &pdev
->dev
;
581 data
->gpio_chip
.of_node
= pdev
->dev
.of_node
;
582 data
->gpio_chip
.ngpio
= data
->nbanks
* 32;
584 platform_set_drvdata(pdev
, data
);
586 data
->dev
= &pdev
->dev
;
588 data
->pctl_dev
= pinctrl_register(&wmt_desc
, &pdev
->dev
, data
);
589 if (!data
->pctl_dev
) {
590 dev_err(&pdev
->dev
, "Failed to register pinctrl\n");
594 err
= gpiochip_add(&data
->gpio_chip
);
596 dev_err(&pdev
->dev
, "could not add GPIO chip\n");
600 err
= gpiochip_add_pin_range(&data
->gpio_chip
, dev_name(data
->dev
),
601 0, 0, data
->nbanks
* 32);
605 dev_info(&pdev
->dev
, "Pin controller initialized\n");
610 if (gpiochip_remove(&data
->gpio_chip
))
611 dev_err(&pdev
->dev
, "failed to remove gpio chip\n");
613 pinctrl_unregister(data
->pctl_dev
);
617 int wmt_pinctrl_remove(struct platform_device
*pdev
)
619 struct wmt_pinctrl_data
*data
= platform_get_drvdata(pdev
);
622 err
= gpiochip_remove(&data
->gpio_chip
);
624 dev_err(&pdev
->dev
, "failed to remove gpio chip\n");
626 pinctrl_unregister(data
->pctl_dev
);