blob: a9e564f3410b234203a3a6243572cf60767e3911 [file] [log] [blame]
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001/*
2 * GPIO driver for Marvell SoCs
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
7 * Andrew Lunn <andrew@lunn.ch>
8 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 *
14 * This driver is a fairly straightforward GPIO driver for the
15 * complete family of Marvell EBU SoC platforms (Orion, Dove,
16 * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
17 * driver is the different register layout that exists between the
18 * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
19 * platforms (MV78200 from the Discovery family and the Armada
20 * XP). Therefore, this driver handles three variants of the GPIO
21 * block:
22 * - the basic variant, called "orion-gpio", with the simplest
23 * register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
24 * non-SMP Discovery systems
25 * - the mv78200 variant for MV78200 Discovery systems. This variant
26 * turns the edge mask and level mask registers into CPU0 edge
27 * mask/level mask registers, and adds CPU1 edge mask/level mask
28 * registers.
29 * - the armadaxp variant for Armada XP systems. This variant keeps
30 * the normal cause/edge mask/level mask registers when the global
31 * interrupts are used, but adds per-CPU cause/edge mask/level mask
32 * registers n a separate memory area for the per-CPU GPIO
33 * interrupts.
34 */
35
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +010036#include <linux/bitops.h>
Gregory CLEMENT6ec015d2017-05-19 18:09:20 +020037#include <linux/clk.h>
38#include <linux/err.h>
39#include <linux/gpio.h>
40#include <linux/init.h>
41#include <linux/io.h>
42#include <linux/irq.h>
43#include <linux/irqchip/chained_irq.h>
44#include <linux/irqdomain.h>
45#include <linux/of_device.h>
46#include <linux/of_irq.h>
47#include <linux/pinctrl/consumer.h>
48#include <linux/platform_device.h>
49#include <linux/pwm.h>
50#include <linux/slab.h>
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020051
Andrew Lunn757642f2017-04-14 17:40:52 +020052#include "gpiolib.h"
53
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020054/*
55 * GPIO unit register offsets.
56 */
Andrew Lunn757642f2017-04-14 17:40:52 +020057#define GPIO_OUT_OFF 0x0000
58#define GPIO_IO_CONF_OFF 0x0004
59#define GPIO_BLINK_EN_OFF 0x0008
60#define GPIO_IN_POL_OFF 0x000c
61#define GPIO_DATA_IN_OFF 0x0010
62#define GPIO_EDGE_CAUSE_OFF 0x0014
63#define GPIO_EDGE_MASK_OFF 0x0018
64#define GPIO_LEVEL_MASK_OFF 0x001c
65#define GPIO_BLINK_CNT_SELECT_OFF 0x0020
66
67/*
68 * PWM register offsets.
69 */
70#define PWM_BLINK_ON_DURATION_OFF 0x0
71#define PWM_BLINK_OFF_DURATION_OFF 0x4
72
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020073
74/* The MV78200 has per-CPU registers for edge mask and level mask */
Andrew Lunna4319a62015-01-10 00:34:47 +010075#define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020076#define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
77
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +010078/*
79 * The Armada XP has per-CPU registers for interrupt cause, interrupt
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020080 * mask and interrupt level mask. Those are relative to the
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +010081 * percpu_membase.
82 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020083#define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
84#define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
85#define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
86
Andrew Lunna4319a62015-01-10 00:34:47 +010087#define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
88#define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020089#define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
90
Andrew Lunna4319a62015-01-10 00:34:47 +010091#define MVEBU_MAX_GPIO_PER_BANK 32
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020092
Andrew Lunn757642f2017-04-14 17:40:52 +020093struct mvebu_pwm {
94 void __iomem *membase;
95 unsigned long clk_rate;
96 struct gpio_desc *gpiod;
97 struct pwm_chip chip;
98 spinlock_t lock;
99 struct mvebu_gpio_chip *mvchip;
100
101 /* Used to preserve GPIO/PWM registers across suspend/resume */
102 u32 blink_select;
103 u32 blink_on_duration;
104 u32 blink_off_duration;
105};
106
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200107struct mvebu_gpio_chip {
108 struct gpio_chip chip;
109 spinlock_t lock;
110 void __iomem *membase;
111 void __iomem *percpu_membase;
Dan Carpenterd5359222013-11-07 10:50:19 +0300112 int irqbase;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200113 struct irq_domain *domain;
Andrew Lunna4319a62015-01-10 00:34:47 +0100114 int soc_variant;
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200115
Andrew Lunn757642f2017-04-14 17:40:52 +0200116 /* Used for PWM support */
117 struct clk *clk;
118 struct mvebu_pwm *mvpwm;
119
Andrew Lunna4319a62015-01-10 00:34:47 +0100120 /* Used to preserve GPIO registers across suspend/resume */
Ralph Sennhauserf4c240c2017-03-16 07:34:00 +0100121 u32 out_reg;
122 u32 io_conf_reg;
123 u32 blink_en_reg;
124 u32 in_pol_reg;
125 u32 edge_mask_regs[4];
126 u32 level_mask_regs[4];
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200127};
128
129/*
130 * Functions returning addresses of individual registers for a given
131 * GPIO controller.
132 */
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100133static void __iomem *mvebu_gpioreg_out(struct mvebu_gpio_chip *mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200134{
135 return mvchip->membase + GPIO_OUT_OFF;
136}
137
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100138static void __iomem *mvebu_gpioreg_blink(struct mvebu_gpio_chip *mvchip)
Jamie Lentine9133762012-10-28 12:23:24 +0000139{
140 return mvchip->membase + GPIO_BLINK_EN_OFF;
141}
142
Andrew Lunn757642f2017-04-14 17:40:52 +0200143static void __iomem *mvebu_gpioreg_blink_counter_select(struct mvebu_gpio_chip
144 *mvchip)
145{
146 return mvchip->membase + GPIO_BLINK_CNT_SELECT_OFF;
147}
148
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100149static void __iomem *mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200150{
151 return mvchip->membase + GPIO_IO_CONF_OFF;
152}
153
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100154static void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200155{
156 return mvchip->membase + GPIO_IN_POL_OFF;
157}
158
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100159static void __iomem *mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200160{
161 return mvchip->membase + GPIO_DATA_IN_OFF;
162}
163
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100164static void __iomem *mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200165{
166 int cpu;
167
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100168 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200169 case MVEBU_GPIO_SOC_VARIANT_ORION:
170 case MVEBU_GPIO_SOC_VARIANT_MV78200:
171 return mvchip->membase + GPIO_EDGE_CAUSE_OFF;
172 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
173 cpu = smp_processor_id();
Andrew Lunna4319a62015-01-10 00:34:47 +0100174 return mvchip->percpu_membase +
175 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200176 default:
177 BUG();
178 }
179}
180
Ralph Sennhauserf07708c2017-03-16 07:34:01 +0100181static void __iomem *mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200182{
183 int cpu;
184
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100185 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200186 case MVEBU_GPIO_SOC_VARIANT_ORION:
187 return mvchip->membase + GPIO_EDGE_MASK_OFF;
188 case MVEBU_GPIO_SOC_VARIANT_MV78200:
189 cpu = smp_processor_id();
190 return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu);
191 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
192 cpu = smp_processor_id();
Andrew Lunna4319a62015-01-10 00:34:47 +0100193 return mvchip->percpu_membase +
194 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200195 default:
196 BUG();
197 }
198}
199
200static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip)
201{
202 int cpu;
203
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100204 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200205 case MVEBU_GPIO_SOC_VARIANT_ORION:
206 return mvchip->membase + GPIO_LEVEL_MASK_OFF;
207 case MVEBU_GPIO_SOC_VARIANT_MV78200:
208 cpu = smp_processor_id();
209 return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu);
210 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
211 cpu = smp_processor_id();
Andrew Lunna4319a62015-01-10 00:34:47 +0100212 return mvchip->percpu_membase +
213 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200214 default:
215 BUG();
216 }
217}
218
219/*
Andrew Lunn757642f2017-04-14 17:40:52 +0200220 * Functions returning addresses of individual registers for a given
221 * PWM controller.
222 */
223static void __iomem *mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm)
224{
225 return mvpwm->membase + PWM_BLINK_ON_DURATION_OFF;
226}
227
228static void __iomem *mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm)
229{
230 return mvpwm->membase + PWM_BLINK_OFF_DURATION_OFF;
231}
232
233/*
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200234 * Functions implementing the gpio_chip methods
235 */
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100236static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200237{
Linus Walleijbbe76002015-12-07 11:09:24 +0100238 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200239 unsigned long flags;
240 u32 u;
241
242 spin_lock_irqsave(&mvchip->lock, flags);
243 u = readl_relaxed(mvebu_gpioreg_out(mvchip));
244 if (value)
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100245 u |= BIT(pin);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200246 else
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100247 u &= ~BIT(pin);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200248 writel_relaxed(u, mvebu_gpioreg_out(mvchip));
249 spin_unlock_irqrestore(&mvchip->lock, flags);
250}
251
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100252static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200253{
Linus Walleijbbe76002015-12-07 11:09:24 +0100254 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200255 u32 u;
256
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100257 if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & BIT(pin)) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200258 u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^
259 readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
260 } else {
261 u = readl_relaxed(mvebu_gpioreg_out(mvchip));
262 }
263
264 return (u >> pin) & 1;
265}
266
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100267static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin,
268 int value)
Jamie Lentine9133762012-10-28 12:23:24 +0000269{
Linus Walleijbbe76002015-12-07 11:09:24 +0100270 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Jamie Lentine9133762012-10-28 12:23:24 +0000271 unsigned long flags;
272 u32 u;
273
274 spin_lock_irqsave(&mvchip->lock, flags);
275 u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
276 if (value)
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100277 u |= BIT(pin);
Jamie Lentine9133762012-10-28 12:23:24 +0000278 else
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100279 u &= ~BIT(pin);
Jamie Lentine9133762012-10-28 12:23:24 +0000280 writel_relaxed(u, mvebu_gpioreg_blink(mvchip));
281 spin_unlock_irqrestore(&mvchip->lock, flags);
282}
283
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100284static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200285{
Linus Walleijbbe76002015-12-07 11:09:24 +0100286 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200287 unsigned long flags;
288 int ret;
289 u32 u;
290
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +0100291 /*
292 * Check with the pinctrl driver whether this pin is usable as
293 * an input GPIO
294 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200295 ret = pinctrl_gpio_direction_input(chip->base + pin);
296 if (ret)
297 return ret;
298
299 spin_lock_irqsave(&mvchip->lock, flags);
300 u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100301 u |= BIT(pin);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200302 writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
303 spin_unlock_irqrestore(&mvchip->lock, flags);
304
305 return 0;
306}
307
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100308static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200309 int value)
310{
Linus Walleijbbe76002015-12-07 11:09:24 +0100311 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200312 unsigned long flags;
313 int ret;
314 u32 u;
315
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +0100316 /*
317 * Check with the pinctrl driver whether this pin is usable as
318 * an output GPIO
319 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200320 ret = pinctrl_gpio_direction_output(chip->base + pin);
321 if (ret)
322 return ret;
323
Jamie Lentine9133762012-10-28 12:23:24 +0000324 mvebu_gpio_blink(chip, pin, 0);
Thomas Petazzonic57d75c2012-10-23 10:17:05 +0200325 mvebu_gpio_set(chip, pin, value);
326
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200327 spin_lock_irqsave(&mvchip->lock, flags);
328 u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100329 u &= ~BIT(pin);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200330 writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
331 spin_unlock_irqrestore(&mvchip->lock, flags);
332
333 return 0;
334}
335
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100336static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200337{
Linus Walleijbbe76002015-12-07 11:09:24 +0100338 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Ralph Sennhauser163ad362017-03-16 07:33:59 +0100339
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200340 return irq_create_mapping(mvchip->domain, pin);
341}
342
343/*
344 * Functions implementing the irq_chip methods
345 */
346static void mvebu_gpio_irq_ack(struct irq_data *d)
347{
348 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
349 struct mvebu_gpio_chip *mvchip = gc->private;
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600350 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200351
352 irq_gc_lock(gc);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600353 writel_relaxed(~mask, mvebu_gpioreg_edge_cause(mvchip));
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200354 irq_gc_unlock(gc);
355}
356
357static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
358{
359 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
360 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200361 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600362 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200363
364 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200365 ct->mask_cache_priv &= ~mask;
366
367 writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_edge_mask(mvchip));
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200368 irq_gc_unlock(gc);
369}
370
371static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
372{
373 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
374 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200375 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600376 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200377
378 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200379 ct->mask_cache_priv |= mask;
380 writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_edge_mask(mvchip));
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200381 irq_gc_unlock(gc);
382}
383
384static void mvebu_gpio_level_irq_mask(struct irq_data *d)
385{
386 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
387 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200388 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600389 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200390
391 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200392 ct->mask_cache_priv &= ~mask;
393 writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_level_mask(mvchip));
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200394 irq_gc_unlock(gc);
395}
396
397static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
398{
399 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
400 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200401 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600402 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200403
404 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200405 ct->mask_cache_priv |= mask;
406 writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_level_mask(mvchip));
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200407 irq_gc_unlock(gc);
408}
409
410/*****************************************************************************
411 * MVEBU GPIO IRQ
412 *
413 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
414 * value of the line or the opposite value.
415 *
416 * Level IRQ handlers: DATA_IN is used directly as cause register.
Andrew Lunna4319a62015-01-10 00:34:47 +0100417 * Interrupt are masked by LEVEL_MASK registers.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200418 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
Andrew Lunna4319a62015-01-10 00:34:47 +0100419 * Interrupt are masked by EDGE_MASK registers.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200420 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
Andrew Lunna4319a62015-01-10 00:34:47 +0100421 * the polarity to catch the next line transaction.
422 * This is a race condition that might not perfectly
423 * work on some use cases.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200424 *
425 * Every eight GPIO lines are grouped (OR'ed) before going up to main
426 * cause register.
427 *
Andrew Lunna4319a62015-01-10 00:34:47 +0100428 * EDGE cause mask
429 * data-in /--------| |-----| |----\
430 * -----| |----- ---- to main cause reg
431 * X \----------------| |----/
432 * polarity LEVEL mask
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200433 *
434 ****************************************************************************/
435
436static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
437{
438 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
439 struct irq_chip_type *ct = irq_data_get_chip_type(d);
440 struct mvebu_gpio_chip *mvchip = gc->private;
441 int pin;
442 u32 u;
443
444 pin = d->hwirq;
445
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100446 u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & BIT(pin);
Andrew Lunna4319a62015-01-10 00:34:47 +0100447 if (!u)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200448 return -EINVAL;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200449
450 type &= IRQ_TYPE_SENSE_MASK;
451 if (type == IRQ_TYPE_NONE)
452 return -EINVAL;
453
454 /* Check if we need to change chip and handler */
455 if (!(ct->type & type))
456 if (irq_setup_alt_chip(d, type))
457 return -EINVAL;
458
459 /*
460 * Configure interrupt polarity.
461 */
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100462 switch (type) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200463 case IRQ_TYPE_EDGE_RISING:
464 case IRQ_TYPE_LEVEL_HIGH:
465 u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100466 u &= ~BIT(pin);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200467 writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800468 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200469 case IRQ_TYPE_EDGE_FALLING:
470 case IRQ_TYPE_LEVEL_LOW:
471 u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100472 u |= BIT(pin);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200473 writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800474 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200475 case IRQ_TYPE_EDGE_BOTH: {
476 u32 v;
477
478 v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
479 readl_relaxed(mvebu_gpioreg_data_in(mvchip));
480
481 /*
482 * set initial polarity based on current input level
483 */
484 u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100485 if (v & BIT(pin))
486 u |= BIT(pin); /* falling */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200487 else
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100488 u &= ~BIT(pin); /* rising */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200489 writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800490 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200491 }
492 }
493 return 0;
494}
495
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200496static void mvebu_gpio_irq_handler(struct irq_desc *desc)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200497{
Jiang Liu476f8b42015-06-04 12:13:15 +0800498 struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc);
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100499 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200500 u32 cause, type;
501 int i;
502
503 if (mvchip == NULL)
504 return;
505
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100506 chained_irq_enter(chip, desc);
507
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200508 cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
509 readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
510 cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
511 readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
512
513 for (i = 0; i < mvchip->chip.ngpio; i++) {
514 int irq;
515
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600516 irq = irq_find_mapping(mvchip->domain, i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200517
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100518 if (!(cause & BIT(i)))
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200519 continue;
520
Javier Martinez Canillasfb90c222013-06-14 18:40:44 +0200521 type = irq_get_trigger_type(irq);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200522 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
523 /* Swap polarity (race with GPIO line) */
524 u32 polarity;
525
526 polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100527 polarity ^= BIT(i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200528 writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
529 }
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100530
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200531 generic_handle_irq(irq);
532 }
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100533
534 chained_irq_exit(chip, desc);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200535}
536
Andrew Lunn757642f2017-04-14 17:40:52 +0200537/*
538 * Functions implementing the pwm_chip methods
539 */
540static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip)
541{
542 return container_of(chip, struct mvebu_pwm, chip);
543}
544
545static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
546{
547 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
548 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
549 struct gpio_desc *desc;
550 unsigned long flags;
551 int ret = 0;
552
553 spin_lock_irqsave(&mvpwm->lock, flags);
554
555 if (mvpwm->gpiod) {
556 ret = -EBUSY;
557 } else {
558 desc = gpio_to_desc(mvchip->chip.base + pwm->hwpwm);
559 if (!desc) {
560 ret = -ENODEV;
561 goto out;
562 }
563
564 ret = gpiod_request(desc, "mvebu-pwm");
565 if (ret)
566 goto out;
567
568 ret = gpiod_direction_output(desc, 0);
569 if (ret) {
570 gpiod_free(desc);
571 goto out;
572 }
573
574 mvpwm->gpiod = desc;
575 }
576out:
577 spin_unlock_irqrestore(&mvpwm->lock, flags);
578 return ret;
579}
580
581static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
582{
583 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
584 unsigned long flags;
585
586 spin_lock_irqsave(&mvpwm->lock, flags);
587 gpiod_free(mvpwm->gpiod);
588 mvpwm->gpiod = NULL;
589 spin_unlock_irqrestore(&mvpwm->lock, flags);
590}
591
592static void mvebu_pwm_get_state(struct pwm_chip *chip,
593 struct pwm_device *pwm,
594 struct pwm_state *state) {
595
596 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
597 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
598 unsigned long long val;
599 unsigned long flags;
600 u32 u;
601
602 spin_lock_irqsave(&mvpwm->lock, flags);
603
604 val = (unsigned long long)
605 readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
606 val *= NSEC_PER_SEC;
607 do_div(val, mvpwm->clk_rate);
608 if (val > UINT_MAX)
609 state->duty_cycle = UINT_MAX;
610 else if (val)
611 state->duty_cycle = val;
612 else
613 state->duty_cycle = 1;
614
615 val = (unsigned long long)
616 readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
617 val *= NSEC_PER_SEC;
618 do_div(val, mvpwm->clk_rate);
619 if (val < state->duty_cycle) {
620 state->period = 1;
621 } else {
622 val -= state->duty_cycle;
623 if (val > UINT_MAX)
624 state->period = UINT_MAX;
625 else if (val)
626 state->period = val;
627 else
628 state->period = 1;
629 }
630
631 u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
632 if (u)
633 state->enabled = true;
634 else
635 state->enabled = false;
636
637 spin_unlock_irqrestore(&mvpwm->lock, flags);
638}
639
640static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
641 struct pwm_state *state)
642{
643 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
644 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
645 unsigned long long val;
646 unsigned long flags;
647 unsigned int on, off;
648
649 val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
650 do_div(val, NSEC_PER_SEC);
651 if (val > UINT_MAX)
652 return -EINVAL;
653 if (val)
654 on = val;
655 else
656 on = 1;
657
658 val = (unsigned long long) mvpwm->clk_rate *
659 (state->period - state->duty_cycle);
660 do_div(val, NSEC_PER_SEC);
661 if (val > UINT_MAX)
662 return -EINVAL;
663 if (val)
664 off = val;
665 else
666 off = 1;
667
668 spin_lock_irqsave(&mvpwm->lock, flags);
669
670 writel_relaxed(on, mvebu_pwmreg_blink_on_duration(mvpwm));
671 writel_relaxed(off, mvebu_pwmreg_blink_off_duration(mvpwm));
672 if (state->enabled)
673 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
674 else
675 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);
676
677 spin_unlock_irqrestore(&mvpwm->lock, flags);
678
679 return 0;
680}
681
682static const struct pwm_ops mvebu_pwm_ops = {
683 .request = mvebu_pwm_request,
684 .free = mvebu_pwm_free,
685 .get_state = mvebu_pwm_get_state,
686 .apply = mvebu_pwm_apply,
687 .owner = THIS_MODULE,
688};
689
690static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
691{
692 struct mvebu_pwm *mvpwm = mvchip->mvpwm;
693
694 mvpwm->blink_select =
695 readl_relaxed(mvebu_gpioreg_blink_counter_select(mvchip));
696 mvpwm->blink_on_duration =
697 readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
698 mvpwm->blink_off_duration =
699 readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
700}
701
702static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
703{
704 struct mvebu_pwm *mvpwm = mvchip->mvpwm;
705
706 writel_relaxed(mvpwm->blink_select,
707 mvebu_gpioreg_blink_counter_select(mvchip));
708 writel_relaxed(mvpwm->blink_on_duration,
709 mvebu_pwmreg_blink_on_duration(mvpwm));
710 writel_relaxed(mvpwm->blink_off_duration,
711 mvebu_pwmreg_blink_off_duration(mvpwm));
712}
713
714static int mvebu_pwm_probe(struct platform_device *pdev,
715 struct mvebu_gpio_chip *mvchip,
716 int id)
717{
718 struct device *dev = &pdev->dev;
719 struct mvebu_pwm *mvpwm;
720 struct resource *res;
721 u32 set;
722
723 if (!of_device_is_compatible(mvchip->chip.of_node,
724 "marvell,armada-370-xp-gpio"))
725 return 0;
726
727 if (IS_ERR(mvchip->clk))
728 return PTR_ERR(mvchip->clk);
729
730 /*
731 * There are only two sets of PWM configuration registers for
732 * all the GPIO lines on those SoCs which this driver reserves
733 * for the first two GPIO chips. So if the resource is missing
734 * we can't treat it as an error.
735 */
736 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm");
737 if (!res)
738 return 0;
739
740 /*
741 * Use set A for lines of GPIO chip with id 0, B for GPIO chip
742 * with id 1. Don't allow further GPIO chips to be used for PWM.
743 */
744 if (id == 0)
745 set = 0;
746 else if (id == 1)
747 set = U32_MAX;
748 else
749 return -EINVAL;
750 writel_relaxed(0, mvebu_gpioreg_blink_counter_select(mvchip));
751
752 mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
753 if (!mvpwm)
754 return -ENOMEM;
755 mvchip->mvpwm = mvpwm;
756 mvpwm->mvchip = mvchip;
757
758 mvpwm->membase = devm_ioremap_resource(dev, res);
759 if (IS_ERR(mvpwm->membase))
760 return PTR_ERR(mvpwm->membase);
761
762 mvpwm->clk_rate = clk_get_rate(mvchip->clk);
763 if (!mvpwm->clk_rate) {
764 dev_err(dev, "failed to get clock rate\n");
765 return -EINVAL;
766 }
767
768 mvpwm->chip.dev = dev;
769 mvpwm->chip.ops = &mvebu_pwm_ops;
770 mvpwm->chip.npwm = mvchip->chip.ngpio;
771
772 spin_lock_init(&mvpwm->lock);
773
774 return pwmchip_add(&mvpwm->chip);
775}
776
Simon Guinota4ba5e12013-03-24 15:45:29 +0100777#ifdef CONFIG_DEBUG_FS
778#include <linux/seq_file.h>
779
780static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
781{
Linus Walleijbbe76002015-12-07 11:09:24 +0100782 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100783 u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
784 int i;
785
786 out = readl_relaxed(mvebu_gpioreg_out(mvchip));
787 io_conf = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
788 blink = readl_relaxed(mvebu_gpioreg_blink(mvchip));
789 in_pol = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
790 data_in = readl_relaxed(mvebu_gpioreg_data_in(mvchip));
791 cause = readl_relaxed(mvebu_gpioreg_edge_cause(mvchip));
792 edg_msk = readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
793 lvl_msk = readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
794
795 for (i = 0; i < chip->ngpio; i++) {
796 const char *label;
797 u32 msk;
798 bool is_out;
799
800 label = gpiochip_is_requested(chip, i);
801 if (!label)
802 continue;
803
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100804 msk = BIT(i);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100805 is_out = !(io_conf & msk);
806
807 seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
808
809 if (is_out) {
810 seq_printf(s, " out %s %s\n",
811 out & msk ? "hi" : "lo",
812 blink & msk ? "(blink )" : "");
813 continue;
814 }
815
816 seq_printf(s, " in %s (act %s) - IRQ",
817 (data_in ^ in_pol) & msk ? "hi" : "lo",
818 in_pol & msk ? "lo" : "hi");
819 if (!((edg_msk | lvl_msk) & msk)) {
Andrew Lunna4319a62015-01-10 00:34:47 +0100820 seq_puts(s, " disabled\n");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100821 continue;
822 }
823 if (edg_msk & msk)
Andrew Lunna4319a62015-01-10 00:34:47 +0100824 seq_puts(s, " edge ");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100825 if (lvl_msk & msk)
Andrew Lunna4319a62015-01-10 00:34:47 +0100826 seq_puts(s, " level");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100827 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
828 }
829}
830#else
831#define mvebu_gpio_dbg_show NULL
832#endif
833
Jingoo Han271b17b2014-05-07 18:06:08 +0900834static const struct of_device_id mvebu_gpio_of_match[] = {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200835 {
836 .compatible = "marvell,orion-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100837 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200838 },
839 {
840 .compatible = "marvell,mv78200-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100841 .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200842 },
843 {
844 .compatible = "marvell,armadaxp-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100845 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200846 },
847 {
Andrew Lunn757642f2017-04-14 17:40:52 +0200848 .compatible = "marvell,armada-370-xp-gpio",
849 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
850 },
851 {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200852 /* sentinel */
853 },
854};
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200855
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200856static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
857{
858 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
859 int i;
860
861 mvchip->out_reg = readl(mvebu_gpioreg_out(mvchip));
862 mvchip->io_conf_reg = readl(mvebu_gpioreg_io_conf(mvchip));
863 mvchip->blink_en_reg = readl(mvebu_gpioreg_blink(mvchip));
864 mvchip->in_pol_reg = readl(mvebu_gpioreg_in_pol(mvchip));
865
866 switch (mvchip->soc_variant) {
867 case MVEBU_GPIO_SOC_VARIANT_ORION:
868 mvchip->edge_mask_regs[0] =
869 readl(mvchip->membase + GPIO_EDGE_MASK_OFF);
870 mvchip->level_mask_regs[0] =
871 readl(mvchip->membase + GPIO_LEVEL_MASK_OFF);
872 break;
873 case MVEBU_GPIO_SOC_VARIANT_MV78200:
874 for (i = 0; i < 2; i++) {
875 mvchip->edge_mask_regs[i] =
876 readl(mvchip->membase +
877 GPIO_EDGE_MASK_MV78200_OFF(i));
878 mvchip->level_mask_regs[i] =
879 readl(mvchip->membase +
880 GPIO_LEVEL_MASK_MV78200_OFF(i));
881 }
882 break;
883 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
884 for (i = 0; i < 4; i++) {
885 mvchip->edge_mask_regs[i] =
886 readl(mvchip->membase +
887 GPIO_EDGE_MASK_ARMADAXP_OFF(i));
888 mvchip->level_mask_regs[i] =
889 readl(mvchip->membase +
890 GPIO_LEVEL_MASK_ARMADAXP_OFF(i));
891 }
892 break;
893 default:
894 BUG();
895 }
896
Andrew Lunn757642f2017-04-14 17:40:52 +0200897 if (IS_ENABLED(CONFIG_PWM))
898 mvebu_pwm_suspend(mvchip);
899
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200900 return 0;
901}
902
903static int mvebu_gpio_resume(struct platform_device *pdev)
904{
905 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
906 int i;
907
908 writel(mvchip->out_reg, mvebu_gpioreg_out(mvchip));
909 writel(mvchip->io_conf_reg, mvebu_gpioreg_io_conf(mvchip));
910 writel(mvchip->blink_en_reg, mvebu_gpioreg_blink(mvchip));
911 writel(mvchip->in_pol_reg, mvebu_gpioreg_in_pol(mvchip));
912
913 switch (mvchip->soc_variant) {
914 case MVEBU_GPIO_SOC_VARIANT_ORION:
915 writel(mvchip->edge_mask_regs[0],
916 mvchip->membase + GPIO_EDGE_MASK_OFF);
917 writel(mvchip->level_mask_regs[0],
918 mvchip->membase + GPIO_LEVEL_MASK_OFF);
919 break;
920 case MVEBU_GPIO_SOC_VARIANT_MV78200:
921 for (i = 0; i < 2; i++) {
922 writel(mvchip->edge_mask_regs[i],
923 mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(i));
924 writel(mvchip->level_mask_regs[i],
925 mvchip->membase +
926 GPIO_LEVEL_MASK_MV78200_OFF(i));
927 }
928 break;
929 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
930 for (i = 0; i < 4; i++) {
931 writel(mvchip->edge_mask_regs[i],
932 mvchip->membase +
933 GPIO_EDGE_MASK_ARMADAXP_OFF(i));
934 writel(mvchip->level_mask_regs[i],
935 mvchip->membase +
936 GPIO_LEVEL_MASK_ARMADAXP_OFF(i));
937 }
938 break;
939 default:
940 BUG();
941 }
942
Andrew Lunn757642f2017-04-14 17:40:52 +0200943 if (IS_ENABLED(CONFIG_PWM))
944 mvebu_pwm_resume(mvchip);
945
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200946 return 0;
947}
948
Bill Pemberton38363092012-11-19 13:22:34 -0500949static int mvebu_gpio_probe(struct platform_device *pdev)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200950{
951 struct mvebu_gpio_chip *mvchip;
952 const struct of_device_id *match;
953 struct device_node *np = pdev->dev.of_node;
954 struct resource *res;
955 struct irq_chip_generic *gc;
956 struct irq_chip_type *ct;
957 unsigned int ngpios;
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600958 bool have_irqs;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200959 int soc_variant;
960 int i, cpu, id;
Andrew Lunnf1d2d082015-01-10 00:34:48 +0100961 int err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200962
963 match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
964 if (match)
Russell Kingf0d50462017-01-10 22:53:28 +0000965 soc_variant = (unsigned long) match->data;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200966 else
967 soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
968
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600969 /* Some gpio controllers do not provide irq support */
970 have_irqs = of_irq_count(np) != 0;
971
Andrew Lunna4319a62015-01-10 00:34:47 +0100972 mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
973 GFP_KERNEL);
Jingoo Han6c8365f2014-04-29 17:38:21 +0900974 if (!mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200975 return -ENOMEM;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200976
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200977 platform_set_drvdata(pdev, mvchip);
978
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200979 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
980 dev_err(&pdev->dev, "Missing ngpios OF property\n");
981 return -ENODEV;
982 }
983
984 id = of_alias_get_id(pdev->dev.of_node, "gpio");
985 if (id < 0) {
986 dev_err(&pdev->dev, "Couldn't get OF id\n");
987 return id;
988 }
989
Andrew Lunn757642f2017-04-14 17:40:52 +0200990 mvchip->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunnde887472013-02-03 11:34:26 +0100991 /* Not all SoCs require a clock.*/
Andrew Lunn757642f2017-04-14 17:40:52 +0200992 if (!IS_ERR(mvchip->clk))
993 clk_prepare_enable(mvchip->clk);
Andrew Lunnde887472013-02-03 11:34:26 +0100994
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200995 mvchip->soc_variant = soc_variant;
996 mvchip->chip.label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100997 mvchip->chip.parent = &pdev->dev;
Jonas Gorski203f0da2015-10-11 17:34:16 +0200998 mvchip->chip.request = gpiochip_generic_request;
999 mvchip->chip.free = gpiochip_generic_free;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001000 mvchip->chip.direction_input = mvebu_gpio_direction_input;
1001 mvchip->chip.get = mvebu_gpio_get;
1002 mvchip->chip.direction_output = mvebu_gpio_direction_output;
1003 mvchip->chip.set = mvebu_gpio_set;
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001004 if (have_irqs)
1005 mvchip->chip.to_irq = mvebu_gpio_to_irq;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001006 mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
1007 mvchip->chip.ngpio = ngpios;
Linus Walleij9fb1f392013-12-04 14:42:46 +01001008 mvchip->chip.can_sleep = false;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001009 mvchip->chip.of_node = np;
Simon Guinota4ba5e12013-03-24 15:45:29 +01001010 mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001011
1012 spin_lock_init(&mvchip->lock);
Julia Lawall08a67a52013-08-14 11:11:07 +02001013 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding641d0342013-01-21 11:09:01 +01001014 mvchip->membase = devm_ioremap_resource(&pdev->dev, res);
Greg Kroah-Hartman422d26b2013-01-25 21:06:30 -08001015 if (IS_ERR(mvchip->membase))
Thierry Reding641d0342013-01-21 11:09:01 +01001016 return PTR_ERR(mvchip->membase);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001017
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +01001018 /*
1019 * The Armada XP has a second range of registers for the
1020 * per-CPU registers
1021 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001022 if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
1023 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Thierry Reding641d0342013-01-21 11:09:01 +01001024 mvchip->percpu_membase = devm_ioremap_resource(&pdev->dev,
1025 res);
Laurent Navetf4dcd2d92013-03-20 13:15:56 +01001026 if (IS_ERR(mvchip->percpu_membase))
Thierry Reding641d0342013-01-21 11:09:01 +01001027 return PTR_ERR(mvchip->percpu_membase);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001028 }
1029
1030 /*
1031 * Mask and clear GPIO interrupts.
1032 */
Laurent Navetf4dcd2d92013-03-20 13:15:56 +01001033 switch (soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001034 case MVEBU_GPIO_SOC_VARIANT_ORION:
1035 writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
1036 writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
1037 writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
1038 break;
1039 case MVEBU_GPIO_SOC_VARIANT_MV78200:
1040 writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
1041 for (cpu = 0; cpu < 2; cpu++) {
1042 writel_relaxed(0, mvchip->membase +
1043 GPIO_EDGE_MASK_MV78200_OFF(cpu));
1044 writel_relaxed(0, mvchip->membase +
1045 GPIO_LEVEL_MASK_MV78200_OFF(cpu));
1046 }
1047 break;
1048 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
1049 writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
1050 writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
1051 writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
1052 for (cpu = 0; cpu < 4; cpu++) {
1053 writel_relaxed(0, mvchip->percpu_membase +
1054 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
1055 writel_relaxed(0, mvchip->percpu_membase +
1056 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
1057 writel_relaxed(0, mvchip->percpu_membase +
1058 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
1059 }
1060 break;
1061 default:
1062 BUG();
1063 }
1064
Laxman Dewangan00b9ab42016-02-22 17:43:28 +05301065 devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001066
1067 /* Some gpio controllers do not provide irq support */
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001068 if (!have_irqs)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001069 return 0;
1070
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001071 mvchip->domain =
1072 irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
1073 if (!mvchip->domain) {
1074 dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
1075 mvchip->chip.label);
1076 return -ENODEV;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001077 }
1078
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001079 err = irq_alloc_domain_generic_chips(
1080 mvchip->domain, ngpios, 2, np->name, handle_level_irq,
1081 IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
1082 if (err) {
1083 dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
1084 mvchip->chip.label);
1085 goto err_domain;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001086 }
1087
Ralph Sennhauser899c37e2017-03-16 07:33:57 +01001088 /*
1089 * NOTE: The common accessors cannot be used because of the percpu
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001090 * access to the mask registers
1091 */
1092 gc = irq_get_domain_generic_chip(mvchip->domain, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001093 gc->private = mvchip;
1094 ct = &gc->chip_types[0];
1095 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
1096 ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
1097 ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
1098 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1099 ct->chip.name = mvchip->chip.label;
1100
1101 ct = &gc->chip_types[1];
1102 ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
1103 ct->chip.irq_ack = mvebu_gpio_irq_ack;
1104 ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
1105 ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
1106 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1107 ct->handler = handle_edge_irq;
1108 ct->chip.name = mvchip->chip.label;
1109
Ralph Sennhauser899c37e2017-03-16 07:33:57 +01001110 /*
1111 * Setup the interrupt handlers. Each chip can have up to 4
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001112 * interrupt handlers, with each handler dealing with 8 GPIO
1113 * pins.
1114 */
1115 for (i = 0; i < 4; i++) {
1116 int irq = platform_get_irq(pdev, i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001117
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001118 if (irq < 0)
1119 continue;
1120 irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
1121 mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001122 }
1123
Andrew Lunn757642f2017-04-14 17:40:52 +02001124 /* Armada 370/XP has simple PWM support for GPIO lines */
1125 if (IS_ENABLED(CONFIG_PWM))
1126 return mvebu_pwm_probe(pdev, mvchip, id);
1127
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001128 return 0;
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001129
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001130err_domain:
1131 irq_domain_remove(mvchip->domain);
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001132
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001133 return err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001134}
1135
1136static struct platform_driver mvebu_gpio_driver = {
1137 .driver = {
Andrew Lunna4319a62015-01-10 00:34:47 +01001138 .name = "mvebu-gpio",
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001139 .of_match_table = mvebu_gpio_of_match,
1140 },
1141 .probe = mvebu_gpio_probe,
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001142 .suspend = mvebu_gpio_suspend,
1143 .resume = mvebu_gpio_resume,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001144};
Paul Gortmakered329f32016-03-27 11:44:45 -04001145builtin_platform_driver(mvebu_gpio_driver);