sunxi: Backport patches needed for A64
[openwrt/staging/lynxis.git] / target / linux / sunxi / patches-4.9 / 0039-pinctrl-sunxi-Make-sunxi_pconf_group_set-use-sunxi_p.patch
1 From 51814827190214986c452a166718bf12d32211c7 Mon Sep 17 00:00:00 2001
2 From: Chen-Yu Tsai <wens@csie.org>
3 Date: Fri, 11 Nov 2016 17:50:36 +0800
4 Subject: pinctrl: sunxi: Make sunxi_pconf_group_set use sunxi_pconf_reg helper
5
6 The sunxi_pconf_reg helper introduced in the last patch gives us the
7 chance to rework sunxi_pconf_group_set to have it match the structure
8 of sunxi_pconf_(group_)get and make it easier to understand.
9
10 For each config to set, it:
11
12 1. checks if the parameter is supported.
13 2. checks if the argument is within limits.
14 3. converts argument to the register value.
15 4. writes to the register with spinlock held.
16
17 As a result the function now blocks unsupported config parameters,
18 instead of silently ignoring them.
19
20 Signed-off-by: Chen-Yu Tsai <wens@csie.org>
21 Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
22 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
23 ---
24 drivers/pinctrl/sunxi/pinctrl-sunxi.c | 64 +++++++++++++++++------------------
25 1 file changed, 32 insertions(+), 32 deletions(-)
26
27 --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
28 +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
29 @@ -532,23 +532,27 @@ static int sunxi_pconf_group_set(struct
30 {
31 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
32 struct sunxi_pinctrl_group *g = &pctl->groups[group];
33 - unsigned long flags;
34 unsigned pin = g->pin - pctl->desc->pin_base;
35 - u32 val, mask;
36 - u16 strength;
37 - u8 dlevel;
38 int i;
39
40 - spin_lock_irqsave(&pctl->lock, flags);
41 -
42 for (i = 0; i < num_configs; i++) {
43 - switch (pinconf_to_config_param(configs[i])) {
44 + enum pin_config_param param;
45 + unsigned long flags;
46 + u32 offset, shift, mask, reg;
47 + u16 arg, val;
48 + int ret;
49 +
50 + param = pinconf_to_config_param(configs[i]);
51 + arg = pinconf_to_config_argument(configs[i]);
52 +
53 + ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
54 + if (ret < 0)
55 + return ret;
56 +
57 + switch (param) {
58 case PIN_CONFIG_DRIVE_STRENGTH:
59 - strength = pinconf_to_config_argument(configs[i]);
60 - if (strength > 40) {
61 - spin_unlock_irqrestore(&pctl->lock, flags);
62 + if (arg < 10 || arg > 40)
63 return -EINVAL;
64 - }
65 /*
66 * We convert from mA to what the register expects:
67 * 0: 10mA
68 @@ -556,37 +560,33 @@ static int sunxi_pconf_group_set(struct
69 * 2: 30mA
70 * 3: 40mA
71 */
72 - dlevel = strength / 10 - 1;
73 - val = readl(pctl->membase + sunxi_dlevel_reg(pin));
74 - mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
75 - writel((val & ~mask)
76 - | dlevel << sunxi_dlevel_offset(pin),
77 - pctl->membase + sunxi_dlevel_reg(pin));
78 + val = arg / 10 - 1;
79 break;
80 case PIN_CONFIG_BIAS_DISABLE:
81 - val = readl(pctl->membase + sunxi_pull_reg(pin));
82 - mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
83 - writel((val & ~mask),
84 - pctl->membase + sunxi_pull_reg(pin));
85 + val = 0;
86 break;
87 case PIN_CONFIG_BIAS_PULL_UP:
88 - val = readl(pctl->membase + sunxi_pull_reg(pin));
89 - mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
90 - writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
91 - pctl->membase + sunxi_pull_reg(pin));
92 + if (arg == 0)
93 + return -EINVAL;
94 + val = 1;
95 break;
96 case PIN_CONFIG_BIAS_PULL_DOWN:
97 - val = readl(pctl->membase + sunxi_pull_reg(pin));
98 - mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
99 - writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
100 - pctl->membase + sunxi_pull_reg(pin));
101 + if (arg == 0)
102 + return -EINVAL;
103 + val = 2;
104 break;
105 default:
106 - break;
107 + /* sunxi_pconf_reg should catch anything unsupported */
108 + WARN_ON(1);
109 + return -ENOTSUPP;
110 }
111 - } /* for each config */
112
113 - spin_unlock_irqrestore(&pctl->lock, flags);
114 + spin_lock_irqsave(&pctl->lock, flags);
115 + reg = readl(pctl->membase + offset);
116 + reg &= ~(mask << shift);
117 + writel(reg | val << shift, pctl->membase + offset);
118 + spin_unlock_irqrestore(&pctl->lock, flags);
119 + } /* for each config */
120
121 return 0;
122 }