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/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
19 #define BCM2835_I2C_C 0x0
20 #define BCM2835_I2C_S 0x4
21 #define BCM2835_I2C_DLEN 0x8
22 #define BCM2835_I2C_A 0xc
23 #define BCM2835_I2C_FIFO 0x10
24 #define BCM2835_I2C_DIV 0x14
25 #define BCM2835_I2C_DEL 0x18
26 #define BCM2835_I2C_CLKT 0x1c
28 #define BCM2835_I2C_C_READ BIT(0)
29 #define BCM2835_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */
30 #define BCM2835_I2C_C_ST BIT(7)
31 #define BCM2835_I2C_C_INTD BIT(8)
32 #define BCM2835_I2C_C_INTT BIT(9)
33 #define BCM2835_I2C_C_INTR BIT(10)
34 #define BCM2835_I2C_C_I2CEN BIT(15)
36 #define BCM2835_I2C_S_TA BIT(0)
37 #define BCM2835_I2C_S_DONE BIT(1)
38 #define BCM2835_I2C_S_TXW BIT(2)
39 #define BCM2835_I2C_S_RXR BIT(3)
40 #define BCM2835_I2C_S_TXD BIT(4)
41 #define BCM2835_I2C_S_RXD BIT(5)
42 #define BCM2835_I2C_S_TXE BIT(6)
43 #define BCM2835_I2C_S_RXF BIT(7)
44 #define BCM2835_I2C_S_ERR BIT(8)
45 #define BCM2835_I2C_S_CLKT BIT(9)
46 #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */
48 #define BCM2835_I2C_FEDL_SHIFT 16
49 #define BCM2835_I2C_REDL_SHIFT 0
51 #define BCM2835_I2C_CDIV_MIN 0x0002
52 #define BCM2835_I2C_CDIV_MAX 0xFFFE
54 struct bcm2835_i2c_dev
{
58 struct i2c_adapter adapter
;
59 struct completion completion
;
60 struct i2c_msg
*curr_msg
;
64 size_t msg_buf_remaining
;
67 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev
*i2c_dev
,
70 writel(val
, i2c_dev
->regs
+ reg
);
73 static inline u32
bcm2835_i2c_readl(struct bcm2835_i2c_dev
*i2c_dev
, u32 reg
)
75 return readl(i2c_dev
->regs
+ reg
);
78 #define to_clk_bcm2835_i2c(_hw) container_of(_hw, struct clk_bcm2835_i2c, hw)
79 struct clk_bcm2835_i2c
{
81 struct bcm2835_i2c_dev
*i2c_dev
;
84 static int clk_bcm2835_i2c_calc_divider(unsigned long rate
,
85 unsigned long parent_rate
)
87 u32 divider
= DIV_ROUND_UP(parent_rate
, rate
);
90 * Per the datasheet, the register is always interpreted as an even
91 * number, by rounding down. In other words, the LSB is ignored. So,
92 * if the LSB is set, increment the divider to avoid any issue.
96 if ((divider
< BCM2835_I2C_CDIV_MIN
) ||
97 (divider
> BCM2835_I2C_CDIV_MAX
))
103 static int clk_bcm2835_i2c_set_rate(struct clk_hw
*hw
, unsigned long rate
,
104 unsigned long parent_rate
)
106 struct clk_bcm2835_i2c
*div
= to_clk_bcm2835_i2c(hw
);
108 u32 divider
= clk_bcm2835_i2c_calc_divider(rate
, parent_rate
);
110 if (divider
== -EINVAL
)
113 bcm2835_i2c_writel(div
->i2c_dev
, BCM2835_I2C_DIV
, divider
);
116 * Number of core clocks to wait after falling edge before
117 * outputting the next data bit. Note that both FEDL and REDL
118 * can't be greater than CDIV/2.
120 fedl
= max(divider
/ 16, 1u);
123 * Number of core clocks to wait after rising edge before
124 * sampling the next incoming data bit.
126 redl
= max(divider
/ 4, 1u);
128 bcm2835_i2c_writel(div
->i2c_dev
, BCM2835_I2C_DEL
,
129 (fedl
<< BCM2835_I2C_FEDL_SHIFT
) |
130 (redl
<< BCM2835_I2C_REDL_SHIFT
));
134 static long clk_bcm2835_i2c_round_rate(struct clk_hw
*hw
, unsigned long rate
,
135 unsigned long *parent_rate
)
137 u32 divider
= clk_bcm2835_i2c_calc_divider(rate
, *parent_rate
);
139 return DIV_ROUND_UP(*parent_rate
, divider
);
142 static unsigned long clk_bcm2835_i2c_recalc_rate(struct clk_hw
*hw
,
143 unsigned long parent_rate
)
145 struct clk_bcm2835_i2c
*div
= to_clk_bcm2835_i2c(hw
);
146 u32 divider
= bcm2835_i2c_readl(div
->i2c_dev
, BCM2835_I2C_DIV
);
148 return DIV_ROUND_UP(parent_rate
, divider
);
151 static const struct clk_ops clk_bcm2835_i2c_ops
= {
152 .set_rate
= clk_bcm2835_i2c_set_rate
,
153 .round_rate
= clk_bcm2835_i2c_round_rate
,
154 .recalc_rate
= clk_bcm2835_i2c_recalc_rate
,
157 static struct clk
*bcm2835_i2c_register_div(struct device
*dev
,
159 struct bcm2835_i2c_dev
*i2c_dev
)
161 struct clk_init_data init
;
162 struct clk_bcm2835_i2c
*priv
;
164 const char *mclk_name
;
166 snprintf(name
, sizeof(name
), "%s_div", dev_name(dev
));
168 mclk_name
= __clk_get_name(mclk
);
170 init
.ops
= &clk_bcm2835_i2c_ops
;
172 init
.parent_names
= (const char* []) { mclk_name
};
173 init
.num_parents
= 1;
176 priv
= devm_kzalloc(dev
, sizeof(struct clk_bcm2835_i2c
), GFP_KERNEL
);
178 return ERR_PTR(-ENOMEM
);
180 priv
->hw
.init
= &init
;
181 priv
->i2c_dev
= i2c_dev
;
183 clk_hw_register_clkdev(&priv
->hw
, "div", dev_name(dev
));
184 return devm_clk_register(dev
, &priv
->hw
);
187 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev
*i2c_dev
)
191 while (i2c_dev
->msg_buf_remaining
) {
192 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
193 if (!(val
& BCM2835_I2C_S_TXD
))
195 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_FIFO
,
198 i2c_dev
->msg_buf_remaining
--;
202 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev
*i2c_dev
)
206 while (i2c_dev
->msg_buf_remaining
) {
207 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
208 if (!(val
& BCM2835_I2C_S_RXD
))
210 *i2c_dev
->msg_buf
= bcm2835_i2c_readl(i2c_dev
,
213 i2c_dev
->msg_buf_remaining
--;
218 * Repeated Start Condition (Sr)
219 * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
220 * talks about reading from a slave with 10 bit address. This is achieved by
221 * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
223 * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
224 * firmware actually does it using polling and says that it's a workaround for
225 * a problem in the state machine.
226 * It turns out that it is possible to use the TXW interrupt to know when the
227 * transfer is active, provided the FIFO has not been prefilled.
230 static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev
*i2c_dev
)
232 u32 c
= BCM2835_I2C_C_ST
| BCM2835_I2C_C_I2CEN
;
233 struct i2c_msg
*msg
= i2c_dev
->curr_msg
;
234 bool last_msg
= (i2c_dev
->num_msgs
== 1);
236 if (!i2c_dev
->num_msgs
)
240 i2c_dev
->msg_buf
= msg
->buf
;
241 i2c_dev
->msg_buf_remaining
= msg
->len
;
243 if (msg
->flags
& I2C_M_RD
)
244 c
|= BCM2835_I2C_C_READ
| BCM2835_I2C_C_INTR
;
246 c
|= BCM2835_I2C_C_INTT
;
249 c
|= BCM2835_I2C_C_INTD
;
251 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_A
, msg
->addr
);
252 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_DLEN
, msg
->len
);
253 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
, c
);
256 static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev
*i2c_dev
)
258 i2c_dev
->curr_msg
= NULL
;
259 i2c_dev
->num_msgs
= 0;
261 i2c_dev
->msg_buf
= NULL
;
262 i2c_dev
->msg_buf_remaining
= 0;
266 * Note about I2C_C_CLEAR on error:
267 * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
268 * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
269 * the state machine to send a NACK and a STOP. Since we're setting CLEAR
270 * without I2CEN, that NACK will be hanging around queued up for next time
271 * we start the engine.
274 static irqreturn_t
bcm2835_i2c_isr(int this_irq
, void *data
)
276 struct bcm2835_i2c_dev
*i2c_dev
= data
;
279 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
281 err
= val
& (BCM2835_I2C_S_CLKT
| BCM2835_I2C_S_ERR
);
283 i2c_dev
->msg_err
= err
;
287 if (val
& BCM2835_I2C_S_DONE
) {
288 if (!i2c_dev
->curr_msg
) {
289 dev_err(i2c_dev
->dev
, "Got unexpected interrupt (from firmware?)\n");
290 } else if (i2c_dev
->curr_msg
->flags
& I2C_M_RD
) {
291 bcm2835_drain_rxfifo(i2c_dev
);
292 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
295 if ((val
& BCM2835_I2C_S_RXD
) || i2c_dev
->msg_buf_remaining
)
296 i2c_dev
->msg_err
= BCM2835_I2C_S_LEN
;
298 i2c_dev
->msg_err
= 0;
302 if (val
& BCM2835_I2C_S_TXW
) {
303 if (!i2c_dev
->msg_buf_remaining
) {
304 i2c_dev
->msg_err
= val
| BCM2835_I2C_S_LEN
;
308 bcm2835_fill_txfifo(i2c_dev
);
310 if (i2c_dev
->num_msgs
&& !i2c_dev
->msg_buf_remaining
) {
312 bcm2835_i2c_start_transfer(i2c_dev
);
318 if (val
& BCM2835_I2C_S_RXR
) {
319 if (!i2c_dev
->msg_buf_remaining
) {
320 i2c_dev
->msg_err
= val
| BCM2835_I2C_S_LEN
;
324 bcm2835_drain_rxfifo(i2c_dev
);
331 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
, BCM2835_I2C_C_CLEAR
);
332 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_S
, BCM2835_I2C_S_CLKT
|
333 BCM2835_I2C_S_ERR
| BCM2835_I2C_S_DONE
);
334 complete(&i2c_dev
->completion
);
339 static int bcm2835_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[],
342 struct bcm2835_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
343 unsigned long time_left
;
346 for (i
= 0; i
< (num
- 1); i
++)
347 if (msgs
[i
].flags
& I2C_M_RD
) {
348 dev_warn_once(i2c_dev
->dev
,
349 "only one read message supported, has to be last\n");
353 i2c_dev
->curr_msg
= msgs
;
354 i2c_dev
->num_msgs
= num
;
355 reinit_completion(&i2c_dev
->completion
);
357 bcm2835_i2c_start_transfer(i2c_dev
);
359 time_left
= wait_for_completion_timeout(&i2c_dev
->completion
,
362 bcm2835_i2c_finish_transfer(i2c_dev
);
365 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
,
366 BCM2835_I2C_C_CLEAR
);
367 dev_err(i2c_dev
->dev
, "i2c transfer timed out\n");
371 if (!i2c_dev
->msg_err
)
374 dev_dbg(i2c_dev
->dev
, "i2c transfer failed: %x\n", i2c_dev
->msg_err
);
376 if (i2c_dev
->msg_err
& BCM2835_I2C_S_ERR
)
382 static u32
bcm2835_i2c_func(struct i2c_adapter
*adap
)
384 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
387 static const struct i2c_algorithm bcm2835_i2c_algo
= {
388 .master_xfer
= bcm2835_i2c_xfer
,
389 .functionality
= bcm2835_i2c_func
,
393 * The BCM2835 was reported to have problems with clock stretching:
394 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
395 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
397 static const struct i2c_adapter_quirks bcm2835_i2c_quirks
= {
398 .flags
= I2C_AQ_NO_CLK_STRETCH
,
401 static int bcm2835_i2c_probe(struct platform_device
*pdev
)
403 struct bcm2835_i2c_dev
*i2c_dev
;
404 struct resource
*mem
, *irq
;
406 struct i2c_adapter
*adap
;
411 i2c_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c_dev
), GFP_KERNEL
);
414 platform_set_drvdata(pdev
, i2c_dev
);
415 i2c_dev
->dev
= &pdev
->dev
;
416 init_completion(&i2c_dev
->completion
);
418 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
419 i2c_dev
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
420 if (IS_ERR(i2c_dev
->regs
))
421 return PTR_ERR(i2c_dev
->regs
);
423 mclk
= devm_clk_get(&pdev
->dev
, NULL
);
425 if (PTR_ERR(mclk
) != -EPROBE_DEFER
)
426 dev_err(&pdev
->dev
, "Could not get clock\n");
427 return PTR_ERR(mclk
);
430 bus_clk
= bcm2835_i2c_register_div(&pdev
->dev
, mclk
, i2c_dev
);
432 if (IS_ERR(bus_clk
)) {
433 dev_err(&pdev
->dev
, "Could not register clock\n");
434 return PTR_ERR(bus_clk
);
437 ret
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
441 "Could not read clock-frequency property\n");
442 bus_clk_rate
= 100000;
445 ret
= clk_set_rate_exclusive(bus_clk
, bus_clk_rate
);
447 dev_err(&pdev
->dev
, "Could not set clock frequency\n");
451 ret
= clk_prepare_enable(bus_clk
);
453 dev_err(&pdev
->dev
, "Couldn't prepare clock");
457 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
459 dev_err(&pdev
->dev
, "No IRQ resource\n");
462 i2c_dev
->irq
= irq
->start
;
464 ret
= request_irq(i2c_dev
->irq
, bcm2835_i2c_isr
, IRQF_SHARED
,
465 dev_name(&pdev
->dev
), i2c_dev
);
467 dev_err(&pdev
->dev
, "Could not request IRQ\n");
471 adap
= &i2c_dev
->adapter
;
472 i2c_set_adapdata(adap
, i2c_dev
);
473 adap
->owner
= THIS_MODULE
;
474 adap
->class = I2C_CLASS_DEPRECATED
;
475 snprintf(adap
->name
, sizeof(adap
->name
), "bcm2835 (%s)",
476 of_node_full_name(pdev
->dev
.of_node
));
477 adap
->algo
= &bcm2835_i2c_algo
;
478 adap
->dev
.parent
= &pdev
->dev
;
479 adap
->dev
.of_node
= pdev
->dev
.of_node
;
480 adap
->quirks
= of_device_get_match_data(&pdev
->dev
);
482 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
, 0);
484 ret
= i2c_add_adapter(adap
);
486 free_irq(i2c_dev
->irq
, i2c_dev
);
491 static int bcm2835_i2c_remove(struct platform_device
*pdev
)
493 struct bcm2835_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
494 struct clk
*bus_clk
= devm_clk_get(i2c_dev
->dev
, "div");
496 clk_rate_exclusive_put(bus_clk
);
497 clk_disable_unprepare(bus_clk
);
499 free_irq(i2c_dev
->irq
, i2c_dev
);
500 i2c_del_adapter(&i2c_dev
->adapter
);
505 static const struct of_device_id bcm2835_i2c_of_match
[] = {
506 { .compatible
= "brcm,bcm2711-i2c" },
507 { .compatible
= "brcm,bcm2835-i2c", .data
= &bcm2835_i2c_quirks
},
510 MODULE_DEVICE_TABLE(of
, bcm2835_i2c_of_match
);
512 static struct platform_driver bcm2835_i2c_driver
= {
513 .probe
= bcm2835_i2c_probe
,
514 .remove
= bcm2835_i2c_remove
,
516 .name
= "i2c-bcm2835",
517 .of_match_table
= bcm2835_i2c_of_match
,
520 module_platform_driver(bcm2835_i2c_driver
);
522 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
523 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
524 MODULE_LICENSE("GPL v2");
525 MODULE_ALIAS("platform:i2c-bcm2835");