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
31 #define BCM2835_I2C_CLKT 0x1c
33 #define BCM2835_I2C_C_READ BIT(0)
34 #define BCM2835_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */
35 #define BCM2835_I2C_C_ST BIT(7)
36 #define BCM2835_I2C_C_INTD BIT(8)
37 #define BCM2835_I2C_C_INTT BIT(9)
38 #define BCM2835_I2C_C_INTR BIT(10)
39 #define BCM2835_I2C_C_I2CEN BIT(15)
41 #define BCM2835_I2C_S_TA BIT(0)
42 #define BCM2835_I2C_S_DONE BIT(1)
43 #define BCM2835_I2C_S_TXW BIT(2)
44 #define BCM2835_I2C_S_RXR BIT(3)
45 #define BCM2835_I2C_S_TXD BIT(4)
46 #define BCM2835_I2C_S_RXD BIT(5)
47 #define BCM2835_I2C_S_TXE BIT(6)
48 #define BCM2835_I2C_S_RXF BIT(7)
49 #define BCM2835_I2C_S_ERR BIT(8)
50 #define BCM2835_I2C_S_CLKT BIT(9)
51 #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */
53 #define BCM2835_I2C_FEDL_SHIFT 16
54 #define BCM2835_I2C_REDL_SHIFT 0
56 #define BCM2835_I2C_CDIV_MIN 0x0002
57 #define BCM2835_I2C_CDIV_MAX 0xFFFE
59 struct bcm2835_i2c_dev
{
65 struct i2c_adapter adapter
;
66 struct completion completion
;
67 struct i2c_msg
*curr_msg
;
71 size_t msg_buf_remaining
;
74 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev
*i2c_dev
,
77 writel(val
, i2c_dev
->regs
+ reg
);
80 static inline u32
bcm2835_i2c_readl(struct bcm2835_i2c_dev
*i2c_dev
, u32 reg
)
82 return readl(i2c_dev
->regs
+ reg
);
85 static int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev
*i2c_dev
)
87 u32 divider
, redl
, fedl
;
89 divider
= DIV_ROUND_UP(clk_get_rate(i2c_dev
->clk
),
90 i2c_dev
->bus_clk_rate
);
92 * Per the datasheet, the register is always interpreted as an even
93 * number, by rounding down. In other words, the LSB is ignored. So,
94 * if the LSB is set, increment the divider to avoid any issue.
98 if ((divider
< BCM2835_I2C_CDIV_MIN
) ||
99 (divider
> BCM2835_I2C_CDIV_MAX
)) {
100 dev_err_ratelimited(i2c_dev
->dev
, "Invalid clock-frequency\n");
104 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_DIV
, divider
);
107 * Number of core clocks to wait after falling edge before
108 * outputting the next data bit. Note that both FEDL and REDL
109 * can't be greater than CDIV/2.
111 fedl
= max(divider
/ 16, 1u);
114 * Number of core clocks to wait after rising edge before
115 * sampling the next incoming data bit.
117 redl
= max(divider
/ 4, 1u);
119 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_DEL
,
120 (fedl
<< BCM2835_I2C_FEDL_SHIFT
) |
121 (redl
<< BCM2835_I2C_REDL_SHIFT
));
125 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev
*i2c_dev
)
129 while (i2c_dev
->msg_buf_remaining
) {
130 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
131 if (!(val
& BCM2835_I2C_S_TXD
))
133 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_FIFO
,
136 i2c_dev
->msg_buf_remaining
--;
140 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev
*i2c_dev
)
144 while (i2c_dev
->msg_buf_remaining
) {
145 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
146 if (!(val
& BCM2835_I2C_S_RXD
))
148 *i2c_dev
->msg_buf
= bcm2835_i2c_readl(i2c_dev
,
151 i2c_dev
->msg_buf_remaining
--;
156 * Repeated Start Condition (Sr)
157 * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
158 * talks about reading from a slave with 10 bit address. This is achieved by
159 * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
161 * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
162 * firmware actually does it using polling and says that it's a workaround for
163 * a problem in the state machine.
164 * It turns out that it is possible to use the TXW interrupt to know when the
165 * transfer is active, provided the FIFO has not been prefilled.
168 static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev
*i2c_dev
)
170 u32 c
= BCM2835_I2C_C_ST
| BCM2835_I2C_C_I2CEN
;
171 struct i2c_msg
*msg
= i2c_dev
->curr_msg
;
172 bool last_msg
= (i2c_dev
->num_msgs
== 1);
174 if (!i2c_dev
->num_msgs
)
178 i2c_dev
->msg_buf
= msg
->buf
;
179 i2c_dev
->msg_buf_remaining
= msg
->len
;
181 if (msg
->flags
& I2C_M_RD
)
182 c
|= BCM2835_I2C_C_READ
| BCM2835_I2C_C_INTR
;
184 c
|= BCM2835_I2C_C_INTT
;
187 c
|= BCM2835_I2C_C_INTD
;
189 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_A
, msg
->addr
);
190 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_DLEN
, msg
->len
);
191 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
, c
);
195 * Note about I2C_C_CLEAR on error:
196 * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
197 * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
198 * the state machine to send a NACK and a STOP. Since we're setting CLEAR
199 * without I2CEN, that NACK will be hanging around queued up for next time
200 * we start the engine.
203 static irqreturn_t
bcm2835_i2c_isr(int this_irq
, void *data
)
205 struct bcm2835_i2c_dev
*i2c_dev
= data
;
208 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
210 err
= val
& (BCM2835_I2C_S_CLKT
| BCM2835_I2C_S_ERR
);
212 i2c_dev
->msg_err
= err
;
216 if (val
& BCM2835_I2C_S_DONE
) {
217 if (!i2c_dev
->curr_msg
) {
218 dev_err(i2c_dev
->dev
, "Got unexpected interrupt (from firmware?)\n");
219 } else if (i2c_dev
->curr_msg
->flags
& I2C_M_RD
) {
220 bcm2835_drain_rxfifo(i2c_dev
);
221 val
= bcm2835_i2c_readl(i2c_dev
, BCM2835_I2C_S
);
224 if ((val
& BCM2835_I2C_S_RXD
) || i2c_dev
->msg_buf_remaining
)
225 i2c_dev
->msg_err
= BCM2835_I2C_S_LEN
;
227 i2c_dev
->msg_err
= 0;
231 if (val
& BCM2835_I2C_S_TXW
) {
232 if (!i2c_dev
->msg_buf_remaining
) {
233 i2c_dev
->msg_err
= val
| BCM2835_I2C_S_LEN
;
237 bcm2835_fill_txfifo(i2c_dev
);
239 if (i2c_dev
->num_msgs
&& !i2c_dev
->msg_buf_remaining
) {
241 bcm2835_i2c_start_transfer(i2c_dev
);
247 if (val
& BCM2835_I2C_S_RXR
) {
248 if (!i2c_dev
->msg_buf_remaining
) {
249 i2c_dev
->msg_err
= val
| BCM2835_I2C_S_LEN
;
253 bcm2835_drain_rxfifo(i2c_dev
);
260 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
, BCM2835_I2C_C_CLEAR
);
261 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_S
, BCM2835_I2C_S_CLKT
|
262 BCM2835_I2C_S_ERR
| BCM2835_I2C_S_DONE
);
263 complete(&i2c_dev
->completion
);
268 static int bcm2835_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[],
271 struct bcm2835_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
272 unsigned long time_left
;
275 for (i
= 0; i
< (num
- 1); i
++)
276 if (msgs
[i
].flags
& I2C_M_RD
) {
277 dev_warn_once(i2c_dev
->dev
,
278 "only one read message supported, has to be last\n");
282 ret
= bcm2835_i2c_set_divider(i2c_dev
);
286 i2c_dev
->curr_msg
= msgs
;
287 i2c_dev
->num_msgs
= num
;
288 reinit_completion(&i2c_dev
->completion
);
290 bcm2835_i2c_start_transfer(i2c_dev
);
292 time_left
= wait_for_completion_timeout(&i2c_dev
->completion
,
295 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
,
296 BCM2835_I2C_C_CLEAR
);
297 dev_err(i2c_dev
->dev
, "i2c transfer timed out\n");
301 if (!i2c_dev
->msg_err
)
304 dev_dbg(i2c_dev
->dev
, "i2c transfer failed: %x\n", i2c_dev
->msg_err
);
306 if (i2c_dev
->msg_err
& BCM2835_I2C_S_ERR
)
312 static u32
bcm2835_i2c_func(struct i2c_adapter
*adap
)
314 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
317 static const struct i2c_algorithm bcm2835_i2c_algo
= {
318 .master_xfer
= bcm2835_i2c_xfer
,
319 .functionality
= bcm2835_i2c_func
,
323 * This HW was reported to have problems with clock stretching:
324 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
325 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
327 static const struct i2c_adapter_quirks bcm2835_i2c_quirks
= {
328 .flags
= I2C_AQ_NO_CLK_STRETCH
,
331 static int bcm2835_i2c_probe(struct platform_device
*pdev
)
333 struct bcm2835_i2c_dev
*i2c_dev
;
334 struct resource
*mem
, *irq
;
336 struct i2c_adapter
*adap
;
338 i2c_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c_dev
), GFP_KERNEL
);
341 platform_set_drvdata(pdev
, i2c_dev
);
342 i2c_dev
->dev
= &pdev
->dev
;
343 init_completion(&i2c_dev
->completion
);
345 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
346 i2c_dev
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
347 if (IS_ERR(i2c_dev
->regs
))
348 return PTR_ERR(i2c_dev
->regs
);
350 i2c_dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
351 if (IS_ERR(i2c_dev
->clk
)) {
352 if (PTR_ERR(i2c_dev
->clk
) != -EPROBE_DEFER
)
353 dev_err(&pdev
->dev
, "Could not get clock\n");
354 return PTR_ERR(i2c_dev
->clk
);
357 ret
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
358 &i2c_dev
->bus_clk_rate
);
361 "Could not read clock-frequency property\n");
362 i2c_dev
->bus_clk_rate
= 100000;
365 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
367 dev_err(&pdev
->dev
, "No IRQ resource\n");
370 i2c_dev
->irq
= irq
->start
;
372 ret
= request_irq(i2c_dev
->irq
, bcm2835_i2c_isr
, IRQF_SHARED
,
373 dev_name(&pdev
->dev
), i2c_dev
);
375 dev_err(&pdev
->dev
, "Could not request IRQ\n");
379 adap
= &i2c_dev
->adapter
;
380 i2c_set_adapdata(adap
, i2c_dev
);
381 adap
->owner
= THIS_MODULE
;
382 adap
->class = I2C_CLASS_DEPRECATED
;
383 strlcpy(adap
->name
, "bcm2835 I2C adapter", sizeof(adap
->name
));
384 adap
->algo
= &bcm2835_i2c_algo
;
385 adap
->dev
.parent
= &pdev
->dev
;
386 adap
->dev
.of_node
= pdev
->dev
.of_node
;
387 adap
->quirks
= &bcm2835_i2c_quirks
;
389 bcm2835_i2c_writel(i2c_dev
, BCM2835_I2C_C
, 0);
391 ret
= i2c_add_adapter(adap
);
393 free_irq(i2c_dev
->irq
, i2c_dev
);
398 static int bcm2835_i2c_remove(struct platform_device
*pdev
)
400 struct bcm2835_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
402 free_irq(i2c_dev
->irq
, i2c_dev
);
403 i2c_del_adapter(&i2c_dev
->adapter
);
408 static const struct of_device_id bcm2835_i2c_of_match
[] = {
409 { .compatible
= "brcm,bcm2835-i2c" },
412 MODULE_DEVICE_TABLE(of
, bcm2835_i2c_of_match
);
414 static struct platform_driver bcm2835_i2c_driver
= {
415 .probe
= bcm2835_i2c_probe
,
416 .remove
= bcm2835_i2c_remove
,
418 .name
= "i2c-bcm2835",
419 .of_match_table
= bcm2835_i2c_of_match
,
422 module_platform_driver(bcm2835_i2c_driver
);
424 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
425 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
426 MODULE_LICENSE("GPL v2");
427 MODULE_ALIAS("platform:i2c-bcm2835");