blob: de87440d8ff2f47f98b90a1ec3db8b0d6897ab36 [file] [log] [blame]
Alexandru Gagniucf64111b2013-12-13 20:44:48 -06001/*
2 * Helpers to multiplex and configure pins on Allwinner SoCs
3 *
4 * Copyright (C) 2013 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 * Subject to the GNU GPL v2, or (at your option) any later version.
6 */
7
8#include "gpio.h"
9
10#include <arch/io.h>
11
12static struct a10_gpio *const gpio = (void *)GPIO_BASE;
13
Alexandru Gagniuc8226dbb2013-12-22 21:30:21 -050014/**
15 * \brief Set the pad function of a single pin
16 *
17 * @param[in] port GPIO port of the pin (GPA -> GPS)
18 * @param[in] pin the pin number in the given port (1 -> 31)
19 * @param[in] pad_func The peripheral function to which to connect this pin
20 */
21void gpio_set_pin_func(u8 port, u8 pin, u8 pad_func)
Alexandru Gagniucf64111b2013-12-13 20:44:48 -060022{
23 u8 reg, bit;
24 u32 reg32;
25
26 if ((port > GPS))
27 return;
28
29 pin &= 0x1f;
30 reg = pin / 8;
31 bit = (pin % 8) * 4;
32
33 reg32 = read32(&gpio->port[port].cfg[reg]);
34 reg32 &= ~(0xf << bit);
35 reg32 |= (pad_func & 0xf) << bit;
36 write32(reg32, &gpio->port[port].cfg[reg]);
37}
Alexandru Gagniuc8226dbb2013-12-22 21:30:21 -050038
39/**
40 * \brief Set the pad function of a group of pins
41 *
42 * Multiplex a group of pins to the same pad function. This is useful for
43 * peripherals that use the same function number for several pins. This function
44 * allows those pins to be set with a single call.
45 *
46 * Example:
47 * gpio_set_multipin_func(GPB, (1 << 23) | (1 << 22), 2);
48 *
49 * @param[in] port GPIO port of the pin (GPA -> GPS)
50 * @param[in] pin_mask 32-bit mask indicating which pins to re-multiplex. For
51 * each set bit, the corresponding pin will be multiplexed.
52 * @param[in] pad_func The peripheral function to which to connect the pins
53 */
54void gpio_set_multipin_func(u8 port, u32 pin_mask, u8 pad_func)
55{
56 int j;
57 u8 reg, bit;
58 u32 reg32, mask_offset;
59
60 if ((port > GPS))
61 return;
62
63 for (reg = 0; reg < 4; reg++) {
64 mask_offset = 8 * reg;
65 /* Don't run the inner loop if we're not touching any pins */
66 if (!(pin_mask & (0xff << mask_offset)))
67 continue;
68
69 reg32 = read32(&gpio->port[port].cfg[reg]);
70 for (j = 0; j < 8; j++) {
71 if (!(pin_mask & (1 << (j + mask_offset))))
72 continue;
73 bit = j * 4;
74 reg32 &= ~(0xf << bit);
75 reg32 |= (pad_func & 0xf) << bit;
76 }
77 write32(reg32, &gpio->port[port].cfg[reg]);
78 }
79}