Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / pinctrl / pxa / pinctrl-pxa2xx.c
blob866aa3ce1ac95a3866526b648e1e806aef5eba71
1 /*
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>
13 #include <linux/io.h>
14 #include <linux/of.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);
31 return pctl->ngroups;
34 static const char *pxa2xx_pctrl_get_group_name(struct pinctrl_dev *pctldev,
35 unsigned tgroup)
37 struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
38 struct pxa_pinctrl_group *group = pctl->groups + tgroup;
40 return group->name;
43 static int pxa2xx_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
44 unsigned tgroup,
45 const unsigned **pins,
46 unsigned *num_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;
52 *num_pins = 1;
54 return 0;
57 static const struct pinctrl_ops pxa2xx_pctl_ops = {
58 #ifdef CONFIG_OF
59 .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
60 .dt_free_map = pinctrl_utils_free_map,
61 #endif
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)
71 int i;
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))
80 return df;
83 return NULL;
86 static int pxa2xx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
87 struct pinctrl_gpio_range *range,
88 unsigned pin,
89 bool input)
91 struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
92 unsigned long flags;
93 uint32_t val;
94 void __iomem *gpdr;
96 gpdr = pctl->base_gpdr[pin / 32];
97 dev_dbg(pctl->dev, "set_direction(pin=%d): dir=%d\n",
98 pin, !input);
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);
108 return 0;
111 static const char *pxa2xx_pmx_get_func_name(struct pinctrl_dev *pctldev,
112 unsigned function)
114 struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
115 struct pxa_pinctrl_function *pf = pctl->functions + function;
117 return pf->name;
120 static int pxa2xx_get_functions_count(struct pinctrl_dev *pctldev)
122 struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
124 return pctl->nfuncs;
127 static int pxa2xx_pmx_get_func_groups(struct pinctrl_dev *pctldev,
128 unsigned function,
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;
138 return 0;
141 static int pxa2xx_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned function,
142 unsigned tgroup)
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;
147 int pin, shift;
148 unsigned long flags;
149 void __iomem *gafr, *gpdr;
150 u32 val;
153 df = pxa_desc_by_func_group(pctl, group->name,
154 (pctl->functions + function)->name);
155 if (!df)
156 return -EINVAL;
158 pin = group->pin;
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);
177 return 0;
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,
188 unsigned group,
189 unsigned long *config)
191 struct pxa_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
192 struct pxa_pinctrl_group *g = pctl->groups + group;
193 unsigned long flags;
194 unsigned pin = g->pin;
195 void __iomem *pgsr = pctl->base_pgsr[pin / 32];
196 u32 val;
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",
204 pin, !!val);
205 return 0;
208 static int pxa2xx_pconf_group_set(struct pinctrl_dev *pctldev,
209 unsigned group,
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;
215 unsigned long flags;
216 unsigned pin = g->pin;
217 void __iomem *pgsr = pctl->base_pgsr[pin / 32];
218 int i, is_set = 0;
219 u32 val;
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]);
225 break;
226 default:
227 return -EINVAL;
231 dev_dbg(pctl->dev, "set sleep gpio state(pin=%d) %d\n",
232 pin, is_set);
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);
240 return 0;
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,
246 .is_generic = true,
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))
263 return func;
265 return NULL;
268 static int pxa2xx_build_functions(struct pxa_pinctrl *pctl)
270 int i;
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
278 * of functions.
280 functions = devm_kcalloc(pctl->dev, pctl->npins * 6,
281 sizeof(*functions), GFP_KERNEL);
282 if (!functions)
283 return -ENOMEM;
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),
291 GFP_KERNEL);
292 if (!pctl->functions)
293 return -ENOMEM;
295 devm_kfree(pctl->dev, functions);
296 return 0;
299 static int pxa2xx_build_groups(struct pxa_pinctrl *pctl)
301 int i, j, ngroups;
302 struct pxa_pinctrl_function *func;
303 struct pxa_desc_function *df;
304 char **gtmp;
306 gtmp = devm_kmalloc_array(pctl->dev, pctl->npins, sizeof(*gtmp),
307 GFP_KERNEL);
308 if (!gtmp)
309 return -ENOMEM;
311 for (i = 0; i < pctl->nfuncs; i++) {
312 ngroups = 0;
313 for (j = 0; j < pctl->npins; j++)
314 for (df = pctl->ppins[j].functions; df->name;
315 df++)
316 if (!strcmp(pctl->functions[i].name,
317 df->name))
318 gtmp[ngroups++] = (char *)
319 pctl->ppins[j].pin.name;
320 func = pctl->functions + i;
321 func->ngroups = ngroups;
322 func->groups =
323 devm_kmalloc_array(pctl->dev, ngroups,
324 sizeof(char *), GFP_KERNEL);
325 if (!func->groups)
326 return -ENOMEM;
328 memcpy(func->groups, gtmp, ngroups * sizeof(*gtmp));
331 devm_kfree(pctl->dev, gtmp);
332 return 0;
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;
340 int ret, i;
342 pctl->npins = npins;
343 pctl->ppins = ppins;
344 pctl->ngroups = npins;
346 pctl->desc.npins = npins;
347 pins = devm_kcalloc(pctl->dev, npins, sizeof(*pins), GFP_KERNEL);
348 if (!pins)
349 return -ENOMEM;
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);
357 if (!pctl->groups)
358 return -ENOMEM;
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);
367 if (ret)
368 return ret;
370 ret = pxa2xx_build_groups(pctl);
371 if (ret)
372 return ret;
374 return 0;
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);
389 if (!pctl)
390 return -ENOMEM;
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)
398 return -ENOMEM;
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);
416 if (ret)
417 return ret;
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");
427 return 0;
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);
436 return 0;
438 EXPORT_SYMBOL_GPL(pxa2xx_pinctrl_exit);