blob: f7e8a481696d37119b6412499bd0063969b210d4 [file] [log] [blame]
Steven Tothd19770e2007-03-11 20:44:05 -03001/*
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
Michael Krufky3c5666a2007-03-20 23:18:04 -030032static unsigned int i2c_debug = 0;
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
36static unsigned int i2c_scan = 0;
37module_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
40#define dprintk(level,fmt, arg...) if (i2c_debug >= level) \
41 printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg)
42
43#define I2C_WAIT_DELAY 32
44#define I2C_WAIT_RETRY 64
45
46#define I2C_EXTEND (1 << 3)
47#define I2C_NOSTOP (1 << 4)
48
49static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
50{
51 struct cx23885_i2c *bus = i2c_adap->algo_data;
52 struct cx23885_dev *dev = bus->dev;
53 return cx_read(bus->reg_stat) & 0x01;
54}
55
56static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
57{
58 struct cx23885_i2c *bus = i2c_adap->algo_data;
59 struct cx23885_dev *dev = bus->dev;
60 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
61}
62
63static int i2c_wait_done(struct i2c_adapter *i2c_adap)
64{
65 int count;
66
67 for (count = 0; count < I2C_WAIT_RETRY; count++) {
68 if (!i2c_is_busy(i2c_adap))
69 break;
70 udelay(I2C_WAIT_DELAY);
71 }
72
73 if (I2C_WAIT_RETRY == count)
74 return 0;
75
76 return 1;
77}
78
Michael Krufky44a64812007-03-20 23:00:18 -030079static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
80 const struct i2c_msg *msg, int last)
Steven Tothd19770e2007-03-11 20:44:05 -030081{
82 struct cx23885_i2c *bus = i2c_adap->algo_data;
83 struct cx23885_dev *dev = bus->dev;
84 u32 wdata, addr, ctrl;
85 int retval, cnt;
86
Steven Tothe92adc22007-09-20 01:44:27 -030087 dprintk(1, "%s(msg->len=%d, last=%d)\n", __FUNCTION__, msg->len, last);
88
Steven Tothd19770e2007-03-11 20:44:05 -030089 /* Deal with i2c probe functions with zero payload */
90 if (msg->len == 0) {
91 cx_write(bus->reg_addr, msg->addr << 25);
92 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
93 if (!i2c_wait_done(i2c_adap))
94 return -EIO;
95 if (!i2c_slave_did_ack(i2c_adap))
96 return -EIO;
97
98 dprintk(1, "%s() returns 0\n", __FUNCTION__);
99 return 0;
100 }
101
102
103 /* dev, reg + first byte */
104 addr = (msg->addr << 25) | msg->buf[0];
105 wdata = msg->buf[0];
106 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
107
108 if (msg->len > 1)
109 ctrl |= I2C_NOSTOP | I2C_EXTEND;
110
111 cx_write(bus->reg_addr, addr);
112 cx_write(bus->reg_wdata, wdata);
113 cx_write(bus->reg_ctrl, ctrl);
114
115 retval = i2c_wait_done(i2c_adap);
116 if (retval < 0)
117 goto err;
118 if (retval == 0)
119 goto eio;
120 if (i2c_debug) {
121 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
122 if (!(ctrl & I2C_NOSTOP))
123 printk(" >\n");
124 }
125
126 for (cnt = 1; cnt < msg->len; cnt++ ) {
127 /* following bytes */
128 wdata = msg->buf[cnt];
129 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
130
Steven Tothe92adc22007-09-20 01:44:27 -0300131 if (cnt < msg->len - 1)
Steven Tothd19770e2007-03-11 20:44:05 -0300132 ctrl |= I2C_NOSTOP | I2C_EXTEND;
133
Steven Tothd19770e2007-03-11 20:44:05 -0300134 cx_write(bus->reg_addr, addr);
135 cx_write(bus->reg_wdata, wdata);
136 cx_write(bus->reg_ctrl, ctrl);
137
138 retval = i2c_wait_done(i2c_adap);
139 if (retval < 0)
140 goto err;
141 if (retval == 0)
142 goto eio;
143 if (i2c_debug) {
144 printk(" %02x", msg->buf[cnt]);
145 if (!(ctrl & I2C_NOSTOP))
146 printk(" >\n");
147 }
148 }
149 return msg->len;
150
151 eio:
152 retval = -EIO;
153 err:
Michael Krufky44a64812007-03-20 23:00:18 -0300154 printk(" 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,
159 const struct i2c_msg *msg, int last)
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
Steven Tothe92adc22007-09-20 01:44:27 -0300166 dprintk(1, "%s(msg->len=%d, last=%d)\n", __FUNCTION__, msg->len, last);
Steven Tothd19770e2007-03-11 20:44:05 -0300167
168 /* Deal with i2c probe functions with zero payload */
169 if (msg->len == 0) {
170 cx_write(bus->reg_addr, msg->addr << 25);
171 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
172 if (!i2c_wait_done(i2c_adap))
173 return -EIO;
174 if (!i2c_slave_did_ack(i2c_adap))
175 return -EIO;
176
177
178 dprintk(1, "%s() returns 0\n", __FUNCTION__);
179 return 0;
180 }
181
Steven Tothe92adc22007-09-20 01:44:27 -0300182 if (i2c_debug)
183 printk(" <R %02x", (msg->addr << 1) + 1);
184
Steven Tothd19770e2007-03-11 20:44:05 -0300185 for(cnt = 0; cnt < msg->len; cnt++) {
186
187 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
188
Steven Tothe92adc22007-09-20 01:44:27 -0300189 if (cnt < msg->len - 1)
Steven Tothd19770e2007-03-11 20:44:05 -0300190 ctrl |= I2C_NOSTOP | I2C_EXTEND;
191
192 cx_write(bus->reg_addr, msg->addr << 25);
193 cx_write(bus->reg_ctrl, ctrl);
194
195 retval = i2c_wait_done(i2c_adap);
196 if (retval < 0)
197 goto err;
198 if (retval == 0)
199 goto eio;
200 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
201 if (i2c_debug) {
Steven Tothe92adc22007-09-20 01:44:27 -0300202 printk(" %02x", msg->buf[cnt]);
Steven Tothd19770e2007-03-11 20:44:05 -0300203 if (!(ctrl & I2C_NOSTOP))
204 printk(" >\n");
205 }
206 }
207 return msg->len;
208
209 eio:
210 retval = -EIO;
211 err:
Michael Krufky44a64812007-03-20 23:00:18 -0300212 printk(" ERR: %d\n", retval);
Steven Tothd19770e2007-03-11 20:44:05 -0300213 return retval;
214}
215
Michael Krufky44a64812007-03-20 23:00:18 -0300216static int i2c_xfer(struct i2c_adapter *i2c_adap,
217 struct i2c_msg *msgs, int num)
Steven Tothd19770e2007-03-11 20:44:05 -0300218{
219 struct cx23885_i2c *bus = i2c_adap->algo_data;
220 struct cx23885_dev *dev = bus->dev;
221 int i, retval = 0;
222
223 dprintk(1, "%s(num = %d)\n", __FUNCTION__, num);
224
225 for (i = 0 ; i < num; i++) {
Michael Krufky44a64812007-03-20 23:00:18 -0300226 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
227 __FUNCTION__, num, msgs[i].addr, msgs[i].len);
Steven Tothd19770e2007-03-11 20:44:05 -0300228 if (msgs[i].flags & I2C_M_RD) {
229 /* read */
230 retval = i2c_readbytes(i2c_adap, &msgs[i], i+1 == num);
231 if (retval < 0)
232 goto err;
233 } else {
234 /* write */
235 retval = i2c_sendbytes(i2c_adap, &msgs[i], i+1 == num);
236 if (retval < 0)
237 goto err;
238 }
239 }
240 return num;
241
242 err:
243 return retval;
244}
245
246static int attach_inform(struct i2c_client *client)
247{
248 struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
249
250 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
251 client->driver->driver.name, client->addr, client->name);
252
253 if (!client->driver->command)
254 return 0;
255
256 return 0;
257}
258
259static int detach_inform(struct i2c_client *client)
260{
261 struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
262
263 dprintk(1, "i2c detach [client=%s]\n", client->name);
264
265 return 0;
266}
267
Michael Krufky44a64812007-03-20 23:00:18 -0300268void cx23885_call_i2c_clients(struct cx23885_i2c *bus,
269 unsigned int cmd, void *arg)
Steven Tothd19770e2007-03-11 20:44:05 -0300270{
Steven Tothd19770e2007-03-11 20:44:05 -0300271 if (bus->i2c_rc != 0)
272 return;
273
Michael Krufky60a41d32007-03-20 23:03:52 -0300274 i2c_clients_command(&bus->i2c_adap, cmd, arg);
Steven Tothd19770e2007-03-11 20:44:05 -0300275}
276
Steven Tothd19770e2007-03-11 20:44:05 -0300277static u32 cx23885_functionality(struct i2c_adapter *adap)
278{
Steven Toth0fc07392007-03-19 18:03:03 -0300279 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
Steven Tothd19770e2007-03-11 20:44:05 -0300280}
281
282static struct i2c_algorithm cx23885_i2c_algo_template = {
283 .master_xfer = i2c_xfer,
Steven Tothd19770e2007-03-11 20:44:05 -0300284 .functionality = cx23885_functionality,
285};
286
287/* ----------------------------------------------------------------------- */
288
289static struct i2c_adapter cx23885_i2c_adap_template = {
290 .name = "cx23885",
291 .owner = THIS_MODULE,
292 .id = I2C_HW_B_CX23885,
293 .algo = &cx23885_i2c_algo_template,
Steven Tothd19770e2007-03-11 20:44:05 -0300294 .client_register = attach_inform,
295 .client_unregister = detach_inform,
296};
297
298static struct i2c_client cx23885_i2c_client_template = {
299 .name = "cx23885 internal",
300};
301
302static char *i2c_devs[128] = {
Michael Krufky9bc37ca2007-09-08 15:17:13 -0300303 [ 0x1c >> 1 ] = "lgdt3303",
304 [ 0x86 >> 1 ] = "tda9887",
Steven Tothd19770e2007-03-11 20:44:05 -0300305 [ 0x32 >> 1 ] = "cx24227",
306 [ 0x88 >> 1 ] = "cx25837",
307 [ 0x84 >> 1 ] = "tda8295",
308 [ 0xa0 >> 1 ] = "eeprom",
Michael Krufky9bc37ca2007-09-08 15:17:13 -0300309 [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275",
310 [ 0xc2 >> 1 ] = "tuner/mt2131/tda8275",
Steven Tothd19770e2007-03-11 20:44:05 -0300311};
312
313static void do_i2c_scan(char *name, struct i2c_client *c)
314{
315 unsigned char buf;
Michael Krufky44a64812007-03-20 23:00:18 -0300316 int i, rc;
Steven Tothd19770e2007-03-11 20:44:05 -0300317
318 for (i = 0; i < 128; i++) {
319 c->addr = i;
Michael Krufky44a64812007-03-20 23:00:18 -0300320 rc = i2c_master_recv(c, &buf, 0);
Steven Tothd19770e2007-03-11 20:44:05 -0300321 if (rc < 0)
322 continue;
323 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
324 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
325 }
326}
327
328/* init + register i2c algo-bit adapter */
329int cx23885_i2c_register(struct cx23885_i2c *bus)
330{
331 struct cx23885_dev *dev = bus->dev;
332
333 dprintk(1, "%s(bus = %d)\n", __FUNCTION__, bus->nr);
334
Michael Krufky44a64812007-03-20 23:00:18 -0300335 memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template,
336 sizeof(bus->i2c_adap));
337 memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
338 sizeof(bus->i2c_algo));
339 memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
340 sizeof(bus->i2c_client));
Steven Tothd19770e2007-03-11 20:44:05 -0300341
342 bus->i2c_adap.dev.parent = &dev->pci->dev;
343
Michael Krufky44a64812007-03-20 23:00:18 -0300344 strlcpy(bus->i2c_adap.name, bus->dev->name,
345 sizeof(bus->i2c_adap.name));
Steven Toth2e52f212007-09-04 21:32:41 -0300346
Steven Tothd19770e2007-03-11 20:44:05 -0300347 bus->i2c_algo.data = bus;
348 bus->i2c_adap.algo_data = bus;
349 i2c_add_adapter(&bus->i2c_adap);
350
351 bus->i2c_client.adapter = &bus->i2c_adap;
352
353 if (0 == bus->i2c_rc) {
354 printk("%s: i2c bus %d registered\n", dev->name, bus->nr);
355 if (i2c_scan)
356 do_i2c_scan(dev->name, &bus->i2c_client);
357 } else
358 printk("%s: i2c bus %d register FAILED\n", dev->name, bus->nr);
359
360 return bus->i2c_rc;
361}
362
363int cx23885_i2c_unregister(struct cx23885_i2c *bus)
364{
365 i2c_del_adapter(&bus->i2c_adap);
366 return 0;
367}
368
369/* ----------------------------------------------------------------------- */
370
371EXPORT_SYMBOL(cx23885_call_i2c_clients);
372
373/*
374 * Local variables:
375 * c-basic-offset: 8
376 * End:
377 */