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