aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/busses/i2c-pnx.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/busses/i2c-pnx.c')
-rw-r--r--drivers/i2c/busses/i2c-pnx.c157
1 files changed, 117 insertions, 40 deletions
diff --git a/drivers/i2c/busses/i2c-pnx.c b/drivers/i2c/busses/i2c-pnx.c
index eb8ad538c79..99389d2eae5 100644
--- a/drivers/i2c/busses/i2c-pnx.c
+++ b/drivers/i2c/busses/i2c-pnx.c
@@ -23,16 +23,61 @@
23#include <linux/err.h> 23#include <linux/err.h>
24#include <linux/clk.h> 24#include <linux/clk.h>
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/of_i2c.h>
27
28#define I2C_PNX_TIMEOUT_DEFAULT 10 /* msec */
29#define I2C_PNX_SPEED_KHZ_DEFAULT 100
30#define I2C_PNX_REGION_SIZE 0x100
31
32enum {
33 mstatus_tdi = 0x00000001,
34 mstatus_afi = 0x00000002,
35 mstatus_nai = 0x00000004,
36 mstatus_drmi = 0x00000008,
37 mstatus_active = 0x00000020,
38 mstatus_scl = 0x00000040,
39 mstatus_sda = 0x00000080,
40 mstatus_rff = 0x00000100,
41 mstatus_rfe = 0x00000200,
42 mstatus_tff = 0x00000400,
43 mstatus_tfe = 0x00000800,
44};
26 45
27#include <mach/hardware.h> 46enum {
28#include <mach/i2c.h> 47 mcntrl_tdie = 0x00000001,
48 mcntrl_afie = 0x00000002,
49 mcntrl_naie = 0x00000004,
50 mcntrl_drmie = 0x00000008,
51 mcntrl_daie = 0x00000020,
52 mcntrl_rffie = 0x00000040,
53 mcntrl_tffie = 0x00000080,
54 mcntrl_reset = 0x00000100,
55 mcntrl_cdbmode = 0x00000400,
56};
29 57
30#define I2C_PNX_TIMEOUT 10 /* msec */ 58enum {
31#define I2C_PNX_SPEED_KHZ 100 59 rw_bit = 1 << 0,
32#define I2C_PNX_REGION_SIZE 0x100 60 start_bit = 1 << 8,
61 stop_bit = 1 << 9,
62};
33 63
34static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data) 64#define I2C_REG_RX(a) ((a)->ioaddr) /* Rx FIFO reg (RO) */
65#define I2C_REG_TX(a) ((a)->ioaddr) /* Tx FIFO reg (WO) */
66#define I2C_REG_STS(a) ((a)->ioaddr + 0x04) /* Status reg (RO) */
67#define I2C_REG_CTL(a) ((a)->ioaddr + 0x08) /* Ctl reg */
68#define I2C_REG_CKL(a) ((a)->ioaddr + 0x0c) /* Clock divider low */
69#define I2C_REG_CKH(a) ((a)->ioaddr + 0x10) /* Clock divider high */
70#define I2C_REG_ADR(a) ((a)->ioaddr + 0x14) /* I2C address */
71#define I2C_REG_RFL(a) ((a)->ioaddr + 0x18) /* Rx FIFO level (RO) */
72#define I2C_REG_TFL(a) ((a)->ioaddr + 0x1c) /* Tx FIFO level (RO) */
73#define I2C_REG_RXB(a) ((a)->ioaddr + 0x20) /* Num of bytes Rx-ed (RO) */
74#define I2C_REG_TXB(a) ((a)->ioaddr + 0x24) /* Num of bytes Tx-ed (RO) */
75#define I2C_REG_TXS(a) ((a)->ioaddr + 0x28) /* Tx slave FIFO (RO) */
76#define I2C_REG_STFL(a) ((a)->ioaddr + 0x2c) /* Tx slave FIFO level (RO) */
77
78static inline int wait_timeout(struct i2c_pnx_algo_data *data)
35{ 79{
80 long timeout = data->timeout;
36 while (timeout > 0 && 81 while (timeout > 0 &&
37 (ioread32(I2C_REG_STS(data)) & mstatus_active)) { 82 (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
38 mdelay(1); 83 mdelay(1);
@@ -41,8 +86,9 @@ static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
41 return (timeout <= 0); 86 return (timeout <= 0);
42} 87}
43 88
44static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data) 89static inline int wait_reset(struct i2c_pnx_algo_data *data)
45{ 90{
91 long timeout = data->timeout;
46 while (timeout > 0 && 92 while (timeout > 0 &&
47 (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) { 93 (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
48 mdelay(1); 94 mdelay(1);
@@ -54,7 +100,7 @@ static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
54static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data) 100static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
55{ 101{
56 struct timer_list *timer = &alg_data->mif.timer; 102 struct timer_list *timer = &alg_data->mif.timer;
57 unsigned long expires = msecs_to_jiffies(I2C_PNX_TIMEOUT); 103 unsigned long expires = msecs_to_jiffies(alg_data->timeout);
58 104
59 if (expires <= 1) 105 if (expires <= 1)
60 expires = 2; 106 expires = 2;
@@ -92,7 +138,7 @@ static int i2c_pnx_start(unsigned char slave_addr,
92 } 138 }
93 139
94 /* First, make sure bus is idle */ 140 /* First, make sure bus is idle */
95 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) { 141 if (wait_timeout(alg_data)) {
96 /* Somebody else is monopolizing the bus */ 142 /* Somebody else is monopolizing the bus */
97 dev_err(&alg_data->adapter.dev, 143 dev_err(&alg_data->adapter.dev,
98 "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n", 144 "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
@@ -185,7 +231,7 @@ static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
185 if (alg_data->mif.len == 0) { 231 if (alg_data->mif.len == 0) {
186 if (alg_data->last) { 232 if (alg_data->last) {
187 /* Wait until the STOP is seen. */ 233 /* Wait until the STOP is seen. */
188 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) 234 if (wait_timeout(alg_data))
189 dev_err(&alg_data->adapter.dev, 235 dev_err(&alg_data->adapter.dev,
190 "The bus is still active after timeout\n"); 236 "The bus is still active after timeout\n");
191 } 237 }
@@ -283,7 +329,7 @@ static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
283 if (alg_data->mif.len == 0) { 329 if (alg_data->mif.len == 0) {
284 if (alg_data->last) 330 if (alg_data->last)
285 /* Wait until the STOP is seen. */ 331 /* Wait until the STOP is seen. */
286 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) 332 if (wait_timeout(alg_data))
287 dev_err(&alg_data->adapter.dev, 333 dev_err(&alg_data->adapter.dev,
288 "The bus is still active after timeout\n"); 334 "The bus is still active after timeout\n");
289 335
@@ -399,7 +445,7 @@ static void i2c_pnx_timeout(unsigned long data)
399 445
400 ctl |= mcntrl_reset; 446 ctl |= mcntrl_reset;
401 iowrite32(ctl, I2C_REG_CTL(alg_data)); 447 iowrite32(ctl, I2C_REG_CTL(alg_data));
402 wait_reset(I2C_PNX_TIMEOUT, alg_data); 448 wait_reset(alg_data);
403 alg_data->mif.ret = -EIO; 449 alg_data->mif.ret = -EIO;
404 complete(&alg_data->mif.complete); 450 complete(&alg_data->mif.complete);
405} 451}
@@ -414,18 +460,18 @@ static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
414 alg_data->adapter.name); 460 alg_data->adapter.name);
415 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset, 461 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
416 I2C_REG_CTL(alg_data)); 462 I2C_REG_CTL(alg_data));
417 wait_reset(I2C_PNX_TIMEOUT, alg_data); 463 wait_reset(alg_data);
418 } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) { 464 } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
419 /* If there is data in the fifo's after transfer, 465 /* If there is data in the fifo's after transfer,
420 * flush fifo's by reset. 466 * flush fifo's by reset.
421 */ 467 */
422 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset, 468 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
423 I2C_REG_CTL(alg_data)); 469 I2C_REG_CTL(alg_data));
424 wait_reset(I2C_PNX_TIMEOUT, alg_data); 470 wait_reset(alg_data);
425 } else if (stat & mstatus_nai) { 471 } else if (stat & mstatus_nai) {
426 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset, 472 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
427 I2C_REG_CTL(alg_data)); 473 I2C_REG_CTL(alg_data));
428 wait_reset(I2C_PNX_TIMEOUT, alg_data); 474 wait_reset(alg_data);
429 } 475 }
430} 476}
431 477
@@ -568,14 +614,8 @@ static int __devinit i2c_pnx_probe(struct platform_device *pdev)
568 int ret = 0; 614 int ret = 0;
569 struct i2c_pnx_algo_data *alg_data; 615 struct i2c_pnx_algo_data *alg_data;
570 unsigned long freq; 616 unsigned long freq;
571 struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data; 617 struct resource *res;
572 618 u32 speed = I2C_PNX_SPEED_KHZ_DEFAULT * 1000;
573 if (!i2c_pnx || !i2c_pnx->name) {
574 dev_err(&pdev->dev, "%s: no platform data supplied\n",
575 __func__);
576 ret = -EINVAL;
577 goto out;
578 }
579 619
580 alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL); 620 alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
581 if (!alg_data) { 621 if (!alg_data) {
@@ -585,14 +625,27 @@ static int __devinit i2c_pnx_probe(struct platform_device *pdev)
585 625
586 platform_set_drvdata(pdev, alg_data); 626 platform_set_drvdata(pdev, alg_data);
587 627
588 strlcpy(alg_data->adapter.name, i2c_pnx->name,
589 sizeof(alg_data->adapter.name));
590 alg_data->adapter.dev.parent = &pdev->dev; 628 alg_data->adapter.dev.parent = &pdev->dev;
591 alg_data->adapter.algo = &pnx_algorithm; 629 alg_data->adapter.algo = &pnx_algorithm;
592 alg_data->adapter.algo_data = alg_data; 630 alg_data->adapter.algo_data = alg_data;
593 alg_data->adapter.nr = pdev->id; 631 alg_data->adapter.nr = pdev->id;
594 alg_data->i2c_pnx = i2c_pnx;
595 632
633 alg_data->timeout = I2C_PNX_TIMEOUT_DEFAULT;
634#ifdef CONFIG_OF
635 alg_data->adapter.dev.of_node = of_node_get(pdev->dev.of_node);
636 if (pdev->dev.of_node) {
637 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
638 &speed);
639 /*
640 * At this point, it is planned to add an OF timeout property.
641 * As soon as there is a consensus about how to call and handle
642 * this, sth. like the following can be put here:
643 *
644 * of_property_read_u32(pdev->dev.of_node, "timeout",
645 * &alg_data->timeout);
646 */
647 }
648#endif
596 alg_data->clk = clk_get(&pdev->dev, NULL); 649 alg_data->clk = clk_get(&pdev->dev, NULL);
597 if (IS_ERR(alg_data->clk)) { 650 if (IS_ERR(alg_data->clk)) {
598 ret = PTR_ERR(alg_data->clk); 651 ret = PTR_ERR(alg_data->clk);
@@ -603,17 +656,27 @@ static int __devinit i2c_pnx_probe(struct platform_device *pdev)
603 alg_data->mif.timer.function = i2c_pnx_timeout; 656 alg_data->mif.timer.function = i2c_pnx_timeout;
604 alg_data->mif.timer.data = (unsigned long)alg_data; 657 alg_data->mif.timer.data = (unsigned long)alg_data;
605 658
659 snprintf(alg_data->adapter.name, sizeof(alg_data->adapter.name),
660 "%s", pdev->name);
661
606 /* Register I/O resource */ 662 /* Register I/O resource */
607 if (!request_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE, 663 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
664 if (!res) {
665 dev_err(&pdev->dev, "Unable to get mem resource.\n");
666 ret = -EBUSY;
667 goto out_clkget;
668 }
669 if (!request_mem_region(res->start, I2C_PNX_REGION_SIZE,
608 pdev->name)) { 670 pdev->name)) {
609 dev_err(&pdev->dev, 671 dev_err(&pdev->dev,
610 "I/O region 0x%08x for I2C already in use.\n", 672 "I/O region 0x%08x for I2C already in use.\n",
611 i2c_pnx->base); 673 res->start);
612 ret = -ENODEV; 674 ret = -ENOMEM;
613 goto out_clkget; 675 goto out_clkget;
614 } 676 }
615 677
616 alg_data->ioaddr = ioremap(i2c_pnx->base, I2C_PNX_REGION_SIZE); 678 alg_data->base = res->start;
679 alg_data->ioaddr = ioremap(res->start, I2C_PNX_REGION_SIZE);
617 if (!alg_data->ioaddr) { 680 if (!alg_data->ioaddr) {
618 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n"); 681 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
619 ret = -ENOMEM; 682 ret = -ENOMEM;
@@ -637,20 +700,25 @@ static int __devinit i2c_pnx_probe(struct platform_device *pdev)
637 * the deglitching filter length. 700 * the deglitching filter length.
638 */ 701 */
639 702
640 tmp = ((freq / 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2; 703 tmp = (freq / speed) / 2 - 2;
641 if (tmp > 0x3FF) 704 if (tmp > 0x3FF)
642 tmp = 0x3FF; 705 tmp = 0x3FF;
643 iowrite32(tmp, I2C_REG_CKH(alg_data)); 706 iowrite32(tmp, I2C_REG_CKH(alg_data));
644 iowrite32(tmp, I2C_REG_CKL(alg_data)); 707 iowrite32(tmp, I2C_REG_CKL(alg_data));
645 708
646 iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data)); 709 iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
647 if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) { 710 if (wait_reset(alg_data)) {
648 ret = -ENODEV; 711 ret = -ENODEV;
649 goto out_clock; 712 goto out_clock;
650 } 713 }
651 init_completion(&alg_data->mif.complete); 714 init_completion(&alg_data->mif.complete);
652 715
653 ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt, 716 alg_data->irq = platform_get_irq(pdev, 0);
717 if (alg_data->irq < 0) {
718 dev_err(&pdev->dev, "Failed to get IRQ from platform resource\n");
719 goto out_irq;
720 }
721 ret = request_irq(alg_data->irq, i2c_pnx_interrupt,
654 0, pdev->name, alg_data); 722 0, pdev->name, alg_data);
655 if (ret) 723 if (ret)
656 goto out_clock; 724 goto out_clock;
@@ -662,39 +730,39 @@ static int __devinit i2c_pnx_probe(struct platform_device *pdev)
662 goto out_irq; 730 goto out_irq;
663 } 731 }
664 732
733 of_i2c_register_devices(&alg_data->adapter);
734
665 dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n", 735 dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
666 alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq); 736 alg_data->adapter.name, res->start, alg_data->irq);
667 737
668 return 0; 738 return 0;
669 739
670out_irq: 740out_irq:
671 free_irq(i2c_pnx->irq, alg_data); 741 free_irq(alg_data->irq, alg_data);
672out_clock: 742out_clock:
673 clk_disable(alg_data->clk); 743 clk_disable(alg_data->clk);
674out_unmap: 744out_unmap:
675 iounmap(alg_data->ioaddr); 745 iounmap(alg_data->ioaddr);
676out_release: 746out_release:
677 release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE); 747 release_mem_region(res->start, I2C_PNX_REGION_SIZE);
678out_clkget: 748out_clkget:
679 clk_put(alg_data->clk); 749 clk_put(alg_data->clk);
680out_drvdata: 750out_drvdata:
681 kfree(alg_data); 751 kfree(alg_data);
682err_kzalloc: 752err_kzalloc:
683 platform_set_drvdata(pdev, NULL); 753 platform_set_drvdata(pdev, NULL);
684out:
685 return ret; 754 return ret;
686} 755}
687 756
688static int __devexit i2c_pnx_remove(struct platform_device *pdev) 757static int __devexit i2c_pnx_remove(struct platform_device *pdev)
689{ 758{
690 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev); 759 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
691 struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx;
692 760
693 free_irq(i2c_pnx->irq, alg_data); 761 free_irq(alg_data->irq, alg_data);
694 i2c_del_adapter(&alg_data->adapter); 762 i2c_del_adapter(&alg_data->adapter);
695 clk_disable(alg_data->clk); 763 clk_disable(alg_data->clk);
696 iounmap(alg_data->ioaddr); 764 iounmap(alg_data->ioaddr);
697 release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE); 765 release_mem_region(alg_data->base, I2C_PNX_REGION_SIZE);
698 clk_put(alg_data->clk); 766 clk_put(alg_data->clk);
699 kfree(alg_data); 767 kfree(alg_data);
700 platform_set_drvdata(pdev, NULL); 768 platform_set_drvdata(pdev, NULL);
@@ -702,10 +770,19 @@ static int __devexit i2c_pnx_remove(struct platform_device *pdev)
702 return 0; 770 return 0;
703} 771}
704 772
773#ifdef CONFIG_OF
774static const struct of_device_id i2c_pnx_of_match[] = {
775 { .compatible = "nxp,pnx-i2c" },
776 { },
777};
778MODULE_DEVICE_TABLE(of, i2c_pnx_of_match);
779#endif
780
705static struct platform_driver i2c_pnx_driver = { 781static struct platform_driver i2c_pnx_driver = {
706 .driver = { 782 .driver = {
707 .name = "pnx-i2c", 783 .name = "pnx-i2c",
708 .owner = THIS_MODULE, 784 .owner = THIS_MODULE,
785 .of_match_table = of_match_ptr(i2c_pnx_of_match),
709 }, 786 },
710 .probe = i2c_pnx_probe, 787 .probe = i2c_pnx_probe,
711 .remove = __devexit_p(i2c_pnx_remove), 788 .remove = __devexit_p(i2c_pnx_remove),