Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Conexant CX23885 PCIe bridge |
| 3 | * |
| 4 | * Copyright (c) 2006 Steven Toth <stoth@hauppauge.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <asm/io.h> |
| 27 | |
| 28 | #include "cx23885.h" |
| 29 | |
| 30 | #include <media/v4l2-common.h> |
| 31 | |
Michael Krufky | 3c5666a | 2007-03-20 23:18:04 -0300 | [diff] [blame] | 32 | static unsigned int i2c_debug = 0; |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 33 | module_param(i2c_debug, int, 0644); |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 34 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 35 | |
| 36 | static unsigned int i2c_scan = 0; |
| 37 | module_param(i2c_scan, int, 0444); |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 38 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 39 | |
| 40 | #define dprintk(level,fmt, arg...) if (i2c_debug >= level) \ |
| 41 | printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg) |
| 42 | |
| 43 | #define I2C_WAIT_DELAY 32 |
| 44 | #define I2C_WAIT_RETRY 64 |
| 45 | |
| 46 | #define I2C_EXTEND (1 << 3) |
| 47 | #define I2C_NOSTOP (1 << 4) |
| 48 | |
| 49 | static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap) |
| 50 | { |
| 51 | struct cx23885_i2c *bus = i2c_adap->algo_data; |
| 52 | struct cx23885_dev *dev = bus->dev; |
| 53 | return cx_read(bus->reg_stat) & 0x01; |
| 54 | } |
| 55 | |
| 56 | static inline int i2c_is_busy(struct i2c_adapter *i2c_adap) |
| 57 | { |
| 58 | struct cx23885_i2c *bus = i2c_adap->algo_data; |
| 59 | struct cx23885_dev *dev = bus->dev; |
| 60 | return cx_read(bus->reg_stat) & 0x02 ? 1 : 0; |
| 61 | } |
| 62 | |
| 63 | static int i2c_wait_done(struct i2c_adapter *i2c_adap) |
| 64 | { |
| 65 | int count; |
| 66 | |
| 67 | for (count = 0; count < I2C_WAIT_RETRY; count++) { |
| 68 | if (!i2c_is_busy(i2c_adap)) |
| 69 | break; |
| 70 | udelay(I2C_WAIT_DELAY); |
| 71 | } |
| 72 | |
| 73 | if (I2C_WAIT_RETRY == count) |
| 74 | return 0; |
| 75 | |
| 76 | return 1; |
| 77 | } |
| 78 | |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 79 | static int i2c_sendbytes(struct i2c_adapter *i2c_adap, |
| 80 | const struct i2c_msg *msg, int last) |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 81 | { |
| 82 | struct cx23885_i2c *bus = i2c_adap->algo_data; |
| 83 | struct cx23885_dev *dev = bus->dev; |
| 84 | u32 wdata, addr, ctrl; |
| 85 | int retval, cnt; |
| 86 | |
| 87 | dprintk(1, "%s()\n", __FUNCTION__); |
| 88 | /* Deal with i2c probe functions with zero payload */ |
| 89 | if (msg->len == 0) { |
| 90 | cx_write(bus->reg_addr, msg->addr << 25); |
| 91 | cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2)); |
| 92 | if (!i2c_wait_done(i2c_adap)) |
| 93 | return -EIO; |
| 94 | if (!i2c_slave_did_ack(i2c_adap)) |
| 95 | return -EIO; |
| 96 | |
| 97 | dprintk(1, "%s() returns 0\n", __FUNCTION__); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* dev, reg + first byte */ |
| 103 | addr = (msg->addr << 25) | msg->buf[0]; |
| 104 | wdata = msg->buf[0]; |
| 105 | ctrl = bus->i2c_period | (1 << 12) | (1 << 2); |
| 106 | |
| 107 | if (msg->len > 1) |
| 108 | ctrl |= I2C_NOSTOP | I2C_EXTEND; |
| 109 | |
| 110 | cx_write(bus->reg_addr, addr); |
| 111 | cx_write(bus->reg_wdata, wdata); |
| 112 | cx_write(bus->reg_ctrl, ctrl); |
| 113 | |
| 114 | retval = i2c_wait_done(i2c_adap); |
| 115 | if (retval < 0) |
| 116 | goto err; |
| 117 | if (retval == 0) |
| 118 | goto eio; |
| 119 | if (i2c_debug) { |
| 120 | printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]); |
| 121 | if (!(ctrl & I2C_NOSTOP)) |
| 122 | printk(" >\n"); |
| 123 | } |
| 124 | |
| 125 | for (cnt = 1; cnt < msg->len; cnt++ ) { |
| 126 | /* following bytes */ |
| 127 | wdata = msg->buf[cnt]; |
| 128 | ctrl = bus->i2c_period | (1 << 12) | (1 << 2); |
| 129 | |
| 130 | if (cnt < msg->len-1 || !last) |
| 131 | ctrl |= I2C_NOSTOP | I2C_EXTEND; |
| 132 | |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 133 | cx_write(bus->reg_addr, addr); |
| 134 | cx_write(bus->reg_wdata, wdata); |
| 135 | cx_write(bus->reg_ctrl, ctrl); |
| 136 | |
| 137 | retval = i2c_wait_done(i2c_adap); |
| 138 | if (retval < 0) |
| 139 | goto err; |
| 140 | if (retval == 0) |
| 141 | goto eio; |
| 142 | if (i2c_debug) { |
| 143 | printk(" %02x", msg->buf[cnt]); |
| 144 | if (!(ctrl & I2C_NOSTOP)) |
| 145 | printk(" >\n"); |
| 146 | } |
| 147 | } |
| 148 | return msg->len; |
| 149 | |
| 150 | eio: |
| 151 | retval = -EIO; |
| 152 | err: |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 153 | printk(" ERR: %d\n", retval); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 154 | return retval; |
| 155 | } |
| 156 | |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 157 | static int i2c_readbytes(struct i2c_adapter *i2c_adap, |
| 158 | const struct i2c_msg *msg, int last) |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 159 | { |
| 160 | struct cx23885_i2c *bus = i2c_adap->algo_data; |
| 161 | struct cx23885_dev *dev = bus->dev; |
| 162 | u32 ctrl, cnt; |
| 163 | int retval; |
| 164 | |
| 165 | dprintk(1, "%s()\n", __FUNCTION__); |
| 166 | |
| 167 | /* Deal with i2c probe functions with zero payload */ |
| 168 | if (msg->len == 0) { |
| 169 | cx_write(bus->reg_addr, msg->addr << 25); |
| 170 | cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1); |
| 171 | if (!i2c_wait_done(i2c_adap)) |
| 172 | return -EIO; |
| 173 | if (!i2c_slave_did_ack(i2c_adap)) |
| 174 | return -EIO; |
| 175 | |
| 176 | |
| 177 | dprintk(1, "%s() returns 0\n", __FUNCTION__); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | for(cnt = 0; cnt < msg->len; cnt++) { |
| 182 | |
| 183 | ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1; |
| 184 | |
| 185 | if (cnt < msg->len-1 || !last) |
| 186 | ctrl |= I2C_NOSTOP | I2C_EXTEND; |
| 187 | |
| 188 | cx_write(bus->reg_addr, msg->addr << 25); |
| 189 | cx_write(bus->reg_ctrl, ctrl); |
| 190 | |
| 191 | retval = i2c_wait_done(i2c_adap); |
| 192 | if (retval < 0) |
| 193 | goto err; |
| 194 | if (retval == 0) |
| 195 | goto eio; |
| 196 | msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff; |
| 197 | if (i2c_debug) { |
| 198 | if (!(ctrl & I2C_NOSTOP)) |
| 199 | printk(" <R %02x", (msg->addr << 1) +1); |
| 200 | printk(" =%02x", msg->buf[cnt]); |
| 201 | if (!(ctrl & I2C_NOSTOP)) |
| 202 | printk(" >\n"); |
| 203 | } |
| 204 | } |
| 205 | return msg->len; |
| 206 | |
| 207 | eio: |
| 208 | retval = -EIO; |
| 209 | err: |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 210 | printk(" ERR: %d\n", retval); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 211 | return retval; |
| 212 | } |
| 213 | |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 214 | static int i2c_xfer(struct i2c_adapter *i2c_adap, |
| 215 | struct i2c_msg *msgs, int num) |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 216 | { |
| 217 | struct cx23885_i2c *bus = i2c_adap->algo_data; |
| 218 | struct cx23885_dev *dev = bus->dev; |
| 219 | int i, retval = 0; |
| 220 | |
| 221 | dprintk(1, "%s(num = %d)\n", __FUNCTION__, num); |
| 222 | |
| 223 | for (i = 0 ; i < num; i++) { |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 224 | dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n", |
| 225 | __FUNCTION__, num, msgs[i].addr, msgs[i].len); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 226 | if (msgs[i].flags & I2C_M_RD) { |
| 227 | /* read */ |
| 228 | retval = i2c_readbytes(i2c_adap, &msgs[i], i+1 == num); |
| 229 | if (retval < 0) |
| 230 | goto err; |
| 231 | } else { |
| 232 | /* write */ |
| 233 | retval = i2c_sendbytes(i2c_adap, &msgs[i], i+1 == num); |
| 234 | if (retval < 0) |
| 235 | goto err; |
| 236 | } |
| 237 | } |
| 238 | return num; |
| 239 | |
| 240 | err: |
| 241 | return retval; |
| 242 | } |
| 243 | |
| 244 | static int attach_inform(struct i2c_client *client) |
| 245 | { |
| 246 | struct cx23885_dev *dev = i2c_get_adapdata(client->adapter); |
| 247 | |
| 248 | dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n", |
| 249 | client->driver->driver.name, client->addr, client->name); |
| 250 | |
| 251 | if (!client->driver->command) |
| 252 | return 0; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int detach_inform(struct i2c_client *client) |
| 258 | { |
| 259 | struct cx23885_dev *dev = i2c_get_adapdata(client->adapter); |
| 260 | |
| 261 | dprintk(1, "i2c detach [client=%s]\n", client->name); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 266 | void cx23885_call_i2c_clients(struct cx23885_i2c *bus, |
| 267 | unsigned int cmd, void *arg) |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 268 | { |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 269 | if (bus->i2c_rc != 0) |
| 270 | return; |
| 271 | |
Michael Krufky | 60a41d3 | 2007-03-20 23:03:52 -0300 | [diff] [blame] | 272 | i2c_clients_command(&bus->i2c_adap, cmd, arg); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int cx23885_algo_control(struct i2c_adapter *adap, |
| 276 | unsigned int cmd, unsigned long arg) |
| 277 | { |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static u32 cx23885_functionality(struct i2c_adapter *adap) |
| 282 | { |
Steven Toth | 0fc0739 | 2007-03-19 18:03:03 -0300 | [diff] [blame] | 283 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static struct i2c_algorithm cx23885_i2c_algo_template = { |
| 287 | .master_xfer = i2c_xfer, |
| 288 | .algo_control = cx23885_algo_control, |
| 289 | .functionality = cx23885_functionality, |
| 290 | }; |
| 291 | |
| 292 | /* ----------------------------------------------------------------------- */ |
| 293 | |
| 294 | static struct i2c_adapter cx23885_i2c_adap_template = { |
| 295 | .name = "cx23885", |
| 296 | .owner = THIS_MODULE, |
| 297 | .id = I2C_HW_B_CX23885, |
| 298 | .algo = &cx23885_i2c_algo_template, |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 299 | .client_register = attach_inform, |
| 300 | .client_unregister = detach_inform, |
| 301 | }; |
| 302 | |
| 303 | static struct i2c_client cx23885_i2c_client_template = { |
| 304 | .name = "cx23885 internal", |
| 305 | }; |
| 306 | |
| 307 | static char *i2c_devs[128] = { |
| 308 | [ 0x32 >> 1 ] = "cx24227", |
| 309 | [ 0x88 >> 1 ] = "cx25837", |
| 310 | [ 0x84 >> 1 ] = "tda8295", |
| 311 | [ 0xa0 >> 1 ] = "eeprom", |
| 312 | [ 0xc0 >> 1 ] = "mt2131/tda8275", |
| 313 | [ 0xc2 >> 1 ] = "mt2131/tda8275", |
| 314 | }; |
| 315 | |
| 316 | static void do_i2c_scan(char *name, struct i2c_client *c) |
| 317 | { |
| 318 | unsigned char buf; |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 319 | int i, rc; |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 320 | |
| 321 | for (i = 0; i < 128; i++) { |
| 322 | c->addr = i; |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 323 | rc = i2c_master_recv(c, &buf, 0); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 324 | if (rc < 0) |
| 325 | continue; |
| 326 | printk("%s: i2c scan: found device @ 0x%x [%s]\n", |
| 327 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* init + register i2c algo-bit adapter */ |
| 332 | int cx23885_i2c_register(struct cx23885_i2c *bus) |
| 333 | { |
| 334 | struct cx23885_dev *dev = bus->dev; |
| 335 | |
| 336 | dprintk(1, "%s(bus = %d)\n", __FUNCTION__, bus->nr); |
| 337 | |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 338 | memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template, |
| 339 | sizeof(bus->i2c_adap)); |
| 340 | memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template, |
| 341 | sizeof(bus->i2c_algo)); |
| 342 | memcpy(&bus->i2c_client, &cx23885_i2c_client_template, |
| 343 | sizeof(bus->i2c_client)); |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 344 | |
| 345 | bus->i2c_adap.dev.parent = &dev->pci->dev; |
| 346 | |
Michael Krufky | 44a6481 | 2007-03-20 23:00:18 -0300 | [diff] [blame] | 347 | strlcpy(bus->i2c_adap.name, bus->dev->name, |
| 348 | sizeof(bus->i2c_adap.name)); |
Steven Toth | 2e52f21 | 2007-09-04 21:32:41 -0300 | [diff] [blame^] | 349 | |
Steven Toth | d19770e | 2007-03-11 20:44:05 -0300 | [diff] [blame] | 350 | bus->i2c_algo.data = bus; |
| 351 | bus->i2c_adap.algo_data = bus; |
| 352 | i2c_add_adapter(&bus->i2c_adap); |
| 353 | |
| 354 | bus->i2c_client.adapter = &bus->i2c_adap; |
| 355 | |
| 356 | if (0 == bus->i2c_rc) { |
| 357 | printk("%s: i2c bus %d registered\n", dev->name, bus->nr); |
| 358 | if (i2c_scan) |
| 359 | do_i2c_scan(dev->name, &bus->i2c_client); |
| 360 | } else |
| 361 | printk("%s: i2c bus %d register FAILED\n", dev->name, bus->nr); |
| 362 | |
| 363 | return bus->i2c_rc; |
| 364 | } |
| 365 | |
| 366 | int cx23885_i2c_unregister(struct cx23885_i2c *bus) |
| 367 | { |
| 368 | i2c_del_adapter(&bus->i2c_adap); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /* ----------------------------------------------------------------------- */ |
| 373 | |
| 374 | EXPORT_SYMBOL(cx23885_call_i2c_clients); |
| 375 | |
| 376 | /* |
| 377 | * Local variables: |
| 378 | * c-basic-offset: 8 |
| 379 | * End: |
| 380 | */ |