3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/imx-regs.h>
18 enum mxc_gpio_direction {
19 MXC_GPIO_DIRECTION_IN,
20 MXC_GPIO_DIRECTION_OUT,
23 #define GPIO_PER_BANK 32
25 struct mxc_gpio_plat {
27 struct gpio_regs *regs;
30 struct mxc_bank_info {
31 struct gpio_regs *regs;
34 #ifndef CONFIG_DM_GPIO
35 #define GPIO_TO_PORT(n) (n / 32)
37 /* GPIO port description */
38 static unsigned long gpio_ports[] = {
39 [0] = GPIO1_BASE_ADDR,
40 [1] = GPIO2_BASE_ADDR,
41 [2] = GPIO3_BASE_ADDR,
42 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
43 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
44 defined(CONFIG_MX7) || defined(CONFIG_MX8M)
45 [3] = GPIO4_BASE_ADDR,
47 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
48 defined(CONFIG_MX7) || defined(CONFIG_MX8M)
49 [4] = GPIO5_BASE_ADDR,
50 #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || defined(CONFIG_MX8M))
51 [5] = GPIO6_BASE_ADDR,
54 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7)
55 #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL))
56 [6] = GPIO7_BASE_ADDR,
61 static int mxc_gpio_direction(unsigned int gpio,
62 enum mxc_gpio_direction direction)
64 unsigned int port = GPIO_TO_PORT(gpio);
65 struct gpio_regs *regs;
68 if (port >= ARRAY_SIZE(gpio_ports))
73 regs = (struct gpio_regs *)gpio_ports[port];
75 l = readl(®s->gpio_dir);
78 case MXC_GPIO_DIRECTION_OUT:
81 case MXC_GPIO_DIRECTION_IN:
84 writel(l, ®s->gpio_dir);
89 int gpio_set_value(unsigned gpio, int value)
91 unsigned int port = GPIO_TO_PORT(gpio);
92 struct gpio_regs *regs;
95 if (port >= ARRAY_SIZE(gpio_ports))
100 regs = (struct gpio_regs *)gpio_ports[port];
102 l = readl(®s->gpio_dr);
107 writel(l, ®s->gpio_dr);
112 int gpio_get_value(unsigned gpio)
114 unsigned int port = GPIO_TO_PORT(gpio);
115 struct gpio_regs *regs;
118 if (port >= ARRAY_SIZE(gpio_ports))
123 regs = (struct gpio_regs *)gpio_ports[port];
125 val = (readl(®s->gpio_psr) >> gpio) & 0x01;
130 int gpio_request(unsigned gpio, const char *label)
132 unsigned int port = GPIO_TO_PORT(gpio);
133 if (port >= ARRAY_SIZE(gpio_ports))
138 int gpio_free(unsigned gpio)
143 int gpio_direction_input(unsigned gpio)
145 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
148 int gpio_direction_output(unsigned gpio, int value)
150 int ret = gpio_set_value(gpio, value);
155 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
159 #ifdef CONFIG_DM_GPIO
161 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
165 val = readl(®s->gpio_dir);
167 return val & (1 << offset) ? 1 : 0;
170 static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
171 enum mxc_gpio_direction direction)
175 l = readl(®s->gpio_dir);
178 case MXC_GPIO_DIRECTION_OUT:
181 case MXC_GPIO_DIRECTION_IN:
184 writel(l, ®s->gpio_dir);
187 static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
192 l = readl(®s->gpio_dr);
197 writel(l, ®s->gpio_dr);
200 static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
202 return (readl(®s->gpio_psr) >> offset) & 0x01;
205 /* set GPIO pin 'gpio' as an input */
206 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
208 struct mxc_bank_info *bank = dev_get_priv(dev);
210 /* Configure GPIO direction as input. */
211 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
216 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
217 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
220 struct mxc_bank_info *bank = dev_get_priv(dev);
222 /* Configure GPIO output value. */
223 mxc_gpio_bank_set_value(bank->regs, offset, value);
225 /* Configure GPIO direction as output. */
226 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
231 /* read GPIO IN value of pin 'gpio' */
232 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
234 struct mxc_bank_info *bank = dev_get_priv(dev);
236 return mxc_gpio_bank_get_value(bank->regs, offset);
239 /* write GPIO OUT value to pin 'gpio' */
240 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
243 struct mxc_bank_info *bank = dev_get_priv(dev);
245 mxc_gpio_bank_set_value(bank->regs, offset, value);
250 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
252 struct mxc_bank_info *bank = dev_get_priv(dev);
254 /* GPIOF_FUNC is not implemented yet */
255 if (mxc_gpio_is_output(bank->regs, offset))
261 static const struct dm_gpio_ops gpio_mxc_ops = {
262 .direction_input = mxc_gpio_direction_input,
263 .direction_output = mxc_gpio_direction_output,
264 .get_value = mxc_gpio_get_value,
265 .set_value = mxc_gpio_set_value,
266 .get_function = mxc_gpio_get_function,
269 static int mxc_gpio_probe(struct udevice *dev)
271 struct mxc_bank_info *bank = dev_get_priv(dev);
272 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
273 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
277 banknum = plat->bank_index;
278 sprintf(name, "GPIO%d_", banknum + 1);
282 uc_priv->bank_name = str;
283 uc_priv->gpio_count = GPIO_PER_BANK;
284 bank->regs = plat->regs;
289 static int mxc_gpio_bind(struct udevice *dev)
291 struct mxc_gpio_plat *plat = dev->platdata;
295 * If platdata already exsits, directly return.
296 * Actually only when DT is not supported, platdata
297 * is statically initialized in U_BOOT_DEVICES.Here
303 addr = devfdt_get_addr(dev);
304 if (addr == FDT_ADDR_T_NONE)
309 * When every board is converted to driver model and DT is supported,
310 * this can be done by auto-alloc feature, but not using calloc
311 * to alloc memory for platdata.
313 * For example mxc_plat below uses platform data rather than device
316 * NOTE: DO NOT COPY this code if you are using device tree.
318 plat = calloc(1, sizeof(*plat));
322 plat->regs = (struct gpio_regs *)addr;
323 plat->bank_index = dev->req_seq;
324 dev->platdata = plat;
329 static const struct udevice_id mxc_gpio_ids[] = {
330 { .compatible = "fsl,imx35-gpio" },
334 U_BOOT_DRIVER(gpio_mxc) = {
337 .ops = &gpio_mxc_ops,
338 .probe = mxc_gpio_probe,
339 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
340 .of_match = mxc_gpio_ids,
341 .bind = mxc_gpio_bind,
344 #if !CONFIG_IS_ENABLED(OF_CONTROL)
345 static const struct mxc_gpio_plat mxc_plat[] = {
346 { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
347 { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
348 { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
349 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
350 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
352 { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
354 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
356 { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
358 { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
361 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
362 { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
366 U_BOOT_DEVICES(mxc_gpios) = {
367 { "gpio_mxc", &mxc_plat[0] },
368 { "gpio_mxc", &mxc_plat[1] },
369 { "gpio_mxc", &mxc_plat[2] },
370 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
371 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
373 { "gpio_mxc", &mxc_plat[3] },
375 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
377 { "gpio_mxc", &mxc_plat[4] },
379 { "gpio_mxc", &mxc_plat[5] },
382 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
383 { "gpio_mxc", &mxc_plat[6] },