Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * at91 pinctrl driver based on at91 pinmux core |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> |
| 5 | * |
| 6 | * Under GPLv2 only |
| 7 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/of_irq.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/irq.h> |
| 20 | #include <linux/irqdomain.h> |
Catalin Marinas | de88cbb | 2013-01-18 15:31:37 +0000 | [diff] [blame] | 21 | #include <linux/irqchip/chained_irq.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 22 | #include <linux/io.h> |
| 23 | #include <linux/gpio.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 24 | #include <linux/pinctrl/machine.h> |
| 25 | #include <linux/pinctrl/pinconf.h> |
| 26 | #include <linux/pinctrl/pinctrl.h> |
| 27 | #include <linux/pinctrl/pinmux.h> |
| 28 | /* Since we request GPIOs from ourself */ |
| 29 | #include <linux/pinctrl/consumer.h> |
| 30 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 31 | #include <mach/hardware.h> |
| 32 | #include <mach/at91_pio.h> |
| 33 | |
| 34 | #include "core.h" |
| 35 | |
| 36 | #define MAX_NB_GPIO_PER_BANK 32 |
| 37 | |
| 38 | struct at91_pinctrl_mux_ops; |
| 39 | |
| 40 | struct at91_gpio_chip { |
| 41 | struct gpio_chip chip; |
| 42 | struct pinctrl_gpio_range range; |
| 43 | struct at91_gpio_chip *next; /* Bank sharing same clock */ |
| 44 | int pioc_hwirq; /* PIO bank interrupt identifier on AIC */ |
| 45 | int pioc_virq; /* PIO bank Linux virtual interrupt */ |
| 46 | int pioc_idx; /* PIO bank index */ |
| 47 | void __iomem *regbase; /* PIO bank virtual address */ |
| 48 | struct clk *clock; /* associated clock */ |
| 49 | struct irq_domain *domain; /* associated irq domain */ |
| 50 | struct at91_pinctrl_mux_ops *ops; /* ops */ |
| 51 | }; |
| 52 | |
| 53 | #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip) |
| 54 | |
| 55 | static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS]; |
| 56 | |
| 57 | static int gpio_banks; |
| 58 | |
Jean-Christophe PLAGNIOL-VILLARD | 525fae2 | 2012-10-23 18:28:00 +0200 | [diff] [blame] | 59 | #define PULL_UP (1 << 0) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 60 | #define MULTI_DRIVE (1 << 1) |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 61 | #define DEGLITCH (1 << 2) |
| 62 | #define PULL_DOWN (1 << 3) |
| 63 | #define DIS_SCHMIT (1 << 4) |
| 64 | #define DEBOUNCE (1 << 16) |
| 65 | #define DEBOUNCE_VAL_SHIFT 17 |
| 66 | #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * struct at91_pmx_func - describes AT91 pinmux functions |
| 70 | * @name: the name of this specific function |
| 71 | * @groups: corresponding pin groups |
| 72 | * @ngroups: the number of groups |
| 73 | */ |
| 74 | struct at91_pmx_func { |
| 75 | const char *name; |
| 76 | const char **groups; |
| 77 | unsigned ngroups; |
| 78 | }; |
| 79 | |
| 80 | enum at91_mux { |
| 81 | AT91_MUX_GPIO = 0, |
| 82 | AT91_MUX_PERIPH_A = 1, |
| 83 | AT91_MUX_PERIPH_B = 2, |
| 84 | AT91_MUX_PERIPH_C = 3, |
| 85 | AT91_MUX_PERIPH_D = 4, |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * struct at91_pmx_pin - describes an At91 pin mux |
| 90 | * @bank: the bank of the pin |
| 91 | * @pin: the pin number in the @bank |
| 92 | * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function. |
| 93 | * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc... |
| 94 | */ |
| 95 | struct at91_pmx_pin { |
| 96 | uint32_t bank; |
| 97 | uint32_t pin; |
| 98 | enum at91_mux mux; |
| 99 | unsigned long conf; |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * struct at91_pin_group - describes an At91 pin group |
| 104 | * @name: the name of this specific pin group |
| 105 | * @pins_conf: the mux mode for each pin in this group. The size of this |
| 106 | * array is the same as pins. |
| 107 | * @pins: an array of discrete physical pins used in this group, taken |
| 108 | * from the driver-local pin enumeration space |
| 109 | * @npins: the number of pins in this group array, i.e. the number of |
| 110 | * elements in .pins so we can iterate over that array |
| 111 | */ |
| 112 | struct at91_pin_group { |
| 113 | const char *name; |
| 114 | struct at91_pmx_pin *pins_conf; |
| 115 | unsigned int *pins; |
| 116 | unsigned npins; |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * struct at91_pinctrl_mux_ops - describes an At91 mux ops group |
| 121 | * on new IP with support for periph C and D the way to mux in |
| 122 | * periph A and B has changed |
| 123 | * So provide the right call back |
| 124 | * if not present means the IP does not support it |
| 125 | * @get_periph: return the periph mode configured |
| 126 | * @mux_A_periph: mux as periph A |
| 127 | * @mux_B_periph: mux as periph B |
| 128 | * @mux_C_periph: mux as periph C |
| 129 | * @mux_D_periph: mux as periph D |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 130 | * @get_deglitch: get deglitch status |
| 131 | * @set_deglitch: enable/disable deglitch |
| 132 | * @get_debounce: get debounce status |
| 133 | * @set_debounce: enable/disable debounce |
| 134 | * @get_pulldown: get pulldown status |
| 135 | * @set_pulldown: enable/disable pulldown |
| 136 | * @get_schmitt_trig: get schmitt trigger status |
| 137 | * @disable_schmitt_trig: disable schmitt trigger |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 138 | * @irq_type: return irq type |
| 139 | */ |
| 140 | struct at91_pinctrl_mux_ops { |
| 141 | enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask); |
| 142 | void (*mux_A_periph)(void __iomem *pio, unsigned mask); |
| 143 | void (*mux_B_periph)(void __iomem *pio, unsigned mask); |
| 144 | void (*mux_C_periph)(void __iomem *pio, unsigned mask); |
| 145 | void (*mux_D_periph)(void __iomem *pio, unsigned mask); |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 146 | bool (*get_deglitch)(void __iomem *pio, unsigned pin); |
| 147 | void (*set_deglitch)(void __iomem *pio, unsigned mask, bool in_on); |
| 148 | bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div); |
| 149 | void (*set_debounce)(void __iomem *pio, unsigned mask, bool in_on, u32 div); |
| 150 | bool (*get_pulldown)(void __iomem *pio, unsigned pin); |
| 151 | void (*set_pulldown)(void __iomem *pio, unsigned mask, bool in_on); |
| 152 | bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin); |
| 153 | void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 154 | /* irq */ |
| 155 | int (*irq_type)(struct irq_data *d, unsigned type); |
| 156 | }; |
| 157 | |
| 158 | static int gpio_irq_type(struct irq_data *d, unsigned type); |
| 159 | static int alt_gpio_irq_type(struct irq_data *d, unsigned type); |
| 160 | |
| 161 | struct at91_pinctrl { |
| 162 | struct device *dev; |
| 163 | struct pinctrl_dev *pctl; |
| 164 | |
| 165 | int nbanks; |
| 166 | |
| 167 | uint32_t *mux_mask; |
| 168 | int nmux; |
| 169 | |
| 170 | struct at91_pmx_func *functions; |
| 171 | int nfunctions; |
| 172 | |
| 173 | struct at91_pin_group *groups; |
| 174 | int ngroups; |
| 175 | |
| 176 | struct at91_pinctrl_mux_ops *ops; |
| 177 | }; |
| 178 | |
| 179 | static const inline struct at91_pin_group *at91_pinctrl_find_group_by_name( |
| 180 | const struct at91_pinctrl *info, |
| 181 | const char *name) |
| 182 | { |
| 183 | const struct at91_pin_group *grp = NULL; |
| 184 | int i; |
| 185 | |
| 186 | for (i = 0; i < info->ngroups; i++) { |
| 187 | if (strcmp(info->groups[i].name, name)) |
| 188 | continue; |
| 189 | |
| 190 | grp = &info->groups[i]; |
| 191 | dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]); |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | return grp; |
| 196 | } |
| 197 | |
| 198 | static int at91_get_groups_count(struct pinctrl_dev *pctldev) |
| 199 | { |
| 200 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 201 | |
| 202 | return info->ngroups; |
| 203 | } |
| 204 | |
| 205 | static const char *at91_get_group_name(struct pinctrl_dev *pctldev, |
| 206 | unsigned selector) |
| 207 | { |
| 208 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 209 | |
| 210 | return info->groups[selector].name; |
| 211 | } |
| 212 | |
| 213 | static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, |
| 214 | const unsigned **pins, |
| 215 | unsigned *npins) |
| 216 | { |
| 217 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 218 | |
| 219 | if (selector >= info->ngroups) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | *pins = info->groups[selector].pins; |
| 223 | *npins = info->groups[selector].npins; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, |
| 229 | unsigned offset) |
| 230 | { |
| 231 | seq_printf(s, "%s", dev_name(pctldev->dev)); |
| 232 | } |
| 233 | |
| 234 | static int at91_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 235 | struct device_node *np, |
| 236 | struct pinctrl_map **map, unsigned *num_maps) |
| 237 | { |
| 238 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 239 | const struct at91_pin_group *grp; |
| 240 | struct pinctrl_map *new_map; |
| 241 | struct device_node *parent; |
| 242 | int map_num = 1; |
| 243 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * first find the group of this node and check if we need create |
| 247 | * config maps for pins |
| 248 | */ |
| 249 | grp = at91_pinctrl_find_group_by_name(info, np->name); |
| 250 | if (!grp) { |
| 251 | dev_err(info->dev, "unable to find group for node %s\n", |
| 252 | np->name); |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | map_num += grp->npins; |
| 257 | new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL); |
| 258 | if (!new_map) |
| 259 | return -ENOMEM; |
| 260 | |
| 261 | *map = new_map; |
| 262 | *num_maps = map_num; |
| 263 | |
| 264 | /* create mux map */ |
| 265 | parent = of_get_parent(np); |
| 266 | if (!parent) { |
Julia Lawall | c62b2b3 | 2012-12-12 15:22:44 +0100 | [diff] [blame] | 267 | devm_kfree(pctldev->dev, new_map); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | } |
| 270 | new_map[0].type = PIN_MAP_TYPE_MUX_GROUP; |
| 271 | new_map[0].data.mux.function = parent->name; |
| 272 | new_map[0].data.mux.group = np->name; |
| 273 | of_node_put(parent); |
| 274 | |
| 275 | /* create config map */ |
| 276 | new_map++; |
| 277 | for (i = 0; i < grp->npins; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 278 | new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN; |
| 279 | new_map[i].data.configs.group_or_pin = |
| 280 | pin_get_name(pctldev, grp->pins[i]); |
| 281 | new_map[i].data.configs.configs = &grp->pins_conf[i].conf; |
| 282 | new_map[i].data.configs.num_configs = 1; |
| 283 | } |
| 284 | |
| 285 | dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n", |
| 286 | (*map)->data.mux.function, (*map)->data.mux.group, map_num); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static void at91_dt_free_map(struct pinctrl_dev *pctldev, |
| 292 | struct pinctrl_map *map, unsigned num_maps) |
| 293 | { |
| 294 | } |
| 295 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 296 | static const struct pinctrl_ops at91_pctrl_ops = { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 297 | .get_groups_count = at91_get_groups_count, |
| 298 | .get_group_name = at91_get_group_name, |
| 299 | .get_group_pins = at91_get_group_pins, |
| 300 | .pin_dbg_show = at91_pin_dbg_show, |
| 301 | .dt_node_to_map = at91_dt_node_to_map, |
| 302 | .dt_free_map = at91_dt_free_map, |
| 303 | }; |
| 304 | |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 305 | static void __iomem *pin_to_controller(struct at91_pinctrl *info, |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 306 | unsigned int bank) |
| 307 | { |
| 308 | return gpio_chips[bank]->regbase; |
| 309 | } |
| 310 | |
| 311 | static inline int pin_to_bank(unsigned pin) |
| 312 | { |
| 313 | return pin /= MAX_NB_GPIO_PER_BANK; |
| 314 | } |
| 315 | |
| 316 | static unsigned pin_to_mask(unsigned int pin) |
| 317 | { |
| 318 | return 1 << pin; |
| 319 | } |
| 320 | |
| 321 | static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask) |
| 322 | { |
| 323 | writel_relaxed(mask, pio + PIO_IDR); |
| 324 | } |
| 325 | |
| 326 | static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin) |
| 327 | { |
| 328 | return (readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1; |
| 329 | } |
| 330 | |
| 331 | static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on) |
| 332 | { |
| 333 | writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR)); |
| 334 | } |
| 335 | |
| 336 | static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin) |
| 337 | { |
| 338 | return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1; |
| 339 | } |
| 340 | |
| 341 | static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on) |
| 342 | { |
| 343 | writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR)); |
| 344 | } |
| 345 | |
| 346 | static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask) |
| 347 | { |
| 348 | writel_relaxed(mask, pio + PIO_ASR); |
| 349 | } |
| 350 | |
| 351 | static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask) |
| 352 | { |
| 353 | writel_relaxed(mask, pio + PIO_BSR); |
| 354 | } |
| 355 | |
| 356 | static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask) |
| 357 | { |
| 358 | |
| 359 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, |
| 360 | pio + PIO_ABCDSR1); |
| 361 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask, |
| 362 | pio + PIO_ABCDSR2); |
| 363 | } |
| 364 | |
| 365 | static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask) |
| 366 | { |
| 367 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, |
| 368 | pio + PIO_ABCDSR1); |
| 369 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask, |
| 370 | pio + PIO_ABCDSR2); |
| 371 | } |
| 372 | |
| 373 | static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask) |
| 374 | { |
| 375 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1); |
| 376 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2); |
| 377 | } |
| 378 | |
| 379 | static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask) |
| 380 | { |
| 381 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1); |
| 382 | writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2); |
| 383 | } |
| 384 | |
| 385 | static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask) |
| 386 | { |
| 387 | unsigned select; |
| 388 | |
| 389 | if (readl_relaxed(pio + PIO_PSR) & mask) |
| 390 | return AT91_MUX_GPIO; |
| 391 | |
| 392 | select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask); |
| 393 | select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1); |
| 394 | |
| 395 | return select + 1; |
| 396 | } |
| 397 | |
| 398 | static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask) |
| 399 | { |
| 400 | unsigned select; |
| 401 | |
| 402 | if (readl_relaxed(pio + PIO_PSR) & mask) |
| 403 | return AT91_MUX_GPIO; |
| 404 | |
| 405 | select = readl_relaxed(pio + PIO_ABSR) & mask; |
| 406 | |
| 407 | return select + 1; |
| 408 | } |
| 409 | |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 410 | static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin) |
| 411 | { |
| 412 | return (__raw_readl(pio + PIO_IFSR) >> pin) & 0x1; |
| 413 | } |
| 414 | |
| 415 | static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on) |
| 416 | { |
| 417 | __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR)); |
| 418 | } |
| 419 | |
| 420 | static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on) |
| 421 | { |
| 422 | if (is_on) |
| 423 | __raw_writel(mask, pio + PIO_IFSCDR); |
| 424 | at91_mux_set_deglitch(pio, mask, is_on); |
| 425 | } |
| 426 | |
| 427 | static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div) |
| 428 | { |
| 429 | *div = __raw_readl(pio + PIO_SCDR); |
| 430 | |
| 431 | return (__raw_readl(pio + PIO_IFSCSR) >> pin) & 0x1; |
| 432 | } |
| 433 | |
| 434 | static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask, |
| 435 | bool is_on, u32 div) |
| 436 | { |
| 437 | if (is_on) { |
| 438 | __raw_writel(mask, pio + PIO_IFSCER); |
| 439 | __raw_writel(div & PIO_SCDR_DIV, pio + PIO_SCDR); |
| 440 | __raw_writel(mask, pio + PIO_IFER); |
| 441 | } else { |
| 442 | __raw_writel(mask, pio + PIO_IFDR); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin) |
| 447 | { |
| 448 | return (__raw_readl(pio + PIO_PPDSR) >> pin) & 0x1; |
| 449 | } |
| 450 | |
| 451 | static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on) |
| 452 | { |
| 453 | __raw_writel(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR)); |
| 454 | } |
| 455 | |
| 456 | static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask) |
| 457 | { |
| 458 | __raw_writel(__raw_readl(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT); |
| 459 | } |
| 460 | |
| 461 | static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin) |
| 462 | { |
| 463 | return (__raw_readl(pio + PIO_SCHMITT) >> pin) & 0x1; |
| 464 | } |
| 465 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 466 | static struct at91_pinctrl_mux_ops at91rm9200_ops = { |
| 467 | .get_periph = at91_mux_get_periph, |
| 468 | .mux_A_periph = at91_mux_set_A_periph, |
| 469 | .mux_B_periph = at91_mux_set_B_periph, |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 470 | .get_deglitch = at91_mux_get_deglitch, |
| 471 | .set_deglitch = at91_mux_set_deglitch, |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 472 | .irq_type = gpio_irq_type, |
| 473 | }; |
| 474 | |
| 475 | static struct at91_pinctrl_mux_ops at91sam9x5_ops = { |
| 476 | .get_periph = at91_mux_pio3_get_periph, |
| 477 | .mux_A_periph = at91_mux_pio3_set_A_periph, |
| 478 | .mux_B_periph = at91_mux_pio3_set_B_periph, |
| 479 | .mux_C_periph = at91_mux_pio3_set_C_periph, |
| 480 | .mux_D_periph = at91_mux_pio3_set_D_periph, |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 481 | .get_deglitch = at91_mux_get_deglitch, |
| 482 | .set_deglitch = at91_mux_pio3_set_deglitch, |
| 483 | .get_debounce = at91_mux_pio3_get_debounce, |
| 484 | .set_debounce = at91_mux_pio3_set_debounce, |
| 485 | .get_pulldown = at91_mux_pio3_get_pulldown, |
| 486 | .set_pulldown = at91_mux_pio3_set_pulldown, |
| 487 | .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig, |
| 488 | .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig, |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 489 | .irq_type = alt_gpio_irq_type, |
| 490 | }; |
| 491 | |
| 492 | static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin) |
| 493 | { |
| 494 | if (pin->mux) { |
| 495 | dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lu\n", |
| 496 | pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf); |
| 497 | } else { |
| 498 | dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lu\n", |
| 499 | pin->bank + 'A', pin->pin, pin->conf); |
| 500 | } |
| 501 | } |
| 502 | |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 503 | static int pin_check_config(struct at91_pinctrl *info, const char *name, |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 504 | int index, const struct at91_pmx_pin *pin) |
| 505 | { |
| 506 | int mux; |
| 507 | |
| 508 | /* check if it's a valid config */ |
| 509 | if (pin->bank >= info->nbanks) { |
| 510 | dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n", |
| 511 | name, index, pin->bank, info->nbanks); |
| 512 | return -EINVAL; |
| 513 | } |
| 514 | |
| 515 | if (pin->pin >= MAX_NB_GPIO_PER_BANK) { |
| 516 | dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n", |
| 517 | name, index, pin->pin, MAX_NB_GPIO_PER_BANK); |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | |
| 521 | if (!pin->mux) |
| 522 | return 0; |
| 523 | |
| 524 | mux = pin->mux - 1; |
| 525 | |
| 526 | if (mux >= info->nmux) { |
| 527 | dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n", |
| 528 | name, index, mux, info->nmux); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) { |
| 533 | dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n", |
| 534 | name, index, mux, pin->bank + 'A', pin->pin); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask) |
| 542 | { |
| 543 | writel_relaxed(mask, pio + PIO_PDR); |
| 544 | } |
| 545 | |
| 546 | static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input) |
| 547 | { |
| 548 | writel_relaxed(mask, pio + PIO_PER); |
| 549 | writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER)); |
| 550 | } |
| 551 | |
| 552 | static int at91_pmx_enable(struct pinctrl_dev *pctldev, unsigned selector, |
| 553 | unsigned group) |
| 554 | { |
| 555 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 556 | const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf; |
| 557 | const struct at91_pmx_pin *pin; |
| 558 | uint32_t npins = info->groups[group].npins; |
| 559 | int i, ret; |
| 560 | unsigned mask; |
| 561 | void __iomem *pio; |
| 562 | |
| 563 | dev_dbg(info->dev, "enable function %s group %s\n", |
| 564 | info->functions[selector].name, info->groups[group].name); |
| 565 | |
| 566 | /* first check that all the pins of the group are valid with a valid |
| 567 | * paramter */ |
| 568 | for (i = 0; i < npins; i++) { |
| 569 | pin = &pins_conf[i]; |
| 570 | ret = pin_check_config(info, info->groups[group].name, i, pin); |
| 571 | if (ret) |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | for (i = 0; i < npins; i++) { |
| 576 | pin = &pins_conf[i]; |
| 577 | at91_pin_dbg(info->dev, pin); |
| 578 | pio = pin_to_controller(info, pin->bank); |
| 579 | mask = pin_to_mask(pin->pin); |
| 580 | at91_mux_disable_interrupt(pio, mask); |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 581 | switch (pin->mux) { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 582 | case AT91_MUX_GPIO: |
| 583 | at91_mux_gpio_enable(pio, mask, 1); |
| 584 | break; |
| 585 | case AT91_MUX_PERIPH_A: |
| 586 | info->ops->mux_A_periph(pio, mask); |
| 587 | break; |
| 588 | case AT91_MUX_PERIPH_B: |
| 589 | info->ops->mux_B_periph(pio, mask); |
| 590 | break; |
| 591 | case AT91_MUX_PERIPH_C: |
| 592 | if (!info->ops->mux_C_periph) |
| 593 | return -EINVAL; |
| 594 | info->ops->mux_C_periph(pio, mask); |
| 595 | break; |
| 596 | case AT91_MUX_PERIPH_D: |
| 597 | if (!info->ops->mux_D_periph) |
| 598 | return -EINVAL; |
| 599 | info->ops->mux_D_periph(pio, mask); |
| 600 | break; |
| 601 | } |
| 602 | if (pin->mux) |
| 603 | at91_mux_gpio_disable(pio, mask); |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static void at91_pmx_disable(struct pinctrl_dev *pctldev, unsigned selector, |
| 610 | unsigned group) |
| 611 | { |
| 612 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 613 | const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf; |
| 614 | const struct at91_pmx_pin *pin; |
| 615 | uint32_t npins = info->groups[group].npins; |
| 616 | int i; |
| 617 | unsigned mask; |
| 618 | void __iomem *pio; |
| 619 | |
| 620 | for (i = 0; i < npins; i++) { |
| 621 | pin = &pins_conf[i]; |
| 622 | at91_pin_dbg(info->dev, pin); |
| 623 | pio = pin_to_controller(info, pin->bank); |
| 624 | mask = pin_to_mask(pin->pin); |
| 625 | at91_mux_gpio_enable(pio, mask, 1); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev) |
| 630 | { |
| 631 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 632 | |
| 633 | return info->nfunctions; |
| 634 | } |
| 635 | |
| 636 | static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 637 | unsigned selector) |
| 638 | { |
| 639 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 640 | |
| 641 | return info->functions[selector].name; |
| 642 | } |
| 643 | |
| 644 | static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector, |
| 645 | const char * const **groups, |
| 646 | unsigned * const num_groups) |
| 647 | { |
| 648 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 649 | |
| 650 | *groups = info->functions[selector].groups; |
| 651 | *num_groups = info->functions[selector].ngroups; |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Axel Lin | f6f94f6 | 2012-11-05 21:23:50 +0800 | [diff] [blame] | 656 | static int at91_gpio_request_enable(struct pinctrl_dev *pctldev, |
| 657 | struct pinctrl_gpio_range *range, |
| 658 | unsigned offset) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 659 | { |
| 660 | struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 661 | struct at91_gpio_chip *at91_chip; |
| 662 | struct gpio_chip *chip; |
| 663 | unsigned mask; |
| 664 | |
| 665 | if (!range) { |
| 666 | dev_err(npct->dev, "invalid range\n"); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | if (!range->gc) { |
| 670 | dev_err(npct->dev, "missing GPIO chip in range\n"); |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | chip = range->gc; |
| 674 | at91_chip = container_of(chip, struct at91_gpio_chip, chip); |
| 675 | |
| 676 | dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset); |
| 677 | |
| 678 | mask = 1 << (offset - chip->base); |
| 679 | |
| 680 | dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n", |
| 681 | offset, 'A' + range->id, offset - chip->base, mask); |
| 682 | |
| 683 | writel_relaxed(mask, at91_chip->regbase + PIO_PER); |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
Axel Lin | f6f94f6 | 2012-11-05 21:23:50 +0800 | [diff] [blame] | 688 | static void at91_gpio_disable_free(struct pinctrl_dev *pctldev, |
| 689 | struct pinctrl_gpio_range *range, |
| 690 | unsigned offset) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 691 | { |
| 692 | struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev); |
| 693 | |
| 694 | dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset); |
| 695 | /* Set the pin to some default state, GPIO is usually default */ |
| 696 | } |
| 697 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 698 | static const struct pinmux_ops at91_pmx_ops = { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 699 | .get_functions_count = at91_pmx_get_funcs_count, |
| 700 | .get_function_name = at91_pmx_get_func_name, |
| 701 | .get_function_groups = at91_pmx_get_groups, |
| 702 | .enable = at91_pmx_enable, |
| 703 | .disable = at91_pmx_disable, |
| 704 | .gpio_request_enable = at91_gpio_request_enable, |
| 705 | .gpio_disable_free = at91_gpio_disable_free, |
| 706 | }; |
| 707 | |
| 708 | static int at91_pinconf_get(struct pinctrl_dev *pctldev, |
| 709 | unsigned pin_id, unsigned long *config) |
| 710 | { |
| 711 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 712 | void __iomem *pio; |
| 713 | unsigned pin; |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 714 | int div; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 715 | |
| 716 | dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__, __LINE__, pin_id, *config); |
| 717 | pio = pin_to_controller(info, pin_to_bank(pin_id)); |
| 718 | pin = pin_id % MAX_NB_GPIO_PER_BANK; |
| 719 | |
| 720 | if (at91_mux_get_multidrive(pio, pin)) |
| 721 | *config |= MULTI_DRIVE; |
| 722 | |
| 723 | if (at91_mux_get_pullup(pio, pin)) |
| 724 | *config |= PULL_UP; |
| 725 | |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 726 | if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin)) |
| 727 | *config |= DEGLITCH; |
| 728 | if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div)) |
| 729 | *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT); |
| 730 | if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin)) |
| 731 | *config |= PULL_DOWN; |
| 732 | if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin)) |
| 733 | *config |= DIS_SCHMIT; |
| 734 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static int at91_pinconf_set(struct pinctrl_dev *pctldev, |
| 739 | unsigned pin_id, unsigned long config) |
| 740 | { |
| 741 | struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev); |
| 742 | unsigned mask; |
| 743 | void __iomem *pio; |
| 744 | |
| 745 | dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__, __LINE__, pin_id, config); |
| 746 | pio = pin_to_controller(info, pin_to_bank(pin_id)); |
| 747 | mask = pin_to_mask(pin_id % MAX_NB_GPIO_PER_BANK); |
| 748 | |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 749 | if (config & PULL_UP && config & PULL_DOWN) |
| 750 | return -EINVAL; |
| 751 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 752 | at91_mux_set_pullup(pio, mask, config & PULL_UP); |
| 753 | at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE); |
Jean-Christophe PLAGNIOL-VILLARD | 7ebd7a3 | 2012-09-26 14:57:45 +0800 | [diff] [blame] | 754 | if (info->ops->set_deglitch) |
| 755 | info->ops->set_deglitch(pio, mask, config & DEGLITCH); |
| 756 | if (info->ops->set_debounce) |
| 757 | info->ops->set_debounce(pio, mask, config & DEBOUNCE, |
| 758 | (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT); |
| 759 | if (info->ops->set_pulldown) |
| 760 | info->ops->set_pulldown(pio, mask, config & PULL_DOWN); |
| 761 | if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT) |
| 762 | info->ops->disable_schmitt_trig(pio, mask); |
| 763 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev, |
| 768 | struct seq_file *s, unsigned pin_id) |
| 769 | { |
| 770 | |
| 771 | } |
| 772 | |
| 773 | static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev, |
| 774 | struct seq_file *s, unsigned group) |
| 775 | { |
| 776 | } |
| 777 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 778 | static const struct pinconf_ops at91_pinconf_ops = { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 779 | .pin_config_get = at91_pinconf_get, |
| 780 | .pin_config_set = at91_pinconf_set, |
| 781 | .pin_config_dbg_show = at91_pinconf_dbg_show, |
| 782 | .pin_config_group_dbg_show = at91_pinconf_group_dbg_show, |
| 783 | }; |
| 784 | |
| 785 | static struct pinctrl_desc at91_pinctrl_desc = { |
| 786 | .pctlops = &at91_pctrl_ops, |
| 787 | .pmxops = &at91_pmx_ops, |
| 788 | .confops = &at91_pinconf_ops, |
| 789 | .owner = THIS_MODULE, |
| 790 | }; |
| 791 | |
| 792 | static const char *gpio_compat = "atmel,at91rm9200-gpio"; |
| 793 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 794 | static void at91_pinctrl_child_count(struct at91_pinctrl *info, |
| 795 | struct device_node *np) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 796 | { |
| 797 | struct device_node *child; |
| 798 | |
| 799 | for_each_child_of_node(np, child) { |
| 800 | if (of_device_is_compatible(child, gpio_compat)) { |
| 801 | info->nbanks++; |
| 802 | } else { |
| 803 | info->nfunctions++; |
| 804 | info->ngroups += of_get_child_count(child); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 809 | static int at91_pinctrl_mux_mask(struct at91_pinctrl *info, |
| 810 | struct device_node *np) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 811 | { |
| 812 | int ret = 0; |
| 813 | int size; |
Sachin Kamat | 1164d73 | 2013-03-15 10:07:02 +0530 | [diff] [blame] | 814 | const __be32 *list; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 815 | |
| 816 | list = of_get_property(np, "atmel,mux-mask", &size); |
| 817 | if (!list) { |
| 818 | dev_err(info->dev, "can not read the mux-mask of %d\n", size); |
| 819 | return -EINVAL; |
| 820 | } |
| 821 | |
| 822 | size /= sizeof(*list); |
| 823 | if (!size || size % info->nbanks) { |
| 824 | dev_err(info->dev, "wrong mux mask array should be by %d\n", info->nbanks); |
| 825 | return -EINVAL; |
| 826 | } |
| 827 | info->nmux = size / info->nbanks; |
| 828 | |
| 829 | info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL); |
| 830 | if (!info->mux_mask) { |
| 831 | dev_err(info->dev, "could not alloc mux_mask\n"); |
| 832 | return -ENOMEM; |
| 833 | } |
| 834 | |
| 835 | ret = of_property_read_u32_array(np, "atmel,mux-mask", |
| 836 | info->mux_mask, size); |
| 837 | if (ret) |
| 838 | dev_err(info->dev, "can not read the mux-mask of %d\n", size); |
| 839 | return ret; |
| 840 | } |
| 841 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 842 | static int at91_pinctrl_parse_groups(struct device_node *np, |
| 843 | struct at91_pin_group *grp, |
| 844 | struct at91_pinctrl *info, u32 index) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 845 | { |
| 846 | struct at91_pmx_pin *pin; |
| 847 | int size; |
Sachin Kamat | 1164d73 | 2013-03-15 10:07:02 +0530 | [diff] [blame] | 848 | const __be32 *list; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 849 | int i, j; |
| 850 | |
| 851 | dev_dbg(info->dev, "group(%d): %s\n", index, np->name); |
| 852 | |
| 853 | /* Initialise group */ |
| 854 | grp->name = np->name; |
| 855 | |
| 856 | /* |
| 857 | * the binding format is atmel,pins = <bank pin mux CONFIG ...>, |
| 858 | * do sanity check and calculate pins number |
| 859 | */ |
| 860 | list = of_get_property(np, "atmel,pins", &size); |
| 861 | /* we do not check return since it's safe node passed down */ |
| 862 | size /= sizeof(*list); |
| 863 | if (!size || size % 4) { |
| 864 | dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n"); |
| 865 | return -EINVAL; |
| 866 | } |
| 867 | |
| 868 | grp->npins = size / 4; |
| 869 | pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin), |
| 870 | GFP_KERNEL); |
| 871 | grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int), |
| 872 | GFP_KERNEL); |
| 873 | if (!grp->pins_conf || !grp->pins) |
| 874 | return -ENOMEM; |
| 875 | |
| 876 | for (i = 0, j = 0; i < size; i += 4, j++) { |
| 877 | pin->bank = be32_to_cpu(*list++); |
| 878 | pin->pin = be32_to_cpu(*list++); |
| 879 | grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin; |
| 880 | pin->mux = be32_to_cpu(*list++); |
| 881 | pin->conf = be32_to_cpu(*list++); |
| 882 | |
| 883 | at91_pin_dbg(info->dev, pin); |
| 884 | pin++; |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 890 | static int at91_pinctrl_parse_functions(struct device_node *np, |
| 891 | struct at91_pinctrl *info, u32 index) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 892 | { |
| 893 | struct device_node *child; |
| 894 | struct at91_pmx_func *func; |
| 895 | struct at91_pin_group *grp; |
| 896 | int ret; |
| 897 | static u32 grp_index; |
| 898 | u32 i = 0; |
| 899 | |
| 900 | dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name); |
| 901 | |
| 902 | func = &info->functions[index]; |
| 903 | |
| 904 | /* Initialise function */ |
| 905 | func->name = np->name; |
| 906 | func->ngroups = of_get_child_count(np); |
| 907 | if (func->ngroups <= 0) { |
| 908 | dev_err(info->dev, "no groups defined\n"); |
| 909 | return -EINVAL; |
| 910 | } |
| 911 | func->groups = devm_kzalloc(info->dev, |
| 912 | func->ngroups * sizeof(char *), GFP_KERNEL); |
| 913 | if (!func->groups) |
| 914 | return -ENOMEM; |
| 915 | |
| 916 | for_each_child_of_node(np, child) { |
| 917 | func->groups[i] = child->name; |
| 918 | grp = &info->groups[grp_index++]; |
| 919 | ret = at91_pinctrl_parse_groups(child, grp, info, i++); |
| 920 | if (ret) |
| 921 | return ret; |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 927 | static struct of_device_id at91_pinctrl_of_match[] = { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 928 | { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops }, |
| 929 | { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops }, |
| 930 | { /* sentinel */ } |
| 931 | }; |
| 932 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 933 | static int at91_pinctrl_probe_dt(struct platform_device *pdev, |
| 934 | struct at91_pinctrl *info) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 935 | { |
| 936 | int ret = 0; |
| 937 | int i, j; |
| 938 | uint32_t *tmp; |
| 939 | struct device_node *np = pdev->dev.of_node; |
| 940 | struct device_node *child; |
| 941 | |
| 942 | if (!np) |
| 943 | return -ENODEV; |
| 944 | |
| 945 | info->dev = &pdev->dev; |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 946 | info->ops = (struct at91_pinctrl_mux_ops *) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 947 | of_match_device(at91_pinctrl_of_match, &pdev->dev)->data; |
| 948 | at91_pinctrl_child_count(info, np); |
| 949 | |
| 950 | if (info->nbanks < 1) { |
| 951 | dev_err(&pdev->dev, "you need to specify atleast one gpio-controller\n"); |
| 952 | return -EINVAL; |
| 953 | } |
| 954 | |
| 955 | ret = at91_pinctrl_mux_mask(info, np); |
| 956 | if (ret) |
| 957 | return ret; |
| 958 | |
| 959 | dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux); |
| 960 | |
| 961 | dev_dbg(&pdev->dev, "mux-mask\n"); |
| 962 | tmp = info->mux_mask; |
| 963 | for (i = 0; i < info->nbanks; i++) { |
| 964 | for (j = 0; j < info->nmux; j++, tmp++) { |
| 965 | dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions); |
| 970 | dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups); |
| 971 | info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func), |
| 972 | GFP_KERNEL); |
| 973 | if (!info->functions) |
| 974 | return -ENOMEM; |
| 975 | |
| 976 | info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group), |
| 977 | GFP_KERNEL); |
| 978 | if (!info->groups) |
| 979 | return -ENOMEM; |
| 980 | |
| 981 | dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks); |
| 982 | dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions); |
| 983 | dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups); |
| 984 | |
| 985 | i = 0; |
| 986 | |
| 987 | for_each_child_of_node(np, child) { |
| 988 | if (of_device_is_compatible(child, gpio_compat)) |
| 989 | continue; |
| 990 | ret = at91_pinctrl_parse_functions(child, info, i++); |
| 991 | if (ret) { |
| 992 | dev_err(&pdev->dev, "failed to parse function\n"); |
| 993 | return ret; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1000 | static int at91_pinctrl_probe(struct platform_device *pdev) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1001 | { |
| 1002 | struct at91_pinctrl *info; |
| 1003 | struct pinctrl_pin_desc *pdesc; |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 1004 | int ret, i, j, k; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1005 | |
| 1006 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 1007 | if (!info) |
| 1008 | return -ENOMEM; |
| 1009 | |
| 1010 | ret = at91_pinctrl_probe_dt(pdev, info); |
| 1011 | if (ret) |
| 1012 | return ret; |
| 1013 | |
| 1014 | /* |
| 1015 | * We need all the GPIO drivers to probe FIRST, or we will not be able |
| 1016 | * to obtain references to the struct gpio_chip * for them, and we |
| 1017 | * need this to proceed. |
| 1018 | */ |
| 1019 | for (i = 0; i < info->nbanks; i++) { |
| 1020 | if (!gpio_chips[i]) { |
| 1021 | dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i); |
| 1022 | devm_kfree(&pdev->dev, info); |
| 1023 | return -EPROBE_DEFER; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | at91_pinctrl_desc.name = dev_name(&pdev->dev); |
| 1028 | at91_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK; |
| 1029 | at91_pinctrl_desc.pins = pdesc = |
| 1030 | devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL); |
| 1031 | |
| 1032 | if (!at91_pinctrl_desc.pins) |
| 1033 | return -ENOMEM; |
| 1034 | |
| 1035 | for (i = 0 , k = 0; i < info->nbanks; i++) { |
| 1036 | for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) { |
| 1037 | pdesc->number = k; |
| 1038 | pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j); |
| 1039 | pdesc++; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | platform_set_drvdata(pdev, info); |
| 1044 | info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info); |
| 1045 | |
| 1046 | if (!info->pctl) { |
| 1047 | dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n"); |
| 1048 | ret = -EINVAL; |
| 1049 | goto err; |
| 1050 | } |
| 1051 | |
| 1052 | /* We will handle a range of GPIO pins */ |
| 1053 | for (i = 0; i < info->nbanks; i++) |
| 1054 | pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range); |
| 1055 | |
| 1056 | dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n"); |
| 1057 | |
| 1058 | return 0; |
| 1059 | |
| 1060 | err: |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1064 | static int at91_pinctrl_remove(struct platform_device *pdev) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1065 | { |
| 1066 | struct at91_pinctrl *info = platform_get_drvdata(pdev); |
| 1067 | |
| 1068 | pinctrl_unregister(info->pctl); |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | static int at91_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 1074 | { |
| 1075 | /* |
| 1076 | * Map back to global GPIO space and request muxing, the direction |
| 1077 | * parameter does not matter for this controller. |
| 1078 | */ |
| 1079 | int gpio = chip->base + offset; |
| 1080 | int bank = chip->base / chip->ngpio; |
| 1081 | |
| 1082 | dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__, |
| 1083 | 'A' + bank, offset, gpio); |
| 1084 | |
| 1085 | return pinctrl_request_gpio(gpio); |
| 1086 | } |
| 1087 | |
| 1088 | static void at91_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 1089 | { |
| 1090 | int gpio = chip->base + offset; |
| 1091 | |
| 1092 | pinctrl_free_gpio(gpio); |
| 1093 | } |
| 1094 | |
| 1095 | static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 1096 | { |
| 1097 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 1098 | void __iomem *pio = at91_gpio->regbase; |
| 1099 | unsigned mask = 1 << offset; |
| 1100 | |
| 1101 | writel_relaxed(mask, pio + PIO_ODR); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static int at91_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 1106 | { |
| 1107 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 1108 | void __iomem *pio = at91_gpio->regbase; |
| 1109 | unsigned mask = 1 << offset; |
| 1110 | u32 pdsr; |
| 1111 | |
| 1112 | pdsr = readl_relaxed(pio + PIO_PDSR); |
| 1113 | return (pdsr & mask) != 0; |
| 1114 | } |
| 1115 | |
| 1116 | static void at91_gpio_set(struct gpio_chip *chip, unsigned offset, |
| 1117 | int val) |
| 1118 | { |
| 1119 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 1120 | void __iomem *pio = at91_gpio->regbase; |
| 1121 | unsigned mask = 1 << offset; |
| 1122 | |
| 1123 | writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR)); |
| 1124 | } |
| 1125 | |
| 1126 | static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 1127 | int val) |
| 1128 | { |
| 1129 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 1130 | void __iomem *pio = at91_gpio->regbase; |
| 1131 | unsigned mask = 1 << offset; |
| 1132 | |
| 1133 | writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR)); |
| 1134 | writel_relaxed(mask, pio + PIO_OER); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | static int at91_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 1140 | { |
| 1141 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 1142 | int virq; |
| 1143 | |
| 1144 | if (offset < chip->ngpio) |
| 1145 | virq = irq_create_mapping(at91_gpio->domain, offset); |
| 1146 | else |
| 1147 | virq = -ENXIO; |
| 1148 | |
| 1149 | dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n", |
| 1150 | chip->label, offset + chip->base, virq); |
| 1151 | return virq; |
| 1152 | } |
| 1153 | |
| 1154 | #ifdef CONFIG_DEBUG_FS |
| 1155 | static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 1156 | { |
| 1157 | enum at91_mux mode; |
| 1158 | int i; |
| 1159 | struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip); |
| 1160 | void __iomem *pio = at91_gpio->regbase; |
| 1161 | |
| 1162 | for (i = 0; i < chip->ngpio; i++) { |
| 1163 | unsigned pin = chip->base + i; |
| 1164 | unsigned mask = pin_to_mask(pin); |
| 1165 | const char *gpio_label; |
| 1166 | u32 pdsr; |
| 1167 | |
| 1168 | gpio_label = gpiochip_is_requested(chip, i); |
| 1169 | if (!gpio_label) |
| 1170 | continue; |
| 1171 | mode = at91_gpio->ops->get_periph(pio, mask); |
| 1172 | seq_printf(s, "[%s] GPIO%s%d: ", |
| 1173 | gpio_label, chip->label, i); |
| 1174 | if (mode == AT91_MUX_GPIO) { |
| 1175 | pdsr = readl_relaxed(pio + PIO_PDSR); |
| 1176 | |
| 1177 | seq_printf(s, "[gpio] %s\n", |
| 1178 | pdsr & mask ? |
| 1179 | "set" : "clear"); |
| 1180 | } else { |
| 1181 | seq_printf(s, "[periph %c]\n", |
| 1182 | mode + 'A' - 1); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | #else |
| 1187 | #define at91_gpio_dbg_show NULL |
| 1188 | #endif |
| 1189 | |
| 1190 | /* Several AIC controller irqs are dispatched through this GPIO handler. |
| 1191 | * To use any AT91_PIN_* as an externally triggered IRQ, first call |
| 1192 | * at91_set_gpio_input() then maybe enable its glitch filter. |
| 1193 | * Then just request_irq() with the pin ID; it works like any ARM IRQ |
| 1194 | * handler. |
| 1195 | * First implementation always triggers on rising and falling edges |
| 1196 | * whereas the newer PIO3 can be additionally configured to trigger on |
| 1197 | * level, edge with any polarity. |
| 1198 | * |
| 1199 | * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after |
| 1200 | * configuring them with at91_set_a_periph() or at91_set_b_periph(). |
| 1201 | * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering. |
| 1202 | */ |
| 1203 | |
| 1204 | static void gpio_irq_mask(struct irq_data *d) |
| 1205 | { |
| 1206 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 1207 | void __iomem *pio = at91_gpio->regbase; |
| 1208 | unsigned mask = 1 << d->hwirq; |
| 1209 | |
| 1210 | if (pio) |
| 1211 | writel_relaxed(mask, pio + PIO_IDR); |
| 1212 | } |
| 1213 | |
| 1214 | static void gpio_irq_unmask(struct irq_data *d) |
| 1215 | { |
| 1216 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 1217 | void __iomem *pio = at91_gpio->regbase; |
| 1218 | unsigned mask = 1 << d->hwirq; |
| 1219 | |
| 1220 | if (pio) |
| 1221 | writel_relaxed(mask, pio + PIO_IER); |
| 1222 | } |
| 1223 | |
| 1224 | static int gpio_irq_type(struct irq_data *d, unsigned type) |
| 1225 | { |
| 1226 | switch (type) { |
| 1227 | case IRQ_TYPE_NONE: |
| 1228 | case IRQ_TYPE_EDGE_BOTH: |
| 1229 | return 0; |
| 1230 | default: |
| 1231 | return -EINVAL; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* Alternate irq type for PIO3 support */ |
| 1236 | static int alt_gpio_irq_type(struct irq_data *d, unsigned type) |
| 1237 | { |
| 1238 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 1239 | void __iomem *pio = at91_gpio->regbase; |
| 1240 | unsigned mask = 1 << d->hwirq; |
| 1241 | |
| 1242 | switch (type) { |
| 1243 | case IRQ_TYPE_EDGE_RISING: |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1244 | irq_set_handler(d->irq, handle_simple_irq); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1245 | writel_relaxed(mask, pio + PIO_ESR); |
| 1246 | writel_relaxed(mask, pio + PIO_REHLSR); |
| 1247 | break; |
| 1248 | case IRQ_TYPE_EDGE_FALLING: |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1249 | irq_set_handler(d->irq, handle_simple_irq); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1250 | writel_relaxed(mask, pio + PIO_ESR); |
| 1251 | writel_relaxed(mask, pio + PIO_FELLSR); |
| 1252 | break; |
| 1253 | case IRQ_TYPE_LEVEL_LOW: |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1254 | irq_set_handler(d->irq, handle_level_irq); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1255 | writel_relaxed(mask, pio + PIO_LSR); |
| 1256 | writel_relaxed(mask, pio + PIO_FELLSR); |
| 1257 | break; |
| 1258 | case IRQ_TYPE_LEVEL_HIGH: |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1259 | irq_set_handler(d->irq, handle_level_irq); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1260 | writel_relaxed(mask, pio + PIO_LSR); |
| 1261 | writel_relaxed(mask, pio + PIO_REHLSR); |
| 1262 | break; |
| 1263 | case IRQ_TYPE_EDGE_BOTH: |
| 1264 | /* |
| 1265 | * disable additional interrupt modes: |
| 1266 | * fall back to default behavior |
| 1267 | */ |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1268 | irq_set_handler(d->irq, handle_simple_irq); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1269 | writel_relaxed(mask, pio + PIO_AIMDR); |
| 1270 | return 0; |
| 1271 | case IRQ_TYPE_NONE: |
| 1272 | default: |
| 1273 | pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq)); |
| 1274 | return -EINVAL; |
| 1275 | } |
| 1276 | |
| 1277 | /* enable additional interrupt modes */ |
| 1278 | writel_relaxed(mask, pio + PIO_AIMER); |
| 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | #ifdef CONFIG_PM |
Ludovic Desroches | 647f8d9 | 2013-03-08 16:18:21 +0100 | [diff] [blame] | 1284 | |
| 1285 | static u32 wakeups[MAX_GPIO_BANKS]; |
| 1286 | static u32 backups[MAX_GPIO_BANKS]; |
| 1287 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1288 | static int gpio_irq_set_wake(struct irq_data *d, unsigned state) |
| 1289 | { |
| 1290 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d); |
| 1291 | unsigned bank = at91_gpio->pioc_idx; |
Ludovic Desroches | 647f8d9 | 2013-03-08 16:18:21 +0100 | [diff] [blame] | 1292 | unsigned mask = 1 << d->hwirq; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1293 | |
| 1294 | if (unlikely(bank >= MAX_GPIO_BANKS)) |
| 1295 | return -EINVAL; |
| 1296 | |
Ludovic Desroches | 647f8d9 | 2013-03-08 16:18:21 +0100 | [diff] [blame] | 1297 | if (state) |
| 1298 | wakeups[bank] |= mask; |
| 1299 | else |
| 1300 | wakeups[bank] &= ~mask; |
| 1301 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1302 | irq_set_irq_wake(at91_gpio->pioc_virq, state); |
| 1303 | |
| 1304 | return 0; |
| 1305 | } |
Ludovic Desroches | 647f8d9 | 2013-03-08 16:18:21 +0100 | [diff] [blame] | 1306 | |
| 1307 | void at91_pinctrl_gpio_suspend(void) |
| 1308 | { |
| 1309 | int i; |
| 1310 | |
| 1311 | for (i = 0; i < gpio_banks; i++) { |
| 1312 | void __iomem *pio; |
| 1313 | |
| 1314 | if (!gpio_chips[i]) |
| 1315 | continue; |
| 1316 | |
| 1317 | pio = gpio_chips[i]->regbase; |
| 1318 | |
| 1319 | backups[i] = __raw_readl(pio + PIO_IMR); |
| 1320 | __raw_writel(backups[i], pio + PIO_IDR); |
| 1321 | __raw_writel(wakeups[i], pio + PIO_IER); |
| 1322 | |
| 1323 | if (!wakeups[i]) { |
| 1324 | clk_unprepare(gpio_chips[i]->clock); |
| 1325 | clk_disable(gpio_chips[i]->clock); |
| 1326 | } else { |
| 1327 | printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", |
| 1328 | 'A'+i, wakeups[i]); |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | void at91_pinctrl_gpio_resume(void) |
| 1334 | { |
| 1335 | int i; |
| 1336 | |
| 1337 | for (i = 0; i < gpio_banks; i++) { |
| 1338 | void __iomem *pio; |
| 1339 | |
| 1340 | if (!gpio_chips[i]) |
| 1341 | continue; |
| 1342 | |
| 1343 | pio = gpio_chips[i]->regbase; |
| 1344 | |
| 1345 | if (!wakeups[i]) { |
| 1346 | if (clk_prepare(gpio_chips[i]->clock) == 0) |
| 1347 | clk_enable(gpio_chips[i]->clock); |
| 1348 | } |
| 1349 | |
| 1350 | __raw_writel(wakeups[i], pio + PIO_IDR); |
| 1351 | __raw_writel(backups[i], pio + PIO_IER); |
| 1352 | } |
| 1353 | } |
| 1354 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1355 | #else |
| 1356 | #define gpio_irq_set_wake NULL |
Ludovic Desroches | 647f8d9 | 2013-03-08 16:18:21 +0100 | [diff] [blame] | 1357 | #endif /* CONFIG_PM */ |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1358 | |
| 1359 | static struct irq_chip gpio_irqchip = { |
| 1360 | .name = "GPIO", |
| 1361 | .irq_disable = gpio_irq_mask, |
| 1362 | .irq_mask = gpio_irq_mask, |
| 1363 | .irq_unmask = gpio_irq_unmask, |
| 1364 | /* .irq_set_type is set dynamically */ |
| 1365 | .irq_set_wake = gpio_irq_set_wake, |
| 1366 | }; |
| 1367 | |
| 1368 | static void gpio_irq_handler(unsigned irq, struct irq_desc *desc) |
| 1369 | { |
| 1370 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 1371 | struct irq_data *idata = irq_desc_get_irq_data(desc); |
| 1372 | struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata); |
| 1373 | void __iomem *pio = at91_gpio->regbase; |
| 1374 | unsigned long isr; |
| 1375 | int n; |
| 1376 | |
| 1377 | chained_irq_enter(chip, desc); |
| 1378 | for (;;) { |
| 1379 | /* Reading ISR acks pending (edge triggered) GPIO interrupts. |
| 1380 | * When there none are pending, we're finished unless we need |
| 1381 | * to process multiple banks (like ID_PIOCDE on sam9263). |
| 1382 | */ |
| 1383 | isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR); |
| 1384 | if (!isr) { |
| 1385 | if (!at91_gpio->next) |
| 1386 | break; |
| 1387 | at91_gpio = at91_gpio->next; |
| 1388 | pio = at91_gpio->regbase; |
| 1389 | continue; |
| 1390 | } |
| 1391 | |
Wei Yongjun | 05daa16 | 2012-10-26 22:50:54 +0800 | [diff] [blame] | 1392 | for_each_set_bit(n, &isr, BITS_PER_LONG) { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1393 | generic_handle_irq(irq_find_mapping(at91_gpio->domain, n)); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | chained_irq_exit(chip, desc); |
| 1397 | /* now it may re-trigger */ |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * This lock class tells lockdep that GPIO irqs are in a different |
| 1402 | * category than their parents, so it won't report false recursion. |
| 1403 | */ |
| 1404 | static struct lock_class_key gpio_lock_class; |
| 1405 | |
| 1406 | static int at91_gpio_irq_map(struct irq_domain *h, unsigned int virq, |
| 1407 | irq_hw_number_t hw) |
| 1408 | { |
| 1409 | struct at91_gpio_chip *at91_gpio = h->host_data; |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1410 | void __iomem *pio = at91_gpio->regbase; |
| 1411 | u32 mask = 1 << hw; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1412 | |
| 1413 | irq_set_lockdep_class(virq, &gpio_lock_class); |
| 1414 | |
| 1415 | /* |
| 1416 | * Can use the "simple" and not "edge" handler since it's |
| 1417 | * shorter, and the AIC handles interrupts sanely. |
| 1418 | */ |
Boris BREZILLON | 99fce02 | 2013-07-20 16:51:33 +0200 | [diff] [blame^] | 1419 | irq_set_chip(virq, &gpio_irqchip); |
| 1420 | if ((at91_gpio->ops == &at91sam9x5_ops) && |
| 1421 | (readl_relaxed(pio + PIO_AIMMR) & mask) && |
| 1422 | (readl_relaxed(pio + PIO_ELSR) & mask)) |
| 1423 | irq_set_handler(virq, handle_level_irq); |
| 1424 | else |
| 1425 | irq_set_handler(virq, handle_simple_irq); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1426 | set_irq_flags(virq, IRQF_VALID); |
| 1427 | irq_set_chip_data(virq, at91_gpio); |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
Axel Lin | f6f94f6 | 2012-11-05 21:23:50 +0800 | [diff] [blame] | 1432 | static int at91_gpio_irq_domain_xlate(struct irq_domain *d, |
| 1433 | struct device_node *ctrlr, |
| 1434 | const u32 *intspec, unsigned int intsize, |
| 1435 | irq_hw_number_t *out_hwirq, |
| 1436 | unsigned int *out_type) |
Jean-Christophe PLAGNIOL-VILLARD | a728c7c | 2012-10-23 15:56:41 +0200 | [diff] [blame] | 1437 | { |
| 1438 | struct at91_gpio_chip *at91_gpio = d->host_data; |
| 1439 | int ret; |
| 1440 | int pin = at91_gpio->chip.base + intspec[0]; |
| 1441 | |
| 1442 | if (WARN_ON(intsize < 2)) |
| 1443 | return -EINVAL; |
| 1444 | *out_hwirq = intspec[0]; |
| 1445 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 1446 | |
| 1447 | ret = gpio_request(pin, ctrlr->full_name); |
| 1448 | if (ret) |
| 1449 | return ret; |
| 1450 | |
| 1451 | ret = gpio_direction_input(pin); |
| 1452 | if (ret) |
| 1453 | return ret; |
| 1454 | |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1458 | static struct irq_domain_ops at91_gpio_ops = { |
| 1459 | .map = at91_gpio_irq_map, |
Jean-Christophe PLAGNIOL-VILLARD | a728c7c | 2012-10-23 15:56:41 +0200 | [diff] [blame] | 1460 | .xlate = at91_gpio_irq_domain_xlate, |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1461 | }; |
| 1462 | |
| 1463 | static int at91_gpio_of_irq_setup(struct device_node *node, |
| 1464 | struct at91_gpio_chip *at91_gpio) |
| 1465 | { |
| 1466 | struct at91_gpio_chip *prev = NULL; |
| 1467 | struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq); |
| 1468 | |
| 1469 | at91_gpio->pioc_hwirq = irqd_to_hwirq(d); |
| 1470 | |
| 1471 | /* Setup proper .irq_set_type function */ |
| 1472 | gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type; |
| 1473 | |
| 1474 | /* Disable irqs of this PIO controller */ |
| 1475 | writel_relaxed(~0, at91_gpio->regbase + PIO_IDR); |
| 1476 | |
| 1477 | /* Setup irq domain */ |
| 1478 | at91_gpio->domain = irq_domain_add_linear(node, at91_gpio->chip.ngpio, |
| 1479 | &at91_gpio_ops, at91_gpio); |
| 1480 | if (!at91_gpio->domain) |
| 1481 | panic("at91_gpio.%d: couldn't allocate irq domain (DT).\n", |
| 1482 | at91_gpio->pioc_idx); |
| 1483 | |
| 1484 | /* Setup chained handler */ |
| 1485 | if (at91_gpio->pioc_idx) |
| 1486 | prev = gpio_chips[at91_gpio->pioc_idx - 1]; |
| 1487 | |
| 1488 | /* The toplevel handler handles one bank of GPIOs, except |
| 1489 | * on some SoC it can handles up to three... |
| 1490 | * We only set up the handler for the first of the list. |
| 1491 | */ |
| 1492 | if (prev && prev->next == at91_gpio) |
| 1493 | return 0; |
| 1494 | |
| 1495 | irq_set_chip_data(at91_gpio->pioc_virq, at91_gpio); |
| 1496 | irq_set_chained_handler(at91_gpio->pioc_virq, gpio_irq_handler); |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | /* This structure is replicated for each GPIO block allocated at probe time */ |
| 1502 | static struct gpio_chip at91_gpio_template = { |
| 1503 | .request = at91_gpio_request, |
| 1504 | .free = at91_gpio_free, |
| 1505 | .direction_input = at91_gpio_direction_input, |
| 1506 | .get = at91_gpio_get, |
| 1507 | .direction_output = at91_gpio_direction_output, |
| 1508 | .set = at91_gpio_set, |
| 1509 | .to_irq = at91_gpio_to_irq, |
| 1510 | .dbg_show = at91_gpio_dbg_show, |
| 1511 | .can_sleep = 0, |
| 1512 | .ngpio = MAX_NB_GPIO_PER_BANK, |
| 1513 | }; |
| 1514 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1515 | static void at91_gpio_probe_fixup(void) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1516 | { |
| 1517 | unsigned i; |
| 1518 | struct at91_gpio_chip *at91_gpio, *last = NULL; |
| 1519 | |
| 1520 | for (i = 0; i < gpio_banks; i++) { |
| 1521 | at91_gpio = gpio_chips[i]; |
| 1522 | |
| 1523 | /* |
| 1524 | * GPIO controller are grouped on some SoC: |
| 1525 | * PIOC, PIOD and PIOE can share the same IRQ line |
| 1526 | */ |
| 1527 | if (last && last->pioc_virq == at91_gpio->pioc_virq) |
| 1528 | last->next = at91_gpio; |
| 1529 | last = at91_gpio; |
| 1530 | } |
| 1531 | } |
| 1532 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1533 | static struct of_device_id at91_gpio_of_match[] = { |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1534 | { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, }, |
| 1535 | { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops }, |
| 1536 | { /* sentinel */ } |
| 1537 | }; |
| 1538 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1539 | static int at91_gpio_probe(struct platform_device *pdev) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1540 | { |
| 1541 | struct device_node *np = pdev->dev.of_node; |
| 1542 | struct resource *res; |
| 1543 | struct at91_gpio_chip *at91_chip = NULL; |
| 1544 | struct gpio_chip *chip; |
| 1545 | struct pinctrl_gpio_range *range; |
| 1546 | int ret = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 32b01a3 | 2012-11-07 00:33:34 +0800 | [diff] [blame] | 1547 | int irq, i; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1548 | int alias_idx = of_alias_get_id(np, "gpio"); |
| 1549 | uint32_t ngpio; |
Jean-Christophe PLAGNIOL-VILLARD | 32b01a3 | 2012-11-07 00:33:34 +0800 | [diff] [blame] | 1550 | char **names; |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1551 | |
| 1552 | BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips)); |
| 1553 | if (gpio_chips[alias_idx]) { |
| 1554 | ret = -EBUSY; |
| 1555 | goto err; |
| 1556 | } |
| 1557 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1558 | irq = platform_get_irq(pdev, 0); |
| 1559 | if (irq < 0) { |
| 1560 | ret = irq; |
| 1561 | goto err; |
| 1562 | } |
| 1563 | |
| 1564 | at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL); |
| 1565 | if (!at91_chip) { |
| 1566 | ret = -ENOMEM; |
| 1567 | goto err; |
| 1568 | } |
| 1569 | |
Wolfram Sang | f50b9e1 | 2013-05-10 10:17:03 +0200 | [diff] [blame] | 1570 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 9e0c1fb | 2013-01-21 11:09:14 +0100 | [diff] [blame] | 1571 | at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res); |
| 1572 | if (IS_ERR(at91_chip->regbase)) { |
| 1573 | ret = PTR_ERR(at91_chip->regbase); |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1574 | goto err; |
| 1575 | } |
| 1576 | |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 1577 | at91_chip->ops = (struct at91_pinctrl_mux_ops *) |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1578 | of_match_device(at91_gpio_of_match, &pdev->dev)->data; |
| 1579 | at91_chip->pioc_virq = irq; |
| 1580 | at91_chip->pioc_idx = alias_idx; |
| 1581 | |
| 1582 | at91_chip->clock = clk_get(&pdev->dev, NULL); |
| 1583 | if (IS_ERR(at91_chip->clock)) { |
| 1584 | dev_err(&pdev->dev, "failed to get clock, ignoring.\n"); |
| 1585 | goto err; |
| 1586 | } |
| 1587 | |
| 1588 | if (clk_prepare(at91_chip->clock)) |
| 1589 | goto clk_prep_err; |
| 1590 | |
| 1591 | /* enable PIO controller's clock */ |
| 1592 | if (clk_enable(at91_chip->clock)) { |
| 1593 | dev_err(&pdev->dev, "failed to enable clock, ignoring.\n"); |
| 1594 | goto clk_err; |
| 1595 | } |
| 1596 | |
| 1597 | at91_chip->chip = at91_gpio_template; |
| 1598 | |
| 1599 | chip = &at91_chip->chip; |
| 1600 | chip->of_node = np; |
| 1601 | chip->label = dev_name(&pdev->dev); |
| 1602 | chip->dev = &pdev->dev; |
| 1603 | chip->owner = THIS_MODULE; |
| 1604 | chip->base = alias_idx * MAX_NB_GPIO_PER_BANK; |
| 1605 | |
| 1606 | if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) { |
| 1607 | if (ngpio >= MAX_NB_GPIO_PER_BANK) |
| 1608 | pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n", |
| 1609 | alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK); |
| 1610 | else |
| 1611 | chip->ngpio = ngpio; |
| 1612 | } |
| 1613 | |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 1614 | names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio, |
| 1615 | GFP_KERNEL); |
Jean-Christophe PLAGNIOL-VILLARD | 32b01a3 | 2012-11-07 00:33:34 +0800 | [diff] [blame] | 1616 | |
| 1617 | if (!names) { |
| 1618 | ret = -ENOMEM; |
| 1619 | goto clk_err; |
| 1620 | } |
| 1621 | |
| 1622 | for (i = 0; i < chip->ngpio; i++) |
| 1623 | names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i); |
| 1624 | |
Sachin Kamat | 3c93600 | 2013-03-15 10:07:03 +0530 | [diff] [blame] | 1625 | chip->names = (const char *const *)names; |
Jean-Christophe PLAGNIOL-VILLARD | 32b01a3 | 2012-11-07 00:33:34 +0800 | [diff] [blame] | 1626 | |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1627 | range = &at91_chip->range; |
| 1628 | range->name = chip->label; |
| 1629 | range->id = alias_idx; |
| 1630 | range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK; |
| 1631 | |
| 1632 | range->npins = chip->ngpio; |
| 1633 | range->gc = chip; |
| 1634 | |
| 1635 | ret = gpiochip_add(chip); |
| 1636 | if (ret) |
| 1637 | goto clk_err; |
| 1638 | |
| 1639 | gpio_chips[alias_idx] = at91_chip; |
| 1640 | gpio_banks = max(gpio_banks, alias_idx + 1); |
| 1641 | |
| 1642 | at91_gpio_probe_fixup(); |
| 1643 | |
| 1644 | at91_gpio_of_irq_setup(np, at91_chip); |
| 1645 | |
| 1646 | dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase); |
| 1647 | |
| 1648 | return 0; |
| 1649 | |
| 1650 | clk_err: |
| 1651 | clk_unprepare(at91_chip->clock); |
| 1652 | clk_prep_err: |
| 1653 | clk_put(at91_chip->clock); |
| 1654 | err: |
| 1655 | dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx); |
| 1656 | |
| 1657 | return ret; |
| 1658 | } |
| 1659 | |
| 1660 | static struct platform_driver at91_gpio_driver = { |
| 1661 | .driver = { |
| 1662 | .name = "gpio-at91", |
| 1663 | .owner = THIS_MODULE, |
| 1664 | .of_match_table = of_match_ptr(at91_gpio_of_match), |
| 1665 | }, |
| 1666 | .probe = at91_gpio_probe, |
| 1667 | }; |
| 1668 | |
| 1669 | static struct platform_driver at91_pinctrl_driver = { |
| 1670 | .driver = { |
| 1671 | .name = "pinctrl-at91", |
| 1672 | .owner = THIS_MODULE, |
| 1673 | .of_match_table = of_match_ptr(at91_pinctrl_of_match), |
| 1674 | }, |
| 1675 | .probe = at91_pinctrl_probe, |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1676 | .remove = at91_pinctrl_remove, |
Jean-Christophe PLAGNIOL-VILLARD | 6732ae5 | 2012-07-12 23:35:02 +0800 | [diff] [blame] | 1677 | }; |
| 1678 | |
| 1679 | static int __init at91_pinctrl_init(void) |
| 1680 | { |
| 1681 | int ret; |
| 1682 | |
| 1683 | ret = platform_driver_register(&at91_gpio_driver); |
| 1684 | if (ret) |
| 1685 | return ret; |
| 1686 | return platform_driver_register(&at91_pinctrl_driver); |
| 1687 | } |
| 1688 | arch_initcall(at91_pinctrl_init); |
| 1689 | |
| 1690 | static void __exit at91_pinctrl_exit(void) |
| 1691 | { |
| 1692 | platform_driver_unregister(&at91_pinctrl_driver); |
| 1693 | } |
| 1694 | |
| 1695 | module_exit(at91_pinctrl_exit); |
| 1696 | MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>"); |
| 1697 | MODULE_DESCRIPTION("Atmel AT91 pinctrl driver"); |
| 1698 | MODULE_LICENSE("GPL v2"); |