2 * Marvell PXA2xx family pin control
4 * Copyright (C) 2015 Robert Jarzmik
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
12 #include <linux/bitops.h>
15 #include <linux/of_address.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include "../pinctrl-utils.h"
25 #include "pinctrl-pxa2xx.h"
27 static int pxa2xx_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
29 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
34 static const char *pxa2xx_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
37 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
38 struct pxa_pinctrl_group
*group
= pctl
->groups
+ tgroup
;
43 static int pxa2xx_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
45 const unsigned **pins
,
48 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
49 struct pxa_pinctrl_group
*group
= pctl
->groups
+ tgroup
;
51 *pins
= (unsigned *)&group
->pin
;
57 static const struct pinctrl_ops pxa2xx_pctl_ops
= {
59 .dt_node_to_map
= pinconf_generic_dt_node_to_map_all
,
60 .dt_free_map
= pinctrl_utils_free_map
,
62 .get_groups_count
= pxa2xx_pctrl_get_groups_count
,
63 .get_group_name
= pxa2xx_pctrl_get_group_name
,
64 .get_group_pins
= pxa2xx_pctrl_get_group_pins
,
67 static struct pxa_desc_function
*
68 pxa_desc_by_func_group(struct pxa_pinctrl
*pctl
, const char *pin_name
,
69 const char *func_name
)
72 struct pxa_desc_function
*df
;
74 for (i
= 0; i
< pctl
->npins
; i
++) {
75 const struct pxa_desc_pin
*pin
= pctl
->ppins
+ i
;
77 if (!strcmp(pin
->pin
.name
, pin_name
))
78 for (df
= pin
->functions
; df
->name
; df
++)
79 if (!strcmp(df
->name
, func_name
))
86 static int pxa2xx_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
87 struct pinctrl_gpio_range
*range
,
91 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
96 gpdr
= pctl
->base_gpdr
[pin
/ 32];
97 dev_dbg(pctl
->dev
, "set_direction(pin=%d): dir=%d\n",
100 spin_lock_irqsave(&pctl
->lock
, flags
);
102 val
= readl_relaxed(gpdr
);
103 val
= (val
& ~BIT(pin
% 32)) | (input
? 0 : BIT(pin
% 32));
104 writel_relaxed(val
, gpdr
);
106 spin_unlock_irqrestore(&pctl
->lock
, flags
);
111 static const char *pxa2xx_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
114 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
115 struct pxa_pinctrl_function
*pf
= pctl
->functions
+ function
;
120 static int pxa2xx_get_functions_count(struct pinctrl_dev
*pctldev
)
122 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
127 static int pxa2xx_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
129 const char * const **groups
,
130 unsigned * const num_groups
)
132 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
133 struct pxa_pinctrl_function
*pf
= pctl
->functions
+ function
;
135 *groups
= pf
->groups
;
136 *num_groups
= pf
->ngroups
;
141 static int pxa2xx_pmx_set_mux(struct pinctrl_dev
*pctldev
, unsigned function
,
144 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
145 struct pxa_pinctrl_group
*group
= pctl
->groups
+ tgroup
;
146 struct pxa_desc_function
*df
;
149 void __iomem
*gafr
, *gpdr
;
153 df
= pxa_desc_by_func_group(pctl
, group
->name
,
154 (pctl
->functions
+ function
)->name
);
159 gafr
= pctl
->base_gafr
[pin
/ 16];
160 gpdr
= pctl
->base_gpdr
[pin
/ 32];
161 shift
= (pin
% 16) << 1;
162 dev_dbg(pctl
->dev
, "set_mux(pin=%d): af=%d dir=%d\n",
163 pin
, df
->muxval
>> 1, df
->muxval
& 0x1);
165 spin_lock_irqsave(&pctl
->lock
, flags
);
167 val
= readl_relaxed(gafr
);
168 val
= (val
& ~(0x3 << shift
)) | ((df
->muxval
>> 1) << shift
);
169 writel_relaxed(val
, gafr
);
171 val
= readl_relaxed(gpdr
);
172 val
= (val
& ~BIT(pin
% 32)) | ((df
->muxval
& 1) ? BIT(pin
% 32) : 0);
173 writel_relaxed(val
, gpdr
);
175 spin_unlock_irqrestore(&pctl
->lock
, flags
);
179 static const struct pinmux_ops pxa2xx_pinmux_ops
= {
180 .get_functions_count
= pxa2xx_get_functions_count
,
181 .get_function_name
= pxa2xx_pmx_get_func_name
,
182 .get_function_groups
= pxa2xx_pmx_get_func_groups
,
183 .set_mux
= pxa2xx_pmx_set_mux
,
184 .gpio_set_direction
= pxa2xx_pmx_gpio_set_direction
,
187 static int pxa2xx_pconf_group_get(struct pinctrl_dev
*pctldev
,
189 unsigned long *config
)
191 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
192 struct pxa_pinctrl_group
*g
= pctl
->groups
+ group
;
194 unsigned pin
= g
->pin
;
195 void __iomem
*pgsr
= pctl
->base_pgsr
[pin
/ 32];
198 spin_lock_irqsave(&pctl
->lock
, flags
);
199 val
= readl_relaxed(pgsr
) & BIT(pin
% 32);
200 *config
= val
? PIN_CONFIG_LOW_POWER_MODE
: 0;
201 spin_unlock_irqrestore(&pctl
->lock
, flags
);
203 dev_dbg(pctl
->dev
, "get sleep gpio state(pin=%d) %d\n",
208 static int pxa2xx_pconf_group_set(struct pinctrl_dev
*pctldev
,
210 unsigned long *configs
,
211 unsigned num_configs
)
213 struct pxa_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
214 struct pxa_pinctrl_group
*g
= pctl
->groups
+ group
;
216 unsigned pin
= g
->pin
;
217 void __iomem
*pgsr
= pctl
->base_pgsr
[pin
/ 32];
221 for (i
= 0; i
< num_configs
; i
++) {
222 switch (pinconf_to_config_param(configs
[i
])) {
223 case PIN_CONFIG_LOW_POWER_MODE
:
224 is_set
= pinconf_to_config_argument(configs
[i
]);
231 dev_dbg(pctl
->dev
, "set sleep gpio state(pin=%d) %d\n",
234 spin_lock_irqsave(&pctl
->lock
, flags
);
235 val
= readl_relaxed(pgsr
);
236 val
= (val
& ~BIT(pin
% 32)) | (is_set
? BIT(pin
% 32) : 0);
237 writel_relaxed(val
, pgsr
);
238 spin_unlock_irqrestore(&pctl
->lock
, flags
);
243 static const struct pinconf_ops pxa2xx_pconf_ops
= {
244 .pin_config_group_get
= pxa2xx_pconf_group_get
,
245 .pin_config_group_set
= pxa2xx_pconf_group_set
,
249 static struct pinctrl_desc pxa2xx_pinctrl_desc
= {
250 .confops
= &pxa2xx_pconf_ops
,
251 .pctlops
= &pxa2xx_pctl_ops
,
252 .pmxops
= &pxa2xx_pinmux_ops
,
255 static const struct pxa_pinctrl_function
*
256 pxa2xx_find_function(struct pxa_pinctrl
*pctl
, const char *fname
,
257 const struct pxa_pinctrl_function
*functions
)
259 const struct pxa_pinctrl_function
*func
;
261 for (func
= functions
; func
->name
; func
++)
262 if (!strcmp(fname
, func
->name
))
268 static int pxa2xx_build_functions(struct pxa_pinctrl
*pctl
)
271 struct pxa_pinctrl_function
*functions
;
272 struct pxa_desc_function
*df
;
275 * Each pin can have at most 6 alternate functions, and 2 gpio functions
276 * which are common to each pin. As there are more than 2 pins without
277 * alternate function, 6 * npins is an absolute high limit of the number
280 functions
= devm_kcalloc(pctl
->dev
, pctl
->npins
* 6,
281 sizeof(*functions
), GFP_KERNEL
);
285 for (i
= 0; i
< pctl
->npins
; i
++)
286 for (df
= pctl
->ppins
[i
].functions
; df
->name
; df
++)
287 if (!pxa2xx_find_function(pctl
, df
->name
, functions
))
288 (functions
+ pctl
->nfuncs
++)->name
= df
->name
;
289 pctl
->functions
= devm_kmemdup(pctl
->dev
, functions
,
290 pctl
->nfuncs
* sizeof(*functions
),
292 if (!pctl
->functions
)
295 devm_kfree(pctl
->dev
, functions
);
299 static int pxa2xx_build_groups(struct pxa_pinctrl
*pctl
)
302 struct pxa_pinctrl_function
*func
;
303 struct pxa_desc_function
*df
;
306 gtmp
= devm_kmalloc_array(pctl
->dev
, pctl
->npins
, sizeof(*gtmp
),
311 for (i
= 0; i
< pctl
->nfuncs
; i
++) {
313 for (j
= 0; j
< pctl
->npins
; j
++)
314 for (df
= pctl
->ppins
[j
].functions
; df
->name
;
316 if (!strcmp(pctl
->functions
[i
].name
,
318 gtmp
[ngroups
++] = (char *)
319 pctl
->ppins
[j
].pin
.name
;
320 func
= pctl
->functions
+ i
;
321 func
->ngroups
= ngroups
;
323 devm_kmalloc_array(pctl
->dev
, ngroups
,
324 sizeof(char *), GFP_KERNEL
);
328 memcpy(func
->groups
, gtmp
, ngroups
* sizeof(*gtmp
));
331 devm_kfree(pctl
->dev
, gtmp
);
335 static int pxa2xx_build_state(struct pxa_pinctrl
*pctl
,
336 const struct pxa_desc_pin
*ppins
, int npins
)
338 struct pxa_pinctrl_group
*group
;
339 struct pinctrl_pin_desc
*pins
;
344 pctl
->ngroups
= npins
;
346 pctl
->desc
.npins
= npins
;
347 pins
= devm_kcalloc(pctl
->dev
, npins
, sizeof(*pins
), GFP_KERNEL
);
351 pctl
->desc
.pins
= pins
;
352 for (i
= 0; i
< npins
; i
++)
353 pins
[i
] = ppins
[i
].pin
;
355 pctl
->groups
= devm_kmalloc_array(pctl
->dev
, pctl
->ngroups
,
356 sizeof(*pctl
->groups
), GFP_KERNEL
);
360 for (i
= 0; i
< npins
; i
++) {
361 group
= pctl
->groups
+ i
;
362 group
->name
= ppins
[i
].pin
.name
;
363 group
->pin
= ppins
[i
].pin
.number
;
366 ret
= pxa2xx_build_functions(pctl
);
370 ret
= pxa2xx_build_groups(pctl
);
377 int pxa2xx_pinctrl_init(struct platform_device
*pdev
,
378 const struct pxa_desc_pin
*ppins
, int npins
,
379 void __iomem
*base_gafr
[], void __iomem
*base_gpdr
[],
380 void __iomem
*base_pgsr
[])
382 struct pxa_pinctrl
*pctl
;
383 int ret
, i
, maxpin
= 0;
385 for (i
= 0; i
< npins
; i
++)
386 maxpin
= max_t(int, ppins
[i
].pin
.number
, maxpin
);
388 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
), GFP_KERNEL
);
391 pctl
->base_gafr
= devm_kcalloc(&pdev
->dev
, roundup(maxpin
, 16),
392 sizeof(*pctl
->base_gafr
), GFP_KERNEL
);
393 pctl
->base_gpdr
= devm_kcalloc(&pdev
->dev
, roundup(maxpin
, 32),
394 sizeof(*pctl
->base_gpdr
), GFP_KERNEL
);
395 pctl
->base_pgsr
= devm_kcalloc(&pdev
->dev
, roundup(maxpin
, 32),
396 sizeof(*pctl
->base_pgsr
), GFP_KERNEL
);
397 if (!pctl
->base_gafr
|| !pctl
->base_gpdr
|| !pctl
->base_pgsr
)
400 platform_set_drvdata(pdev
, pctl
);
401 spin_lock_init(&pctl
->lock
);
403 pctl
->dev
= &pdev
->dev
;
404 pctl
->desc
= pxa2xx_pinctrl_desc
;
405 pctl
->desc
.name
= dev_name(&pdev
->dev
);
406 pctl
->desc
.owner
= THIS_MODULE
;
408 for (i
= 0; i
< roundup(maxpin
, 16); i
+= 16)
409 pctl
->base_gafr
[i
/ 16] = base_gafr
[i
/ 16];
410 for (i
= 0; i
< roundup(maxpin
, 32); i
+= 32) {
411 pctl
->base_gpdr
[i
/ 32] = base_gpdr
[i
/ 32];
412 pctl
->base_pgsr
[i
/ 32] = base_pgsr
[i
/ 32];
415 ret
= pxa2xx_build_state(pctl
, ppins
, npins
);
419 pctl
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &pctl
->desc
, pctl
);
420 if (IS_ERR(pctl
->pctl_dev
)) {
421 dev_err(&pdev
->dev
, "couldn't register pinctrl driver\n");
422 return PTR_ERR(pctl
->pctl_dev
);
425 dev_info(&pdev
->dev
, "initialized pxa2xx pinctrl driver\n");
429 EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_init
);
431 int pxa2xx_pinctrl_exit(struct platform_device
*pdev
)
433 struct pxa_pinctrl
*pctl
= platform_get_drvdata(pdev
);
435 pinctrl_unregister(pctl
->pctl_dev
);
438 EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_exit
);
440 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
441 MODULE_DESCRIPTION("Marvell PXA2xx pinctrl driver");
442 MODULE_LICENSE("GPL v2");