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