Blame view

drivers/gpio/gpio-ath79.c 8.28 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
2
3
4
  /*
   *  Atheros AR71XX/AR724X/AR913X GPIO API support
   *
28be55dfe   Alban Bedel   gpio: ath79: Upda...
5
   *  Copyright (C) 2015 Alban Bedel <albeu@free.fr>
5b5b544ed   Gabor Juhos   MIPS: ath79: add ...
6
7
   *  Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
   *  Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
8
   *  Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
9
   */
49a5bd880   Alban Bedel   gpio: ath79: Conv...
10
  #include <linux/gpio/driver.h>
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
11
12
  #include <linux/platform_data/gpio-ath79.h>
  #include <linux/of_device.h>
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
13
  #include <linux/interrupt.h>
2034b9dcf   Paul Gortmaker   gpio: ath79: fix ...
14
  #include <linux/module.h>
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
15
  #include <linux/irq.h>
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
16

409d87838   Alban Bedel   gpio: ath79: Allo...
17
18
19
20
  #define AR71XX_GPIO_REG_OE		0x00
  #define AR71XX_GPIO_REG_IN		0x04
  #define AR71XX_GPIO_REG_SET		0x0c
  #define AR71XX_GPIO_REG_CLEAR		0x10
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
21

2b8f89e19   Alban Bedel   gpio: ath79: Add ...
22
23
24
25
26
  #define AR71XX_GPIO_REG_INT_ENABLE	0x14
  #define AR71XX_GPIO_REG_INT_TYPE	0x18
  #define AR71XX_GPIO_REG_INT_POLARITY	0x1c
  #define AR71XX_GPIO_REG_INT_PENDING	0x20
  #define AR71XX_GPIO_REG_INT_MASK	0x24
