1*4771d831SDaniel Scheller // SPDX-License-Identifier: GPL-2.0
2a96e5ab8SDaniel Scheller /*
322e74389SDaniel Scheller * ddbridge-i2c.c: Digital Devices bridge i2c driver
4a96e5ab8SDaniel Scheller *
522e74389SDaniel Scheller * Copyright (C) 2010-2017 Digital Devices GmbH
622e74389SDaniel Scheller * Ralph Metzler <rjkm@metzlerbros.de>
722e74389SDaniel Scheller * Marcus Metzler <mocm@metzlerbros.de>
8a96e5ab8SDaniel Scheller */
9a96e5ab8SDaniel Scheller
10a96e5ab8SDaniel Scheller #include <linux/module.h>
11a96e5ab8SDaniel Scheller #include <linux/init.h>
12a96e5ab8SDaniel Scheller #include <linux/interrupt.h>
13a96e5ab8SDaniel Scheller #include <linux/delay.h>
14a96e5ab8SDaniel Scheller #include <linux/slab.h>
15a96e5ab8SDaniel Scheller #include <linux/poll.h>
16a96e5ab8SDaniel Scheller #include <linux/io.h>
17a96e5ab8SDaniel Scheller #include <linux/pci.h>
18a96e5ab8SDaniel Scheller #include <linux/pci_ids.h>
19a96e5ab8SDaniel Scheller #include <linux/timer.h>
20a96e5ab8SDaniel Scheller #include <linux/i2c.h>
21a96e5ab8SDaniel Scheller #include <linux/swab.h>
22a96e5ab8SDaniel Scheller #include <linux/vmalloc.h>
23a96e5ab8SDaniel Scheller
24a96e5ab8SDaniel Scheller #include "ddbridge.h"
25a96e5ab8SDaniel Scheller #include "ddbridge-i2c.h"
2622e74389SDaniel Scheller #include "ddbridge-regs.h"
2714e27a10SDaniel Scheller #include "ddbridge-io.h"
28a96e5ab8SDaniel Scheller
29a96e5ab8SDaniel Scheller /******************************************************************************/
30a96e5ab8SDaniel Scheller
ddb_i2c_cmd(struct ddb_i2c * i2c,u32 adr,u32 cmd)31a96e5ab8SDaniel Scheller static int ddb_i2c_cmd(struct ddb_i2c *i2c, u32 adr, u32 cmd)
32a96e5ab8SDaniel Scheller {
33a96e5ab8SDaniel Scheller struct ddb *dev = i2c->dev;
3422e74389SDaniel Scheller unsigned long stat;
35a96e5ab8SDaniel Scheller u32 val;
36a96e5ab8SDaniel Scheller
3722e74389SDaniel Scheller ddbwritel(dev, (adr << 9) | cmd, i2c->regs + I2C_COMMAND);
3822e74389SDaniel Scheller stat = wait_for_completion_timeout(&i2c->completion, HZ);
3922e74389SDaniel Scheller val = ddbreadl(dev, i2c->regs + I2C_COMMAND);
40a96e5ab8SDaniel Scheller if (stat == 0) {
4122e74389SDaniel Scheller dev_err(dev->dev, "I2C timeout, card %d, port %d, link %u\n",
4222e74389SDaniel Scheller dev->nr, i2c->nr, i2c->link);
4322e74389SDaniel Scheller {
4422e74389SDaniel Scheller u32 istat = ddbreadl(dev, INTERRUPT_STATUS);
4522e74389SDaniel Scheller
4622e74389SDaniel Scheller dev_err(dev->dev, "DDBridge IRS %08x\n", istat);
4722e74389SDaniel Scheller if (i2c->link) {
4822e74389SDaniel Scheller u32 listat = ddbreadl(dev,
4922e74389SDaniel Scheller DDB_LINK_TAG(i2c->link) |
5022e74389SDaniel Scheller INTERRUPT_STATUS);
5122e74389SDaniel Scheller
5222e74389SDaniel Scheller dev_err(dev->dev, "DDBridge link %u IRS %08x\n",
5322e74389SDaniel Scheller i2c->link, listat);
5422e74389SDaniel Scheller }
5522e74389SDaniel Scheller if (istat & 1) {
5622e74389SDaniel Scheller ddbwritel(dev, istat & 1, INTERRUPT_ACK);
5722e74389SDaniel Scheller } else {
5822e74389SDaniel Scheller u32 mon = ddbreadl(dev,
5922e74389SDaniel Scheller i2c->regs + I2C_MONITOR);
6022e74389SDaniel Scheller
6122e74389SDaniel Scheller dev_err(dev->dev, "I2C cmd=%08x mon=%08x\n",
6222e74389SDaniel Scheller val, mon);
6322e74389SDaniel Scheller }
64a96e5ab8SDaniel Scheller }
65a96e5ab8SDaniel Scheller return -EIO;
66a96e5ab8SDaniel Scheller }
67d96eeee5SDaniel Scheller val &= 0x70000;
68d96eeee5SDaniel Scheller if (val == 0x20000)
69d96eeee5SDaniel Scheller dev_err(dev->dev, "I2C bus error\n");
70d96eeee5SDaniel Scheller if (val)
71a96e5ab8SDaniel Scheller return -EIO;
72a96e5ab8SDaniel Scheller return 0;
73a96e5ab8SDaniel Scheller }
74a96e5ab8SDaniel Scheller
ddb_i2c_master_xfer(struct i2c_adapter * adapter,struct i2c_msg msg[],int num)75a96e5ab8SDaniel Scheller static int ddb_i2c_master_xfer(struct i2c_adapter *adapter,
76a96e5ab8SDaniel Scheller struct i2c_msg msg[], int num)
77a96e5ab8SDaniel Scheller {
78a96e5ab8SDaniel Scheller struct ddb_i2c *i2c = (struct ddb_i2c *)i2c_get_adapdata(adapter);
79a96e5ab8SDaniel Scheller struct ddb *dev = i2c->dev;
80a96e5ab8SDaniel Scheller u8 addr = 0;
81a96e5ab8SDaniel Scheller
82a96e5ab8SDaniel Scheller addr = msg[0].addr;
8322e74389SDaniel Scheller if (msg[0].len > i2c->bsize)
8422e74389SDaniel Scheller return -EIO;
8522e74389SDaniel Scheller switch (num) {
8622e74389SDaniel Scheller case 1:
8722e74389SDaniel Scheller if (msg[0].flags & I2C_M_RD) {
8822e74389SDaniel Scheller ddbwritel(dev, msg[0].len << 16,
89a96e5ab8SDaniel Scheller i2c->regs + I2C_TASKLENGTH);
9022e74389SDaniel Scheller if (ddb_i2c_cmd(i2c, addr, 3))
9122e74389SDaniel Scheller break;
9222e74389SDaniel Scheller ddbcpyfrom(dev, msg[0].buf,
9322e74389SDaniel Scheller i2c->rbuf, msg[0].len);
9422e74389SDaniel Scheller return num;
9522e74389SDaniel Scheller }
9622e74389SDaniel Scheller ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
9722e74389SDaniel Scheller ddbwritel(dev, msg[0].len, i2c->regs + I2C_TASKLENGTH);
9822e74389SDaniel Scheller if (ddb_i2c_cmd(i2c, addr, 2))
9922e74389SDaniel Scheller break;
10022e74389SDaniel Scheller return num;
10122e74389SDaniel Scheller case 2:
10222e74389SDaniel Scheller if ((msg[0].flags & I2C_M_RD) == I2C_M_RD)
10322e74389SDaniel Scheller break;
10422e74389SDaniel Scheller if ((msg[1].flags & I2C_M_RD) != I2C_M_RD)
10522e74389SDaniel Scheller break;
10622e74389SDaniel Scheller if (msg[1].len > i2c->bsize)
10722e74389SDaniel Scheller break;
10822e74389SDaniel Scheller ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
10922e74389SDaniel Scheller ddbwritel(dev, msg[0].len | (msg[1].len << 16),
11022e74389SDaniel Scheller i2c->regs + I2C_TASKLENGTH);
11122e74389SDaniel Scheller if (ddb_i2c_cmd(i2c, addr, 1))
11222e74389SDaniel Scheller break;
11322e74389SDaniel Scheller ddbcpyfrom(dev, msg[1].buf,
11422e74389SDaniel Scheller i2c->rbuf,
115a96e5ab8SDaniel Scheller msg[1].len);
116a96e5ab8SDaniel Scheller return num;
11722e74389SDaniel Scheller default:
11822e74389SDaniel Scheller break;
119a96e5ab8SDaniel Scheller }
120a96e5ab8SDaniel Scheller return -EIO;
121a96e5ab8SDaniel Scheller }
122a96e5ab8SDaniel Scheller
ddb_i2c_functionality(struct i2c_adapter * adap)123a96e5ab8SDaniel Scheller static u32 ddb_i2c_functionality(struct i2c_adapter *adap)
124a96e5ab8SDaniel Scheller {
12522e74389SDaniel Scheller return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
126a96e5ab8SDaniel Scheller }
127a96e5ab8SDaniel Scheller
128a96e5ab8SDaniel Scheller static const struct i2c_algorithm ddb_i2c_algo = {
129a96e5ab8SDaniel Scheller .master_xfer = ddb_i2c_master_xfer,
130a96e5ab8SDaniel Scheller .functionality = ddb_i2c_functionality,
131a96e5ab8SDaniel Scheller };
132a96e5ab8SDaniel Scheller
ddb_i2c_release(struct ddb * dev)133a96e5ab8SDaniel Scheller void ddb_i2c_release(struct ddb *dev)
134a96e5ab8SDaniel Scheller {
135a96e5ab8SDaniel Scheller int i;
136a96e5ab8SDaniel Scheller struct ddb_i2c *i2c;
137a96e5ab8SDaniel Scheller
13822e74389SDaniel Scheller for (i = 0; i < dev->i2c_num; i++) {
139a96e5ab8SDaniel Scheller i2c = &dev->i2c[i];
14022e74389SDaniel Scheller i2c_del_adapter(&i2c->adap);
141a96e5ab8SDaniel Scheller }
142a96e5ab8SDaniel Scheller }
143a96e5ab8SDaniel Scheller
i2c_handler(void * priv)1441dda87acSDaniel Scheller static void i2c_handler(void *priv)
145a96e5ab8SDaniel Scheller {
14622e74389SDaniel Scheller struct ddb_i2c *i2c = (struct ddb_i2c *)priv;
14722e74389SDaniel Scheller
14822e74389SDaniel Scheller complete(&i2c->completion);
14922e74389SDaniel Scheller }
15022e74389SDaniel Scheller
ddb_i2c_add(struct ddb * dev,struct ddb_i2c * i2c,const struct ddb_regmap * regmap,int link,int i,int num)15122e74389SDaniel Scheller static int ddb_i2c_add(struct ddb *dev, struct ddb_i2c *i2c,
1520937e7e7SDaniel Scheller const struct ddb_regmap *regmap, int link,
1530937e7e7SDaniel Scheller int i, int num)
15422e74389SDaniel Scheller {
155a96e5ab8SDaniel Scheller struct i2c_adapter *adap;
156a96e5ab8SDaniel Scheller
157a96e5ab8SDaniel Scheller i2c->nr = i;
15822e74389SDaniel Scheller i2c->dev = dev;
15922e74389SDaniel Scheller i2c->link = link;
16022e74389SDaniel Scheller i2c->bsize = regmap->i2c_buf->size;
16122e74389SDaniel Scheller i2c->wbuf = DDB_LINK_TAG(link) |
16222e74389SDaniel Scheller (regmap->i2c_buf->base + i2c->bsize * i);
16322e74389SDaniel Scheller i2c->rbuf = i2c->wbuf; /* + i2c->bsize / 2 */
16422e74389SDaniel Scheller i2c->regs = DDB_LINK_TAG(link) |
16522e74389SDaniel Scheller (regmap->i2c->base + regmap->i2c->size * i);
16622e74389SDaniel Scheller ddbwritel(dev, I2C_SPEED_100, i2c->regs + I2C_TIMING);
16722e74389SDaniel Scheller ddbwritel(dev, ((i2c->rbuf & 0xffff) << 16) | (i2c->wbuf & 0xffff),
168a96e5ab8SDaniel Scheller i2c->regs + I2C_TASKADDRESS);
16922e74389SDaniel Scheller init_completion(&i2c->completion);
170a96e5ab8SDaniel Scheller
171a96e5ab8SDaniel Scheller adap = &i2c->adap;
172a96e5ab8SDaniel Scheller i2c_set_adapdata(adap, i2c);
173a96e5ab8SDaniel Scheller #ifdef I2C_ADAP_CLASS_TV_DIGITAL
174a96e5ab8SDaniel Scheller adap->class = I2C_ADAP_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
175a96e5ab8SDaniel Scheller #else
176a96e5ab8SDaniel Scheller #ifdef I2C_CLASS_TV_ANALOG
177a96e5ab8SDaniel Scheller adap->class = I2C_CLASS_TV_ANALOG;
178a96e5ab8SDaniel Scheller #endif
179a96e5ab8SDaniel Scheller #endif
18022e74389SDaniel Scheller snprintf(adap->name, I2C_NAME_SIZE, "ddbridge_%02x.%x.%x",
18122e74389SDaniel Scheller dev->nr, i2c->link, i);
182a96e5ab8SDaniel Scheller adap->algo = &ddb_i2c_algo;
183a96e5ab8SDaniel Scheller adap->algo_data = (void *)i2c;
18422e74389SDaniel Scheller adap->dev.parent = dev->dev;
18522e74389SDaniel Scheller return i2c_add_adapter(adap);
18622e74389SDaniel Scheller }
18722e74389SDaniel Scheller
ddb_i2c_init(struct ddb * dev)18822e74389SDaniel Scheller int ddb_i2c_init(struct ddb *dev)
18922e74389SDaniel Scheller {
19022e74389SDaniel Scheller int stat = 0;
19122e74389SDaniel Scheller u32 i, j, num = 0, l, base;
19222e74389SDaniel Scheller struct ddb_i2c *i2c;
19322e74389SDaniel Scheller struct i2c_adapter *adap;
1940937e7e7SDaniel Scheller const struct ddb_regmap *regmap;
19522e74389SDaniel Scheller
19622e74389SDaniel Scheller for (l = 0; l < DDB_MAX_LINK; l++) {
19722e74389SDaniel Scheller if (!dev->link[l].info)
19822e74389SDaniel Scheller continue;
19922e74389SDaniel Scheller regmap = dev->link[l].info->regmap;
20022e74389SDaniel Scheller if (!regmap || !regmap->i2c)
20122e74389SDaniel Scheller continue;
20222e74389SDaniel Scheller base = regmap->irq_base_i2c;
20322e74389SDaniel Scheller for (i = 0; i < regmap->i2c->num; i++) {
20422e74389SDaniel Scheller if (!(dev->link[l].info->i2c_mask & (1 << i)))
20522e74389SDaniel Scheller continue;
20622e74389SDaniel Scheller i2c = &dev->i2c[num];
2071dda87acSDaniel Scheller ddb_irq_set(dev, l, i + base, i2c_handler, i2c);
20822e74389SDaniel Scheller stat = ddb_i2c_add(dev, i2c, regmap, l, i, num);
209a96e5ab8SDaniel Scheller if (stat)
210a96e5ab8SDaniel Scheller break;
21122e74389SDaniel Scheller num++;
212a96e5ab8SDaniel Scheller }
21322e74389SDaniel Scheller }
21422e74389SDaniel Scheller if (stat) {
21522e74389SDaniel Scheller for (j = 0; j < num; j++) {
216a96e5ab8SDaniel Scheller i2c = &dev->i2c[j];
217a96e5ab8SDaniel Scheller adap = &i2c->adap;
218a96e5ab8SDaniel Scheller i2c_del_adapter(adap);
219a96e5ab8SDaniel Scheller }
220757d78d3SDaniel Scheller } else {
22122e74389SDaniel Scheller dev->i2c_num = num;
222757d78d3SDaniel Scheller }
223757d78d3SDaniel Scheller
224a96e5ab8SDaniel Scheller return stat;
225a96e5ab8SDaniel Scheller }
226