V4L/DVB: tm6000: add send and recv function
[safe/jmp/linux-2.6] / drivers / staging / tm6000 / tm6000-i2c.c
1 /*
2    tm6000-i2c.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3
4    Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6    Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7         - Fix SMBus Read Byte command
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation version 2
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/usb.h>
26 #include <linux/i2c.h>
27
28 #include "tm6000.h"
29 #include "tm6000-regs.h"
30 #include <media/v4l2-common.h>
31 #include <media/tuner.h>
32 #include "tuner-xc2028.h"
33
34
35 /*FIXME: Hack to avoid needing to patch i2c-id.h */
36 #define I2C_HW_B_TM6000 I2C_HW_B_EM28XX
37 /* ----------------------------------------------------------- */
38
39 static unsigned int i2c_debug = 0;
40 module_param(i2c_debug, int, 0644);
41 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
42
43 #define i2c_dprintk(lvl,fmt, args...) if (i2c_debug>=lvl) do{ \
44                         printk(KERN_DEBUG "%s at %s: " fmt, \
45                         dev->name, __FUNCTION__ , ##args); } while (0)
46
47 static int tm6000_i2c_send_regs(struct tm6000_core *dev, unsigned char addr,
48                                 __u8 reg, char *buf, int len)
49 {
50         return tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
51                 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
52 }
53
54 /* Generic read - doesn't work fine with 16bit registers */
55 static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
56                                 __u8 reg, char *buf, int len)
57 {
58         int rc;
59
60         rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
61                         REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
62
63         return rc;
64 }
65
66 /*
67  * read from a 16bit register
68  * for example xc2028, xc3028 or xc3028L
69  */
70 static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
71                                   __u16 reg, char *buf, int len)
72 {
73         return tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
74                 REQ_14_SET_GET_I2C_WR2_RDN, addr, reg, buf, len);
75 }
76
77 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
78                            struct i2c_msg msgs[], int num)
79 {
80         struct tm6000_core *dev = i2c_adap->algo_data;
81         int addr, rc, i, byte;
82
83         if (num <= 0)
84                 return 0;
85         for (i = 0; i < num; i++) {
86                 addr = (msgs[i].addr << 1) & 0xff;
87                 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
88                          (msgs[i].flags & I2C_M_RD) ? "read" : "write",
89                          i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
90                 if (msgs[i].flags & I2C_M_RD) {
91                         /* read request without preceding register selection */
92                         /*
93                          * The TM6000 only supports a read transaction
94                          * immediately after a 1 or 2 byte write to select
95                          * a register.  We cannot fulfil this request.
96                          */
97                         i2c_dprintk(2, " read without preceding write not"
98                                        " supported");
99                         rc = -EOPNOTSUPP;
100                         goto err;
101                 } else if (i + 1 < num && msgs[i].len <= 2 &&
102                            (msgs[i + 1].flags & I2C_M_RD) &&
103                            msgs[i].addr == msgs[i + 1].addr) {
104                         /* 1 or 2 byte write followed by a read */
105                         if (i2c_debug >= 2)
106                                 for (byte = 0; byte < msgs[i].len; byte++)
107                                         printk(" %02x", msgs[i].buf[byte]);
108                         i2c_dprintk(2, "; joined to read %s len=%d:",
109                                     i == num - 2 ? "stop" : "nonstop",
110                                     msgs[i + 1].len);
111
112                         if (msgs[i].len == 2) {
113                                 rc = tm6000_i2c_recv_regs16(dev, addr,
114                                         msgs[i].buf[0] << 8 | msgs[i].buf[1],
115                                         msgs[i + 1].buf, msgs[i + 1].len);
116                         } else {
117                                 rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
118                                         msgs[i + 1].buf, msgs[i + 1].len);
119                         }
120
121                         i++;
122
123                         if (addr == dev->tuner_addr << 1) {
124                                 tm6000_set_reg(dev, 0x32, 0,0);
125                                 tm6000_set_reg(dev, 0x33, 0,0);
126                         }
127                         if (i2c_debug >= 2)
128                                 for (byte = 0; byte < msgs[i].len; byte++)
129                                         printk(" %02x", msgs[i].buf[byte]);
130                 } else {
131                         /* write bytes */
132                         if (i2c_debug >= 2)
133                                 for (byte = 0; byte < msgs[i].len; byte++)
134                                         printk(" %02x", msgs[i].buf[byte]);
135                         rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
136                                 msgs[i].buf + 1, msgs[i].len - 1);
137
138                         if (addr == dev->tuner_addr  << 1) {
139                                 tm6000_set_reg(dev, 0x32, 0,0);
140                                 tm6000_set_reg(dev, 0x33, 0,0);
141                         }
142                 }
143                 if (i2c_debug >= 2)
144                         printk("\n");
145                 if (rc < 0)
146                         goto err;
147         }
148
149         return num;
150 err:
151         i2c_dprintk(2," ERROR: %i\n", rc);
152         return rc;
153 }
154
155 static int tm6000_i2c_eeprom(struct tm6000_core *dev,
156                              unsigned char *eedata, int len)
157 {
158         int i, rc;
159         unsigned char *p = eedata;
160         unsigned char bytes[17];
161
162         dev->i2c_client.addr = 0xa0 >> 1;
163
164         bytes[16] = '\0';
165         for (i = 0; i < len; ) {
166         *p = i;
167         rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
168                 if (rc < 1) {
169                         if (p == eedata)
170                                 goto noeeprom;
171                         else {
172                                 printk(KERN_WARNING
173                                 "%s: i2c eeprom read error (err=%d)\n",
174                                 dev->name, rc);
175                         }
176                         return -1;
177                 }
178                 p++;
179                 if (0 == (i % 16))
180                         printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
181                 printk(" %02x", eedata[i]);
182                 if ((eedata[i] >= ' ') && (eedata[i] <= 'z')) {
183                         bytes[i%16] = eedata[i];
184                 } else {
185                         bytes[i%16]='.';
186                 }
187
188                 i++;
189
190                 if (0 == (i % 16)) {
191                         bytes[16] = '\0';
192                         printk("  %s\n", bytes);
193                 }
194         }
195         if (0 != (i%16)) {
196                 bytes[i%16] = '\0';
197                 for (i %= 16; i < 16; i++)
198                         printk("   ");
199         }
200         printk("  %s\n", bytes);
201
202         return 0;
203
204 noeeprom:
205         printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
206                        dev->name, rc);
207         return rc;
208 }
209
210 /* ----------------------------------------------------------- */
211
212 /*
213  * functionality()
214  */
215 static u32 functionality(struct i2c_adapter *adap)
216 {
217         return I2C_FUNC_SMBUS_EMUL;
218 }
219
220 #define mass_write(addr, reg, data...)                                  \
221         { const static u8 _val[] = data;                                \
222         rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR,     \
223         REQ_16_SET_GET_I2C_WR1_RDN,(reg<<8)+addr, 0x00, (u8 *) _val,    \
224         ARRAY_SIZE(_val));                                              \
225         if (rc<0) {                                                     \
226                 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);  \
227                 return rc;                                              \
228         }                                                               \
229         msleep (10);                                                    \
230         }
231
232 static struct i2c_algorithm tm6000_algo = {
233         .master_xfer   = tm6000_i2c_xfer,
234         .functionality = functionality,
235 };
236
237 static struct i2c_adapter tm6000_adap_template = {
238         .owner = THIS_MODULE,
239         .class = I2C_CLASS_TV_ANALOG | I2C_CLASS_TV_DIGITAL,
240         .name = "tm6000",
241         .id = I2C_HW_B_TM6000,
242         .algo = &tm6000_algo,
243 };
244
245 static struct i2c_client tm6000_client_template = {
246         .name = "tm6000 internal",
247 };
248
249 /* ----------------------------------------------------------- */
250
251 /*
252  * tm6000_i2c_register()
253  * register i2c bus
254  */
255 int tm6000_i2c_register(struct tm6000_core *dev)
256 {
257         unsigned char eedata[256];
258
259         dev->i2c_adap = tm6000_adap_template;
260         dev->i2c_adap.dev.parent = &dev->udev->dev;
261         strcpy(dev->i2c_adap.name, dev->name);
262         dev->i2c_adap.algo_data = dev;
263         i2c_add_adapter(&dev->i2c_adap);
264
265         dev->i2c_client = tm6000_client_template;
266         dev->i2c_client.adapter = &dev->i2c_adap;
267
268         i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
269
270         tm6000_i2c_eeprom(dev, eedata, sizeof(eedata));
271
272         return 0;
273 }
274
275 /*
276  * tm6000_i2c_unregister()
277  * unregister i2c_bus
278  */
279 int tm6000_i2c_unregister(struct tm6000_core *dev)
280 {
281         i2c_del_adapter(&dev->i2c_adap);
282         return 0;
283 }