x86/xen: resume timer irqs early
[linux/fpc-iii.git] / arch / mips / mti-sead3 / sead3-pic32-i2c-drv.c
blobb921e5ec507cb2d5946d130c2434c5f00f4ed9dc
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
6 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
7 */
8 #include <linux/delay.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/platform_device.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
18 #define PIC32_I2CxCON 0x0000
19 #define PIC32_I2CxCONCLR 0x0004
20 #define PIC32_I2CxCONSET 0x0008
21 #define PIC32_I2CxCONINV 0x000C
22 #define I2CCON_ON (1<<15)
23 #define I2CCON_FRZ (1<<14)
24 #define I2CCON_SIDL (1<<13)
25 #define I2CCON_SCLREL (1<<12)
26 #define I2CCON_STRICT (1<<11)
27 #define I2CCON_A10M (1<<10)
28 #define I2CCON_DISSLW (1<<9)
29 #define I2CCON_SMEN (1<<8)
30 #define I2CCON_GCEN (1<<7)
31 #define I2CCON_STREN (1<<6)
32 #define I2CCON_ACKDT (1<<5)
33 #define I2CCON_ACKEN (1<<4)
34 #define I2CCON_RCEN (1<<3)
35 #define I2CCON_PEN (1<<2)
36 #define I2CCON_RSEN (1<<1)
37 #define I2CCON_SEN (1<<0)
39 #define PIC32_I2CxSTAT 0x0010
40 #define PIC32_I2CxSTATCLR 0x0014
41 #define PIC32_I2CxSTATSET 0x0018
42 #define PIC32_I2CxSTATINV 0x001C
43 #define I2CSTAT_ACKSTAT (1<<15)
44 #define I2CSTAT_TRSTAT (1<<14)
45 #define I2CSTAT_BCL (1<<10)
46 #define I2CSTAT_GCSTAT (1<<9)
47 #define I2CSTAT_ADD10 (1<<8)
48 #define I2CSTAT_IWCOL (1<<7)
49 #define I2CSTAT_I2COV (1<<6)
50 #define I2CSTAT_DA (1<<5)
51 #define I2CSTAT_P (1<<4)
52 #define I2CSTAT_S (1<<3)
53 #define I2CSTAT_RW (1<<2)
54 #define I2CSTAT_RBF (1<<1)
55 #define I2CSTAT_TBF (1<<0)
57 #define PIC32_I2CxADD 0x0020
58 #define PIC32_I2CxADDCLR 0x0024
59 #define PIC32_I2CxADDSET 0x0028
60 #define PIC32_I2CxADDINV 0x002C
61 #define PIC32_I2CxMSK 0x0030
62 #define PIC32_I2CxMSKCLR 0x0034
63 #define PIC32_I2CxMSKSET 0x0038
64 #define PIC32_I2CxMSKINV 0x003C
65 #define PIC32_I2CxBRG 0x0040
66 #define PIC32_I2CxBRGCLR 0x0044
67 #define PIC32_I2CxBRGSET 0x0048
68 #define PIC32_I2CxBRGINV 0x004C
69 #define PIC32_I2CxTRN 0x0050
70 #define PIC32_I2CxTRNCLR 0x0054
71 #define PIC32_I2CxTRNSET 0x0058
72 #define PIC32_I2CxTRNINV 0x005C
73 #define PIC32_I2CxRCV 0x0060
75 struct i2c_platform_data {
76 u32 base;
77 struct i2c_adapter adap;
78 u32 xfer_timeout;
79 u32 ack_timeout;
80 u32 ctl_timeout;
83 extern u32 pic32_bus_readl(u32 reg);
84 extern void pic32_bus_writel(u32 val, u32 reg);
86 static inline void
87 StartI2C(struct i2c_platform_data *adap)
89 pr_debug("StartI2C\n");
90 pic32_bus_writel(I2CCON_SEN, adap->base + PIC32_I2CxCONSET);
93 static inline void
94 StopI2C(struct i2c_platform_data *adap)
96 pr_debug("StopI2C\n");
97 pic32_bus_writel(I2CCON_PEN, adap->base + PIC32_I2CxCONSET);
100 static inline void
101 AckI2C(struct i2c_platform_data *adap)
103 pr_debug("AckI2C\n");
104 pic32_bus_writel(I2CCON_ACKDT, adap->base + PIC32_I2CxCONCLR);
105 pic32_bus_writel(I2CCON_ACKEN, adap->base + PIC32_I2CxCONSET);
108 static inline void
109 NotAckI2C(struct i2c_platform_data *adap)
111 pr_debug("NakI2C\n");
112 pic32_bus_writel(I2CCON_ACKDT, adap->base + PIC32_I2CxCONSET);
113 pic32_bus_writel(I2CCON_ACKEN, adap->base + PIC32_I2CxCONSET);
116 static inline int
117 IdleI2C(struct i2c_platform_data *adap)
119 int i;
121 pr_debug("IdleI2C\n");
122 for (i = 0; i < adap->ctl_timeout; i++) {
123 if (((pic32_bus_readl(adap->base + PIC32_I2CxCON) &
124 (I2CCON_ACKEN | I2CCON_RCEN | I2CCON_PEN | I2CCON_RSEN |
125 I2CCON_SEN)) == 0) &&
126 ((pic32_bus_readl(adap->base + PIC32_I2CxSTAT) &
127 (I2CSTAT_TRSTAT)) == 0))
128 return 0;
129 udelay(1);
131 return -ETIMEDOUT;
134 static inline u32
135 MasterWriteI2C(struct i2c_platform_data *adap, u32 byte)
137 pr_debug("MasterWriteI2C\n");
139 pic32_bus_writel(byte, adap->base + PIC32_I2CxTRN);
141 return pic32_bus_readl(adap->base + PIC32_I2CxSTAT) & I2CSTAT_IWCOL;
144 static inline u32
145 MasterReadI2C(struct i2c_platform_data *adap)
147 pr_debug("MasterReadI2C\n");
149 pic32_bus_writel(I2CCON_RCEN, adap->base + PIC32_I2CxCONSET);
151 while (pic32_bus_readl(adap->base + PIC32_I2CxCON) & I2CCON_RCEN)
154 pic32_bus_writel(I2CSTAT_I2COV, adap->base + PIC32_I2CxSTATCLR);
156 return pic32_bus_readl(adap->base + PIC32_I2CxRCV);
159 static int
160 do_address(struct i2c_platform_data *adap, unsigned int addr, int rd)
162 pr_debug("doaddress\n");
164 IdleI2C(adap);
165 StartI2C(adap);
166 IdleI2C(adap);
168 addr <<= 1;
169 if (rd)
170 addr |= 1;
172 if (MasterWriteI2C(adap, addr))
173 return -EIO;
174 IdleI2C(adap);
175 if (pic32_bus_readl(adap->base + PIC32_I2CxSTAT) & I2CSTAT_ACKSTAT)
176 return -EIO;
177 return 0;
180 static int
181 i2c_read(struct i2c_platform_data *adap, unsigned char *buf,
182 unsigned int len)
184 int i;
185 u32 data;
187 pr_debug("i2c_read\n");
189 i = 0;
190 while (i < len) {
191 data = MasterReadI2C(adap);
192 buf[i++] = data;
193 if (i < len)
194 AckI2C(adap);
195 else
196 NotAckI2C(adap);
199 StopI2C(adap);
200 IdleI2C(adap);
201 return 0;
204 static int
205 i2c_write(struct i2c_platform_data *adap, unsigned char *buf,
206 unsigned int len)
208 int i;
209 u32 data;
211 pr_debug("i2c_write\n");
213 i = 0;
214 while (i < len) {
215 data = buf[i];
216 if (MasterWriteI2C(adap, data))
217 return -EIO;
218 IdleI2C(adap);
219 if (pic32_bus_readl(adap->base + PIC32_I2CxSTAT) &
220 I2CSTAT_ACKSTAT)
221 return -EIO;
222 i++;
225 StopI2C(adap);
226 IdleI2C(adap);
227 return 0;
230 static int
231 platform_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
233 struct i2c_platform_data *adap = i2c_adap->algo_data;
234 struct i2c_msg *p;
235 int i, err = 0;
237 pr_debug("platform_xfer\n");
238 for (i = 0; i < num; i++) {
239 #define __BUFSIZE 80
240 int ii;
241 static char buf[__BUFSIZE];
242 char *b = buf;
244 p = &msgs[i];
245 b += sprintf(buf, " [%d bytes]", p->len);
246 if ((p->flags & I2C_M_RD) == 0) {
247 for (ii = 0; ii < p->len; ii++) {
248 if (b < &buf[__BUFSIZE-4]) {
249 b += sprintf(b, " %02x", p->buf[ii]);
250 } else {
251 strcat(b, "...");
252 break;
256 pr_debug("xfer%d: DevAddr: %04x Op:%s Data:%s\n", i, p->addr,
257 (p->flags & I2C_M_RD) ? "Rd" : "Wr", buf);
261 for (i = 0; !err && i < num; i++) {
262 p = &msgs[i];
263 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
264 if (err || !p->len)
265 continue;
266 if (p->flags & I2C_M_RD)
267 err = i2c_read(adap, p->buf, p->len);
268 else
269 err = i2c_write(adap, p->buf, p->len);
272 /* Return the number of messages processed, or the error code. */
273 if (err == 0)
274 err = num;
276 return err;
279 static u32
280 platform_func(struct i2c_adapter *adap)
282 pr_debug("platform_algo\n");
283 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
286 static const struct i2c_algorithm platform_algo = {
287 .master_xfer = platform_xfer,
288 .functionality = platform_func,
291 static void i2c_platform_setup(struct i2c_platform_data *priv)
293 pr_debug("i2c_platform_setup\n");
295 pic32_bus_writel(500, priv->base + PIC32_I2CxBRG);
296 pic32_bus_writel(I2CCON_ON, priv->base + PIC32_I2CxCONCLR);
297 pic32_bus_writel(I2CCON_ON, priv->base + PIC32_I2CxCONSET);
298 pic32_bus_writel((I2CSTAT_BCL | I2CSTAT_IWCOL),
299 (priv->base + PIC32_I2CxSTATCLR));
302 static void i2c_platform_disable(struct i2c_platform_data *priv)
304 pr_debug("i2c_platform_disable\n");
307 static int i2c_platform_probe(struct platform_device *pdev)
309 struct i2c_platform_data *priv;
310 struct resource *r;
311 int ret;
313 pr_debug("i2c_platform_probe\n");
314 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
315 if (!r) {
316 ret = -ENODEV;
317 goto out;
320 priv = kzalloc(sizeof(struct i2c_platform_data), GFP_KERNEL);
321 if (!priv) {
322 ret = -ENOMEM;
323 goto out;
326 /* FIXME: need to allocate resource in PIC32 space */
327 #if 0
328 priv->base = bus_request_region(r->start, resource_size(r),
329 pdev->name);
330 #else
331 priv->base = r->start;
332 #endif
333 if (!priv->base) {
334 ret = -EBUSY;
335 goto out_mem;
338 priv->xfer_timeout = 200;
339 priv->ack_timeout = 200;
340 priv->ctl_timeout = 200;
342 priv->adap.nr = pdev->id;
343 priv->adap.algo = &platform_algo;
344 priv->adap.algo_data = priv;
345 priv->adap.dev.parent = &pdev->dev;
346 strlcpy(priv->adap.name, "PIC32 I2C", sizeof(priv->adap.name));
348 i2c_platform_setup(priv);
350 ret = i2c_add_numbered_adapter(&priv->adap);
351 if (ret == 0) {
352 platform_set_drvdata(pdev, priv);
353 return 0;
356 i2c_platform_disable(priv);
358 out_mem:
359 kfree(priv);
360 out:
361 return ret;
364 static int i2c_platform_remove(struct platform_device *pdev)
366 struct i2c_platform_data *priv = platform_get_drvdata(pdev);
368 pr_debug("i2c_platform_remove\n");
369 platform_set_drvdata(pdev, NULL);
370 i2c_del_adapter(&priv->adap);
371 i2c_platform_disable(priv);
372 kfree(priv);
373 return 0;
376 #ifdef CONFIG_PM
377 static int
378 i2c_platform_suspend(struct platform_device *pdev, pm_message_t state)
380 struct i2c_platform_data *priv = platform_get_drvdata(pdev);
382 dev_dbg(&pdev->dev, "i2c_platform_disable\n");
383 i2c_platform_disable(priv);
385 return 0;
388 static int
389 i2c_platform_resume(struct platform_device *pdev)
391 struct i2c_platform_data *priv = platform_get_drvdata(pdev);
393 dev_dbg(&pdev->dev, "i2c_platform_setup\n");
394 i2c_platform_setup(priv);
396 return 0;
398 #else
399 #define i2c_platform_suspend NULL
400 #define i2c_platform_resume NULL
401 #endif
403 static struct platform_driver i2c_platform_driver = {
404 .driver = {
405 .name = "i2c_pic32",
406 .owner = THIS_MODULE,
408 .probe = i2c_platform_probe,
409 .remove = i2c_platform_remove,
410 .suspend = i2c_platform_suspend,
411 .resume = i2c_platform_resume,
414 static int __init
415 i2c_platform_init(void)
417 pr_debug("i2c_platform_init\n");
418 return platform_driver_register(&i2c_platform_driver);
421 static void __exit
422 i2c_platform_exit(void)
424 pr_debug("i2c_platform_exit\n");
425 platform_driver_unregister(&i2c_platform_driver);
428 MODULE_AUTHOR("Chris Dearman, MIPS Technologies INC.");
429 MODULE_DESCRIPTION("PIC32 I2C driver");
430 MODULE_LICENSE("GPL");
432 module_init(i2c_platform_init);
433 module_exit(i2c_platform_exit);