blob: f579bec6e102658353d60e1033e2f5c0b0150677 [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 {
62 uint16_t val_regs;
63 uint16_t irq_regs;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093064 uint16_t debounce_regs;
Andrew Jeffery1b43d262017-11-30 14:25:25 +103065 uint16_t tolerance_regs;
Joel Stanley7153f8e2017-01-23 15:56:06 +103066 const char names[4][3];
Joel Stanley361b7912016-08-30 17:24:27 +093067};
68
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093069static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
70
Joel Stanley361b7912016-08-30 17:24:27 +093071static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
72 {
73 .val_regs = 0x0000,
74 .irq_regs = 0x0008,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093075 .debounce_regs = 0x0040,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103076 .tolerance_regs = 0x001c,
Joel Stanley7153f8e2017-01-23 15:56:06 +103077 .names = { "A", "B", "C", "D" },
Joel Stanley361b7912016-08-30 17:24:27 +093078 },
79 {
80 .val_regs = 0x0020,
81 .irq_regs = 0x0028,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093082 .debounce_regs = 0x0048,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103083 .tolerance_regs = 0x003c,
Joel Stanley7153f8e2017-01-23 15:56:06 +103084 .names = { "E", "F", "G", "H" },
Joel Stanley361b7912016-08-30 17:24:27 +093085 },
86 {
87 .val_regs = 0x0070,
88 .irq_regs = 0x0098,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093089 .debounce_regs = 0x00b0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103090 .tolerance_regs = 0x00ac,
Joel Stanley7153f8e2017-01-23 15:56:06 +103091 .names = { "I", "J", "K", "L" },
Joel Stanley361b7912016-08-30 17:24:27 +093092 },
93 {
94 .val_regs = 0x0078,
95 .irq_regs = 0x00e8,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093096 .debounce_regs = 0x0100,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103097 .tolerance_regs = 0x00fc,
Joel Stanley7153f8e2017-01-23 15:56:06 +103098 .names = { "M", "N", "O", "P" },
Joel Stanley361b7912016-08-30 17:24:27 +093099 },
100 {
101 .val_regs = 0x0080,
102 .irq_regs = 0x0118,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930103 .debounce_regs = 0x0130,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030104 .tolerance_regs = 0x012c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030105 .names = { "Q", "R", "S", "T" },
Joel Stanley361b7912016-08-30 17:24:27 +0930106 },
107 {
108 .val_regs = 0x0088,
109 .irq_regs = 0x0148,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930110 .debounce_regs = 0x0160,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030111 .tolerance_regs = 0x015c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030112 .names = { "U", "V", "W", "X" },
Joel Stanley361b7912016-08-30 17:24:27 +0930113 },
Andrew Jeffery1736f752017-01-24 16:46:46 +1030114 {
115 .val_regs = 0x01E0,
116 .irq_regs = 0x0178,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930117 .debounce_regs = 0x0190,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030118 .tolerance_regs = 0x018c,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030119 .names = { "Y", "Z", "AA", "AB" },
120 },
121 {
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030122 .val_regs = 0x01e8,
123 .irq_regs = 0x01a8,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930124 .debounce_regs = 0x01c0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030125 .tolerance_regs = 0x01bc,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030126 .names = { "AC", "", "", "" },
127 },
Joel Stanley361b7912016-08-30 17:24:27 +0930128};
129
130#define GPIO_BANK(x) ((x) >> 5)
131#define GPIO_OFFSET(x) ((x) & 0x1f)
132#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
133
134#define GPIO_DATA 0x00
135#define GPIO_DIR 0x04
136
137#define GPIO_IRQ_ENABLE 0x00
138#define GPIO_IRQ_TYPE0 0x04
139#define GPIO_IRQ_TYPE1 0x08
140#define GPIO_IRQ_TYPE2 0x0c
141#define GPIO_IRQ_STATUS 0x10
142
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930143#define GPIO_DEBOUNCE_SEL1 0x00
144#define GPIO_DEBOUNCE_SEL2 0x04
145
146#define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
147#define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
148#define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
149
Joel Stanley361b7912016-08-30 17:24:27 +0930150static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
151{
152 unsigned int bank = GPIO_BANK(offset);
153
Vasyl Gomonovychfe138622017-12-21 16:55:10 +0100154 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
Joel Stanley361b7912016-08-30 17:24:27 +0930155 return &aspeed_gpio_banks[bank];
156}
157
Andrew Jeffery1736f752017-01-24 16:46:46 +1030158static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
159{
160 return !(props->input || props->output);
161}
162
163static inline const struct aspeed_bank_props *find_bank_props(
164 struct aspeed_gpio *gpio, unsigned int offset)
165{
166 const struct aspeed_bank_props *props = gpio->config->props;
167
168 while (!is_bank_props_sentinel(props)) {
169 if (props->bank == GPIO_BANK(offset))
170 return props;
171 props++;
172 }
173
174 return NULL;
175}
176
177static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
178{
179 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
180 const struct aspeed_gpio_bank *bank = to_bank(offset);
181 unsigned int group = GPIO_OFFSET(offset) / 8;
182
183 return bank->names[group][0] != '\0' &&
184 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
185}
186
187static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
188{
189 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
190
191 return !props || (props->input & GPIO_BIT(offset));
192}
193
194#define have_irq(g, o) have_input((g), (o))
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930195#define have_debounce(g, o) have_input((g), (o))
Andrew Jeffery1736f752017-01-24 16:46:46 +1030196
197static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
198{
199 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
200
201 return !props || (props->output & GPIO_BIT(offset));
202}
203
Joel Stanley361b7912016-08-30 17:24:27 +0930204static void __iomem *bank_val_reg(struct aspeed_gpio *gpio,
205 const struct aspeed_gpio_bank *bank,
206 unsigned int reg)
207{
208 return gpio->base + bank->val_regs + reg;
209}
210
211static void __iomem *bank_irq_reg(struct aspeed_gpio *gpio,
212 const struct aspeed_gpio_bank *bank,
213 unsigned int reg)
214{
215 return gpio->base + bank->irq_regs + reg;
216}
217
218static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
219{
220 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
221 const struct aspeed_gpio_bank *bank = to_bank(offset);
222
223 return !!(ioread32(bank_val_reg(gpio, bank, GPIO_DATA))
224 & GPIO_BIT(offset));
225}
226
227static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
228 int val)
229{
230 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
231 const struct aspeed_gpio_bank *bank = to_bank(offset);
232 void __iomem *addr;
233 u32 reg;
234
235 addr = bank_val_reg(gpio, bank, GPIO_DATA);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000236 reg = gpio->dcache[GPIO_BANK(offset)];
Joel Stanley361b7912016-08-30 17:24:27 +0930237
238 if (val)
239 reg |= GPIO_BIT(offset);
240 else
241 reg &= ~GPIO_BIT(offset);
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000242 gpio->dcache[GPIO_BANK(offset)] = reg;
Joel Stanley361b7912016-08-30 17:24:27 +0930243
244 iowrite32(reg, addr);
245}
246
247static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
248 int val)
249{
250 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
251 unsigned long flags;
252
253 spin_lock_irqsave(&gpio->lock, flags);
254
255 __aspeed_gpio_set(gc, offset, val);
256
257 spin_unlock_irqrestore(&gpio->lock, flags);
258}
259
260static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
261{
262 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
263 const struct aspeed_gpio_bank *bank = to_bank(offset);
264 unsigned long flags;
265 u32 reg;
266
Andrew Jeffery1736f752017-01-24 16:46:46 +1030267 if (!have_input(gpio, offset))
268 return -ENOTSUPP;
269
Joel Stanley361b7912016-08-30 17:24:27 +0930270 spin_lock_irqsave(&gpio->lock, flags);
271
272 reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
273 iowrite32(reg & ~GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
274
275 spin_unlock_irqrestore(&gpio->lock, flags);
276
277 return 0;
278}
279
280static int aspeed_gpio_dir_out(struct gpio_chip *gc,
281 unsigned int offset, int val)
282{
283 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
284 const struct aspeed_gpio_bank *bank = to_bank(offset);
285 unsigned long flags;
286 u32 reg;
287
Andrew Jeffery1736f752017-01-24 16:46:46 +1030288 if (!have_output(gpio, offset))
289 return -ENOTSUPP;
290
Joel Stanley361b7912016-08-30 17:24:27 +0930291 spin_lock_irqsave(&gpio->lock, flags);
292
Benjamin Herrenschmidtaf794922018-05-17 18:11:56 +1000293 __aspeed_gpio_set(gc, offset, val);
Joel Stanley361b7912016-08-30 17:24:27 +0930294 reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
295 iowrite32(reg | GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
296
Joel Stanley361b7912016-08-30 17:24:27 +0930297 spin_unlock_irqrestore(&gpio->lock, flags);
298
299 return 0;
300}
301
302static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
303{
304 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
305 const struct aspeed_gpio_bank *bank = to_bank(offset);
306 unsigned long flags;
307 u32 val;
308
Andrew Jeffery1736f752017-01-24 16:46:46 +1030309 if (!have_input(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030310 return 0;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030311
312 if (!have_output(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030313 return 1;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030314
Joel Stanley361b7912016-08-30 17:24:27 +0930315 spin_lock_irqsave(&gpio->lock, flags);
316
317 val = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)) & GPIO_BIT(offset);
318
319 spin_unlock_irqrestore(&gpio->lock, flags);
320
321 return !val;
322
323}
324
325static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
326 struct aspeed_gpio **gpio,
327 const struct aspeed_gpio_bank **bank,
328 u32 *bit)
329{
330 int offset;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030331 struct aspeed_gpio *internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930332
333 offset = irqd_to_hwirq(d);
334
Andrew Jeffery1736f752017-01-24 16:46:46 +1030335 internal = irq_data_get_irq_chip_data(d);
336
337 /* This might be a bit of a questionable place to check */
338 if (!have_irq(internal, offset))
339 return -ENOTSUPP;
340
341 *gpio = internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930342 *bank = to_bank(offset);
343 *bit = GPIO_BIT(offset);
344
345 return 0;
346}
347
348static void aspeed_gpio_irq_ack(struct irq_data *d)
349{
350 const struct aspeed_gpio_bank *bank;
351 struct aspeed_gpio *gpio;
352 unsigned long flags;
353 void __iomem *status_addr;
354 u32 bit;
355 int rc;
356
357 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
358 if (rc)
359 return;
360
361 status_addr = bank_irq_reg(gpio, bank, GPIO_IRQ_STATUS);
362
363 spin_lock_irqsave(&gpio->lock, flags);
364 iowrite32(bit, status_addr);
365 spin_unlock_irqrestore(&gpio->lock, flags);
366}
367
368static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
369{
370 const struct aspeed_gpio_bank *bank;
371 struct aspeed_gpio *gpio;
372 unsigned long flags;
373 u32 reg, bit;
374 void __iomem *addr;
375 int rc;
376
377 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
378 if (rc)
379 return;
380
381 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_ENABLE);
382
383 spin_lock_irqsave(&gpio->lock, flags);
384
385 reg = ioread32(addr);
386 if (set)
387 reg |= bit;
388 else
389 reg &= bit;
390 iowrite32(reg, addr);
391
392 spin_unlock_irqrestore(&gpio->lock, flags);
393}
394
395static void aspeed_gpio_irq_mask(struct irq_data *d)
396{
397 aspeed_gpio_irq_set_mask(d, false);
398}
399
400static void aspeed_gpio_irq_unmask(struct irq_data *d)
401{
402 aspeed_gpio_irq_set_mask(d, true);
403}
404
405static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
406{
407 u32 type0 = 0;
408 u32 type1 = 0;
409 u32 type2 = 0;
410 u32 bit, reg;
411 const struct aspeed_gpio_bank *bank;
412 irq_flow_handler_t handler;
413 struct aspeed_gpio *gpio;
414 unsigned long flags;
415 void __iomem *addr;
416 int rc;
417
418 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
419 if (rc)
420 return -EINVAL;
421
422 switch (type & IRQ_TYPE_SENSE_MASK) {
423 case IRQ_TYPE_EDGE_BOTH:
424 type2 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500425 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930426 case IRQ_TYPE_EDGE_RISING:
427 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500428 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930429 case IRQ_TYPE_EDGE_FALLING:
430 handler = handle_edge_irq;
431 break;
432 case IRQ_TYPE_LEVEL_HIGH:
433 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500434 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930435 case IRQ_TYPE_LEVEL_LOW:
436 type1 |= bit;
437 handler = handle_level_irq;
438 break;
439 default:
440 return -EINVAL;
441 }
442
443 spin_lock_irqsave(&gpio->lock, flags);
444
445 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0);
446 reg = ioread32(addr);
447 reg = (reg & ~bit) | type0;
448 iowrite32(reg, addr);
449
450 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1);
451 reg = ioread32(addr);
452 reg = (reg & ~bit) | type1;
453 iowrite32(reg, addr);
454
455 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2);
456 reg = ioread32(addr);
457 reg = (reg & ~bit) | type2;
458 iowrite32(reg, addr);
459
460 spin_unlock_irqrestore(&gpio->lock, flags);
461
462 irq_set_handler_locked(d, handler);
463
464 return 0;
465}
466
467static void aspeed_gpio_irq_handler(struct irq_desc *desc)
468{
469 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
470 struct irq_chip *ic = irq_desc_get_chip(desc);
471 struct aspeed_gpio *data = gpiochip_get_data(gc);
472 unsigned int i, p, girq;
473 unsigned long reg;
474
475 chained_irq_enter(ic, desc);
476
477 for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
478 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
479
480 reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
481
482 for_each_set_bit(p, &reg, 32) {
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100483 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
Joel Stanley361b7912016-08-30 17:24:27 +0930484 generic_handle_irq(girq);
485 }
486
487 }
488
489 chained_irq_exit(ic, desc);
490}
491
492static struct irq_chip aspeed_gpio_irqchip = {
493 .name = "aspeed-gpio",
494 .irq_ack = aspeed_gpio_irq_ack,
495 .irq_mask = aspeed_gpio_irq_mask,
496 .irq_unmask = aspeed_gpio_irq_unmask,
497 .irq_set_type = aspeed_gpio_set_type,
498};
499
Andrew Jeffery1736f752017-01-24 16:46:46 +1030500static void set_irq_valid_mask(struct aspeed_gpio *gpio)
501{
502 const struct aspeed_bank_props *props = gpio->config->props;
503
504 while (!is_bank_props_sentinel(props)) {
505 unsigned int offset;
506 const unsigned long int input = props->input;
507
508 /* Pretty crummy approach, but similar to GPIO core */
509 for_each_clear_bit(offset, &input, 32) {
510 unsigned int i = props->bank * 32 + offset;
511
512 if (i >= gpio->config->nr_gpios)
513 break;
514
Thierry Redingdc7b0382017-11-07 19:15:52 +0100515 clear_bit(i, gpio->chip.irq.valid_mask);
Andrew Jeffery1736f752017-01-24 16:46:46 +1030516 }
517
518 props++;
519 }
520}
521
Joel Stanley361b7912016-08-30 17:24:27 +0930522static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
523 struct platform_device *pdev)
524{
525 int rc;
526
527 rc = platform_get_irq(pdev, 0);
528 if (rc < 0)
529 return rc;
530
531 gpio->irq = rc;
532
Andrew Jeffery1736f752017-01-24 16:46:46 +1030533 set_irq_valid_mask(gpio);
534
Joel Stanley361b7912016-08-30 17:24:27 +0930535 rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
536 0, handle_bad_irq, IRQ_TYPE_NONE);
537 if (rc) {
538 dev_info(&pdev->dev, "Could not add irqchip\n");
539 return rc;
540 }
541
542 gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
543 gpio->irq, aspeed_gpio_irq_handler);
544
545 return 0;
546}
547
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030548static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
549 unsigned int offset, bool enable)
550{
551 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
552 const struct aspeed_gpio_bank *bank;
553 unsigned long flags;
554 u32 val;
555
556 bank = to_bank(offset);
557
558 spin_lock_irqsave(&gpio->lock, flags);
559 val = readl(gpio->base + bank->tolerance_regs);
560
561 if (enable)
562 val |= GPIO_BIT(offset);
563 else
564 val &= ~GPIO_BIT(offset);
565
566 writel(val, gpio->base + bank->tolerance_regs);
567 spin_unlock_irqrestore(&gpio->lock, flags);
568
569 return 0;
570}
571
Joel Stanley361b7912016-08-30 17:24:27 +0930572static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
573{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030574 if (!have_gpio(gpiochip_get_data(chip), offset))
575 return -ENODEV;
576
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200577 return pinctrl_gpio_request(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930578}
579
580static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
581{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200582 pinctrl_gpio_free(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930583}
584
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930585static inline void __iomem *bank_debounce_reg(struct aspeed_gpio *gpio,
586 const struct aspeed_gpio_bank *bank,
587 unsigned int reg)
588{
589 return gpio->base + bank->debounce_regs + reg;
590}
591
592static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
593 u32 *cycles)
594{
595 u64 rate;
596 u64 n;
597 u32 r;
598
599 rate = clk_get_rate(gpio->clk);
600 if (!rate)
601 return -ENOTSUPP;
602
603 n = rate * usecs;
604 r = do_div(n, 1000000);
605
606 if (n >= U32_MAX)
607 return -ERANGE;
608
609 /* At least as long as the requested time */
610 *cycles = n + (!!r);
611
612 return 0;
613}
614
615/* Call under gpio->lock */
616static int register_allocated_timer(struct aspeed_gpio *gpio,
617 unsigned int offset, unsigned int timer)
618{
619 if (WARN(gpio->offset_timer[offset] != 0,
620 "Offset %d already allocated timer %d\n",
621 offset, gpio->offset_timer[offset]))
622 return -EINVAL;
623
624 if (WARN(gpio->timer_users[timer] == UINT_MAX,
625 "Timer user count would overflow\n"))
626 return -EPERM;
627
628 gpio->offset_timer[offset] = timer;
629 gpio->timer_users[timer]++;
630
631 return 0;
632}
633
634/* Call under gpio->lock */
635static int unregister_allocated_timer(struct aspeed_gpio *gpio,
636 unsigned int offset)
637{
638 if (WARN(gpio->offset_timer[offset] == 0,
639 "No timer allocated to offset %d\n", offset))
640 return -EINVAL;
641
642 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
643 "No users recorded for timer %d\n",
644 gpio->offset_timer[offset]))
645 return -EINVAL;
646
647 gpio->timer_users[gpio->offset_timer[offset]]--;
648 gpio->offset_timer[offset] = 0;
649
650 return 0;
651}
652
653/* Call under gpio->lock */
654static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
655 unsigned int offset)
656{
657 return gpio->offset_timer[offset] > 0;
658}
659
660/* Call under gpio->lock */
661static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
662 unsigned int timer)
663{
664 const struct aspeed_gpio_bank *bank = to_bank(offset);
665 const u32 mask = GPIO_BIT(offset);
666 void __iomem *addr;
667 u32 val;
668
669 addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL1);
670 val = ioread32(addr);
671 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
672
673 addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL2);
674 val = ioread32(addr);
675 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
676}
677
678static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
679 unsigned long usecs)
680{
681 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
682 u32 requested_cycles;
683 unsigned long flags;
684 int rc;
685 int i;
686
Joel Stanleydf563c82017-05-02 15:38:24 +0930687 if (!gpio->clk)
688 return -EINVAL;
689
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930690 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
691 if (rc < 0) {
692 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
693 usecs, clk_get_rate(gpio->clk), rc);
694 return rc;
695 }
696
697 spin_lock_irqsave(&gpio->lock, flags);
698
699 if (timer_allocation_registered(gpio, offset)) {
700 rc = unregister_allocated_timer(gpio, offset);
701 if (rc < 0)
702 goto out;
703 }
704
705 /* Try to find a timer already configured for the debounce period */
706 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
707 u32 cycles;
708
709 cycles = ioread32(gpio->base + debounce_timers[i]);
710 if (requested_cycles == cycles)
711 break;
712 }
713
714 if (i == ARRAY_SIZE(debounce_timers)) {
715 int j;
716
717 /*
718 * As there are no timers configured for the requested debounce
719 * period, find an unused timer instead
720 */
721 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
722 if (gpio->timer_users[j] == 0)
723 break;
724 }
725
726 if (j == ARRAY_SIZE(gpio->timer_users)) {
727 dev_warn(chip->parent,
728 "Debounce timers exhausted, cannot debounce for period %luus\n",
729 usecs);
730
731 rc = -EPERM;
732
733 /*
734 * We already adjusted the accounting to remove @offset
735 * as a user of its previous timer, so also configure
736 * the hardware so @offset has timers disabled for
737 * consistency.
738 */
739 configure_timer(gpio, offset, 0);
740 goto out;
741 }
742
743 i = j;
744
745 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
746 }
747
748 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
749 rc = -EINVAL;
750 goto out;
751 }
752
753 register_allocated_timer(gpio, offset, i);
754 configure_timer(gpio, offset, i);
755
756out:
757 spin_unlock_irqrestore(&gpio->lock, flags);
758
759 return rc;
760}
761
762static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
763{
764 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
765 unsigned long flags;
766 int rc;
767
768 spin_lock_irqsave(&gpio->lock, flags);
769
770 rc = unregister_allocated_timer(gpio, offset);
771 if (!rc)
772 configure_timer(gpio, offset, 0);
773
774 spin_unlock_irqrestore(&gpio->lock, flags);
775
776 return rc;
777}
778
779static int set_debounce(struct gpio_chip *chip, unsigned int offset,
780 unsigned long usecs)
781{
782 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
783
784 if (!have_debounce(gpio, offset))
785 return -ENOTSUPP;
786
787 if (usecs)
788 return enable_debounce(chip, offset, usecs);
789
790 return disable_debounce(chip, offset);
791}
792
793static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
794 unsigned long config)
795{
796 unsigned long param = pinconf_to_config_param(config);
797 u32 arg = pinconf_to_config_argument(config);
798
799 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
800 return set_debounce(chip, offset, arg);
801 else if (param == PIN_CONFIG_BIAS_DISABLE ||
802 param == PIN_CONFIG_BIAS_PULL_DOWN ||
803 param == PIN_CONFIG_DRIVE_STRENGTH)
804 return pinctrl_gpio_set_config(offset, config);
Andrew Jefferyc3bafe02017-04-07 22:29:02 +0930805 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
806 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
807 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
808 return -ENOTSUPP;
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030809 else if (param == PIN_CONFIG_PERSIST_STATE)
810 return aspeed_gpio_reset_tolerance(chip, offset, arg);
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930811
812 return -ENOTSUPP;
813}
814
Andrew Jeffery1736f752017-01-24 16:46:46 +1030815/*
816 * Any banks not specified in a struct aspeed_bank_props array are assumed to
817 * have the properties:
818 *
819 * { .input = 0xffffffff, .output = 0xffffffff }
820 */
821
822static const struct aspeed_bank_props ast2400_bank_props[] = {
823 /* input output */
824 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
825 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
826 { },
827};
828
829static const struct aspeed_gpio_config ast2400_config =
830 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
831 { .nr_gpios = 220, .props = ast2400_bank_props, };
832
833static const struct aspeed_bank_props ast2500_bank_props[] = {
834 /* input output */
835 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
836 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
837 { 7, 0x000000ff, 0x000000ff }, /* AC */
838 { },
839};
840
841static const struct aspeed_gpio_config ast2500_config =
842 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
843 { .nr_gpios = 232, .props = ast2500_bank_props, };
844
845static const struct of_device_id aspeed_gpio_of_table[] = {
846 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
847 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
848 {}
849};
850MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
851
Joel Stanley361b7912016-08-30 17:24:27 +0930852static int __init aspeed_gpio_probe(struct platform_device *pdev)
853{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030854 const struct of_device_id *gpio_id;
Joel Stanley361b7912016-08-30 17:24:27 +0930855 struct aspeed_gpio *gpio;
856 struct resource *res;
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000857 int rc, i, banks;
Joel Stanley361b7912016-08-30 17:24:27 +0930858
859 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
860 if (!gpio)
861 return -ENOMEM;
862
863 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joel Stanley361b7912016-08-30 17:24:27 +0930864 gpio->base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun7f8b9652016-09-15 01:30:32 +0000865 if (IS_ERR(gpio->base))
866 return PTR_ERR(gpio->base);
Joel Stanley361b7912016-08-30 17:24:27 +0930867
868 spin_lock_init(&gpio->lock);
869
Andrew Jeffery1736f752017-01-24 16:46:46 +1030870 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
871 if (!gpio_id)
872 return -EINVAL;
Joel Stanley361b7912016-08-30 17:24:27 +0930873
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930874 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
875 if (IS_ERR(gpio->clk)) {
876 dev_warn(&pdev->dev,
Andrew Jeffery754c0452017-08-08 15:37:36 +0930877 "Failed to get clock from devicetree, debouncing disabled\n");
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930878 gpio->clk = NULL;
879 }
880
Andrew Jeffery1736f752017-01-24 16:46:46 +1030881 gpio->config = gpio_id->data;
882
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930883 gpio->chip.parent = &pdev->dev;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030884 gpio->chip.ngpio = gpio->config->nr_gpios;
Joel Stanley361b7912016-08-30 17:24:27 +0930885 gpio->chip.parent = &pdev->dev;
886 gpio->chip.direction_input = aspeed_gpio_dir_in;
887 gpio->chip.direction_output = aspeed_gpio_dir_out;
888 gpio->chip.get_direction = aspeed_gpio_get_direction;
889 gpio->chip.request = aspeed_gpio_request;
890 gpio->chip.free = aspeed_gpio_free;
891 gpio->chip.get = aspeed_gpio_get;
892 gpio->chip.set = aspeed_gpio_set;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930893 gpio->chip.set_config = aspeed_gpio_set_config;
Joel Stanley361b7912016-08-30 17:24:27 +0930894 gpio->chip.label = dev_name(&pdev->dev);
895 gpio->chip.base = -1;
Thierry Redingdc7b0382017-11-07 19:15:52 +0100896 gpio->chip.irq.need_valid_mask = true;
Joel Stanley361b7912016-08-30 17:24:27 +0930897
Benjamin Herrenschmidted5cab42018-05-17 18:12:02 +1000898 /* Allocate a cache of the output registers */
899 banks = gpio->config->nr_gpios >> 5;
900 gpio->dcache = devm_kzalloc(&pdev->dev,
901 sizeof(u32) * banks, GFP_KERNEL);
902 if (!gpio->dcache)
903 return -ENOMEM;
904
905 /* Populate it with initial values read from the HW */
906 for (i = 0; i < banks; i++) {
907 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
908 gpio->dcache[i] = ioread32(gpio->base + bank->val_regs +
909 GPIO_DATA);
910 }
911
Joel Stanley361b7912016-08-30 17:24:27 +0930912 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
913 if (rc < 0)
914 return rc;
915
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930916 gpio->offset_timer =
917 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
918
Joel Stanley361b7912016-08-30 17:24:27 +0930919 return aspeed_gpio_setup_irqs(gpio, pdev);
920}
921
Joel Stanley361b7912016-08-30 17:24:27 +0930922static struct platform_driver aspeed_gpio_driver = {
923 .driver = {
924 .name = KBUILD_MODNAME,
925 .of_match_table = aspeed_gpio_of_table,
926 },
927};
928
929module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
930
931MODULE_DESCRIPTION("Aspeed GPIO Driver");
Linus Walleije50237c2016-09-13 13:43:34 +0200932MODULE_LICENSE("GPL");