#include <linux/string.h>
#include <linux/slab.h>
#include <linux/firmware.h>
+#include <linux/regmap.h>
#include "dvb_frontend.h"
#include "dvb_math.h"
struct si2165_state {
struct i2c_client *client;
- struct i2c_adapter *i2c;
+ struct regmap *regmap;
struct dvb_frontend fe;
const u8 *src, const int count)
{
int ret;
- struct i2c_msg msg;
- u8 buf[2 + 4]; /* write a maximum of 4 bytes of data */
-
- if (count + 2 > sizeof(buf)) {
- dev_warn(&state->client->dev,
- "%s: i2c wr reg=%04x: count=%d is too big!\n",
- KBUILD_MODNAME, reg, count);
- return -EINVAL;
- }
- buf[0] = reg >> 8;
- buf[1] = reg & 0xff;
- memcpy(buf + 2, src, count);
-
- msg.addr = state->config.i2c_addr;
- msg.flags = 0;
- msg.buf = buf;
- msg.len = count + 2;
if (debug & DEBUG_I2C_WRITE)
deb_i2c_write("reg: 0x%04x, data: %*ph\n", reg, count, src);
- ret = i2c_transfer(state->i2c, &msg, 1);
+ ret = regmap_bulk_write(state->regmap, reg, src, count);
- if (ret != 1) {
+ if (ret)
dev_err(&state->client->dev, "%s: ret == %d\n", __func__, ret);
- if (ret < 0)
- return ret;
- else
- return -EREMOTEIO;
- }
- return 0;
+ return ret;
}
static int si2165_read(struct si2165_state *state,
const u16 reg, u8 *val, const int count)
{
- int ret;
- u8 reg_buf[] = { reg >> 8, reg & 0xff };
- struct i2c_msg msg[] = {
- { .addr = state->config.i2c_addr,
- .flags = 0, .buf = reg_buf, .len = 2 },
- { .addr = state->config.i2c_addr,
- .flags = I2C_M_RD, .buf = val, .len = count },
- };
+ int ret = regmap_bulk_read(state->regmap, reg, val, count);
- ret = i2c_transfer(state->i2c, msg, 2);
-
- if (ret != 2) {
+ if (ret) {
dev_err(&state->client->dev, "%s: error (addr %02x reg %04x error (ret == %i)\n",
__func__, state->config.i2c_addr, reg, ret);
- if (ret < 0)
- return ret;
- else
- return -EREMOTEIO;
+ return ret;
}
if (debug & DEBUG_I2C_READ)
static int si2165_readreg8(struct si2165_state *state,
const u16 reg, u8 *val)
{
- int ret;
-
- ret = si2165_read(state, reg, val, 1);
+ unsigned int val_tmp;
+ int ret = regmap_read(state->regmap, reg, &val_tmp);
+ *val = (u8)val_tmp;
deb_readreg("R(0x%04x)=0x%02x\n", reg, *val);
return ret;
}
static int si2165_writereg8(struct si2165_state *state, const u16 reg, u8 val)
{
- return si2165_write(state, reg, &val, 1);
+ return regmap_write(state->regmap, reg, val);
}
static int si2165_writereg16(struct si2165_state *state, const u16 reg, u16 val)
u8 val;
char rev_char;
const char *chip_name;
+ static const struct regmap_config regmap_config = {
+ .reg_bits = 16,
+ .val_bits = 8,
+ .max_register = 0x08ff,
+ };
/* allocate memory for the internal state */
state = kzalloc(sizeof(struct si2165_state), GFP_KERNEL);
goto error;
}
+ /* create regmap */
+ state->regmap = devm_regmap_init_i2c(client, ®map_config);
+ if (IS_ERR(state->regmap)) {
+ ret = PTR_ERR(state->regmap);
+ goto error;
+ }
+
/* setup the state */
state->client = client;
- state->i2c = client->adapter;
state->config.i2c_addr = client->addr;
state->config.chip_mode = pdata->chip_mode;
state->config.ref_freq_Hz = pdata->ref_freq_Hz;