blob: f283b714aa79e2e4685ed95b04b6b289f7e9eee7 [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 Wicki7b618632015-06-16 17:40:59 +020053#define BCM2835_I2C_BITMSK_S 0x03FF
54
Silvan Wickia294aba2015-06-18 11:10:11 +020055#define BCM2835_I2C_CDIV_MIN 0x0002
56#define BCM2835_I2C_CDIV_MAX 0xFFFE
57
Stephen Warrenf3b54b92013-02-11 19:47:56 -070058#define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
59
60struct bcm2835_i2c_dev {
61 struct device *dev;
62 void __iomem *regs;
63 struct clk *clk;
64 int irq;
65 struct i2c_adapter adapter;
66 struct completion completion;
Noralf Trønnese2474542016-10-03 22:06:08 +020067 struct i2c_msg *curr_msg;
Stephen Warrenf3b54b92013-02-11 19:47:56 -070068 u32 msg_err;
69 u8 *msg_buf;
70 size_t msg_buf_remaining;
71};
72
73static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
74 u32 reg, u32 val)
75{
76 writel(val, i2c_dev->regs + reg);
77}
78
79static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
80{
81 return readl(i2c_dev->regs + reg);
82}
83
84static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
85{
86 u32 val;
87
88 while (i2c_dev->msg_buf_remaining) {
89 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
90 if (!(val & BCM2835_I2C_S_TXD))
91 break;
92 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
93 *i2c_dev->msg_buf);
94 i2c_dev->msg_buf++;
95 i2c_dev->msg_buf_remaining--;
96 }
97}
98
99static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
100{
101 u32 val;
102
103 while (i2c_dev->msg_buf_remaining) {
104 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
105 if (!(val & BCM2835_I2C_S_RXD))
106 break;
107 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
108 BCM2835_I2C_FIFO);
109 i2c_dev->msg_buf++;
110 i2c_dev->msg_buf_remaining--;
111 }
112}
113
114static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
115{
116 struct bcm2835_i2c_dev *i2c_dev = data;
117 u32 val, err;
118
119 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
Silvan Wicki7b618632015-06-16 17:40:59 +0200120 val &= BCM2835_I2C_BITMSK_S;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700121 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val);
122
123 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
124 if (err) {
125 i2c_dev->msg_err = err;
126 complete(&i2c_dev->completion);
127 return IRQ_HANDLED;
128 }
129
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700130 if (val & BCM2835_I2C_S_DONE) {
Noralf Trønnese2474542016-10-03 22:06:08 +0200131 if (i2c_dev->curr_msg->flags & I2C_M_RD) {
132 bcm2835_drain_rxfifo(i2c_dev);
133 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
134 }
135
136 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700137 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
138 else
139 i2c_dev->msg_err = 0;
140 complete(&i2c_dev->completion);
141 return IRQ_HANDLED;
142 }
143
Noralf Trønnese2474542016-10-03 22:06:08 +0200144 if (val & BCM2835_I2C_S_TXW) {
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700145 bcm2835_fill_txfifo(i2c_dev);
146 return IRQ_HANDLED;
147 }
148
Noralf Trønnese2474542016-10-03 22:06:08 +0200149 if (val & BCM2835_I2C_S_RXR) {
150 bcm2835_drain_rxfifo(i2c_dev);
151 return IRQ_HANDLED;
152 }
153
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700154 return IRQ_NONE;
155}
156
157static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
158 struct i2c_msg *msg)
159{
160 u32 c;
Nicholas Mc Guirec4dc1212015-02-08 06:04:18 -0500161 unsigned long time_left;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700162
Noralf Trønnese2474542016-10-03 22:06:08 +0200163 i2c_dev->curr_msg = msg;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700164 i2c_dev->msg_buf = msg->buf;
165 i2c_dev->msg_buf_remaining = msg->len;
Wolfram Sang16735d02013-11-14 14:32:02 -0800166 reinit_completion(&i2c_dev->completion);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700167
168 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
169
170 if (msg->flags & I2C_M_RD) {
171 c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
172 } else {
173 c = BCM2835_I2C_C_INTT;
174 bcm2835_fill_txfifo(i2c_dev);
175 }
176 c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
177
178 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
179 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
180 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
181
182 time_left = wait_for_completion_timeout(&i2c_dev->completion,
183 BCM2835_I2C_TIMEOUT);
184 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
185 if (!time_left) {
186 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
187 return -ETIMEDOUT;
188 }
189
190 if (likely(!i2c_dev->msg_err))
191 return 0;
192
193 if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
194 (msg->flags & I2C_M_IGNORE_NAK))
195 return 0;
196
197 dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
198
199 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
200 return -EREMOTEIO;
201 else
202 return -EIO;
203}
204
205static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
206 int num)
207{
208 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
209 int i;
210 int ret = 0;
211
212 for (i = 0; i < num; i++) {
213 ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
214 if (ret)
215 break;
216 }
217
218 return ret ?: i;
219}
220
221static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
222{
223 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
224}
225
226static const struct i2c_algorithm bcm2835_i2c_algo = {
227 .master_xfer = bcm2835_i2c_xfer,
228 .functionality = bcm2835_i2c_func,
229};
230
Nicola Corna4dbfb5f2015-10-29 12:34:25 +0100231/*
232 * This HW was reported to have problems with clock stretching:
233 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
234 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
235 */
236static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
237 .flags = I2C_AQ_NO_CLK_STRETCH,
238};
239
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700240static int bcm2835_i2c_probe(struct platform_device *pdev)
241{
242 struct bcm2835_i2c_dev *i2c_dev;
Jingoo Hanae50b1d2014-02-11 22:02:36 +0900243 struct resource *mem, *irq;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700244 u32 bus_clk_rate, divider;
245 int ret;
246 struct i2c_adapter *adap;
247
248 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900249 if (!i2c_dev)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700250 return -ENOMEM;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700251 platform_set_drvdata(pdev, i2c_dev);
252 i2c_dev->dev = &pdev->dev;
253 init_completion(&i2c_dev->completion);
254
255 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Hanae50b1d2014-02-11 22:02:36 +0900256 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
257 if (IS_ERR(i2c_dev->regs))
258 return PTR_ERR(i2c_dev->regs);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700259
260 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
261 if (IS_ERR(i2c_dev->clk)) {
Eric Anholt85946ab2016-06-01 23:43:33 +0200262 if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
263 dev_err(&pdev->dev, "Could not get clock\n");
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700264 return PTR_ERR(i2c_dev->clk);
265 }
266
267 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
268 &bus_clk_rate);
269 if (ret < 0) {
270 dev_warn(&pdev->dev,
271 "Could not read clock-frequency property\n");
272 bus_clk_rate = 100000;
273 }
274
275 divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
276 /*
277 * Per the datasheet, the register is always interpreted as an even
278 * number, by rounding down. In other words, the LSB is ignored. So,
279 * if the LSB is set, increment the divider to avoid any issue.
280 */
281 if (divider & 1)
282 divider++;
Silvan Wickia294aba2015-06-18 11:10:11 +0200283 if ((divider < BCM2835_I2C_CDIV_MIN) ||
284 (divider > BCM2835_I2C_CDIV_MAX)) {
285 dev_err(&pdev->dev, "Invalid clock-frequency\n");
286 return -ENODEV;
287 }
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700288 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
289
290 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
291 if (!irq) {
292 dev_err(&pdev->dev, "No IRQ resource\n");
293 return -ENODEV;
294 }
295 i2c_dev->irq = irq->start;
296
297 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
298 dev_name(&pdev->dev), i2c_dev);
299 if (ret) {
300 dev_err(&pdev->dev, "Could not request IRQ\n");
301 return -ENODEV;
302 }
303
304 adap = &i2c_dev->adapter;
305 i2c_set_adapdata(adap, i2c_dev);
306 adap->owner = THIS_MODULE;
Wolfram Sang37e4f912014-07-10 13:46:23 +0200307 adap->class = I2C_CLASS_DEPRECATED;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700308 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
309 adap->algo = &bcm2835_i2c_algo;
310 adap->dev.parent = &pdev->dev;
Florian Meier07a27a02013-11-25 09:01:50 +0100311 adap->dev.of_node = pdev->dev.of_node;
Nicola Corna4dbfb5f2015-10-29 12:34:25 +0100312 adap->quirks = &bcm2835_i2c_quirks;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700313
314 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
315
316 ret = i2c_add_adapter(adap);
317 if (ret)
318 free_irq(i2c_dev->irq, i2c_dev);
319
320 return ret;
321}
322
323static int bcm2835_i2c_remove(struct platform_device *pdev)
324{
325 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
326
327 free_irq(i2c_dev->irq, i2c_dev);
328 i2c_del_adapter(&i2c_dev->adapter);
329
330 return 0;
331}
332
333static const struct of_device_id bcm2835_i2c_of_match[] = {
334 { .compatible = "brcm,bcm2835-i2c" },
335 {},
336};
337MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
338
339static struct platform_driver bcm2835_i2c_driver = {
340 .probe = bcm2835_i2c_probe,
341 .remove = bcm2835_i2c_remove,
342 .driver = {
343 .name = "i2c-bcm2835",
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700344 .of_match_table = bcm2835_i2c_of_match,
345 },
346};
347module_platform_driver(bcm2835_i2c_driver);
348
349MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
350MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
351MODULE_LICENSE("GPL v2");
352MODULE_ALIAS("platform:i2c-bcm2835");