blob: dee0352737d722c94683ef2b1352576e91e8bd81 [file] [log] [blame]
Peter Korsgaard18f98b12006-06-04 20:01:08 +02001/*
2 * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
3 * (http://www.opencores.org/projects.cgi/web/i2c/overview).
4 *
5 * Peter Korsgaard <jacmet@sunsite.dk>
6 *
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
10 */
11
Peter Korsgaard18f98b12006-06-04 20:01:08 +020012#include <linux/kernel.h>
13#include <linux/module.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020014#include <linux/init.h>
15#include <linux/errno.h>
16#include <linux/platform_device.h>
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
19#include <linux/wait.h>
20#include <linux/i2c-ocores.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020022#include <linux/io.h>
Peter Korsgaard18f98b12006-06-04 20:01:08 +020023
24struct ocores_i2c {
25 void __iomem *base;
26 int regstep;
27 wait_queue_head_t wait;
28 struct i2c_adapter adap;
29 struct i2c_msg *msg;
30 int pos;
31 int nmsgs;
32 int state; /* see STATE_ */
Manuel Lauss2373c182008-07-14 22:38:33 +020033 int clock_khz;
Peter Korsgaard18f98b12006-06-04 20:01:08 +020034};
35
36/* registers */
37#define OCI2C_PRELOW 0
38#define OCI2C_PREHIGH 1
39#define OCI2C_CONTROL 2
40#define OCI2C_DATA 3
Peter Korsgaard1ded9692006-06-12 21:40:53 +020041#define OCI2C_CMD 4 /* write only */
42#define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
Peter Korsgaard18f98b12006-06-04 20:01:08 +020043
44#define OCI2C_CTRL_IEN 0x40
45#define OCI2C_CTRL_EN 0x80
46
47#define OCI2C_CMD_START 0x91
48#define OCI2C_CMD_STOP 0x41
49#define OCI2C_CMD_READ 0x21
50#define OCI2C_CMD_WRITE 0x11
51#define OCI2C_CMD_READ_ACK 0x21
52#define OCI2C_CMD_READ_NACK 0x29
53#define OCI2C_CMD_IACK 0x01
54
55#define OCI2C_STAT_IF 0x01
56#define OCI2C_STAT_TIP 0x02
57#define OCI2C_STAT_ARBLOST 0x20
58#define OCI2C_STAT_BUSY 0x40
59#define OCI2C_STAT_NACK 0x80
60
61#define STATE_DONE 0
62#define STATE_START 1
63#define STATE_WRITE 2
64#define STATE_READ 3
65#define STATE_ERROR 4
66
67static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
68{
69 iowrite8(value, i2c->base + reg * i2c->regstep);
70}
71
72static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
73{
74 return ioread8(i2c->base + reg * i2c->regstep);
75}
76
77static void ocores_process(struct ocores_i2c *i2c)
78{
79 struct i2c_msg *msg = i2c->msg;
80 u8 stat = oc_getreg(i2c, OCI2C_STATUS);
81
82 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
83 /* stop has been sent */
84 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
85 wake_up(&i2c->wait);
86 return;
87 }
88
89 /* error? */
90 if (stat & OCI2C_STAT_ARBLOST) {
91 i2c->state = STATE_ERROR;
92 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
93 return;
94 }
95
96 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
97 i2c->state =
98 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
99
100 if (stat & OCI2C_STAT_NACK) {
101 i2c->state = STATE_ERROR;
102 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
103 return;
104 }
105 } else
106 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
107
108 /* end of msg? */
109 if (i2c->pos == msg->len) {
110 i2c->nmsgs--;
111 i2c->msg++;
112 i2c->pos = 0;
113 msg = i2c->msg;
114
115 if (i2c->nmsgs) { /* end? */
116 /* send start? */
117 if (!(msg->flags & I2C_M_NOSTART)) {
118 u8 addr = (msg->addr << 1);
119
120 if (msg->flags & I2C_M_RD)
121 addr |= 1;
122
123 i2c->state = STATE_START;
124
125 oc_setreg(i2c, OCI2C_DATA, addr);
126 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
127 return;
128 } else
129 i2c->state = (msg->flags & I2C_M_RD)
130 ? STATE_READ : STATE_WRITE;
131 } else {
132 i2c->state = STATE_DONE;
133 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
134 return;
135 }
136 }
137
138 if (i2c->state == STATE_READ) {
139 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
140 OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
141 } else {
142 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
143 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
144 }
145}
146
David Howells7d12e782006-10-05 14:55:46 +0100147static irqreturn_t ocores_isr(int irq, void *dev_id)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200148{
149 struct ocores_i2c *i2c = dev_id;
150
151 ocores_process(i2c);
152
153 return IRQ_HANDLED;
154}
155
156static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
157{
158 struct ocores_i2c *i2c = i2c_get_adapdata(adap);
159
160 i2c->msg = msgs;
161 i2c->pos = 0;
162 i2c->nmsgs = num;
163 i2c->state = STATE_START;
164
165 oc_setreg(i2c, OCI2C_DATA,
166 (i2c->msg->addr << 1) |
167 ((i2c->msg->flags & I2C_M_RD) ? 1:0));
168
169 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
170
171 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
172 (i2c->state == STATE_DONE), HZ))
173 return (i2c->state == STATE_DONE) ? num : -EIO;
174 else
175 return -ETIMEDOUT;
176}
177
Manuel Lauss2373c182008-07-14 22:38:33 +0200178static void ocores_init(struct ocores_i2c *i2c)
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200179{
180 int prescale;
181 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
182
183 /* make sure the device is disabled */
184 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
185
Manuel Lauss2373c182008-07-14 22:38:33 +0200186 prescale = (i2c->clock_khz / (5*100)) - 1;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200187 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
188 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
189
190 /* Init the device */
191 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
192 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
193}
194
195
196static u32 ocores_func(struct i2c_adapter *adap)
197{
198 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
199}
200
Jean Delvare8f9082c2006-09-03 22:39:46 +0200201static const struct i2c_algorithm ocores_algorithm = {
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200202 .master_xfer = ocores_xfer,
203 .functionality = ocores_func,
204};
205
206static struct i2c_adapter ocores_adapter = {
207 .owner = THIS_MODULE,
208 .name = "i2c-ocores",
Jean Delvare3401b2f2008-07-14 22:38:29 +0200209 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200210 .algo = &ocores_algorithm,
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200211};
212
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100213#ifdef CONFIG_OF
214static int ocores_i2c_of_probe(struct platform_device* pdev,
215 struct ocores_i2c* i2c)
216{
217 __be32* val;
218
219 val = of_get_property(pdev->dev.of_node, "regstep", NULL);
220 if (!val) {
221 dev_err(&pdev->dev, "Missing required parameter 'regstep'");
222 return -ENODEV;
223 }
224 i2c->regstep = be32_to_cpup(val);
225
226 val = of_get_property(pdev->dev.of_node, "clock-frequency", NULL);
227 if (!val) {
228 dev_err(&pdev->dev,
229 "Missing required parameter 'clock-frequency'");
230 return -ENODEV;
231 }
232 i2c->clock_khz = be32_to_cpup(val) / 1000;
233
234 return 0;
235}
236#else
237#define ocores_i2c_of_probe(pdev,i2c) -ENODEV
238#endif
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200239
240static int __devinit ocores_i2c_probe(struct platform_device *pdev)
241{
242 struct ocores_i2c *i2c;
243 struct ocores_i2c_platform_data *pdata;
244 struct resource *res, *res2;
245 int ret;
Richard Röjforsdd14be42009-06-05 15:40:32 +0200246 int i;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200247
248 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249 if (!res)
250 return -ENODEV;
251
252 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
253 if (!res2)
254 return -ENODEV;
255
Jonas Bonn47def5b2010-11-24 17:26:21 +0100256 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200257 if (!i2c)
258 return -ENOMEM;
259
Jonas Bonn47def5b2010-11-24 17:26:21 +0100260 if (!devm_request_mem_region(&pdev->dev, res->start,
261 resource_size(res), pdev->name)) {
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200262 dev_err(&pdev->dev, "Memory region busy\n");
Jonas Bonn47def5b2010-11-24 17:26:21 +0100263 return -EBUSY;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200264 }
265
Jonas Bonn47def5b2010-11-24 17:26:21 +0100266 i2c->base = devm_ioremap_nocache(&pdev->dev, res->start,
267 resource_size(res));
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200268 if (!i2c->base) {
269 dev_err(&pdev->dev, "Unable to map registers\n");
Jonas Bonn47def5b2010-11-24 17:26:21 +0100270 return -EIO;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200271 }
272
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100273 pdata = pdev->dev.platform_data;
274 if (pdata) {
275 i2c->regstep = pdata->regstep;
276 i2c->clock_khz = pdata->clock_khz;
277 } else {
278 ret = ocores_i2c_of_probe(pdev, i2c);
279 if (ret)
280 return ret;
281 }
282
Manuel Lauss2373c182008-07-14 22:38:33 +0200283 ocores_init(i2c);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200284
285 init_waitqueue_head(&i2c->wait);
Jonas Bonn47def5b2010-11-24 17:26:21 +0100286 ret = devm_request_irq(&pdev->dev, res2->start, ocores_isr, 0,
287 pdev->name, i2c);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200288 if (ret) {
289 dev_err(&pdev->dev, "Cannot claim IRQ\n");
Jonas Bonn47def5b2010-11-24 17:26:21 +0100290 return ret;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200291 }
292
293 /* hook up driver to tree */
294 platform_set_drvdata(pdev, i2c);
295 i2c->adap = ocores_adapter;
296 i2c_set_adapdata(&i2c->adap, i2c);
297 i2c->adap.dev.parent = &pdev->dev;
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100298#ifdef CONFIG_OF
299 i2c->adap.dev.of_node = pdev->dev.of_node;
300#endif
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200301
302 /* add i2c adapter to i2c tree */
303 ret = i2c_add_adapter(&i2c->adap);
304 if (ret) {
305 dev_err(&pdev->dev, "Failed to add adapter\n");
Jonas Bonn47def5b2010-11-24 17:26:21 +0100306 return ret;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200307 }
308
Richard Röjforsdd14be42009-06-05 15:40:32 +0200309 /* add in known devices to the bus */
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100310 if (pdata) {
311 for (i = 0; i < pdata->num_devices; i++)
312 i2c_new_device(&i2c->adap, pdata->devices + i);
313 }
Richard Röjforsdd14be42009-06-05 15:40:32 +0200314
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200315 return 0;
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200316}
317
318static int __devexit ocores_i2c_remove(struct platform_device* pdev)
319{
320 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200321
322 /* disable i2c logic */
323 oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
324 & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
325
326 /* remove adapter & data */
327 i2c_del_adapter(&i2c->adap);
328 platform_set_drvdata(pdev, NULL);
329
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200330 return 0;
331}
332
Manuel Lauss2373c182008-07-14 22:38:33 +0200333#ifdef CONFIG_PM
334static int ocores_i2c_suspend(struct platform_device *pdev, pm_message_t state)
335{
336 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
337 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
338
339 /* make sure the device is disabled */
340 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
341
342 return 0;
343}
344
345static int ocores_i2c_resume(struct platform_device *pdev)
346{
347 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
348
349 ocores_init(i2c);
350
351 return 0;
352}
353#else
354#define ocores_i2c_suspend NULL
355#define ocores_i2c_resume NULL
356#endif
357
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100358#ifdef CONFIG_OF
359static struct of_device_id ocores_i2c_match[] = {
360 {
361 .compatible = "opencores,i2c-ocores",
362 },
363 {},
364};
365MODULE_DEVICE_TABLE(of, ocores_i2c_match);
366#endif
367
Kay Sieversadd8eda2008-04-22 22:16:49 +0200368/* work with hotplug and coldplug */
369MODULE_ALIAS("platform:ocores-i2c");
370
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200371static struct platform_driver ocores_i2c_driver = {
Manuel Lauss2373c182008-07-14 22:38:33 +0200372 .probe = ocores_i2c_probe,
373 .remove = __devexit_p(ocores_i2c_remove),
374 .suspend = ocores_i2c_suspend,
375 .resume = ocores_i2c_resume,
376 .driver = {
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200377 .owner = THIS_MODULE,
378 .name = "ocores-i2c",
Jonas Bonn049bb69d2010-11-24 17:26:20 +0100379#ifdef CONFIG_OF
380 .of_match_table = ocores_i2c_match,
381#endif
Peter Korsgaard18f98b12006-06-04 20:01:08 +0200382 },
383};
384
385static int __init ocores_i2c_init(void)
386{
387 return platform_driver_register(&ocores_i2c_driver);
388}
389
390static void __exit ocores_i2c_exit(void)
391{
392 platform_driver_unregister(&ocores_i2c_driver);
393}
394
395module_init(ocores_i2c_init);
396module_exit(ocores_i2c_exit);
397
398MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
399MODULE_DESCRIPTION("OpenCores I2C bus driver");
400MODULE_LICENSE("GPL");