0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011
0012 #include <linux/module.h>
0013 #include <linux/i2c.h>
0014 #include "cx25821.h"
0015
0016 static unsigned int i2c_debug;
0017 module_param(i2c_debug, int, 0644);
0018 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
0019
0020 static unsigned int i2c_scan;
0021 module_param(i2c_scan, int, 0444);
0022 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
0023
0024 #define dprintk(level, fmt, arg...) \
0025 do { \
0026 if (i2c_debug >= level) \
0027 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \
0028 } while (0)
0029
0030 #define I2C_WAIT_DELAY 32
0031 #define I2C_WAIT_RETRY 64
0032
0033 #define I2C_EXTEND (1 << 3)
0034 #define I2C_NOSTOP (1 << 4)
0035
0036 static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
0037 {
0038 struct cx25821_i2c *bus = i2c_adap->algo_data;
0039 struct cx25821_dev *dev = bus->dev;
0040 return cx_read(bus->reg_stat) & 0x01;
0041 }
0042
0043 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
0044 {
0045 struct cx25821_i2c *bus = i2c_adap->algo_data;
0046 struct cx25821_dev *dev = bus->dev;
0047 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
0048 }
0049
0050 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
0051 {
0052 int count;
0053
0054 for (count = 0; count < I2C_WAIT_RETRY; count++) {
0055 if (!i2c_is_busy(i2c_adap))
0056 break;
0057 udelay(I2C_WAIT_DELAY);
0058 }
0059
0060 if (I2C_WAIT_RETRY == count)
0061 return 0;
0062
0063 return 1;
0064 }
0065
0066 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
0067 const struct i2c_msg *msg, int joined_rlen)
0068 {
0069 struct cx25821_i2c *bus = i2c_adap->algo_data;
0070 struct cx25821_dev *dev = bus->dev;
0071 u32 wdata, addr, ctrl;
0072 int retval, cnt;
0073
0074 if (joined_rlen)
0075 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
0076 msg->len, joined_rlen);
0077 else
0078 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
0079
0080
0081 if (msg->len == 0) {
0082 cx_write(bus->reg_addr, msg->addr << 25);
0083 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
0084
0085 if (!i2c_wait_done(i2c_adap))
0086 return -EIO;
0087
0088 if (!i2c_slave_did_ack(i2c_adap))
0089 return -EIO;
0090
0091 dprintk(1, "%s(): returns 0\n", __func__);
0092 return 0;
0093 }
0094
0095
0096 addr = (msg->addr << 25) | msg->buf[0];
0097 wdata = msg->buf[0];
0098
0099 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
0100
0101 if (msg->len > 1)
0102 ctrl |= I2C_NOSTOP | I2C_EXTEND;
0103 else if (joined_rlen)
0104 ctrl |= I2C_NOSTOP;
0105
0106 cx_write(bus->reg_addr, addr);
0107 cx_write(bus->reg_wdata, wdata);
0108 cx_write(bus->reg_ctrl, ctrl);
0109
0110 retval = i2c_wait_done(i2c_adap);
0111 if (retval < 0)
0112 goto err;
0113
0114 if (retval == 0)
0115 goto eio;
0116
0117 if (i2c_debug) {
0118 if (!(ctrl & I2C_NOSTOP))
0119 printk(" >\n");
0120 }
0121
0122 for (cnt = 1; cnt < msg->len; cnt++) {
0123
0124 wdata = msg->buf[cnt];
0125 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
0126
0127 if (cnt < msg->len - 1)
0128 ctrl |= I2C_NOSTOP | I2C_EXTEND;
0129 else if (joined_rlen)
0130 ctrl |= I2C_NOSTOP;
0131
0132 cx_write(bus->reg_addr, addr);
0133 cx_write(bus->reg_wdata, wdata);
0134 cx_write(bus->reg_ctrl, ctrl);
0135
0136 retval = i2c_wait_done(i2c_adap);
0137 if (retval < 0)
0138 goto err;
0139
0140 if (retval == 0)
0141 goto eio;
0142
0143 if (i2c_debug) {
0144 dprintk(1, " %02x", msg->buf[cnt]);
0145 if (!(ctrl & I2C_NOSTOP))
0146 dprintk(1, " >\n");
0147 }
0148 }
0149
0150 return msg->len;
0151
0152 eio:
0153 retval = -EIO;
0154 err:
0155 if (i2c_debug)
0156 pr_err(" ERR: %d\n", retval);
0157 return retval;
0158 }
0159
0160 static int i2c_readbytes(struct i2c_adapter *i2c_adap,
0161 const struct i2c_msg *msg, int joined)
0162 {
0163 struct cx25821_i2c *bus = i2c_adap->algo_data;
0164 struct cx25821_dev *dev = bus->dev;
0165 u32 ctrl, cnt;
0166 int retval;
0167
0168 if (i2c_debug && !joined)
0169 dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len);
0170
0171
0172 if (msg->len == 0) {
0173 cx_write(bus->reg_addr, msg->addr << 25);
0174 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
0175 if (!i2c_wait_done(i2c_adap))
0176 return -EIO;
0177 if (!i2c_slave_did_ack(i2c_adap))
0178 return -EIO;
0179
0180 dprintk(1, "%s(): returns 0\n", __func__);
0181 return 0;
0182 }
0183
0184 if (i2c_debug) {
0185 if (joined)
0186 dprintk(1, " R");
0187 else
0188 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
0189 }
0190
0191 for (cnt = 0; cnt < msg->len; cnt++) {
0192
0193 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
0194
0195 if (cnt < msg->len - 1)
0196 ctrl |= I2C_NOSTOP | I2C_EXTEND;
0197
0198 cx_write(bus->reg_addr, msg->addr << 25);
0199 cx_write(bus->reg_ctrl, ctrl);
0200
0201 retval = i2c_wait_done(i2c_adap);
0202 if (retval < 0)
0203 goto err;
0204 if (retval == 0)
0205 goto eio;
0206 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
0207
0208 if (i2c_debug) {
0209 dprintk(1, " %02x", msg->buf[cnt]);
0210 if (!(ctrl & I2C_NOSTOP))
0211 dprintk(1, " >\n");
0212 }
0213 }
0214
0215 return msg->len;
0216 eio:
0217 retval = -EIO;
0218 err:
0219 if (i2c_debug)
0220 pr_err(" ERR: %d\n", retval);
0221 return retval;
0222 }
0223
0224 static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
0225 {
0226 struct cx25821_i2c *bus = i2c_adap->algo_data;
0227 struct cx25821_dev *dev = bus->dev;
0228 int i, retval = 0;
0229
0230 dprintk(1, "%s(num = %d)\n", __func__, num);
0231
0232 for (i = 0; i < num; i++) {
0233 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
0234 __func__, num, msgs[i].addr, msgs[i].len);
0235
0236 if (msgs[i].flags & I2C_M_RD) {
0237
0238 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
0239 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
0240 msgs[i].addr == msgs[i + 1].addr) {
0241
0242 retval = i2c_sendbytes(i2c_adap, &msgs[i],
0243 msgs[i + 1].len);
0244
0245 if (retval < 0)
0246 goto err;
0247 i++;
0248 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
0249 } else {
0250
0251 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
0252 }
0253
0254 if (retval < 0)
0255 goto err;
0256 }
0257 return num;
0258
0259 err:
0260 return retval;
0261 }
0262
0263
0264 static u32 cx25821_functionality(struct i2c_adapter *adap)
0265 {
0266 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WORD_DATA |
0267 I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
0268 }
0269
0270 static const struct i2c_algorithm cx25821_i2c_algo_template = {
0271 .master_xfer = i2c_xfer,
0272 .functionality = cx25821_functionality,
0273 #ifdef NEED_ALGO_CONTROL
0274 .algo_control = dummy_algo_control,
0275 #endif
0276 };
0277
0278 static const struct i2c_adapter cx25821_i2c_adap_template = {
0279 .name = "cx25821",
0280 .owner = THIS_MODULE,
0281 .algo = &cx25821_i2c_algo_template,
0282 };
0283
0284 static const struct i2c_client cx25821_i2c_client_template = {
0285 .name = "cx25821 internal",
0286 };
0287
0288
0289 int cx25821_i2c_register(struct cx25821_i2c *bus)
0290 {
0291 struct cx25821_dev *dev = bus->dev;
0292
0293 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
0294
0295 bus->i2c_adap = cx25821_i2c_adap_template;
0296 bus->i2c_client = cx25821_i2c_client_template;
0297 bus->i2c_adap.dev.parent = &dev->pci->dev;
0298
0299 strscpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
0300
0301 bus->i2c_adap.algo_data = bus;
0302 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
0303 i2c_add_adapter(&bus->i2c_adap);
0304
0305 bus->i2c_client.adapter = &bus->i2c_adap;
0306
0307
0308 bus->i2c_client.addr = (0x88 >> 1);
0309
0310 return bus->i2c_rc;
0311 }
0312
0313 int cx25821_i2c_unregister(struct cx25821_i2c *bus)
0314 {
0315 i2c_del_adapter(&bus->i2c_adap);
0316 return 0;
0317 }
0318
0319 #if 0
0320 static void cx25821_av_clk(struct cx25821_dev *dev, int enable)
0321 {
0322
0323 char buffer[3];
0324 struct i2c_msg msg;
0325 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
0326
0327
0328 buffer[0] = 0x01;
0329 buffer[1] = 0x44;
0330 if (enable == 1)
0331 buffer[2] = 0x05;
0332 else
0333 buffer[2] = 0x00;
0334
0335 msg.addr = 0x44;
0336 msg.flags = I2C_M_TEN;
0337 msg.len = 3;
0338 msg.buf = buffer;
0339
0340 i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1);
0341 }
0342 #endif
0343
0344 int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value)
0345 {
0346 struct i2c_client *client = &bus->i2c_client;
0347 int v = 0;
0348 u8 addr[2] = { 0, 0 };
0349 u8 buf[4] = { 0, 0, 0, 0 };
0350
0351 struct i2c_msg msgs[2] = {
0352 {
0353 .addr = client->addr,
0354 .flags = 0,
0355 .len = 2,
0356 .buf = addr,
0357 }, {
0358 .addr = client->addr,
0359 .flags = I2C_M_RD,
0360 .len = 4,
0361 .buf = buf,
0362 }
0363 };
0364
0365 addr[0] = (reg_addr >> 8);
0366 addr[1] = (reg_addr & 0xff);
0367 msgs[0].addr = 0x44;
0368 msgs[1].addr = 0x44;
0369
0370 i2c_xfer(client->adapter, msgs, 2);
0371
0372 v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
0373 *value = v;
0374
0375 return v;
0376 }
0377
0378 int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value)
0379 {
0380 struct i2c_client *client = &bus->i2c_client;
0381 int retval = 0;
0382 u8 buf[6] = { 0, 0, 0, 0, 0, 0 };
0383
0384 struct i2c_msg msgs[1] = {
0385 {
0386 .addr = client->addr,
0387 .flags = 0,
0388 .len = 6,
0389 .buf = buf,
0390 }
0391 };
0392
0393 buf[0] = reg_addr >> 8;
0394 buf[1] = reg_addr & 0xff;
0395 buf[5] = (value >> 24) & 0xff;
0396 buf[4] = (value >> 16) & 0xff;
0397 buf[3] = (value >> 8) & 0xff;
0398 buf[2] = value & 0xff;
0399 client->flags = 0;
0400 msgs[0].addr = 0x44;
0401
0402 retval = i2c_xfer(client->adapter, msgs, 1);
0403
0404 return retval;
0405 }