blob: 841232dd7ddd478f28efd25d42f63f6f6867acbe [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 *
5 * Author: Sean Wang <sean.wang@mediatek.com>
6 *
7 */
8
9#ifndef __PINCTRL_MTK_COMMON_V2_H
10#define __PINCTRL_MTK_COMMON_V2_H
11
12#include <linux/gpio/driver.h>
13
14#define MTK_INPUT 0
15#define MTK_OUTPUT 1
16#define MTK_DISABLE 0
17#define MTK_ENABLE 1
18#define MTK_PULLDOWN 0
19#define MTK_PULLUP 1
20
21#define EINT_NA U16_MAX
22#define NO_EINT_SUPPORT EINT_NA
23
24#define PIN_FIELD_CALC(_s_pin, _e_pin, _i_base, _s_addr, _x_addrs, \
25 _s_bit, _x_bits, _sz_reg, _fixed) { \
26 .s_pin = _s_pin, \
27 .e_pin = _e_pin, \
28 .i_base = _i_base, \
29 .s_addr = _s_addr, \
30 .x_addrs = _x_addrs, \
31 .s_bit = _s_bit, \
32 .x_bits = _x_bits, \
33 .sz_reg = _sz_reg, \
34 .fixed = _fixed, \
35 }
36
37#define PIN_FIELD(_s_pin, _e_pin, _s_addr, _x_addrs, _s_bit, _x_bits) \
38 PIN_FIELD_CALC(_s_pin, _e_pin, 0, _s_addr, _x_addrs, _s_bit, \
39 _x_bits, 32, 0)
40
41#define PINS_FIELD(_s_pin, _e_pin, _s_addr, _x_addrs, _s_bit, _x_bits) \
42 PIN_FIELD_CALC(_s_pin, _e_pin, 0, _s_addr, _x_addrs, _s_bit, \
43 _x_bits, 32, 1)
44
45/* List these attributes which could be modified for the pin */
46enum {
47 PINCTRL_PIN_REG_MODE,
48 PINCTRL_PIN_REG_DIR,
49 PINCTRL_PIN_REG_DI,
50 PINCTRL_PIN_REG_DO,
51 PINCTRL_PIN_REG_SR,
52 PINCTRL_PIN_REG_SMT,
53 PINCTRL_PIN_REG_PD,
54 PINCTRL_PIN_REG_PU,
55 PINCTRL_PIN_REG_E4,
56 PINCTRL_PIN_REG_E8,
57 PINCTRL_PIN_REG_TDSEL,
58 PINCTRL_PIN_REG_RDSEL,
59 PINCTRL_PIN_REG_DRV,
60 PINCTRL_PIN_REG_PUPD,
61 PINCTRL_PIN_REG_R0,
62 PINCTRL_PIN_REG_R1,
63 PINCTRL_PIN_REG_IES,
64 PINCTRL_PIN_REG_PULLEN,
65 PINCTRL_PIN_REG_PULLSEL,
66 PINCTRL_PIN_REG_DRV_EH,
67 PINCTRL_PIN_REG_DRV_EN,
68 PINCTRL_PIN_REG_DRV_E0,
69 PINCTRL_PIN_REG_DRV_E1,
70 PINCTRL_PIN_REG_RES_SEL,
71 PINCTRL_PIN_REG_MAX,
72};
73
74/* Group the pins by the driving current */
75enum {
76 DRV_FIXED,
77 DRV_GRP0,
78 DRV_GRP1,
79 DRV_GRP2,
80 DRV_GRP3,
81 DRV_GRP4,
82 DRV_GRP_MAX,
83};
84
85static const char * const mtk_default_register_base_names[] = {
86 "base",
87};
88
89/* struct mtk_pin_field - the structure that holds the information of the field
90 * used to describe the attribute for the pin
91 * @base: the index pointing to the entry in base address list
92 * @offset: the register offset relative to the base address
93 * @mask: the mask used to filter out the field from the register
94 * @bitpos: the start bit relative to the register
95 * @next: the indication that the field would be extended to the
96 next register
97 */
98struct mtk_pin_field {
99 u8 index;
100 u32 offset;
101 u32 mask;
102 u8 bitpos;
103 u8 next;
104};
105
106/* struct mtk_pin_field_calc - the structure that holds the range providing
107 * the guide used to look up the relevant field
108 * @s_pin: the start pin within the range
109 * @e_pin: the end pin within the range
110 * @i_base: the index pointing to the entry in base address list
111 * @s_addr: the start address for the range
112 * @x_addrs: the address distance between two consecutive registers
113 * within the range
114 * @s_bit: the start bit for the first register within the range
115 * @x_bits: the bit distance between two consecutive pins within
116 * the range
117 * @sz_reg: the size of bits in a register
118 * @fixed: the consecutive pins share the same bits with the 1st
119 * pin
120 */
121struct mtk_pin_field_calc {
122 u16 s_pin;
123 u16 e_pin;
124 u8 i_base;
125 u32 s_addr;
126 u8 x_addrs;
127 u8 s_bit;
128 u8 x_bits;
129 u8 sz_reg;
130 u8 fixed;
131};
132
133/* struct mtk_pin_reg_calc - the structure that holds all ranges used to
134 * determine which register the pin would make use of
135 * for certain pin attribute.
136 * @range: the start address for the range
137 * @nranges: the number of items in the range
138 */
139struct mtk_pin_reg_calc {
140 const struct mtk_pin_field_calc *range;
141 unsigned int nranges;
142};
143
144/**
145 * struct mtk_func_desc - the structure that providing information
146 * all the funcs for this pin
147 * @name: the name of function
148 * @muxval: the mux to the function
149 */
150struct mtk_func_desc {
151 const char *name;
152 u8 muxval;
153};
154
155/**
156 * struct mtk_eint_desc - the structure that providing information
157 * for eint data per pin
158 * @eint_m: the eint mux for this pin
159 * @eitn_n: the eint number for this pin
160 */
161struct mtk_eint_desc {
162 u16 eint_m;
163 u16 eint_n;
164};
165
166/**
167 * struct mtk_eh_pin_pinmux - entry recording (pin, pinmux) whose
168 * eh can be enabled
169 * @pin: pin numbereint mux for this pin
170 * @pinmux: pinmux number
171 */
172struct mtk_eh_pin_pinmux {
173 u16 pin;
174 u16 pinmux;
175};
176
177/**
178 * struct mtk_pin_desc - the structure that providing information
179 * for each pin of chips
180 * @number: unique pin number from the global pin number space
181 * @name: name for this pin
182 * @eint: the eint data for this pin
183 * @drv_n: the index with the driving group
184 * @funcs: all available functions for this pins (only used in
185 * those drivers compatible to pinctrl-mtk-common.c-like
186 * ones)
187 */
188struct mtk_pin_desc {
189 unsigned int number;
190 const char *name;
191 struct mtk_eint_desc eint;
192 u8 drv_n;
193 struct mtk_func_desc *funcs;
194};
195
196struct mtk_pinctrl_group {
197 const char *name;
198 unsigned long config;
199 unsigned pin;
200};
201
202struct mtk_pinctrl;
203
204/* struct mtk_pin_soc - the structure that holds SoC-specific data */
205struct mtk_pin_soc {
206 const struct mtk_pin_reg_calc *reg_cal;
207 const struct mtk_pin_desc *pins;
208 unsigned int npins;
209 const struct group_desc *grps;
210 unsigned int ngrps;
211 const struct function_desc *funcs;
212 unsigned int nfuncs;
213 const struct mtk_eint_regs *eint_regs;
214 const struct mtk_eint_hw *eint_hw;
215
216 /* Specific parameters per SoC */
217 u8 gpio_m;
218 bool ies_present;
219 bool race_free_access;
220 const char * const *base_names;
221 unsigned int nbase_names;
222 const struct mtk_eh_pin_pinmux *eh_pin_pinmux;
223 unsigned int neh_pins;
224
225 /* Specific pinconfig operations */
226 int (*bias_disable_set)(struct mtk_pinctrl *hw,
227 const struct mtk_pin_desc *desc);
228 int (*bias_disable_get)(struct mtk_pinctrl *hw,
229 const struct mtk_pin_desc *desc, int *res);
230 int (*bias_set)(struct mtk_pinctrl *hw,
231 const struct mtk_pin_desc *desc, bool pullup);
232 int (*bias_get)(struct mtk_pinctrl *hw,
233 const struct mtk_pin_desc *desc, bool pullup, int *res);
234
235 int (*bias_set_combo)(struct mtk_pinctrl *hw,
236 const struct mtk_pin_desc *desc, u32 pullup, u32 arg);
237 int (*bias_get_combo)(struct mtk_pinctrl *hw,
238 const struct mtk_pin_desc *desc, u32 *pullup, u32 *arg);
239
240 int (*drive_set)(struct mtk_pinctrl *hw,
241 const struct mtk_pin_desc *desc, u32 arg);
242 int (*drive_get)(struct mtk_pinctrl *hw,
243 const struct mtk_pin_desc *desc, int *val);
244
245 int (*adv_pull_set)(struct mtk_pinctrl *hw,
246 const struct mtk_pin_desc *desc, bool pullup,
247 u32 arg);
248 int (*adv_pull_get)(struct mtk_pinctrl *hw,
249 const struct mtk_pin_desc *desc, bool pullup,
250 u32 *val);
251 int (*adv_drive_set)(struct mtk_pinctrl *hw,
252 const struct mtk_pin_desc *desc, u32 arg);
253 int (*adv_drive_get)(struct mtk_pinctrl *hw,
254 const struct mtk_pin_desc *desc, u32 *val);
255
256 /* Specific driver data */
257 void *driver_data;
258};
259
260struct mtk_pinctrl {
261 struct pinctrl_dev *pctrl;
262 void __iomem **base;
263 u8 nbase;
264 struct device *dev;
265 struct gpio_chip chip;
266 const struct mtk_pin_soc *soc;
267 struct mtk_eint *eint;
268 struct mtk_pinctrl_group *groups;
269 const char **grp_names;
270};
271
272void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set);
273
274int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
275 int field, int value);
276int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
277 int field, int *value);
278
279int mtk_eh_ctrl(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
280 u16 mode);
281
282int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev);
283
284int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
285 const struct mtk_pin_desc *desc);
286int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
287 const struct mtk_pin_desc *desc, int *res);
288int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
289 const struct mtk_pin_desc *desc, bool pullup);
290int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
291 const struct mtk_pin_desc *desc, bool pullup,
292 int *res);
293
294int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
295 const struct mtk_pin_desc *desc);
296int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
297 const struct mtk_pin_desc *desc,
298 int *res);
299int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
300 const struct mtk_pin_desc *desc, bool pullup);
301int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
302 const struct mtk_pin_desc *desc, bool pullup,
303 int *res);
304
305int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
306 const struct mtk_pin_desc *desc, u32 arg);
307int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
308 const struct mtk_pin_desc *desc, int *val);
309
310int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
311 const struct mtk_pin_desc *desc, u32 arg);
312int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
313 const struct mtk_pin_desc *desc, int *val);
314
315int mtk_pinconf_drive_set_raw(struct mtk_pinctrl *hw,
316 const struct mtk_pin_desc *desc, u32 arg);
317int mtk_pinconf_drive_get_raw(struct mtk_pinctrl *hw,
318 const struct mtk_pin_desc *desc, int *val);
319
320int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
321 const struct mtk_pin_desc *desc, bool pullup,
322 u32 arg);
323int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
324 const struct mtk_pin_desc *desc, bool pullup,
325 u32 *val);
326int mtk_pinconf_adv_drive_set(struct mtk_pinctrl *hw,
327 const struct mtk_pin_desc *desc, u32 arg);
328int mtk_pinconf_adv_drive_get(struct mtk_pinctrl *hw,
329 const struct mtk_pin_desc *desc, u32 *val);
330
331int mtk_pinconf_bias_set_combo(struct mtk_pinctrl *hw,
332 const struct mtk_pin_desc *desc,
333 u32 pullup, u32 enable);
334
335int mtk_pinconf_bias_get_combo(struct mtk_pinctrl *hw,
336 const struct mtk_pin_desc *desc,
337 u32 *pullup, u32 *enable);
338
339bool mtk_is_virt_gpio(struct mtk_pinctrl *hw, unsigned int gpio_n);
340#endif /* __PINCTRL_MTK_COMMON_V2_H */