developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Copyright (C) 2018 MediaTek Inc. |
| 4 | * Author: Ryder Lee <ryder.lee@mediatek.com> |
| 5 | */ |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 6 | #ifndef __PINCTRL_MEDIATEK_H__ |
| 7 | #define __PINCTRL_MEDIATEK_H__ |
| 8 | |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 9 | #define MTK_PINCTRL_V0 0x0 |
| 10 | #define MTK_PINCTRL_V1 0x1 |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 11 | #define BASE_CALC_NONE 0 |
| 12 | #define MAX_BASE_CALC 10 |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 13 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 14 | #define MTK_RANGE(_a) { .range = (_a), .nranges = ARRAY_SIZE(_a), } |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 15 | |
| 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) { \ |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 20 | .number = _number, \ |
| 21 | .name = _name, \ |
| 22 | .drv_n = _drv_n, \ |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 23 | .io_n = _io_n, \ |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | #define PINCTRL_PIN_GROUP(name, id) \ |
| 27 | { \ |
| 28 | name, \ |
| 29 | id##_pins, \ |
| 30 | ARRAY_SIZE(id##_pins), \ |
| 31 | id##_funcs, \ |
| 32 | } |
| 33 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 34 | #define PIN_FIELD_BASE_CALC(_s_pin, _e_pin, _i_base, _s_addr, _x_addrs, \ |
| 35 | _s_bit, _x_bits, _sz_reg, _fixed) { \ |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 36 | .s_pin = _s_pin, \ |
| 37 | .e_pin = _e_pin, \ |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 38 | .i_base = _i_base, \ |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 39 | .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 | |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 47 | #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 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 52 | /* List these attributes which could be modified for the pin */ |
| 53 | enum { |
| 54 | PINCTRL_PIN_REG_MODE, |
| 55 | PINCTRL_PIN_REG_DIR, |
| 56 | PINCTRL_PIN_REG_DI, |
| 57 | PINCTRL_PIN_REG_DO, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 58 | PINCTRL_PIN_REG_SMT, |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 59 | PINCTRL_PIN_REG_PD, |
| 60 | PINCTRL_PIN_REG_PU, |
| 61 | PINCTRL_PIN_REG_E4, |
| 62 | PINCTRL_PIN_REG_E8, |
| 63 | PINCTRL_PIN_REG_IES, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 64 | PINCTRL_PIN_REG_PULLEN, |
| 65 | PINCTRL_PIN_REG_PULLSEL, |
| 66 | PINCTRL_PIN_REG_DRV, |
David Woodhouse | 3210275 | 2020-06-19 12:40:20 +0100 | [diff] [blame] | 67 | PINCTRL_PIN_REG_PUPD, |
| 68 | PINCTRL_PIN_REG_R0, |
| 69 | PINCTRL_PIN_REG_R1, |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 70 | PINCTRL_PIN_REG_MAX, |
| 71 | }; |
| 72 | |
| 73 | /* Group the pins by the driving current */ |
| 74 | enum { |
| 75 | DRV_FIXED, |
| 76 | DRV_GRP0, |
| 77 | DRV_GRP1, |
| 78 | DRV_GRP2, |
| 79 | DRV_GRP3, |
| 80 | DRV_GRP4, |
| 81 | }; |
| 82 | |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 83 | /* Group the pins by the io type */ |
| 84 | enum { |
| 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 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 95 | /** |
| 96 | * struct mtk_pin_field - the structure that holds the information of the field |
| 97 | * used to describe the attribute for the pin |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 98 | * @index: the index pointing to the entry in base address list |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 99 | * @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 | */ |
| 105 | struct mtk_pin_field { |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 106 | u8 index; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 107 | 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 |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 118 | * @i_base: the index pointing to the entry in base address list |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 119 | * @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 | */ |
| 129 | struct mtk_pin_field_calc { |
| 130 | u16 s_pin; |
| 131 | u16 e_pin; |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 132 | u8 i_base; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 133 | 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 | */ |
| 148 | struct 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 |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 159 | * @io_n: the index with the io type |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 160 | */ |
| 161 | struct mtk_pin_desc { |
| 162 | unsigned int number; |
| 163 | const char *name; |
| 164 | u8 drv_n; |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 165 | u8 io_n; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 166 | }; |
| 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 | */ |
| 175 | struct mtk_group_desc { |
| 176 | const char *name; |
| 177 | int *pins; |
| 178 | int num_pins; |
| 179 | void *data; |
| 180 | }; |
| 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 | */ |
| 188 | struct mtk_function_desc { |
| 189 | const char *name; |
| 190 | const char * const *group_names; |
| 191 | int num_group_names; |
| 192 | }; |
| 193 | |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 194 | /** |
| 195 | * struct mtk_io_type_desc - io class descriptor for specific pins |
| 196 | * @name: name of the io class |
| 197 | */ |
| 198 | struct 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 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 209 | /* struct mtk_pin_soc - the structure that holds SoC-specific data */ |
| 210 | struct 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; |
developer | f8d2269 | 2022-04-21 14:23:53 +0800 | [diff] [blame^] | 219 | const struct mtk_io_type_desc *io_type; |
| 220 | u8 ntype; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 221 | int gpio_mode; |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 222 | const char * const *base_names; |
| 223 | unsigned int nbase_names; |
developer | 74d6901 | 2020-01-10 16:30:28 +0800 | [diff] [blame] | 224 | int rev; |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 225 | int base_calc; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 226 | }; |
| 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 | */ |
| 234 | struct mtk_pinctrl_priv { |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 235 | void __iomem *base[MAX_BASE_CALC]; |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 236 | struct mtk_pinctrl_soc *soc; |
| 237 | }; |
| 238 | |
| 239 | extern const struct pinctrl_ops mtk_pinctrl_ops; |
| 240 | |
developer | 5c1111c | 2018-11-15 10:07:59 +0800 | [diff] [blame] | 241 | /* A common read-modify-write helper for MediaTek chips */ |
| 242 | void mtk_rmw(struct udevice *dev, u32 reg, u32 mask, u32 set); |
developer | a7482d3 | 2022-04-21 14:23:52 +0800 | [diff] [blame] | 243 | void mtk_i_rmw(struct udevice *dev, u8 i, u32 reg, u32 mask, u32 set); |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 244 | int mtk_pinctrl_common_probe(struct udevice *dev, |
| 245 | struct mtk_pinctrl_soc *soc); |
| 246 | |
developer | 6e84c2f | 2022-04-21 14:23:51 +0800 | [diff] [blame] | 247 | #if CONFIG_IS_ENABLED(PINCONF) |
| 248 | |
| 249 | int mtk_pinconf_bias_set_pu_pd(struct udevice *dev, u32 pin, bool disable, |
| 250 | bool pullup, u32 val); |
| 251 | int mtk_pinconf_bias_set_pullen_pullsel(struct udevice *dev, u32 pin, |
| 252 | bool disable, bool pullup, u32 val); |
| 253 | int mtk_pinconf_bias_set_pupd_r1_r0(struct udevice *dev, u32 pin, bool disable, |
| 254 | bool pullup, u32 val); |
| 255 | int mtk_pinconf_bias_set_v0(struct udevice *dev, u32 pin, bool disable, |
| 256 | bool pullup, u32 val); |
| 257 | int mtk_pinconf_bias_set_v1(struct udevice *dev, u32 pin, bool disable, |
| 258 | bool pullup, u32 val); |
| 259 | int mtk_pinconf_input_enable_v1(struct udevice *dev, u32 pin, u32 arg); |
| 260 | int mtk_pinconf_drive_set_v0(struct udevice *dev, u32 pin, u32 arg); |
| 261 | int mtk_pinconf_drive_set_v1(struct udevice *dev, u32 pin, u32 arg); |
| 262 | |
| 263 | #endif |
| 264 | |
developer | 84c7a63 | 2018-11-15 10:07:58 +0800 | [diff] [blame] | 265 | #endif /* __PINCTRL_MEDIATEK_H__ */ |