Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Pin Function Controller GPIO driver. |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * Copyright (C) 2009 - 2012 Paul Mundt |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
Laurent Pinchart | c6193ea | 2012-12-15 23:50:47 +0100 | [diff] [blame] | 11 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 13 | |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 14 | #include <linux/device.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 15 | #include <linux/gpio.h> |
Laurent Pinchart | 90efde2 | 2012-12-15 23:50:52 +0100 | [diff] [blame] | 16 | #include <linux/init.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 17 | #include <linux/module.h> |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 18 | #include <linux/pinctrl/consumer.h> |
Laurent Pinchart | 90efde2 | 2012-12-15 23:50:52 +0100 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <linux/spinlock.h> |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 21 | |
Laurent Pinchart | f916513 | 2012-12-15 23:50:44 +0100 | [diff] [blame] | 22 | #include "core.h" |
| 23 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 24 | struct sh_pfc_gpio_data_reg { |
| 25 | const struct pinmux_data_reg *info; |
| 26 | unsigned long shadow; |
| 27 | }; |
| 28 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 29 | struct sh_pfc_chip { |
| 30 | struct sh_pfc *pfc; |
| 31 | struct gpio_chip gpio_chip; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 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; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc) |
| 38 | { |
| 39 | return container_of(gc, struct sh_pfc_chip, gpio_chip); |
| 40 | } |
| 41 | |
| 42 | static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc) |
| 43 | { |
| 44 | return gpio_to_pfc_chip(gc)->pfc; |
| 45 | } |
| 46 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 47 | static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int gpio, |
| 48 | struct sh_pfc_gpio_data_reg **reg, |
| 49 | unsigned int *bit) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 50 | { |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 51 | struct sh_pfc_pin *gpiop = sh_pfc_get_pin(chip->pfc, gpio); |
| 52 | unsigned int reg_idx; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 53 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 54 | reg_idx = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT; |
| 55 | |
| 56 | *reg = &chip->regs[reg_idx]; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 57 | *bit = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT; |
| 58 | } |
| 59 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 60 | static unsigned long gpio_read_data_reg(struct sh_pfc_chip *chip, |
| 61 | const struct pinmux_data_reg *dreg) |
| 62 | { |
| 63 | void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt; |
| 64 | |
| 65 | return sh_pfc_read_raw_reg(mem, dreg->reg_width); |
| 66 | } |
| 67 | |
| 68 | static void gpio_write_data_reg(struct sh_pfc_chip *chip, |
| 69 | const struct pinmux_data_reg *dreg, |
| 70 | unsigned long value) |
| 71 | { |
| 72 | void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt; |
| 73 | |
| 74 | sh_pfc_write_raw_reg(mem, dreg->reg_width, value); |
| 75 | } |
| 76 | |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 77 | static void gpio_setup_data_reg(struct sh_pfc *pfc, unsigned gpio) |
| 78 | { |
| 79 | struct sh_pfc_pin *gpiop = &pfc->info->pins[gpio]; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 80 | const struct pinmux_data_reg *dreg; |
| 81 | unsigned int bit; |
| 82 | unsigned int i; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 83 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 84 | for (i = 0, dreg = pfc->info->data_regs; dreg->reg; ++i, ++dreg) { |
| 85 | for (bit = 0; bit < dreg->reg_width; bit++) { |
| 86 | if (dreg->enum_ids[bit] == gpiop->enum_id) { |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 87 | gpiop->flags &= ~PINMUX_FLAG_DREG; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 88 | gpiop->flags |= i << PINMUX_FLAG_DREG_SHIFT; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 89 | gpiop->flags &= ~PINMUX_FLAG_DBIT; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 90 | gpiop->flags |= bit << PINMUX_FLAG_DBIT_SHIFT; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | } |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | BUG(); |
| 97 | } |
| 98 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 99 | static int gpio_setup_data_regs(struct sh_pfc_chip *chip) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 100 | { |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 101 | struct sh_pfc *pfc = chip->pfc; |
| 102 | unsigned long addr = pfc->info->data_regs[0].reg; |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 103 | const struct pinmux_data_reg *dreg; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 104 | unsigned int i; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 105 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 106 | /* Find the window that contain the GPIO registers. */ |
| 107 | for (i = 0; i < pfc->num_windows; ++i) { |
| 108 | struct sh_pfc_window *window = &pfc->window[i]; |
| 109 | |
| 110 | if (addr >= window->phys && addr < window->phys + window->size) |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | if (i == pfc->num_windows) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | /* GPIO data registers must be in the first memory resource. */ |
| 118 | chip->mem = &pfc->window[i]; |
| 119 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 120 | /* Count the number of data registers, allocate memory and initialize |
| 121 | * them. |
| 122 | */ |
| 123 | for (i = 0; pfc->info->data_regs[i].reg_width; ++i) |
| 124 | ; |
| 125 | |
| 126 | chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs), |
| 127 | GFP_KERNEL); |
| 128 | if (chip->regs == NULL) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) { |
| 132 | chip->regs[i].info = dreg; |
| 133 | chip->regs[i].shadow = gpio_read_data_reg(chip, dreg); |
| 134 | } |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 135 | |
| 136 | for (i = 0; i < pfc->info->nr_pins; i++) { |
| 137 | if (pfc->info->pins[i].enum_id == 0) |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 138 | continue; |
| 139 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 140 | gpio_setup_data_reg(pfc, i); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 143 | return 0; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 146 | /* ----------------------------------------------------------------------------- |
| 147 | * Pin GPIOs |
| 148 | */ |
| 149 | |
| 150 | static int gpio_pin_request(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 151 | { |
Laurent Pinchart | 0b73ee5 | 2013-02-14 22:12:11 +0100 | [diff] [blame] | 152 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | 934cb02 | 2013-02-14 22:35:09 +0100 | [diff] [blame] | 153 | struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset); |
Laurent Pinchart | 0b73ee5 | 2013-02-14 22:12:11 +0100 | [diff] [blame] | 154 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 155 | if (pin == NULL || pin->enum_id == 0) |
Laurent Pinchart | 0b73ee5 | 2013-02-14 22:12:11 +0100 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 158 | return pinctrl_request_gpio(offset); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 159 | } |
| 160 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 161 | static void gpio_pin_free(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 162 | { |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 163 | return pinctrl_free_gpio(offset); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 164 | } |
| 165 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 166 | static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset, |
| 167 | int value) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 168 | { |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 169 | struct sh_pfc_gpio_data_reg *reg; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 170 | unsigned long pos; |
| 171 | unsigned int bit; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 172 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 173 | gpio_get_data_reg(chip, offset, ®, &bit); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 174 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 175 | pos = reg->info->reg_width - (bit + 1); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 176 | |
| 177 | if (value) |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 178 | set_bit(pos, ®->shadow); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 179 | else |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 180 | clear_bit(pos, ®->shadow); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 181 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 182 | gpio_write_data_reg(chip, reg->info, reg->shadow); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 183 | } |
| 184 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 185 | static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 186 | { |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 187 | return pinctrl_gpio_direction_input(offset); |
| 188 | } |
| 189 | |
| 190 | static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset, |
| 191 | int value) |
| 192 | { |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 193 | gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 194 | |
| 195 | return pinctrl_gpio_direction_output(offset); |
| 196 | } |
| 197 | |
| 198 | static int gpio_pin_get(struct gpio_chip *gc, unsigned offset) |
| 199 | { |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 200 | struct sh_pfc_chip *chip = gpio_to_pfc_chip(gc); |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 201 | struct sh_pfc_gpio_data_reg *reg; |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 202 | unsigned long pos; |
| 203 | unsigned int bit; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 204 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 205 | gpio_get_data_reg(chip, offset, ®, &bit); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 206 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 207 | pos = reg->info->reg_width - (bit + 1); |
Laurent Pinchart | 41f1219 | 2013-02-15 02:04:55 +0100 | [diff] [blame] | 208 | |
Laurent Pinchart | 51cb226 | 2013-02-16 18:34:32 +0100 | [diff] [blame^] | 209 | return (gpio_read_data_reg(chip, reg->info) >> pos) & 1; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 210 | } |
| 211 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 212 | 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] | 213 | { |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 214 | gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value); |
Paul Mundt | ca5481c6 | 2012-07-10 12:08:14 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 217 | static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 218 | { |
| 219 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | c07f54f | 2013-01-03 14:12:14 +0100 | [diff] [blame] | 220 | int i, k; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 221 | |
Laurent Pinchart | c07f54f | 2013-01-03 14:12:14 +0100 | [diff] [blame] | 222 | for (i = 0; i < pfc->info->gpio_irq_size; i++) { |
| 223 | unsigned short *gpios = pfc->info->gpio_irq[i].gpios; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 224 | |
Laurent Pinchart | c07f54f | 2013-01-03 14:12:14 +0100 | [diff] [blame] | 225 | for (k = 0; gpios[k]; k++) { |
| 226 | if (gpios[k] == offset) |
| 227 | return pfc->info->gpio_irq[i].irq; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return -ENOSYS; |
| 232 | } |
| 233 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 234 | static int gpio_pin_setup(struct sh_pfc_chip *chip) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 235 | { |
| 236 | struct sh_pfc *pfc = chip->pfc; |
| 237 | struct gpio_chip *gc = &chip->gpio_chip; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 238 | int ret; |
| 239 | |
| 240 | ret = gpio_setup_data_regs(chip); |
| 241 | if (ret < 0) |
| 242 | return ret; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 243 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 244 | gc->request = gpio_pin_request; |
| 245 | gc->free = gpio_pin_free; |
| 246 | gc->direction_input = gpio_pin_direction_input; |
| 247 | gc->get = gpio_pin_get; |
| 248 | gc->direction_output = gpio_pin_direction_output; |
| 249 | gc->set = gpio_pin_set; |
| 250 | gc->to_irq = gpio_pin_to_irq; |
| 251 | |
| 252 | gc->label = pfc->info->name; |
| 253 | gc->dev = pfc->dev; |
| 254 | gc->owner = THIS_MODULE; |
| 255 | gc->base = 0; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 256 | gc->ngpio = pfc->nr_pins; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 257 | |
| 258 | return 0; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* ----------------------------------------------------------------------------- |
| 262 | * Function GPIOs |
| 263 | */ |
| 264 | |
| 265 | static int gpio_function_request(struct gpio_chip *gc, unsigned offset) |
| 266 | { |
| 267 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 268 | unsigned int mark = pfc->info->func_gpios[offset].enum_id; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 269 | unsigned long flags; |
| 270 | int ret = -EINVAL; |
| 271 | |
| 272 | pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n"); |
| 273 | |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 274 | if (mark == 0) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 275 | return ret; |
| 276 | |
| 277 | spin_lock_irqsave(&pfc->lock, flags); |
| 278 | |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 279 | if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN)) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 280 | goto done; |
| 281 | |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 282 | if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ)) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 283 | goto done; |
| 284 | |
| 285 | ret = 0; |
| 286 | |
| 287 | done: |
| 288 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static void gpio_function_free(struct gpio_chip *gc, unsigned offset) |
| 293 | { |
| 294 | struct sh_pfc *pfc = gpio_to_pfc(gc); |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 295 | unsigned int mark = pfc->info->func_gpios[offset].enum_id; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 296 | unsigned long flags; |
| 297 | |
| 298 | spin_lock_irqsave(&pfc->lock, flags); |
| 299 | |
Laurent Pinchart | a68fdca9 | 2013-02-14 17:36:56 +0100 | [diff] [blame] | 300 | sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE); |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 301 | |
| 302 | spin_unlock_irqrestore(&pfc->lock, flags); |
| 303 | } |
| 304 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 305 | static int gpio_function_setup(struct sh_pfc_chip *chip) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 306 | { |
| 307 | struct sh_pfc *pfc = chip->pfc; |
| 308 | struct gpio_chip *gc = &chip->gpio_chip; |
| 309 | |
| 310 | gc->request = gpio_function_request; |
| 311 | gc->free = gpio_function_free; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 312 | |
Laurent Pinchart | 19bb7fe3 | 2012-12-15 23:51:20 +0100 | [diff] [blame] | 313 | gc->label = pfc->info->name; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 314 | gc->owner = THIS_MODULE; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 315 | gc->base = pfc->nr_pins; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 316 | gc->ngpio = pfc->info->nr_func_gpios; |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 317 | |
| 318 | return 0; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 319 | } |
| 320 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 321 | /* ----------------------------------------------------------------------------- |
| 322 | * Register/unregister |
| 323 | */ |
| 324 | |
| 325 | static struct sh_pfc_chip * |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 326 | sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *)) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 327 | { |
| 328 | struct sh_pfc_chip *chip; |
| 329 | int ret; |
| 330 | |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 331 | chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 332 | if (unlikely(!chip)) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 333 | return ERR_PTR(-ENOMEM); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 334 | |
| 335 | chip->pfc = pfc; |
| 336 | |
Laurent Pinchart | e51d534 | 2013-02-17 00:26:33 +0100 | [diff] [blame] | 337 | ret = setup(chip); |
| 338 | if (ret < 0) |
| 339 | return ERR_PTR(ret); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 340 | |
| 341 | ret = gpiochip_add(&chip->gpio_chip); |
Laurent Pinchart | 1724acf | 2012-12-15 23:50:48 +0100 | [diff] [blame] | 342 | if (unlikely(ret < 0)) |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 343 | return ERR_PTR(ret); |
| 344 | |
| 345 | pr_info("%s handling gpio %u -> %u\n", |
| 346 | chip->gpio_chip.label, chip->gpio_chip.base, |
| 347 | chip->gpio_chip.base + chip->gpio_chip.ngpio - 1); |
| 348 | |
| 349 | return chip; |
| 350 | } |
| 351 | |
| 352 | int sh_pfc_register_gpiochip(struct sh_pfc *pfc) |
| 353 | { |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 354 | const struct pinmux_range *ranges; |
| 355 | struct pinmux_range def_range; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 356 | struct sh_pfc_chip *chip; |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 357 | unsigned int nr_ranges; |
| 358 | unsigned int i; |
Laurent Pinchart | 247127f | 2013-03-08 00:45:12 +0100 | [diff] [blame] | 359 | int ret; |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 360 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 361 | /* Register the real GPIOs chip. */ |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 362 | chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup); |
| 363 | if (IS_ERR(chip)) |
| 364 | return PTR_ERR(chip); |
Laurent Pinchart | 6f6a4a6 | 2012-12-15 23:50:46 +0100 | [diff] [blame] | 365 | |
| 366 | pfc->gpio = chip; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 367 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 368 | /* Register the GPIO to pin mappings. */ |
| 369 | if (pfc->info->ranges == NULL) { |
| 370 | def_range.begin = 0; |
| 371 | def_range.end = pfc->info->nr_pins - 1; |
| 372 | ranges = &def_range; |
| 373 | nr_ranges = 1; |
| 374 | } else { |
| 375 | ranges = pfc->info->ranges; |
| 376 | nr_ranges = pfc->info->nr_ranges; |
| 377 | } |
Laurent Pinchart | 247127f | 2013-03-08 00:45:12 +0100 | [diff] [blame] | 378 | |
Laurent Pinchart | 63d5738 | 2013-02-15 01:33:38 +0100 | [diff] [blame] | 379 | for (i = 0; i < nr_ranges; ++i) { |
| 380 | const struct pinmux_range *range = &ranges[i]; |
| 381 | |
| 382 | ret = gpiochip_add_pin_range(&chip->gpio_chip, |
| 383 | dev_name(pfc->dev), |
| 384 | range->begin, range->begin, |
| 385 | range->end - range->begin + 1); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* Register the function GPIOs chip. */ |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 391 | chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup); |
| 392 | if (IS_ERR(chip)) |
| 393 | return PTR_ERR(chip); |
| 394 | |
| 395 | pfc->func = chip; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 396 | |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Laurent Pinchart | 6f6a4a6 | 2012-12-15 23:50:46 +0100 | [diff] [blame] | 400 | int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc) |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 401 | { |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 402 | int err; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 403 | int ret; |
| 404 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 405 | ret = gpiochip_remove(&pfc->gpio->gpio_chip); |
| 406 | err = gpiochip_remove(&pfc->func->gpio_chip); |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 407 | |
Laurent Pinchart | 1688381 | 2012-12-06 14:49:25 +0100 | [diff] [blame] | 408 | return ret < 0 ? ret : err; |
Paul Mundt | b3c185a | 2012-06-20 17:29:04 +0900 | [diff] [blame] | 409 | } |