blob: f727b85743ac1e5220af98ffa0aca3f60a8c5f9c [file] [log] [blame]
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -03001/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030024#include "cx25821.h"
25#include <linux/i2c.h>
26
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030027static unsigned int i2c_debug;
28module_param(i2c_debug, int, 0644);
29MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
30
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030031static unsigned int i2c_scan = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030032module_param(i2c_scan, int, 0444);
33MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
34
35#define dprintk(level, fmt, arg...)\
36 do { if (i2c_debug >= level)\
37 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
38 } while (0)
39
40#define I2C_WAIT_DELAY 32
41#define I2C_WAIT_RETRY 64
42
43#define I2C_EXTEND (1 << 3)
44#define I2C_NOSTOP (1 << 4)
45
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030046static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
47{
48 struct cx25821_i2c *bus = i2c_adap->algo_data;
49 struct cx25821_dev *dev = bus->dev;
50 return cx_read(bus->reg_stat) & 0x01;
51}
52
53static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
54{
55 struct cx25821_i2c *bus = i2c_adap->algo_data;
56 struct cx25821_dev *dev = bus->dev;
57 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
58}
59
60static int i2c_wait_done(struct i2c_adapter *i2c_adap)
61{
62 int count;
63
64 for (count = 0; count < I2C_WAIT_RETRY; count++) {
65 if (!i2c_is_busy(i2c_adap))
66 break;
67 udelay(I2C_WAIT_DELAY);
68 }
69
70 if (I2C_WAIT_RETRY == count)
71 return 0;
72
73 return 1;
74}
75
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030076static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
77 const struct i2c_msg *msg, int joined_rlen)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030078{
79 struct cx25821_i2c *bus = i2c_adap->algo_data;
80 struct cx25821_dev *dev = bus->dev;
81 u32 wdata, addr, ctrl;
82 int retval, cnt;
83
84 if (joined_rlen)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030085 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
86 msg->len, joined_rlen);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030087 else
88 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
89
90 /* Deal with i2c probe functions with zero payload */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030091 if (msg->len == 0) {
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030092 cx_write(bus->reg_addr, msg->addr << 25);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030093 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
94
95 if (!i2c_wait_done(i2c_adap))
96 return -EIO;
97
98 if (!i2c_slave_did_ack(i2c_adap))
99 return -EIO;
100
101 dprintk(1, "%s() returns 0\n", __func__);
102 return 0;
103 }
104
105 /* dev, reg + first byte */
106 addr = (msg->addr << 25) | msg->buf[0];
107 wdata = msg->buf[0];
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300108
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300109 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
110
111 if (msg->len > 1)
112 ctrl |= I2C_NOSTOP | I2C_EXTEND;
113 else if (joined_rlen)
114 ctrl |= I2C_NOSTOP;
115
116 cx_write(bus->reg_addr, addr);
117 cx_write(bus->reg_wdata, wdata);
118 cx_write(bus->reg_ctrl, ctrl);
119
120 retval = i2c_wait_done(i2c_adap);
121 if (retval < 0)
122 goto err;
123
124 if (retval == 0)
125 goto eio;
126
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300127 if (i2c_debug) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300128 if (!(ctrl & I2C_NOSTOP))
129 printk(" >\n");
130 }
131
132 for (cnt = 1; cnt < msg->len; cnt++) {
133 /* following bytes */
134 wdata = msg->buf[cnt];
135 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
136
137 if (cnt < msg->len - 1)
138 ctrl |= I2C_NOSTOP | I2C_EXTEND;
139 else if (joined_rlen)
140 ctrl |= I2C_NOSTOP;
141
142 cx_write(bus->reg_addr, addr);
143 cx_write(bus->reg_wdata, wdata);
144 cx_write(bus->reg_ctrl, ctrl);
145
146 retval = i2c_wait_done(i2c_adap);
147 if (retval < 0)
148 goto err;
149
150 if (retval == 0)
151 goto eio;
152
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300153 if (i2c_debug) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300154 dprintk(1, " %02x", msg->buf[cnt]);
155 if (!(ctrl & I2C_NOSTOP))
156 dprintk(1, " >\n");
157 }
158 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300159
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300160 return msg->len;
161
Olimpiu Pascariu51e9dd32010-03-21 14:52:31 -0300162eio:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300163 retval = -EIO;
Olimpiu Pascariu51e9dd32010-03-21 14:52:31 -0300164err:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300165 if (i2c_debug)
166 printk(KERN_ERR " ERR: %d\n", retval);
167 return retval;
168}
169
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300170static int i2c_readbytes(struct i2c_adapter *i2c_adap,
171 const struct i2c_msg *msg, int joined)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300172{
173 struct cx25821_i2c *bus = i2c_adap->algo_data;
174 struct cx25821_dev *dev = bus->dev;
175 u32 ctrl, cnt;
176 int retval;
177
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300178 if (i2c_debug && !joined)
179 dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len);
180
181 /* Deal with i2c probe functions with zero payload */
182 if (msg->len == 0) {
183 cx_write(bus->reg_addr, msg->addr << 25);
184 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
185 if (!i2c_wait_done(i2c_adap))
186 return -EIO;
187 if (!i2c_slave_did_ack(i2c_adap))
188 return -EIO;
189
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300190 dprintk(1, "%s() returns 0\n", __func__);
191 return 0;
192 }
193
194 if (i2c_debug) {
195 if (joined)
196 dprintk(1, " R");
197 else
198 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
199 }
200
201 for (cnt = 0; cnt < msg->len; cnt++) {
202
203 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
204
205 if (cnt < msg->len - 1)
206 ctrl |= I2C_NOSTOP | I2C_EXTEND;
207
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300208 cx_write(bus->reg_addr, msg->addr << 25);
209 cx_write(bus->reg_ctrl, ctrl);
210
211 retval = i2c_wait_done(i2c_adap);
212 if (retval < 0)
213 goto err;
214 if (retval == 0)
215 goto eio;
216 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
217
218 if (i2c_debug) {
219 dprintk(1, " %02x", msg->buf[cnt]);
220 if (!(ctrl & I2C_NOSTOP))
221 dprintk(1, " >\n");
222 }
223 }
224
225 return msg->len;
Olimpiu Pascariu51e9dd32010-03-21 14:52:31 -0300226eio:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300227 retval = -EIO;
Olimpiu Pascariu51e9dd32010-03-21 14:52:31 -0300228err:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300229 if (i2c_debug)
230 printk(KERN_ERR " ERR: %d\n", retval);
231 return retval;
232}
233
234static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
235{
236 struct cx25821_i2c *bus = i2c_adap->algo_data;
237 struct cx25821_dev *dev = bus->dev;
238 int i, retval = 0;
239
240 dprintk(1, "%s(num = %d)\n", __func__, num);
241
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300242 for (i = 0; i < num; i++) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300243 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
244 __func__, num, msgs[i].addr, msgs[i].len);
245
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300246 if (msgs[i].flags & I2C_M_RD) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300247 /* read */
248 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300249 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
250 msgs[i].addr == msgs[i + 1].addr) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300251 /* write then read from same address */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300252 retval =
253 i2c_sendbytes(i2c_adap, &msgs[i], msgs[i + 1].len);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300254
255 if (retval < 0)
256 goto err;
257 i++;
258 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300259 } else {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300260 /* write */
261 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
262 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300263
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300264 if (retval < 0)
265 goto err;
266 }
267 return num;
268
Olimpiu Pascariu51e9dd32010-03-21 14:52:31 -0300269err:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300270 return retval;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300271}
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300272
273
274static u32 cx25821_functionality(struct i2c_adapter *adap)
275{
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300276 return I2C_FUNC_SMBUS_EMUL |
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300277 I2C_FUNC_I2C |
278 I2C_FUNC_SMBUS_WORD_DATA |
279 I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300280}
281
282static struct i2c_algorithm cx25821_i2c_algo_template = {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300283 .master_xfer = i2c_xfer,
284 .functionality = cx25821_functionality,
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300285};
286
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300287static struct i2c_adapter cx25821_i2c_adap_template = {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300288 .name = "cx25821",
289 .owner = THIS_MODULE,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300290 .algo = &cx25821_i2c_algo_template,
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300291};
292
293static struct i2c_client cx25821_i2c_client_template = {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300294 .name = "cx25821 internal",
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300295};
296
297/* init + register i2c algo-bit adapter */
298int cx25821_i2c_register(struct cx25821_i2c *bus)
299{
300 struct cx25821_dev *dev = bus->dev;
301
302 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
303
304 memcpy(&bus->i2c_adap, &cx25821_i2c_adap_template,
305 sizeof(bus->i2c_adap));
306 memcpy(&bus->i2c_algo, &cx25821_i2c_algo_template,
307 sizeof(bus->i2c_algo));
308 memcpy(&bus->i2c_client, &cx25821_i2c_client_template,
309 sizeof(bus->i2c_client));
310
311 bus->i2c_adap.dev.parent = &dev->pci->dev;
312
313 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
314
315 bus->i2c_algo.data = bus;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300316 bus->i2c_adap.algo_data = bus;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300317 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
318 i2c_add_adapter(&bus->i2c_adap);
319
320 bus->i2c_client.adapter = &bus->i2c_adap;
321
Olimpiu Pascariu51e9dd32010-03-21 14:52:31 -0300322 /* set up the I2c */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300323 bus->i2c_client.addr = (0x88 >> 1);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300324
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300325 return bus->i2c_rc;
326}
327
328int cx25821_i2c_unregister(struct cx25821_i2c *bus)
329{
330 i2c_del_adapter(&bus->i2c_adap);
331 return 0;
332}
333
334void cx25821_av_clk(struct cx25821_dev *dev, int enable)
335{
336 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
337 char buffer[3];
338 struct i2c_msg msg;
339 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
340
341 /* Register 0x144 */
342 buffer[0] = 0x01;
343 buffer[1] = 0x44;
344 if (enable == 1)
345 buffer[2] = 0x05;
346 else
347 buffer[2] = 0x00;
348
349 msg.addr = 0x44;
350 msg.flags = I2C_M_TEN;
351 msg.len = 3;
352 msg.buf = buffer;
353
354 i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1);
355}
356
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300357int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value)
358{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300359 struct i2c_client *client = &bus->i2c_client;
360 int retval = 0;
361 int v = 0;
362 u8 addr[2] = { 0, 0 };
363 u8 buf[4] = { 0, 0, 0, 0 };
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300364
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300365 struct i2c_msg msgs[2] = {
366 {
367 .addr = client->addr,
368 .flags = 0,
369 .len = 2,
370 .buf = addr,
371 }, {
372 .addr = client->addr,
373 .flags = I2C_M_RD,
374 .len = 4,
375 .buf = buf,
376 }
377 };
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300378
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300379 addr[0] = (reg_addr >> 8);
380 addr[1] = (reg_addr & 0xff);
381 msgs[0].addr = 0x44;
382 msgs[1].addr = 0x44;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300383
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300384 retval = i2c_xfer(client->adapter, msgs, 2);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300385
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300386 v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
387 *value = v;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300388
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300389 return v;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300390}
391
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300392int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value)
393{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300394 struct i2c_client *client = &bus->i2c_client;
395 int retval = 0;
396 u8 buf[6] = { 0, 0, 0, 0, 0, 0 };
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300397
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300398 struct i2c_msg msgs[1] = {
399 {
400 .addr = client->addr,
401 .flags = 0,
402 .len = 6,
403 .buf = buf,
404 }
405 };
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300406
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300407 buf[0] = reg_addr >> 8;
408 buf[1] = reg_addr & 0xff;
409 buf[5] = (value >> 24) & 0xff;
410 buf[4] = (value >> 16) & 0xff;
411 buf[3] = (value >> 8) & 0xff;
412 buf[2] = value & 0xff;
413 client->flags = 0;
414 msgs[0].addr = 0x44;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300415
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300416 retval = i2c_xfer(client->adapter, msgs, 1);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300417
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300418 return retval;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300419}