V4L/DVB (12814): tm6000: fix eeprom reading on tm6000
[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_eeprom  && (addr==0xa0))
63            ) {
64                 printk("Hack: enabling device at addr 0x%02x\n",addr);
65                 return (1);
66         } else {
67                 return -ENODEV;
68         }
69 #else
70         int rc=-ENODEV;
71         char buf[1];
72
73         /* This sends addr + 1 byte with 0 */
74         rc = tm6000_read_write_usb (dev,
75                 USB_DIR_IN | USB_TYPE_VENDOR,
76                 REQ_16_SET_GET_I2CSEQ,
77                 addr, 0,
78                 buf, 0);
79         msleep(10);
80
81         if (rc<0) {
82                 if (i2c_debug>=2)
83                         printk("no device at addr 0x%02x\n",addr);
84         }
85
86         printk("Hack: check on addr 0x%02x returned %d\n",addr,rc);
87
88         return rc;
89 #endif
90 }
91
92 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
93                            struct i2c_msg msgs[], int num)
94 {
95         struct tm6000_core *dev = i2c_adap->algo_data;
96         int addr, rc, i, byte;
97         u8 prev_reg = 0;
98
99         if (num <= 0)
100                 return 0;
101         for (i = 0; i < num; i++) {
102                 addr = (msgs[i].addr << 1) &0xff;
103                 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
104                          (msgs[i].flags & I2C_M_RD) ? "read" : "write",
105                          i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
106                 if (!msgs[i].len) {
107                         /* Do I2C scan */
108                         rc=tm6000_i2c_scan(i2c_adap, addr);
109                 } else if (msgs[i].flags & I2C_M_RD) {
110                         /* Read bytes */
111         /* I2C is assumed to have always a subaddr at the first byte of the
112            message bus. Also, the first i2c value of the answer is returned
113            out of message data.
114          */
115                         /* SMBus Read Byte command */
116                         rc = tm6000_read_write_usb (dev,
117                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
118                                 REQ_16_SET_GET_I2CSEQ,
119                                 addr | (prev_reg << 8), 0,
120                                 msgs[i].buf, msgs[i].len);
121                         if (i2c_debug>=2) {
122                                 for (byte = 0; byte < msgs[i].len; byte++) {
123                                         printk(" %02x", msgs[i].buf[byte]);
124                                 }
125                         }
126                 } else {
127                         /* write bytes */
128                         if (i2c_debug>=2) {
129                                 for (byte = 0; byte < msgs[i].len; byte++)
130                                         printk(" %02x", msgs[i].buf[byte]);
131                         }
132
133                         /* SMBus Write Byte command followed by a read command */
134                         if(msgs[i].len == 1 && i+1 < num && msgs[i+1].flags & I2C_M_RD
135                                             && msgs[i+1].addr == msgs[i].addr) {
136                                 prev_reg = msgs[i].buf[0];
137                                 continue;
138                         }
139
140                         rc = tm6000_read_write_usb (dev,
141                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
142                                 REQ_16_SET_GET_I2CSEQ,
143                                 addr|(*msgs[i].buf)<<8, 0,
144                                 msgs[i].buf+1, msgs[i].len-1);
145
146                         if(msgs[i].len >= 1) {
147                                 prev_reg = msgs[i].buf[0];
148                         }
149                         else {
150                                 prev_reg = 0;
151                         }
152                 }
153                 if (i2c_debug>=2)
154                         printk("\n");
155                 if (rc < 0)
156                         goto err;
157         }
158
159         return num;
160 err:
161         i2c_dprintk(2," ERROR: %i\n", rc);
162         return rc;
163 }
164
165
166 static int tm6000_i2c_eeprom(struct tm6000_core *dev,
167                              unsigned char *eedata, int len)
168 {
169         int i, rc;
170         unsigned char *p = eedata;
171         unsigned char bytes[17];
172
173         dev->i2c_client.addr = 0xa0 >> 1;
174
175         bytes[16] = '\0';
176         for (i = 0; i < len; ) {
177         *p = i;
178         rc = tm6000_read_write_usb (dev,
179                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
180                 REQ_16_SET_GET_I2CSEQ, 0xa0 | i<<8, 0, p, 1);
181                 if (rc < 1) {
182                         if (p == eedata)
183                                 goto noeeprom;
184                         else {
185                                 printk(KERN_WARNING
186                                 "%s: i2c eeprom read error (err=%d)\n",
187                                 dev->name, rc);
188                         }
189                         return -1;
190                 }
191                 p++;
192                 if (0 == (i % 16))
193                         printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
194                 printk(" %02x", eedata[i]);
195                 if ((eedata[i] >= ' ') && (eedata[i] <= 'z')) {
196                         bytes[i%16] = eedata[i];
197                 } else {
198                         bytes[i%16]='.';
199                 }
200
201                 i++;
202
203                 if (0 == (i % 16)) {
204                         bytes[16] = '\0';
205                         printk("  %s\n", bytes);
206                 }
207         }
208         if (0 != (i%16)) {
209                 bytes[i%16] = '\0';
210                 for (i %= 16; i < 16; i++)
211                         printk("   ");
212         }
213         printk("  %s\n", bytes);
214
215         return 0;
216
217 noeeprom:
218         printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
219                        dev->name, rc);
220         return rc;
221 }
222
223 /* ----------------------------------------------------------- */
224
225 /*
226  * algo_control()
227  */
228 static int algo_control(struct i2c_adapter *adapter,
229                         unsigned int cmd, unsigned long arg)
230 {
231         return 0;
232 }
233
234 /*
235  * functionality()
236  */
237 static u32 functionality(struct i2c_adapter *adap)
238 {
239         return I2C_FUNC_SMBUS_EMUL;
240 }
241
242 #ifndef I2C_PEC
243 static void inc_use(struct i2c_adapter *adap)
244 {
245         MOD_INC_USE_COUNT;
246 }
247
248 static void dec_use(struct i2c_adapter *adap)
249 {
250         MOD_DEC_USE_COUNT;
251 }
252 #endif
253
254 #define mass_write(addr, reg, data...)                                  \
255         { const static u8 _val[] = data;                                \
256         rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR,     \
257         REQ_16_SET_GET_I2CSEQ,(reg<<8)+addr, 0x00, (u8 *) _val,         \
258         ARRAY_SIZE(_val));                                              \
259         if (rc<0) {                                                     \
260                 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);  \
261                 return rc;                                              \
262         }                                                               \
263         msleep (10);                                                    \
264         }
265
266 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
267
268 static int tm6000_tuner_callback(void *ptr, int command, int arg)
269 {
270         int rc=0;
271         struct tm6000_core *dev = ptr;
272
273         if (dev->tuner_type!=TUNER_XC2028)
274                 return 0;
275
276         switch (command) {
277         case XC2028_RESET_CLK:
278                 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
279                                         0x02, arg);
280                 msleep(10);
281                 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
282                                         TM6000_GPIO_CLK, 0);
283                 if (rc<0)
284                         return rc;
285                 msleep(10);
286                 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
287                                         TM6000_GPIO_CLK, 1);
288                 break;
289         case XC2028_TUNER_RESET:
290                 /* Reset codes during load firmware */
291                 switch (arg) {
292                 case 0:
293                         tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
294                                         dev->tuner_reset_gpio, 0x00);
295                         msleep(130);
296                         tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
297                                         dev->tuner_reset_gpio, 0x01);
298                         msleep(130);
299                         break;
300                 case 1:
301                         tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
302                                                 0x02, 0x01);
303                         msleep(10);
304                         break;
305
306                 case 2:
307                         rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
308                                                 TM6000_GPIO_CLK, 0);
309                         if (rc<0)
310                                 return rc;
311                         msleep(100);
312                         rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
313                                                 TM6000_GPIO_CLK, 1);
314                         msleep(100);
315                         break;
316                 }
317         }
318         return (rc);
319 }
320
321 static int attach_inform(struct i2c_client *client)
322 {
323         struct tm6000_core *dev = client->adapter->algo_data;
324         struct tuner_setup tun_setup;
325
326         i2c_dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
327                 client->driver->driver.name, client->addr, client->name);
328
329         switch (client->addr<<1) {
330         case 0xb0:
331                 request_module("tvaudio");
332                 return 0;
333         }
334
335         /* If tuner, initialize the tuner part */
336         if ( dev->tuner_addr != client->addr<<1 ) {
337                 return 0;
338         }
339
340         memset (&tun_setup, 0, sizeof(tun_setup));
341
342         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
343         tun_setup.type = dev->tuner_type;
344         tun_setup.addr = dev->tuner_addr>>1;
345         tun_setup.tuner_callback = tm6000_tuner_callback;
346
347         client->driver->command (client,TUNER_SET_TYPE_ADDR, &tun_setup);
348
349         return 0;
350 }
351
352 static struct i2c_algorithm tm6000_algo = {
353         .master_xfer   = tm6000_i2c_xfer,
354         .algo_control  = algo_control,
355         .functionality = functionality,
356 };
357
358 static struct i2c_adapter tm6000_adap_template = {
359 #ifdef I2C_PEC
360         .owner = THIS_MODULE,
361 #else
362         .inc_use = inc_use,
363         .dec_use = dec_use,
364 #endif
365         .class = I2C_CLASS_TV_ANALOG,
366         .name = "tm6000",
367         .id = I2C_HW_B_TM6000,
368         .algo = &tm6000_algo,
369         .client_register = attach_inform,
370 };
371
372 static struct i2c_client tm6000_client_template = {
373         .name = "tm6000 internal",
374 };
375
376 /* ----------------------------------------------------------- */
377
378 /*
379  * i2c_devs
380  * incomplete list of known devices
381  */
382 static char *i2c_devs[128] = {
383         [0xc2 >> 1] = "tuner (analog)",
384 };
385
386 /*
387  * do_i2c_scan()
388  * check i2c address range for devices
389  */
390 static void do_i2c_scan(char *name, struct i2c_client *c)
391 {
392         unsigned char buf;
393         int i, rc;
394
395         for (i = 0; i < 128; i++) {
396                 c->addr = i;
397                 rc = i2c_master_recv(c, &buf, 0);
398                 if (rc < 0)
399                         continue;
400                 printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", name,
401                        i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
402         }
403 }
404
405 /*
406  * tm6000_i2c_call_clients()
407  * send commands to all attached i2c devices
408  */
409 void tm6000_i2c_call_clients(struct tm6000_core *dev, unsigned int cmd, void *arg)
410 {
411         BUG_ON(NULL == dev->i2c_adap.algo_data);
412         i2c_clients_command(&dev->i2c_adap, cmd, arg);
413 }
414
415 /*
416  * tm6000_i2c_register()
417  * register i2c bus
418  */
419 int tm6000_i2c_register(struct tm6000_core *dev)
420 {
421         unsigned char eedata[256];
422
423         dev->i2c_adap = tm6000_adap_template;
424         dev->i2c_adap.dev.parent = &dev->udev->dev;
425         strcpy(dev->i2c_adap.name, dev->name);
426         dev->i2c_adap.algo_data = dev;
427         i2c_add_adapter(&dev->i2c_adap);
428
429         dev->i2c_client = tm6000_client_template;
430         dev->i2c_client.adapter = &dev->i2c_adap;
431
432         if (i2c_scan)
433                 do_i2c_scan(dev->name, &dev->i2c_client);
434
435         tm6000_i2c_eeprom(dev, eedata, sizeof(eedata));
436
437         return 0;
438 }
439
440 /*
441  * tm6000_i2c_unregister()
442  * unregister i2c_bus
443  */
444 int tm6000_i2c_unregister(struct tm6000_core *dev)
445 {
446         i2c_del_adapter(&dev->i2c_adap);
447         return 0;
448 }