Kuninori Morimoto | 63b6d7e | 2018-09-07 02:13:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 2 | /* |
| 3 | * SuperH Pin Function Controller GPIO driver. |
| 4 | * |
| 5 | * Copyright (C) 2008 Magnus Damm |
| 6 | * Copyright (C) 2009 - 2012 Paul Mundt |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 7 | */ |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 8 | |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 9 | #include <linux/device.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 10 | #include <linux/gpio.h> |
Laurent Pinchart | 90efde2 | 2012-12-15 23:50:52 +0100 | [diff] [blame] | 11 | #include <linux/init.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 12 | #include <linux/module.h> |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 13 | #include <linux/pinctrl/consumer.h> |
Laurent Pinchart | 90efde2 | 2012-12-15 23:50:52 +0100 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/spinlock.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 16 | |
Laurent Pinchart | f916513 | 2012-12-15 23:50:44 +0100 | [diff] [blame] | 17 | #include "core.h" |
| 18 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 19 | struct sh_pfc_gpio_data_reg { |
| 20 | const struct pinmux_data_reg *info; |
Geert Uytterhoeven | fc88936 | 2015-02-27 18:38:04 +0100 | [diff] [blame] | 21 | u32 shadow; |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 22 | }; |
| 23 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 24 | struct sh_pfc_gpio_pin { |
| 25 | u8 dbit; |
| 26 | u8 dreg; |
| 27 | }; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 28 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 29 | struct sh_pfc_chip { |
| 30 | struct sh_pfc *pfc; |
| 31 | struct gpio_chip gpio_chip; |
| 32 | |
| 33 | struct sh_pfc_window *mem; |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 34 | struct sh_pfc_gpio_data_reg *regs; |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 35 | struct sh_pfc_gpio_pin *pins; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 36 | }; |
| 37 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 38 | static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc) |
| 39 | { |
Linus Walleij | 7cb093c | 2015-12-08 10:24:54 +0100 | [diff] [blame] | 40 | struct sh_pfc_chip *chip = gpiochip_get_data(gc); |
| 41 | return chip->pfc; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 42 | } |
| 43 | |
Laurent Pinchart | 757b055 | 2013-07-15 13:25:08 +0200 | [diff] [blame] | 44 | static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int offset, |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 45 | struct sh_pfc_gpio_data_reg **reg, |
| 46 | unsigned int *bit) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 47 | { |
Laurent Pinchart | 757b055 | 2013-07-15 13:25:08 +0200 | [diff] [blame] | 48 | int idx = sh_pfc_get_pin_index(chip->pfc, offset); |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 49 | struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx]; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 50 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 51 | *reg = &chip->regs[gpio_pin->dreg]; |
| 52 | *bit = gpio_pin->dbit; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Geert Uytterhoeven | fc88936 | 2015-02-27 18:38:04 +0100 | [diff] [blame] | 55 | static u32 gpio_read_data_reg(struct sh_pfc_chip *chip, |
| 56 | const struct pinmux_data_reg *dreg) |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 57 | { |
Geert Uytterhoeven | 1f34de0 | 2015-03-12 11:09:16 +0100 | [diff] [blame] | 58 | phys_addr_t address = dreg->reg; |
| 59 | void __iomem *mem = address - chip->mem->phys + chip->mem->virt; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 60 | |
| 61 | return sh_pfc_read_raw_reg(mem, dreg->reg_width); |
| 62 | } |
| 63 | |
| 64 | static void gpio_write_data_reg(struct sh_pfc_chip *chip, |
Geert Uytterhoeven | fc88936 | 2015-02-27 18:38:04 +0100 | [diff] [blame] | 65 | const struct pinmux_data_reg *dreg, u32 value) |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 66 | { |
Geert Uytterhoeven | 1f34de0 | 2015-03-12 11:09:16 +0100 | [diff] [blame] | 67 | phys_addr_t address = dreg->reg; |
| 68 | void __iomem *mem = address - chip->mem->phys + chip->mem->virt; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 69 | |
| 70 | sh_pfc_write_raw_reg(mem, dreg->reg_width, value); |
| 71 | } |
| 72 | |
Laurent Pinchart | 757b055 | 2013-07-15 13:25:08 +0200 | [diff] [blame] | 73 | static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 74 | { |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 75 | struct sh_pfc *pfc = chip->pfc; |
Laurent Pinchart | 757b055 | 2013-07-15 13:25:08 +0200 | [diff] [blame] | 76 | struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx]; |
| 77 | const struct sh_pfc_pin *pin = &pfc->info->pins[idx]; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 78 | const struct pinmux_data_reg *dreg; |
| 79 | unsigned int bit; |
| 80 | unsigned int i; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 81 | |
Geert Uytterhoeven | 17c7cbb | 2015-03-12 11:09:15 +0100 | [diff] [blame] | 82 | for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) { |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 83 | for (bit = 0; bit < dreg->reg_width; bit++) { |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 84 | if (dreg->enum_ids[bit] == pin->enum_id) { |
| 85 | gpio_pin->dreg = i; |
| 86 | gpio_pin->dbit = bit; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | } |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | BUG(); |
| 93 | } |
| 94 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 95 | static int gpio_setup_data_regs(struct sh_pfc_chip *chip) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 96 | { |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 97 | struct sh_pfc *pfc = chip->pfc; |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 98 | const struct pinmux_data_reg *dreg; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 99 | unsigned int i; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 100 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 101 | /* Count the number of data registers, allocate memory and initialize |
| 102 | * them. |
| 103 | */ |
| 104 | for (i = 0; pfc->info->data_regs[i].reg_width; ++i) |
| 105 | ; |
| 106 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 107 | chip->regs = devm_kcalloc(pfc->dev, i, sizeof(*chip->regs), |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 108 | GFP_KERNEL); |
| 109 | if (chip->regs == NULL) |
| 110 | return -ENOMEM; |
| 111 | |
| 112 | for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) { |
| 113 | chip->regs[i].info = dreg; |
| 114 | chip->regs[i].shadow = gpio_read_data_reg(chip, dreg); |
| 115 | } |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 116 | |
| 117 | for (i = 0; i < pfc->info->nr_pins; i++) { |
| 118 | if (pfc->info->pins[i].enum_id == 0) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 119 | continue; |
| 120 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 121 | gpio_setup_data_reg(chip, i); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 124 | return 0; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 127 | /* ----------------------------------------------------------------------------- |
| 128 | * Pin GPIOs |
| 129 | */ |
| 130 | |
| 131 | static int gpio_pin_request(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 132 | { |
Laurent Pinchart | 0b73ee5 | 2013-02-14 22:12:11 +0100 | [diff] [blame] | 133 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 134 | int idx = sh_pfc_get_pin_index(pfc, offset); |
Laurent Pinchart | 0b73ee5 | 2013-02-14 22:12:11 +0100 | [diff] [blame] | 135 | |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 136 | if (idx < 0 || pfc->info->pins[idx].enum_id == 0) |
Laurent Pinchart | 0b73ee5 | 2013-02-14 22:12:11 +0100 | [diff] [blame] | 137 | return -EINVAL; |
| 138 | |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 139 | return pinctrl_gpio_request(offset); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 142 | static void gpio_pin_free(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 143 | { |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 144 | return pinctrl_gpio_free(offset); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 147 | static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset, |
| 148 | int value) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 149 | { |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 150 | struct sh_pfc_gpio_data_reg *reg; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 151 | unsigned int bit; |
Geert Uytterhoeven | cef28a2 | 2015-03-12 11:09:14 +0100 | [diff] [blame] | 152 | unsigned int pos; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 153 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 154 | gpio_get_data_reg(chip, offset, ®, &bit); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 155 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 156 | pos = reg->info->reg_width - (bit + 1); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 157 | |
| 158 | if (value) |
Geert Uytterhoeven | fc88936 | 2015-02-27 18:38:04 +0100 | [diff] [blame] | 159 | reg->shadow |= BIT(pos); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 160 | else |
Geert Uytterhoeven | fc88936 | 2015-02-27 18:38:04 +0100 | [diff] [blame] | 161 | reg->shadow &= ~BIT(pos); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 162 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 163 | gpio_write_data_reg(chip, reg->info, reg->shadow); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 164 | } |
| 165 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 166 | static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 167 | { |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 168 | return pinctrl_gpio_direction_input(offset); |
| 169 | } |
| 170 | |
| 171 | static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset, |
| 172 | int value) |
| 173 | { |
Linus Walleij | 7cb093c | 2015-12-08 10:24:54 +0100 | [diff] [blame] | 174 | gpio_pin_set_value(gpiochip_get_data(gc), offset, value); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 175 | |
| 176 | return pinctrl_gpio_direction_output(offset); |
| 177 | } |
| 178 | |
| 179 | static int gpio_pin_get(struct gpio_chip *gc, unsigned offset) |
| 180 | { |
Linus Walleij | 7cb093c | 2015-12-08 10:24:54 +0100 | [diff] [blame] | 181 | struct sh_pfc_chip *chip = gpiochip_get_data(gc); |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 182 | struct sh_pfc_gpio_data_reg *reg; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 183 | unsigned int bit; |
Geert Uytterhoeven | cef28a2 | 2015-03-12 11:09:14 +0100 | [diff] [blame] | 184 | unsigned int pos; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 185 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 186 | gpio_get_data_reg(chip, offset, ®, &bit); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 187 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 188 | pos = reg->info->reg_width - (bit + 1); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 189 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame] | 190 | return (gpio_read_data_reg(chip, reg->info) >> pos) & 1; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 193 | static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value) |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 194 | { |
Linus Walleij | 7cb093c | 2015-12-08 10:24:54 +0100 | [diff] [blame] | 195 | gpio_pin_set_value(gpiochip_get_data(gc), offset, value); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 196 | } |
| 197 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 198 | static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 199 | { |
| 200 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | 8d72a7f | 2013-12-11 04:26:21 +0100 | [diff] [blame] | 201 | unsigned int i, k; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 202 | |
Laurent Pinchart | c07f54f | 2013-01-03 14:12:14 +0100 | [diff] [blame] | 203 | for (i = 0; i < pfc->info->gpio_irq_size; i++) { |
Laurent Pinchart | 6d5bddd | 2013-12-16 20:25:15 +0100 | [diff] [blame] | 204 | const short *gpios = pfc->info->gpio_irq[i].gpios; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 205 | |
Laurent Pinchart | 316b255 | 2013-12-11 04:26:22 +0100 | [diff] [blame] | 206 | for (k = 0; gpios[k] >= 0; k++) { |
Laurent Pinchart | c07f54f | 2013-01-03 14:12:14 +0100 | [diff] [blame] | 207 | if (gpios[k] == offset) |
Laurent Pinchart | 70c8f01 | 2013-12-11 04:26:26 +0100 | [diff] [blame] | 208 | goto found; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Geert Uytterhoeven | 9697643 | 2016-05-04 10:21:53 +0200 | [diff] [blame] | 212 | return 0; |
Laurent Pinchart | 70c8f01 | 2013-12-11 04:26:26 +0100 | [diff] [blame] | 213 | |
| 214 | found: |
Laurent Pinchart | 4adeabd | 2015-09-22 10:08:13 +0300 | [diff] [blame] | 215 | return pfc->irqs[i]; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 216 | } |
| 217 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 218 | static int gpio_pin_setup(struct sh_pfc_chip *chip) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 219 | { |
| 220 | struct sh_pfc *pfc = chip->pfc; |
| 221 | struct gpio_chip *gc = &chip->gpio_chip; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 222 | int ret; |
| 223 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 224 | chip->pins = devm_kcalloc(pfc->dev, |
| 225 | pfc->info->nr_pins, sizeof(*chip->pins), |
| 226 | GFP_KERNEL); |
Laurent Pinchart | 1a0039d | 2013-03-08 17:43:54 +0100 | [diff] [blame] | 227 | if (chip->pins == NULL) |
| 228 | return -ENOMEM; |
| 229 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 230 | ret = gpio_setup_data_regs(chip); |
| 231 | if (ret < 0) |
| 232 | return ret; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 233 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 234 | gc->request = gpio_pin_request; |
| 235 | gc->free = gpio_pin_free; |
| 236 | gc->direction_input = gpio_pin_direction_input; |
| 237 | gc->get = gpio_pin_get; |
| 238 | gc->direction_output = gpio_pin_direction_output; |
| 239 | gc->set = gpio_pin_set; |
| 240 | gc->to_irq = gpio_pin_to_irq; |
| 241 | |
| 242 | gc->label = pfc->info->name; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 243 | gc->parent = pfc->dev; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 244 | gc->owner = THIS_MODULE; |
| 245 | gc->base = 0; |
Laurent Pinchart | 28818fa | 2013-07-15 13:48:56 +0200 | [diff] [blame] | 246 | gc->ngpio = pfc->nr_gpio_pins; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 247 | |
| 248 | return 0; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* ----------------------------------------------------------------------------- |
| 252 | * Function GPIOs |
| 253 | */ |
| 254 | |
Geert Uytterhoeven | 0ace959 | 2019-01-21 17:05:45 +0100 | [diff] [blame] | 255 | #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 256 | static int gpio_function_request(struct gpio_chip *gc, unsigned offset) |
| 257 | { |
Laurent Pinchart | 9a643c9 | 2013-03-10 18:00:02 +0100 | [diff] [blame] | 258 | static bool __print_once; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 259 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 260 | unsigned int mark = pfc->info->func_gpios[offset].enum_id; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 261 | unsigned long flags; |
Laurent Pinchart | b705c05 | 2013-03-10 16:38:23 +0100 | [diff] [blame] | 262 | int ret; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 263 | |
Laurent Pinchart | 9a643c9 | 2013-03-10 18:00:02 +0100 | [diff] [blame] | 264 | if (!__print_once) { |
| 265 | dev_notice(pfc->dev, |
| 266 | "Use of GPIO API for function requests is deprecated." |
| 267 | " Convert to pinctrl\n"); |
| 268 | __print_once = true; |
| 269 | } |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 270 | |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 271 | if (mark == 0) |
Laurent Pinchart | b705c05 | 2013-03-10 16:38:23 +0100 | [diff] [blame] | 272 | return -EINVAL; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 273 | |
| 274 | spin_lock_irqsave(&pfc->lock, flags); |
Laurent Pinchart | b705c05 | 2013-03-10 16:38:23 +0100 | [diff] [blame] | 275 | ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 276 | spin_unlock_irqrestore(&pfc->lock, flags); |
Laurent Pinchart | b705c05 | 2013-03-10 16:38:23 +0100 | [diff] [blame] | 277 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 278 | return ret; |
| 279 | } |
| 280 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 281 | static int gpio_function_setup(struct sh_pfc_chip *chip) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 282 | { |
| 283 | struct sh_pfc *pfc = chip->pfc; |
| 284 | struct gpio_chip *gc = &chip->gpio_chip; |
| 285 | |
| 286 | gc->request = gpio_function_request; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 287 | |
Laurent Pinchart | 19bb7fe3 | 2012-12-15 23:51:20 +0100 | [diff] [blame] | 288 | gc->label = pfc->info->name; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 289 | gc->owner = THIS_MODULE; |
Laurent Pinchart | 28818fa | 2013-07-15 13:48:56 +0200 | [diff] [blame] | 290 | gc->base = pfc->nr_gpio_pins; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 291 | gc->ngpio = pfc->info->nr_func_gpios; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 292 | |
| 293 | return 0; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 294 | } |
Geert Uytterhoeven | 0ace959 | 2019-01-21 17:05:45 +0100 | [diff] [blame] | 295 | #endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */ |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 296 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 297 | /* ----------------------------------------------------------------------------- |
| 298 | * Register/unregister |
| 299 | */ |
| 300 | |
| 301 | static struct sh_pfc_chip * |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 302 | sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *), |
| 303 | struct sh_pfc_window *mem) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 304 | { |
| 305 | struct sh_pfc_chip *chip; |
| 306 | int ret; |
| 307 | |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 308 | chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 309 | if (unlikely(!chip)) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 310 | return ERR_PTR(-ENOMEM); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 311 | |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 312 | chip->mem = mem; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 313 | chip->pfc = pfc; |
| 314 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 315 | ret = setup(chip); |
| 316 | if (ret < 0) |
| 317 | return ERR_PTR(ret); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 318 | |
Geert Uytterhoeven | c29e2f2 | 2016-06-10 11:22:44 +0200 | [diff] [blame] | 319 | ret = devm_gpiochip_add_data(pfc->dev, &chip->gpio_chip, chip); |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 320 | if (unlikely(ret < 0)) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 321 | return ERR_PTR(ret); |
| 322 | |
Laurent Pinchart | 9a643c9 | 2013-03-10 18:00:02 +0100 | [diff] [blame] | 323 | dev_info(pfc->dev, "%s handling gpio %u -> %u\n", |
| 324 | chip->gpio_chip.label, chip->gpio_chip.base, |
| 325 | chip->gpio_chip.base + chip->gpio_chip.ngpio - 1); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 326 | |
| 327 | return chip; |
| 328 | } |
| 329 | |
| 330 | int sh_pfc_register_gpiochip(struct sh_pfc *pfc) |
| 331 | { |
| 332 | struct sh_pfc_chip *chip; |
Geert Uytterhoeven | 1f34de0 | 2015-03-12 11:09:16 +0100 | [diff] [blame] | 333 | phys_addr_t address; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 334 | unsigned int i; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 335 | |
Laurent Pinchart | 1a4fd58 | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 336 | if (pfc->info->data_regs == NULL) |
| 337 | return 0; |
| 338 | |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 339 | /* Find the memory window that contain the GPIO registers. Boards that |
| 340 | * register a separate GPIO device will not supply a memory resource |
| 341 | * that covers the data registers. In that case don't try to handle |
| 342 | * GPIOs. |
| 343 | */ |
Geert Uytterhoeven | 1f34de0 | 2015-03-12 11:09:16 +0100 | [diff] [blame] | 344 | address = pfc->info->data_regs[0].reg; |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 345 | for (i = 0; i < pfc->num_windows; ++i) { |
Laurent Pinchart | 5b46ac3 | 2013-12-11 04:26:25 +0100 | [diff] [blame] | 346 | struct sh_pfc_window *window = &pfc->windows[i]; |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 347 | |
Geert Uytterhoeven | 1f34de0 | 2015-03-12 11:09:16 +0100 | [diff] [blame] | 348 | if (address >= window->phys && |
| 349 | address < window->phys + window->size) |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | |
| 353 | if (i == pfc->num_windows) |
| 354 | return 0; |
| 355 | |
Laurent Pinchart | 70c8f01 | 2013-12-11 04:26:26 +0100 | [diff] [blame] | 356 | /* If we have IRQ resources make sure their number is correct. */ |
Laurent Pinchart | 4adeabd | 2015-09-22 10:08:13 +0300 | [diff] [blame] | 357 | if (pfc->num_irqs != pfc->info->gpio_irq_size) { |
Laurent Pinchart | 70c8f01 | 2013-12-11 04:26:26 +0100 | [diff] [blame] | 358 | dev_err(pfc->dev, "invalid number of IRQ resources\n"); |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 362 | /* Register the real GPIOs chip. */ |
Laurent Pinchart | 5b46ac3 | 2013-12-11 04:26:25 +0100 | [diff] [blame] | 363 | chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 364 | if (IS_ERR(chip)) |
| 365 | return PTR_ERR(chip); |
Laurent Pinchart | 6f6a4a6 | 2012-12-15 23:50:46 +0100 | [diff] [blame] | 366 | |
| 367 | pfc->gpio = chip; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 368 | |
Geert Uytterhoeven | 18fab399 | 2015-08-04 15:55:17 +0200 | [diff] [blame] | 369 | if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node) |
| 370 | return 0; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 371 | |
Geert Uytterhoeven | 0ace959 | 2019-01-21 17:05:45 +0100 | [diff] [blame] | 372 | #ifdef CONFIG_PINCTRL_SH_FUNC_GPIO |
Geert Uytterhoeven | 90d0661 | 2015-08-27 22:07:23 +0200 | [diff] [blame] | 373 | /* |
| 374 | * Register the GPIO to pin mappings. As pins with GPIO ports |
| 375 | * must come first in the ranges, skip the pins without GPIO |
| 376 | * ports by stopping at the first range that contains such a |
| 377 | * pin. |
| 378 | */ |
| 379 | for (i = 0; i < pfc->nr_ranges; ++i) { |
| 380 | const struct sh_pfc_pin_range *range = &pfc->ranges[i]; |
| 381 | int ret; |
Laurent Pinchart | 4f82e3e | 2013-07-15 21:10:54 +0200 | [diff] [blame] | 382 | |
Geert Uytterhoeven | 90d0661 | 2015-08-27 22:07:23 +0200 | [diff] [blame] | 383 | if (range->start >= pfc->nr_gpio_pins) |
| 384 | break; |
Geert Uytterhoeven | 18fab399 | 2015-08-04 15:55:17 +0200 | [diff] [blame] | 385 | |
Geert Uytterhoeven | 90d0661 | 2015-08-27 22:07:23 +0200 | [diff] [blame] | 386 | ret = gpiochip_add_pin_range(&chip->gpio_chip, |
| 387 | dev_name(pfc->dev), range->start, range->start, |
| 388 | range->end - range->start + 1); |
| 389 | if (ret < 0) |
| 390 | return ret; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* Register the function GPIOs chip. */ |
Laurent Pinchart | 542a564 | 2013-03-07 14:31:57 +0100 | [diff] [blame] | 394 | if (pfc->info->nr_func_gpios == 0) |
| 395 | return 0; |
| 396 | |
Laurent Pinchart | ceef91d | 2013-03-10 03:19:44 +0100 | [diff] [blame] | 397 | chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 398 | if (IS_ERR(chip)) |
| 399 | return PTR_ERR(chip); |
Geert Uytterhoeven | 0ace959 | 2019-01-21 17:05:45 +0100 | [diff] [blame] | 400 | #endif /* CONFIG_PINCTRL_SH_FUNC_GPIO */ |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 401 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 402 | return 0; |
| 403 | } |