1 // SPDX-License-Identifier: GPL-2.0
3 * BCM2835 master mode driver
7 #include <linux/clkdev.h>
8 #include <linux/clk-provider.h>
9 #include <linux/completion.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #define BCM2835_I2C_C 0x0
19 #define BCM2835_I2C_S 0x4
20 #define BCM2835_I2C_DLEN 0x8
21 #define BCM2835_I2C_A 0xc
22 #define BCM2835_I2C_FIFO 0x10
23 #define BCM2835_I2C_DIV 0x14
24 #define BCM2835_I2C_DEL 0x18
25 #define BCM2835_I2C_CLKT 0x1c
27 #define BCM2835_I2C_C_READ BIT(0)
28 #define BCM2835_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */
29 #define BCM2835_I2C_C_ST BIT(7)
30 #define BCM2835_I2C_C_INTD BIT(8)
31 #define BCM2835_I2C_C_INTT BIT(9)
32 #define BCM2835_I2C_C_INTR BIT(10)
33 #define BCM2835_I2C_C_I2CEN BIT(15)
35 #define BCM2835_I2C_S_TA BIT(0)
36 #define BCM2835_I2C_S_DONE BIT(1)
37 #define BCM2835_I2C_S_TXW BIT(2)
38 #define BCM2835_I2C_S_RXR BIT(3)
39 #define BCM2835_I2C_S_TXD BIT(4)
40 #define BCM2835_I2C_S_RXD BIT(5)
41 #define BCM2835_I2C_S_TXE BIT(6)
42 #define BCM2835_I2C_S_RXF BIT(7)
43 #define BCM2835_I2C_S_ERR BIT(8)
44 #define BCM2835_I2C_S_CLKT BIT(9)
45 #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */
47 #define BCM2835_I2C_FEDL_SHIFT 16
48 #define BCM2835_I2C_REDL_SHIFT 0
50 #define BCM2835_I2C_CDIV_MIN 0x0002
51 #define BCM2835_I2C_CDIV_MAX 0xFFFE
53 struct bcm2835_i2c_dev {
57 struct i2c_adapter adapter;
58 struct completion completion;
59 struct i2c_msg *curr_msg;
63 size_t msg_buf_remaining;
66 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
69 writel(val, i2c_dev->regs + reg);
72 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
74 return readl(i2c_dev->regs + reg);
77 #define to_clk_bcm2835_i2c(_hw) container_of(_hw, struct clk_bcm2835_i2c, hw)
78 struct clk_bcm2835_i2c {
80 struct bcm2835_i2c_dev *i2c_dev;
83 static int clk_bcm2835_i2c_calc_divider(unsigned long rate,
84 unsigned long parent_rate)
86 u32 divider = DIV_ROUND_UP(parent_rate, rate);
89 * Per the datasheet, the register is always interpreted as an even
90 * number, by rounding down. In other words, the LSB is ignored. So,
91 * if the LSB is set, increment the divider to avoid any issue.
95 if ((divider < BCM2835_I2C_CDIV_MIN) ||
96 (divider > BCM2835_I2C_CDIV_MAX))
102 static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
103 unsigned long parent_rate)
105 struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
107 u32 divider = clk_bcm2835_i2c_calc_divider(rate, parent_rate);
109 if (divider == -EINVAL)
112 bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DIV, divider);
115 * Number of core clocks to wait after falling edge before
116 * outputting the next data bit. Note that both FEDL and REDL
117 * can't be greater than CDIV/2.
119 fedl = max(divider / 16, 1u);
122 * Number of core clocks to wait after rising edge before
123 * sampling the next incoming data bit.
125 redl = max(divider / 4, 1u);
127 bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DEL,
128 (fedl << BCM2835_I2C_FEDL_SHIFT) |
129 (redl << BCM2835_I2C_REDL_SHIFT));
133 static long clk_bcm2835_i2c_round_rate(struct clk_hw *hw, unsigned long rate,
134 unsigned long *parent_rate)
136 u32 divider = clk_bcm2835_i2c_calc_divider(rate, *parent_rate);
138 return DIV_ROUND_UP(*parent_rate, divider);
141 static unsigned long clk_bcm2835_i2c_recalc_rate(struct clk_hw *hw,
142 unsigned long parent_rate)
144 struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
145 u32 divider = bcm2835_i2c_readl(div->i2c_dev, BCM2835_I2C_DIV);
147 return DIV_ROUND_UP(parent_rate, divider);
150 static const struct clk_ops clk_bcm2835_i2c_ops = {
151 .set_rate = clk_bcm2835_i2c_set_rate,
152 .round_rate = clk_bcm2835_i2c_round_rate,
153 .recalc_rate = clk_bcm2835_i2c_recalc_rate,
156 static struct clk *bcm2835_i2c_register_div(struct device *dev,
158 struct bcm2835_i2c_dev *i2c_dev)
160 struct clk_init_data init;
161 struct clk_bcm2835_i2c *priv;
163 const char *mclk_name;
165 snprintf(name, sizeof(name), "%s_div", dev_name(dev));
167 mclk_name = __clk_get_name(mclk);
169 init.ops = &clk_bcm2835_i2c_ops;
171 init.parent_names = (const char* []) { mclk_name };
172 init.num_parents = 1;
175 priv = devm_kzalloc(dev, sizeof(struct clk_bcm2835_i2c), GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
179 priv->hw.init = &init;
180 priv->i2c_dev = i2c_dev;
182 clk_hw_register_clkdev(&priv->hw, "div", dev_name(dev));
183 return devm_clk_register(dev, &priv->hw);
186 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
190 while (i2c_dev->msg_buf_remaining) {
191 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
192 if (!(val & BCM2835_I2C_S_TXD))
194 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
197 i2c_dev->msg_buf_remaining--;
201 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
205 while (i2c_dev->msg_buf_remaining) {
206 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
207 if (!(val & BCM2835_I2C_S_RXD))
209 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
212 i2c_dev->msg_buf_remaining--;
217 * Repeated Start Condition (Sr)
218 * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
219 * talks about reading from a slave with 10 bit address. This is achieved by
220 * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
222 * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
223 * firmware actually does it using polling and says that it's a workaround for
224 * a problem in the state machine.
225 * It turns out that it is possible to use the TXW interrupt to know when the
226 * transfer is active, provided the FIFO has not been prefilled.
229 static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
231 u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
232 struct i2c_msg *msg = i2c_dev->curr_msg;
233 bool last_msg = (i2c_dev->num_msgs == 1);
235 if (!i2c_dev->num_msgs)
239 i2c_dev->msg_buf = msg->buf;
240 i2c_dev->msg_buf_remaining = msg->len;
242 if (msg->flags & I2C_M_RD)
243 c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
245 c |= BCM2835_I2C_C_INTT;
248 c |= BCM2835_I2C_C_INTD;
250 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
251 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
252 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
255 static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev)
257 i2c_dev->curr_msg = NULL;
258 i2c_dev->num_msgs = 0;
260 i2c_dev->msg_buf = NULL;
261 i2c_dev->msg_buf_remaining = 0;
265 * Note about I2C_C_CLEAR on error:
266 * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
267 * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
268 * the state machine to send a NACK and a STOP. Since we're setting CLEAR
269 * without I2CEN, that NACK will be hanging around queued up for next time
270 * we start the engine.
273 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
275 struct bcm2835_i2c_dev *i2c_dev = data;
278 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
280 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
282 i2c_dev->msg_err = err;
286 if (val & BCM2835_I2C_S_DONE) {
287 if (!i2c_dev->curr_msg) {
288 dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
289 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
290 bcm2835_drain_rxfifo(i2c_dev);
291 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
294 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
295 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
297 i2c_dev->msg_err = 0;
301 if (val & BCM2835_I2C_S_TXW) {
302 if (!i2c_dev->msg_buf_remaining) {
303 i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
307 bcm2835_fill_txfifo(i2c_dev);
309 if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
311 bcm2835_i2c_start_transfer(i2c_dev);
317 if (val & BCM2835_I2C_S_RXR) {
318 if (!i2c_dev->msg_buf_remaining) {
319 i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
323 bcm2835_drain_rxfifo(i2c_dev);
330 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
331 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
332 BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
333 complete(&i2c_dev->completion);
338 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
341 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
342 unsigned long time_left;
345 for (i = 0; i < (num - 1); i++)
346 if (msgs[i].flags & I2C_M_RD) {
347 dev_warn_once(i2c_dev->dev,
348 "only one read message supported, has to be last\n");
352 i2c_dev->curr_msg = msgs;
353 i2c_dev->num_msgs = num;
354 reinit_completion(&i2c_dev->completion);
356 bcm2835_i2c_start_transfer(i2c_dev);
358 time_left = wait_for_completion_timeout(&i2c_dev->completion,
361 bcm2835_i2c_finish_transfer(i2c_dev);
364 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
365 BCM2835_I2C_C_CLEAR);
366 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
370 if (!i2c_dev->msg_err)
373 dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
375 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
381 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
383 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
386 static const struct i2c_algorithm bcm2835_i2c_algo = {
387 .master_xfer = bcm2835_i2c_xfer,
388 .functionality = bcm2835_i2c_func,
392 * This HW was reported to have problems with clock stretching:
393 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
394 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
396 static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
397 .flags = I2C_AQ_NO_CLK_STRETCH,
400 static int bcm2835_i2c_probe(struct platform_device *pdev)
402 struct bcm2835_i2c_dev *i2c_dev;
403 struct resource *mem, *irq;
405 struct i2c_adapter *adap;
410 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
413 platform_set_drvdata(pdev, i2c_dev);
414 i2c_dev->dev = &pdev->dev;
415 init_completion(&i2c_dev->completion);
417 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
418 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
419 if (IS_ERR(i2c_dev->regs))
420 return PTR_ERR(i2c_dev->regs);
422 mclk = devm_clk_get(&pdev->dev, NULL);
424 if (PTR_ERR(mclk) != -EPROBE_DEFER)
425 dev_err(&pdev->dev, "Could not get clock\n");
426 return PTR_ERR(mclk);
429 bus_clk = bcm2835_i2c_register_div(&pdev->dev, mclk, i2c_dev);
431 if (IS_ERR(bus_clk)) {
432 dev_err(&pdev->dev, "Could not register clock\n");
433 return PTR_ERR(bus_clk);
436 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
440 "Could not read clock-frequency property\n");
441 bus_clk_rate = 100000;
444 ret = clk_set_rate_exclusive(bus_clk, bus_clk_rate);
446 dev_err(&pdev->dev, "Could not set clock frequency\n");
450 ret = clk_prepare_enable(bus_clk);
452 dev_err(&pdev->dev, "Couldn't prepare clock");
456 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
458 dev_err(&pdev->dev, "No IRQ resource\n");
461 i2c_dev->irq = irq->start;
463 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
464 dev_name(&pdev->dev), i2c_dev);
466 dev_err(&pdev->dev, "Could not request IRQ\n");
470 adap = &i2c_dev->adapter;
471 i2c_set_adapdata(adap, i2c_dev);
472 adap->owner = THIS_MODULE;
473 adap->class = I2C_CLASS_DEPRECATED;
474 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
475 adap->algo = &bcm2835_i2c_algo;
476 adap->dev.parent = &pdev->dev;
477 adap->dev.of_node = pdev->dev.of_node;
478 adap->quirks = &bcm2835_i2c_quirks;
480 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
482 ret = i2c_add_adapter(adap);
484 free_irq(i2c_dev->irq, i2c_dev);
489 static int bcm2835_i2c_remove(struct platform_device *pdev)
491 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
492 struct clk *bus_clk = devm_clk_get(i2c_dev->dev, "div");
494 clk_rate_exclusive_put(bus_clk);
495 clk_disable_unprepare(bus_clk);
497 free_irq(i2c_dev->irq, i2c_dev);
498 i2c_del_adapter(&i2c_dev->adapter);
503 static const struct of_device_id bcm2835_i2c_of_match[] = {
504 { .compatible = "brcm,bcm2835-i2c" },
507 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
509 static struct platform_driver bcm2835_i2c_driver = {
510 .probe = bcm2835_i2c_probe,
511 .remove = bcm2835_i2c_remove,
513 .name = "i2c-bcm2835",
514 .of_match_table = bcm2835_i2c_of_match,
517 module_platform_driver(bcm2835_i2c_driver);
519 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
520 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
521 MODULE_LICENSE("GPL v2");
522 MODULE_ALIAS("platform:i2c-bcm2835");