blob: a912a8fed197ac32bb393a87ba17c63421f12184 [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>
Linus Walleijba78d832018-04-13 15:40:45 +020039#include <linux/gpio/driver.h>
40#include <linux/gpio/consumer.h>
Linus Walleij5923ea62019-04-26 14:40:18 +020041#include <linux/gpio/machine.h>
Gregory CLEMENT6ec015d2017-05-19 18:09:20 +020042#include <linux/init.h>
43#include <linux/io.h>
44#include <linux/irq.h>
45#include <linux/irqchip/chained_irq.h>
46#include <linux/irqdomain.h>
Gregory CLEMENTb6730b22017-06-12 17:34:59 +020047#include <linux/mfd/syscon.h>
Gregory CLEMENT6ec015d2017-05-19 18:09:20 +020048#include <linux/of_device.h>
Gregory CLEMENT6ec015d2017-05-19 18:09:20 +020049#include <linux/pinctrl/consumer.h>
50#include <linux/platform_device.h>
51#include <linux/pwm.h>
Thomas Petazzoni2233bf72017-05-19 18:09:21 +020052#include <linux/regmap.h>
Gregory CLEMENT6ec015d2017-05-19 18:09:20 +020053#include <linux/slab.h>
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020054
55/*
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
Baruch Siach64b19f62020-12-02 09:15:33 +020081 * mask and interrupt level mask. Those are in percpu_regs range.
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +010082 */
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
Gregory CLEMENTb6730b22017-06-12 17:34:59 +020090#define MVEBU_GPIO_SOC_VARIANT_A8K 0x4
Thomas Petazzonifefe7b02012-09-19 22:52:58 +020091
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 {
Baruch Siach48f32a82020-12-02 09:15:34 +020095 struct regmap *regs;
Andrew Lunn757642f2017-04-14 17:40:52 +020096 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;
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200111 u32 offset;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200112 struct regmap *percpu_regs;
Dan Carpenterd5359222013-11-07 10:50:19 +0300113 int irqbase;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200114 struct irq_domain *domain;
Andrew Lunna4319a62015-01-10 00:34:47 +0100115 int soc_variant;
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200116
Andrew Lunn757642f2017-04-14 17:40:52 +0200117 /* Used for PWM support */
118 struct clk *clk;
119 struct mvebu_pwm *mvpwm;
120
Andrew Lunna4319a62015-01-10 00:34:47 +0100121 /* Used to preserve GPIO registers across suspend/resume */
Ralph Sennhauserf4c240c2017-03-16 07:34:00 +0100122 u32 out_reg;
123 u32 io_conf_reg;
124 u32 blink_en_reg;
125 u32 in_pol_reg;
126 u32 edge_mask_regs[4];
127 u32 level_mask_regs[4];
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200128};
129
130/*
131 * Functions returning addresses of individual registers for a given
132 * GPIO controller.
133 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200134
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200135static void mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip,
136 struct regmap **map, unsigned int *offset)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200137{
138 int cpu;
139
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100140 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200141 case MVEBU_GPIO_SOC_VARIANT_ORION:
142 case MVEBU_GPIO_SOC_VARIANT_MV78200:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200143 case MVEBU_GPIO_SOC_VARIANT_A8K:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200144 *map = mvchip->regs;
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200145 *offset = GPIO_EDGE_CAUSE_OFF + mvchip->offset;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200146 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200147 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
148 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200149 *map = mvchip->percpu_regs;
150 *offset = GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
151 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200152 default:
153 BUG();
154 }
155}
156
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200157static u32
158mvebu_gpio_read_edge_cause(struct mvebu_gpio_chip *mvchip)
159{
160 struct regmap *map;
161 unsigned int offset;
162 u32 val;
163
164 mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
165 regmap_read(map, offset, &val);
166
167 return val;
168}
169
170static void
171mvebu_gpio_write_edge_cause(struct mvebu_gpio_chip *mvchip, u32 val)
172{
173 struct regmap *map;
174 unsigned int offset;
175
176 mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
177 regmap_write(map, offset, val);
178}
179
180static inline void
181mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip,
182 struct regmap **map, unsigned int *offset)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200183{
184 int cpu;
185
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100186 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200187 case MVEBU_GPIO_SOC_VARIANT_ORION:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200188 case MVEBU_GPIO_SOC_VARIANT_A8K:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200189 *map = mvchip->regs;
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200190 *offset = GPIO_EDGE_MASK_OFF + mvchip->offset;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200191 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200192 case MVEBU_GPIO_SOC_VARIANT_MV78200:
193 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200194 *map = mvchip->regs;
195 *offset = GPIO_EDGE_MASK_MV78200_OFF(cpu);
196 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200197 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
198 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200199 *map = mvchip->percpu_regs;
200 *offset = GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
201 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200202 default:
203 BUG();
204 }
205}
206
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200207static u32
208mvebu_gpio_read_edge_mask(struct mvebu_gpio_chip *mvchip)
209{
210 struct regmap *map;
211 unsigned int offset;
212 u32 val;
213
214 mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
215 regmap_read(map, offset, &val);
216
217 return val;
218}
219
220static void
221mvebu_gpio_write_edge_mask(struct mvebu_gpio_chip *mvchip, u32 val)
222{
223 struct regmap *map;
224 unsigned int offset;
225
226 mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
227 regmap_write(map, offset, val);
228}
229
230static void
231mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip,
232 struct regmap **map, unsigned int *offset)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200233{
234 int cpu;
235
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100236 switch (mvchip->soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200237 case MVEBU_GPIO_SOC_VARIANT_ORION:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200238 case MVEBU_GPIO_SOC_VARIANT_A8K:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200239 *map = mvchip->regs;
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200240 *offset = GPIO_LEVEL_MASK_OFF + mvchip->offset;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200241 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200242 case MVEBU_GPIO_SOC_VARIANT_MV78200:
243 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200244 *map = mvchip->regs;
245 *offset = GPIO_LEVEL_MASK_MV78200_OFF(cpu);
246 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200247 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
248 cpu = smp_processor_id();
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200249 *map = mvchip->percpu_regs;
250 *offset = GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
251 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200252 default:
253 BUG();
254 }
255}
256
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200257static u32
258mvebu_gpio_read_level_mask(struct mvebu_gpio_chip *mvchip)
259{
260 struct regmap *map;
261 unsigned int offset;
262 u32 val;
263
264 mvebu_gpioreg_level_mask(mvchip, &map, &offset);
265 regmap_read(map, offset, &val);
266
267 return val;
268}
269
270static void
271mvebu_gpio_write_level_mask(struct mvebu_gpio_chip *mvchip, u32 val)
272{
273 struct regmap *map;
274 unsigned int offset;
275
276 mvebu_gpioreg_level_mask(mvchip, &map, &offset);
277 regmap_write(map, offset, val);
278}
279
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200280/*
Baruch Siach48f32a82020-12-02 09:15:34 +0200281 * Functions returning offsets of individual registers for a given
Andrew Lunn757642f2017-04-14 17:40:52 +0200282 * PWM controller.
283 */
Baruch Siach48f32a82020-12-02 09:15:34 +0200284static unsigned int mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm)
Andrew Lunn757642f2017-04-14 17:40:52 +0200285{
Baruch Siach48f32a82020-12-02 09:15:34 +0200286 return PWM_BLINK_ON_DURATION_OFF;
Andrew Lunn757642f2017-04-14 17:40:52 +0200287}
288
Baruch Siach48f32a82020-12-02 09:15:34 +0200289static unsigned int mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm)
Andrew Lunn757642f2017-04-14 17:40:52 +0200290{
Baruch Siach48f32a82020-12-02 09:15:34 +0200291 return PWM_BLINK_OFF_DURATION_OFF;
Andrew Lunn757642f2017-04-14 17:40:52 +0200292}
293
294/*
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200295 * Functions implementing the gpio_chip methods
296 */
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100297static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200298{
Linus Walleijbbe76002015-12-07 11:09:24 +0100299 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200300
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200301 regmap_update_bits(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200302 BIT(pin), value ? BIT(pin) : 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200303}
304
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100305static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200306{
Linus Walleijbbe76002015-12-07 11:09:24 +0100307 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200308 u32 u;
309
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200310 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200311
312 if (u & BIT(pin)) {
313 u32 data_in, in_pol;
314
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200315 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset,
316 &data_in);
317 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
318 &in_pol);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200319 u = data_in ^ in_pol;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200320 } else {
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200321 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &u);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200322 }
323
324 return (u >> pin) & 1;
325}
326
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100327static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin,
328 int value)
Jamie Lentine9133762012-10-28 12:23:24 +0000329{
Linus Walleijbbe76002015-12-07 11:09:24 +0100330 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Jamie Lentine9133762012-10-28 12:23:24 +0000331
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200332 regmap_update_bits(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200333 BIT(pin), value ? BIT(pin) : 0);
Jamie Lentine9133762012-10-28 12:23:24 +0000334}
335
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100336static int mvebu_gpio_direction_input(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);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200339 int ret;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200340
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +0100341 /*
342 * Check with the pinctrl driver whether this pin is usable as
343 * an input GPIO
344 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200345 ret = pinctrl_gpio_direction_input(chip->base + pin);
346 if (ret)
347 return ret;
348
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200349 regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
Gregory CLEMENT43a2dce2017-06-09 12:09:17 +0200350 BIT(pin), BIT(pin));
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200351
352 return 0;
353}
354
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100355static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200356 int value)
357{
Linus Walleijbbe76002015-12-07 11:09:24 +0100358 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200359 int ret;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200360
Ralph Sennhauser7077f4c2017-03-16 07:33:56 +0100361 /*
362 * Check with the pinctrl driver whether this pin is usable as
363 * an output GPIO
364 */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200365 ret = pinctrl_gpio_direction_output(chip->base + pin);
366 if (ret)
367 return ret;
368
Jamie Lentine9133762012-10-28 12:23:24 +0000369 mvebu_gpio_blink(chip, pin, 0);
Thomas Petazzonic57d75c2012-10-23 10:17:05 +0200370 mvebu_gpio_set(chip, pin, value);
371
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200372 regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200373 BIT(pin), 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200374
375 return 0;
376}
377
Baruch Siache8dacf52019-01-10 14:26:21 +0200378static int mvebu_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
379{
380 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
381 u32 u;
382
383 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
384
Matti Vaittinene42615e2019-11-06 10:54:12 +0200385 if (u & BIT(pin))
386 return GPIO_LINE_DIRECTION_IN;
387
388 return GPIO_LINE_DIRECTION_OUT;
Baruch Siache8dacf52019-01-10 14:26:21 +0200389}
390
Ralph Sennhauserd276de72017-03-16 07:33:58 +0100391static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200392{
Linus Walleijbbe76002015-12-07 11:09:24 +0100393 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Ralph Sennhauser163ad362017-03-16 07:33:59 +0100394
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200395 return irq_create_mapping(mvchip->domain, pin);
396}
397
398/*
399 * Functions implementing the irq_chip methods
400 */
401static void mvebu_gpio_irq_ack(struct irq_data *d)
402{
403 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
404 struct mvebu_gpio_chip *mvchip = gc->private;
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600405 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200406
407 irq_gc_lock(gc);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200408 mvebu_gpio_write_edge_cause(mvchip, ~mask);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200409 irq_gc_unlock(gc);
410}
411
412static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
413{
414 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
415 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200416 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600417 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200418
419 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200420 ct->mask_cache_priv &= ~mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200421 mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200422 irq_gc_unlock(gc);
423}
424
425static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
426{
427 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
428 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200429 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600430 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200431
432 irq_gc_lock(gc);
Maxim Kiselevd5331ec2020-01-15 10:38:11 +0300433 mvebu_gpio_write_edge_cause(mvchip, ~mask);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200434 ct->mask_cache_priv |= mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200435 mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200436 irq_gc_unlock(gc);
437}
438
439static void mvebu_gpio_level_irq_mask(struct irq_data *d)
440{
441 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
442 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200443 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600444 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200445
446 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200447 ct->mask_cache_priv &= ~mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200448 mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200449 irq_gc_unlock(gc);
450}
451
452static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
453{
454 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
455 struct mvebu_gpio_chip *mvchip = gc->private;
Gregory CLEMENT61819542015-04-02 17:11:11 +0200456 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600457 u32 mask = d->mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200458
459 irq_gc_lock(gc);
Gregory CLEMENT61819542015-04-02 17:11:11 +0200460 ct->mask_cache_priv |= mask;
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200461 mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200462 irq_gc_unlock(gc);
463}
464
465/*****************************************************************************
466 * MVEBU GPIO IRQ
467 *
468 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
469 * value of the line or the opposite value.
470 *
471 * Level IRQ handlers: DATA_IN is used directly as cause register.
Andrew Lunna4319a62015-01-10 00:34:47 +0100472 * Interrupt are masked by LEVEL_MASK registers.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200473 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
Andrew Lunna4319a62015-01-10 00:34:47 +0100474 * Interrupt are masked by EDGE_MASK registers.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200475 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
Andrew Lunna4319a62015-01-10 00:34:47 +0100476 * the polarity to catch the next line transaction.
477 * This is a race condition that might not perfectly
478 * work on some use cases.
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200479 *
480 * Every eight GPIO lines are grouped (OR'ed) before going up to main
481 * cause register.
482 *
Andrew Lunna4319a62015-01-10 00:34:47 +0100483 * EDGE cause mask
484 * data-in /--------| |-----| |----\
485 * -----| |----- ---- to main cause reg
486 * X \----------------| |----/
487 * polarity LEVEL mask
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200488 *
489 ****************************************************************************/
490
491static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
492{
493 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
494 struct irq_chip_type *ct = irq_data_get_chip_type(d);
495 struct mvebu_gpio_chip *mvchip = gc->private;
496 int pin;
497 u32 u;
498
499 pin = d->hwirq;
500
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200501 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200502 if ((u & BIT(pin)) == 0)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200503 return -EINVAL;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200504
505 type &= IRQ_TYPE_SENSE_MASK;
506 if (type == IRQ_TYPE_NONE)
507 return -EINVAL;
508
509 /* Check if we need to change chip and handler */
510 if (!(ct->type & type))
511 if (irq_setup_alt_chip(d, type))
512 return -EINVAL;
513
514 /*
515 * Configure interrupt polarity.
516 */
Laurent Navetf4dcd2d92013-03-20 13:15:56 +0100517 switch (type) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200518 case IRQ_TYPE_EDGE_RISING:
519 case IRQ_TYPE_LEVEL_HIGH:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200520 regmap_update_bits(mvchip->regs,
521 GPIO_IN_POL_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200522 BIT(pin), 0);
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800523 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200524 case IRQ_TYPE_EDGE_FALLING:
525 case IRQ_TYPE_LEVEL_LOW:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200526 regmap_update_bits(mvchip->regs,
527 GPIO_IN_POL_OFF + mvchip->offset,
Gregory CLEMENT43a2dce2017-06-09 12:09:17 +0200528 BIT(pin), BIT(pin));
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800529 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200530 case IRQ_TYPE_EDGE_BOTH: {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200531 u32 data_in, in_pol, val;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200532
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200533 regmap_read(mvchip->regs,
534 GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
535 regmap_read(mvchip->regs,
536 GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200537
538 /*
539 * set initial polarity based on current input level
540 */
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200541 if ((data_in ^ in_pol) & BIT(pin))
542 val = BIT(pin); /* falling */
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200543 else
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200544 val = 0; /* raising */
545
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200546 regmap_update_bits(mvchip->regs,
547 GPIO_IN_POL_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200548 BIT(pin), val);
Axel Lin7cf8c9f2012-09-30 16:23:27 +0800549 break;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200550 }
551 }
552 return 0;
553}
554
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200555static void mvebu_gpio_irq_handler(struct irq_desc *desc)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200556{
Jiang Liu476f8b42015-06-04 12:13:15 +0800557 struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc);
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100558 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200559 u32 cause, type, data_in, level_mask, edge_cause, edge_mask;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200560 int i;
561
562 if (mvchip == NULL)
563 return;
564
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100565 chained_irq_enter(chip, desc);
566
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200567 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200568 level_mask = mvebu_gpio_read_level_mask(mvchip);
569 edge_cause = mvebu_gpio_read_edge_cause(mvchip);
570 edge_mask = mvebu_gpio_read_edge_mask(mvchip);
571
Gregory CLEMENT3f13b6a2017-07-12 13:22:29 +0200572 cause = (data_in & level_mask) | (edge_cause & edge_mask);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200573
574 for (i = 0; i < mvchip->chip.ngpio; i++) {
575 int irq;
576
Jason Gunthorpe812d4782016-10-19 15:03:41 -0600577 irq = irq_find_mapping(mvchip->domain, i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200578
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100579 if (!(cause & BIT(i)))
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200580 continue;
581
Javier Martinez Canillasfb90c222013-06-14 18:40:44 +0200582 type = irq_get_trigger_type(irq);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200583 if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
584 /* Swap polarity (race with GPIO line) */
585 u32 polarity;
586
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200587 regmap_read(mvchip->regs,
588 GPIO_IN_POL_OFF + mvchip->offset,
589 &polarity);
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100590 polarity ^= BIT(i);
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200591 regmap_write(mvchip->regs,
592 GPIO_IN_POL_OFF + mvchip->offset,
593 polarity);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200594 }
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100595
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200596 generic_handle_irq(irq);
597 }
Thomas Petazzoni01ca59f2014-02-07 12:29:19 +0100598
599 chained_irq_exit(chip, desc);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200600}
601
Baruch Siach48f32a82020-12-02 09:15:34 +0200602static const struct regmap_config mvebu_gpio_regmap_config = {
603 .reg_bits = 32,
604 .reg_stride = 4,
605 .val_bits = 32,
606 .fast_io = true,
607};
608
Andrew Lunn757642f2017-04-14 17:40:52 +0200609/*
610 * Functions implementing the pwm_chip methods
611 */
612static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip)
613{
614 return container_of(chip, struct mvebu_pwm, chip);
615}
616
617static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
618{
619 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
620 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
621 struct gpio_desc *desc;
622 unsigned long flags;
623 int ret = 0;
624
625 spin_lock_irqsave(&mvpwm->lock, flags);
626
627 if (mvpwm->gpiod) {
628 ret = -EBUSY;
629 } else {
Linus Walleijba78d832018-04-13 15:40:45 +0200630 desc = gpiochip_request_own_desc(&mvchip->chip,
Linus Walleij5923ea62019-04-26 14:40:18 +0200631 pwm->hwpwm, "mvebu-pwm",
632 GPIO_ACTIVE_HIGH,
633 GPIOD_OUT_LOW);
Linus Walleijba78d832018-04-13 15:40:45 +0200634 if (IS_ERR(desc)) {
635 ret = PTR_ERR(desc);
Andrew Lunn757642f2017-04-14 17:40:52 +0200636 goto out;
637 }
638
Andrew Lunn757642f2017-04-14 17:40:52 +0200639 mvpwm->gpiod = desc;
640 }
641out:
642 spin_unlock_irqrestore(&mvpwm->lock, flags);
643 return ret;
644}
645
646static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
647{
648 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
649 unsigned long flags;
650
651 spin_lock_irqsave(&mvpwm->lock, flags);
Linus Walleijba78d832018-04-13 15:40:45 +0200652 gpiochip_free_own_desc(mvpwm->gpiod);
Andrew Lunn757642f2017-04-14 17:40:52 +0200653 mvpwm->gpiod = NULL;
654 spin_unlock_irqrestore(&mvpwm->lock, flags);
655}
656
657static void mvebu_pwm_get_state(struct pwm_chip *chip,
658 struct pwm_device *pwm,
659 struct pwm_state *state) {
660
661 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
662 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
663 unsigned long long val;
664 unsigned long flags;
665 u32 u;
666
667 spin_lock_irqsave(&mvpwm->lock, flags);
668
Baruch Siach48f32a82020-12-02 09:15:34 +0200669 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), &u);
670 val = (unsigned long long) u * NSEC_PER_SEC;
Andrew Lunn757642f2017-04-14 17:40:52 +0200671 do_div(val, mvpwm->clk_rate);
672 if (val > UINT_MAX)
673 state->duty_cycle = UINT_MAX;
674 else if (val)
675 state->duty_cycle = val;
676 else
677 state->duty_cycle = 1;
678
Baruch Siache73b0102021-01-17 15:17:02 +0200679 val = (unsigned long long) u; /* on duration */
Baruch Siach48f32a82020-12-02 09:15:34 +0200680 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), &u);
Baruch Siache73b0102021-01-17 15:17:02 +0200681 val += (unsigned long long) u; /* period = on + off duration */
682 val *= NSEC_PER_SEC;
Andrew Lunn757642f2017-04-14 17:40:52 +0200683 do_div(val, mvpwm->clk_rate);
Baruch Siache73b0102021-01-17 15:17:02 +0200684 if (val > UINT_MAX)
685 state->period = UINT_MAX;
686 else if (val)
687 state->period = val;
688 else
Andrew Lunn757642f2017-04-14 17:40:52 +0200689 state->period = 1;
Andrew Lunn757642f2017-04-14 17:40:52 +0200690
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200691 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u);
Andrew Lunn757642f2017-04-14 17:40:52 +0200692 if (u)
693 state->enabled = true;
694 else
695 state->enabled = false;
696
697 spin_unlock_irqrestore(&mvpwm->lock, flags);
698}
699
700static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
Uwe Kleine-König71523d12019-08-24 17:37:07 +0200701 const struct pwm_state *state)
Andrew Lunn757642f2017-04-14 17:40:52 +0200702{
703 struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
704 struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
705 unsigned long long val;
706 unsigned long flags;
707 unsigned int on, off;
708
709 val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
710 do_div(val, NSEC_PER_SEC);
711 if (val > UINT_MAX)
712 return -EINVAL;
713 if (val)
714 on = val;
715 else
716 on = 1;
717
718 val = (unsigned long long) mvpwm->clk_rate *
719 (state->period - state->duty_cycle);
720 do_div(val, NSEC_PER_SEC);
721 if (val > UINT_MAX)
722 return -EINVAL;
723 if (val)
724 off = val;
725 else
726 off = 1;
727
728 spin_lock_irqsave(&mvpwm->lock, flags);
729
Baruch Siach48f32a82020-12-02 09:15:34 +0200730 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm), on);
731 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm), off);
Andrew Lunn757642f2017-04-14 17:40:52 +0200732 if (state->enabled)
733 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
734 else
735 mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);
736
737 spin_unlock_irqrestore(&mvpwm->lock, flags);
738
739 return 0;
740}
741
742static const struct pwm_ops mvebu_pwm_ops = {
743 .request = mvebu_pwm_request,
744 .free = mvebu_pwm_free,
745 .get_state = mvebu_pwm_get_state,
746 .apply = mvebu_pwm_apply,
747 .owner = THIS_MODULE,
748};
749
750static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
751{
752 struct mvebu_pwm *mvpwm = mvchip->mvpwm;
753
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200754 regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200755 &mvpwm->blink_select);
Baruch Siach48f32a82020-12-02 09:15:34 +0200756 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm),
757 &mvpwm->blink_on_duration);
758 regmap_read(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm),
759 &mvpwm->blink_off_duration);
Andrew Lunn757642f2017-04-14 17:40:52 +0200760}
761
762static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
763{
764 struct mvebu_pwm *mvpwm = mvchip->mvpwm;
765
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200766 regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200767 mvpwm->blink_select);
Baruch Siach48f32a82020-12-02 09:15:34 +0200768 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_on_duration(mvpwm),
769 mvpwm->blink_on_duration);
770 regmap_write(mvpwm->regs, mvebu_pwmreg_blink_off_duration(mvpwm),
771 mvpwm->blink_off_duration);
Andrew Lunn757642f2017-04-14 17:40:52 +0200772}
773
774static int mvebu_pwm_probe(struct platform_device *pdev,
775 struct mvebu_gpio_chip *mvchip,
776 int id)
777{
778 struct device *dev = &pdev->dev;
779 struct mvebu_pwm *mvpwm;
Baruch Siach48f32a82020-12-02 09:15:34 +0200780 void __iomem *base;
Andrew Lunn757642f2017-04-14 17:40:52 +0200781 u32 set;
782
783 if (!of_device_is_compatible(mvchip->chip.of_node,
Ralph Sennhauser6c7515c2017-06-01 22:08:20 +0200784 "marvell,armada-370-gpio"))
Andrew Lunn757642f2017-04-14 17:40:52 +0200785 return 0;
786
Sascha Hauer19c26d92020-04-17 11:21:57 +0200787 /*
788 * There are only two sets of PWM configuration registers for
789 * all the GPIO lines on those SoCs which this driver reserves
790 * for the first two GPIO chips. So if the resource is missing
791 * we can't treat it as an error.
792 */
793 if (!platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm"))
794 return 0;
795
Uwe Kleine-Königc8da6422018-12-17 09:43:13 +0100796 if (IS_ERR(mvchip->clk))
797 return PTR_ERR(mvchip->clk);
798
Andrew Lunn757642f2017-04-14 17:40:52 +0200799 /*
800 * Use set A for lines of GPIO chip with id 0, B for GPIO chip
801 * with id 1. Don't allow further GPIO chips to be used for PWM.
802 */
803 if (id == 0)
804 set = 0;
805 else if (id == 1)
806 set = U32_MAX;
807 else
808 return -EINVAL;
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200809 regmap_write(mvchip->regs,
Linus Torvaldsc7d28ec2017-07-07 12:40:27 -0700810 GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, set);
Andrew Lunn757642f2017-04-14 17:40:52 +0200811
812 mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
813 if (!mvpwm)
814 return -ENOMEM;
815 mvchip->mvpwm = mvpwm;
816 mvpwm->mvchip = mvchip;
817
Baruch Siach48f32a82020-12-02 09:15:34 +0200818 base = devm_platform_ioremap_resource_byname(pdev, "pwm");
819 if (IS_ERR(base))
820 return PTR_ERR(base);
821
822 mvpwm->regs = devm_regmap_init_mmio(&pdev->dev, base,
823 &mvebu_gpio_regmap_config);
824 if (IS_ERR(mvpwm->regs))
825 return PTR_ERR(mvpwm->regs);
Andrew Lunn757642f2017-04-14 17:40:52 +0200826
827 mvpwm->clk_rate = clk_get_rate(mvchip->clk);
828 if (!mvpwm->clk_rate) {
829 dev_err(dev, "failed to get clock rate\n");
830 return -EINVAL;
831 }
832
833 mvpwm->chip.dev = dev;
834 mvpwm->chip.ops = &mvebu_pwm_ops;
835 mvpwm->chip.npwm = mvchip->chip.ngpio;
Richard Genoudfc7a9062017-06-01 14:18:26 +0200836 /*
837 * There may already be some PWM allocated, so we can't force
838 * mvpwm->chip.base to a fixed point like mvchip->chip.base.
839 * So, we let pwmchip_add() do the numbering and take the next free
840 * region.
841 */
842 mvpwm->chip.base = -1;
Andrew Lunn757642f2017-04-14 17:40:52 +0200843
844 spin_lock_init(&mvpwm->lock);
845
846 return pwmchip_add(&mvpwm->chip);
847}
848
Simon Guinota4ba5e12013-03-24 15:45:29 +0100849#ifdef CONFIG_DEBUG_FS
850#include <linux/seq_file.h>
851
852static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
853{
Linus Walleijbbe76002015-12-07 11:09:24 +0100854 struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100855 u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
Andy Shevchenko86661fd2020-06-15 18:05:43 +0300856 const char *label;
Simon Guinota4ba5e12013-03-24 15:45:29 +0100857 int i;
858
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200859 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &out);
860 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &io_conf);
861 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &blink);
862 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
863 regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200864 cause = mvebu_gpio_read_edge_cause(mvchip);
865 edg_msk = mvebu_gpio_read_edge_mask(mvchip);
866 lvl_msk = mvebu_gpio_read_level_mask(mvchip);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100867
Andy Shevchenko86661fd2020-06-15 18:05:43 +0300868 for_each_requested_gpio(chip, i, label) {
Simon Guinota4ba5e12013-03-24 15:45:29 +0100869 u32 msk;
870 bool is_out;
871
Ralph Sennhauserd2cabc42017-03-17 18:44:06 +0100872 msk = BIT(i);
Simon Guinota4ba5e12013-03-24 15:45:29 +0100873 is_out = !(io_conf & msk);
874
875 seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
876
877 if (is_out) {
878 seq_printf(s, " out %s %s\n",
879 out & msk ? "hi" : "lo",
880 blink & msk ? "(blink )" : "");
881 continue;
882 }
883
884 seq_printf(s, " in %s (act %s) - IRQ",
885 (data_in ^ in_pol) & msk ? "hi" : "lo",
886 in_pol & msk ? "lo" : "hi");
887 if (!((edg_msk | lvl_msk) & msk)) {
Andrew Lunna4319a62015-01-10 00:34:47 +0100888 seq_puts(s, " disabled\n");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100889 continue;
890 }
891 if (edg_msk & msk)
Andrew Lunna4319a62015-01-10 00:34:47 +0100892 seq_puts(s, " edge ");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100893 if (lvl_msk & msk)
Andrew Lunna4319a62015-01-10 00:34:47 +0100894 seq_puts(s, " level");
Simon Guinota4ba5e12013-03-24 15:45:29 +0100895 seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
896 }
897}
898#else
899#define mvebu_gpio_dbg_show NULL
900#endif
901
Jingoo Han271b17b2014-05-07 18:06:08 +0900902static const struct of_device_id mvebu_gpio_of_match[] = {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200903 {
904 .compatible = "marvell,orion-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100905 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200906 },
907 {
908 .compatible = "marvell,mv78200-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100909 .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200910 },
911 {
912 .compatible = "marvell,armadaxp-gpio",
Andrew Lunna4319a62015-01-10 00:34:47 +0100913 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200914 },
915 {
Ralph Sennhauser6c7515c2017-06-01 22:08:20 +0200916 .compatible = "marvell,armada-370-gpio",
Andrew Lunn757642f2017-04-14 17:40:52 +0200917 .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
918 },
919 {
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200920 .compatible = "marvell,armada-8k-gpio",
921 .data = (void *) MVEBU_GPIO_SOC_VARIANT_A8K,
922 },
923 {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200924 /* sentinel */
925 },
926};
Thomas Petazzonifefe7b02012-09-19 22:52:58 +0200927
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200928static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
929{
930 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
931 int i;
932
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200933 regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
934 &mvchip->out_reg);
935 regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
936 &mvchip->io_conf_reg);
937 regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
938 &mvchip->blink_en_reg);
939 regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
940 &mvchip->in_pol_reg);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200941
942 switch (mvchip->soc_variant) {
943 case MVEBU_GPIO_SOC_VARIANT_ORION:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200944 case MVEBU_GPIO_SOC_VARIANT_A8K:
945 regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200946 &mvchip->edge_mask_regs[0]);
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200947 regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200948 &mvchip->level_mask_regs[0]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200949 break;
950 case MVEBU_GPIO_SOC_VARIANT_MV78200:
951 for (i = 0; i < 2; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200952 regmap_read(mvchip->regs,
953 GPIO_EDGE_MASK_MV78200_OFF(i),
954 &mvchip->edge_mask_regs[i]);
955 regmap_read(mvchip->regs,
956 GPIO_LEVEL_MASK_MV78200_OFF(i),
957 &mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200958 }
959 break;
960 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
961 for (i = 0; i < 4; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200962 regmap_read(mvchip->regs,
963 GPIO_EDGE_MASK_ARMADAXP_OFF(i),
964 &mvchip->edge_mask_regs[i]);
965 regmap_read(mvchip->regs,
966 GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
967 &mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200968 }
969 break;
970 default:
971 BUG();
972 }
973
Andrew Lunn757642f2017-04-14 17:40:52 +0200974 if (IS_ENABLED(CONFIG_PWM))
975 mvebu_pwm_suspend(mvchip);
976
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200977 return 0;
978}
979
980static int mvebu_gpio_resume(struct platform_device *pdev)
981{
982 struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
983 int i;
984
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200985 regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
986 mvchip->out_reg);
987 regmap_write(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
988 mvchip->io_conf_reg);
989 regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
990 mvchip->blink_en_reg);
991 regmap_write(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
992 mvchip->in_pol_reg);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +0200993
994 switch (mvchip->soc_variant) {
995 case MVEBU_GPIO_SOC_VARIANT_ORION:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200996 case MVEBU_GPIO_SOC_VARIANT_A8K:
997 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +0200998 mvchip->edge_mask_regs[0]);
Gregory CLEMENTb6730b22017-06-12 17:34:59 +0200999 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001000 mvchip->level_mask_regs[0]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001001 break;
1002 case MVEBU_GPIO_SOC_VARIANT_MV78200:
1003 for (i = 0; i < 2; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001004 regmap_write(mvchip->regs,
1005 GPIO_EDGE_MASK_MV78200_OFF(i),
1006 mvchip->edge_mask_regs[i]);
1007 regmap_write(mvchip->regs,
1008 GPIO_LEVEL_MASK_MV78200_OFF(i),
1009 mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001010 }
1011 break;
1012 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
1013 for (i = 0; i < 4; i++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001014 regmap_write(mvchip->regs,
1015 GPIO_EDGE_MASK_ARMADAXP_OFF(i),
1016 mvchip->edge_mask_regs[i]);
1017 regmap_write(mvchip->regs,
1018 GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
1019 mvchip->level_mask_regs[i]);
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001020 }
1021 break;
1022 default:
1023 BUG();
1024 }
1025
Andrew Lunn757642f2017-04-14 17:40:52 +02001026 if (IS_ENABLED(CONFIG_PWM))
1027 mvebu_pwm_resume(mvchip);
1028
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001029 return 0;
1030}
1031
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001032static int mvebu_gpio_probe_raw(struct platform_device *pdev,
1033 struct mvebu_gpio_chip *mvchip)
1034{
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001035 void __iomem *base;
1036
Enrico Weigelt, metux IT consultdc02a0c2019-03-11 19:55:00 +01001037 base = devm_platform_ioremap_resource(pdev, 0);
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001038 if (IS_ERR(base))
1039 return PTR_ERR(base);
1040
1041 mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
1042 &mvebu_gpio_regmap_config);
1043 if (IS_ERR(mvchip->regs))
1044 return PTR_ERR(mvchip->regs);
1045
1046 /*
1047 * For the legacy SoCs, the regmap directly maps to the GPIO
1048 * registers, so no offset is needed.
1049 */
1050 mvchip->offset = 0;
1051
1052 /*
1053 * The Armada XP has a second range of registers for the
1054 * per-CPU registers
1055 */
1056 if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
Enrico Weigelt, metux IT consultdc02a0c2019-03-11 19:55:00 +01001057 base = devm_platform_ioremap_resource(pdev, 1);
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001058 if (IS_ERR(base))
1059 return PTR_ERR(base);
1060
1061 mvchip->percpu_regs =
1062 devm_regmap_init_mmio(&pdev->dev, base,
1063 &mvebu_gpio_regmap_config);
1064 if (IS_ERR(mvchip->percpu_regs))
1065 return PTR_ERR(mvchip->percpu_regs);
1066 }
1067
1068 return 0;
1069}
1070
1071static int mvebu_gpio_probe_syscon(struct platform_device *pdev,
1072 struct mvebu_gpio_chip *mvchip)
1073{
1074 mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node);
1075 if (IS_ERR(mvchip->regs))
1076 return PTR_ERR(mvchip->regs);
1077
1078 if (of_property_read_u32(pdev->dev.of_node, "offset", &mvchip->offset))
1079 return -EINVAL;
1080
1081 return 0;
1082}
1083
Bill Pemberton38363092012-11-19 13:22:34 -05001084static int mvebu_gpio_probe(struct platform_device *pdev)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001085{
1086 struct mvebu_gpio_chip *mvchip;
1087 const struct of_device_id *match;
1088 struct device_node *np = pdev->dev.of_node;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001089 struct irq_chip_generic *gc;
1090 struct irq_chip_type *ct;
1091 unsigned int ngpios;
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001092 bool have_irqs;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001093 int soc_variant;
1094 int i, cpu, id;
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001095 int err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001096
1097 match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
1098 if (match)
Russell Kingf0d50462017-01-10 22:53:28 +00001099 soc_variant = (unsigned long) match->data;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001100 else
1101 soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
1102
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001103 /* Some gpio controllers do not provide irq support */
Peng Fan0c216392019-12-04 09:24:35 +00001104 err = platform_irq_count(pdev);
1105 if (err < 0)
1106 return err;
1107
1108 have_irqs = err != 0;
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001109
Andrew Lunna4319a62015-01-10 00:34:47 +01001110 mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
1111 GFP_KERNEL);
Jingoo Han6c8365f2014-04-29 17:38:21 +09001112 if (!mvchip)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001113 return -ENOMEM;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001114
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001115 platform_set_drvdata(pdev, mvchip);
1116
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001117 if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
1118 dev_err(&pdev->dev, "Missing ngpios OF property\n");
1119 return -ENODEV;
1120 }
1121
1122 id = of_alias_get_id(pdev->dev.of_node, "gpio");
1123 if (id < 0) {
1124 dev_err(&pdev->dev, "Couldn't get OF id\n");
1125 return id;
1126 }
1127
Andrew Lunn757642f2017-04-14 17:40:52 +02001128 mvchip->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunnde887472013-02-03 11:34:26 +01001129 /* Not all SoCs require a clock.*/
Andrew Lunn757642f2017-04-14 17:40:52 +02001130 if (!IS_ERR(mvchip->clk))
1131 clk_prepare_enable(mvchip->clk);
Andrew Lunnde887472013-02-03 11:34:26 +01001132
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001133 mvchip->soc_variant = soc_variant;
1134 mvchip->chip.label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001135 mvchip->chip.parent = &pdev->dev;
Jonas Gorski203f0da2015-10-11 17:34:16 +02001136 mvchip->chip.request = gpiochip_generic_request;
1137 mvchip->chip.free = gpiochip_generic_free;
Baruch Siache8dacf52019-01-10 14:26:21 +02001138 mvchip->chip.get_direction = mvebu_gpio_get_direction;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001139 mvchip->chip.direction_input = mvebu_gpio_direction_input;
1140 mvchip->chip.get = mvebu_gpio_get;
1141 mvchip->chip.direction_output = mvebu_gpio_direction_output;
1142 mvchip->chip.set = mvebu_gpio_set;
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001143 if (have_irqs)
1144 mvchip->chip.to_irq = mvebu_gpio_to_irq;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001145 mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
1146 mvchip->chip.ngpio = ngpios;
Linus Walleij9fb1f392013-12-04 14:42:46 +01001147 mvchip->chip.can_sleep = false;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001148 mvchip->chip.of_node = np;
Simon Guinota4ba5e12013-03-24 15:45:29 +01001149 mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001150
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001151 if (soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K)
1152 err = mvebu_gpio_probe_syscon(pdev, mvchip);
1153 else
1154 err = mvebu_gpio_probe_raw(pdev, mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001155
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001156 if (err)
1157 return err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001158
1159 /*
1160 * Mask and clear GPIO interrupts.
1161 */
Laurent Navetf4dcd2d92013-03-20 13:15:56 +01001162 switch (soc_variant) {
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001163 case MVEBU_GPIO_SOC_VARIANT_ORION:
Gregory CLEMENTb6730b22017-06-12 17:34:59 +02001164 case MVEBU_GPIO_SOC_VARIANT_A8K:
1165 regmap_write(mvchip->regs,
1166 GPIO_EDGE_CAUSE_OFF + mvchip->offset, 0);
1167 regmap_write(mvchip->regs,
1168 GPIO_EDGE_MASK_OFF + mvchip->offset, 0);
1169 regmap_write(mvchip->regs,
1170 GPIO_LEVEL_MASK_OFF + mvchip->offset, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001171 break;
1172 case MVEBU_GPIO_SOC_VARIANT_MV78200:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001173 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001174 for (cpu = 0; cpu < 2; cpu++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001175 regmap_write(mvchip->regs,
1176 GPIO_EDGE_MASK_MV78200_OFF(cpu), 0);
1177 regmap_write(mvchip->regs,
1178 GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001179 }
1180 break;
1181 case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001182 regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
1183 regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0);
1184 regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001185 for (cpu = 0; cpu < 4; cpu++) {
Thomas Petazzoni2233bf72017-05-19 18:09:21 +02001186 regmap_write(mvchip->percpu_regs,
1187 GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu), 0);
1188 regmap_write(mvchip->percpu_regs,
1189 GPIO_EDGE_MASK_ARMADAXP_OFF(cpu), 0);
1190 regmap_write(mvchip->percpu_regs,
1191 GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu), 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001192 }
1193 break;
1194 default:
1195 BUG();
1196 }
1197
Laxman Dewangan00b9ab42016-02-22 17:43:28 +05301198 devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001199
Baruch Siach7ee1a01e2020-12-02 09:15:32 +02001200 /* Some MVEBU SoCs have simple PWM support for GPIO lines */
1201 if (IS_ENABLED(CONFIG_PWM)) {
1202 err = mvebu_pwm_probe(pdev, mvchip, id);
1203 if (err)
1204 return err;
1205 }
1206
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001207 /* Some gpio controllers do not provide irq support */
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001208 if (!have_irqs)
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001209 return 0;
1210
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001211 mvchip->domain =
1212 irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
1213 if (!mvchip->domain) {
1214 dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
1215 mvchip->chip.label);
Baruch Siach7ee1a01e2020-12-02 09:15:32 +02001216 err = -ENODEV;
1217 goto err_pwm;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001218 }
1219
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001220 err = irq_alloc_domain_generic_chips(
1221 mvchip->domain, ngpios, 2, np->name, handle_level_irq,
1222 IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
1223 if (err) {
1224 dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
1225 mvchip->chip.label);
1226 goto err_domain;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001227 }
1228
Ralph Sennhauser899c37e2017-03-16 07:33:57 +01001229 /*
1230 * NOTE: The common accessors cannot be used because of the percpu
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001231 * access to the mask registers
1232 */
1233 gc = irq_get_domain_generic_chip(mvchip->domain, 0);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001234 gc->private = mvchip;
1235 ct = &gc->chip_types[0];
1236 ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
1237 ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
1238 ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
1239 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1240 ct->chip.name = mvchip->chip.label;
1241
1242 ct = &gc->chip_types[1];
1243 ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
1244 ct->chip.irq_ack = mvebu_gpio_irq_ack;
1245 ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
1246 ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
1247 ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
1248 ct->handler = handle_edge_irq;
1249 ct->chip.name = mvchip->chip.label;
1250
Ralph Sennhauser899c37e2017-03-16 07:33:57 +01001251 /*
1252 * Setup the interrupt handlers. Each chip can have up to 4
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001253 * interrupt handlers, with each handler dealing with 8 GPIO
1254 * pins.
1255 */
1256 for (i = 0; i < 4; i++) {
Chris Packham525b0852020-03-13 16:42:44 +13001257 int irq = platform_get_irq_optional(pdev, i);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001258
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001259 if (irq < 0)
1260 continue;
1261 irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
1262 mvchip);
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001263 }
1264
1265 return 0;
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001266
Jason Gunthorpe812d4782016-10-19 15:03:41 -06001267err_domain:
1268 irq_domain_remove(mvchip->domain);
Baruch Siach7ee1a01e2020-12-02 09:15:32 +02001269err_pwm:
1270 pwmchip_remove(&mvchip->mvpwm->chip);
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001271
Andrew Lunnf1d2d082015-01-10 00:34:48 +01001272 return err;
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001273}
1274
1275static struct platform_driver mvebu_gpio_driver = {
1276 .driver = {
Andrew Lunna4319a62015-01-10 00:34:47 +01001277 .name = "mvebu-gpio",
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001278 .of_match_table = mvebu_gpio_of_match,
1279 },
1280 .probe = mvebu_gpio_probe,
Thomas Petazzonib5b7b482014-10-24 13:59:19 +02001281 .suspend = mvebu_gpio_suspend,
1282 .resume = mvebu_gpio_resume,
Thomas Petazzonifefe7b02012-09-19 22:52:58 +02001283};
Paul Gortmakered329f32016-03-27 11:44:45 -04001284builtin_platform_driver(mvebu_gpio_driver);