diff options
Diffstat (limited to 'drivers/i2c/busses/i2c-mpc.c')
| -rw-r--r-- | drivers/i2c/busses/i2c-mpc.c | 222 | 
1 files changed, 158 insertions, 64 deletions
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c index b74e6dc6886..6a32aa095f8 100644 --- a/drivers/i2c/busses/i2c-mpc.c +++ b/drivers/i2c/busses/i2c-mpc.c @@ -16,11 +16,12 @@  #include <linux/kernel.h>  #include <linux/module.h>  #include <linux/sched.h> -#include <linux/init.h> +#include <linux/of_address.h> +#include <linux/of_irq.h>  #include <linux/of_platform.h> -#include <linux/of_i2c.h>  #include <linux/slab.h> +#include <linux/clk.h>  #include <linux/io.h>  #include <linux/fsl_devices.h>  #include <linux/i2c.h> @@ -64,6 +65,10 @@ struct mpc_i2c {  	struct i2c_adapter adap;  	int irq;  	u32 real_clk; +#ifdef CONFIG_PM_SLEEP +	u8 fdr, dfsrr; +#endif +	struct clk *clk_per;  };  struct mpc_i2c_divider { @@ -110,7 +115,7 @@ static void mpc_i2c_fixup(struct mpc_i2c *i2c)  	for (k = 9; k; k--) {  		writeccr(i2c, 0);  		writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN); -		udelay(delay_val); +		readb(i2c->base + MPC_I2C_DR);  		writeccr(i2c, CCR_MEN);  		udelay(delay_val << 1);  	} @@ -172,7 +177,7 @@ static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)  }  #if defined(CONFIG_PPC_MPC52xx) || defined(CONFIG_PPC_MPC512x) -static const struct mpc_i2c_divider mpc_i2c_dividers_52xx[] __devinitconst = { +static const struct mpc_i2c_divider mpc_i2c_dividers_52xx[] = {  	{20, 0x20}, {22, 0x21}, {24, 0x22}, {26, 0x23},  	{28, 0x24}, {30, 0x01}, {32, 0x25}, {34, 0x02},  	{36, 0x26}, {40, 0x27}, {44, 0x04}, {48, 0x28}, @@ -193,7 +198,7 @@ static const struct mpc_i2c_divider mpc_i2c_dividers_52xx[] __devinitconst = {  	{10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}  }; -static int __devinit mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, +static int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock,  					  int prescaler, u32 *real_clk)  {  	const struct mpc_i2c_divider *div = NULL; @@ -227,7 +232,7 @@ static int __devinit mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock,  	return (int)div->fdr;  } -static void __devinit mpc_i2c_setup_52xx(struct device_node *node, +static void mpc_i2c_setup_52xx(struct device_node *node,  					 struct mpc_i2c *i2c,  					 u32 clock, u32 prescaler)  { @@ -249,7 +254,7 @@ static void __devinit mpc_i2c_setup_52xx(struct device_node *node,  			 fdr);  }  #else /* !(CONFIG_PPC_MPC52xx || CONFIG_PPC_MPC512x) */ -static void __devinit mpc_i2c_setup_52xx(struct device_node *node, +static void mpc_i2c_setup_52xx(struct device_node *node,  					 struct mpc_i2c *i2c,  					 u32 clock, u32 prescaler)  { @@ -257,7 +262,7 @@ static void __devinit mpc_i2c_setup_52xx(struct device_node *node,  #endif /* CONFIG_PPC_MPC52xx || CONFIG_PPC_MPC512x */  #ifdef CONFIG_PPC_MPC512x -static void __devinit mpc_i2c_setup_512x(struct device_node *node, +static void mpc_i2c_setup_512x(struct device_node *node,  					 struct mpc_i2c *i2c,  					 u32 clock, u32 prescaler)  { @@ -285,7 +290,7 @@ static void __devinit mpc_i2c_setup_512x(struct device_node *node,  	mpc_i2c_setup_52xx(node, i2c, clock, prescaler);  }  #else /* CONFIG_PPC_MPC512x */ -static void __devinit mpc_i2c_setup_512x(struct device_node *node, +static void mpc_i2c_setup_512x(struct device_node *node,  					 struct mpc_i2c *i2c,  					 u32 clock, u32 prescaler)  { @@ -293,7 +298,7 @@ static void __devinit mpc_i2c_setup_512x(struct device_node *node,  #endif /* CONFIG_PPC_MPC512x */  #ifdef CONFIG_FSL_SOC -static const struct mpc_i2c_divider mpc_i2c_dividers_8xxx[] __devinitconst = { +static const struct mpc_i2c_divider mpc_i2c_dividers_8xxx[] = {  	{160, 0x0120}, {192, 0x0121}, {224, 0x0122}, {256, 0x0123},  	{288, 0x0100}, {320, 0x0101}, {352, 0x0601}, {384, 0x0102},  	{416, 0x0602}, {448, 0x0126}, {480, 0x0103}, {512, 0x0127}, @@ -313,7 +318,7 @@ static const struct mpc_i2c_divider mpc_i2c_dividers_8xxx[] __devinitconst = {  	{49152, 0x011e}, {61440, 0x011f}  }; -static u32 __devinit mpc_i2c_get_sec_cfg_8xxx(void) +static u32 mpc_i2c_get_sec_cfg_8xxx(void)  {  	struct device_node *node = NULL;  	u32 __iomem *reg; @@ -342,7 +347,7 @@ static u32 __devinit mpc_i2c_get_sec_cfg_8xxx(void)  	return val;  } -static int __devinit mpc_i2c_get_fdr_8xxx(struct device_node *node, u32 clock, +static int mpc_i2c_get_fdr_8xxx(struct device_node *node, u32 clock,  					  u32 prescaler, u32 *real_clk)  {  	const struct mpc_i2c_divider *div = NULL; @@ -380,7 +385,7 @@ static int __devinit mpc_i2c_get_fdr_8xxx(struct device_node *node, u32 clock,  	return div ? (int)div->fdr : -EINVAL;  } -static void __devinit mpc_i2c_setup_8xxx(struct device_node *node, +static void mpc_i2c_setup_8xxx(struct device_node *node,  					 struct mpc_i2c *i2c,  					 u32 clock, u32 prescaler)  { @@ -405,7 +410,7 @@ static void __devinit mpc_i2c_setup_8xxx(struct device_node *node,  }  #else /* !CONFIG_FSL_SOC */ -static void __devinit mpc_i2c_setup_8xxx(struct device_node *node, +static void mpc_i2c_setup_8xxx(struct device_node *node,  					 struct mpc_i2c *i2c,  					 u32 clock, u32 prescaler)  { @@ -454,7 +459,7 @@ static int mpc_write(struct mpc_i2c *i2c, int target,  }  static int mpc_read(struct mpc_i2c *i2c, int target, -		    u8 *data, int length, int restart) +		    u8 *data, int length, int restart, bool recv_len)  {  	unsigned timeout = i2c->adap.timeout;  	int i, result; @@ -470,7 +475,7 @@ static int mpc_read(struct mpc_i2c *i2c, int target,  		return result;  	if (length) { -		if (length == 1) +		if (length == 1 && !recv_len)  			writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);  		else  			writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA); @@ -479,17 +484,46 @@ static int mpc_read(struct mpc_i2c *i2c, int target,  	}  	for (i = 0; i < length; i++) { +		u8 byte; +  		result = i2c_wait(i2c, timeout, 0);  		if (result < 0)  			return result; -		/* Generate txack on next to last byte */ -		if (i == length - 2) -			writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK); -		/* Do not generate stop on last byte */ -		if (i == length - 1) -			writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX); -		data[i] = readb(i2c->base + MPC_I2C_DR); +		/* +		 * For block reads, we have to know the total length (1st byte) +		 * before we can determine if we are done. +		 */ +		if (i || !recv_len) { +			/* Generate txack on next to last byte */ +			if (i == length - 2) +				writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA +					 | CCR_TXAK); +			/* Do not generate stop on last byte */ +			if (i == length - 1) +				writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA +					 | CCR_MTX); +		} + +		byte = readb(i2c->base + MPC_I2C_DR); + +		/* +		 * Adjust length if first received byte is length. +		 * The length is 1 length byte plus actually data length +		 */ +		if (i == 0 && recv_len) { +			if (byte == 0 || byte > I2C_SMBUS_BLOCK_MAX) +				return -EPROTO; +			length += byte; +			/* +			 * For block reads, generate txack here if data length +			 * is 1 byte (total length is 2 bytes). +			 */ +			if (length == 2) +				writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA +					 | CCR_TXAK); +		} +		data[i] = byte;  	}  	return length; @@ -532,20 +566,42 @@ static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)  			"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); -		else +		if (pmsg->flags & I2C_M_RD) { +			bool recv_len = pmsg->flags & I2C_M_RECV_LEN; + +			ret = mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i, +				       recv_len); +			if (recv_len && ret > 0) +				pmsg->len = ret; +		} else {  			ret =  			    mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i); +		} +	} +	mpc_i2c_stop(i2c); /* Initiate STOP */ +	orig_jiffies = jiffies; +	/* Wait until STOP is seen, allow up to 1 s */ +	while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) { +		if (time_after(jiffies, orig_jiffies + HZ)) { +			u8 status = readb(i2c->base + MPC_I2C_SR); + +			dev_dbg(i2c->dev, "timeout\n"); +			if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) { +				writeb(status & ~CSR_MAL, +				       i2c->base + MPC_I2C_SR); +				mpc_i2c_fixup(i2c); +			} +			return -EIO; +		} +		cond_resched();  	} -	mpc_i2c_stop(i2c);  	return (ret < 0) ? ret : num;  }  static u32 mpc_functionality(struct i2c_adapter *adap)  { -	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; +	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL +	  | I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL;  }  static const struct i2c_algorithm mpc_algo = { @@ -555,19 +611,26 @@ static const struct i2c_algorithm mpc_algo = {  static struct i2c_adapter mpc_ops = {  	.owner = THIS_MODULE, -	.name = "MPC adapter",  	.algo = &mpc_algo,  	.timeout = HZ,  }; -static int __devinit fsl_i2c_probe(struct platform_device *op, -				   const struct of_device_id *match) +static const struct of_device_id mpc_i2c_of_match[]; +static int fsl_i2c_probe(struct platform_device *op)  { +	const struct of_device_id *match;  	struct mpc_i2c *i2c;  	const u32 *prop;  	u32 clock = MPC_I2C_CLOCK_LEGACY;  	int result = 0;  	int plen; +	struct resource res; +	struct clk *clk; +	int err; + +	match = of_match_device(mpc_i2c_of_match, &op->dev); +	if (!match) +		return -EINVAL;  	i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);  	if (!i2c) @@ -594,6 +657,21 @@ static int __devinit fsl_i2c_probe(struct platform_device *op,  		}  	} +	/* +	 * enable clock for the I2C peripheral (non fatal), +	 * keep a reference upon successful allocation +	 */ +	clk = devm_clk_get(&op->dev, NULL); +	if (!IS_ERR(clk)) { +		err = clk_prepare_enable(clk); +		if (err) { +			dev_err(&op->dev, "failed to enable clock\n"); +			goto fail_request; +		} else { +			i2c->clk_per = clk; +		} +	} +  	if (of_get_property(op->dev.of_node, "fsl,preserve-clocking", NULL)) {  		clock = MPC_I2C_CLOCK_PRESERVE;  	} else { @@ -604,7 +682,7 @@ static int __devinit fsl_i2c_probe(struct platform_device *op,  	}  	if (match->data) { -		struct mpc_i2c_data *data = match->data; +		const struct mpc_i2c_data *data = match->data;  		data->setup(op->dev.of_node, i2c, clock, data->prescaler);  	} else {  		/* Backwards compatibility */ @@ -620,9 +698,12 @@ static int __devinit fsl_i2c_probe(struct platform_device *op,  	}  	dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ); -	dev_set_drvdata(&op->dev, i2c); +	platform_set_drvdata(op, i2c);  	i2c->adap = mpc_ops; +	of_address_to_resource(op->dev.of_node, 0, &res); +	scnprintf(i2c->adap.name, sizeof(i2c->adap.name), +		  "MPC adapter at 0x%llx", (unsigned long long)res.start);  	i2c_set_adapdata(&i2c->adap, i2c);  	i2c->adap.dev.parent = &op->dev;  	i2c->adap.dev.of_node = of_node_get(op->dev.of_node); @@ -632,12 +713,12 @@ static int __devinit fsl_i2c_probe(struct platform_device *op,  		dev_err(i2c->dev, "failed to add adapter\n");  		goto fail_add;  	} -	of_i2c_register_devices(&i2c->adap);  	return result;   fail_add: -	dev_set_drvdata(&op->dev, NULL); +	if (i2c->clk_per) +		clk_disable_unprepare(i2c->clk_per);  	free_irq(i2c->irq, i2c);   fail_request:  	irq_dispose_mapping(i2c->irq); @@ -647,12 +728,14 @@ static int __devinit fsl_i2c_probe(struct platform_device *op,  	return result;  }; -static int __devexit fsl_i2c_remove(struct platform_device *op) +static int fsl_i2c_remove(struct platform_device *op)  { -	struct mpc_i2c *i2c = dev_get_drvdata(&op->dev); +	struct mpc_i2c *i2c = platform_get_drvdata(op);  	i2c_del_adapter(&i2c->adap); -	dev_set_drvdata(&op->dev, NULL); + +	if (i2c->clk_per) +		clk_disable_unprepare(i2c->clk_per);  	if (i2c->irq)  		free_irq(i2c->irq, i2c); @@ -663,24 +746,51 @@ static int __devexit fsl_i2c_remove(struct platform_device *op)  	return 0;  }; -static struct mpc_i2c_data mpc_i2c_data_512x __devinitdata = { +#ifdef CONFIG_PM_SLEEP +static int mpc_i2c_suspend(struct device *dev) +{ +	struct mpc_i2c *i2c = dev_get_drvdata(dev); + +	i2c->fdr = readb(i2c->base + MPC_I2C_FDR); +	i2c->dfsrr = readb(i2c->base + MPC_I2C_DFSRR); + +	return 0; +} + +static int mpc_i2c_resume(struct device *dev) +{ +	struct mpc_i2c *i2c = dev_get_drvdata(dev); + +	writeb(i2c->fdr, i2c->base + MPC_I2C_FDR); +	writeb(i2c->dfsrr, i2c->base + MPC_I2C_DFSRR); + +	return 0; +} + +static SIMPLE_DEV_PM_OPS(mpc_i2c_pm_ops, mpc_i2c_suspend, mpc_i2c_resume); +#define MPC_I2C_PM_OPS	(&mpc_i2c_pm_ops) +#else +#define MPC_I2C_PM_OPS	NULL +#endif + +static const struct mpc_i2c_data mpc_i2c_data_512x = {  	.setup = mpc_i2c_setup_512x,  }; -static struct mpc_i2c_data mpc_i2c_data_52xx __devinitdata = { +static const struct mpc_i2c_data mpc_i2c_data_52xx = {  	.setup = mpc_i2c_setup_52xx,  }; -static struct mpc_i2c_data mpc_i2c_data_8313 __devinitdata = { +static const struct mpc_i2c_data mpc_i2c_data_8313 = {  	.setup = mpc_i2c_setup_8xxx,  }; -static struct mpc_i2c_data mpc_i2c_data_8543 __devinitdata = { +static const struct mpc_i2c_data mpc_i2c_data_8543 = {  	.setup = mpc_i2c_setup_8xxx,  	.prescaler = 2,  }; -static struct mpc_i2c_data mpc_i2c_data_8544 __devinitdata = { +static const struct mpc_i2c_data mpc_i2c_data_8544 = {  	.setup = mpc_i2c_setup_8xxx,  	.prescaler = 3,  }; @@ -700,34 +810,18 @@ static const struct of_device_id mpc_i2c_of_match[] = {  MODULE_DEVICE_TABLE(of, mpc_i2c_of_match);  /* Structure for a device driver */ -static struct of_platform_driver mpc_i2c_driver = { +static struct platform_driver mpc_i2c_driver = {  	.probe		= fsl_i2c_probe, -	.remove		= __devexit_p(fsl_i2c_remove), +	.remove		= fsl_i2c_remove,  	.driver = {  		.owner = THIS_MODULE,  		.name = DRV_NAME,  		.of_match_table = mpc_i2c_of_match, +		.pm = MPC_I2C_PM_OPS,  	},  }; -static int __init fsl_i2c_init(void) -{ -	int rv; - -	rv = of_register_platform_driver(&mpc_i2c_driver); -	if (rv) -		printk(KERN_ERR DRV_NAME -		       " of_register_platform_driver failed (%i)\n", rv); -	return rv; -} - -static void __exit fsl_i2c_exit(void) -{ -	of_unregister_platform_driver(&mpc_i2c_driver); -} - -module_init(fsl_i2c_init); -module_exit(fsl_i2c_exit); +module_platform_driver(mpc_i2c_driver);  MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");  MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "  | 
