Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | |
3 | bttv-i2c.c -- all the i2c code is here | |
4 | ||
5 | bttv - Bt848 frame grabber driver | |
6 | ||
7 | Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de) | |
4ac97914 | 8 | & Marcus Metzler (mocm@thp.uni-koeln.de) |
1da177e4 LT |
9 | (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org> |
10 | ||
141276b5 MCC |
11 | (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org> |
12 | - Multituner support and i2c address binding | |
13 | ||
1da177e4 LT |
14 | This program is free software; you can redistribute it and/or modify |
15 | it under the terms of the GNU General Public License as published by | |
16 | the Free Software Foundation; either version 2 of the License, or | |
17 | (at your option) any later version. | |
18 | ||
19 | This program is distributed in the hope that it will be useful, | |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
23 | ||
24 | You should have received a copy of the GNU General Public License | |
25 | along with this program; if not, write to the Free Software | |
26 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27 | ||
28 | */ | |
29 | ||
8af443e5 JP |
30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
31 | ||
1da177e4 | 32 | #include <linux/module.h> |
1da177e4 LT |
33 | #include <linux/init.h> |
34 | #include <linux/delay.h> | |
1da177e4 LT |
35 | |
36 | #include "bttvp.h" | |
5e453dc7 | 37 | #include <media/v4l2-common.h> |
b5b8ab8d MCC |
38 | #include <linux/jiffies.h> |
39 | #include <asm/io.h> | |
1da177e4 | 40 | |
a5ed425c MCC |
41 | static int i2c_debug; |
42 | static int i2c_hw; | |
43 | static int i2c_scan; | |
1da177e4 | 44 | module_param(i2c_debug, int, 0644); |
0a25f3b2 | 45 | MODULE_PARM_DESC(i2c_debug, "configure i2c debug level"); |
1da177e4 | 46 | module_param(i2c_hw, int, 0444); |
141276b5 MCC |
47 | MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, " |
48 | "instead of software bitbang"); | |
1da177e4 LT |
49 | module_param(i2c_scan, int, 0444); |
50 | MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time"); | |
51 | ||
141276b5 MCC |
52 | static unsigned int i2c_udelay = 5; |
53 | module_param(i2c_udelay, int, 0444); | |
54 | MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs " | |
55 | "(should be 5 or higher). Lower value means higher bus speed."); | |
56 | ||
1da177e4 LT |
57 | /* ----------------------------------------------------------------------- */ |
58 | /* I2C functions - bitbanging adapter (software i2c) */ | |
59 | ||
60 | static void bttv_bit_setscl(void *data, int state) | |
61 | { | |
62 | struct bttv *btv = (struct bttv*)data; | |
63 | ||
64 | if (state) | |
65 | btv->i2c_state |= 0x02; | |
66 | else | |
67 | btv->i2c_state &= ~0x02; | |
68 | btwrite(btv->i2c_state, BT848_I2C); | |
69 | btread(BT848_I2C); | |
70 | } | |
71 | ||
72 | static void bttv_bit_setsda(void *data, int state) | |
73 | { | |
74 | struct bttv *btv = (struct bttv*)data; | |
75 | ||
76 | if (state) | |
77 | btv->i2c_state |= 0x01; | |
78 | else | |
79 | btv->i2c_state &= ~0x01; | |
80 | btwrite(btv->i2c_state, BT848_I2C); | |
81 | btread(BT848_I2C); | |
82 | } | |
83 | ||
84 | static int bttv_bit_getscl(void *data) | |
85 | { | |
86 | struct bttv *btv = (struct bttv*)data; | |
87 | int state; | |
88 | ||
89 | state = btread(BT848_I2C) & 0x02 ? 1 : 0; | |
90 | return state; | |
91 | } | |
92 | ||
93 | static int bttv_bit_getsda(void *data) | |
94 | { | |
95 | struct bttv *btv = (struct bttv*)data; | |
96 | int state; | |
97 | ||
98 | state = btread(BT848_I2C) & 0x01; | |
99 | return state; | |
100 | } | |
101 | ||
4c62e976 | 102 | static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = { |
1da177e4 LT |
103 | .setsda = bttv_bit_setsda, |
104 | .setscl = bttv_bit_setscl, | |
105 | .getsda = bttv_bit_getsda, | |
106 | .getscl = bttv_bit_getscl, | |
107 | .udelay = 16, | |
1da177e4 LT |
108 | .timeout = 200, |
109 | }; | |
110 | ||
1da177e4 LT |
111 | /* ----------------------------------------------------------------------- */ |
112 | /* I2C functions - hardware i2c */ | |
113 | ||
1da177e4 LT |
114 | static u32 functionality(struct i2c_adapter *adap) |
115 | { | |
116 | return I2C_FUNC_SMBUS_EMUL; | |
117 | } | |
118 | ||
119 | static int | |
120 | bttv_i2c_wait_done(struct bttv *btv) | |
121 | { | |
1da177e4 LT |
122 | int rc = 0; |
123 | ||
fc9d53af MA |
124 | /* timeout */ |
125 | if (wait_event_interruptible_timeout(btv->i2c_queue, | |
3074fc84 JL |
126 | btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS) |
127 | rc = -EIO; | |
1da177e4 | 128 | |
1da177e4 LT |
129 | if (btv->i2c_done & BT848_INT_RACK) |
130 | rc = 1; | |
131 | btv->i2c_done = 0; | |
132 | return rc; | |
133 | } | |
134 | ||
135 | #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\ | |
136 | BT848_I2C_SCL | BT848_I2C_SDA) | |
137 | ||
138 | static int | |
139 | bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last) | |
140 | { | |
141 | u32 xmit; | |
142 | int retval,cnt; | |
143 | ||
144 | /* sanity checks */ | |
145 | if (0 == msg->len) | |
146 | return -EINVAL; | |
147 | ||
148 | /* start, address + first byte */ | |
149 | xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW; | |
150 | if (msg->len > 1 || !last) | |
151 | xmit |= BT878_I2C_NOSTOP; | |
152 | btwrite(xmit, BT848_I2C); | |
153 | retval = bttv_i2c_wait_done(btv); | |
154 | if (retval < 0) | |
155 | goto err; | |
156 | if (retval == 0) | |
157 | goto eio; | |
158 | if (i2c_debug) { | |
8af443e5 | 159 | pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]); |
1da177e4 LT |
160 | } |
161 | ||
162 | for (cnt = 1; cnt < msg->len; cnt++ ) { | |
163 | /* following bytes */ | |
164 | xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART; | |
165 | if (cnt < msg->len-1 || !last) | |
166 | xmit |= BT878_I2C_NOSTOP; | |
167 | btwrite(xmit, BT848_I2C); | |
168 | retval = bttv_i2c_wait_done(btv); | |
169 | if (retval < 0) | |
170 | goto err; | |
171 | if (retval == 0) | |
172 | goto eio; | |
8af443e5 JP |
173 | if (i2c_debug) |
174 | pr_cont(" %02x", msg->buf[cnt]); | |
1da177e4 | 175 | } |
8af443e5 JP |
176 | if (!(xmit & BT878_I2C_NOSTOP)) |
177 | pr_cont(">\n"); | |
1da177e4 LT |
178 | return msg->len; |
179 | ||
180 | eio: | |
181 | retval = -EIO; | |
182 | err: | |
183 | if (i2c_debug) | |
8af443e5 | 184 | pr_cont(" ERR: %d\n",retval); |
1da177e4 LT |
185 | return retval; |
186 | } | |
187 | ||
188 | static int | |
189 | bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last) | |
190 | { | |
191 | u32 xmit; | |
192 | u32 cnt; | |
193 | int retval; | |
194 | ||
8af443e5 | 195 | for (cnt = 0; cnt < msg->len; cnt++) { |
1da177e4 LT |
196 | xmit = (msg->addr << 25) | (1 << 24) | I2C_HW; |
197 | if (cnt < msg->len-1) | |
198 | xmit |= BT848_I2C_W3B; | |
199 | if (cnt < msg->len-1 || !last) | |
200 | xmit |= BT878_I2C_NOSTOP; | |
201 | if (cnt) | |
202 | xmit |= BT878_I2C_NOSTART; | |
8af443e5 JP |
203 | |
204 | if (i2c_debug) { | |
205 | if (!(xmit & BT878_I2C_NOSTART)) | |
206 | pr_cont(" <R %02x", (msg->addr << 1) +1); | |
207 | } | |
208 | ||
1da177e4 LT |
209 | btwrite(xmit, BT848_I2C); |
210 | retval = bttv_i2c_wait_done(btv); | |
211 | if (retval < 0) | |
212 | goto err; | |
213 | if (retval == 0) | |
214 | goto eio; | |
215 | msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff; | |
216 | if (i2c_debug) { | |
8af443e5 | 217 | pr_cont(" =%02x", msg->buf[cnt]); |
1da177e4 | 218 | } |
8af443e5 JP |
219 | if (i2c_debug && !(xmit & BT878_I2C_NOSTOP)) |
220 | pr_cont(" >\n"); | |
1da177e4 | 221 | } |
8af443e5 JP |
222 | |
223 | ||
1da177e4 LT |
224 | return msg->len; |
225 | ||
226 | eio: | |
227 | retval = -EIO; | |
228 | err: | |
229 | if (i2c_debug) | |
8af443e5 | 230 | pr_cont(" ERR: %d\n",retval); |
4ac97914 | 231 | return retval; |
1da177e4 LT |
232 | } |
233 | ||
234 | static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num) | |
235 | { | |
74fc7bd9 HV |
236 | struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap); |
237 | struct bttv *btv = to_bttv(v4l2_dev); | |
1da177e4 LT |
238 | int retval = 0; |
239 | int i; | |
240 | ||
241 | if (i2c_debug) | |
8af443e5 JP |
242 | pr_debug("bt-i2c:"); |
243 | ||
1da177e4 LT |
244 | btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT); |
245 | for (i = 0 ; i < num; i++) { | |
246 | if (msgs[i].flags & I2C_M_RD) { | |
247 | /* read */ | |
248 | retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num); | |
249 | if (retval < 0) | |
250 | goto err; | |
251 | } else { | |
252 | /* write */ | |
253 | retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num); | |
254 | if (retval < 0) | |
255 | goto err; | |
256 | } | |
257 | } | |
258 | return num; | |
259 | ||
260 | err: | |
261 | return retval; | |
262 | } | |
263 | ||
df9b5d4c | 264 | static const struct i2c_algorithm bttv_algo = { |
1da177e4 | 265 | .master_xfer = bttv_i2c_xfer, |
1da177e4 LT |
266 | .functionality = functionality, |
267 | }; | |
268 | ||
1da177e4 LT |
269 | /* ----------------------------------------------------------------------- */ |
270 | /* I2C functions - common stuff */ | |
271 | ||
1da177e4 LT |
272 | /* read I2C */ |
273 | int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) | |
274 | { | |
4ac97914 | 275 | unsigned char buffer = 0; |
1da177e4 LT |
276 | |
277 | if (0 != btv->i2c_rc) | |
278 | return -1; | |
279 | if (bttv_verbose && NULL != probe_for) | |
8af443e5 JP |
280 | pr_info("%d: i2c: checking for %s @ 0x%02x... ", |
281 | btv->c.nr, probe_for, addr); | |
4ac97914 MCC |
282 | btv->i2c_client.addr = addr >> 1; |
283 | if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) { | |
1da177e4 LT |
284 | if (NULL != probe_for) { |
285 | if (bttv_verbose) | |
8af443e5 | 286 | pr_cont("not found\n"); |
1da177e4 | 287 | } else |
8af443e5 JP |
288 | pr_warn("%d: i2c read 0x%x: error\n", |
289 | btv->c.nr, addr); | |
4ac97914 | 290 | return -1; |
1da177e4 LT |
291 | } |
292 | if (bttv_verbose && NULL != probe_for) | |
8af443e5 | 293 | pr_cont("found\n"); |
4ac97914 | 294 | return buffer; |
1da177e4 LT |
295 | } |
296 | ||
297 | /* write I2C */ | |
298 | int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1, | |
4ac97914 | 299 | unsigned char b2, int both) |
1da177e4 | 300 | { |
4ac97914 MCC |
301 | unsigned char buffer[2]; |
302 | int bytes = both ? 2 : 1; | |
1da177e4 LT |
303 | |
304 | if (0 != btv->i2c_rc) | |
305 | return -1; | |
4ac97914 MCC |
306 | btv->i2c_client.addr = addr >> 1; |
307 | buffer[0] = b1; | |
308 | buffer[1] = b2; | |
309 | if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes)) | |
1da177e4 | 310 | return -1; |
4ac97914 | 311 | return 0; |
1da177e4 LT |
312 | } |
313 | ||
314 | /* read EEPROM content */ | |
4c62e976 | 315 | void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr) |
1da177e4 | 316 | { |
5daf05fb GK |
317 | memset(eedata, 0, 256); |
318 | if (0 != btv->i2c_rc) | |
319 | return; | |
1da177e4 LT |
320 | btv->i2c_client.addr = addr >> 1; |
321 | tveeprom_read(&btv->i2c_client, eedata, 256); | |
322 | } | |
323 | ||
324 | static char *i2c_devs[128] = { | |
24a70fdc | 325 | [ 0x1c >> 1 ] = "lgdt330x", |
1da177e4 LT |
326 | [ 0x30 >> 1 ] = "IR (hauppauge)", |
327 | [ 0x80 >> 1 ] = "msp34xx", | |
328 | [ 0x86 >> 1 ] = "tda9887", | |
329 | [ 0xa0 >> 1 ] = "eeprom", | |
330 | [ 0xc0 >> 1 ] = "tuner (analog)", | |
331 | [ 0xc2 >> 1 ] = "tuner (analog)", | |
332 | }; | |
333 | ||
334 | static void do_i2c_scan(char *name, struct i2c_client *c) | |
335 | { | |
336 | unsigned char buf; | |
337 | int i,rc; | |
338 | ||
53c4e955 | 339 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { |
1da177e4 LT |
340 | c->addr = i; |
341 | rc = i2c_master_recv(c,&buf,0); | |
342 | if (rc < 0) | |
343 | continue; | |
8af443e5 JP |
344 | pr_info("%s: i2c scan: found device @ 0x%x [%s]\n", |
345 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); | |
1da177e4 LT |
346 | } |
347 | } | |
348 | ||
a824f0f4 | 349 | /* init + register i2c adapter */ |
4c62e976 | 350 | int init_bttv_i2c(struct bttv *btv) |
1da177e4 | 351 | { |
df9b5d4c | 352 | strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE); |
1da177e4 LT |
353 | |
354 | if (i2c_hw) | |
355 | btv->use_i2c_hw = 1; | |
356 | if (btv->use_i2c_hw) { | |
357 | /* bt878 */ | |
df9b5d4c JD |
358 | strlcpy(btv->c.i2c_adap.name, "bt878", |
359 | sizeof(btv->c.i2c_adap.name)); | |
df9b5d4c | 360 | btv->c.i2c_adap.algo = &bttv_algo; |
1da177e4 LT |
361 | } else { |
362 | /* bt848 */ | |
141276b5 MCC |
363 | /* Prevents usage of invalid delay values */ |
364 | if (i2c_udelay<5) | |
365 | i2c_udelay=5; | |
141276b5 | 366 | |
df9b5d4c JD |
367 | strlcpy(btv->c.i2c_adap.name, "bttv", |
368 | sizeof(btv->c.i2c_adap.name)); | |
1da177e4 LT |
369 | memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template, |
370 | sizeof(bttv_i2c_algo_bit_template)); | |
df9b5d4c | 371 | btv->i2c_algo.udelay = i2c_udelay; |
1da177e4 LT |
372 | btv->i2c_algo.data = btv; |
373 | btv->c.i2c_adap.algo_data = &btv->i2c_algo; | |
374 | } | |
df9b5d4c | 375 | btv->c.i2c_adap.owner = THIS_MODULE; |
1da177e4 LT |
376 | |
377 | btv->c.i2c_adap.dev.parent = &btv->c.pci->dev; | |
378 | snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name), | |
379 | "bt%d #%d [%s]", btv->id, btv->c.nr, | |
380 | btv->use_i2c_hw ? "hw" : "sw"); | |
381 | ||
74fc7bd9 | 382 | i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev); |
4ac97914 | 383 | btv->i2c_client.adapter = &btv->c.i2c_adap; |
1da177e4 | 384 | |
1da177e4 LT |
385 | |
386 | if (btv->use_i2c_hw) { | |
387 | btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap); | |
388 | } else { | |
389 | bttv_bit_setscl(btv,1); | |
390 | bttv_bit_setsda(btv,1); | |
391 | btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap); | |
392 | } | |
393 | if (0 == btv->i2c_rc && i2c_scan) | |
74fc7bd9 | 394 | do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client); |
c668f32d | 395 | |
24344664 JD |
396 | return btv->i2c_rc; |
397 | } |