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,
157 const char *mclk_name,
158 struct bcm2835_i2c_dev *i2c_dev)
160 struct clk_init_data init;
161 struct clk_bcm2835_i2c *priv;
164 snprintf(name, sizeof(name), "%s_div", dev_name(dev));
166 init.ops = &clk_bcm2835_i2c_ops;
168 init.parent_names = (const char* []) { mclk_name };
169 init.num_parents = 1;
172 priv = devm_kzalloc(dev, sizeof(struct clk_bcm2835_i2c), GFP_KERNEL);
174 return ERR_PTR(-ENOMEM);
176 priv->hw.init = &init;
177 priv->i2c_dev = i2c_dev;
179 clk_hw_register_clkdev(&priv->hw, "div", dev_name(dev));
180 return devm_clk_register(dev, &priv->hw);
183 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
187 while (i2c_dev->msg_buf_remaining) {
188 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
189 if (!(val & BCM2835_I2C_S_TXD))
191 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
194 i2c_dev->msg_buf_remaining--;
198 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
202 while (i2c_dev->msg_buf_remaining) {
203 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
204 if (!(val & BCM2835_I2C_S_RXD))
206 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
209 i2c_dev->msg_buf_remaining--;
214 * Repeated Start Condition (Sr)
215 * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
216 * talks about reading from a slave with 10 bit address. This is achieved by
217 * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
219 * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
220 * firmware actually does it using polling and says that it's a workaround for
221 * a problem in the state machine.
222 * It turns out that it is possible to use the TXW interrupt to know when the
223 * transfer is active, provided the FIFO has not been prefilled.
226 static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
228 u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
229 struct i2c_msg *msg = i2c_dev->curr_msg;
230 bool last_msg = (i2c_dev->num_msgs == 1);
232 if (!i2c_dev->num_msgs)
236 i2c_dev->msg_buf = msg->buf;
237 i2c_dev->msg_buf_remaining = msg->len;
239 if (msg->flags & I2C_M_RD)
240 c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
242 c |= BCM2835_I2C_C_INTT;
245 c |= BCM2835_I2C_C_INTD;
247 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
248 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
249 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
252 static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev)
254 i2c_dev->curr_msg = NULL;
255 i2c_dev->num_msgs = 0;
257 i2c_dev->msg_buf = NULL;
258 i2c_dev->msg_buf_remaining = 0;
262 * Note about I2C_C_CLEAR on error:
263 * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
264 * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
265 * the state machine to send a NACK and a STOP. Since we're setting CLEAR
266 * without I2CEN, that NACK will be hanging around queued up for next time
267 * we start the engine.
270 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
272 struct bcm2835_i2c_dev *i2c_dev = data;
275 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
277 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
279 i2c_dev->msg_err = err;
283 if (val & BCM2835_I2C_S_DONE) {
284 if (!i2c_dev->curr_msg) {
285 dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
286 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
287 bcm2835_drain_rxfifo(i2c_dev);
288 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
291 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
292 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
294 i2c_dev->msg_err = 0;
298 if (val & BCM2835_I2C_S_TXW) {
299 if (!i2c_dev->msg_buf_remaining) {
300 i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
304 bcm2835_fill_txfifo(i2c_dev);
306 if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
308 bcm2835_i2c_start_transfer(i2c_dev);
314 if (val & BCM2835_I2C_S_RXR) {
315 if (!i2c_dev->msg_buf_remaining) {
316 i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
320 bcm2835_drain_rxfifo(i2c_dev);
327 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
328 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
329 BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
330 complete(&i2c_dev->completion);
335 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
338 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
339 unsigned long time_left;
342 for (i = 0; i < (num - 1); i++)
343 if (msgs[i].flags & I2C_M_RD) {
344 dev_warn_once(i2c_dev->dev,
345 "only one read message supported, has to be last\n");
349 i2c_dev->curr_msg = msgs;
350 i2c_dev->num_msgs = num;
351 reinit_completion(&i2c_dev->completion);
353 bcm2835_i2c_start_transfer(i2c_dev);
355 time_left = wait_for_completion_timeout(&i2c_dev->completion,
358 bcm2835_i2c_finish_transfer(i2c_dev);
361 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
362 BCM2835_I2C_C_CLEAR);
363 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
367 if (!i2c_dev->msg_err)
370 dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
372 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
378 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
380 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
383 static const struct i2c_algorithm bcm2835_i2c_algo = {
384 .master_xfer = bcm2835_i2c_xfer,
385 .functionality = bcm2835_i2c_func,
389 * This HW was reported to have problems with clock stretching:
390 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
391 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
393 static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
394 .flags = I2C_AQ_NO_CLK_STRETCH,
397 static int bcm2835_i2c_probe(struct platform_device *pdev)
399 struct bcm2835_i2c_dev *i2c_dev;
400 struct resource *mem, *irq;
402 struct i2c_adapter *adap;
403 const char *mclk_name;
407 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
410 platform_set_drvdata(pdev, i2c_dev);
411 i2c_dev->dev = &pdev->dev;
412 init_completion(&i2c_dev->completion);
414 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
415 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
416 if (IS_ERR(i2c_dev->regs))
417 return PTR_ERR(i2c_dev->regs);
419 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
421 dev_err(&pdev->dev, "No IRQ resource\n");
424 i2c_dev->irq = irq->start;
426 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
427 dev_name(&pdev->dev), i2c_dev);
429 dev_err(&pdev->dev, "Could not request IRQ\n");
433 mclk_name = of_clk_get_parent_name(pdev->dev.of_node, 0);
435 bus_clk = bcm2835_i2c_register_div(&pdev->dev, mclk_name, i2c_dev);
437 if (IS_ERR(bus_clk)) {
438 dev_err(&pdev->dev, "Could not register clock\n");
439 return PTR_ERR(bus_clk);
442 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
446 "Could not read clock-frequency property\n");
447 bus_clk_rate = 100000;
450 ret = clk_set_rate_exclusive(bus_clk, bus_clk_rate);
452 dev_err(&pdev->dev, "Could not set clock frequency\n");
456 ret = clk_prepare_enable(bus_clk);
458 dev_err(&pdev->dev, "Couldn't prepare clock");
462 adap = &i2c_dev->adapter;
463 i2c_set_adapdata(adap, i2c_dev);
464 adap->owner = THIS_MODULE;
465 adap->class = I2C_CLASS_DEPRECATED;
466 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
467 adap->algo = &bcm2835_i2c_algo;
468 adap->dev.parent = &pdev->dev;
469 adap->dev.of_node = pdev->dev.of_node;
470 adap->quirks = &bcm2835_i2c_quirks;
472 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
474 ret = i2c_add_adapter(adap);
476 free_irq(i2c_dev->irq, i2c_dev);
481 static int bcm2835_i2c_remove(struct platform_device *pdev)
483 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
484 struct clk *bus_clk = devm_clk_get(i2c_dev->dev, "div");
486 clk_rate_exclusive_put(bus_clk);
487 clk_disable_unprepare(bus_clk);
489 free_irq(i2c_dev->irq, i2c_dev);
490 i2c_del_adapter(&i2c_dev->adapter);
495 static const struct of_device_id bcm2835_i2c_of_match[] = {
496 { .compatible = "brcm,bcm2835-i2c" },
499 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
501 static struct platform_driver bcm2835_i2c_driver = {
502 .probe = bcm2835_i2c_probe,
503 .remove = bcm2835_i2c_remove,
505 .name = "i2c-bcm2835",
506 .of_match_table = bcm2835_i2c_of_match,
509 module_platform_driver(bcm2835_i2c_driver);
511 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
512 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
513 MODULE_LICENSE("GPL v2");
514 MODULE_ALIAS("platform:i2c-bcm2835");