sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-i2c.c
blobb01d3ee56e770f8b4d8feca78faf8fa017ca9f88
1 /*
2 * tm6000-i2c.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
6 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 * - Fix SMBus Read Byte command
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
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.
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.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/usb.h>
26 #include <linux/i2c.h>
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"
35 /* ----------------------------------------------------------- */
37 static unsigned int i2c_debug;
38 module_param(i2c_debug, int, 0644);
39 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
41 #define i2c_dprintk(lvl, fmt, args...) if (i2c_debug >= lvl) do { \
42 printk(KERN_DEBUG "%s at %s: " fmt, \
43 dev->name, __func__, ##args); } while (0)
45 static int tm6000_i2c_send_regs(struct tm6000_core *dev, unsigned char addr,
46 __u8 reg, char *buf, int len)
48 int rc;
49 unsigned int i2c_packet_limit = 16;
51 if (dev->dev_type == TM6010)
52 i2c_packet_limit = 80;
54 if (!buf)
55 return -1;
57 if (len < 1 || len > i2c_packet_limit) {
58 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
59 len, i2c_packet_limit);
60 return -1;
63 /* capture mutex */
64 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
65 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
66 addr | reg << 8, 0, buf, len);
68 if (rc < 0) {
69 /* release mutex */
70 return rc;
73 /* release mutex */
74 return rc;
77 /* Generic read - doesn't work fine with 16bit registers */
78 static int tm6000_i2c_recv_regs(struct tm6000_core *dev, unsigned char addr,
79 __u8 reg, char *buf, int len)
81 int rc;
82 u8 b[2];
83 unsigned int i2c_packet_limit = 16;
85 if (dev->dev_type == TM6010)
86 i2c_packet_limit = 64;
88 if (!buf)
89 return -1;
91 if (len < 1 || len > i2c_packet_limit) {
92 printk(KERN_ERR "Incorrect length of i2c packet = %d, limit set to %d\n",
93 len, i2c_packet_limit);
94 return -1;
97 /* capture mutex */
98 if ((dev->caps.has_zl10353) && (dev->demod_addr << 1 == addr) && (reg % 2 == 0)) {
100 * Workaround an I2C bug when reading from zl10353
102 reg -= 1;
103 len += 1;
105 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
106 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, b, len);
108 *buf = b[1];
109 } else {
110 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
111 REQ_16_SET_GET_I2C_WR1_RDN, addr | reg << 8, 0, buf, len);
114 /* release mutex */
115 return rc;
119 * read from a 16bit register
120 * for example xc2028, xc3028 or xc3028L
122 static int tm6000_i2c_recv_regs16(struct tm6000_core *dev, unsigned char addr,
123 __u16 reg, char *buf, int len)
125 int rc;
126 unsigned char ureg;
128 if (!buf || len != 2)
129 return -1;
131 /* capture mutex */
132 if (dev->dev_type == TM6010) {
133 ureg = reg & 0xFF;
134 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
135 USB_RECIP_DEVICE, REQ_16_SET_GET_I2C_WR1_RDN,
136 addr | (reg & 0xFF00), 0, &ureg, 1);
138 if (rc < 0) {
139 /* release mutex */
140 return rc;
143 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
144 USB_RECIP_DEVICE, REQ_35_AFTEK_TUNER_READ,
145 reg, 0, buf, len);
146 } else {
147 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
148 USB_RECIP_DEVICE, REQ_14_SET_GET_I2C_WR2_RDN,
149 addr, reg, buf, len);
152 /* release mutex */
153 return rc;
156 static int tm6000_i2c_xfer(struct i2c_adapter *i2c_adap,
157 struct i2c_msg msgs[], int num)
159 struct tm6000_core *dev = i2c_adap->algo_data;
160 int addr, rc, i, byte;
162 if (num <= 0)
163 return 0;
164 for (i = 0; i < num; i++) {
165 addr = (msgs[i].addr << 1) & 0xff;
166 i2c_dprintk(2, "%s %s addr=0x%x len=%d:",
167 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
168 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
169 if (msgs[i].flags & I2C_M_RD) {
170 /* read request without preceding register selection */
172 * The TM6000 only supports a read transaction
173 * immediately after a 1 or 2 byte write to select
174 * a register. We cannot fulfil this request.
176 i2c_dprintk(2, " read without preceding write not supported");
177 rc = -EOPNOTSUPP;
178 goto err;
179 } else if (i + 1 < num && msgs[i].len <= 2 &&
180 (msgs[i + 1].flags & I2C_M_RD) &&
181 msgs[i].addr == msgs[i + 1].addr) {
182 /* 1 or 2 byte write followed by a read */
183 if (i2c_debug >= 2)
184 for (byte = 0; byte < msgs[i].len; byte++)
185 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
186 i2c_dprintk(2, "; joined to read %s len=%d:",
187 i == num - 2 ? "stop" : "nonstop",
188 msgs[i + 1].len);
190 if (msgs[i].len == 2) {
191 rc = tm6000_i2c_recv_regs16(dev, addr,
192 msgs[i].buf[0] << 8 | msgs[i].buf[1],
193 msgs[i + 1].buf, msgs[i + 1].len);
194 } else {
195 rc = tm6000_i2c_recv_regs(dev, addr, msgs[i].buf[0],
196 msgs[i + 1].buf, msgs[i + 1].len);
199 i++;
201 if (addr == dev->tuner_addr << 1) {
202 tm6000_set_reg(dev, REQ_50_SET_START, 0, 0);
203 tm6000_set_reg(dev, REQ_51_SET_STOP, 0, 0);
205 if (i2c_debug >= 2)
206 for (byte = 0; byte < msgs[i].len; byte++)
207 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
208 } else {
209 /* write bytes */
210 if (i2c_debug >= 2)
211 for (byte = 0; byte < msgs[i].len; byte++)
212 printk(KERN_CONT " %02x", msgs[i].buf[byte]);
213 rc = tm6000_i2c_send_regs(dev, addr, msgs[i].buf[0],
214 msgs[i].buf + 1, msgs[i].len - 1);
216 if (i2c_debug >= 2)
217 printk(KERN_CONT "\n");
218 if (rc < 0)
219 goto err;
222 return num;
223 err:
224 i2c_dprintk(2, " ERROR: %i\n", rc);
225 return rc;
228 static int tm6000_i2c_eeprom(struct tm6000_core *dev)
230 int i, rc;
231 unsigned char *p = dev->eedata;
232 unsigned char bytes[17];
234 dev->i2c_client.addr = 0xa0 >> 1;
235 dev->eedata_size = 0;
237 bytes[16] = '\0';
238 for (i = 0; i < sizeof(dev->eedata); ) {
239 *p = i;
240 rc = tm6000_i2c_recv_regs(dev, 0xa0, i, p, 1);
241 if (rc < 1) {
242 if (p == dev->eedata)
243 goto noeeprom;
244 else {
245 printk(KERN_WARNING
246 "%s: i2c eeprom read error (err=%d)\n",
247 dev->name, rc);
249 return -EINVAL;
251 dev->eedata_size++;
252 p++;
253 if (0 == (i % 16))
254 printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
255 printk(KERN_CONT " %02x", dev->eedata[i]);
256 if ((dev->eedata[i] >= ' ') && (dev->eedata[i] <= 'z'))
257 bytes[i%16] = dev->eedata[i];
258 else
259 bytes[i%16] = '.';
261 i++;
263 if (0 == (i % 16)) {
264 bytes[16] = '\0';
265 printk(KERN_CONT " %s\n", bytes);
268 if (0 != (i%16)) {
269 bytes[i%16] = '\0';
270 for (i %= 16; i < 16; i++)
271 printk(KERN_CONT " ");
272 printk(KERN_CONT " %s\n", bytes);
275 return 0;
277 noeeprom:
278 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
279 dev->name, rc);
280 return -EINVAL;
283 /* ----------------------------------------------------------- */
286 * functionality()
288 static u32 functionality(struct i2c_adapter *adap)
290 return I2C_FUNC_SMBUS_EMUL;
293 static const struct i2c_algorithm tm6000_algo = {
294 .master_xfer = tm6000_i2c_xfer,
295 .functionality = functionality,
298 /* ----------------------------------------------------------- */
301 * tm6000_i2c_register()
302 * register i2c bus
304 int tm6000_i2c_register(struct tm6000_core *dev)
306 int rc;
308 dev->i2c_adap.owner = THIS_MODULE;
309 dev->i2c_adap.algo = &tm6000_algo;
310 dev->i2c_adap.dev.parent = &dev->udev->dev;
311 strlcpy(dev->i2c_adap.name, dev->name, sizeof(dev->i2c_adap.name));
312 dev->i2c_adap.algo_data = dev;
313 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
314 rc = i2c_add_adapter(&dev->i2c_adap);
315 if (rc)
316 return rc;
318 dev->i2c_client.adapter = &dev->i2c_adap;
319 strlcpy(dev->i2c_client.name, "tm6000 internal", I2C_NAME_SIZE);
320 tm6000_i2c_eeprom(dev);
322 return 0;
326 * tm6000_i2c_unregister()
327 * unregister i2c_bus
329 int tm6000_i2c_unregister(struct tm6000_core *dev)
331 i2c_del_adapter(&dev->i2c_adap);
332 return 0;