pinctrl-sunxi.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Allwinner A1X SoCs pinctrl driver.
  3. *
  4. * Copyright (C) 2012 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #ifndef __PINCTRL_SUNXI_H
  13. #define __PINCTRL_SUNXI_H
  14. #include <linux/kernel.h>
  15. #include <linux/spinlock.h>
  16. #define PA_BASE 0
  17. #define PB_BASE 32
  18. #define PC_BASE 64
  19. #define PD_BASE 96
  20. #define PE_BASE 128
  21. #define PF_BASE 160
  22. #define PG_BASE 192
  23. #define PH_BASE 224
  24. #define PI_BASE 256
  25. #define PL_BASE 352
  26. #define PM_BASE 384
  27. #define PN_BASE 416
  28. #define SUNXI_PINCTRL_PIN(bank, pin) \
  29. PINCTRL_PIN(P ## bank ## _BASE + (pin), "P" #bank #pin)
  30. #define SUNXI_PIN_NAME_MAX_LEN 5
  31. #define BANK_MEM_SIZE 0x24
  32. #define MUX_REGS_OFFSET 0x0
  33. #define DATA_REGS_OFFSET 0x10
  34. #define DLEVEL_REGS_OFFSET 0x14
  35. #define PULL_REGS_OFFSET 0x1c
  36. #define PINS_PER_BANK 32
  37. #define MUX_PINS_PER_REG 8
  38. #define MUX_PINS_BITS 4
  39. #define MUX_PINS_MASK 0x0f
  40. #define DATA_PINS_PER_REG 32
  41. #define DATA_PINS_BITS 1
  42. #define DATA_PINS_MASK 0x01
  43. #define DLEVEL_PINS_PER_REG 16
  44. #define DLEVEL_PINS_BITS 2
  45. #define DLEVEL_PINS_MASK 0x03
  46. #define PULL_PINS_PER_REG 16
  47. #define PULL_PINS_BITS 2
  48. #define PULL_PINS_MASK 0x03
  49. #define IRQ_PER_BANK 32
  50. #define IRQ_CFG_REG 0x200
  51. #define IRQ_CFG_IRQ_PER_REG 8
  52. #define IRQ_CFG_IRQ_BITS 4
  53. #define IRQ_CFG_IRQ_MASK ((1 << IRQ_CFG_IRQ_BITS) - 1)
  54. #define IRQ_CTRL_REG 0x210
  55. #define IRQ_CTRL_IRQ_PER_REG 32
  56. #define IRQ_CTRL_IRQ_BITS 1
  57. #define IRQ_CTRL_IRQ_MASK ((1 << IRQ_CTRL_IRQ_BITS) - 1)
  58. #define IRQ_STATUS_REG 0x214
  59. #define IRQ_STATUS_IRQ_PER_REG 32
  60. #define IRQ_STATUS_IRQ_BITS 1
  61. #define IRQ_STATUS_IRQ_MASK ((1 << IRQ_STATUS_IRQ_BITS) - 1)
  62. #define IRQ_DEBOUNCE_REG 0x218
  63. #define IRQ_MEM_SIZE 0x20
  64. #define IRQ_EDGE_RISING 0x00
  65. #define IRQ_EDGE_FALLING 0x01
  66. #define IRQ_LEVEL_HIGH 0x02
  67. #define IRQ_LEVEL_LOW 0x03
  68. #define IRQ_EDGE_BOTH 0x04
  69. #define SUN4I_FUNC_INPUT 0
  70. #define SUN4I_FUNC_IRQ 6
  71. #define PINCTRL_SUN5I_A10S BIT(1)
  72. #define PINCTRL_SUN5I_A13 BIT(2)
  73. #define PINCTRL_SUN5I_GR8 BIT(3)
  74. #define PINCTRL_SUN6I_A31 BIT(4)
  75. #define PINCTRL_SUN6I_A31S BIT(5)
  76. #define PINCTRL_SUN4I_A10 BIT(6)
  77. #define PINCTRL_SUN7I_A20 BIT(7)
  78. #define PINCTRL_SUN8I_R40 BIT(8)
  79. struct sunxi_desc_function {
  80. unsigned long variant;
  81. const char *name;
  82. u8 muxval;
  83. u8 irqbank;
  84. u8 irqnum;
  85. };
  86. struct sunxi_desc_pin {
  87. struct pinctrl_pin_desc pin;
  88. unsigned long variant;
  89. struct sunxi_desc_function *functions;
  90. };
  91. struct sunxi_pinctrl_desc {
  92. const struct sunxi_desc_pin *pins;
  93. int npins;
  94. unsigned pin_base;
  95. unsigned irq_banks;
  96. const unsigned int *irq_bank_map;
  97. bool irq_read_needs_mux;
  98. bool disable_strict_mode;
  99. };
  100. struct sunxi_pinctrl_function {
  101. const char *name;
  102. const char **groups;
  103. unsigned ngroups;
  104. };
  105. struct sunxi_pinctrl_group {
  106. const char *name;
  107. unsigned pin;
  108. };
  109. struct sunxi_pinctrl {
  110. void __iomem *membase;
  111. struct gpio_chip *chip;
  112. const struct sunxi_pinctrl_desc *desc;
  113. struct device *dev;
  114. struct irq_domain *domain;
  115. struct sunxi_pinctrl_function *functions;
  116. unsigned nfunctions;
  117. struct sunxi_pinctrl_group *groups;
  118. unsigned ngroups;
  119. int *irq;
  120. unsigned *irq_array;
  121. raw_spinlock_t lock;
  122. struct pinctrl_dev *pctl_dev;
  123. unsigned long variant;
  124. };
  125. #define SUNXI_PIN(_pin, ...) \
  126. { \
  127. .pin = _pin, \
  128. .functions = (struct sunxi_desc_function[]){ \
  129. __VA_ARGS__, { } }, \
  130. }
  131. #define SUNXI_PIN_VARIANT(_pin, _variant, ...) \
  132. { \
  133. .pin = _pin, \
  134. .variant = _variant, \
  135. .functions = (struct sunxi_desc_function[]){ \
  136. __VA_ARGS__, { } }, \
  137. }
  138. #define SUNXI_FUNCTION(_val, _name) \
  139. { \
  140. .name = _name, \
  141. .muxval = _val, \
  142. }
  143. #define SUNXI_FUNCTION_VARIANT(_val, _name, _variant) \
  144. { \
  145. .name = _name, \
  146. .muxval = _val, \
  147. .variant = _variant, \
  148. }
  149. #define SUNXI_FUNCTION_IRQ(_val, _irq) \
  150. { \
  151. .name = "irq", \
  152. .muxval = _val, \
  153. .irqnum = _irq, \
  154. }
  155. #define SUNXI_FUNCTION_IRQ_BANK(_val, _bank, _irq) \
  156. { \
  157. .name = "irq", \
  158. .muxval = _val, \
  159. .irqbank = _bank, \
  160. .irqnum = _irq, \
  161. }
  162. /*
  163. * The sunXi PIO registers are organized as is:
  164. * 0x00 - 0x0c Muxing values.
  165. * 8 pins per register, each pin having a 4bits value
  166. * 0x10 Pin values
  167. * 32 bits per register, each pin corresponding to one bit
  168. * 0x14 - 0x18 Drive level
  169. * 16 pins per register, each pin having a 2bits value
  170. * 0x1c - 0x20 Pull-Up values
  171. * 16 pins per register, each pin having a 2bits value
  172. *
  173. * This is for the first bank. Each bank will have the same layout,
  174. * with an offset being a multiple of 0x24.
  175. *
  176. * The following functions calculate from the pin number the register
  177. * and the bit offset that we should access.
  178. */
  179. static inline u32 sunxi_mux_reg(u16 pin)
  180. {
  181. u8 bank = pin / PINS_PER_BANK;
  182. u32 offset = bank * BANK_MEM_SIZE;
  183. offset += MUX_REGS_OFFSET;
  184. offset += pin % PINS_PER_BANK / MUX_PINS_PER_REG * 0x04;
  185. return round_down(offset, 4);
  186. }
  187. static inline u32 sunxi_mux_offset(u16 pin)
  188. {
  189. u32 pin_num = pin % MUX_PINS_PER_REG;
  190. return pin_num * MUX_PINS_BITS;
  191. }
  192. static inline u32 sunxi_data_reg(u16 pin)
  193. {
  194. u8 bank = pin / PINS_PER_BANK;
  195. u32 offset = bank * BANK_MEM_SIZE;
  196. offset += DATA_REGS_OFFSET;
  197. offset += pin % PINS_PER_BANK / DATA_PINS_PER_REG * 0x04;
  198. return round_down(offset, 4);
  199. }
  200. static inline u32 sunxi_data_offset(u16 pin)
  201. {
  202. u32 pin_num = pin % DATA_PINS_PER_REG;
  203. return pin_num * DATA_PINS_BITS;
  204. }
  205. static inline u32 sunxi_dlevel_reg(u16 pin)
  206. {
  207. u8 bank = pin / PINS_PER_BANK;
  208. u32 offset = bank * BANK_MEM_SIZE;
  209. offset += DLEVEL_REGS_OFFSET;
  210. offset += pin % PINS_PER_BANK / DLEVEL_PINS_PER_REG * 0x04;
  211. return round_down(offset, 4);
  212. }
  213. static inline u32 sunxi_dlevel_offset(u16 pin)
  214. {
  215. u32 pin_num = pin % DLEVEL_PINS_PER_REG;
  216. return pin_num * DLEVEL_PINS_BITS;
  217. }
  218. static inline u32 sunxi_pull_reg(u16 pin)
  219. {
  220. u8 bank = pin / PINS_PER_BANK;
  221. u32 offset = bank * BANK_MEM_SIZE;
  222. offset += PULL_REGS_OFFSET;
  223. offset += pin % PINS_PER_BANK / PULL_PINS_PER_REG * 0x04;
  224. return round_down(offset, 4);
  225. }
  226. static inline u32 sunxi_pull_offset(u16 pin)
  227. {
  228. u32 pin_num = pin % PULL_PINS_PER_REG;
  229. return pin_num * PULL_PINS_BITS;
  230. }
  231. static inline u32 sunxi_irq_hw_bank_num(const struct sunxi_pinctrl_desc *desc, u8 bank)
  232. {
  233. if (!desc->irq_bank_map)
  234. return bank;
  235. else
  236. return desc->irq_bank_map[bank];
  237. }
  238. static inline u32 sunxi_irq_cfg_reg(const struct sunxi_pinctrl_desc *desc,
  239. u16 irq)
  240. {
  241. u8 bank = irq / IRQ_PER_BANK;
  242. u8 reg = (irq % IRQ_PER_BANK) / IRQ_CFG_IRQ_PER_REG * 0x04;
  243. return IRQ_CFG_REG +
  244. sunxi_irq_hw_bank_num(desc, bank) * IRQ_MEM_SIZE + reg;
  245. }
  246. static inline u32 sunxi_irq_cfg_offset(u16 irq)
  247. {
  248. u32 irq_num = irq % IRQ_CFG_IRQ_PER_REG;
  249. return irq_num * IRQ_CFG_IRQ_BITS;
  250. }
  251. static inline u32 sunxi_irq_ctrl_reg_from_bank(const struct sunxi_pinctrl_desc *desc, u8 bank)
  252. {
  253. return IRQ_CTRL_REG + sunxi_irq_hw_bank_num(desc, bank) * IRQ_MEM_SIZE;
  254. }
  255. static inline u32 sunxi_irq_ctrl_reg(const struct sunxi_pinctrl_desc *desc,
  256. u16 irq)
  257. {
  258. u8 bank = irq / IRQ_PER_BANK;
  259. return sunxi_irq_ctrl_reg_from_bank(desc, bank);
  260. }
  261. static inline u32 sunxi_irq_ctrl_offset(u16 irq)
  262. {
  263. u32 irq_num = irq % IRQ_CTRL_IRQ_PER_REG;
  264. return irq_num * IRQ_CTRL_IRQ_BITS;
  265. }
  266. static inline u32 sunxi_irq_debounce_reg_from_bank(const struct sunxi_pinctrl_desc *desc, u8 bank)
  267. {
  268. return IRQ_DEBOUNCE_REG +
  269. sunxi_irq_hw_bank_num(desc, bank) * IRQ_MEM_SIZE;
  270. }
  271. static inline u32 sunxi_irq_status_reg_from_bank(const struct sunxi_pinctrl_desc *desc, u8 bank)
  272. {
  273. return IRQ_STATUS_REG +
  274. sunxi_irq_hw_bank_num(desc, bank) * IRQ_MEM_SIZE;
  275. }
  276. static inline u32 sunxi_irq_status_reg(const struct sunxi_pinctrl_desc *desc,
  277. u16 irq)
  278. {
  279. u8 bank = irq / IRQ_PER_BANK;
  280. return sunxi_irq_status_reg_from_bank(desc, bank);
  281. }
  282. static inline u32 sunxi_irq_status_offset(u16 irq)
  283. {
  284. u32 irq_num = irq % IRQ_STATUS_IRQ_PER_REG;
  285. return irq_num * IRQ_STATUS_IRQ_BITS;
  286. }
  287. int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
  288. const struct sunxi_pinctrl_desc *desc,
  289. unsigned long variant);
  290. #define sunxi_pinctrl_init(_dev, _desc) \
  291. sunxi_pinctrl_init_with_variant(_dev, _desc, 0)
  292. #endif /* __PINCTRL_SUNXI_H */