ion: rockchip-ion: add API to set memory region secured
[firefly-linux-kernel-4.4.55.git] / drivers / gpio / gpio-langwell.c
1 /*
2  * Moorestown platform Langwell chip GPIO driver
3  *
4  * Copyright (c) 2008 - 2009,  Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /* Supports:
21  * Moorestown platform Langwell chip.
22  * Medfield platform Penwell chip.
23  * Whitney point.
24  */
25
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/platform_device.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/stddef.h>
32 #include <linux/interrupt.h>
33 #include <linux/init.h>
34 #include <linux/irq.h>
35 #include <linux/io.h>
36 #include <linux/gpio.h>
37 #include <linux/slab.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/irqdomain.h>
40
41 /*
42  * Langwell chip has 64 pins and thus there are 2 32bit registers to control
43  * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
44  * registers to control them, so we only define the order here instead of a
45  * structure, to get a bit offset for a pin (use GPDR as an example):
46  *
47  * nreg = ngpio / 32;
48  * reg = offset / 32;
49  * bit = offset % 32;
50  * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
51  *
52  * so the bit of reg_addr is to control pin offset's GPDR feature
53 */
54
55 enum GPIO_REG {
56         GPLR = 0,       /* pin level read-only */
57         GPDR,           /* pin direction */
58         GPSR,           /* pin set */
59         GPCR,           /* pin clear */
60         GRER,           /* rising edge detect */
61         GFER,           /* falling edge detect */
62         GEDR,           /* edge detect result */
63         GAFR,           /* alt function */
64 };
65
66 struct lnw_gpio {
67         struct gpio_chip                chip;
68         void                            *reg_base;
69         spinlock_t                      lock;
70         struct pci_dev                  *pdev;
71         struct irq_domain               *domain;
72 };
73
74 #define to_lnw_priv(chip)       container_of(chip, struct lnw_gpio, chip)
75
76 static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
77                         enum GPIO_REG reg_type)
78 {
79         struct lnw_gpio *lnw = to_lnw_priv(chip);
80         unsigned nreg = chip->ngpio / 32;
81         u8 reg = offset / 32;
82         void __iomem *ptr;
83
84         ptr = (void __iomem *)(lnw->reg_base + reg_type * nreg * 4 + reg * 4);
85         return ptr;
86 }
87
88 static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
89                                    enum GPIO_REG reg_type)
90 {
91         struct lnw_gpio *lnw = to_lnw_priv(chip);
92         unsigned nreg = chip->ngpio / 32;
93         u8 reg = offset / 16;
94         void __iomem *ptr;
95
96         ptr = (void __iomem *)(lnw->reg_base + reg_type * nreg * 4 + reg * 4);
97         return ptr;
98 }
99
100 static int lnw_gpio_request(struct gpio_chip *chip, unsigned offset)
101 {
102         void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
103         u32 value = readl(gafr);
104         int shift = (offset % 16) << 1, af = (value >> shift) & 3;
105
106         if (af) {
107                 value &= ~(3 << shift);
108                 writel(value, gafr);
109         }
110         return 0;
111 }
112
113 static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
114 {
115         void __iomem *gplr = gpio_reg(chip, offset, GPLR);
116
117         return readl(gplr) & BIT(offset % 32);
118 }
119
120 static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
121 {
122         void __iomem *gpsr, *gpcr;
123
124         if (value) {
125                 gpsr = gpio_reg(chip, offset, GPSR);
126                 writel(BIT(offset % 32), gpsr);
127         } else {
128                 gpcr = gpio_reg(chip, offset, GPCR);
129                 writel(BIT(offset % 32), gpcr);
130         }
131 }
132
133 static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
134 {
135         struct lnw_gpio *lnw = to_lnw_priv(chip);
136         void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
137         u32 value;
138         unsigned long flags;
139
140         if (lnw->pdev)
141                 pm_runtime_get(&lnw->pdev->dev);
142
143         spin_lock_irqsave(&lnw->lock, flags);
144         value = readl(gpdr);
145         value &= ~BIT(offset % 32);
146         writel(value, gpdr);
147         spin_unlock_irqrestore(&lnw->lock, flags);
148
149         if (lnw->pdev)
150                 pm_runtime_put(&lnw->pdev->dev);
151
152         return 0;
153 }
154
155 static int lnw_gpio_direction_output(struct gpio_chip *chip,
156                         unsigned offset, int value)
157 {
158         struct lnw_gpio *lnw = to_lnw_priv(chip);
159         void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
160         unsigned long flags;
161
162         lnw_gpio_set(chip, offset, value);
163
164         if (lnw->pdev)
165                 pm_runtime_get(&lnw->pdev->dev);
166
167         spin_lock_irqsave(&lnw->lock, flags);
168         value = readl(gpdr);
169         value |= BIT(offset % 32);
170         writel(value, gpdr);
171         spin_unlock_irqrestore(&lnw->lock, flags);
172
173         if (lnw->pdev)
174                 pm_runtime_put(&lnw->pdev->dev);
175
176         return 0;
177 }
178
179 static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
180 {
181         struct lnw_gpio *lnw = to_lnw_priv(chip);
182         return irq_create_mapping(lnw->domain, offset);
183 }
184
185 static int lnw_irq_type(struct irq_data *d, unsigned type)
186 {
187         struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
188         u32 gpio = irqd_to_hwirq(d);
189         unsigned long flags;
190         u32 value;
191         void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
192         void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
193
194         if (gpio >= lnw->chip.ngpio)
195                 return -EINVAL;
196
197         if (lnw->pdev)
198                 pm_runtime_get(&lnw->pdev->dev);
199
200         spin_lock_irqsave(&lnw->lock, flags);
201         if (type & IRQ_TYPE_EDGE_RISING)
202                 value = readl(grer) | BIT(gpio % 32);
203         else
204                 value = readl(grer) & (~BIT(gpio % 32));
205         writel(value, grer);
206
207         if (type & IRQ_TYPE_EDGE_FALLING)
208                 value = readl(gfer) | BIT(gpio % 32);
209         else
210                 value = readl(gfer) & (~BIT(gpio % 32));
211         writel(value, gfer);
212         spin_unlock_irqrestore(&lnw->lock, flags);
213
214         if (lnw->pdev)
215                 pm_runtime_put(&lnw->pdev->dev);
216
217         return 0;
218 }
219
220 static void lnw_irq_unmask(struct irq_data *d)
221 {
222 }
223
224 static void lnw_irq_mask(struct irq_data *d)
225 {
226 }
227
228 static struct irq_chip lnw_irqchip = {
229         .name           = "LNW-GPIO",
230         .irq_mask       = lnw_irq_mask,
231         .irq_unmask     = lnw_irq_unmask,
232         .irq_set_type   = lnw_irq_type,
233 };
234
235 static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = {   /* pin number */
236         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 },
237         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 },
238         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 },
239         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb), .driver_data = 96 },
240         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7), .driver_data = 96 },
241         { 0, }
242 };
243 MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
244
245 static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
246 {
247         struct irq_data *data = irq_desc_get_irq_data(desc);
248         struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data);
249         struct irq_chip *chip = irq_data_get_irq_chip(data);
250         u32 base, gpio, mask;
251         unsigned long pending;
252         void __iomem *gedr;
253
254         /* check GPIO controller to check which pin triggered the interrupt */
255         for (base = 0; base < lnw->chip.ngpio; base += 32) {
256                 gedr = gpio_reg(&lnw->chip, base, GEDR);
257                 while ((pending = readl(gedr))) {
258                         gpio = __ffs(pending);
259                         mask = BIT(gpio);
260                         /* Clear before handling so we can't lose an edge */
261                         writel(mask, gedr);
262                         generic_handle_irq(irq_find_mapping(lnw->domain,
263                                                             base + gpio));
264                 }
265         }
266
267         chip->irq_eoi(data);
268 }
269
270 static void lnw_irq_init_hw(struct lnw_gpio *lnw)
271 {
272         void __iomem *reg;
273         unsigned base;
274
275         for (base = 0; base < lnw->chip.ngpio; base += 32) {
276                 /* Clear the rising-edge detect register */
277                 reg = gpio_reg(&lnw->chip, base, GRER);
278                 writel(0, reg);
279                 /* Clear the falling-edge detect register */
280                 reg = gpio_reg(&lnw->chip, base, GFER);
281                 writel(0, reg);
282                 /* Clear the edge detect status register */
283                 reg = gpio_reg(&lnw->chip, base, GEDR);
284                 writel(~0, reg);
285         }
286 }
287
288 static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq,
289                             irq_hw_number_t hw)
290 {
291         struct lnw_gpio *lnw = d->host_data;
292
293         irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq,
294                                       "demux");
295         irq_set_chip_data(virq, lnw);
296         irq_set_irq_type(virq, IRQ_TYPE_NONE);
297
298         return 0;
299 }
300
301 static const struct irq_domain_ops lnw_gpio_irq_ops = {
302         .map = lnw_gpio_irq_map,
303         .xlate = irq_domain_xlate_twocell,
304 };
305
306 static int lnw_gpio_runtime_idle(struct device *dev)
307 {
308         pm_schedule_suspend(dev, 500);
309         return -EBUSY;
310 }
311
312 static const struct dev_pm_ops lnw_gpio_pm_ops = {
313         SET_RUNTIME_PM_OPS(NULL, NULL, lnw_gpio_runtime_idle)
314 };
315
316 static int lnw_gpio_probe(struct pci_dev *pdev,
317                         const struct pci_device_id *id)
318 {
319         void *base;
320         resource_size_t start, len;
321         struct lnw_gpio *lnw;
322         u32 gpio_base;
323         u32 irq_base;
324         int retval;
325         int ngpio = id->driver_data;
326
327         retval = pci_enable_device(pdev);
328         if (retval)
329                 return retval;
330
331         retval = pci_request_regions(pdev, "langwell_gpio");
332         if (retval) {
333                 dev_err(&pdev->dev, "error requesting resources\n");
334                 goto err_pci_req_region;
335         }
336         /* get the gpio_base from bar1 */
337         start = pci_resource_start(pdev, 1);
338         len = pci_resource_len(pdev, 1);
339         base = ioremap_nocache(start, len);
340         if (!base) {
341                 dev_err(&pdev->dev, "error mapping bar1\n");
342                 retval = -EFAULT;
343                 goto err_ioremap;
344         }
345         irq_base = *(u32 *)base;
346         gpio_base = *((u32 *)base + 1);
347         /* release the IO mapping, since we already get the info from bar1 */
348         iounmap(base);
349         /* get the register base from bar0 */
350         start = pci_resource_start(pdev, 0);
351         len = pci_resource_len(pdev, 0);
352         base = devm_ioremap_nocache(&pdev->dev, start, len);
353         if (!base) {
354                 dev_err(&pdev->dev, "error mapping bar0\n");
355                 retval = -EFAULT;
356                 goto err_ioremap;
357         }
358
359         lnw = devm_kzalloc(&pdev->dev, sizeof(*lnw), GFP_KERNEL);
360         if (!lnw) {
361                 dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
362                 retval = -ENOMEM;
363                 goto err_ioremap;
364         }
365
366         lnw->reg_base = base;
367         lnw->chip.label = dev_name(&pdev->dev);
368         lnw->chip.request = lnw_gpio_request;
369         lnw->chip.direction_input = lnw_gpio_direction_input;
370         lnw->chip.direction_output = lnw_gpio_direction_output;
371         lnw->chip.get = lnw_gpio_get;
372         lnw->chip.set = lnw_gpio_set;
373         lnw->chip.to_irq = lnw_gpio_to_irq;
374         lnw->chip.base = gpio_base;
375         lnw->chip.ngpio = ngpio;
376         lnw->chip.can_sleep = 0;
377         lnw->pdev = pdev;
378
379         lnw->domain = irq_domain_add_simple(pdev->dev.of_node, ngpio, irq_base,
380                                             &lnw_gpio_irq_ops, lnw);
381         if (!lnw->domain) {
382                 retval = -ENOMEM;
383                 goto err_ioremap;
384         }
385
386         pci_set_drvdata(pdev, lnw);
387         retval = gpiochip_add(&lnw->chip);
388         if (retval) {
389                 dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
390                 goto err_ioremap;
391         }
392
393         lnw_irq_init_hw(lnw);
394
395         irq_set_handler_data(pdev->irq, lnw);
396         irq_set_chained_handler(pdev->irq, lnw_irq_handler);
397
398         spin_lock_init(&lnw->lock);
399
400         pm_runtime_put_noidle(&pdev->dev);
401         pm_runtime_allow(&pdev->dev);
402
403         return 0;
404
405 err_ioremap:
406         pci_release_regions(pdev);
407 err_pci_req_region:
408         pci_disable_device(pdev);
409         return retval;
410 }
411
412 static struct pci_driver lnw_gpio_driver = {
413         .name           = "langwell_gpio",
414         .id_table       = lnw_gpio_ids,
415         .probe          = lnw_gpio_probe,
416         .driver         = {
417                 .pm     = &lnw_gpio_pm_ops,
418         },
419 };
420
421
422 static int wp_gpio_probe(struct platform_device *pdev)
423 {
424         struct lnw_gpio *lnw;
425         struct gpio_chip *gc;
426         struct resource *rc;
427         int retval = 0;
428
429         rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
430         if (!rc)
431                 return -EINVAL;
432
433         lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
434         if (!lnw) {
435                 dev_err(&pdev->dev,
436                         "can't allocate whitneypoint_gpio chip data\n");
437                 return -ENOMEM;
438         }
439         lnw->reg_base = ioremap_nocache(rc->start, resource_size(rc));
440         if (lnw->reg_base == NULL) {
441                 retval = -EINVAL;
442                 goto err_kmalloc;
443         }
444         spin_lock_init(&lnw->lock);
445         gc = &lnw->chip;
446         gc->label = dev_name(&pdev->dev);
447         gc->owner = THIS_MODULE;
448         gc->direction_input = lnw_gpio_direction_input;
449         gc->direction_output = lnw_gpio_direction_output;
450         gc->get = lnw_gpio_get;
451         gc->set = lnw_gpio_set;
452         gc->to_irq = NULL;
453         gc->base = 0;
454         gc->ngpio = 64;
455         gc->can_sleep = 0;
456         retval = gpiochip_add(gc);
457         if (retval) {
458                 dev_err(&pdev->dev, "whitneypoint gpiochip_add error %d\n",
459                                                                 retval);
460                 goto err_ioremap;
461         }
462         platform_set_drvdata(pdev, lnw);
463         return 0;
464 err_ioremap:
465         iounmap(lnw->reg_base);
466 err_kmalloc:
467         kfree(lnw);
468         return retval;
469 }
470
471 static int wp_gpio_remove(struct platform_device *pdev)
472 {
473         struct lnw_gpio *lnw = platform_get_drvdata(pdev);
474         int err;
475         err = gpiochip_remove(&lnw->chip);
476         if (err)
477                 dev_err(&pdev->dev, "failed to remove gpio_chip.\n");
478         iounmap(lnw->reg_base);
479         kfree(lnw);
480         platform_set_drvdata(pdev, NULL);
481         return 0;
482 }
483
484 static struct platform_driver wp_gpio_driver = {
485         .probe          = wp_gpio_probe,
486         .remove         = wp_gpio_remove,
487         .driver         = {
488                 .name   = "wp_gpio",
489                 .owner  = THIS_MODULE,
490         },
491 };
492
493 static int __init lnw_gpio_init(void)
494 {
495         int ret;
496         ret =  pci_register_driver(&lnw_gpio_driver);
497         if (ret < 0)
498                 return ret;
499         ret = platform_driver_register(&wp_gpio_driver);
500         if (ret < 0)
501                 pci_unregister_driver(&lnw_gpio_driver);
502         return ret;
503 }
504
505 device_initcall(lnw_gpio_init);