1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell PXA2xx family pin control
5 * Copyright (C) 2015 Robert Jarzmik
8 #include <linux/bitops.h>
11 #include <linux/of_address.h>
12 #include <linux/module.h>
13 #include <linux/pinctrl/machine.h>
14 #include <linux/pinctrl/pinconf.h>
15 #include <linux/pinctrl/pinconf-generic.h>
16 #include <linux/pinctrl/pinmux.h>
17 #include <linux/pinctrl/pinctrl.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
21 #include "../pinctrl-utils.h"
22 #include "pinctrl-pxa2xx.h"
24 static int pxa2xx_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
26 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
31 static const char *pxa2xx_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
34 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
35 struct pxa_pinctrl_group
*group
= pctl
->groups
+ tgroup
;
40 static int pxa2xx_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
42 const unsigned **pins
,
45 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
46 struct pxa_pinctrl_group
*group
= pctl
->groups
+ tgroup
;
48 *pins
= (unsigned *)&group
->pin
;
54 static const struct pinctrl_ops pxa2xx_pctl_ops
= {
56 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
57 .dt_free_map
= pinctrl_utils_free_map
,
59 .get_groups_count
= pxa2xx_pctrl_get_groups_count
,
60 .get_group_name
= pxa2xx_pctrl_get_group_name
,
61 .get_group_pins
= pxa2xx_pctrl_get_group_pins
,
64 static struct pxa_desc_function
*
65 pxa_desc_by_func_group(struct pxa_pinctrl
*pctl
, const char *pin_name
,
66 const char *func_name
)
69 struct pxa_desc_function
*df
;
71 for (i
= 0; i
< pctl
->npins
; i
++) {
72 const struct pxa_desc_pin
*pin
= pctl
->ppins
+ i
;
74 if (!strcmp(pin
->pin
.name
, pin_name
))
75 for (df
= pin
->functions
; df
->name
; df
++)
76 if (!strcmp(df
->name
, func_name
))
83 static int pxa2xx_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
84 struct pinctrl_gpio_range
*range
,
88 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
93 gpdr
= pctl
->base_gpdr
[pin
/ 32];
94 dev_dbg(pctl
->dev
, "set_direction(pin=%d): dir=%d\n",
97 spin_lock_irqsave(&pctl
->lock
, flags
);
99 val
= readl_relaxed(gpdr
);
100 val
= (val
& ~BIT(pin
% 32)) | (input
? 0 : BIT(pin
% 32));
101 writel_relaxed(val
, gpdr
);
103 spin_unlock_irqrestore(&pctl
->lock
, flags
);
108 static const char *pxa2xx_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
111 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
112 struct pxa_pinctrl_function
*pf
= pctl
->functions
+ function
;
117 static int pxa2xx_get_functions_count(struct pinctrl_dev
*pctldev
)
119 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
124 static int pxa2xx_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
126 const char * const **groups
,
127 unsigned * const num_groups
)
129 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
130 struct pxa_pinctrl_function
*pf
= pctl
->functions
+ function
;
132 *groups
= pf
->groups
;
133 *num_groups
= pf
->ngroups
;
138 static int pxa2xx_pmx_set_mux(struct pinctrl_dev
*pctldev
, unsigned function
,
141 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
142 struct pxa_pinctrl_group
*group
= pctl
->groups
+ tgroup
;
143 struct pxa_desc_function
*df
;
146 void __iomem
*gafr
, *gpdr
;
150 df
= pxa_desc_by_func_group(pctl
, group
->name
,
151 (pctl
->functions
+ function
)->name
);
156 gafr
= pctl
->base_gafr
[pin
/ 16];
157 gpdr
= pctl
->base_gpdr
[pin
/ 32];
158 shift
= (pin
% 16) << 1;
159 dev_dbg(pctl
->dev
, "set_mux(pin=%d): af=%d dir=%d\n",
160 pin
, df
->muxval
>> 1, df
->muxval
& 0x1);
162 spin_lock_irqsave(&pctl
->lock
, flags
);
164 val
= readl_relaxed(gafr
);
165 val
= (val
& ~(0x3 << shift
)) | ((df
->muxval
>> 1) << shift
);
166 writel_relaxed(val
, gafr
);
168 val
= readl_relaxed(gpdr
);
169 val
= (val
& ~BIT(pin
% 32)) | ((df
->muxval
& 1) ? BIT(pin
% 32) : 0);
170 writel_relaxed(val
, gpdr
);
172 spin_unlock_irqrestore(&pctl
->lock
, flags
);
176 static const struct pinmux_ops pxa2xx_pinmux_ops
= {
177 .get_functions_count
= pxa2xx_get_functions_count
,
178 .get_function_name
= pxa2xx_pmx_get_func_name
,
179 .get_function_groups
= pxa2xx_pmx_get_func_groups
,
180 .set_mux
= pxa2xx_pmx_set_mux
,
181 .gpio_set_direction
= pxa2xx_pmx_gpio_set_direction
,
184 static int pxa2xx_pconf_group_get(struct pinctrl_dev
*pctldev
,
186 unsigned long *config
)
188 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
189 struct pxa_pinctrl_group
*g
= pctl
->groups
+ group
;
191 unsigned pin
= g
->pin
;
192 void __iomem
*pgsr
= pctl
->base_pgsr
[pin
/ 32];
195 spin_lock_irqsave(&pctl
->lock
, flags
);
196 val
= readl_relaxed(pgsr
) & BIT(pin
% 32);
197 *config
= val
? PIN_CONFIG_LOW_POWER_MODE
: 0;
198 spin_unlock_irqrestore(&pctl
->lock
, flags
);
200 dev_dbg(pctl
->dev
, "get sleep gpio state(pin=%d) %d\n",
205 static int pxa2xx_pconf_group_set(struct pinctrl_dev
*pctldev
,
207 unsigned long *configs
,
208 unsigned num_configs
)
210 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
211 struct pxa_pinctrl_group
*g
= pctl
->groups
+ group
;
213 unsigned pin
= g
->pin
;
214 void __iomem
*pgsr
= pctl
->base_pgsr
[pin
/ 32];
218 for (i
= 0; i
< num_configs
; i
++) {
219 switch (pinconf_to_config_param(configs
[i
])) {
220 case PIN_CONFIG_LOW_POWER_MODE
:
221 is_set
= pinconf_to_config_argument(configs
[i
]);
228 dev_dbg(pctl
->dev
, "set sleep gpio state(pin=%d) %d\n",
231 spin_lock_irqsave(&pctl
->lock
, flags
);
232 val
= readl_relaxed(pgsr
);
233 val
= (val
& ~BIT(pin
% 32)) | (is_set
? BIT(pin
% 32) : 0);
234 writel_relaxed(val
, pgsr
);
235 spin_unlock_irqrestore(&pctl
->lock
, flags
);
240 static const struct pinconf_ops pxa2xx_pconf_ops
= {
241 .pin_config_group_get
= pxa2xx_pconf_group_get
,
242 .pin_config_group_set
= pxa2xx_pconf_group_set
,
246 static struct pinctrl_desc pxa2xx_pinctrl_desc
= {
247 .confops
= &pxa2xx_pconf_ops
,
248 .pctlops
= &pxa2xx_pctl_ops
,
249 .pmxops
= &pxa2xx_pinmux_ops
,
252 static const struct pxa_pinctrl_function
*
253 pxa2xx_find_function(struct pxa_pinctrl
*pctl
, const char *fname
,
254 const struct pxa_pinctrl_function
*functions
)
256 const struct pxa_pinctrl_function
*func
;
258 for (func
= functions
; func
->name
; func
++)
259 if (!strcmp(fname
, func
->name
))
265 static int pxa2xx_build_functions(struct pxa_pinctrl
*pctl
)
268 struct pxa_pinctrl_function
*functions
;
269 struct pxa_desc_function
*df
;
272 * Each pin can have at most 6 alternate functions, and 2 gpio functions
273 * which are common to each pin. As there are more than 2 pins without
274 * alternate function, 6 * npins is an absolute high limit of the number
277 functions
= devm_kcalloc(pctl
->dev
, pctl
->npins
* 6,
278 sizeof(*functions
), GFP_KERNEL
);
282 for (i
= 0; i
< pctl
->npins
; i
++)
283 for (df
= pctl
->ppins
[i
].functions
; df
->name
; df
++)
284 if (!pxa2xx_find_function(pctl
, df
->name
, functions
))
285 (functions
+ pctl
->nfuncs
++)->name
= df
->name
;
286 pctl
->functions
= devm_kmemdup(pctl
->dev
, functions
,
287 pctl
->nfuncs
* sizeof(*functions
),
289 if (!pctl
->functions
)
292 devm_kfree(pctl
->dev
, functions
);
296 static int pxa2xx_build_groups(struct pxa_pinctrl
*pctl
)
299 struct pxa_pinctrl_function
*func
;
300 struct pxa_desc_function
*df
;
303 gtmp
= devm_kmalloc_array(pctl
->dev
, pctl
->npins
, sizeof(*gtmp
),
308 for (i
= 0; i
< pctl
->nfuncs
; i
++) {
310 for (j
= 0; j
< pctl
->npins
; j
++)
311 for (df
= pctl
->ppins
[j
].functions
; df
->name
;
313 if (!strcmp(pctl
->functions
[i
].name
,
315 gtmp
[ngroups
++] = (char *)
316 pctl
->ppins
[j
].pin
.name
;
317 func
= pctl
->functions
+ i
;
318 func
->ngroups
= ngroups
;
320 devm_kmalloc_array(pctl
->dev
, ngroups
,
321 sizeof(char *), GFP_KERNEL
);
325 memcpy(func
->groups
, gtmp
, ngroups
* sizeof(*gtmp
));
328 devm_kfree(pctl
->dev
, gtmp
);
332 static int pxa2xx_build_state(struct pxa_pinctrl
*pctl
,
333 const struct pxa_desc_pin
*ppins
, int npins
)
335 struct pxa_pinctrl_group
*group
;
336 struct pinctrl_pin_desc
*pins
;
341 pctl
->ngroups
= npins
;
343 pctl
->desc
.npins
= npins
;
344 pins
= devm_kcalloc(pctl
->dev
, npins
, sizeof(*pins
), GFP_KERNEL
);
348 pctl
->desc
.pins
= pins
;
349 for (i
= 0; i
< npins
; i
++)
350 pins
[i
] = ppins
[i
].pin
;
352 pctl
->groups
= devm_kmalloc_array(pctl
->dev
, pctl
->ngroups
,
353 sizeof(*pctl
->groups
), GFP_KERNEL
);
357 for (i
= 0; i
< npins
; i
++) {
358 group
= pctl
->groups
+ i
;
359 group
->name
= ppins
[i
].pin
.name
;
360 group
->pin
= ppins
[i
].pin
.number
;
363 ret
= pxa2xx_build_functions(pctl
);
367 ret
= pxa2xx_build_groups(pctl
);
374 int pxa2xx_pinctrl_init(struct platform_device
*pdev
,
375 const struct pxa_desc_pin
*ppins
, int npins
,
376 void __iomem
*base_gafr
[], void __iomem
*base_gpdr
[],
377 void __iomem
*base_pgsr
[])
379 struct pxa_pinctrl
*pctl
;
380 int ret
, i
, maxpin
= 0;
382 for (i
= 0; i
< npins
; i
++)
383 maxpin
= max_t(int, ppins
[i
].pin
.number
, maxpin
);
385 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
), GFP_KERNEL
);
388 pctl
->base_gafr
= devm_kcalloc(&pdev
->dev
, roundup(maxpin
, 16),
389 sizeof(*pctl
->base_gafr
), GFP_KERNEL
);
390 pctl
->base_gpdr
= devm_kcalloc(&pdev
->dev
, roundup(maxpin
, 32),
391 sizeof(*pctl
->base_gpdr
), GFP_KERNEL
);
392 pctl
->base_pgsr
= devm_kcalloc(&pdev
->dev
, roundup(maxpin
, 32),
393 sizeof(*pctl
->base_pgsr
), GFP_KERNEL
);
394 if (!pctl
->base_gafr
|| !pctl
->base_gpdr
|| !pctl
->base_pgsr
)
397 platform_set_drvdata(pdev
, pctl
);
398 spin_lock_init(&pctl
->lock
);
400 pctl
->dev
= &pdev
->dev
;
401 pctl
->desc
= pxa2xx_pinctrl_desc
;
402 pctl
->desc
.name
= dev_name(&pdev
->dev
);
403 pctl
->desc
.owner
= THIS_MODULE
;
405 for (i
= 0; i
< roundup(maxpin
, 16); i
+= 16)
406 pctl
->base_gafr
[i
/ 16] = base_gafr
[i
/ 16];
407 for (i
= 0; i
< roundup(maxpin
, 32); i
+= 32) {
408 pctl
->base_gpdr
[i
/ 32] = base_gpdr
[i
/ 32];
409 pctl
->base_pgsr
[i
/ 32] = base_pgsr
[i
/ 32];
412 ret
= pxa2xx_build_state(pctl
, ppins
, npins
);
416 pctl
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &pctl
->desc
, pctl
);
417 if (IS_ERR(pctl
->pctl_dev
)) {
418 dev_err(&pdev
->dev
, "couldn't register pinctrl driver\n");
419 return PTR_ERR(pctl
->pctl_dev
);
422 dev_info(&pdev
->dev
, "initialized pxa2xx pinctrl driver\n");
426 EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_init
);
428 int pxa2xx_pinctrl_exit(struct platform_device
*pdev
)
430 struct pxa_pinctrl
*pctl
= platform_get_drvdata(pdev
);
432 pinctrl_unregister(pctl
->pctl_dev
);
435 EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_exit
);
437 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
438 MODULE_DESCRIPTION("Marvell PXA2xx pinctrl driver");
439 MODULE_LICENSE("GPL v2");