blob: ed3d8f55029b936968d5bf384f70beaaaaa3f4c9 [file] [log] [blame]
Steven Tothd19770e2007-03-11 20:44:05 -03001/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
Steven Tothd19770e2007-03-11 20:44:05 -03005 *
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
Steven Toth4513fc692008-01-12 11:36:36 -030032static unsigned int i2c_debug;
Steven Tothd19770e2007-03-11 20:44:05 -030033module_param(i2c_debug, int, 0644);
Michael Krufky44a64812007-03-20 23:00:18 -030034MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
Steven Tothd19770e2007-03-11 20:44:05 -030035
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030036static unsigned int i2c_scan;
Steven Tothd19770e2007-03-11 20:44:05 -030037module_param(i2c_scan, int, 0444);
Michael Krufky44a64812007-03-20 23:00:18 -030038MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
Steven Tothd19770e2007-03-11 20:44:05 -030039
Steven Toth4513fc692008-01-12 11:36:36 -030040#define dprintk(level, fmt, arg...)\
41 do { if (i2c_debug >= level)\
42 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
Chris Pascoe5e3c5962007-12-15 03:31:09 -030043 } while (0)
Steven Tothd19770e2007-03-11 20:44:05 -030044
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
Michael Krufky44a64812007-03-20 23:00:18 -030081static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
Chris Pascoe5e3c5962007-12-15 03:31:09 -030082 const struct i2c_msg *msg, int joined_rlen)
Steven Tothd19770e2007-03-11 20:44:05 -030083{
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
Chris Pascoe5e3c5962007-12-15 03:31:09 -030089 if (joined_rlen)
Harvey Harrison22b4e642008-04-08 23:20:00 -030090 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
Chris Pascoe5e3c5962007-12-15 03:31:09 -030091 msg->len, joined_rlen);
92 else
Harvey Harrison22b4e642008-04-08 23:20:00 -030093 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
Steven Tothe92adc22007-09-20 01:44:27 -030094
Steven Tothd19770e2007-03-11 20:44:05 -030095 /* 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))
Jean Delvaref4acb3c2010-07-18 16:48:47 -0300102 return -ENXIO;
Steven Tothd19770e2007-03-11 20:44:05 -0300103
Harvey Harrison22b4e642008-04-08 23:20:00 -0300104 dprintk(1, "%s() returns 0\n", __func__);
Steven Tothd19770e2007-03-11 20:44:05 -0300105 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;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300116 else if (joined_rlen)
117 ctrl |= I2C_NOSTOP;
Steven Tothd19770e2007-03-11 20:44:05 -0300118
119 cx_write(bus->reg_addr, addr);
120 cx_write(bus->reg_wdata, wdata);
121 cx_write(bus->reg_ctrl, ctrl);
122
Jean Delvare18a87be2010-07-18 17:05:17 -0300123 if (!i2c_wait_done(i2c_adap))
Steven Tothd19770e2007-03-11 20:44:05 -0300124 goto eio;
Jean Delvare44835f12010-07-18 16:52:05 -0300125 if (!i2c_slave_did_ack(i2c_adap)) {
126 retval = -ENXIO;
127 goto err;
128 }
Steven Tothd19770e2007-03-11 20:44:05 -0300129 if (i2c_debug) {
130 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
131 if (!(ctrl & I2C_NOSTOP))
132 printk(" >\n");
133 }
134
Steven Toth9c8ced52008-10-16 20:18:44 -0300135 for (cnt = 1; cnt < msg->len; cnt++) {
Steven Tothd19770e2007-03-11 20:44:05 -0300136 /* following bytes */
137 wdata = msg->buf[cnt];
138 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
139
Steven Tothe92adc22007-09-20 01:44:27 -0300140 if (cnt < msg->len - 1)
Steven Tothd19770e2007-03-11 20:44:05 -0300141 ctrl |= I2C_NOSTOP | I2C_EXTEND;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300142 else if (joined_rlen)
143 ctrl |= I2C_NOSTOP;
Steven Tothd19770e2007-03-11 20:44:05 -0300144
Steven Tothd19770e2007-03-11 20:44:05 -0300145 cx_write(bus->reg_addr, addr);
146 cx_write(bus->reg_wdata, wdata);
147 cx_write(bus->reg_ctrl, ctrl);
148
Jean Delvare18a87be2010-07-18 17:05:17 -0300149 if (!i2c_wait_done(i2c_adap))
Steven Tothd19770e2007-03-11 20:44:05 -0300150 goto eio;
151 if (i2c_debug) {
Steven Toth9c8ced52008-10-16 20:18:44 -0300152 dprintk(1, " %02x", msg->buf[cnt]);
Steven Tothd19770e2007-03-11 20:44:05 -0300153 if (!(ctrl & I2C_NOSTOP))
Steven Toth9c8ced52008-10-16 20:18:44 -0300154 dprintk(1, " >\n");
Steven Tothd19770e2007-03-11 20:44:05 -0300155 }
156 }
157 return msg->len;
158
159 eio:
160 retval = -EIO;
161 err:
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300162 if (i2c_debug)
Steven Toth9c8ced52008-10-16 20:18:44 -0300163 printk(KERN_ERR " ERR: %d\n", retval);
Steven Tothd19770e2007-03-11 20:44:05 -0300164 return retval;
165}
166
Michael Krufky44a64812007-03-20 23:00:18 -0300167static int i2c_readbytes(struct i2c_adapter *i2c_adap,
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300168 const struct i2c_msg *msg, int joined)
Steven Tothd19770e2007-03-11 20:44:05 -0300169{
170 struct cx23885_i2c *bus = i2c_adap->algo_data;
171 struct cx23885_dev *dev = bus->dev;
172 u32 ctrl, cnt;
173 int retval;
174
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300175
176 if (i2c_debug && !joined)
Harvey Harrison22b4e642008-04-08 23:20:00 -0300177 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
Steven Tothd19770e2007-03-11 20:44:05 -0300178
179 /* Deal with i2c probe functions with zero payload */
180 if (msg->len == 0) {
181 cx_write(bus->reg_addr, msg->addr << 25);
182 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
183 if (!i2c_wait_done(i2c_adap))
184 return -EIO;
185 if (!i2c_slave_did_ack(i2c_adap))
Jean Delvaref4acb3c2010-07-18 16:48:47 -0300186 return -ENXIO;
Steven Tothd19770e2007-03-11 20:44:05 -0300187
188
Harvey Harrison22b4e642008-04-08 23:20:00 -0300189 dprintk(1, "%s() returns 0\n", __func__);
Steven Tothd19770e2007-03-11 20:44:05 -0300190 return 0;
191 }
192
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300193 if (i2c_debug) {
194 if (joined)
Steven Toth9c8ced52008-10-16 20:18:44 -0300195 dprintk(1, " R");
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300196 else
Steven Toth9c8ced52008-10-16 20:18:44 -0300197 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300198 }
Steven Tothe92adc22007-09-20 01:44:27 -0300199
Steven Toth9c8ced52008-10-16 20:18:44 -0300200 for (cnt = 0; cnt < msg->len; cnt++) {
Steven Tothd19770e2007-03-11 20:44:05 -0300201
202 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
203
Steven Tothe92adc22007-09-20 01:44:27 -0300204 if (cnt < msg->len - 1)
Steven Tothd19770e2007-03-11 20:44:05 -0300205 ctrl |= I2C_NOSTOP | I2C_EXTEND;
206
207 cx_write(bus->reg_addr, msg->addr << 25);
208 cx_write(bus->reg_ctrl, ctrl);
209
Jean Delvare18a87be2010-07-18 17:05:17 -0300210 if (!i2c_wait_done(i2c_adap))
Steven Tothd19770e2007-03-11 20:44:05 -0300211 goto eio;
Jean Delvare44835f12010-07-18 16:52:05 -0300212 if (cnt == 0 && !i2c_slave_did_ack(i2c_adap)) {
213 retval = -ENXIO;
214 goto err;
215 }
Steven Tothd19770e2007-03-11 20:44:05 -0300216 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
217 if (i2c_debug) {
Steven Toth9c8ced52008-10-16 20:18:44 -0300218 dprintk(1, " %02x", msg->buf[cnt]);
Steven Tothd19770e2007-03-11 20:44:05 -0300219 if (!(ctrl & I2C_NOSTOP))
Steven Toth9c8ced52008-10-16 20:18:44 -0300220 dprintk(1, " >\n");
Steven Tothd19770e2007-03-11 20:44:05 -0300221 }
222 }
223 return msg->len;
224
225 eio:
226 retval = -EIO;
227 err:
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300228 if (i2c_debug)
Steven Toth9c8ced52008-10-16 20:18:44 -0300229 printk(KERN_ERR " ERR: %d\n", retval);
Steven Tothd19770e2007-03-11 20:44:05 -0300230 return retval;
231}
232
Michael Krufky44a64812007-03-20 23:00:18 -0300233static int i2c_xfer(struct i2c_adapter *i2c_adap,
234 struct i2c_msg *msgs, int num)
Steven Tothd19770e2007-03-11 20:44:05 -0300235{
236 struct cx23885_i2c *bus = i2c_adap->algo_data;
237 struct cx23885_dev *dev = bus->dev;
238 int i, retval = 0;
239
Harvey Harrison22b4e642008-04-08 23:20:00 -0300240 dprintk(1, "%s(num = %d)\n", __func__, num);
Steven Tothd19770e2007-03-11 20:44:05 -0300241
242 for (i = 0 ; i < num; i++) {
Michael Krufky44a64812007-03-20 23:00:18 -0300243 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300244 __func__, num, msgs[i].addr, msgs[i].len);
Steven Tothd19770e2007-03-11 20:44:05 -0300245 if (msgs[i].flags & I2C_M_RD) {
246 /* read */
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300247 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
248 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
249 msgs[i].addr == msgs[i + 1].addr) {
250 /* write then read from same address */
251 retval = i2c_sendbytes(i2c_adap, &msgs[i],
252 msgs[i + 1].len);
Steven Tothd19770e2007-03-11 20:44:05 -0300253 if (retval < 0)
254 goto err;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300255 i++;
256 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
Steven Tothd19770e2007-03-11 20:44:05 -0300257 } else {
258 /* write */
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300259 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
Steven Tothd19770e2007-03-11 20:44:05 -0300260 }
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300261 if (retval < 0)
262 goto err;
Steven Tothd19770e2007-03-11 20:44:05 -0300263 }
264 return num;
265
266 err:
267 return retval;
268}
269
Steven Tothd19770e2007-03-11 20:44:05 -0300270static u32 cx23885_functionality(struct i2c_adapter *adap)
271{
Steven Toth0fc07392007-03-19 18:03:03 -0300272 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
Steven Tothd19770e2007-03-11 20:44:05 -0300273}
274
275static struct i2c_algorithm cx23885_i2c_algo_template = {
276 .master_xfer = i2c_xfer,
Steven Tothd19770e2007-03-11 20:44:05 -0300277 .functionality = cx23885_functionality,
278};
279
280/* ----------------------------------------------------------------------- */
281
282static struct i2c_adapter cx23885_i2c_adap_template = {
283 .name = "cx23885",
284 .owner = THIS_MODULE,
Steven Tothd19770e2007-03-11 20:44:05 -0300285 .algo = &cx23885_i2c_algo_template,
Steven Tothd19770e2007-03-11 20:44:05 -0300286};
287
288static struct i2c_client cx23885_i2c_client_template = {
289 .name = "cx23885 internal",
290};
291
292static char *i2c_devs[128] = {
Steven Toth9c8ced52008-10-16 20:18:44 -0300293 [0x10 >> 1] = "tda10048",
294 [0x12 >> 1] = "dib7000pc",
295 [0x1c >> 1] = "lgdt3303",
296 [0x86 >> 1] = "tda9887",
297 [0x32 >> 1] = "cx24227",
298 [0x88 >> 1] = "cx25837",
299 [0x84 >> 1] = "tda8295",
300 [0xa0 >> 1] = "eeprom",
301 [0xc0 >> 1] = "tuner/mt2131/tda8275",
Steven Toth66762372008-04-22 15:38:26 -0300302 [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
Steven Toth9c8ced52008-10-16 20:18:44 -0300303 [0xc8 >> 1] = "tuner/xc3028L",
Steven Tothd19770e2007-03-11 20:44:05 -0300304};
305
306static void do_i2c_scan(char *name, struct i2c_client *c)
307{
308 unsigned char buf;
Michael Krufky44a64812007-03-20 23:00:18 -0300309 int i, rc;
Steven Tothd19770e2007-03-11 20:44:05 -0300310
311 for (i = 0; i < 128; i++) {
312 c->addr = i;
Michael Krufky44a64812007-03-20 23:00:18 -0300313 rc = i2c_master_recv(c, &buf, 0);
Steven Tothd19770e2007-03-11 20:44:05 -0300314 if (rc < 0)
315 continue;
Steven Toth9c8ced52008-10-16 20:18:44 -0300316 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
Steven Tothd19770e2007-03-11 20:44:05 -0300317 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
318 }
319}
320
321/* init + register i2c algo-bit adapter */
322int cx23885_i2c_register(struct cx23885_i2c *bus)
323{
324 struct cx23885_dev *dev = bus->dev;
325
Harvey Harrison22b4e642008-04-08 23:20:00 -0300326 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
Steven Tothd19770e2007-03-11 20:44:05 -0300327
Michael Krufky44a64812007-03-20 23:00:18 -0300328 memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template,
329 sizeof(bus->i2c_adap));
330 memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
331 sizeof(bus->i2c_algo));
332 memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
333 sizeof(bus->i2c_client));
Steven Tothd19770e2007-03-11 20:44:05 -0300334
335 bus->i2c_adap.dev.parent = &dev->pci->dev;
336
Michael Krufky44a64812007-03-20 23:00:18 -0300337 strlcpy(bus->i2c_adap.name, bus->dev->name,
338 sizeof(bus->i2c_adap.name));
Steven Toth2e52f212007-09-04 21:32:41 -0300339
Steven Tothd19770e2007-03-11 20:44:05 -0300340 bus->i2c_algo.data = bus;
341 bus->i2c_adap.algo_data = bus;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300342 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
Steven Tothd19770e2007-03-11 20:44:05 -0300343 i2c_add_adapter(&bus->i2c_adap);
344
345 bus->i2c_client.adapter = &bus->i2c_adap;
346
347 if (0 == bus->i2c_rc) {
Steven Toth9c8ced52008-10-16 20:18:44 -0300348 dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300349 if (i2c_scan) {
350 printk(KERN_INFO "%s: scan bus %d:\n",
351 dev->name, bus->nr);
Steven Tothd19770e2007-03-11 20:44:05 -0300352 do_i2c_scan(dev->name, &bus->i2c_client);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300353 }
Steven Tothd19770e2007-03-11 20:44:05 -0300354 } else
Steven Toth9c8ced52008-10-16 20:18:44 -0300355 printk(KERN_WARNING "%s: i2c bus %d register FAILED\n",
356 dev->name, bus->nr);
Steven Tothd19770e2007-03-11 20:44:05 -0300357
Jean Delvarec668f322009-05-13 16:48:50 -0300358 /* Instantiate the IR receiver device, if present */
359 if (0 == bus->i2c_rc) {
360 struct i2c_board_info info;
361 const unsigned short addr_list[] = {
362 0x6b, I2C_CLIENT_END
363 };
364
365 memset(&info, 0, sizeof(struct i2c_board_info));
366 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
Jean Delvared44f19d2010-08-11 18:20:57 +0200367 /* Use quick read command for probe, some IR chips don't
368 * support writes */
369 i2c_new_probed_device(&bus->i2c_adap, &info, addr_list,
370 i2c_probe_func_quick_read);
Jean Delvarec668f322009-05-13 16:48:50 -0300371 }
372
Steven Tothd19770e2007-03-11 20:44:05 -0300373 return bus->i2c_rc;
374}
375
376int cx23885_i2c_unregister(struct cx23885_i2c *bus)
377{
378 i2c_del_adapter(&bus->i2c_adap);
379 return 0;
380}
381
Steven Totha589b662008-01-13 23:44:47 -0300382void cx23885_av_clk(struct cx23885_dev *dev, int enable)
383{
384 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
385 char buffer[3];
386 struct i2c_msg msg;
387 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
388
389 /* Register 0x144 */
390 buffer[0] = 0x01;
391 buffer[1] = 0x44;
392 if (enable == 1)
393 buffer[2] = 0x05;
394 else
395 buffer[2] = 0x00;
396
397 msg.addr = 0x44;
398 msg.flags = I2C_M_TEN;
399 msg.len = 3;
400 msg.buf = buffer;
401
402 i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
403}
404
Steven Tothd19770e2007-03-11 20:44:05 -0300405/* ----------------------------------------------------------------------- */
406
Steven Tothd19770e2007-03-11 20:44:05 -0300407/*
408 * Local variables:
409 * c-basic-offset: 8
410 * End:
411 */