blob: 659b63febf8525d378d7c3d5ff751960b9b3119a [file] [log] [blame]
Mauro Carvalho Chehab75d1e3e2017-12-01 08:47:10 -05001// SPDX-License-Identifier: GPL-2.0
2// tm6000-i2c.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3//
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04004// Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehab75d1e3e2017-12-01 08:47:10 -05005//
6// Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7// - Fix SMBus Read Byte command
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03008
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/usb.h>
12#include <linux/i2c.h>
13
14#include "tm6000.h"
15#include "tm6000-regs.h"
16#include <media/v4l2-common.h>
17#include <media/tuner.h>
18#include "tuner-xc2028.h"
19
20
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030021/* ----------------------------------------------------------- */
22
Ruslan Pisarevd7fe4a62010-10-20 06:34:40 -030023static unsigned int i2c_debug;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030024module_param(i2c_debug, int, 0644);
25MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
26
Timofey Trofimov52e0a722010-05-29 13:52:46 -030027#define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030028 printk(KERN_DEBUG "%s at %s: " fmt, \
Curtis McEnroe0f063c62011-06-02 20:33:32 -040029 dev->name, __func__, ##args); } while (0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030030
Stefan Ringel4e115022010-02-22 14:35:05 -030031static int tm6000_i2c_send_regs(struct tm6000_core *dev, unsigned char addr,
32 __u8 reg, char *buf, int len)
33{
Dmitri Belimov20dead82010-04-27 22:32:43 -030034 int rc;
Dmitri Belimov20dead82010-04-27 22:32:43 -030035 unsigned int i2c_packet_limit = 16;
36
37 if (dev->dev_type == TM6010)
matthieu castet3874cd72011-12-16 14:15:07 -030038 i2c_packet_limit = 80;
Dmitri Belimov20dead82010-04-27 22:32:43 -030039
40 if (!buf)
41 return -1;
42
43 if (len < 1 || len > i2c_packet_limit) {
44 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
45 len, i2c_packet_limit);
46 return -1;
47 }
48
49 /* capture mutex */
50 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
51 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
52 addr | reg << 8, 0, buf, len);
53
54 if (rc < 0) {
55 /* release mutex */
56 return rc;
57 }
58
Dmitri Belimov20dead82010-04-27 22:32:43 -030059 /* release mutex */
60 return rc;
Stefan Ringel4e115022010-02-22 14:35:05 -030061}
62
63/* Generic read - doesn't work fine with 16bit registers */
64static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
65 __u8 reg, char *buf, int len)
66{
67 int rc;
Stefan Ringel02512fe2010-02-22 14:35:06 -030068 u8 b[2];
Dmitri Belimov20dead82010-04-27 22:32:43 -030069 unsigned int i2c_packet_limit = 16;
Stefan Ringel4e115022010-02-22 14:35:05 -030070
Dmitri Belimov20dead82010-04-27 22:32:43 -030071 if (dev->dev_type == TM6010)
72 i2c_packet_limit = 64;
73
74 if (!buf)
75 return -1;
76
77 if (len < 1 || len > i2c_packet_limit) {
78 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
79 len, i2c_packet_limit);
80 return -1;
81 }
82
83 /* capture mutex */
Stefan Ringel02512fe2010-02-22 14:35:06 -030084 if ((dev->caps.has_zl10353) && (dev->demod_addr << 1 == addr) && (reg % 2 == 0)) {
85 /*
86 * Workaround an I2C bug when reading from zl10353
87 */
88 reg -= 1;
89 len += 1;
90
91 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
92 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, b, len);
93
94 *buf = b[1];
95 } else {
96 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Stefan Ringel4e115022010-02-22 14:35:05 -030097 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
Stefan Ringel02512fe2010-02-22 14:35:06 -030098 }
Stefan Ringel4e115022010-02-22 14:35:05 -030099
Dmitri Belimov20dead82010-04-27 22:32:43 -0300100 /* release mutex */
Stefan Ringel4e115022010-02-22 14:35:05 -0300101 return rc;
102}
103
104/*
105 * read from a 16bit register
106 * for example xc2028, xc3028 or xc3028L
107 */
108static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
109 __u16 reg, char *buf, int len)
110{
Dmitri Belimov20dead82010-04-27 22:32:43 -0300111 int rc;
112 unsigned char ureg;
113
114 if (!buf || len != 2)
115 return -1;
116
117 /* capture mutex */
118 if (dev->dev_type == TM6010) {
119 ureg = reg & 0xFF;
120 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
121 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
122 addr | (reg & 0xFF00), 0, &ureg, 1);
123
124 if (rc < 0) {
125 /* release mutex */
126 return rc;
127 }
128
Dmitri Belimov20dead82010-04-27 22:32:43 -0300129 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
130 USB_RECIP_DEVICE, REQ_35_AFTEK_TUNER_READ,
131 reg, 0, buf, len);
132 } else {
133 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
134 USB_RECIP_DEVICE, REQ_14_SET_GET_I2C_WR2_RDN,
135 addr, reg, buf, len);
136 }
137
138 /* release mutex */
139 return rc;
Stefan Ringel4e115022010-02-22 14:35:05 -0300140}
141
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300142static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
143 struct i2c_msg msgs[], int num)
144{
145 struct tm6000_core *dev = i2c_adap->algo_data;
146 int addr, rc, i, byte;
147
148 if (num <= 0)
149 return 0;
150 for (i = 0; i < num; i++) {
Chris Pascoee30b9d62007-11-24 04:34:42 -0300151 addr = (msgs[i].addr << 1) & 0xff;
Timofey Trofimov52e0a722010-05-29 13:52:46 -0300152 i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300153 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
154 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -0300155 if (msgs[i].flags & I2C_M_RD) {
Chris Pascoee30b9d62007-11-24 04:34:42 -0300156 /* read request without preceding register selection */
157 /*
158 * The TM6000 only supports a read transaction
159 * immediately after a 1 or 2 byte write to select
160 * a register. We cannot fulfil this request.
161 */
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200162 i2c_dprintk(2, " read without preceding write not supported");
Chris Pascoee30b9d62007-11-24 04:34:42 -0300163 rc = -EOPNOTSUPP;
164 goto err;
165 } else if (i + 1 < num && msgs[i].len <= 2 &&
166 (msgs[i + 1].flags & I2C_M_RD) &&
167 msgs[i].addr == msgs[i + 1].addr) {
168 /* 1 or 2 byte write followed by a read */
169 if (i2c_debug >= 2)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300170 for (byte = 0; byte < msgs[i].len; byte++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300171 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
Chris Pascoee30b9d62007-11-24 04:34:42 -0300172 i2c_dprintk(2, "; joined to read %s len=%d:",
173 i == num - 2 ? "stop" : "nonstop",
174 msgs[i + 1].len);
Stefan Ringel4e115022010-02-22 14:35:05 -0300175
176 if (msgs[i].len == 2) {
177 rc = tm6000_i2c_recv_regs16(dev, addr,
178 msgs[i].buf[0] << 8 | msgs[i].buf[1],
179 msgs[i + 1].buf, msgs[i + 1].len);
180 } else {
181 rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
182 msgs[i + 1].buf, msgs[i + 1].len);
183 }
184
Chris Pascoee30b9d62007-11-24 04:34:42 -0300185 i++;
Stefan Ringel20cabed2010-02-05 19:57:04 -0300186
Stefan Ringel685b1222010-02-21 17:10:36 -0300187 if (addr == dev->tuner_addr << 1) {
Stefan Ringelf1434f42010-04-02 13:52:50 -0300188 tm6000_set_reg(dev, REQ_50_SET_START, 0, 0);
189 tm6000_set_reg(dev, REQ_51_SET_STOP, 0, 0);
Stefan Ringel20cabed2010-02-05 19:57:04 -0300190 }
Chris Pascoee30b9d62007-11-24 04:34:42 -0300191 if (i2c_debug >= 2)
192 for (byte = 0; byte < msgs[i].len; byte++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300193 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
Chris Pascoee30b9d62007-11-24 04:34:42 -0300194 } else {
195 /* write bytes */
196 if (i2c_debug >= 2)
197 for (byte = 0; byte < msgs[i].len; byte++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300198 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
Stefan Ringel4e115022010-02-22 14:35:05 -0300199 rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
Chris Pascoee30b9d62007-11-24 04:34:42 -0300200 msgs[i].buf + 1, msgs[i].len - 1);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300201 }
Chris Pascoee30b9d62007-11-24 04:34:42 -0300202 if (i2c_debug >= 2)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300203 printk(KERN_CONT "\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300204 if (rc < 0)
205 goto err;
206 }
207
208 return num;
209err:
Timofey Trofimov52e0a722010-05-29 13:52:46 -0300210 i2c_dprintk(2, " ERROR: %i\n", rc);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300211 return rc;
212}
213
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300214static int tm6000_i2c_eeprom(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300215{
216 int i, rc;
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300217 unsigned char *p = dev->eedata;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300218 unsigned char bytes[17];
219
220 dev->i2c_client.addr = 0xa0 >> 1;
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300221 dev->eedata_size = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300222
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300223 bytes[16] = '\0';
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300224 for (i = 0; i < sizeof(dev->eedata); ) {
225 *p = i;
226 rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300227 if (rc < 1) {
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300228 if (p == dev->eedata)
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300229 goto noeeprom;
230 else {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300231 printk(KERN_WARNING
232 "%s: i2c eeprom read error (err=%d)\n",
233 dev->name, rc);
234 }
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300235 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300236 }
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300237 dev->eedata_size++;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300238 p++;
239 if (0 == (i % 16))
240 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300241 printk(KERN_CONT " %02x", dev->eedata[i]);
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300242 if ((dev->eedata[i] >= ' ') && (dev->eedata[i] <= 'z'))
243 bytes[i%16] = dev->eedata[i];
Timofey Trofimov52e0a722010-05-29 13:52:46 -0300244 else
245 bytes[i%16] = '.';
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300246
247 i++;
248
249 if (0 == (i % 16)) {
250 bytes[16] = '\0';
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300251 printk(KERN_CONT " %s\n", bytes);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300252 }
253 }
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300254 if (0 != (i%16)) {
255 bytes[i%16] = '\0';
256 for (i %= 16; i < 16; i++)
Mauro Carvalho Chehab45dbf0d2011-09-23 09:26:22 -0300257 printk(KERN_CONT " ");
258 printk(KERN_CONT " %s\n", bytes);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300259 }
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300260
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300261 return 0;
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300262
263noeeprom:
264 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300265 dev->name, rc);
266 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300267}
268
269/* ----------------------------------------------------------- */
270
271/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300272 * functionality()
273 */
274static u32 functionality(struct i2c_adapter *adap)
275{
276 return I2C_FUNC_SMBUS_EMUL;
277}
278
Jean Delvare7bd444e2010-11-07 12:53:44 -0300279static const struct i2c_algorithm tm6000_algo = {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300280 .master_xfer = tm6000_i2c_xfer,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300281 .functionality = functionality,
282};
283
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300284/* ----------------------------------------------------------- */
285
286/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300287 * tm6000_i2c_register()
288 * register i2c bus
289 */
290int tm6000_i2c_register(struct tm6000_core *dev)
291{
Jean Delvare7bd444e2010-11-07 12:53:44 -0300292 int rc;
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300293
Jean Delvare7bd444e2010-11-07 12:53:44 -0300294 dev->i2c_adap.owner = THIS_MODULE;
295 dev->i2c_adap.algo = &tm6000_algo;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300296 dev->i2c_adap.dev.parent = &dev->udev->dev;
Jean Delvare7bd444e2010-11-07 12:53:44 -0300297 strlcpy(dev->i2c_adap.name, dev->name, sizeof(dev->i2c_adap.name));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300298 dev->i2c_adap.algo_data = dev;
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -0300299 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
Jean Delvare7bd444e2010-11-07 12:53:44 -0300300 rc = i2c_add_adapter(&dev->i2c_adap);
301 if (rc)
302 return rc;
303
304 dev->i2c_client.adapter = &dev->i2c_adap;
305 strlcpy(dev->i2c_client.name, "tm6000 internal", I2C_NAME_SIZE);
Mauro Carvalho Chehab792bc092011-04-20 18:43:24 -0300306 tm6000_i2c_eeprom(dev);
Mauro Carvalho Chehabd4e15bc2007-11-05 15:39:41 -0300307
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300308 return 0;
309}
310
311/*
312 * tm6000_i2c_unregister()
313 * unregister i2c_bus
314 */
315int tm6000_i2c_unregister(struct tm6000_core *dev)
316{
317 i2c_del_adapter(&dev->i2c_adap);
318 return 0;
319}