blob: a5ded50c6db02534d72eb70191e9249674464c05 [file] [log] [blame]
Joel Stanley361b7912016-08-30 17:24:27 +09301/*
2 * Copyright 2015 IBM Corp.
3 *
4 * Joel Stanley <joel@jms.id.au>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093012#include <asm/div64.h>
13#include <linux/clk.h>
14#include <linux/gpio/driver.h>
15#include <linux/hashtable.h>
Joel Stanley361b7912016-08-30 17:24:27 +093016#include <linux/init.h>
17#include <linux/io.h>
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093018#include <linux/kernel.h>
19#include <linux/module.h>
Joel Stanley361b7912016-08-30 17:24:27 +093020#include <linux/pinctrl/consumer.h>
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093021#include <linux/platform_device.h>
22#include <linux/spinlock.h>
23#include <linux/string.h>
Joel Stanley361b7912016-08-30 17:24:27 +093024
Andrew Jeffery1736f752017-01-24 16:46:46 +103025struct aspeed_bank_props {
26 unsigned int bank;
27 u32 input;
28 u32 output;
29};
30
31struct aspeed_gpio_config {
32 unsigned int nr_gpios;
33 const struct aspeed_bank_props *props;
34};
35
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093036/*
37 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
38 * @timer_users: Tracks the number of users for each timer
39 *
40 * The @timer_users has four elements but the first element is unused. This is
41 * to simplify accounting and indexing, as a zero value in @offset_timer
42 * represents disabled debouncing for the GPIO. Any other value for an element
43 * of @offset_timer is used as an index into @timer_users. This behaviour of
44 * the zero value aligns with the behaviour of zero built from the timer
45 * configuration registers (i.e. debouncing is disabled).
46 */
Joel Stanley361b7912016-08-30 17:24:27 +093047struct aspeed_gpio {
48 struct gpio_chip chip;
49 spinlock_t lock;
50 void __iomem *base;
51 int irq;
Andrew Jeffery1736f752017-01-24 16:46:46 +103052 const struct aspeed_gpio_config *config;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093053
54 u8 *offset_timer;
55 unsigned int timer_users[4];
56 struct clk *clk;
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +100057
58 u32 *dcache;
Joel Stanley361b7912016-08-30 17:24:27 +093059};
60
61struct aspeed_gpio_bank {
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100062 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
63 * +4: Rd/Wr: Direction (0=in, 1=out)
64 */
65 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
Joel Stanley361b7912016-08-30 17:24:27 +093066 uint16_t irq_regs;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093067 uint16_t debounce_regs;
Andrew Jeffery1b43d262017-11-30 14:25:25 +103068 uint16_t tolerance_regs;
Joel Stanley7153f8e2017-01-23 15:56:06 +103069 const char names[4][3];
Joel Stanley361b7912016-08-30 17:24:27 +093070};
71
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100072/*
73 * Note: The "value" register returns the input value sampled on the
74 * line even when the GPIO is configured as an output. Since
75 * that input goes through synchronizers, writing, then reading
76 * back may not return the written value right away.
77 *
78 * The "rdata" register returns the content of the write latch
79 * and thus can be used to read back what was last written
80 * reliably.
81 */
82
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093083static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
84
Joel Stanley361b7912016-08-30 17:24:27 +093085static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
86 {
87 .val_regs = 0x0000,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100088 .rdata_reg = 0x00c0,
Joel Stanley361b7912016-08-30 17:24:27 +093089 .irq_regs = 0x0008,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093090 .debounce_regs = 0x0040,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103091 .tolerance_regs = 0x001c,
Joel Stanley7153f8e2017-01-23 15:56:06 +103092 .names = { "A", "B", "C", "D" },
Joel Stanley361b7912016-08-30 17:24:27 +093093 },
94 {
95 .val_regs = 0x0020,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +100096 .rdata_reg = 0x00c4,
Joel Stanley361b7912016-08-30 17:24:27 +093097 .irq_regs = 0x0028,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093098 .debounce_regs = 0x0048,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103099 .tolerance_regs = 0x003c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030100 .names = { "E", "F", "G", "H" },
Joel Stanley361b7912016-08-30 17:24:27 +0930101 },
102 {
103 .val_regs = 0x0070,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000104 .rdata_reg = 0x00c8,
Joel Stanley361b7912016-08-30 17:24:27 +0930105 .irq_regs = 0x0098,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930106 .debounce_regs = 0x00b0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030107 .tolerance_regs = 0x00ac,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030108 .names = { "I", "J", "K", "L" },
Joel Stanley361b7912016-08-30 17:24:27 +0930109 },
110 {
111 .val_regs = 0x0078,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000112 .rdata_reg = 0x00cc,
Joel Stanley361b7912016-08-30 17:24:27 +0930113 .irq_regs = 0x00e8,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930114 .debounce_regs = 0x0100,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030115 .tolerance_regs = 0x00fc,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030116 .names = { "M", "N", "O", "P" },
Joel Stanley361b7912016-08-30 17:24:27 +0930117 },
118 {
119 .val_regs = 0x0080,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000120 .rdata_reg = 0x00d0,
Joel Stanley361b7912016-08-30 17:24:27 +0930121 .irq_regs = 0x0118,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930122 .debounce_regs = 0x0130,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030123 .tolerance_regs = 0x012c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030124 .names = { "Q", "R", "S", "T" },
Joel Stanley361b7912016-08-30 17:24:27 +0930125 },
126 {
127 .val_regs = 0x0088,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000128 .rdata_reg = 0x00d4,
Joel Stanley361b7912016-08-30 17:24:27 +0930129 .irq_regs = 0x0148,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930130 .debounce_regs = 0x0160,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030131 .tolerance_regs = 0x015c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030132 .names = { "U", "V", "W", "X" },
Joel Stanley361b7912016-08-30 17:24:27 +0930133 },
Andrew Jeffery1736f752017-01-24 16:46:46 +1030134 {
135 .val_regs = 0x01E0,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000136 .rdata_reg = 0x00d8,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030137 .irq_regs = 0x0178,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930138 .debounce_regs = 0x0190,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030139 .tolerance_regs = 0x018c,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030140 .names = { "Y", "Z", "AA", "AB" },
141 },
142 {
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030143 .val_regs = 0x01e8,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000144 .rdata_reg = 0x00dc,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030145 .irq_regs = 0x01a8,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930146 .debounce_regs = 0x01c0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030147 .tolerance_regs = 0x01bc,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030148 .names = { "AC", "", "", "" },
149 },
Joel Stanley361b7912016-08-30 17:24:27 +0930150};
151
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000152enum aspeed_gpio_reg {
153 reg_val,
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000154 reg_rdata,
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000155 reg_dir,
156 reg_irq_enable,
157 reg_irq_type0,
158 reg_irq_type1,
159 reg_irq_type2,
160 reg_irq_status,
161 reg_debounce_sel1,
162 reg_debounce_sel2,
163 reg_tolerance,
164};
Joel Stanley361b7912016-08-30 17:24:27 +0930165
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000166#define GPIO_VAL_VALUE 0x00
167#define GPIO_VAL_DIR 0x04
Joel Stanley361b7912016-08-30 17:24:27 +0930168
169#define GPIO_IRQ_ENABLE 0x00
170#define GPIO_IRQ_TYPE0 0x04
171#define GPIO_IRQ_TYPE1 0x08
172#define GPIO_IRQ_TYPE2 0x0c
173#define GPIO_IRQ_STATUS 0x10
174
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930175#define GPIO_DEBOUNCE_SEL1 0x00
176#define GPIO_DEBOUNCE_SEL2 0x04
177
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000178/* This will be resolved at compile time */
179static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
180 const struct aspeed_gpio_bank *bank,
181 const enum aspeed_gpio_reg reg)
182{
183 switch (reg) {
184 case reg_val:
185 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000186 case reg_rdata:
187 return gpio->base + bank->rdata_reg;
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000188 case reg_dir:
189 return gpio->base + bank->val_regs + GPIO_VAL_DIR;
190 case reg_irq_enable:
191 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
192 case reg_irq_type0:
193 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
194 case reg_irq_type1:
195 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
196 case reg_irq_type2:
197 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
198 case reg_irq_status:
199 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
200 case reg_debounce_sel1:
201 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
202 case reg_debounce_sel2:
203 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
204 case reg_tolerance:
205 return gpio->base + bank->tolerance_regs;
206 }
207 BUG_ON(1);
208}
209
210#define GPIO_BANK(x) ((x) >> 5)
211#define GPIO_OFFSET(x) ((x) & 0x1f)
212#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
213
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930214#define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
215#define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
216#define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
217
Joel Stanley361b7912016-08-30 17:24:27 +0930218static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
219{
220 unsigned int bank = GPIO_BANK(offset);
221
Vasyl Gomonovychfe138622017-12-21 16:55:10 +0100222 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
Joel Stanley361b7912016-08-30 17:24:27 +0930223 return &aspeed_gpio_banks[bank];
224}
225
Andrew Jeffery1736f752017-01-24 16:46:46 +1030226static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
227{
228 return !(props->input || props->output);
229}
230
231static inline const struct aspeed_bank_props *find_bank_props(
232 struct aspeed_gpio *gpio, unsigned int offset)
233{
234 const struct aspeed_bank_props *props = gpio->config->props;
235
236 while (!is_bank_props_sentinel(props)) {
237 if (props->bank == GPIO_BANK(offset))
238 return props;
239 props++;
240 }
241
242 return NULL;
243}
244
245static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
246{
247 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
248 const struct aspeed_gpio_bank *bank = to_bank(offset);
249 unsigned int group = GPIO_OFFSET(offset) / 8;
250
251 return bank->names[group][0] != '\0' &&
252 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
253}
254
255static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
256{
257 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
258
259 return !props || (props->input & GPIO_BIT(offset));
260}
261
262#define have_irq(g, o) have_input((g), (o))
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930263#define have_debounce(g, o) have_input((g), (o))
Andrew Jeffery1736f752017-01-24 16:46:46 +1030264
265static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
266{
267 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
268
269 return !props || (props->output & GPIO_BIT(offset));
270}
271
Joel Stanley361b7912016-08-30 17:24:27 +0930272static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
273{
274 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
275 const struct aspeed_gpio_bank *bank = to_bank(offset);
276
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000277 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
Joel Stanley361b7912016-08-30 17:24:27 +0930278}
279
280static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
281 int val)
282{
283 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
284 const struct aspeed_gpio_bank *bank = to_bank(offset);
285 void __iomem *addr;
286 u32 reg;
287
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000288 addr = bank_reg(gpio, bank, reg_val);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000289 reg = gpio->dcache[GPIO_BANK(offset)];
Joel Stanley361b7912016-08-30 17:24:27 +0930290
291 if (val)
292 reg |= GPIO_BIT(offset);
293 else
294 reg &= ~GPIO_BIT(offset);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000295 gpio->dcache[GPIO_BANK(offset)] = reg;
Joel Stanley361b7912016-08-30 17:24:27 +0930296
297 iowrite32(reg, addr);
298}
299
300static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
301 int val)
302{
303 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
304 unsigned long flags;
305
306 spin_lock_irqsave(&gpio->lock, flags);
307
308 __aspeed_gpio_set(gc, offset, val);
309
310 spin_unlock_irqrestore(&gpio->lock, flags);
311}
312
313static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
314{
315 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
316 const struct aspeed_gpio_bank *bank = to_bank(offset);
317 unsigned long flags;
318 u32 reg;
319
Andrew Jeffery1736f752017-01-24 16:46:46 +1030320 if (!have_input(gpio, offset))
321 return -ENOTSUPP;
322
Joel Stanley361b7912016-08-30 17:24:27 +0930323 spin_lock_irqsave(&gpio->lock, flags);
324
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000325 reg = ioread32(bank_reg(gpio, bank, reg_dir));
326 iowrite32(reg & ~GPIO_BIT(offset), bank_reg(gpio, bank, reg_dir));
Joel Stanley361b7912016-08-30 17:24:27 +0930327
328 spin_unlock_irqrestore(&gpio->lock, flags);
329
330 return 0;
331}
332
333static int aspeed_gpio_dir_out(struct gpio_chip *gc,
334 unsigned int offset, int val)
335{
336 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
337 const struct aspeed_gpio_bank *bank = to_bank(offset);
338 unsigned long flags;
339 u32 reg;
340
Andrew Jeffery1736f752017-01-24 16:46:46 +1030341 if (!have_output(gpio, offset))
342 return -ENOTSUPP;
343
Joel Stanley361b7912016-08-30 17:24:27 +0930344 spin_lock_irqsave(&gpio->lock, flags);
345
Benjamin Herrenschmidtaf794922018-05-17 18:11:56 +1000346 __aspeed_gpio_set(gc, offset, val);
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000347 reg = ioread32(bank_reg(gpio, bank, reg_dir));
348 iowrite32(reg | GPIO_BIT(offset), bank_reg(gpio, bank, reg_dir));
Joel Stanley361b7912016-08-30 17:24:27 +0930349
Joel Stanley361b7912016-08-30 17:24:27 +0930350 spin_unlock_irqrestore(&gpio->lock, flags);
351
352 return 0;
353}
354
355static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
356{
357 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
358 const struct aspeed_gpio_bank *bank = to_bank(offset);
359 unsigned long flags;
360 u32 val;
361
Andrew Jeffery1736f752017-01-24 16:46:46 +1030362 if (!have_input(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030363 return 0;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030364
365 if (!have_output(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030366 return 1;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030367
Joel Stanley361b7912016-08-30 17:24:27 +0930368 spin_lock_irqsave(&gpio->lock, flags);
369
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000370 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930371
372 spin_unlock_irqrestore(&gpio->lock, flags);
373
374 return !val;
375
376}
377
378static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
379 struct aspeed_gpio **gpio,
380 const struct aspeed_gpio_bank **bank,
381 u32 *bit)
382{
383 int offset;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030384 struct aspeed_gpio *internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930385
386 offset = irqd_to_hwirq(d);
387
Andrew Jeffery1736f752017-01-24 16:46:46 +1030388 internal = irq_data_get_irq_chip_data(d);
389
390 /* This might be a bit of a questionable place to check */
391 if (!have_irq(internal, offset))
392 return -ENOTSUPP;
393
394 *gpio = internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930395 *bank = to_bank(offset);
396 *bit = GPIO_BIT(offset);
397
398 return 0;
399}
400
401static void aspeed_gpio_irq_ack(struct irq_data *d)
402{
403 const struct aspeed_gpio_bank *bank;
404 struct aspeed_gpio *gpio;
405 unsigned long flags;
406 void __iomem *status_addr;
407 u32 bit;
408 int rc;
409
410 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
411 if (rc)
412 return;
413
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000414 status_addr = bank_reg(gpio, bank, reg_irq_status);
Joel Stanley361b7912016-08-30 17:24:27 +0930415
416 spin_lock_irqsave(&gpio->lock, flags);
417 iowrite32(bit, status_addr);
418 spin_unlock_irqrestore(&gpio->lock, flags);
419}
420
421static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
422{
423 const struct aspeed_gpio_bank *bank;
424 struct aspeed_gpio *gpio;
425 unsigned long flags;
426 u32 reg, bit;
427 void __iomem *addr;
428 int rc;
429
430 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
431 if (rc)
432 return;
433
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000434 addr = bank_reg(gpio, bank, reg_irq_enable);
Joel Stanley361b7912016-08-30 17:24:27 +0930435
436 spin_lock_irqsave(&gpio->lock, flags);
437
438 reg = ioread32(addr);
439 if (set)
440 reg |= bit;
441 else
Govert Overgaauwf2416322018-04-06 14:41:35 +0200442 reg &= ~bit;
Joel Stanley361b7912016-08-30 17:24:27 +0930443 iowrite32(reg, addr);
444
445 spin_unlock_irqrestore(&gpio->lock, flags);
446}
447
448static void aspeed_gpio_irq_mask(struct irq_data *d)
449{
450 aspeed_gpio_irq_set_mask(d, false);
451}
452
453static void aspeed_gpio_irq_unmask(struct irq_data *d)
454{
455 aspeed_gpio_irq_set_mask(d, true);
456}
457
458static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
459{
460 u32 type0 = 0;
461 u32 type1 = 0;
462 u32 type2 = 0;
463 u32 bit, reg;
464 const struct aspeed_gpio_bank *bank;
465 irq_flow_handler_t handler;
466 struct aspeed_gpio *gpio;
467 unsigned long flags;
468 void __iomem *addr;
469 int rc;
470
471 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
472 if (rc)
473 return -EINVAL;
474
475 switch (type & IRQ_TYPE_SENSE_MASK) {
476 case IRQ_TYPE_EDGE_BOTH:
477 type2 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500478 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930479 case IRQ_TYPE_EDGE_RISING:
480 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500481 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930482 case IRQ_TYPE_EDGE_FALLING:
483 handler = handle_edge_irq;
484 break;
485 case IRQ_TYPE_LEVEL_HIGH:
486 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500487 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930488 case IRQ_TYPE_LEVEL_LOW:
489 type1 |= bit;
490 handler = handle_level_irq;
491 break;
492 default:
493 return -EINVAL;
494 }
495
496 spin_lock_irqsave(&gpio->lock, flags);
497
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000498 addr = bank_reg(gpio, bank, reg_irq_type0);
Joel Stanley361b7912016-08-30 17:24:27 +0930499 reg = ioread32(addr);
500 reg = (reg & ~bit) | type0;
501 iowrite32(reg, addr);
502
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000503 addr = bank_reg(gpio, bank, reg_irq_type1);
Joel Stanley361b7912016-08-30 17:24:27 +0930504 reg = ioread32(addr);
505 reg = (reg & ~bit) | type1;
506 iowrite32(reg, addr);
507
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000508 addr = bank_reg(gpio, bank, reg_irq_type2);
Joel Stanley361b7912016-08-30 17:24:27 +0930509 reg = ioread32(addr);
510 reg = (reg & ~bit) | type2;
511 iowrite32(reg, addr);
512
513 spin_unlock_irqrestore(&gpio->lock, flags);
514
515 irq_set_handler_locked(d, handler);
516
517 return 0;
518}
519
520static void aspeed_gpio_irq_handler(struct irq_desc *desc)
521{
522 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
523 struct irq_chip *ic = irq_desc_get_chip(desc);
524 struct aspeed_gpio *data = gpiochip_get_data(gc);
525 unsigned int i, p, girq;
526 unsigned long reg;
527
528 chained_irq_enter(ic, desc);
529
530 for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
531 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
532
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000533 reg = ioread32(bank_reg(data, bank, reg_irq_status));
Joel Stanley361b7912016-08-30 17:24:27 +0930534
535 for_each_set_bit(p, &reg, 32) {
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100536 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
Joel Stanley361b7912016-08-30 17:24:27 +0930537 generic_handle_irq(girq);
538 }
539
540 }
541
542 chained_irq_exit(ic, desc);
543}
544
545static struct irq_chip aspeed_gpio_irqchip = {
546 .name = "aspeed-gpio",
547 .irq_ack = aspeed_gpio_irq_ack,
548 .irq_mask = aspeed_gpio_irq_mask,
549 .irq_unmask = aspeed_gpio_irq_unmask,
550 .irq_set_type = aspeed_gpio_set_type,
551};
552
Andrew Jeffery1736f752017-01-24 16:46:46 +1030553static void set_irq_valid_mask(struct aspeed_gpio *gpio)
554{
555 const struct aspeed_bank_props *props = gpio->config->props;
556
557 while (!is_bank_props_sentinel(props)) {
558 unsigned int offset;
559 const unsigned long int input = props->input;
560
561 /* Pretty crummy approach, but similar to GPIO core */
562 for_each_clear_bit(offset, &input, 32) {
563 unsigned int i = props->bank * 32 + offset;
564
565 if (i >= gpio->config->nr_gpios)
566 break;
567
Thierry Redingdc7b0382017-11-07 19:15:52 +0100568 clear_bit(i, gpio->chip.irq.valid_mask);
Andrew Jeffery1736f752017-01-24 16:46:46 +1030569 }
570
571 props++;
572 }
573}
574
Joel Stanley361b7912016-08-30 17:24:27 +0930575static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
576 struct platform_device *pdev)
577{
578 int rc;
579
580 rc = platform_get_irq(pdev, 0);
581 if (rc < 0)
582 return rc;
583
584 gpio->irq = rc;
585
Andrew Jeffery1736f752017-01-24 16:46:46 +1030586 set_irq_valid_mask(gpio);
587
Joel Stanley361b7912016-08-30 17:24:27 +0930588 rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
589 0, handle_bad_irq, IRQ_TYPE_NONE);
590 if (rc) {
591 dev_info(&pdev->dev, "Could not add irqchip\n");
592 return rc;
593 }
594
595 gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
596 gpio->irq, aspeed_gpio_irq_handler);
597
598 return 0;
599}
600
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030601static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
602 unsigned int offset, bool enable)
603{
604 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030605 unsigned long flags;
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000606 void __iomem *treg;
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030607 u32 val;
608
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000609 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030610
611 spin_lock_irqsave(&gpio->lock, flags);
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000612 val = readl(treg);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030613
614 if (enable)
615 val |= GPIO_BIT(offset);
616 else
617 val &= ~GPIO_BIT(offset);
618
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000619 writel(val, treg);
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030620 spin_unlock_irqrestore(&gpio->lock, flags);
621
622 return 0;
623}
624
Joel Stanley361b7912016-08-30 17:24:27 +0930625static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
626{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030627 if (!have_gpio(gpiochip_get_data(chip), offset))
628 return -ENODEV;
629
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200630 return pinctrl_gpio_request(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930631}
632
633static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
634{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200635 pinctrl_gpio_free(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930636}
637
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930638static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
639 u32 *cycles)
640{
641 u64 rate;
642 u64 n;
643 u32 r;
644
645 rate = clk_get_rate(gpio->clk);
646 if (!rate)
647 return -ENOTSUPP;
648
649 n = rate * usecs;
650 r = do_div(n, 1000000);
651
652 if (n >= U32_MAX)
653 return -ERANGE;
654
655 /* At least as long as the requested time */
656 *cycles = n + (!!r);
657
658 return 0;
659}
660
661/* Call under gpio->lock */
662static int register_allocated_timer(struct aspeed_gpio *gpio,
663 unsigned int offset, unsigned int timer)
664{
665 if (WARN(gpio->offset_timer[offset] != 0,
666 "Offset %d already allocated timer %d\n",
667 offset, gpio->offset_timer[offset]))
668 return -EINVAL;
669
670 if (WARN(gpio->timer_users[timer] == UINT_MAX,
671 "Timer user count would overflow\n"))
672 return -EPERM;
673
674 gpio->offset_timer[offset] = timer;
675 gpio->timer_users[timer]++;
676
677 return 0;
678}
679
680/* Call under gpio->lock */
681static int unregister_allocated_timer(struct aspeed_gpio *gpio,
682 unsigned int offset)
683{
684 if (WARN(gpio->offset_timer[offset] == 0,
685 "No timer allocated to offset %d\n", offset))
686 return -EINVAL;
687
688 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
689 "No users recorded for timer %d\n",
690 gpio->offset_timer[offset]))
691 return -EINVAL;
692
693 gpio->timer_users[gpio->offset_timer[offset]]--;
694 gpio->offset_timer[offset] = 0;
695
696 return 0;
697}
698
699/* Call under gpio->lock */
700static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
701 unsigned int offset)
702{
703 return gpio->offset_timer[offset] > 0;
704}
705
706/* Call under gpio->lock */
707static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
708 unsigned int timer)
709{
710 const struct aspeed_gpio_bank *bank = to_bank(offset);
711 const u32 mask = GPIO_BIT(offset);
712 void __iomem *addr;
713 u32 val;
714
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000715 addr = bank_reg(gpio, bank, reg_debounce_sel1);
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930716 val = ioread32(addr);
717 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
718
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000719 addr = bank_reg(gpio, bank, reg_debounce_sel2);
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930720 val = ioread32(addr);
721 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
722}
723
724static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
725 unsigned long usecs)
726{
727 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
728 u32 requested_cycles;
729 unsigned long flags;
730 int rc;
731 int i;
732
Joel Stanleydf563c82017-05-02 15:38:24 +0930733 if (!gpio->clk)
734 return -EINVAL;
735
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930736 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
737 if (rc < 0) {
738 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
739 usecs, clk_get_rate(gpio->clk), rc);
740 return rc;
741 }
742
743 spin_lock_irqsave(&gpio->lock, flags);
744
745 if (timer_allocation_registered(gpio, offset)) {
746 rc = unregister_allocated_timer(gpio, offset);
747 if (rc < 0)
748 goto out;
749 }
750
751 /* Try to find a timer already configured for the debounce period */
752 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
753 u32 cycles;
754
755 cycles = ioread32(gpio->base + debounce_timers[i]);
756 if (requested_cycles == cycles)
757 break;
758 }
759
760 if (i == ARRAY_SIZE(debounce_timers)) {
761 int j;
762
763 /*
764 * As there are no timers configured for the requested debounce
765 * period, find an unused timer instead
766 */
767 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
768 if (gpio->timer_users[j] == 0)
769 break;
770 }
771
772 if (j == ARRAY_SIZE(gpio->timer_users)) {
773 dev_warn(chip->parent,
774 "Debounce timers exhausted, cannot debounce for period %luus\n",
775 usecs);
776
777 rc = -EPERM;
778
779 /*
780 * We already adjusted the accounting to remove @offset
781 * as a user of its previous timer, so also configure
782 * the hardware so @offset has timers disabled for
783 * consistency.
784 */
785 configure_timer(gpio, offset, 0);
786 goto out;
787 }
788
789 i = j;
790
791 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
792 }
793
794 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
795 rc = -EINVAL;
796 goto out;
797 }
798
799 register_allocated_timer(gpio, offset, i);
800 configure_timer(gpio, offset, i);
801
802out:
803 spin_unlock_irqrestore(&gpio->lock, flags);
804
805 return rc;
806}
807
808static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
809{
810 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
811 unsigned long flags;
812 int rc;
813
814 spin_lock_irqsave(&gpio->lock, flags);
815
816 rc = unregister_allocated_timer(gpio, offset);
817 if (!rc)
818 configure_timer(gpio, offset, 0);
819
820 spin_unlock_irqrestore(&gpio->lock, flags);
821
822 return rc;
823}
824
825static int set_debounce(struct gpio_chip *chip, unsigned int offset,
826 unsigned long usecs)
827{
828 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
829
830 if (!have_debounce(gpio, offset))
831 return -ENOTSUPP;
832
833 if (usecs)
834 return enable_debounce(chip, offset, usecs);
835
836 return disable_debounce(chip, offset);
837}
838
839static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
840 unsigned long config)
841{
842 unsigned long param = pinconf_to_config_param(config);
843 u32 arg = pinconf_to_config_argument(config);
844
845 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
846 return set_debounce(chip, offset, arg);
847 else if (param == PIN_CONFIG_BIAS_DISABLE ||
848 param == PIN_CONFIG_BIAS_PULL_DOWN ||
849 param == PIN_CONFIG_DRIVE_STRENGTH)
850 return pinctrl_gpio_set_config(offset, config);
Andrew Jefferyc3bafe02017-04-07 22:29:02 +0930851 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
852 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
853 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
854 return -ENOTSUPP;
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030855 else if (param == PIN_CONFIG_PERSIST_STATE)
856 return aspeed_gpio_reset_tolerance(chip, offset, arg);
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930857
858 return -ENOTSUPP;
859}
860
Andrew Jeffery1736f752017-01-24 16:46:46 +1030861/*
862 * Any banks not specified in a struct aspeed_bank_props array are assumed to
863 * have the properties:
864 *
865 * { .input = 0xffffffff, .output = 0xffffffff }
866 */
867
868static const struct aspeed_bank_props ast2400_bank_props[] = {
869 /* input output */
870 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
871 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
872 { },
873};
874
875static const struct aspeed_gpio_config ast2400_config =
876 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
877 { .nr_gpios = 220, .props = ast2400_bank_props, };
878
879static const struct aspeed_bank_props ast2500_bank_props[] = {
880 /* input output */
881 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
882 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
883 { 7, 0x000000ff, 0x000000ff }, /* AC */
884 { },
885};
886
887static const struct aspeed_gpio_config ast2500_config =
888 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
889 { .nr_gpios = 232, .props = ast2500_bank_props, };
890
891static const struct of_device_id aspeed_gpio_of_table[] = {
892 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
893 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
894 {}
895};
896MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
897
Joel Stanley361b7912016-08-30 17:24:27 +0930898static int __init aspeed_gpio_probe(struct platform_device *pdev)
899{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030900 const struct of_device_id *gpio_id;
Joel Stanley361b7912016-08-30 17:24:27 +0930901 struct aspeed_gpio *gpio;
902 struct resource *res;
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000903 int rc, i, banks;
Joel Stanley361b7912016-08-30 17:24:27 +0930904
905 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
906 if (!gpio)
907 return -ENOMEM;
908
909 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joel Stanley361b7912016-08-30 17:24:27 +0930910 gpio->base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun7f8b9652016-09-15 01:30:32 +0000911 if (IS_ERR(gpio->base))
912 return PTR_ERR(gpio->base);
Joel Stanley361b7912016-08-30 17:24:27 +0930913
914 spin_lock_init(&gpio->lock);
915
Andrew Jeffery1736f752017-01-24 16:46:46 +1030916 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
917 if (!gpio_id)
918 return -EINVAL;
Joel Stanley361b7912016-08-30 17:24:27 +0930919
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930920 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
921 if (IS_ERR(gpio->clk)) {
922 dev_warn(&pdev->dev,
Andrew Jeffery754c0452017-08-08 15:37:36 +0930923 "Failed to get clock from devicetree, debouncing disabled\n");
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930924 gpio->clk = NULL;
925 }
926
Andrew Jeffery1736f752017-01-24 16:46:46 +1030927 gpio->config = gpio_id->data;
928
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930929 gpio->chip.parent = &pdev->dev;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030930 gpio->chip.ngpio = gpio->config->nr_gpios;
Joel Stanley361b7912016-08-30 17:24:27 +0930931 gpio->chip.parent = &pdev->dev;
932 gpio->chip.direction_input = aspeed_gpio_dir_in;
933 gpio->chip.direction_output = aspeed_gpio_dir_out;
934 gpio->chip.get_direction = aspeed_gpio_get_direction;
935 gpio->chip.request = aspeed_gpio_request;
936 gpio->chip.free = aspeed_gpio_free;
937 gpio->chip.get = aspeed_gpio_get;
938 gpio->chip.set = aspeed_gpio_set;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930939 gpio->chip.set_config = aspeed_gpio_set_config;
Joel Stanley361b7912016-08-30 17:24:27 +0930940 gpio->chip.label = dev_name(&pdev->dev);
941 gpio->chip.base = -1;
Thierry Redingdc7b0382017-11-07 19:15:52 +0100942 gpio->chip.irq.need_valid_mask = true;
Joel Stanley361b7912016-08-30 17:24:27 +0930943
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000944 /* Allocate a cache of the output registers */
945 banks = gpio->config->nr_gpios >> 5;
Kees Cooka86854d2018-06-12 14:07:58 -0700946 gpio->dcache = devm_kcalloc(&pdev->dev,
947 banks, sizeof(u32), GFP_KERNEL);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000948 if (!gpio->dcache)
949 return -ENOMEM;
950
951 /* Populate it with initial values read from the HW */
952 for (i = 0; i < banks; i++) {
Benjamin Herrenschmidtc67dda82018-06-29 14:11:17 +1000953 void __iomem *addr = bank_reg(gpio, &aspeed_gpio_banks[i], reg_rdata);
Benjamin Herrenschmidt44ddf552018-06-29 14:11:16 +1000954 gpio->dcache[i] = ioread32(addr);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000955 }
956
Joel Stanley361b7912016-08-30 17:24:27 +0930957 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
958 if (rc < 0)
959 return rc;
960
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930961 gpio->offset_timer =
962 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
963
Joel Stanley361b7912016-08-30 17:24:27 +0930964 return aspeed_gpio_setup_irqs(gpio, pdev);
965}
966
Joel Stanley361b7912016-08-30 17:24:27 +0930967static struct platform_driver aspeed_gpio_driver = {
968 .driver = {
969 .name = KBUILD_MODNAME,
970 .of_match_table = aspeed_gpio_of_table,
971 },
972};
973
974module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
975
976MODULE_DESCRIPTION("Aspeed GPIO Driver");
Linus Walleije50237c2016-09-13 13:43:34 +0200977MODULE_LICENSE("GPL");