powerpc: i2c-mpc: make I2C bus speed configurable
Subsystems (5)
PATCH v2 1/1 powerpc: i2c-mpc: make I2C bus speed configurable
This patch makes the I2C bus speed configurable by using the I2C node property "clock-frequency". If the property is not defined, the old fixed clock settings will be used for backward compatibility.
The generic I2C clock properties, especially the CPU-specific source clock pre-scaler are defined via the OF match table:
static const struct of_device_id mpc_i2c_of_match[] = {
...
{.compatible = "fsl,mpc8543-i2c",
.data = &(struct fsl_i2c_match_data) {
.setclock = mpc_i2c_setclock_8xxx,
.prescaler = 2,
},
},The "data" field defines the relevant I2C setclock function and the relevant prescaler for the I2C source clock frequency.
It uses arch-specific tables and functions to determine resonable Freqency Divider Register (fdr) values for MPC83xx, MPC85xx, MPC86xx, MPC5200 and MPC5200B.
The i2c->flags field and the corresponding FSL_I2C_DEV_* definitions have been removed as they are obsolete.
Furthermore dev_dbg() and dev_info() are now used to profit from a more comprehensive output.
--- Documentation/powerpc/dts-bindings/fsl/i2c.txt | 43 +++++-- arch/powerpc/include/asm/mpc52xx.h | 11 + arch/powerpc/platforms/52xx/mpc52xx_common.c | 57 ++++++++++ arch/powerpc/sysdev/fsl_soc.c | 90 ++++++++++++++++ arch/powerpc/sysdev/fsl_soc.h | 12 ++ drivers/i2c/busses/i2c-mpc.c | 138 ++++++++++++++++++------- include/linux/fsl_devices.h | 9 - 7 files changed, 302 insertions(+), 58 deletions(-)
drivers/i2c/busses/i2c-mpc.c +104 -34
@@ -20,12 +20,15 @@#include <linux/of_platform.h> #include <linux/of_i2c.h> -#include <asm/io.h> +#include <linux/io.h> #include <linux/fsl_devices.h> #include <linux/i2c.h> #include <linux/interrupt.h> #include <linux/delay.h> +#include <asm/mpc52xx.h> +#include <sysdev/fsl_soc.h> +#define DRV_NAME "mpc-i2c"#define MPC_I2C_FDR 0x04
@@ -50,12 +53,19 @@#define CSR_RXAK 0x01struct mpc_i2c { +struct device *dev;void __iomem *base;u32 interrupt;wait_queue_head_t queue;struct i2c_adapter adap;int irq; -u32 flags; +}; + +struct fsl_i2c_match_data { +void (*setclock)(struct device_node *node, +struct mpc_i2c *i2c, +u32 clock,u32 prescaler); +u32 prescaler; };static __inline__void writeccr (struct mpc_i2c *i2c,u32 x)
@@ -105,7 +115,7 @@while (!(readb (i2c->base +MPC_I2C_SR ) &CSR_MIF )) {schedule ();if (time_after (jiffies, orig_jiffies + timeout)) { -pr_debug ("I2C: timeout\n" ); +dev_dbg (i2c->dev,"timeout\n" );writeccr (i2c,0 ); result = -EIO ;break ;
@@ -119,10 +129,10 @@ (i2c->interrupt & CSR_MIF), timeout * HZ);if (unlikely (result <0 )) { -pr_debug ("I2C: wait interrupted\n" ); +dev_dbg (i2c->dev,"wait interrupted\n" );writeccr (i2c,0 ); }else if (unlikely (!(i2c->interrupt &CSR_MIF ))) { -pr_debug ("I2C: wait timeout\n" ); +dev_dbg (i2c->dev,"wait timeout\n" );writeccr (i2c,0 ); result = -ETIMEDOUT ; }
@@ -135,17 +145,17 @@return result;if (!(x &CSR_MCF )) { -pr_debug ("I2C: unfinished\n" ); +dev_dbg (i2c->dev,"unfinished\n" );return -EIO ; }if (x &CSR_MAL ) { -pr_debug ("I2C: MAL\n" ); +dev_dbg (i2c->dev,"MAL\n" );return -EIO ; }if (writing && (x &CSR_RXAK )) { -pr_debug ("I2C: No RXAK\n" ); +dev_dbg (i2c->dev,"No RXAK\n" );/* generate stop */ writeccr (i2c,CCR_MEN );return -EIO ;
@@ -153,16 +163,30 @@return 0 ; } -static void mpc_i2c_setclock (struct mpc_i2c *i2c) -{ -/* Set clock and filters */ -if (i2c->flags &FSL_I2C_DEV_SEPARATE_DFSRR ) { -writeb (0x31 , i2c->base +MPC_I2C_FDR ); -writeb (0x10 , i2c->base +MPC_I2C_DFSRR ); - }else if (i2c->flags &FSL_I2C_DEV_CLOCK_5200 ) -writeb (0x3f , i2c->base +MPC_I2C_FDR ); -else -writel (0x1031 , i2c->base +MPC_I2C_FDR ); +static void mpc_i2c_setclock_52xx (struct device_node *node, +struct mpc_i2c *i2c, +u32 clock,u32 prescaler) +{ +int fdr =mpc52xx_i2c_get_fdr (node, clock, prescaler); + +if (fdr <0 ) + fdr =0x3f ;/* backward compatibility */ +writeb (fdr &0xff , i2c->base +MPC_I2C_FDR ); +dev_info (i2c->dev,"clock %d Hz (fdr=%d)\n" , clock, fdr); +} + +static void mpc_i2c_setclock_8xxx (struct device_node *node, +struct mpc_i2c *i2c, +u32 clock,u32 prescaler) +{ +int fdr =fsl_i2c_get_fdr (node, clock, prescaler); + +if (fdr <0 ) + fdr =0x1031 ;/* backward compatibility */ +writeb (fdr &0xff , i2c->base +MPC_I2C_FDR ); +writeb ((fdr >>8 ) &0xff , i2c->base +MPC_I2C_DFSRR ); +dev_info (i2c->dev,"clock %d Hz (dfsrr=%d fdr=%d)\n" , + clock, fdr >>8 , fdr &0xff ); }static void mpc_i2c_start (struct mpc_i2c *i2c)
@@ -267,12 +291,12 @@/* Allow bus up to 1s to become not busy */ while (readb (i2c->base +MPC_I2C_SR ) &CSR_MBB ) {if (signal_pending (current)) { -pr_debug ("I2C: Interrupted\n" ); +dev_dbg (i2c->dev,"interrupted\n" );writeccr (i2c,0 );return -EINTR ; }if (time_after (jiffies, orig_jiffies +HZ )) { -pr_debug ("I2C: timeout\n" ); +dev_dbg (i2c->dev,"timeout\n" );if (readb (i2c->base +MPC_I2C_SR ) == (CSR_MCF |CSR_MBB |CSR_RXAK ))mpc_i2c_fixup (i2c);
@@ -283,9 +307,10 @@for (i =0 ; ret >=0 && i < num; i++) { pmsg = &msgs[i]; -pr_debug ("Doing %s %d bytes to 0x%02x - %d of %d messages\n" , - pmsg->flags &I2C_M_RD ?"read" :"write" , - pmsg->len, pmsg->addr, i +1 , num); +dev_dbg (i2c->dev, +"doing %s %d bytes to 0x%02x - %d of %d messages\n" , + pmsg->flags &I2C_M_RD ?"read" :"write" , + pmsg->len, pmsg->addr, i +1 , num);if (pmsg->flags &I2C_M_RD ) ret =mpc_read (i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
@@ -316,13 +341,18 @@static int __devinit fsl_i2c_probe (struct of_device *op,const struct of_device_id *match) { -int result =0 ;struct mpc_i2c *i2c; +const u32 *prop; +u32 clock =0 ; +int result =0 ; +int plen; i2c =kzalloc (sizeof (*i2c),GFP_KERNEL );if (!i2c)return -ENOMEM ; + i2c->dev = &op->dev;/* for device printouts */ +init_waitqueue_head (&i2c->queue); i2c->base =of_iomap (op->node,0 );
@@ -343,14 +373,20 @@ }if (!of_get_property (op->node,"fsl,preserve-clocking" ,NULL )) { -if (of_get_property (op->node,"dfsrr" ,NULL )) - i2c->flags |=FSL_I2C_DEV_SEPARATE_DFSRR ; - -if (of_device_is_compatible (op->node,"fsl,mpc5200-i2c" ) || -of_device_is_compatible (op->node,"mpc5200-i2c" )) - i2c->flags |=FSL_I2C_DEV_CLOCK_5200 ; - -mpc_i2c_setclock (i2c); + prop =of_get_property (op->node,"clock-frequency" , &plen); +if (prop && plen ==sizeof (u32)) + clock = *prop; + +if (match->data) { +struct fsl_i2c_match_data *data = + (struct fsl_i2c_match_data *)match->data; + data->setclock (op->node, i2c, clock, data->prescaler); + }else { +/* Backwards compatibility */ +if (of_get_property (op->node,"dfsrr" ,NULL )) +mpc_i2c_setclock_8xxx (op->node, i2c, + clock,0 ); + } }dev_set_drvdata (&op->dev, i2c);
@@ -396,9 +432,43 @@ };static const struct of_device_id mpc_i2c_of_match[] = { - {.compatible ="fsl-i2c" ,}, + {.compatible ="mpc5200-i2c" , + .data = &(struct fsl_i2c_match_data ) { + .setclock = mpc_i2c_setclock_52xx, + }, + }, + {.compatible ="fsl,mpc5200b-i2c" , + .data = &(struct fsl_i2c_match_data ) { + .setclock = mpc_i2c_setclock_52xx, + }, + }, + {.compatible ="fsl,mpc5200-i2c" , + .data = &(struct fsl_i2c_match_data ) { + .setclock = mpc_i2c_setclock_52xx, + }, + }, + {.compatible ="fsl,mpc8313-i2c" , + .data = &(struct fsl_i2c_match_data ) { + .setclock = mpc_i2c_setclock_8xxx, + }, + }, + {.compatible ="fsl,mpc8543-i2c" , + .data = &(struct fsl_i2c_match_data ) { + .setclock = mpc_i2c_setclock_8xxx, + .prescaler =2 , + }, + }, + {.compatible ="fsl,mpc8544-i2c" , + .data = &(struct fsl_i2c_match_data ) { + .setclock = mpc_i2c_setclock_8xxx, + .prescaler =3 , + }, +/* Backward compatibility */ + }, + {.compatible ="fsl-i2c" , }, {}, }; +MODULE_DEVICE_TABLE (of, mpc_i2c_of_match);
@@ -419,7 +489,7 @@ rv =of_register_platform_driver (&mpc_i2c_driver);if (rv) - printk(KERN_ERR DRV_NAME + printk(KERN_ERR DRV_NAME" of_register_platform_driver failed (%i)\n" , rv);return rv; }
arch/powerpc/sysdev/fsl_soc.c +89 -1
102 lines not on this page · raw message
include/linux/fsl_devices.h +0 -9
@@ -60,15 +60,6 @@#define FSL_GIANFAR_BRD_HAS_PHY_INTR 0x00000001/* set or use a timer */ #define FSL_GIANFAR_BRD_IS_REDUCED 0x00000002/* Set if RGMII, RMII */ -struct fsl_i2c_platform_data { -/* device specific information */ -u32 device_flags; -}; - -/* Flags related to I2C device features */ -#define FSL_I2C_DEV_SEPARATE_DFSRR 0x00000001 -#define FSL_I2C_DEV_CLOCK_5200 0x00000002 -enum fsl_usb2_operating_modes {FSL_USB2_MPH_HOST ,FSL_USB2_DR_HOST ,
arch/powerpc/platforms/52xx/mpc52xx_common.c +57 -0
60 lines not on this page · raw message
arch/powerpc/include/asm/mpc52xx.h +11 -0
@@ -287,6 +287,17 @@static inline void mpc52xx_setup_pci (void ) { }#endif +#ifdef CONFIG_PPC_MPC52xx +extern int mpc52xx_i2c_get_fdr (struct device_node *node, +u32 clock,int prescaler); +#else +static inline int mpc52xx_i2c_get_fdr (struct device_node *node, +u32 clock,u32 prescaler) +{ +return -EINVAL ; +} +#endif +#endif /* __ASSEMBLY__ */ #ifdef CONFIG_PM
Documentation/powerpc/dts-bindings/fsl/i2c.txt +29 -14
56 lines not on this page · raw message
arch/powerpc/sysdev/fsl_soc.h +12 -0
@@ -14,6 +14,18 @@#endif extern u32 fsl_get_sys_freq (void ); + +#ifdef CONFIG_FSL_SOC +extern int fsl_i2c_get_fdr (struct device_node *node, +u32 clock,u32 divider); +#else +static inline int fsl_i2c_get_fdr (struct device_node *node, +u32 clock,u32 prescaler) +{ +return -EINVAL ; +} +#endif +struct spi_board_info ;struct device_node ;
Quoted 29 lines · Wolfgang Grandegger
This patch makes the I2C bus speed configurable by using the I2C node property "clock-frequency". If the property is not defined, the old fixed clock settings will be used for backward compatibility.
The generic I2C clock properties, especially the CPU-specific source clock pre-scaler are defined via the OF match table:
static const struct
of_device_idmpc_i2c_of_match[] = {
...
{.compatible = "fsl,mpc8543-i2c",
.data = &(structfsl_i2c_match_data) { .setclock =mpc_i2c_setclock_8xxx, .prescaler = 2,
},
},The "data" field defines the relevant I2C setclock function and the relevant prescaler for the I2C source clock frequency.
It uses arch-specific tables and functions to determine resonable Freqency Divider Register (fdr) values for MPC83xx, MPC85xx, MPC86xx, MPC5200 and MPC5200B.
The
i2c->flagsfield and the corresponding FSL_I2C_DEV_* definitions have been removed as they are obsolete.Furthermore
dev_dbg()anddev_info()are now used to profit from a more comprehensive output.
Quoted 6 lines
Signed-off-by Wolfgang Grandegger <[email protected]>arch/powerpc/include/asm/mpc52xx.h | 11 +
arch/powerpc/platforms/52xx/mpc52xx_common.c| 57 ++++++++++arch/powerpc/sysdev/fsl_soc.c| 90 ++++++++++++++++arch/powerpc/sysdev/fsl_soc.h| 12 ++
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I mostly like this patch. However, as I mentioned in my other reply, I'm going to NAK it because the divisor tables are not common code and do not belong in common code files.
Other than that, I like this version of the patch and I think it is almost finished. More comments below.
Quoted 34 lines
drivers/i2c/busses/i2c-mpc.c +24
-10-static void mpc_i2c_setclock (struct mpc_i2c *i2c) -{ - /* Set clock and filters */ - if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) { - writeb(0x31, i2c->base + MPC_I2C_FDR); - writeb(0x10, i2c->base + MPC_I2C_DFSRR); - } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200) - writeb(0x3f, i2c->base + MPC_I2C_FDR); - else - writel(0x1031, i2c->base + MPC_I2C_FDR); +static void mpc_i2c_setclock_52xx (struct device_node *node, + struct mpc_i2c *i2c, + u32 clock, u32 prescaler) +{ + int fdr = mpc52xx_i2c_get_fdr(node, clock, prescaler); + + if (fdr < 0) + fdr = 0x3f; /* backward compatibility */ + writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR); + dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr); +} + +static void mpc_i2c_setclock_8xxx (struct device_node *node, + struct mpc_i2c *i2c, + u32 clock, u32 prescaler) +{ + int fdr = fsl_i2c_get_fdr(node, clock, prescaler); + + if (fdr < 0) + fdr = 0x1031; /* backward compatibility */ + writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR); + writeb((fdr >> 8) & 0xff, i2c->base + MPC_I2C_DFSRR); + dev_info(i2c->dev, "clock %d Hz (dfsrr=%d fdr=%d)\n", + clock, fdr >> 8, fdr & 0xff);
Quoted 1 line
}
Very neat and tidy indeed. I like this.
arch/powerpc/sysdev/fsl_soc.c 2009-04-01 12:09:18.796719327 +0200 line 42
Quoted 10 lines
Index:
linux-2.6/arch/powerpc/sysdev/fsl_soc.c
===================================================================arch/powerpc/sysdev/fsl_soc.c 2009-04-01 12:09:18.796719327 +0200 renamed from arch/powerpc/sysdev/fsl_soc.c 2009-03-31 21:27:52.000000000 +0200 +1
-1@@ -39,7 +39,7 @@ #include <sysdev/fsl_soc.h> #include <mm/mmu_decl.h> #include <asm/cpm2.h> - +#define DEBUG
^^^^^^^^^^^^^^^^^^^^^^^ oops?
g.
Signature · 2 lines
Grant Likely, B.Sc., P.Eng. Secret Lab Technologies Ltd.
Quoted 11 lines · Grant Likely
Quoted 3 lines · Wolfgang Grandegger
This patch makes the I2C bus speed configurable by using the I2C node property "clock-frequency". If the property is not defined, the old fixed clock settings will be used for backward compatibility.
I mostly like this patch. However, as I mentioned in my other reply, I'm going to NAK it because the divisor tables are not common code and do not belong in common code files.
Other than that, I like this version of the patch and I think it is almost finished. More comments below.
Oh, and you should look in MAINTAINERS and cc this patch to Ben Dooks and the [email protected] mailing list.
g.
Quoted 57 lines
Quoted 35 lines
drivers/i2c/busses/i2c-mpc.c +24
-10-static void mpc_i2c_setclock (struct mpc_i2c *i2c) -{ - /* Set clock and filters */ - if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) { - writeb(0x31, i2c->base + MPC_I2C_FDR); - writeb(0x10, i2c->base + MPC_I2C_DFSRR); - } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200) - writeb(0x3f, i2c->base + MPC_I2C_FDR); - else - writel(0x1031, i2c->base + MPC_I2C_FDR); +static void mpc_i2c_setclock_52xx (struct device_node *node, + struct mpc_i2c *i2c, + u32 clock, u32 prescaler) +{ + int fdr = mpc52xx_i2c_get_fdr(node, clock, prescaler); + + if (fdr < 0) + fdr = 0x3f; /* backward compatibility */ + writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR); + dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr); +} + +static void mpc_i2c_setclock_8xxx (struct device_node *node, + struct mpc_i2c *i2c, + u32 clock, u32 prescaler) +{ + int fdr = fsl_i2c_get_fdr(node, clock, prescaler); + + if (fdr < 0) + fdr = 0x1031; /* backward compatibility */ + writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR); + writeb((fdr >> 8) & 0xff, i2c->base + MPC_I2C_DFSRR); + dev_info(i2c->dev, "clock %d Hz (dfsrr=%d fdr=%d)\n", + clock, fdr >> 8, fdr & 0xff);Quoted 1 line
}
Very neat and tidy indeed. I like this.
arch/powerpc/sysdev/fsl_soc.c 2009-04-01 12:09:18.796719327 +0200 line 42
Quoted 10 lines
Index:
linux-2.6/arch/powerpc/sysdev/fsl_soc.c
===================================================================arch/powerpc/sysdev/fsl_soc.c 2009-04-01 12:09:18.796719327 +0200 renamed from arch/powerpc/sysdev/fsl_soc.c 2009-03-31 21:27:52.000000000 +0200 +1
-1@@ -39,7 +39,7 @@ #include <sysdev/fsl_soc.h> #include <mm/mmu_decl.h> #include <asm/cpm2.h> - +#define DEBUG^^^^^^^^^^^^^^^^^^^^^^^ oops?
g.
--
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
Signature · 2 lines
Grant Likely, B.Sc., P.Eng. Secret Lab Technologies Ltd.
Quoted 11 lines · Grant Likely
Quoted 10 lines · Grant Likely
Quoted 3 lines · Wolfgang Grandegger
This patch makes the I2C bus speed configurable by using the I2C node property "clock-frequency". If the property is not defined, the old fixed clock settings will be used for backward compatibility.
I mostly like this patch. However, as I mentioned in my other reply, I'm going to NAK it because the divisor tables are not common code and do not belong in common code files.
Other than that, I like this version of the patch and I think it is almost finished. More comments below.
Looks good now, indeed.
Quoted 2 lines
Oh, and you should look in
MAINTAINERSand cc this patch to Ben Dooks and the [email protected] mailing list.
Right, and I need to separate I2C and PowerPC related patches. And it will require another round but some acked-by lines will help.
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
drivers/i2c/busses/i2c-mpc.c +18
-10-static void mpc_i2c_setclock (struct mpc_i2c *i2c) -{ -/* Set clock and filters */ -if (i2c->flags &FSL_I2C_DEV_SEPARATE_DFSRR ) { -writeb (0x31 , i2c->base +MPC_I2C_FDR ); -writeb (0x10 , i2c->base +MPC_I2C_DFSRR ); - }else if (i2c->flags &FSL_I2C_DEV_CLOCK_5200 ) -writeb (0x3f , i2c->base +MPC_I2C_FDR ); -else -writel (0x1031 , i2c->base +MPC_I2C_FDR ); +static void mpc_i2c_setclock_52xx (struct device_node *node, +struct mpc_i2c *i2c, +u32 clock,u32 prescaler) +{ +int fdr =mpc52xx_i2c_get_fdr (node, clock, prescaler); + +if (fdr <0 ) + fdr =0x3f ;/* backward compatibility */ +writeb (fdr &0xff , i2c->base +MPC_I2C_FDR ); +dev_info (i2c->dev,"clock %d Hz (fdr=%d)\n" , clock, fdr); +} + +static void mpc_i2c_setclock_8xxx (struct device_node *node, +struct mpc_i2c *i2c, +u32 clock,u32 prescaler) +{ +int fdr =fsl_i2c_get_fdr (node, clock, prescaler); +
Quoted 8 lines
Quoted 8 lines
drivers/i2c/busses/i2c-mpc.c line 190
Quoted 7 lines
drivers/i2c/busses/i2c-mpc.c +6
-0+if (fdr <0 ) + fdr =0x1031 ;/* backward compatibility */ +writeb (fdr &0xff , i2c->base +MPC_I2C_FDR ); +writeb ((fdr >>8 ) &0xff , i2c->base +MPC_I2C_DFSRR ); +dev_info (i2c->dev,"clock %d Hz (dfsrr=%d fdr=%d)\n" , + clock, fdr >>8 , fdr &0xff ); }Very neat and tidy indeed. I like this.
Yep.
Quoted 11 lines
Quoted 11 lines
arch/powerpc/sysdev/fsl_soc.c 2009-04-01 12:09:18.796719327 +0200 line 42
Quoted 10 lines
Index:
linux-2.6/arch/powerpc/sysdev/fsl_soc.c
===================================================================arch/powerpc/sysdev/fsl_soc.c 2009-04-01 12:09:18.796719327 +0200 renamed from arch/powerpc/sysdev/fsl_soc.c 2009-03-31 21:27:52.000000000 +0200 +1
-1@@ -39,7 +39,7 @@ #include <sysdev/fsl_soc.h> #include <mm/mmu_decl.h> #include <asm/cpm2.h> - +#define DEBUG^^^^^^^^^^^^^^^^^^^^^^^ oops?
Will fix.
Wolfgang.