pinctrl-sunxi.h 6.8 KB

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