eca379a2b9764654414d5996ba733c09b95fb029
[cascardo/linux.git] / drivers / pinctrl / uniphier / pinctrl-uniphier.h
1 /*
2  * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
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
15 #ifndef __PINCTRL_UNIPHIER_H__
16 #define __PINCTRL_UNIPHIER_H__
17
18 #include <linux/bug.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21
22 struct platform_device;
23
24 #define UNIPHIER_PINCTRL_PINMUX_BASE    0x0
25 #define UNIPHIER_PINCTRL_LOAD_PINMUX    0x700
26 #define UNIPHIER_PINCTRL_DRVCTRL_BASE   0x800
27 #define UNIPHIER_PINCTRL_DRV2CTRL_BASE  0x900
28 #define UNIPHIER_PINCTRL_DRV3CTRL_BASE  0x980
29 #define UNIPHIER_PINCTRL_PUPDCTRL_BASE  0xa00
30 #define UNIPHIER_PINCTRL_IECTRL         0xd00
31
32 /* input enable control register bit */
33 #define UNIPHIER_PIN_IECTRL_SHIFT       0
34 #define UNIPHIER_PIN_IECTRL_BITS        8
35 #define UNIPHIER_PIN_IECTRL_MASK        ((1UL << (UNIPHIER_PIN_IECTRL_BITS)) \
36                                          - 1)
37
38 /* drive strength control register number */
39 #define UNIPHIER_PIN_DRVCTRL_SHIFT      ((UNIPHIER_PIN_IECTRL_SHIFT) + \
40                                         (UNIPHIER_PIN_IECTRL_BITS))
41 #define UNIPHIER_PIN_DRVCTRL_BITS       9
42 #define UNIPHIER_PIN_DRVCTRL_MASK       ((1UL << (UNIPHIER_PIN_DRVCTRL_BITS)) \
43                                          - 1)
44
45 /* drive control type */
46 #define UNIPHIER_PIN_DRV_TYPE_SHIFT     ((UNIPHIER_PIN_DRVCTRL_SHIFT) + \
47                                          (UNIPHIER_PIN_DRVCTRL_BITS))
48 #define UNIPHIER_PIN_DRV_TYPE_BITS      3
49 #define UNIPHIER_PIN_DRV_TYPE_MASK      ((1UL << (UNIPHIER_PIN_DRV_TYPE_BITS)) \
50                                          - 1)
51
52 /* pull-up / pull-down register number */
53 #define UNIPHIER_PIN_PUPDCTRL_SHIFT     ((UNIPHIER_PIN_DRV_TYPE_SHIFT) + \
54                                          (UNIPHIER_PIN_DRV_TYPE_BITS))
55 #define UNIPHIER_PIN_PUPDCTRL_BITS      9
56 #define UNIPHIER_PIN_PUPDCTRL_MASK      ((1UL << (UNIPHIER_PIN_PUPDCTRL_BITS))\
57                                          - 1)
58
59 /* direction of pull register */
60 #define UNIPHIER_PIN_PULL_DIR_SHIFT     ((UNIPHIER_PIN_PUPDCTRL_SHIFT) + \
61                                          (UNIPHIER_PIN_PUPDCTRL_BITS))
62 #define UNIPHIER_PIN_PULL_DIR_BITS      3
63 #define UNIPHIER_PIN_PULL_DIR_MASK      ((1UL << (UNIPHIER_PIN_PULL_DIR_BITS))\
64                                          - 1)
65
66 #if UNIPHIER_PIN_PULL_DIR_SHIFT + UNIPHIER_PIN_PULL_DIR_BITS > BITS_PER_LONG
67 #error "unable to pack pin attributes."
68 #endif
69
70 #define UNIPHIER_PIN_IECTRL_NONE        (UNIPHIER_PIN_IECTRL_MASK)
71
72 /* drive control type */
73 enum uniphier_pin_drv_type {
74         UNIPHIER_PIN_DRV_1BIT,          /* 2 level control: 4/8 mA */
75         UNIPHIER_PIN_DRV_2BIT,          /* 4 level control: 8/12/16/20 mA */
76         UNIPHIER_PIN_DRV_3BIT,          /* 8 level control: 4/5/7/9/11/12/14/16 mA */
77         UNIPHIER_PIN_DRV_FIXED4,        /* fixed to 4mA */
78         UNIPHIER_PIN_DRV_FIXED5,        /* fixed to 5mA */
79         UNIPHIER_PIN_DRV_FIXED8,        /* fixed to 8mA */
80         UNIPHIER_PIN_DRV_NONE,          /* no support (input only pin) */
81 };
82
83 /* direction of pull register (no pin supports bi-directional pull biasing) */
84 enum uniphier_pin_pull_dir {
85         UNIPHIER_PIN_PULL_UP,           /* pull-up or disabled */
86         UNIPHIER_PIN_PULL_DOWN,         /* pull-down or disabled */
87         UNIPHIER_PIN_PULL_UP_FIXED,     /* always pull-up */
88         UNIPHIER_PIN_PULL_DOWN_FIXED,   /* always pull-down */
89         UNIPHIER_PIN_PULL_NONE,         /* no pull register */
90 };
91
92 #define UNIPHIER_PIN_IECTRL(x) \
93         (((x) & (UNIPHIER_PIN_IECTRL_MASK)) << (UNIPHIER_PIN_IECTRL_SHIFT))
94 #define UNIPHIER_PIN_DRVCTRL(x) \
95         (((x) & (UNIPHIER_PIN_DRVCTRL_MASK)) << (UNIPHIER_PIN_DRVCTRL_SHIFT))
96 #define UNIPHIER_PIN_DRV_TYPE(x) \
97         (((x) & (UNIPHIER_PIN_DRV_TYPE_MASK)) << (UNIPHIER_PIN_DRV_TYPE_SHIFT))
98 #define UNIPHIER_PIN_PUPDCTRL(x) \
99         (((x) & (UNIPHIER_PIN_PUPDCTRL_MASK)) << (UNIPHIER_PIN_PUPDCTRL_SHIFT))
100 #define UNIPHIER_PIN_PULL_DIR(x) \
101         (((x) & (UNIPHIER_PIN_PULL_DIR_MASK)) << (UNIPHIER_PIN_PULL_DIR_SHIFT))
102
103 #define UNIPHIER_PIN_ATTR_PACKED(iectrl, drvctrl, drv_type, pupdctrl, pull_dir)\
104                                 (UNIPHIER_PIN_IECTRL(iectrl) |          \
105                                  UNIPHIER_PIN_DRVCTRL(drvctrl) |        \
106                                  UNIPHIER_PIN_DRV_TYPE(drv_type) |      \
107                                  UNIPHIER_PIN_PUPDCTRL(pupdctrl) |      \
108                                  UNIPHIER_PIN_PULL_DIR(pull_dir))
109
110 static inline unsigned int uniphier_pin_get_iectrl(void *drv_data)
111 {
112         return ((unsigned long)drv_data >> UNIPHIER_PIN_IECTRL_SHIFT) &
113                                                 UNIPHIER_PIN_IECTRL_MASK;
114 }
115
116 static inline unsigned int uniphier_pin_get_drvctrl(void *drv_data)
117 {
118         return ((unsigned long)drv_data >> UNIPHIER_PIN_DRVCTRL_SHIFT) &
119                                                 UNIPHIER_PIN_DRVCTRL_MASK;
120 }
121
122 static inline unsigned int uniphier_pin_get_drv_type(void *drv_data)
123 {
124         return ((unsigned long)drv_data >> UNIPHIER_PIN_DRV_TYPE_SHIFT) &
125                                                 UNIPHIER_PIN_DRV_TYPE_MASK;
126 }
127
128 static inline unsigned int uniphier_pin_get_pupdctrl(void *drv_data)
129 {
130         return ((unsigned long)drv_data >> UNIPHIER_PIN_PUPDCTRL_SHIFT) &
131                                                 UNIPHIER_PIN_PUPDCTRL_MASK;
132 }
133
134 static inline unsigned int uniphier_pin_get_pull_dir(void *drv_data)
135 {
136         return ((unsigned long)drv_data >> UNIPHIER_PIN_PULL_DIR_SHIFT) &
137                                                 UNIPHIER_PIN_PULL_DIR_MASK;
138 }
139
140 enum uniphier_pinmux_gpio_range_type {
141         UNIPHIER_PINMUX_GPIO_RANGE_PORT,
142         UNIPHIER_PINMUX_GPIO_RANGE_IRQ,
143         UNIPHIER_PINMUX_GPIO_RANGE_NONE,
144 };
145
146 struct uniphier_pinctrl_group {
147         const char *name;
148         const unsigned *pins;
149         unsigned num_pins;
150         const unsigned *muxvals;
151         enum uniphier_pinmux_gpio_range_type range_type;
152 };
153
154 struct uniphier_pinmux_function {
155         const char *name;
156         const char * const *groups;
157         unsigned num_groups;
158 };
159
160 struct uniphier_pinctrl_socdata {
161         const struct pinctrl_pin_desc *pins;
162         unsigned int npins;
163         const struct uniphier_pinctrl_group *groups;
164         int groups_count;
165         const struct uniphier_pinmux_function *functions;
166         int functions_count;
167         unsigned mux_bits;
168         unsigned reg_stride;
169         bool load_pinctrl;
170 };
171
172 #define UNIPHIER_PINCTRL_PIN(a, b, c, d, e, f, g)                       \
173 {                                                                       \
174         .number = a,                                                    \
175         .name = b,                                                      \
176         .drv_data = (void *)UNIPHIER_PIN_ATTR_PACKED(c, d, e, f, g),    \
177 }
178
179 #define __UNIPHIER_PINCTRL_GROUP(grp, type)                             \
180         {                                                               \
181                 .name = #grp,                                           \
182                 .pins = grp##_pins,                                     \
183                 .num_pins = ARRAY_SIZE(grp##_pins),                     \
184                 .muxvals = grp##_muxvals +                              \
185                         BUILD_BUG_ON_ZERO(ARRAY_SIZE(grp##_pins) !=     \
186                                           ARRAY_SIZE(grp##_muxvals)),   \
187                 .range_type = type,                                     \
188         }
189
190 #define UNIPHIER_PINCTRL_GROUP(grp)                                     \
191         __UNIPHIER_PINCTRL_GROUP(grp, UNIPHIER_PINMUX_GPIO_RANGE_NONE)
192
193 #define UNIPHIER_PINCTRL_GROUP_GPIO_RANGE_PORT(grp)                     \
194         __UNIPHIER_PINCTRL_GROUP(grp, UNIPHIER_PINMUX_GPIO_RANGE_PORT)
195
196 #define UNIPHIER_PINCTRL_GROUP_GPIO_RANGE_IRQ(grp)                      \
197         __UNIPHIER_PINCTRL_GROUP(grp, UNIPHIER_PINMUX_GPIO_RANGE_IRQ)
198
199 #define UNIPHIER_PINCTRL_GROUP_SINGLE(grp, array, ofst)                 \
200         {                                                               \
201                 .name = #grp,                                           \
202                 .pins = array##_pins + ofst,                            \
203                 .num_pins = 1,                                          \
204                 .muxvals = array##_muxvals + ofst,                      \
205         }
206
207 #define UNIPHIER_PINMUX_FUNCTION(func)                                  \
208         {                                                               \
209                 .name = #func,                                          \
210                 .groups = func##_groups,                                \
211                 .num_groups = ARRAY_SIZE(func##_groups),                \
212         }
213
214 int uniphier_pinctrl_probe(struct platform_device *pdev,
215                            struct uniphier_pinctrl_socdata *socdata);
216
217 #endif /* __PINCTRL_UNIPHIER_H__ */