Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[sfrench/cifs-2.6.git] / drivers / media / pci / cx23885 / cx23885-i2c.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
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
18 #include "cx23885.h"
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <asm/io.h>
25
26 #include <media/v4l2-common.h>
27
28 static unsigned int i2c_debug;
29 module_param(i2c_debug, int, 0644);
30 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
31
32 static unsigned int i2c_scan;
33 module_param(i2c_scan, int, 0444);
34 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
35
36 #define dprintk(level, fmt, arg...)\
37         do { if (i2c_debug >= level)\
38                 printk(KERN_DEBUG pr_fmt("%s: i2c:" fmt), \
39                         __func__, ##arg); \
40         } while (0)
41
42 #define I2C_WAIT_DELAY 32
43 #define I2C_WAIT_RETRY 64
44
45 #define I2C_EXTEND  (1 << 3)
46 #define I2C_NOSTOP  (1 << 4)
47
48 static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
49 {
50         struct cx23885_i2c *bus = i2c_adap->algo_data;
51         struct cx23885_dev *dev = bus->dev;
52         return cx_read(bus->reg_stat) & 0x01;
53 }
54
55 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
56 {
57         struct cx23885_i2c *bus = i2c_adap->algo_data;
58         struct cx23885_dev *dev = bus->dev;
59         return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
60 }
61
62 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
63 {
64         int count;
65
66         for (count = 0; count < I2C_WAIT_RETRY; count++) {
67                 if (!i2c_is_busy(i2c_adap))
68                         break;
69                 udelay(I2C_WAIT_DELAY);
70         }
71
72         if (I2C_WAIT_RETRY == count)
73                 return 0;
74
75         return 1;
76 }
77
78 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
79                          const struct i2c_msg *msg, int joined_rlen)
80 {
81         struct cx23885_i2c *bus = i2c_adap->algo_data;
82         struct cx23885_dev *dev = bus->dev;
83         u32 wdata, addr, ctrl;
84         int retval, cnt;
85
86         if (joined_rlen)
87                 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
88                         msg->len, joined_rlen);
89         else
90                 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
91
92         /* Deal with i2c probe functions with zero payload */
93         if (msg->len == 0) {
94                 cx_write(bus->reg_addr, msg->addr << 25);
95                 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
96                 if (!i2c_wait_done(i2c_adap))
97                         return -EIO;
98                 if (!i2c_slave_did_ack(i2c_adap))
99                         return -ENXIO;
100
101                 dprintk(1, "%s() returns 0\n", __func__);
102                 return 0;
103         }
104
105
106         /* dev, reg + first byte */
107         addr = (msg->addr << 25) | msg->buf[0];
108         wdata = msg->buf[0];
109         ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
110
111         if (msg->len > 1)
112                 ctrl |= I2C_NOSTOP | I2C_EXTEND;
113         else if (joined_rlen)
114                 ctrl |= I2C_NOSTOP;
115
116         cx_write(bus->reg_addr, addr);
117         cx_write(bus->reg_wdata, wdata);
118         cx_write(bus->reg_ctrl, ctrl);
119
120         if (!i2c_wait_done(i2c_adap))
121                 goto eio;
122         if (i2c_debug) {
123                 printk(KERN_DEBUG " <W %02x %02x", msg->addr << 1, msg->buf[0]);
124                 if (!(ctrl & I2C_NOSTOP))
125                         pr_cont(" >\n");
126         }
127
128         for (cnt = 1; cnt < msg->len; cnt++) {
129                 /* following bytes */
130                 wdata = msg->buf[cnt];
131                 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
132
133                 if (cnt < msg->len - 1)
134                         ctrl |= I2C_NOSTOP | I2C_EXTEND;
135                 else if (joined_rlen)
136                         ctrl |= I2C_NOSTOP;
137
138                 cx_write(bus->reg_addr, addr);
139                 cx_write(bus->reg_wdata, wdata);
140                 cx_write(bus->reg_ctrl, ctrl);
141
142                 if (!i2c_wait_done(i2c_adap))
143                         goto eio;
144                 if (i2c_debug) {
145                         pr_cont(" %02x", msg->buf[cnt]);
146                         if (!(ctrl & I2C_NOSTOP))
147                                 pr_cont(" >\n");
148                 }
149         }
150         return msg->len;
151
152  eio:
153         retval = -EIO;
154         if (i2c_debug)
155                 pr_err(" ERR: %d\n", retval);
156         return retval;
157 }
158
159 static int i2c_readbytes(struct i2c_adapter *i2c_adap,
160                          const struct i2c_msg *msg, int joined)
161 {
162         struct cx23885_i2c *bus = i2c_adap->algo_data;
163         struct cx23885_dev *dev = bus->dev;
164         u32 ctrl, cnt;
165         int retval;
166
167
168         if (i2c_debug && !joined)
169                 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
170
171         /* Deal with i2c probe functions with zero payload */
172         if (msg->len == 0) {
173                 cx_write(bus->reg_addr, msg->addr << 25);
174                 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
175                 if (!i2c_wait_done(i2c_adap))
176                         return -EIO;
177                 if (!i2c_slave_did_ack(i2c_adap))
178                         return -ENXIO;
179
180
181                 dprintk(1, "%s() returns 0\n", __func__);
182                 return 0;
183         }
184
185         if (i2c_debug) {
186                 if (joined)
187                         dprintk(1, " R");
188                 else
189                         dprintk(1, " <R %02x", (msg->addr << 1) + 1);
190         }
191
192         for (cnt = 0; cnt < msg->len; cnt++) {
193
194                 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
195
196                 if (cnt < msg->len - 1)
197                         ctrl |= I2C_NOSTOP | I2C_EXTEND;
198
199                 cx_write(bus->reg_addr, msg->addr << 25);
200                 cx_write(bus->reg_ctrl, ctrl);
201
202                 if (!i2c_wait_done(i2c_adap))
203                         goto eio;
204                 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
205                 if (i2c_debug) {
206                         dprintk(1, " %02x", msg->buf[cnt]);
207                         if (!(ctrl & I2C_NOSTOP))
208                                 dprintk(1, " >\n");
209                 }
210         }
211         return msg->len;
212
213  eio:
214         retval = -EIO;
215         if (i2c_debug)
216                 pr_err(" ERR: %d\n", retval);
217         return retval;
218 }
219
220 static int i2c_xfer(struct i2c_adapter *i2c_adap,
221                     struct i2c_msg *msgs, int num)
222 {
223         int i, retval = 0;
224
225         dprintk(1, "%s(num = %d)\n", __func__, num);
226
227         for (i = 0 ; i < num; i++) {
228                 dprintk(1, "%s(num = %d) addr = 0x%02x  len = 0x%x\n",
229                         __func__, num, msgs[i].addr, msgs[i].len);
230                 if (msgs[i].flags & I2C_M_RD) {
231                         /* read */
232                         retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
233                 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
234                            msgs[i].addr == msgs[i + 1].addr) {
235                         /* write then read from same address */
236                         retval = i2c_sendbytes(i2c_adap, &msgs[i],
237                                                msgs[i + 1].len);
238                         if (retval < 0)
239                                 goto err;
240                         i++;
241                         retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
242                 } else {
243                         /* write */
244                         retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
245                 }
246                 if (retval < 0)
247                         goto err;
248         }
249         return num;
250
251  err:
252         return retval;
253 }
254
255 static u32 cx23885_functionality(struct i2c_adapter *adap)
256 {
257         return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
258 }
259
260 static const struct i2c_algorithm cx23885_i2c_algo_template = {
261         .master_xfer    = i2c_xfer,
262         .functionality  = cx23885_functionality,
263 };
264
265 /* ----------------------------------------------------------------------- */
266
267 static const struct i2c_adapter cx23885_i2c_adap_template = {
268         .name              = "cx23885",
269         .owner             = THIS_MODULE,
270         .algo              = &cx23885_i2c_algo_template,
271 };
272
273 static const struct i2c_client cx23885_i2c_client_template = {
274         .name   = "cx23885 internal",
275 };
276
277 static char *i2c_devs[128] = {
278         [0x10 >> 1] = "tda10048",
279         [0x12 >> 1] = "dib7000pc",
280         [0x1c >> 1] = "lgdt3303",
281         [0x80 >> 1] = "cs3308",
282         [0x82 >> 1] = "cs3308",
283         [0x86 >> 1] = "tda9887",
284         [0x32 >> 1] = "cx24227",
285         [0x88 >> 1] = "cx25837",
286         [0x84 >> 1] = "tda8295",
287         [0x98 >> 1] = "flatiron",
288         [0xa0 >> 1] = "eeprom",
289         [0xc0 >> 1] = "tuner/mt2131/tda8275",
290         [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
291         [0xc8 >> 1] = "tuner/xc3028L",
292 };
293
294 static void do_i2c_scan(char *name, struct i2c_client *c)
295 {
296         unsigned char buf;
297         int i, rc;
298
299         for (i = 0; i < 128; i++) {
300                 c->addr = i;
301                 rc = i2c_master_recv(c, &buf, 0);
302                 if (rc < 0)
303                         continue;
304                 pr_info("%s: i2c scan: found device @ 0x%04x  [%s]\n",
305                        name, i, i2c_devs[i] ? i2c_devs[i] : "???");
306         }
307 }
308
309 /* init + register i2c adapter */
310 int cx23885_i2c_register(struct cx23885_i2c *bus)
311 {
312         struct cx23885_dev *dev = bus->dev;
313
314         dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
315
316         bus->i2c_adap = cx23885_i2c_adap_template;
317         bus->i2c_client = cx23885_i2c_client_template;
318         bus->i2c_adap.dev.parent = &dev->pci->dev;
319
320         strlcpy(bus->i2c_adap.name, bus->dev->name,
321                 sizeof(bus->i2c_adap.name));
322
323         bus->i2c_adap.algo_data = bus;
324         i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
325         i2c_add_adapter(&bus->i2c_adap);
326
327         bus->i2c_client.adapter = &bus->i2c_adap;
328
329         if (0 == bus->i2c_rc) {
330                 dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
331                 if (i2c_scan) {
332                         pr_info("%s: scan bus %d:\n",
333                                         dev->name, bus->nr);
334                         do_i2c_scan(dev->name, &bus->i2c_client);
335                 }
336         } else
337                 pr_warn("%s: i2c bus %d register FAILED\n",
338                         dev->name, bus->nr);
339
340         /* Instantiate the IR receiver device, if present */
341         if (0 == bus->i2c_rc) {
342                 struct i2c_board_info info;
343                 const unsigned short addr_list[] = {
344                         0x6b, I2C_CLIENT_END
345                 };
346
347                 memset(&info, 0, sizeof(struct i2c_board_info));
348                 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
349                 /* Use quick read command for probe, some IR chips don't
350                  * support writes */
351                 i2c_new_probed_device(&bus->i2c_adap, &info, addr_list,
352                                       i2c_probe_func_quick_read);
353         }
354
355         return bus->i2c_rc;
356 }
357
358 int cx23885_i2c_unregister(struct cx23885_i2c *bus)
359 {
360         i2c_del_adapter(&bus->i2c_adap);
361         return 0;
362 }
363
364 void cx23885_av_clk(struct cx23885_dev *dev, int enable)
365 {
366         /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
367         char buffer[3];
368         struct i2c_msg msg;
369         dprintk(1, "%s(enabled = %d)\n", __func__, enable);
370
371         /* Register 0x144 */
372         buffer[0] = 0x01;
373         buffer[1] = 0x44;
374         if (enable == 1)
375                 buffer[2] = 0x05;
376         else
377                 buffer[2] = 0x00;
378
379         msg.addr = 0x44;
380         msg.flags = I2C_M_TEN;
381         msg.len = 3;
382         msg.buf = buffer;
383
384         i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
385 }