Skip to contentSkip to conversation

i2c: add bcm2835 driver

PATCH Superseded Stephen Warren 2 participants 4 messages Feb 9, 2013 – Feb 12, 2013

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 i2c: add bcm2835 driver

This implements a very basic I2C host driver for the BCM2835 SoC. Missing features so far are:

* 10-bit addressing.
* DMA.

---
v3:
* Return i2c_add_numbered_adapter() from probe() directly. v2:
* Implemented clock divider configuration based on desired bus rate.
* Make use of module_platform_driver().
* Removed use of devinit.

---
 .../devicetree/bindings/i2c/brcm,bcm2835-i2c.txt   |   20 ++
 drivers/i2c/busses/Kconfig                         |   12 +
 drivers/i2c/busses/Makefile                        |    1 +
 drivers/i2c/busses/i2c-bcm2835.c                   |  340 ++++++++++++++++++++
 4 files changed, 373 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/brcm,bcm2835-i2c.txt
 create mode 100644 drivers/i2c/busses/i2c-bcm2835.c
Documentation/devicetree/bindings/i2c/brcm,bcm2835-i2c.txt new file +20 -0
new file mode 100644
index 0000000..e9de375
@@ -0,0 +1,20 @@
+Broadcom BCM2835 I2C controller
+
+Required properties:
+- compatible : Should be "brcm,bcm2835-i2c".
+- reg: Should contain register location and length.
+- interrupts: Should contain interrupt.
+- clocks : The clock feeding the I2C controller.
+
+Recommended properties:
+- clock-frequency : desired I2C bus clock frequency in Hz.
+
+Example:
+
+i2c at 20205000 {
+	compatible = "brcm,bcm2835-i2c";
+	reg = <0x7e205000 0x1000>;
+	interrupts = <2 21>;
+	clocks = <&clk_i2c>;
+	clock-frequency = <100000>;
+};
drivers/i2c/busses/Kconfig +12 -0
index 8bb810e..3cc40ab 100644
@@ -319,6 +319,18 @@ config I2C_AU1550
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-au1550.
 
+config I2C_BCM2835
+	tristate "Broadcom BCM2835 I2C controller"
+	depends on ARCH_BCM2835
+	help
+	  If you say yes to this option, support will be included for the
+	  BCM2835 I2C controller.
+
+	  If you don't know what to do here, say N.
+
+	  This support is also available as a module.  If so, the module
+	  will be called i2c-bcm2835.
+
 config I2C_BLACKFIN_TWI
 	tristate "Blackfin TWI I2C support"
 	depends on BLACKFIN
drivers/i2c/busses/Makefile +1 -0
index 6181f3f..a52a891 100644
@@ -30,6 +30,7 @@ obj-$(CONFIG_I2C_POWERMAC)	+= i2c-powermac.o
 # Embedded system I2C/SMBus host controller drivers
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
+obj-$(CONFIG_I2C_BCM2835)	+= i2c-bcm2835.o
 obj-$(CONFIG_I2C_BLACKFIN_TWI)	+= i2c-bfin-twi.o
 obj-$(CONFIG_I2C_CBUS_GPIO)	+= i2c-cbus-gpio.o
 obj-$(CONFIG_I2C_CPM)		+= i2c-cpm.o
drivers/i2c/busses/i2c-bcm2835.c new file +340 -0

340 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v3 1/1 i2c: add bcm2835 driver

Hi Stephen,

Quoted 9 lines · Stephen Warren

This implements a very basic I2C host driver for the BCM2835 SoC. Missing features so far are:

* 10-bit addressing.
* DMA.

---

...

Quoted 11 lines
drivers/i2c/busses/i2c-bcm2835.c new file +11 -0
+struct bcm2835_i2c_dev {
+	struct device *dev;
+	void __iomem *regs;
+	struct clk *clk;
+	struct i2c_adapter adapter;
+	struct completion completion;
+	u32 msg_err;
+	u8 *msg_buf;
+	size_t msg_buf_remaining;
+};
+

drivers/i2c/busses/i2c-bcm2835.c line 75

Quoted 10 lines
drivers/i2c/busses/i2c-bcm2835.c new file +10 -0
+static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
+				      u32 reg, u32 val)
+{
+	writel(val, i2c_dev->regs + reg);
+}
+
+static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
+{
+	return readl(i2c_dev->regs + reg);
+}

Hmm, not sure if those functions add readability, but no need to change.

Quoted 6 lines
drivers/i2c/busses/i2c-bcm2835.c new file +6 -0
+
+static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
+{
+	u32 val;
+
+	for (;;) {

drivers/i2c/busses/i2c-bcm2835.c line 91

Quoted 10 lines
drivers/i2c/busses/i2c-bcm2835.c new file +10 -0
+		if (!i2c_dev->msg_buf_remaining)
+			return;
+		val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
+		if (!(val & BCM2835_I2C_S_TXD))
+			break;
+		bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
+				   *i2c_dev->msg_buf);
+		i2c_dev->msg_buf++;
+		i2c_dev->msg_buf_remaining--;
+	}

