2 * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
3 * (http://www.opencores.org/projects.cgi/web/i2c/overview).
5 * Peter Korsgaard <jacmet@sunsite.dk>
7 * Support for the GRLIB port of the controller by
8 * Andreas Larsson <andreas@gaisler.com>
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
12 * kind, whether express or implied.
16 * This driver can be used from the device tree, see
17 * Documentation/devicetree/bindings/i2c/ocore-i2c.txt
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/errno.h>
23 #include <linux/platform_device.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/wait.h>
27 #include <linux/i2c-ocores.h>
28 #include <linux/slab.h>
30 #include <linux/of_i2c.h>
31 #include <linux/log2.h>
37 wait_queue_head_t wait;
38 struct i2c_adapter adap;
42 int state; /* see STATE_ */
44 void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
45 u8 (*getreg)(struct ocores_i2c *i2c, int reg);
49 #define OCI2C_PRELOW 0
50 #define OCI2C_PREHIGH 1
51 #define OCI2C_CONTROL 2
53 #define OCI2C_CMD 4 /* write only */
54 #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
56 #define OCI2C_CTRL_IEN 0x40
57 #define OCI2C_CTRL_EN 0x80
59 #define OCI2C_CMD_START 0x91
60 #define OCI2C_CMD_STOP 0x41
61 #define OCI2C_CMD_READ 0x21
62 #define OCI2C_CMD_WRITE 0x11
63 #define OCI2C_CMD_READ_ACK 0x21
64 #define OCI2C_CMD_READ_NACK 0x29
65 #define OCI2C_CMD_IACK 0x01
67 #define OCI2C_STAT_IF 0x01
68 #define OCI2C_STAT_TIP 0x02
69 #define OCI2C_STAT_ARBLOST 0x20
70 #define OCI2C_STAT_BUSY 0x40
71 #define OCI2C_STAT_NACK 0x80
82 static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
84 iowrite8(value, i2c->base + (reg << i2c->reg_shift));
87 static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
89 iowrite16(value, i2c->base + (reg << i2c->reg_shift));
92 static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
94 iowrite32(value, i2c->base + (reg << i2c->reg_shift));
97 static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
99 return ioread8(i2c->base + (reg << i2c->reg_shift));
102 static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
104 return ioread16(i2c->base + (reg << i2c->reg_shift));
107 static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
109 return ioread32(i2c->base + (reg << i2c->reg_shift));
112 /* Read and write functions for the GRLIB port of the controller. Registers are
113 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
114 * register. The subsequent registers has their offset decreased accordingly. */
115 static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
119 if (reg != OCI2C_PRELOW)
121 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
122 if (reg == OCI2C_PREHIGH)
123 return (u8)(rd >> 8);
128 static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
132 if (reg != OCI2C_PRELOW)
134 if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
135 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
136 if (reg == OCI2C_PRELOW)
137 wr = (curr & 0xff00) | value;
139 wr = (((u32)value) << 8) | (curr & 0xff);
143 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
146 static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
148 i2c->setreg(i2c, reg, value);
151 static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
153 return i2c->getreg(i2c, reg);
156 static void ocores_process(struct ocores_i2c *i2c)
158 struct i2c_msg *msg = i2c->msg;
159 u8 stat = oc_getreg(i2c, OCI2C_STATUS);
161 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
162 /* stop has been sent */
163 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
169 if (stat & OCI2C_STAT_ARBLOST) {
170 i2c->state = STATE_ERROR;
171 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
175 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
177 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
179 if (stat & OCI2C_STAT_NACK) {
180 i2c->state = STATE_ERROR;
181 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
185 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
188 if (i2c->pos == msg->len) {
194 if (i2c->nmsgs) { /* end? */
196 if (!(msg->flags & I2C_M_NOSTART)) {
197 u8 addr = (msg->addr << 1);
199 if (msg->flags & I2C_M_RD)
202 i2c->state = STATE_START;
204 oc_setreg(i2c, OCI2C_DATA, addr);
205 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
208 i2c->state = (msg->flags & I2C_M_RD)
209 ? STATE_READ : STATE_WRITE;
211 i2c->state = STATE_DONE;
212 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
217 if (i2c->state == STATE_READ) {
218 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
219 OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
221 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
222 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
226 static irqreturn_t ocores_isr(int irq, void *dev_id)
228 struct ocores_i2c *i2c = dev_id;
235 static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
237 struct ocores_i2c *i2c = i2c_get_adapdata(adap);
242 i2c->state = STATE_START;
244 oc_setreg(i2c, OCI2C_DATA,
245 (i2c->msg->addr << 1) |
246 ((i2c->msg->flags & I2C_M_RD) ? 1:0));
248 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
250 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
251 (i2c->state == STATE_DONE), HZ))
252 return (i2c->state == STATE_DONE) ? num : -EIO;
257 static void ocores_init(struct ocores_i2c *i2c)
260 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
262 /* make sure the device is disabled */
263 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
265 prescale = (i2c->clock_khz / (5*100)) - 1;
266 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
267 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
269 /* Init the device */
270 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
271 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
275 static u32 ocores_func(struct i2c_adapter *adap)
277 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
280 static const struct i2c_algorithm ocores_algorithm = {
281 .master_xfer = ocores_xfer,
282 .functionality = ocores_func,
285 static struct i2c_adapter ocores_adapter = {
286 .owner = THIS_MODULE,
287 .name = "i2c-ocores",
288 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
289 .algo = &ocores_algorithm,
292 static struct of_device_id ocores_i2c_match[] = {
294 .compatible = "opencores,i2c-ocores",
295 .data = (void *)TYPE_OCORES,
298 .compatible = "aeroflexgaisler,i2cmst",
299 .data = (void *)TYPE_GRLIB,
303 MODULE_DEVICE_TABLE(of, ocores_i2c_match);
306 static int ocores_i2c_of_probe(struct platform_device *pdev,
307 struct ocores_i2c *i2c)
309 struct device_node *np = pdev->dev.of_node;
310 const struct of_device_id *match;
313 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
314 /* no 'reg-shift', check for deprecated 'regstep' */
315 if (!of_property_read_u32(np, "regstep", &val)) {
316 if (!is_power_of_2(val)) {
317 dev_err(&pdev->dev, "invalid regstep %d\n",
321 i2c->reg_shift = ilog2(val);
323 "regstep property deprecated, use reg-shift\n");
327 if (of_property_read_u32(np, "clock-frequency", &val)) {
329 "Missing required parameter 'clock-frequency'\n");
332 i2c->clock_khz = val / 1000;
334 of_property_read_u32(pdev->dev.of_node, "reg-io-width",
337 match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
338 if (match && (int)match->data == TYPE_GRLIB) {
339 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
340 i2c->setreg = oc_setreg_grlib;
341 i2c->getreg = oc_getreg_grlib;
347 #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
350 static int __devinit ocores_i2c_probe(struct platform_device *pdev)
352 struct ocores_i2c *i2c;
353 struct ocores_i2c_platform_data *pdata;
354 struct resource *res;
359 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 irq = platform_get_irq(pdev, 0);
367 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
371 i2c->base = devm_request_and_ioremap(&pdev->dev, res);
373 return -EADDRNOTAVAIL;
375 pdata = pdev->dev.platform_data;
377 i2c->reg_shift = pdata->reg_shift;
378 i2c->reg_io_width = pdata->reg_io_width;
379 i2c->clock_khz = pdata->clock_khz;
381 ret = ocores_i2c_of_probe(pdev, i2c);
386 if (i2c->reg_io_width == 0)
387 i2c->reg_io_width = 1; /* Set to default value */
389 if (!i2c->setreg || !i2c->getreg) {
390 switch (i2c->reg_io_width) {
392 i2c->setreg = oc_setreg_8;
393 i2c->getreg = oc_getreg_8;
397 i2c->setreg = oc_setreg_16;
398 i2c->getreg = oc_getreg_16;
402 i2c->setreg = oc_setreg_32;
403 i2c->getreg = oc_getreg_32;
407 dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
415 init_waitqueue_head(&i2c->wait);
416 ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
419 dev_err(&pdev->dev, "Cannot claim IRQ\n");
423 /* hook up driver to tree */
424 platform_set_drvdata(pdev, i2c);
425 i2c->adap = ocores_adapter;
426 i2c_set_adapdata(&i2c->adap, i2c);
427 i2c->adap.dev.parent = &pdev->dev;
428 i2c->adap.dev.of_node = pdev->dev.of_node;
430 /* add i2c adapter to i2c tree */
431 ret = i2c_add_adapter(&i2c->adap);
433 dev_err(&pdev->dev, "Failed to add adapter\n");
437 /* add in known devices to the bus */
439 for (i = 0; i < pdata->num_devices; i++)
440 i2c_new_device(&i2c->adap, pdata->devices + i);
442 of_i2c_register_devices(&i2c->adap);
448 static int __devexit ocores_i2c_remove(struct platform_device *pdev)
450 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
452 /* disable i2c logic */
453 oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
454 & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
456 /* remove adapter & data */
457 i2c_del_adapter(&i2c->adap);
458 platform_set_drvdata(pdev, NULL);
464 static int ocores_i2c_suspend(struct device *dev)
466 struct ocores_i2c *i2c = dev_get_drvdata(dev);
467 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
469 /* make sure the device is disabled */
470 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
475 static int ocores_i2c_resume(struct device *dev)
477 struct ocores_i2c *i2c = dev_get_drvdata(dev);
484 static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
485 #define OCORES_I2C_PM (&ocores_i2c_pm)
487 #define OCORES_I2C_PM NULL
490 static struct platform_driver ocores_i2c_driver = {
491 .probe = ocores_i2c_probe,
492 .remove = __devexit_p(ocores_i2c_remove),
494 .owner = THIS_MODULE,
495 .name = "ocores-i2c",
496 .of_match_table = ocores_i2c_match,
501 module_platform_driver(ocores_i2c_driver);
503 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
504 MODULE_DESCRIPTION("OpenCores I2C bus driver");
505 MODULE_LICENSE("GPL");
506 MODULE_ALIAS("platform:ocores-i2c");