Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / 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 int rc;
51 unsigned int tsleep;
52 unsigned int i2c_packet_limit = 16;
53
54 if (dev->dev_type == TM6010)
55 i2c_packet_limit = 64;
56
57 if (!buf)
58 return -1;
59
60 if (len < 1 || len > i2c_packet_limit) {
61 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
62 len, i2c_packet_limit);
63 return -1;
64 }
65
66 /* capture mutex */
67 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
68 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
69 addr | reg << 8, 0, buf, len);
70
71 if (rc < 0) {
72 /* release mutex */
73 return rc;
74 }
75
76 /* Calculate delay time, 14000us for 64 bytes */
77 tsleep = ((len * 200) + 200 + 1000) / 1000;
78 msleep(tsleep);
79
80 /* release mutex */
81 return rc;
82 }
83
84 /* Generic read - doesn't work fine with 16bit registers */
85 static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
86 __u8 reg, char *buf, int len)
87 {
88 int rc;
89 u8 b[2];
90 unsigned int i2c_packet_limit = 16;
91
92 if (dev->dev_type == TM6010)
93 i2c_packet_limit = 64;
94
95 if (!buf)
96 return -1;
97
98 if (len < 1 || len > i2c_packet_limit) {
99 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
100 len, i2c_packet_limit);
101 return -1;
102 }
103
104 /* capture mutex */
105 if ((dev->caps.has_zl10353) && (dev->demod_addr << 1 == addr) && (reg % 2 == 0)) {
106 /*
107 * Workaround an I2C bug when reading from zl10353
108 */
109 reg -= 1;
110 len += 1;
111
112 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
113 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, b, len);
114
115 *buf = b[1];
116 } else {
117 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
118 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
119 }
120
121 /* release mutex */
122 return rc;
123 }
124
125 /*
126 * read from a 16bit register
127 * for example xc2028, xc3028 or xc3028L
128 */
129 static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
130 __u16 reg, char *buf, int len)
131 {
132 int rc;
133 unsigned char ureg;
134
135 if (!buf || len != 2)
136 return -1;
137
138 /* capture mutex */
139 if (dev->dev_type == TM6010) {
140 ureg = reg & 0xFF;
141 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
142 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
143 addr | (reg & 0xFF00), 0, &ureg, 1);
144
145 if (rc < 0) {
146 /* release mutex */
147 return rc;
148 }
149
150 msleep(1400 / 1000);
151 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
152 USB_RECIP_DEVICE, REQ_35_AFTEK_TUNER_READ,
153 reg, 0, buf, len);
154 } else {
155 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
156 USB_RECIP_DEVICE, REQ_14_SET_GET_I2C_WR2_RDN,
157 addr, reg, buf, len);
158 }
159
160 /* release mutex */
161 return rc;
162 }
163
164 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
165 struct i2c_msg msgs[], int num)
166 {
167 struct tm6000_core *dev = i2c_adap->algo_data;
168 int addr, rc, i, byte;
169
170 if (num <= 0)
171 return 0;
172 for (i = 0; i < num; i++) {
173 addr = (msgs[i].addr << 1) & 0xff;
174 i2c_dprintk(2,"%s %s addr=0x%x len=%d:",
175 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
176 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
177 if (msgs[i].flags & I2C_M_RD) {
178 /* read request without preceding register selection */
179 /*
180 * The TM6000 only supports a read transaction
181 * immediately after a 1 or 2 byte write to select
182 * a register. We cannot fulfil this request.
183 */
184 i2c_dprintk(2, " read without preceding write not"
185 " supported");
186 rc = -EOPNOTSUPP;
187 goto err;
188 } else if (i + 1 < num && msgs[i].len <= 2 &&
189 (msgs[i + 1].flags & I2C_M_RD) &&
190 msgs[i].addr == msgs[i + 1].addr) {
191 /* 1 or 2 byte write followed by a read */
192 if (i2c_debug >= 2)
193 for (byte = 0; byte < msgs[i].len; byte++)
194 printk(" %02x", msgs[i].buf[byte]);
195 i2c_dprintk(2, "; joined to read %s len=%d:",
196 i == num - 2 ? "stop" : "nonstop",
197 msgs[i + 1].len);
198
199 if (msgs[i].len == 2) {
200 rc = tm6000_i2c_recv_regs16(dev, addr,
201 msgs[i].buf[0] << 8 | msgs[i].buf[1],
202 msgs[i + 1].buf, msgs[i + 1].len);
203 } else {
204 rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
205 msgs[i + 1].buf, msgs[i + 1].len);
206 }
207
208 i++;
209
210 if (addr == dev->tuner_addr << 1) {
211 tm6000_set_reg(dev, REQ_50_SET_START, 0, 0);
212 tm6000_set_reg(dev, REQ_51_SET_STOP, 0, 0);
213 }
214 if (i2c_debug >= 2)
215 for (byte = 0; byte < msgs[i].len; byte++)
216 printk(" %02x", msgs[i].buf[byte]);
217 } else {
218 /* write bytes */
219 if (i2c_debug >= 2)
220 for (byte = 0; byte < msgs[i].len; byte++)
221 printk(" %02x", msgs[i].buf[byte]);
222 rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
223 msgs[i].buf + 1, msgs[i].len - 1);
224
225 if (addr == dev->tuner_addr << 1) {
226 tm6000_set_reg(dev, REQ_50_SET_START, 0, 0);
227 tm6000_set_reg(dev, REQ_51_SET_STOP, 0, 0);
228 }
229 }
230 if (i2c_debug >= 2)
231 printk("\n");
232 if (rc < 0)
233 goto err;
234 }
235
236 return num;
237 err:
238 i2c_dprintk(2," ERROR: %i\n", rc);
239 return rc;
240 }
241
242 static int tm6000_i2c_eeprom(struct tm6000_core *dev,
243 unsigned char *eedata, int len)
244 {
245 int i, rc;
246 unsigned char *p = eedata;
247 unsigned char bytes[17];
248
249 dev->i2c_client.addr = 0xa0 >> 1;
250
251 bytes[16] = '\0';
252 for (i = 0; i < len; ) {
253 *p = i;
254 rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
255 if (rc < 1) {
256 if (p == eedata)
257 goto noeeprom;
258 else {
259 printk(KERN_WARNING
260 "%s: i2c eeprom read error (err=%d)\n",
261 dev->name, rc);
262 }
263 return -1;
264 }
265 p++;
266 if (0 == (i % 16))
267 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
268 printk(" %02x", eedata[i]);
269 if ((eedata[i] >= ' ') && (eedata[i] <= 'z')) {
270 bytes[i%16] = eedata[i];
271 } else {
272 bytes[i%16]='.';
273 }
274
275 i++;
276
277 if (0 == (i % 16)) {
278 bytes[16] = '\0';
279 printk(" %s\n", bytes);
280 }
281 }
282 if (0 != (i%16)) {
283 bytes[i%16] = '\0';
284 for (i %= 16; i < 16; i++)
285 printk(" ");
286 }
287 printk(" %s\n", bytes);
288
289 return 0;
290
291 noeeprom:
292 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
293 dev->name, rc);
294 return rc;
295 }
296
297 /* ----------------------------------------------------------- */
298
299 /*
300 * functionality()
301 */
302 static u32 functionality(struct i2c_adapter *adap)
303 {
304 return I2C_FUNC_SMBUS_EMUL;
305 }
306
307 #define mass_write(addr, reg, data...) \
308 { const static u8 _val[] = data; \
309 rc=tm6000_read_write_usb(dev,USB_DIR_OUT | USB_TYPE_VENDOR, \
310 REQ_16_SET_GET_I2C_WR1_RDN,(reg<<8)+addr, 0x00, (u8 *) _val, \
311 ARRAY_SIZE(_val)); \
312 if (rc<0) { \
313 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
314 return rc; \
315 } \
316 msleep (10); \
317 }
318
319 static struct i2c_algorithm tm6000_algo = {
320 .master_xfer = tm6000_i2c_xfer,
321 .functionality = functionality,
322 };
323
324 static struct i2c_adapter tm6000_adap_template = {
325 .owner = THIS_MODULE,
326 .class = I2C_CLASS_TV_ANALOG | I2C_CLASS_TV_DIGITAL,
327 .name = "tm6000",
328 .id = I2C_HW_B_TM6000,
329 .algo = &tm6000_algo,
330 };
331
332 static struct i2c_client tm6000_client_template = {
333 .name = "tm6000 internal",
334 };
335
336 /* ----------------------------------------------------------- */
337
338 /*
339 * tm6000_i2c_register()
340 * register i2c bus
341 */
342 int tm6000_i2c_register(struct tm6000_core *dev)
343 {
344 unsigned char eedata[256];
345
346 dev->i2c_adap = tm6000_adap_template;
347 dev->i2c_adap.dev.parent = &dev->udev->dev;
348 strcpy(dev->i2c_adap.name, dev->name);
349 dev->i2c_adap.algo_data = dev;
350 i2c_add_adapter(&dev->i2c_adap);
351
352 dev->i2c_client = tm6000_client_template;
353 dev->i2c_client.adapter = &dev->i2c_adap;
354
355 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
356
357 tm6000_i2c_eeprom(dev, eedata, sizeof(eedata));
358
359 return 0;
360 }
361
362 /*
363 * tm6000_i2c_unregister()
364 * unregister i2c_bus
365 */
366 int tm6000_i2c_unregister(struct tm6000_core *dev)
367 {
368 i2c_del_adapter(&dev->i2c_adap);
369 return 0;
370 }