blob: c948b808434bbf019e700ad942257271d4a0c1c9 [file] [log] [blame]
developer84c7a632018-11-15 10:07:58 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 * Author: Ryder Lee <ryder.lee@mediatek.com>
5 */
developer84c7a632018-11-15 10:07:58 +08006#ifndef __PINCTRL_MEDIATEK_H__
7#define __PINCTRL_MEDIATEK_H__
8
developer74d69012020-01-10 16:30:28 +08009#define MTK_PINCTRL_V0 0x0
10#define MTK_PINCTRL_V1 0x1
developera7482d32022-04-21 14:23:52 +080011#define BASE_CALC_NONE 0
12#define MAX_BASE_CALC 10
developer74d69012020-01-10 16:30:28 +080013
developer84c7a632018-11-15 10:07:58 +080014#define MTK_RANGE(_a) { .range = (_a), .nranges = ARRAY_SIZE(_a), }
developerf8d22692022-04-21 14:23:53 +080015
16#define MTK_PIN(_number, _name, _drv_n) \
17 MTK_TYPED_PIN(_number, _name, _drv_n, IO_TYPE_DEFAULT)
18
19#define MTK_TYPED_PIN(_number, _name, _drv_n, _io_n) { \
developer84c7a632018-11-15 10:07:58 +080020 .number = _number, \
21 .name = _name, \
22 .drv_n = _drv_n, \
developerf8d22692022-04-21 14:23:53 +080023 .io_n = _io_n, \
developer84c7a632018-11-15 10:07:58 +080024 }
25
26#define PINCTRL_PIN_GROUP(name, id) \
27 { \
28 name, \
29 id##_pins, \
30 ARRAY_SIZE(id##_pins), \
31 id##_funcs, \
32 }
33
developera7482d32022-04-21 14:23:52 +080034#define PIN_FIELD_BASE_CALC(_s_pin, _e_pin, _i_base, _s_addr, _x_addrs, \
35 _s_bit, _x_bits, _sz_reg, _fixed) { \
developer84c7a632018-11-15 10:07:58 +080036 .s_pin = _s_pin, \
37 .e_pin = _e_pin, \
developera7482d32022-04-21 14:23:52 +080038 .i_base = _i_base, \
developer84c7a632018-11-15 10:07:58 +080039 .s_addr = _s_addr, \
40 .x_addrs = _x_addrs, \
41 .s_bit = _s_bit, \
42 .x_bits = _x_bits, \
43 .sz_reg = _sz_reg, \
44 .fixed = _fixed, \
45 }
46
developera7482d32022-04-21 14:23:52 +080047#define PIN_FIELD_CALC(_s_pin, _e_pin, _s_addr, _x_addrs, _s_bit, \
48 _x_bits, _sz_reg, _fixed) \
49 PIN_FIELD_BASE_CALC(_s_pin, _e_pin, BASE_CALC_NONE, _s_addr, \
50 _x_addrs, _s_bit, _x_bits, _sz_reg, _fixed)
51
developer84c7a632018-11-15 10:07:58 +080052/* List these attributes which could be modified for the pin */
53enum {
54 PINCTRL_PIN_REG_MODE,
55 PINCTRL_PIN_REG_DIR,
56 PINCTRL_PIN_REG_DI,
57 PINCTRL_PIN_REG_DO,
developer84c7a632018-11-15 10:07:58 +080058 PINCTRL_PIN_REG_SMT,
developer74d69012020-01-10 16:30:28 +080059 PINCTRL_PIN_REG_PD,
60 PINCTRL_PIN_REG_PU,
61 PINCTRL_PIN_REG_E4,
62 PINCTRL_PIN_REG_E8,
63 PINCTRL_PIN_REG_IES,
developer84c7a632018-11-15 10:07:58 +080064 PINCTRL_PIN_REG_PULLEN,
65 PINCTRL_PIN_REG_PULLSEL,
66 PINCTRL_PIN_REG_DRV,
David Woodhouse32102752020-06-19 12:40:20 +010067 PINCTRL_PIN_REG_PUPD,
68 PINCTRL_PIN_REG_R0,
69 PINCTRL_PIN_REG_R1,
developer84c7a632018-11-15 10:07:58 +080070 PINCTRL_PIN_REG_MAX,
71};
72
73/* Group the pins by the driving current */
74enum {
75 DRV_FIXED,
76 DRV_GRP0,
77 DRV_GRP1,
78 DRV_GRP2,
79 DRV_GRP3,
80 DRV_GRP4,
81};
82
developerf8d22692022-04-21 14:23:53 +080083/* Group the pins by the io type */
84enum {
85 IO_TYPE_DEFAULT,
86 IO_TYPE_GRP0,
87 IO_TYPE_GRP1,
88 IO_TYPE_GRP2,
89 IO_TYPE_GRP3,
90 IO_TYPE_GRP4,
91 IO_TYPE_GRP5,
92 IO_TYPE_GRP6,
93};
94
developer84c7a632018-11-15 10:07:58 +080095/**
96 * struct mtk_pin_field - the structure that holds the information of the field
97 * used to describe the attribute for the pin
developera7482d32022-04-21 14:23:52 +080098 * @index: the index pointing to the entry in base address list
developer84c7a632018-11-15 10:07:58 +080099 * @offset: the register offset relative to the base address
100 * @mask: the mask used to filter out the field from the register
101 * @bitpos: the start bit relative to the register
102 * @next: the indication that the field would be extended to the
103 next register
104 */
105struct mtk_pin_field {
developera7482d32022-04-21 14:23:52 +0800106 u8 index;
developer84c7a632018-11-15 10:07:58 +0800107 u32 offset;
108 u32 mask;
109 u8 bitpos;
110 u8 next;
111};
112
113/**
114 * struct mtk_pin_field_calc - the structure that holds the range providing
115 * the guide used to look up the relevant field
116 * @s_pin: the start pin within the range
117 * @e_pin: the end pin within the range
developera7482d32022-04-21 14:23:52 +0800118 * @i_base: the index pointing to the entry in base address list
developer84c7a632018-11-15 10:07:58 +0800119 * @s_addr: the start address for the range
120 * @x_addrs: the address distance between two consecutive registers
121 * within the range
122 * @s_bit: the start bit for the first register within the range
123 * @x_bits: the bit distance between two consecutive pins within
124 * the range
125 * @sz_reg: the size of bits in a register
126 * @fixed: the consecutive pins share the same bits with the 1st
127 * pin
128 */
129struct mtk_pin_field_calc {
130 u16 s_pin;
131 u16 e_pin;
developera7482d32022-04-21 14:23:52 +0800132 u8 i_base;
developer84c7a632018-11-15 10:07:58 +0800133 u32 s_addr;
134 u8 x_addrs;
135 u8 s_bit;
136 u8 x_bits;
137 u8 sz_reg;
138 u8 fixed;
139};
140
141/**
142 * struct mtk_pin_reg_calc - the structure that holds all ranges used to
143 * determine which register the pin would make use of
144 * for certain pin attribute.
145 * @range: the start address for the range
146 * @nranges: the number of items in the range
147 */
148struct mtk_pin_reg_calc {
149 const struct mtk_pin_field_calc *range;
150 unsigned int nranges;
151};
152
153/**
154 * struct mtk_pin_desc - the structure that providing information
155 * for each pin of chips
156 * @number: unique pin number from the global pin number space
157 * @name: name for this pin
158 * @drv_n: the index with the driving group
developerf8d22692022-04-21 14:23:53 +0800159 * @io_n: the index with the io type
developer84c7a632018-11-15 10:07:58 +0800160 */
161struct mtk_pin_desc {
162 unsigned int number;
163 const char *name;
164 u8 drv_n;
developerf8d22692022-04-21 14:23:53 +0800165 u8 io_n;
developer84c7a632018-11-15 10:07:58 +0800166};
167
168/**
169 * struct mtk_group_desc - generic pin group descriptor
170 * @name: name of the pin group
171 * @pins: array of pins that belong to the group
172 * @num_pins: number of pins in the group
173 * @data: pin controller driver specific data
174 */
175struct mtk_group_desc {
176 const char *name;
developer67873bd2023-07-19 17:16:37 +0800177 const int *pins;
developer84c7a632018-11-15 10:07:58 +0800178 int num_pins;
developer67873bd2023-07-19 17:16:37 +0800179 const void *data;
developer84c7a632018-11-15 10:07:58 +0800180};
181
182/**
183 * struct mtk_function_desc - generic function descriptor
184 * @name: name of the function
185 * @group_names: array of pin group names
186 * @num_group_names: number of pin group names
187 */
188struct mtk_function_desc {
189 const char *name;
190 const char * const *group_names;
191 int num_group_names;
192};
193
developerf8d22692022-04-21 14:23:53 +0800194/**
195 * struct mtk_io_type_desc - io class descriptor for specific pins
196 * @name: name of the io class
197 */
198struct mtk_io_type_desc {
199 const char *name;
200#if CONFIG_IS_ENABLED(PINCONF)
201 /* Specific pinconfig operations */
202 int (*bias_set)(struct udevice *dev, u32 pin, bool disable,
203 bool pullup, u32 val);
204 int (*drive_set)(struct udevice *dev, u32 pin, u32 arg);
205 int (*input_enable)(struct udevice *dev, u32 pin, u32 arg);
206#endif
207};
208
developer84c7a632018-11-15 10:07:58 +0800209/* struct mtk_pin_soc - the structure that holds SoC-specific data */
210struct mtk_pinctrl_soc {
211 const char *name;
212 const struct mtk_pin_reg_calc *reg_cal;
213 const struct mtk_pin_desc *pins;
214 int npins;
215 const struct mtk_group_desc *grps;
216 int ngrps;
217 const struct mtk_function_desc *funcs;
218 int nfuncs;
developerf8d22692022-04-21 14:23:53 +0800219 const struct mtk_io_type_desc *io_type;
220 u8 ntype;
developer74d69012020-01-10 16:30:28 +0800221 int gpio_mode;
developera7482d32022-04-21 14:23:52 +0800222 const char * const *base_names;
223 unsigned int nbase_names;
developer74d69012020-01-10 16:30:28 +0800224 int rev;
developera7482d32022-04-21 14:23:52 +0800225 int base_calc;
developer84c7a632018-11-15 10:07:58 +0800226};
227
228/**
229 * struct mtk_pinctrl_priv - private data for MTK pinctrl driver
230 *
231 * @base: base address of the pinctrl device
232 * @soc: SoC specific data
233 */
234struct mtk_pinctrl_priv {
developera7482d32022-04-21 14:23:52 +0800235 void __iomem *base[MAX_BASE_CALC];
developer67873bd2023-07-19 17:16:37 +0800236 const struct mtk_pinctrl_soc *soc;
developer84c7a632018-11-15 10:07:58 +0800237};
238
239extern const struct pinctrl_ops mtk_pinctrl_ops;
240
developer5c1111c2018-11-15 10:07:59 +0800241/* A common read-modify-write helper for MediaTek chips */
242void mtk_rmw(struct udevice *dev, u32 reg, u32 mask, u32 set);
developera7482d32022-04-21 14:23:52 +0800243void mtk_i_rmw(struct udevice *dev, u8 i, u32 reg, u32 mask, u32 set);
developer84c7a632018-11-15 10:07:58 +0800244int mtk_pinctrl_common_probe(struct udevice *dev,
developer67873bd2023-07-19 17:16:37 +0800245 const struct mtk_pinctrl_soc *soc);
developer84c7a632018-11-15 10:07:58 +0800246
developer6e84c2f2022-04-21 14:23:51 +0800247#if CONFIG_IS_ENABLED(PINCONF)
248
249int mtk_pinconf_bias_set_pu_pd(struct udevice *dev, u32 pin, bool disable,
250 bool pullup, u32 val);
251int mtk_pinconf_bias_set_pullen_pullsel(struct udevice *dev, u32 pin,
252 bool disable, bool pullup, u32 val);
253int mtk_pinconf_bias_set_pupd_r1_r0(struct udevice *dev, u32 pin, bool disable,
254 bool pullup, u32 val);
255int mtk_pinconf_bias_set_v0(struct udevice *dev, u32 pin, bool disable,
256 bool pullup, u32 val);
257int mtk_pinconf_bias_set_v1(struct udevice *dev, u32 pin, bool disable,
258 bool pullup, u32 val);
259int mtk_pinconf_input_enable_v1(struct udevice *dev, u32 pin, u32 arg);
260int mtk_pinconf_drive_set_v0(struct udevice *dev, u32 pin, u32 arg);
261int mtk_pinconf_drive_set_v1(struct udevice *dev, u32 pin, u32 arg);
262
263#endif
264
developer84c7a632018-11-15 10:07:58 +0800265#endif /* __PINCTRL_MEDIATEK_H__ */