blob: cbcc1472f1c75d477c4c965f4bdecbadc2e25192 [file] [log] [blame]
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001/*
Ruslan Pisarevd00586452010-10-20 06:35:54 -03002 * 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.
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030017 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/usb.h>
22#include <linux/i2c.h>
23
24#include "tm6000.h"
25#include "tm6000-regs.h"
26#include <media/v4l2-common.h>
27#include <media/tuner.h>
28#include "tuner-xc2028.h"
29
30
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030031/* ----------------------------------------------------------- */
32
Ruslan Pisarevd7fe4a62010-10-20 06:34:40 -030033static unsigned int i2c_debug;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030034module_param(i2c_debug, int, 0644);
35MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
36
Timofey Trofimov52e0a722010-05-29 13:52:46 -030037#define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030038 printk(KERN_DEBUG "%s at %s: " fmt, \
Curtis McEnroe0f063c62011-06-02 20:33:32 -040039 dev->name, __func__, ##args); } while (0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030040
Stefan Ringel4e115022010-02-22 14:35:05 -030041static int tm6000_i2c_send_regs(struct tm6000_core *dev, unsigned char addr,
42 __u8 reg, char *buf, int len)
43{
Dmitri Belimov20dead82010-04-27 22:32:43 -030044 int rc;
Dmitri Belimov20dead82010-04-27 22:32:43 -030045 unsigned int i2c_packet_limit = 16;
46
47 if (dev->dev_type == TM6010)
matthieu castet3874cd72011-12-16 14:15:07 -030048 i2c_packet_limit = 80;
Dmitri Belimov20dead82010-04-27 22:32:43 -030049
50 if (!buf)
51 return -1;
52
53 if (len < 1 || len > i2c_packet_limit) {
54 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
55 len, i2c_packet_limit);
56 return -1;
57 }
58
59 /* capture mutex */
60 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
61 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
62 addr | reg << 8, 0, buf, len);
63
64 if (rc < 0) {
65 /* release mutex */
66 return rc;
67 }
68
Dmitri Belimov20dead82010-04-27 22:32:43 -030069 /* release mutex */
70 return rc;
Stefan Ringel4e115022010-02-22 14:35:05 -030071}
72
73/* Generic read - doesn't work fine with 16bit registers */
74static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
75 __u8 reg, char *buf, int len)
76{
77 int rc;
Stefan Ringel02512fe2010-02-22 14:35:06 -030078 u8 b[2];
Dmitri Belimov20dead82010-04-27 22:32:43 -030079 unsigned int i2c_packet_limit = 16;
Stefan Ringel4e115022010-02-22 14:35:05 -030080
Dmitri Belimov20dead82010-04-27 22:32:43 -030081 if (dev->dev_type == TM6010)
82 i2c_packet_limit = 64;
83
84 if (!buf)
85 return -1;
86
87 if (len < 1 || len > i2c_packet_limit) {
88 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
89 len, i2c_packet_limit);
90 return -1;
91 }
92
93 /* capture mutex */
Stefan Ringel02512fe2010-02-22 14:35:06 -030094 if ((dev->caps.has_zl10353) && (dev->demod_addr << 1 == addr) && (reg % 2 == 0)) {
95 /*
96 * Workaround an I2C bug when reading from zl10353
97 */
98 reg -= 1;
99 len += 1;
100
101 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
102 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, b, len);
103
104 *buf = b[1];
105 } else {
106 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Stefan Ringel4e115022010-02-22 14:35:05 -0300107 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
Stefan Ringel02512fe2010-02-22 14:35:06 -0300108 }
Stefan Ringel4e115022010-02-22 14:35:05 -0300109
Dmitri Belimov20dead82010-04-27 22:32:43 -0300110 /* release mutex */
Stefan Ringel4e115022010-02-22 14:35:05 -0300111 return rc;
112}
113
114/*
115 * read from a 16bit register
116 * for example xc2028, xc3028 or xc3028L
117 */
118static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
119 __u16 reg, char *buf, int len)
120{
Dmitri Belimov20dead82010-04-27 22:32:43 -0300121 int rc;
122 unsigned char ureg;
123
124 if (!buf || len != 2)
125 return -1;
126
127 /* capture mutex */
128 if (dev->dev_type == TM6010) {
129 ureg = reg & 0xFF;
130 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
131 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
132 addr | (reg & 0xFF00), 0, &ureg, 1);
133
134 if (rc < 0) {
135 /* release mutex */
136 return rc;
137 }
138
Dmitri Belimov20dead82010-04-27 22:32:43 -0300139 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
140 USB_RECIP_DEVICE, REQ_35_AFTEK_TUNER_READ,
141 reg, 0, buf, len);
142 } else {
143 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
144 USB_RECIP_DEVICE, REQ_14_SET_GET_I2C_WR2_RDN,
145 addr, reg, buf, len);
146 }
147
148 /* release mutex */
149 return rc;
Stefan Ringel4e115022010-02-22 14:35:05 -0300150}
151
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300152static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
153 struct i2c_msg msgs[], int num)
154{
155 struct tm6000_core *dev = i2c_adap->algo_data;
156 int addr, rc, i, byte;
157
158 if (num <= 0)
159 return 0;
160 for (i = 0; i < num; i++) {
Chris Pascoee30b9d62007-11-24 04:34:42 -0300161 addr = (msgs[i].addr << 1) & 0xff;
Timofey Trofimov52e0a722010-05-29 13:52:46 -0300162 i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300163 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
164 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -0300165 if (msgs[i].flags & I2C_M_RD) {
Chris Pascoee30b9d62007-11-24 04:34:42 -0300166 /* read request without preceding register selection */
167 /*
168 * The TM6000 only supports a read transaction
169 * immediately after a 1 or 2 byte write to select
170 * a register. We cannot fulfil this request.
171 */
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200172 i2c_dprintk(2, " read without preceding write not supported");
Chris Pascoee30b9d62007-11-24 04:34:42 -0300173 rc = -EOPNOTSUPP;
174 goto err;
175 } else if (i + 1 < num && msgs[i].len <= 2 &&
176 (msgs[i + 1].flags & I2C_M_RD) &&
177 msgs[i].addr == msgs[i + 1].addr) {
178 /* 1 or 2 byte write followed by a read */
179 if (i2c_debug >= 2)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300180 for (byte = 0; byte < msgs[i].len; byte++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300181 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
Chris Pascoee30b9d62007-11-24 04:34:42 -0300182 i2c_dprintk(2, "; joined to read %s len=%d:",
183 i == num - 2 ? "stop" : "nonstop",
184 msgs[i + 1].len);
Stefan Ringel4e115022010-02-22 14:35:05 -0300185
186 if (msgs[i].len == 2) {
187 rc = tm6000_i2c_recv_regs16(dev, addr,
188 msgs[i].buf[0] << 8 | msgs[i].buf[1],
189 msgs[i + 1].buf, msgs[i + 1].len);
190 } else {
191 rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
192 msgs[i + 1].buf, msgs[i + 1].len);
193 }
194
Chris Pascoee30b9d62007-11-24 04:34:42 -0300195 i++;
Stefan Ringel20cabed2010-02-05 19:57:04 -0300196
Stefan Ringel685b1222010-02-21 17:10:36 -0300197 if (addr == dev->tuner_addr << 1) {
Stefan Ringelf1434f42010-04-02 13:52:50 -0300198 tm6000_set_reg(dev, REQ_50_SET_START, 0, 0);
199 tm6000_set_reg(dev, REQ_51_SET_STOP, 0, 0);
Stefan Ringel20cabed2010-02-05 19:57:04 -0300200 }
Chris Pascoee30b9d62007-11-24 04:34:42 -0300201 if (i2c_debug >= 2)
202 for (byte = 0; byte < msgs[i].len; byte++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300203 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
Chris Pascoee30b9d62007-11-24 04:34:42 -0300204 } else {
205 /* write bytes */
206 if (i2c_debug >= 2)
207 for (byte = 0; byte < msgs[i].len; byte++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300208 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
Stefan Ringel4e115022010-02-22 14:35:05 -0300209 rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
Chris Pascoee30b9d62007-11-24 04:34:42 -0300210 msgs[i].buf + 1, msgs[i].len - 1);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300211 }
Chris Pascoee30b9d62007-11-24 04:34:42 -0300212 if (i2c_debug >= 2)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300213 printk(KERN_CONT "\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300214 if (rc < 0)
215 goto err;
216 }
217
218 return num;
219err:
Timofey Trofimov52e0a722010-05-29 13:52:46 -0300220 i2c_dprintk(2, " ERROR: %i\n", rc);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300221 return rc;
222}
223
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300224static int tm6000_i2c_eeprom(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300225{
226 int i, rc;
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300227 unsigned char *p = dev->eedata;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300228 unsigned char bytes[17];
229
230 dev->i2c_client.addr = 0xa0 >> 1;
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300231 dev->eedata_size = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300232
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300233 bytes[16] = '\0';
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300234 for (i = 0; i < sizeof(dev->eedata); ) {
235 *p = i;
236 rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300237 if (rc < 1) {
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300238 if (p == dev->eedata)
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300239 goto noeeprom;
240 else {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300241 printk(KERN_WARNING
242 "%s: i2c eeprom read error (err=%d)\n",
243 dev->name, rc);
244 }
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300245 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300246 }
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300247 dev->eedata_size++;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300248 p++;
249 if (0 == (i % 16))
250 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300251 printk(KERN_CONT " %02x", dev->eedata[i]);
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300252 if ((dev->eedata[i] >= ' ') && (dev->eedata[i] <= 'z'))
253 bytes[i%16] = dev->eedata[i];
Timofey Trofimov52e0a722010-05-29 13:52:46 -0300254 else
255 bytes[i%16] = '.';
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300256
257 i++;
258
259 if (0 == (i % 16)) {
260 bytes[16] = '\0';
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300261 printk(KERN_CONT " %s\n", bytes);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300262 }
263 }
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300264 if (0 != (i%16)) {
265 bytes[i%16] = '\0';
266 for (i %= 16; i < 16; i++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300267 printk(KERN_CONT " ");
268 printk(KERN_CONT " %s\n", bytes);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300269 }
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300270
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300271 return 0;
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300272
273noeeprom:
274 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300275 dev->name, rc);
276 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300277}
278
279/* ----------------------------------------------------------- */
280
281/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300282 * functionality()
283 */
284static u32 functionality(struct i2c_adapter *adap)
285{
286 return I2C_FUNC_SMBUS_EMUL;
287}
288
Jean Delvare7bd444e2010-11-07 12:53:44 -0300289static const struct i2c_algorithm tm6000_algo = {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300290 .master_xfer = tm6000_i2c_xfer,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300291 .functionality = functionality,
292};
293
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300294/* ----------------------------------------------------------- */
295
296/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300297 * tm6000_i2c_register()
298 * register i2c bus
299 */
300int tm6000_i2c_register(struct tm6000_core *dev)
301{
Jean Delvare7bd444e2010-11-07 12:53:44 -0300302 int rc;
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300303
Jean Delvare7bd444e2010-11-07 12:53:44 -0300304 dev->i2c_adap.owner = THIS_MODULE;
305 dev->i2c_adap.algo = &tm6000_algo;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300306 dev->i2c_adap.dev.parent = &dev->udev->dev;
Jean Delvare7bd444e2010-11-07 12:53:44 -0300307 strlcpy(dev->i2c_adap.name, dev->name, sizeof(dev->i2c_adap.name));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300308 dev->i2c_adap.algo_data = dev;
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -0300309 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
Jean Delvare7bd444e2010-11-07 12:53:44 -0300310 rc = i2c_add_adapter(&dev->i2c_adap);
311 if (rc)
312 return rc;
313
314 dev->i2c_client.adapter = &dev->i2c_adap;
315 strlcpy(dev->i2c_client.name, "tm6000 internal", I2C_NAME_SIZE);
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300316 tm6000_i2c_eeprom(dev);
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300317
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300318 return 0;
319}
320
321/*
322 * tm6000_i2c_unregister()
323 * unregister i2c_bus
324 */
325int tm6000_i2c_unregister(struct tm6000_core *dev)
326{
327 i2c_del_adapter(&dev->i2c_adap);
328 return 0;
329}