2 * linux/drivers/pinctrl/pinctrl-pxa3xx.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * publishhed by the Free Software Foundation.
8 * Copyright (C) 2011, Marvell Technology Group Ltd.
10 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
14 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include "pinctrl-pxa3xx.h"
21 static struct pinctrl_gpio_range pxa3xx_pinctrl_gpio_range
= {
22 .name
= "PXA3xx GPIO",
28 static int pxa3xx_get_groups_count(struct pinctrl_dev
*pctrldev
)
30 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
32 return info
->num_grps
;
35 static const char *pxa3xx_get_group_name(struct pinctrl_dev
*pctrldev
,
38 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
40 return info
->grps
[selector
].name
;
43 static int pxa3xx_get_group_pins(struct pinctrl_dev
*pctrldev
,
45 const unsigned **pins
,
48 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
50 *pins
= info
->grps
[selector
].pins
;
51 *num_pins
= info
->grps
[selector
].npins
;
55 static struct pinctrl_ops pxa3xx_pctrl_ops
= {
56 .get_groups_count
= pxa3xx_get_groups_count
,
57 .get_group_name
= pxa3xx_get_group_name
,
58 .get_group_pins
= pxa3xx_get_group_pins
,
61 static int pxa3xx_pmx_get_funcs_count(struct pinctrl_dev
*pctrldev
)
63 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
65 return info
->num_funcs
;
68 static const char *pxa3xx_pmx_get_func_name(struct pinctrl_dev
*pctrldev
,
71 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
72 return info
->funcs
[func
].name
;
75 static int pxa3xx_pmx_get_groups(struct pinctrl_dev
*pctrldev
, unsigned func
,
76 const char * const **groups
,
77 unsigned * const num_groups
)
79 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
80 *groups
= info
->funcs
[func
].groups
;
81 *num_groups
= info
->funcs
[func
].num_groups
;
85 /* Return function number. If failure, return negative value. */
86 static int match_mux(struct pxa3xx_mfp_pin
*mfp
, unsigned mux
)
89 for (i
= 0; i
< PXA3xx_MAX_MUX
; i
++) {
90 if (mfp
->func
[i
] == mux
)
93 if (i
>= PXA3xx_MAX_MUX
)
98 /* check whether current pin configuration is valid. Negative for failure */
99 static int match_group_mux(struct pxa3xx_pin_group
*grp
,
100 struct pxa3xx_pinmux_info
*info
,
104 for (i
= 0; i
< grp
->npins
; i
++) {
106 ret
= match_mux(&info
->mfp
[pin
], mux
);
108 dev_err(info
->dev
, "Can't find mux %d on pin%d\n",
116 static int pxa3xx_pmx_enable(struct pinctrl_dev
*pctrldev
, unsigned func
,
119 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
120 struct pxa3xx_pin_group
*pin_grp
= &info
->grps
[group
];
122 int i
, mfpr
, pin
, pin_func
;
124 if (!pin_grp
->npins
||
125 (match_group_mux(pin_grp
, info
, pin_grp
->mux
) < 0)) {
126 dev_err(info
->dev
, "Failed to set the pin group: %d\n", group
);
129 for (i
= 0; i
< pin_grp
->npins
; i
++) {
130 pin
= pin_grp
->pins
[i
];
131 pin_func
= match_mux(&info
->mfp
[pin
], pin_grp
->mux
);
132 mfpr
= info
->mfp
[pin
].mfpr
;
133 data
= readl_relaxed(info
->virt_base
+ mfpr
);
134 data
&= ~MFPR_FUNC_MASK
;
136 writel_relaxed(data
, info
->virt_base
+ mfpr
);
141 static int pxa3xx_pmx_request_gpio(struct pinctrl_dev
*pctrldev
,
142 struct pinctrl_gpio_range
*range
,
145 struct pxa3xx_pinmux_info
*info
= pinctrl_dev_get_drvdata(pctrldev
);
149 pin_func
= match_mux(&info
->mfp
[pin
], PXA3xx_MUX_GPIO
);
151 dev_err(info
->dev
, "No GPIO function on pin%d (%s)\n",
152 pin
, info
->pads
[pin
].name
);
155 mfpr
= info
->mfp
[pin
].mfpr
;
156 /* write gpio function into mfpr register */
157 data
= readl_relaxed(info
->virt_base
+ mfpr
) & ~MFPR_FUNC_MASK
;
159 writel_relaxed(data
, info
->virt_base
+ mfpr
);
163 static struct pinmux_ops pxa3xx_pmx_ops
= {
164 .get_functions_count
= pxa3xx_pmx_get_funcs_count
,
165 .get_function_name
= pxa3xx_pmx_get_func_name
,
166 .get_function_groups
= pxa3xx_pmx_get_groups
,
167 .enable
= pxa3xx_pmx_enable
,
168 .gpio_request_enable
= pxa3xx_pmx_request_gpio
,
171 int pxa3xx_pinctrl_register(struct platform_device
*pdev
,
172 struct pxa3xx_pinmux_info
*info
)
174 struct pinctrl_desc
*desc
;
175 struct resource
*res
;
177 if (!info
|| !info
->cputype
)
180 desc
->pins
= info
->pads
;
181 desc
->npins
= info
->num_pads
;
182 desc
->pctlops
= &pxa3xx_pctrl_ops
;
183 desc
->pmxops
= &pxa3xx_pmx_ops
;
184 info
->dev
= &pdev
->dev
;
185 pxa3xx_pinctrl_gpio_range
.npins
= info
->num_gpio
;
187 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
190 info
->virt_base
= devm_request_and_ioremap(&pdev
->dev
, res
);
191 if (!info
->virt_base
)
193 info
->pctrl
= pinctrl_register(desc
, &pdev
->dev
, info
);
195 dev_err(&pdev
->dev
, "failed to register PXA pinmux driver\n");
198 pinctrl_add_gpio_range(info
->pctrl
, &pxa3xx_pinctrl_gpio_range
);
199 platform_set_drvdata(pdev
, info
);
203 int pxa3xx_pinctrl_unregister(struct platform_device
*pdev
)
205 struct pxa3xx_pinmux_info
*info
= platform_get_drvdata(pdev
);
207 pinctrl_unregister(info
->pctrl
);
208 platform_set_drvdata(pdev
, NULL
);
212 static int __init
pxa3xx_pinctrl_init(void)
214 pr_info("pxa3xx-pinctrl: PXA3xx pinctrl driver initializing\n");
217 core_initcall_sync(pxa3xx_pinctrl_init
);
219 static void __exit
pxa3xx_pinctrl_exit(void)
222 module_exit(pxa3xx_pinctrl_exit
);
224 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
225 MODULE_DESCRIPTION("PXA3xx pin control driver");
226 MODULE_LICENSE("GPL v2");