Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 1 | #include <linux/i2c.h> |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 2 | #include <linux/mutex.h> |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 3 | |
| 4 | #include "dibx000_common.h" |
| 5 | |
| 6 | static int debug; |
| 7 | module_param(debug, int, 0644); |
| 8 | MODULE_PARM_DESC(debug, "turn on debugging (default: 0)"); |
| 9 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 10 | #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiBX000: "); printk(args); printk("\n"); } } while (0) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 11 | |
| 12 | static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val) |
| 13 | { |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 14 | int ret; |
| 15 | |
| 16 | if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { |
| 17 | dprintk("could not acquire lock"); |
| 18 | return -EINVAL; |
| 19 | } |
| 20 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 21 | mst->i2c_write_buffer[0] = (reg >> 8) & 0xff; |
| 22 | mst->i2c_write_buffer[1] = reg & 0xff; |
| 23 | mst->i2c_write_buffer[2] = (val >> 8) & 0xff; |
| 24 | mst->i2c_write_buffer[3] = val & 0xff; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 25 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 26 | memset(mst->msg, 0, sizeof(struct i2c_msg)); |
| 27 | mst->msg[0].addr = mst->i2c_addr; |
| 28 | mst->msg[0].flags = 0; |
| 29 | mst->msg[0].buf = mst->i2c_write_buffer; |
| 30 | mst->msg[0].len = 4; |
| 31 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 32 | ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0; |
| 33 | mutex_unlock(&mst->i2c_buffer_lock); |
| 34 | |
| 35 | return ret; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 36 | } |
| 37 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 38 | static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg) |
| 39 | { |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 40 | u16 ret; |
| 41 | |
| 42 | if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { |
| 43 | dprintk("could not acquire lock"); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 47 | mst->i2c_write_buffer[0] = reg >> 8; |
| 48 | mst->i2c_write_buffer[1] = reg & 0xff; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 49 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 50 | memset(mst->msg, 0, 2 * sizeof(struct i2c_msg)); |
| 51 | mst->msg[0].addr = mst->i2c_addr; |
| 52 | mst->msg[0].flags = 0; |
| 53 | mst->msg[0].buf = mst->i2c_write_buffer; |
| 54 | mst->msg[0].len = 2; |
| 55 | mst->msg[1].addr = mst->i2c_addr; |
| 56 | mst->msg[1].flags = I2C_M_RD; |
| 57 | mst->msg[1].buf = mst->i2c_read_buffer; |
| 58 | mst->msg[1].len = 2; |
| 59 | |
| 60 | if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2) |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 61 | dprintk("i2c read error on %d", reg); |
| 62 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 63 | ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1]; |
| 64 | mutex_unlock(&mst->i2c_buffer_lock); |
| 65 | |
| 66 | return ret; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst) |
| 70 | { |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 71 | int i = 100; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 72 | u16 status; |
| 73 | |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 74 | while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0) |
| 75 | ; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 76 | |
| 77 | /* i2c timed out */ |
| 78 | if (i == 0) |
| 79 | return -EREMOTEIO; |
| 80 | |
| 81 | /* no acknowledge */ |
| 82 | if ((status & 0x0080) == 0) |
| 83 | return -EREMOTEIO; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop) |
| 89 | { |
| 90 | u16 data; |
| 91 | u16 da; |
| 92 | u16 i; |
| 93 | u16 txlen = msg->len, len; |
| 94 | const u8 *b = msg->buf; |
| 95 | |
| 96 | while (txlen) { |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 97 | dibx000_read_word(mst, mst->base_reg + 2); |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 98 | |
| 99 | len = txlen > 8 ? 8 : txlen; |
| 100 | for (i = 0; i < len; i += 2) { |
| 101 | data = *b++ << 8; |
| 102 | if (i+1 < len) |
| 103 | data |= *b++; |
| 104 | dibx000_write_word(mst, mst->base_reg, data); |
| 105 | } |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 106 | da = (((u8) (msg->addr)) << 9) | |
| 107 | (1 << 8) | |
| 108 | (1 << 7) | |
| 109 | (0 << 6) | |
| 110 | (0 << 5) | |
| 111 | ((len & 0x7) << 2) | |
| 112 | (0 << 1) | |
| 113 | (0 << 0); |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 114 | |
| 115 | if (txlen == msg->len) |
| 116 | da |= 1 << 5; /* start */ |
| 117 | |
| 118 | if (txlen-len == 0 && stop) |
| 119 | da |= 1 << 6; /* stop */ |
| 120 | |
| 121 | dibx000_write_word(mst, mst->base_reg+1, da); |
| 122 | |
| 123 | if (dibx000_is_i2c_done(mst) != 0) |
| 124 | return -EREMOTEIO; |
| 125 | txlen -= len; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg) |
| 132 | { |
| 133 | u16 da; |
| 134 | u8 *b = msg->buf; |
| 135 | u16 rxlen = msg->len, len; |
| 136 | |
| 137 | while (rxlen) { |
| 138 | len = rxlen > 8 ? 8 : rxlen; |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 139 | da = (((u8) (msg->addr)) << 9) | |
| 140 | (1 << 8) | |
| 141 | (1 << 7) | |
| 142 | (0 << 6) | |
| 143 | (0 << 5) | |
| 144 | ((len & 0x7) << 2) | |
| 145 | (1 << 1) | |
| 146 | (0 << 0); |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 147 | |
| 148 | if (rxlen == msg->len) |
| 149 | da |= 1 << 5; /* start */ |
| 150 | |
| 151 | if (rxlen-len == 0) |
| 152 | da |= 1 << 6; /* stop */ |
| 153 | dibx000_write_word(mst, mst->base_reg+1, da); |
| 154 | |
| 155 | if (dibx000_is_i2c_done(mst) != 0) |
| 156 | return -EREMOTEIO; |
| 157 | |
| 158 | rxlen -= len; |
| 159 | |
| 160 | while (len) { |
| 161 | da = dibx000_read_word(mst, mst->base_reg); |
| 162 | *b++ = (da >> 8) & 0xff; |
| 163 | len--; |
| 164 | if (len >= 1) { |
| 165 | *b++ = da & 0xff; |
| 166 | len--; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed) |
| 175 | { |
| 176 | struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap); |
| 177 | |
| 178 | if (mst->device_rev < DIB7000MC && speed < 235) |
| 179 | speed = 235; |
| 180 | return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed)); |
| 181 | |
| 182 | } |
| 183 | EXPORT_SYMBOL(dibx000_i2c_set_speed); |
| 184 | |
| 185 | static u32 dibx000_i2c_func(struct i2c_adapter *adapter) |
| 186 | { |
| 187 | return I2C_FUNC_I2C; |
| 188 | } |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 189 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 190 | static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst, |
| 191 | enum dibx000_i2c_interface intf) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 192 | { |
| 193 | if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 194 | dprintk("selecting interface: %d", intf); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 195 | mst->selected_interface = intf; |
| 196 | return dibx000_write_word(mst, mst->base_reg + 4, intf); |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 201 | static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num) |
| 202 | { |
| 203 | struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap); |
| 204 | int msg_index; |
| 205 | int ret = 0; |
| 206 | |
| 207 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2); |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 208 | for (msg_index = 0; msg_index < num; msg_index++) { |
| 209 | if (msg[msg_index].flags & I2C_M_RD) { |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 210 | ret = dibx000_master_i2c_read(mst, &msg[msg_index]); |
| 211 | if (ret != 0) |
| 212 | return 0; |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 213 | } else { |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 214 | ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1); |
| 215 | if (ret != 0) |
| 216 | return 0; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return num; |
| 221 | } |
| 222 | |
| 223 | static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num) |
| 224 | { |
| 225 | struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap); |
| 226 | int msg_index; |
| 227 | int ret = 0; |
| 228 | |
| 229 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4); |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 230 | for (msg_index = 0; msg_index < num; msg_index++) { |
| 231 | if (msg[msg_index].flags & I2C_M_RD) { |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 232 | ret = dibx000_master_i2c_read(mst, &msg[msg_index]); |
| 233 | if (ret != 0) |
| 234 | return 0; |
Olivier Grenie | b4d6046e | 2011-01-04 13:08:14 -0300 | [diff] [blame] | 235 | } else { |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 236 | ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1); |
| 237 | if (ret != 0) |
| 238 | return 0; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return num; |
| 243 | } |
| 244 | |
| 245 | static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = { |
| 246 | .master_xfer = dibx000_i2c_master_xfer_gpio12, |
| 247 | .functionality = dibx000_i2c_func, |
| 248 | }; |
| 249 | |
| 250 | static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = { |
| 251 | .master_xfer = dibx000_i2c_master_xfer_gpio34, |
| 252 | .functionality = dibx000_i2c_func, |
| 253 | }; |
| 254 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 255 | static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4], |
| 256 | u8 addr, int onoff) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 257 | { |
| 258 | u16 val; |
| 259 | |
| 260 | |
| 261 | if (onoff) |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 262 | val = addr << 8; // bit 7 = use master or not, if 0, the gate is open |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 263 | else |
| 264 | val = 1 << 7; |
| 265 | |
| 266 | if (mst->device_rev > DIB7000) |
| 267 | val <<= 1; |
| 268 | |
| 269 | tx[0] = (((mst->base_reg + 1) >> 8) & 0xff); |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 270 | tx[1] = ((mst->base_reg + 1) & 0xff); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 271 | tx[2] = val >> 8; |
| 272 | tx[3] = val & 0xff; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 277 | static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap, |
| 278 | struct i2c_msg msg[], int num) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 279 | { |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 280 | struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap); |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 281 | int ret; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 282 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 283 | if (num > 32) { |
| 284 | dprintk("%s: too much I2C message to be transmitted (%i).\ |
| 285 | Maximum is 32", __func__, num); |
| 286 | return -ENOMEM; |
| 287 | } |
| 288 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 289 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7); |
| 290 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 291 | if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { |
| 292 | dprintk("could not acquire lock"); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
| 296 | memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num)); |
| 297 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 298 | /* open the gate */ |
| 299 | dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1); |
| 300 | mst->msg[0].addr = mst->i2c_addr; |
| 301 | mst->msg[0].buf = &mst->i2c_write_buffer[0]; |
| 302 | mst->msg[0].len = 4; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 303 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 304 | memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num); |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 305 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 306 | /* close the gate */ |
| 307 | dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0); |
| 308 | mst->msg[num + 1].addr = mst->i2c_addr; |
| 309 | mst->msg[num + 1].buf = &mst->i2c_write_buffer[4]; |
| 310 | mst->msg[num + 1].len = 4; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 311 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 312 | ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? |
| 313 | num : -EIO); |
| 314 | |
| 315 | mutex_unlock(&mst->i2c_buffer_lock); |
| 316 | return ret; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 317 | } |
| 318 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 319 | static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = { |
| 320 | .master_xfer = dibx000_i2c_gated_gpio67_xfer, |
| 321 | .functionality = dibx000_i2c_func, |
| 322 | }; |
| 323 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 324 | static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap, |
| 325 | struct i2c_msg msg[], int num) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 326 | { |
| 327 | struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap); |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 328 | int ret; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 329 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 330 | if (num > 32) { |
| 331 | dprintk("%s: too much I2C message to be transmitted (%i).\ |
| 332 | Maximum is 32", __func__, num); |
| 333 | return -ENOMEM; |
| 334 | } |
| 335 | |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 336 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER); |
| 337 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 338 | if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { |
| 339 | dprintk("could not acquire lock"); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num)); |
| 343 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 344 | /* open the gate */ |
| 345 | dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1); |
| 346 | mst->msg[0].addr = mst->i2c_addr; |
| 347 | mst->msg[0].buf = &mst->i2c_write_buffer[0]; |
| 348 | mst->msg[0].len = 4; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 349 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 350 | memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 351 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 352 | /* close the gate */ |
| 353 | dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0); |
| 354 | mst->msg[num + 1].addr = mst->i2c_addr; |
| 355 | mst->msg[num + 1].buf = &mst->i2c_write_buffer[4]; |
| 356 | mst->msg[num + 1].len = 4; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 357 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 358 | ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? |
| 359 | num : -EIO); |
| 360 | mutex_unlock(&mst->i2c_buffer_lock); |
| 361 | return ret; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = { |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 365 | .master_xfer = dibx000_i2c_gated_tuner_xfer, |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 366 | .functionality = dibx000_i2c_func, |
| 367 | }; |
| 368 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 369 | struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst, |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 370 | enum dibx000_i2c_interface intf, |
| 371 | int gating) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 372 | { |
| 373 | struct i2c_adapter *i2c = NULL; |
| 374 | |
| 375 | switch (intf) { |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 376 | case DIBX000_I2C_INTERFACE_TUNER: |
| 377 | if (gating) |
| 378 | i2c = &mst->gated_tuner_i2c_adap; |
| 379 | break; |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 380 | case DIBX000_I2C_INTERFACE_GPIO_1_2: |
| 381 | if (!gating) |
| 382 | i2c = &mst->master_i2c_adap_gpio12; |
| 383 | break; |
| 384 | case DIBX000_I2C_INTERFACE_GPIO_3_4: |
| 385 | if (!gating) |
| 386 | i2c = &mst->master_i2c_adap_gpio34; |
| 387 | break; |
| 388 | case DIBX000_I2C_INTERFACE_GPIO_6_7: |
| 389 | if (gating) |
| 390 | i2c = &mst->master_i2c_adap_gpio67; |
| 391 | break; |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 392 | default: |
| 393 | printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n"); |
| 394 | break; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return i2c; |
| 398 | } |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 399 | |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 400 | EXPORT_SYMBOL(dibx000_get_i2c_adapter); |
| 401 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 402 | void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst) |
| 403 | { |
| 404 | /* initialize the i2c-master by closing the gate */ |
| 405 | u8 tx[4]; |
| 406 | struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 }; |
| 407 | |
| 408 | dibx000_i2c_gate_ctrl(mst, tx, 0, 0); |
| 409 | i2c_transfer(mst->i2c_adap, &m, 1); |
| 410 | mst->selected_interface = 0xff; // the first time force a select of the I2C |
| 411 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER); |
| 412 | } |
| 413 | |
| 414 | EXPORT_SYMBOL(dibx000_reset_i2c_master); |
| 415 | |
| 416 | static int i2c_adapter_init(struct i2c_adapter *i2c_adap, |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 417 | struct i2c_algorithm *algo, const char *name, |
| 418 | struct dibx000_i2c_master *mst) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 419 | { |
David Brownell | 2096b95 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 420 | strncpy(i2c_adap->name, name, sizeof(i2c_adap->name)); |
Jean Delvare | 2c2742d | 2010-11-05 07:35:35 -0300 | [diff] [blame] | 421 | i2c_adap->algo = algo; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 422 | i2c_adap->algo_data = NULL; |
| 423 | i2c_set_adapdata(i2c_adap, mst); |
| 424 | if (i2c_add_adapter(i2c_adap) < 0) |
| 425 | return -ENODEV; |
| 426 | return 0; |
| 427 | } |
| 428 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 429 | int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev, |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 430 | struct i2c_adapter *i2c_adap, u8 i2c_addr) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 431 | { |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 432 | int ret; |
| 433 | |
| 434 | mutex_init(&mst->i2c_buffer_lock); |
| 435 | if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { |
| 436 | dprintk("could not acquire lock"); |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | memset(mst->msg, 0, sizeof(struct i2c_msg)); |
| 440 | mst->msg[0].addr = i2c_addr >> 1; |
| 441 | mst->msg[0].flags = 0; |
| 442 | mst->msg[0].buf = mst->i2c_write_buffer; |
| 443 | mst->msg[0].len = 4; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 444 | |
| 445 | mst->device_rev = device_rev; |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 446 | mst->i2c_adap = i2c_adap; |
| 447 | mst->i2c_addr = i2c_addr >> 1; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 448 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 449 | if (device_rev == DIB7000P || device_rev == DIB8000) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 450 | mst->base_reg = 1024; |
| 451 | else |
| 452 | mst->base_reg = 768; |
| 453 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 454 | mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent; |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 455 | if (i2c_adapter_init |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 456 | (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo, |
| 457 | "DiBX000 tuner I2C bus", mst) != 0) |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 458 | printk(KERN_ERR |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 459 | "DiBX000: could not initialize the tuner i2c_adapter\n"); |
| 460 | |
| 461 | mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent; |
| 462 | if (i2c_adapter_init |
| 463 | (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo, |
| 464 | "DiBX000 master GPIO12 I2C bus", mst) != 0) |
| 465 | printk(KERN_ERR |
| 466 | "DiBX000: could not initialize the master i2c_adapter\n"); |
| 467 | |
| 468 | mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent; |
| 469 | if (i2c_adapter_init |
| 470 | (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo, |
| 471 | "DiBX000 master GPIO34 I2C bus", mst) != 0) |
| 472 | printk(KERN_ERR |
| 473 | "DiBX000: could not initialize the master i2c_adapter\n"); |
| 474 | |
| 475 | mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent; |
| 476 | if (i2c_adapter_init |
| 477 | (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo, |
| 478 | "DiBX000 master GPIO67 I2C bus", mst) != 0) |
| 479 | printk(KERN_ERR |
| 480 | "DiBX000: could not initialize the master i2c_adapter\n"); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 481 | |
| 482 | /* initialize the i2c-master by closing the gate */ |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 483 | dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 484 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame^] | 485 | ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1); |
| 486 | mutex_unlock(&mst->i2c_buffer_lock); |
| 487 | |
| 488 | return ret; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 489 | } |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 490 | |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 491 | EXPORT_SYMBOL(dibx000_init_i2c_master); |
| 492 | |
| 493 | void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst) |
| 494 | { |
| 495 | i2c_del_adapter(&mst->gated_tuner_i2c_adap); |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 496 | i2c_del_adapter(&mst->master_i2c_adap_gpio12); |
| 497 | i2c_del_adapter(&mst->master_i2c_adap_gpio34); |
| 498 | i2c_del_adapter(&mst->master_i2c_adap_gpio67); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 499 | } |
| 500 | EXPORT_SYMBOL(dibx000_exit_i2c_master); |
Patrick Boettcher | c6d74c2 | 2006-08-06 08:49:09 -0300 | [diff] [blame] | 501 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 502 | |
Randy Dunlap | 7ccf1ee | 2010-02-14 23:39:32 -0300 | [diff] [blame] | 503 | u32 systime(void) |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 504 | { |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 505 | struct timespec t; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 506 | |
Olivier Grenie | b994d19 | 2011-01-03 15:39:35 -0300 | [diff] [blame] | 507 | t = current_kernel_time(); |
| 508 | return (t.tv_sec * 10000) + (t.tv_nsec / 100000); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 509 | } |
| 510 | EXPORT_SYMBOL(systime); |
| 511 | |
Patrick Boettcher | c6d74c2 | 2006-08-06 08:49:09 -0300 | [diff] [blame] | 512 | MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>"); |
| 513 | MODULE_DESCRIPTION("Common function the DiBcom demodulator family"); |
| 514 | MODULE_LICENSE("GPL"); |