GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / i2c / busses / i2c-bcm2835.c
1 /*
2  * BCM2835 master mode driver
3  *
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.
7  *
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.
12  */
13
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>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23
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 /*
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
34  * timeout detection.
35  */
36 #define BCM2835_I2C_CLKT        0x1c
37
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)
45
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 */
57
58 #define BCM2835_I2C_FEDL_SHIFT  16
59 #define BCM2835_I2C_REDL_SHIFT  0
60
61 #define BCM2835_I2C_CDIV_MIN    0x0002
62 #define BCM2835_I2C_CDIV_MAX    0xFFFE
63
64 struct bcm2835_i2c_dev {
65         struct device *dev;
66         void __iomem *regs;
67         struct clk *clk;
68         int irq;
69         u32 bus_clk_rate;
70         struct i2c_adapter adapter;
71         struct completion completion;
72         struct i2c_msg *curr_msg;
73         int num_msgs;
74         u32 msg_err;
75         u8 *msg_buf;
76         size_t msg_buf_remaining;
77 };
78
79 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
80                                       u32 reg, u32 val)
81 {
82         writel(val, i2c_dev->regs + reg);
83 }
84
85 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
86 {
87         return readl(i2c_dev->regs + reg);
88 }
89
90 static int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev *i2c_dev)
91 {
92         u32 divider, redl, fedl;
93
94         divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk),
95                                i2c_dev->bus_clk_rate);
96         /*
97          * Per the datasheet, the register is always interpreted as an even
98          * number, by rounding down. In other words, the LSB is ignored. So,
99          * if the LSB is set, increment the divider to avoid any issue.
100          */
101         if (divider & 1)
102                 divider++;
103         if ((divider < BCM2835_I2C_CDIV_MIN) ||
104             (divider > BCM2835_I2C_CDIV_MAX)) {
105                 dev_err_ratelimited(i2c_dev->dev, "Invalid clock-frequency\n");
106                 return -EINVAL;
107         }
108
109         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
110
111         /*
112          * Number of core clocks to wait after falling edge before
113          * outputting the next data bit.  Note that both FEDL and REDL
114          * can't be greater than CDIV/2.
115          */
116         fedl = max(divider / 16, 1u);
117
118         /*
119          * Number of core clocks to wait after rising edge before
120          * sampling the next incoming data bit.
121          */
122         redl = max(divider / 4, 1u);
123
124         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DEL,
125                            (fedl << BCM2835_I2C_FEDL_SHIFT) |
126                            (redl << BCM2835_I2C_REDL_SHIFT));
127         return 0;
128 }
129
130 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
131 {
132         u32 val;
133
134         while (i2c_dev->msg_buf_remaining) {
135                 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
136                 if (!(val & BCM2835_I2C_S_TXD))
137                         break;
138                 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
139                                    *i2c_dev->msg_buf);
140                 i2c_dev->msg_buf++;
141                 i2c_dev->msg_buf_remaining--;
142         }
143 }
144
145 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
146 {
147         u32 val;
148
149         while (i2c_dev->msg_buf_remaining) {
150                 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
151                 if (!(val & BCM2835_I2C_S_RXD))
152                         break;
153                 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
154                                                       BCM2835_I2C_FIFO);
155                 i2c_dev->msg_buf++;
156                 i2c_dev->msg_buf_remaining--;
157         }
158 }
159
160 /*
161  * Repeated Start Condition (Sr)
162  * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
163  * talks about reading from a slave with 10 bit address. This is achieved by
164  * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
165  * issue a read.
166  * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
167  * firmware actually does it using polling and says that it's a workaround for
168  * a problem in the state machine.
169  * It turns out that it is possible to use the TXW interrupt to know when the
170  * transfer is active, provided the FIFO has not been prefilled.
171  */
172
173 static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
174 {
175         u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
176         struct i2c_msg *msg = i2c_dev->curr_msg;
177         bool last_msg = (i2c_dev->num_msgs == 1);
178
179         if (!i2c_dev->num_msgs)
180                 return;
181
182         i2c_dev->num_msgs--;
183         i2c_dev->msg_buf = msg->buf;
184         i2c_dev->msg_buf_remaining = msg->len;
185
186         if (msg->flags & I2C_M_RD)
187                 c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
188         else
189                 c |= BCM2835_I2C_C_INTT;
190
191         if (last_msg)
192                 c |= BCM2835_I2C_C_INTD;
193
194         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
195         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
196         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
197 }
198
199 static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev)
200 {
201         i2c_dev->curr_msg = NULL;
202         i2c_dev->num_msgs = 0;
203
204         i2c_dev->msg_buf = NULL;
205         i2c_dev->msg_buf_remaining = 0;
206 }
207
208 /*
209  * Note about I2C_C_CLEAR on error:
210  * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
211  * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
212  * the state machine to send a NACK and a STOP. Since we're setting CLEAR
213  * without I2CEN, that NACK will be hanging around queued up for next time
214  * we start the engine.
215  */
216
217 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
218 {
219         struct bcm2835_i2c_dev *i2c_dev = data;
220         u32 val, err;
221
222         val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
223
224         err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
225         if (err) {
226                 i2c_dev->msg_err = err;
227                 goto complete;
228         }
229
230         if (val & BCM2835_I2C_S_DONE) {
231                 if (!i2c_dev->curr_msg) {
232                         dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
233                 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
234                         bcm2835_drain_rxfifo(i2c_dev);
235                         val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
236                 }
237
238                 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
239                         i2c_dev->msg_err = BCM2835_I2C_S_LEN;
240                 else
241                         i2c_dev->msg_err = 0;
242                 goto complete;
243         }
244
245         if (val & BCM2835_I2C_S_TXW) {
246                 if (!i2c_dev->msg_buf_remaining) {
247                         i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
248                         goto complete;
249                 }
250
251                 bcm2835_fill_txfifo(i2c_dev);
252
253                 if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
254                         i2c_dev->curr_msg++;
255                         bcm2835_i2c_start_transfer(i2c_dev);
256                 }
257
258                 return IRQ_HANDLED;
259         }
260
261         if (val & BCM2835_I2C_S_RXR) {
262                 if (!i2c_dev->msg_buf_remaining) {
263                         i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
264                         goto complete;
265                 }
266
267                 bcm2835_drain_rxfifo(i2c_dev);
268                 return IRQ_HANDLED;
269         }
270
271         return IRQ_NONE;
272
273 complete:
274         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
275         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
276                            BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
277         complete(&i2c_dev->completion);
278
279         return IRQ_HANDLED;
280 }
281
282 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
283                             int num)
284 {
285         struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
286         unsigned long time_left;
287         int i, ret;
288
289         for (i = 0; i < (num - 1); i++)
290                 if (msgs[i].flags & I2C_M_RD) {
291                         dev_warn_once(i2c_dev->dev,
292                                       "only one read message supported, has to be last\n");
293                         return -EOPNOTSUPP;
294                 }
295
296         ret = bcm2835_i2c_set_divider(i2c_dev);
297         if (ret)
298                 return ret;
299
300         i2c_dev->curr_msg = msgs;
301         i2c_dev->num_msgs = num;
302         reinit_completion(&i2c_dev->completion);
303
304         bcm2835_i2c_start_transfer(i2c_dev);
305
306         time_left = wait_for_completion_timeout(&i2c_dev->completion,
307                                                 adap->timeout);
308
309         bcm2835_i2c_finish_transfer(i2c_dev);
310
311         if (!time_left) {
312                 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
313                                    BCM2835_I2C_C_CLEAR);
314                 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
315                 return -ETIMEDOUT;
316         }
317
318         if (!i2c_dev->msg_err)
319                 return num;
320
321         dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
322
323         if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
324                 return -EREMOTEIO;
325
326         return -EIO;
327 }
328
329 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
330 {
331         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
332 }
333
334 static const struct i2c_algorithm bcm2835_i2c_algo = {
335         .master_xfer    = bcm2835_i2c_xfer,
336         .functionality  = bcm2835_i2c_func,
337 };
338
339 /*
340  * This HW was reported to have problems with clock stretching:
341  * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
342  * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
343  */
344 static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
345         .flags = I2C_AQ_NO_CLK_STRETCH,
346 };
347
348 static int bcm2835_i2c_probe(struct platform_device *pdev)
349 {
350         struct bcm2835_i2c_dev *i2c_dev;
351         struct resource *mem, *irq;
352         int ret;
353         struct i2c_adapter *adap;
354
355         i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
356         if (!i2c_dev)
357                 return -ENOMEM;
358         platform_set_drvdata(pdev, i2c_dev);
359         i2c_dev->dev = &pdev->dev;
360         init_completion(&i2c_dev->completion);
361
362         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363         i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
364         if (IS_ERR(i2c_dev->regs))
365                 return PTR_ERR(i2c_dev->regs);
366
367         i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
368         if (IS_ERR(i2c_dev->clk)) {
369                 if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
370                         dev_err(&pdev->dev, "Could not get clock\n");
371                 return PTR_ERR(i2c_dev->clk);
372         }
373
374         ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
375                                    &i2c_dev->bus_clk_rate);
376         if (ret < 0) {
377                 dev_warn(&pdev->dev,
378                          "Could not read clock-frequency property\n");
379                 i2c_dev->bus_clk_rate = 100000;
380         }
381
382         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
383         if (!irq) {
384                 dev_err(&pdev->dev, "No IRQ resource\n");
385                 return -ENODEV;
386         }
387         i2c_dev->irq = irq->start;
388
389         ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
390                           dev_name(&pdev->dev), i2c_dev);
391         if (ret) {
392                 dev_err(&pdev->dev, "Could not request IRQ\n");
393                 return -ENODEV;
394         }
395
396         adap = &i2c_dev->adapter;
397         i2c_set_adapdata(adap, i2c_dev);
398         adap->owner = THIS_MODULE;
399         adap->class = I2C_CLASS_DEPRECATED;
400         strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
401         adap->algo = &bcm2835_i2c_algo;
402         adap->dev.parent = &pdev->dev;
403         adap->dev.of_node = pdev->dev.of_node;
404         adap->quirks = &bcm2835_i2c_quirks;
405
406         /*
407          * Disable the hardware clock stretching timeout. SMBUS
408          * specifies a limit for how long the device can stretch the
409          * clock, but core I2C doesn't.
410          */
411         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_CLKT, 0);
412         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
413
414         ret = i2c_add_adapter(adap);
415         if (ret)
416                 free_irq(i2c_dev->irq, i2c_dev);
417
418         return ret;
419 }
420
421 static int bcm2835_i2c_remove(struct platform_device *pdev)
422 {
423         struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
424
425         free_irq(i2c_dev->irq, i2c_dev);
426         i2c_del_adapter(&i2c_dev->adapter);
427
428         return 0;
429 }
430
431 static const struct of_device_id bcm2835_i2c_of_match[] = {
432         { .compatible = "brcm,bcm2835-i2c" },
433         {},
434 };
435 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
436
437 static struct platform_driver bcm2835_i2c_driver = {
438         .probe          = bcm2835_i2c_probe,
439         .remove         = bcm2835_i2c_remove,
440         .driver         = {
441                 .name   = "i2c-bcm2835",
442                 .of_match_table = bcm2835_i2c_of_match,
443         },
444 };
445 module_platform_driver(bcm2835_i2c_driver);
446
447 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
448 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
449 MODULE_LICENSE("GPL v2");
450 MODULE_ALIAS("platform:i2c-bcm2835");