pinctrl-mtk-common-v2.h 11 KB

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