pinctrl-mtk-common.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __PINCTRL_MTK_COMMON_H
  15. #define __PINCTRL_MTK_COMMON_H
  16. #include <linux/pinctrl/pinctrl.h>
  17. #include <linux/regmap.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #define NO_EINT_SUPPORT 255
  20. #define MTK_CHIP_TYPE_BASE 0
  21. #define MT_EDGE_SENSITIVE 0
  22. #define MT_LEVEL_SENSITIVE 1
  23. #define EINT_DBNC_SET_DBNC_BITS 4
  24. #define EINT_DBNC_RST_BIT (0x1 << 1)
  25. #define EINT_DBNC_SET_EN (0x1 << 0)
  26. #define MTK_PINCTRL_NOT_SUPPORT (0xffff)
  27. struct mtk_desc_function {
  28. const char *name;
  29. unsigned char muxval;
  30. };
  31. struct mtk_desc_eint {
  32. unsigned char eintmux;
  33. unsigned char eintnum;
  34. };
  35. struct mtk_desc_pin {
  36. struct pinctrl_pin_desc pin;
  37. const char *chip;
  38. const struct mtk_desc_eint eint;
  39. const struct mtk_desc_function *functions;
  40. };
  41. #define MTK_PIN(_pin, _pad, _chip, _eint, ...) \
  42. { \
  43. .pin = _pin, \
  44. .chip = _chip, \
  45. .eint = _eint, \
  46. .functions = (struct mtk_desc_function[]){ \
  47. __VA_ARGS__, { } }, \
  48. }
  49. #define MTK_EINT_FUNCTION(_eintmux, _eintnum) \
  50. { \
  51. .eintmux = _eintmux, \
  52. .eintnum = _eintnum, \
  53. }
  54. #define MTK_FUNCTION(_val, _name) \
  55. { \
  56. .muxval = _val, \
  57. .name = _name, \
  58. }
  59. #define SET_ADDR(x, y) (x + (y->devdata->port_align))
  60. #define CLR_ADDR(x, y) (x + (y->devdata->port_align << 1))
  61. struct mtk_pinctrl_group {
  62. const char *name;
  63. unsigned long config;
  64. unsigned pin;
  65. };
  66. /**
  67. * struct mtk_drv_group_desc - Provide driving group data.
  68. * @max_drv: The maximum current of this group.
  69. * @min_drv: The minimum current of this group.
  70. * @low_bit: The lowest bit of this group.
  71. * @high_bit: The highest bit of this group.
  72. * @step: The step current of this group.
  73. */
  74. struct mtk_drv_group_desc {
  75. unsigned char min_drv;
  76. unsigned char max_drv;
  77. unsigned char low_bit;
  78. unsigned char high_bit;
  79. unsigned char step;
  80. };
  81. #define MTK_DRV_GRP(_min, _max, _low, _high, _step) \
  82. { \
  83. .min_drv = _min, \
  84. .max_drv = _max, \
  85. .low_bit = _low, \
  86. .high_bit = _high, \
  87. .step = _step, \
  88. }
  89. /**
  90. * struct mtk_pin_drv_grp - Provide each pin driving info.
  91. * @pin: The pin number.
  92. * @offset: The offset of driving register for this pin.
  93. * @bit: The bit of driving register for this pin.
  94. * @grp: The group for this pin belongs to.
  95. */
  96. struct mtk_pin_drv_grp {
  97. unsigned short pin;
  98. unsigned short offset;
  99. unsigned char bit;
  100. unsigned char grp;
  101. };
  102. #define MTK_PIN_DRV_GRP(_pin, _offset, _bit, _grp) \
  103. { \
  104. .pin = _pin, \
  105. .offset = _offset, \
  106. .bit = _bit, \
  107. .grp = _grp, \
  108. }
  109. /**
  110. * struct mtk_pin_spec_pupd_set_samereg
  111. * - For special pins' pull up/down setting which resides in same register
  112. * @pin: The pin number.
  113. * @offset: The offset of special pull up/down setting register.
  114. * @pupd_bit: The pull up/down bit in this register.
  115. * @r0_bit: The r0 bit of pull resistor.
  116. * @r1_bit: The r1 bit of pull resistor.
  117. */
  118. struct mtk_pin_spec_pupd_set_samereg {
  119. unsigned short pin;
  120. unsigned short offset;
  121. unsigned char pupd_bit;
  122. unsigned char r1_bit;
  123. unsigned char r0_bit;
  124. };
  125. #define MTK_PIN_PUPD_SPEC_SR(_pin, _offset, _pupd, _r1, _r0) \
  126. { \
  127. .pin = _pin, \
  128. .offset = _offset, \
  129. .pupd_bit = _pupd, \
  130. .r1_bit = _r1, \
  131. .r0_bit = _r0, \
  132. }
  133. /**
  134. * struct mtk_pin_ies_set - For special pins' ies and smt setting.
  135. * @start: The start pin number of those special pins.
  136. * @end: The end pin number of those special pins.
  137. * @offset: The offset of special setting register.
  138. * @bit: The bit of special setting register.
  139. */
  140. struct mtk_pin_ies_smt_set {
  141. unsigned short start;
  142. unsigned short end;
  143. unsigned short offset;
  144. unsigned char bit;
  145. };
  146. #define MTK_PIN_IES_SMT_SPEC(_start, _end, _offset, _bit) \
  147. { \
  148. .start = _start, \
  149. .end = _end, \
  150. .bit = _bit, \
  151. .offset = _offset, \
  152. }
  153. struct mtk_eint_offsets {
  154. const char *name;
  155. unsigned int stat;
  156. unsigned int ack;
  157. unsigned int mask;
  158. unsigned int mask_set;
  159. unsigned int mask_clr;
  160. unsigned int sens;
  161. unsigned int sens_set;
  162. unsigned int sens_clr;
  163. unsigned int soft;
  164. unsigned int soft_set;
  165. unsigned int soft_clr;
  166. unsigned int pol;
  167. unsigned int pol_set;
  168. unsigned int pol_clr;
  169. unsigned int dom_en;
  170. unsigned int dbnc_ctrl;
  171. unsigned int dbnc_set;
  172. unsigned int dbnc_clr;
  173. u8 port_mask;
  174. u8 ports;
  175. };
  176. /**
  177. * struct mtk_pinctrl_devdata - Provide HW GPIO related data.
  178. * @pins: An array describing all pins the pin controller affects.
  179. * @npins: The number of entries in @pins.
  180. *
  181. * @grp_desc: The driving group info.
  182. * @pin_drv_grp: The driving group for all pins.
  183. * @spec_pull_set: Each SoC may have special pins for pull up/down setting,
  184. * these pins' pull setting are very different, they have separate pull
  185. * up/down bit, R0 and R1 resistor bit, so they need special pull setting.
  186. * If special setting is success, this should return 0, otherwise it should
  187. * return non-zero value.
  188. * @spec_ies_smt_set: Some pins are irregular, their input enable and smt
  189. * control register are discontinuous, but they are mapping together. That
  190. * means when user set smt, input enable is set at the same time. So they
  191. * also need special control. If special control is success, this should
  192. * return 0, otherwise return non-zero value.
  193. *
  194. * @dir_offset: The direction register offset.
  195. * @pullen_offset: The pull-up/pull-down enable register offset.
  196. * @pinmux_offset: The pinmux register offset.
  197. *
  198. * @type1_start: Some chips have two base addresses for pull select register,
  199. * that means some pins use the first address and others use the second. This
  200. * member record the start of pin number to use the second address.
  201. * @type1_end: The end of pin number to use the second address.
  202. *
  203. * @port_shf: The shift between two registers.
  204. * @port_mask: The mask of register.
  205. * @port_align: Provide clear register and set register step.
  206. */
  207. extern int mt_set_gpio_mode(unsigned long pin, unsigned long mode);
  208. extern int mt_set_gpio_dir(unsigned long pin, unsigned long dir);
  209. extern int mt_get_gpio_dir(unsigned long pin);
  210. extern int mt_get_gpio_out(unsigned long pin);
  211. extern int mt_set_gpio_out(unsigned long pin, unsigned long output);
  212. extern int mt_get_gpio_in(unsigned long pin);
  213. extern int mt_set_gpio_ies(unsigned long pin, unsigned long enable);
  214. extern int mt_set_gpio_smt(unsigned long pin, unsigned long enable);
  215. extern int mt_set_gpio_pull_enable(unsigned long pin, unsigned long enable);
  216. extern int mt_set_gpio_pull_select(unsigned long pin, unsigned long select);
  217. struct mtk_pinctrl_devdata {
  218. const struct mtk_desc_pin *pins;
  219. unsigned int npins;
  220. const struct mtk_drv_group_desc *grp_desc;
  221. unsigned int n_grp_cls;
  222. const struct mtk_pin_drv_grp *pin_drv_grp;
  223. unsigned int n_pin_drv_grps;
  224. int (*spec_pull_set)(struct regmap *reg, unsigned int pin,
  225. unsigned char align, bool isup, unsigned int arg);
  226. int (*spec_ies_smt_set)(struct regmap *reg, unsigned int pin,
  227. unsigned char align, int value,
  228. enum pin_config_param arg);
  229. int (*spec_pull_get)(struct regmap *reg, unsigned int pin);
  230. int (*spec_ies_get)(struct regmap *reg, unsigned int pin);
  231. int (*spec_smt_get)(struct regmap *reg, unsigned int pin);
  232. int (*spec_set_gpio_mode)(unsigned long pin, unsigned long mode);
  233. int (*mt_set_gpio_dir)(unsigned long pin, unsigned long dir);
  234. int (*mt_get_gpio_dir)(unsigned long pin);
  235. int (*mt_get_gpio_out)(unsigned long pin);
  236. int (*mt_set_gpio_out)(unsigned long pin, unsigned long output);
  237. int (*mt_get_gpio_in)(unsigned long pin);
  238. int (*mt_set_gpio_ies)(unsigned long pin, unsigned long enable);
  239. int (*mt_set_gpio_smt)(unsigned long pin, unsigned long enable);
  240. int (*mt_set_gpio_pull_enable)(unsigned long pin, unsigned long enable);
  241. int (*mt_set_gpio_pull_select)(unsigned long pin, unsigned long select);
  242. unsigned int dir_offset;
  243. unsigned int ies_offset;
  244. unsigned int smt_offset;
  245. unsigned int pullen_offset;
  246. unsigned int pullsel_offset;
  247. unsigned int drv_offset;
  248. unsigned int dout_offset;
  249. unsigned int din_offset;
  250. unsigned int pinmux_offset;
  251. unsigned short type1_start;
  252. unsigned short type1_end;
  253. unsigned char port_shf;
  254. unsigned char port_mask;
  255. unsigned char port_align;
  256. unsigned char chip_type;
  257. struct mtk_eint_offsets eint_offsets;
  258. unsigned int ap_num;
  259. unsigned int db_cnt;
  260. };
  261. struct mtk_pinctrl {
  262. struct regmap *regmap1;
  263. struct regmap *regmap2;
  264. struct device *dev;
  265. struct gpio_chip *chip;
  266. struct mtk_pinctrl_group *groups;
  267. unsigned ngroups;
  268. const char **grp_names;
  269. struct pinctrl_dev *pctl_dev;
  270. const struct mtk_pinctrl_devdata *devdata;
  271. void __iomem *eint_reg_base;
  272. struct irq_domain *domain;
  273. int *eint_dual_edges;
  274. u32 *wake_mask;
  275. u32 *cur_mask;
  276. };
  277. int mtk_pctrl_init(struct platform_device *pdev,
  278. const struct mtk_pinctrl_devdata *data,
  279. struct regmap *regmap);
  280. int mtk_pctrl_spec_pull_set_samereg(struct regmap *regmap,
  281. const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
  282. unsigned int info_num, unsigned int pin,
  283. unsigned char align, bool isup, unsigned int r1r0);
  284. int mtk_pconf_spec_set_ies_smt_range(struct regmap *regmap,
  285. const struct mtk_pin_ies_smt_set *ies_smt_infos,
  286. unsigned int info_num,
  287. unsigned int pin, unsigned char align, int value);
  288. int mtk_spec_pull_get_samereg(struct regmap *regmap,
  289. const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
  290. unsigned int info_num, unsigned int pin);
  291. int mtk_spec_get_ies_smt_range(struct regmap *regmap,
  292. const struct mtk_pin_ies_smt_set *ies_smt_infos,
  293. unsigned int info_num,
  294. unsigned int pin);
  295. extern const struct dev_pm_ops mtk_eint_pm_ops;
  296. #ifdef CONFIG_MTK_EIC
  297. void mt_eint_set_hw_debounce(unsigned int eint_num, unsigned int ms);
  298. unsigned int mt_gpio_to_irq(unsigned gpio);
  299. #endif
  300. #endif /* __PINCTRL_MTK_COMMON_H */