pinctrl-sunxi.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. struct sunxi_desc_function {
  77. unsigned long variant;
  78. const char *name;
  79. u8 muxval;
  80. u8 irqbank;
  81. u8 irqnum;
  82. };
  83. struct sunxi_desc_pin {
  84. struct pinctrl_pin_desc pin;
  85. unsigned long variant;
  86. struct sunxi_desc_function *functions;
  87. };
  88. struct sunxi_pinctrl_desc {
  89. const struct sunxi_desc_pin *pins;
  90. int npins;
  91. unsigned pin_base;
  92. unsigned irq_banks;
  93. unsigned irq_bank_base;
  94. bool irq_read_needs_mux;
  95. };
  96. struct sunxi_pinctrl_function {
  97. const char *name;
  98. const char **groups;
  99. unsigned ngroups;
  100. };
  101. struct sunxi_pinctrl_group {
  102. const char *name;
  103. unsigned pin;
  104. };
  105. struct sunxi_pinctrl {
  106. void __iomem *membase;
  107. struct gpio_chip *chip;
  108. const struct sunxi_pinctrl_desc *desc;
  109. struct device *dev;
  110. struct irq_domain *domain;
  111. struct sunxi_pinctrl_function *functions;
  112. unsigned nfunctions;
  113. struct sunxi_pinctrl_group *groups;
  114. unsigned ngroups;
  115. int *irq;
  116. unsigned *irq_array;
  117. raw_spinlock_t lock;
  118. struct pinctrl_dev *pctl_dev;
  119. unsigned long variant;
  120. };
  121. #define SUNXI_PIN(_pin, ...) \
  122. { \
  123. .pin = _pin, \
  124. .functions = (struct sunxi_desc_function[]){ \
  125. __VA_ARGS__, { } }, \
  126. }
  127. #define SUNXI_PIN_VARIANT(_pin, _variant, ...) \
  128. { \
  129. .pin = _pin, \
  130. .variant = _variant, \
  131. .functions = (struct sunxi_desc_function[]){ \
  132. __VA_ARGS__, { } }, \
  133. }
  134. #define SUNXI_FUNCTION(_val, _name) \
  135. { \
  136. .name = _name, \
  137. .muxval = _val, \
  138. }
  139. #define SUNXI_FUNCTION_VARIANT(_val, _name, _variant) \
  140. { \
  141. .name = _name, \
  142. .muxval = _val, \
  143. .variant = _variant, \
  144. }
  145. #define SUNXI_FUNCTION_IRQ(_val, _irq) \
  146. { \
  147. .name = "irq", \
  148. .muxval = _val, \
  149. .irqnum = _irq, \
  150. }
  151. #define SUNXI_FUNCTION_IRQ_BANK(_val, _bank, _irq) \
  152. { \
  153. .name = "irq", \
  154. .muxval = _val, \
  155. .irqbank = _bank, \
  156. .irqnum = _irq, \
  157. }
  158. /*
  159. * The sunXi PIO registers are organized as is:
  160. * 0x00 - 0x0c Muxing values.
  161. * 8 pins per register, each pin having a 4bits value
  162. * 0x10 Pin values
  163. * 32 bits per register, each pin corresponding to one bit
  164. * 0x14 - 0x18 Drive level
  165. * 16 pins per register, each pin having a 2bits value
  166. * 0x1c - 0x20 Pull-Up values
  167. * 16 pins per register, each pin having a 2bits value
  168. *
  169. * This is for the first bank. Each bank will have the same layout,
  170. * with an offset being a multiple of 0x24.
  171. *
  172. * The following functions calculate from the pin number the register
  173. * and the bit offset that we should access.
  174. */
  175. static inline u32 sunxi_mux_reg(u16 pin)
  176. {
  177. u8 bank = pin / PINS_PER_BANK;
  178. u32 offset = bank * BANK_MEM_SIZE;
  179. offset += MUX_REGS_OFFSET;
  180. offset += pin % PINS_PER_BANK / MUX_PINS_PER_REG * 0x04;
  181. return round_down(offset, 4);
  182. }
  183. static inline u32 sunxi_mux_offset(u16 pin)
  184. {
  185. u32 pin_num = pin % MUX_PINS_PER_REG;
  186. return pin_num * MUX_PINS_BITS;
  187. }
  188. static inline u32 sunxi_data_reg(u16 pin)
  189. {
  190. u8 bank = pin / PINS_PER_BANK;
  191. u32 offset = bank * BANK_MEM_SIZE;
  192. offset += DATA_REGS_OFFSET;
  193. offset += pin % PINS_PER_BANK / DATA_PINS_PER_REG * 0x04;
  194. return round_down(offset, 4);
  195. }
  196. static inline u32 sunxi_data_offset(u16 pin)
  197. {
  198. u32 pin_num = pin % DATA_PINS_PER_REG;
  199. return pin_num * DATA_PINS_BITS;
  200. }
  201. static inline u32 sunxi_dlevel_reg(u16 pin)
  202. {
  203. u8 bank = pin / PINS_PER_BANK;
  204. u32 offset = bank * BANK_MEM_SIZE;
  205. offset += DLEVEL_REGS_OFFSET;
  206. offset += pin % PINS_PER_BANK / DLEVEL_PINS_PER_REG * 0x04;
  207. return round_down(offset, 4);
  208. }
  209. static inline u32 sunxi_dlevel_offset(u16 pin)
  210. {
  211. u32 pin_num = pin % DLEVEL_PINS_PER_REG;
  212. return pin_num * DLEVEL_PINS_BITS;
  213. }
  214. static inline u32 sunxi_pull_reg(u16 pin)
  215. {
  216. u8 bank = pin / PINS_PER_BANK;
  217. u32 offset = bank * BANK_MEM_SIZE;
  218. offset += PULL_REGS_OFFSET;
  219. offset += pin % PINS_PER_BANK / PULL_PINS_PER_REG * 0x04;
  220. return round_down(offset, 4);
  221. }
  222. static inline u32 sunxi_pull_offset(u16 pin)
  223. {
  224. u32 pin_num = pin % PULL_PINS_PER_REG;
  225. return pin_num * PULL_PINS_BITS;
  226. }
  227. static inline u32 sunxi_irq_cfg_reg(u16 irq, unsigned bank_base)
  228. {
  229. u8 bank = irq / IRQ_PER_BANK;
  230. u8 reg = (irq % IRQ_PER_BANK) / IRQ_CFG_IRQ_PER_REG * 0x04;
  231. return IRQ_CFG_REG + (bank_base + bank) * IRQ_MEM_SIZE + reg;
  232. }
  233. static inline u32 sunxi_irq_cfg_offset(u16 irq)
  234. {
  235. u32 irq_num = irq % IRQ_CFG_IRQ_PER_REG;
  236. return irq_num * IRQ_CFG_IRQ_BITS;
  237. }
  238. static inline u32 sunxi_irq_ctrl_reg_from_bank(u8 bank, unsigned bank_base)
  239. {
  240. return IRQ_CTRL_REG + (bank_base + bank) * IRQ_MEM_SIZE;
  241. }
  242. static inline u32 sunxi_irq_ctrl_reg(u16 irq, unsigned bank_base)
  243. {
  244. u8 bank = irq / IRQ_PER_BANK;
  245. return sunxi_irq_ctrl_reg_from_bank(bank, bank_base);
  246. }
  247. static inline u32 sunxi_irq_ctrl_offset(u16 irq)
  248. {
  249. u32 irq_num = irq % IRQ_CTRL_IRQ_PER_REG;
  250. return irq_num * IRQ_CTRL_IRQ_BITS;
  251. }
  252. static inline u32 sunxi_irq_debounce_reg_from_bank(u8 bank, unsigned bank_base)
  253. {
  254. return IRQ_DEBOUNCE_REG + (bank_base + bank) * IRQ_MEM_SIZE;
  255. }
  256. static inline u32 sunxi_irq_status_reg_from_bank(u8 bank, unsigned bank_base)
  257. {
  258. return IRQ_STATUS_REG + (bank_base + bank) * IRQ_MEM_SIZE;
  259. }
  260. static inline u32 sunxi_irq_status_reg(u16 irq, unsigned bank_base)
  261. {
  262. u8 bank = irq / IRQ_PER_BANK;
  263. return sunxi_irq_status_reg_from_bank(bank, bank_base);
  264. }
  265. static inline u32 sunxi_irq_status_offset(u16 irq)
  266. {
  267. u32 irq_num = irq % IRQ_STATUS_IRQ_PER_REG;
  268. return irq_num * IRQ_STATUS_IRQ_BITS;
  269. }
  270. int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
  271. const struct sunxi_pinctrl_desc *desc,
  272. unsigned long variant);
  273. #define sunxi_pinctrl_init(_dev, _desc) \
  274. sunxi_pinctrl_init_with_variant(_dev, _desc, 0)
  275. #endif /* __PINCTRL_SUNXI_H */