V4L/DVB (12780): tm6000: Improve handling of SMBus Write Byte commands followed by...
[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_scan = 0;
40 module_param(i2c_scan, int, 0444);
41 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
42
43 static unsigned int i2c_debug = 0;
44 module_param(i2c_debug, int, 0644);
45 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
46
47 #define i2c_dprintk(lvl,fmt, args...) if (i2c_debug>=lvl) do{ \
48                         printk(KERN_DEBUG "%s at %s: " fmt, \
49                         dev->name, __FUNCTION__ , ##args); } while (0)
50
51
52 /* Returns 0 if address is found */
53 static int tm6000_i2c_scan(struct i2c_adapter *i2c_adap, int addr)
54 {
55         struct tm6000_core *dev = i2c_adap->algo_data;
56
57 #if 1
58         /* HACK: i2c scan is not working yet */
59         if (
60                 (dev->caps.has_tuner   && (addr==dev->tuner_addr)) ||
61                 (dev->caps.has_tda9874 && (addr==0xb0)) ||
62                 (dev->caps.has_zl10353 && (addr==0x1e)) ||
63                 (dev->caps.has_eeprom  && (addr==0xa0))
64            ) {
65                 printk("Hack: enabling device at addr 0x%02x\n",addr);
66                 return (1);
67         } else {
68                 return -ENODEV;
69         }
70 #else
71         int rc=-ENODEV;
72         char buf[1];
73
74         /* This sends addr + 1 byte with 0 */
75         rc = tm6000_read_write_usb (dev,
76                 USB_DIR_IN | USB_TYPE_VENDOR,
77                 REQ_16_SET_GET_I2CSEQ,
78                 addr, 0,
79                 buf, 0);
80         msleep(10);
81
82         if (rc<0) {
83                 if (i2c_debug>=2)
84                         printk("no device at addr 0x%02x\n",addr);
85         }
86
87         printk("Hack: check on addr 0x%02x returned %d\n",addr,rc);
88
89         return rc;
90 #endif
91 }
92
93 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
94                            struct i2c_msg msgs[], int num)
95 {
96         struct tm6000_core *dev = i2c_adap->algo_data;
97         int addr, rc, i, byte;
98         u8 prev_reg = 0;
99
100         if (num <= 0)
101                 return 0;
102         for (i = 0; i < num; i++) {
103                 addr = (msgs[i].addr << 1) &0xff;
104                 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
105                          (msgs[i].flags & I2C_M_RD) ? "read" : "write",
106                          i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
107                 if (!msgs[i].len) {
108                         /* Do I2C scan */
109                         rc=tm6000_i2c_scan(i2c_adap, addr);
110                 } else if (msgs[i].flags & I2C_M_RD) {
111                         /* Read bytes */
112         /* I2C is assumed to have always a subaddr at the first byte of the
113            message bus. Also, the first i2c value of the answer is returned
114            out of message data.
115          */
116                         /* SMBus Read Byte command */
117                         if(msgs[i].len == 1) {
118                                 // we use the previously used register to read from
119                                 rc = tm6000_read_write_usb (dev,
120                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121                                         REQ_16_SET_GET_I2CSEQ,
122                                         addr | prev_reg<<8, 0,
123                                         msgs[i].buf, msgs[i].len);
124                         }
125                         else {
126                                 rc = tm6000_read_write_usb (dev,
127                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
128                                         REQ_16_SET_GET_I2CSEQ,
129                                         addr|(*msgs[i].buf)<<8, 0,
130                                         msgs[i].buf, msgs[i].len);
131                         }
132                         if (i2c_debug>=2) {
133                                 for (byte = 0; byte < msgs[i].len; byte++) {
134                                         printk(" %02x", msgs[i].buf[byte]);
135                                 }
136                         }
137                 } else {
138                         /* write bytes */
139                         if (i2c_debug>=2) {
140                                 for (byte = 0; byte < msgs[i].len; byte++)
141                                         printk(" %02x", msgs[i].buf[byte]);
142                         }
143
144                         /* SMBus Write Byte command followed by a read command */
145                         if(msgs[i].len == 1 && i+1 < num && msgs[i+1].flags & I2C_M_RD
146                                             && msgs[i+1].addr == msgs[i].addr) {
147                                 prev_reg = msgs[i].buf[0];
148                                 continue;
149                         }
150
151                         rc = tm6000_read_write_usb (dev,
152                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
153                                 REQ_16_SET_GET_I2CSEQ,
154                                 addr|(*msgs[i].buf)<<8, 0,
155                                 msgs[i].buf+1, msgs[i].len-1);
156
157                         if(msgs[i].len >= 1) {
158                                 prev_reg = msgs[i].buf[0];
159                         }
160                         else {
161                                 prev_reg = 0;
162                         }
163                 }
164                 if (i2c_debug>=2)
165                         printk("\n");
166                 if (rc < 0)
167                         goto err;
168         }
169
170         return num;
171 err:
172         i2c_dprintk(2," ERROR: %i\n", rc);
173         return rc;
174 }
175
176
177 static int tm6000_i2c_eeprom( struct tm6000_core *dev,
178                               unsigned char *eedata, int len )
179 {
180         int i, rc;
181         unsigned char *p = eedata;
182         unsigned char bytes[17];
183
184         dev->i2c_client.addr = 0xa0 >> 1;
185
186 //006779:  OUT: 000006 ms 089867 ms c0 0e a0 00 00 00 01 00 <<<  00
187 //006780:  OUT: 000005 ms 089873 ms c0 10 a0 00 00 00 01 00 <<<  00
188 //006781:  OUT: 000108 ms 089878 ms 40 0e a0 00 00 00 01 00 >>>  99
189 //006782:  OUT: 000015 ms 089986 ms c0 0e a0 00 01 00 01 00 <<<  99
190 //006783:  OUT: 000004 ms 090001 ms c0 0e a0 00 10 00 01 00 <<<  99
191 //006784:  OUT: 000005 ms 090005 ms 40 10 a0 00 00 00 01 00 >>>  00
192 //006785:  OUT: 000308 ms 090010 ms 40 0e a0 00 00 00 01 00 >>>  00
193
194
195         for (i = 0; i < len; i++) {
196                 bytes[0x14+i] = 0;
197
198                 rc = i2c_master_recv(&dev->i2c_client, p, 1);
199                 if (rc<1) {
200                         if (p==eedata) {
201                                 printk (KERN_WARNING "%s doesn't have eeprom",
202                                         dev->name);
203                         } else {
204                                 printk(KERN_WARNING
205                                 "%s: i2c eeprom read error (err=%d)\n",
206                                 dev->name, rc);
207                         }
208                         return -1;
209                 }
210                 p++;
211                 if (0 == (i % 16))
212                         printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
213                 printk(" %02x", eedata[i]);
214                 if ((eedata[i]>=' ')&&(eedata[i]<='z')) {
215                         bytes[i%16]=eedata[i];
216                 } else {
217                         bytes[i%16]='.';
218                 }
219                 if (15 == (i % 16)) {
220                         bytes[i%16]='\0';
221                         printk("  %s\n", bytes);
222                 }
223         }
224         if ((i%16)!=15) {
225                 bytes[i%16]='\0';
226                 printk("  %s\n", bytes);
227         }
228         return 0;
229 }
230
231 /* ----------------------------------------------------------- */
232
233 /*
234  * algo_control()
235  */
236 static int algo_control(struct i2c_adapter *adapter,
237                         unsigned int cmd, unsigned long arg)
238 {
239         return 0;
240 }
241
242 /*
243  * functionality()
244  */
245 static u32 functionality(struct i2c_adapter *adap)
246 {
247         return I2C_FUNC_SMBUS_EMUL;
248 }
249
250 #ifndef I2C_PEC
251 static void inc_use(struct i2c_adapter *adap)
252 {
253         MOD_INC_USE_COUNT;
254 }
255
256 static void dec_use(struct i2c_adapter *adap)
257 {
258         MOD_DEC_USE_COUNT;
259 }
260 #endif
261
262 #define mass_write(addr, reg, data...)                                  \
263         { const static u8 _val[] = data;                                \
264         rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR,     \
265         REQ_16_SET_GET_I2CSEQ,(reg<<8)+addr, 0x00, (u8 *) _val,         \
266         ARRAY_SIZE(_val));                                              \
267         if (rc<0) {                                                     \
268                 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);  \
269                 return rc;                                              \
270         }                                                               \
271         msleep (10);                                                    \
272         }
273
274 int static init_zl10353 (struct tm6000_core *dev, u8 addr)
275 {
276         int rc=0;
277
278         mass_write (addr, 0x89, { 0x38 });
279         mass_write (addr, 0x8a, { 0x2d });
280         mass_write (addr, 0x50, { 0xff });
281         mass_write (addr, 0x51, { 0x00 , 0x00 , 0x50 });
282         mass_write (addr, 0x54, { 0x72 , 0x49 });
283         mass_write (addr, 0x87, { 0x0e , 0x0e });
284         mass_write (addr, 0x7b, { 0x04 });
285         mass_write (addr, 0x57, { 0xb8 , 0xc2 });
286         mass_write (addr, 0x59, { 0x00 , 0x02 , 0x00 , 0x00 , 0x01 });
287         mass_write (addr, 0x59, { 0x00 , 0x00 , 0xb3 , 0xd0 , 0x01 });
288         mass_write (addr, 0x58, { 0xc0 , 0x11 , 0xc5 , 0xc2 , 0xa4 , 0x01 });
289         mass_write (addr, 0x5e, { 0x01 });
290         mass_write (addr, 0x67, { 0x1c , 0x20 });
291         mass_write (addr, 0x75, { 0x33 });
292         mass_write (addr, 0x85, { 0x10 , 0x40 });
293         mass_write (addr, 0x8c, { 0x0b , 0x00 , 0x40 , 0x00 });
294
295         return 0;
296 }
297
298 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
299
300 static int tm6000_tuner_callback(void *ptr, int command, int arg)
301 {
302         int rc=0;
303         struct tm6000_core *dev = ptr;
304
305         if (dev->tuner_type!=TUNER_XC2028)
306                 return 0;
307
308         switch (command) {
309         case XC2028_RESET_CLK:
310                 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
311                                         0x02, arg);
312                 msleep(10);
313                 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
314                                         TM6000_GPIO_CLK, 0);
315                 if (rc<0)
316                         return rc;
317                 msleep(10);
318                 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
319                                         TM6000_GPIO_CLK, 1);
320                 break;
321         case XC2028_TUNER_RESET:
322                 /* Reset codes during load firmware */
323                 switch (arg) {
324                 case 0:
325                         tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
326                                                 TM6000_GPIO_1, 0x00);
327                         msleep(10);
328                         tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
329                                                 TM6000_GPIO_1, 0x01);
330                         break;
331                 case 1:
332                         tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
333                                                 0x02, 0x01);
334                         msleep(10);
335                         break;
336
337                 case 2:
338                         rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
339                                                 TM6000_GPIO_CLK, 0);
340                         if (rc<0)
341                                 return rc;
342                         msleep(10);
343                         rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
344                                                 TM6000_GPIO_CLK, 1);
345                         break;
346                 }
347         }
348         return (rc);
349 }
350
351 static int attach_inform(struct i2c_client *client)
352 {
353         struct tm6000_core *dev = client->adapter->algo_data;
354         struct tuner_setup tun_setup;
355         unsigned char eedata[11];
356
357         i2c_dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
358                 client->driver->driver.name, client->addr, client->name);
359
360         switch (client->addr<<1) {
361         case 0x1e:
362                 init_zl10353 (dev, client->addr);
363                 return 0;
364         case 0xa0:
365                 tm6000_i2c_eeprom(dev, eedata, sizeof(eedata)-1);
366                 eedata[sizeof(eedata)]='\0';
367
368                 printk("Board string ID = %s\n",eedata);
369                 return 0;
370         case 0xb0:
371                 request_module("tvaudio");
372                 return 0;
373         }
374
375         /* If tuner, initialize the tuner part */
376         if ( dev->tuner_addr != client->addr<<1 ) {
377                 return 0;
378         }
379
380         memset (&tun_setup, 0, sizeof(tun_setup));
381
382         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
383         tun_setup.type = dev->tuner_type;
384         tun_setup.addr = dev->tuner_addr>>1;
385         tun_setup.tuner_callback = tm6000_tuner_callback;
386
387         client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
388
389         return 0;
390 }
391
392 static struct i2c_algorithm tm6000_algo = {
393         .master_xfer   = tm6000_i2c_xfer,
394         .algo_control  = algo_control,
395         .functionality = functionality,
396 };
397
398 static struct i2c_adapter tm6000_adap_template = {
399 #ifdef I2C_PEC
400         .owner = THIS_MODULE,
401 #else
402         .inc_use = inc_use,
403         .dec_use = dec_use,
404 #endif
405         .class = I2C_CLASS_TV_ANALOG,
406         .name = "tm6000",
407         .id = I2C_HW_B_TM6000,
408         .algo = &tm6000_algo,
409         .client_register = attach_inform,
410 };
411
412 static struct i2c_client tm6000_client_template = {
413         .name = "tm6000 internal",
414 };
415
416 /* ----------------------------------------------------------- */
417
418 /*
419  * i2c_devs
420  * incomplete list of known devices
421  */
422 static char *i2c_devs[128] = {
423         [0xc2 >> 1] = "tuner (analog)",
424 };
425
426 /*
427  * do_i2c_scan()
428  * check i2c address range for devices
429  */
430 static void do_i2c_scan(char *name, struct i2c_client *c)
431 {
432         unsigned char buf;
433         int i, rc;
434
435         for (i = 0; i < 128; i++) {
436                 c->addr = i;
437                 rc = i2c_master_recv(c, &buf, 0);
438                 if (rc < 0)
439                         continue;
440                 printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", name,
441                        i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
442         }
443 }
444
445 /*
446  * tm6000_i2c_call_clients()
447  * send commands to all attached i2c devices
448  */
449 void tm6000_i2c_call_clients(struct tm6000_core *dev, unsigned int cmd, void *arg)
450 {
451         BUG_ON(NULL == dev->i2c_adap.algo_data);
452         i2c_clients_command(&dev->i2c_adap, cmd, arg);
453 }
454
455 /*
456  * tm6000_i2c_register()
457  * register i2c bus
458  */
459 int tm6000_i2c_register(struct tm6000_core *dev)
460 {
461         dev->i2c_adap = tm6000_adap_template;
462         dev->i2c_adap.dev.parent = &dev->udev->dev;
463         strcpy(dev->i2c_adap.name, dev->name);
464         dev->i2c_adap.algo_data = dev;
465         i2c_add_adapter(&dev->i2c_adap);
466
467         dev->i2c_client = tm6000_client_template;
468         dev->i2c_client.adapter = &dev->i2c_adap;
469
470         if (i2c_scan)
471                 do_i2c_scan(dev->name, &dev->i2c_client);
472
473         return 0;
474 }
475
476 /*
477  * tm6000_i2c_unregister()
478  * unregister i2c_bus
479  */
480 int tm6000_i2c_unregister(struct tm6000_core *dev)
481 {
482         i2c_del_adapter(&dev->i2c_adap);
483         return 0;
484 }