2019-11-04 19:12:52 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-08-05 17:22:42 +02:00
|
|
|
/*
|
|
|
|
* ddbridge-i2c.c: Digital Devices bridge i2c driver
|
|
|
|
*
|
2019-11-04 19:12:52 +01:00
|
|
|
* Copyright (C) 2010-2017 Digital Devices GmbH
|
2015-08-05 17:22:42 +02:00
|
|
|
* Ralph Metzler <rjkm@metzlerbros.de>
|
|
|
|
* Marcus Metzler <mocm@metzlerbros.de>
|
|
|
|
*
|
2019-11-04 19:12:52 +01:00
|
|
|
*
|
2015-08-05 17:22:42 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* version 2 only, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-11-04 19:12:52 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2015-08-05 17:22:42 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-02 17:40:24 +02:00
|
|
|
#include "ddbridge.h"
|
2017-08-02 20:22:52 +02:00
|
|
|
#include "ddbridge-io.h"
|
2015-08-05 17:22:42 +02:00
|
|
|
|
|
|
|
static int ddb_i2c_cmd(struct ddb_i2c *i2c, u32 adr, u32 cmd)
|
|
|
|
{
|
|
|
|
struct ddb *dev = i2c->dev;
|
|
|
|
unsigned long stat;
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
ddbwritel(dev, (adr << 9) | cmd, i2c->regs + I2C_COMMAND);
|
|
|
|
stat = wait_for_completion_timeout(&i2c->completion, HZ);
|
2015-09-09 12:13:11 +02:00
|
|
|
val = ddbreadl(dev, i2c->regs + I2C_COMMAND);
|
2015-08-05 17:22:42 +02:00
|
|
|
if (stat == 0) {
|
2020-08-29 15:32:42 +02:00
|
|
|
u32 istat = ddbreadl(dev, INTERRUPT_STATUS);
|
|
|
|
|
2017-08-25 23:30:58 +02:00
|
|
|
dev_err(dev->dev, "I2C timeout, card %d, port %d, link %u\n",
|
|
|
|
dev->nr, i2c->nr, i2c->link);
|
2020-08-29 15:32:42 +02:00
|
|
|
dev_err(dev->dev, "DDBridge IRS %08x\n", istat);
|
2016-03-24 12:10:20 +01:00
|
|
|
|
2020-08-29 15:32:42 +02:00
|
|
|
if (i2c->link) {
|
|
|
|
u32 listat =
|
|
|
|
ddbreadl(dev,
|
|
|
|
DDB_LINK_TAG(i2c->link) |
|
|
|
|
INTERRUPT_STATUS);
|
|
|
|
dev_err(dev->dev,
|
|
|
|
"DDBridge link %u IRS %08x\n",
|
|
|
|
i2c->link, listat);
|
|
|
|
}
|
|
|
|
if (istat & 1) {
|
|
|
|
ddbwritel(dev, istat & 1, INTERRUPT_ACK);
|
|
|
|
} else {
|
|
|
|
u32 mon = ddbreadl(dev,
|
|
|
|
i2c->regs + I2C_MONITOR);
|
2015-09-09 12:13:11 +02:00
|
|
|
|
2020-08-29 15:32:42 +02:00
|
|
|
dev_err(dev->dev, "I2C cmd=%08x mon=%08x\n",
|
|
|
|
val, mon);
|
2015-08-05 17:22:42 +02:00
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
}
|
2018-03-28 19:43:04 +02:00
|
|
|
val &= 0x70000;
|
|
|
|
if (val == 0x20000)
|
2019-07-08 10:01:22 +02:00
|
|
|
dev_err(dev->dev, "I2C bus error\n");
|
2018-03-28 19:43:04 +02:00
|
|
|
if (val)
|
2015-08-05 17:22:42 +02:00
|
|
|
return -EIO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ddb_i2c_master_xfer(struct i2c_adapter *adapter,
|
|
|
|
struct i2c_msg msg[], int num)
|
|
|
|
{
|
2017-08-26 22:04:37 +02:00
|
|
|
struct ddb_i2c *i2c = (struct ddb_i2c *)i2c_get_adapdata(adapter);
|
2015-08-05 17:22:42 +02:00
|
|
|
struct ddb *dev = i2c->dev;
|
|
|
|
u8 addr = 0;
|
2016-03-24 12:10:20 +01:00
|
|
|
|
2015-08-05 17:22:42 +02:00
|
|
|
addr = msg[0].addr;
|
|
|
|
if (msg[0].len > i2c->bsize)
|
|
|
|
return -EIO;
|
2017-04-07 22:21:06 +02:00
|
|
|
switch (num) {
|
|
|
|
case 1:
|
|
|
|
if (msg[0].flags & I2C_M_RD) {
|
|
|
|
ddbwritel(dev, msg[0].len << 16,
|
|
|
|
i2c->regs + I2C_TASKLENGTH);
|
|
|
|
if (ddb_i2c_cmd(i2c, addr, 3))
|
|
|
|
break;
|
|
|
|
ddbcpyfrom(dev, msg[0].buf,
|
|
|
|
i2c->rbuf, msg[0].len);
|
|
|
|
return num;
|
2017-04-16 21:20:52 +02:00
|
|
|
}
|
2017-04-07 22:21:06 +02:00
|
|
|
ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
|
|
|
|
ddbwritel(dev, msg[0].len, i2c->regs + I2C_TASKLENGTH);
|
|
|
|
if (ddb_i2c_cmd(i2c, addr, 2))
|
|
|
|
break;
|
|
|
|
return num;
|
|
|
|
case 2:
|
|
|
|
if ((msg[0].flags & I2C_M_RD) == I2C_M_RD)
|
|
|
|
break;
|
|
|
|
if ((msg[1].flags & I2C_M_RD) != I2C_M_RD)
|
|
|
|
break;
|
2015-08-05 17:22:42 +02:00
|
|
|
if (msg[1].len > i2c->bsize)
|
2017-04-07 22:21:06 +02:00
|
|
|
break;
|
2015-08-05 17:22:42 +02:00
|
|
|
ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
|
|
|
|
ddbwritel(dev, msg[0].len | (msg[1].len << 16),
|
|
|
|
i2c->regs + I2C_TASKLENGTH);
|
2017-04-07 22:21:06 +02:00
|
|
|
if (ddb_i2c_cmd(i2c, addr, 1))
|
|
|
|
break;
|
|
|
|
ddbcpyfrom(dev, msg[1].buf,
|
|
|
|
i2c->rbuf,
|
|
|
|
msg[1].len);
|
|
|
|
return num;
|
|
|
|
default:
|
|
|
|
break;
|
2015-08-05 17:22:42 +02:00
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 ddb_i2c_functionality(struct i2c_adapter *adap)
|
|
|
|
{
|
|
|
|
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct i2c_algorithm ddb_i2c_algo = {
|
|
|
|
.master_xfer = ddb_i2c_master_xfer,
|
|
|
|
.functionality = ddb_i2c_functionality,
|
|
|
|
};
|
|
|
|
|
2017-08-02 17:40:24 +02:00
|
|
|
void ddb_i2c_release(struct ddb *dev)
|
2015-08-05 17:22:42 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct ddb_i2c *i2c;
|
|
|
|
|
|
|
|
for (i = 0; i < dev->i2c_num; i++) {
|
|
|
|
i2c = &dev->i2c[i];
|
|
|
|
i2c_del_adapter(&i2c->adap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 23:03:16 +02:00
|
|
|
static void i2c_handler(void *priv)
|
2015-08-05 17:22:42 +02:00
|
|
|
{
|
2017-08-26 22:04:37 +02:00
|
|
|
struct ddb_i2c *i2c = (struct ddb_i2c *)priv;
|
2015-08-05 17:22:42 +02:00
|
|
|
|
|
|
|
complete(&i2c->completion);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ddb_i2c_add(struct ddb *dev, struct ddb_i2c *i2c,
|
2017-09-08 14:47:14 +02:00
|
|
|
const struct ddb_regmap *regmap,
|
|
|
|
int link, int i, int num)
|
2015-08-05 17:22:42 +02:00
|
|
|
{
|
|
|
|
struct i2c_adapter *adap;
|
2016-03-24 12:10:20 +01:00
|
|
|
|
2015-08-05 17:22:42 +02:00
|
|
|
i2c->nr = i;
|
|
|
|
i2c->dev = dev;
|
|
|
|
i2c->link = link;
|
|
|
|
i2c->bsize = regmap->i2c_buf->size;
|
2016-03-24 12:10:20 +01:00
|
|
|
i2c->wbuf = DDB_LINK_TAG(link) |
|
|
|
|
(regmap->i2c_buf->base + i2c->bsize * i);
|
|
|
|
i2c->rbuf = i2c->wbuf;/* + i2c->bsize / 2; */
|
|
|
|
i2c->regs = DDB_LINK_TAG(link) |
|
|
|
|
(regmap->i2c->base + regmap->i2c->size * i);
|
2015-08-05 17:22:42 +02:00
|
|
|
ddbwritel(dev, I2C_SPEED_100, i2c->regs + I2C_TIMING);
|
|
|
|
ddbwritel(dev, ((i2c->rbuf & 0xffff) << 16) | (i2c->wbuf & 0xffff),
|
|
|
|
i2c->regs + I2C_TASKADDRESS);
|
|
|
|
init_completion(&i2c->completion);
|
|
|
|
adap = &i2c->adap;
|
|
|
|
i2c_set_adapdata(adap, i2c);
|
|
|
|
#ifdef I2C_ADAP_CLASS_TV_DIGITAL
|
2017-08-26 22:04:37 +02:00
|
|
|
adap->class = I2C_ADAP_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
|
2015-08-05 17:22:42 +02:00
|
|
|
#else
|
|
|
|
#ifdef I2C_CLASS_TV_ANALOG
|
|
|
|
adap->class = I2C_CLASS_TV_ANALOG;
|
|
|
|
#endif
|
|
|
|
#endif
|
2016-05-10 11:59:08 +02:00
|
|
|
snprintf(adap->name, I2C_NAME_SIZE, "ddbridge_%02x.%x.%x",
|
|
|
|
dev->nr, i2c->link, i);
|
2015-08-05 17:22:42 +02:00
|
|
|
adap->algo = &ddb_i2c_algo;
|
2017-08-26 22:04:37 +02:00
|
|
|
adap->algo_data = (void *)i2c;
|
2015-08-05 17:22:42 +02:00
|
|
|
adap->dev.parent = dev->dev;
|
|
|
|
return i2c_add_adapter(adap);
|
|
|
|
}
|
|
|
|
|
2017-08-02 17:40:24 +02:00
|
|
|
int ddb_i2c_init(struct ddb *dev)
|
2015-08-05 17:22:42 +02:00
|
|
|
{
|
|
|
|
int stat = 0;
|
2016-05-02 16:27:32 +02:00
|
|
|
u32 i, j, num = 0, l, base;
|
2015-08-05 17:22:42 +02:00
|
|
|
struct ddb_i2c *i2c;
|
|
|
|
struct i2c_adapter *adap;
|
2017-09-08 14:47:14 +02:00
|
|
|
const struct ddb_regmap *regmap;
|
2016-03-24 12:10:20 +01:00
|
|
|
|
2015-08-05 17:22:42 +02:00
|
|
|
for (l = 0; l < DDB_MAX_LINK; l++) {
|
|
|
|
if (!dev->link[l].info)
|
|
|
|
continue;
|
|
|
|
regmap = dev->link[l].info->regmap;
|
|
|
|
if (!regmap || !regmap->i2c)
|
|
|
|
continue;
|
2016-05-02 16:27:32 +02:00
|
|
|
base = regmap->irq_base_i2c;
|
2015-08-05 17:22:42 +02:00
|
|
|
for (i = 0; i < regmap->i2c->num; i++) {
|
|
|
|
if (!(dev->link[l].info->i2c_mask & (1 << i)))
|
|
|
|
continue;
|
|
|
|
i2c = &dev->i2c[num];
|
2017-10-25 23:03:16 +02:00
|
|
|
ddb_irq_set(dev, l, i + base, i2c_handler, i2c);
|
2015-08-05 17:22:42 +02:00
|
|
|
stat = ddb_i2c_add(dev, i2c, regmap, l, i, num);
|
|
|
|
if (stat)
|
|
|
|
break;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stat) {
|
|
|
|
for (j = 0; j < num; j++) {
|
|
|
|
i2c = &dev->i2c[j];
|
|
|
|
adap = &i2c->adap;
|
|
|
|
i2c_del_adapter(adap);
|
|
|
|
}
|
2017-08-26 22:04:37 +02:00
|
|
|
} else {
|
2015-08-05 17:22:42 +02:00
|
|
|
dev->i2c_num = num;
|
2017-08-26 22:04:37 +02:00
|
|
|
}
|
2015-08-05 17:22:42 +02:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|