]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/pinctrl/aspeed/pinctrl-aspeed.c
Merge remote-tracking branches 'asoc/topic/dpcm', 'asoc/topic/es8328', 'asoc/topic...
[mirror_ubuntu-zesty-kernel.git] / drivers / pinctrl / aspeed / pinctrl-aspeed.c
CommitLineData
4d3d0e42
AJ
1/*
2 * Copyright (C) 2016 IBM Corp.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/mfd/syscon.h>
11#include <linux/platform_device.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14#include "../core.h"
15#include "pinctrl-aspeed.h"
16
17int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
18{
19 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
20
21 return pdata->ngroups;
22}
23
24const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
25 unsigned int group)
26{
27 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
28
29 return pdata->groups[group].name;
30}
31
32int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
33 unsigned int group, const unsigned int **pins,
34 unsigned int *npins)
35{
36 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
37
38 *pins = &pdata->groups[group].pins[0];
39 *npins = pdata->groups[group].npins;
40
41 return 0;
42}
43
44void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
45 struct seq_file *s, unsigned int offset)
46{
47 seq_printf(s, " %s", dev_name(pctldev->dev));
48}
49
50int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev)
51{
52 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
53
54 return pdata->nfunctions;
55}
56
57const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev,
58 unsigned int function)
59{
60 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
61
62 return pdata->functions[function].name;
63}
64
65int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev,
66 unsigned int function,
67 const char * const **groups,
68 unsigned int * const num_groups)
69{
70 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev);
71
72 *groups = pdata->functions[function].groups;
73 *num_groups = pdata->functions[function].ngroups;
74
75 return 0;
76}
77
78static inline void aspeed_sig_desc_print_val(
79 const struct aspeed_sig_desc *desc, bool enable, u32 rv)
80{
81 pr_debug("SCU%x[0x%08x]=0x%x, got 0x%x from 0x%08x\n", desc->reg,
82 desc->mask, enable ? desc->enable : desc->disable,
83 (rv & desc->mask) >> __ffs(desc->mask), rv);
84}
85
86/**
87 * Query the enabled or disabled state of a signal descriptor
88 *
89 * @desc: The signal descriptor of interest
90 * @enabled: True to query the enabled state, false to query disabled state
91 * @regmap: The SCU regmap instance
92 *
93 * @return True if the descriptor's bitfield is configured to the state
94 * selected by @enabled, false otherwise
95 *
96 * Evaluation of descriptor state is non-trivial in that it is not a binary
97 * outcome: The bitfields can be greater than one bit in size and thus can take
98 * a value that is neither the enabled nor disabled state recorded in the
99 * descriptor (typically this means a different function to the one of interest
100 * is enabled). Thus we must explicitly test for either condition as required.
101 */
102static bool aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc,
103 bool enabled, struct regmap *map)
104{
105 unsigned int raw;
106 u32 want;
107
108 if (regmap_read(map, desc->reg, &raw) < 0)
109 return false;
110
111 aspeed_sig_desc_print_val(desc, enabled, raw);
112 want = enabled ? desc->enable : desc->disable;
113
114 return ((raw & desc->mask) >> __ffs(desc->mask)) == want;
115}
116
117/**
118 * Query the enabled or disabled state for a mux function's signal on a pin
119 *
120 * @expr: An expression controlling the signal for a mux function on a pin
121 * @enabled: True to query the enabled state, false to query disabled state
122 * @regmap: The SCU regmap instance
123 *
124 * @return True if the expression composed by @enabled evaluates true, false
125 * otherwise
126 *
127 * A mux function is enabled or disabled if the function's signal expression
128 * for each pin in the function's pin group evaluates true for the desired
129 * state. An signal expression evaluates true if all of its associated signal
130 * descriptors evaluate true for the desired state.
131 *
132 * If an expression's state is described by more than one bit, either through
133 * multi-bit bitfields in a single signal descriptor or through multiple signal
134 * descriptors of a single bit then it is possible for the expression to be in
135 * neither the enabled nor disabled state. Thus we must explicitly test for
136 * either condition as required.
137 */
138static bool aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr,
139 bool enabled, struct regmap *map)
140{
141 int i;
142
143 for (i = 0; i < expr->ndescs; i++) {
144 const struct aspeed_sig_desc *desc = &expr->descs[i];
145
146 if (!aspeed_sig_desc_eval(desc, enabled, map))
147 return false;
148 }
149
150 return true;
151}
152
153/**
154 * Configure a pin's signal by applying an expression's descriptor state for
155 * all descriptors in the expression.
156 *
157 * @expr: The expression associated with the function whose signal is to be
158 * configured
159 * @enable: true to enable an function's signal through a pin's signal
160 * expression, false to disable the function's signal
161 * @map: The SCU's regmap instance for pinmux register access.
162 *
163 * @return true if the expression is configured as requested, false otherwise
164 */
165static bool aspeed_sig_expr_set(const struct aspeed_sig_expr *expr,
166 bool enable, struct regmap *map)
167{
168 int i;
4d3d0e42
AJ
169
170 for (i = 0; i < expr->ndescs; i++) {
5366f146 171 bool ret;
4d3d0e42
AJ
172 const struct aspeed_sig_desc *desc = &expr->descs[i];
173 u32 pattern = enable ? desc->enable : desc->disable;
174
175 /*
176 * Strap registers are configured in hardware or by early-boot
177 * firmware. Treat them as read-only despite that we can write
178 * them. This may mean that certain functions cannot be
179 * deconfigured and is the reason we re-evaluate after writing
180 * all descriptor bits.
181 */
182 if (desc->reg == HW_STRAP1 || desc->reg == HW_STRAP2)
183 continue;
184
185 ret = regmap_update_bits(map, desc->reg, desc->mask,
55956035 186 pattern << __ffs(desc->mask)) == 0;
4d3d0e42 187
55956035
AB
188 if (!ret)
189 return ret;
4d3d0e42
AJ
190 }
191
192 return aspeed_sig_expr_eval(expr, enable, map);
193}
194
195static bool aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr,
196 struct regmap *map)
197{
5366f146
AJ
198 if (aspeed_sig_expr_eval(expr, true, map))
199 return true;
200
4d3d0e42
AJ
201 return aspeed_sig_expr_set(expr, true, map);
202}
203
204static bool aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr,
205 struct regmap *map)
206{
5366f146
AJ
207 if (!aspeed_sig_expr_eval(expr, true, map))
208 return true;
209
4d3d0e42
AJ
210 return aspeed_sig_expr_set(expr, false, map);
211}
212
213/**
214 * Disable a signal on a pin by disabling all provided signal expressions.
215 *
216 * @exprs: The list of signal expressions (from a priority level on a pin)
217 * @map: The SCU's regmap instance for pinmux register access.
218 *
219 * @return true if all expressions in the list are successfully disabled, false
220 * otherwise
221 */
222static bool aspeed_disable_sig(const struct aspeed_sig_expr **exprs,
223 struct regmap *map)
224{
225 bool disabled = true;
226
227 if (!exprs)
228 return true;
229
230 while (*exprs) {
231 bool ret;
232
233 ret = aspeed_sig_expr_disable(*exprs, map);
234 disabled = disabled && ret;
235
236 exprs++;
237 }
238
239 return disabled;
240}
241
242/**
243 * Search for the signal expression needed to enable the pin's signal for the
244 * requested function.
245 *
246 * @exprs: List of signal expressions (haystack)
247 * @name: The name of the requested function (needle)
248 *
249 * @return A pointer to the signal expression whose function tag matches the
250 * provided name, otherwise NULL.
251 *
252 */
253static const struct aspeed_sig_expr *aspeed_find_expr_by_name(
254 const struct aspeed_sig_expr **exprs, const char *name)
255{
256 while (*exprs) {
257 if (strcmp((*exprs)->function, name) == 0)
258 return *exprs;
259 exprs++;
260 }
261
262 return NULL;
263}
264
265static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc,
266 const char *(*get)(
267 const struct aspeed_sig_expr *))
268{
269 char *found = NULL;
270 size_t len = 0;
271 const struct aspeed_sig_expr ***prios, **funcs, *expr;
272
273 prios = pdesc->prios;
274
275 while ((funcs = *prios)) {
276 while ((expr = *funcs)) {
277 const char *str = get(expr);
278 size_t delta = strlen(str) + 2;
279 char *expanded;
280
281 expanded = krealloc(found, len + delta + 1, GFP_KERNEL);
282 if (!expanded) {
283 kfree(found);
284 return expanded;
285 }
286
287 found = expanded;
288 found[len] = '\0';
289 len += delta;
290
291 strcat(found, str);
292 strcat(found, ", ");
293
294 funcs++;
295 }
296 prios++;
297 }
298
299 if (len < 2) {
300 kfree(found);
301 return NULL;
302 }
303
304 found[len - 2] = '\0';
305
306 return found;
307}
308
309static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr)
310{
311 return expr->function;
312}
313
314static char *get_defined_functions(const struct aspeed_pin_desc *pdesc)
315{
316 return get_defined_attribute(pdesc, aspeed_sig_expr_function);
317}
318
319static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr)
320{
321 return expr->signal;
322}
323
324static char *get_defined_signals(const struct aspeed_pin_desc *pdesc)
325{
326 return get_defined_attribute(pdesc, aspeed_sig_expr_signal);
327}
328
329int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
330 unsigned int group)
331{
332 int i;
333 const struct aspeed_pinctrl_data *pdata =
334 pinctrl_dev_get_drvdata(pctldev);
335 const struct aspeed_pin_group *pgroup = &pdata->groups[group];
336 const struct aspeed_pin_function *pfunc =
337 &pdata->functions[function];
338
339 for (i = 0; i < pgroup->npins; i++) {
340 int pin = pgroup->pins[i];
341 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data;
342 const struct aspeed_sig_expr *expr = NULL;
343 const struct aspeed_sig_expr **funcs;
344 const struct aspeed_sig_expr ***prios;
345
346 if (!pdesc)
347 return -EINVAL;
348
349 prios = pdesc->prios;
350
351 if (!prios)
352 continue;
353
354 /* Disable functions at a higher priority than that requested */
355 while ((funcs = *prios)) {
356 expr = aspeed_find_expr_by_name(funcs, pfunc->name);
357
358 if (expr)
359 break;
360
361 if (!aspeed_disable_sig(funcs, pdata->map))
362 return -EPERM;
363
364 prios++;
365 }
366
367 if (!expr) {
368 char *functions = get_defined_functions(pdesc);
369 char *signals = get_defined_signals(pdesc);
370
371 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n",
372 pfunc->name, pdesc->name, pin, signals,
373 functions);
374 kfree(signals);
375 kfree(functions);
376
377 return -ENXIO;
378 }
379
380 if (!aspeed_sig_expr_enable(expr, pdata->map))
381 return -EPERM;
382 }
383
384 return 0;
385}
386
387static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
388{
389 /*
390 * The signal type is GPIO if the signal name has "GPIO" as a prefix.
391 * strncmp (rather than strcmp) is used to implement the prefix
392 * requirement.
393 *
394 * expr->signal might look like "GPIOT3" in the GPIO case.
395 */
396 return strncmp(expr->signal, "GPIO", 4) == 0;
397}
398
399static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
400{
401 if (!exprs)
402 return false;
403
404 while (*exprs) {
405 if (aspeed_expr_is_gpio(*exprs))
406 return true;
407 exprs++;
408 }
409
410 return false;
411}
412
413int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev,
414 struct pinctrl_gpio_range *range,
415 unsigned int offset)
416{
417 const struct aspeed_pinctrl_data *pdata =
418 pinctrl_dev_get_drvdata(pctldev);
419 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data;
420 const struct aspeed_sig_expr ***prios, **funcs, *expr;
421
422 if (!pdesc)
423 return -EINVAL;
424
425 prios = pdesc->prios;
426
427 if (!prios)
428 return -ENXIO;
429
430 /* Disable any functions of higher priority than GPIO */
431 while ((funcs = *prios)) {
432 if (aspeed_gpio_in_exprs(funcs))
433 break;
434
435 if (!aspeed_disable_sig(funcs, pdata->map))
436 return -EPERM;
437
438 prios++;
439 }
440
441 if (!funcs) {
442 char *signals = get_defined_signals(pdesc);
443
444 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n",
445 pdesc->name, offset, signals);
446 kfree(signals);
447
448 return -ENXIO;
449 }
450
451 expr = *funcs;
452
453 /*
454 * Disabling all higher-priority expressions is enough to enable the
455 * lowest-priority signal type. As such it has no associated
456 * expression.
457 */
458 if (!expr)
459 return 0;
460
461 /*
462 * If GPIO is not the lowest priority signal type, assume there is only
463 * one expression defined to enable the GPIO function
464 */
465 if (!aspeed_sig_expr_enable(expr, pdata->map))
466 return -EPERM;
467
468 return 0;
469}
470
471int aspeed_pinctrl_probe(struct platform_device *pdev,
472 struct pinctrl_desc *pdesc,
473 struct aspeed_pinctrl_data *pdata)
474{
475 struct device *parent;
476 struct pinctrl_dev *pctl;
477
478 parent = pdev->dev.parent;
479 if (!parent) {
480 dev_err(&pdev->dev, "No parent for syscon pincontroller\n");
481 return -ENODEV;
482 }
483
484 pdata->map = syscon_node_to_regmap(parent->of_node);
485 if (IS_ERR(pdata->map)) {
486 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n");
487 return PTR_ERR(pdata->map);
488 }
489
490 pctl = pinctrl_register(pdesc, &pdev->dev, pdata);
491
492 if (IS_ERR(pctl)) {
493 dev_err(&pdev->dev, "Failed to register pinctrl\n");
494 return PTR_ERR(pctl);
495 }
496
497 platform_set_drvdata(pdev, pdata);
498
499 return 0;
500}