GNU Linux-libre 4.9.337-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_BITMSK_S    0x03FF
59
60 #define BCM2835_I2C_CDIV_MIN    0x0002
61 #define BCM2835_I2C_CDIV_MAX    0xFFFE
62
63 #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
64
65 struct bcm2835_i2c_dev {
66         struct device *dev;
67         void __iomem *regs;
68         struct clk *clk;
69         int irq;
70         struct i2c_adapter adapter;
71         struct completion completion;
72         struct i2c_msg *curr_msg;
73         u32 msg_err;
74         u8 *msg_buf;
75         size_t msg_buf_remaining;
76 };
77
78 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
79                                       u32 reg, u32 val)
80 {
81         writel(val, i2c_dev->regs + reg);
82 }
83
84 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
85 {
86         return readl(i2c_dev->regs + reg);
87 }
88
89 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
90 {
91         u32 val;
92
93         while (i2c_dev->msg_buf_remaining) {
94                 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
95                 if (!(val & BCM2835_I2C_S_TXD))
96                         break;
97                 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
98                                    *i2c_dev->msg_buf);
99                 i2c_dev->msg_buf++;
100                 i2c_dev->msg_buf_remaining--;
101         }
102 }
103
104 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
105 {
106         u32 val;
107
108         while (i2c_dev->msg_buf_remaining) {
109                 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
110                 if (!(val & BCM2835_I2C_S_RXD))
111                         break;
112                 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
113                                                       BCM2835_I2C_FIFO);
114                 i2c_dev->msg_buf++;
115                 i2c_dev->msg_buf_remaining--;
116         }
117 }
118
119 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
120 {
121         struct bcm2835_i2c_dev *i2c_dev = data;
122         u32 val, err;
123
124         val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
125         val &= BCM2835_I2C_BITMSK_S;
126         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val);
127
128         err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
129         if (err) {
130                 i2c_dev->msg_err = err;
131                 complete(&i2c_dev->completion);
132                 return IRQ_HANDLED;
133         }
134
135         if (val & BCM2835_I2C_S_DONE) {
136                 if (!i2c_dev->curr_msg) {
137                         dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
138                 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
139                         bcm2835_drain_rxfifo(i2c_dev);
140                         val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
141                 }
142
143                 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
144                         i2c_dev->msg_err = BCM2835_I2C_S_LEN;
145                 else
146                         i2c_dev->msg_err = 0;
147                 complete(&i2c_dev->completion);
148                 return IRQ_HANDLED;
149         }
150
151         if (val & BCM2835_I2C_S_TXW) {
152                 bcm2835_fill_txfifo(i2c_dev);
153                 return IRQ_HANDLED;
154         }
155
156         if (val & BCM2835_I2C_S_RXR) {
157                 bcm2835_drain_rxfifo(i2c_dev);
158                 return IRQ_HANDLED;
159         }
160
161         return IRQ_NONE;
162 }
163
164 static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
165                                 struct i2c_msg *msg)
166 {
167         u32 c;
168         unsigned long time_left;
169
170         i2c_dev->curr_msg = msg;
171         i2c_dev->msg_buf = msg->buf;
172         i2c_dev->msg_buf_remaining = msg->len;
173         reinit_completion(&i2c_dev->completion);
174
175         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
176
177         if (msg->flags & I2C_M_RD) {
178                 c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
179         } else {
180                 c = BCM2835_I2C_C_INTT;
181                 bcm2835_fill_txfifo(i2c_dev);
182         }
183         c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
184
185         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
186         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
187         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
188
189         time_left = wait_for_completion_timeout(&i2c_dev->completion,
190                                                 BCM2835_I2C_TIMEOUT);
191         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
192         if (!time_left) {
193                 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
194                 return -ETIMEDOUT;
195         }
196
197         if (likely(!i2c_dev->msg_err))
198                 return 0;
199
200         if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
201             (msg->flags & I2C_M_IGNORE_NAK))
202                 return 0;
203
204         dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
205
206         if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
207                 return -EREMOTEIO;
208         else
209                 return -EIO;
210 }
211
212 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
213                             int num)
214 {
215         struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
216         int i;
217         int ret = 0;
218
219         for (i = 0; i < num; i++) {
220                 ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
221                 if (ret)
222                         break;
223         }
224
225         return ret ?: i;
226 }
227
228 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
229 {
230         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
231 }
232
233 static const struct i2c_algorithm bcm2835_i2c_algo = {
234         .master_xfer    = bcm2835_i2c_xfer,
235         .functionality  = bcm2835_i2c_func,
236 };
237
238 /*
239  * This HW was reported to have problems with clock stretching:
240  * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
241  * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
242  */
243 static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
244         .flags = I2C_AQ_NO_CLK_STRETCH,
245 };
246
247 static int bcm2835_i2c_probe(struct platform_device *pdev)
248 {
249         struct bcm2835_i2c_dev *i2c_dev;
250         struct resource *mem, *irq;
251         u32 bus_clk_rate, divider;
252         int ret;
253         struct i2c_adapter *adap;
254
255         i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
256         if (!i2c_dev)
257                 return -ENOMEM;
258         platform_set_drvdata(pdev, i2c_dev);
259         i2c_dev->dev = &pdev->dev;
260         init_completion(&i2c_dev->completion);
261
262         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
263         i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
264         if (IS_ERR(i2c_dev->regs))
265                 return PTR_ERR(i2c_dev->regs);
266
267         i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
268         if (IS_ERR(i2c_dev->clk)) {
269                 if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
270                         dev_err(&pdev->dev, "Could not get clock\n");
271                 return PTR_ERR(i2c_dev->clk);
272         }
273
274         ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
275                                    &bus_clk_rate);
276         if (ret < 0) {
277                 dev_warn(&pdev->dev,
278                          "Could not read clock-frequency property\n");
279                 bus_clk_rate = 100000;
280         }
281
282         divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
283         /*
284          * Per the datasheet, the register is always interpreted as an even
285          * number, by rounding down. In other words, the LSB is ignored. So,
286          * if the LSB is set, increment the divider to avoid any issue.
287          */
288         if (divider & 1)
289                 divider++;
290         if ((divider < BCM2835_I2C_CDIV_MIN) ||
291             (divider > BCM2835_I2C_CDIV_MAX)) {
292                 dev_err(&pdev->dev, "Invalid clock-frequency\n");
293                 return -ENODEV;
294         }
295         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
296
297         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
298         if (!irq) {
299                 dev_err(&pdev->dev, "No IRQ resource\n");
300                 return -ENODEV;
301         }
302         i2c_dev->irq = irq->start;
303
304         ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
305                           dev_name(&pdev->dev), i2c_dev);
306         if (ret) {
307                 dev_err(&pdev->dev, "Could not request IRQ\n");
308                 return -ENODEV;
309         }
310
311         adap = &i2c_dev->adapter;
312         i2c_set_adapdata(adap, i2c_dev);
313         adap->owner = THIS_MODULE;
314         adap->class = I2C_CLASS_DEPRECATED;
315         strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
316         adap->algo = &bcm2835_i2c_algo;
317         adap->dev.parent = &pdev->dev;
318         adap->dev.of_node = pdev->dev.of_node;
319         adap->quirks = &bcm2835_i2c_quirks;
320
321         /*
322          * Disable the hardware clock stretching timeout. SMBUS
323          * specifies a limit for how long the device can stretch the
324          * clock, but core I2C doesn't.
325          */
326         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_CLKT, 0);
327         bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
328
329         ret = i2c_add_adapter(adap);
330         if (ret)
331                 free_irq(i2c_dev->irq, i2c_dev);
332
333         return ret;
334 }
335
336 static int bcm2835_i2c_remove(struct platform_device *pdev)
337 {
338         struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
339
340         free_irq(i2c_dev->irq, i2c_dev);
341         i2c_del_adapter(&i2c_dev->adapter);
342
343         return 0;
344 }
345
346 static const struct of_device_id bcm2835_i2c_of_match[] = {
347         { .compatible = "brcm,bcm2835-i2c" },
348         {},
349 };
350 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
351
352 static struct platform_driver bcm2835_i2c_driver = {
353         .probe          = bcm2835_i2c_probe,
354         .remove         = bcm2835_i2c_remove,
355         .driver         = {
356                 .name   = "i2c-bcm2835",
357                 .of_match_table = bcm2835_i2c_of_match,
358         },
359 };
360 module_platform_driver(bcm2835_i2c_driver);
361
362 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
363 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
364 MODULE_LICENSE("GPL v2");
365 MODULE_ALIAS("platform:i2c-bcm2835");