blob: 7f77007163985762abc6110f282eec762bdc8a92 [file] [log] [blame]
Ray Juib64333c2015-03-09 13:45:00 -07001/*
2 * Copyright (C) 2014-2015 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
Pramod Kumarafc8c782015-11-19 09:22:17 +053013 * This file contains the Broadcom Iproc GPIO driver that supports 3
14 * GPIO controllers on Iproc including the ASIU GPIO controller, the
Ray Juib64333c2015-03-09 13:45:00 -070015 * chipCommonG GPIO controller, and the always-on GPIO controller. Basic
16 * PINCONF such as bias pull up/down, and drive strength are also supported
17 * in this driver.
18 *
Pramod Kumarafc8c782015-11-19 09:22:17 +053019 * It provides the functionality where pins from the GPIO can be
20 * individually muxed to GPIO function, if individual pad
21 * configuration is supported, through the interaction with respective
22 * SoCs IOMUX controller.
Ray Juib64333c2015-03-09 13:45:00 -070023 */
24
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
Linus Walleij69fd6ae2015-12-07 23:48:26 +010029#include <linux/gpio/driver.h>
Ray Juib64333c2015-03-09 13:45:00 -070030#include <linux/ioport.h>
31#include <linux/of_device.h>
32#include <linux/of_irq.h>
33#include <linux/pinctrl/pinctrl.h>
Ray Juib64333c2015-03-09 13:45:00 -070034#include <linux/pinctrl/pinconf.h>
35#include <linux/pinctrl/pinconf-generic.h>
36
37#include "../pinctrl-utils.h"
38
Pramod Kumarafc8c782015-11-19 09:22:17 +053039#define IPROC_GPIO_DATA_IN_OFFSET 0x00
40#define IPROC_GPIO_DATA_OUT_OFFSET 0x04
41#define IPROC_GPIO_OUT_EN_OFFSET 0x08
42#define IPROC_GPIO_INT_TYPE_OFFSET 0x0c
43#define IPROC_GPIO_INT_DE_OFFSET 0x10
44#define IPROC_GPIO_INT_EDGE_OFFSET 0x14
45#define IPROC_GPIO_INT_MSK_OFFSET 0x18
46#define IPROC_GPIO_INT_STAT_OFFSET 0x1c
47#define IPROC_GPIO_INT_MSTAT_OFFSET 0x20
48#define IPROC_GPIO_INT_CLR_OFFSET 0x24
49#define IPROC_GPIO_PAD_RES_OFFSET 0x34
50#define IPROC_GPIO_RES_EN_OFFSET 0x38
Ray Juib64333c2015-03-09 13:45:00 -070051
52/* drive strength control for ASIU GPIO */
Pramod Kumarafc8c782015-11-19 09:22:17 +053053#define IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET 0x58
Ray Juib64333c2015-03-09 13:45:00 -070054
55/* drive strength control for CCM/CRMU (AON) GPIO */
Pramod Kumarafc8c782015-11-19 09:22:17 +053056#define IPROC_GPIO_DRV0_CTRL_OFFSET 0x00
Ray Juib64333c2015-03-09 13:45:00 -070057
58#define GPIO_BANK_SIZE 0x200
59#define NGPIOS_PER_BANK 32
60#define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)
61
Pramod Kumarafc8c782015-11-19 09:22:17 +053062#define IPROC_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
63#define IPROC_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
Ray Juib64333c2015-03-09 13:45:00 -070064
65#define GPIO_DRV_STRENGTH_BIT_SHIFT 20
66#define GPIO_DRV_STRENGTH_BITS 3
67#define GPIO_DRV_STRENGTH_BIT_MASK ((1 << GPIO_DRV_STRENGTH_BITS) - 1)
68
Ray Juif58de3d2016-07-18 10:20:18 -070069enum iproc_pinconf_param {
70 IPROC_PINCONF_DRIVE_STRENGTH = 0,
71 IPROC_PINCONF_BIAS_DISABLE,
72 IPROC_PINCONF_BIAS_PULL_UP,
73 IPROC_PINCONF_BIAS_PULL_DOWN,
74 IPROC_PINCON_MAX,
75};
76
Ray Juib64333c2015-03-09 13:45:00 -070077/*
Pramod Kumarafc8c782015-11-19 09:22:17 +053078 * Iproc GPIO core
Ray Juib64333c2015-03-09 13:45:00 -070079 *
80 * @dev: pointer to device
Pramod Kumarafc8c782015-11-19 09:22:17 +053081 * @base: I/O register base for Iproc GPIO controller
82 * @io_ctrl: I/O register base for certain type of Iproc GPIO controller that
Ray Juib64333c2015-03-09 13:45:00 -070083 * has the PINCONF support implemented outside of the GPIO block
84 * @lock: lock to protect access to I/O registers
85 * @gc: GPIO chip
86 * @num_banks: number of GPIO banks, each bank supports up to 32 GPIOs
87 * @pinmux_is_supported: flag to indicate this GPIO controller contains pins
88 * that can be individually muxed to GPIO
Ray Juif58de3d2016-07-18 10:20:18 -070089 * @pinconf_disable: contains a list of PINCONF parameters that need to be
90 * disabled
91 * @nr_pinconf_disable: total number of PINCONF parameters that need to be
92 * disabled
Ray Juib64333c2015-03-09 13:45:00 -070093 * @pctl: pointer to pinctrl_dev
94 * @pctldesc: pinctrl descriptor
95 */
Pramod Kumarafc8c782015-11-19 09:22:17 +053096struct iproc_gpio {
Ray Juib64333c2015-03-09 13:45:00 -070097 struct device *dev;
98
99 void __iomem *base;
100 void __iomem *io_ctrl;
101
102 spinlock_t lock;
103
104 struct gpio_chip gc;
105 unsigned num_banks;
106
107 bool pinmux_is_supported;
108
Ray Juif58de3d2016-07-18 10:20:18 -0700109 enum pin_config_param *pinconf_disable;
110 unsigned int nr_pinconf_disable;
111
Ray Juib64333c2015-03-09 13:45:00 -0700112 struct pinctrl_dev *pctl;
113 struct pinctrl_desc pctldesc;
114};
115
Ray Juib64333c2015-03-09 13:45:00 -0700116/*
117 * Mapping from PINCONF pins to GPIO pins is 1-to-1
118 */
Pramod Kumarafc8c782015-11-19 09:22:17 +0530119static inline unsigned iproc_pin_to_gpio(unsigned pin)
Ray Juib64333c2015-03-09 13:45:00 -0700120{
121 return pin;
122}
123
124/**
Pramod Kumarafc8c782015-11-19 09:22:17 +0530125 * iproc_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
126 * Iproc GPIO register
Ray Juib64333c2015-03-09 13:45:00 -0700127 *
Pramod Kumarafc8c782015-11-19 09:22:17 +0530128 * @iproc_gpio: Iproc GPIO device
Ray Juib64333c2015-03-09 13:45:00 -0700129 * @reg: register offset
130 * @gpio: GPIO pin
131 * @set: set or clear
132 */
Pramod Kumarafc8c782015-11-19 09:22:17 +0530133static inline void iproc_set_bit(struct iproc_gpio *chip, unsigned int reg,
Ray Juib64333c2015-03-09 13:45:00 -0700134 unsigned gpio, bool set)
135{
Pramod Kumarafc8c782015-11-19 09:22:17 +0530136 unsigned int offset = IPROC_GPIO_REG(gpio, reg);
137 unsigned int shift = IPROC_GPIO_SHIFT(gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700138 u32 val;
139
140 val = readl(chip->base + offset);
141 if (set)
142 val |= BIT(shift);
143 else
144 val &= ~BIT(shift);
145 writel(val, chip->base + offset);
146}
147
Pramod Kumarafc8c782015-11-19 09:22:17 +0530148static inline bool iproc_get_bit(struct iproc_gpio *chip, unsigned int reg,
Ray Juib64333c2015-03-09 13:45:00 -0700149 unsigned gpio)
150{
Pramod Kumarafc8c782015-11-19 09:22:17 +0530151 unsigned int offset = IPROC_GPIO_REG(gpio, reg);
152 unsigned int shift = IPROC_GPIO_SHIFT(gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700153
154 return !!(readl(chip->base + offset) & BIT(shift));
155}
156
Pramod Kumarafc8c782015-11-19 09:22:17 +0530157static void iproc_gpio_irq_handler(struct irq_desc *desc)
Ray Juib64333c2015-03-09 13:45:00 -0700158{
159 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100160 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700161 struct irq_chip *irq_chip = irq_desc_get_chip(desc);
162 int i, bit;
163
164 chained_irq_enter(irq_chip, desc);
165
166 /* go through the entire GPIO banks and handle all interrupts */
167 for (i = 0; i < chip->num_banks; i++) {
168 unsigned long val = readl(chip->base + (i * GPIO_BANK_SIZE) +
Pramod Kumarafc8c782015-11-19 09:22:17 +0530169 IPROC_GPIO_INT_MSTAT_OFFSET);
Ray Juib64333c2015-03-09 13:45:00 -0700170
171 for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
172 unsigned pin = NGPIOS_PER_BANK * i + bit;
173 int child_irq = irq_find_mapping(gc->irqdomain, pin);
174
175 /*
176 * Clear the interrupt before invoking the
177 * handler, so we do not leave any window
178 */
179 writel(BIT(bit), chip->base + (i * GPIO_BANK_SIZE) +
Pramod Kumarafc8c782015-11-19 09:22:17 +0530180 IPROC_GPIO_INT_CLR_OFFSET);
Ray Juib64333c2015-03-09 13:45:00 -0700181
182 generic_handle_irq(child_irq);
183 }
184 }
185
186 chained_irq_exit(irq_chip, desc);
187}
188
189
Pramod Kumarafc8c782015-11-19 09:22:17 +0530190static void iproc_gpio_irq_ack(struct irq_data *d)
Ray Juib64333c2015-03-09 13:45:00 -0700191{
192 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100193 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700194 unsigned gpio = d->hwirq;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530195 unsigned int offset = IPROC_GPIO_REG(gpio,
196 IPROC_GPIO_INT_CLR_OFFSET);
197 unsigned int shift = IPROC_GPIO_SHIFT(gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700198 u32 val = BIT(shift);
199
200 writel(val, chip->base + offset);
201}
202
203/**
Pramod Kumarafc8c782015-11-19 09:22:17 +0530204 * iproc_gpio_irq_set_mask - mask/unmask a GPIO interrupt
Ray Juib64333c2015-03-09 13:45:00 -0700205 *
206 * @d: IRQ chip data
207 * @unmask: mask/unmask GPIO interrupt
208 */
Pramod Kumarafc8c782015-11-19 09:22:17 +0530209static void iproc_gpio_irq_set_mask(struct irq_data *d, bool unmask)
Ray Juib64333c2015-03-09 13:45:00 -0700210{
211 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100212 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700213 unsigned gpio = d->hwirq;
214
Pramod Kumarafc8c782015-11-19 09:22:17 +0530215 iproc_set_bit(chip, IPROC_GPIO_INT_MSK_OFFSET, gpio, unmask);
Ray Juib64333c2015-03-09 13:45:00 -0700216}
217
Pramod Kumarafc8c782015-11-19 09:22:17 +0530218static void iproc_gpio_irq_mask(struct irq_data *d)
Ray Juib64333c2015-03-09 13:45:00 -0700219{
220 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100221 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700222 unsigned long flags;
223
224 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530225 iproc_gpio_irq_set_mask(d, false);
Ray Juib64333c2015-03-09 13:45:00 -0700226 spin_unlock_irqrestore(&chip->lock, flags);
227}
228
Pramod Kumarafc8c782015-11-19 09:22:17 +0530229static void iproc_gpio_irq_unmask(struct irq_data *d)
Ray Juib64333c2015-03-09 13:45:00 -0700230{
231 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100232 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700233 unsigned long flags;
234
235 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530236 iproc_gpio_irq_set_mask(d, true);
Ray Juib64333c2015-03-09 13:45:00 -0700237 spin_unlock_irqrestore(&chip->lock, flags);
238}
239
Pramod Kumarafc8c782015-11-19 09:22:17 +0530240static int iproc_gpio_irq_set_type(struct irq_data *d, unsigned int type)
Ray Juib64333c2015-03-09 13:45:00 -0700241{
242 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100243 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700244 unsigned gpio = d->hwirq;
245 bool level_triggered = false;
246 bool dual_edge = false;
247 bool rising_or_high = false;
248 unsigned long flags;
249
250 switch (type & IRQ_TYPE_SENSE_MASK) {
251 case IRQ_TYPE_EDGE_RISING:
252 rising_or_high = true;
253 break;
254
255 case IRQ_TYPE_EDGE_FALLING:
256 break;
257
258 case IRQ_TYPE_EDGE_BOTH:
259 dual_edge = true;
260 break;
261
262 case IRQ_TYPE_LEVEL_HIGH:
263 level_triggered = true;
264 rising_or_high = true;
265 break;
266
267 case IRQ_TYPE_LEVEL_LOW:
268 level_triggered = true;
269 break;
270
271 default:
272 dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
273 type);
274 return -EINVAL;
275 }
276
277 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530278 iproc_set_bit(chip, IPROC_GPIO_INT_TYPE_OFFSET, gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700279 level_triggered);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530280 iproc_set_bit(chip, IPROC_GPIO_INT_DE_OFFSET, gpio, dual_edge);
281 iproc_set_bit(chip, IPROC_GPIO_INT_EDGE_OFFSET, gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700282 rising_or_high);
283 spin_unlock_irqrestore(&chip->lock, flags);
284
285 dev_dbg(chip->dev,
286 "gpio:%u level_triggered:%d dual_edge:%d rising_or_high:%d\n",
287 gpio, level_triggered, dual_edge, rising_or_high);
288
289 return 0;
290}
291
Pramod Kumarafc8c782015-11-19 09:22:17 +0530292static struct irq_chip iproc_gpio_irq_chip = {
293 .name = "bcm-iproc-gpio",
294 .irq_ack = iproc_gpio_irq_ack,
295 .irq_mask = iproc_gpio_irq_mask,
296 .irq_unmask = iproc_gpio_irq_unmask,
297 .irq_set_type = iproc_gpio_irq_set_type,
Ray Juib64333c2015-03-09 13:45:00 -0700298};
299
300/*
Pramod Kumarafc8c782015-11-19 09:22:17 +0530301 * Request the Iproc IOMUX pinmux controller to mux individual pins to GPIO
Ray Juib64333c2015-03-09 13:45:00 -0700302 */
Pramod Kumarafc8c782015-11-19 09:22:17 +0530303static int iproc_gpio_request(struct gpio_chip *gc, unsigned offset)
Ray Juib64333c2015-03-09 13:45:00 -0700304{
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100305 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700306 unsigned gpio = gc->base + offset;
307
Pramod Kumarafc8c782015-11-19 09:22:17 +0530308 /* not all Iproc GPIO pins can be muxed individually */
Ray Juib64333c2015-03-09 13:45:00 -0700309 if (!chip->pinmux_is_supported)
310 return 0;
311
312 return pinctrl_request_gpio(gpio);
313}
314
Pramod Kumarafc8c782015-11-19 09:22:17 +0530315static void iproc_gpio_free(struct gpio_chip *gc, unsigned offset)
Ray Juib64333c2015-03-09 13:45:00 -0700316{
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100317 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700318 unsigned gpio = gc->base + offset;
319
320 if (!chip->pinmux_is_supported)
321 return;
322
323 pinctrl_free_gpio(gpio);
324}
325
Pramod Kumarafc8c782015-11-19 09:22:17 +0530326static int iproc_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
Ray Juib64333c2015-03-09 13:45:00 -0700327{
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100328 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700329 unsigned long flags;
330
331 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530332 iproc_set_bit(chip, IPROC_GPIO_OUT_EN_OFFSET, gpio, false);
Ray Juib64333c2015-03-09 13:45:00 -0700333 spin_unlock_irqrestore(&chip->lock, flags);
334
335 dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
336
337 return 0;
338}
339
Pramod Kumarafc8c782015-11-19 09:22:17 +0530340static int iproc_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700341 int val)
342{
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100343 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700344 unsigned long flags;
345
346 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530347 iproc_set_bit(chip, IPROC_GPIO_OUT_EN_OFFSET, gpio, true);
348 iproc_set_bit(chip, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
Ray Juib64333c2015-03-09 13:45:00 -0700349 spin_unlock_irqrestore(&chip->lock, flags);
350
351 dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
352
353 return 0;
354}
355
Pramod Kumarafc8c782015-11-19 09:22:17 +0530356static void iproc_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
Ray Juib64333c2015-03-09 13:45:00 -0700357{
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100358 struct iproc_gpio *chip = gpiochip_get_data(gc);
Ray Juib64333c2015-03-09 13:45:00 -0700359 unsigned long flags;
360
361 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530362 iproc_set_bit(chip, IPROC_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
Ray Juib64333c2015-03-09 13:45:00 -0700363 spin_unlock_irqrestore(&chip->lock, flags);
364
365 dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
366}
367
Pramod Kumarafc8c782015-11-19 09:22:17 +0530368static int iproc_gpio_get(struct gpio_chip *gc, unsigned gpio)
Ray Juib64333c2015-03-09 13:45:00 -0700369{
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100370 struct iproc_gpio *chip = gpiochip_get_data(gc);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530371 unsigned int offset = IPROC_GPIO_REG(gpio,
372 IPROC_GPIO_DATA_IN_OFFSET);
373 unsigned int shift = IPROC_GPIO_SHIFT(gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700374
375 return !!(readl(chip->base + offset) & BIT(shift));
376}
377
Ray Juif58de3d2016-07-18 10:20:18 -0700378/*
379 * Mapping of the iProc PINCONF parameters to the generic pin configuration
380 * parameters
381 */
382static const enum pin_config_param iproc_pinconf_disable_map[] = {
383 [IPROC_PINCONF_DRIVE_STRENGTH] = PIN_CONFIG_DRIVE_STRENGTH,
384 [IPROC_PINCONF_BIAS_DISABLE] = PIN_CONFIG_BIAS_DISABLE,
385 [IPROC_PINCONF_BIAS_PULL_UP] = PIN_CONFIG_BIAS_PULL_UP,
386 [IPROC_PINCONF_BIAS_PULL_DOWN] = PIN_CONFIG_BIAS_PULL_DOWN,
387};
388
389static bool iproc_pinconf_param_is_disabled(struct iproc_gpio *chip,
390 enum pin_config_param param)
391{
392 unsigned int i;
393
394 if (!chip->nr_pinconf_disable)
395 return false;
396
397 for (i = 0; i < chip->nr_pinconf_disable; i++)
398 if (chip->pinconf_disable[i] == param)
399 return true;
400
401 return false;
402}
403
404static int iproc_pinconf_disable_map_create(struct iproc_gpio *chip,
405 unsigned long disable_mask)
406{
407 unsigned int map_size = ARRAY_SIZE(iproc_pinconf_disable_map);
408 unsigned int bit, nbits = 0;
409
410 /* figure out total number of PINCONF parameters to disable */
411 for_each_set_bit(bit, &disable_mask, map_size)
412 nbits++;
413
414 if (!nbits)
415 return 0;
416
417 /*
418 * Allocate an array to store PINCONF parameters that need to be
419 * disabled
420 */
421 chip->pinconf_disable = devm_kcalloc(chip->dev, nbits,
422 sizeof(*chip->pinconf_disable),
423 GFP_KERNEL);
424 if (!chip->pinconf_disable)
425 return -ENOMEM;
426
427 chip->nr_pinconf_disable = nbits;
428
429 /* now store these parameters */
430 nbits = 0;
431 for_each_set_bit(bit, &disable_mask, map_size)
432 chip->pinconf_disable[nbits++] = iproc_pinconf_disable_map[bit];
433
434 return 0;
435}
436
Pramod Kumarafc8c782015-11-19 09:22:17 +0530437static int iproc_get_groups_count(struct pinctrl_dev *pctldev)
Ray Juib64333c2015-03-09 13:45:00 -0700438{
439 return 1;
440}
441
442/*
443 * Only one group: "gpio_grp", since this local pinctrl device only performs
444 * GPIO specific PINCONF configurations
445 */
Pramod Kumarafc8c782015-11-19 09:22:17 +0530446static const char *iproc_get_group_name(struct pinctrl_dev *pctldev,
Ray Juib64333c2015-03-09 13:45:00 -0700447 unsigned selector)
448{
449 return "gpio_grp";
450}
451
Pramod Kumarafc8c782015-11-19 09:22:17 +0530452static const struct pinctrl_ops iproc_pctrl_ops = {
453 .get_groups_count = iproc_get_groups_count,
454 .get_group_name = iproc_get_group_name,
Ray Juib64333c2015-03-09 13:45:00 -0700455 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300456 .dt_free_map = pinctrl_utils_free_map,
Ray Juib64333c2015-03-09 13:45:00 -0700457};
458
Pramod Kumarafc8c782015-11-19 09:22:17 +0530459static int iproc_gpio_set_pull(struct iproc_gpio *chip, unsigned gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700460 bool disable, bool pull_up)
461{
462 unsigned long flags;
463
464 spin_lock_irqsave(&chip->lock, flags);
465
466 if (disable) {
Pramod Kumarafc8c782015-11-19 09:22:17 +0530467 iproc_set_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio, false);
Ray Juib64333c2015-03-09 13:45:00 -0700468 } else {
Pramod Kumarafc8c782015-11-19 09:22:17 +0530469 iproc_set_bit(chip, IPROC_GPIO_PAD_RES_OFFSET, gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700470 pull_up);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530471 iproc_set_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio, true);
Ray Juib64333c2015-03-09 13:45:00 -0700472 }
473
474 spin_unlock_irqrestore(&chip->lock, flags);
475
476 dev_dbg(chip->dev, "gpio:%u set pullup:%d\n", gpio, pull_up);
477
478 return 0;
479}
480
Pramod Kumarafc8c782015-11-19 09:22:17 +0530481static void iproc_gpio_get_pull(struct iproc_gpio *chip, unsigned gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700482 bool *disable, bool *pull_up)
483{
484 unsigned long flags;
485
486 spin_lock_irqsave(&chip->lock, flags);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530487 *disable = !iproc_get_bit(chip, IPROC_GPIO_RES_EN_OFFSET, gpio);
488 *pull_up = iproc_get_bit(chip, IPROC_GPIO_PAD_RES_OFFSET, gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700489 spin_unlock_irqrestore(&chip->lock, flags);
490}
491
Pramod Kumarafc8c782015-11-19 09:22:17 +0530492static int iproc_gpio_set_strength(struct iproc_gpio *chip, unsigned gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700493 unsigned strength)
494{
495 void __iomem *base;
496 unsigned int i, offset, shift;
497 u32 val;
498 unsigned long flags;
499
500 /* make sure drive strength is supported */
501 if (strength < 2 || strength > 16 || (strength % 2))
502 return -ENOTSUPP;
503
504 if (chip->io_ctrl) {
505 base = chip->io_ctrl;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530506 offset = IPROC_GPIO_DRV0_CTRL_OFFSET;
Ray Juib64333c2015-03-09 13:45:00 -0700507 } else {
508 base = chip->base;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530509 offset = IPROC_GPIO_REG(gpio,
510 IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET);
Ray Juib64333c2015-03-09 13:45:00 -0700511 }
512
Pramod Kumarafc8c782015-11-19 09:22:17 +0530513 shift = IPROC_GPIO_SHIFT(gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700514
515 dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
516 strength);
517
518 spin_lock_irqsave(&chip->lock, flags);
519 strength = (strength / 2) - 1;
520 for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
521 val = readl(base + offset);
522 val &= ~BIT(shift);
523 val |= ((strength >> i) & 0x1) << shift;
524 writel(val, base + offset);
525 offset += 4;
526 }
527 spin_unlock_irqrestore(&chip->lock, flags);
528
529 return 0;
530}
531
Pramod Kumarafc8c782015-11-19 09:22:17 +0530532static int iproc_gpio_get_strength(struct iproc_gpio *chip, unsigned gpio,
Ray Juib64333c2015-03-09 13:45:00 -0700533 u16 *strength)
534{
535 void __iomem *base;
536 unsigned int i, offset, shift;
537 u32 val;
538 unsigned long flags;
539
540 if (chip->io_ctrl) {
541 base = chip->io_ctrl;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530542 offset = IPROC_GPIO_DRV0_CTRL_OFFSET;
Ray Juib64333c2015-03-09 13:45:00 -0700543 } else {
544 base = chip->base;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530545 offset = IPROC_GPIO_REG(gpio,
546 IPROC_GPIO_ASIU_DRV0_CTRL_OFFSET);
Ray Juib64333c2015-03-09 13:45:00 -0700547 }
548
Pramod Kumarafc8c782015-11-19 09:22:17 +0530549 shift = IPROC_GPIO_SHIFT(gpio);
Ray Juib64333c2015-03-09 13:45:00 -0700550
551 spin_lock_irqsave(&chip->lock, flags);
552 *strength = 0;
553 for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
554 val = readl(base + offset) & BIT(shift);
555 val >>= shift;
556 *strength += (val << i);
557 offset += 4;
558 }
559
560 /* convert to mA */
561 *strength = (*strength + 1) * 2;
562 spin_unlock_irqrestore(&chip->lock, flags);
563
564 return 0;
565}
566
Pramod Kumarafc8c782015-11-19 09:22:17 +0530567static int iproc_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
Ray Juib64333c2015-03-09 13:45:00 -0700568 unsigned long *config)
569{
Pramod Kumarafc8c782015-11-19 09:22:17 +0530570 struct iproc_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
Ray Juib64333c2015-03-09 13:45:00 -0700571 enum pin_config_param param = pinconf_to_config_param(*config);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530572 unsigned gpio = iproc_pin_to_gpio(pin);
Ray Juib64333c2015-03-09 13:45:00 -0700573 u16 arg;
574 bool disable, pull_up;
575 int ret;
576
Ray Juif58de3d2016-07-18 10:20:18 -0700577 if (iproc_pinconf_param_is_disabled(chip, param))
578 return -ENOTSUPP;
579
Ray Juib64333c2015-03-09 13:45:00 -0700580 switch (param) {
581 case PIN_CONFIG_BIAS_DISABLE:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530582 iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
Ray Juib64333c2015-03-09 13:45:00 -0700583 if (disable)
584 return 0;
585 else
586 return -EINVAL;
587
588 case PIN_CONFIG_BIAS_PULL_UP:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530589 iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
Ray Juib64333c2015-03-09 13:45:00 -0700590 if (!disable && pull_up)
591 return 0;
592 else
593 return -EINVAL;
594
595 case PIN_CONFIG_BIAS_PULL_DOWN:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530596 iproc_gpio_get_pull(chip, gpio, &disable, &pull_up);
Ray Juib64333c2015-03-09 13:45:00 -0700597 if (!disable && !pull_up)
598 return 0;
599 else
600 return -EINVAL;
601
602 case PIN_CONFIG_DRIVE_STRENGTH:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530603 ret = iproc_gpio_get_strength(chip, gpio, &arg);
Ray Juib64333c2015-03-09 13:45:00 -0700604 if (ret)
605 return ret;
Pramod Kumar616043d2015-11-19 09:22:19 +0530606 *config = pinconf_to_config_packed(param, arg);
Ray Juib64333c2015-03-09 13:45:00 -0700607
608 return 0;
609
610 default:
611 return -ENOTSUPP;
612 }
613
614 return -ENOTSUPP;
615}
616
Pramod Kumarafc8c782015-11-19 09:22:17 +0530617static int iproc_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
Ray Juib64333c2015-03-09 13:45:00 -0700618 unsigned long *configs, unsigned num_configs)
619{
Pramod Kumarafc8c782015-11-19 09:22:17 +0530620 struct iproc_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
Ray Juib64333c2015-03-09 13:45:00 -0700621 enum pin_config_param param;
622 u16 arg;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530623 unsigned i, gpio = iproc_pin_to_gpio(pin);
Ray Juib64333c2015-03-09 13:45:00 -0700624 int ret = -ENOTSUPP;
625
626 for (i = 0; i < num_configs; i++) {
627 param = pinconf_to_config_param(configs[i]);
Ray Juif58de3d2016-07-18 10:20:18 -0700628
629 if (iproc_pinconf_param_is_disabled(chip, param))
630 return -ENOTSUPP;
631
Ray Juib64333c2015-03-09 13:45:00 -0700632 arg = pinconf_to_config_argument(configs[i]);
633
634 switch (param) {
635 case PIN_CONFIG_BIAS_DISABLE:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530636 ret = iproc_gpio_set_pull(chip, gpio, true, false);
Ray Juib64333c2015-03-09 13:45:00 -0700637 if (ret < 0)
638 goto out;
639 break;
640
641 case PIN_CONFIG_BIAS_PULL_UP:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530642 ret = iproc_gpio_set_pull(chip, gpio, false, true);
Ray Juib64333c2015-03-09 13:45:00 -0700643 if (ret < 0)
644 goto out;
645 break;
646
647 case PIN_CONFIG_BIAS_PULL_DOWN:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530648 ret = iproc_gpio_set_pull(chip, gpio, false, false);
Ray Juib64333c2015-03-09 13:45:00 -0700649 if (ret < 0)
650 goto out;
651 break;
652
653 case PIN_CONFIG_DRIVE_STRENGTH:
Pramod Kumarafc8c782015-11-19 09:22:17 +0530654 ret = iproc_gpio_set_strength(chip, gpio, arg);
Ray Juib64333c2015-03-09 13:45:00 -0700655 if (ret < 0)
656 goto out;
657 break;
658
659 default:
660 dev_err(chip->dev, "invalid configuration\n");
661 return -ENOTSUPP;
662 }
663 } /* for each config */
664
665out:
666 return ret;
667}
668
Pramod Kumarafc8c782015-11-19 09:22:17 +0530669static const struct pinconf_ops iproc_pconf_ops = {
Ray Juib64333c2015-03-09 13:45:00 -0700670 .is_generic = true,
Pramod Kumarafc8c782015-11-19 09:22:17 +0530671 .pin_config_get = iproc_pin_config_get,
672 .pin_config_set = iproc_pin_config_set,
Ray Juib64333c2015-03-09 13:45:00 -0700673};
674
675/*
Pramod Kumarafc8c782015-11-19 09:22:17 +0530676 * Iproc GPIO controller supports some PINCONF related configurations such as
Ray Juib64333c2015-03-09 13:45:00 -0700677 * pull up, pull down, and drive strength, when the pin is configured to GPIO
678 *
679 * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
680 * local GPIO pins
681 */
Pramod Kumarafc8c782015-11-19 09:22:17 +0530682static int iproc_gpio_register_pinconf(struct iproc_gpio *chip)
Ray Juib64333c2015-03-09 13:45:00 -0700683{
684 struct pinctrl_desc *pctldesc = &chip->pctldesc;
685 struct pinctrl_pin_desc *pins;
686 struct gpio_chip *gc = &chip->gc;
687 int i;
688
689 pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
690 if (!pins)
691 return -ENOMEM;
692
693 for (i = 0; i < gc->ngpio; i++) {
694 pins[i].number = i;
695 pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
696 "gpio-%d", i);
697 if (!pins[i].name)
698 return -ENOMEM;
699 }
700
701 pctldesc->name = dev_name(chip->dev);
Pramod Kumarafc8c782015-11-19 09:22:17 +0530702 pctldesc->pctlops = &iproc_pctrl_ops;
Ray Juib64333c2015-03-09 13:45:00 -0700703 pctldesc->pins = pins;
704 pctldesc->npins = gc->ngpio;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530705 pctldesc->confops = &iproc_pconf_ops;
Ray Juib64333c2015-03-09 13:45:00 -0700706
Laxman Dewanganee17e042016-02-24 14:44:07 +0530707 chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900708 if (IS_ERR(chip->pctl)) {
Ray Juib64333c2015-03-09 13:45:00 -0700709 dev_err(chip->dev, "unable to register pinctrl device\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900710 return PTR_ERR(chip->pctl);
Ray Juib64333c2015-03-09 13:45:00 -0700711 }
712
713 return 0;
714}
715
Pramod Kumarafc8c782015-11-19 09:22:17 +0530716static const struct of_device_id iproc_gpio_of_match[] = {
Ray Juif58de3d2016-07-18 10:20:18 -0700717 { .compatible = "brcm,iproc-gpio" },
Pramod Kumare1aaaf32015-11-19 09:22:15 +0530718 { .compatible = "brcm,cygnus-ccm-gpio" },
719 { .compatible = "brcm,cygnus-asiu-gpio" },
720 { .compatible = "brcm,cygnus-crmu-gpio" },
Ray Juif58de3d2016-07-18 10:20:18 -0700721 { .compatible = "brcm,iproc-nsp-gpio" },
722 { .compatible = "brcm,iproc-stingray-gpio" },
723 { /* sentinel */ }
Ray Juib64333c2015-03-09 13:45:00 -0700724};
725
Pramod Kumarafc8c782015-11-19 09:22:17 +0530726static int iproc_gpio_probe(struct platform_device *pdev)
Ray Juib64333c2015-03-09 13:45:00 -0700727{
728 struct device *dev = &pdev->dev;
729 struct resource *res;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530730 struct iproc_gpio *chip;
Ray Juib64333c2015-03-09 13:45:00 -0700731 struct gpio_chip *gc;
Ray Juif58de3d2016-07-18 10:20:18 -0700732 u32 ngpios, pinconf_disable_mask = 0;
Ray Juib64333c2015-03-09 13:45:00 -0700733 int irq, ret;
Ray Juif58de3d2016-07-18 10:20:18 -0700734 bool no_pinconf = false;
735
736 /* NSP does not support drive strength config */
737 if (of_device_is_compatible(dev->of_node, "brcm,iproc-nsp-gpio"))
738 pinconf_disable_mask = BIT(IPROC_PINCONF_DRIVE_STRENGTH);
739 /* Stingray does not support pinconf in this controller */
740 else if (of_device_is_compatible(dev->of_node,
741 "brcm,iproc-stingray-gpio"))
742 no_pinconf = true;
Ray Juib64333c2015-03-09 13:45:00 -0700743
744 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
745 if (!chip)
746 return -ENOMEM;
747
748 chip->dev = dev;
749 platform_set_drvdata(pdev, chip);
750
751 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
752 chip->base = devm_ioremap_resource(dev, res);
753 if (IS_ERR(chip->base)) {
754 dev_err(dev, "unable to map I/O memory\n");
755 return PTR_ERR(chip->base);
756 }
757
758 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
759 if (res) {
760 chip->io_ctrl = devm_ioremap_resource(dev, res);
761 if (IS_ERR(chip->io_ctrl)) {
762 dev_err(dev, "unable to map I/O memory\n");
763 return PTR_ERR(chip->io_ctrl);
764 }
765 }
766
Pramod Kumare1aaaf32015-11-19 09:22:15 +0530767 if (of_property_read_u32(dev->of_node, "ngpios", &ngpios)) {
768 dev_err(&pdev->dev, "missing ngpios DT property\n");
769 return -ENODEV;
770 }
771
Ray Juib64333c2015-03-09 13:45:00 -0700772 spin_lock_init(&chip->lock);
773
774 gc = &chip->gc;
775 gc->base = -1;
776 gc->ngpio = ngpios;
777 chip->num_banks = (ngpios + NGPIOS_PER_BANK - 1) / NGPIOS_PER_BANK;
778 gc->label = dev_name(dev);
Linus Torvalds58cf279a2016-01-17 12:32:01 -0800779 gc->parent = dev;
Ray Juib64333c2015-03-09 13:45:00 -0700780 gc->of_node = dev->of_node;
Pramod Kumarafc8c782015-11-19 09:22:17 +0530781 gc->request = iproc_gpio_request;
782 gc->free = iproc_gpio_free;
783 gc->direction_input = iproc_gpio_direction_input;
784 gc->direction_output = iproc_gpio_direction_output;
785 gc->set = iproc_gpio_set;
786 gc->get = iproc_gpio_get;
Ray Juib64333c2015-03-09 13:45:00 -0700787
Pramod Kumarea922112015-10-19 11:13:09 +0530788 chip->pinmux_is_supported = of_property_read_bool(dev->of_node,
789 "gpio-ranges");
790
Linus Walleij69fd6ae2015-12-07 23:48:26 +0100791 ret = gpiochip_add_data(gc, chip);
Ray Juib64333c2015-03-09 13:45:00 -0700792 if (ret < 0) {
793 dev_err(dev, "unable to add GPIO chip\n");
794 return ret;
795 }
796
Ray Juif58de3d2016-07-18 10:20:18 -0700797 if (!no_pinconf) {
798 ret = iproc_gpio_register_pinconf(chip);
799 if (ret) {
800 dev_err(dev, "unable to register pinconf\n");
801 goto err_rm_gpiochip;
802 }
803
804 if (pinconf_disable_mask) {
805 ret = iproc_pinconf_disable_map_create(chip,
806 pinconf_disable_mask);
807 if (ret) {
808 dev_err(dev,
809 "unable to create pinconf disable map\n");
810 goto err_rm_gpiochip;
811 }
812 }
Ray Juib64333c2015-03-09 13:45:00 -0700813 }
814
815 /* optional GPIO interrupt support */
816 irq = platform_get_irq(pdev, 0);
817 if (irq) {
Pramod Kumarafc8c782015-11-19 09:22:17 +0530818 ret = gpiochip_irqchip_add(gc, &iproc_gpio_irq_chip, 0,
Ray Juib64333c2015-03-09 13:45:00 -0700819 handle_simple_irq, IRQ_TYPE_NONE);
820 if (ret) {
821 dev_err(dev, "no GPIO irqchip\n");
Laxman Dewanganee17e042016-02-24 14:44:07 +0530822 goto err_rm_gpiochip;
Ray Juib64333c2015-03-09 13:45:00 -0700823 }
824
Pramod Kumarafc8c782015-11-19 09:22:17 +0530825 gpiochip_set_chained_irqchip(gc, &iproc_gpio_irq_chip, irq,
826 iproc_gpio_irq_handler);
Ray Juib64333c2015-03-09 13:45:00 -0700827 }
828
829 return 0;
830
Ray Juib64333c2015-03-09 13:45:00 -0700831err_rm_gpiochip:
832 gpiochip_remove(gc);
833
834 return ret;
835}
836
Pramod Kumarafc8c782015-11-19 09:22:17 +0530837static struct platform_driver iproc_gpio_driver = {
Ray Juib64333c2015-03-09 13:45:00 -0700838 .driver = {
Pramod Kumarafc8c782015-11-19 09:22:17 +0530839 .name = "iproc-gpio",
840 .of_match_table = iproc_gpio_of_match,
Ray Juib64333c2015-03-09 13:45:00 -0700841 },
Pramod Kumarafc8c782015-11-19 09:22:17 +0530842 .probe = iproc_gpio_probe,
Ray Juib64333c2015-03-09 13:45:00 -0700843};
844
Pramod Kumarafc8c782015-11-19 09:22:17 +0530845static int __init iproc_gpio_init(void)
Ray Juib64333c2015-03-09 13:45:00 -0700846{
Pramod Kumarafc8c782015-11-19 09:22:17 +0530847 return platform_driver_probe(&iproc_gpio_driver, iproc_gpio_probe);
Ray Juib64333c2015-03-09 13:45:00 -0700848}
Pramod Kumarafc8c782015-11-19 09:22:17 +0530849arch_initcall_sync(iproc_gpio_init);