2 * Driver for the Conexant CX23885 PCIe bridge
4 * Copyright (c) 2006 Steven Toth <stoth@hauppauge.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
30 #include <media/v4l2-common.h>
32 static unsigned int i2c_debug
;
33 module_param(i2c_debug
, int, 0644);
34 MODULE_PARM_DESC(i2c_debug
, "enable debug messages [i2c]");
36 static unsigned int i2c_scan
;
37 module_param(i2c_scan
, int, 0444);
38 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
40 #define dprintk(level, fmt, arg...)\
41 do { if (i2c_debug >= level)\
42 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
45 #define I2C_WAIT_DELAY 32
46 #define I2C_WAIT_RETRY 64
48 #define I2C_EXTEND (1 << 3)
49 #define I2C_NOSTOP (1 << 4)
51 static inline int i2c_slave_did_ack(struct i2c_adapter
*i2c_adap
)
53 struct cx23885_i2c
*bus
= i2c_adap
->algo_data
;
54 struct cx23885_dev
*dev
= bus
->dev
;
55 return cx_read(bus
->reg_stat
) & 0x01;
58 static inline int i2c_is_busy(struct i2c_adapter
*i2c_adap
)
60 struct cx23885_i2c
*bus
= i2c_adap
->algo_data
;
61 struct cx23885_dev
*dev
= bus
->dev
;
62 return cx_read(bus
->reg_stat
) & 0x02 ? 1 : 0;
65 static int i2c_wait_done(struct i2c_adapter
*i2c_adap
)
69 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
70 if (!i2c_is_busy(i2c_adap
))
72 udelay(I2C_WAIT_DELAY
);
75 if (I2C_WAIT_RETRY
== count
)
81 static int i2c_sendbytes(struct i2c_adapter
*i2c_adap
,
82 const struct i2c_msg
*msg
, int joined_rlen
)
84 struct cx23885_i2c
*bus
= i2c_adap
->algo_data
;
85 struct cx23885_dev
*dev
= bus
->dev
;
86 u32 wdata
, addr
, ctrl
;
90 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__
,
91 msg
->len
, joined_rlen
);
93 dprintk(1, "%s(msg->len=%d)\n", __func__
, msg
->len
);
95 /* Deal with i2c probe functions with zero payload */
97 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
98 cx_write(bus
->reg_ctrl
, bus
->i2c_period
| (1 << 2));
99 if (!i2c_wait_done(i2c_adap
))
101 if (!i2c_slave_did_ack(i2c_adap
))
104 dprintk(1, "%s() returns 0\n", __func__
);
109 /* dev, reg + first byte */
110 addr
= (msg
->addr
<< 25) | msg
->buf
[0];
112 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2);
115 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
116 else if (joined_rlen
)
119 cx_write(bus
->reg_addr
, addr
);
120 cx_write(bus
->reg_wdata
, wdata
);
121 cx_write(bus
->reg_ctrl
, ctrl
);
123 retval
= i2c_wait_done(i2c_adap
);
129 printk(" <W %02x %02x", msg
->addr
<< 1, msg
->buf
[0]);
130 if (!(ctrl
& I2C_NOSTOP
))
134 for (cnt
= 1; cnt
< msg
->len
; cnt
++ ) {
135 /* following bytes */
136 wdata
= msg
->buf
[cnt
];
137 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2);
139 if (cnt
< msg
->len
- 1)
140 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
141 else if (joined_rlen
)
144 cx_write(bus
->reg_addr
, addr
);
145 cx_write(bus
->reg_wdata
, wdata
);
146 cx_write(bus
->reg_ctrl
, ctrl
);
148 retval
= i2c_wait_done(i2c_adap
);
154 printk(" %02x", msg
->buf
[cnt
]);
155 if (!(ctrl
& I2C_NOSTOP
))
165 printk(" ERR: %d\n", retval
);
169 static int i2c_readbytes(struct i2c_adapter
*i2c_adap
,
170 const struct i2c_msg
*msg
, int joined
)
172 struct cx23885_i2c
*bus
= i2c_adap
->algo_data
;
173 struct cx23885_dev
*dev
= bus
->dev
;
178 if (i2c_debug
&& !joined
)
179 dprintk(1, "%s(msg->len=%d)\n", __func__
, msg
->len
);
181 /* Deal with i2c probe functions with zero payload */
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
))
187 if (!i2c_slave_did_ack(i2c_adap
))
191 dprintk(1, "%s() returns 0\n", __func__
);
199 printk(" <R %02x", (msg
->addr
<< 1) + 1);
202 for(cnt
= 0; cnt
< msg
->len
; cnt
++) {
204 ctrl
= bus
->i2c_period
| (1 << 12) | (1 << 2) | 1;
206 if (cnt
< msg
->len
- 1)
207 ctrl
|= I2C_NOSTOP
| I2C_EXTEND
;
209 cx_write(bus
->reg_addr
, msg
->addr
<< 25);
210 cx_write(bus
->reg_ctrl
, ctrl
);
212 retval
= i2c_wait_done(i2c_adap
);
217 msg
->buf
[cnt
] = cx_read(bus
->reg_rdata
) & 0xff;
219 printk(" %02x", msg
->buf
[cnt
]);
220 if (!(ctrl
& I2C_NOSTOP
))
230 printk(" ERR: %d\n", retval
);
234 static int i2c_xfer(struct i2c_adapter
*i2c_adap
,
235 struct i2c_msg
*msgs
, int num
)
237 struct cx23885_i2c
*bus
= i2c_adap
->algo_data
;
238 struct cx23885_dev
*dev
= bus
->dev
;
241 dprintk(1, "%s(num = %d)\n", __func__
, num
);
243 for (i
= 0 ; i
< num
; i
++) {
244 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
245 __func__
, num
, msgs
[i
].addr
, msgs
[i
].len
);
246 if (msgs
[i
].flags
& I2C_M_RD
) {
248 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 0);
249 } else if (i
+ 1 < num
&& (msgs
[i
+ 1].flags
& I2C_M_RD
) &&
250 msgs
[i
].addr
== msgs
[i
+ 1].addr
) {
251 /* write then read from same address */
252 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
],
257 retval
= i2c_readbytes(i2c_adap
, &msgs
[i
], 1);
260 retval
= i2c_sendbytes(i2c_adap
, &msgs
[i
], 0);
271 static int attach_inform(struct i2c_client
*client
)
273 struct cx23885_i2c
*bus
= i2c_get_adapdata(client
->adapter
);
274 struct cx23885_dev
*dev
= bus
->dev
;
275 struct tuner_setup tun_setup
;
277 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
278 client
->driver
->driver
.name
, client
->addr
, client
->name
);
280 if (!client
->driver
->command
)
283 if (dev
->tuner_type
!= UNSET
) {
285 dprintk(1, "%s (tuner) i2c attach [addr=0x%x,client=%s]\n",
286 client
->driver
->driver
.name
, client
->addr
,
289 if ((dev
->tuner_addr
== ADDR_UNSET
) ||
290 (dev
->tuner_addr
== client
->addr
)) {
292 dprintk(1, "%s (tuner || addr UNSET)\n",
293 client
->driver
->driver
.name
);
295 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
296 client
->driver
->driver
.name
,
297 client
->addr
, client
->name
);
299 tun_setup
.mode_mask
= T_ANALOG_TV
;
300 tun_setup
.type
= dev
->tuner_type
;
301 tun_setup
.addr
= dev
->tuner_addr
;
303 client
->driver
->command(client
, TUNER_SET_TYPE_ADDR
,
311 static int detach_inform(struct i2c_client
*client
)
313 struct cx23885_dev
*dev
= i2c_get_adapdata(client
->adapter
);
315 dprintk(1, "i2c detach [client=%s]\n", client
->name
);
320 void cx23885_call_i2c_clients(struct cx23885_i2c
*bus
,
321 unsigned int cmd
, void *arg
)
323 if (bus
->i2c_rc
!= 0)
326 i2c_clients_command(&bus
->i2c_adap
, cmd
, arg
);
329 static u32
cx23885_functionality(struct i2c_adapter
*adap
)
331 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
334 static struct i2c_algorithm cx23885_i2c_algo_template
= {
335 .master_xfer
= i2c_xfer
,
336 .functionality
= cx23885_functionality
,
339 /* ----------------------------------------------------------------------- */
341 static struct i2c_adapter cx23885_i2c_adap_template
= {
343 .owner
= THIS_MODULE
,
344 .id
= I2C_HW_B_CX23885
,
345 .algo
= &cx23885_i2c_algo_template
,
346 .class = I2C_CLASS_TV_ANALOG
,
347 .client_register
= attach_inform
,
348 .client_unregister
= detach_inform
,
351 static struct i2c_client cx23885_i2c_client_template
= {
352 .name
= "cx23885 internal",
355 static char *i2c_devs
[128] = {
356 [0x10 >> 1] = "tda10048",
357 [0x12 >> 1] = "dib7000pc",
358 [ 0x1c >> 1 ] = "lgdt3303",
359 [ 0x86 >> 1 ] = "tda9887",
360 [ 0x32 >> 1 ] = "cx24227",
361 [ 0x88 >> 1 ] = "cx25837",
362 [ 0x84 >> 1 ] = "tda8295",
363 [ 0xa0 >> 1 ] = "eeprom",
364 [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275",
365 [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
366 [0xc8 >> 1] = "tuner/xc3028L",
369 static void do_i2c_scan(char *name
, struct i2c_client
*c
)
374 for (i
= 0; i
< 128; i
++) {
376 rc
= i2c_master_recv(c
, &buf
, 0);
379 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
380 name
, i
<< 1, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
384 /* init + register i2c algo-bit adapter */
385 int cx23885_i2c_register(struct cx23885_i2c
*bus
)
387 struct cx23885_dev
*dev
= bus
->dev
;
389 dprintk(1, "%s(bus = %d)\n", __func__
, bus
->nr
);
391 memcpy(&bus
->i2c_adap
, &cx23885_i2c_adap_template
,
392 sizeof(bus
->i2c_adap
));
393 memcpy(&bus
->i2c_algo
, &cx23885_i2c_algo_template
,
394 sizeof(bus
->i2c_algo
));
395 memcpy(&bus
->i2c_client
, &cx23885_i2c_client_template
,
396 sizeof(bus
->i2c_client
));
398 bus
->i2c_adap
.dev
.parent
= &dev
->pci
->dev
;
400 strlcpy(bus
->i2c_adap
.name
, bus
->dev
->name
,
401 sizeof(bus
->i2c_adap
.name
));
403 bus
->i2c_algo
.data
= bus
;
404 bus
->i2c_adap
.algo_data
= bus
;
405 i2c_set_adapdata(&bus
->i2c_adap
, bus
);
406 i2c_add_adapter(&bus
->i2c_adap
);
408 bus
->i2c_client
.adapter
= &bus
->i2c_adap
;
410 if (0 == bus
->i2c_rc
) {
411 printk("%s: i2c bus %d registered\n", dev
->name
, bus
->nr
);
413 do_i2c_scan(dev
->name
, &bus
->i2c_client
);
415 printk("%s: i2c bus %d register FAILED\n", dev
->name
, bus
->nr
);
420 int cx23885_i2c_unregister(struct cx23885_i2c
*bus
)
422 i2c_del_adapter(&bus
->i2c_adap
);
426 void cx23885_av_clk(struct cx23885_dev
*dev
, int enable
)
428 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
431 dprintk(1, "%s(enabled = %d)\n", __func__
, enable
);
442 msg
.flags
= I2C_M_TEN
;
446 i2c_xfer(&dev
->i2c_bus
[2].i2c_adap
, &msg
, 1);
449 /* ----------------------------------------------------------------------- */