Input: sprd_eic_keys: remove event log
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / gpio / gpio-eic.c
1 /*
2  * Copyright (C) 2012 Spreadtrum Communications Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/gpio.h>
19 #include <linux/io.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/bug.h>
23 #include <linux/of_device.h>
24 #include <linux/irqdomain.h>
25
26 #include <soc/sprd/sci.h>
27 #include <soc/sprd/sci_glb_regs.h>
28 #include <soc/sprd/gpio.h>
29 #include <soc/sprd/adi.h>
30
31 /* 16 GPIO share a group of registers */
32 #define GPIO_GROUP_NR           (16)
33 #define GPIO_GROUP_MASK         (0xFFFF)
34
35 #define GPIO_GROUP_OFFSET       (0x80)
36 #define ANA_GPIO_GROUP_OFFSET   (0x40)
37
38 /* registers definitions for GPIO controller */
39 #define REG_GPIO_DATA           (0x0000)
40 #define REG_GPIO_DMSK           (0x0004)
41 #define REG_GPIO_DIR            (0x0008)        /* only for gpio */
42 #define REG_GPIO_IS             (0x000c)        /* only for gpio */
43 #define REG_GPIO_IBE            (0x0010)        /* only for gpio */
44 #define REG_GPIO_IEV            (0x0014)
45 #define REG_GPIO_IE             (0x0018)
46 #define REG_GPIO_RIS            (0x001c)
47 #define REG_GPIO_MIS            (0x0020)
48 #define REG_GPIO_IC             (0x0024)
49 #define REG_GPIO_INEN           (0x0028)        /* only for gpio */
50
51 /* 8 EIC share a group of registers */
52 #define EIC_GROUP_NR            (8)
53 #define EIC_GROUP_MASK          (0xFF)
54
55 /* registers definitions for EIC controller */
56 #define REG_EIC_DATA            REG_GPIO_DATA
57 #define REG_EIC_DMSK            REG_GPIO_DMSK
58 #define REG_EIC_IEV             REG_GPIO_IEV
59 #define REG_EIC_IE              REG_GPIO_IE
60 #define REG_EIC_RIS             REG_GPIO_RIS
61 #define REG_EIC_MIS             REG_GPIO_MIS
62 #define REG_EIC_IC              REG_GPIO_IC
63 #define REG_EIC_TRIG            (0x0028)        /* only for eic */
64 #define REG_EIC_0CTRL           (0x0040)
65 #define REG_EIC_1CTRL           (0x0044)
66 #define REG_EIC_2CTRL           (0x0048)
67 #define REG_EIC_3CTRL           (0x004c)
68 #define REG_EIC_4CTRL           (0x0050)
69 #define REG_EIC_5CTRL           (0x0054)
70 #define REG_EIC_6CTRL           (0x0058)
71 #define REG_EIC_7CTRL           (0x005c)
72 #define REG_EIC_DUMMYCTRL       (0x0000)
73
74 /* bits definitions for register REG_EIC_DUMMYCTRL */
75 #define BIT_FORCE_CLK_DBNC      BIT(15)
76 #define BIT_EIC_DBNC_EN         BIT(14)
77 #define SHIFT_EIC_DBNC_CNT      (0)
78 #define MASK_EIC_DBNC_CNT       (0xFFF)
79 #define BITS_EIC_DBNC_CNT(_x_)  ((_x) & 0xFFF)
80
81 struct sci_gpio_chip {
82         struct gpio_chip chip;
83
84         unsigned long base_addr;
85         uint32_t group_offset;
86         struct irq_domain *irq_domain;
87         int is_adi_gpio;
88
89         uint32_t(*read_reg) (unsigned long addr);
90         void (*write_reg) (uint32_t value, unsigned long addr);
91         void (*set_bits) (uint32_t bits, unsigned long addr);
92         void (*clr_bits) (uint32_t bits, unsigned long addr);
93 };
94
95 #define to_sci_gpio(c)          container_of(c, struct sci_gpio_chip, chip)
96
97 /* D-Die regs ops */
98 static uint32_t d_read_reg(unsigned long addr)
99 {
100         return __raw_readl((const volatile void *)addr);
101 }
102
103 static void d_write_reg(uint32_t value, unsigned long addr)
104 {
105         __raw_writel(value, (volatile void *)addr);
106 }
107
108 static void d_set_bits(uint32_t bits, unsigned long addr)
109 {
110         __raw_writel(__raw_readl((const volatile void *)addr) | bits, (volatile void *)addr);
111 }
112
113 static void d_clr_bits(uint32_t bits, unsigned long addr)
114 {
115         __raw_writel(__raw_readl((const volatile void *)addr) & ~bits, (volatile void *)addr);
116 }
117
118 /* A-Die regs ops */
119 static uint32_t a_read_reg(unsigned long addr)
120 {
121         return sci_adi_read(addr);
122 }
123
124 static void a_write_reg(uint32_t value, unsigned long addr)
125 {
126         sci_adi_raw_write(addr, value);
127 }
128
129 static void a_set_bits(uint32_t bits, unsigned long addr)
130 {
131         sci_adi_set(addr, bits);
132 }
133
134 static void a_clr_bits(uint32_t bits, unsigned long addr)
135 {
136         sci_adi_clr(addr, bits);
137 }
138
139 static int sci_gpio_read(struct gpio_chip *chip, uint32_t offset, uint32_t reg)
140 {
141         struct sci_gpio_chip *sci_gpio = to_sci_gpio(chip);
142         int group = offset / GPIO_GROUP_NR;
143         int bitof = offset & (GPIO_GROUP_NR - 1);
144         unsigned long addr = sci_gpio->base_addr + sci_gpio->group_offset * group + reg;
145         int value = sci_gpio->read_reg(addr) & GPIO_GROUP_MASK;
146
147         return (value >> bitof) & 0x1;
148 }
149
150 static void sci_gpio_write(struct gpio_chip *chip, uint32_t offset,
151                            uint32_t reg, int value)
152 {
153         struct sci_gpio_chip *sci_gpio = to_sci_gpio(chip);
154         int group = offset / GPIO_GROUP_NR;
155         int bitof = offset & (GPIO_GROUP_NR - 1);
156         unsigned long addr = sci_gpio->base_addr + sci_gpio->group_offset * group + reg;
157
158         if (value) {
159                 sci_gpio->set_bits(1 << bitof, addr);
160         } else {
161                 sci_gpio->clr_bits(1 << bitof, addr);
162         }
163 }
164
165 /* GPIO/EIC libgpio interfaces */
166 static int sci_gpio_request(struct gpio_chip *chip, unsigned offset)
167 {
168         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
169         sci_gpio_write(chip, offset, REG_GPIO_DMSK, 1);
170         return 0;
171 }
172
173 static void sci_gpio_free(struct gpio_chip *chip, unsigned offset)
174 {
175         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
176         sci_gpio_write(chip, offset, REG_GPIO_DMSK, 0);
177 }
178
179 static int sci_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
180 {
181         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
182         sci_gpio_write(chip, offset, REG_GPIO_DIR, 0);
183         sci_gpio_write(chip, offset, REG_GPIO_INEN, 1);
184         return 0;
185 }
186
187 static int sci_eic_direction_input(struct gpio_chip *chip, unsigned offset)
188 {
189         /* do nothing */
190         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
191         return 0;
192 }
193
194 static int sci_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
195                                      int value)
196 {
197         pr_debug("%s %d+%d %d\n", __FUNCTION__, chip->base, offset, value);
198         sci_gpio_write(chip, offset, REG_GPIO_DIR, 1);
199         sci_gpio_write(chip, offset, REG_GPIO_INEN, 0);
200         sci_gpio_write(chip, offset, REG_GPIO_DATA, value);
201         return 0;
202 }
203
204 static int sci_gpio_get(struct gpio_chip *chip, unsigned offset)
205 {
206         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
207         return sci_gpio_read(chip, offset, REG_GPIO_DATA);
208 }
209
210 static void sci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
211 {
212         pr_debug("%s %d+%d %d\n", __FUNCTION__, chip->base, offset, value);
213         sci_gpio_write(chip, offset, REG_GPIO_DATA, value);
214 }
215
216 static int sci_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
217                                  unsigned debounce)
218 {
219         /* not supported */
220         pr_err("%s %d+%d\n", __FUNCTION__, chip->base, offset);
221         return -EINVAL;
222 }
223
224 static int sci_eic_set_debounce(struct gpio_chip *chip, unsigned offset,
225                                 unsigned debounce)
226 {
227         /* TODO */
228         pr_info("%s %d+%d\n", __FUNCTION__, chip->base, offset);
229         return 0;
230 }
231
232 #ifdef CONFIG_OF
233 static int sci_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
234 {
235         struct irq_domain *irq_domain;
236         struct sci_gpio_chip *sci_gpio = to_sci_gpio(chip);
237
238         irq_domain = sci_gpio->irq_domain;
239         return irq_find_mapping(irq_domain, offset);
240 }
241
242 static int sci_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
243 {
244         int base_irq;
245         struct irq_domain *irq_domain;
246         struct sci_gpio_chip *sci_gpio = to_sci_gpio(chip);
247
248         irq_domain = sci_gpio->irq_domain;
249         base_irq = irq_find_mapping(irq_domain, 0);
250         return irq - base_irq;
251 }
252 #else
253 static int sci_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
254 {
255         return chip->base + offset + GPIO_IRQ_START;
256 }
257
258 static int sci_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
259 {
260         return irq - GPIO_IRQ_START - chip->base;
261 }
262
263 #endif
264
265 static struct sci_gpio_chip d_sci_gpio = {
266         .chip.label = "sprd-d-gpio",
267         .chip.request = sci_gpio_request,
268         .chip.free = sci_gpio_free,
269         .chip.direction_input = sci_gpio_direction_input,
270         .chip.get = sci_gpio_get,
271         .chip.direction_output = sci_gpio_direction_output,
272         .chip.set = sci_gpio_set,
273         .chip.set_debounce = sci_gpio_set_debounce,
274         .chip.to_irq = sci_gpio_to_irq,
275
276         .group_offset = GPIO_GROUP_OFFSET,
277         .read_reg = d_read_reg,
278         .write_reg = d_write_reg,
279         .set_bits = d_set_bits,
280         .clr_bits = d_clr_bits,
281         .is_adi_gpio = 0,
282 };
283
284 static struct sci_gpio_chip a_sci_gpio = {
285         .chip.label = "sprd-a-gpio",
286         .chip.request = sci_gpio_request,
287         .chip.free = sci_gpio_free,
288         .chip.direction_input = sci_gpio_direction_input,
289         .chip.get = sci_gpio_get,
290         .chip.direction_output = sci_gpio_direction_output,
291         .chip.set = sci_gpio_set,
292         .chip.set_debounce = sci_gpio_set_debounce,
293         .chip.to_irq = sci_gpio_to_irq,
294
295         .group_offset = ANA_GPIO_GROUP_OFFSET,
296         .read_reg = a_read_reg,
297         .write_reg = a_write_reg,
298         .set_bits = a_set_bits,
299         .clr_bits = a_clr_bits,
300         .is_adi_gpio = 1,
301 };
302
303 /*
304  * EIC has the same register layout with GPIO when it's used as GPI.
305  * So most implementation of GPIO can be shared by EIC.
306  */
307 static struct sci_gpio_chip d_sci_eic = {
308         .chip.label = "sprd-d-eic",
309         .chip.request = sci_gpio_request,
310         .chip.free = sci_gpio_free,
311         .chip.direction_input = sci_eic_direction_input,
312         .chip.get = sci_gpio_get,
313         .chip.direction_output = NULL,
314         .chip.set = NULL,
315         .chip.set_debounce = sci_eic_set_debounce,
316         .chip.to_irq = sci_gpio_to_irq,
317
318         .group_offset = GPIO_GROUP_OFFSET,
319         .read_reg = d_read_reg,
320         .write_reg = d_write_reg,
321         .set_bits = d_set_bits,
322         .clr_bits = d_clr_bits,
323         .is_adi_gpio = 0,
324 };
325
326 static struct sci_gpio_chip a_sci_eic = {
327         .chip.label = "sprd-a-eic",
328         .chip.request = sci_gpio_request,
329         .chip.free = sci_gpio_free,
330         .chip.direction_input = sci_eic_direction_input,
331         .chip.get = sci_gpio_get,
332         .chip.direction_output = NULL,
333         .chip.set = NULL,
334         .chip.set_debounce = sci_eic_set_debounce,
335         .chip.to_irq = sci_gpio_to_irq,
336
337         .group_offset = ANA_GPIO_GROUP_OFFSET,
338         .read_reg = a_read_reg,
339         .write_reg = a_write_reg,
340         .set_bits = a_set_bits,
341         .clr_bits = a_clr_bits,
342         .is_adi_gpio = 1,
343 };
344
345 /* GPIO/EIC irq interfaces */
346 static void sci_gpio_irq_ack(struct irq_data *data)
347 {
348         struct gpio_chip *chip = (struct gpio_chip *)data->chip_data;
349         int offset = sci_irq_to_gpio(chip, data->irq);
350         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
351         sci_gpio_write(chip, offset, REG_GPIO_IC, 1);
352 }
353
354 static void sci_gpio_irq_mask(struct irq_data *data)
355 {
356         struct gpio_chip *chip = (struct gpio_chip *)data->chip_data;
357         int offset = sci_irq_to_gpio(chip, data->irq);
358         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
359         sci_gpio_write(chip, offset, REG_GPIO_IE, 0);
360 }
361
362 static void sci_gpio_irq_unmask(struct irq_data *data)
363 {
364         struct gpio_chip *chip = (struct gpio_chip *)data->chip_data;
365         int offset = sci_irq_to_gpio(chip, data->irq);
366         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
367         sci_gpio_write(chip, offset, REG_GPIO_IE, 1);
368 }
369
370 static void sci_eic_irq_unmask(struct irq_data *data)
371 {
372         struct gpio_chip *chip = (struct gpio_chip *)data->chip_data;
373         int offset = sci_irq_to_gpio(chip, data->irq);
374         pr_debug("%s %d+%d\n", __FUNCTION__, chip->base, offset);
375         sci_gpio_write(chip, offset, REG_EIC_IE, 1);
376
377         /* TODO: the interval of two EIC trigger needs be longer than 2ms */
378         sci_gpio_write(chip, offset, REG_EIC_TRIG, 1);
379 }
380
381 static int sci_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type)
382 {
383         struct gpio_chip *chip = (struct gpio_chip *)data->chip_data;
384         int offset = sci_irq_to_gpio(chip, data->irq);
385         pr_debug("%s %d+%d %d\n", __FUNCTION__, chip->base, offset, flow_type);
386         switch (flow_type) {
387         case IRQ_TYPE_EDGE_RISING:
388                 sci_gpio_write(chip, offset, REG_GPIO_IS, 0);
389                 sci_gpio_write(chip, offset, REG_GPIO_IBE, 0);
390                 sci_gpio_write(chip, offset, REG_GPIO_IEV, 1);
391                 __irq_set_handler_locked(data->irq, handle_edge_irq);
392                 break;
393         case IRQ_TYPE_EDGE_FALLING:
394                 sci_gpio_write(chip, offset, REG_GPIO_IS, 0);
395                 sci_gpio_write(chip, offset, REG_GPIO_IBE, 0);
396                 sci_gpio_write(chip, offset, REG_GPIO_IEV, 0);
397                 __irq_set_handler_locked(data->irq, handle_edge_irq);
398                 break;
399         case IRQ_TYPE_EDGE_BOTH:
400                 sci_gpio_write(chip, offset, REG_GPIO_IS, 0);
401                 sci_gpio_write(chip, offset, REG_GPIO_IBE, 1);
402                 __irq_set_handler_locked(data->irq, handle_edge_irq);
403                 break;
404         case IRQ_TYPE_LEVEL_HIGH:
405                 sci_gpio_write(chip, offset, REG_GPIO_IS, 1);
406                 sci_gpio_write(chip, offset, REG_GPIO_IBE, 0);
407                 sci_gpio_write(chip, offset, REG_GPIO_IEV, 1);
408                 __irq_set_handler_locked(data->irq, handle_level_irq);
409                 break;
410         case IRQ_TYPE_LEVEL_LOW:
411                 sci_gpio_write(chip, offset, REG_GPIO_IS, 1);
412                 sci_gpio_write(chip, offset, REG_GPIO_IBE, 0);
413                 sci_gpio_write(chip, offset, REG_GPIO_IEV, 0);
414                 __irq_set_handler_locked(data->irq, handle_level_irq);
415                 break;
416         default:
417                 return -EINVAL;
418         }
419
420         return 0;
421 }
422
423 static int sci_eic_irq_set_type(struct irq_data *data, unsigned int flow_type)
424 {
425         struct gpio_chip *chip = (struct gpio_chip *)data->chip_data;
426         int offset = sci_irq_to_gpio(chip, data->irq);
427         pr_debug("%s %d+%d %d\n", __FUNCTION__, chip->base, offset, flow_type);
428         switch (flow_type) {
429         case IRQ_TYPE_LEVEL_HIGH:
430                 sci_gpio_write(chip, offset, REG_EIC_IEV, 1);
431                 __irq_set_handler_locked(data->irq, handle_level_irq);
432                 break;
433         case IRQ_TYPE_LEVEL_LOW:
434                 sci_gpio_write(chip, offset, REG_EIC_IEV, 0);
435                 __irq_set_handler_locked(data->irq, handle_level_irq);
436                 break;
437         default:
438                 return -EINVAL;
439         }
440
441         return 0;
442 }
443
444 static int sci_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
445 {
446         return on ? 0 : -EPERM;
447 }
448
449 static struct irq_chip d_gpio_irq_chip = {
450         .name = "irq-d-gpio",
451         .irq_disable = sci_gpio_irq_mask,
452         .irq_ack = sci_gpio_irq_ack,
453         .irq_mask = sci_gpio_irq_mask,
454         .irq_unmask = sci_gpio_irq_unmask,
455         .irq_set_type = sci_gpio_irq_set_type,
456         .irq_set_wake = sci_gpio_irq_set_wake,
457 };
458
459 static struct irq_chip a_gpio_irq_chip = {
460         .name = "irq-a-gpio",
461         .irq_disable = sci_gpio_irq_mask,
462         .irq_ack = sci_gpio_irq_ack,
463         .irq_mask = sci_gpio_irq_mask,
464         .irq_unmask = sci_gpio_irq_unmask,
465         .irq_set_type = sci_gpio_irq_set_type,
466         .irq_set_wake = sci_gpio_irq_set_wake,
467 };
468
469 /*
470  * EIC has the same register layout with GPIO when it's used as GPI.
471  * So most implementation of GPIO can be shared by EIC.
472  */
473 static struct irq_chip d_eic_irq_chip = {
474         .name = "irq-d-eic",
475         .irq_disable = sci_gpio_irq_mask,
476         .irq_ack = sci_gpio_irq_ack,
477         .irq_mask = sci_gpio_irq_mask,
478         .irq_unmask = sci_eic_irq_unmask,
479         .irq_set_type = sci_eic_irq_set_type,
480 };
481
482 static struct irq_chip a_eic_irq_chip = {
483         .name = "irq-a-eic",
484         .irq_disable = sci_gpio_irq_mask,
485         .irq_ack = sci_gpio_irq_ack,
486         .irq_mask = sci_gpio_irq_mask,
487         .irq_unmask = sci_eic_irq_unmask,
488         .irq_set_type = sci_eic_irq_set_type,
489 };
490
491 static void gpio_eic_handler(int irq, struct gpio_chip *chip)
492 {
493         struct sci_gpio_chip *sci_gpio = to_sci_gpio(chip);
494         int group, n, value, count = 0;
495         unsigned long addr;
496
497         pr_debug("%s %d+%d %d\n", __FUNCTION__, chip->base, chip->ngpio, irq);
498         for (group = 0; group * GPIO_GROUP_NR < chip->ngpio; group++) {
499                 addr =
500                     sci_gpio->base_addr + sci_gpio->group_offset * group +
501                     REG_GPIO_MIS;
502                 value = sci_gpio->read_reg(addr) & GPIO_GROUP_MASK;
503
504                 while (value) {
505                         n = __ffs(value);
506                         value &= ~(1 << n);
507                         n = chip->to_irq(chip, group * GPIO_GROUP_NR + n);
508                         pr_debug("%s generic_handle_n %d\n", __FUNCTION__, n);
509                         count++;
510                         generic_handle_irq(n);
511                 }
512         }
513
514 }
515
516 static irqreturn_t gpio_muxed_handler(int irq, void *dev_id)
517 {
518         struct gpio_chip *chip = dev_id;
519         gpio_eic_handler(irq, chip);
520         return IRQ_HANDLED;
521 }
522
523 /* gpio/eic cascaded irq handler */
524 static void gpio_muxed_flow_handler(unsigned int irq, struct irq_desc *desc)
525 {
526         struct gpio_chip *chip = irq_get_handler_data(irq);
527         gpio_eic_handler(irq, chip);
528 }
529
530 static struct irqaction __d_gpio_irq = {
531         .name = "gpio",
532         .flags = IRQF_DISABLED | IRQF_NO_SUSPEND,
533         .handler = gpio_muxed_handler,
534         .dev_id = &d_sci_gpio.chip,
535 };
536
537 static struct irqaction __d_eic_irq = {
538         .name = "eic",
539         .flags = IRQF_DISABLED | IRQF_NO_SUSPEND,
540         .handler = gpio_muxed_handler,
541         .dev_id = &d_sci_eic.chip,
542 };
543
544 #if (NR_GPIO_IRQS < ARCH_NR_GPIOS)
545 #error "NR_GPIO_IRQS is not match with the sum of builtin/SoC GPIOs and EICs"
546 #endif
547
548 #ifdef CONFIG_OF
549 static void gpio_irq_init(int irq, struct gpio_chip *gpiochip,
550                           struct irq_chip *irqchip)
551 {
552         int n, i;
553         struct sci_gpio_chip *sci_gpio = to_sci_gpio(gpiochip);
554         struct irq_domain *irq_domain = sci_gpio->irq_domain;
555
556         /* setup the cascade irq handlers */
557         if (sci_gpio->is_adi_gpio) {
558                 /*TODO*/ irq_set_chained_handler(irq, gpio_muxed_flow_handler);
559                 irq_set_handler_data(irq, gpiochip);
560         }
561
562         for (i = 0; i < gpiochip->ngpio; i++) {
563                 n = irq_create_mapping(irq_domain, i);
564                 irq_set_chip_and_handler(n, irqchip, handle_level_irq);
565                 irq_set_chip_data(n, gpiochip);
566                 set_irq_flags(n, IRQF_VALID);
567         }
568 }
569 #else
570 static void gpio_irq_init(int irq, struct gpio_chip *gpiochip,
571                           struct irq_chip *irqchip)
572 {
573         int n = gpiochip->to_irq(gpiochip, 0);
574         int irqend = n + gpiochip->ngpio;
575
576         /* setup the cascade irq handlers */
577         if (irq >= NR_SCI_PHY_IRQS) {
578                 irq_set_chained_handler(irq, gpio_muxed_flow_handler);
579                 irq_set_handler_data(irq, gpiochip);
580         }
581
582         for (; n < irqend; n++) {
583                 irq_set_chip_and_handler(n, irqchip, handle_level_irq);
584                 irq_set_chip_data(n, gpiochip);
585                 set_irq_flags(n, IRQF_VALID);
586         }
587 }
588
589 #endif
590
591 #ifdef CONFIG_OF
592 struct sprd_gpio_match_data {
593         struct sci_gpio_chip *sci_gpio_chip;
594         struct irq_chip *irq_chip;
595         struct irqaction *irqaction;
596 };
597 static struct sprd_gpio_match_data d_eic_match = {
598         .sci_gpio_chip = &d_sci_eic,
599         .irq_chip = &d_eic_irq_chip,
600         .irqaction = &__d_eic_irq,
601 };
602
603 static struct sprd_gpio_match_data d_gpio_match = {
604         .sci_gpio_chip = &d_sci_gpio,
605         .irq_chip = &d_gpio_irq_chip,
606         .irqaction = &__d_gpio_irq,
607 };
608
609 static struct sprd_gpio_match_data a_eic_match = {
610         .sci_gpio_chip = &a_sci_eic,
611         .irq_chip = &a_eic_irq_chip,
612         .irqaction = NULL,
613 };
614
615 static struct sprd_gpio_match_data a_gpio_match = {
616         .sci_gpio_chip = &a_sci_gpio,
617         .irq_chip = &a_gpio_irq_chip,
618         .irqaction = NULL,
619 };
620
621 static struct of_device_id eic_gpio_match_table[] = {
622         {.compatible = "sprd,d-eic-gpio",.data = &d_eic_match},
623         {.compatible = "sprd,d-gpio-gpio",.data = &d_gpio_match},
624         {.compatible = "sprd,a-eic-gpio",.data = &a_eic_match},
625         {.compatible = "sprd,a-gpio-gpio",.data = &a_gpio_match},
626         {},
627 };
628
629 static int eic_gpio_probe(struct platform_device *pdev)
630 {
631         struct eic_gpio_resource *r = pdev->dev.platform_data;
632         const struct of_device_id *match;
633         struct sprd_gpio_match_data *match_data;
634         struct device_node *np = pdev->dev.of_node;
635         struct sci_gpio_chip *sgc;
636         struct irq_chip *ic;
637         struct irqaction *ia;
638         static bool init_done = false;
639         struct resource *res;
640         struct irq_domain *irq_domain;
641         int irq;
642
643         if (!np && !r)
644                 BUG();
645
646         if (!init_done) {
647                 /* enable EIC */
648 #if defined(CONFIG_ARCH_SC8825)
649         sci_glb_set(REG_GLB_GEN0, BIT_EIC_EB);
650         sci_glb_set(REG_GLB_GEN0, BIT_GPIO_EB);
651         sci_glb_set(REG_GLB_GEN0, BIT_RTC_EIC_EB);
652         sci_adi_set(ANA_REG_GLB_ANA_APB_CLK_EN,
653                     BIT_ANA_EIC_EB | BIT_ANA_GPIO_EB | BIT_ANA_RTC_EIC_EB);
654 #elif (defined(CONFIG_ARCH_SCX15)||defined(CONFIG_ADIE_SC2723S)||defined(CONFIG_ADIE_SC2723))
655         sci_glb_set(REG_AON_APB_APB_EB0, BIT_GPIO_EB | BIT_EIC_EB);
656         sci_glb_set(REG_AON_APB_APB_RTC_EB, BIT_EIC_RTC_EB);
657         sci_adi_set(ANA_REG_GLB_ARM_MODULE_EN, BIT_ANA_EIC_EN);
658         sci_adi_set(ANA_REG_GLB_RTC_CLK_EN, BIT_RTC_EIC_EN);
659 #elif defined(CONFIG_ARCH_SCX35)
660                 sci_glb_set(REG_AON_APB_APB_EB0, BIT_GPIO_EB | BIT_EIC_EB);
661                 sci_glb_set(REG_AON_APB_APB_RTC_EB, BIT_EIC_RTC_EB);
662                 sci_adi_set(ANA_REG_GLB_ARM_MODULE_EN,
663                             BIT_ANA_EIC_EN | BIT_ANA_GPIO_EN);
664                 sci_adi_set(ANA_REG_GLB_RTC_CLK_EN, BIT_RTC_EIC_EN);
665 #endif
666                 init_done = true;
667         }
668
669         match = of_match_device(eic_gpio_match_table, &pdev->dev);
670         match_data = (struct sprd_gpio_match_data *)(match->data);
671         sgc = match_data->sci_gpio_chip;
672         ic = match_data->irq_chip;
673         ia = match_data->irqaction;
674
675         if(match_data==&a_eic_match)
676                 sgc->base_addr = ANA_EIC_BASE;
677         else
678                 {
679         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
680         if (!res) {
681                 dev_err(&pdev->dev, "No reg of property specified\n");
682                 return -ENODEV;
683         }
684
685         sgc->base_addr = (unsigned long)ioremap_nocache(res->start,
686                         res->end - res->start);
687         if (!sgc->base_addr)
688                 panic("ioremap failed!\n");
689                 }
690
691         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
692         if (!res)
693                 irq = -1;
694         else
695                 irq = res->start;
696
697         if (of_property_read_u32(np, "ngpios", (u32 *)(&sgc->chip.ngpio))) {
698                 dev_err(&pdev->dev, "No ngpios of property specified\n");
699                 release_mem_region(res->start, resource_size(res));
700                 return -ENODEV;
701         }
702         if (of_property_read_u32(np, "gpiobase", (u32 *)(&sgc->chip.base))) {
703                 dev_err(&pdev->dev, "No gpiobase of property specified\n");
704                 sgc->chip.base = -1;
705         }
706
707         dev_info(&pdev->dev, "base_addr %lx, gpio base %d, ngpio %d, irq %d",
708                 sgc->base_addr, sgc->chip.base, sgc->chip.ngpio, irq);
709
710 #ifdef CONFIG_OF
711         sgc->chip.of_node = np;
712 #endif
713         gpiochip_add(&sgc->chip);
714
715         irq_domain = irq_domain_add_linear(np, sgc->chip.ngpio,
716                                            &irq_domain_simple_ops, NULL);
717         if (!irq_domain) {
718                 dev_err(&pdev->dev, "failed to add irq domain\n");
719                 gpiochip_remove(&sgc->chip);
720                 release_mem_region(res->start, resource_size(res));
721                 return -ENODEV;
722         }
723         sgc->irq_domain = irq_domain;
724
725         if (-1 != irq) {
726                 gpio_irq_init(irq, &sgc->chip, ic);
727                 if (ia != NULL)
728                         setup_irq(irq, ia);
729         }
730
731         return 0;
732 }
733
734 static int eic_gpio_remove(struct platform_device *pdev)
735 {
736         int ret = 0;
737         const struct of_device_id *match;
738         struct sprd_gpio_match_data *match_data;
739         struct sci_gpio_chip *sgc;
740
741         match = of_match_device(eic_gpio_match_table, &pdev->dev);
742         match_data = (struct sprd_gpio_match_data *)(match->data);
743         sgc = match_data->sci_gpio_chip;
744         ret = gpiochip_remove(&sgc->chip);
745
746         return ret;
747 }
748 #else
749 static int eic_gpio_probe(struct platform_device *pdev)
750 {
751         struct eic_gpio_resource *r = pdev->dev.platform_data;
752         struct irq_domain *irq_domain;
753         if (!r)
754                 BUG();
755
756         /* enable EIC */
757 #if defined(CONFIG_ARCH_SC8825)
758         sci_glb_set(REG_GLB_GEN0, BIT_EIC_EB);
759         sci_glb_set(REG_GLB_GEN0, BIT_GPIO_EB);
760         sci_glb_set(REG_GLB_GEN0, BIT_RTC_EIC_EB);
761         sci_adi_set(ANA_REG_GLB_ANA_APB_CLK_EN,
762                     BIT_ANA_EIC_EB | BIT_ANA_GPIO_EB | BIT_ANA_RTC_EIC_EB);
763 #elif (defined(CONFIG_ARCH_SCX15)||defined(CONFIG_ADIE_SC2723S)||defined(CONFIG_ADIE_SC2723))
764         sci_glb_set(REG_AON_APB_APB_EB0, BIT_GPIO_EB | BIT_EIC_EB);
765         sci_glb_set(REG_AON_APB_APB_RTC_EB, BIT_EIC_RTC_EB);
766         sci_adi_set(ANA_REG_GLB_ARM_MODULE_EN, BIT_ANA_EIC_EN);
767         sci_adi_set(ANA_REG_GLB_RTC_CLK_EN, BIT_RTC_EIC_EN);
768 #elif defined(CONFIG_ARCH_SCX35)
769         sci_glb_set(REG_AON_APB_APB_EB0, BIT_GPIO_EB | BIT_EIC_EB);
770         sci_glb_set(REG_AON_APB_APB_RTC_EB, BIT_EIC_RTC_EB);
771         sci_adi_set(ANA_REG_GLB_ARM_MODULE_EN,
772                     BIT_ANA_EIC_EN | BIT_ANA_GPIO_EN);
773         sci_adi_set(ANA_REG_GLB_RTC_CLK_EN, BIT_RTC_EIC_EN);
774 #endif
775
776         d_sci_gpio.base_addr = r[ENUM_ID_D_GPIO].base_addr;
777         d_sci_gpio.chip.base = r[ENUM_ID_D_GPIO].chip_base;
778         d_sci_gpio.chip.ngpio = r[ENUM_ID_D_GPIO].chip_ngpio;
779
780         d_sci_eic.base_addr = r[ENUM_ID_D_EIC].base_addr;
781         d_sci_eic.chip.base = r[ENUM_ID_D_EIC].chip_base;
782         d_sci_eic.chip.ngpio = r[ENUM_ID_D_EIC].chip_ngpio;
783
784         a_sci_gpio.base_addr = r[ENUM_ID_A_GPIO].base_addr;
785         a_sci_gpio.chip.base = r[ENUM_ID_A_GPIO].chip_base;
786         a_sci_gpio.chip.ngpio = r[ENUM_ID_A_GPIO].chip_ngpio;
787
788         a_sci_eic.base_addr = r[ENUM_ID_A_EIC].base_addr;
789         a_sci_eic.chip.base = r[ENUM_ID_A_EIC].chip_base;
790         a_sci_eic.chip.ngpio = r[ENUM_ID_A_EIC].chip_ngpio;
791
792         if (d_sci_eic.chip.ngpio > 0)
793                 gpiochip_add(&d_sci_eic.chip);
794
795         if (d_sci_gpio.chip.ngpio > 0)
796                 gpiochip_add(&d_sci_gpio.chip);
797
798         if (a_sci_eic.chip.ngpio > 0)
799                 gpiochip_add(&a_sci_eic.chip);
800
801         if (a_sci_gpio.chip.ngpio > 0)
802                 gpiochip_add(&a_sci_gpio.chip);
803
804         if (-1 != r[ENUM_ID_D_GPIO].irq) {
805                 gpio_irq_init(r[ENUM_ID_D_GPIO].irq, &d_sci_gpio.chip,
806                               &d_gpio_irq_chip);
807                 setup_irq(r[ENUM_ID_D_GPIO].irq, &__d_gpio_irq);
808         }
809
810         if (-1 != r[ENUM_ID_D_EIC].irq) {
811                 gpio_irq_init(r[ENUM_ID_D_EIC].irq, &d_sci_eic.chip,
812                               &d_eic_irq_chip);
813                 setup_irq(r[ENUM_ID_D_EIC].irq, &__d_eic_irq);
814         }
815
816         if (-1 != r[ENUM_ID_A_GPIO].irq)
817                 gpio_irq_init(r[ENUM_ID_A_GPIO].irq, &a_sci_gpio.chip,
818                               &a_gpio_irq_chip);
819
820         if (-1 != r[ENUM_ID_A_EIC].irq)
821                 gpio_irq_init(r[ENUM_ID_A_EIC].irq, &a_sci_eic.chip,
822                               &a_eic_irq_chip);
823
824         return 0;
825 }
826
827 static int eic_gpio_remove(struct platform_device *pdev)
828 {
829         int ret = 0;
830
831         if (d_sci_eic.chip.ngpio > 0)
832                 ret += gpiochip_remove(&d_sci_eic.chip);
833         if (d_sci_gpio.chip.ngpio > 0)
834                 ret += gpiochip_remove(&d_sci_gpio.chip);
835         if (a_sci_eic.chip.ngpio > 0)
836                 ret += gpiochip_remove(&a_sci_eic.chip);
837         if (a_sci_gpio.chip.ngpio > 0)
838                 ret += gpiochip_remove(&a_sci_gpio.chip);
839
840         return ret;
841 }
842 #endif
843
844 static struct platform_driver eic_gpio_driver = {
845         .driver.name = "eic-gpio",
846         .driver.owner = THIS_MODULE,
847         .driver.of_match_table = of_match_ptr(eic_gpio_match_table),
848         .probe = eic_gpio_probe,
849         .remove = eic_gpio_remove,
850 };
851
852 static int __init eic_gpio_init(void)
853 {
854         return platform_driver_register(&eic_gpio_driver);
855 }
856
857 postcore_initcall(eic_gpio_init);
858
859 static void __exit eic_gpio_exit(void)
860 {
861         return platform_driver_unregister(&eic_gpio_driver);
862 }
863
864 module_exit(eic_gpio_exit);