pinctrl-sunxi.h 6.6 KB

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