while (i2c_dev->msg_buf_remaining) ...

?

Quoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c new file +7 -0
+}
+
+static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
+{
+	u32 val;
+
+	for (;;) {

drivers/i2c/busses/i2c-bcm2835.c line 108

Quoted 10 lines
drivers/i2c/busses/i2c-bcm2835.c new file +10 -0
+		if (!i2c_dev->msg_buf_remaining)
+			return;
+		val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
+		if (!(val & BCM2835_I2C_S_RXD))
+			break;
+		*i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
+						      BCM2835_I2C_FIFO);
+		i2c_dev->msg_buf++;
+		i2c_dev->msg_buf_remaining--;
+	}

ditto?

...

drivers/i2c/busses/i2c-bcm2835.c line 179

Quoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c new file +7 -0
+	ret = wait_for_completion_timeout(&i2c_dev->completion,
+					  BCM2835_I2C_TIMEOUT);
+	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
+	if (WARN_ON(ret == 0)) {
+		dev_err(i2c_dev->dev, "i2c transfer timed out\n");
+		return -ETIMEDOUT;
+	}

I'd suggest to skip the WARN_ON. Timeout is the expected case when you want to read from an EEPROM which is just in the process of erasing/writing data from the previous command.

...

drivers/i2c/busses/i2c-bcm2835.c line 300

Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c new file +4 -0
+	adap = &i2c_dev->adapter;
+	i2c_set_adapdata(adap, i2c_dev);
+	adap->owner = THIS_MODULE;
+	adap->class = I2C_CLASS_HWMON;

Do you really need the class?

drivers/i2c/busses/i2c-bcm2835.c line 304

Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c new file +4 -0
+	strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
+	adap->algo = &bcm2835_i2c_algo;
+	adap->dev.parent = &pdev->dev;
+	adap->nr = -1;

If you are using '-1' anyhow, you could skip setting it and simply call i2c_add_adapter(). Or you can init it here to pdev->id. Whatever you prefer.

drivers/i2c/busses/i2c-bcm2835.c line 315

Quoted 11 lines
drivers/i2c/busses/i2c-bcm2835.c new file +11 -0
+
+	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
+
+	return i2c_add_numbered_adapter(adap);
+}
+
+static int bcm2835_i2c_remove(struct platform_device *pdev)
+{
+	struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
+
+	i2c_del_adapter(&i2c_dev->adapter);

Is it guaranteed that interrupts cannot occur anymore? Otherwise OOPS might happen, since the adapter is cleared here already but devm will remove the interrupt only a little later.

Thanks,

Wolfram

#2 lore raw

PATCH v3 1/1 i2c: add bcm2835 driver

Quoted 8 lines · Wolfram Sang

Hi Stephen,

Quoted 5 lines · Stephen Warren

This implements a very basic I2C host driver for the BCM2835 SoC. Missing features so far are:

* 10-bit addressing.
* DMA.

...

Quoted 11 lines

drivers/i2c/busses/i2c-bcm2835.c line 179

Quoted 7 lines
drivers/i2c/busses/i2c-bcm2835.c new file +7 -0
+	ret = wait_for_completion_timeout(&i2c_dev->completion,
+					  BCM2835_I2C_TIMEOUT);
+	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
+	if (WARN_ON(ret == 0)) {
+		dev_err(i2c_dev->dev, "i2c transfer timed out\n");
+		return -ETIMEDOUT;
+	}

I'd suggest to skip the WARN_ON. Timeout is the expected case when you want to read from an EEPROM which is just in the process of erasing/writing data from the previous command.

I copied that from Tegra. Should that driver be changed too?

Quoted 6 lines

drivers/i2c/busses/i2c-bcm2835.c line 300

Quoted 4 lines
drivers/i2c/busses/i2c-bcm2835.c new file +4 -0
+	adap = &i2c_dev->adapter;
+	i2c_set_adapdata(adap, i2c_dev);
+	adap->owner = THIS_MODULE;
+	adap->class = I2C_CLASS_HWMON;

Do you really need the class?

If I grep for I2C_CLASS_HWMON, I see a ton of hits. I think I'll keep it unless you strongly object, since it seems typical.

I'll fix up the other points you mentioned.

#3 lore raw

PATCH v3 1/1 i2c: add bcm2835 driver

Quoted 13 lines
Quoted 12 lines

drivers/i2c/busses/i2c-bcm2835.c line 179

Quoted 8 lines
drivers/i2c/busses/i2c-bcm2835.c new file +7 -0
+	ret = wait_for_completion_timeout(&i2c_dev->completion,
+					  BCM2835_I2C_TIMEOUT);
+	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
+	if (WARN_ON(ret == 0)) {
+		dev_err(i2c_dev->dev, "i2c transfer timed out\n");
+		return -ETIMEDOUT;
+	}

I'd suggest to skip the WARN_ON. Timeout is the expected case when you want to read from an EEPROM which is just in the process of erasing/writing data from the previous command.

Quoted 1 line

I copied that from Tegra. Should that driver be changed too?

The suggestion applies to all drivers, yes :)