blob: 925f3a04e53c40daf32e28970a9e7df22397f81c [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
2 cx231xx-i2c.c - driver for Conexant Cx23100/101/102 USB video capture devices
3
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
Sri Deevib9255172009-03-04 17:49:01 -03005 Based on em28xx driver
6 Based on Cx23885 driver
Sri Deevie0d3baf2009-03-03 14:37:50 -03007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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#include <media/v4l2-common.h>
28#include <media/tuner.h>
29
30#include "cx231xx.h"
31
Sri Deevie0d3baf2009-03-03 14:37:50 -030032/* ----------------------------------------------------------- */
33
34static unsigned int i2c_scan;
35module_param(i2c_scan, int, 0444);
36MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
37
38static unsigned int i2c_debug;
39module_param(i2c_debug, int, 0644);
40MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
41
Sri Deevie0d3baf2009-03-03 14:37:50 -030042#define dprintk1(lvl, fmt, args...) \
43do { \
44 if (i2c_debug >= lvl) { \
Sri Deevi6e4f5742009-03-10 21:16:26 -030045 printk(fmt, ##args); \
46 } \
Sri Deevie0d3baf2009-03-03 14:37:50 -030047} while (0)
48
49#define dprintk2(lvl, fmt, args...) \
50do { \
51 if (i2c_debug >= lvl) { \
52 printk(KERN_DEBUG "%s at %s: " fmt, \
53 dev->name, __func__ , ##args); \
54 } \
55} while (0)
56
Mauro Carvalho Chehab3f25ffa2011-01-30 09:03:24 -030057static inline bool is_tuner(struct cx231xx *dev, struct cx231xx_i2c *bus,
58 const struct i2c_msg *msg, int tuner_type)
59{
60 if (bus->nr != dev->board.tuner_i2c_master)
61 return false;
62
63 if (msg->addr != dev->board.tuner_addr)
64 return false;
65
66 if (dev->tuner_type != tuner_type)
67 return false;
68
69 return true;
70}
71
Sri Deevie0d3baf2009-03-03 14:37:50 -030072/*
73 * cx231xx_i2c_send_bytes()
74 */
75int cx231xx_i2c_send_bytes(struct i2c_adapter *i2c_adap,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030076 const struct i2c_msg *msg)
Sri Deevie0d3baf2009-03-03 14:37:50 -030077{
78 struct cx231xx_i2c *bus = i2c_adap->algo_data;
79 struct cx231xx *dev = bus->dev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030080 struct cx231xx_i2c_xfer_data req_data;
81 int status = 0;
82 u16 size = 0;
83 u8 loop = 0;
84 u8 saddr_len = 1;
85 u8 *buf_ptr = NULL;
86 u16 saddr = 0;
87 u8 need_gpio = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -030088
Mauro Carvalho Chehab3f25ffa2011-01-30 09:03:24 -030089 if (is_tuner(dev, bus, msg, TUNER_XC5000)) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030090 size = msg->len;
Sri Deevie0d3baf2009-03-03 14:37:50 -030091
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030092 if (size == 2) { /* register write sub addr */
Sri Deevi6e4f5742009-03-10 21:16:26 -030093 /* Just writing sub address will cause problem
94 * to XC5000. So ignore the request */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030095 return 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030096 } else if (size == 4) { /* register write with sub addr */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030097 if (msg->len >= 2)
98 saddr = msg->buf[0] << 8 | msg->buf[1];
99 else if (msg->len == 1)
100 saddr = msg->buf[0];
Sri Deevie0d3baf2009-03-03 14:37:50 -0300101
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300102 switch (saddr) {
103 case 0x0000: /* start tuner calibration mode */
104 need_gpio = 1;
Sri Deevib9255172009-03-04 17:49:01 -0300105 /* FW Loading is done */
106 dev->xc_fw_load_done = 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300107 break;
108 case 0x000D: /* Set signal source */
109 case 0x0001: /* Set TV standard - Video */
110 case 0x0002: /* Set TV standard - Audio */
111 case 0x0003: /* Set RF Frequency */
112 need_gpio = 1;
113 break;
114 default:
115 if (dev->xc_fw_load_done)
116 need_gpio = 1;
117 break;
118 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300119
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300120 if (need_gpio) {
121 dprintk1(1,
Sri Deevib9255172009-03-04 17:49:01 -0300122 "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",
123 msg->addr, msg->len, saddr);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300124
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300125 return dev->cx231xx_gpio_i2c_write(dev,
126 msg->addr,
127 msg->buf,
128 msg->len);
129 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300130 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300131
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300132 /* special case for Xc5000 tuner case */
133 saddr_len = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300134
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300135 /* adjust the length to correct length */
136 size -= saddr_len;
137 buf_ptr = (u8 *) (msg->buf + 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300138
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300139 do {
140 /* prepare xfer_data struct */
141 req_data.dev_addr = msg->addr;
142 req_data.direction = msg->flags;
143 req_data.saddr_len = saddr_len;
144 req_data.saddr_dat = msg->buf[0];
145 req_data.buf_size = size > 16 ? 16 : size;
146 req_data.p_buffer = (u8 *) (buf_ptr + loop * 16);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300147
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300148 bus->i2c_nostop = (size > 16) ? 1 : 0;
149 bus->i2c_reserve = (loop == 0) ? 0 : 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300150
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300151 /* usb send command */
152 status = dev->cx231xx_send_usb_command(bus, &req_data);
153 loop++;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300154
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300155 if (size >= 16)
156 size -= 16;
157 else
158 size = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300159
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300160 } while (size > 0);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300161
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300162 bus->i2c_nostop = 0;
163 bus->i2c_reserve = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300164
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300165 } else { /* regular case */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300166
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300167 /* prepare xfer_data struct */
168 req_data.dev_addr = msg->addr;
169 req_data.direction = msg->flags;
170 req_data.saddr_len = 0;
171 req_data.saddr_dat = 0;
172 req_data.buf_size = msg->len;
173 req_data.p_buffer = msg->buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300174
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300175 /* usb send command */
176 status = dev->cx231xx_send_usb_command(bus, &req_data);
177 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300178
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300179 return status < 0 ? status : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300180}
181
182/*
183 * cx231xx_i2c_recv_bytes()
184 * read a byte from the i2c device
185 */
186static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300187 const struct i2c_msg *msg)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300188{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300189 struct cx231xx_i2c *bus = i2c_adap->algo_data;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300190 struct cx231xx *dev = bus->dev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300191 struct cx231xx_i2c_xfer_data req_data;
192 int status = 0;
193 u16 saddr = 0;
194 u8 need_gpio = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300195
Mauro Carvalho Chehab3f25ffa2011-01-30 09:03:24 -0300196 if (is_tuner(dev, bus, msg, TUNER_XC5000)) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300197 if (msg->len == 2)
198 saddr = msg->buf[0] << 8 | msg->buf[1];
199 else if (msg->len == 1)
200 saddr = msg->buf[0];
Sri Deevie0d3baf2009-03-03 14:37:50 -0300201
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300202 if (dev->xc_fw_load_done) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300203
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300204 switch (saddr) {
205 case 0x0009: /* BUSY check */
206 dprintk1(1,
Sri Deevib9255172009-03-04 17:49:01 -0300207 "GPIO R E A D: Special case BUSY check \n");
208 /*Try read BUSY register, just set it to zero*/
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300209 msg->buf[0] = 0;
210 if (msg->len == 2)
211 msg->buf[1] = 0;
212 return 0;
213 case 0x0004: /* read Lock status */
214 need_gpio = 1;
215 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300216
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300217 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300218
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300219 if (need_gpio) {
Sri Deevib9255172009-03-04 17:49:01 -0300220 /* this is a special case to handle Xceive tuner
221 clock stretch issue with gpio based I2C */
222
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300223 dprintk1(1,
Sri Deevib9255172009-03-04 17:49:01 -0300224 "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",
225 msg->addr, msg->len,
226 msg->buf[0] << 8 | msg->buf[1]);
227
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300228 status =
229 dev->cx231xx_gpio_i2c_write(dev, msg->addr,
230 msg->buf,
231 msg->len);
232 status =
233 dev->cx231xx_gpio_i2c_read(dev, msg->addr,
234 msg->buf,
235 msg->len);
236 return status;
237 }
238 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300239
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300240 /* prepare xfer_data struct */
241 req_data.dev_addr = msg->addr;
242 req_data.direction = msg->flags;
243 req_data.saddr_len = msg->len;
244 req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1];
245 req_data.buf_size = msg->len;
246 req_data.p_buffer = msg->buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300247
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300248 /* usb send command */
249 status = dev->cx231xx_send_usb_command(bus, &req_data);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300250
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300251 } else {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300252
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300253 /* prepare xfer_data struct */
254 req_data.dev_addr = msg->addr;
255 req_data.direction = msg->flags;
256 req_data.saddr_len = 0;
257 req_data.saddr_dat = 0;
258 req_data.buf_size = msg->len;
259 req_data.p_buffer = msg->buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300260
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300261 /* usb send command */
262 status = dev->cx231xx_send_usb_command(bus, &req_data);
263 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300264
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300265 return status < 0 ? status : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300266}
267
268/*
269 * cx231xx_i2c_recv_bytes_with_saddr()
270 * read a byte from the i2c device
271 */
272static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300273 const struct i2c_msg *msg1,
274 const struct i2c_msg *msg2)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300275{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300276 struct cx231xx_i2c *bus = i2c_adap->algo_data;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300277 struct cx231xx *dev = bus->dev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300278 struct cx231xx_i2c_xfer_data req_data;
279 int status = 0;
280 u16 saddr = 0;
281 u8 need_gpio = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300282
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300283 if (msg1->len == 2)
284 saddr = msg1->buf[0] << 8 | msg1->buf[1];
285 else if (msg1->len == 1)
286 saddr = msg1->buf[0];
Sri Deevie0d3baf2009-03-03 14:37:50 -0300287
Mauro Carvalho Chehab3f25ffa2011-01-30 09:03:24 -0300288 if (is_tuner(dev, bus, msg2, TUNER_XC5000)) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300289 if ((msg2->len < 16)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300290
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300291 dprintk1(1,
Sri Deevib9255172009-03-04 17:49:01 -0300292 "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",
293 msg2->addr, msg2->len, saddr, msg1->len);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300294
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300295 switch (saddr) {
296 case 0x0008: /* read FW load status */
297 need_gpio = 1;
298 break;
299 case 0x0004: /* read Lock status */
300 need_gpio = 1;
301 break;
302 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300303
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300304 if (need_gpio) {
305 status =
306 dev->cx231xx_gpio_i2c_write(dev, msg1->addr,
307 msg1->buf,
308 msg1->len);
309 status =
310 dev->cx231xx_gpio_i2c_read(dev, msg2->addr,
311 msg2->buf,
312 msg2->len);
313 return status;
314 }
315 }
316 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300317
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300318 /* prepare xfer_data struct */
319 req_data.dev_addr = msg2->addr;
320 req_data.direction = msg2->flags;
321 req_data.saddr_len = msg1->len;
322 req_data.saddr_dat = saddr;
323 req_data.buf_size = msg2->len;
324 req_data.p_buffer = msg2->buf;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300325
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300326 /* usb send command */
327 status = dev->cx231xx_send_usb_command(bus, &req_data);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300328
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300329 return status < 0 ? status : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300330}
331
332/*
333 * cx231xx_i2c_check_for_device()
334 * check if there is a i2c_device at the supplied address
335 */
336static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300337 const struct i2c_msg *msg)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300338{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300339 struct cx231xx_i2c *bus = i2c_adap->algo_data;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300340 struct cx231xx *dev = bus->dev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300341 struct cx231xx_i2c_xfer_data req_data;
342 int status = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300343
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300344 /* prepare xfer_data struct */
345 req_data.dev_addr = msg->addr;
346 req_data.direction = msg->flags;
347 req_data.saddr_len = 0;
348 req_data.saddr_dat = 0;
349 req_data.buf_size = 0;
350 req_data.p_buffer = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300351
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300352 /* usb send command */
353 status = dev->cx231xx_send_usb_command(bus, &req_data);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300354
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300355 return status < 0 ? status : 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300356}
357
358/*
359 * cx231xx_i2c_xfer()
360 * the main i2c transfer function
361 */
362static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300363 struct i2c_msg msgs[], int num)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300364{
365 struct cx231xx_i2c *bus = i2c_adap->algo_data;
366 struct cx231xx *dev = bus->dev;
367 int addr, rc, i, byte;
368
369 if (num <= 0)
370 return 0;
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300371 mutex_lock(&dev->i2c_lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300372 for (i = 0; i < num; i++) {
373
374 addr = msgs[i].addr >> 1;
375
376 dprintk2(2, "%s %s addr=%x len=%d:",
377 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
378 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
Sri Deevib9255172009-03-04 17:49:01 -0300379 if (!msgs[i].len) {
380 /* no len: check only for device presence */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300381 rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]);
382 if (rc < 0) {
383 dprintk2(2, " no device\n");
Dan Carpenterb8383962010-10-21 16:22:14 -0300384 mutex_unlock(&dev->i2c_lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300385 return rc;
386 }
387
388 } else if (msgs[i].flags & I2C_M_RD) {
389 /* read bytes */
390 rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]);
391 if (i2c_debug >= 2) {
392 for (byte = 0; byte < msgs[i].len; byte++)
393 printk(" %02x", msgs[i].buf[byte]);
394 }
395 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300396 msgs[i].addr == msgs[i + 1].addr
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300397 && (msgs[i].len <= 2) && (bus->nr < 3)) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300398 /* read bytes */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300399 rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap,
400 &msgs[i],
401 &msgs[i + 1]);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300402 if (i2c_debug >= 2) {
403 for (byte = 0; byte < msgs[i].len; byte++)
404 printk(" %02x", msgs[i].buf[byte]);
405 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300406 i++;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300407 } else {
408 /* write bytes */
409 if (i2c_debug >= 2) {
410 for (byte = 0; byte < msgs[i].len; byte++)
411 printk(" %02x", msgs[i].buf[byte]);
412 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300413 rc = cx231xx_i2c_send_bytes(i2c_adap, &msgs[i]);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300414 }
415 if (rc < 0)
416 goto err;
417 if (i2c_debug >= 2)
418 printk("\n");
419 }
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300420 mutex_unlock(&dev->i2c_lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300421 return num;
Sri Deevib9255172009-03-04 17:49:01 -0300422err:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300423 dprintk2(2, " ERROR: %i\n", rc);
Palash Bandyopadhyay64fbf442010-07-06 18:12:25 -0300424 mutex_unlock(&dev->i2c_lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300425 return rc;
426}
427
428/* ----------------------------------------------------------- */
429
430/*
431 * functionality()
432 */
433static u32 functionality(struct i2c_adapter *adap)
434{
435 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
436}
437
Sri Deevie0d3baf2009-03-03 14:37:50 -0300438static struct i2c_algorithm cx231xx_algo = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300439 .master_xfer = cx231xx_i2c_xfer,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300440 .functionality = functionality,
441};
442
443static struct i2c_adapter cx231xx_adap_template = {
444 .owner = THIS_MODULE,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300445 .name = "cx231xx",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300446 .algo = &cx231xx_algo,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300447};
448
449static struct i2c_client cx231xx_client_template = {
450 .name = "cx231xx internal",
451};
452
453/* ----------------------------------------------------------- */
454
455/*
456 * i2c_devs
457 * incomplete list of known devices
458 */
459static char *i2c_devs[128] = {
460 [0x60 >> 1] = "colibri",
461 [0x88 >> 1] = "hammerhead",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300462 [0x8e >> 1] = "CIR",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300463 [0x32 >> 1] = "GeminiIII",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300464 [0x02 >> 1] = "Aquarius",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300465 [0xa0 >> 1] = "eeprom",
Mauro Carvalho Chehab3f25ffa2011-01-30 09:03:24 -0300466 [0xc0 >> 1] = "tuner",
467 [0xc2 >> 1] = "tuner",
Sri Deevie0d3baf2009-03-03 14:37:50 -0300468};
469
470/*
471 * cx231xx_do_i2c_scan()
472 * check i2c address range for devices
473 */
474void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c)
475{
476 unsigned char buf;
477 int i, rc;
478
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300479 cx231xx_info(": Checking for I2C devices ..\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300480 for (i = 0; i < 128; i++) {
481 c->addr = i;
482 rc = i2c_master_recv(c, &buf, 0);
483 if (rc < 0)
484 continue;
485 cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300486 dev->name, i << 1,
487 i2c_devs[i] ? i2c_devs[i] : "???");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300488 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300489 cx231xx_info(": Completed Checking for I2C devices.\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300490}
491
492/*
Sri Deevie0d3baf2009-03-03 14:37:50 -0300493 * cx231xx_i2c_register()
494 * register i2c bus
495 */
496int cx231xx_i2c_register(struct cx231xx_i2c *bus)
497{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300498 struct cx231xx *dev = bus->dev;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300499
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300500 BUG_ON(!dev->cx231xx_send_usb_command);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300501
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300502 memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap));
503 memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300504 memcpy(&bus->i2c_client, &cx231xx_client_template,
505 sizeof(bus->i2c_client));
506
507 bus->i2c_adap.dev.parent = &dev->udev->dev;
508
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300509 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300510
511 bus->i2c_algo.data = bus;
512 bus->i2c_adap.algo_data = bus;
Sri Deevib1196122009-03-20 23:33:48 -0300513 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300514 i2c_add_adapter(&bus->i2c_adap);
515
516 bus->i2c_client.adapter = &bus->i2c_adap;
517
518 if (0 == bus->i2c_rc) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300519 if (i2c_scan)
520 cx231xx_do_i2c_scan(dev, &bus->i2c_client);
521 } else
522 cx231xx_warn("%s: i2c bus %d register FAILED\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300523 dev->name, bus->nr);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300524
525 return bus->i2c_rc;
526}
527
528/*
529 * cx231xx_i2c_unregister()
530 * unregister i2c_bus
531 */
532int cx231xx_i2c_unregister(struct cx231xx_i2c *bus)
533{
534 i2c_del_adapter(&bus->i2c_adap);
535 return 0;
536}