blob: f210bed83dd6f0ef9355c2a710ea9f7fa10712d5 [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;
Joel Stanley361b7912016-08-30 17:24:27 +093057};
58
59struct aspeed_gpio_bank {
60 uint16_t val_regs;
61 uint16_t irq_regs;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093062 uint16_t debounce_regs;
Andrew Jeffery1b43d262017-11-30 14:25:25 +103063 uint16_t tolerance_regs;
Joel Stanley7153f8e2017-01-23 15:56:06 +103064 const char names[4][3];
Joel Stanley361b7912016-08-30 17:24:27 +093065};
66
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093067static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
68
Joel Stanley361b7912016-08-30 17:24:27 +093069static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
70 {
71 .val_regs = 0x0000,
72 .irq_regs = 0x0008,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093073 .debounce_regs = 0x0040,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103074 .tolerance_regs = 0x001c,
Joel Stanley7153f8e2017-01-23 15:56:06 +103075 .names = { "A", "B", "C", "D" },
Joel Stanley361b7912016-08-30 17:24:27 +093076 },
77 {
78 .val_regs = 0x0020,
79 .irq_regs = 0x0028,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093080 .debounce_regs = 0x0048,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103081 .tolerance_regs = 0x003c,
Joel Stanley7153f8e2017-01-23 15:56:06 +103082 .names = { "E", "F", "G", "H" },
Joel Stanley361b7912016-08-30 17:24:27 +093083 },
84 {
85 .val_regs = 0x0070,
86 .irq_regs = 0x0098,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093087 .debounce_regs = 0x00b0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103088 .tolerance_regs = 0x00ac,
Joel Stanley7153f8e2017-01-23 15:56:06 +103089 .names = { "I", "J", "K", "L" },
Joel Stanley361b7912016-08-30 17:24:27 +093090 },
91 {
92 .val_regs = 0x0078,
93 .irq_regs = 0x00e8,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +093094 .debounce_regs = 0x0100,
Andrew Jeffery1b43d262017-11-30 14:25:25 +103095 .tolerance_regs = 0x00fc,
Joel Stanley7153f8e2017-01-23 15:56:06 +103096 .names = { "M", "N", "O", "P" },
Joel Stanley361b7912016-08-30 17:24:27 +093097 },
98 {
99 .val_regs = 0x0080,
100 .irq_regs = 0x0118,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930101 .debounce_regs = 0x0130,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030102 .tolerance_regs = 0x012c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030103 .names = { "Q", "R", "S", "T" },
Joel Stanley361b7912016-08-30 17:24:27 +0930104 },
105 {
106 .val_regs = 0x0088,
107 .irq_regs = 0x0148,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930108 .debounce_regs = 0x0160,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030109 .tolerance_regs = 0x015c,
Joel Stanley7153f8e2017-01-23 15:56:06 +1030110 .names = { "U", "V", "W", "X" },
Joel Stanley361b7912016-08-30 17:24:27 +0930111 },
Andrew Jeffery1736f752017-01-24 16:46:46 +1030112 {
113 .val_regs = 0x01E0,
114 .irq_regs = 0x0178,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930115 .debounce_regs = 0x0190,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030116 .tolerance_regs = 0x018c,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030117 .names = { "Y", "Z", "AA", "AB" },
118 },
119 {
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030120 .val_regs = 0x01e8,
121 .irq_regs = 0x01a8,
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930122 .debounce_regs = 0x01c0,
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030123 .tolerance_regs = 0x01bc,
Andrew Jeffery1736f752017-01-24 16:46:46 +1030124 .names = { "AC", "", "", "" },
125 },
Joel Stanley361b7912016-08-30 17:24:27 +0930126};
127
128#define GPIO_BANK(x) ((x) >> 5)
129#define GPIO_OFFSET(x) ((x) & 0x1f)
130#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
131
132#define GPIO_DATA 0x00
133#define GPIO_DIR 0x04
134
135#define GPIO_IRQ_ENABLE 0x00
136#define GPIO_IRQ_TYPE0 0x04
137#define GPIO_IRQ_TYPE1 0x08
138#define GPIO_IRQ_TYPE2 0x0c
139#define GPIO_IRQ_STATUS 0x10
140
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930141#define GPIO_DEBOUNCE_SEL1 0x00
142#define GPIO_DEBOUNCE_SEL2 0x04
143
144#define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
145#define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
146#define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
147
Joel Stanley361b7912016-08-30 17:24:27 +0930148static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
149{
150 unsigned int bank = GPIO_BANK(offset);
151
Vasyl Gomonovychfe138622017-12-21 16:55:10 +0100152 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
Joel Stanley361b7912016-08-30 17:24:27 +0930153 return &aspeed_gpio_banks[bank];
154}
155
Andrew Jeffery1736f752017-01-24 16:46:46 +1030156static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
157{
158 return !(props->input || props->output);
159}
160
161static inline const struct aspeed_bank_props *find_bank_props(
162 struct aspeed_gpio *gpio, unsigned int offset)
163{
164 const struct aspeed_bank_props *props = gpio->config->props;
165
166 while (!is_bank_props_sentinel(props)) {
167 if (props->bank == GPIO_BANK(offset))
168 return props;
169 props++;
170 }
171
172 return NULL;
173}
174
175static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
176{
177 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
178 const struct aspeed_gpio_bank *bank = to_bank(offset);
179 unsigned int group = GPIO_OFFSET(offset) / 8;
180
181 return bank->names[group][0] != '\0' &&
182 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
183}
184
185static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
186{
187 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
188
189 return !props || (props->input & GPIO_BIT(offset));
190}
191
192#define have_irq(g, o) have_input((g), (o))
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930193#define have_debounce(g, o) have_input((g), (o))
Andrew Jeffery1736f752017-01-24 16:46:46 +1030194
195static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
196{
197 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
198
199 return !props || (props->output & GPIO_BIT(offset));
200}
201
Joel Stanley361b7912016-08-30 17:24:27 +0930202static void __iomem *bank_val_reg(struct aspeed_gpio *gpio,
203 const struct aspeed_gpio_bank *bank,
204 unsigned int reg)
205{
206 return gpio->base + bank->val_regs + reg;
207}
208
209static void __iomem *bank_irq_reg(struct aspeed_gpio *gpio,
210 const struct aspeed_gpio_bank *bank,
211 unsigned int reg)
212{
213 return gpio->base + bank->irq_regs + reg;
214}
215
216static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
217{
218 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
219 const struct aspeed_gpio_bank *bank = to_bank(offset);
220
221 return !!(ioread32(bank_val_reg(gpio, bank, GPIO_DATA))
222 & GPIO_BIT(offset));
223}
224
225static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
226 int val)
227{
228 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
229 const struct aspeed_gpio_bank *bank = to_bank(offset);
230 void __iomem *addr;
231 u32 reg;
232
233 addr = bank_val_reg(gpio, bank, GPIO_DATA);
234 reg = ioread32(addr);
235
236 if (val)
237 reg |= GPIO_BIT(offset);
238 else
239 reg &= ~GPIO_BIT(offset);
240
241 iowrite32(reg, addr);
242}
243
244static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
245 int val)
246{
247 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
248 unsigned long flags;
249
250 spin_lock_irqsave(&gpio->lock, flags);
251
252 __aspeed_gpio_set(gc, offset, val);
253
254 spin_unlock_irqrestore(&gpio->lock, flags);
255}
256
257static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
258{
259 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
260 const struct aspeed_gpio_bank *bank = to_bank(offset);
261 unsigned long flags;
262 u32 reg;
263
Andrew Jeffery1736f752017-01-24 16:46:46 +1030264 if (!have_input(gpio, offset))
265 return -ENOTSUPP;
266
Joel Stanley361b7912016-08-30 17:24:27 +0930267 spin_lock_irqsave(&gpio->lock, flags);
268
269 reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
270 iowrite32(reg & ~GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
271
272 spin_unlock_irqrestore(&gpio->lock, flags);
273
274 return 0;
275}
276
277static int aspeed_gpio_dir_out(struct gpio_chip *gc,
278 unsigned int offset, int val)
279{
280 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
281 const struct aspeed_gpio_bank *bank = to_bank(offset);
282 unsigned long flags;
283 u32 reg;
284
Andrew Jeffery1736f752017-01-24 16:46:46 +1030285 if (!have_output(gpio, offset))
286 return -ENOTSUPP;
287
Joel Stanley361b7912016-08-30 17:24:27 +0930288 spin_lock_irqsave(&gpio->lock, flags);
289
Benjamin Herrenschmidtaf794922018-05-17 18:11:56 +1000290 __aspeed_gpio_set(gc, offset, val);
Joel Stanley361b7912016-08-30 17:24:27 +0930291 reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
292 iowrite32(reg | GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
293
Joel Stanley361b7912016-08-30 17:24:27 +0930294 spin_unlock_irqrestore(&gpio->lock, flags);
295
296 return 0;
297}
298
299static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
300{
301 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
302 const struct aspeed_gpio_bank *bank = to_bank(offset);
303 unsigned long flags;
304 u32 val;
305
Andrew Jeffery1736f752017-01-24 16:46:46 +1030306 if (!have_input(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030307 return 0;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030308
309 if (!have_output(gpio, offset))
Andrew Jeffery619e96f2017-02-02 14:58:17 +1030310 return 1;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030311
Joel Stanley361b7912016-08-30 17:24:27 +0930312 spin_lock_irqsave(&gpio->lock, flags);
313
314 val = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)) & GPIO_BIT(offset);
315
316 spin_unlock_irqrestore(&gpio->lock, flags);
317
318 return !val;
319
320}
321
322static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
323 struct aspeed_gpio **gpio,
324 const struct aspeed_gpio_bank **bank,
325 u32 *bit)
326{
327 int offset;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030328 struct aspeed_gpio *internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930329
330 offset = irqd_to_hwirq(d);
331
Andrew Jeffery1736f752017-01-24 16:46:46 +1030332 internal = irq_data_get_irq_chip_data(d);
333
334 /* This might be a bit of a questionable place to check */
335 if (!have_irq(internal, offset))
336 return -ENOTSUPP;
337
338 *gpio = internal;
Joel Stanley361b7912016-08-30 17:24:27 +0930339 *bank = to_bank(offset);
340 *bit = GPIO_BIT(offset);
341
342 return 0;
343}
344
345static void aspeed_gpio_irq_ack(struct irq_data *d)
346{
347 const struct aspeed_gpio_bank *bank;
348 struct aspeed_gpio *gpio;
349 unsigned long flags;
350 void __iomem *status_addr;
351 u32 bit;
352 int rc;
353
354 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
355 if (rc)
356 return;
357
358 status_addr = bank_irq_reg(gpio, bank, GPIO_IRQ_STATUS);
359
360 spin_lock_irqsave(&gpio->lock, flags);
361 iowrite32(bit, status_addr);
362 spin_unlock_irqrestore(&gpio->lock, flags);
363}
364
365static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
366{
367 const struct aspeed_gpio_bank *bank;
368 struct aspeed_gpio *gpio;
369 unsigned long flags;
370 u32 reg, bit;
371 void __iomem *addr;
372 int rc;
373
374 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
375 if (rc)
376 return;
377
378 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_ENABLE);
379
380 spin_lock_irqsave(&gpio->lock, flags);
381
382 reg = ioread32(addr);
383 if (set)
384 reg |= bit;
385 else
386 reg &= bit;
387 iowrite32(reg, addr);
388
389 spin_unlock_irqrestore(&gpio->lock, flags);
390}
391
392static void aspeed_gpio_irq_mask(struct irq_data *d)
393{
394 aspeed_gpio_irq_set_mask(d, false);
395}
396
397static void aspeed_gpio_irq_unmask(struct irq_data *d)
398{
399 aspeed_gpio_irq_set_mask(d, true);
400}
401
402static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
403{
404 u32 type0 = 0;
405 u32 type1 = 0;
406 u32 type2 = 0;
407 u32 bit, reg;
408 const struct aspeed_gpio_bank *bank;
409 irq_flow_handler_t handler;
410 struct aspeed_gpio *gpio;
411 unsigned long flags;
412 void __iomem *addr;
413 int rc;
414
415 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
416 if (rc)
417 return -EINVAL;
418
419 switch (type & IRQ_TYPE_SENSE_MASK) {
420 case IRQ_TYPE_EDGE_BOTH:
421 type2 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500422 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930423 case IRQ_TYPE_EDGE_RISING:
424 type0 |= 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_FALLING:
427 handler = handle_edge_irq;
428 break;
429 case IRQ_TYPE_LEVEL_HIGH:
430 type0 |= bit;
Gustavo A. R. Silvae80df7b2017-10-13 15:43:53 -0500431 /* fall through */
Joel Stanley361b7912016-08-30 17:24:27 +0930432 case IRQ_TYPE_LEVEL_LOW:
433 type1 |= bit;
434 handler = handle_level_irq;
435 break;
436 default:
437 return -EINVAL;
438 }
439
440 spin_lock_irqsave(&gpio->lock, flags);
441
442 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0);
443 reg = ioread32(addr);
444 reg = (reg & ~bit) | type0;
445 iowrite32(reg, addr);
446
447 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1);
448 reg = ioread32(addr);
449 reg = (reg & ~bit) | type1;
450 iowrite32(reg, addr);
451
452 addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2);
453 reg = ioread32(addr);
454 reg = (reg & ~bit) | type2;
455 iowrite32(reg, addr);
456
457 spin_unlock_irqrestore(&gpio->lock, flags);
458
459 irq_set_handler_locked(d, handler);
460
461 return 0;
462}
463
464static void aspeed_gpio_irq_handler(struct irq_desc *desc)
465{
466 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
467 struct irq_chip *ic = irq_desc_get_chip(desc);
468 struct aspeed_gpio *data = gpiochip_get_data(gc);
469 unsigned int i, p, girq;
470 unsigned long reg;
471
472 chained_irq_enter(ic, desc);
473
474 for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
475 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
476
477 reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
478
479 for_each_set_bit(p, &reg, 32) {
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100480 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
Joel Stanley361b7912016-08-30 17:24:27 +0930481 generic_handle_irq(girq);
482 }
483
484 }
485
486 chained_irq_exit(ic, desc);
487}
488
489static struct irq_chip aspeed_gpio_irqchip = {
490 .name = "aspeed-gpio",
491 .irq_ack = aspeed_gpio_irq_ack,
492 .irq_mask = aspeed_gpio_irq_mask,
493 .irq_unmask = aspeed_gpio_irq_unmask,
494 .irq_set_type = aspeed_gpio_set_type,
495};
496
Andrew Jeffery1736f752017-01-24 16:46:46 +1030497static void set_irq_valid_mask(struct aspeed_gpio *gpio)
498{
499 const struct aspeed_bank_props *props = gpio->config->props;
500
501 while (!is_bank_props_sentinel(props)) {
502 unsigned int offset;
503 const unsigned long int input = props->input;
504
505 /* Pretty crummy approach, but similar to GPIO core */
506 for_each_clear_bit(offset, &input, 32) {
507 unsigned int i = props->bank * 32 + offset;
508
509 if (i >= gpio->config->nr_gpios)
510 break;
511
Thierry Redingdc7b0382017-11-07 19:15:52 +0100512 clear_bit(i, gpio->chip.irq.valid_mask);
Andrew Jeffery1736f752017-01-24 16:46:46 +1030513 }
514
515 props++;
516 }
517}
518
Joel Stanley361b7912016-08-30 17:24:27 +0930519static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
520 struct platform_device *pdev)
521{
522 int rc;
523
524 rc = platform_get_irq(pdev, 0);
525 if (rc < 0)
526 return rc;
527
528 gpio->irq = rc;
529
Andrew Jeffery1736f752017-01-24 16:46:46 +1030530 set_irq_valid_mask(gpio);
531
Joel Stanley361b7912016-08-30 17:24:27 +0930532 rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
533 0, handle_bad_irq, IRQ_TYPE_NONE);
534 if (rc) {
535 dev_info(&pdev->dev, "Could not add irqchip\n");
536 return rc;
537 }
538
539 gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
540 gpio->irq, aspeed_gpio_irq_handler);
541
542 return 0;
543}
544
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030545static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
546 unsigned int offset, bool enable)
547{
548 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
549 const struct aspeed_gpio_bank *bank;
550 unsigned long flags;
551 u32 val;
552
553 bank = to_bank(offset);
554
555 spin_lock_irqsave(&gpio->lock, flags);
556 val = readl(gpio->base + bank->tolerance_regs);
557
558 if (enable)
559 val |= GPIO_BIT(offset);
560 else
561 val &= ~GPIO_BIT(offset);
562
563 writel(val, gpio->base + bank->tolerance_regs);
564 spin_unlock_irqrestore(&gpio->lock, flags);
565
566 return 0;
567}
568
Joel Stanley361b7912016-08-30 17:24:27 +0930569static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
570{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030571 if (!have_gpio(gpiochip_get_data(chip), offset))
572 return -ENODEV;
573
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200574 return pinctrl_gpio_request(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930575}
576
577static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
578{
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200579 pinctrl_gpio_free(chip->base + offset);
Joel Stanley361b7912016-08-30 17:24:27 +0930580}
581
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930582static inline void __iomem *bank_debounce_reg(struct aspeed_gpio *gpio,
583 const struct aspeed_gpio_bank *bank,
584 unsigned int reg)
585{
586 return gpio->base + bank->debounce_regs + reg;
587}
588
589static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
590 u32 *cycles)
591{
592 u64 rate;
593 u64 n;
594 u32 r;
595
596 rate = clk_get_rate(gpio->clk);
597 if (!rate)
598 return -ENOTSUPP;
599
600 n = rate * usecs;
601 r = do_div(n, 1000000);
602
603 if (n >= U32_MAX)
604 return -ERANGE;
605
606 /* At least as long as the requested time */
607 *cycles = n + (!!r);
608
609 return 0;
610}
611
612/* Call under gpio->lock */
613static int register_allocated_timer(struct aspeed_gpio *gpio,
614 unsigned int offset, unsigned int timer)
615{
616 if (WARN(gpio->offset_timer[offset] != 0,
617 "Offset %d already allocated timer %d\n",
618 offset, gpio->offset_timer[offset]))
619 return -EINVAL;
620
621 if (WARN(gpio->timer_users[timer] == UINT_MAX,
622 "Timer user count would overflow\n"))
623 return -EPERM;
624
625 gpio->offset_timer[offset] = timer;
626 gpio->timer_users[timer]++;
627
628 return 0;
629}
630
631/* Call under gpio->lock */
632static int unregister_allocated_timer(struct aspeed_gpio *gpio,
633 unsigned int offset)
634{
635 if (WARN(gpio->offset_timer[offset] == 0,
636 "No timer allocated to offset %d\n", offset))
637 return -EINVAL;
638
639 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
640 "No users recorded for timer %d\n",
641 gpio->offset_timer[offset]))
642 return -EINVAL;
643
644 gpio->timer_users[gpio->offset_timer[offset]]--;
645 gpio->offset_timer[offset] = 0;
646
647 return 0;
648}
649
650/* Call under gpio->lock */
651static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
652 unsigned int offset)
653{
654 return gpio->offset_timer[offset] > 0;
655}
656
657/* Call under gpio->lock */
658static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
659 unsigned int timer)
660{
661 const struct aspeed_gpio_bank *bank = to_bank(offset);
662 const u32 mask = GPIO_BIT(offset);
663 void __iomem *addr;
664 u32 val;
665
666 addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL1);
667 val = ioread32(addr);
668 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
669
670 addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL2);
671 val = ioread32(addr);
672 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
673}
674
675static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
676 unsigned long usecs)
677{
678 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
679 u32 requested_cycles;
680 unsigned long flags;
681 int rc;
682 int i;
683
Joel Stanleydf563c82017-05-02 15:38:24 +0930684 if (!gpio->clk)
685 return -EINVAL;
686
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930687 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
688 if (rc < 0) {
689 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
690 usecs, clk_get_rate(gpio->clk), rc);
691 return rc;
692 }
693
694 spin_lock_irqsave(&gpio->lock, flags);
695
696 if (timer_allocation_registered(gpio, offset)) {
697 rc = unregister_allocated_timer(gpio, offset);
698 if (rc < 0)
699 goto out;
700 }
701
702 /* Try to find a timer already configured for the debounce period */
703 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
704 u32 cycles;
705
706 cycles = ioread32(gpio->base + debounce_timers[i]);
707 if (requested_cycles == cycles)
708 break;
709 }
710
711 if (i == ARRAY_SIZE(debounce_timers)) {
712 int j;
713
714 /*
715 * As there are no timers configured for the requested debounce
716 * period, find an unused timer instead
717 */
718 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
719 if (gpio->timer_users[j] == 0)
720 break;
721 }
722
723 if (j == ARRAY_SIZE(gpio->timer_users)) {
724 dev_warn(chip->parent,
725 "Debounce timers exhausted, cannot debounce for period %luus\n",
726 usecs);
727
728 rc = -EPERM;
729
730 /*
731 * We already adjusted the accounting to remove @offset
732 * as a user of its previous timer, so also configure
733 * the hardware so @offset has timers disabled for
734 * consistency.
735 */
736 configure_timer(gpio, offset, 0);
737 goto out;
738 }
739
740 i = j;
741
742 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
743 }
744
745 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
746 rc = -EINVAL;
747 goto out;
748 }
749
750 register_allocated_timer(gpio, offset, i);
751 configure_timer(gpio, offset, i);
752
753out:
754 spin_unlock_irqrestore(&gpio->lock, flags);
755
756 return rc;
757}
758
759static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
760{
761 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
762 unsigned long flags;
763 int rc;
764
765 spin_lock_irqsave(&gpio->lock, flags);
766
767 rc = unregister_allocated_timer(gpio, offset);
768 if (!rc)
769 configure_timer(gpio, offset, 0);
770
771 spin_unlock_irqrestore(&gpio->lock, flags);
772
773 return rc;
774}
775
776static int set_debounce(struct gpio_chip *chip, unsigned int offset,
777 unsigned long usecs)
778{
779 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
780
781 if (!have_debounce(gpio, offset))
782 return -ENOTSUPP;
783
784 if (usecs)
785 return enable_debounce(chip, offset, usecs);
786
787 return disable_debounce(chip, offset);
788}
789
790static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
791 unsigned long config)
792{
793 unsigned long param = pinconf_to_config_param(config);
794 u32 arg = pinconf_to_config_argument(config);
795
796 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
797 return set_debounce(chip, offset, arg);
798 else if (param == PIN_CONFIG_BIAS_DISABLE ||
799 param == PIN_CONFIG_BIAS_PULL_DOWN ||
800 param == PIN_CONFIG_DRIVE_STRENGTH)
801 return pinctrl_gpio_set_config(offset, config);
Andrew Jefferyc3bafe02017-04-07 22:29:02 +0930802 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
803 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
804 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
805 return -ENOTSUPP;
Andrew Jeffery1b43d262017-11-30 14:25:25 +1030806 else if (param == PIN_CONFIG_PERSIST_STATE)
807 return aspeed_gpio_reset_tolerance(chip, offset, arg);
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930808
809 return -ENOTSUPP;
810}
811
Andrew Jeffery1736f752017-01-24 16:46:46 +1030812/*
813 * Any banks not specified in a struct aspeed_bank_props array are assumed to
814 * have the properties:
815 *
816 * { .input = 0xffffffff, .output = 0xffffffff }
817 */
818
819static const struct aspeed_bank_props ast2400_bank_props[] = {
820 /* input output */
821 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
822 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
823 { },
824};
825
826static const struct aspeed_gpio_config ast2400_config =
827 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
828 { .nr_gpios = 220, .props = ast2400_bank_props, };
829
830static const struct aspeed_bank_props ast2500_bank_props[] = {
831 /* input output */
832 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
833 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
834 { 7, 0x000000ff, 0x000000ff }, /* AC */
835 { },
836};
837
838static const struct aspeed_gpio_config ast2500_config =
839 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
840 { .nr_gpios = 232, .props = ast2500_bank_props, };
841
842static const struct of_device_id aspeed_gpio_of_table[] = {
843 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
844 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
845 {}
846};
847MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
848
Joel Stanley361b7912016-08-30 17:24:27 +0930849static int __init aspeed_gpio_probe(struct platform_device *pdev)
850{
Andrew Jeffery1736f752017-01-24 16:46:46 +1030851 const struct of_device_id *gpio_id;
Joel Stanley361b7912016-08-30 17:24:27 +0930852 struct aspeed_gpio *gpio;
853 struct resource *res;
854 int rc;
855
856 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
857 if (!gpio)
858 return -ENOMEM;
859
860 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joel Stanley361b7912016-08-30 17:24:27 +0930861 gpio->base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun7f8b9652016-09-15 01:30:32 +0000862 if (IS_ERR(gpio->base))
863 return PTR_ERR(gpio->base);
Joel Stanley361b7912016-08-30 17:24:27 +0930864
865 spin_lock_init(&gpio->lock);
866
Andrew Jeffery1736f752017-01-24 16:46:46 +1030867 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
868 if (!gpio_id)
869 return -EINVAL;
Joel Stanley361b7912016-08-30 17:24:27 +0930870
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930871 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
872 if (IS_ERR(gpio->clk)) {
873 dev_warn(&pdev->dev,
Andrew Jeffery754c0452017-08-08 15:37:36 +0930874 "Failed to get clock from devicetree, debouncing disabled\n");
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930875 gpio->clk = NULL;
876 }
877
Andrew Jeffery1736f752017-01-24 16:46:46 +1030878 gpio->config = gpio_id->data;
879
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930880 gpio->chip.parent = &pdev->dev;
Andrew Jeffery1736f752017-01-24 16:46:46 +1030881 gpio->chip.ngpio = gpio->config->nr_gpios;
Joel Stanley361b7912016-08-30 17:24:27 +0930882 gpio->chip.parent = &pdev->dev;
883 gpio->chip.direction_input = aspeed_gpio_dir_in;
884 gpio->chip.direction_output = aspeed_gpio_dir_out;
885 gpio->chip.get_direction = aspeed_gpio_get_direction;
886 gpio->chip.request = aspeed_gpio_request;
887 gpio->chip.free = aspeed_gpio_free;
888 gpio->chip.get = aspeed_gpio_get;
889 gpio->chip.set = aspeed_gpio_set;
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930890 gpio->chip.set_config = aspeed_gpio_set_config;
Joel Stanley361b7912016-08-30 17:24:27 +0930891 gpio->chip.label = dev_name(&pdev->dev);
892 gpio->chip.base = -1;
Thierry Redingdc7b0382017-11-07 19:15:52 +0100893 gpio->chip.irq.need_valid_mask = true;
Joel Stanley361b7912016-08-30 17:24:27 +0930894
895 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
896 if (rc < 0)
897 return rc;
898
Andrew Jeffery5ae4cb92017-04-07 22:29:01 +0930899 gpio->offset_timer =
900 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
901
Joel Stanley361b7912016-08-30 17:24:27 +0930902 return aspeed_gpio_setup_irqs(gpio, pdev);
903}
904
Joel Stanley361b7912016-08-30 17:24:27 +0930905static struct platform_driver aspeed_gpio_driver = {
906 .driver = {
907 .name = KBUILD_MODNAME,
908 .of_match_table = aspeed_gpio_of_table,
909 },
910};
911
912module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
913
914MODULE_DESCRIPTION("Aspeed GPIO Driver");
Linus Walleije50237c2016-09-13 13:43:34 +0200915MODULE_LICENSE("GPL");