V4L/DVB: tm6000: fix i2c addr test
[safe/jmp/linux-2.6] / drivers / staging / tm6000 / tm6000-i2c.c
1 /*
2    tm6000-i2c.c - driver for TM5600/TM6000 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_xfer(struct i2c_adapter *i2c_adap,
48                            struct i2c_msg msgs[], int num)
49 {
50         struct tm6000_core *dev = i2c_adap->algo_data;
51         int addr, rc, i, byte;
52
53         if (num <= 0)
54                 return 0;
55         for (i = 0; i < num; i++) {
56                 addr = (msgs[i].addr << 1) & 0xff;
57                 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
58                          (msgs[i].flags & I2C_M_RD) ? "read" : "write",
59                          i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
60                 if (msgs[i].flags & I2C_M_RD) {
61                         /* read request without preceding register selection */
62                         /*
63                          * The TM6000 only supports a read transaction
64                          * immediately after a 1 or 2 byte write to select
65                          * a register.  We cannot fulfil this request.
66                          */
67                         i2c_dprintk(2, " read without preceding write not"
68                                        " supported");
69                         rc = -EOPNOTSUPP;
70                         goto err;
71                 } else if (i + 1 < num && msgs[i].len <= 2 &&
72                            (msgs[i + 1].flags & I2C_M_RD) &&
73                            msgs[i].addr == msgs[i + 1].addr) {
74                         /* 1 or 2 byte write followed by a read */
75                         if (i2c_debug >= 2)
76                                 for (byte = 0; byte < msgs[i].len; byte++)
77                                         printk(" %02x", msgs[i].buf[byte]);
78                         i2c_dprintk(2, "; joined to read %s len=%d:",
79                                     i == num - 2 ? "stop" : "nonstop",
80                                     msgs[i + 1].len);
81                         rc = tm6000_read_write_usb (dev,
82                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
83                                 msgs[i].len == 1 ? REQ_16_SET_GET_I2C_WR1_RDN
84                                                  : REQ_14_SET_GET_I2C_WR2_RDN,
85                                 addr | msgs[i].buf[0] << 8,
86                                 msgs[i].len == 1 ? 0 : msgs[i].buf[1],
87                                 msgs[i + 1].buf, msgs[i + 1].len);
88                         i++;
89
90                         if (addr == dev->tuner_addr << 1) {
91                                 tm6000_set_reg(dev, 0x32, 0,0);
92                                 tm6000_set_reg(dev, 0x33, 0,0);
93                         }
94                         if (i2c_debug >= 2)
95                                 for (byte = 0; byte < msgs[i].len; byte++)
96                                         printk(" %02x", msgs[i].buf[byte]);
97                 } else {
98                         /* write bytes */
99                         if (i2c_debug >= 2)
100                                 for (byte = 0; byte < msgs[i].len; byte++)
101                                         printk(" %02x", msgs[i].buf[byte]);
102                         rc = tm6000_read_write_usb(dev,
103                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
104                                 REQ_16_SET_GET_I2C_WR1_RDN,
105                                 addr | msgs[i].buf[0] << 8, 0,
106                                 msgs[i].buf + 1, msgs[i].len - 1);
107
108                         if (addr == dev->tuner_addr  << 1) {
109                                 tm6000_set_reg(dev, 0x32, 0,0);
110                                 tm6000_set_reg(dev, 0x33, 0,0);
111                         }
112                 }
113                 if (i2c_debug >= 2)
114                         printk("\n");
115                 if (rc < 0)
116                         goto err;
117         }
118
119         return num;
120 err:
121         i2c_dprintk(2," ERROR: %i\n", rc);
122         return rc;
123 }
124
125 static int tm6000_i2c_eeprom(struct tm6000_core *dev,
126                              unsigned char *eedata, int len)
127 {
128         int i, rc;
129         unsigned char *p = eedata;
130         unsigned char bytes[17];
131
132         dev->i2c_client.addr = 0xa0 >> 1;
133
134         bytes[16] = '\0';
135         for (i = 0; i < len; ) {
136         *p = i;
137         rc = tm6000_read_write_usb (dev,
138                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
139                 REQ_16_SET_GET_I2C_WR1_RDN, 0xa0 | i<<8, 0, p, 1);
140                 if (rc < 1) {
141                         if (p == eedata)
142                                 goto noeeprom;
143                         else {
144                                 printk(KERN_WARNING
145                                 "%s: i2c eeprom read error (err=%d)\n",
146                                 dev->name, rc);
147                         }
148                         return -1;
149                 }
150                 p++;
151                 if (0 == (i % 16))
152                         printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
153                 printk(" %02x", eedata[i]);
154                 if ((eedata[i] >= ' ') && (eedata[i] <= 'z')) {
155                         bytes[i%16] = eedata[i];
156                 } else {
157                         bytes[i%16]='.';
158                 }
159
160                 i++;
161
162                 if (0 == (i % 16)) {
163                         bytes[16] = '\0';
164                         printk("  %s\n", bytes);
165                 }
166         }
167         if (0 != (i%16)) {
168                 bytes[i%16] = '\0';
169                 for (i %= 16; i < 16; i++)
170                         printk("   ");
171         }
172         printk("  %s\n", bytes);
173
174         return 0;
175
176 noeeprom:
177         printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
178                        dev->name, rc);
179         return rc;
180 }
181
182 /* ----------------------------------------------------------- */
183
184 /*
185  * functionality()
186  */
187 static u32 functionality(struct i2c_adapter *adap)
188 {
189         return I2C_FUNC_SMBUS_EMUL;
190 }
191
192 #define mass_write(addr, reg, data...)                                  \
193         { const static u8 _val[] = data;                                \
194         rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR,     \
195         REQ_16_SET_GET_I2C_WR1_RDN,(reg<<8)+addr, 0x00, (u8 *) _val,    \
196         ARRAY_SIZE(_val));                                              \
197         if (rc<0) {                                                     \
198                 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);  \
199                 return rc;                                              \
200         }                                                               \
201         msleep (10);                                                    \
202         }
203
204 static struct i2c_algorithm tm6000_algo = {
205         .master_xfer   = tm6000_i2c_xfer,
206         .functionality = functionality,
207 };
208
209 static struct i2c_adapter tm6000_adap_template = {
210         .owner = THIS_MODULE,
211         .class = I2C_CLASS_TV_ANALOG | I2C_CLASS_TV_DIGITAL,
212         .name = "tm6000",
213         .id = I2C_HW_B_TM6000,
214         .algo = &tm6000_algo,
215 };
216
217 static struct i2c_client tm6000_client_template = {
218         .name = "tm6000 internal",
219 };
220
221 /* ----------------------------------------------------------- */
222
223 /*
224  * tm6000_i2c_register()
225  * register i2c bus
226  */
227 int tm6000_i2c_register(struct tm6000_core *dev)
228 {
229         unsigned char eedata[256];
230
231         dev->i2c_adap = tm6000_adap_template;
232         dev->i2c_adap.dev.parent = &dev->udev->dev;
233         strcpy(dev->i2c_adap.name, dev->name);
234         dev->i2c_adap.algo_data = dev;
235         i2c_add_adapter(&dev->i2c_adap);
236
237         dev->i2c_client = tm6000_client_template;
238         dev->i2c_client.adapter = &dev->i2c_adap;
239
240         i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
241
242         tm6000_i2c_eeprom(dev, eedata, sizeof(eedata));
243
244         return 0;
245 }
246
247 /*
248  * tm6000_i2c_unregister()
249  * unregister i2c_bus
250  */
251 int tm6000_i2c_unregister(struct tm6000_core *dev)
252 {
253         i2c_del_adapter(&dev->i2c_adap);
254         return 0;
255 }