1 /* 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 */
46 enum {
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_MAX,
67 };
68 
69 /* Group the pins by the driving current */
70 enum {
71 	DRV_FIXED,
72 	DRV_GRP0,
73 	DRV_GRP1,
74 	DRV_GRP2,
75 	DRV_GRP3,
76 	DRV_GRP4,
77 	DRV_GRP_MAX,
78 };
79 
80 static const char * const mtk_default_register_base_names[] = {
81 	"base",
82 };
83 
84 /* struct mtk_pin_field - the structure that holds the information of the field
85  *			  used to describe the attribute for the pin
86  * @base:		the index pointing to the entry in base address list
87  * @offset:		the register offset relative to the base address
88  * @mask:		the mask used to filter out the field from the register
89  * @bitpos:		the start bit relative to the register
90  * @next:		the indication that the field would be extended to the
91 			next register
92  */
93 struct mtk_pin_field {
94 	u8  index;
95 	u32 offset;
96 	u32 mask;
97 	u8  bitpos;
98 	u8  next;
99 };
100 
101 /* struct mtk_pin_field_calc - the structure that holds the range providing
102  *			       the guide used to look up the relevant field
103  * @s_pin:		the start pin within the range
104  * @e_pin:		the end pin within the range
105  * @i_base:		the index pointing to the entry in base address list
106  * @s_addr:		the start address for the range
107  * @x_addrs:		the address distance between two consecutive registers
108  *			within the range
109  * @s_bit:		the start bit for the first register within the range
110  * @x_bits:		the bit distance between two consecutive pins within
111  *			the range
112  * @sz_reg:		the size of bits in a register
113  * @fixed:		the consecutive pins share the same bits with the 1st
114  *			pin
115  */
116 struct mtk_pin_field_calc {
117 	u16 s_pin;
118 	u16 e_pin;
119 	u8  i_base;
120 	u32 s_addr;
121 	u8  x_addrs;
122 	u8  s_bit;
123 	u8  x_bits;
124 	u8  sz_reg;
125 	u8  fixed;
126 };
127 
128 /* struct mtk_pin_reg_calc - the structure that holds all ranges used to
129  *			     determine which register the pin would make use of
130  *			     for certain pin attribute.
131  * @range:		     the start address for the range
132  * @nranges:		     the number of items in the range
133  */
134 struct mtk_pin_reg_calc {
135 	const struct mtk_pin_field_calc *range;
136 	unsigned int nranges;
137 };
138 
139 /**
140  * struct mtk_func_desc - the structure that providing information
141  *			  all the funcs for this pin
142  * @name:		the name of function
143  * @muxval:		the mux to the function
144  */
145 struct mtk_func_desc {
146 	const char *name;
147 	u8 muxval;
148 };
149 
150 /**
151  * struct mtk_eint_desc - the structure that providing information
152  *			       for eint data per pin
153  * @eint_m:		the eint mux for this pin
154  * @eitn_n:		the eint number for this pin
155  */
156 struct mtk_eint_desc {
157 	u16 eint_m;
158 	u16 eint_n;
159 };
160 
161 /**
162  * struct mtk_pin_desc - the structure that providing information
163  *			       for each pin of chips
164  * @number:		unique pin number from the global pin number space
165  * @name:		name for this pin
166  * @eint:		the eint data for this pin
167  * @drv_n:		the index with the driving group
168  * @funcs:		all available functions for this pins (only used in
169  *			those drivers compatible to pinctrl-mtk-common.c-like
170  *			ones)
171  */
172 struct mtk_pin_desc {
173 	unsigned int number;
174 	const char *name;
175 	struct mtk_eint_desc eint;
176 	u8 drv_n;
177 	struct mtk_func_desc *funcs;
178 };
179 
180 struct mtk_pinctrl_group {
181 	const char	*name;
182 	unsigned long	config;
183 	unsigned	pin;
184 };
185 
186 struct mtk_pinctrl;
187 
188 /* struct mtk_pin_soc - the structure that holds SoC-specific data */
189 struct mtk_pin_soc {
190 	const struct mtk_pin_reg_calc	*reg_cal;
191 	const struct mtk_pin_desc	*pins;
192 	unsigned int			npins;
193 	const struct group_desc		*grps;
194 	unsigned int			ngrps;
195 	const struct function_desc	*funcs;
196 	unsigned int			nfuncs;
197 	const struct mtk_eint_regs	*eint_regs;
198 	const struct mtk_eint_hw	*eint_hw;
199 
200 	/* Specific parameters per SoC */
201 	u8				gpio_m;
202 	bool				ies_present;
203 	const char * const		*base_names;
204 	unsigned int			nbase_names;
205 
206 	/* Specific pinconfig operations */
207 	int (*bias_disable_set)(struct mtk_pinctrl *hw,
208 				const struct mtk_pin_desc *desc);
209 	int (*bias_disable_get)(struct mtk_pinctrl *hw,
210 				const struct mtk_pin_desc *desc, int *res);
211 	int (*bias_set)(struct mtk_pinctrl *hw,
212 			const struct mtk_pin_desc *desc, bool pullup);
213 	int (*bias_get)(struct mtk_pinctrl *hw,
214 			const struct mtk_pin_desc *desc, bool pullup, int *res);
215 
216 	int (*drive_set)(struct mtk_pinctrl *hw,
217 			 const struct mtk_pin_desc *desc, u32 arg);
218 	int (*drive_get)(struct mtk_pinctrl *hw,
219 			 const struct mtk_pin_desc *desc, int *val);
220 
221 	int (*adv_pull_set)(struct mtk_pinctrl *hw,
222 			    const struct mtk_pin_desc *desc, bool pullup,
223 			    u32 arg);
224 	int (*adv_pull_get)(struct mtk_pinctrl *hw,
225 			    const struct mtk_pin_desc *desc, bool pullup,
226 			    u32 *val);
227 
228 	/* Specific driver data */
229 	void				*driver_data;
230 };
231 
232 struct mtk_pinctrl {
233 	struct pinctrl_dev		*pctrl;
234 	void __iomem			**base;
235 	u8				nbase;
236 	struct device			*dev;
237 	struct gpio_chip		chip;
238 	const struct mtk_pin_soc        *soc;
239 	struct mtk_eint			*eint;
240 	struct mtk_pinctrl_group	*groups;
241 	const char          **grp_names;
242 };
243 
244 void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set);
245 
246 int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
247 		     int field, int value);
248 int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
249 		     int field, int *value);
250 
251 int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev);
252 
253 int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
254 				 const struct mtk_pin_desc *desc);
255 int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
256 				 const struct mtk_pin_desc *desc, int *res);
257 int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
258 			 const struct mtk_pin_desc *desc, bool pullup);
259 int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
260 			 const struct mtk_pin_desc *desc, bool pullup,
261 			 int *res);
262 
263 int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
264 				      const struct mtk_pin_desc *desc);
265 int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
266 				      const struct mtk_pin_desc *desc,
267 				      int *res);
268 int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
269 			      const struct mtk_pin_desc *desc, bool pullup);
270 int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
271 			      const struct mtk_pin_desc *desc, bool pullup,
272 			      int *res);
273 
274 int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
275 			  const struct mtk_pin_desc *desc, u32 arg);
276 int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
277 			  const struct mtk_pin_desc *desc, int *val);
278 
279 int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
280 			       const struct mtk_pin_desc *desc, u32 arg);
281 int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
282 			       const struct mtk_pin_desc *desc, int *val);
283 
284 int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
285 			     const struct mtk_pin_desc *desc, bool pullup,
286 			     u32 arg);
287 int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
288 			     const struct mtk_pin_desc *desc, bool pullup,
289 			     u32 *val);
290 
291 #endif /* __PINCTRL_MTK_COMMON_V2_H */
292