blob: fd71306af6e2f8facfc64964a8c05eb88fe3c146 [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.
Steven Tothd19770e2007-03-11 20:44:05 -030016 */
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <asm/io.h>
23
24#include "cx23885.h"
25
26#include <media/v4l2-common.h>
27
Steven Toth4513fc692008-01-12 11:36:36 -030028static unsigned int i2c_debug;
Steven Tothd19770e2007-03-11 20:44:05 -030029module_param(i2c_debug, int, 0644);
Michael Krufky44a64812007-03-20 23:00:18 -030030MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
Steven Tothd19770e2007-03-11 20:44:05 -030031
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030032static unsigned int i2c_scan;
Steven Tothd19770e2007-03-11 20:44:05 -030033module_param(i2c_scan, int, 0444);
Michael Krufky44a64812007-03-20 23:00:18 -030034MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
Steven Tothd19770e2007-03-11 20:44:05 -030035
Steven Toth4513fc692008-01-12 11:36:36 -030036#define dprintk(level, fmt, arg...)\
37 do { if (i2c_debug >= level)\
38 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
Chris Pascoe5e3c5962007-12-15 03:31:09 -030039 } while (0)
Steven Tothd19770e2007-03-11 20:44:05 -030040
41#define I2C_WAIT_DELAY 32
42#define I2C_WAIT_RETRY 64
43
44#define I2C_EXTEND (1 << 3)
45#define I2C_NOSTOP (1 << 4)
46
47static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
48{
49 struct cx23885_i2c *bus = i2c_adap->algo_data;
50 struct cx23885_dev *dev = bus->dev;
51 return cx_read(bus->reg_stat) & 0x01;
52}
53
54static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
55{
56 struct cx23885_i2c *bus = i2c_adap->algo_data;
57 struct cx23885_dev *dev = bus->dev;
58 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
59}
60
61static int i2c_wait_done(struct i2c_adapter *i2c_adap)
62{
63 int count;
64
65 for (count = 0; count < I2C_WAIT_RETRY; count++) {
66 if (!i2c_is_busy(i2c_adap))
67 break;
68 udelay(I2C_WAIT_DELAY);
69 }
70
71 if (I2C_WAIT_RETRY == count)
72 return 0;
73
74 return 1;
75}
76
Michael Krufky44a64812007-03-20 23:00:18 -030077static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
Chris Pascoe5e3c5962007-12-15 03:31:09 -030078 const struct i2c_msg *msg, int joined_rlen)
Steven Tothd19770e2007-03-11 20:44:05 -030079{
80 struct cx23885_i2c *bus = i2c_adap->algo_data;
81 struct cx23885_dev *dev = bus->dev;
82 u32 wdata, addr, ctrl;
83 int retval, cnt;
84
Chris Pascoe5e3c5962007-12-15 03:31:09 -030085 if (joined_rlen)
Harvey Harrison22b4e642008-04-08 23:20:00 -030086 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
Chris Pascoe5e3c5962007-12-15 03:31:09 -030087 msg->len, joined_rlen);
88 else
Harvey Harrison22b4e642008-04-08 23:20:00 -030089 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
Steven Tothe92adc22007-09-20 01:44:27 -030090
Steven Tothd19770e2007-03-11 20:44:05 -030091 /* Deal with i2c probe functions with zero payload */
92 if (msg->len == 0) {
93 cx_write(bus->reg_addr, msg->addr << 25);
94 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
95 if (!i2c_wait_done(i2c_adap))
96 return -EIO;
97 if (!i2c_slave_did_ack(i2c_adap))
Jean Delvaref4acb3c2010-07-18 16:48:47 -030098 return -ENXIO;
Steven Tothd19770e2007-03-11 20:44:05 -030099
Harvey Harrison22b4e642008-04-08 23:20:00 -0300100 dprintk(1, "%s() returns 0\n", __func__);
Steven Tothd19770e2007-03-11 20:44:05 -0300101 return 0;
102 }
103
104
105 /* dev, reg + first byte */
106 addr = (msg->addr << 25) | msg->buf[0];
107 wdata = msg->buf[0];
108 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
109
110 if (msg->len > 1)
111 ctrl |= I2C_NOSTOP | I2C_EXTEND;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300112 else if (joined_rlen)
113 ctrl |= I2C_NOSTOP;
Steven Tothd19770e2007-03-11 20:44:05 -0300114
115 cx_write(bus->reg_addr, addr);
116 cx_write(bus->reg_wdata, wdata);
117 cx_write(bus->reg_ctrl, ctrl);
118
Jean Delvare18a87be2010-07-18 17:05:17 -0300119 if (!i2c_wait_done(i2c_adap))
Steven Tothd19770e2007-03-11 20:44:05 -0300120 goto eio;
121 if (i2c_debug) {
122 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
123 if (!(ctrl & I2C_NOSTOP))
124 printk(" >\n");
125 }
126
Steven Toth9c8ced52008-10-16 20:18:44 -0300127 for (cnt = 1; cnt < msg->len; cnt++) {
Steven Tothd19770e2007-03-11 20:44:05 -0300128 /* following bytes */
129 wdata = msg->buf[cnt];
130 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
131
Steven Tothe92adc22007-09-20 01:44:27 -0300132 if (cnt < msg->len - 1)
Steven Tothd19770e2007-03-11 20:44:05 -0300133 ctrl |= I2C_NOSTOP | I2C_EXTEND;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300134 else if (joined_rlen)
135 ctrl |= I2C_NOSTOP;
Steven Tothd19770e2007-03-11 20:44:05 -0300136
Steven Tothd19770e2007-03-11 20:44:05 -0300137 cx_write(bus->reg_addr, addr);
138 cx_write(bus->reg_wdata, wdata);
139 cx_write(bus->reg_ctrl, ctrl);
140
Jean Delvare18a87be2010-07-18 17:05:17 -0300141 if (!i2c_wait_done(i2c_adap))
Steven Tothd19770e2007-03-11 20:44:05 -0300142 goto eio;
143 if (i2c_debug) {
Steven Toth9c8ced52008-10-16 20:18:44 -0300144 dprintk(1, " %02x", msg->buf[cnt]);
Steven Tothd19770e2007-03-11 20:44:05 -0300145 if (!(ctrl & I2C_NOSTOP))
Steven Toth9c8ced52008-10-16 20:18:44 -0300146 dprintk(1, " >\n");
Steven Tothd19770e2007-03-11 20:44:05 -0300147 }
148 }
149 return msg->len;
150
151 eio:
152 retval = -EIO;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300153 if (i2c_debug)
Steven Toth9c8ced52008-10-16 20:18:44 -0300154 printk(KERN_ERR " ERR: %d\n", retval);
Steven Tothd19770e2007-03-11 20:44:05 -0300155 return retval;
156}
157
Michael Krufky44a64812007-03-20 23:00:18 -0300158static int i2c_readbytes(struct i2c_adapter *i2c_adap,
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300159 const struct i2c_msg *msg, int joined)
Steven Tothd19770e2007-03-11 20:44:05 -0300160{
161 struct cx23885_i2c *bus = i2c_adap->algo_data;
162 struct cx23885_dev *dev = bus->dev;
163 u32 ctrl, cnt;
164 int retval;
165
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300166
167 if (i2c_debug && !joined)
Harvey Harrison22b4e642008-04-08 23:20:00 -0300168 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
Steven Tothd19770e2007-03-11 20:44:05 -0300169
170 /* Deal with i2c probe functions with zero payload */
171 if (msg->len == 0) {
172 cx_write(bus->reg_addr, msg->addr << 25);
173 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
174 if (!i2c_wait_done(i2c_adap))
175 return -EIO;
176 if (!i2c_slave_did_ack(i2c_adap))
Jean Delvaref4acb3c2010-07-18 16:48:47 -0300177 return -ENXIO;
Steven Tothd19770e2007-03-11 20:44:05 -0300178
179
Harvey Harrison22b4e642008-04-08 23:20:00 -0300180 dprintk(1, "%s() returns 0\n", __func__);
Steven Tothd19770e2007-03-11 20:44:05 -0300181 return 0;
182 }
183
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300184 if (i2c_debug) {
185 if (joined)
Steven Toth9c8ced52008-10-16 20:18:44 -0300186 dprintk(1, " R");
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300187 else
Steven Toth9c8ced52008-10-16 20:18:44 -0300188 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300189 }
Steven Tothe92adc22007-09-20 01:44:27 -0300190
Steven Toth9c8ced52008-10-16 20:18:44 -0300191 for (cnt = 0; cnt < msg->len; cnt++) {
Steven Tothd19770e2007-03-11 20:44:05 -0300192
193 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
194
Steven Tothe92adc22007-09-20 01:44:27 -0300195 if (cnt < msg->len - 1)
Steven Tothd19770e2007-03-11 20:44:05 -0300196 ctrl |= I2C_NOSTOP | I2C_EXTEND;
197
198 cx_write(bus->reg_addr, msg->addr << 25);
199 cx_write(bus->reg_ctrl, ctrl);
200
Jean Delvare18a87be2010-07-18 17:05:17 -0300201 if (!i2c_wait_done(i2c_adap))
Steven Tothd19770e2007-03-11 20:44:05 -0300202 goto eio;
203 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
204 if (i2c_debug) {
Steven Toth9c8ced52008-10-16 20:18:44 -0300205 dprintk(1, " %02x", msg->buf[cnt]);
Steven Tothd19770e2007-03-11 20:44:05 -0300206 if (!(ctrl & I2C_NOSTOP))
Steven Toth9c8ced52008-10-16 20:18:44 -0300207 dprintk(1, " >\n");
Steven Tothd19770e2007-03-11 20:44:05 -0300208 }
209 }
210 return msg->len;
211
212 eio:
213 retval = -EIO;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300214 if (i2c_debug)
Steven Toth9c8ced52008-10-16 20:18:44 -0300215 printk(KERN_ERR " ERR: %d\n", retval);
Steven Tothd19770e2007-03-11 20:44:05 -0300216 return retval;
217}
218
Michael Krufky44a64812007-03-20 23:00:18 -0300219static int i2c_xfer(struct i2c_adapter *i2c_adap,
220 struct i2c_msg *msgs, int num)
Steven Tothd19770e2007-03-11 20:44:05 -0300221{
222 struct cx23885_i2c *bus = i2c_adap->algo_data;
223 struct cx23885_dev *dev = bus->dev;
224 int i, retval = 0;
225
Harvey Harrison22b4e642008-04-08 23:20:00 -0300226 dprintk(1, "%s(num = %d)\n", __func__, num);
Steven Tothd19770e2007-03-11 20:44:05 -0300227
228 for (i = 0 ; i < num; i++) {
Michael Krufky44a64812007-03-20 23:00:18 -0300229 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300230 __func__, num, msgs[i].addr, msgs[i].len);
Steven Tothd19770e2007-03-11 20:44:05 -0300231 if (msgs[i].flags & I2C_M_RD) {
232 /* read */
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300233 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
234 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
235 msgs[i].addr == msgs[i + 1].addr) {
236 /* write then read from same address */
237 retval = i2c_sendbytes(i2c_adap, &msgs[i],
238 msgs[i + 1].len);
Steven Tothd19770e2007-03-11 20:44:05 -0300239 if (retval < 0)
240 goto err;
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300241 i++;
242 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
Steven Tothd19770e2007-03-11 20:44:05 -0300243 } else {
244 /* write */
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300245 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
Steven Tothd19770e2007-03-11 20:44:05 -0300246 }
Chris Pascoe5e3c5962007-12-15 03:31:09 -0300247 if (retval < 0)
248 goto err;
Steven Tothd19770e2007-03-11 20:44:05 -0300249 }
250 return num;
251
252 err:
253 return retval;
254}
255
Steven Tothd19770e2007-03-11 20:44:05 -0300256static u32 cx23885_functionality(struct i2c_adapter *adap)
257{
Steven Toth0fc07392007-03-19 18:03:03 -0300258 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
Steven Tothd19770e2007-03-11 20:44:05 -0300259}
260
261static struct i2c_algorithm cx23885_i2c_algo_template = {
262 .master_xfer = i2c_xfer,
Steven Tothd19770e2007-03-11 20:44:05 -0300263 .functionality = cx23885_functionality,
264};
265
266/* ----------------------------------------------------------------------- */
267
268static struct i2c_adapter cx23885_i2c_adap_template = {
269 .name = "cx23885",
270 .owner = THIS_MODULE,
Steven Tothd19770e2007-03-11 20:44:05 -0300271 .algo = &cx23885_i2c_algo_template,
Steven Tothd19770e2007-03-11 20:44:05 -0300272};
273
274static struct i2c_client cx23885_i2c_client_template = {
275 .name = "cx23885 internal",
276};
277
278static char *i2c_devs[128] = {
Steven Toth9c8ced52008-10-16 20:18:44 -0300279 [0x10 >> 1] = "tda10048",
280 [0x12 >> 1] = "dib7000pc",
281 [0x1c >> 1] = "lgdt3303",
282 [0x86 >> 1] = "tda9887",
283 [0x32 >> 1] = "cx24227",
284 [0x88 >> 1] = "cx25837",
285 [0x84 >> 1] = "tda8295",
Steven Tothd9368da2011-10-10 11:09:55 -0300286 [0x98 >> 1] = "flatiron",
Steven Toth9c8ced52008-10-16 20:18:44 -0300287 [0xa0 >> 1] = "eeprom",
288 [0xc0 >> 1] = "tuner/mt2131/tda8275",
Steven Toth66762372008-04-22 15:38:26 -0300289 [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
Steven Toth9c8ced52008-10-16 20:18:44 -0300290 [0xc8 >> 1] = "tuner/xc3028L",
Steven Tothd19770e2007-03-11 20:44:05 -0300291};
292
293static void do_i2c_scan(char *name, struct i2c_client *c)
294{
295 unsigned char buf;
Michael Krufky44a64812007-03-20 23:00:18 -0300296 int i, rc;
Steven Tothd19770e2007-03-11 20:44:05 -0300297
298 for (i = 0; i < 128; i++) {
299 c->addr = i;
Michael Krufky44a64812007-03-20 23:00:18 -0300300 rc = i2c_master_recv(c, &buf, 0);
Steven Tothd19770e2007-03-11 20:44:05 -0300301 if (rc < 0)
302 continue;
Steven Toth9c8ced52008-10-16 20:18:44 -0300303 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
Steven Tothd19770e2007-03-11 20:44:05 -0300304 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
305 }
306}
307
Jean Delvarea824f0f2011-11-09 13:27:53 -0300308/* init + register i2c adapter */
Steven Tothd19770e2007-03-11 20:44:05 -0300309int cx23885_i2c_register(struct cx23885_i2c *bus)
310{
311 struct cx23885_dev *dev = bus->dev;
312
Harvey Harrison22b4e642008-04-08 23:20:00 -0300313 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
Steven Tothd19770e2007-03-11 20:44:05 -0300314
Ezequiel GarcĂ­a630081e2012-06-27 12:52:52 -0300315 bus->i2c_adap = cx23885_i2c_adap_template;
316 bus->i2c_client = cx23885_i2c_client_template;
Steven Tothd19770e2007-03-11 20:44:05 -0300317 bus->i2c_adap.dev.parent = &dev->pci->dev;
318
Michael Krufky44a64812007-03-20 23:00:18 -0300319 strlcpy(bus->i2c_adap.name, bus->dev->name,
320 sizeof(bus->i2c_adap.name));
Steven Toth2e52f212007-09-04 21:32:41 -0300321
Steven Tothd19770e2007-03-11 20:44:05 -0300322 bus->i2c_adap.algo_data = bus;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300323 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
Steven Tothd19770e2007-03-11 20:44:05 -0300324 i2c_add_adapter(&bus->i2c_adap);
325
326 bus->i2c_client.adapter = &bus->i2c_adap;
327
328 if (0 == bus->i2c_rc) {
Steven Toth9c8ced52008-10-16 20:18:44 -0300329 dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300330 if (i2c_scan) {
331 printk(KERN_INFO "%s: scan bus %d:\n",
332 dev->name, bus->nr);
Steven Tothd19770e2007-03-11 20:44:05 -0300333 do_i2c_scan(dev->name, &bus->i2c_client);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300334 }
Steven Tothd19770e2007-03-11 20:44:05 -0300335 } else
Steven Toth9c8ced52008-10-16 20:18:44 -0300336 printk(KERN_WARNING "%s: i2c bus %d register FAILED\n",
337 dev->name, bus->nr);
Steven Tothd19770e2007-03-11 20:44:05 -0300338
Jean Delvarec668f322009-05-13 16:48:50 -0300339 /* Instantiate the IR receiver device, if present */
340 if (0 == bus->i2c_rc) {
341 struct i2c_board_info info;
342 const unsigned short addr_list[] = {
343 0x6b, I2C_CLIENT_END
344 };
345
346 memset(&info, 0, sizeof(struct i2c_board_info));
347 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
Jean Delvared44f19d2010-08-11 18:20:57 +0200348 /* Use quick read command for probe, some IR chips don't
349 * support writes */
350 i2c_new_probed_device(&bus->i2c_adap, &info, addr_list,
351 i2c_probe_func_quick_read);
Jean Delvarec668f322009-05-13 16:48:50 -0300352 }
353
Steven Tothd19770e2007-03-11 20:44:05 -0300354 return bus->i2c_rc;
355}
356
357int cx23885_i2c_unregister(struct cx23885_i2c *bus)
358{
359 i2c_del_adapter(&bus->i2c_adap);
360 return 0;
361}
362
Steven Totha589b662008-01-13 23:44:47 -0300363void cx23885_av_clk(struct cx23885_dev *dev, int enable)
364{
365 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
366 char buffer[3];
367 struct i2c_msg msg;
368 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
369
370 /* Register 0x144 */
371 buffer[0] = 0x01;
372 buffer[1] = 0x44;
373 if (enable == 1)
374 buffer[2] = 0x05;
375 else
376 buffer[2] = 0x00;
377
378 msg.addr = 0x44;
379 msg.flags = I2C_M_TEN;
380 msg.len = 3;
381 msg.buf = buffer;
382
383 i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
384}