Linux 4.1.16
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-bcm2835.c
blobc9336a3202d5620681b38d6e8c2b47651a75af0e
1 /*
2 * BCM2835 master mode driver
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.
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.
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>
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
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)
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 */
53 #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000))
55 struct bcm2835_i2c_dev {
56 struct device *dev;
57 void __iomem *regs;
58 struct clk *clk;
59 int irq;
60 struct i2c_adapter adapter;
61 struct completion completion;
62 u32 msg_err;
63 u8 *msg_buf;
64 size_t msg_buf_remaining;
67 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
68 u32 reg, u32 val)
70 writel(val, i2c_dev->regs + reg);
73 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
75 return readl(i2c_dev->regs + reg);
78 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
80 u32 val;
82 while (i2c_dev->msg_buf_remaining) {
83 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
84 if (!(val & BCM2835_I2C_S_TXD))
85 break;
86 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
87 *i2c_dev->msg_buf);
88 i2c_dev->msg_buf++;
89 i2c_dev->msg_buf_remaining--;
93 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
95 u32 val;
97 while (i2c_dev->msg_buf_remaining) {
98 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
99 if (!(val & BCM2835_I2C_S_RXD))
100 break;
101 *i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
102 BCM2835_I2C_FIFO);
103 i2c_dev->msg_buf++;
104 i2c_dev->msg_buf_remaining--;
108 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
110 struct bcm2835_i2c_dev *i2c_dev = data;
111 u32 val, err;
113 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
114 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val);
116 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
117 if (err) {
118 i2c_dev->msg_err = err;
119 complete(&i2c_dev->completion);
120 return IRQ_HANDLED;
123 if (val & BCM2835_I2C_S_RXD) {
124 bcm2835_drain_rxfifo(i2c_dev);
125 if (!(val & BCM2835_I2C_S_DONE))
126 return IRQ_HANDLED;
129 if (val & BCM2835_I2C_S_DONE) {
130 if (i2c_dev->msg_buf_remaining)
131 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
132 else
133 i2c_dev->msg_err = 0;
134 complete(&i2c_dev->completion);
135 return IRQ_HANDLED;
138 if (val & BCM2835_I2C_S_TXD) {
139 bcm2835_fill_txfifo(i2c_dev);
140 return IRQ_HANDLED;
143 return IRQ_NONE;
146 static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
147 struct i2c_msg *msg)
149 u32 c;
150 unsigned long time_left;
152 i2c_dev->msg_buf = msg->buf;
153 i2c_dev->msg_buf_remaining = msg->len;
154 reinit_completion(&i2c_dev->completion);
156 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
158 if (msg->flags & I2C_M_RD) {
159 c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
160 } else {
161 c = BCM2835_I2C_C_INTT;
162 bcm2835_fill_txfifo(i2c_dev);
164 c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN;
166 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
167 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
168 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
170 time_left = wait_for_completion_timeout(&i2c_dev->completion,
171 BCM2835_I2C_TIMEOUT);
172 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
173 if (!time_left) {
174 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
175 return -ETIMEDOUT;
178 if (likely(!i2c_dev->msg_err))
179 return 0;
181 if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) &&
182 (msg->flags & I2C_M_IGNORE_NAK))
183 return 0;
185 dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
187 if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
188 return -EREMOTEIO;
189 else
190 return -EIO;
193 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
194 int num)
196 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
197 int i;
198 int ret = 0;
200 for (i = 0; i < num; i++) {
201 ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]);
202 if (ret)
203 break;
206 return ret ?: i;
209 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
211 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
214 static const struct i2c_algorithm bcm2835_i2c_algo = {
215 .master_xfer = bcm2835_i2c_xfer,
216 .functionality = bcm2835_i2c_func,
219 static int bcm2835_i2c_probe(struct platform_device *pdev)
221 struct bcm2835_i2c_dev *i2c_dev;
222 struct resource *mem, *irq;
223 u32 bus_clk_rate, divider;
224 int ret;
225 struct i2c_adapter *adap;
227 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
228 if (!i2c_dev)
229 return -ENOMEM;
230 platform_set_drvdata(pdev, i2c_dev);
231 i2c_dev->dev = &pdev->dev;
232 init_completion(&i2c_dev->completion);
234 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
235 i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
236 if (IS_ERR(i2c_dev->regs))
237 return PTR_ERR(i2c_dev->regs);
239 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
240 if (IS_ERR(i2c_dev->clk)) {
241 dev_err(&pdev->dev, "Could not get clock\n");
242 return PTR_ERR(i2c_dev->clk);
245 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
246 &bus_clk_rate);
247 if (ret < 0) {
248 dev_warn(&pdev->dev,
249 "Could not read clock-frequency property\n");
250 bus_clk_rate = 100000;
253 divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate);
255 * Per the datasheet, the register is always interpreted as an even
256 * number, by rounding down. In other words, the LSB is ignored. So,
257 * if the LSB is set, increment the divider to avoid any issue.
259 if (divider & 1)
260 divider++;
261 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
263 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
264 if (!irq) {
265 dev_err(&pdev->dev, "No IRQ resource\n");
266 return -ENODEV;
268 i2c_dev->irq = irq->start;
270 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
271 dev_name(&pdev->dev), i2c_dev);
272 if (ret) {
273 dev_err(&pdev->dev, "Could not request IRQ\n");
274 return -ENODEV;
277 adap = &i2c_dev->adapter;
278 i2c_set_adapdata(adap, i2c_dev);
279 adap->owner = THIS_MODULE;
280 adap->class = I2C_CLASS_DEPRECATED;
281 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
282 adap->algo = &bcm2835_i2c_algo;
283 adap->dev.parent = &pdev->dev;
284 adap->dev.of_node = pdev->dev.of_node;
286 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
288 ret = i2c_add_adapter(adap);
289 if (ret)
290 free_irq(i2c_dev->irq, i2c_dev);
292 return ret;
295 static int bcm2835_i2c_remove(struct platform_device *pdev)
297 struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
299 free_irq(i2c_dev->irq, i2c_dev);
300 i2c_del_adapter(&i2c_dev->adapter);
302 return 0;
305 static const struct of_device_id bcm2835_i2c_of_match[] = {
306 { .compatible = "brcm,bcm2835-i2c" },
309 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
311 static struct platform_driver bcm2835_i2c_driver = {
312 .probe = bcm2835_i2c_probe,
313 .remove = bcm2835_i2c_remove,
314 .driver = {
315 .name = "i2c-bcm2835",
316 .of_match_table = bcm2835_i2c_of_match,
319 module_platform_driver(bcm2835_i2c_driver);
321 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
322 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
323 MODULE_LICENSE("GPL v2");
324 MODULE_ALIAS("platform:i2c-bcm2835");