aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio-davinci.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/gpio-davinci.c')
-rw-r--r--drivers/gpio/gpio-davinci.c189
1 files changed, 130 insertions, 59 deletions
diff --git a/drivers/gpio/gpio-davinci.c b/drivers/gpio/gpio-davinci.c
index 8847adf392b7..7629b4f12b7f 100644
--- a/drivers/gpio/gpio-davinci.c
+++ b/drivers/gpio/gpio-davinci.c
@@ -16,8 +16,13 @@
16#include <linux/err.h> 16#include <linux/err.h>
17#include <linux/io.h> 17#include <linux/io.h>
18#include <linux/irq.h> 18#include <linux/irq.h>
19#include <linux/irqdomain.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
19#include <linux/platform_device.h> 23#include <linux/platform_device.h>
20#include <linux/platform_data/gpio-davinci.h> 24#include <linux/platform_data/gpio-davinci.h>
25#include <linux/irqchip/chained_irq.h>
21 26
22struct davinci_gpio_regs { 27struct davinci_gpio_regs {
23 u32 dir; 28 u32 dir;
@@ -82,14 +87,14 @@ static inline int __davinci_direction(struct gpio_chip *chip,
82 u32 mask = 1 << offset; 87 u32 mask = 1 << offset;
83 88
84 spin_lock_irqsave(&d->lock, flags); 89 spin_lock_irqsave(&d->lock, flags);
85 temp = __raw_readl(&g->dir); 90 temp = readl_relaxed(&g->dir);
86 if (out) { 91 if (out) {
87 temp &= ~mask; 92 temp &= ~mask;
88 __raw_writel(mask, value ? &g->set_data : &g->clr_data); 93 writel_relaxed(mask, value ? &g->set_data : &g->clr_data);
89 } else { 94 } else {
90 temp |= mask; 95 temp |= mask;
91 } 96 }
92 __raw_writel(temp, &g->dir); 97 writel_relaxed(temp, &g->dir);
93 spin_unlock_irqrestore(&d->lock, flags); 98 spin_unlock_irqrestore(&d->lock, flags);
94 99
95 return 0; 100 return 0;
@@ -118,7 +123,7 @@ static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
118 struct davinci_gpio_controller *d = chip2controller(chip); 123 struct davinci_gpio_controller *d = chip2controller(chip);
119 struct davinci_gpio_regs __iomem *g = d->regs; 124 struct davinci_gpio_regs __iomem *g = d->regs;
120 125
121 return (1 << offset) & __raw_readl(&g->in_data); 126 return (1 << offset) & readl_relaxed(&g->in_data);
122} 127}
123 128
124/* 129/*
@@ -130,7 +135,41 @@ davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
130 struct davinci_gpio_controller *d = chip2controller(chip); 135 struct davinci_gpio_controller *d = chip2controller(chip);
131 struct davinci_gpio_regs __iomem *g = d->regs; 136 struct davinci_gpio_regs __iomem *g = d->regs;
132 137
133 __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data); 138 writel_relaxed((1 << offset), value ? &g->set_data : &g->clr_data);
139}
140
141static struct davinci_gpio_platform_data *
142davinci_gpio_get_pdata(struct platform_device *pdev)
143{
144 struct device_node *dn = pdev->dev.of_node;
145 struct davinci_gpio_platform_data *pdata;
146 int ret;
147 u32 val;
148
149 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
150 return pdev->dev.platform_data;
151
152 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
153 if (!pdata)
154 return NULL;
155
156 ret = of_property_read_u32(dn, "ti,ngpio", &val);
157 if (ret)
158 goto of_err;
159
160 pdata->ngpio = val;
161
162 ret = of_property_read_u32(dn, "ti,davinci-gpio-unbanked", &val);
163 if (ret)
164 goto of_err;
165
166 pdata->gpio_unbanked = val;
167
168 return pdata;
169
170of_err:
171 dev_err(&pdev->dev, "Populating pdata from DT failed: err %d\n", ret);
172 return NULL;
134} 173}
135 174
136static int davinci_gpio_probe(struct platform_device *pdev) 175static int davinci_gpio_probe(struct platform_device *pdev)
@@ -143,12 +182,14 @@ static int davinci_gpio_probe(struct platform_device *pdev)
143 struct device *dev = &pdev->dev; 182 struct device *dev = &pdev->dev;
144 struct resource *res; 183 struct resource *res;
145 184
146 pdata = dev->platform_data; 185 pdata = davinci_gpio_get_pdata(pdev);
147 if (!pdata) { 186 if (!pdata) {
148 dev_err(dev, "No platform data found\n"); 187 dev_err(dev, "No platform data found\n");
149 return -EINVAL; 188 return -EINVAL;
150 } 189 }
151 190
191 dev->platform_data = pdata;
192
152 /* 193 /*
153 * The gpio banks conceptually expose a segmented bitmap, 194 * The gpio banks conceptually expose a segmented bitmap,
154 * and "ngpio" is one more than the largest zero-based 195 * and "ngpio" is one more than the largest zero-based
@@ -160,8 +201,8 @@ static int davinci_gpio_probe(struct platform_device *pdev)
160 return -EINVAL; 201 return -EINVAL;
161 } 202 }
162 203
163 if (WARN_ON(DAVINCI_N_GPIO < ngpio)) 204 if (WARN_ON(ARCH_NR_GPIOS < ngpio))
164 ngpio = DAVINCI_N_GPIO; 205 ngpio = ARCH_NR_GPIOS;
165 206
166 chips = devm_kzalloc(dev, 207 chips = devm_kzalloc(dev,
167 ngpio * sizeof(struct davinci_gpio_controller), 208 ngpio * sizeof(struct davinci_gpio_controller),
@@ -194,6 +235,9 @@ static int davinci_gpio_probe(struct platform_device *pdev)
194 if (chips[i].chip.ngpio > 32) 235 if (chips[i].chip.ngpio > 32)
195 chips[i].chip.ngpio = 32; 236 chips[i].chip.ngpio = 32;
196 237
238#ifdef CONFIG_OF_GPIO
239 chips[i].chip.of_node = dev->of_node;
240#endif
197 spin_lock_init(&chips[i].lock); 241 spin_lock_init(&chips[i].lock);
198 242
199 regs = gpio2regs(base); 243 regs = gpio2regs(base);
@@ -227,8 +271,8 @@ static void gpio_irq_disable(struct irq_data *d)
227 struct davinci_gpio_regs __iomem *g = irq2regs(d->irq); 271 struct davinci_gpio_regs __iomem *g = irq2regs(d->irq);
228 u32 mask = (u32) irq_data_get_irq_handler_data(d); 272 u32 mask = (u32) irq_data_get_irq_handler_data(d);
229 273
230 __raw_writel(mask, &g->clr_falling); 274 writel_relaxed(mask, &g->clr_falling);
231 __raw_writel(mask, &g->clr_rising); 275 writel_relaxed(mask, &g->clr_rising);
232} 276}
233 277
234static void gpio_irq_enable(struct irq_data *d) 278static void gpio_irq_enable(struct irq_data *d)
@@ -242,9 +286,9 @@ static void gpio_irq_enable(struct irq_data *d)
242 status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING; 286 status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
243 287
244 if (status & IRQ_TYPE_EDGE_FALLING) 288 if (status & IRQ_TYPE_EDGE_FALLING)
245 __raw_writel(mask, &g->set_falling); 289 writel_relaxed(mask, &g->set_falling);
246 if (status & IRQ_TYPE_EDGE_RISING) 290 if (status & IRQ_TYPE_EDGE_RISING)
247 __raw_writel(mask, &g->set_rising); 291 writel_relaxed(mask, &g->set_rising);
248} 292}
249 293
250static int gpio_irq_type(struct irq_data *d, unsigned trigger) 294static int gpio_irq_type(struct irq_data *d, unsigned trigger)
@@ -278,34 +322,28 @@ gpio_irq_handler(unsigned irq, struct irq_desc *desc)
278 mask <<= 16; 322 mask <<= 16;
279 323
280 /* temporarily mask (level sensitive) parent IRQ */ 324 /* temporarily mask (level sensitive) parent IRQ */
281 desc->irq_data.chip->irq_mask(&desc->irq_data); 325 chained_irq_enter(irq_desc_get_chip(desc), desc);
282 desc->irq_data.chip->irq_ack(&desc->irq_data);
283 while (1) { 326 while (1) {
284 u32 status; 327 u32 status;
285 int n; 328 int bit;
286 int res;
287 329
288 /* ack any irqs */ 330 /* ack any irqs */
289 status = __raw_readl(&g->intstat) & mask; 331 status = readl_relaxed(&g->intstat) & mask;
290 if (!status) 332 if (!status)
291 break; 333 break;
292 __raw_writel(status, &g->intstat); 334 writel_relaxed(status, &g->intstat);
293 335
294 /* now demux them to the right lowlevel handler */ 336 /* now demux them to the right lowlevel handler */
295 n = d->irq_base;
296 if (irq & 1) {
297 n += 16;
298 status >>= 16;
299 }
300 337
301 while (status) { 338 while (status) {
302 res = ffs(status); 339 bit = __ffs(status);
303 n += res; 340 status &= ~BIT(bit);
304 generic_handle_irq(n - 1); 341 generic_handle_irq(
305 status >>= res; 342 irq_find_mapping(d->irq_domain,
343 d->chip.base + bit));
306 } 344 }
307 } 345 }
308 desc->irq_data.chip->irq_unmask(&desc->irq_data); 346 chained_irq_exit(irq_desc_get_chip(desc), desc);
309 /* now it may re-trigger */ 347 /* now it may re-trigger */
310} 348}
311 349
@@ -313,10 +351,10 @@ static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
313{ 351{
314 struct davinci_gpio_controller *d = chip2controller(chip); 352 struct davinci_gpio_controller *d = chip2controller(chip);
315 353
316 if (d->irq_base >= 0) 354 if (d->irq_domain)
317 return d->irq_base + offset; 355 return irq_create_mapping(d->irq_domain, d->chip.base + offset);
318 else 356 else
319 return -ENODEV; 357 return -ENXIO;
320} 358}
321 359
322static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset) 360static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
@@ -327,7 +365,7 @@ static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
327 * NOTE: we assume for now that only irqs in the first gpio_chip 365 * NOTE: we assume for now that only irqs in the first gpio_chip
328 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs). 366 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
329 */ 367 */
330 if (offset < d->irq_base) 368 if (offset < d->gpio_unbanked)
331 return d->gpio_irq + offset; 369 return d->gpio_irq + offset;
332 else 370 else
333 return -ENODEV; 371 return -ENODEV;
@@ -346,14 +384,35 @@ static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger)
346 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) 384 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
347 return -EINVAL; 385 return -EINVAL;
348 386
349 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING) 387 writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
350 ? &g->set_falling : &g->clr_falling); 388 ? &g->set_falling : &g->clr_falling);
351 __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING) 389 writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_RISING)
352 ? &g->set_rising : &g->clr_rising); 390 ? &g->set_rising : &g->clr_rising);
353 391
354 return 0; 392 return 0;
355} 393}
356 394
395static int
396davinci_gpio_irq_map(struct irq_domain *d, unsigned int irq,
397 irq_hw_number_t hw)
398{
399 struct davinci_gpio_regs __iomem *g = gpio2regs(hw);
400
401 irq_set_chip_and_handler_name(irq, &gpio_irqchip, handle_simple_irq,
402 "davinci_gpio");
403 irq_set_irq_type(irq, IRQ_TYPE_NONE);
404 irq_set_chip_data(irq, (__force void *)g);
405 irq_set_handler_data(irq, (void *)__gpio_mask(hw));
406 set_irq_flags(irq, IRQF_VALID);
407
408 return 0;
409}
410
411static const struct irq_domain_ops davinci_gpio_irq_ops = {
412 .map = davinci_gpio_irq_map,
413 .xlate = irq_domain_xlate_onetwocell,
414};
415
357/* 416/*
358 * NOTE: for suspend/resume, probably best to make a platform_device with 417 * NOTE: for suspend/resume, probably best to make a platform_device with
359 * suspend_late/resume_resume calls hooking into results of the set_wake() 418 * suspend_late/resume_resume calls hooking into results of the set_wake()
@@ -373,6 +432,7 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
373 struct davinci_gpio_controller *chips = platform_get_drvdata(pdev); 432 struct davinci_gpio_controller *chips = platform_get_drvdata(pdev);
374 struct davinci_gpio_platform_data *pdata = dev->platform_data; 433 struct davinci_gpio_platform_data *pdata = dev->platform_data;
375 struct davinci_gpio_regs __iomem *g; 434 struct davinci_gpio_regs __iomem *g;
435 struct irq_domain *irq_domain = NULL;
376 436
377 ngpio = pdata->ngpio; 437 ngpio = pdata->ngpio;
378 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 438 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
@@ -396,6 +456,22 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
396 } 456 }
397 clk_prepare_enable(clk); 457 clk_prepare_enable(clk);
398 458
459 if (!pdata->gpio_unbanked) {
460 irq = irq_alloc_descs(-1, 0, ngpio, 0);
461 if (irq < 0) {
462 dev_err(dev, "Couldn't allocate IRQ numbers\n");
463 return irq;
464 }
465
466 irq_domain = irq_domain_add_legacy(NULL, ngpio, irq, 0,
467 &davinci_gpio_irq_ops,
468 chips);
469 if (!irq_domain) {
470 dev_err(dev, "Couldn't register an IRQ domain\n");
471 return -ENODEV;
472 }
473 }
474
399 /* 475 /*
400 * Arrange gpio_to_irq() support, handling either direct IRQs or 476 * Arrange gpio_to_irq() support, handling either direct IRQs or
401 * banked IRQs. Having GPIOs in the first GPIO bank use direct 477 * banked IRQs. Having GPIOs in the first GPIO bank use direct
@@ -404,9 +480,7 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
404 */ 480 */
405 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) { 481 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) {
406 chips[bank].chip.to_irq = gpio_to_irq_banked; 482 chips[bank].chip.to_irq = gpio_to_irq_banked;
407 chips[bank].irq_base = pdata->gpio_unbanked 483 chips[bank].irq_domain = irq_domain;
408 ? -EINVAL
409 : (pdata->intc_irq_num + gpio);
410 } 484 }
411 485
412 /* 486 /*
@@ -419,6 +493,8 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
419 493
420 /* pass "bank 0" GPIO IRQs to AINTC */ 494 /* pass "bank 0" GPIO IRQs to AINTC */
421 chips[0].chip.to_irq = gpio_to_irq_unbanked; 495 chips[0].chip.to_irq = gpio_to_irq_unbanked;
496 chips[0].gpio_irq = bank_irq;
497 chips[0].gpio_unbanked = pdata->gpio_unbanked;
422 binten = BIT(0); 498 binten = BIT(0);
423 499
424 /* AINTC handles mask/unmask; GPIO handles triggering */ 500 /* AINTC handles mask/unmask; GPIO handles triggering */
@@ -430,8 +506,8 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
430 506
431 /* default trigger: both edges */ 507 /* default trigger: both edges */
432 g = gpio2regs(0); 508 g = gpio2regs(0);
433 __raw_writel(~0, &g->set_falling); 509 writel_relaxed(~0, &g->set_falling);
434 __raw_writel(~0, &g->set_rising); 510 writel_relaxed(~0, &g->set_rising);
435 511
436 /* set the direct IRQs up to use that irqchip */ 512 /* set the direct IRQs up to use that irqchip */
437 for (gpio = 0; gpio < pdata->gpio_unbanked; gpio++, irq++) { 513 for (gpio = 0; gpio < pdata->gpio_unbanked; gpio++, irq++) {
@@ -447,15 +523,11 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
447 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we 523 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
448 * then chain through our own handler. 524 * then chain through our own handler.
449 */ 525 */
450 for (gpio = 0, irq = gpio_to_irq(0), bank = 0; 526 for (gpio = 0, bank = 0; gpio < ngpio; bank++, bank_irq++, gpio += 16) {
451 gpio < ngpio;
452 bank++, bank_irq++) {
453 unsigned i;
454
455 /* disabled by default, enabled only as needed */ 527 /* disabled by default, enabled only as needed */
456 g = gpio2regs(gpio); 528 g = gpio2regs(gpio);
457 __raw_writel(~0, &g->clr_falling); 529 writel_relaxed(~0, &g->clr_falling);
458 __raw_writel(~0, &g->clr_rising); 530 writel_relaxed(~0, &g->clr_rising);
459 531
460 /* set up all irqs in this bank */ 532 /* set up all irqs in this bank */
461 irq_set_chained_handler(bank_irq, gpio_irq_handler); 533 irq_set_chained_handler(bank_irq, gpio_irq_handler);
@@ -467,14 +539,6 @@ static int davinci_gpio_irq_setup(struct platform_device *pdev)
467 */ 539 */
468 irq_set_handler_data(bank_irq, &chips[gpio / 32]); 540 irq_set_handler_data(bank_irq, &chips[gpio / 32]);
469 541
470 for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
471 irq_set_chip(irq, &gpio_irqchip);
472 irq_set_chip_data(irq, (__force void *)g);
473 irq_set_handler_data(irq, (void *)__gpio_mask(gpio));
474 irq_set_handler(irq, handle_simple_irq);
475 set_irq_flags(irq, IRQF_VALID);
476 }
477
478 binten |= BIT(bank); 542 binten |= BIT(bank);
479 } 543 }
480 544
@@ -483,18 +547,25 @@ done:
483 * BINTEN -- per-bank interrupt enable. genirq would also let these 547 * BINTEN -- per-bank interrupt enable. genirq would also let these
484 * bits be set/cleared dynamically. 548 * bits be set/cleared dynamically.
485 */ 549 */
486 __raw_writel(binten, gpio_base + BINTEN); 550 writel_relaxed(binten, gpio_base + BINTEN);
487
488 printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
489 551
490 return 0; 552 return 0;
491} 553}
492 554
555#if IS_ENABLED(CONFIG_OF)
556static const struct of_device_id davinci_gpio_ids[] = {
557 { .compatible = "ti,dm6441-gpio", },
558 { /* sentinel */ },
559};
560MODULE_DEVICE_TABLE(of, davinci_gpio_ids);
561#endif
562
493static struct platform_driver davinci_gpio_driver = { 563static struct platform_driver davinci_gpio_driver = {
494 .probe = davinci_gpio_probe, 564 .probe = davinci_gpio_probe,
495 .driver = { 565 .driver = {
496 .name = "davinci_gpio", 566 .name = "davinci_gpio",
497 .owner = THIS_MODULE, 567 .owner = THIS_MODULE,
568 .of_match_table = of_match_ptr(davinci_gpio_ids),
498 }, 569 },
499}; 570};
500 571