diff options
author | Ben Dooks <ben-linux@fluff.org> | 2008-07-28 07:04:07 -0400 |
---|---|---|
committer | Ben Dooks <ben-linux@fluff.org> | 2008-07-28 07:41:01 -0400 |
commit | 61c7cff89224fc5651b5ba5ff2185d19304b2484 (patch) | |
tree | 9bd975a1c8cd39f4cde3038749260fd8f7029ed0 /drivers/i2c | |
parent | 1efe7c55d2c4acc6c1d1c1a68bd9070f13815272 (diff) |
i2c: S3C24XX I2C frequency scaling support.
Add support for CPU frequency scaling to the S3C24XX I2C driver.
Signed-off-by: Ben Dooks <ben-linux@fluff.org>
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/busses/i2c-s3c2410.c | 116 |
1 files changed, 103 insertions, 13 deletions
diff --git a/drivers/i2c/busses/i2c-s3c2410.c b/drivers/i2c/busses/i2c-s3c2410.c index eef35d3f6073..4864723c7425 100644 --- a/drivers/i2c/busses/i2c-s3c2410.c +++ b/drivers/i2c/busses/i2c-s3c2410.c | |||
@@ -33,6 +33,7 @@ | |||
33 | #include <linux/err.h> | 33 | #include <linux/err.h> |
34 | #include <linux/platform_device.h> | 34 | #include <linux/platform_device.h> |
35 | #include <linux/clk.h> | 35 | #include <linux/clk.h> |
36 | #include <linux/cpufreq.h> | ||
36 | 37 | ||
37 | #include <asm/hardware.h> | 38 | #include <asm/hardware.h> |
38 | #include <asm/irq.h> | 39 | #include <asm/irq.h> |
@@ -64,6 +65,7 @@ struct s3c24xx_i2c { | |||
64 | unsigned int tx_setup; | 65 | unsigned int tx_setup; |
65 | 66 | ||
66 | enum s3c24xx_i2c_state state; | 67 | enum s3c24xx_i2c_state state; |
68 | unsigned long clkrate; | ||
67 | 69 | ||
68 | void __iomem *regs; | 70 | void __iomem *regs; |
69 | struct clk *clk; | 71 | struct clk *clk; |
@@ -71,6 +73,10 @@ struct s3c24xx_i2c { | |||
71 | struct resource *irq; | 73 | struct resource *irq; |
72 | struct resource *ioarea; | 74 | struct resource *ioarea; |
73 | struct i2c_adapter adap; | 75 | struct i2c_adapter adap; |
76 | |||
77 | #ifdef CONFIG_CPU_FREQ | ||
78 | struct notifier_block freq_transition; | ||
79 | #endif | ||
74 | }; | 80 | }; |
75 | 81 | ||
76 | /* default platform data to use if not supplied in the platform_device | 82 | /* default platform data to use if not supplied in the platform_device |
@@ -501,6 +507,9 @@ static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int | |||
501 | unsigned long timeout; | 507 | unsigned long timeout; |
502 | int ret; | 508 | int ret; |
503 | 509 | ||
510 | if (!readl(i2c->regs + S3C2410_IICCON) & S3C2410_IICCON_IRQEN) | ||
511 | return -EIO; | ||
512 | |||
504 | ret = s3c24xx_i2c_set_master(i2c); | 513 | ret = s3c24xx_i2c_set_master(i2c); |
505 | if (ret != 0) { | 514 | if (ret != 0) { |
506 | dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); | 515 | dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); |
@@ -636,27 +645,28 @@ static inline int freq_acceptable(unsigned int freq, unsigned int wanted) | |||
636 | return (diff >= -2 && diff <= 2); | 645 | return (diff >= -2 && diff <= 2); |
637 | } | 646 | } |
638 | 647 | ||
639 | /* s3c24xx_i2c_getdivisor | 648 | /* s3c24xx_i2c_clockrate |
640 | * | 649 | * |
641 | * work out a divisor for the user requested frequency setting, | 650 | * work out a divisor for the user requested frequency setting, |
642 | * either by the requested frequency, or scanning the acceptable | 651 | * either by the requested frequency, or scanning the acceptable |
643 | * range of frequencies until something is found | 652 | * range of frequencies until something is found |
644 | */ | 653 | */ |
645 | 654 | ||
646 | static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c *i2c, | 655 | static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) |
647 | struct s3c2410_platform_i2c *pdata, | ||
648 | unsigned long *iicon, | ||
649 | unsigned int *got) | ||
650 | { | 656 | { |
657 | struct s3c2410_platform_i2c *pdata; | ||
651 | unsigned long clkin = clk_get_rate(i2c->clk); | 658 | unsigned long clkin = clk_get_rate(i2c->clk); |
652 | |||
653 | unsigned int divs, div1; | 659 | unsigned int divs, div1; |
660 | u32 iiccon; | ||
654 | int freq; | 661 | int freq; |
655 | int start, end; | 662 | int start, end; |
656 | 663 | ||
664 | i2c->clkrate = clkin; | ||
665 | |||
666 | pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent); | ||
657 | clkin /= 1000; /* clkin now in KHz */ | 667 | clkin /= 1000; /* clkin now in KHz */ |
658 | 668 | ||
659 | dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", | 669 | dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", |
660 | pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq); | 670 | pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq); |
661 | 671 | ||
662 | if (pdata->bus_freq != 0) { | 672 | if (pdata->bus_freq != 0) { |
@@ -688,11 +698,79 @@ static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c *i2c, | |||
688 | 698 | ||
689 | found: | 699 | found: |
690 | *got = freq; | 700 | *got = freq; |
691 | *iicon |= (divs-1); | 701 | |
692 | *iicon |= (div1 == 512) ? S3C2410_IICCON_TXDIV_512 : 0; | 702 | iiccon = readl(i2c->regs + S3C2410_IICCON); |
703 | iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); | ||
704 | iiccon |= (divs-1); | ||
705 | |||
706 | if (div1 == 512) | ||
707 | iiccon |= S3C2410_IICCON_TXDIV_512; | ||
708 | |||
709 | writel(iiccon, i2c->regs + S3C2410_IICCON); | ||
710 | |||
711 | return 0; | ||
712 | } | ||
713 | |||
714 | #ifdef CONFIG_CPU_FREQ | ||
715 | |||
716 | #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) | ||
717 | |||
718 | static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, | ||
719 | unsigned long val, void *data) | ||
720 | { | ||
721 | struct s3c24xx_i2c *i2c = freq_to_i2c(nb); | ||
722 | unsigned long flags; | ||
723 | unsigned int got; | ||
724 | int delta_f; | ||
725 | int ret; | ||
726 | |||
727 | delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; | ||
728 | |||
729 | /* if we're post-change and the input clock has slowed down | ||
730 | * or at pre-change and the clock is about to speed up, then | ||
731 | * adjust our clock rate. <0 is slow, >0 speedup. | ||
732 | */ | ||
733 | |||
734 | if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || | ||
735 | (val == CPUFREQ_PRECHANGE && delta_f > 0)) { | ||
736 | spin_lock_irqsave(&i2c->lock, flags); | ||
737 | ret = s3c24xx_i2c_clockrate(i2c, &got); | ||
738 | spin_unlock_irqrestore(&i2c->lock, flags); | ||
739 | |||
740 | if (ret < 0) | ||
741 | dev_err(i2c->dev, "cannot find frequency\n"); | ||
742 | else | ||
743 | dev_info(i2c->dev, "setting freq %d\n", got); | ||
744 | } | ||
745 | |||
693 | return 0; | 746 | return 0; |
694 | } | 747 | } |
695 | 748 | ||
749 | static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) | ||
750 | { | ||
751 | i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; | ||
752 | |||
753 | return cpufreq_register_notifier(&i2c->freq_transition, | ||
754 | CPUFREQ_TRANSITION_NOTIFIER); | ||
755 | } | ||
756 | |||
757 | static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) | ||
758 | { | ||
759 | cpufreq_unregister_notifier(&i2c->freq_transition, | ||
760 | CPUFREQ_TRANSITION_NOTIFIER); | ||
761 | } | ||
762 | |||
763 | #else | ||
764 | static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) | ||
765 | { | ||
766 | return 0; | ||
767 | } | ||
768 | |||
769 | static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) | ||
770 | { | ||
771 | } | ||
772 | #endif | ||
773 | |||
696 | /* s3c24xx_i2c_init | 774 | /* s3c24xx_i2c_init |
697 | * | 775 | * |
698 | * initialise the controller, set the IO lines and frequency | 776 | * initialise the controller, set the IO lines and frequency |
@@ -719,9 +797,12 @@ static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) | |||
719 | 797 | ||
720 | dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); | 798 | dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); |
721 | 799 | ||
800 | writel(iicon, i2c->regs + S3C2410_IICCON); | ||
801 | |||
722 | /* we need to work out the divisors for the clock... */ | 802 | /* we need to work out the divisors for the clock... */ |
723 | 803 | ||
724 | if (s3c24xx_i2c_getdivisor(i2c, pdata, &iicon, &freq) != 0) { | 804 | if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { |
805 | writel(0, i2c->regs + S3C2410_IICCON); | ||
725 | dev_err(i2c->dev, "cannot meet bus frequency required\n"); | 806 | dev_err(i2c->dev, "cannot meet bus frequency required\n"); |
726 | return -EINVAL; | 807 | return -EINVAL; |
727 | } | 808 | } |
@@ -730,8 +811,6 @@ static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) | |||
730 | 811 | ||
731 | dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); | 812 | dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); |
732 | dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); | 813 | dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); |
733 | |||
734 | writel(iicon, i2c->regs + S3C2410_IICCON); | ||
735 | 814 | ||
736 | /* check for s3c2440 i2c controller */ | 815 | /* check for s3c2440 i2c controller */ |
737 | 816 | ||
@@ -835,6 +914,12 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) | |||
835 | dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res, | 914 | dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res, |
836 | (unsigned long)res->start); | 915 | (unsigned long)res->start); |
837 | 916 | ||
917 | ret = s3c24xx_i2c_register_cpufreq(i2c); | ||
918 | if (ret < 0) { | ||
919 | dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); | ||
920 | goto err_irq; | ||
921 | } | ||
922 | |||
838 | /* Note, previous versions of the driver used i2c_add_adapter() | 923 | /* Note, previous versions of the driver used i2c_add_adapter() |
839 | * to add the bus at any number. We now pass the bus number via | 924 | * to add the bus at any number. We now pass the bus number via |
840 | * the platform data, so if unset it will now default to always | 925 | * the platform data, so if unset it will now default to always |
@@ -846,7 +931,7 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) | |||
846 | ret = i2c_add_numbered_adapter(&i2c->adap); | 931 | ret = i2c_add_numbered_adapter(&i2c->adap); |
847 | if (ret < 0) { | 932 | if (ret < 0) { |
848 | dev_err(&pdev->dev, "failed to add bus to i2c core\n"); | 933 | dev_err(&pdev->dev, "failed to add bus to i2c core\n"); |
849 | goto err_irq; | 934 | goto err_cpufreq; |
850 | } | 935 | } |
851 | 936 | ||
852 | platform_set_drvdata(pdev, i2c); | 937 | platform_set_drvdata(pdev, i2c); |
@@ -854,6 +939,9 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev) | |||
854 | dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id); | 939 | dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id); |
855 | return 0; | 940 | return 0; |
856 | 941 | ||
942 | err_cpufreq: | ||
943 | s3c24xx_i2c_deregister_cpufreq(i2c); | ||
944 | |||
857 | err_irq: | 945 | err_irq: |
858 | free_irq(i2c->irq->start, i2c); | 946 | free_irq(i2c->irq->start, i2c); |
859 | 947 | ||
@@ -881,6 +969,8 @@ static int s3c24xx_i2c_remove(struct platform_device *pdev) | |||
881 | { | 969 | { |
882 | struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); | 970 | struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); |
883 | 971 | ||
972 | s3c24xx_i2c_deregister_cpufreq(i2c); | ||
973 | |||
884 | i2c_del_adapter(&i2c->adap); | 974 | i2c_del_adapter(&i2c->adap); |
885 | free_irq(i2c->irq->start, i2c); | 975 | free_irq(i2c->irq->start, i2c); |
886 | 976 | ||