blob: 9defe25d472165308b1a280a780fb250ec0aed8a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joel Stanley361b7912016-08-30 17:24:27 +09302/*
3 * Copyright 2015 IBM Corp.
4 *
5 * Joel Stanley <joel@jms.id.au>
Joel Stanley361b7912016-08-30 17:24:27 +09306 */
7
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09308#include <asm/div64.h>
9#include <linux/clk.h>
10#include <linux/gpio/driver.h>
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +100011#include <linux/gpio/aspeed.h>
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093012#include <linux/hashtable.h>
Joel Stanley361b7912016-08-30 17:24:27 +093013#include <linux/init.h>
14#include <linux/io.h>
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093015#include <linux/kernel.h>
16#include <linux/module.h>
Joel Stanley361b7912016-08-30 17:24:27 +093017#include <linux/pinctrl/consumer.h>
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093018#include <linux/platform_device.h>
19#include <linux/spinlock.h>
20#include <linux/string.h>
Joel Stanley361b7912016-08-30 17:24:27 +093021
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +100022/*
23 * These two headers aren't meant to be used by GPIO drivers. We need
24 * them in order to access gpio_chip_hwgpio() which we need to implement
25 * the aspeed specific API which allows the coprocessor to request
26 * access to some GPIOs and to arbitrate between coprocessor and ARM.
27 */
28#include <linux/gpio/consumer.h>
29#include "gpiolib.h"
30
Andrew Jeffery1736f752017-01-24 16:46:46 +103031struct aspeed_bank_props {
32 unsigned int bank;
33 u32 input;
34 u32 output;
35};
36
37struct aspeed_gpio_config {
38 unsigned int nr_gpios;
39 const struct aspeed_bank_props *props;
40};
41
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093042/*
43 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
44 * @timer_users: Tracks the number of users for each timer
45 *
46 * The @timer_users has four elements but the first element is unused. This is
47 * to simplify accounting and indexing, as a zero value in @offset_timer
48 * represents disabled debouncing for the GPIO. Any other value for an element
49 * of @offset_timer is used as an index into @timer_users. This behaviour of
50 * the zero value aligns with the behaviour of zero built from the timer
51 * configuration registers (i.e. debouncing is disabled).
52 */
Joel Stanley361b7912016-08-30 17:24:27 +093053struct aspeed_gpio {
54 struct gpio_chip chip;
55 spinlock_t lock;
56 void __iomem *base;
57 int irq;
Andrew Jeffery1736f752017-01-24 16:46:46 +103058 const struct aspeed_gpio_config *config;
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093059
60 u8 *offset_timer;
61 unsigned int timer_users[4];
62 struct clk *clk;
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +100063
64 u32 *dcache;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +100065 u8 *cf_copro_bankmap;
Joel Stanley361b7912016-08-30 17:24:27 +093066};
67
68struct aspeed_gpio_bank {
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100069 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
70 * +4: Rd/Wr: Direction (0=in, 1=out)
71 */
72 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
Joel Stanley361b7912016-08-30 17:24:27 +093073 uint16_t irq_regs;
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093074 uint16_t debounce_regs;
Andrew Jeffery1b43d262017-11-30 14:25:25 +103075 uint16_t tolerance_regs;
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +100076 uint16_t cmdsrc_regs;
Joel Stanley7153f8e2017-01-23 15:56:06 +103077 const char names[4][3];
Joel Stanley361b7912016-08-30 17:24:27 +093078};
79
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100080/*
81 * Note: The "value" register returns the input value sampled on the
82 * line even when the GPIO is configured as an output. Since
83 * that input goes through synchronizers, writing, then reading
84 * back may not return the written value right away.
85 *
86 * The "rdata" register returns the content of the write latch
87 * and thus can be used to read back what was last written
88 * reliably.
89 */
90
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +093091static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
92
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +100093static const struct aspeed_gpio_copro_ops *copro_ops;
94static void *copro_data;
95
Joel Stanley361b7912016-08-30 17:24:27 +093096static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
97 {
98 .val_regs = 0x0000,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100099 .rdata_reg = 0x00c0,
Joel Stanley361b7912016-08-30 17:24:27 +0930100 .irq_regs = 0x0008,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930101 .debounce_regs = 0x0040,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030102 .tolerance_regs = 0x001c,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000103 .cmdsrc_regs = 0x0060,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030104 .names = { "A", "B", "C", "D" },
Joel Stanley361b7912016-08-30 17:24:27 +0930105 },
106 {
107 .val_regs = 0x0020,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000108 .rdata_reg = 0x00c4,
Joel Stanley361b7912016-08-30 17:24:27 +0930109 .irq_regs = 0x0028,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930110 .debounce_regs = 0x0048,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030111 .tolerance_regs = 0x003c,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000112 .cmdsrc_regs = 0x0068,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030113 .names = { "E", "F", "G", "H" },
Joel Stanley361b7912016-08-30 17:24:27 +0930114 },
115 {
116 .val_regs = 0x0070,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000117 .rdata_reg = 0x00c8,
Joel Stanley361b7912016-08-30 17:24:27 +0930118 .irq_regs = 0x0098,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930119 .debounce_regs = 0x00b0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030120 .tolerance_regs = 0x00ac,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000121 .cmdsrc_regs = 0x0090,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030122 .names = { "I", "J", "K", "L" },
Joel Stanley361b7912016-08-30 17:24:27 +0930123 },
124 {
125 .val_regs = 0x0078,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000126 .rdata_reg = 0x00cc,
Joel Stanley361b7912016-08-30 17:24:27 +0930127 .irq_regs = 0x00e8,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930128 .debounce_regs = 0x0100,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030129 .tolerance_regs = 0x00fc,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000130 .cmdsrc_regs = 0x00e0,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030131 .names = { "M", "N", "O", "P" },
Joel Stanley361b7912016-08-30 17:24:27 +0930132 },
133 {
134 .val_regs = 0x0080,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000135 .rdata_reg = 0x00d0,
Joel Stanley361b7912016-08-30 17:24:27 +0930136 .irq_regs = 0x0118,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930137 .debounce_regs = 0x0130,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030138 .tolerance_regs = 0x012c,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000139 .cmdsrc_regs = 0x0110,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030140 .names = { "Q", "R", "S", "T" },
Joel Stanley361b7912016-08-30 17:24:27 +0930141 },
142 {
143 .val_regs = 0x0088,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000144 .rdata_reg = 0x00d4,
Joel Stanley361b7912016-08-30 17:24:27 +0930145 .irq_regs = 0x0148,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930146 .debounce_regs = 0x0160,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030147 .tolerance_regs = 0x015c,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000148 .cmdsrc_regs = 0x0140,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030149 .names = { "U", "V", "W", "X" },
Joel Stanley361b7912016-08-30 17:24:27 +0930150 },
Andrew Jeffery1736f752017-01-24 16:46:46 +1030151 {
152 .val_regs = 0x01E0,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000153 .rdata_reg = 0x00d8,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030154 .irq_regs = 0x0178,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930155 .debounce_regs = 0x0190,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030156 .tolerance_regs = 0x018c,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000157 .cmdsrc_regs = 0x0170,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030158 .names = { "Y", "Z", "AA", "AB" },
159 },
160 {
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030161 .val_regs = 0x01e8,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000162 .rdata_reg = 0x00dc,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030163 .irq_regs = 0x01a8,
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930164 .debounce_regs = 0x01c0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030165 .tolerance_regs = 0x01bc,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000166 .cmdsrc_regs = 0x01a0,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030167 .names = { "AC", "", "", "" },
168 },
Joel Stanley361b7912016-08-30 17:24:27 +0930169};
170
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000171enum aspeed_gpio_reg {
172 reg_val,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000173 reg_rdata,
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000174 reg_dir,
175 reg_irq_enable,
176 reg_irq_type0,
177 reg_irq_type1,
178 reg_irq_type2,
179 reg_irq_status,
180 reg_debounce_sel1,
181 reg_debounce_sel2,
182 reg_tolerance,
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000183 reg_cmdsrc0,
184 reg_cmdsrc1,
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000185};
Joel Stanley361b7912016-08-30 17:24:27 +0930186
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000187#define GPIO_VAL_VALUE 0x00
188#define GPIO_VAL_DIR 0x04
Joel Stanley361b7912016-08-30 17:24:27 +0930189
190#define GPIO_IRQ_ENABLE 0x00
191#define GPIO_IRQ_TYPE0 0x04
192#define GPIO_IRQ_TYPE1 0x08
193#define GPIO_IRQ_TYPE2 0x0c
194#define GPIO_IRQ_STATUS 0x10
195
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930196#define GPIO_DEBOUNCE_SEL1 0x00
197#define GPIO_DEBOUNCE_SEL2 0x04
198
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000199#define GPIO_CMDSRC_0 0x00
200#define GPIO_CMDSRC_1 0x04
201#define GPIO_CMDSRC_ARM 0
202#define GPIO_CMDSRC_LPC 1
203#define GPIO_CMDSRC_COLDFIRE 2
204#define GPIO_CMDSRC_RESERVED 3
205
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000206/* This will be resolved at compile time */
207static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
208 const struct aspeed_gpio_bank *bank,
209 const enum aspeed_gpio_reg reg)
210{
211 switch (reg) {
212 case reg_val:
213 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000214 case reg_rdata:
215 return gpio->base + bank->rdata_reg;
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000216 case reg_dir:
217 return gpio->base + bank->val_regs + GPIO_VAL_DIR;
218 case reg_irq_enable:
219 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
220 case reg_irq_type0:
221 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
222 case reg_irq_type1:
223 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
224 case reg_irq_type2:
225 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
226 case reg_irq_status:
227 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
228 case reg_debounce_sel1:
229 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
230 case reg_debounce_sel2:
231 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
232 case reg_tolerance:
233 return gpio->base + bank->tolerance_regs;
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000234 case reg_cmdsrc0:
235 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
236 case reg_cmdsrc1:
237 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000238 }
Arnd Bergmannc2967732018-07-09 16:56:03 +0200239 BUG();
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000240}
241
242#define GPIO_BANK(x) ((x) >> 5)
243#define GPIO_OFFSET(x) ((x) & 0x1f)
244#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
245
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930246#define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
247#define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
248#define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
249
Joel Stanley361b7912016-08-30 17:24:27 +0930250static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
251{
252 unsigned int bank = GPIO_BANK(offset);
253
Vasyl Gomonovychfe138622017-12-21 16:55:10 +0100254 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
Joel Stanley361b7912016-08-30 17:24:27 +0930255 return &aspeed_gpio_banks[bank];
256}
257
Andrew Jeffery1736f752017-01-24 16:46:46 +1030258static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
259{
260 return !(props->input || props->output);
261}
262
263static inline const struct aspeed_bank_props *find_bank_props(
264 struct aspeed_gpio *gpio, unsigned int offset)
265{
266 const struct aspeed_bank_props *props = gpio->config->props;
267
268 while (!is_bank_props_sentinel(props)) {
269 if (props->bank == GPIO_BANK(offset))
270 return props;
271 props++;
272 }
273
274 return NULL;
275}
276
277static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
278{
279 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
280 const struct aspeed_gpio_bank *bank = to_bank(offset);
281 unsigned int group = GPIO_OFFSET(offset) / 8;
282
283 return bank->names[group][0] != '\0' &&
284 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
285}
286
287static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
288{
289 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
290
291 return !props || (props->input & GPIO_BIT(offset));
292}
293
294#define have_irq(g, o) have_input((g), (o))
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930295#define have_debounce(g, o) have_input((g), (o))
Andrew Jeffery1736f752017-01-24 16:46:46 +1030296
297static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
298{
299 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
300
301 return !props || (props->output & GPIO_BIT(offset));
302}
303
Benjamin Herrenschmidt0f1e03c2018-06-29 14:11:18 +1000304static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio,
305 const struct aspeed_gpio_bank *bank,
306 int bindex, int cmdsrc)
307{
308 void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0);
309 void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1);
310 u32 bit, reg;
311
312 /*
313 * Each register controls 4 banks, so take the bottom 2
314 * bits of the bank index, and use them to select the
315 * right control bit (0, 8, 16 or 24).
316 */
317 bit = BIT((bindex & 3) << 3);
318
319 /* Source 1 first to avoid illegal 11 combination */
320 reg = ioread32(c1);
321 if (cmdsrc & 2)
322 reg |= bit;
323 else
324 reg &= ~bit;
325 iowrite32(reg, c1);
326
327 /* Then Source 0 */
328 reg = ioread32(c0);
329 if (cmdsrc & 1)
330 reg |= bit;
331 else
332 reg &= ~bit;
333 iowrite32(reg, c0);
334}
335
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000336static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
337 unsigned int offset)
338{
339 const struct aspeed_gpio_bank *bank = to_bank(offset);
340
341 if (!copro_ops || !gpio->cf_copro_bankmap)
342 return false;
343 if (!gpio->cf_copro_bankmap[offset >> 3])
344 return false;
345 if (!copro_ops->request_access)
346 return false;
347
348 /* Pause the coprocessor */
349 copro_ops->request_access(copro_data);
350
351 /* Change command source back to ARM */
352 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM);
353
354 /* Update cache */
355 gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata));
356
357 return true;
358}
359
360static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
361 unsigned int offset)
362{
363 const struct aspeed_gpio_bank *bank = to_bank(offset);
364
365 if (!copro_ops || !gpio->cf_copro_bankmap)
366 return;
367 if (!gpio->cf_copro_bankmap[offset >> 3])
368 return;
369 if (!copro_ops->release_access)
370 return;
371
372 /* Change command source back to ColdFire */
373 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3,
374 GPIO_CMDSRC_COLDFIRE);
375
376 /* Restart the coprocessor */
377 copro_ops->release_access(copro_data);
378}
379
Joel Stanley361b7912016-08-30 17:24:27 +0930380static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
381{
382 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
383 const struct aspeed_gpio_bank *bank = to_bank(offset);
384
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000385 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
Joel Stanley361b7912016-08-30 17:24:27 +0930386}
387
388static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
389 int val)
390{
391 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
392 const struct aspeed_gpio_bank *bank = to_bank(offset);
393 void __iomem *addr;
394 u32 reg;
395
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000396 addr = bank_reg(gpio, bank, reg_val);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000397 reg = gpio->dcache[GPIO_BANK(offset)];
Joel Stanley361b7912016-08-30 17:24:27 +0930398
399 if (val)
400 reg |= GPIO_BIT(offset);
401 else
402 reg &= ~GPIO_BIT(offset);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000403 gpio->dcache[GPIO_BANK(offset)] = reg;
Joel Stanley361b7912016-08-30 17:24:27 +0930404
405 iowrite32(reg, addr);
406}
407
408static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
409 int val)
410{
411 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
412 unsigned long flags;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000413 bool copro;
Joel Stanley361b7912016-08-30 17:24:27 +0930414
415 spin_lock_irqsave(&gpio->lock, flags);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000416 copro = aspeed_gpio_copro_request(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930417
418 __aspeed_gpio_set(gc, offset, val);
419
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000420 if (copro)
421 aspeed_gpio_copro_release(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930422 spin_unlock_irqrestore(&gpio->lock, flags);
423}
424
425static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
426{
427 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
428 const struct aspeed_gpio_bank *bank = to_bank(offset);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000429 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
Joel Stanley361b7912016-08-30 17:24:27 +0930430 unsigned long flags;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000431 bool copro;
Joel Stanley361b7912016-08-30 17:24:27 +0930432 u32 reg;
433
Andrew Jeffery1736f752017-01-24 16:46:46 +1030434 if (!have_input(gpio, offset))
435 return -ENOTSUPP;
436
Joel Stanley361b7912016-08-30 17:24:27 +0930437 spin_lock_irqsave(&gpio->lock, flags);
438
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000439 reg = ioread32(addr);
440 reg &= ~GPIO_BIT(offset);
441
442 copro = aspeed_gpio_copro_request(gpio, offset);
443 iowrite32(reg, addr);
444 if (copro)
445 aspeed_gpio_copro_release(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930446
447 spin_unlock_irqrestore(&gpio->lock, flags);
448
449 return 0;
450}
451
452static int aspeed_gpio_dir_out(struct gpio_chip *gc,
453 unsigned int offset, int val)
454{
455 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
456 const struct aspeed_gpio_bank *bank = to_bank(offset);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000457 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
Joel Stanley361b7912016-08-30 17:24:27 +0930458 unsigned long flags;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000459 bool copro;
Joel Stanley361b7912016-08-30 17:24:27 +0930460 u32 reg;
461
Andrew Jeffery1736f752017-01-24 16:46:46 +1030462 if (!have_output(gpio, offset))
463 return -ENOTSUPP;
464
Joel Stanley361b7912016-08-30 17:24:27 +0930465 spin_lock_irqsave(&gpio->lock, flags);
466
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000467 reg = ioread32(addr);
468 reg |= GPIO_BIT(offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930469
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000470 copro = aspeed_gpio_copro_request(gpio, offset);
471 __aspeed_gpio_set(gc, offset, val);
472 iowrite32(reg, addr);
473
474 if (copro)
475 aspeed_gpio_copro_release(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930476 spin_unlock_irqrestore(&gpio->lock, flags);
477
478 return 0;
479}
480
481static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
482{
483 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
484 const struct aspeed_gpio_bank *bank = to_bank(offset);
485 unsigned long flags;
486 u32 val;
487
Andrew Jeffery1736f752017-01-24 16:46:46 +1030488 if (!have_input(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030489 return 0;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030490
491 if (!have_output(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030492 return 1;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030493
Joel Stanley361b7912016-08-30 17:24:27 +0930494 spin_lock_irqsave(&gpio->lock, flags);
495
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000496 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930497
498 spin_unlock_irqrestore(&gpio->lock, flags);
499
500 return !val;
501
502}
503
504static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000505 struct aspeed_gpio **gpio,
506 const struct aspeed_gpio_bank **bank,
507 u32 *bit, int *offset)
Joel Stanley361b7912016-08-30 17:24:27 +0930508{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030509 struct aspeed_gpio *internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930510
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000511 *offset = irqd_to_hwirq(d);
Joel Stanley361b7912016-08-30 17:24:27 +0930512
Andrew Jeffery1736f752017-01-24 16:46:46 +1030513 internal = irq_data_get_irq_chip_data(d);
514
515 /* This might be a bit of a questionable place to check */
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000516 if (!have_irq(internal, *offset))
Andrew Jeffery1736f752017-01-24 16:46:46 +1030517 return -ENOTSUPP;
518
519 *gpio = internal;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000520 *bank = to_bank(*offset);
521 *bit = GPIO_BIT(*offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930522
523 return 0;
524}
525
526static void aspeed_gpio_irq_ack(struct irq_data *d)
527{
528 const struct aspeed_gpio_bank *bank;
529 struct aspeed_gpio *gpio;
530 unsigned long flags;
531 void __iomem *status_addr;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000532 int rc, offset;
533 bool copro;
Joel Stanley361b7912016-08-30 17:24:27 +0930534 u32 bit;
Joel Stanley361b7912016-08-30 17:24:27 +0930535
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000536 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930537 if (rc)
538 return;
539
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000540 status_addr = bank_reg(gpio, bank, reg_irq_status);
Joel Stanley361b7912016-08-30 17:24:27 +0930541
542 spin_lock_irqsave(&gpio->lock, flags);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000543 copro = aspeed_gpio_copro_request(gpio, offset);
544
Joel Stanley361b7912016-08-30 17:24:27 +0930545 iowrite32(bit, status_addr);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000546
547 if (copro)
548 aspeed_gpio_copro_release(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930549 spin_unlock_irqrestore(&gpio->lock, flags);
550}
551
552static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
553{
554 const struct aspeed_gpio_bank *bank;
555 struct aspeed_gpio *gpio;
556 unsigned long flags;
557 u32 reg, bit;
558 void __iomem *addr;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000559 int rc, offset;
560 bool copro;
Joel Stanley361b7912016-08-30 17:24:27 +0930561
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000562 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930563 if (rc)
564 return;
565
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000566 addr = bank_reg(gpio, bank, reg_irq_enable);
Joel Stanley361b7912016-08-30 17:24:27 +0930567
568 spin_lock_irqsave(&gpio->lock, flags);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000569 copro = aspeed_gpio_copro_request(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930570
571 reg = ioread32(addr);
572 if (set)
573 reg |= bit;
574 else
Govert Overgaauwf2416322018-04-06 14:41:35 +0200575 reg &= ~bit;
Joel Stanley361b7912016-08-30 17:24:27 +0930576 iowrite32(reg, addr);
577
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000578 if (copro)
579 aspeed_gpio_copro_release(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930580 spin_unlock_irqrestore(&gpio->lock, flags);
581}
582
583static void aspeed_gpio_irq_mask(struct irq_data *d)
584{
585 aspeed_gpio_irq_set_mask(d, false);
586}
587
588static void aspeed_gpio_irq_unmask(struct irq_data *d)
589{
590 aspeed_gpio_irq_set_mask(d, true);
591}
592
593static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
594{
595 u32 type0 = 0;
596 u32 type1 = 0;
597 u32 type2 = 0;
598 u32 bit, reg;
599 const struct aspeed_gpio_bank *bank;
600 irq_flow_handler_t handler;
601 struct aspeed_gpio *gpio;
602 unsigned long flags;
603 void __iomem *addr;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000604 int rc, offset;
605 bool copro;
Joel Stanley361b7912016-08-30 17:24:27 +0930606
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000607 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930608 if (rc)
609 return -EINVAL;
610
611 switch (type & IRQ_TYPE_SENSE_MASK) {
612 case IRQ_TYPE_EDGE_BOTH:
613 type2 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500614 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930615 case IRQ_TYPE_EDGE_RISING:
616 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500617 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930618 case IRQ_TYPE_EDGE_FALLING:
619 handler = handle_edge_irq;
620 break;
621 case IRQ_TYPE_LEVEL_HIGH:
622 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500623 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930624 case IRQ_TYPE_LEVEL_LOW:
625 type1 |= bit;
626 handler = handle_level_irq;
627 break;
628 default:
629 return -EINVAL;
630 }
631
632 spin_lock_irqsave(&gpio->lock, flags);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000633 copro = aspeed_gpio_copro_request(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930634
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000635 addr = bank_reg(gpio, bank, reg_irq_type0);
Joel Stanley361b7912016-08-30 17:24:27 +0930636 reg = ioread32(addr);
637 reg = (reg & ~bit) | type0;
638 iowrite32(reg, addr);
639
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000640 addr = bank_reg(gpio, bank, reg_irq_type1);
Joel Stanley361b7912016-08-30 17:24:27 +0930641 reg = ioread32(addr);
642 reg = (reg & ~bit) | type1;
643 iowrite32(reg, addr);
644
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000645 addr = bank_reg(gpio, bank, reg_irq_type2);
Joel Stanley361b7912016-08-30 17:24:27 +0930646 reg = ioread32(addr);
647 reg = (reg & ~bit) | type2;
648 iowrite32(reg, addr);
649
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000650 if (copro)
651 aspeed_gpio_copro_release(gpio, offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930652 spin_unlock_irqrestore(&gpio->lock, flags);
653
654 irq_set_handler_locked(d, handler);
655
656 return 0;
657}
658
659static void aspeed_gpio_irq_handler(struct irq_desc *desc)
660{
661 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
662 struct irq_chip *ic = irq_desc_get_chip(desc);
663 struct aspeed_gpio *data = gpiochip_get_data(gc);
664 unsigned int i, p, girq;
665 unsigned long reg;
666
667 chained_irq_enter(ic, desc);
668
669 for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
670 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
671
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000672 reg = ioread32(bank_reg(data, bank, reg_irq_status));
Joel Stanley361b7912016-08-30 17:24:27 +0930673
674 for_each_set_bit(p, &reg, 32) {
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100675 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
Joel Stanley361b7912016-08-30 17:24:27 +0930676 generic_handle_irq(girq);
677 }
678
679 }
680
681 chained_irq_exit(ic, desc);
682}
683
684static struct irq_chip aspeed_gpio_irqchip = {
685 .name = "aspeed-gpio",
686 .irq_ack = aspeed_gpio_irq_ack,
687 .irq_mask = aspeed_gpio_irq_mask,
688 .irq_unmask = aspeed_gpio_irq_unmask,
689 .irq_set_type = aspeed_gpio_set_type,
690};
691
Andrew Jeffery1736f752017-01-24 16:46:46 +1030692static void set_irq_valid_mask(struct aspeed_gpio *gpio)
693{
694 const struct aspeed_bank_props *props = gpio->config->props;
695
696 while (!is_bank_props_sentinel(props)) {
697 unsigned int offset;
698 const unsigned long int input = props->input;
699
700 /* Pretty crummy approach, but similar to GPIO core */
701 for_each_clear_bit(offset, &input, 32) {
702 unsigned int i = props->bank * 32 + offset;
703
704 if (i >= gpio->config->nr_gpios)
705 break;
706
Thierry Redingdc7b0382017-11-07 19:15:52 +0100707 clear_bit(i, gpio->chip.irq.valid_mask);
Andrew Jeffery1736f752017-01-24 16:46:46 +1030708 }
709
710 props++;
711 }
712}
713
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030714static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
715 unsigned int offset, bool enable)
716{
717 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030718 unsigned long flags;
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000719 void __iomem *treg;
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000720 bool copro;
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030721 u32 val;
722
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000723 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030724
725 spin_lock_irqsave(&gpio->lock, flags);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000726 copro = aspeed_gpio_copro_request(gpio, offset);
727
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000728 val = readl(treg);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030729
730 if (enable)
731 val |= GPIO_BIT(offset);
732 else
733 val &= ~GPIO_BIT(offset);
734
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000735 writel(val, treg);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000736
737 if (copro)
738 aspeed_gpio_copro_release(gpio, offset);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030739 spin_unlock_irqrestore(&gpio->lock, flags);
740
741 return 0;
742}
743
Joel Stanley361b7912016-08-30 17:24:27 +0930744static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
745{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030746 if (!have_gpio(gpiochip_get_data(chip), offset))
747 return -ENODEV;
748
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200749 return pinctrl_gpio_request(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930750}
751
752static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
753{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200754 pinctrl_gpio_free(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930755}
756
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930757static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
758 u32 *cycles)
759{
760 u64 rate;
761 u64 n;
762 u32 r;
763
764 rate = clk_get_rate(gpio->clk);
765 if (!rate)
766 return -ENOTSUPP;
767
768 n = rate * usecs;
769 r = do_div(n, 1000000);
770
771 if (n >= U32_MAX)
772 return -ERANGE;
773
774 /* At least as long as the requested time */
775 *cycles = n + (!!r);
776
777 return 0;
778}
779
780/* Call under gpio->lock */
781static int register_allocated_timer(struct aspeed_gpio *gpio,
782 unsigned int offset, unsigned int timer)
783{
784 if (WARN(gpio->offset_timer[offset] != 0,
785 "Offset %d already allocated timer %d\n",
786 offset, gpio->offset_timer[offset]))
787 return -EINVAL;
788
789 if (WARN(gpio->timer_users[timer] == UINT_MAX,
790 "Timer user count would overflow\n"))
791 return -EPERM;
792
793 gpio->offset_timer[offset] = timer;
794 gpio->timer_users[timer]++;
795
796 return 0;
797}
798
799/* Call under gpio->lock */
800static int unregister_allocated_timer(struct aspeed_gpio *gpio,
801 unsigned int offset)
802{
803 if (WARN(gpio->offset_timer[offset] == 0,
804 "No timer allocated to offset %d\n", offset))
805 return -EINVAL;
806
807 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
808 "No users recorded for timer %d\n",
809 gpio->offset_timer[offset]))
810 return -EINVAL;
811
812 gpio->timer_users[gpio->offset_timer[offset]]--;
813 gpio->offset_timer[offset] = 0;
814
815 return 0;
816}
817
818/* Call under gpio->lock */
819static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
820 unsigned int offset)
821{
822 return gpio->offset_timer[offset] > 0;
823}
824
825/* Call under gpio->lock */
826static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
827 unsigned int timer)
828{
829 const struct aspeed_gpio_bank *bank = to_bank(offset);
830 const u32 mask = GPIO_BIT(offset);
831 void __iomem *addr;
832 u32 val;
833
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000834 /* Note: Debounce timer isn't under control of the command
835 * source registers, so no need to sync with the coprocessor
836 */
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000837 addr = bank_reg(gpio, bank, reg_debounce_sel1);
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930838 val = ioread32(addr);
839 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
840
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000841 addr = bank_reg(gpio, bank, reg_debounce_sel2);
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930842 val = ioread32(addr);
843 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
844}
845
846static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
847 unsigned long usecs)
848{
849 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
850 u32 requested_cycles;
851 unsigned long flags;
852 int rc;
853 int i;
854
Joel Stanleydf563c82017-05-02 15:38:24 +0930855 if (!gpio->clk)
856 return -EINVAL;
857
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930858 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
859 if (rc < 0) {
860 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
861 usecs, clk_get_rate(gpio->clk), rc);
862 return rc;
863 }
864
865 spin_lock_irqsave(&gpio->lock, flags);
866
867 if (timer_allocation_registered(gpio, offset)) {
868 rc = unregister_allocated_timer(gpio, offset);
869 if (rc < 0)
870 goto out;
871 }
872
873 /* Try to find a timer already configured for the debounce period */
874 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
875 u32 cycles;
876
877 cycles = ioread32(gpio->base + debounce_timers[i]);
878 if (requested_cycles == cycles)
879 break;
880 }
881
882 if (i == ARRAY_SIZE(debounce_timers)) {
883 int j;
884
885 /*
886 * As there are no timers configured for the requested debounce
887 * period, find an unused timer instead
888 */
889 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
890 if (gpio->timer_users[j] == 0)
891 break;
892 }
893
894 if (j == ARRAY_SIZE(gpio->timer_users)) {
895 dev_warn(chip->parent,
896 "Debounce timers exhausted, cannot debounce for period %luus\n",
897 usecs);
898
899 rc = -EPERM;
900
901 /*
902 * We already adjusted the accounting to remove @offset
903 * as a user of its previous timer, so also configure
904 * the hardware so @offset has timers disabled for
905 * consistency.
906 */
907 configure_timer(gpio, offset, 0);
908 goto out;
909 }
910
911 i = j;
912
913 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
914 }
915
916 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
917 rc = -EINVAL;
918 goto out;
919 }
920
921 register_allocated_timer(gpio, offset, i);
922 configure_timer(gpio, offset, i);
923
924out:
925 spin_unlock_irqrestore(&gpio->lock, flags);
926
927 return rc;
928}
929
930static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
931{
932 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
933 unsigned long flags;
934 int rc;
935
936 spin_lock_irqsave(&gpio->lock, flags);
937
938 rc = unregister_allocated_timer(gpio, offset);
939 if (!rc)
940 configure_timer(gpio, offset, 0);
941
942 spin_unlock_irqrestore(&gpio->lock, flags);
943
944 return rc;
945}
946
947static int set_debounce(struct gpio_chip *chip, unsigned int offset,
948 unsigned long usecs)
949{
950 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
951
952 if (!have_debounce(gpio, offset))
953 return -ENOTSUPP;
954
955 if (usecs)
956 return enable_debounce(chip, offset, usecs);
957
958 return disable_debounce(chip, offset);
959}
960
961static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
962 unsigned long config)
963{
964 unsigned long param = pinconf_to_config_param(config);
965 u32 arg = pinconf_to_config_argument(config);
966
967 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
968 return set_debounce(chip, offset, arg);
969 else if (param == PIN_CONFIG_BIAS_DISABLE ||
970 param == PIN_CONFIG_BIAS_PULL_DOWN ||
971 param == PIN_CONFIG_DRIVE_STRENGTH)
972 return pinctrl_gpio_set_config(offset, config);
Andrew Jefferyc3bafe02017-04-07 22:29:02 +0930973 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
974 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
975 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
976 return -ENOTSUPP;
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030977 else if (param == PIN_CONFIG_PERSIST_STATE)
978 return aspeed_gpio_reset_tolerance(chip, offset, arg);
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +0930979
980 return -ENOTSUPP;
981}
982
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +1000983/**
984 * aspeed_gpio_copro_set_ops - Sets the callbacks used for handhsaking with
985 * the coprocessor for shared GPIO banks
986 * @ops: The callbacks
987 * @data: Pointer passed back to the callbacks
988 */
989int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops *ops, void *data)
990{
991 copro_data = data;
992 copro_ops = ops;
993
994 return 0;
995}
996EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops);
997
998/**
999 * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire
1000 * bank gets marked and any access from the ARM will
1001 * result in handshaking via callbacks.
1002 * @desc: The GPIO to be marked
1003 * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space
1004 * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space
1005 * @bit: If non-NULL, returns the bit number of the GPIO in the registers
1006 */
1007int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc,
1008 u16 *vreg_offset, u16 *dreg_offset, u8 *bit)
1009{
1010 struct gpio_chip *chip = gpiod_to_chip(desc);
1011 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1012 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1013 const struct aspeed_gpio_bank *bank = to_bank(offset);
1014 unsigned long flags;
1015
1016 if (!gpio->cf_copro_bankmap)
1017 gpio->cf_copro_bankmap = kzalloc(gpio->config->nr_gpios >> 3, GFP_KERNEL);
1018 if (!gpio->cf_copro_bankmap)
1019 return -ENOMEM;
1020 if (offset < 0 || offset > gpio->config->nr_gpios)
1021 return -EINVAL;
1022 bindex = offset >> 3;
1023
1024 spin_lock_irqsave(&gpio->lock, flags);
1025
1026 /* Sanity check, this shouldn't happen */
1027 if (gpio->cf_copro_bankmap[bindex] == 0xff) {
1028 rc = -EIO;
1029 goto bail;
1030 }
1031 gpio->cf_copro_bankmap[bindex]++;
1032
1033 /* Switch command source */
1034 if (gpio->cf_copro_bankmap[bindex] == 1)
1035 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1036 GPIO_CMDSRC_COLDFIRE);
1037
1038 if (vreg_offset)
1039 *vreg_offset = bank->val_regs;
1040 if (dreg_offset)
1041 *dreg_offset = bank->rdata_reg;
1042 if (bit)
1043 *bit = GPIO_OFFSET(offset);
1044 bail:
1045 spin_unlock_irqrestore(&gpio->lock, flags);
1046 return rc;
1047}
1048EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio);
1049
1050/**
1051 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1052 * @desc: The GPIO to be marked
1053 */
1054int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc)
1055{
1056 struct gpio_chip *chip = gpiod_to_chip(desc);
1057 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1058 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1059 const struct aspeed_gpio_bank *bank = to_bank(offset);
1060 unsigned long flags;
1061
1062 if (!gpio->cf_copro_bankmap)
1063 return -ENXIO;
1064
1065 if (offset < 0 || offset > gpio->config->nr_gpios)
1066 return -EINVAL;
1067 bindex = offset >> 3;
1068
1069 spin_lock_irqsave(&gpio->lock, flags);
1070
1071 /* Sanity check, this shouldn't happen */
1072 if (gpio->cf_copro_bankmap[bindex] == 0) {
1073 rc = -EIO;
1074 goto bail;
1075 }
1076 gpio->cf_copro_bankmap[bindex]--;
1077
1078 /* Switch command source */
1079 if (gpio->cf_copro_bankmap[bindex] == 0)
1080 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1081 GPIO_CMDSRC_ARM);
1082 bail:
1083 spin_unlock_irqrestore(&gpio->lock, flags);
1084 return rc;
1085}
1086EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1087
Andrew Jeffery1736f752017-01-24 16:46:46 +10301088/*
1089 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1090 * have the properties:
1091 *
1092 * { .input = 0xffffffff, .output = 0xffffffff }
1093 */
1094
1095static const struct aspeed_bank_props ast2400_bank_props[] = {
1096 /* input output */
1097 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1098 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1099 { },
1100};
1101
1102static const struct aspeed_gpio_config ast2400_config =
1103 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1104 { .nr_gpios = 220, .props = ast2400_bank_props, };
1105
1106static const struct aspeed_bank_props ast2500_bank_props[] = {
1107 /* input output */
1108 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1109 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1110 { 7, 0x000000ff, 0x000000ff }, /* AC */
1111 { },
1112};
1113
1114static const struct aspeed_gpio_config ast2500_config =
1115 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1116 { .nr_gpios = 232, .props = ast2500_bank_props, };
1117
1118static const struct of_device_id aspeed_gpio_of_table[] = {
1119 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1120 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1121 {}
1122};
1123MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1124
Joel Stanley361b7912016-08-30 17:24:27 +09301125static int __init aspeed_gpio_probe(struct platform_device *pdev)
1126{
Andrew Jeffery1736f752017-01-24 16:46:46 +10301127 const struct of_device_id *gpio_id;
Joel Stanley361b7912016-08-30 17:24:27 +09301128 struct aspeed_gpio *gpio;
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +10001129 int rc, i, banks;
Joel Stanley361b7912016-08-30 17:24:27 +09301130
1131 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
1132 if (!gpio)
1133 return -ENOMEM;
1134
Enrico Weigelt, metux IT consultaee70b72019-03-11 19:54:43 +01001135 gpio->base = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjun7f8b9652016-09-15 01:30:32 +00001136 if (IS_ERR(gpio->base))
1137 return PTR_ERR(gpio->base);
Joel Stanley361b7912016-08-30 17:24:27 +09301138
1139 spin_lock_init(&gpio->lock);
1140
Andrew Jeffery1736f752017-01-24 16:46:46 +10301141 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
1142 if (!gpio_id)
1143 return -EINVAL;
Joel Stanley361b7912016-08-30 17:24:27 +09301144
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09301145 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
1146 if (IS_ERR(gpio->clk)) {
1147 dev_warn(&pdev->dev,
Andrew Jeffery754c0452017-08-08 15:37:36 +09301148 "Failed to get clock from devicetree, debouncing disabled\n");
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09301149 gpio->clk = NULL;
1150 }
1151
Andrew Jeffery1736f752017-01-24 16:46:46 +10301152 gpio->config = gpio_id->data;
1153
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09301154 gpio->chip.parent = &pdev->dev;
Andrew Jeffery1736f752017-01-24 16:46:46 +10301155 gpio->chip.ngpio = gpio->config->nr_gpios;
Joel Stanley361b7912016-08-30 17:24:27 +09301156 gpio->chip.direction_input = aspeed_gpio_dir_in;
1157 gpio->chip.direction_output = aspeed_gpio_dir_out;
1158 gpio->chip.get_direction = aspeed_gpio_get_direction;
1159 gpio->chip.request = aspeed_gpio_request;
1160 gpio->chip.free = aspeed_gpio_free;
1161 gpio->chip.get = aspeed_gpio_get;
1162 gpio->chip.set = aspeed_gpio_set;
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09301163 gpio->chip.set_config = aspeed_gpio_set_config;
Joel Stanley361b7912016-08-30 17:24:27 +09301164 gpio->chip.label = dev_name(&pdev->dev);
1165 gpio->chip.base = -1;
1166
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +10001167 /* Allocate a cache of the output registers */
1168 banks = gpio->config->nr_gpios >> 5;
Kees Cooka86854d2018-06-12 14:07:58 -07001169 gpio->dcache = devm_kcalloc(&pdev->dev,
1170 banks, sizeof(u32), GFP_KERNEL);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +10001171 if (!gpio->dcache)
1172 return -ENOMEM;
1173
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +10001174 /*
1175 * Populate it with initial values read from the HW and switch
1176 * all command sources to the ARM by default
1177 */
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +10001178 for (i = 0; i < banks; i++) {
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +10001179 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
1180 void __iomem *addr = bank_reg(gpio, bank, reg_rdata);
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +10001181 gpio->dcache[i] = ioread32(addr);
Benjamin Herrenschmidta7ca1382018-06-29 14:11:19 +10001182 aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM);
1183 aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM);
1184 aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM);
1185 aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +10001186 }
1187
Linus Walleij8512ee32019-08-09 14:55:15 +02001188 /* Optionally set up an irqchip if there is an IRQ */
1189 rc = platform_get_irq(pdev, 0);
1190 if (rc > 0) {
1191 struct gpio_irq_chip *girq;
1192
1193 gpio->irq = rc;
1194 girq = &gpio->chip.irq;
1195 girq->chip = &aspeed_gpio_irqchip;
1196 girq->parent_handler = aspeed_gpio_irq_handler;
1197 girq->num_parents = 1;
1198 girq->parents = devm_kcalloc(&pdev->dev, 1,
1199 sizeof(*girq->parents),
1200 GFP_KERNEL);
1201 if (!girq->parents)
1202 return -ENOMEM;
1203 girq->parents[0] = gpio->irq;
1204 girq->default_type = IRQ_TYPE_NONE;
1205 girq->handler = handle_bad_irq;
1206 girq->need_valid_mask = true;
1207 }
Joel Stanley361b7912016-08-30 17:24:27 +09301208
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09301209 gpio->offset_timer =
1210 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
Kangjie Lu6cf45112019-03-24 18:10:02 -05001211 if (!gpio->offset_timer)
1212 return -ENOMEM;
Andrew Jeffery5ae4cb942017-04-07 22:29:01 +09301213
Linus Walleij8512ee32019-08-09 14:55:15 +02001214 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1215 if (rc < 0)
1216 return rc;
1217
1218 /* Now the valid mask is allocated */
1219 if (gpio->irq)
1220 set_irq_valid_mask(gpio);
1221
1222 return 0;
Joel Stanley361b7912016-08-30 17:24:27 +09301223}
1224
Joel Stanley361b7912016-08-30 17:24:27 +09301225static struct platform_driver aspeed_gpio_driver = {
1226 .driver = {
1227 .name = KBUILD_MODNAME,
1228 .of_match_table = aspeed_gpio_of_table,
1229 },
1230};
1231
1232module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
1233
1234MODULE_DESCRIPTION("Aspeed GPIO Driver");
Linus Walleije50237c2016-09-13 13:43:34 +02001235MODULE_LICENSE("GPL");