blob: 7ed09865cb4b90a2445b99de2d2c25896ae1781e [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ønnes938f8e82016-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) {
Stefan Wahren08ae439c2017-02-16 21:20:45 +0000131 if (!i2c_dev->curr_msg) {
132 dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
133 } else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
Noralf Trønnes938f8e82016-10-03 22:06:08 +0200134 bcm2835_drain_rxfifo(i2c_dev);
135 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
136 }
137
138 if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700139 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
140 else
141 i2c_dev->msg_err = 0;
142 complete(&i2c_dev->completion);
143 return IRQ_HANDLED;
144 }
145
Noralf Trønnes938f8e82016-10-03 22:06:08 +0200146 if (val & BCM2835_I2C_S_TXW) {
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700147 bcm2835_fill_txfifo(i2c_dev);
148 return IRQ_HANDLED;
149 }
150
Noralf Trønnes938f8e82016-10-03 22:06:08 +0200151 if (val & BCM2835_I2C_S_RXR) {
152 bcm2835_drain_rxfifo(i2c_dev);
153 return IRQ_HANDLED;
154 }
155
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700156 return IRQ_NONE;
157}
158
159static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
160 struct i2c_msg *msg)
161{
162 u32 c;
Nicholas Mc Guirec4dc1212015-02-08 06:04:18 -0500163 unsigned long time_left;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700164
Noralf Trønnes938f8e82016-10-03 22:06:08 +0200165 i2c_dev->curr_msg = msg;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700166 i2c_dev->msg_buf = msg->buf;
167 i2c_dev->msg_buf_remaining = msg->len;
Wolfram Sang16735d02013-11-14 14:32:02 -0800168 reinit_completion(&i2c_dev->completion);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700169
170 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
171
172 if (msg->flags & I2C_M_RD) {
173 c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
174 } else {
175 c = BCM2835_I2C_C_INTT;
176 bcm2835_fill_txfifo(i2c_dev);
177 }
178 c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
179
180 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
181 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
182 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
183
184 time_left = wait_for_completion_timeout(&i2c_dev->completion,
185 BCM2835_I2C_TIMEOUT);
186 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
187 if (!time_left) {
188 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
189 return -ETIMEDOUT;
190 }
191
192 if (likely(!i2c_dev->msg_err))
193 return 0;
194
195 if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
196 (msg->flags & I2C_M_IGNORE_NAK))
197 return 0;
198
199 dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
200
201 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
202 return -EREMOTEIO;
203 else
204 return -EIO;
205}
206
207static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
208 int num)
209{
210 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
211 int i;
212 int ret = 0;
213
214 for (i = 0; i < num; i++) {
215 ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
216 if (ret)
217 break;
218 }
219
220 return ret ?: i;
221}
222
223static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
224{
225 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
226}
227
228static const struct i2c_algorithm bcm2835_i2c_algo = {
229 .master_xfer = bcm2835_i2c_xfer,
230 .functionality = bcm2835_i2c_func,
231};
232
Nicola Corna4dbfb5f2015-10-29 12:34:25 +0100233/*
234 * This HW was reported to have problems with clock stretching:
235 * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
236 * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
237 */
238static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
239 .flags = I2C_AQ_NO_CLK_STRETCH,
240};
241
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700242static int bcm2835_i2c_probe(struct platform_device *pdev)
243{
244 struct bcm2835_i2c_dev *i2c_dev;
Jingoo Hanae50b1d2014-02-11 22:02:36 +0900245 struct resource *mem, *irq;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700246 u32 bus_clk_rate, divider;
247 int ret;
248 struct i2c_adapter *adap;
249
250 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900251 if (!i2c_dev)
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700252 return -ENOMEM;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700253 platform_set_drvdata(pdev, i2c_dev);
254 i2c_dev->dev = &pdev->dev;
255 init_completion(&i2c_dev->completion);
256
257 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Hanae50b1d2014-02-11 22:02:36 +0900258 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
259 if (IS_ERR(i2c_dev->regs))
260 return PTR_ERR(i2c_dev->regs);
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700261
262 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
263 if (IS_ERR(i2c_dev->clk)) {
Eric Anholt85946ab2016-06-01 23:43:33 +0200264 if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
265 dev_err(&pdev->dev, "Could not get clock\n");
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700266 return PTR_ERR(i2c_dev->clk);
267 }
268
269 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
270 &bus_clk_rate);
271 if (ret < 0) {
272 dev_warn(&pdev->dev,
273 "Could not read clock-frequency property\n");
274 bus_clk_rate = 100000;
275 }
276
277 divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
278 /*
279 * Per the datasheet, the register is always interpreted as an even
280 * number, by rounding down. In other words, the LSB is ignored. So,
281 * if the LSB is set, increment the divider to avoid any issue.
282 */
283 if (divider & 1)
284 divider++;
Silvan Wickia294aba2015-06-18 11:10:11 +0200285 if ((divider < BCM2835_I2C_CDIV_MIN) ||
286 (divider > BCM2835_I2C_CDIV_MAX)) {
287 dev_err(&pdev->dev, "Invalid clock-frequency\n");
288 return -ENODEV;
289 }
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700290 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
291
292 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
293 if (!irq) {
294 dev_err(&pdev->dev, "No IRQ resource\n");
295 return -ENODEV;
296 }
297 i2c_dev->irq = irq->start;
298
299 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
300 dev_name(&pdev->dev), i2c_dev);
301 if (ret) {
302 dev_err(&pdev->dev, "Could not request IRQ\n");
303 return -ENODEV;
304 }
305
306 adap = &i2c_dev->adapter;
307 i2c_set_adapdata(adap, i2c_dev);
308 adap->owner = THIS_MODULE;
Wolfram Sang37e4f912014-07-10 13:46:23 +0200309 adap->class = I2C_CLASS_DEPRECATED;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700310 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
311 adap->algo = &bcm2835_i2c_algo;
312 adap->dev.parent = &pdev->dev;
Florian Meier07a27a02013-11-25 09:01:50 +0100313 adap->dev.of_node = pdev->dev.of_node;
Nicola Corna4dbfb5f2015-10-29 12:34:25 +0100314 adap->quirks = &bcm2835_i2c_quirks;
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700315
316 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
317
318 ret = i2c_add_adapter(adap);
319 if (ret)
320 free_irq(i2c_dev->irq, i2c_dev);
321
322 return ret;
323}
324
325static int bcm2835_i2c_remove(struct platform_device *pdev)
326{
327 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
328
329 free_irq(i2c_dev->irq, i2c_dev);
330 i2c_del_adapter(&i2c_dev->adapter);
331
332 return 0;
333}
334
335static const struct of_device_id bcm2835_i2c_of_match[] = {
336 { .compatible = "brcm,bcm2835-i2c" },
337 {},
338};
339MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
340
341static struct platform_driver bcm2835_i2c_driver = {
342 .probe = bcm2835_i2c_probe,
343 .remove = bcm2835_i2c_remove,
344 .driver = {
345 .name = "i2c-bcm2835",
Stephen Warrenf3b54b92013-02-11 19:47:56 -0700346 .of_match_table = bcm2835_i2c_of_match,
347 },
348};
349module_platform_driver(bcm2835_i2c_driver);
350
351MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
352MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
353MODULE_LICENSE("GPL v2");
354MODULE_ALIAS("platform:i2c-bcm2835");