]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/gpio/gpio-tc3589x.c
6e8900933972ee7de0921fc3abef6704eaca9337
[can-eth-gw-linux.git] / drivers / gpio / gpio-tc3589x.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * License Terms: GNU General Public License, version 2
5  * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
6  * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
7  */
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/gpio.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/tc3589x.h>
18
19 /*
20  * These registers are modified under the irq bus lock and cached to avoid
21  * unnecessary writes in bus_sync_unlock.
22  */
23 enum { REG_IBE, REG_IEV, REG_IS, REG_IE };
24
25 #define CACHE_NR_REGS   4
26 #define CACHE_NR_BANKS  3
27
28 struct tc3589x_gpio {
29         struct gpio_chip chip;
30         struct tc3589x *tc3589x;
31         struct device *dev;
32         struct mutex irq_lock;
33         struct irq_domain *domain;
34
35         int irq_base;
36
37         /* Caches of interrupt control registers for bus_lock */
38         u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
39         u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
40 };
41
42 static inline struct tc3589x_gpio *to_tc3589x_gpio(struct gpio_chip *chip)
43 {
44         return container_of(chip, struct tc3589x_gpio, chip);
45 }
46
47 static int tc3589x_gpio_get(struct gpio_chip *chip, unsigned offset)
48 {
49         struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
50         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
51         u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
52         u8 mask = 1 << (offset % 8);
53         int ret;
54
55         ret = tc3589x_reg_read(tc3589x, reg);
56         if (ret < 0)
57                 return ret;
58
59         return ret & mask;
60 }
61
62 static void tc3589x_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
63 {
64         struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
65         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
66         u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
67         unsigned pos = offset % 8;
68         u8 data[] = {!!val << pos, 1 << pos};
69
70         tc3589x_block_write(tc3589x, reg, ARRAY_SIZE(data), data);
71 }
72
73 static int tc3589x_gpio_direction_output(struct gpio_chip *chip,
74                                          unsigned offset, int val)
75 {
76         struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
77         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
78         u8 reg = TC3589x_GPIODIR0 + offset / 8;
79         unsigned pos = offset % 8;
80
81         tc3589x_gpio_set(chip, offset, val);
82
83         return tc3589x_set_bits(tc3589x, reg, 1 << pos, 1 << pos);
84 }
85
86 static int tc3589x_gpio_direction_input(struct gpio_chip *chip,
87                                         unsigned offset)
88 {
89         struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
90         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
91         u8 reg = TC3589x_GPIODIR0 + offset / 8;
92         unsigned pos = offset % 8;
93
94         return tc3589x_set_bits(tc3589x, reg, 1 << pos, 0);
95 }
96
97 /**
98  * tc3589x_gpio_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
99  *
100  * @tc3589x_gpio: tc3589x_gpio_irq controller to operate on.
101  * @irq: index of the interrupt requested in the chip IRQs
102  *
103  * Useful for drivers to request their own IRQs.
104  */
105 static int tc3589x_gpio_irq_get_virq(struct tc3589x_gpio *tc3589x_gpio,
106                                      int irq)
107 {
108         if (!tc3589x_gpio)
109                 return -EINVAL;
110
111         return irq_create_mapping(tc3589x_gpio->domain, irq);
112 }
113
114 static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
115 {
116         struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
117
118         return tc3589x_gpio_irq_get_virq(tc3589x_gpio, offset);
119 }
120
121 static struct gpio_chip template_chip = {
122         .label                  = "tc3589x",
123         .owner                  = THIS_MODULE,
124         .direction_input        = tc3589x_gpio_direction_input,
125         .get                    = tc3589x_gpio_get,
126         .direction_output       = tc3589x_gpio_direction_output,
127         .set                    = tc3589x_gpio_set,
128         .to_irq                 = tc3589x_gpio_to_irq,
129         .can_sleep              = 1,
130 };
131
132 static int tc3589x_gpio_irq_set_type(struct irq_data *d, unsigned int type)
133 {
134         struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
135         int offset = d->hwirq;
136         int regoffset = offset / 8;
137         int mask = 1 << (offset % 8);
138
139         if (type == IRQ_TYPE_EDGE_BOTH) {
140                 tc3589x_gpio->regs[REG_IBE][regoffset] |= mask;
141                 return 0;
142         }
143
144         tc3589x_gpio->regs[REG_IBE][regoffset] &= ~mask;
145
146         if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH)
147                 tc3589x_gpio->regs[REG_IS][regoffset] |= mask;
148         else
149                 tc3589x_gpio->regs[REG_IS][regoffset] &= ~mask;
150
151         if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
152                 tc3589x_gpio->regs[REG_IEV][regoffset] |= mask;
153         else
154                 tc3589x_gpio->regs[REG_IEV][regoffset] &= ~mask;
155
156         return 0;
157 }
158
159 static void tc3589x_gpio_irq_lock(struct irq_data *d)
160 {
161         struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
162
163         mutex_lock(&tc3589x_gpio->irq_lock);
164 }
165
166 static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d)
167 {
168         struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
169         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
170         static const u8 regmap[] = {
171                 [REG_IBE]       = TC3589x_GPIOIBE0,
172                 [REG_IEV]       = TC3589x_GPIOIEV0,
173                 [REG_IS]        = TC3589x_GPIOIS0,
174                 [REG_IE]        = TC3589x_GPIOIE0,
175         };
176         int i, j;
177
178         for (i = 0; i < CACHE_NR_REGS; i++) {
179                 for (j = 0; j < CACHE_NR_BANKS; j++) {
180                         u8 old = tc3589x_gpio->oldregs[i][j];
181                         u8 new = tc3589x_gpio->regs[i][j];
182
183                         if (new == old)
184                                 continue;
185
186                         tc3589x_gpio->oldregs[i][j] = new;
187                         tc3589x_reg_write(tc3589x, regmap[i] + j * 8, new);
188                 }
189         }
190
191         mutex_unlock(&tc3589x_gpio->irq_lock);
192 }
193
194 static void tc3589x_gpio_irq_mask(struct irq_data *d)
195 {
196         struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
197         int offset = d->hwirq;
198         int regoffset = offset / 8;
199         int mask = 1 << (offset % 8);
200
201         tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask;
202 }
203
204 static void tc3589x_gpio_irq_unmask(struct irq_data *d)
205 {
206         struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
207         int offset = d->hwirq;
208         int regoffset = offset / 8;
209         int mask = 1 << (offset % 8);
210
211         tc3589x_gpio->regs[REG_IE][regoffset] |= mask;
212 }
213
214 static struct irq_chip tc3589x_gpio_irq_chip = {
215         .name                   = "tc3589x-gpio",
216         .irq_bus_lock           = tc3589x_gpio_irq_lock,
217         .irq_bus_sync_unlock    = tc3589x_gpio_irq_sync_unlock,
218         .irq_mask               = tc3589x_gpio_irq_mask,
219         .irq_unmask             = tc3589x_gpio_irq_unmask,
220         .irq_set_type           = tc3589x_gpio_irq_set_type,
221 };
222
223 static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
224 {
225         struct tc3589x_gpio *tc3589x_gpio = dev;
226         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
227         u8 status[CACHE_NR_BANKS];
228         int ret;
229         int i;
230
231         ret = tc3589x_block_read(tc3589x, TC3589x_GPIOMIS0,
232                                  ARRAY_SIZE(status), status);
233         if (ret < 0)
234                 return IRQ_NONE;
235
236         for (i = 0; i < ARRAY_SIZE(status); i++) {
237                 unsigned int stat = status[i];
238                 if (!stat)
239                         continue;
240
241                 while (stat) {
242                         int bit = __ffs(stat);
243                         int line = i * 8 + bit;
244                         int virq = tc3589x_gpio_irq_get_virq(tc3589x_gpio, line);
245
246                         handle_nested_irq(virq);
247                         stat &= ~(1 << bit);
248                 }
249
250                 tc3589x_reg_write(tc3589x, TC3589x_GPIOIC0 + i, status[i]);
251         }
252
253         return IRQ_HANDLED;
254 }
255
256 static int tc3589x_gpio_irq_map(struct irq_domain *d, unsigned int virq,
257                                 irq_hw_number_t hwirq)
258 {
259         struct tc3589x *tc3589x_gpio = d->host_data;
260
261         irq_set_chip_data(virq, tc3589x_gpio);
262         irq_set_chip_and_handler(virq, &tc3589x_gpio_irq_chip,
263                                 handle_simple_irq);
264         irq_set_nested_thread(virq, 1);
265 #ifdef CONFIG_ARM
266         set_irq_flags(virq, IRQF_VALID);
267 #else
268         irq_set_noprobe(virq);
269 #endif
270
271         return 0;
272 }
273
274 static void tc3589x_gpio_irq_unmap(struct irq_domain *d, unsigned int virq)
275 {
276 #ifdef CONFIG_ARM
277         set_irq_flags(virq, 0);
278 #endif
279         irq_set_chip_and_handler(virq, NULL, NULL);
280         irq_set_chip_data(virq, NULL);
281 }
282
283 static struct irq_domain_ops tc3589x_irq_ops = {
284         .map    = tc3589x_gpio_irq_map,
285         .unmap  = tc3589x_gpio_irq_unmap,
286         .xlate  = irq_domain_xlate_twocell,
287 };
288
289 static int tc3589x_gpio_irq_init(struct tc3589x_gpio *tc3589x_gpio)
290 {
291         int base = tc3589x_gpio->irq_base;
292
293         if (base) {
294                 tc3589x_gpio->domain = irq_domain_add_legacy(
295                         NULL, tc3589x_gpio->chip.ngpio, base,
296                         0, &tc3589x_irq_ops, tc3589x_gpio);
297         }
298         else {
299                 tc3589x_gpio->domain = irq_domain_add_linear(
300                         NULL, tc3589x_gpio->chip.ngpio,
301                         &tc3589x_irq_ops, tc3589x_gpio);
302         }
303
304         if (!tc3589x_gpio->domain) {
305                 dev_err(tc3589x_gpio->dev, "Failed to create irqdomain\n");
306                 return -ENOSYS;
307         }
308
309         return 0;
310 }
311
312 static int __devinit tc3589x_gpio_probe(struct platform_device *pdev)
313 {
314         struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
315         struct tc3589x_gpio_platform_data *pdata;
316         struct tc3589x_gpio *tc3589x_gpio;
317         int ret;
318         int irq;
319
320         pdata = tc3589x->pdata->gpio;
321         if (!pdata)
322                 return -ENODEV;
323
324         irq = platform_get_irq(pdev, 0);
325         if (irq < 0)
326                 return irq;
327
328         tc3589x_gpio = kzalloc(sizeof(struct tc3589x_gpio), GFP_KERNEL);
329         if (!tc3589x_gpio)
330                 return -ENOMEM;
331
332         mutex_init(&tc3589x_gpio->irq_lock);
333
334         tc3589x_gpio->dev = &pdev->dev;
335         tc3589x_gpio->tc3589x = tc3589x;
336
337         tc3589x_gpio->chip = template_chip;
338         tc3589x_gpio->chip.ngpio = tc3589x->num_gpio;
339         tc3589x_gpio->chip.dev = &pdev->dev;
340         tc3589x_gpio->chip.base = pdata->gpio_base;
341
342         tc3589x_gpio->irq_base = tc3589x->irq_base + TC3589x_INT_GPIO(0);
343
344         tc3589x_gpio->irq_base = tc3589x->irq_base ?
345                 tc3589x->irq_base + TC3589x_INT_GPIO(0) : 0;
346
347         /* Bring the GPIO module out of reset */
348         ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL,
349                                TC3589x_RSTCTRL_GPIRST, 0);
350         if (ret < 0)
351                 goto out_free;
352
353         ret = tc3589x_gpio_irq_init(tc3589x_gpio);
354         if (ret)
355                 goto out_free;
356
357         ret = request_threaded_irq(irq, NULL, tc3589x_gpio_irq, IRQF_ONESHOT,
358                                    "tc3589x-gpio", tc3589x_gpio);
359         if (ret) {
360                 dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
361                 goto out_free;
362         }
363
364         ret = gpiochip_add(&tc3589x_gpio->chip);
365         if (ret) {
366                 dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
367                 goto out_freeirq;
368         }
369
370         if (pdata->setup)
371                 pdata->setup(tc3589x, tc3589x_gpio->chip.base);
372
373         platform_set_drvdata(pdev, tc3589x_gpio);
374
375         return 0;
376
377 out_freeirq:
378         free_irq(irq, tc3589x_gpio);
379 out_free:
380         kfree(tc3589x_gpio);
381         return ret;
382 }
383
384 static int __devexit tc3589x_gpio_remove(struct platform_device *pdev)
385 {
386         struct tc3589x_gpio *tc3589x_gpio = platform_get_drvdata(pdev);
387         struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
388         struct tc3589x_gpio_platform_data *pdata = tc3589x->pdata->gpio;
389         int irq = platform_get_irq(pdev, 0);
390         int ret;
391
392         if (pdata->remove)
393                 pdata->remove(tc3589x, tc3589x_gpio->chip.base);
394
395         ret = gpiochip_remove(&tc3589x_gpio->chip);
396         if (ret < 0) {
397                 dev_err(tc3589x_gpio->dev,
398                         "unable to remove gpiochip: %d\n", ret);
399                 return ret;
400         }
401
402         free_irq(irq, tc3589x_gpio);
403
404         platform_set_drvdata(pdev, NULL);
405         kfree(tc3589x_gpio);
406
407         return 0;
408 }
409
410 static struct platform_driver tc3589x_gpio_driver = {
411         .driver.name    = "tc3589x-gpio",
412         .driver.owner   = THIS_MODULE,
413         .probe          = tc3589x_gpio_probe,
414         .remove         = __devexit_p(tc3589x_gpio_remove),
415 };
416
417 static int __init tc3589x_gpio_init(void)
418 {
419         return platform_driver_register(&tc3589x_gpio_driver);
420 }
421 subsys_initcall(tc3589x_gpio_init);
422
423 static void __exit tc3589x_gpio_exit(void)
424 {
425         platform_driver_unregister(&tc3589x_gpio_driver);
426 }
427 module_exit(tc3589x_gpio_exit);
428
429 MODULE_LICENSE("GPL v2");
430 MODULE_DESCRIPTION("TC3589x GPIO driver");
431 MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");