]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pinctrl/pinctrl-baytrail.c
Merge remote-tracking branch 'pinctrl/for-next'
[karo-tx-linux.git] / drivers / pinctrl / pinctrl-baytrail.c
1 /*
2  * Pinctrl GPIO driver for Intel Baytrail
3  * Copyright (c) 2012-2013, Intel Corporation.
4  *
5  * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/bitops.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/gpio.h>
30 #include <linux/irqdomain.h>
31 #include <linux/acpi.h>
32 #include <linux/acpi_gpio.h>
33 #include <linux/platform_device.h>
34 #include <linux/seq_file.h>
35 #include <linux/io.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/pinctrl/pinctrl.h>
38
39 /* memory mapped register offsets */
40 #define BYT_CONF0_REG           0x000
41 #define BYT_CONF1_REG           0x004
42 #define BYT_VAL_REG             0x008
43 #define BYT_DFT_REG             0x00c
44 #define BYT_INT_STAT_REG        0x800
45
46 /* BYT_CONF0_REG register bits */
47 #define BYT_TRIG_NEG            BIT(26)
48 #define BYT_TRIG_POS            BIT(25)
49 #define BYT_TRIG_LVL            BIT(24)
50 #define BYT_PIN_MUX             0x07
51
52 /* BYT_VAL_REG register bits */
53 #define BYT_INPUT_EN            BIT(2)  /* 0: input enabled (active low)*/
54 #define BYT_OUTPUT_EN           BIT(1)  /* 0: output enabled (active low)*/
55 #define BYT_LEVEL               BIT(0)
56
57 #define BYT_DIR_MASK            (BIT(1) | BIT(2))
58 #define BYT_TRIG_MASK           (BIT(26) | BIT(25) | BIT(24))
59
60 #define BYT_NGPIO_SCORE         102
61 #define BYT_NGPIO_NCORE         28
62 #define BYT_NGPIO_SUS           44
63
64 /*
65  * Baytrail gpio controller consist of three separate sub-controllers called
66  * SCORE, NCORE and SUS. The sub-controllers are identified by their acpi UID.
67  *
68  * GPIO numbering is _not_ ordered meaning that gpio # 0 in ACPI namespace does
69  * _not_ correspond to the first gpio register at controller's gpio base.
70  * There is no logic or pattern in mapping gpio numbers to registers (pads) so
71  * each sub-controller needs to have its own mapping table
72  */
73
74 /* score_pins[gpio_nr] = pad_nr */
75
76 static unsigned const score_pins[BYT_NGPIO_SCORE] = {
77         85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
78         36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
79         54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
80         52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
81         95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
82         86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
83         80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
84         2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
85         31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
86         24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
87         97, 100,
88 };
89
90 static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
91         19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
92         14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
93         3, 6, 10, 13, 2, 5, 9, 7,
94 };
95
96 static unsigned const sus_pins[BYT_NGPIO_SUS] = {
97         29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
98         18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
99         0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
100         26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
101         52, 53, 59, 40,
102 };
103
104 static struct pinctrl_gpio_range byt_ranges[] = {
105         {
106                 .name = "1", /* match with acpi _UID in probe */
107                 .npins = BYT_NGPIO_SCORE,
108                 .pins = score_pins,
109         },
110         {
111                 .name = "2",
112                 .npins = BYT_NGPIO_NCORE,
113                 .pins = ncore_pins,
114         },
115         {
116                 .name = "3",
117                 .npins = BYT_NGPIO_SUS,
118                 .pins = sus_pins,
119         },
120         {
121         },
122 };
123
124 struct byt_gpio {
125         struct gpio_chip                chip;
126         struct irq_domain               *domain;
127         struct platform_device          *pdev;
128         spinlock_t                      lock;
129         void __iomem                    *reg_base;
130         struct pinctrl_gpio_range       *range;
131 };
132
133 #define to_byt_gpio(c)  container_of(c, struct byt_gpio, chip)
134
135 static void __iomem *byt_gpio_reg(struct gpio_chip *chip, unsigned offset,
136                                  int reg)
137 {
138         struct byt_gpio *vg = to_byt_gpio(chip);
139         u32 reg_offset;
140
141         if (reg == BYT_INT_STAT_REG)
142                 reg_offset = (offset / 32) * 4;
143         else
144                 reg_offset = vg->range->pins[offset] * 16;
145
146         return vg->reg_base + reg_offset + reg;
147 }
148
149 static int byt_gpio_request(struct gpio_chip *chip, unsigned offset)
150 {
151         struct byt_gpio *vg = to_byt_gpio(chip);
152
153         pm_runtime_get(&vg->pdev->dev);
154
155         return 0;
156 }
157
158 static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
159 {
160         struct byt_gpio *vg = to_byt_gpio(chip);
161         void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
162         u32 value;
163
164         /* clear interrupt triggering */
165         value = readl(reg);
166         value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
167         writel(value, reg);
168
169         pm_runtime_put(&vg->pdev->dev);
170 }
171
172 static int byt_irq_type(struct irq_data *d, unsigned type)
173 {
174         struct byt_gpio *vg = irq_data_get_irq_chip_data(d);
175         u32 offset = irqd_to_hwirq(d);
176         u32 value;
177         unsigned long flags;
178         void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
179
180         if (offset >= vg->chip.ngpio)
181                 return -EINVAL;
182
183         spin_lock_irqsave(&vg->lock, flags);
184         value = readl(reg);
185
186         /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
187          * are used to indicate high and low level triggering
188          */
189         value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
190
191         switch (type) {
192         case IRQ_TYPE_LEVEL_HIGH:
193                 value |= BYT_TRIG_LVL;
194         case IRQ_TYPE_EDGE_RISING:
195                 value |= BYT_TRIG_POS;
196                 break;
197         case IRQ_TYPE_LEVEL_LOW:
198                 value |= BYT_TRIG_LVL;
199         case IRQ_TYPE_EDGE_FALLING:
200                 value |= BYT_TRIG_NEG;
201                 break;
202         case IRQ_TYPE_EDGE_BOTH:
203                 value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
204                 break;
205         }
206         writel(value, reg);
207
208         spin_unlock_irqrestore(&vg->lock, flags);
209
210         return 0;
211 }
212
213 static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
214 {
215         void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
216         return readl(reg) & BYT_LEVEL;
217 }
218
219 static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
220 {
221         struct byt_gpio *vg = to_byt_gpio(chip);
222         void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
223         unsigned long flags;
224         u32 old_val;
225
226         spin_lock_irqsave(&vg->lock, flags);
227
228         old_val = readl(reg);
229
230         if (value)
231                 writel(old_val | BYT_LEVEL, reg);
232         else
233                 writel(old_val & ~BYT_LEVEL, reg);
234
235         spin_unlock_irqrestore(&vg->lock, flags);
236 }
237
238 static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
239 {
240         struct byt_gpio *vg = to_byt_gpio(chip);
241         void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
242         unsigned long flags;
243         u32 value;
244
245         spin_lock_irqsave(&vg->lock, flags);
246
247         value = readl(reg) | BYT_DIR_MASK;
248         value &= ~BYT_INPUT_EN;         /* active low */
249         writel(value, reg);
250
251         spin_unlock_irqrestore(&vg->lock, flags);
252
253         return 0;
254 }
255
256 static int byt_gpio_direction_output(struct gpio_chip *chip,
257                                      unsigned gpio, int value)
258 {
259         struct byt_gpio *vg = to_byt_gpio(chip);
260         void __iomem *reg = byt_gpio_reg(chip, gpio, BYT_VAL_REG);
261         unsigned long flags;
262         u32 reg_val;
263
264         spin_lock_irqsave(&vg->lock, flags);
265
266         reg_val = readl(reg) | BYT_DIR_MASK;
267         reg_val &= ~BYT_OUTPUT_EN;
268
269         if (value)
270                 writel(reg_val | BYT_LEVEL, reg);
271         else
272                 writel(reg_val & ~BYT_LEVEL, reg);
273
274         spin_unlock_irqrestore(&vg->lock, flags);
275
276         return 0;
277 }
278
279 static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
280 {
281         struct byt_gpio *vg = to_byt_gpio(chip);
282         int i;
283         unsigned long flags;
284         u32 conf0, val, offs;
285
286         spin_lock_irqsave(&vg->lock, flags);
287
288         for (i = 0; i < vg->chip.ngpio; i++) {
289                 const char *label;
290                 offs = vg->range->pins[i] * 16;
291                 conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
292                 val = readl(vg->reg_base + offs + BYT_VAL_REG);
293
294                 label = gpiochip_is_requested(chip, i);
295                 if (!label)
296                         label = "Unrequested";
297
298                 seq_printf(s,
299                            " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s\n",
300                            i,
301                            label,
302                            val & BYT_INPUT_EN ? "  " : "in",
303                            val & BYT_OUTPUT_EN ? "   " : "out",
304                            val & BYT_LEVEL ? "hi" : "lo",
305                            vg->range->pins[i], offs,
306                            conf0 & 0x7,
307                            conf0 & BYT_TRIG_NEG ? " fall" : "",
308                            conf0 & BYT_TRIG_POS ? " rise" : "",
309                            conf0 & BYT_TRIG_LVL ? " level" : "");
310         }
311         spin_unlock_irqrestore(&vg->lock, flags);
312 }
313
314 static int byt_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
315 {
316         struct byt_gpio *vg = to_byt_gpio(chip);
317         return irq_create_mapping(vg->domain, offset);
318 }
319
320 static void byt_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
321 {
322         struct irq_data *data = irq_desc_get_irq_data(desc);
323         struct byt_gpio *vg = irq_data_get_irq_handler_data(data);
324         struct irq_chip *chip = irq_data_get_irq_chip(data);
325         u32 base, pin, mask;
326         void __iomem *reg;
327         u32 pending;
328         unsigned virq;
329         int looplimit = 0;
330
331         /* check from GPIO controller which pin triggered the interrupt */
332         for (base = 0; base < vg->chip.ngpio; base += 32) {
333
334                 reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
335
336                 while ((pending = readl(reg))) {
337                         pin = __ffs(pending);
338                         mask = BIT(pin);
339                         /* Clear before handling so we can't lose an edge */
340                         writel(mask, reg);
341
342                         virq = irq_find_mapping(vg->domain, base + pin);
343                         generic_handle_irq(virq);
344
345                         /* In case bios or user sets triggering incorretly a pin
346                          * might remain in "interrupt triggered" state.
347                          */
348                         if (looplimit++ > 32) {
349                                 dev_err(&vg->pdev->dev,
350                                         "Gpio %d interrupt flood, disabling\n",
351                                         base + pin);
352
353                                 reg = byt_gpio_reg(&vg->chip, base + pin,
354                                                    BYT_CONF0_REG);
355                                 mask = readl(reg);
356                                 mask &= ~(BYT_TRIG_NEG | BYT_TRIG_POS |
357                                           BYT_TRIG_LVL);
358                                 writel(mask, reg);
359                                 mask = readl(reg); /* flush */
360                                 break;
361                         }
362                 }
363         }
364         chip->irq_eoi(data);
365 }
366
367 static void byt_irq_unmask(struct irq_data *d)
368 {
369 }
370
371 static void byt_irq_mask(struct irq_data *d)
372 {
373 }
374
375 static unsigned int byt_irq_startup(struct irq_data *d)
376 {
377         struct byt_gpio *vg = irq_data_get_irq_chip_data(d);
378
379         if (gpio_lock_as_irq(&vg->chip, irqd_to_hwirq(d)))
380                 dev_err(vg->chip.dev,
381                         "unable to lock HW IRQ %lu for IRQ\n",
382                         irqd_to_hwirq(d));
383         byt_irq_unmask(d);
384         return 0;
385 }
386
387 static void byt_irq_shutdown(struct irq_data *d)
388 {
389         struct byt_gpio *vg = irq_data_get_irq_chip_data(d);
390
391         byt_irq_mask(d);
392         gpio_unlock_as_irq(&vg->chip, irqd_to_hwirq(d));
393 }
394
395 static struct irq_chip byt_irqchip = {
396         .name = "BYT-GPIO",
397         .irq_mask = byt_irq_mask,
398         .irq_unmask = byt_irq_unmask,
399         .irq_set_type = byt_irq_type,
400         .irq_startup = byt_irq_startup,
401         .irq_shutdown = byt_irq_shutdown,
402 };
403
404 static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
405 {
406         void __iomem *reg;
407         u32 base, value;
408
409         /* clear interrupt status trigger registers */
410         for (base = 0; base < vg->chip.ngpio; base += 32) {
411                 reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
412                 writel(0xffffffff, reg);
413                 /* make sure trigger bits are cleared, if not then a pin
414                    might be misconfigured in bios */
415                 value = readl(reg);
416                 if (value)
417                         dev_err(&vg->pdev->dev,
418                                 "GPIO interrupt error, pins misconfigured\n");
419         }
420 }
421
422 static int byt_gpio_irq_map(struct irq_domain *d, unsigned int virq,
423                             irq_hw_number_t hw)
424 {
425         struct byt_gpio *vg = d->host_data;
426
427         irq_set_chip_and_handler_name(virq, &byt_irqchip, handle_simple_irq,
428                                       "demux");
429         irq_set_chip_data(virq, vg);
430         irq_set_irq_type(virq, IRQ_TYPE_NONE);
431
432         return 0;
433 }
434
435 static const struct irq_domain_ops byt_gpio_irq_ops = {
436         .map = byt_gpio_irq_map,
437 };
438
439 static int byt_gpio_probe(struct platform_device *pdev)
440 {
441         struct byt_gpio *vg;
442         struct gpio_chip *gc;
443         struct resource *mem_rc, *irq_rc;
444         struct device *dev = &pdev->dev;
445         struct acpi_device *acpi_dev;
446         struct pinctrl_gpio_range *range;
447         acpi_handle handle = ACPI_HANDLE(dev);
448         unsigned hwirq;
449         int ret;
450
451         if (acpi_bus_get_device(handle, &acpi_dev))
452                 return -ENODEV;
453
454         vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
455         if (!vg) {
456                 dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
457                 return -ENOMEM;
458         }
459
460         for (range = byt_ranges; range->name; range++) {
461                 if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
462                         vg->chip.ngpio = range->npins;
463                         vg->range = range;
464                         break;
465                 }
466         }
467
468         if (!vg->chip.ngpio || !vg->range)
469                 return -ENODEV;
470
471         vg->pdev = pdev;
472         platform_set_drvdata(pdev, vg);
473
474         mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
475         vg->reg_base = devm_ioremap_resource(dev, mem_rc);
476         if (IS_ERR(vg->reg_base))
477                 return PTR_ERR(vg->reg_base);
478
479         spin_lock_init(&vg->lock);
480
481         gc = &vg->chip;
482         gc->label = dev_name(&pdev->dev);
483         gc->owner = THIS_MODULE;
484         gc->request = byt_gpio_request;
485         gc->free = byt_gpio_free;
486         gc->direction_input = byt_gpio_direction_input;
487         gc->direction_output = byt_gpio_direction_output;
488         gc->get = byt_gpio_get;
489         gc->set = byt_gpio_set;
490         gc->dbg_show = byt_gpio_dbg_show;
491         gc->base = -1;
492         gc->can_sleep = 0;
493         gc->dev = dev;
494
495         ret = gpiochip_add(gc);
496         if (ret) {
497                 dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
498                 return ret;
499         }
500
501         /* set up interrupts  */
502         irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
503         if (irq_rc && irq_rc->start) {
504                 hwirq = irq_rc->start;
505                 gc->to_irq = byt_gpio_to_irq;
506
507                 vg->domain = irq_domain_add_linear(NULL, gc->ngpio,
508                                                    &byt_gpio_irq_ops, vg);
509                 if (!vg->domain)
510                         return -ENXIO;
511
512                 byt_gpio_irq_init_hw(vg);
513
514                 irq_set_handler_data(hwirq, vg);
515                 irq_set_chained_handler(hwirq, byt_gpio_irq_handler);
516
517                 /* Register interrupt handlers for gpio signaled acpi events */
518                 acpi_gpiochip_request_interrupts(gc);
519         }
520
521         pm_runtime_enable(dev);
522
523         return 0;
524 }
525
526 static int byt_gpio_runtime_suspend(struct device *dev)
527 {
528         return 0;
529 }
530
531 static int byt_gpio_runtime_resume(struct device *dev)
532 {
533         return 0;
534 }
535
536 static const struct dev_pm_ops byt_gpio_pm_ops = {
537         .runtime_suspend = byt_gpio_runtime_suspend,
538         .runtime_resume = byt_gpio_runtime_resume,
539 };
540
541 static const struct acpi_device_id byt_gpio_acpi_match[] = {
542         { "INT33B2", 0 },
543         { "INT33FC", 0 },
544         { }
545 };
546 MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
547
548 static int byt_gpio_remove(struct platform_device *pdev)
549 {
550         struct byt_gpio *vg = platform_get_drvdata(pdev);
551         int err;
552
553         pm_runtime_disable(&pdev->dev);
554         err = gpiochip_remove(&vg->chip);
555         if (err)
556                 dev_warn(&pdev->dev, "failed to remove gpio_chip.\n");
557
558         return 0;
559 }
560
561 static struct platform_driver byt_gpio_driver = {
562         .probe          = byt_gpio_probe,
563         .remove         = byt_gpio_remove,
564         .driver         = {
565                 .name   = "byt_gpio",
566                 .owner  = THIS_MODULE,
567                 .pm     = &byt_gpio_pm_ops,
568                 .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
569         },
570 };
571
572 static int __init byt_gpio_init(void)
573 {
574         return platform_driver_register(&byt_gpio_driver);
575 }
576
577 subsys_initcall(byt_gpio_init);