49a5bd880   Alban Bedel   gpio: ath79: Conv...
27
  struct ath79_gpio_ctrl {
ab32770ec   Alban Bedel   gpio: ath79: Move...
28
  	struct gpio_chip gc;
49a5bd880   Alban Bedel   gpio: ath79: Conv...
29
  	void __iomem *base;
a080ce53e   Julia Cartwright   gpio: ath79: make...
30
  	raw_spinlock_t lock;
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  	unsigned long both_edges;
  };
  
  static struct ath79_gpio_ctrl *irq_data_to_ath79_gpio(struct irq_data *data)
  {
  	struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  
  	return container_of(gc, struct ath79_gpio_ctrl, gc);
  }
  
  static u32 ath79_gpio_read(struct ath79_gpio_ctrl *ctrl, unsigned reg)
  {
  	return readl(ctrl->base + reg);
  }
  
  static void ath79_gpio_write(struct ath79_gpio_ctrl *ctrl,
  			unsigned reg, u32 val)
  {
23211b08c   zhong jiang   gpio: fix meaning...
49
  	writel(val, ctrl->base + reg);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  }
  
  static bool ath79_gpio_update_bits(
  	struct ath79_gpio_ctrl *ctrl, unsigned reg, u32 mask, u32 bits)
  {
  	u32 old_val, new_val;
  
  	old_val = ath79_gpio_read(ctrl, reg);
  	new_val = (old_val & ~mask) | (bits & mask);
  
  	if (new_val != old_val)
  		ath79_gpio_write(ctrl, reg, new_val);
  
  	return new_val != old_val;
  }
  
  static void ath79_gpio_irq_unmask(struct irq_data *data)
  {
  	struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  	u32 mask = BIT(irqd_to_hwirq(data));
  	unsigned long flags;
a080ce53e   Julia Cartwright   gpio: ath79: make...
71
  	raw_spin_lock_irqsave(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
72
  	ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
a080ce53e   Julia Cartwright   gpio: ath79: make...
73
  	raw_spin_unlock_irqrestore(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
74
75
76
77
78
79
80
  }
  
  static void ath79_gpio_irq_mask(struct irq_data *data)
  {
  	struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  	u32 mask = BIT(irqd_to_hwirq(data));
  	unsigned long flags;
a080ce53e   Julia Cartwright   gpio: ath79: make...
81
  	raw_spin_lock_irqsave(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
82
  	ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
a080ce53e   Julia Cartwright   gpio: ath79: make...
83
  	raw_spin_unlock_irqrestore(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
84
85
86
87
88
89
90
  }
  
  static void ath79_gpio_irq_enable(struct irq_data *data)
  {
  	struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  	u32 mask = BIT(irqd_to_hwirq(data));
  	unsigned long flags;
a080ce53e   Julia Cartwright   gpio: ath79: make...
91
  	raw_spin_lock_irqsave(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
92
93
  	ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
  	ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
a080ce53e   Julia Cartwright   gpio: ath79: make...
94
  	raw_spin_unlock_irqrestore(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
95
96
97
98
99
100
101
  }
  
  static void ath79_gpio_irq_disable(struct irq_data *data)
  {
  	struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  	u32 mask = BIT(irqd_to_hwirq(data));
  	unsigned long flags;
a080ce53e   Julia Cartwright   gpio: ath79: make...
102
  	raw_spin_lock_irqsave(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
103
104
  	ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
  	ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
a080ce53e   Julia Cartwright   gpio: ath79: make...
105
  	raw_spin_unlock_irqrestore(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  }
  
  static int ath79_gpio_irq_set_type(struct irq_data *data,
  				unsigned int flow_type)
  {
  	struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  	u32 mask = BIT(irqd_to_hwirq(data));
  	u32 type = 0, polarity = 0;
  	unsigned long flags;
  	bool disabled;
  
  	switch (flow_type) {
  	case IRQ_TYPE_EDGE_RISING:
  		polarity |= mask;
  	case IRQ_TYPE_EDGE_FALLING:
  	case IRQ_TYPE_EDGE_BOTH:
  		break;
  
  	case IRQ_TYPE_LEVEL_HIGH:
  		polarity |= mask;
e80df7b82   Gustavo A. R. Silva   gpio: mark expect...
126
  		/* fall through */
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
127
128
129
130
131
132
133
  	case IRQ_TYPE_LEVEL_LOW:
  		type |= mask;
  		break;
  
  	default:
  		return -EINVAL;
  	}
a080ce53e   Julia Cartwright   gpio: ath79: make...
134
  	raw_spin_lock_irqsave(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  
  	if (flow_type == IRQ_TYPE_EDGE_BOTH) {
  		ctrl->both_edges |= mask;
  		polarity = ~ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
  	} else {
  		ctrl->both_edges &= ~mask;
  	}
  
  	/* As the IRQ configuration can't be loaded atomically we
  	 * have to disable the interrupt while the configuration state
  	 * is invalid.
  	 */
  	disabled = ath79_gpio_update_bits(
  		ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
  
  	ath79_gpio_update_bits(
  		ctrl, AR71XX_GPIO_REG_INT_TYPE, mask, type);
  	ath79_gpio_update_bits(
  		ctrl, AR71XX_GPIO_REG_INT_POLARITY, mask, polarity);
  
  	if (disabled)
  		ath79_gpio_update_bits(
  			ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
a080ce53e   Julia Cartwright   gpio: ath79: make...
158
  	raw_spin_unlock_irqrestore(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
159
160
161
162
163
164
165
166
167
168
169
  
  	return 0;
  }
  
  static struct irq_chip ath79_gpio_irqchip = {
  	.name = "gpio-ath79",
  	.irq_enable = ath79_gpio_irq_enable,
  	.irq_disable = ath79_gpio_irq_disable,
  	.irq_mask = ath79_gpio_irq_mask,
  	.irq_unmask = ath79_gpio_irq_unmask,
  	.irq_set_type = ath79_gpio_irq_set_type,
49a5bd880   Alban Bedel   gpio: ath79: Conv...
170
  };
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
171
172
173
174
175
176
177
178
179
180
181
  static void ath79_gpio_irq_handler(struct irq_desc *desc)
  {
  	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  	struct irq_chip *irqchip = irq_desc_get_chip(desc);
  	struct ath79_gpio_ctrl *ctrl =
  		container_of(gc, struct ath79_gpio_ctrl, gc);
  	unsigned long flags, pending;
  	u32 both_edges, state;
  	int irq;
  
  	chained_irq_enter(irqchip, desc);
a080ce53e   Julia Cartwright   gpio: ath79: make...
182
  	raw_spin_lock_irqsave(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
183
184
185
186
187
188
189
190
191
192
  
  	pending = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_INT_PENDING);
  
  	/* Update the polarity of the both edges irqs */
  	both_edges = ctrl->both_edges & pending;
  	if (both_edges) {
  		state = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
  		ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_POLARITY,
  				both_edges, ~state);
  	}
a080ce53e   Julia Cartwright   gpio: ath79: make...
193
  	raw_spin_unlock_irqrestore(&ctrl->lock, flags);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
194
195
196
197
  
  	if (pending) {
  		for_each_set_bit(irq, &pending, gc->ngpio)
  			generic_handle_irq(
f0fbe7bce   Thierry Reding   gpio: Move irqdom...
198
  				irq_linear_revmap(gc->irq.domain, irq));
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
199
200
201
202
  	}
  
  	chained_irq_exit(irqchip, desc);
  }
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
203
204
205
206
207
  static const struct of_device_id ath79_gpio_of_match[] = {
  	{ .compatible = "qca,ar7100-gpio" },
  	{ .compatible = "qca,ar9340-gpio" },
  	{},
  };
6d8d271ee   Javier Martinez Canillas   gpio: ath79: Fix ...
208
  MODULE_DEVICE_TABLE(of, ath79_gpio_of_match);
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
209
210
  
  static int ath79_gpio_probe(struct platform_device *pdev)
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
211
  {
ab128afce   Nizam Haider   gpio: use dev_get...
212
  	struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
213
214
  	struct device *dev = &pdev->dev;
  	struct device_node *np = dev->of_node;
49a5bd880   Alban Bedel   gpio: ath79: Conv...
215
  	struct ath79_gpio_ctrl *ctrl;
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
216
  	struct gpio_irq_chip *girq;
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
217
  	struct resource *res;
49a5bd880   Alban Bedel   gpio: ath79: Conv...
218
  	u32 ath79_gpio_count;
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
219
  	bool oe_inverted;
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
220
  	int err;
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
221
  	ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
49a5bd880   Alban Bedel   gpio: ath79: Conv...
222
223
  	if (!ctrl)
  		return -ENOMEM;
2f890cf0d   Alban Bedel   gpio: ath79: Make...
224
  	platform_set_drvdata(pdev, ctrl);
49a5bd880   Alban Bedel   gpio: ath79: Conv...
225

2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
226
227
228
  	if (np) {
  		err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
  		if (err) {
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
229
230
  			dev_err(dev, "ngpios property is not valid
  ");
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
231
232
  			return err;
  		}
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
233
234
235
236
237
  		oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
  	} else if (pdata) {
  		ath79_gpio_count = pdata->ngpios;
  		oe_inverted = pdata->oe_inverted;
  	} else {
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
238
239
  		dev_err(dev, "No DT node or platform data found
  ");
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
240
241
  		return -EINVAL;
  	}
f0d3c72cc   Axel Lin   gpio: ath79: Chec...
242
  	if (ath79_gpio_count >= 32) {
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
243
244
  		dev_err(dev, "ngpios must be less than 32
  ");
f0d3c72cc   Axel Lin   gpio: ath79: Chec...
245
246
  		return -EINVAL;
  	}
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
247
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
f79b55d9b   Wei Yongjun   gpio: ath79: Fix ...
248
249
  	if (!res)
  		return -EINVAL;
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
250
  	ctrl->base = devm_ioremap_nocache(dev, res->start, resource_size(res));
49a5bd880   Alban Bedel   gpio: ath79: Conv...
251
  	if (!ctrl->base)
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
252
  		return -ENOMEM;
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
253

a080ce53e   Julia Cartwright   gpio: ath79: make...
254
  	raw_spin_lock_init(&ctrl->lock);
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
255
  	err = bgpio_init(&ctrl->gc, dev, 4,
ab32770ec   Alban Bedel   gpio: ath79: Move...
256
257
258
259
260
261
262
  			ctrl->base + AR71XX_GPIO_REG_IN,
  			ctrl->base + AR71XX_GPIO_REG_SET,
  			ctrl->base + AR71XX_GPIO_REG_CLEAR,
  			oe_inverted ? NULL : ctrl->base + AR71XX_GPIO_REG_OE,
  			oe_inverted ? ctrl->base + AR71XX_GPIO_REG_OE : NULL,
  			0);
  	if (err) {
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
263
264
  		dev_err(dev, "bgpio_init failed
  ");
ab32770ec   Alban Bedel   gpio: ath79: Move...
265
  		return err;
5b5b544ed   Gabor Juhos   MIPS: ath79: add ...
266
  	}
ab32770ec   Alban Bedel   gpio: ath79: Move...
267
268
  	/* Use base 0 to stay compatible with legacy platforms */
  	ctrl->gc.base = 0;
6eae43c57   Gabor Juhos   MIPS: ath79: add ...
269

aee5cec5b   Linus Walleij   gpio: ath79: Pass...
270
271
272
273
274
275
276
277
278
279
280
281
282
  	/* Optional interrupt setup */
  	if (!np || of_property_read_bool(np, "interrupt-controller")) {
  		girq = &ctrl->gc.irq;
  		girq->chip = &ath79_gpio_irqchip;
  		girq->parent_handler = ath79_gpio_irq_handler;
  		girq->num_parents = 1;
  		girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents),
  					     GFP_KERNEL);
  		if (!girq->parents)
  			return -ENOMEM;
  		girq->parents[0] = platform_get_irq(pdev, 0);
  		girq->default_type = IRQ_TYPE_NONE;
  		girq->handler = handle_simple_irq;
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
283
  	}
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
284
  	err = devm_gpiochip_add_data(dev, &ctrl->gc, ctrl);
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
285
  	if (err) {
aee5cec5b   Linus Walleij   gpio: ath79: Pass...
286
287
288
  		dev_err(dev,
  			"cannot add AR71xx GPIO chip, error=%d", err);
  		return err;
2b8f89e19   Alban Bedel   gpio: ath79: Add ...
289
  	}
2f890cf0d   Alban Bedel   gpio: ath79: Make...
290
291
  	return 0;
  }
2ddf3a792   Alban Bedel   MIPS: ath79: Add ...
292
293
294
295
296
297
298
299
300
  static struct platform_driver ath79_gpio_driver = {
  	.driver = {
  		.name = "ath79-gpio",
  		.of_match_table	= ath79_gpio_of_match,
  	},
  	.probe = ath79_gpio_probe,
  };
  
  module_platform_driver(ath79_gpio_driver);
539340f37   Jesse Chan   gpio: ath79: add ...
301
302
303
  
  MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X GPIO API support");
  MODULE_LICENSE("GPL v2");