blob: cd07a69e2e9355540442785f95e90823b05c9d10 [file] [log] [blame]
Stephen Warrenf3b54b92013-02-11 19:47:56 -07001/*
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#define BCM2835_I2C_CLKT 0x1c
32
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)
40
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 */
52
Silvan Wickia294aba2015-06-18 11:10:11 +020053#define BCM2835_I2C_CDIV_MIN 0x0002
54#define BCM2835_I2C_CDIV_MAX 0xFFFE
55
Stephen Warrenf3b54b92013-02-11 19:47:56 -070056struct bcm2835_i2c_dev {
57 struct device *dev;
58 void __iomem *regs;
59 struct clk *clk;
60 int irq;
Noralf Trønnes9446f622016-10-03 22:06:14 +020061 u32 bus_clk_rate;
Stephen Warrenf3b54b92013-02-11 19:47:56 -070062 struct i2c_adapter adapter;
63 struct completion completion;
Noralf Trønnese2474542016-10-03 22:06:08 +020064 struct i2c_msg *curr_msg;
Noralf Trønnesee05fea2016-10-03 22:06:12 +020065 int num_msgs;
Stephen Warrenf3b54b92013-02-11 19:47:56 -070066 u32 msg_err;
67 u8 *msg_buf;
68 size_t msg_buf_remaining;
69};
70
71static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
72 u32 reg, u32 val)
73{
74 writel(val, i2c_dev->regs + reg);
75}
76
77static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
78{
79 return readl(i2c_dev->regs + reg);
80}
81
Noralf Trønnes9446f622016-10-03 22:06:14 +020082static int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev *i2c_dev)
83{
84 u32 divider;
85
86 divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk),
87 i2c_dev->bus_clk_rate);
88 /*
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.
92 */
93 if (divider & 1)
94 divider++;
95 if ((divider < BCM2835_I2C_CDIV_MIN) ||
96 (divider > BCM2835_I2C_CDIV_MAX)) {
97 dev_err_ratelimited(i2c_dev->dev, "Invalid clock-frequency\n");
98 return -EINVAL;
99 }
100
101 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
102
103 return 0;
104}
105
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700106static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
107{
108 u32 val;
109
110 while (i2c_dev->msg_buf_remaining) {
111 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
112 if (!(val & BCM2835_I2C_S_TXD))
113 break;
114 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
115 *i2c_dev->msg_buf);
116 i2c_dev->msg_buf++;
117 i2c_dev->msg_buf_remaining--;
118 }
119}
120
121static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
122{
123 u32 val;
124
125 while (i2c_dev->msg_buf_remaining) {
126 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
127 if (!(val & BCM2835_I2C_S_RXD))
128 break;
129 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
130 BCM2835_I2C_FIFO);
131 i2c_dev->msg_buf++;
132 i2c_dev->msg_buf_remaining--;
133 }
134}
135
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200136/*
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200137 * Repeated Start Condition (Sr)
138 * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
139 * talks about reading from a slave with 10 bit address. This is achieved by
140 * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
141 * issue a read.
142 * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
143 * firmware actually does it using polling and says that it's a workaround for
144 * a problem in the state machine.
145 * It turns out that it is possible to use the TXW interrupt to know when the
146 * transfer is active, provided the FIFO has not been prefilled.
147 */
148
149static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
150{
151 u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
152 struct i2c_msg *msg = i2c_dev->curr_msg;
153 bool last_msg = (i2c_dev->num_msgs == 1);
154
155 if (!i2c_dev->num_msgs)
156 return;
157
158 i2c_dev->num_msgs--;
159 i2c_dev->msg_buf = msg->buf;
160 i2c_dev->msg_buf_remaining = msg->len;
161
162 if (msg->flags & I2C_M_RD)
163 c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
164 else
165 c |= BCM2835_I2C_C_INTT;
166
167 if (last_msg)
168 c |= BCM2835_I2C_C_INTD;
169
170 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
171 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
172 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
173}
174
175/*
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200176 * Note about I2C_C_CLEAR on error:
177 * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
178 * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
179 * the state machine to send a NACK and a STOP. Since we're setting CLEAR
180 * without I2CEN, that NACK will be hanging around queued up for next time
181 * we start the engine.
182 */
183
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700184static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
185{
186 struct bcm2835_i2c_dev *i2c_dev = data;
187 u32 val, err;
188
189 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700190
191 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
192 if (err) {
193 i2c_dev->msg_err = err;
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200194 goto complete;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700195 }
196
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700197 if (val & BCM2835_I2C_S_DONE) {
Stefan Wahrenababb082017-02-16 21:20:45 +0000198 if (!i2c_dev->curr_msg) {
199 dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
200 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
Noralf Trønnese2474542016-10-03 22:06:08 +0200201 bcm2835_drain_rxfifo(i2c_dev);
202 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
203 }
204
205 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700206 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
207 else
208 i2c_dev->msg_err = 0;
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200209 goto complete;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700210 }
211
Noralf Trønnese2474542016-10-03 22:06:08 +0200212 if (val & BCM2835_I2C_S_TXW) {
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200213 if (!i2c_dev->msg_buf_remaining) {
214 i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
215 goto complete;
216 }
217
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700218 bcm2835_fill_txfifo(i2c_dev);
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200219
220 if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
221 i2c_dev->curr_msg++;
222 bcm2835_i2c_start_transfer(i2c_dev);
223 }
224
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700225 return IRQ_HANDLED;
226 }
227
Noralf Trønnese2474542016-10-03 22:06:08 +0200228 if (val & BCM2835_I2C_S_RXR) {
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200229 if (!i2c_dev->msg_buf_remaining) {
230 i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
231 goto complete;
232 }
233
Noralf Trønnese2474542016-10-03 22:06:08 +0200234 bcm2835_drain_rxfifo(i2c_dev);
235 return IRQ_HANDLED;
236 }
237
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700238 return IRQ_NONE;
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200239
240complete:
241 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
242 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
243 BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
244 complete(&i2c_dev->completion);
245
246 return IRQ_HANDLED;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700247}
248
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200249static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
250 int num)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700251{
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200252 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Nicholas Mc Guirec4dc1212015-02-08 06:04:18 -0500253 unsigned long time_left;
Noralf Trønnes9446f622016-10-03 22:06:14 +0200254 int i, ret;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700255
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200256 for (i = 0; i < (num - 1); i++)
257 if (msgs[i].flags & I2C_M_RD) {
258 dev_warn_once(i2c_dev->dev,
259 "only one read message supported, has to be last\n");
260 return -EOPNOTSUPP;
261 }
262
Noralf Trønnes9446f622016-10-03 22:06:14 +0200263 ret = bcm2835_i2c_set_divider(i2c_dev);
264 if (ret)
265 return ret;
266
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200267 i2c_dev->curr_msg = msgs;
268 i2c_dev->num_msgs = num;
Wolfram Sang16735d02013-11-14 14:32:02 -0800269 reinit_completion(&i2c_dev->completion);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700270
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200271 bcm2835_i2c_start_transfer(i2c_dev);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700272
273 time_left = wait_for_completion_timeout(&i2c_dev->completion,
Noralf Trønnese13e19e2016-10-03 22:06:13 +0200274 adap->timeout);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700275 if (!time_left) {
Noralf Trønnesd4030d72016-10-03 22:06:09 +0200276 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
277 BCM2835_I2C_C_CLEAR);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700278 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
279 return -ETIMEDOUT;
280 }
281
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200282 if (!i2c_dev->msg_err)
283 return num;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700284
Noralf Trønnes23c95402016-10-03 22:06:10 +0200285 dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700286
287 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
288 return -EREMOTEIO;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700289
Noralf Trønnesee05fea2016-10-03 22:06:12 +0200290 return -EIO;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700291}
292
293static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
294{
295 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
296}
297
298static const struct i2c_algorithm bcm2835_i2c_algo = {
299 .master_xfer = bcm2835_i2c_xfer,
300 .functionality = bcm2835_i2c_func,
301};
302
Nicola Corna4dbfb5f2015-10-29 12:34:25 +0100303/*
304 * This HW was reported to have problems with clock stretching:
305 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
306 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
307 */
308static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
309 .flags = I2C_AQ_NO_CLK_STRETCH,
310};
311
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700312static int bcm2835_i2c_probe(struct platform_device *pdev)
313{
314 struct bcm2835_i2c_dev *i2c_dev;
Jingoo Hanae50b1d2014-02-11 22:02:36 +0900315 struct resource *mem, *irq;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700316 int ret;
317 struct i2c_adapter *adap;
318
319 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900320 if (!i2c_dev)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700321 return -ENOMEM;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700322 platform_set_drvdata(pdev, i2c_dev);
323 i2c_dev->dev = &pdev->dev;
324 init_completion(&i2c_dev->completion);
325
326 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Hanae50b1d2014-02-11 22:02:36 +0900327 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
328 if (IS_ERR(i2c_dev->regs))
329 return PTR_ERR(i2c_dev->regs);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700330
331 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
332 if (IS_ERR(i2c_dev->clk)) {
Eric Anholt85946ab2016-06-01 23:43:33 +0200333 if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
334 dev_err(&pdev->dev, "Could not get clock\n");
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700335 return PTR_ERR(i2c_dev->clk);
336 }
337
338 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
Noralf Trønnes9446f622016-10-03 22:06:14 +0200339 &i2c_dev->bus_clk_rate);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700340 if (ret < 0) {
341 dev_warn(&pdev->dev,
342 "Could not read clock-frequency property\n");
Noralf Trønnes9446f622016-10-03 22:06:14 +0200343 i2c_dev->bus_clk_rate = 100000;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700344 }
345
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700346 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
347 if (!irq) {
348 dev_err(&pdev->dev, "No IRQ resource\n");
349 return -ENODEV;
350 }
351 i2c_dev->irq = irq->start;
352
353 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
354 dev_name(&pdev->dev), i2c_dev);
355 if (ret) {
356 dev_err(&pdev->dev, "Could not request IRQ\n");
357 return -ENODEV;
358 }
359
360 adap = &i2c_dev->adapter;
361 i2c_set_adapdata(adap, i2c_dev);
362 adap->owner = THIS_MODULE;
Wolfram Sang37e4f912014-07-10 13:46:23 +0200363 adap->class = I2C_CLASS_DEPRECATED;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700364 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
365 adap->algo = &bcm2835_i2c_algo;
366 adap->dev.parent = &pdev->dev;
Florian Meier07a27a02013-11-25 09:01:50 +0100367 adap->dev.of_node = pdev->dev.of_node;
Nicola Corna4dbfb5f2015-10-29 12:34:25 +0100368 adap->quirks = &bcm2835_i2c_quirks;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700369
370 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
371
372 ret = i2c_add_adapter(adap);
373 if (ret)
374 free_irq(i2c_dev->irq, i2c_dev);
375
376 return ret;
377}
378
379static int bcm2835_i2c_remove(struct platform_device *pdev)
380{
381 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
382
383 free_irq(i2c_dev->irq, i2c_dev);
384 i2c_del_adapter(&i2c_dev->adapter);
385
386 return 0;
387}
388
389static const struct of_device_id bcm2835_i2c_of_match[] = {
390 { .compatible = "brcm,bcm2835-i2c" },
391 {},
392};
393MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
394
395static struct platform_driver bcm2835_i2c_driver = {
396 .probe = bcm2835_i2c_probe,
397 .remove = bcm2835_i2c_remove,
398 .driver = {
399 .name = "i2c-bcm2835",
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700400 .of_match_table = bcm2835_i2c_of_match,
401 },
402};
403module_platform_driver(bcm2835_i2c_driver);
404
405MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
406MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
407MODULE_LICENSE("GPL v2");
408MODULE_ALIAS("platform:i2c-bcm2835");