blob: 4f2623c2393ebcd63ea7aa5868cff8ee55661b4e [file] [log] [blame]
Y Vob2b35e12015-01-16 14:34:19 +07001/*
2 * AppliedMicro X-Gene SoC GPIO-Standby Driver
3 *
4 * Copyright (c) 2014, Applied Micro Circuits Corporation
Quan Nguyen1013fc42016-02-17 20:15:07 +07005 * Author: Tin Huynh <tnhuynh@apm.com>.
6 * Y Vo <yvo@apm.com>.
7 * Quan Nguyen <qnguyen@apm.com>.
Y Vob2b35e12015-01-16 14:34:19 +07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <linux/module.h>
24#include <linux/io.h>
25#include <linux/platform_device.h>
26#include <linux/of_gpio.h>
Y Vob2b35e12015-01-16 14:34:19 +070027#include <linux/gpio/driver.h>
Y Vo733cf012015-05-29 16:52:41 +070028#include <linux/acpi.h>
Y Vob2b35e12015-01-16 14:34:19 +070029
Y Vo733cf012015-05-29 16:52:41 +070030#include "gpiolib.h"
31
Quan Nguyen1013fc42016-02-17 20:15:07 +070032/* Common property names */
33#define XGENE_NIRQ_PROPERTY "apm,nr-irqs"
34#define XGENE_NGPIO_PROPERTY "apm,nr-gpios"
35#define XGENE_IRQ_START_PROPERTY "apm,irq-start"
Y Vob2b35e12015-01-16 14:34:19 +070036
Quan Nguyen1013fc42016-02-17 20:15:07 +070037#define XGENE_DFLT_MAX_NGPIO 22
38#define XGENE_DFLT_MAX_NIRQ 6
39#define XGENE_DFLT_IRQ_START_PIN 8
Y Vob2b35e12015-01-16 14:34:19 +070040#define GPIO_MASK(x) (1U << ((x) % 32))
41
42#define MPA_GPIO_INT_LVL 0x0290
43#define MPA_GPIO_OE_ADDR 0x029c
44#define MPA_GPIO_OUT_ADDR 0x02a0
45#define MPA_GPIO_IN_ADDR 0x02a4
46#define MPA_GPIO_SEL_LO 0x0294
47
Quan Nguyen1013fc42016-02-17 20:15:07 +070048#define GPIO_INT_LEVEL_H 0x000001
49#define GPIO_INT_LEVEL_L 0x000000
50
Y Vob2b35e12015-01-16 14:34:19 +070051/**
52 * struct xgene_gpio_sb - GPIO-Standby private data structure.
Linus Walleij0f4630f2015-12-04 14:02:58 +010053 * @gc: memory-mapped GPIO controllers.
Quan Nguyen1013fc42016-02-17 20:15:07 +070054 * @regs: GPIO register base offset
55 * @irq_domain: GPIO interrupt domain
56 * @irq_start: GPIO pin that start support interrupt
57 * @nirq: Number of GPIO pins that supports interrupt
58 * @parent_irq_base: Start parent HWIRQ
Y Vob2b35e12015-01-16 14:34:19 +070059 */
60struct xgene_gpio_sb {
Linus Walleij0f4630f2015-12-04 14:02:58 +010061 struct gpio_chip gc;
Quan Nguyen1013fc42016-02-17 20:15:07 +070062 void __iomem *regs;
63 struct irq_domain *irq_domain;
64 u16 irq_start;
65 u16 nirq;
66 u16 parent_irq_base;
Y Vob2b35e12015-01-16 14:34:19 +070067};
68
Quan Nguyen1013fc42016-02-17 20:15:07 +070069#define HWIRQ_TO_GPIO(priv, hwirq) ((hwirq) + (priv)->irq_start)
70#define GPIO_TO_HWIRQ(priv, gpio) ((gpio) - (priv)->irq_start)
71
72static void xgene_gpio_set_bit(struct gpio_chip *gc,
73 void __iomem *reg, u32 gpio, int val)
Y Vob2b35e12015-01-16 14:34:19 +070074{
75 u32 data;
76
Linus Walleij0f4630f2015-12-04 14:02:58 +010077 data = gc->read_reg(reg);
Y Vob2b35e12015-01-16 14:34:19 +070078 if (val)
79 data |= GPIO_MASK(gpio);
80 else
81 data &= ~GPIO_MASK(gpio);
Linus Walleij0f4630f2015-12-04 14:02:58 +010082 gc->write_reg(reg, data);
Y Vob2b35e12015-01-16 14:34:19 +070083}
84
Quan Nguyen1013fc42016-02-17 20:15:07 +070085static int xgene_gpio_sb_irq_set_type(struct irq_data *d, unsigned int type)
86{
87 struct xgene_gpio_sb *priv = irq_data_get_irq_chip_data(d);
88 int gpio = HWIRQ_TO_GPIO(priv, d->hwirq);
89 int lvl_type = GPIO_INT_LEVEL_H;
90
91 switch (type & IRQ_TYPE_SENSE_MASK) {
92 case IRQ_TYPE_EDGE_RISING:
93 case IRQ_TYPE_LEVEL_HIGH:
94 lvl_type = GPIO_INT_LEVEL_H;
95 break;
96 case IRQ_TYPE_EDGE_FALLING:
97 case IRQ_TYPE_LEVEL_LOW:
98 lvl_type = GPIO_INT_LEVEL_L;
99 break;
100 default:
101 break;
102 }
103
104 xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_SEL_LO,
105 gpio * 2, 1);
106 xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_INT_LVL,
107 d->hwirq, lvl_type);
108
109 /* Propagate IRQ type setting to parent */
110 if (type & IRQ_TYPE_EDGE_BOTH)
111 return irq_chip_set_type_parent(d, IRQ_TYPE_EDGE_RISING);
112 else
113 return irq_chip_set_type_parent(d, IRQ_TYPE_LEVEL_HIGH);
114}
115
116static struct irq_chip xgene_gpio_sb_irq_chip = {
117 .name = "sbgpio",
118 .irq_eoi = irq_chip_eoi_parent,
119 .irq_mask = irq_chip_mask_parent,
120 .irq_unmask = irq_chip_unmask_parent,
121 .irq_set_type = xgene_gpio_sb_irq_set_type,
122};
123
124static int xgene_gpio_sb_to_irq(struct gpio_chip *gc, u32 gpio)
Y Vob2b35e12015-01-16 14:34:19 +0700125{
Linus Walleij0f4630f2015-12-04 14:02:58 +0100126 struct xgene_gpio_sb *priv = gpiochip_get_data(gc);
Quan Nguyen1013fc42016-02-17 20:15:07 +0700127 struct irq_fwspec fwspec;
Y Vob2b35e12015-01-16 14:34:19 +0700128
Quan Nguyen1013fc42016-02-17 20:15:07 +0700129 if ((gpio < priv->irq_start) ||
130 (gpio > HWIRQ_TO_GPIO(priv, priv->nirq)))
131 return -ENXIO;
Y Vob2b35e12015-01-16 14:34:19 +0700132
Robin Murphyaa5c2a82017-08-24 18:24:11 +0100133 fwspec.fwnode = gc->parent->fwnode;
Quan Nguyen1013fc42016-02-17 20:15:07 +0700134 fwspec.param_count = 2;
135 fwspec.param[0] = GPIO_TO_HWIRQ(priv, gpio);
136 fwspec.param[1] = IRQ_TYPE_NONE;
137 return irq_create_fwspec_mapping(&fwspec);
Y Vob2b35e12015-01-16 14:34:19 +0700138}
139
Quan Nguyen1013fc42016-02-17 20:15:07 +0700140static void xgene_gpio_sb_domain_activate(struct irq_domain *d,
141 struct irq_data *irq_data)
142{
143 struct xgene_gpio_sb *priv = d->host_data;
144 u32 gpio = HWIRQ_TO_GPIO(priv, irq_data->hwirq);
145
146 if (gpiochip_lock_as_irq(&priv->gc, gpio)) {
147 dev_err(priv->gc.parent,
148 "Unable to configure XGene GPIO standby pin %d as IRQ\n",
149 gpio);
150 return;
151 }
152
153 xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_SEL_LO,
154 gpio * 2, 1);
155}
156
157static void xgene_gpio_sb_domain_deactivate(struct irq_domain *d,
158 struct irq_data *irq_data)
159{
160 struct xgene_gpio_sb *priv = d->host_data;
161 u32 gpio = HWIRQ_TO_GPIO(priv, irq_data->hwirq);
162
163 gpiochip_unlock_as_irq(&priv->gc, gpio);
164 xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_SEL_LO,
165 gpio * 2, 0);
166}
167
168static int xgene_gpio_sb_domain_translate(struct irq_domain *d,
169 struct irq_fwspec *fwspec,
170 unsigned long *hwirq,
171 unsigned int *type)
172{
173 struct xgene_gpio_sb *priv = d->host_data;
174
175 if ((fwspec->param_count != 2) ||
176 (fwspec->param[0] >= priv->nirq))
177 return -EINVAL;
178 *hwirq = fwspec->param[0];
179 *type = fwspec->param[1];
180 return 0;
181}
182
183static int xgene_gpio_sb_domain_alloc(struct irq_domain *domain,
184 unsigned int virq,
185 unsigned int nr_irqs, void *data)
186{
187 struct irq_fwspec *fwspec = data;
188 struct irq_fwspec parent_fwspec;
189 struct xgene_gpio_sb *priv = domain->host_data;
190 irq_hw_number_t hwirq;
191 unsigned int i;
192
193 hwirq = fwspec->param[0];
194 for (i = 0; i < nr_irqs; i++)
195 irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
196 &xgene_gpio_sb_irq_chip, priv);
197
198 parent_fwspec.fwnode = domain->parent->fwnode;
199 if (is_of_node(parent_fwspec.fwnode)) {
200 parent_fwspec.param_count = 3;
201 parent_fwspec.param[0] = 0;/* SPI */
202 /* Skip SGIs and PPIs*/
203 parent_fwspec.param[1] = hwirq + priv->parent_irq_base - 32;
204 parent_fwspec.param[2] = fwspec->param[1];
205 } else if (is_fwnode_irqchip(parent_fwspec.fwnode)) {
206 parent_fwspec.param_count = 2;
207 parent_fwspec.param[0] = hwirq + priv->parent_irq_base;
208 parent_fwspec.param[1] = fwspec->param[1];
209 } else
210 return -EINVAL;
211
212 return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
213 &parent_fwspec);
214}
215
Quan Nguyen1013fc42016-02-17 20:15:07 +0700216static const struct irq_domain_ops xgene_gpio_sb_domain_ops = {
217 .translate = xgene_gpio_sb_domain_translate,
218 .alloc = xgene_gpio_sb_domain_alloc,
Axel Linc6cc75f2016-03-17 12:01:43 +0800219 .free = irq_domain_free_irqs_common,
Quan Nguyen1013fc42016-02-17 20:15:07 +0700220 .activate = xgene_gpio_sb_domain_activate,
221 .deactivate = xgene_gpio_sb_domain_deactivate,
222};
223
Y Vob2b35e12015-01-16 14:34:19 +0700224static int xgene_gpio_sb_probe(struct platform_device *pdev)
225{
226 struct xgene_gpio_sb *priv;
Andrzej Hajda67ebb7422016-02-23 07:45:34 +0100227 int ret;
Y Vob2b35e12015-01-16 14:34:19 +0700228 struct resource *res;
229 void __iomem *regs;
Quan Nguyen1013fc42016-02-17 20:15:07 +0700230 struct irq_domain *parent_domain = NULL;
Quan Nguyen1013fc42016-02-17 20:15:07 +0700231 u32 val32;
Y Vob2b35e12015-01-16 14:34:19 +0700232
233 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
234 if (!priv)
235 return -ENOMEM;
236
237 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 regs = devm_ioremap_resource(&pdev->dev, res);
Vladimir Zapolskiy33c07b42015-03-29 05:37:20 +0300239 if (IS_ERR(regs))
Y Vob2b35e12015-01-16 14:34:19 +0700240 return PTR_ERR(regs);
241
Quan Nguyen1013fc42016-02-17 20:15:07 +0700242 priv->regs = regs;
243
244 ret = platform_get_irq(pdev, 0);
245 if (ret > 0) {
246 priv->parent_irq_base = irq_get_irq_data(ret)->hwirq;
247 parent_domain = irq_get_irq_data(ret)->domain;
248 }
249 if (!parent_domain) {
250 dev_err(&pdev->dev, "unable to obtain parent domain\n");
251 return -ENODEV;
252 }
253
Linus Walleij0f4630f2015-12-04 14:02:58 +0100254 ret = bgpio_init(&priv->gc, &pdev->dev, 4,
Y Vob2b35e12015-01-16 14:34:19 +0700255 regs + MPA_GPIO_IN_ADDR,
256 regs + MPA_GPIO_OUT_ADDR, NULL,
257 regs + MPA_GPIO_OE_ADDR, NULL, 0);
258 if (ret)
259 return ret;
260
Quan Nguyen1013fc42016-02-17 20:15:07 +0700261 priv->gc.to_irq = xgene_gpio_sb_to_irq;
Y Vob2b35e12015-01-16 14:34:19 +0700262
Quan Nguyen1013fc42016-02-17 20:15:07 +0700263 /* Retrieve start irq pin, use default if property not found */
264 priv->irq_start = XGENE_DFLT_IRQ_START_PIN;
265 if (!device_property_read_u32(&pdev->dev,
266 XGENE_IRQ_START_PROPERTY, &val32))
267 priv->irq_start = val32;
Y Vob2b35e12015-01-16 14:34:19 +0700268
Quan Nguyen1013fc42016-02-17 20:15:07 +0700269 /* Retrieve number irqs, use default if property not found */
270 priv->nirq = XGENE_DFLT_MAX_NIRQ;
271 if (!device_property_read_u32(&pdev->dev, XGENE_NIRQ_PROPERTY, &val32))
272 priv->nirq = val32;
Y Vob2b35e12015-01-16 14:34:19 +0700273
Quan Nguyen1013fc42016-02-17 20:15:07 +0700274 /* Retrieve number gpio, use default if property not found */
275 priv->gc.ngpio = XGENE_DFLT_MAX_NGPIO;
276 if (!device_property_read_u32(&pdev->dev, XGENE_NGPIO_PROPERTY, &val32))
277 priv->gc.ngpio = val32;
278
279 dev_info(&pdev->dev, "Support %d gpios, %d irqs start from pin %d\n",
280 priv->gc.ngpio, priv->nirq, priv->irq_start);
Y Vob2b35e12015-01-16 14:34:19 +0700281
282 platform_set_drvdata(pdev, priv);
283
Quan Nguyen1013fc42016-02-17 20:15:07 +0700284 priv->irq_domain = irq_domain_create_hierarchy(parent_domain,
Robin Murphyaa5c2a82017-08-24 18:24:11 +0100285 0, priv->nirq, pdev->dev.fwnode,
Quan Nguyen1013fc42016-02-17 20:15:07 +0700286 &xgene_gpio_sb_domain_ops, priv);
287 if (!priv->irq_domain)
288 return -ENODEV;
289
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100290 priv->gc.irq.domain = priv->irq_domain;
Quan Nguyen1013fc42016-02-17 20:15:07 +0700291
Laxman Dewangan29862052016-02-23 20:25:04 +0530292 ret = devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
Quan Nguyen1013fc42016-02-17 20:15:07 +0700293 if (ret) {
294 dev_err(&pdev->dev,
295 "failed to register X-Gene GPIO Standby driver\n");
296 irq_domain_remove(priv->irq_domain);
297 return ret;
298 }
299
300 dev_info(&pdev->dev, "X-Gene GPIO Standby driver registered\n");
Y Vob2b35e12015-01-16 14:34:19 +0700301
Y Vo733cf012015-05-29 16:52:41 +0700302 if (priv->nirq > 0) {
303 /* Register interrupt handlers for gpio signaled acpi events */
Linus Walleij0f4630f2015-12-04 14:02:58 +0100304 acpi_gpiochip_request_interrupts(&priv->gc);
Y Vo733cf012015-05-29 16:52:41 +0700305 }
306
Y Vob2b35e12015-01-16 14:34:19 +0700307 return ret;
308}
309
310static int xgene_gpio_sb_remove(struct platform_device *pdev)
311{
312 struct xgene_gpio_sb *priv = platform_get_drvdata(pdev);
313
Y Vo733cf012015-05-29 16:52:41 +0700314 if (priv->nirq > 0) {
Linus Walleij0f4630f2015-12-04 14:02:58 +0100315 acpi_gpiochip_free_interrupts(&priv->gc);
Y Vo733cf012015-05-29 16:52:41 +0700316 }
317
Quan Nguyen1013fc42016-02-17 20:15:07 +0700318 irq_domain_remove(priv->irq_domain);
319
Linus Walleij0f4630f2015-12-04 14:02:58 +0100320 return 0;
Y Vob2b35e12015-01-16 14:34:19 +0700321}
322
323static const struct of_device_id xgene_gpio_sb_of_match[] = {
324 {.compatible = "apm,xgene-gpio-sb", },
325 {},
326};
327MODULE_DEVICE_TABLE(of, xgene_gpio_sb_of_match);
328
Y Vo733cf012015-05-29 16:52:41 +0700329#ifdef CONFIG_ACPI
330static const struct acpi_device_id xgene_gpio_sb_acpi_match[] = {
331 {"APMC0D15", 0},
332 {},
333};
334MODULE_DEVICE_TABLE(acpi, xgene_gpio_sb_acpi_match);
335#endif
336
Y Vob2b35e12015-01-16 14:34:19 +0700337static struct platform_driver xgene_gpio_sb_driver = {
338 .driver = {
339 .name = "xgene-gpio-sb",
340 .of_match_table = xgene_gpio_sb_of_match,
Y Vo733cf012015-05-29 16:52:41 +0700341 .acpi_match_table = ACPI_PTR(xgene_gpio_sb_acpi_match),
Y Vob2b35e12015-01-16 14:34:19 +0700342 },
343 .probe = xgene_gpio_sb_probe,
344 .remove = xgene_gpio_sb_remove,
345};
346module_platform_driver(xgene_gpio_sb_driver);
347
348MODULE_AUTHOR("AppliedMicro");
349MODULE_DESCRIPTION("APM X-Gene GPIO Standby driver");
350MODULE_LICENSE("GPL");