#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
+#include <linux/regmap.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/mutex.h>
struct cxd {
struct dvb_ca_en50221 en;
- struct i2c_adapter *i2c;
struct cxd2099_cfg cfg;
+ struct i2c_client *client;
+ struct regmap *regmap;
u8 regs[0x23];
u8 lastaddress;
u8 wbuf[1028];
};
-static int i2c_write_reg(struct i2c_adapter *adapter, u8 adr,
- u8 reg, u8 data)
-{
- u8 m[2] = {reg, data};
- struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = m, .len = 2};
-
- if (i2c_transfer(adapter, &msg, 1) != 1) {
- dev_err(&adapter->dev,
- "Failed to write to I2C register %02x@%02x!\n",
- reg, adr);
- return -1;
- }
- return 0;
-}
-
-static int i2c_write(struct i2c_adapter *adapter, u8 adr,
- u8 *data, u16 len)
-{
- struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len};
-
- if (i2c_transfer(adapter, &msg, 1) != 1) {
- dev_err(&adapter->dev, "Failed to write to I2C!\n");
- return -1;
- }
- return 0;
-}
-
-static int i2c_read_reg(struct i2c_adapter *adapter, u8 adr,
- u8 reg, u8 *val)
-{
- struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
- .buf = ®, .len = 1},
- {.addr = adr, .flags = I2C_M_RD,
- .buf = val, .len = 1} };
-
- if (i2c_transfer(adapter, msgs, 2) != 2) {
- dev_err(&adapter->dev, "error in %s()\n", __func__);
- return -1;
- }
- return 0;
-}
-
-static int i2c_read(struct i2c_adapter *adapter, u8 adr,
- u8 reg, u8 *data, u16 n)
-{
- struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
- .buf = ®, .len = 1},
- {.addr = adr, .flags = I2C_M_RD,
- .buf = data, .len = n} };
-
- if (i2c_transfer(adapter, msgs, 2) != 2) {
- dev_err(&adapter->dev, "error in %s()\n", __func__);
- return -1;
- }
- return 0;
-}
-
static int read_block(struct cxd *ci, u8 adr, u8 *data, u16 n)
{
int status = 0;
if (ci->lastaddress != adr)
- status = i2c_write_reg(ci->i2c, ci->cfg.adr, 0, adr);
+ status = regmap_write(ci->regmap, 0, adr);
if (!status) {
ci->lastaddress = adr;
if (ci->cfg.max_i2c && len > ci->cfg.max_i2c)
len = ci->cfg.max_i2c;
- status = i2c_read(ci->i2c, ci->cfg.adr, 1, data, len);
+ status = regmap_raw_read(ci->regmap, 1, data, len);
if (status)
return status;
data += len;
static int read_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
{
int status;
- u8 addr[3] = {2, address & 0xff, address >> 8};
+ u8 addr[2] = {address & 0xff, address >> 8};
- status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
+ status = regmap_raw_write(ci->regmap, 2, addr, 2);
if (!status)
- status = i2c_read(ci->i2c, ci->cfg.adr, 3, data, n);
+ status = regmap_raw_read(ci->regmap, 3, data, n);
return status;
}
static int write_pccard(struct cxd *ci, u16 address, u8 *data, u8 n)
{
int status;
- u8 addr[3] = {2, address & 0xff, address >> 8};
+ u8 addr[2] = {address & 0xff, address >> 8};
- status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
+ status = regmap_raw_write(ci->regmap, 2, addr, 2);
if (!status) {
- u8 buf[256] = {3};
+ u8 buf[256];
- memcpy(buf + 1, data, n);
- status = i2c_write(ci->i2c, ci->cfg.adr, buf, n + 1);
+ memcpy(buf, data, n);
+ status = regmap_raw_write(ci->regmap, 3, buf, n);
}
return status;
}
-static int read_io(struct cxd *ci, u16 address, u8 *val)
+static int read_io(struct cxd *ci, u16 address, unsigned int *val)
{
int status;
- u8 addr[3] = {2, address & 0xff, address >> 8};
+ u8 addr[2] = {address & 0xff, address >> 8};
- status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
+ status = regmap_raw_write(ci->regmap, 2, addr, 2);
if (!status)
- status = i2c_read(ci->i2c, ci->cfg.adr, 3, val, 1);
+ status = regmap_read(ci->regmap, 3, val);
return status;
}
static int write_io(struct cxd *ci, u16 address, u8 val)
{
int status;
- u8 addr[3] = {2, address & 0xff, address >> 8};
- u8 buf[2] = {3, val};
+ u8 addr[2] = {address & 0xff, address >> 8};
- status = i2c_write(ci->i2c, ci->cfg.adr, addr, 3);
+ status = regmap_raw_write(ci->regmap, 2, addr, 2);
if (!status)
- status = i2c_write(ci->i2c, ci->cfg.adr, buf, 2);
+ status = regmap_write(ci->regmap, 3, val);
return status;
}
static int write_regm(struct cxd *ci, u8 reg, u8 val, u8 mask)
{
int status = 0;
+ unsigned int regval;
if (ci->lastaddress != reg)
- status = i2c_write_reg(ci->i2c, ci->cfg.adr, 0, reg);
- if (!status && reg >= 6 && reg <= 8 && mask != 0xff)
- status = i2c_read_reg(ci->i2c, ci->cfg.adr, 1, &ci->regs[reg]);
+ status = regmap_write(ci->regmap, 0, reg);
+ if (!status && reg >= 6 && reg <= 8 && mask != 0xff) {
+ status = regmap_read(ci->regmap, 1, ®val);
+ ci->regs[reg] = regval;
+ }
ci->lastaddress = reg;
ci->regs[reg] = (ci->regs[reg] & (~mask)) | val;
if (!status)
- status = i2c_write_reg(ci->i2c, ci->cfg.adr, 1, ci->regs[reg]);
+ status = regmap_write(ci->regmap, 1, ci->regs[reg]);
if (reg == 0x20)
ci->regs[reg] &= 0x7f;
return status;
u8 *buf = ci->wbuf;
if (ci->lastaddress != adr)
- status = i2c_write_reg(ci->i2c, ci->cfg.adr, 0, adr);
+ status = regmap_write(ci->regmap, 0, adr);
if (status)
return status;
ci->lastaddress = adr;
- buf[0] = 1;
while (n) {
int len = n;
if (ci->cfg.max_i2c && (len + 1 > ci->cfg.max_i2c))
len = ci->cfg.max_i2c - 1;
- memcpy(buf + 1, data, len);
- status = i2c_write(ci->i2c, ci->cfg.adr, buf, len + 1);
+ memcpy(buf, data, len);
+ status = regmap_raw_write(ci->regmap, 1, buf, len);
if (status)
return status;
n -= len;
if (!ci->en.read_data)
return;
ci->write_busy = 0;
- dev_info(&ci->i2c->dev, "enable cam buffer mode\n");
+ dev_info(&ci->client->dev, "enable cam buffer mode\n");
write_reg(ci, 0x0d, 0x00);
write_reg(ci, 0x0e, 0x01);
write_regm(ci, 0x08, 0x40, 0x40);
int slot, u8 address)
{
struct cxd *ci = ca->data;
- u8 val;
+ unsigned int val;
mutex_lock(&ci->lock);
set_mode(ci, 0);
{
struct cxd *ci = ca->data;
- dev_dbg(&ci->i2c->dev, "%s\n", __func__);
+ dev_dbg(&ci->client->dev, "%s\n", __func__);
if (ci->cammode)
read_data(ca, slot, ci->rbuf, 0);
mutex_lock(&ci->lock);
if (ci->slot_stat) {
ci->slot_stat = 0;
write_regm(ci, 0x03, 0x00, 0x08);
- dev_info(&ci->i2c->dev, "NO CAM\n");
+ dev_info(&ci->client->dev, "NO CAM\n");
ci->ready = 0;
}
}
.write_data = write_data,
};
-struct dvb_ca_en50221 *cxd2099_attach(struct cxd2099_cfg *cfg,
- void *priv,
- struct i2c_adapter *i2c)
+static int cxd2099_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
{
struct cxd *ci;
- u8 val;
+ struct cxd2099_cfg *cfg = client->dev.platform_data;
+ static const struct regmap_config rm_cfg = {
+ .reg_bits = 8,
+ .val_bits = 8,
+ };
+ unsigned int val;
+ int ret;
- if (i2c_read_reg(i2c, cfg->adr, 0, &val) < 0) {
- dev_info(&i2c->dev, "No CXD2099AR detected at 0x%02x\n",
- cfg->adr);
- return NULL;
+ ci = kzalloc(sizeof(*ci), GFP_KERNEL);
+ if (!ci) {
+ ret = -ENOMEM;
+ goto err;
}
- ci = kzalloc(sizeof(*ci), GFP_KERNEL);
- if (!ci)
- return NULL;
+ ci->client = client;
+ memcpy(&ci->cfg, cfg, sizeof(ci->cfg));
+
+ ci->regmap = regmap_init_i2c(client, &rm_cfg);
+ if (IS_ERR(ci->regmap)) {
+ ret = PTR_ERR(ci->regmap);
+ goto err_kfree;
+ }
+
+ ret = regmap_read(ci->regmap, 0x00, &val);
+ if (ret < 0) {
+ dev_info(&client->dev, "No CXD2099AR detected at 0x%02x\n",
+ client->addr);
+ goto err_rmexit;
+ }
mutex_init(&ci->lock);
- ci->cfg = *cfg;
- ci->i2c = i2c;
ci->lastaddress = 0xff;
ci->clk_reg_b = 0x4a;
ci->clk_reg_f = 0x1b;
ci->en = en_templ;
ci->en.data = ci;
init(ci);
- dev_info(&i2c->dev, "Attached CXD2099AR at 0x%02x\n", ci->cfg.adr);
+ dev_info(&client->dev, "Attached CXD2099AR at 0x%02x\n", client->addr);
+
+ *cfg->en = &ci->en;
if (!buffermode) {
ci->en.read_data = NULL;
ci->en.write_data = NULL;
} else {
- dev_info(&i2c->dev, "Using CXD2099AR buffer mode");
+ dev_info(&client->dev, "Using CXD2099AR buffer mode");
}
- return &ci->en;
+ i2c_set_clientdata(client, ci);
+
+ return 0;
+
+err_rmexit:
+ regmap_exit(ci->regmap);
+err_kfree:
+ kfree(ci);
+err:
+
+ return ret;
}
-EXPORT_SYMBOL(cxd2099_attach);
+
+static int cxd2099_remove(struct i2c_client *client)
+{
+ struct cxd *ci = i2c_get_clientdata(client);
+
+ regmap_exit(ci->regmap);
+ kfree(ci);
+
+ return 0;
+}
+
+static const struct i2c_device_id cxd2099_id[] = {
+ {"cxd2099", 0},
+ {}
+};
+MODULE_DEVICE_TABLE(i2c, cxd2099_id);
+
+static struct i2c_driver cxd2099_driver = {
+ .driver = {
+ .name = "cxd2099",
+ },
+ .probe = cxd2099_probe,
+ .remove = cxd2099_remove,
+ .id_table = cxd2099_id,
+};
+
+module_i2c_driver(cxd2099_driver);
MODULE_DESCRIPTION("CXD2099AR Common Interface controller driver");
MODULE_AUTHOR("Ralph Metzler");