1e78d57b2SSean Wang // SPDX-License-Identifier: GPL-2.0
2e78d57b2SSean Wang /*
3e78d57b2SSean Wang * MediaTek Pinctrl Moore Driver, which implement the generic dt-binding
4e78d57b2SSean Wang * pinctrl-bindings.txt for MediaTek SoC.
5e78d57b2SSean Wang *
6e78d57b2SSean Wang * Copyright (C) 2017-2018 MediaTek Inc.
7e78d57b2SSean Wang * Author: Sean Wang <sean.wang@mediatek.com>
8e78d57b2SSean Wang *
9e78d57b2SSean Wang */
10e78d57b2SSean Wang
11fae82621SSam Shih #include <dt-bindings/pinctrl/mt65xx.h>
1222d7fe49SLinus Walleij #include <linux/gpio/driver.h>
139abef9f2SAndy Shevchenko
149abef9f2SAndy Shevchenko #include <linux/pinctrl/consumer.h>
159abef9f2SAndy Shevchenko
16e78d57b2SSean Wang #include "pinctrl-moore.h"
17e78d57b2SSean Wang
18e78d57b2SSean Wang #define PINCTRL_PINCTRL_DEV KBUILD_MODNAME
19e78d57b2SSean Wang
20e78d57b2SSean Wang /* Custom pinconf parameters */
21e78d57b2SSean Wang #define MTK_PIN_CONFIG_TDSEL (PIN_CONFIG_END + 1)
22e78d57b2SSean Wang #define MTK_PIN_CONFIG_RDSEL (PIN_CONFIG_END + 2)
230d7ca772SSean Wang #define MTK_PIN_CONFIG_PU_ADV (PIN_CONFIG_END + 3)
240d7ca772SSean Wang #define MTK_PIN_CONFIG_PD_ADV (PIN_CONFIG_END + 4)
25e78d57b2SSean Wang
26e78d57b2SSean Wang static const struct pinconf_generic_params mtk_custom_bindings[] = {
27e78d57b2SSean Wang {"mediatek,tdsel", MTK_PIN_CONFIG_TDSEL, 0},
28e78d57b2SSean Wang {"mediatek,rdsel", MTK_PIN_CONFIG_RDSEL, 0},
290d7ca772SSean Wang {"mediatek,pull-up-adv", MTK_PIN_CONFIG_PU_ADV, 1},
300d7ca772SSean Wang {"mediatek,pull-down-adv", MTK_PIN_CONFIG_PD_ADV, 1},
31e78d57b2SSean Wang };
32e78d57b2SSean Wang
33e78d57b2SSean Wang #ifdef CONFIG_DEBUG_FS
34e78d57b2SSean Wang static const struct pin_config_item mtk_conf_items[] = {
35e78d57b2SSean Wang PCONFDUMP(MTK_PIN_CONFIG_TDSEL, "tdsel", NULL, true),
36e78d57b2SSean Wang PCONFDUMP(MTK_PIN_CONFIG_RDSEL, "rdsel", NULL, true),
370d7ca772SSean Wang PCONFDUMP(MTK_PIN_CONFIG_PU_ADV, "pu-adv", NULL, true),
380d7ca772SSean Wang PCONFDUMP(MTK_PIN_CONFIG_PD_ADV, "pd-adv", NULL, true),
39e78d57b2SSean Wang };
40e78d57b2SSean Wang #endif
41e78d57b2SSean Wang
mtk_pinmux_set_mux(struct pinctrl_dev * pctldev,unsigned int selector,unsigned int group)42e78d57b2SSean Wang static int mtk_pinmux_set_mux(struct pinctrl_dev *pctldev,
43e78d57b2SSean Wang unsigned int selector, unsigned int group)
44e78d57b2SSean Wang {
45e78d57b2SSean Wang struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
46e78d57b2SSean Wang struct function_desc *func;
47e78d57b2SSean Wang struct group_desc *grp;
48e78d57b2SSean Wang int i;
49e78d57b2SSean Wang
50e78d57b2SSean Wang func = pinmux_generic_get_function(pctldev, selector);
51e78d57b2SSean Wang if (!func)
52e78d57b2SSean Wang return -EINVAL;
53e78d57b2SSean Wang
54e78d57b2SSean Wang grp = pinctrl_generic_get_group(pctldev, group);
55e78d57b2SSean Wang if (!grp)
56e78d57b2SSean Wang return -EINVAL;
57e78d57b2SSean Wang
58e78d57b2SSean Wang dev_dbg(pctldev->dev, "enable function %s group %s\n",
59e78d57b2SSean Wang func->name, grp->name);
60e78d57b2SSean Wang
61e78d57b2SSean Wang for (i = 0; i < grp->num_pins; i++) {
62ea051eb3SSean Wang const struct mtk_pin_desc *desc;
63e78d57b2SSean Wang int *pin_modes = grp->data;
64ea051eb3SSean Wang int pin = grp->pins[i];
65e78d57b2SSean Wang
66ea051eb3SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
67d8b94c9fSSam Shih if (!desc->name)
68d8b94c9fSSam Shih return -ENOTSUPP;
69ea051eb3SSean Wang
70ea051eb3SSean Wang mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
71e78d57b2SSean Wang pin_modes[i]);
72e78d57b2SSean Wang }
73e78d57b2SSean Wang
74e78d57b2SSean Wang return 0;
75e78d57b2SSean Wang }
76e78d57b2SSean Wang
mtk_pinmux_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin)77e78d57b2SSean Wang static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev,
78e78d57b2SSean Wang struct pinctrl_gpio_range *range,
79e78d57b2SSean Wang unsigned int pin)
80e78d57b2SSean Wang {
81e78d57b2SSean Wang struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
82ea051eb3SSean Wang const struct mtk_pin_desc *desc;
83e78d57b2SSean Wang
84ea051eb3SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
85d8b94c9fSSam Shih if (!desc->name)
86d8b94c9fSSam Shih return -ENOTSUPP;
87ea051eb3SSean Wang
88ea051eb3SSean Wang return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
89ea051eb3SSean Wang hw->soc->gpio_m);
90e78d57b2SSean Wang }
91e78d57b2SSean Wang
mtk_pinmux_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned int pin,bool input)92e78d57b2SSean Wang static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
93e78d57b2SSean Wang struct pinctrl_gpio_range *range,
94e78d57b2SSean Wang unsigned int pin, bool input)
95e78d57b2SSean Wang {
96e78d57b2SSean Wang struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
97ea051eb3SSean Wang const struct mtk_pin_desc *desc;
98ea051eb3SSean Wang
99ea051eb3SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
100d8b94c9fSSam Shih if (!desc->name)
101d8b94c9fSSam Shih return -ENOTSUPP;
102e78d57b2SSean Wang
103e78d57b2SSean Wang /* hardware would take 0 as input direction */
104ea051eb3SSean Wang return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, !input);
105e78d57b2SSean Wang }
106e78d57b2SSean Wang
mtk_pinconf_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)107e78d57b2SSean Wang static int mtk_pinconf_get(struct pinctrl_dev *pctldev,
108e78d57b2SSean Wang unsigned int pin, unsigned long *config)
109e78d57b2SSean Wang {
110e78d57b2SSean Wang struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
111e78d57b2SSean Wang u32 param = pinconf_to_config_param(*config);
112fae82621SSam Shih int val, val2, err, pullup, reg, ret = 1;
113c2832197SSean Wang const struct mtk_pin_desc *desc;
114c2832197SSean Wang
115c2832197SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
116d8b94c9fSSam Shih if (!desc->name)
117d8b94c9fSSam Shih return -ENOTSUPP;
118e78d57b2SSean Wang
119e78d57b2SSean Wang switch (param) {
120e78d57b2SSean Wang case PIN_CONFIG_BIAS_DISABLE:
121fae82621SSam Shih if (hw->soc->bias_get_combo) {
122fae82621SSam Shih err = hw->soc->bias_get_combo(hw, desc, &pullup, &ret);
123fae82621SSam Shih if (err)
124fae82621SSam Shih return err;
125fae82621SSam Shih if (ret != MTK_PUPD_SET_R1R0_00 && ret != MTK_DISABLE)
126fae82621SSam Shih return -EINVAL;
127fae82621SSam Shih } else if (hw->soc->bias_disable_get) {
12885430152SSean Wang err = hw->soc->bias_disable_get(hw, desc, &ret);
129e78d57b2SSean Wang if (err)
130e78d57b2SSean Wang return err;
13185430152SSean Wang } else {
13285430152SSean Wang return -ENOTSUPP;
13385430152SSean Wang }
134e78d57b2SSean Wang break;
135e78d57b2SSean Wang case PIN_CONFIG_BIAS_PULL_UP:
136fae82621SSam Shih if (hw->soc->bias_get_combo) {
137fae82621SSam Shih err = hw->soc->bias_get_combo(hw, desc, &pullup, &ret);
138fae82621SSam Shih if (err)
139fae82621SSam Shih return err;
140fae82621SSam Shih if (ret == MTK_PUPD_SET_R1R0_00 || ret == MTK_DISABLE)
141fae82621SSam Shih return -EINVAL;
142fae82621SSam Shih if (!pullup)
143fae82621SSam Shih return -EINVAL;
144fae82621SSam Shih } else if (hw->soc->bias_get) {
14585430152SSean Wang err = hw->soc->bias_get(hw, desc, 1, &ret);
14685430152SSean Wang if (err)
14785430152SSean Wang return err;
14885430152SSean Wang } else {
14985430152SSean Wang return -ENOTSUPP;
15085430152SSean Wang }
15185430152SSean Wang break;
152e78d57b2SSean Wang case PIN_CONFIG_BIAS_PULL_DOWN:
153fae82621SSam Shih if (hw->soc->bias_get_combo) {
154fae82621SSam Shih err = hw->soc->bias_get_combo(hw, desc, &pullup, &ret);
155fae82621SSam Shih if (err)
156fae82621SSam Shih return err;
157fae82621SSam Shih if (ret == MTK_PUPD_SET_R1R0_00 || ret == MTK_DISABLE)
158fae82621SSam Shih return -EINVAL;
159fae82621SSam Shih if (pullup)
160fae82621SSam Shih return -EINVAL;
161fae82621SSam Shih } else if (hw->soc->bias_get) {
16285430152SSean Wang err = hw->soc->bias_get(hw, desc, 0, &ret);
16385430152SSean Wang if (err)
16485430152SSean Wang return err;
16585430152SSean Wang } else {
16685430152SSean Wang return -ENOTSUPP;
16785430152SSean Wang }
16885430152SSean Wang break;
169e78d57b2SSean Wang case PIN_CONFIG_SLEW_RATE:
170ea051eb3SSean Wang err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SR, &val);
171e78d57b2SSean Wang if (err)
172e78d57b2SSean Wang return err;
173e78d57b2SSean Wang
174e78d57b2SSean Wang if (!val)
175e78d57b2SSean Wang return -EINVAL;
176e78d57b2SSean Wang
177e78d57b2SSean Wang break;
178e78d57b2SSean Wang case PIN_CONFIG_INPUT_ENABLE:
179e78d57b2SSean Wang case PIN_CONFIG_OUTPUT_ENABLE:
180ea051eb3SSean Wang err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &val);
181e78d57b2SSean Wang if (err)
182e78d57b2SSean Wang return err;
183e78d57b2SSean Wang
184e78d57b2SSean Wang /* HW takes input mode as zero; output mode as non-zero */
185e78d57b2SSean Wang if ((val && param == PIN_CONFIG_INPUT_ENABLE) ||
186e78d57b2SSean Wang (!val && param == PIN_CONFIG_OUTPUT_ENABLE))
187e78d57b2SSean Wang return -EINVAL;
188e78d57b2SSean Wang
189e78d57b2SSean Wang break;
190e78d57b2SSean Wang case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
191ea051eb3SSean Wang err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &val);
192e78d57b2SSean Wang if (err)
193e78d57b2SSean Wang return err;
194e78d57b2SSean Wang
195ea051eb3SSean Wang err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SMT, &val2);
196e78d57b2SSean Wang if (err)
197e78d57b2SSean Wang return err;
198e78d57b2SSean Wang
199e78d57b2SSean Wang if (val || !val2)
200e78d57b2SSean Wang return -EINVAL;
201e78d57b2SSean Wang
202e78d57b2SSean Wang break;
203e78d57b2SSean Wang case PIN_CONFIG_DRIVE_STRENGTH:
204c2832197SSean Wang if (hw->soc->drive_get) {
205c2832197SSean Wang err = hw->soc->drive_get(hw, desc, &ret);
206e78d57b2SSean Wang if (err)
207e78d57b2SSean Wang return err;
208c2832197SSean Wang } else {
209c2832197SSean Wang err = -ENOTSUPP;
210c2832197SSean Wang }
211e78d57b2SSean Wang break;
212e78d57b2SSean Wang case MTK_PIN_CONFIG_TDSEL:
213e78d57b2SSean Wang case MTK_PIN_CONFIG_RDSEL:
214e78d57b2SSean Wang reg = (param == MTK_PIN_CONFIG_TDSEL) ?
215e78d57b2SSean Wang PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
216e78d57b2SSean Wang
217ea051eb3SSean Wang err = mtk_hw_get_value(hw, desc, reg, &val);
218e78d57b2SSean Wang if (err)
219e78d57b2SSean Wang return err;
220e78d57b2SSean Wang
221e78d57b2SSean Wang ret = val;
222e78d57b2SSean Wang
223e78d57b2SSean Wang break;
2240d7ca772SSean Wang case MTK_PIN_CONFIG_PU_ADV:
2250d7ca772SSean Wang case MTK_PIN_CONFIG_PD_ADV:
2260d7ca772SSean Wang if (hw->soc->adv_pull_get) {
2270d7ca772SSean Wang bool pullup;
2280d7ca772SSean Wang
2290d7ca772SSean Wang pullup = param == MTK_PIN_CONFIG_PU_ADV;
2300d7ca772SSean Wang err = hw->soc->adv_pull_get(hw, desc, pullup, &ret);
2310d7ca772SSean Wang if (err)
2320d7ca772SSean Wang return err;
2330d7ca772SSean Wang } else {
2340d7ca772SSean Wang return -ENOTSUPP;
2350d7ca772SSean Wang }
2360d7ca772SSean Wang break;
237e78d57b2SSean Wang default:
238e78d57b2SSean Wang return -ENOTSUPP;
239e78d57b2SSean Wang }
240e78d57b2SSean Wang
241e78d57b2SSean Wang *config = pinconf_to_config_packed(param, ret);
242e78d57b2SSean Wang
243e78d57b2SSean Wang return 0;
244e78d57b2SSean Wang }
245e78d57b2SSean Wang
mtk_pinconf_set(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * configs,unsigned int num_configs)246e78d57b2SSean Wang static int mtk_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
247e78d57b2SSean Wang unsigned long *configs, unsigned int num_configs)
248e78d57b2SSean Wang {
249e78d57b2SSean Wang struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
250c2832197SSean Wang const struct mtk_pin_desc *desc;
251e78d57b2SSean Wang u32 reg, param, arg;
252e78d57b2SSean Wang int cfg, err = 0;
253e78d57b2SSean Wang
254c2832197SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
255d8b94c9fSSam Shih if (!desc->name)
256d8b94c9fSSam Shih return -ENOTSUPP;
257c2832197SSean Wang
258e78d57b2SSean Wang for (cfg = 0; cfg < num_configs; cfg++) {
259e78d57b2SSean Wang param = pinconf_to_config_param(configs[cfg]);
260e78d57b2SSean Wang arg = pinconf_to_config_argument(configs[cfg]);
261e78d57b2SSean Wang
262e78d57b2SSean Wang switch (param) {
263e78d57b2SSean Wang case PIN_CONFIG_BIAS_DISABLE:
264fae82621SSam Shih if (hw->soc->bias_set_combo) {
265fae82621SSam Shih err = hw->soc->bias_set_combo(hw, desc, 0, MTK_DISABLE);
266fae82621SSam Shih if (err)
267fae82621SSam Shih return err;
268fae82621SSam Shih } else if (hw->soc->bias_disable_set) {
26985430152SSean Wang err = hw->soc->bias_disable_set(hw, desc);
27085430152SSean Wang if (err)
27185430152SSean Wang return err;
27285430152SSean Wang } else {
27385430152SSean Wang return -ENOTSUPP;
27485430152SSean Wang }
27585430152SSean Wang break;
276e78d57b2SSean Wang case PIN_CONFIG_BIAS_PULL_UP:
277fae82621SSam Shih if (hw->soc->bias_set_combo) {
278fae82621SSam Shih err = hw->soc->bias_set_combo(hw, desc, 1, arg);
279fae82621SSam Shih if (err)
280fae82621SSam Shih return err;
281fae82621SSam Shih } else if (hw->soc->bias_set) {
28285430152SSean Wang err = hw->soc->bias_set(hw, desc, 1);
28385430152SSean Wang if (err)
28485430152SSean Wang return err;
28585430152SSean Wang } else {
28685430152SSean Wang return -ENOTSUPP;
28785430152SSean Wang }
28885430152SSean Wang break;
289e78d57b2SSean Wang case PIN_CONFIG_BIAS_PULL_DOWN:
290fae82621SSam Shih if (hw->soc->bias_set_combo) {
291fae82621SSam Shih err = hw->soc->bias_set_combo(hw, desc, 0, arg);
292fae82621SSam Shih if (err)
293fae82621SSam Shih return err;
294fae82621SSam Shih } else if (hw->soc->bias_set) {
29585430152SSean Wang err = hw->soc->bias_set(hw, desc, 0);
296e78d57b2SSean Wang if (err)
29785430152SSean Wang return err;
29885430152SSean Wang } else {
29985430152SSean Wang return -ENOTSUPP;
30085430152SSean Wang }
301e78d57b2SSean Wang break;
302e78d57b2SSean Wang case PIN_CONFIG_OUTPUT_ENABLE:
303ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT,
304e78d57b2SSean Wang MTK_DISABLE);
305e78d57b2SSean Wang if (err)
306e78d57b2SSean Wang goto err;
307e78d57b2SSean Wang
308ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
309182c842fSSean Wang MTK_OUTPUT);
310182c842fSSean Wang if (err)
311182c842fSSean Wang goto err;
312182c842fSSean Wang break;
313182c842fSSean Wang case PIN_CONFIG_INPUT_ENABLE:
314182c842fSSean Wang
315182c842fSSean Wang if (hw->soc->ies_present) {
316ea051eb3SSean Wang mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_IES,
317182c842fSSean Wang MTK_ENABLE);
318182c842fSSean Wang }
319182c842fSSean Wang
320ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
321182c842fSSean Wang MTK_INPUT);
322182c842fSSean Wang if (err)
323182c842fSSean Wang goto err;
324182c842fSSean Wang break;
325182c842fSSean Wang case PIN_CONFIG_SLEW_RATE:
326ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SR,
327182c842fSSean Wang arg);
328e78d57b2SSean Wang if (err)
329e78d57b2SSean Wang goto err;
330e78d57b2SSean Wang
331e78d57b2SSean Wang break;
332e78d57b2SSean Wang case PIN_CONFIG_OUTPUT:
333ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
334e78d57b2SSean Wang MTK_OUTPUT);
335e78d57b2SSean Wang if (err)
336e78d57b2SSean Wang goto err;
337e78d57b2SSean Wang
338ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO,
339e78d57b2SSean Wang arg);
340e78d57b2SSean Wang if (err)
341e78d57b2SSean Wang goto err;
342e78d57b2SSean Wang break;
343e78d57b2SSean Wang case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
344e78d57b2SSean Wang /* arg = 1: Input mode & SMT enable ;
345e78d57b2SSean Wang * arg = 0: Output mode & SMT disable
346e78d57b2SSean Wang */
347e78d57b2SSean Wang arg = arg ? 2 : 1;
348ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
349e78d57b2SSean Wang arg & 1);
350e78d57b2SSean Wang if (err)
351e78d57b2SSean Wang goto err;
352e78d57b2SSean Wang
353ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT,
354e78d57b2SSean Wang !!(arg & 2));
355e78d57b2SSean Wang if (err)
356e78d57b2SSean Wang goto err;
357e78d57b2SSean Wang break;
358e78d57b2SSean Wang case PIN_CONFIG_DRIVE_STRENGTH:
359c2832197SSean Wang if (hw->soc->drive_set) {
360c2832197SSean Wang err = hw->soc->drive_set(hw, desc, arg);
361e78d57b2SSean Wang if (err)
362c2832197SSean Wang return err;
363e78d57b2SSean Wang } else {
364e78d57b2SSean Wang err = -ENOTSUPP;
365e78d57b2SSean Wang }
366e78d57b2SSean Wang break;
367e78d57b2SSean Wang case MTK_PIN_CONFIG_TDSEL:
368e78d57b2SSean Wang case MTK_PIN_CONFIG_RDSEL:
369e78d57b2SSean Wang reg = (param == MTK_PIN_CONFIG_TDSEL) ?
370e78d57b2SSean Wang PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
371e78d57b2SSean Wang
372ea051eb3SSean Wang err = mtk_hw_set_value(hw, desc, reg, arg);
373e78d57b2SSean Wang if (err)
374e78d57b2SSean Wang goto err;
375e78d57b2SSean Wang break;
3760d7ca772SSean Wang case MTK_PIN_CONFIG_PU_ADV:
3770d7ca772SSean Wang case MTK_PIN_CONFIG_PD_ADV:
3780d7ca772SSean Wang if (hw->soc->adv_pull_set) {
3790d7ca772SSean Wang bool pullup;
3800d7ca772SSean Wang
3810d7ca772SSean Wang pullup = param == MTK_PIN_CONFIG_PU_ADV;
3820d7ca772SSean Wang err = hw->soc->adv_pull_set(hw, desc, pullup,
3830d7ca772SSean Wang arg);
3840d7ca772SSean Wang if (err)
3850d7ca772SSean Wang return err;
3860d7ca772SSean Wang } else {
3870d7ca772SSean Wang return -ENOTSUPP;
3880d7ca772SSean Wang }
3890d7ca772SSean Wang break;
390e78d57b2SSean Wang default:
391e78d57b2SSean Wang err = -ENOTSUPP;
392e78d57b2SSean Wang }
393e78d57b2SSean Wang }
394e78d57b2SSean Wang err:
395e78d57b2SSean Wang return err;
396e78d57b2SSean Wang }
397e78d57b2SSean Wang
mtk_pinconf_group_get(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * config)398e78d57b2SSean Wang static int mtk_pinconf_group_get(struct pinctrl_dev *pctldev,
399e78d57b2SSean Wang unsigned int group, unsigned long *config)
400e78d57b2SSean Wang {
401e78d57b2SSean Wang const unsigned int *pins;
402e78d57b2SSean Wang unsigned int i, npins, old = 0;
403e78d57b2SSean Wang int ret;
404e78d57b2SSean Wang
405e78d57b2SSean Wang ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
406e78d57b2SSean Wang if (ret)
407e78d57b2SSean Wang return ret;
408e78d57b2SSean Wang
409e78d57b2SSean Wang for (i = 0; i < npins; i++) {
410e78d57b2SSean Wang if (mtk_pinconf_get(pctldev, pins[i], config))
411e78d57b2SSean Wang return -ENOTSUPP;
412e78d57b2SSean Wang
413e78d57b2SSean Wang /* configs do not match between two pins */
414e78d57b2SSean Wang if (i && old != *config)
415e78d57b2SSean Wang return -ENOTSUPP;
416e78d57b2SSean Wang
417e78d57b2SSean Wang old = *config;
418e78d57b2SSean Wang }
419e78d57b2SSean Wang
420e78d57b2SSean Wang return 0;
421e78d57b2SSean Wang }
422e78d57b2SSean Wang
mtk_pinconf_group_set(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * configs,unsigned int num_configs)423e78d57b2SSean Wang static int mtk_pinconf_group_set(struct pinctrl_dev *pctldev,
424e78d57b2SSean Wang unsigned int group, unsigned long *configs,
425e78d57b2SSean Wang unsigned int num_configs)
426e78d57b2SSean Wang {
427e78d57b2SSean Wang const unsigned int *pins;
428e78d57b2SSean Wang unsigned int i, npins;
429e78d57b2SSean Wang int ret;
430e78d57b2SSean Wang
431e78d57b2SSean Wang ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
432e78d57b2SSean Wang if (ret)
433e78d57b2SSean Wang return ret;
434e78d57b2SSean Wang
435e78d57b2SSean Wang for (i = 0; i < npins; i++) {
436e78d57b2SSean Wang ret = mtk_pinconf_set(pctldev, pins[i], configs, num_configs);
437e78d57b2SSean Wang if (ret)
438e78d57b2SSean Wang return ret;
439e78d57b2SSean Wang }
440e78d57b2SSean Wang
441e78d57b2SSean Wang return 0;
442e78d57b2SSean Wang }
443e78d57b2SSean Wang
444e78d57b2SSean Wang static const struct pinctrl_ops mtk_pctlops = {
445e78d57b2SSean Wang .get_groups_count = pinctrl_generic_get_group_count,
446e78d57b2SSean Wang .get_group_name = pinctrl_generic_get_group_name,
447e78d57b2SSean Wang .get_group_pins = pinctrl_generic_get_group_pins,
448e78d57b2SSean Wang .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
449e78d57b2SSean Wang .dt_free_map = pinconf_generic_dt_free_map,
450e78d57b2SSean Wang };
451e78d57b2SSean Wang
452e78d57b2SSean Wang static const struct pinmux_ops mtk_pmxops = {
453e78d57b2SSean Wang .get_functions_count = pinmux_generic_get_function_count,
454e78d57b2SSean Wang .get_function_name = pinmux_generic_get_function_name,
455e78d57b2SSean Wang .get_function_groups = pinmux_generic_get_function_groups,
456e78d57b2SSean Wang .set_mux = mtk_pinmux_set_mux,
457e78d57b2SSean Wang .gpio_request_enable = mtk_pinmux_gpio_request_enable,
458e78d57b2SSean Wang .gpio_set_direction = mtk_pinmux_gpio_set_direction,
459e78d57b2SSean Wang .strict = true,
460e78d57b2SSean Wang };
461e78d57b2SSean Wang
462e78d57b2SSean Wang static const struct pinconf_ops mtk_confops = {
463e78d57b2SSean Wang .is_generic = true,
464e78d57b2SSean Wang .pin_config_get = mtk_pinconf_get,
465e78d57b2SSean Wang .pin_config_set = mtk_pinconf_set,
466e78d57b2SSean Wang .pin_config_group_get = mtk_pinconf_group_get,
467e78d57b2SSean Wang .pin_config_group_set = mtk_pinconf_group_set,
468e78d57b2SSean Wang .pin_config_config_dbg_show = pinconf_generic_dump_config,
469e78d57b2SSean Wang };
470e78d57b2SSean Wang
471e78d57b2SSean Wang static struct pinctrl_desc mtk_desc = {
472e78d57b2SSean Wang .name = PINCTRL_PINCTRL_DEV,
473e78d57b2SSean Wang .pctlops = &mtk_pctlops,
474e78d57b2SSean Wang .pmxops = &mtk_pmxops,
475e78d57b2SSean Wang .confops = &mtk_confops,
476e78d57b2SSean Wang .owner = THIS_MODULE,
477e78d57b2SSean Wang };
478e78d57b2SSean Wang
mtk_gpio_get(struct gpio_chip * chip,unsigned int gpio)479e78d57b2SSean Wang static int mtk_gpio_get(struct gpio_chip *chip, unsigned int gpio)
480e78d57b2SSean Wang {
481e78d57b2SSean Wang struct mtk_pinctrl *hw = gpiochip_get_data(chip);
482ea051eb3SSean Wang const struct mtk_pin_desc *desc;
483e78d57b2SSean Wang int value, err;
484e78d57b2SSean Wang
485ea051eb3SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
486d8b94c9fSSam Shih if (!desc->name)
487d8b94c9fSSam Shih return -ENOTSUPP;
488ea051eb3SSean Wang
489ea051eb3SSean Wang err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
490e78d57b2SSean Wang if (err)
491e78d57b2SSean Wang return err;
492e78d57b2SSean Wang
493e78d57b2SSean Wang return !!value;
494e78d57b2SSean Wang }
495e78d57b2SSean Wang
mtk_gpio_set(struct gpio_chip * chip,unsigned int gpio,int value)496e78d57b2SSean Wang static void mtk_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
497e78d57b2SSean Wang {
498e78d57b2SSean Wang struct mtk_pinctrl *hw = gpiochip_get_data(chip);
499ea051eb3SSean Wang const struct mtk_pin_desc *desc;
500e78d57b2SSean Wang
501ea051eb3SSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
502d8b94c9fSSam Shih if (!desc->name) {
503d8b94c9fSSam Shih dev_err(hw->dev, "Failed to set gpio %d\n", gpio);
504d8b94c9fSSam Shih return;
505d8b94c9fSSam Shih }
506ea051eb3SSean Wang
507ea051eb3SSean Wang mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO, !!value);
508e78d57b2SSean Wang }
509e78d57b2SSean Wang
mtk_gpio_direction_input(struct gpio_chip * chip,unsigned int gpio)510e78d57b2SSean Wang static int mtk_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
511e78d57b2SSean Wang {
512e78d57b2SSean Wang return pinctrl_gpio_direction_input(chip->base + gpio);
513e78d57b2SSean Wang }
514e78d57b2SSean Wang
mtk_gpio_direction_output(struct gpio_chip * chip,unsigned int gpio,int value)515e78d57b2SSean Wang static int mtk_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
516e78d57b2SSean Wang int value)
517e78d57b2SSean Wang {
518e78d57b2SSean Wang mtk_gpio_set(chip, gpio, value);
519e78d57b2SSean Wang
520e78d57b2SSean Wang return pinctrl_gpio_direction_output(chip->base + gpio);
521e78d57b2SSean Wang }
522e78d57b2SSean Wang
mtk_gpio_to_irq(struct gpio_chip * chip,unsigned int offset)523e78d57b2SSean Wang static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
524e78d57b2SSean Wang {
525e78d57b2SSean Wang struct mtk_pinctrl *hw = gpiochip_get_data(chip);
526fb5fa8dcSSean Wang const struct mtk_pin_desc *desc;
527e78d57b2SSean Wang
528e78d57b2SSean Wang if (!hw->eint)
529e78d57b2SSean Wang return -ENOTSUPP;
530e78d57b2SSean Wang
531fb5fa8dcSSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset];
532e78d57b2SSean Wang
5337a52127eSColin Ian King if (desc->eint.eint_n == (u16)EINT_NA)
534fb5fa8dcSSean Wang return -ENOTSUPP;
535fb5fa8dcSSean Wang
536b7d7f9eeSSean Wang return mtk_eint_find_irq(hw->eint, desc->eint.eint_n);
537e78d57b2SSean Wang }
538e78d57b2SSean Wang
mtk_gpio_set_config(struct gpio_chip * chip,unsigned int offset,unsigned long config)539e78d57b2SSean Wang static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
540e78d57b2SSean Wang unsigned long config)
541e78d57b2SSean Wang {
542e78d57b2SSean Wang struct mtk_pinctrl *hw = gpiochip_get_data(chip);
543fb5fa8dcSSean Wang const struct mtk_pin_desc *desc;
544e78d57b2SSean Wang u32 debounce;
545e78d57b2SSean Wang
546fb5fa8dcSSean Wang desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset];
547d8b94c9fSSam Shih if (!desc->name)
548d8b94c9fSSam Shih return -ENOTSUPP;
549fb5fa8dcSSean Wang
550e78d57b2SSean Wang if (!hw->eint ||
551fb5fa8dcSSean Wang pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE ||
5527a52127eSColin Ian King desc->eint.eint_n == (u16)EINT_NA)
553e78d57b2SSean Wang return -ENOTSUPP;
554e78d57b2SSean Wang
555e78d57b2SSean Wang debounce = pinconf_to_config_argument(config);
556e78d57b2SSean Wang
557b7d7f9eeSSean Wang return mtk_eint_set_debounce(hw->eint, desc->eint.eint_n, debounce);
558e78d57b2SSean Wang }
559e78d57b2SSean Wang
mtk_build_gpiochip(struct mtk_pinctrl * hw)5608a8d6bbeSAndy Shevchenko static int mtk_build_gpiochip(struct mtk_pinctrl *hw)
561e78d57b2SSean Wang {
562e78d57b2SSean Wang struct gpio_chip *chip = &hw->chip;
563e78d57b2SSean Wang int ret;
564e78d57b2SSean Wang
565e78d57b2SSean Wang chip->label = PINCTRL_PINCTRL_DEV;
566e78d57b2SSean Wang chip->parent = hw->dev;
567e78d57b2SSean Wang chip->request = gpiochip_generic_request;
568e78d57b2SSean Wang chip->free = gpiochip_generic_free;
569e78d57b2SSean Wang chip->direction_input = mtk_gpio_direction_input;
570e78d57b2SSean Wang chip->direction_output = mtk_gpio_direction_output;
571e78d57b2SSean Wang chip->get = mtk_gpio_get;
572e78d57b2SSean Wang chip->set = mtk_gpio_set;
5730014d7a9SZheng Yongjun chip->to_irq = mtk_gpio_to_irq;
5740014d7a9SZheng Yongjun chip->set_config = mtk_gpio_set_config;
575e78d57b2SSean Wang chip->base = -1;
576e78d57b2SSean Wang chip->ngpio = hw->soc->npins;
577e78d57b2SSean Wang
578e78d57b2SSean Wang ret = gpiochip_add_data(chip, hw);
579e78d57b2SSean Wang if (ret < 0)
580e78d57b2SSean Wang return ret;
581e78d57b2SSean Wang
582e78d57b2SSean Wang /* Just for backward compatible for these old pinctrl nodes without
583e78d57b2SSean Wang * "gpio-ranges" property. Otherwise, called directly from a
584e78d57b2SSean Wang * DeviceTree-supported pinctrl driver is DEPRECATED.
585e78d57b2SSean Wang * Please see Section 2.1 of
586e78d57b2SSean Wang * Documentation/devicetree/bindings/gpio/gpio.txt on how to
587e78d57b2SSean Wang * bind pinctrl and gpio drivers via the "gpio-ranges" property.
588e78d57b2SSean Wang */
589*e0e8fbf8SRob Herring if (!of_property_present(hw->dev->of_node, "gpio-ranges")) {
590e78d57b2SSean Wang ret = gpiochip_add_pin_range(chip, dev_name(hw->dev), 0, 0,
591e78d57b2SSean Wang chip->ngpio);
592e78d57b2SSean Wang if (ret < 0) {
593e78d57b2SSean Wang gpiochip_remove(chip);
594e78d57b2SSean Wang return ret;
595e78d57b2SSean Wang }
596e78d57b2SSean Wang }
597e78d57b2SSean Wang
598e78d57b2SSean Wang return 0;
599e78d57b2SSean Wang }
600e78d57b2SSean Wang
mtk_build_groups(struct mtk_pinctrl * hw)601e78d57b2SSean Wang static int mtk_build_groups(struct mtk_pinctrl *hw)
602e78d57b2SSean Wang {
603e78d57b2SSean Wang int err, i;
604e78d57b2SSean Wang
605e78d57b2SSean Wang for (i = 0; i < hw->soc->ngrps; i++) {
606e78d57b2SSean Wang const struct group_desc *group = hw->soc->grps + i;
607e78d57b2SSean Wang
608e78d57b2SSean Wang err = pinctrl_generic_add_group(hw->pctrl, group->name,
609e78d57b2SSean Wang group->pins, group->num_pins,
610e78d57b2SSean Wang group->data);
611e78d57b2SSean Wang if (err < 0) {
612e78d57b2SSean Wang dev_err(hw->dev, "Failed to register group %s\n",
613e78d57b2SSean Wang group->name);
614e78d57b2SSean Wang return err;
615e78d57b2SSean Wang }
616e78d57b2SSean Wang }
617e78d57b2SSean Wang
618e78d57b2SSean Wang return 0;
619e78d57b2SSean Wang }
620e78d57b2SSean Wang
mtk_build_functions(struct mtk_pinctrl * hw)621e78d57b2SSean Wang static int mtk_build_functions(struct mtk_pinctrl *hw)
622e78d57b2SSean Wang {
623e78d57b2SSean Wang int i, err;
624e78d57b2SSean Wang
625e78d57b2SSean Wang for (i = 0; i < hw->soc->nfuncs ; i++) {
626e78d57b2SSean Wang const struct function_desc *func = hw->soc->funcs + i;
627e78d57b2SSean Wang
628e78d57b2SSean Wang err = pinmux_generic_add_function(hw->pctrl, func->name,
629e78d57b2SSean Wang func->group_names,
630e78d57b2SSean Wang func->num_group_names,
631e78d57b2SSean Wang func->data);
632e78d57b2SSean Wang if (err < 0) {
633e78d57b2SSean Wang dev_err(hw->dev, "Failed to register function %s\n",
634e78d57b2SSean Wang func->name);
635e78d57b2SSean Wang return err;
636e78d57b2SSean Wang }
637e78d57b2SSean Wang }
638e78d57b2SSean Wang
639e78d57b2SSean Wang return 0;
640e78d57b2SSean Wang }
641e78d57b2SSean Wang
mtk_moore_pinctrl_probe(struct platform_device * pdev,const struct mtk_pin_soc * soc)642e78d57b2SSean Wang int mtk_moore_pinctrl_probe(struct platform_device *pdev,
643e78d57b2SSean Wang const struct mtk_pin_soc *soc)
644e78d57b2SSean Wang {
6459c59fda3SAngeloGioacchino Del Regno struct device *dev = &pdev->dev;
646b7d7f9eeSSean Wang struct pinctrl_pin_desc *pins;
647e78d57b2SSean Wang struct mtk_pinctrl *hw;
6482bc47dfeSSean Wang int err, i;
649e78d57b2SSean Wang
650e78d57b2SSean Wang hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL);
651e78d57b2SSean Wang if (!hw)
652e78d57b2SSean Wang return -ENOMEM;
653e78d57b2SSean Wang
654e78d57b2SSean Wang hw->soc = soc;
6552bc47dfeSSean Wang hw->dev = &pdev->dev;
656e78d57b2SSean Wang
6579c59fda3SAngeloGioacchino Del Regno if (!hw->soc->nbase_names)
6589c59fda3SAngeloGioacchino Del Regno return dev_err_probe(dev, -EINVAL,
6592bc47dfeSSean Wang "SoC should be assigned at least one register base\n");
6602bc47dfeSSean Wang
6612bc47dfeSSean Wang hw->base = devm_kmalloc_array(&pdev->dev, hw->soc->nbase_names,
6622bc47dfeSSean Wang sizeof(*hw->base), GFP_KERNEL);
663068cfb9aSWei Yongjun if (!hw->base)
664068cfb9aSWei Yongjun return -ENOMEM;
6652bc47dfeSSean Wang
6662bc47dfeSSean Wang for (i = 0; i < hw->soc->nbase_names; i++) {
66748548c78SWang Xiaojun hw->base[i] = devm_platform_ioremap_resource_byname(pdev,
6682bc47dfeSSean Wang hw->soc->base_names[i]);
6692bc47dfeSSean Wang if (IS_ERR(hw->base[i]))
6702bc47dfeSSean Wang return PTR_ERR(hw->base[i]);
6712bc47dfeSSean Wang }
6722bc47dfeSSean Wang
6732bc47dfeSSean Wang hw->nbase = hw->soc->nbase_names;
674e78d57b2SSean Wang
67556ab29ecSTzung-Bi Shih spin_lock_init(&hw->lock);
67642a46434SZhiyong Tao
677b7d7f9eeSSean Wang /* Copy from internal struct mtk_pin_desc to register to the core */
678b7d7f9eeSSean Wang pins = devm_kmalloc_array(&pdev->dev, hw->soc->npins, sizeof(*pins),
679b7d7f9eeSSean Wang GFP_KERNEL);
680068cfb9aSWei Yongjun if (!pins)
681068cfb9aSWei Yongjun return -ENOMEM;
682b7d7f9eeSSean Wang
683b7d7f9eeSSean Wang for (i = 0; i < hw->soc->npins; i++) {
684b7d7f9eeSSean Wang pins[i].number = hw->soc->pins[i].number;
685b7d7f9eeSSean Wang pins[i].name = hw->soc->pins[i].name;
686b7d7f9eeSSean Wang }
687b7d7f9eeSSean Wang
688e78d57b2SSean Wang /* Setup pins descriptions per SoC types */
689b7d7f9eeSSean Wang mtk_desc.pins = (const struct pinctrl_pin_desc *)pins;
690e78d57b2SSean Wang mtk_desc.npins = hw->soc->npins;
691e78d57b2SSean Wang mtk_desc.num_custom_params = ARRAY_SIZE(mtk_custom_bindings);
692e78d57b2SSean Wang mtk_desc.custom_params = mtk_custom_bindings;
693e78d57b2SSean Wang #ifdef CONFIG_DEBUG_FS
694e78d57b2SSean Wang mtk_desc.custom_conf_items = mtk_conf_items;
695e78d57b2SSean Wang #endif
696e78d57b2SSean Wang
697e78d57b2SSean Wang err = devm_pinctrl_register_and_init(&pdev->dev, &mtk_desc, hw,
698e78d57b2SSean Wang &hw->pctrl);
699e78d57b2SSean Wang if (err)
700e78d57b2SSean Wang return err;
701e78d57b2SSean Wang
702e78d57b2SSean Wang /* Setup groups descriptions per SoC types */
703e78d57b2SSean Wang err = mtk_build_groups(hw);
7049c59fda3SAngeloGioacchino Del Regno if (err)
7059c59fda3SAngeloGioacchino Del Regno return dev_err_probe(dev, err, "Failed to build groups\n");
706e78d57b2SSean Wang
707e78d57b2SSean Wang /* Setup functions descriptions per SoC types */
708e78d57b2SSean Wang err = mtk_build_functions(hw);
7099c59fda3SAngeloGioacchino Del Regno if (err)
7109c59fda3SAngeloGioacchino Del Regno return dev_err_probe(dev, err, "Failed to build functions\n");
711e78d57b2SSean Wang
712e78d57b2SSean Wang /* For able to make pinctrl_claim_hogs, we must not enable pinctrl
713e78d57b2SSean Wang * until all groups and functions are being added one.
714e78d57b2SSean Wang */
715e78d57b2SSean Wang err = pinctrl_enable(hw->pctrl);
716e78d57b2SSean Wang if (err)
717e78d57b2SSean Wang return err;
718e78d57b2SSean Wang
719e78d57b2SSean Wang err = mtk_build_eint(hw, pdev);
720e78d57b2SSean Wang if (err)
721e78d57b2SSean Wang dev_warn(&pdev->dev,
722e78d57b2SSean Wang "Failed to add EINT, but pinctrl still can work\n");
723e78d57b2SSean Wang
724e78d57b2SSean Wang /* Build gpiochip should be after pinctrl_enable is done */
7258a8d6bbeSAndy Shevchenko err = mtk_build_gpiochip(hw);
7269c59fda3SAngeloGioacchino Del Regno if (err)
7279c59fda3SAngeloGioacchino Del Regno return dev_err_probe(dev, err, "Failed to add gpio_chip\n");
728e78d57b2SSean Wang
729e78d57b2SSean Wang platform_set_drvdata(pdev, hw);
730e78d57b2SSean Wang
731e78d57b2SSean Wang return 0;
732e78d57b2SSean Wang }
733