]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/cx23885/cx23885-i2c.c
V4L/DVB (7672): dib7000p: Add output mode param to the attach struct
[net-next-2.6.git] / drivers / media / video / cx23885 / cx23885-i2c.c
CommitLineData
d19770e5
ST
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@hauppauge.com>
5 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
16 *
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.
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <asm/io.h>
27
28#include "cx23885.h"
29
30#include <media/v4l2-common.h>
31
4513fc69 32static unsigned int i2c_debug;
d19770e5 33module_param(i2c_debug, int, 0644);
44a6481d 34MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
d19770e5 35
ff699e6b 36static unsigned int i2c_scan;
d19770e5 37module_param(i2c_scan, int, 0444);
44a6481d 38MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
d19770e5 39
4513fc69
ST
40#define dprintk(level, fmt, arg...)\
41 do { if (i2c_debug >= level)\
42 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
5e3c5967 43 } while (0)
d19770e5
ST
44
45#define I2C_WAIT_DELAY 32
46#define I2C_WAIT_RETRY 64
47
48#define I2C_EXTEND (1 << 3)
49#define I2C_NOSTOP (1 << 4)
50
51static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
52{
53 struct cx23885_i2c *bus = i2c_adap->algo_data;
54 struct cx23885_dev *dev = bus->dev;
55 return cx_read(bus->reg_stat) & 0x01;
56}
57
58static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
59{
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;
63}
64
65static int i2c_wait_done(struct i2c_adapter *i2c_adap)
66{
67 int count;
68
69 for (count = 0; count < I2C_WAIT_RETRY; count++) {
70 if (!i2c_is_busy(i2c_adap))
71 break;
72 udelay(I2C_WAIT_DELAY);
73 }
74
75 if (I2C_WAIT_RETRY == count)
76 return 0;
77
78 return 1;
79}
80
44a6481d 81static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
5e3c5967 82 const struct i2c_msg *msg, int joined_rlen)
d19770e5
ST
83{
84 struct cx23885_i2c *bus = i2c_adap->algo_data;
85 struct cx23885_dev *dev = bus->dev;
86 u32 wdata, addr, ctrl;
87 int retval, cnt;
88
5e3c5967 89 if (joined_rlen)
22b4e64f 90 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
5e3c5967
CP
91 msg->len, joined_rlen);
92 else
22b4e64f 93 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
e92adc2c 94
d19770e5
ST
95 /* Deal with i2c probe functions with zero payload */
96 if (msg->len == 0) {
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))
100 return -EIO;
101 if (!i2c_slave_did_ack(i2c_adap))
102 return -EIO;
103
22b4e64f 104 dprintk(1, "%s() returns 0\n", __func__);
d19770e5
ST
105 return 0;
106 }
107
108
109 /* dev, reg + first byte */
110 addr = (msg->addr << 25) | msg->buf[0];
111 wdata = msg->buf[0];
112 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
113
114 if (msg->len > 1)
115 ctrl |= I2C_NOSTOP | I2C_EXTEND;
5e3c5967
CP
116 else if (joined_rlen)
117 ctrl |= I2C_NOSTOP;
d19770e5
ST
118
119 cx_write(bus->reg_addr, addr);
120 cx_write(bus->reg_wdata, wdata);
121 cx_write(bus->reg_ctrl, ctrl);
122
123 retval = i2c_wait_done(i2c_adap);
124 if (retval < 0)
125 goto err;
126 if (retval == 0)
127 goto eio;
128 if (i2c_debug) {
129 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
130 if (!(ctrl & I2C_NOSTOP))
131 printk(" >\n");
132 }
133
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);
138
e92adc2c 139 if (cnt < msg->len - 1)
d19770e5 140 ctrl |= I2C_NOSTOP | I2C_EXTEND;
5e3c5967
CP
141 else if (joined_rlen)
142 ctrl |= I2C_NOSTOP;
d19770e5 143
d19770e5
ST
144 cx_write(bus->reg_addr, addr);
145 cx_write(bus->reg_wdata, wdata);
146 cx_write(bus->reg_ctrl, ctrl);
147
148 retval = i2c_wait_done(i2c_adap);
149 if (retval < 0)
150 goto err;
151 if (retval == 0)
152 goto eio;
153 if (i2c_debug) {
154 printk(" %02x", msg->buf[cnt]);
155 if (!(ctrl & I2C_NOSTOP))
156 printk(" >\n");
157 }
158 }
159 return msg->len;
160
161 eio:
162 retval = -EIO;
163 err:
5e3c5967
CP
164 if (i2c_debug)
165 printk(" ERR: %d\n", retval);
d19770e5
ST
166 return retval;
167}
168
44a6481d 169static int i2c_readbytes(struct i2c_adapter *i2c_adap,
5e3c5967 170 const struct i2c_msg *msg, int joined)
d19770e5
ST
171{
172 struct cx23885_i2c *bus = i2c_adap->algo_data;
173 struct cx23885_dev *dev = bus->dev;
174 u32 ctrl, cnt;
175 int retval;
176
5e3c5967
CP
177
178 if (i2c_debug && !joined)
22b4e64f 179 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
d19770e5
ST
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
190
22b4e64f 191 dprintk(1, "%s() returns 0\n", __func__);
d19770e5
ST
192 return 0;
193 }
194
5e3c5967
CP
195 if (i2c_debug) {
196 if (joined)
197 printk(" R");
198 else
199 printk(" <R %02x", (msg->addr << 1) + 1);
200 }
e92adc2c 201
d19770e5
ST
202 for(cnt = 0; cnt < msg->len; cnt++) {
203
204 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
205
e92adc2c 206 if (cnt < msg->len - 1)
d19770e5
ST
207 ctrl |= I2C_NOSTOP | I2C_EXTEND;
208
209 cx_write(bus->reg_addr, msg->addr << 25);
210 cx_write(bus->reg_ctrl, ctrl);
211
212 retval = i2c_wait_done(i2c_adap);
213 if (retval < 0)
214 goto err;
215 if (retval == 0)
216 goto eio;
217 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
218 if (i2c_debug) {
e92adc2c 219 printk(" %02x", msg->buf[cnt]);
d19770e5
ST
220 if (!(ctrl & I2C_NOSTOP))
221 printk(" >\n");
222 }
223 }
224 return msg->len;
225
226 eio:
227 retval = -EIO;
228 err:
5e3c5967
CP
229 if (i2c_debug)
230 printk(" ERR: %d\n", retval);
d19770e5
ST
231 return retval;
232}
233
44a6481d
MK
234static int i2c_xfer(struct i2c_adapter *i2c_adap,
235 struct i2c_msg *msgs, int num)
d19770e5
ST
236{
237 struct cx23885_i2c *bus = i2c_adap->algo_data;
238 struct cx23885_dev *dev = bus->dev;
239 int i, retval = 0;
240
22b4e64f 241 dprintk(1, "%s(num = %d)\n", __func__, num);
d19770e5
ST
242
243 for (i = 0 ; i < num; i++) {
44a6481d 244 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
22b4e64f 245 __func__, num, msgs[i].addr, msgs[i].len);
d19770e5
ST
246 if (msgs[i].flags & I2C_M_RD) {
247 /* read */
5e3c5967
CP
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],
253 msgs[i + 1].len);
d19770e5
ST
254 if (retval < 0)
255 goto err;
5e3c5967
CP
256 i++;
257 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
d19770e5
ST
258 } else {
259 /* write */
5e3c5967 260 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
d19770e5 261 }
5e3c5967
CP
262 if (retval < 0)
263 goto err;
d19770e5
ST
264 }
265 return num;
266
267 err:
268 return retval;
269}
270
271static int attach_inform(struct i2c_client *client)
272{
7b888014
ST
273 struct cx23885_i2c *bus = i2c_get_adapdata(client->adapter);
274 struct cx23885_dev *dev = bus->dev;
275 struct tuner_setup tun_setup;
d19770e5
ST
276
277 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
278 client->driver->driver.name, client->addr, client->name);
279
280 if (!client->driver->command)
281 return 0;
282
7b888014
ST
283 if (dev->tuner_type != UNSET) {
284
285 dprintk(1, "%s (tuner) i2c attach [addr=0x%x,client=%s]\n",
286 client->driver->driver.name, client->addr,
287 client->name);
288
289 if ((dev->tuner_addr == ADDR_UNSET) ||
290 (dev->tuner_addr == client->addr)) {
291
292 dprintk(1, "%s (tuner || addr UNSET)\n",
293 client->driver->driver.name);
294
295 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
296 client->driver->driver.name,
297 client->addr, client->name);
298
299 tun_setup.mode_mask = T_ANALOG_TV;
300 tun_setup.type = dev->tuner_type;
301 tun_setup.addr = dev->tuner_addr;
302
303 client->driver->command(client, TUNER_SET_TYPE_ADDR,
304 &tun_setup);
305 }
306 }
307
d19770e5
ST
308 return 0;
309}
310
311static int detach_inform(struct i2c_client *client)
312{
313 struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
314
315 dprintk(1, "i2c detach [client=%s]\n", client->name);
316
317 return 0;
318}
319
44a6481d
MK
320void cx23885_call_i2c_clients(struct cx23885_i2c *bus,
321 unsigned int cmd, void *arg)
d19770e5 322{
d19770e5
ST
323 if (bus->i2c_rc != 0)
324 return;
325
60a41d3b 326 i2c_clients_command(&bus->i2c_adap, cmd, arg);
d19770e5
ST
327}
328
d19770e5
ST
329static u32 cx23885_functionality(struct i2c_adapter *adap)
330{
0fc0739b 331 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
d19770e5
ST
332}
333
334static struct i2c_algorithm cx23885_i2c_algo_template = {
335 .master_xfer = i2c_xfer,
d19770e5
ST
336 .functionality = cx23885_functionality,
337};
338
339/* ----------------------------------------------------------------------- */
340
341static struct i2c_adapter cx23885_i2c_adap_template = {
342 .name = "cx23885",
343 .owner = THIS_MODULE,
344 .id = I2C_HW_B_CX23885,
345 .algo = &cx23885_i2c_algo_template,
7b888014 346 .class = I2C_CLASS_TV_ANALOG,
d19770e5
ST
347 .client_register = attach_inform,
348 .client_unregister = detach_inform,
349};
350
351static struct i2c_client cx23885_i2c_client_template = {
352 .name = "cx23885 internal",
353};
354
355static char *i2c_devs[128] = {
b3ea0166 356 [0x10 >> 1] = "tda10048",
9bc37caa
MK
357 [ 0x1c >> 1 ] = "lgdt3303",
358 [ 0x86 >> 1 ] = "tda9887",
d19770e5
ST
359 [ 0x32 >> 1 ] = "cx24227",
360 [ 0x88 >> 1 ] = "cx25837",
361 [ 0x84 >> 1 ] = "tda8295",
362 [ 0xa0 >> 1 ] = "eeprom",
9bc37caa 363 [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275",
d1987d55 364 [ 0xc2 >> 1 ] = "tuner/mt2131/tda8275/xc5000",
d19770e5
ST
365};
366
367static void do_i2c_scan(char *name, struct i2c_client *c)
368{
369 unsigned char buf;
44a6481d 370 int i, rc;
d19770e5
ST
371
372 for (i = 0; i < 128; i++) {
373 c->addr = i;
44a6481d 374 rc = i2c_master_recv(c, &buf, 0);
d19770e5
ST
375 if (rc < 0)
376 continue;
377 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
378 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
379 }
380}
381
382/* init + register i2c algo-bit adapter */
383int cx23885_i2c_register(struct cx23885_i2c *bus)
384{
385 struct cx23885_dev *dev = bus->dev;
386
22b4e64f 387 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
d19770e5 388
44a6481d
MK
389 memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template,
390 sizeof(bus->i2c_adap));
391 memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
392 sizeof(bus->i2c_algo));
393 memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
394 sizeof(bus->i2c_client));
d19770e5
ST
395
396 bus->i2c_adap.dev.parent = &dev->pci->dev;
397
44a6481d
MK
398 strlcpy(bus->i2c_adap.name, bus->dev->name,
399 sizeof(bus->i2c_adap.name));
2e52f215 400
d19770e5
ST
401 bus->i2c_algo.data = bus;
402 bus->i2c_adap.algo_data = bus;
7b888014 403 i2c_set_adapdata(&bus->i2c_adap, bus);
d19770e5
ST
404 i2c_add_adapter(&bus->i2c_adap);
405
406 bus->i2c_client.adapter = &bus->i2c_adap;
407
408 if (0 == bus->i2c_rc) {
409 printk("%s: i2c bus %d registered\n", dev->name, bus->nr);
410 if (i2c_scan)
411 do_i2c_scan(dev->name, &bus->i2c_client);
412 } else
413 printk("%s: i2c bus %d register FAILED\n", dev->name, bus->nr);
414
415 return bus->i2c_rc;
416}
417
418int cx23885_i2c_unregister(struct cx23885_i2c *bus)
419{
420 i2c_del_adapter(&bus->i2c_adap);
421 return 0;
422}
423
424/* ----------------------------------------------------------------------- */
425
d19770e5
ST
426/*
427 * Local variables:
428 * c-basic-offset: 8
429 * End:
430 */