Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Conexant CX25821 PCIe bridge |
| 3 | * |
| 4 | * Copyright (C) 2009 Conexant Systems Inc. |
| 5 | * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com> |
| 6 | * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver |
| 7 | * |
| 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 | * |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
Joe Perches | 36d89f7 | 2010-11-07 17:48:21 -0300 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
Hans Verkuil | 89c2138 | 2013-04-13 08:01:31 -0300 | [diff] [blame] | 26 | #include <linux/module.h> |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 27 | #include <linux/i2c.h> |
Hans Verkuil | 89c2138 | 2013-04-13 08:01:31 -0300 | [diff] [blame] | 28 | #include "cx25821.h" |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 29 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 30 | static unsigned int i2c_debug; |
| 31 | module_param(i2c_debug, int, 0644); |
| 32 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); |
| 33 | |
sai | bde4301 | 2010-03-20 02:34:22 -0300 | [diff] [blame] | 34 | static unsigned int i2c_scan; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 35 | module_param(i2c_scan, int, 0444); |
| 36 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); |
| 37 | |
Joe Perches | 36d89f7 | 2010-11-07 17:48:21 -0300 | [diff] [blame] | 38 | #define dprintk(level, fmt, arg...) \ |
| 39 | do { \ |
| 40 | if (i2c_debug >= level) \ |
| 41 | printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \ |
| 42 | } while (0) |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 43 | |
| 44 | #define I2C_WAIT_DELAY 32 |
| 45 | #define I2C_WAIT_RETRY 64 |
| 46 | |
| 47 | #define I2C_EXTEND (1 << 3) |
| 48 | #define I2C_NOSTOP (1 << 4) |
| 49 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 50 | static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap) |
| 51 | { |
| 52 | struct cx25821_i2c *bus = i2c_adap->algo_data; |
| 53 | struct cx25821_dev *dev = bus->dev; |
| 54 | return cx_read(bus->reg_stat) & 0x01; |
| 55 | } |
| 56 | |
| 57 | static inline int i2c_is_busy(struct i2c_adapter *i2c_adap) |
| 58 | { |
| 59 | struct cx25821_i2c *bus = i2c_adap->algo_data; |
| 60 | struct cx25821_dev *dev = bus->dev; |
| 61 | return cx_read(bus->reg_stat) & 0x02 ? 1 : 0; |
| 62 | } |
| 63 | |
| 64 | static int i2c_wait_done(struct i2c_adapter *i2c_adap) |
| 65 | { |
| 66 | int count; |
| 67 | |
| 68 | for (count = 0; count < I2C_WAIT_RETRY; count++) { |
| 69 | if (!i2c_is_busy(i2c_adap)) |
| 70 | break; |
| 71 | udelay(I2C_WAIT_DELAY); |
| 72 | } |
| 73 | |
| 74 | if (I2C_WAIT_RETRY == count) |
| 75 | return 0; |
| 76 | |
| 77 | return 1; |
| 78 | } |
| 79 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 80 | static int i2c_sendbytes(struct i2c_adapter *i2c_adap, |
| 81 | const struct i2c_msg *msg, int joined_rlen) |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 82 | { |
| 83 | struct cx25821_i2c *bus = i2c_adap->algo_data; |
| 84 | struct cx25821_dev *dev = bus->dev; |
| 85 | u32 wdata, addr, ctrl; |
| 86 | int retval, cnt; |
| 87 | |
| 88 | if (joined_rlen) |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 89 | dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__, |
| 90 | msg->len, joined_rlen); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 91 | else |
| 92 | dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len); |
| 93 | |
| 94 | /* Deal with i2c probe functions with zero payload */ |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 95 | if (msg->len == 0) { |
Mauro Carvalho Chehab | bb4c9a7 | 2009-09-13 11:25:45 -0300 | [diff] [blame] | 96 | cx_write(bus->reg_addr, msg->addr << 25); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 97 | cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2)); |
| 98 | |
| 99 | if (!i2c_wait_done(i2c_adap)) |
| 100 | return -EIO; |
| 101 | |
| 102 | if (!i2c_slave_did_ack(i2c_adap)) |
| 103 | return -EIO; |
| 104 | |
Joe Perches | 36d89f7 | 2010-11-07 17:48:21 -0300 | [diff] [blame] | 105 | dprintk(1, "%s(): returns 0\n", __func__); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* dev, reg + first byte */ |
| 110 | addr = (msg->addr << 25) | msg->buf[0]; |
| 111 | wdata = msg->buf[0]; |
Mauro Carvalho Chehab | bb4c9a7 | 2009-09-13 11:25:45 -0300 | [diff] [blame] | 112 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 113 | ctrl = bus->i2c_period | (1 << 12) | (1 << 2); |
| 114 | |
| 115 | if (msg->len > 1) |
| 116 | ctrl |= I2C_NOSTOP | I2C_EXTEND; |
| 117 | else if (joined_rlen) |
| 118 | ctrl |= I2C_NOSTOP; |
| 119 | |
| 120 | cx_write(bus->reg_addr, addr); |
| 121 | cx_write(bus->reg_wdata, wdata); |
| 122 | cx_write(bus->reg_ctrl, ctrl); |
| 123 | |
| 124 | retval = i2c_wait_done(i2c_adap); |
| 125 | if (retval < 0) |
| 126 | goto err; |
| 127 | |
| 128 | if (retval == 0) |
| 129 | goto eio; |
| 130 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 131 | if (i2c_debug) { |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 132 | if (!(ctrl & I2C_NOSTOP)) |
| 133 | printk(" >\n"); |
| 134 | } |
| 135 | |
| 136 | for (cnt = 1; cnt < msg->len; cnt++) { |
| 137 | /* following bytes */ |
| 138 | wdata = msg->buf[cnt]; |
| 139 | ctrl = bus->i2c_period | (1 << 12) | (1 << 2); |
| 140 | |
| 141 | if (cnt < msg->len - 1) |
| 142 | ctrl |= I2C_NOSTOP | I2C_EXTEND; |
| 143 | else if (joined_rlen) |
| 144 | ctrl |= I2C_NOSTOP; |
| 145 | |
| 146 | cx_write(bus->reg_addr, addr); |
| 147 | cx_write(bus->reg_wdata, wdata); |
| 148 | cx_write(bus->reg_ctrl, ctrl); |
| 149 | |
| 150 | retval = i2c_wait_done(i2c_adap); |
| 151 | if (retval < 0) |
| 152 | goto err; |
| 153 | |
| 154 | if (retval == 0) |
| 155 | goto eio; |
| 156 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 157 | if (i2c_debug) { |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 158 | dprintk(1, " %02x", msg->buf[cnt]); |
| 159 | if (!(ctrl & I2C_NOSTOP)) |
| 160 | dprintk(1, " >\n"); |
| 161 | } |
| 162 | } |
Mauro Carvalho Chehab | bb4c9a7 | 2009-09-13 11:25:45 -0300 | [diff] [blame] | 163 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 164 | return msg->len; |
| 165 | |
Olimpiu Pascariu | 51e9dd3 | 2010-03-21 14:52:31 -0300 | [diff] [blame] | 166 | eio: |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 167 | retval = -EIO; |
Olimpiu Pascariu | 51e9dd3 | 2010-03-21 14:52:31 -0300 | [diff] [blame] | 168 | err: |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 169 | if (i2c_debug) |
Joe Perches | 36d89f7 | 2010-11-07 17:48:21 -0300 | [diff] [blame] | 170 | pr_err(" ERR: %d\n", retval); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 171 | return retval; |
| 172 | } |
| 173 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 174 | static int i2c_readbytes(struct i2c_adapter *i2c_adap, |
| 175 | const struct i2c_msg *msg, int joined) |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 176 | { |
| 177 | struct cx25821_i2c *bus = i2c_adap->algo_data; |
| 178 | struct cx25821_dev *dev = bus->dev; |
| 179 | u32 ctrl, cnt; |
| 180 | int retval; |
| 181 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 182 | if (i2c_debug && !joined) |
| 183 | dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len); |
| 184 | |
| 185 | /* Deal with i2c probe functions with zero payload */ |
| 186 | if (msg->len == 0) { |
| 187 | cx_write(bus->reg_addr, msg->addr << 25); |
| 188 | cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1); |
| 189 | if (!i2c_wait_done(i2c_adap)) |
| 190 | return -EIO; |
| 191 | if (!i2c_slave_did_ack(i2c_adap)) |
| 192 | return -EIO; |
| 193 | |
Joe Perches | 36d89f7 | 2010-11-07 17:48:21 -0300 | [diff] [blame] | 194 | dprintk(1, "%s(): returns 0\n", __func__); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | if (i2c_debug) { |
| 199 | if (joined) |
| 200 | dprintk(1, " R"); |
| 201 | else |
| 202 | dprintk(1, " <R %02x", (msg->addr << 1) + 1); |
| 203 | } |
| 204 | |
| 205 | for (cnt = 0; cnt < msg->len; cnt++) { |
| 206 | |
| 207 | ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1; |
| 208 | |
| 209 | if (cnt < msg->len - 1) |
| 210 | ctrl |= I2C_NOSTOP | I2C_EXTEND; |
| 211 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 212 | cx_write(bus->reg_addr, msg->addr << 25); |
| 213 | cx_write(bus->reg_ctrl, ctrl); |
| 214 | |
| 215 | retval = i2c_wait_done(i2c_adap); |
| 216 | if (retval < 0) |
| 217 | goto err; |
| 218 | if (retval == 0) |
| 219 | goto eio; |
| 220 | msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff; |
| 221 | |
| 222 | if (i2c_debug) { |
| 223 | dprintk(1, " %02x", msg->buf[cnt]); |
| 224 | if (!(ctrl & I2C_NOSTOP)) |
| 225 | dprintk(1, " >\n"); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return msg->len; |
Olimpiu Pascariu | 51e9dd3 | 2010-03-21 14:52:31 -0300 | [diff] [blame] | 230 | eio: |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 231 | retval = -EIO; |
Olimpiu Pascariu | 51e9dd3 | 2010-03-21 14:52:31 -0300 | [diff] [blame] | 232 | err: |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 233 | if (i2c_debug) |
Joe Perches | 36d89f7 | 2010-11-07 17:48:21 -0300 | [diff] [blame] | 234 | pr_err(" ERR: %d\n", retval); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 235 | return retval; |
| 236 | } |
| 237 | |
| 238 | static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num) |
| 239 | { |
| 240 | struct cx25821_i2c *bus = i2c_adap->algo_data; |
| 241 | struct cx25821_dev *dev = bus->dev; |
| 242 | int i, retval = 0; |
| 243 | |
| 244 | dprintk(1, "%s(num = %d)\n", __func__, num); |
| 245 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 246 | for (i = 0; i < num; i++) { |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 247 | dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n", |
| 248 | __func__, num, msgs[i].addr, msgs[i].len); |
| 249 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 250 | if (msgs[i].flags & I2C_M_RD) { |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 251 | /* read */ |
| 252 | retval = i2c_readbytes(i2c_adap, &msgs[i], 0); |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 253 | } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && |
| 254 | msgs[i].addr == msgs[i + 1].addr) { |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 255 | /* write then read from same address */ |
Leonid V. Fedorenchik | 654d075 | 2011-10-22 01:43:34 -0300 | [diff] [blame] | 256 | retval = i2c_sendbytes(i2c_adap, &msgs[i], |
| 257 | msgs[i + 1].len); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 258 | |
| 259 | if (retval < 0) |
| 260 | goto err; |
| 261 | i++; |
| 262 | retval = i2c_readbytes(i2c_adap, &msgs[i], 1); |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 263 | } else { |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 264 | /* write */ |
| 265 | retval = i2c_sendbytes(i2c_adap, &msgs[i], 0); |
| 266 | } |
Mauro Carvalho Chehab | bb4c9a7 | 2009-09-13 11:25:45 -0300 | [diff] [blame] | 267 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 268 | if (retval < 0) |
| 269 | goto err; |
| 270 | } |
| 271 | return num; |
| 272 | |
Olimpiu Pascariu | 51e9dd3 | 2010-03-21 14:52:31 -0300 | [diff] [blame] | 273 | err: |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 274 | return retval; |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 275 | } |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 276 | |
| 277 | |
| 278 | static u32 cx25821_functionality(struct i2c_adapter *adap) |
| 279 | { |
Leonid V. Fedorenchik | 654d075 | 2011-10-22 01:43:34 -0300 | [diff] [blame] | 280 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WORD_DATA | |
| 281 | I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static struct i2c_algorithm cx25821_i2c_algo_template = { |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 285 | .master_xfer = i2c_xfer, |
| 286 | .functionality = cx25821_functionality, |
Palash Bandyopadhyay | 6d8c2ba | 2010-07-04 14:15:38 -0300 | [diff] [blame] | 287 | #ifdef NEED_ALGO_CONTROL |
Ruslan Pisarev | e4115bb | 2010-09-27 10:01:36 -0300 | [diff] [blame] | 288 | .algo_control = dummy_algo_control, |
Palash Bandyopadhyay | 6d8c2ba | 2010-07-04 14:15:38 -0300 | [diff] [blame] | 289 | #endif |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 290 | }; |
| 291 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 292 | static struct i2c_adapter cx25821_i2c_adap_template = { |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 293 | .name = "cx25821", |
| 294 | .owner = THIS_MODULE, |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 295 | .algo = &cx25821_i2c_algo_template, |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | static struct i2c_client cx25821_i2c_client_template = { |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 299 | .name = "cx25821 internal", |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 300 | }; |
| 301 | |
Jean Delvare | a824f0f | 2011-11-09 13:27:53 -0300 | [diff] [blame] | 302 | /* init + register i2c adapter */ |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 303 | int cx25821_i2c_register(struct cx25821_i2c *bus) |
| 304 | { |
| 305 | struct cx25821_dev *dev = bus->dev; |
| 306 | |
| 307 | dprintk(1, "%s(bus = %d)\n", __func__, bus->nr); |
| 308 | |
Ezequiel GarcÃa | c21813e | 2012-06-27 12:52:54 -0300 | [diff] [blame] | 309 | bus->i2c_adap = cx25821_i2c_adap_template; |
| 310 | bus->i2c_client = cx25821_i2c_client_template; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 311 | bus->i2c_adap.dev.parent = &dev->pci->dev; |
| 312 | |
| 313 | strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name)); |
| 314 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 315 | bus->i2c_adap.algo_data = bus; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 316 | i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev); |
| 317 | i2c_add_adapter(&bus->i2c_adap); |
| 318 | |
| 319 | bus->i2c_client.adapter = &bus->i2c_adap; |
| 320 | |
Olimpiu Pascariu | 51e9dd3 | 2010-03-21 14:52:31 -0300 | [diff] [blame] | 321 | /* set up the I2c */ |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 322 | bus->i2c_client.addr = (0x88 >> 1); |
Mauro Carvalho Chehab | bb4c9a7 | 2009-09-13 11:25:45 -0300 | [diff] [blame] | 323 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 324 | return bus->i2c_rc; |
| 325 | } |
| 326 | |
| 327 | int cx25821_i2c_unregister(struct cx25821_i2c *bus) |
| 328 | { |
| 329 | i2c_del_adapter(&bus->i2c_adap); |
| 330 | return 0; |
| 331 | } |
| 332 | |
Mauro Carvalho Chehab | dafc456 | 2012-10-27 12:42:59 -0300 | [diff] [blame] | 333 | #if 0 /* Currently unused */ |
| 334 | static void cx25821_av_clk(struct cx25821_dev *dev, int enable) |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 335 | { |
| 336 | /* write 0 to bus 2 addr 0x144 via i2x_xfer() */ |
| 337 | char buffer[3]; |
| 338 | struct i2c_msg msg; |
| 339 | dprintk(1, "%s(enabled = %d)\n", __func__, enable); |
| 340 | |
| 341 | /* Register 0x144 */ |
| 342 | buffer[0] = 0x01; |
| 343 | buffer[1] = 0x44; |
| 344 | if (enable == 1) |
| 345 | buffer[2] = 0x05; |
| 346 | else |
| 347 | buffer[2] = 0x00; |
| 348 | |
| 349 | msg.addr = 0x44; |
| 350 | msg.flags = I2C_M_TEN; |
| 351 | msg.len = 3; |
| 352 | msg.buf = buffer; |
| 353 | |
| 354 | i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1); |
| 355 | } |
Mauro Carvalho Chehab | dafc456 | 2012-10-27 12:42:59 -0300 | [diff] [blame] | 356 | #endif |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 357 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 358 | int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value) |
| 359 | { |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 360 | struct i2c_client *client = &bus->i2c_client; |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 361 | int v = 0; |
| 362 | u8 addr[2] = { 0, 0 }; |
| 363 | u8 buf[4] = { 0, 0, 0, 0 }; |
Mauro Carvalho Chehab | bb4c9a7 | 2009-09-13 11:25:45 -0300 | [diff] [blame] | 364 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 365 | struct i2c_msg msgs[2] = { |
| 366 | { |
Leonid V. Fedorenchik | 7bfcbd6 | 2011-09-02 11:55:36 +0800 | [diff] [blame] | 367 | .addr = client->addr, |
| 368 | .flags = 0, |
| 369 | .len = 2, |
| 370 | .buf = addr, |
| 371 | }, { |
| 372 | .addr = client->addr, |
| 373 | .flags = I2C_M_RD, |
| 374 | .len = 4, |
| 375 | .buf = buf, |
| 376 | } |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 377 | }; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 378 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 379 | addr[0] = (reg_addr >> 8); |
| 380 | addr[1] = (reg_addr & 0xff); |
| 381 | msgs[0].addr = 0x44; |
| 382 | msgs[1].addr = 0x44; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 383 | |
Hans Verkuil | 30fdf03 | 2012-04-20 06:26:19 -0300 | [diff] [blame] | 384 | i2c_xfer(client->adapter, msgs, 2); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 385 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 386 | v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 387 | *value = v; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 388 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 389 | return v; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 390 | } |
| 391 | |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 392 | int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value) |
| 393 | { |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 394 | struct i2c_client *client = &bus->i2c_client; |
| 395 | int retval = 0; |
| 396 | u8 buf[6] = { 0, 0, 0, 0, 0, 0 }; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 397 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 398 | struct i2c_msg msgs[1] = { |
| 399 | { |
Leonid V. Fedorenchik | 7bfcbd6 | 2011-09-02 11:55:36 +0800 | [diff] [blame] | 400 | .addr = client->addr, |
| 401 | .flags = 0, |
| 402 | .len = 6, |
| 403 | .buf = buf, |
| 404 | } |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 405 | }; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 406 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 407 | buf[0] = reg_addr >> 8; |
| 408 | buf[1] = reg_addr & 0xff; |
| 409 | buf[5] = (value >> 24) & 0xff; |
| 410 | buf[4] = (value >> 16) & 0xff; |
| 411 | buf[3] = (value >> 8) & 0xff; |
| 412 | buf[2] = value & 0xff; |
| 413 | client->flags = 0; |
| 414 | msgs[0].addr = 0x44; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 415 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 416 | retval = i2c_xfer(client->adapter, msgs, 1); |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 417 | |
Mauro Carvalho Chehab | 1a9fc85 | 2009-09-13 11:30:11 -0300 | [diff] [blame] | 418 | return retval; |
Mauro Carvalho Chehab | 02b20b0 | 2009-09-15 11:33:54 -0300 | [diff] [blame] | 419 | } |