2 * BCM2835 master mode driver
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #define BCM2835_I2C_C 0x0
25 #define BCM2835_I2C_S 0x4
26 #define BCM2835_I2C_DLEN 0x8
27 #define BCM2835_I2C_A 0xc
28 #define BCM2835_I2C_FIFO 0x10
29 #define BCM2835_I2C_DIV 0x14
30 #define BCM2835_I2C_DEL 0x18
32 * 16-bit field for the number of SCL cycles to wait after rising SCL
33 * before deciding the slave is not responding. 0 disables the
36 #define BCM2835_I2C_CLKT 0x1c
38 #define BCM2835_I2C_C_READ BIT(0)
39 #define BCM2835_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */
40 #define BCM2835_I2C_C_ST BIT(7)
41 #define BCM2835_I2C_C_INTD BIT(8)
42 #define BCM2835_I2C_C_INTT BIT(9)
43 #define BCM2835_I2C_C_INTR BIT(10)
44 #define BCM2835_I2C_C_I2CEN BIT(15)
46 #define BCM2835_I2C_S_TA BIT(0)
47 #define BCM2835_I2C_S_DONE BIT(1)
48 #define BCM2835_I2C_S_TXW BIT(2)
49 #define BCM2835_I2C_S_RXR BIT(3)
50 #define BCM2835_I2C_S_TXD BIT(4)
51 #define BCM2835_I2C_S_RXD BIT(5)
52 #define BCM2835_I2C_S_TXE BIT(6)
53 #define BCM2835_I2C_S_RXF BIT(7)
54 #define BCM2835_I2C_S_ERR BIT(8)
55 #define BCM2835_I2C_S_CLKT BIT(9)
56 #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */
58 #define BCM2835_I2C_BITMSK_S 0x03FF
60 #define BCM2835_I2C_CDIV_MIN 0x0002
61 #define BCM2835_I2C_CDIV_MAX 0xFFFE
63 #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
65 struct bcm2835_i2c_dev {
70 struct i2c_adapter adapter;
71 struct completion completion;
72 struct i2c_msg *curr_msg;
75 size_t msg_buf_remaining;
78 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
81 writel(val, i2c_dev->regs + reg);
84 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
86 return readl(i2c_dev->regs + reg);
89 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
93 while (i2c_dev->msg_buf_remaining) {
94 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
95 if (!(val & BCM2835_I2C_S_TXD))
97 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
100 i2c_dev->msg_buf_remaining--;
104 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
108 while (i2c_dev->msg_buf_remaining) {
109 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
110 if (!(val & BCM2835_I2C_S_RXD))
112 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
115 i2c_dev->msg_buf_remaining--;
119 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
121 struct bcm2835_i2c_dev *i2c_dev = data;
124 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
125 val &= BCM2835_I2C_BITMSK_S;
126 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val);
128 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
130 i2c_dev->msg_err = err;
131 complete(&i2c_dev->completion);
135 if (val & BCM2835_I2C_S_DONE) {
136 if (!i2c_dev->curr_msg) {
137 dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
138 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
139 bcm2835_drain_rxfifo(i2c_dev);
140 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
143 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
144 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
146 i2c_dev->msg_err = 0;
147 complete(&i2c_dev->completion);
151 if (val & BCM2835_I2C_S_TXW) {
152 bcm2835_fill_txfifo(i2c_dev);
156 if (val & BCM2835_I2C_S_RXR) {
157 bcm2835_drain_rxfifo(i2c_dev);
164 static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
168 unsigned long time_left;
170 i2c_dev->curr_msg = msg;
171 i2c_dev->msg_buf = msg->buf;
172 i2c_dev->msg_buf_remaining = msg->len;
173 reinit_completion(&i2c_dev->completion);
175 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
177 if (msg->flags & I2C_M_RD) {
178 c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
180 c = BCM2835_I2C_C_INTT;
181 bcm2835_fill_txfifo(i2c_dev);
183 c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
185 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
186 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
187 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
189 time_left = wait_for_completion_timeout(&i2c_dev->completion,
190 BCM2835_I2C_TIMEOUT);
191 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
193 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
197 if (likely(!i2c_dev->msg_err))
200 if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
201 (msg->flags & I2C_M_IGNORE_NAK))
204 dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
206 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
212 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
215 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
219 for (i = 0; i < num; i++) {
220 ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
228 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
230 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
233 static const struct i2c_algorithm bcm2835_i2c_algo = {
234 .master_xfer = bcm2835_i2c_xfer,
235 .functionality = bcm2835_i2c_func,
239 * This HW was reported to have problems with clock stretching:
240 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
241 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
243 static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
244 .flags = I2C_AQ_NO_CLK_STRETCH,
247 static int bcm2835_i2c_probe(struct platform_device *pdev)
249 struct bcm2835_i2c_dev *i2c_dev;
250 struct resource *mem, *irq;
251 u32 bus_clk_rate, divider;
253 struct i2c_adapter *adap;
255 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
258 platform_set_drvdata(pdev, i2c_dev);
259 i2c_dev->dev = &pdev->dev;
260 init_completion(&i2c_dev->completion);
262 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
263 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
264 if (IS_ERR(i2c_dev->regs))
265 return PTR_ERR(i2c_dev->regs);
267 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
268 if (IS_ERR(i2c_dev->clk)) {
269 if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
270 dev_err(&pdev->dev, "Could not get clock\n");
271 return PTR_ERR(i2c_dev->clk);
274 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
278 "Could not read clock-frequency property\n");
279 bus_clk_rate = 100000;
282 divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
284 * Per the datasheet, the register is always interpreted as an even
285 * number, by rounding down. In other words, the LSB is ignored. So,
286 * if the LSB is set, increment the divider to avoid any issue.
290 if ((divider < BCM2835_I2C_CDIV_MIN) ||
291 (divider > BCM2835_I2C_CDIV_MAX)) {
292 dev_err(&pdev->dev, "Invalid clock-frequency\n");
295 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
297 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
299 dev_err(&pdev->dev, "No IRQ resource\n");
302 i2c_dev->irq = irq->start;
304 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
305 dev_name(&pdev->dev), i2c_dev);
307 dev_err(&pdev->dev, "Could not request IRQ\n");
311 adap = &i2c_dev->adapter;
312 i2c_set_adapdata(adap, i2c_dev);
313 adap->owner = THIS_MODULE;
314 adap->class = I2C_CLASS_DEPRECATED;
315 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
316 adap->algo = &bcm2835_i2c_algo;
317 adap->dev.parent = &pdev->dev;
318 adap->dev.of_node = pdev->dev.of_node;
319 adap->quirks = &bcm2835_i2c_quirks;
322 * Disable the hardware clock stretching timeout. SMBUS
323 * specifies a limit for how long the device can stretch the
324 * clock, but core I2C doesn't.
326 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_CLKT, 0);
327 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
329 ret = i2c_add_adapter(adap);
331 free_irq(i2c_dev->irq, i2c_dev);
336 static int bcm2835_i2c_remove(struct platform_device *pdev)
338 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
340 free_irq(i2c_dev->irq, i2c_dev);
341 i2c_del_adapter(&i2c_dev->adapter);
346 static const struct of_device_id bcm2835_i2c_of_match[] = {
347 { .compatible = "brcm,bcm2835-i2c" },
350 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
352 static struct platform_driver bcm2835_i2c_driver = {
353 .probe = bcm2835_i2c_probe,
354 .remove = bcm2835_i2c_remove,
356 .name = "i2c-bcm2835",
357 .of_match_table = bcm2835_i2c_of_match,
360 module_platform_driver(bcm2835_i2c_driver);
362 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
363 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
364 MODULE_LICENSE("GPL v2");
365 MODULE_ALIAS("platform:i2c-bcm2835");