blob: d173d56dbb8c5790ecc77d0718a438947b1867d9 [file] [log] [blame]
Magnus Damm119f5e42013-03-13 20:32:13 +09001/*
2 * Renesas R-Car GPIO Support
3 *
4 * Copyright (C) 2013 Magnus Damm
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 as published by
8 * the Free Software Foundation; either version 2 of the License
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
16#include <linux/err.h>
17#include <linux/gpio.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/ioport.h>
22#include <linux/irq.h>
23#include <linux/irqdomain.h>
24#include <linux/module.h>
Laurent Pinchartdc3465a2013-03-10 03:27:00 +010025#include <linux/pinctrl/consumer.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090026#include <linux/platform_data/gpio-rcar.h>
27#include <linux/platform_device.h>
28#include <linux/spinlock.h>
29#include <linux/slab.h>
30
31struct gpio_rcar_priv {
32 void __iomem *base;
33 spinlock_t lock;
34 struct gpio_rcar_config config;
35 struct platform_device *pdev;
36 struct gpio_chip gpio_chip;
37 struct irq_chip irq_chip;
38 struct irq_domain *irq_domain;
39};
40
41#define IOINTSEL 0x00
42#define INOUTSEL 0x04
43#define OUTDT 0x08
44#define INDT 0x0c
45#define INTDT 0x10
46#define INTCLR 0x14
47#define INTMSK 0x18
48#define MSKCLR 0x1c
49#define POSNEG 0x20
50#define EDGLEVEL 0x24
51#define FILONOFF 0x28
Simon Horman7e1092b2013-05-24 18:47:24 +090052#define BOTHEDGE 0x4c
Magnus Damm119f5e42013-03-13 20:32:13 +090053
54static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
55{
56 return ioread32(p->base + offs);
57}
58
59static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
60 u32 value)
61{
62 iowrite32(value, p->base + offs);
63}
64
65static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
66 int bit, bool value)
67{
68 u32 tmp = gpio_rcar_read(p, offs);
69
70 if (value)
71 tmp |= BIT(bit);
72 else
73 tmp &= ~BIT(bit);
74
75 gpio_rcar_write(p, offs, tmp);
76}
77
78static void gpio_rcar_irq_disable(struct irq_data *d)
79{
80 struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
81
82 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
83}
84
85static void gpio_rcar_irq_enable(struct irq_data *d)
86{
87 struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
88
89 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
90}
91
92static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
93 unsigned int hwirq,
94 bool active_high_rising_edge,
Simon Horman7e1092b2013-05-24 18:47:24 +090095 bool level_trigger,
96 bool both)
Magnus Damm119f5e42013-03-13 20:32:13 +090097{
98 unsigned long flags;
99
100 /* follow steps in the GPIO documentation for
101 * "Setting Edge-Sensitive Interrupt Input Mode" and
102 * "Setting Level-Sensitive Interrupt Input Mode"
103 */
104
105 spin_lock_irqsave(&p->lock, flags);
106
107 /* Configure postive or negative logic in POSNEG */
108 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
109
110 /* Configure edge or level trigger in EDGLEVEL */
111 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
112
Simon Horman7e1092b2013-05-24 18:47:24 +0900113 /* Select one edge or both edges in BOTHEDGE */
114 if (p->config.has_both_edge_trigger)
115 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
116
Magnus Damm119f5e42013-03-13 20:32:13 +0900117 /* Select "Interrupt Input Mode" in IOINTSEL */
118 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
119
120 /* Write INTCLR in case of edge trigger */
121 if (!level_trigger)
122 gpio_rcar_write(p, INTCLR, BIT(hwirq));
123
124 spin_unlock_irqrestore(&p->lock, flags);
125}
126
127static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
128{
129 struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
130 unsigned int hwirq = irqd_to_hwirq(d);
131
132 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
133
134 switch (type & IRQ_TYPE_SENSE_MASK) {
135 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900136 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
137 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900138 break;
139 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900140 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
141 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900142 break;
143 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900144 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
145 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900146 break;
147 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900148 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
149 false);
150 break;
151 case IRQ_TYPE_EDGE_BOTH:
152 if (!p->config.has_both_edge_trigger)
153 return -EINVAL;
154 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
155 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900156 break;
157 default:
158 return -EINVAL;
159 }
160 return 0;
161}
162
163static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
164{
165 struct gpio_rcar_priv *p = dev_id;
166 u32 pending;
167 unsigned int offset, irqs_handled = 0;
168
169 while ((pending = gpio_rcar_read(p, INTDT))) {
170 offset = __ffs(pending);
171 gpio_rcar_write(p, INTCLR, BIT(offset));
172 generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
173 irqs_handled++;
174 }
175
176 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
177}
178
179static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
180{
181 return container_of(chip, struct gpio_rcar_priv, gpio_chip);
182}
183
184static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
185 unsigned int gpio,
186 bool output)
187{
188 struct gpio_rcar_priv *p = gpio_to_priv(chip);
189 unsigned long flags;
190
191 /* follow steps in the GPIO documentation for
192 * "Setting General Output Mode" and
193 * "Setting General Input Mode"
194 */
195
196 spin_lock_irqsave(&p->lock, flags);
197
198 /* Configure postive logic in POSNEG */
199 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
200
201 /* Select "General Input/Output Mode" in IOINTSEL */
202 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
203
204 /* Select Input Mode or Output Mode in INOUTSEL */
205 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
206
207 spin_unlock_irqrestore(&p->lock, flags);
208}
209
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100210static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
211{
212 return pinctrl_request_gpio(chip->base + offset);
213}
214
215static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
216{
217 pinctrl_free_gpio(chip->base + offset);
218
219 /* Set the GPIO as an input to ensure that the next GPIO request won't
220 * drive the GPIO pin as an output.
221 */
222 gpio_rcar_config_general_input_output_mode(chip, offset, false);
223}
224
Magnus Damm119f5e42013-03-13 20:32:13 +0900225static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
226{
227 gpio_rcar_config_general_input_output_mode(chip, offset, false);
228 return 0;
229}
230
231static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
232{
233 return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & BIT(offset));
234}
235
236static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
237{
238 struct gpio_rcar_priv *p = gpio_to_priv(chip);
239 unsigned long flags;
240
241 spin_lock_irqsave(&p->lock, flags);
242 gpio_rcar_modify_bit(p, OUTDT, offset, value);
243 spin_unlock_irqrestore(&p->lock, flags);
244}
245
246static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
247 int value)
248{
249 /* write GPIO value to output before selecting output mode of pin */
250 gpio_rcar_set(chip, offset, value);
251 gpio_rcar_config_general_input_output_mode(chip, offset, true);
252 return 0;
253}
254
255static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
256{
257 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
258}
259
260static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int virq,
261 irq_hw_number_t hw)
262{
263 struct gpio_rcar_priv *p = h->host_data;
264
265 dev_dbg(&p->pdev->dev, "map hw irq = %d, virq = %d\n", (int)hw, virq);
266
267 irq_set_chip_data(virq, h->host_data);
268 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
269 set_irq_flags(virq, IRQF_VALID); /* kill me now */
270 return 0;
271}
272
273static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
274 .map = gpio_rcar_irq_domain_map,
275};
276
277static int gpio_rcar_probe(struct platform_device *pdev)
278{
279 struct gpio_rcar_config *pdata = pdev->dev.platform_data;
280 struct gpio_rcar_priv *p;
281 struct resource *io, *irq;
282 struct gpio_chip *gpio_chip;
283 struct irq_chip *irq_chip;
284 const char *name = dev_name(&pdev->dev);
285 int ret;
286
287 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
288 if (!p) {
289 dev_err(&pdev->dev, "failed to allocate driver data\n");
290 ret = -ENOMEM;
291 goto err0;
292 }
293
294 /* deal with driver instance configuration */
295 if (pdata)
296 p->config = *pdata;
297
298 p->pdev = pdev;
299 platform_set_drvdata(pdev, p);
300 spin_lock_init(&p->lock);
301
302 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
303 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
304
305 if (!io || !irq) {
306 dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
307 ret = -EINVAL;
308 goto err0;
309 }
310
311 p->base = devm_ioremap_nocache(&pdev->dev, io->start,
312 resource_size(io));
313 if (!p->base) {
314 dev_err(&pdev->dev, "failed to remap I/O memory\n");
315 ret = -ENXIO;
316 goto err0;
317 }
318
319 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100320 gpio_chip->request = gpio_rcar_request;
321 gpio_chip->free = gpio_rcar_free;
Magnus Damm119f5e42013-03-13 20:32:13 +0900322 gpio_chip->direction_input = gpio_rcar_direction_input;
323 gpio_chip->get = gpio_rcar_get;
324 gpio_chip->direction_output = gpio_rcar_direction_output;
325 gpio_chip->set = gpio_rcar_set;
326 gpio_chip->to_irq = gpio_rcar_to_irq;
327 gpio_chip->label = name;
328 gpio_chip->owner = THIS_MODULE;
329 gpio_chip->base = p->config.gpio_base;
330 gpio_chip->ngpio = p->config.number_of_pins;
331
332 irq_chip = &p->irq_chip;
333 irq_chip->name = name;
334 irq_chip->irq_mask = gpio_rcar_irq_disable;
335 irq_chip->irq_unmask = gpio_rcar_irq_enable;
336 irq_chip->irq_enable = gpio_rcar_irq_enable;
337 irq_chip->irq_disable = gpio_rcar_irq_disable;
338 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
339 irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED;
340
341 p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
342 p->config.number_of_pins,
343 p->config.irq_base,
344 &gpio_rcar_irq_domain_ops, p);
345 if (!p->irq_domain) {
346 ret = -ENXIO;
347 dev_err(&pdev->dev, "cannot initialize irq domain\n");
348 goto err1;
349 }
350
351 if (devm_request_irq(&pdev->dev, irq->start,
Kuninori Morimotoc2349622013-04-17 23:40:57 -0700352 gpio_rcar_irq_handler, IRQF_SHARED, name, p)) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900353 dev_err(&pdev->dev, "failed to request IRQ\n");
354 ret = -ENOENT;
355 goto err1;
356 }
357
358 ret = gpiochip_add(gpio_chip);
359 if (ret) {
360 dev_err(&pdev->dev, "failed to add GPIO controller\n");
361 goto err1;
362 }
363
364 dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins);
365
366 /* warn in case of mismatch if irq base is specified */
367 if (p->config.irq_base) {
368 ret = irq_find_mapping(p->irq_domain, 0);
369 if (p->config.irq_base != ret)
370 dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n",
371 p->config.irq_base, ret);
372 }
373
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100374 ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
375 gpio_chip->base, gpio_chip->ngpio);
376 if (ret < 0)
377 dev_warn(&pdev->dev, "failed to add pin range\n");
378
Magnus Damm119f5e42013-03-13 20:32:13 +0900379 return 0;
380
381err1:
382 irq_domain_remove(p->irq_domain);
383err0:
384 return ret;
385}
386
387static int gpio_rcar_remove(struct platform_device *pdev)
388{
389 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
390 int ret;
391
392 ret = gpiochip_remove(&p->gpio_chip);
393 if (ret)
394 return ret;
395
396 irq_domain_remove(p->irq_domain);
397 return 0;
398}
399
400static struct platform_driver gpio_rcar_device_driver = {
401 .probe = gpio_rcar_probe,
402 .remove = gpio_rcar_remove,
403 .driver = {
404 .name = "gpio_rcar",
405 }
406};
407
408module_platform_driver(gpio_rcar_device_driver);
409
410MODULE_AUTHOR("Magnus Damm");
411MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
412MODULE_LICENSE("GPL v2");