Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio/aspeed-sgpio: enable access to all 80 input & output sgpios

Currently, the aspeed-sgpio driver exposes up to 80 GPIO lines,
corresponding to the 80 status bits available in hardware. Each of these
lines can be configured as either an input or an output.

However, each of these GPIOs is actually an input *and* an output; we
actually have 80 inputs plus 80 outputs.

This change expands the maximum number of GPIOs to 160; the lower half
of this range are the input-only GPIOs, the upper half are the outputs.
We fix the GPIO directions to correspond to this mapping.

This also fixes a bug when setting GPIOs - we were reading from the
input register, making it impossible to set more than one output GPIO.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
Fixes: 7db47faae79b ("gpio: aspeed: Add SGPIO driver")
Reviewed-by: Joel Stanley <joel@jms.id.au>
Reviewed-by: Andrew Jeffery <andrew@aj.id.au>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

authored by

Jeremy Kerr and committed by
Bartosz Golaszewski
ac67b07e e43c26e1

+87 -44
+3 -2
Documentation/devicetree/bindings/gpio/sgpio-aspeed.txt
··· 20 20 - gpio-controller : Marks the device node as a GPIO controller 21 21 - interrupts : Interrupt specifier, see interrupt-controller/interrupts.txt 22 22 - interrupt-controller : Mark the GPIO controller as an interrupt-controller 23 - - ngpios : number of GPIO lines, see gpio.txt 24 - (should be multiple of 8, up to 80 pins) 23 + - ngpios : number of *hardware* GPIO lines, see gpio.txt. This will expose 24 + 2 software GPIOs per hardware GPIO: one for hardware input, one for hardware 25 + output. Up to 80 pins, must be a multiple of 8. 25 26 - clocks : A phandle to the APB clock for SGPM clock division 26 27 - bus-frequency : SGPM CLK frequency 27 28
+84 -42
drivers/gpio/gpio-aspeed-sgpio.c
··· 17 17 #include <linux/spinlock.h> 18 18 #include <linux/string.h> 19 19 20 - #define MAX_NR_SGPIO 80 20 + /* 21 + * MAX_NR_HW_GPIO represents the number of actual hardware-supported GPIOs (ie, 22 + * slots within the clocked serial GPIO data). Since each HW GPIO is both an 23 + * input and an output, we provide MAX_NR_HW_GPIO * 2 lines on our gpiochip 24 + * device. 25 + * 26 + * We use SGPIO_OUTPUT_OFFSET to define the split between the inputs and 27 + * outputs; the inputs start at line 0, the outputs start at OUTPUT_OFFSET. 28 + */ 29 + #define MAX_NR_HW_SGPIO 80 30 + #define SGPIO_OUTPUT_OFFSET MAX_NR_HW_SGPIO 21 31 22 32 #define ASPEED_SGPIO_CTRL 0x54 23 33 ··· 40 30 struct clk *pclk; 41 31 spinlock_t lock; 42 32 void __iomem *base; 43 - uint32_t dir_in[3]; 44 33 int irq; 34 + int n_sgpio; 45 35 }; 46 36 47 37 struct aspeed_sgpio_bank { ··· 121 111 } 122 112 } 123 113 124 - #define GPIO_BANK(x) ((x) >> 5) 125 - #define GPIO_OFFSET(x) ((x) & 0x1f) 114 + #define GPIO_BANK(x) ((x % SGPIO_OUTPUT_OFFSET) >> 5) 115 + #define GPIO_OFFSET(x) ((x % SGPIO_OUTPUT_OFFSET) & 0x1f) 126 116 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x)) 127 117 128 118 static const struct aspeed_sgpio_bank *to_bank(unsigned int offset) 129 119 { 130 - unsigned int bank = GPIO_BANK(offset); 120 + unsigned int bank; 121 + 122 + bank = GPIO_BANK(offset); 131 123 132 124 WARN_ON(bank >= ARRAY_SIZE(aspeed_sgpio_banks)); 133 125 return &aspeed_sgpio_banks[bank]; 126 + } 127 + 128 + static int aspeed_sgpio_init_valid_mask(struct gpio_chip *gc, 129 + unsigned long *valid_mask, unsigned int ngpios) 130 + { 131 + struct aspeed_sgpio *sgpio = gpiochip_get_data(gc); 132 + int n = sgpio->n_sgpio; 133 + int c = SGPIO_OUTPUT_OFFSET - n; 134 + 135 + WARN_ON(ngpios < MAX_NR_HW_SGPIO * 2); 136 + 137 + /* input GPIOs in the lower range */ 138 + bitmap_set(valid_mask, 0, n); 139 + bitmap_clear(valid_mask, n, c); 140 + 141 + /* output GPIOS above SGPIO_OUTPUT_OFFSET */ 142 + bitmap_set(valid_mask, SGPIO_OUTPUT_OFFSET, n); 143 + bitmap_clear(valid_mask, SGPIO_OUTPUT_OFFSET + n, c); 144 + 145 + return 0; 146 + } 147 + 148 + static void aspeed_sgpio_irq_init_valid_mask(struct gpio_chip *gc, 149 + unsigned long *valid_mask, unsigned int ngpios) 150 + { 151 + struct aspeed_sgpio *sgpio = gpiochip_get_data(gc); 152 + int n = sgpio->n_sgpio; 153 + 154 + WARN_ON(ngpios < MAX_NR_HW_SGPIO * 2); 155 + 156 + /* input GPIOs in the lower range */ 157 + bitmap_set(valid_mask, 0, n); 158 + bitmap_clear(valid_mask, n, ngpios - n); 159 + } 160 + 161 + static bool aspeed_sgpio_is_input(unsigned int offset) 162 + { 163 + return offset < SGPIO_OUTPUT_OFFSET; 134 164 } 135 165 136 166 static int aspeed_sgpio_get(struct gpio_chip *gc, unsigned int offset) ··· 179 129 const struct aspeed_sgpio_bank *bank = to_bank(offset); 180 130 unsigned long flags; 181 131 enum aspeed_sgpio_reg reg; 182 - bool is_input; 183 132 int rc = 0; 184 133 185 134 spin_lock_irqsave(&gpio->lock, flags); 186 135 187 - is_input = gpio->dir_in[GPIO_BANK(offset)] & GPIO_BIT(offset); 188 - reg = is_input ? reg_val : reg_rdata; 136 + reg = aspeed_sgpio_is_input(offset) ? reg_val : reg_rdata; 189 137 rc = !!(ioread32(bank_reg(gpio, bank, reg)) & GPIO_BIT(offset)); 190 138 191 139 spin_unlock_irqrestore(&gpio->lock, flags); ··· 191 143 return rc; 192 144 } 193 145 194 - static void sgpio_set_value(struct gpio_chip *gc, unsigned int offset, int val) 146 + static int sgpio_set_value(struct gpio_chip *gc, unsigned int offset, int val) 195 147 { 196 148 struct aspeed_sgpio *gpio = gpiochip_get_data(gc); 197 149 const struct aspeed_sgpio_bank *bank = to_bank(offset); 198 - void __iomem *addr; 150 + void __iomem *addr_r, *addr_w; 199 151 u32 reg = 0; 200 152 201 - addr = bank_reg(gpio, bank, reg_val); 202 - reg = ioread32(addr); 153 + if (aspeed_sgpio_is_input(offset)) 154 + return -EINVAL; 155 + 156 + /* Since this is an output, read the cached value from rdata, then 157 + * update val. */ 158 + addr_r = bank_reg(gpio, bank, reg_rdata); 159 + addr_w = bank_reg(gpio, bank, reg_val); 160 + 161 + reg = ioread32(addr_r); 203 162 204 163 if (val) 205 164 reg |= GPIO_BIT(offset); 206 165 else 207 166 reg &= ~GPIO_BIT(offset); 208 167 209 - iowrite32(reg, addr); 168 + iowrite32(reg, addr_w); 169 + 170 + return 0; 210 171 } 211 172 212 173 static void aspeed_sgpio_set(struct gpio_chip *gc, unsigned int offset, int val) ··· 232 175 233 176 static int aspeed_sgpio_dir_in(struct gpio_chip *gc, unsigned int offset) 234 177 { 235 - struct aspeed_sgpio *gpio = gpiochip_get_data(gc); 236 - unsigned long flags; 237 - 238 - spin_lock_irqsave(&gpio->lock, flags); 239 - gpio->dir_in[GPIO_BANK(offset)] |= GPIO_BIT(offset); 240 - spin_unlock_irqrestore(&gpio->lock, flags); 241 - 242 - return 0; 178 + return aspeed_sgpio_is_input(offset) ? 0 : -EINVAL; 243 179 } 244 180 245 181 static int aspeed_sgpio_dir_out(struct gpio_chip *gc, unsigned int offset, int val) 246 182 { 247 183 struct aspeed_sgpio *gpio = gpiochip_get_data(gc); 248 184 unsigned long flags; 185 + int rc; 186 + 187 + /* No special action is required for setting the direction; we'll 188 + * error-out in sgpio_set_value if this isn't an output GPIO */ 249 189 250 190 spin_lock_irqsave(&gpio->lock, flags); 251 - 252 - gpio->dir_in[GPIO_BANK(offset)] &= ~GPIO_BIT(offset); 253 - sgpio_set_value(gc, offset, val); 254 - 191 + rc = sgpio_set_value(gc, offset, val); 255 192 spin_unlock_irqrestore(&gpio->lock, flags); 256 193 257 - return 0; 194 + return rc; 258 195 } 259 196 260 197 static int aspeed_sgpio_get_direction(struct gpio_chip *gc, unsigned int offset) 261 198 { 262 - int dir_status; 263 - struct aspeed_sgpio *gpio = gpiochip_get_data(gc); 264 - unsigned long flags; 265 - 266 - spin_lock_irqsave(&gpio->lock, flags); 267 - dir_status = gpio->dir_in[GPIO_BANK(offset)] & GPIO_BIT(offset); 268 - spin_unlock_irqrestore(&gpio->lock, flags); 269 - 270 - return dir_status; 271 - 199 + return !!aspeed_sgpio_is_input(offset); 272 200 } 273 201 274 202 static void irqd_to_aspeed_sgpio_data(struct irq_data *d, ··· 444 402 445 403 irq = &gpio->chip.irq; 446 404 irq->chip = &aspeed_sgpio_irqchip; 405 + irq->init_valid_mask = aspeed_sgpio_irq_init_valid_mask; 447 406 irq->handler = handle_bad_irq; 448 407 irq->default_type = IRQ_TYPE_NONE; 449 408 irq->parent_handler = aspeed_sgpio_irq_handler; ··· 495 452 if (rc < 0) { 496 453 dev_err(&pdev->dev, "Could not read ngpios property\n"); 497 454 return -EINVAL; 498 - } else if (nr_gpios > MAX_NR_SGPIO) { 455 + } else if (nr_gpios > MAX_NR_HW_SGPIO) { 499 456 dev_err(&pdev->dev, "Number of GPIOs exceeds the maximum of %d: %d\n", 500 - MAX_NR_SGPIO, nr_gpios); 457 + MAX_NR_HW_SGPIO, nr_gpios); 501 458 return -EINVAL; 502 459 } 460 + gpio->n_sgpio = nr_gpios; 503 461 504 462 rc = of_property_read_u32(pdev->dev.of_node, "bus-frequency", &sgpio_freq); 505 463 if (rc < 0) { ··· 541 497 spin_lock_init(&gpio->lock); 542 498 543 499 gpio->chip.parent = &pdev->dev; 544 - gpio->chip.ngpio = nr_gpios; 500 + gpio->chip.ngpio = MAX_NR_HW_SGPIO * 2; 501 + gpio->chip.init_valid_mask = aspeed_sgpio_init_valid_mask; 545 502 gpio->chip.direction_input = aspeed_sgpio_dir_in; 546 503 gpio->chip.direction_output = aspeed_sgpio_dir_out; 547 504 gpio->chip.get_direction = aspeed_sgpio_get_direction; ··· 553 508 gpio->chip.set_config = NULL; 554 509 gpio->chip.label = dev_name(&pdev->dev); 555 510 gpio->chip.base = -1; 556 - 557 - /* set all SGPIO pins as input (1). */ 558 - memset(gpio->dir_in, 0xff, sizeof(gpio->dir_in)); 559 511 560 512 aspeed_sgpio_setup_irqs(gpio, pdev); 561 513