Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2013 |
| 3 | * |
| 4 | * Author: Patrice Chotard <patrice.chotard@st.com> |
| 5 | * License terms: GNU General Public License (GPL) version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/err.h> |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/gpio.h> |
| 21 | #include <linux/irq.h> |
Lee Jones | ac652d7 | 2013-01-31 10:43:00 +0000 | [diff] [blame] | 22 | #include <linux/irqdomain.h> |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/bitops.h> |
| 25 | #include <linux/mfd/abx500.h> |
| 26 | #include <linux/mfd/abx500/ab8500.h> |
| 27 | #include <linux/mfd/abx500/ab8500-gpio.h> |
| 28 | #include <linux/pinctrl/pinctrl.h> |
| 29 | #include <linux/pinctrl/consumer.h> |
| 30 | #include <linux/pinctrl/pinmux.h> |
| 31 | #include <linux/pinctrl/pinconf.h> |
| 32 | #include <linux/pinctrl/pinconf-generic.h> |
Patrice Chotard | 64a45c9 | 2013-06-20 16:04:59 +0200 | [diff] [blame] | 33 | #include <linux/pinctrl/machine.h> |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 34 | |
| 35 | #include "pinctrl-abx500.h" |
Patrice Chotard | 61ce135 | 2013-06-20 16:05:00 +0200 | [diff] [blame] | 36 | #include "core.h" |
Patrice Chotard | 64a45c9 | 2013-06-20 16:04:59 +0200 | [diff] [blame] | 37 | #include "pinconf.h" |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * The AB9540 and AB8540 GPIO support are extended versions |
| 41 | * of the AB8500 GPIO support. |
| 42 | * The AB9540 supports an additional (7th) register so that |
| 43 | * more GPIO may be configured and used. |
| 44 | * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have |
| 45 | * internal pull-up and pull-down capabilities. |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * GPIO registers offset |
| 50 | * Bank: 0x10 |
| 51 | */ |
| 52 | #define AB8500_GPIO_SEL1_REG 0x00 |
| 53 | #define AB8500_GPIO_SEL2_REG 0x01 |
| 54 | #define AB8500_GPIO_SEL3_REG 0x02 |
| 55 | #define AB8500_GPIO_SEL4_REG 0x03 |
| 56 | #define AB8500_GPIO_SEL5_REG 0x04 |
| 57 | #define AB8500_GPIO_SEL6_REG 0x05 |
| 58 | #define AB9540_GPIO_SEL7_REG 0x06 |
| 59 | |
| 60 | #define AB8500_GPIO_DIR1_REG 0x10 |
| 61 | #define AB8500_GPIO_DIR2_REG 0x11 |
| 62 | #define AB8500_GPIO_DIR3_REG 0x12 |
| 63 | #define AB8500_GPIO_DIR4_REG 0x13 |
| 64 | #define AB8500_GPIO_DIR5_REG 0x14 |
| 65 | #define AB8500_GPIO_DIR6_REG 0x15 |
| 66 | #define AB9540_GPIO_DIR7_REG 0x16 |
| 67 | |
| 68 | #define AB8500_GPIO_OUT1_REG 0x20 |
| 69 | #define AB8500_GPIO_OUT2_REG 0x21 |
| 70 | #define AB8500_GPIO_OUT3_REG 0x22 |
| 71 | #define AB8500_GPIO_OUT4_REG 0x23 |
| 72 | #define AB8500_GPIO_OUT5_REG 0x24 |
| 73 | #define AB8500_GPIO_OUT6_REG 0x25 |
| 74 | #define AB9540_GPIO_OUT7_REG 0x26 |
| 75 | |
| 76 | #define AB8500_GPIO_PUD1_REG 0x30 |
| 77 | #define AB8500_GPIO_PUD2_REG 0x31 |
| 78 | #define AB8500_GPIO_PUD3_REG 0x32 |
| 79 | #define AB8500_GPIO_PUD4_REG 0x33 |
| 80 | #define AB8500_GPIO_PUD5_REG 0x34 |
| 81 | #define AB8500_GPIO_PUD6_REG 0x35 |
| 82 | #define AB9540_GPIO_PUD7_REG 0x36 |
| 83 | |
| 84 | #define AB8500_GPIO_IN1_REG 0x40 |
| 85 | #define AB8500_GPIO_IN2_REG 0x41 |
| 86 | #define AB8500_GPIO_IN3_REG 0x42 |
| 87 | #define AB8500_GPIO_IN4_REG 0x43 |
| 88 | #define AB8500_GPIO_IN5_REG 0x44 |
| 89 | #define AB8500_GPIO_IN6_REG 0x45 |
| 90 | #define AB9540_GPIO_IN7_REG 0x46 |
| 91 | #define AB8540_GPIO_VINSEL_REG 0x47 |
| 92 | #define AB8540_GPIO_PULL_UPDOWN_REG 0x48 |
| 93 | #define AB8500_GPIO_ALTFUN_REG 0x50 |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 94 | #define AB8540_GPIO_PULL_UPDOWN_MASK 0x03 |
| 95 | #define AB8540_GPIO_VINSEL_MASK 0x03 |
| 96 | #define AB8540_GPIOX_VBAT_START 51 |
| 97 | #define AB8540_GPIOX_VBAT_END 54 |
| 98 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 99 | struct abx500_pinctrl { |
| 100 | struct device *dev; |
| 101 | struct pinctrl_dev *pctldev; |
| 102 | struct abx500_pinctrl_soc_data *soc; |
| 103 | struct gpio_chip chip; |
| 104 | struct ab8500 *parent; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 105 | struct abx500_gpio_irq_cluster *irq_cluster; |
| 106 | int irq_cluster_size; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * to_abx500_pinctrl() - get the pointer to abx500_pinctrl |
| 111 | * @chip: Member of the structure abx500_pinctrl |
| 112 | */ |
| 113 | static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip) |
| 114 | { |
| 115 | return container_of(chip, struct abx500_pinctrl, chip); |
| 116 | } |
| 117 | |
| 118 | static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 119 | unsigned offset, bool *bit) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 120 | { |
| 121 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
| 122 | u8 pos = offset % 8; |
| 123 | u8 val; |
| 124 | int ret; |
| 125 | |
| 126 | reg += offset / 8; |
| 127 | ret = abx500_get_register_interruptible(pct->dev, |
| 128 | AB8500_MISC, reg, &val); |
| 129 | |
| 130 | *bit = !!(val & BIT(pos)); |
| 131 | |
| 132 | if (ret < 0) |
| 133 | dev_err(pct->dev, |
| 134 | "%s read reg =%x, offset=%x failed\n", |
| 135 | __func__, reg, offset); |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 141 | unsigned offset, int val) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 142 | { |
| 143 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
| 144 | u8 pos = offset % 8; |
| 145 | int ret; |
| 146 | |
| 147 | reg += offset / 8; |
| 148 | ret = abx500_mask_and_set_register_interruptible(pct->dev, |
Lee Jones | 49dcf08 | 2013-01-23 13:26:02 +0000 | [diff] [blame] | 149 | AB8500_MISC, reg, BIT(pos), val << pos); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 150 | if (ret < 0) |
| 151 | dev_err(pct->dev, "%s write failed\n", __func__); |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 152 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 153 | return ret; |
| 154 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 155 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 156 | /** |
| 157 | * abx500_gpio_get() - Get the particular GPIO value |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 158 | * @chip: Gpio device |
| 159 | * @offset: GPIO number to read |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 160 | */ |
| 161 | static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 162 | { |
| 163 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
| 164 | bool bit; |
Patrice Chotard | d8d4f7f | 2013-06-20 16:05:43 +0200 | [diff] [blame^] | 165 | bool is_out; |
| 166 | u8 gpio_offset = offset - 1; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 167 | int ret; |
| 168 | |
Patrice Chotard | d8d4f7f | 2013-06-20 16:05:43 +0200 | [diff] [blame^] | 169 | ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, |
| 170 | gpio_offset, &is_out); |
| 171 | if (ret < 0) { |
| 172 | dev_err(pct->dev, "%s failed\n", __func__); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | if (is_out) |
| 177 | ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG, |
| 178 | gpio_offset, &bit); |
| 179 | else |
| 180 | ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG, |
| 181 | gpio_offset, &bit); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 182 | if (ret < 0) { |
| 183 | dev_err(pct->dev, "%s failed\n", __func__); |
| 184 | return ret; |
| 185 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 186 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 187 | return bit; |
| 188 | } |
| 189 | |
| 190 | static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val) |
| 191 | { |
| 192 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
| 193 | int ret; |
| 194 | |
| 195 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val); |
| 196 | if (ret < 0) |
| 197 | dev_err(pct->dev, "%s write failed\n", __func__); |
| 198 | } |
| 199 | |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 200 | static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset, |
| 201 | enum abx500_gpio_pull_updown *pull_updown) |
| 202 | { |
| 203 | u8 pos; |
| 204 | u8 val; |
| 205 | int ret; |
| 206 | struct pullud *pullud; |
| 207 | |
| 208 | if (!pct->soc->pullud) { |
| 209 | dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature", |
| 210 | __func__); |
| 211 | ret = -EPERM; |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | pullud = pct->soc->pullud; |
| 216 | |
| 217 | if ((offset < pullud->first_pin) |
| 218 | || (offset > pullud->last_pin)) { |
| 219 | ret = -EINVAL; |
| 220 | goto out; |
| 221 | } |
| 222 | |
| 223 | ret = abx500_get_register_interruptible(pct->dev, |
| 224 | AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val); |
| 225 | |
| 226 | pos = (offset - pullud->first_pin) << 1; |
| 227 | *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK; |
| 228 | |
| 229 | out: |
| 230 | if (ret < 0) |
| 231 | dev_err(pct->dev, "%s failed (%d)\n", __func__, ret); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static int abx500_set_pull_updown(struct abx500_pinctrl *pct, |
| 237 | int offset, enum abx500_gpio_pull_updown val) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 238 | { |
| 239 | u8 pos; |
| 240 | int ret; |
| 241 | struct pullud *pullud; |
| 242 | |
| 243 | if (!pct->soc->pullud) { |
| 244 | dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature", |
| 245 | __func__); |
| 246 | ret = -EPERM; |
| 247 | goto out; |
| 248 | } |
| 249 | |
| 250 | pullud = pct->soc->pullud; |
| 251 | |
| 252 | if ((offset < pullud->first_pin) |
| 253 | || (offset > pullud->last_pin)) { |
| 254 | ret = -EINVAL; |
| 255 | goto out; |
| 256 | } |
Patrice Chotard | 10a8be5 | 2013-05-24 14:06:29 +0200 | [diff] [blame] | 257 | pos = (offset - pullud->first_pin) << 1; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 258 | |
| 259 | ret = abx500_mask_and_set_register_interruptible(pct->dev, |
| 260 | AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, |
| 261 | AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos); |
| 262 | |
| 263 | out: |
| 264 | if (ret < 0) |
| 265 | dev_err(pct->dev, "%s failed (%d)\n", __func__, ret); |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 266 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int abx500_gpio_direction_output(struct gpio_chip *chip, |
| 271 | unsigned offset, |
| 272 | int val) |
| 273 | { |
| 274 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
| 275 | struct pullud *pullud = pct->soc->pullud; |
| 276 | unsigned gpio; |
| 277 | int ret; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 278 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 279 | /* set direction as output */ |
| 280 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1); |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | |
| 284 | /* disable pull down */ |
| 285 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1); |
| 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
| 289 | /* if supported, disable both pull down and pull up */ |
| 290 | gpio = offset + 1; |
| 291 | if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) { |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 292 | ret = abx500_set_pull_updown(pct, |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 293 | gpio, |
| 294 | ABX500_GPIO_PULL_NONE); |
| 295 | if (ret < 0) |
| 296 | return ret; |
| 297 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 298 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 299 | /* set the output as 1 or 0 */ |
| 300 | return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val); |
| 301 | } |
| 302 | |
| 303 | static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 304 | { |
| 305 | /* set the register as input */ |
| 306 | return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0); |
| 307 | } |
| 308 | |
| 309 | static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 310 | { |
| 311 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
Lee Jones | b9fab6e | 2013-01-31 09:45:17 +0000 | [diff] [blame] | 312 | /* The AB8500 GPIO numbers are off by one */ |
| 313 | int gpio = offset + 1; |
Lee Jones | a6a16d2 | 2013-01-31 09:57:52 +0000 | [diff] [blame] | 314 | int hwirq; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 315 | int i; |
| 316 | |
| 317 | for (i = 0; i < pct->irq_cluster_size; i++) { |
| 318 | struct abx500_gpio_irq_cluster *cluster = |
| 319 | &pct->irq_cluster[i]; |
| 320 | |
Lee Jones | a6a16d2 | 2013-01-31 09:57:52 +0000 | [diff] [blame] | 321 | if (gpio >= cluster->start && gpio <= cluster->end) { |
| 322 | /* |
| 323 | * The ABx500 GPIO's associated IRQs are clustered together |
| 324 | * throughout the interrupt numbers at irregular intervals. |
| 325 | * To solve this quandry, we have placed the read-in values |
| 326 | * into the cluster information table. |
| 327 | */ |
Linus Walleij | 43a255d | 2013-02-04 15:21:41 +0100 | [diff] [blame] | 328 | hwirq = gpio - cluster->start + cluster->to_irq; |
Lee Jones | a6a16d2 | 2013-01-31 09:57:52 +0000 | [diff] [blame] | 329 | return irq_create_mapping(pct->parent->domain, hwirq); |
| 330 | } |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 337 | unsigned gpio, int alt_setting) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 338 | { |
| 339 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 340 | struct alternate_functions af = pct->soc->alternate_functions[gpio]; |
| 341 | int ret; |
| 342 | int val; |
| 343 | unsigned offset; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 344 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 345 | const char *modes[] = { |
| 346 | [ABX500_DEFAULT] = "default", |
| 347 | [ABX500_ALT_A] = "altA", |
| 348 | [ABX500_ALT_B] = "altB", |
| 349 | [ABX500_ALT_C] = "altC", |
| 350 | }; |
| 351 | |
| 352 | /* sanity check */ |
| 353 | if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) || |
| 354 | ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) || |
| 355 | ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) { |
| 356 | dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio, |
| 357 | modes[alt_setting]); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | /* on ABx5xx, there is no GPIO0, so adjust the offset */ |
| 362 | offset = gpio - 1; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 363 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 364 | switch (alt_setting) { |
| 365 | case ABX500_DEFAULT: |
| 366 | /* |
| 367 | * for ABx5xx family, default mode is always selected by |
| 368 | * writing 0 to GPIOSELx register, except for pins which |
| 369 | * support at least ALT_B mode, default mode is selected |
| 370 | * by writing 1 to GPIOSELx register |
| 371 | */ |
| 372 | val = 0; |
| 373 | if (af.alt_bit1 != UNUSED) |
| 374 | val++; |
| 375 | |
| 376 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG, |
| 377 | offset, val); |
| 378 | break; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 379 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 380 | case ABX500_ALT_A: |
| 381 | /* |
| 382 | * for ABx5xx family, alt_a mode is always selected by |
| 383 | * writing 1 to GPIOSELx register, except for pins which |
| 384 | * support at least ALT_B mode, alt_a mode is selected |
| 385 | * by writing 0 to GPIOSELx register and 0 in ALTFUNC |
| 386 | * register |
| 387 | */ |
| 388 | if (af.alt_bit1 != UNUSED) { |
| 389 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG, |
| 390 | offset, 0); |
| 391 | ret = abx500_gpio_set_bits(chip, |
| 392 | AB8500_GPIO_ALTFUN_REG, |
| 393 | af.alt_bit1, |
| 394 | !!(af.alta_val && BIT(0))); |
| 395 | if (af.alt_bit2 != UNUSED) |
| 396 | ret = abx500_gpio_set_bits(chip, |
| 397 | AB8500_GPIO_ALTFUN_REG, |
| 398 | af.alt_bit2, |
| 399 | !!(af.alta_val && BIT(1))); |
| 400 | } else |
| 401 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG, |
| 402 | offset, 1); |
| 403 | break; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 404 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 405 | case ABX500_ALT_B: |
| 406 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG, |
| 407 | offset, 0); |
| 408 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG, |
| 409 | af.alt_bit1, !!(af.altb_val && BIT(0))); |
| 410 | if (af.alt_bit2 != UNUSED) |
| 411 | ret = abx500_gpio_set_bits(chip, |
| 412 | AB8500_GPIO_ALTFUN_REG, |
| 413 | af.alt_bit2, |
| 414 | !!(af.altb_val && BIT(1))); |
| 415 | break; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 416 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 417 | case ABX500_ALT_C: |
| 418 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG, |
| 419 | offset, 0); |
| 420 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG, |
| 421 | af.alt_bit2, !!(af.altc_val && BIT(0))); |
| 422 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG, |
| 423 | af.alt_bit2, !!(af.altc_val && BIT(1))); |
| 424 | break; |
| 425 | |
| 426 | default: |
| 427 | dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting); |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 428 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 429 | return -EINVAL; |
| 430 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 431 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 436 | unsigned gpio) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 437 | { |
| 438 | u8 mode; |
| 439 | bool bit_mode; |
| 440 | bool alt_bit1; |
| 441 | bool alt_bit2; |
| 442 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 443 | struct alternate_functions af = pct->soc->alternate_functions[gpio]; |
Linus Walleij | a950cb7 | 2013-02-05 20:10:57 +0100 | [diff] [blame] | 444 | /* on ABx5xx, there is no GPIO0, so adjust the offset */ |
| 445 | unsigned offset = gpio - 1; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * if gpiosel_bit is set to unused, |
| 449 | * it means no GPIO or special case |
| 450 | */ |
| 451 | if (af.gpiosel_bit == UNUSED) |
| 452 | return ABX500_DEFAULT; |
| 453 | |
| 454 | /* read GpioSelx register */ |
Linus Walleij | a950cb7 | 2013-02-05 20:10:57 +0100 | [diff] [blame] | 455 | abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8), |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 456 | af.gpiosel_bit, &bit_mode); |
| 457 | mode = bit_mode; |
| 458 | |
| 459 | /* sanity check */ |
| 460 | if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) || |
| 461 | (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) { |
| 462 | dev_err(pct->dev, |
| 463 | "alt_bitX value not in correct range (-1 to 7)\n"); |
| 464 | return -EINVAL; |
| 465 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 466 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 467 | /* if alt_bit2 is used, alt_bit1 must be used too */ |
| 468 | if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) { |
| 469 | dev_err(pct->dev, |
| 470 | "if alt_bit2 is used, alt_bit1 can't be unused\n"); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | /* check if pin use AlternateFunction register */ |
Axel Lin | 6a40cdd | 2013-03-05 14:58:53 +0800 | [diff] [blame] | 475 | if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED)) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 476 | return mode; |
| 477 | /* |
| 478 | * if pin GPIOSEL bit is set and pin supports alternate function, |
| 479 | * it means DEFAULT mode |
| 480 | */ |
| 481 | if (mode) |
| 482 | return ABX500_DEFAULT; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 483 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 484 | /* |
| 485 | * pin use the AlternatFunction register |
| 486 | * read alt_bit1 value |
| 487 | */ |
| 488 | abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, |
| 489 | af.alt_bit1, &alt_bit1); |
| 490 | |
| 491 | if (af.alt_bit2 != UNUSED) |
| 492 | /* read alt_bit2 value */ |
| 493 | abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2, |
| 494 | &alt_bit2); |
| 495 | else |
| 496 | alt_bit2 = 0; |
| 497 | |
| 498 | mode = (alt_bit2 << 1) + alt_bit1; |
| 499 | if (mode == af.alta_val) |
| 500 | return ABX500_ALT_A; |
| 501 | else if (mode == af.altb_val) |
| 502 | return ABX500_ALT_B; |
| 503 | else |
| 504 | return ABX500_ALT_C; |
| 505 | } |
| 506 | |
| 507 | #ifdef CONFIG_DEBUG_FS |
| 508 | |
| 509 | #include <linux/seq_file.h> |
| 510 | |
| 511 | static void abx500_gpio_dbg_show_one(struct seq_file *s, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 512 | struct pinctrl_dev *pctldev, |
| 513 | struct gpio_chip *chip, |
| 514 | unsigned offset, unsigned gpio) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 515 | { |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 516 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 517 | struct pullud *pullud = pct->soc->pullud; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 518 | const char *label = gpiochip_is_requested(chip, offset - 1); |
| 519 | u8 gpio_offset = offset - 1; |
| 520 | int mode = -1; |
| 521 | bool is_out; |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 522 | bool pd; |
Patrice Chotard | ce06f40 | 2013-06-11 10:48:21 +0200 | [diff] [blame] | 523 | enum abx500_gpio_pull_updown pud = 0; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 524 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 525 | const char *modes[] = { |
| 526 | [ABX500_DEFAULT] = "default", |
| 527 | [ABX500_ALT_A] = "altA", |
| 528 | [ABX500_ALT_B] = "altB", |
| 529 | [ABX500_ALT_C] = "altC", |
| 530 | }; |
| 531 | |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 532 | const char *pull_up_down[] = { |
| 533 | [ABX500_GPIO_PULL_DOWN] = "pull down", |
| 534 | [ABX500_GPIO_PULL_NONE] = "pull none", |
| 535 | [ABX500_GPIO_PULL_NONE + 1] = "pull none", |
| 536 | [ABX500_GPIO_PULL_UP] = "pull up", |
| 537 | }; |
| 538 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 539 | abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out); |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 540 | |
| 541 | seq_printf(s, " gpio-%-3d (%-20.20s) %-3s", |
| 542 | gpio, label ?: "(none)", |
| 543 | is_out ? "out" : "in "); |
| 544 | |
| 545 | if (!is_out) { |
| 546 | if (pullud && |
| 547 | (offset >= pullud->first_pin) && |
| 548 | (offset <= pullud->last_pin)) { |
| 549 | abx500_get_pull_updown(pct, offset, &pud); |
| 550 | seq_printf(s, " %-9s", pull_up_down[pud]); |
| 551 | } else { |
| 552 | abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG, |
| 553 | gpio_offset, &pd); |
| 554 | seq_printf(s, " %-9s", pull_up_down[pd]); |
| 555 | } |
| 556 | } else |
| 557 | seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo"); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 558 | |
| 559 | if (pctldev) |
| 560 | mode = abx500_get_mode(pctldev, chip, offset); |
| 561 | |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 562 | seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 566 | { |
| 567 | unsigned i; |
| 568 | unsigned gpio = chip->base; |
| 569 | struct abx500_pinctrl *pct = to_abx500_pinctrl(chip); |
| 570 | struct pinctrl_dev *pctldev = pct->pctldev; |
| 571 | |
| 572 | for (i = 0; i < chip->ngpio; i++, gpio++) { |
| 573 | /* On AB8500, there is no GPIO0, the first is the GPIO 1 */ |
| 574 | abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio); |
| 575 | seq_printf(s, "\n"); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | #else |
| 580 | static inline void abx500_gpio_dbg_show_one(struct seq_file *s, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 581 | struct pinctrl_dev *pctldev, |
| 582 | struct gpio_chip *chip, |
| 583 | unsigned offset, unsigned gpio) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 584 | { |
| 585 | } |
| 586 | #define abx500_gpio_dbg_show NULL |
| 587 | #endif |
| 588 | |
Sachin Kamat | 9c4154e | 2013-03-19 12:01:17 +0530 | [diff] [blame] | 589 | static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 590 | { |
| 591 | int gpio = chip->base + offset; |
| 592 | |
| 593 | return pinctrl_request_gpio(gpio); |
| 594 | } |
| 595 | |
Sachin Kamat | 9c4154e | 2013-03-19 12:01:17 +0530 | [diff] [blame] | 596 | static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 597 | { |
| 598 | int gpio = chip->base + offset; |
| 599 | |
| 600 | pinctrl_free_gpio(gpio); |
| 601 | } |
| 602 | |
| 603 | static struct gpio_chip abx500gpio_chip = { |
| 604 | .label = "abx500-gpio", |
| 605 | .owner = THIS_MODULE, |
| 606 | .request = abx500_gpio_request, |
| 607 | .free = abx500_gpio_free, |
| 608 | .direction_input = abx500_gpio_direction_input, |
| 609 | .get = abx500_gpio_get, |
| 610 | .direction_output = abx500_gpio_direction_output, |
| 611 | .set = abx500_gpio_set, |
| 612 | .to_irq = abx500_gpio_to_irq, |
| 613 | .dbg_show = abx500_gpio_dbg_show, |
| 614 | }; |
| 615 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 616 | static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev) |
| 617 | { |
| 618 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 619 | |
| 620 | return pct->soc->nfunctions; |
| 621 | } |
| 622 | |
| 623 | static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 624 | unsigned function) |
| 625 | { |
| 626 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 627 | |
| 628 | return pct->soc->functions[function].name; |
| 629 | } |
| 630 | |
| 631 | static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 632 | unsigned function, |
| 633 | const char * const **groups, |
| 634 | unsigned * const num_groups) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 635 | { |
| 636 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 637 | |
| 638 | *groups = pct->soc->functions[function].groups; |
| 639 | *num_groups = pct->soc->functions[function].ngroups; |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 644 | static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 645 | unsigned group) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 646 | { |
| 647 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 648 | struct gpio_chip *chip = &pct->chip; |
| 649 | const struct abx500_pingroup *g; |
| 650 | int i; |
| 651 | int ret = 0; |
| 652 | |
| 653 | g = &pct->soc->groups[group]; |
| 654 | if (g->altsetting < 0) |
| 655 | return -EINVAL; |
| 656 | |
| 657 | dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins); |
| 658 | |
| 659 | for (i = 0; i < g->npins; i++) { |
| 660 | dev_dbg(pct->dev, "setting pin %d to altsetting %d\n", |
| 661 | g->pins[i], g->altsetting); |
| 662 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 663 | ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting); |
| 664 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 665 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | static void abx500_pmx_disable(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 670 | unsigned function, unsigned group) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 671 | { |
| 672 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 673 | const struct abx500_pingroup *g; |
| 674 | |
| 675 | g = &pct->soc->groups[group]; |
| 676 | if (g->altsetting < 0) |
| 677 | return; |
| 678 | |
| 679 | /* FIXME: poke out the mux, set the pin to some default state? */ |
| 680 | dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins); |
| 681 | } |
| 682 | |
Sachin Kamat | 9c4154e | 2013-03-19 12:01:17 +0530 | [diff] [blame] | 683 | static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 684 | struct pinctrl_gpio_range *range, |
| 685 | unsigned offset) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 686 | { |
| 687 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 688 | const struct abx500_pinrange *p; |
| 689 | int ret; |
| 690 | int i; |
| 691 | |
| 692 | /* |
| 693 | * Different ranges have different ways to enable GPIO function on a |
| 694 | * pin, so refer back to our local range type, where we handily define |
| 695 | * what altfunc enables GPIO for a certain pin. |
| 696 | */ |
| 697 | for (i = 0; i < pct->soc->gpio_num_ranges; i++) { |
| 698 | p = &pct->soc->gpio_ranges[i]; |
| 699 | if ((offset >= p->offset) && |
| 700 | (offset < (p->offset + p->npins))) |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | if (i == pct->soc->gpio_num_ranges) { |
| 705 | dev_err(pct->dev, "%s failed to locate range\n", __func__); |
| 706 | return -ENODEV; |
| 707 | } |
| 708 | |
| 709 | dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n", |
| 710 | p->altfunc, offset); |
| 711 | |
| 712 | ret = abx500_set_mode(pct->pctldev, &pct->chip, |
| 713 | offset, p->altfunc); |
| 714 | if (ret < 0) { |
| 715 | dev_err(pct->dev, "%s setting altfunc failed\n", __func__); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 723 | struct pinctrl_gpio_range *range, |
| 724 | unsigned offset) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 725 | { |
| 726 | } |
| 727 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 728 | static const struct pinmux_ops abx500_pinmux_ops = { |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 729 | .get_functions_count = abx500_pmx_get_funcs_cnt, |
| 730 | .get_function_name = abx500_pmx_get_func_name, |
| 731 | .get_function_groups = abx500_pmx_get_func_groups, |
| 732 | .enable = abx500_pmx_enable, |
| 733 | .disable = abx500_pmx_disable, |
| 734 | .gpio_request_enable = abx500_gpio_request_enable, |
| 735 | .gpio_disable_free = abx500_gpio_disable_free, |
| 736 | }; |
| 737 | |
| 738 | static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev) |
| 739 | { |
| 740 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 741 | |
| 742 | return pct->soc->ngroups; |
| 743 | } |
| 744 | |
| 745 | static const char *abx500_get_group_name(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 746 | unsigned selector) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 747 | { |
| 748 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 749 | |
| 750 | return pct->soc->groups[selector].name; |
| 751 | } |
| 752 | |
| 753 | static int abx500_get_group_pins(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 754 | unsigned selector, |
| 755 | const unsigned **pins, |
| 756 | unsigned *num_pins) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 757 | { |
| 758 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 759 | |
| 760 | *pins = pct->soc->groups[selector].pins; |
| 761 | *num_pins = pct->soc->groups[selector].npins; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 762 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 767 | struct seq_file *s, unsigned offset) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 768 | { |
| 769 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 770 | struct gpio_chip *chip = &pct->chip; |
| 771 | |
| 772 | abx500_gpio_dbg_show_one(s, pctldev, chip, offset, |
| 773 | chip->base + offset - 1); |
| 774 | } |
| 775 | |
Patrice Chotard | 64a45c9 | 2013-06-20 16:04:59 +0200 | [diff] [blame] | 776 | static void abx500_dt_free_map(struct pinctrl_dev *pctldev, |
| 777 | struct pinctrl_map *map, unsigned num_maps) |
| 778 | { |
| 779 | int i; |
| 780 | |
| 781 | for (i = 0; i < num_maps; i++) |
| 782 | if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN) |
| 783 | kfree(map[i].data.configs.configs); |
| 784 | kfree(map); |
| 785 | } |
| 786 | |
| 787 | static int abx500_dt_reserve_map(struct pinctrl_map **map, |
| 788 | unsigned *reserved_maps, |
| 789 | unsigned *num_maps, |
| 790 | unsigned reserve) |
| 791 | { |
| 792 | unsigned old_num = *reserved_maps; |
| 793 | unsigned new_num = *num_maps + reserve; |
| 794 | struct pinctrl_map *new_map; |
| 795 | |
| 796 | if (old_num >= new_num) |
| 797 | return 0; |
| 798 | |
| 799 | new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL); |
| 800 | if (!new_map) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map)); |
| 804 | |
| 805 | *map = new_map; |
| 806 | *reserved_maps = new_num; |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static int abx500_dt_add_map_mux(struct pinctrl_map **map, |
| 812 | unsigned *reserved_maps, |
| 813 | unsigned *num_maps, const char *group, |
| 814 | const char *function) |
| 815 | { |
| 816 | if (*num_maps == *reserved_maps) |
| 817 | return -ENOSPC; |
| 818 | |
| 819 | (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP; |
| 820 | (*map)[*num_maps].data.mux.group = group; |
| 821 | (*map)[*num_maps].data.mux.function = function; |
| 822 | (*num_maps)++; |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static int abx500_dt_add_map_configs(struct pinctrl_map **map, |
| 828 | unsigned *reserved_maps, |
| 829 | unsigned *num_maps, const char *group, |
| 830 | unsigned long *configs, unsigned num_configs) |
| 831 | { |
| 832 | unsigned long *dup_configs; |
| 833 | |
| 834 | if (*num_maps == *reserved_maps) |
| 835 | return -ENOSPC; |
| 836 | |
| 837 | dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs), |
| 838 | GFP_KERNEL); |
| 839 | if (!dup_configs) |
| 840 | return -ENOMEM; |
| 841 | |
| 842 | (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN; |
| 843 | |
| 844 | (*map)[*num_maps].data.configs.group_or_pin = group; |
| 845 | (*map)[*num_maps].data.configs.configs = dup_configs; |
| 846 | (*map)[*num_maps].data.configs.num_configs = num_configs; |
| 847 | (*num_maps)++; |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev, |
| 853 | const char *pin_name) |
| 854 | { |
| 855 | int i, pin_number; |
| 856 | struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 857 | |
| 858 | if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1) |
| 859 | for (i = 0; i < npct->soc->npins; i++) |
| 860 | if (npct->soc->pins[i].number == pin_number) |
| 861 | return npct->soc->pins[i].name; |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev, |
| 866 | struct device_node *np, |
| 867 | struct pinctrl_map **map, |
| 868 | unsigned *reserved_maps, |
| 869 | unsigned *num_maps) |
| 870 | { |
| 871 | int ret; |
| 872 | const char *function = NULL; |
| 873 | unsigned long *configs; |
| 874 | unsigned int nconfigs = 0; |
| 875 | bool has_config = 0; |
| 876 | unsigned reserve = 0; |
| 877 | struct property *prop; |
| 878 | const char *group, *gpio_name; |
| 879 | struct device_node *np_config; |
| 880 | |
| 881 | ret = of_property_read_string(np, "ste,function", &function); |
| 882 | if (ret >= 0) |
| 883 | reserve = 1; |
| 884 | |
| 885 | ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs); |
| 886 | if (nconfigs) |
| 887 | has_config = 1; |
| 888 | |
| 889 | np_config = of_parse_phandle(np, "ste,config", 0); |
| 890 | if (np_config) { |
| 891 | ret = pinconf_generic_parse_dt_config(np_config, &configs, |
| 892 | &nconfigs); |
| 893 | if (ret) |
| 894 | goto exit; |
| 895 | has_config |= nconfigs; |
| 896 | } |
| 897 | |
| 898 | ret = of_property_count_strings(np, "ste,pins"); |
| 899 | if (ret < 0) |
| 900 | goto exit; |
| 901 | |
| 902 | if (has_config) |
| 903 | reserve++; |
| 904 | |
| 905 | reserve *= ret; |
| 906 | |
| 907 | ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve); |
| 908 | if (ret < 0) |
| 909 | goto exit; |
| 910 | |
| 911 | of_property_for_each_string(np, "ste,pins", prop, group) { |
| 912 | if (function) { |
| 913 | ret = abx500_dt_add_map_mux(map, reserved_maps, |
| 914 | num_maps, group, function); |
| 915 | if (ret < 0) |
| 916 | goto exit; |
| 917 | } |
| 918 | if (has_config) { |
| 919 | gpio_name = abx500_find_pin_name(pctldev, group); |
| 920 | |
| 921 | ret = abx500_dt_add_map_configs(map, reserved_maps, |
| 922 | num_maps, gpio_name, configs, 1); |
| 923 | if (ret < 0) |
| 924 | goto exit; |
| 925 | } |
| 926 | |
| 927 | } |
| 928 | exit: |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 933 | struct device_node *np_config, |
| 934 | struct pinctrl_map **map, unsigned *num_maps) |
| 935 | { |
| 936 | unsigned reserved_maps; |
| 937 | struct device_node *np; |
| 938 | int ret; |
| 939 | |
| 940 | reserved_maps = 0; |
| 941 | *map = NULL; |
| 942 | *num_maps = 0; |
| 943 | |
| 944 | for_each_child_of_node(np_config, np) { |
| 945 | ret = abx500_dt_subnode_to_map(pctldev, np, map, |
| 946 | &reserved_maps, num_maps); |
| 947 | if (ret < 0) { |
| 948 | abx500_dt_free_map(pctldev, *map, *num_maps); |
| 949 | return ret; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 956 | static const struct pinctrl_ops abx500_pinctrl_ops = { |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 957 | .get_groups_count = abx500_get_groups_cnt, |
| 958 | .get_group_name = abx500_get_group_name, |
| 959 | .get_group_pins = abx500_get_group_pins, |
| 960 | .pin_dbg_show = abx500_pin_dbg_show, |
Patrice Chotard | 64a45c9 | 2013-06-20 16:04:59 +0200 | [diff] [blame] | 961 | .dt_node_to_map = abx500_dt_node_to_map, |
| 962 | .dt_free_map = abx500_dt_free_map, |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 963 | }; |
| 964 | |
Sachin Kamat | 9c4154e | 2013-03-19 12:01:17 +0530 | [diff] [blame] | 965 | static int abx500_pin_config_get(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 966 | unsigned pin, |
| 967 | unsigned long *config) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 968 | { |
Lee Jones | 1abeebe | 2012-12-20 11:11:19 +0000 | [diff] [blame] | 969 | return -ENOSYS; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Sachin Kamat | 9c4154e | 2013-03-19 12:01:17 +0530 | [diff] [blame] | 972 | static int abx500_pin_config_set(struct pinctrl_dev *pctldev, |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 973 | unsigned pin, |
| 974 | unsigned long config) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 975 | { |
| 976 | struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); |
| 977 | struct pullud *pullud = pct->soc->pullud; |
| 978 | struct gpio_chip *chip = &pct->chip; |
| 979 | unsigned offset; |
Patrice Chotard | 61ce135 | 2013-06-20 16:05:00 +0200 | [diff] [blame] | 980 | int ret = -EINVAL; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 981 | enum pin_config_param param = pinconf_to_config_param(config); |
| 982 | enum pin_config_param argument = pinconf_to_config_argument(config); |
| 983 | |
| 984 | dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n", |
| 985 | pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input", |
| 986 | (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") : |
| 987 | (argument ? "pull up" : "pull down")); |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 988 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 989 | /* on ABx500, there is no GPIO0, so adjust the offset */ |
| 990 | offset = pin - 1; |
| 991 | |
| 992 | switch (param) { |
Patrice Chotard | 61ce135 | 2013-06-20 16:05:00 +0200 | [diff] [blame] | 993 | case PIN_CONFIG_BIAS_DISABLE: |
| 994 | ret = abx500_gpio_direction_input(chip, offset); |
| 995 | /* |
| 996 | * Some chips only support pull down, while some actually |
| 997 | * support both pull up and pull down. Such chips have |
| 998 | * a "pullud" range specified for the pins that support |
| 999 | * both features. If the pin is not within that range, we |
| 1000 | * fall back to the old bit set that only support pull down. |
| 1001 | */ |
| 1002 | if (pullud && |
| 1003 | pin >= pullud->first_pin && |
| 1004 | pin <= pullud->last_pin) |
| 1005 | ret = abx500_set_pull_updown(pct, |
| 1006 | pin, |
| 1007 | ABX500_GPIO_PULL_NONE); |
| 1008 | else |
| 1009 | /* Chip only supports pull down */ |
| 1010 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, |
| 1011 | offset, ABX500_GPIO_PULL_NONE); |
| 1012 | break; |
| 1013 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1014 | case PIN_CONFIG_BIAS_PULL_DOWN: |
Patrice Chotard | 61ce135 | 2013-06-20 16:05:00 +0200 | [diff] [blame] | 1015 | ret = abx500_gpio_direction_input(chip, offset); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1016 | /* |
| 1017 | * if argument = 1 set the pull down |
| 1018 | * else clear the pull down |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1019 | * Some chips only support pull down, while some actually |
| 1020 | * support both pull up and pull down. Such chips have |
| 1021 | * a "pullud" range specified for the pins that support |
| 1022 | * both features. If the pin is not within that range, we |
| 1023 | * fall back to the old bit set that only support pull down. |
| 1024 | */ |
| 1025 | if (pullud && |
| 1026 | pin >= pullud->first_pin && |
| 1027 | pin <= pullud->last_pin) |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 1028 | ret = abx500_set_pull_updown(pct, |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1029 | pin, |
| 1030 | argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE); |
| 1031 | else |
| 1032 | /* Chip only supports pull down */ |
| 1033 | ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, |
| 1034 | offset, argument ? 0 : 1); |
| 1035 | break; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1036 | |
Patrice Chotard | 9ed3cd3 | 2013-05-24 14:06:30 +0200 | [diff] [blame] | 1037 | case PIN_CONFIG_BIAS_PULL_UP: |
Patrice Chotard | 61ce135 | 2013-06-20 16:05:00 +0200 | [diff] [blame] | 1038 | ret = abx500_gpio_direction_input(chip, offset); |
Patrice Chotard | 9ed3cd3 | 2013-05-24 14:06:30 +0200 | [diff] [blame] | 1039 | /* |
| 1040 | * if argument = 1 set the pull up |
| 1041 | * else clear the pull up |
| 1042 | */ |
| 1043 | ret = abx500_gpio_direction_input(chip, offset); |
| 1044 | /* |
| 1045 | * Some chips only support pull down, while some actually |
| 1046 | * support both pull up and pull down. Such chips have |
| 1047 | * a "pullud" range specified for the pins that support |
| 1048 | * both features. If the pin is not within that range, do |
| 1049 | * nothing |
| 1050 | */ |
| 1051 | if (pullud && |
| 1052 | pin >= pullud->first_pin && |
| 1053 | pin <= pullud->last_pin) { |
Patrice Chotard | d2752ae | 2013-05-24 14:06:31 +0200 | [diff] [blame] | 1054 | ret = abx500_set_pull_updown(pct, |
Patrice Chotard | 9ed3cd3 | 2013-05-24 14:06:30 +0200 | [diff] [blame] | 1055 | pin, |
| 1056 | argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE); |
| 1057 | } |
| 1058 | break; |
| 1059 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1060 | case PIN_CONFIG_OUTPUT: |
| 1061 | ret = abx500_gpio_direction_output(chip, offset, argument); |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1062 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1063 | break; |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1064 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1065 | default: |
| 1066 | dev_err(chip->dev, "illegal configuration requested\n"); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1067 | } |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1068 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1069 | return ret; |
| 1070 | } |
| 1071 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 1072 | static const struct pinconf_ops abx500_pinconf_ops = { |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1073 | .pin_config_get = abx500_pin_config_get, |
| 1074 | .pin_config_set = abx500_pin_config_set, |
| 1075 | }; |
| 1076 | |
| 1077 | static struct pinctrl_desc abx500_pinctrl_desc = { |
| 1078 | .name = "pinctrl-abx500", |
| 1079 | .pctlops = &abx500_pinctrl_ops, |
| 1080 | .pmxops = &abx500_pinmux_ops, |
| 1081 | .confops = &abx500_pinconf_ops, |
| 1082 | .owner = THIS_MODULE, |
| 1083 | }; |
| 1084 | |
| 1085 | static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc) |
| 1086 | { |
| 1087 | unsigned int lowest = 0; |
| 1088 | unsigned int highest = 0; |
| 1089 | unsigned int npins = 0; |
| 1090 | int i; |
| 1091 | |
| 1092 | /* |
| 1093 | * Compute number of GPIOs from the last SoC gpio range descriptors |
| 1094 | * These ranges may include "holes" but the GPIO number space shall |
| 1095 | * still be homogeneous, so we need to detect and account for any |
| 1096 | * such holes so that these are included in the number of GPIO pins. |
| 1097 | */ |
| 1098 | for (i = 0; i < soc->gpio_num_ranges; i++) { |
| 1099 | unsigned gstart; |
| 1100 | unsigned gend; |
| 1101 | const struct abx500_pinrange *p; |
| 1102 | |
| 1103 | p = &soc->gpio_ranges[i]; |
| 1104 | gstart = p->offset; |
| 1105 | gend = p->offset + p->npins - 1; |
| 1106 | |
| 1107 | if (i == 0) { |
| 1108 | /* First iteration, set start values */ |
| 1109 | lowest = gstart; |
| 1110 | highest = gend; |
| 1111 | } else { |
| 1112 | if (gstart < lowest) |
| 1113 | lowest = gstart; |
| 1114 | if (gend > highest) |
| 1115 | highest = gend; |
| 1116 | } |
| 1117 | } |
| 1118 | /* this gives the absolute number of pins */ |
| 1119 | npins = highest - lowest + 1; |
| 1120 | return npins; |
| 1121 | } |
| 1122 | |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1123 | static const struct of_device_id abx500_gpio_match[] = { |
| 1124 | { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, }, |
| 1125 | { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, }, |
| 1126 | { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, }, |
| 1127 | { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, }, |
Axel Lin | e392971 | 2013-02-17 21:58:47 +0800 | [diff] [blame] | 1128 | { } |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1129 | }; |
| 1130 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1131 | static int abx500_gpio_probe(struct platform_device *pdev) |
| 1132 | { |
| 1133 | struct ab8500_platform_data *abx500_pdata = |
| 1134 | dev_get_platdata(pdev->dev.parent); |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1135 | struct abx500_gpio_platform_data *pdata = NULL; |
| 1136 | struct device_node *np = pdev->dev.of_node; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1137 | struct abx500_pinctrl *pct; |
| 1138 | const struct platform_device_id *platid = platform_get_device_id(pdev); |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1139 | unsigned int id = -1; |
Lee Jones | fa1ec99 | 2013-01-31 11:06:33 +0000 | [diff] [blame] | 1140 | int ret, err; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1141 | int i; |
| 1142 | |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1143 | if (abx500_pdata) |
| 1144 | pdata = abx500_pdata->gpio; |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1145 | |
Lee Jones | 86c976e | 2013-05-08 14:29:08 +0100 | [diff] [blame] | 1146 | if (!(pdata || np)) { |
| 1147 | dev_err(&pdev->dev, "gpio dt and platform data missing\n"); |
| 1148 | return -ENODEV; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl), |
| 1152 | GFP_KERNEL); |
| 1153 | if (pct == NULL) { |
| 1154 | dev_err(&pdev->dev, |
| 1155 | "failed to allocate memory for pct\n"); |
| 1156 | return -ENOMEM; |
| 1157 | } |
| 1158 | |
| 1159 | pct->dev = &pdev->dev; |
| 1160 | pct->parent = dev_get_drvdata(pdev->dev.parent); |
| 1161 | pct->chip = abx500gpio_chip; |
| 1162 | pct->chip.dev = &pdev->dev; |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1163 | pct->chip.base = (np) ? -1 : pdata->gpio_base; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1164 | |
Lee Jones | 86c976e | 2013-05-08 14:29:08 +0100 | [diff] [blame] | 1165 | if (platid) |
| 1166 | id = platid->driver_data; |
| 1167 | else if (np) { |
| 1168 | const struct of_device_id *match; |
| 1169 | |
| 1170 | match = of_match_device(abx500_gpio_match, &pdev->dev); |
| 1171 | if (match) |
| 1172 | id = (unsigned long)match->data; |
| 1173 | } |
| 1174 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1175 | /* Poke in other ASIC variants here */ |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1176 | switch (id) { |
Patrice Chotard | 3c93799 | 2013-01-08 10:59:53 +0100 | [diff] [blame] | 1177 | case PINCTRL_AB8500: |
| 1178 | abx500_pinctrl_ab8500_init(&pct->soc); |
| 1179 | break; |
Patrice Chotard | a8f96e4 | 2013-01-28 14:35:19 +0100 | [diff] [blame] | 1180 | case PINCTRL_AB8540: |
| 1181 | abx500_pinctrl_ab8540_init(&pct->soc); |
| 1182 | break; |
Patrice Chotard | 09dbec3 | 2013-01-28 14:29:35 +0100 | [diff] [blame] | 1183 | case PINCTRL_AB9540: |
| 1184 | abx500_pinctrl_ab9540_init(&pct->soc); |
| 1185 | break; |
Patrice Chotard | 1aa2d8d | 2013-01-28 14:23:45 +0100 | [diff] [blame] | 1186 | case PINCTRL_AB8505: |
| 1187 | abx500_pinctrl_ab8505_init(&pct->soc); |
| 1188 | break; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1189 | default: |
Lee Jones | 2fcad12 | 2013-05-08 14:29:07 +0100 | [diff] [blame] | 1190 | dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id); |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1191 | return -EINVAL; |
| 1192 | } |
| 1193 | |
| 1194 | if (!pct->soc) { |
| 1195 | dev_err(&pdev->dev, "Invalid SOC data\n"); |
| 1196 | return -EINVAL; |
| 1197 | } |
| 1198 | |
| 1199 | pct->chip.ngpio = abx500_get_gpio_num(pct->soc); |
| 1200 | pct->irq_cluster = pct->soc->gpio_irq_cluster; |
| 1201 | pct->irq_cluster_size = pct->soc->ngpio_irq_cluster; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1202 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1203 | ret = gpiochip_add(&pct->chip); |
| 1204 | if (ret) { |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1205 | dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret); |
Lee Jones | ac652d7 | 2013-01-31 10:43:00 +0000 | [diff] [blame] | 1206 | return ret; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1207 | } |
| 1208 | dev_info(&pdev->dev, "added gpiochip\n"); |
| 1209 | |
| 1210 | abx500_pinctrl_desc.pins = pct->soc->pins; |
| 1211 | abx500_pinctrl_desc.npins = pct->soc->npins; |
| 1212 | pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct); |
| 1213 | if (!pct->pctldev) { |
| 1214 | dev_err(&pdev->dev, |
| 1215 | "could not register abx500 pinctrl driver\n"); |
Lee Jones | fa1ec99 | 2013-01-31 11:06:33 +0000 | [diff] [blame] | 1216 | ret = -EINVAL; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1217 | goto out_rem_chip; |
| 1218 | } |
| 1219 | dev_info(&pdev->dev, "registered pin controller\n"); |
| 1220 | |
| 1221 | /* We will handle a range of GPIO pins */ |
| 1222 | for (i = 0; i < pct->soc->gpio_num_ranges; i++) { |
| 1223 | const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i]; |
| 1224 | |
| 1225 | ret = gpiochip_add_pin_range(&pct->chip, |
| 1226 | dev_name(&pdev->dev), |
| 1227 | p->offset - 1, p->offset, p->npins); |
| 1228 | if (ret < 0) |
Lee Jones | fa1ec99 | 2013-01-31 11:06:33 +0000 | [diff] [blame] | 1229 | goto out_rem_chip; |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | platform_set_drvdata(pdev, pct); |
| 1233 | dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n"); |
| 1234 | |
| 1235 | return 0; |
| 1236 | |
| 1237 | out_rem_chip: |
Lee Jones | fa1ec99 | 2013-01-31 11:06:33 +0000 | [diff] [blame] | 1238 | err = gpiochip_remove(&pct->chip); |
| 1239 | if (err) |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1240 | dev_info(&pdev->dev, "failed to remove gpiochip\n"); |
Lee Jones | ac652d7 | 2013-01-31 10:43:00 +0000 | [diff] [blame] | 1241 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1242 | return ret; |
| 1243 | } |
| 1244 | |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1245 | /** |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1246 | * abx500_gpio_remove() - remove Ab8500-gpio driver |
Lee Jones | 83b423c | 2013-01-23 13:24:08 +0000 | [diff] [blame] | 1247 | * @pdev: Platform device registered |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1248 | */ |
| 1249 | static int abx500_gpio_remove(struct platform_device *pdev) |
| 1250 | { |
| 1251 | struct abx500_pinctrl *pct = platform_get_drvdata(pdev); |
| 1252 | int ret; |
| 1253 | |
| 1254 | ret = gpiochip_remove(&pct->chip); |
| 1255 | if (ret < 0) { |
| 1256 | dev_err(pct->dev, "unable to remove gpiochip: %d\n", |
| 1257 | ret); |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static const struct platform_device_id abx500_pinctrl_id[] = { |
| 1265 | { "pinctrl-ab8500", PINCTRL_AB8500 }, |
| 1266 | { "pinctrl-ab8540", PINCTRL_AB8540 }, |
| 1267 | { "pinctrl-ab9540", PINCTRL_AB9540 }, |
| 1268 | { "pinctrl-ab8505", PINCTRL_AB8505 }, |
| 1269 | { }, |
| 1270 | }; |
| 1271 | |
| 1272 | static struct platform_driver abx500_gpio_driver = { |
| 1273 | .driver = { |
| 1274 | .name = "abx500-gpio", |
| 1275 | .owner = THIS_MODULE, |
Lee Jones | f30a383 | 2013-01-31 11:07:40 +0000 | [diff] [blame] | 1276 | .of_match_table = abx500_gpio_match, |
Patrice Chotard | 0493e64 | 2013-01-08 10:41:02 +0100 | [diff] [blame] | 1277 | }, |
| 1278 | .probe = abx500_gpio_probe, |
| 1279 | .remove = abx500_gpio_remove, |
| 1280 | .id_table = abx500_pinctrl_id, |
| 1281 | }; |
| 1282 | |
| 1283 | static int __init abx500_gpio_init(void) |
| 1284 | { |
| 1285 | return platform_driver_register(&abx500_gpio_driver); |
| 1286 | } |
| 1287 | core_initcall(abx500_gpio_init); |
| 1288 | |
| 1289 | MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>"); |
| 1290 | MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO"); |
| 1291 | MODULE_ALIAS("platform:abx500-gpio"); |
| 1292 | MODULE_LICENSE("GPL v2"); |