xref: /linux/drivers/pinctrl/pxa/pinctrl-pxa2xx.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
273317712SRobert Jarzmik /*
373317712SRobert Jarzmik  * Marvell PXA2xx family pin control
473317712SRobert Jarzmik  *
573317712SRobert Jarzmik  * Copyright (C) 2015 Robert Jarzmik
673317712SRobert Jarzmik  */
773317712SRobert Jarzmik 
873317712SRobert Jarzmik #include <linux/bitops.h>
973317712SRobert Jarzmik #include <linux/io.h>
1073317712SRobert Jarzmik #include <linux/of.h>
1173317712SRobert Jarzmik #include <linux/of_address.h>
1273317712SRobert Jarzmik #include <linux/module.h>
139a8c8b43SRandy Dunlap #include <linux/pinctrl/machine.h>
1473317712SRobert Jarzmik #include <linux/pinctrl/pinconf.h>
1573317712SRobert Jarzmik #include <linux/pinctrl/pinconf-generic.h>
1673317712SRobert Jarzmik #include <linux/pinctrl/pinmux.h>
1773317712SRobert Jarzmik #include <linux/pinctrl/pinctrl.h>
1873317712SRobert Jarzmik #include <linux/platform_device.h>
1973317712SRobert Jarzmik #include <linux/slab.h>
2073317712SRobert Jarzmik 
2173317712SRobert Jarzmik #include "../pinctrl-utils.h"
2273317712SRobert Jarzmik #include "pinctrl-pxa2xx.h"
2373317712SRobert Jarzmik 
2473317712SRobert Jarzmik static int pxa2xx_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
2573317712SRobert Jarzmik {
2673317712SRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
2773317712SRobert Jarzmik 
2873317712SRobert Jarzmik 	return pctl->ngroups;
2973317712SRobert Jarzmik }
3073317712SRobert Jarzmik 
3173317712SRobert Jarzmik static const char *pxa2xx_pctrl_get_group_name(struct pinctrl_dev *pctldev,
3273317712SRobert Jarzmik 					       unsigned tgroup)
3373317712SRobert Jarzmik {
3473317712SRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
35*76c22f09SAndy Shevchenko 	struct pingroup *group = pctl->groups + tgroup;
3673317712SRobert Jarzmik 
3773317712SRobert Jarzmik 	return group->name;
3873317712SRobert Jarzmik }
3973317712SRobert Jarzmik 
4073317712SRobert Jarzmik static int pxa2xx_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
4173317712SRobert Jarzmik 				       unsigned tgroup,
4273317712SRobert Jarzmik 				       const unsigned **pins,
4373317712SRobert Jarzmik 				       unsigned *num_pins)
4473317712SRobert Jarzmik {
4573317712SRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
46*76c22f09SAndy Shevchenko 	struct pingroup *group = pctl->groups + tgroup;
4773317712SRobert Jarzmik 
48*76c22f09SAndy Shevchenko 	*pins = group->pins;
49*76c22f09SAndy Shevchenko 	*num_pins = group->npins;
5073317712SRobert Jarzmik 
5173317712SRobert Jarzmik 	return 0;
5273317712SRobert Jarzmik }
5373317712SRobert Jarzmik 
5473317712SRobert Jarzmik static const struct pinctrl_ops pxa2xx_pctl_ops = {
5573317712SRobert Jarzmik #ifdef CONFIG_OF
5673317712SRobert Jarzmik 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_all,
57d32f7fd3SIrina Tirdea 	.dt_free_map		= pinctrl_utils_free_map,
5873317712SRobert Jarzmik #endif
5973317712SRobert Jarzmik 	.get_groups_count	= pxa2xx_pctrl_get_groups_count,
6073317712SRobert Jarzmik 	.get_group_name		= pxa2xx_pctrl_get_group_name,
6173317712SRobert Jarzmik 	.get_group_pins		= pxa2xx_pctrl_get_group_pins,
6273317712SRobert Jarzmik };
6373317712SRobert Jarzmik 
64d530ef9bSRobert Jarzmik static struct pxa_desc_function *
65d530ef9bSRobert Jarzmik pxa_desc_by_func_group(struct pxa_pinctrl *pctl, const char *pin_name,
66d530ef9bSRobert Jarzmik 		       const char *func_name)
67d530ef9bSRobert Jarzmik {
68d530ef9bSRobert Jarzmik 	int i;
69d530ef9bSRobert Jarzmik 	struct pxa_desc_function *df;
70d530ef9bSRobert Jarzmik 
71d530ef9bSRobert Jarzmik 	for (i = 0; i < pctl->npins; i++) {
72d530ef9bSRobert Jarzmik 		const struct pxa_desc_pin *pin = pctl->ppins + i;
73d530ef9bSRobert Jarzmik 
74d530ef9bSRobert Jarzmik 		if (!strcmp(pin->pin.name, pin_name))
75d530ef9bSRobert Jarzmik 			for (df = pin->functions; df->name; df++)
76d530ef9bSRobert Jarzmik 				if (!strcmp(df->name, func_name))
77d530ef9bSRobert Jarzmik 					return df;
78d530ef9bSRobert Jarzmik 	}
79d530ef9bSRobert Jarzmik 
80d530ef9bSRobert Jarzmik 	return NULL;
81d530ef9bSRobert Jarzmik }
82d530ef9bSRobert Jarzmik 
83d530ef9bSRobert Jarzmik static int pxa2xx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
84d530ef9bSRobert Jarzmik 					 struct pinctrl_gpio_range *range,
85d530ef9bSRobert Jarzmik 					 unsigned pin,
86d530ef9bSRobert Jarzmik 					 bool input)
87d530ef9bSRobert Jarzmik {
88d530ef9bSRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
89d530ef9bSRobert Jarzmik 	unsigned long flags;
90d530ef9bSRobert Jarzmik 	uint32_t val;
91d530ef9bSRobert Jarzmik 	void __iomem *gpdr;
92d530ef9bSRobert Jarzmik 
93d530ef9bSRobert Jarzmik 	gpdr = pctl->base_gpdr[pin / 32];
94d530ef9bSRobert Jarzmik 	dev_dbg(pctl->dev, "set_direction(pin=%d): dir=%d\n",
95d530ef9bSRobert Jarzmik 		pin, !input);
96d530ef9bSRobert Jarzmik 
97d530ef9bSRobert Jarzmik 	spin_lock_irqsave(&pctl->lock, flags);
98d530ef9bSRobert Jarzmik 
99d530ef9bSRobert Jarzmik 	val = readl_relaxed(gpdr);
100d530ef9bSRobert Jarzmik 	val = (val & ~BIT(pin % 32)) | (input ? 0 : BIT(pin % 32));
101d530ef9bSRobert Jarzmik 	writel_relaxed(val, gpdr);
102d530ef9bSRobert Jarzmik 
103d530ef9bSRobert Jarzmik 	spin_unlock_irqrestore(&pctl->lock, flags);
104d530ef9bSRobert Jarzmik 
105d530ef9bSRobert Jarzmik 	return 0;
106d530ef9bSRobert Jarzmik }
107d530ef9bSRobert Jarzmik 
108d530ef9bSRobert Jarzmik static const char *pxa2xx_pmx_get_func_name(struct pinctrl_dev *pctldev,
109d530ef9bSRobert Jarzmik 					    unsigned function)
110d530ef9bSRobert Jarzmik {
111d530ef9bSRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
112ea217fefSAndy Shevchenko 	struct pinfunction *pf = pctl->functions + function;
113d530ef9bSRobert Jarzmik 
114d530ef9bSRobert Jarzmik 	return pf->name;
115d530ef9bSRobert Jarzmik }
116d530ef9bSRobert Jarzmik 
117d530ef9bSRobert Jarzmik static int pxa2xx_get_functions_count(struct pinctrl_dev *pctldev)
118d530ef9bSRobert Jarzmik {
119d530ef9bSRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
120d530ef9bSRobert Jarzmik 
121d530ef9bSRobert Jarzmik 	return pctl->nfuncs;
122d530ef9bSRobert Jarzmik }
123d530ef9bSRobert Jarzmik 
124d530ef9bSRobert Jarzmik static int pxa2xx_pmx_get_func_groups(struct pinctrl_dev *pctldev,
125d530ef9bSRobert Jarzmik 				      unsigned function,
126d530ef9bSRobert Jarzmik 				      const char * const **groups,
127d530ef9bSRobert Jarzmik 				      unsigned * const num_groups)
128d530ef9bSRobert Jarzmik {
129d530ef9bSRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
130ea217fefSAndy Shevchenko 	struct pinfunction *pf = pctl->functions + function;
131d530ef9bSRobert Jarzmik 
132d530ef9bSRobert Jarzmik 	*groups = pf->groups;
133d530ef9bSRobert Jarzmik 	*num_groups = pf->ngroups;
134d530ef9bSRobert Jarzmik 
135d530ef9bSRobert Jarzmik 	return 0;
136d530ef9bSRobert Jarzmik }
137d530ef9bSRobert Jarzmik 
138d530ef9bSRobert Jarzmik static int pxa2xx_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned function,
139d530ef9bSRobert Jarzmik 			      unsigned tgroup)
140d530ef9bSRobert Jarzmik {
141d530ef9bSRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
142*76c22f09SAndy Shevchenko 	struct pingroup *g = pctl->groups + tgroup;
143*76c22f09SAndy Shevchenko 	unsigned int pin = g->pins[0];
144d530ef9bSRobert Jarzmik 	struct pxa_desc_function *df;
145d530ef9bSRobert Jarzmik 	unsigned long flags;
146d530ef9bSRobert Jarzmik 	void __iomem *gafr, *gpdr;
147*76c22f09SAndy Shevchenko 	int shift;
148d530ef9bSRobert Jarzmik 	u32 val;
149d530ef9bSRobert Jarzmik 
150*76c22f09SAndy Shevchenko 	df = pxa_desc_by_func_group(pctl, g->name, (pctl->functions + function)->name);
151d530ef9bSRobert Jarzmik 	if (!df)
152d530ef9bSRobert Jarzmik 		return -EINVAL;
153d530ef9bSRobert Jarzmik 
154d530ef9bSRobert Jarzmik 	gafr = pctl->base_gafr[pin / 16];
155d530ef9bSRobert Jarzmik 	gpdr = pctl->base_gpdr[pin / 32];
156d530ef9bSRobert Jarzmik 	shift = (pin % 16) << 1;
157d530ef9bSRobert Jarzmik 	dev_dbg(pctl->dev, "set_mux(pin=%d): af=%d dir=%d\n",
158d530ef9bSRobert Jarzmik 		pin, df->muxval >> 1, df->muxval & 0x1);
159d530ef9bSRobert Jarzmik 
160d530ef9bSRobert Jarzmik 	spin_lock_irqsave(&pctl->lock, flags);
161d530ef9bSRobert Jarzmik 
162d530ef9bSRobert Jarzmik 	val = readl_relaxed(gafr);
163d530ef9bSRobert Jarzmik 	val = (val & ~(0x3 << shift)) | ((df->muxval >> 1) << shift);
164d530ef9bSRobert Jarzmik 	writel_relaxed(val, gafr);
165d530ef9bSRobert Jarzmik 
166d530ef9bSRobert Jarzmik 	val = readl_relaxed(gpdr);
167d530ef9bSRobert Jarzmik 	val = (val & ~BIT(pin % 32)) | ((df->muxval & 1) ? BIT(pin % 32) : 0);
168d530ef9bSRobert Jarzmik 	writel_relaxed(val, gpdr);
169d530ef9bSRobert Jarzmik 
170d530ef9bSRobert Jarzmik 	spin_unlock_irqrestore(&pctl->lock, flags);
171d530ef9bSRobert Jarzmik 
172d530ef9bSRobert Jarzmik 	return 0;
173d530ef9bSRobert Jarzmik }
174d530ef9bSRobert Jarzmik static const struct pinmux_ops pxa2xx_pinmux_ops = {
175d530ef9bSRobert Jarzmik 	.get_functions_count = pxa2xx_get_functions_count,
176d530ef9bSRobert Jarzmik 	.get_function_name = pxa2xx_pmx_get_func_name,
177d530ef9bSRobert Jarzmik 	.get_function_groups = pxa2xx_pmx_get_func_groups,
178d530ef9bSRobert Jarzmik 	.set_mux = pxa2xx_pmx_set_mux,
179d530ef9bSRobert Jarzmik 	.gpio_set_direction = pxa2xx_pmx_gpio_set_direction,
180d530ef9bSRobert Jarzmik };
181d530ef9bSRobert Jarzmik 
182aedf08b6SRobert Jarzmik static int pxa2xx_pconf_group_get(struct pinctrl_dev *pctldev,
183aedf08b6SRobert Jarzmik 				  unsigned group,
184aedf08b6SRobert Jarzmik 				  unsigned long *config)
185aedf08b6SRobert Jarzmik {
186aedf08b6SRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
187*76c22f09SAndy Shevchenko 	struct pingroup *g = pctl->groups + group;
188*76c22f09SAndy Shevchenko 	unsigned int pin = g->pins[0];
189aedf08b6SRobert Jarzmik 	unsigned long flags;
190aedf08b6SRobert Jarzmik 	void __iomem *pgsr = pctl->base_pgsr[pin / 32];
191aedf08b6SRobert Jarzmik 	u32 val;
192aedf08b6SRobert Jarzmik 
193aedf08b6SRobert Jarzmik 	spin_lock_irqsave(&pctl->lock, flags);
194aedf08b6SRobert Jarzmik 	val = readl_relaxed(pgsr) & BIT(pin % 32);
19531f9a421SAndy Shevchenko 	*config = val ? PIN_CONFIG_MODE_LOW_POWER : 0;
196aedf08b6SRobert Jarzmik 	spin_unlock_irqrestore(&pctl->lock, flags);
197aedf08b6SRobert Jarzmik 
198aedf08b6SRobert Jarzmik 	dev_dbg(pctl->dev, "get sleep gpio state(pin=%d) %d\n",
199aedf08b6SRobert Jarzmik 		pin, !!val);
200aedf08b6SRobert Jarzmik 	return 0;
201aedf08b6SRobert Jarzmik }
202aedf08b6SRobert Jarzmik 
203aedf08b6SRobert Jarzmik static int pxa2xx_pconf_group_set(struct pinctrl_dev *pctldev,
204aedf08b6SRobert Jarzmik 				  unsigned group,
205aedf08b6SRobert Jarzmik 				  unsigned long *configs,
206aedf08b6SRobert Jarzmik 				  unsigned num_configs)
207aedf08b6SRobert Jarzmik {
208aedf08b6SRobert Jarzmik 	struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
209*76c22f09SAndy Shevchenko 	struct pingroup *g = pctl->groups + group;
210*76c22f09SAndy Shevchenko 	unsigned int pin = g->pins[0];
211aedf08b6SRobert Jarzmik 	unsigned long flags;
212aedf08b6SRobert Jarzmik 	void __iomem *pgsr = pctl->base_pgsr[pin / 32];
213aedf08b6SRobert Jarzmik 	int i, is_set = 0;
214aedf08b6SRobert Jarzmik 	u32 val;
215aedf08b6SRobert Jarzmik 
216aedf08b6SRobert Jarzmik 	for (i = 0; i < num_configs; i++) {
217aedf08b6SRobert Jarzmik 		switch (pinconf_to_config_param(configs[i])) {
21831f9a421SAndy Shevchenko 		case PIN_CONFIG_MODE_LOW_POWER:
219aedf08b6SRobert Jarzmik 			is_set = pinconf_to_config_argument(configs[i]);
220aedf08b6SRobert Jarzmik 			break;
221aedf08b6SRobert Jarzmik 		default:
222aedf08b6SRobert Jarzmik 			return -EINVAL;
223aedf08b6SRobert Jarzmik 		}
224aedf08b6SRobert Jarzmik 	}
225aedf08b6SRobert Jarzmik 
226aedf08b6SRobert Jarzmik 	dev_dbg(pctl->dev, "set sleep gpio state(pin=%d) %d\n",
227aedf08b6SRobert Jarzmik 		pin, is_set);
228aedf08b6SRobert Jarzmik 
229aedf08b6SRobert Jarzmik 	spin_lock_irqsave(&pctl->lock, flags);
230aedf08b6SRobert Jarzmik 	val = readl_relaxed(pgsr);
231aedf08b6SRobert Jarzmik 	val = (val & ~BIT(pin % 32)) | (is_set ? BIT(pin % 32) : 0);
232aedf08b6SRobert Jarzmik 	writel_relaxed(val, pgsr);
233aedf08b6SRobert Jarzmik 	spin_unlock_irqrestore(&pctl->lock, flags);
234aedf08b6SRobert Jarzmik 
235aedf08b6SRobert Jarzmik 	return 0;
236aedf08b6SRobert Jarzmik }
237aedf08b6SRobert Jarzmik 
238aedf08b6SRobert Jarzmik static const struct pinconf_ops pxa2xx_pconf_ops = {
239aedf08b6SRobert Jarzmik 	.pin_config_group_get	= pxa2xx_pconf_group_get,
240aedf08b6SRobert Jarzmik 	.pin_config_group_set	= pxa2xx_pconf_group_set,
241aedf08b6SRobert Jarzmik 	.is_generic		= true,
242aedf08b6SRobert Jarzmik };
243aedf08b6SRobert Jarzmik 
24473317712SRobert Jarzmik static struct pinctrl_desc pxa2xx_pinctrl_desc = {
245aedf08b6SRobert Jarzmik 	.confops	= &pxa2xx_pconf_ops,
24673317712SRobert Jarzmik 	.pctlops	= &pxa2xx_pctl_ops,
247d530ef9bSRobert Jarzmik 	.pmxops		= &pxa2xx_pinmux_ops,
24873317712SRobert Jarzmik };
24973317712SRobert Jarzmik 
250ea217fefSAndy Shevchenko static const struct pinfunction *pxa2xx_find_function(struct pxa_pinctrl *pctl,
251ea217fefSAndy Shevchenko 						      const char *fname,
252ea217fefSAndy Shevchenko 						      const struct pinfunction *functions)
25373317712SRobert Jarzmik {
254ea217fefSAndy Shevchenko 	const struct pinfunction *func;
25573317712SRobert Jarzmik 
25673317712SRobert Jarzmik 	for (func = functions; func->name; func++)
25773317712SRobert Jarzmik 		if (!strcmp(fname, func->name))
25873317712SRobert Jarzmik 			return func;
25973317712SRobert Jarzmik 
26073317712SRobert Jarzmik 	return NULL;
26173317712SRobert Jarzmik }
26273317712SRobert Jarzmik 
26373317712SRobert Jarzmik static int pxa2xx_build_functions(struct pxa_pinctrl *pctl)
26473317712SRobert Jarzmik {
265ea217fefSAndy Shevchenko 	struct pinfunction *functions;
26673317712SRobert Jarzmik 	int i;
26773317712SRobert Jarzmik 	struct pxa_desc_function *df;
26873317712SRobert Jarzmik 
26973317712SRobert Jarzmik 	/*
27073317712SRobert Jarzmik 	 * Each pin can have at most 6 alternate functions, and 2 gpio functions
27173317712SRobert Jarzmik 	 * which are common to each pin. As there are more than 2 pins without
27273317712SRobert Jarzmik 	 * alternate function, 6 * npins is an absolute high limit of the number
27373317712SRobert Jarzmik 	 * of functions.
27473317712SRobert Jarzmik 	 */
27573317712SRobert Jarzmik 	functions = devm_kcalloc(pctl->dev, pctl->npins * 6,
27673317712SRobert Jarzmik 				 sizeof(*functions), GFP_KERNEL);
27773317712SRobert Jarzmik 	if (!functions)
27873317712SRobert Jarzmik 		return -ENOMEM;
27973317712SRobert Jarzmik 
28073317712SRobert Jarzmik 	for (i = 0; i < pctl->npins; i++)
28173317712SRobert Jarzmik 		for (df = pctl->ppins[i].functions; df->name; df++)
28273317712SRobert Jarzmik 			if (!pxa2xx_find_function(pctl, df->name, functions))
28373317712SRobert Jarzmik 				(functions + pctl->nfuncs++)->name = df->name;
28473317712SRobert Jarzmik 	pctl->functions = devm_kmemdup(pctl->dev, functions,
28573317712SRobert Jarzmik 				       pctl->nfuncs * sizeof(*functions),
28673317712SRobert Jarzmik 				       GFP_KERNEL);
28773317712SRobert Jarzmik 	if (!pctl->functions)
28873317712SRobert Jarzmik 		return -ENOMEM;
28973317712SRobert Jarzmik 
290e670b298SRobert Jarzmik 	devm_kfree(pctl->dev, functions);
29173317712SRobert Jarzmik 	return 0;
29273317712SRobert Jarzmik }
29373317712SRobert Jarzmik 
29473317712SRobert Jarzmik static int pxa2xx_build_groups(struct pxa_pinctrl *pctl)
29573317712SRobert Jarzmik {
29673317712SRobert Jarzmik 	int i, j, ngroups;
29773317712SRobert Jarzmik 	struct pxa_desc_function *df;
298ea217fefSAndy Shevchenko 	struct pinfunction *func;
299ea217fefSAndy Shevchenko 	const char **gtmp;
30073317712SRobert Jarzmik 
30173317712SRobert Jarzmik 	gtmp = devm_kmalloc_array(pctl->dev, pctl->npins, sizeof(*gtmp),
30273317712SRobert Jarzmik 				  GFP_KERNEL);
30373317712SRobert Jarzmik 	if (!gtmp)
30473317712SRobert Jarzmik 		return -ENOMEM;
30573317712SRobert Jarzmik 
30673317712SRobert Jarzmik 	for (i = 0; i < pctl->nfuncs; i++) {
30773317712SRobert Jarzmik 		ngroups = 0;
30873317712SRobert Jarzmik 		for (j = 0; j < pctl->npins; j++)
30973317712SRobert Jarzmik 			for (df = pctl->ppins[j].functions; df->name;
31073317712SRobert Jarzmik 			     df++)
31173317712SRobert Jarzmik 				if (!strcmp(pctl->functions[i].name,
31273317712SRobert Jarzmik 					    df->name))
31373317712SRobert Jarzmik 					gtmp[ngroups++] = (char *)
31473317712SRobert Jarzmik 						pctl->ppins[j].pin.name;
31573317712SRobert Jarzmik 		func = pctl->functions + i;
31673317712SRobert Jarzmik 		func->ngroups = ngroups;
317ea217fefSAndy Shevchenko 		func->groups = devm_kmemdup(pctl->dev, gtmp, ngroups * sizeof(*gtmp), GFP_KERNEL);
31873317712SRobert Jarzmik 		if (!func->groups)
31973317712SRobert Jarzmik 			return -ENOMEM;
32073317712SRobert Jarzmik 	}
32173317712SRobert Jarzmik 
322e670b298SRobert Jarzmik 	devm_kfree(pctl->dev, gtmp);
32373317712SRobert Jarzmik 	return 0;
32473317712SRobert Jarzmik }
32573317712SRobert Jarzmik 
32673317712SRobert Jarzmik static int pxa2xx_build_state(struct pxa_pinctrl *pctl,
32773317712SRobert Jarzmik 			      const struct pxa_desc_pin *ppins, int npins)
32873317712SRobert Jarzmik {
32973317712SRobert Jarzmik 	struct pinctrl_pin_desc *pins;
330*76c22f09SAndy Shevchenko 	struct pingroup *group;
33173317712SRobert Jarzmik 	int ret, i;
33273317712SRobert Jarzmik 
33373317712SRobert Jarzmik 	pctl->npins = npins;
33473317712SRobert Jarzmik 	pctl->ppins = ppins;
33573317712SRobert Jarzmik 	pctl->ngroups = npins;
33673317712SRobert Jarzmik 
33773317712SRobert Jarzmik 	pctl->desc.npins = npins;
33873317712SRobert Jarzmik 	pins = devm_kcalloc(pctl->dev, npins, sizeof(*pins), GFP_KERNEL);
33973317712SRobert Jarzmik 	if (!pins)
34073317712SRobert Jarzmik 		return -ENOMEM;
34173317712SRobert Jarzmik 
34273317712SRobert Jarzmik 	pctl->desc.pins = pins;
34373317712SRobert Jarzmik 	for (i = 0; i < npins; i++)
34473317712SRobert Jarzmik 		pins[i] = ppins[i].pin;
34573317712SRobert Jarzmik 
34673317712SRobert Jarzmik 	pctl->groups = devm_kmalloc_array(pctl->dev, pctl->ngroups,
34773317712SRobert Jarzmik 					  sizeof(*pctl->groups), GFP_KERNEL);
34873317712SRobert Jarzmik 	if (!pctl->groups)
34973317712SRobert Jarzmik 		return -ENOMEM;
35073317712SRobert Jarzmik 
35173317712SRobert Jarzmik 	for (i = 0; i < npins; i++) {
35273317712SRobert Jarzmik 		group = pctl->groups + i;
35373317712SRobert Jarzmik 		group->name = ppins[i].pin.name;
354*76c22f09SAndy Shevchenko 		group->pins = &ppins[i].pin.number;
355*76c22f09SAndy Shevchenko 		group->npins = 1;
35673317712SRobert Jarzmik 	}
35773317712SRobert Jarzmik 
35873317712SRobert Jarzmik 	ret = pxa2xx_build_functions(pctl);
35973317712SRobert Jarzmik 	if (ret)
36073317712SRobert Jarzmik 		return ret;
36173317712SRobert Jarzmik 
36273317712SRobert Jarzmik 	ret = pxa2xx_build_groups(pctl);
36373317712SRobert Jarzmik 	if (ret)
36473317712SRobert Jarzmik 		return ret;
36573317712SRobert Jarzmik 
36673317712SRobert Jarzmik 	return 0;
36773317712SRobert Jarzmik }
36873317712SRobert Jarzmik 
36973317712SRobert Jarzmik int pxa2xx_pinctrl_init(struct platform_device *pdev,
37073317712SRobert Jarzmik 			const struct pxa_desc_pin *ppins, int npins,
37173317712SRobert Jarzmik 			void __iomem *base_gafr[], void __iomem *base_gpdr[],
37273317712SRobert Jarzmik 			void __iomem *base_pgsr[])
37373317712SRobert Jarzmik {
37473317712SRobert Jarzmik 	struct pxa_pinctrl *pctl;
37573317712SRobert Jarzmik 	int ret, i, maxpin = 0;
37673317712SRobert Jarzmik 
37773317712SRobert Jarzmik 	for (i = 0; i < npins; i++)
37873317712SRobert Jarzmik 		maxpin = max_t(int, ppins[i].pin.number, maxpin);
37973317712SRobert Jarzmik 
38073317712SRobert Jarzmik 	pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
38173317712SRobert Jarzmik 	if (!pctl)
38273317712SRobert Jarzmik 		return -ENOMEM;
38373317712SRobert Jarzmik 	pctl->base_gafr = devm_kcalloc(&pdev->dev, roundup(maxpin, 16),
38473317712SRobert Jarzmik 				       sizeof(*pctl->base_gafr), GFP_KERNEL);
38573317712SRobert Jarzmik 	pctl->base_gpdr = devm_kcalloc(&pdev->dev, roundup(maxpin, 32),
38673317712SRobert Jarzmik 				       sizeof(*pctl->base_gpdr), GFP_KERNEL);
38773317712SRobert Jarzmik 	pctl->base_pgsr = devm_kcalloc(&pdev->dev, roundup(maxpin, 32),
38873317712SRobert Jarzmik 				       sizeof(*pctl->base_pgsr), GFP_KERNEL);
38973317712SRobert Jarzmik 	if (!pctl->base_gafr || !pctl->base_gpdr || !pctl->base_pgsr)
39073317712SRobert Jarzmik 		return -ENOMEM;
39173317712SRobert Jarzmik 
39273317712SRobert Jarzmik 	platform_set_drvdata(pdev, pctl);
39373317712SRobert Jarzmik 	spin_lock_init(&pctl->lock);
39473317712SRobert Jarzmik 
39573317712SRobert Jarzmik 	pctl->dev = &pdev->dev;
39673317712SRobert Jarzmik 	pctl->desc = pxa2xx_pinctrl_desc;
39773317712SRobert Jarzmik 	pctl->desc.name = dev_name(&pdev->dev);
39873317712SRobert Jarzmik 	pctl->desc.owner = THIS_MODULE;
39973317712SRobert Jarzmik 
40073317712SRobert Jarzmik 	for (i = 0; i < roundup(maxpin, 16); i += 16)
40173317712SRobert Jarzmik 		pctl->base_gafr[i / 16] = base_gafr[i / 16];
40273317712SRobert Jarzmik 	for (i = 0; i < roundup(maxpin, 32); i += 32) {
40373317712SRobert Jarzmik 		pctl->base_gpdr[i / 32] = base_gpdr[i / 32];
40473317712SRobert Jarzmik 		pctl->base_pgsr[i / 32] = base_pgsr[i / 32];
40573317712SRobert Jarzmik 	}
40673317712SRobert Jarzmik 
40773317712SRobert Jarzmik 	ret = pxa2xx_build_state(pctl, ppins, npins);
40873317712SRobert Jarzmik 	if (ret)
40973317712SRobert Jarzmik 		return ret;
41073317712SRobert Jarzmik 
4116d33ee7aSLaxman Dewangan 	pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->desc, pctl);
41273317712SRobert Jarzmik 	if (IS_ERR(pctl->pctl_dev)) {
41373317712SRobert Jarzmik 		dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
41473317712SRobert Jarzmik 		return PTR_ERR(pctl->pctl_dev);
41573317712SRobert Jarzmik 	}
41673317712SRobert Jarzmik 
41773317712SRobert Jarzmik 	dev_info(&pdev->dev, "initialized pxa2xx pinctrl driver\n");
41873317712SRobert Jarzmik 
41973317712SRobert Jarzmik 	return 0;
42073317712SRobert Jarzmik }
421cc2a73a4SLinus Walleij EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_init);
42273317712SRobert Jarzmik 
4230b9335cbSJesse Chan MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
4240b9335cbSJesse Chan MODULE_DESCRIPTION("Marvell PXA2xx pinctrl driver");
4250b9335cbSJesse Chan MODULE_LICENSE("GPL v2");
426