Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 8 | & Marcus Metzler (mocm@thp.uni-koeln.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org> |
| 10 | |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 11 | (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 12 | - Multituner support and i2c address binding |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/init.h> |
| 34 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include "bttvp.h" |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 37 | #include <media/v4l2-common.h> |
Mauro Carvalho Chehab | b5b8ab8 | 2006-01-09 15:25:20 -0200 | [diff] [blame] | 38 | #include <linux/jiffies.h> |
| 39 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Mauro Carvalho Chehab | a5ed425 | 2006-01-13 14:10:19 -0200 | [diff] [blame] | 41 | static int i2c_debug; |
| 42 | static int i2c_hw; |
| 43 | static int i2c_scan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | module_param(i2c_debug, int, 0644); |
Jarod Wilson | 0a25f3b | 2009-12-10 16:54:48 -0300 | [diff] [blame] | 45 | MODULE_PARM_DESC(i2c_debug, "configure i2c debug level"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | module_param(i2c_hw, int, 0444); |
Mauro Carvalho Chehab | 652fd6e | 2016-10-18 17:44:02 -0200 | [diff] [blame] | 47 | MODULE_PARM_DESC(i2c_hw, "force use of hardware i2c support, instead of software bitbang"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | module_param(i2c_scan, int, 0444); |
| 49 | MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time"); |
| 50 | |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 51 | static unsigned int i2c_udelay = 5; |
| 52 | module_param(i2c_udelay, int, 0444); |
Mauro Carvalho Chehab | 652fd6e | 2016-10-18 17:44:02 -0200 | [diff] [blame] | 53 | MODULE_PARM_DESC(i2c_udelay, "soft i2c delay at insmod time, in usecs (should be 5 or higher). Lower value means higher bus speed."); |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* ----------------------------------------------------------------------- */ |
| 56 | /* I2C functions - bitbanging adapter (software i2c) */ |
| 57 | |
| 58 | static void bttv_bit_setscl(void *data, int state) |
| 59 | { |
| 60 | struct bttv *btv = (struct bttv*)data; |
| 61 | |
| 62 | if (state) |
| 63 | btv->i2c_state |= 0x02; |
| 64 | else |
| 65 | btv->i2c_state &= ~0x02; |
| 66 | btwrite(btv->i2c_state, BT848_I2C); |
| 67 | btread(BT848_I2C); |
| 68 | } |
| 69 | |
| 70 | static void bttv_bit_setsda(void *data, int state) |
| 71 | { |
| 72 | struct bttv *btv = (struct bttv*)data; |
| 73 | |
| 74 | if (state) |
| 75 | btv->i2c_state |= 0x01; |
| 76 | else |
| 77 | btv->i2c_state &= ~0x01; |
| 78 | btwrite(btv->i2c_state, BT848_I2C); |
| 79 | btread(BT848_I2C); |
| 80 | } |
| 81 | |
| 82 | static int bttv_bit_getscl(void *data) |
| 83 | { |
| 84 | struct bttv *btv = (struct bttv*)data; |
| 85 | int state; |
| 86 | |
| 87 | state = btread(BT848_I2C) & 0x02 ? 1 : 0; |
| 88 | return state; |
| 89 | } |
| 90 | |
| 91 | static int bttv_bit_getsda(void *data) |
| 92 | { |
| 93 | struct bttv *btv = (struct bttv*)data; |
| 94 | int state; |
| 95 | |
| 96 | state = btread(BT848_I2C) & 0x01; |
| 97 | return state; |
| 98 | } |
| 99 | |
Bhumika Goyal | 4d94744 | 2017-08-21 09:33:32 -0400 | [diff] [blame] | 100 | static const struct i2c_algo_bit_data bttv_i2c_algo_bit_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .setsda = bttv_bit_setsda, |
| 102 | .setscl = bttv_bit_setscl, |
| 103 | .getsda = bttv_bit_getsda, |
| 104 | .getscl = bttv_bit_getscl, |
| 105 | .udelay = 16, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | .timeout = 200, |
| 107 | }; |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* ----------------------------------------------------------------------- */ |
| 110 | /* I2C functions - hardware i2c */ |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static u32 functionality(struct i2c_adapter *adap) |
| 113 | { |
| 114 | return I2C_FUNC_SMBUS_EMUL; |
| 115 | } |
| 116 | |
| 117 | static int |
| 118 | bttv_i2c_wait_done(struct bttv *btv) |
| 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | int rc = 0; |
| 121 | |
Manu Abraham | fc9d53a | 2005-05-05 16:16:01 -0700 | [diff] [blame] | 122 | /* timeout */ |
| 123 | if (wait_event_interruptible_timeout(btv->i2c_queue, |
Julia Lawall | 3074fc8 | 2010-08-05 17:29:55 -0300 | [diff] [blame] | 124 | btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS) |
| 125 | rc = -EIO; |
Manu Abraham | fc9d53a | 2005-05-05 16:16:01 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (btv->i2c_done & BT848_INT_RACK) |
| 128 | rc = 1; |
| 129 | btv->i2c_done = 0; |
| 130 | return rc; |
| 131 | } |
| 132 | |
| 133 | #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\ |
| 134 | BT848_I2C_SCL | BT848_I2C_SDA) |
| 135 | |
| 136 | static int |
| 137 | bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last) |
| 138 | { |
| 139 | u32 xmit; |
| 140 | int retval,cnt; |
| 141 | |
| 142 | /* sanity checks */ |
| 143 | if (0 == msg->len) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | /* start, address + first byte */ |
| 147 | xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW; |
| 148 | if (msg->len > 1 || !last) |
| 149 | xmit |= BT878_I2C_NOSTOP; |
| 150 | btwrite(xmit, BT848_I2C); |
| 151 | retval = bttv_i2c_wait_done(btv); |
| 152 | if (retval < 0) |
| 153 | goto err; |
| 154 | if (retval == 0) |
| 155 | goto eio; |
| 156 | if (i2c_debug) { |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 157 | pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | for (cnt = 1; cnt < msg->len; cnt++ ) { |
| 161 | /* following bytes */ |
| 162 | xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART; |
| 163 | if (cnt < msg->len-1 || !last) |
| 164 | xmit |= BT878_I2C_NOSTOP; |
| 165 | btwrite(xmit, BT848_I2C); |
| 166 | retval = bttv_i2c_wait_done(btv); |
| 167 | if (retval < 0) |
| 168 | goto err; |
| 169 | if (retval == 0) |
| 170 | goto eio; |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 171 | if (i2c_debug) |
| 172 | pr_cont(" %02x", msg->buf[cnt]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
John Törnblom | 1ca6ae8 | 2012-12-17 08:53:54 -0300 | [diff] [blame] | 174 | if (i2c_debug && !(xmit & BT878_I2C_NOSTOP)) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 175 | pr_cont(">\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return msg->len; |
| 177 | |
| 178 | eio: |
| 179 | retval = -EIO; |
| 180 | err: |
| 181 | if (i2c_debug) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 182 | pr_cont(" ERR: %d\n",retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return retval; |
| 184 | } |
| 185 | |
| 186 | static int |
| 187 | bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last) |
| 188 | { |
| 189 | u32 xmit; |
| 190 | u32 cnt; |
| 191 | int retval; |
| 192 | |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 193 | for (cnt = 0; cnt < msg->len; cnt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | xmit = (msg->addr << 25) | (1 << 24) | I2C_HW; |
| 195 | if (cnt < msg->len-1) |
| 196 | xmit |= BT848_I2C_W3B; |
| 197 | if (cnt < msg->len-1 || !last) |
| 198 | xmit |= BT878_I2C_NOSTOP; |
| 199 | if (cnt) |
| 200 | xmit |= BT878_I2C_NOSTART; |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 201 | |
| 202 | if (i2c_debug) { |
| 203 | if (!(xmit & BT878_I2C_NOSTART)) |
| 204 | pr_cont(" <R %02x", (msg->addr << 1) +1); |
| 205 | } |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | btwrite(xmit, BT848_I2C); |
| 208 | retval = bttv_i2c_wait_done(btv); |
| 209 | if (retval < 0) |
| 210 | goto err; |
| 211 | if (retval == 0) |
| 212 | goto eio; |
| 213 | msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff; |
| 214 | if (i2c_debug) { |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 215 | pr_cont(" =%02x", msg->buf[cnt]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 217 | if (i2c_debug && !(xmit & BT878_I2C_NOSTOP)) |
| 218 | pr_cont(" >\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 220 | |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return msg->len; |
| 223 | |
| 224 | eio: |
| 225 | retval = -EIO; |
| 226 | err: |
| 227 | if (i2c_debug) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 228 | pr_cont(" ERR: %d\n",retval); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 229 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num) |
| 233 | { |
Hans Verkuil | 74fc7bd | 2009-03-14 12:36:54 -0300 | [diff] [blame] | 234 | struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap); |
| 235 | struct bttv *btv = to_bttv(v4l2_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | int retval = 0; |
| 237 | int i; |
| 238 | |
| 239 | if (i2c_debug) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 240 | pr_debug("bt-i2c:"); |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT); |
| 243 | for (i = 0 ; i < num; i++) { |
| 244 | if (msgs[i].flags & I2C_M_RD) { |
| 245 | /* read */ |
| 246 | retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num); |
| 247 | if (retval < 0) |
| 248 | goto err; |
| 249 | } else { |
| 250 | /* write */ |
| 251 | retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num); |
| 252 | if (retval < 0) |
| 253 | goto err; |
| 254 | } |
| 255 | } |
| 256 | return num; |
| 257 | |
| 258 | err: |
| 259 | return retval; |
| 260 | } |
| 261 | |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 262 | static const struct i2c_algorithm bttv_algo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | .master_xfer = bttv_i2c_xfer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | .functionality = functionality, |
| 265 | }; |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* ----------------------------------------------------------------------- */ |
| 268 | /* I2C functions - common stuff */ |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* read I2C */ |
| 271 | int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) |
| 272 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 273 | unsigned char buffer = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | if (0 != btv->i2c_rc) |
| 276 | return -1; |
| 277 | if (bttv_verbose && NULL != probe_for) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 278 | pr_info("%d: i2c: checking for %s @ 0x%02x... ", |
| 279 | btv->c.nr, probe_for, addr); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 280 | btv->i2c_client.addr = addr >> 1; |
| 281 | if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (NULL != probe_for) { |
| 283 | if (bttv_verbose) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 284 | pr_cont("not found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } else |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 286 | pr_warn("%d: i2c read 0x%x: error\n", |
| 287 | btv->c.nr, addr); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 288 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | if (bttv_verbose && NULL != probe_for) |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 291 | pr_cont("found\n"); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 292 | return buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* write I2C */ |
| 296 | int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1, |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 297 | unsigned char b2, int both) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 299 | unsigned char buffer[2]; |
| 300 | int bytes = both ? 2 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | if (0 != btv->i2c_rc) |
| 303 | return -1; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 304 | btv->i2c_client.addr = addr >> 1; |
| 305 | buffer[0] = b1; |
| 306 | buffer[1] = b2; |
| 307 | if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return -1; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 309 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* read EEPROM content */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 313 | void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Gerd Knorr | 5daf05f | 2005-05-25 12:31:26 -0700 | [diff] [blame] | 315 | memset(eedata, 0, 256); |
| 316 | if (0 != btv->i2c_rc) |
| 317 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | btv->i2c_client.addr = addr >> 1; |
| 319 | tveeprom_read(&btv->i2c_client, eedata, 256); |
| 320 | } |
| 321 | |
| 322 | static char *i2c_devs[128] = { |
Mauro Carvalho Chehab | 24a70fd | 2005-09-09 13:03:39 -0700 | [diff] [blame] | 323 | [ 0x1c >> 1 ] = "lgdt330x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | [ 0x30 >> 1 ] = "IR (hauppauge)", |
| 325 | [ 0x80 >> 1 ] = "msp34xx", |
| 326 | [ 0x86 >> 1 ] = "tda9887", |
| 327 | [ 0xa0 >> 1 ] = "eeprom", |
| 328 | [ 0xc0 >> 1 ] = "tuner (analog)", |
| 329 | [ 0xc2 >> 1 ] = "tuner (analog)", |
| 330 | }; |
| 331 | |
| 332 | static void do_i2c_scan(char *name, struct i2c_client *c) |
| 333 | { |
| 334 | unsigned char buf; |
| 335 | int i,rc; |
| 336 | |
Mauro Carvalho Chehab | 53c4e95 | 2007-03-29 08:42:30 -0300 | [diff] [blame] | 337 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | c->addr = i; |
| 339 | rc = i2c_master_recv(c,&buf,0); |
| 340 | if (rc < 0) |
| 341 | continue; |
Joe Perches | 8af443e | 2011-08-21 19:56:48 -0300 | [diff] [blame] | 342 | pr_info("%s: i2c scan: found device @ 0x%x [%s]\n", |
| 343 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Jean Delvare | a824f0f | 2011-11-09 13:27:53 -0300 | [diff] [blame] | 347 | /* init + register i2c adapter */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 348 | int init_bttv_i2c(struct bttv *btv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 350 | strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | if (i2c_hw) |
| 353 | btv->use_i2c_hw = 1; |
| 354 | if (btv->use_i2c_hw) { |
| 355 | /* bt878 */ |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 356 | strlcpy(btv->c.i2c_adap.name, "bt878", |
| 357 | sizeof(btv->c.i2c_adap.name)); |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 358 | btv->c.i2c_adap.algo = &bttv_algo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } else { |
| 360 | /* bt848 */ |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 361 | /* Prevents usage of invalid delay values */ |
| 362 | if (i2c_udelay<5) |
| 363 | i2c_udelay=5; |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 364 | |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 365 | strlcpy(btv->c.i2c_adap.name, "bttv", |
| 366 | sizeof(btv->c.i2c_adap.name)); |
Ezequiel Garcia | b4b1d04 | 2012-10-23 15:57:22 -0300 | [diff] [blame] | 367 | btv->i2c_algo = bttv_i2c_algo_bit_template; |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 368 | btv->i2c_algo.udelay = i2c_udelay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | btv->i2c_algo.data = btv; |
| 370 | btv->c.i2c_adap.algo_data = &btv->i2c_algo; |
| 371 | } |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame] | 372 | btv->c.i2c_adap.owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | btv->c.i2c_adap.dev.parent = &btv->c.pci->dev; |
| 375 | snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name), |
| 376 | "bt%d #%d [%s]", btv->id, btv->c.nr, |
| 377 | btv->use_i2c_hw ? "hw" : "sw"); |
| 378 | |
Hans Verkuil | 74fc7bd | 2009-03-14 12:36:54 -0300 | [diff] [blame] | 379 | i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 380 | btv->i2c_client.adapter = &btv->c.i2c_adap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | if (btv->use_i2c_hw) { |
| 384 | btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap); |
| 385 | } else { |
| 386 | bttv_bit_setscl(btv,1); |
| 387 | bttv_bit_setsda(btv,1); |
| 388 | btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap); |
| 389 | } |
| 390 | if (0 == btv->i2c_rc && i2c_scan) |
Hans Verkuil | 74fc7bd | 2009-03-14 12:36:54 -0300 | [diff] [blame] | 391 | do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client); |
Jean Delvare | c668f32 | 2009-05-13 16:48:50 -0300 | [diff] [blame] | 392 | |
Jean Delvare | 2434466 | 2010-02-19 00:18:41 -0300 | [diff] [blame] | 393 | return btv->i2c_rc; |
| 394 | } |
Frank Schaefer | 457ba4c | 2013-02-17 09:41:29 -0300 | [diff] [blame] | 395 | |
| 396 | int fini_bttv_i2c(struct bttv *btv) |
| 397 | { |
Linus Torvalds | 99bece7 | 2013-05-02 14:38:53 -0700 | [diff] [blame] | 398 | if (btv->i2c_rc == 0) |
| 399 | i2c_del_adapter(&btv->c.i2c_adap); |
Frank Schaefer | 457ba4c | 2013-02-17 09:41:29 -0300 | [diff] [blame] | 400 | |
Linus Torvalds | 99bece7 | 2013-05-02 14:38:53 -0700 | [diff] [blame] | 401 | return 0; |
Frank Schaefer | 457ba4c | 2013-02-17 09:41:29 -0300 | [diff] [blame] | 402 | } |