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