blob: 3d03740a20e7c50ff1af07c9003e9f4d896184d3 [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>
Thomas Petazzoni2233bf72017-05-19 18:09:21 +020050#include <linux/regmap.h>
Gregory CLEMENT6ec015d2017-05-19 18:09:20 +020051#include <linux/slab.h>
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020052
Andrew Lunn757642f2017-04-14 17:40:52 +020053#include "gpiolib.h"
54
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020055/*
56 * GPIO unit register offsets.
57 */
Andrew Lunn757642f2017-04-14 17:40:52 +020058#define GPIO_OUT_OFF 0x0000
59#define GPIO_IO_CONF_OFF 0x0004
60#define GPIO_BLINK_EN_OFF 0x0008
61#define GPIO_IN_POL_OFF 0x000c
62#define GPIO_DATA_IN_OFF 0x0010
63#define GPIO_EDGE_CAUSE_OFF 0x0014
64#define GPIO_EDGE_MASK_OFF 0x0018
65#define GPIO_LEVEL_MASK_OFF 0x001c
66#define GPIO_BLINK_CNT_SELECT_OFF 0x0020
67
68/*
69 * PWM register offsets.
70 */
71#define PWM_BLINK_ON_DURATION_OFF 0x0
72#define PWM_BLINK_OFF_DURATION_OFF 0x4
73
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020074
75/* The MV78200 has per-CPU registers for edge mask and level mask */
Andrew Lunna4319a62015-01-10 00:34:47 +010076#define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020077#define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
78
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +010079/*
80 * The Armada XP has per-CPU registers for interrupt cause, interrupt
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020081 * mask and interrupt level mask. Those are relative to the
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +010082 * percpu_membase.
83 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020084#define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
85#define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
86#define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
87
Andrew Lunna4319a62015-01-10 00:34:47 +010088#define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
89#define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020090#define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
91
Andrew Lunna4319a62015-01-10 00:34:47 +010092#define MVEBU_MAX_GPIO_PER_BANK 32
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020093
Andrew Lunn757642f2017-04-14 17:40:52 +020094struct mvebu_pwm {
95 void __iomem *membase;
96 unsigned long clk_rate;
97 struct gpio_desc *gpiod;
98 struct pwm_chip chip;
99 spinlock_t lock;
100 struct mvebu_gpio_chip *mvchip;
101
102 /* Used to preserve GPIO/PWM registers across suspend/resume */
103 u32 blink_select;
104 u32 blink_on_duration;
105 u32 blink_off_duration;
106};
107
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200108struct mvebu_gpio_chip {
109 struct gpio_chip chip;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200110 struct regmap *regs;
111 struct regmap *percpu_regs;
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 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200133
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200134static void mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip,
135 struct regmap **map, unsigned int *offset)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200136{
137 int cpu;
138
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100139 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200140 case MVEBU_GPIO_SOC_VARIANT_ORION:
141 case MVEBU_GPIO_SOC_VARIANT_MV78200:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200142 *map = mvchip->regs;
143 *offset = GPIO_EDGE_CAUSE_OFF;
144 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200145 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
146 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200147 *map = mvchip->percpu_regs;
148 *offset = GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
149 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200150 default:
151 BUG();
152 }
153}
154
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200155static u32
156mvebu_gpio_read_edge_cause(struct mvebu_gpio_chip *mvchip)
157{
158 struct regmap *map;
159 unsigned int offset;
160 u32 val;
161
162 mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
163 regmap_read(map, offset, &val);
164
165 return val;
166}
167
168static void
169mvebu_gpio_write_edge_cause(struct mvebu_gpio_chip *mvchip, u32 val)
170{
171 struct regmap *map;
172 unsigned int offset;
173
174 mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
175 regmap_write(map, offset, val);
176}
177
178static inline void
179mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip,
180 struct regmap **map, unsigned int *offset)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200181{
182 int cpu;
183
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100184 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200185 case MVEBU_GPIO_SOC_VARIANT_ORION:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200186 *map = mvchip->regs;
187 *offset = GPIO_EDGE_MASK_OFF;
188 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200189 case MVEBU_GPIO_SOC_VARIANT_MV78200:
190 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200191 *map = mvchip->regs;
192 *offset = GPIO_EDGE_MASK_MV78200_OFF(cpu);
193 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200194 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
195 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200196 *map = mvchip->percpu_regs;
197 *offset = GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
198 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200199 default:
200 BUG();
201 }
202}
203
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200204static u32
205mvebu_gpio_read_edge_mask(struct mvebu_gpio_chip *mvchip)
206{
207 struct regmap *map;
208 unsigned int offset;
209 u32 val;
210
211 mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
212 regmap_read(map, offset, &val);
213
214 return val;
215}
216
217static void
218mvebu_gpio_write_edge_mask(struct mvebu_gpio_chip *mvchip, u32 val)
219{
220 struct regmap *map;
221 unsigned int offset;
222
223 mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
224 regmap_write(map, offset, val);
225}
226
227static void
228mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip,
229 struct regmap **map, unsigned int *offset)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200230{
231 int cpu;
232
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100233 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200234 case MVEBU_GPIO_SOC_VARIANT_ORION:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200235 *map = mvchip->regs;
236 *offset = GPIO_LEVEL_MASK_OFF;
237 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200238 case MVEBU_GPIO_SOC_VARIANT_MV78200:
239 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200240 *map = mvchip->regs;
241 *offset = GPIO_LEVEL_MASK_MV78200_OFF(cpu);
242 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200243 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
244 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200245 *map = mvchip->percpu_regs;
246 *offset = GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
247 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200248 default:
249 BUG();
250 }
251}
252
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200253static u32
254mvebu_gpio_read_level_mask(struct mvebu_gpio_chip *mvchip)
255{
256 struct regmap *map;
257 unsigned int offset;
258 u32 val;
259
260 mvebu_gpioreg_level_mask(mvchip, &map, &offset);
261 regmap_read(map, offset, &val);
262
263 return val;
264}
265
266static void
267mvebu_gpio_write_level_mask(struct mvebu_gpio_chip *mvchip, u32 val)
268{
269 struct regmap *map;
270 unsigned int offset;
271
272 mvebu_gpioreg_level_mask(mvchip, &map, &offset);
273 regmap_write(map, offset, val);
274}
275
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200276/*
Andrew Lunn757642f2017-04-14 17:40:52 +0200277 * Functions returning addresses of individual registers for a given
278 * PWM controller.
279 */
280static void __iomem *mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm)
281{
282 return mvpwm->membase + PWM_BLINK_ON_DURATION_OFF;
283}
284
285static void __iomem *mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm)
286{
287 return mvpwm->membase + PWM_BLINK_OFF_DURATION_OFF;
288}
289
290/*
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200291 * Functions implementing the gpio_chip methods
292 */
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100293static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200294{
Linus Walleijbbe76002015-12-07 11:09:24 +0100295 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200296
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200297 regmap_update_bits(mvchip->regs, GPIO_OUT_OFF,
298 BIT(pin), value ? BIT(pin) : 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200299}
300
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100301static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200302{
Linus Walleijbbe76002015-12-07 11:09:24 +0100303 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200304 u32 u;
305
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200306 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF, &u);
307
308 if (u & BIT(pin)) {
309 u32 data_in, in_pol;
310
311 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF, &data_in);
312 regmap_read(mvchip->regs, GPIO_IN_POL_OFF, &in_pol);
313 u = data_in ^ in_pol;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200314 } else {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200315 regmap_read(mvchip->regs, GPIO_OUT_OFF, &u);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200316 }
317
318 return (u >> pin) & 1;
319}
320
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100321static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin,
322 int value)
Jamie Lentine9133762012-10-28 12:23:24 +0000323{
Linus Walleijbbe76002015-12-07 11:09:24 +0100324 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Jamie Lentine9133762012-10-28 12:23:24 +0000325
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200326 regmap_update_bits(mvchip->regs, GPIO_BLINK_EN_OFF,
327 BIT(pin), value ? BIT(pin) : 0);
Jamie Lentine9133762012-10-28 12:23:24 +0000328}
329
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100330static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200331{
Linus Walleijbbe76002015-12-07 11:09:24 +0100332 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200333 int ret;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200334
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +0100335 /*
336 * Check with the pinctrl driver whether this pin is usable as
337 * an input GPIO
338 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200339 ret = pinctrl_gpio_direction_input(chip->base + pin);
340 if (ret)
341 return ret;
342
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200343 regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF,
344 BIT(pin), 1);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200345
346 return 0;
347}
348
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100349static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200350 int value)
351{
Linus Walleijbbe76002015-12-07 11:09:24 +0100352 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200353 int ret;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200354
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +0100355 /*
356 * Check with the pinctrl driver whether this pin is usable as
357 * an output GPIO
358 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200359 ret = pinctrl_gpio_direction_output(chip->base + pin);
360 if (ret)
361 return ret;
362
Jamie Lentine9133762012-10-28 12:23:24 +0000363 mvebu_gpio_blink(chip, pin, 0);
Thomas Petazzonic57d75c2012-10-23 10:17:05 +0200364 mvebu_gpio_set(chip, pin, value);
365
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200366 regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF,
367 BIT(pin), 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200368
369 return 0;
370}
371
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100372static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200373{
Linus Walleijbbe76002015-12-07 11:09:24 +0100374 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Ralph Sennhauser163ad362017-03-16 07:33:59 +0100375
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200376 return irq_create_mapping(mvchip->domain, pin);
377}
378
379/*
380 * Functions implementing the irq_chip methods
381 */
382static void mvebu_gpio_irq_ack(struct irq_data *d)
383{
384 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
385 struct mvebu_gpio_chip *mvchip = gc->private;
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600386 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200387
388 irq_gc_lock(gc);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200389 mvebu_gpio_write_edge_cause(mvchip, ~mask);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200390 irq_gc_unlock(gc);
391}
392
393static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
394{
395 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
396 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200397 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600398 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200399
400 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200401 ct->mask_cache_priv &= ~mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200402 mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200403 irq_gc_unlock(gc);
404}
405
406static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
407{
408 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
409 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200410 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600411 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200412
413 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200414 ct->mask_cache_priv |= mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200415 mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200416 irq_gc_unlock(gc);
417}
418
419static void mvebu_gpio_level_irq_mask(struct irq_data *d)
420{
421 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
422 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200423 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600424 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200425
426 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200427 ct->mask_cache_priv &= ~mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200428 mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200429 irq_gc_unlock(gc);
430}
431
432static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
433{
434 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
435 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200436 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600437 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200438
439 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200440 ct->mask_cache_priv |= mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200441 mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200442 irq_gc_unlock(gc);
443}
444
445/*****************************************************************************
446 * MVEBU GPIO IRQ
447 *
448 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
449 * value of the line or the opposite value.
450 *
451 * Level IRQ handlers: DATA_IN is used directly as cause register.
Andrew Lunna4319a62015-01-10 00:34:47 +0100452 * Interrupt are masked by LEVEL_MASK registers.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200453 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
Andrew Lunna4319a62015-01-10 00:34:47 +0100454 * Interrupt are masked by EDGE_MASK registers.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200455 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
Andrew Lunna4319a62015-01-10 00:34:47 +0100456 * the polarity to catch the next line transaction.
457 * This is a race condition that might not perfectly
458 * work on some use cases.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200459 *
460 * Every eight GPIO lines are grouped (OR'ed) before going up to main
461 * cause register.
462 *
Andrew Lunna4319a62015-01-10 00:34:47 +0100463 * EDGE cause mask
464 * data-in /--------| |-----| |----\
465 * -----| |----- ---- to main cause reg
466 * X \----------------| |----/
467 * polarity LEVEL mask
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200468 *
469 ****************************************************************************/
470
471static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
472{
473 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
474 struct irq_chip_type *ct = irq_data_get_chip_type(d);
475 struct mvebu_gpio_chip *mvchip = gc->private;
476 int pin;
477 u32 u;
478
479 pin = d->hwirq;
480
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200481 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF, &u);
482 if ((u & BIT(pin)) == 0)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200483 return -EINVAL;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200484
485 type &= IRQ_TYPE_SENSE_MASK;
486 if (type == IRQ_TYPE_NONE)
487 return -EINVAL;
488
489 /* Check if we need to change chip and handler */
490 if (!(ct->type & type))
491 if (irq_setup_alt_chip(d, type))
492 return -EINVAL;
493
494 /*
495 * Configure interrupt polarity.
496 */
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100497 switch (type) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200498 case IRQ_TYPE_EDGE_RISING:
499 case IRQ_TYPE_LEVEL_HIGH:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200500 regmap_update_bits(mvchip->regs, GPIO_IN_POL_OFF,
501 BIT(pin), 0);
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800502 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200503 case IRQ_TYPE_EDGE_FALLING:
504 case IRQ_TYPE_LEVEL_LOW:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200505 regmap_update_bits(mvchip->regs, GPIO_IN_POL_OFF,
506 BIT(pin), 1);
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800507 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200508 case IRQ_TYPE_EDGE_BOTH: {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200509 u32 data_in, in_pol, val;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200510
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200511 regmap_read(mvchip->regs, GPIO_IN_POL_OFF, &in_pol);
512 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF, &data_in);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200513
514 /*
515 * set initial polarity based on current input level
516 */
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200517 if ((data_in ^ in_pol) & BIT(pin))
518 val = BIT(pin); /* falling */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200519 else
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200520 val = 0; /* raising */
521
522 regmap_update_bits(mvchip->regs, GPIO_IN_POL_OFF,
523 BIT(pin), val);
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800524 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200525 }
526 }
527 return 0;
528}
529
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200530static void mvebu_gpio_irq_handler(struct irq_desc *desc)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200531{
Jiang Liu476f8b42015-06-04 12:13:15 +0800532 struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc);
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100533 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200534 u32 cause, type, data_in, level_mask, edge_cause, edge_mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200535 int i;
536
537 if (mvchip == NULL)
538 return;
539
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100540 chained_irq_enter(chip, desc);
541
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200542 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF, &data_in);
543 level_mask = mvebu_gpio_read_level_mask(mvchip);
544 edge_cause = mvebu_gpio_read_edge_cause(mvchip);
545 edge_mask = mvebu_gpio_read_edge_mask(mvchip);
546
547 cause = (data_in ^ level_mask) | (edge_cause & edge_mask);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200548
549 for (i = 0; i < mvchip->chip.ngpio; i++) {
550 int irq;
551
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600552 irq = irq_find_mapping(mvchip->domain, i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200553
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100554 if (!(cause & BIT(i)))
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200555 continue;
556
Javier Martinez Canillasfb90c222013-06-14 18:40:44 +0200557 type = irq_get_trigger_type(irq);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200558 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
559 /* Swap polarity (race with GPIO line) */
560 u32 polarity;
561
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200562 regmap_read(mvchip->regs, GPIO_IN_POL_OFF, &polarity);
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100563 polarity ^= BIT(i);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200564 regmap_write(mvchip->regs, GPIO_IN_POL_OFF, polarity);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200565 }
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100566
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200567 generic_handle_irq(irq);
568 }
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100569
570 chained_irq_exit(chip, desc);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200571}
572
Andrew Lunn757642f2017-04-14 17:40:52 +0200573/*
574 * Functions implementing the pwm_chip methods
575 */
576static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip)
577{
578 return container_of(chip, struct mvebu_pwm, chip);
579}
580
581static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
582{
583 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
584 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
585 struct gpio_desc *desc;
586 unsigned long flags;
587 int ret = 0;
588
589 spin_lock_irqsave(&mvpwm->lock, flags);
590
591 if (mvpwm->gpiod) {
592 ret = -EBUSY;
593 } else {
594 desc = gpio_to_desc(mvchip->chip.base + pwm->hwpwm);
595 if (!desc) {
596 ret = -ENODEV;
597 goto out;
598 }
599
600 ret = gpiod_request(desc, "mvebu-pwm");
601 if (ret)
602 goto out;
603
604 ret = gpiod_direction_output(desc, 0);
605 if (ret) {
606 gpiod_free(desc);
607 goto out;
608 }
609
610 mvpwm->gpiod = desc;
611 }
612out:
613 spin_unlock_irqrestore(&mvpwm->lock, flags);
614 return ret;
615}
616
617static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
618{
619 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
620 unsigned long flags;
621
622 spin_lock_irqsave(&mvpwm->lock, flags);
623 gpiod_free(mvpwm->gpiod);
624 mvpwm->gpiod = NULL;
625 spin_unlock_irqrestore(&mvpwm->lock, flags);
626}
627
628static void mvebu_pwm_get_state(struct pwm_chip *chip,
629 struct pwm_device *pwm,
630 struct pwm_state *state) {
631
632 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
633 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
634 unsigned long long val;
635 unsigned long flags;
636 u32 u;
637
638 spin_lock_irqsave(&mvpwm->lock, flags);
639
640 val = (unsigned long long)
641 readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
642 val *= NSEC_PER_SEC;
643 do_div(val, mvpwm->clk_rate);
644 if (val > UINT_MAX)
645 state->duty_cycle = UINT_MAX;
646 else if (val)
647 state->duty_cycle = val;
648 else
649 state->duty_cycle = 1;
650
651 val = (unsigned long long)
652 readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
653 val *= NSEC_PER_SEC;
654 do_div(val, mvpwm->clk_rate);
655 if (val < state->duty_cycle) {
656 state->period = 1;
657 } else {
658 val -= state->duty_cycle;
659 if (val > UINT_MAX)
660 state->period = UINT_MAX;
661 else if (val)
662 state->period = val;
663 else
664 state->period = 1;
665 }
666
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200667 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF, &u);
Andrew Lunn757642f2017-04-14 17:40:52 +0200668 if (u)
669 state->enabled = true;
670 else
671 state->enabled = false;
672
673 spin_unlock_irqrestore(&mvpwm->lock, flags);
674}
675
676static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
677 struct pwm_state *state)
678{
679 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
680 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
681 unsigned long long val;
682 unsigned long flags;
683 unsigned int on, off;
684
685 val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
686 do_div(val, NSEC_PER_SEC);
687 if (val > UINT_MAX)
688 return -EINVAL;
689 if (val)
690 on = val;
691 else
692 on = 1;
693
694 val = (unsigned long long) mvpwm->clk_rate *
695 (state->period - state->duty_cycle);
696 do_div(val, NSEC_PER_SEC);
697 if (val > UINT_MAX)
698 return -EINVAL;
699 if (val)
700 off = val;
701 else
702 off = 1;
703
704 spin_lock_irqsave(&mvpwm->lock, flags);
705
706 writel_relaxed(on, mvebu_pwmreg_blink_on_duration(mvpwm));
707 writel_relaxed(off, mvebu_pwmreg_blink_off_duration(mvpwm));
708 if (state->enabled)
709 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
710 else
711 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);
712
713 spin_unlock_irqrestore(&mvpwm->lock, flags);
714
715 return 0;
716}
717
718static const struct pwm_ops mvebu_pwm_ops = {
719 .request = mvebu_pwm_request,
720 .free = mvebu_pwm_free,
721 .get_state = mvebu_pwm_get_state,
722 .apply = mvebu_pwm_apply,
723 .owner = THIS_MODULE,
724};
725
726static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
727{
728 struct mvebu_pwm *mvpwm = mvchip->mvpwm;
729
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200730 regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF,
731 &mvpwm->blink_select);
Andrew Lunn757642f2017-04-14 17:40:52 +0200732 mvpwm->blink_on_duration =
733 readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
734 mvpwm->blink_off_duration =
735 readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
736}
737
738static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
739{
740 struct mvebu_pwm *mvpwm = mvchip->mvpwm;
741
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200742 regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF,
743 mvpwm->blink_select);
Andrew Lunn757642f2017-04-14 17:40:52 +0200744 writel_relaxed(mvpwm->blink_on_duration,
745 mvebu_pwmreg_blink_on_duration(mvpwm));
746 writel_relaxed(mvpwm->blink_off_duration,
747 mvebu_pwmreg_blink_off_duration(mvpwm));
748}
749
750static int mvebu_pwm_probe(struct platform_device *pdev,
751 struct mvebu_gpio_chip *mvchip,
752 int id)
753{
754 struct device *dev = &pdev->dev;
755 struct mvebu_pwm *mvpwm;
756 struct resource *res;
757 u32 set;
758
759 if (!of_device_is_compatible(mvchip->chip.of_node,
760 "marvell,armada-370-xp-gpio"))
761 return 0;
762
763 if (IS_ERR(mvchip->clk))
764 return PTR_ERR(mvchip->clk);
765
766 /*
767 * There are only two sets of PWM configuration registers for
768 * all the GPIO lines on those SoCs which this driver reserves
769 * for the first two GPIO chips. So if the resource is missing
770 * we can't treat it as an error.
771 */
772 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm");
773 if (!res)
774 return 0;
775
776 /*
777 * Use set A for lines of GPIO chip with id 0, B for GPIO chip
778 * with id 1. Don't allow further GPIO chips to be used for PWM.
779 */
780 if (id == 0)
781 set = 0;
782 else if (id == 1)
783 set = U32_MAX;
784 else
785 return -EINVAL;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200786 regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF, 0);
Andrew Lunn757642f2017-04-14 17:40:52 +0200787
788 mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
789 if (!mvpwm)
790 return -ENOMEM;
791 mvchip->mvpwm = mvpwm;
792 mvpwm->mvchip = mvchip;
793
794 mvpwm->membase = devm_ioremap_resource(dev, res);
795 if (IS_ERR(mvpwm->membase))
796 return PTR_ERR(mvpwm->membase);
797
798 mvpwm->clk_rate = clk_get_rate(mvchip->clk);
799 if (!mvpwm->clk_rate) {
800 dev_err(dev, "failed to get clock rate\n");
801 return -EINVAL;
802 }
803
804 mvpwm->chip.dev = dev;
805 mvpwm->chip.ops = &mvebu_pwm_ops;
806 mvpwm->chip.npwm = mvchip->chip.ngpio;
807
808 spin_lock_init(&mvpwm->lock);
809
810 return pwmchip_add(&mvpwm->chip);
811}
812
Simon Guinota4ba5e12013-03-24 15:45:29 +0100813#ifdef CONFIG_DEBUG_FS
814#include <linux/seq_file.h>
815
816static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
817{
Linus Walleijbbe76002015-12-07 11:09:24 +0100818 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100819 u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
820 int i;
821
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200822 regmap_read(mvchip->regs, GPIO_OUT_OFF, &out);
823 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF, &io_conf);
824 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF, &blink);
825 regmap_read(mvchip->regs, GPIO_IN_POL_OFF, &in_pol);
826 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF, &data_in);
827 cause = mvebu_gpio_read_edge_cause(mvchip);
828 edg_msk = mvebu_gpio_read_edge_mask(mvchip);
829 lvl_msk = mvebu_gpio_read_level_mask(mvchip);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100830
831 for (i = 0; i < chip->ngpio; i++) {
832 const char *label;
833 u32 msk;
834 bool is_out;
835
836 label = gpiochip_is_requested(chip, i);
837 if (!label)
838 continue;
839
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100840 msk = BIT(i);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100841 is_out = !(io_conf & msk);
842
843 seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
844
845 if (is_out) {
846 seq_printf(s, " out %s %s\n",
847 out & msk ? "hi" : "lo",
848 blink & msk ? "(blink )" : "");
849 continue;
850 }
851
852 seq_printf(s, " in %s (act %s) - IRQ",
853 (data_in ^ in_pol) & msk ? "hi" : "lo",
854 in_pol & msk ? "lo" : "hi");
855 if (!((edg_msk | lvl_msk) & msk)) {
Andrew Lunna4319a62015-01-10 00:34:47 +0100856 seq_puts(s, " disabled\n");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100857 continue;
858 }
859 if (edg_msk & msk)
Andrew Lunna4319a62015-01-10 00:34:47 +0100860 seq_puts(s, " edge ");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100861 if (lvl_msk & msk)
Andrew Lunna4319a62015-01-10 00:34:47 +0100862 seq_puts(s, " level");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100863 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
864 }
865}
866#else
867#define mvebu_gpio_dbg_show NULL
868#endif
869
Jingoo Han271b17b2014-05-07 18:06:08 +0900870static const struct of_device_id mvebu_gpio_of_match[] = {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200871 {
872 .compatible = "marvell,orion-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100873 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200874 },
875 {
876 .compatible = "marvell,mv78200-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100877 .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200878 },
879 {
880 .compatible = "marvell,armadaxp-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100881 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200882 },
883 {
Andrew Lunn757642f2017-04-14 17:40:52 +0200884 .compatible = "marvell,armada-370-xp-gpio",
885 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
886 },
887 {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200888 /* sentinel */
889 },
890};
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200891
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200892static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
893{
894 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
895 int i;
896
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200897 regmap_read(mvchip->regs, GPIO_OUT_OFF, &mvchip->out_reg);
898 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF, &mvchip->io_conf_reg);
899 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF, &mvchip->blink_en_reg);
900 regmap_read(mvchip->regs, GPIO_IN_POL_OFF, &mvchip->in_pol_reg);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200901
902 switch (mvchip->soc_variant) {
903 case MVEBU_GPIO_SOC_VARIANT_ORION:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200904 regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF,
905 &mvchip->edge_mask_regs[0]);
906 regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF,
907 &mvchip->level_mask_regs[0]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200908 break;
909 case MVEBU_GPIO_SOC_VARIANT_MV78200:
910 for (i = 0; i < 2; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200911 regmap_read(mvchip->regs,
912 GPIO_EDGE_MASK_MV78200_OFF(i),
913 &mvchip->edge_mask_regs[i]);
914 regmap_read(mvchip->regs,
915 GPIO_LEVEL_MASK_MV78200_OFF(i),
916 &mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200917 }
918 break;
919 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
920 for (i = 0; i < 4; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200921 regmap_read(mvchip->regs,
922 GPIO_EDGE_MASK_ARMADAXP_OFF(i),
923 &mvchip->edge_mask_regs[i]);
924 regmap_read(mvchip->regs,
925 GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
926 &mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200927 }
928 break;
929 default:
930 BUG();
931 }
932
Andrew Lunn757642f2017-04-14 17:40:52 +0200933 if (IS_ENABLED(CONFIG_PWM))
934 mvebu_pwm_suspend(mvchip);
935
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200936 return 0;
937}
938
939static int mvebu_gpio_resume(struct platform_device *pdev)
940{
941 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
942 int i;
943
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200944 regmap_write(mvchip->regs, GPIO_OUT_OFF, mvchip->out_reg);
945 regmap_write(mvchip->regs, GPIO_IO_CONF_OFF, mvchip->io_conf_reg);
946 regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF, mvchip->blink_en_reg);
947 regmap_write(mvchip->regs, GPIO_IN_POL_OFF, mvchip->in_pol_reg);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200948
949 switch (mvchip->soc_variant) {
950 case MVEBU_GPIO_SOC_VARIANT_ORION:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200951 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF,
952 mvchip->edge_mask_regs[0]);
953 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF,
954 mvchip->level_mask_regs[0]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200955 break;
956 case MVEBU_GPIO_SOC_VARIANT_MV78200:
957 for (i = 0; i < 2; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200958 regmap_write(mvchip->regs,
959 GPIO_EDGE_MASK_MV78200_OFF(i),
960 mvchip->edge_mask_regs[i]);
961 regmap_write(mvchip->regs,
962 GPIO_LEVEL_MASK_MV78200_OFF(i),
963 mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200964 }
965 break;
966 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
967 for (i = 0; i < 4; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200968 regmap_write(mvchip->regs,
969 GPIO_EDGE_MASK_ARMADAXP_OFF(i),
970 mvchip->edge_mask_regs[i]);
971 regmap_write(mvchip->regs,
972 GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
973 mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200974 }
975 break;
976 default:
977 BUG();
978 }
979
Andrew Lunn757642f2017-04-14 17:40:52 +0200980 if (IS_ENABLED(CONFIG_PWM))
981 mvebu_pwm_resume(mvchip);
982
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200983 return 0;
984}
985
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200986static const struct regmap_config mvebu_gpio_regmap_config = {
987 .reg_bits = 32,
988 .reg_stride = 4,
989 .val_bits = 32,
990 .fast_io = true,
991};
992
Bill Pemberton38363092012-11-19 13:22:34 -0500993static int mvebu_gpio_probe(struct platform_device *pdev)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200994{
995 struct mvebu_gpio_chip *mvchip;
996 const struct of_device_id *match;
997 struct device_node *np = pdev->dev.of_node;
998 struct resource *res;
999 struct irq_chip_generic *gc;
1000 struct irq_chip_type *ct;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001001 void __iomem *base;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001002 unsigned int ngpios;
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001003 bool have_irqs;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001004 int soc_variant;
1005 int i, cpu, id;
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001006 int err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001007
1008 match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
1009 if (match)
Russell Kingf0d50462017-01-10 22:53:28 +00001010 soc_variant = (unsigned long) match->data;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001011 else
1012 soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
1013
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001014 /* Some gpio controllers do not provide irq support */
1015 have_irqs = of_irq_count(np) != 0;
1016
Andrew Lunna4319a62015-01-10 00:34:47 +01001017 mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
1018 GFP_KERNEL);
Jingoo Han6c8365f2014-04-29 17:38:21 +09001019 if (!mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001020 return -ENOMEM;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001021
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001022 platform_set_drvdata(pdev, mvchip);
1023
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001024 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
1025 dev_err(&pdev->dev, "Missing ngpios OF property\n");
1026 return -ENODEV;
1027 }
1028
1029 id = of_alias_get_id(pdev->dev.of_node, "gpio");
1030 if (id < 0) {
1031 dev_err(&pdev->dev, "Couldn't get OF id\n");
1032 return id;
1033 }
1034
Andrew Lunn757642f2017-04-14 17:40:52 +02001035 mvchip->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunnde887472013-02-03 11:34:26 +01001036 /* Not all SoCs require a clock.*/
Andrew Lunn757642f2017-04-14 17:40:52 +02001037 if (!IS_ERR(mvchip->clk))
1038 clk_prepare_enable(mvchip->clk);
Andrew Lunnde887472013-02-03 11:34:26 +01001039
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001040 mvchip->soc_variant = soc_variant;
1041 mvchip->chip.label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001042 mvchip->chip.parent = &pdev->dev;
Jonas Gorski203f0da2015-10-11 17:34:16 +02001043 mvchip->chip.request = gpiochip_generic_request;
1044 mvchip->chip.free = gpiochip_generic_free;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001045 mvchip->chip.direction_input = mvebu_gpio_direction_input;
1046 mvchip->chip.get = mvebu_gpio_get;
1047 mvchip->chip.direction_output = mvebu_gpio_direction_output;
1048 mvchip->chip.set = mvebu_gpio_set;
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001049 if (have_irqs)
1050 mvchip->chip.to_irq = mvebu_gpio_to_irq;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001051 mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
1052 mvchip->chip.ngpio = ngpios;
Linus Walleij9fb1f392013-12-04 14:42:46 +01001053 mvchip->chip.can_sleep = false;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001054 mvchip->chip.of_node = np;
Simon Guinota4ba5e12013-03-24 15:45:29 +01001055 mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001056
Julia Lawall08a67a52013-08-14 11:11:07 +02001057 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001058 base = devm_ioremap_resource(&pdev->dev, res);
1059 if (IS_ERR(base))
1060 return PTR_ERR(base);
1061
1062 mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
1063 &mvebu_gpio_regmap_config);
1064 if (IS_ERR(mvchip->regs))
1065 return PTR_ERR(mvchip->regs);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001066
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +01001067 /*
1068 * The Armada XP has a second range of registers for the
1069 * per-CPU registers
1070 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001071 if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
1072 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001073 base = devm_ioremap_resource(&pdev->dev, res);
1074 if (IS_ERR(base))
1075 return PTR_ERR(base);
1076
1077 mvchip->percpu_regs =
1078 devm_regmap_init_mmio(&pdev->dev, base,
1079 &mvebu_gpio_regmap_config);
1080 if (IS_ERR(mvchip->percpu_regs))
1081 return PTR_ERR(mvchip->percpu_regs);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001082 }
1083
1084 /*
1085 * Mask and clear GPIO interrupts.
1086 */
Laurent Navetf4dcd2d92013-03-20 13:15:56 +01001087 switch (soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001088 case MVEBU_GPIO_SOC_VARIANT_ORION:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001089 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
1090 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0);
1091 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001092 break;
1093 case MVEBU_GPIO_SOC_VARIANT_MV78200:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001094 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001095 for (cpu = 0; cpu < 2; cpu++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001096 regmap_write(mvchip->regs,
1097 GPIO_EDGE_MASK_MV78200_OFF(cpu), 0);
1098 regmap_write(mvchip->regs,
1099 GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001100 }
1101 break;
1102 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001103 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
1104 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0);
1105 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001106 for (cpu = 0; cpu < 4; cpu++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001107 regmap_write(mvchip->percpu_regs,
1108 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu), 0);
1109 regmap_write(mvchip->percpu_regs,
1110 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu), 0);
1111 regmap_write(mvchip->percpu_regs,
1112 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu), 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001113 }
1114 break;
1115 default:
1116 BUG();
1117 }
1118
Laxman Dewangan00b9ab42016-02-22 17:43:28 +05301119 devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001120
1121 /* Some gpio controllers do not provide irq support */
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001122 if (!have_irqs)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001123 return 0;
1124
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001125 mvchip->domain =
1126 irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
1127 if (!mvchip->domain) {
1128 dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
1129 mvchip->chip.label);
1130 return -ENODEV;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001131 }
1132
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001133 err = irq_alloc_domain_generic_chips(
1134 mvchip->domain, ngpios, 2, np->name, handle_level_irq,
1135 IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
1136 if (err) {
1137 dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
1138 mvchip->chip.label);
1139 goto err_domain;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001140 }
1141
Ralph Sennhauser899c37e2017-03-16 07:33:57 +01001142 /*
1143 * NOTE: The common accessors cannot be used because of the percpu
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001144 * access to the mask registers
1145 */
1146 gc = irq_get_domain_generic_chip(mvchip->domain, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001147 gc->private = mvchip;
1148 ct = &gc->chip_types[0];
1149 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
1150 ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
1151 ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
1152 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1153 ct->chip.name = mvchip->chip.label;
1154
1155 ct = &gc->chip_types[1];
1156 ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
1157 ct->chip.irq_ack = mvebu_gpio_irq_ack;
1158 ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
1159 ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
1160 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1161 ct->handler = handle_edge_irq;
1162 ct->chip.name = mvchip->chip.label;
1163
Ralph Sennhauser899c37e2017-03-16 07:33:57 +01001164 /*
1165 * Setup the interrupt handlers. Each chip can have up to 4
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001166 * interrupt handlers, with each handler dealing with 8 GPIO
1167 * pins.
1168 */
1169 for (i = 0; i < 4; i++) {
1170 int irq = platform_get_irq(pdev, i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001171
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001172 if (irq < 0)
1173 continue;
1174 irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
1175 mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001176 }
1177
Andrew Lunn757642f2017-04-14 17:40:52 +02001178 /* Armada 370/XP has simple PWM support for GPIO lines */
1179 if (IS_ENABLED(CONFIG_PWM))
1180 return mvebu_pwm_probe(pdev, mvchip, id);
1181
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001182 return 0;
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001183
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001184err_domain:
1185 irq_domain_remove(mvchip->domain);
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001186
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001187 return err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001188}
1189
1190static struct platform_driver mvebu_gpio_driver = {
1191 .driver = {
Andrew Lunna4319a62015-01-10 00:34:47 +01001192 .name = "mvebu-gpio",
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001193 .of_match_table = mvebu_gpio_of_match,
1194 },
1195 .probe = mvebu_gpio_probe,
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001196 .suspend = mvebu_gpio_suspend,
1197 .resume = mvebu_gpio_resume,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001198};
Paul Gortmakered329f32016-03-27 11:44:45 -04001199builtin_platform_driver(mvebu_gpio_driver);