common: Drop log.h from common header
[pandora-u-boot.git] / drivers / gpio / mxs_gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale i.MX28 GPIO control code
4  *
5  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6  * on behalf of DENX Software Engineering GmbH
7  */
8
9 #include <common.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <linux/errno.h>
13 #include <asm/io.h>
14 #include <asm/arch/iomux.h>
15 #include <asm/arch/imx-regs.h>
16
17 #if     defined(CONFIG_MX23)
18 #define PINCTRL_BANKS           3
19 #define PINCTRL_DOUT(n)         (0x0500 + ((n) * 0x10))
20 #define PINCTRL_DIN(n)          (0x0600 + ((n) * 0x10))
21 #define PINCTRL_DOE(n)          (0x0700 + ((n) * 0x10))
22 #define PINCTRL_PIN2IRQ(n)      (0x0800 + ((n) * 0x10))
23 #define PINCTRL_IRQEN(n)        (0x0900 + ((n) * 0x10))
24 #define PINCTRL_IRQSTAT(n)      (0x0c00 + ((n) * 0x10))
25 #elif   defined(CONFIG_MX28)
26 #define PINCTRL_BANKS           5
27 #define PINCTRL_DOUT(n)         (0x0700 + ((n) * 0x10))
28 #define PINCTRL_DIN(n)          (0x0900 + ((n) * 0x10))
29 #define PINCTRL_DOE(n)          (0x0b00 + ((n) * 0x10))
30 #define PINCTRL_PIN2IRQ(n)      (0x1000 + ((n) * 0x10))
31 #define PINCTRL_IRQEN(n)        (0x1100 + ((n) * 0x10))
32 #define PINCTRL_IRQSTAT(n)      (0x1400 + ((n) * 0x10))
33 #else
34 #error "Please select CONFIG_MX23 or CONFIG_MX28"
35 #endif
36
37 #define GPIO_INT_FALL_EDGE      0x0
38 #define GPIO_INT_LOW_LEV        0x1
39 #define GPIO_INT_RISE_EDGE      0x2
40 #define GPIO_INT_HIGH_LEV       0x3
41 #define GPIO_INT_LEV_MASK       (1 << 0)
42 #define GPIO_INT_POL_MASK       (1 << 1)
43
44 void mxs_gpio_init(void)
45 {
46         int i;
47
48         for (i = 0; i < PINCTRL_BANKS; i++) {
49                 writel(0, MXS_PINCTRL_BASE + PINCTRL_PIN2IRQ(i));
50                 writel(0, MXS_PINCTRL_BASE + PINCTRL_IRQEN(i));
51                 /* Use SCT address here to clear the IRQSTAT bits */
52                 writel(0xffffffff, MXS_PINCTRL_BASE + PINCTRL_IRQSTAT(i) + 8);
53         }
54 }
55
56 #if !CONFIG_IS_ENABLED(DM_GPIO)
57 int gpio_get_value(unsigned gpio)
58 {
59         uint32_t bank = PAD_BANK(gpio);
60         uint32_t offset = PINCTRL_DIN(bank);
61         struct mxs_register_32 *reg =
62                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
63
64         return (readl(&reg->reg) >> PAD_PIN(gpio)) & 1;
65 }
66
67 void gpio_set_value(unsigned gpio, int value)
68 {
69         uint32_t bank = PAD_BANK(gpio);
70         uint32_t offset = PINCTRL_DOUT(bank);
71         struct mxs_register_32 *reg =
72                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
73
74         if (value)
75                 writel(1 << PAD_PIN(gpio), &reg->reg_set);
76         else
77                 writel(1 << PAD_PIN(gpio), &reg->reg_clr);
78 }
79
80 int gpio_direction_input(unsigned gpio)
81 {
82         uint32_t bank = PAD_BANK(gpio);
83         uint32_t offset = PINCTRL_DOE(bank);
84         struct mxs_register_32 *reg =
85                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
86
87         writel(1 << PAD_PIN(gpio), &reg->reg_clr);
88
89         return 0;
90 }
91
92 int gpio_direction_output(unsigned gpio, int value)
93 {
94         uint32_t bank = PAD_BANK(gpio);
95         uint32_t offset = PINCTRL_DOE(bank);
96         struct mxs_register_32 *reg =
97                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
98
99         gpio_set_value(gpio, value);
100
101         writel(1 << PAD_PIN(gpio), &reg->reg_set);
102
103         return 0;
104 }
105
106 int gpio_request(unsigned gpio, const char *label)
107 {
108         if (PAD_BANK(gpio) >= PINCTRL_BANKS)
109                 return -1;
110
111         return 0;
112 }
113
114 int gpio_free(unsigned gpio)
115 {
116         return 0;
117 }
118
119 int name_to_gpio(const char *name)
120 {
121         unsigned bank, pin;
122         char *end;
123
124         bank = simple_strtoul(name, &end, 10);
125
126         if (!*end || *end != ':')
127                 return bank;
128
129         pin = simple_strtoul(end + 1, NULL, 10);
130
131         return (bank << MXS_PAD_BANK_SHIFT) | (pin << MXS_PAD_PIN_SHIFT);
132 }
133 #else /* DM_GPIO */
134 #include <dm.h>
135 #include <asm/gpio.h>
136 #include <dt-structs.h>
137 #include <asm/arch/gpio.h>
138 #define MXS_MAX_GPIO_PER_BANK           32
139
140 #ifdef CONFIG_MX28
141 #define dtd_fsl_imx_gpio dtd_fsl_imx28_gpio
142 #else /* CONFIG_MX23 */
143 #define dtd_fsl_imx_gpio dtd_fsl_imx23_gpio
144 #endif
145
146 DECLARE_GLOBAL_DATA_PTR;
147 /*
148  * According to i.MX28 Reference Manual:
149  * 'i.MX28 Applications Processor Reference Manual, Rev. 1, 2010'
150  * The i.MX28 has following number of GPIOs available:
151  * Bank 0: 0-28 -> 29 PINS
152  * Bank 1: 0-31 -> 32 PINS
153  * Bank 2: 0-27 -> 28 PINS
154  * Bank 3: 0-30 -> 31 PINS
155  * Bank 4: 0-20 -> 21 PINS
156  */
157
158 struct mxs_gpio_platdata {
159 #if CONFIG_IS_ENABLED(OF_PLATDATA)
160         struct dtd_fsl_imx_gpio dtplat;
161 #endif
162         unsigned int bank;
163         int gpio_ranges;
164 };
165
166 struct mxs_gpio_priv {
167         unsigned int bank;
168 };
169
170 static int mxs_gpio_get_value(struct udevice *dev, unsigned offset)
171 {
172         struct mxs_gpio_priv *priv = dev_get_priv(dev);
173         struct mxs_register_32 *reg =
174                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE +
175                                            PINCTRL_DIN(priv->bank));
176
177         return (readl(&reg->reg) >> offset) & 1;
178 }
179
180 static int mxs_gpio_set_value(struct udevice *dev, unsigned offset,
181                               int value)
182 {
183         struct mxs_gpio_priv *priv = dev_get_priv(dev);
184         struct mxs_register_32 *reg =
185                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE +
186                                            PINCTRL_DOUT(priv->bank));
187         if (value)
188                 writel(BIT(offset), &reg->reg_set);
189         else
190                 writel(BIT(offset), &reg->reg_clr);
191
192         return 0;
193 }
194
195 static int mxs_gpio_direction_input(struct udevice *dev, unsigned offset)
196 {
197         struct mxs_gpio_priv *priv = dev_get_priv(dev);
198         struct mxs_register_32 *reg =
199                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE +
200                                            PINCTRL_DOE(priv->bank));
201
202         writel(BIT(offset), &reg->reg_clr);
203
204         return 0;
205 }
206
207 static int mxs_gpio_direction_output(struct udevice *dev, unsigned offset,
208                                      int value)
209 {
210         struct mxs_gpio_priv *priv = dev_get_priv(dev);
211         struct mxs_register_32 *reg =
212                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE +
213                                            PINCTRL_DOE(priv->bank));
214
215         mxs_gpio_set_value(dev, offset, value);
216
217         writel(BIT(offset), &reg->reg_set);
218
219         return 0;
220 }
221
222 static int mxs_gpio_get_function(struct udevice *dev, unsigned offset)
223 {
224         struct mxs_gpio_priv *priv = dev_get_priv(dev);
225         struct mxs_register_32 *reg =
226                 (struct mxs_register_32 *)(MXS_PINCTRL_BASE +
227                                            PINCTRL_DOE(priv->bank));
228         bool is_output = !!(readl(&reg->reg) >> offset);
229
230         return is_output ? GPIOF_OUTPUT : GPIOF_INPUT;
231 }
232
233 static const struct dm_gpio_ops gpio_mxs_ops = {
234         .direction_input        = mxs_gpio_direction_input,
235         .direction_output       = mxs_gpio_direction_output,
236         .get_value              = mxs_gpio_get_value,
237         .set_value              = mxs_gpio_set_value,
238         .get_function           = mxs_gpio_get_function,
239 };
240
241 static int mxs_gpio_probe(struct udevice *dev)
242 {
243         struct mxs_gpio_platdata *plat = dev_get_platdata(dev);
244         struct mxs_gpio_priv *priv = dev_get_priv(dev);
245         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
246         char name[16], *str;
247
248 #if CONFIG_IS_ENABLED(OF_PLATDATA)
249         struct dtd_fsl_imx_gpio *dtplat = &plat->dtplat;
250         priv->bank = (unsigned int)dtplat->reg[0];
251         uc_priv->gpio_count = dtplat->gpio_ranges[3];
252 #else
253         priv->bank = (unsigned int)plat->bank;
254         uc_priv->gpio_count = plat->gpio_ranges;
255 #endif
256         snprintf(name, sizeof(name), "GPIO%d_", priv->bank);
257         str = strdup(name);
258         if (!str)
259                 return -ENOMEM;
260
261         uc_priv->bank_name = str;
262
263         debug("%s: %s: %d pins base: 0x%x\n", __func__, uc_priv->bank_name,
264               uc_priv->gpio_count, priv->bank);
265
266         return 0;
267 }
268
269 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
270 static int mxs_ofdata_to_platdata(struct udevice *dev)
271 {
272         struct mxs_gpio_platdata *plat = dev->platdata;
273         struct fdtdec_phandle_args args;
274         int node = dev_of_offset(dev);
275         int ret;
276
277         plat->bank = devfdt_get_addr(dev);
278         if (plat->bank == FDT_ADDR_T_NONE) {
279                 printf("%s: No 'reg' property defined!\n", __func__);
280                 return -EINVAL;
281         }
282
283         ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, node, "gpio-ranges",
284                                              NULL, 3, 0, &args);
285         if (ret)
286                 printf("%s: 'gpio-ranges' not defined - using default!\n",
287                        __func__);
288
289         plat->gpio_ranges = ret == 0 ? args.args[2] : MXS_MAX_GPIO_PER_BANK;
290
291         return 0;
292 }
293
294 static const struct udevice_id mxs_gpio_ids[] = {
295         { .compatible = "fsl,imx23-gpio" },
296         { .compatible = "fsl,imx28-gpio" },
297         { }
298 };
299 #endif
300
301 U_BOOT_DRIVER(gpio_mxs) = {
302 #ifdef CONFIG_MX28
303         .name = "fsl_imx28_gpio",
304 #else /* CONFIG_MX23 */
305         .name = "fsl_imx23_gpio",
306 #endif
307         .id     = UCLASS_GPIO,
308         .ops    = &gpio_mxs_ops,
309         .probe  = mxs_gpio_probe,
310         .priv_auto_alloc_size = sizeof(struct mxs_gpio_priv),
311         .platdata_auto_alloc_size = sizeof(struct mxs_gpio_platdata),
312 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
313         .of_match = mxs_gpio_ids,
314         .ofdata_to_platdata = mxs_ofdata_to_platdata,
315 #endif
316 };
317 #endif /* DM_GPIO */