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