Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 1 | #include <linux/i2c.h> |
| 2 | |
| 3 | #include "dibx000_common.h" |
| 4 | |
| 5 | static int debug; |
| 6 | module_param(debug, int, 0644); |
| 7 | MODULE_PARM_DESC(debug, "turn on debugging (default: 0)"); |
| 8 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 9 | #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] | 10 | |
| 11 | static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val) |
| 12 | { |
| 13 | u8 b[4] = { |
| 14 | (reg >> 8) & 0xff, reg & 0xff, |
| 15 | (val >> 8) & 0xff, val & 0xff, |
| 16 | }; |
| 17 | struct i2c_msg msg = { |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 18 | .addr = mst->i2c_addr,.flags = 0,.buf = b,.len = 4 |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 19 | }; |
| 20 | return i2c_transfer(mst->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0; |
| 21 | } |
| 22 | |
| 23 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 24 | static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst, |
| 25 | enum dibx000_i2c_interface intf) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 26 | { |
| 27 | if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 28 | dprintk("selecting interface: %d", intf); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 29 | mst->selected_interface = intf; |
| 30 | return dibx000_write_word(mst, mst->base_reg + 4, intf); |
| 31 | } |
| 32 | return 0; |
| 33 | } |
| 34 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 35 | static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4], |
| 36 | u8 addr, int onoff) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 37 | { |
| 38 | u16 val; |
| 39 | |
| 40 | |
| 41 | if (onoff) |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 42 | 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] | 43 | else |
| 44 | val = 1 << 7; |
| 45 | |
| 46 | if (mst->device_rev > DIB7000) |
| 47 | val <<= 1; |
| 48 | |
| 49 | tx[0] = (((mst->base_reg + 1) >> 8) & 0xff); |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 50 | tx[1] = ((mst->base_reg + 1) & 0xff); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 51 | tx[2] = val >> 8; |
| 52 | tx[3] = val & 0xff; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static u32 dibx000_i2c_func(struct i2c_adapter *adapter) |
| 58 | { |
| 59 | return I2C_FUNC_I2C; |
| 60 | } |
| 61 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 62 | static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap, |
| 63 | struct i2c_msg msg[], int num) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 64 | { |
| 65 | struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap); |
| 66 | struct i2c_msg m[2 + num]; |
| 67 | u8 tx_open[4], tx_close[4]; |
| 68 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 69 | memset(m, 0, sizeof(struct i2c_msg) * (2 + num)); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 70 | |
| 71 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER); |
| 72 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 73 | dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 74 | m[0].addr = mst->i2c_addr; |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 75 | m[0].buf = tx_open; |
| 76 | m[0].len = 4; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 77 | |
| 78 | memcpy(&m[1], msg, sizeof(struct i2c_msg) * num); |
| 79 | |
| 80 | dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0); |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 81 | m[num + 1].addr = mst->i2c_addr; |
| 82 | m[num + 1].buf = tx_close; |
| 83 | m[num + 1].len = 4; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 84 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 85 | return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = { |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 89 | .master_xfer = dibx000_i2c_gated_tuner_xfer, |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 90 | .functionality = dibx000_i2c_func, |
| 91 | }; |
| 92 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 93 | struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst, |
| 94 | enum dibx000_i2c_interface intf, |
| 95 | int gating) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 96 | { |
| 97 | struct i2c_adapter *i2c = NULL; |
| 98 | |
| 99 | switch (intf) { |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 100 | case DIBX000_I2C_INTERFACE_TUNER: |
| 101 | if (gating) |
| 102 | i2c = &mst->gated_tuner_i2c_adap; |
| 103 | break; |
| 104 | default: |
| 105 | printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n"); |
| 106 | break; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return i2c; |
| 110 | } |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 111 | |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 112 | EXPORT_SYMBOL(dibx000_get_i2c_adapter); |
| 113 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 114 | void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst) |
| 115 | { |
| 116 | /* initialize the i2c-master by closing the gate */ |
| 117 | u8 tx[4]; |
| 118 | struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 }; |
| 119 | |
| 120 | dibx000_i2c_gate_ctrl(mst, tx, 0, 0); |
| 121 | i2c_transfer(mst->i2c_adap, &m, 1); |
| 122 | mst->selected_interface = 0xff; // the first time force a select of the I2C |
| 123 | dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER); |
| 124 | } |
| 125 | |
| 126 | EXPORT_SYMBOL(dibx000_reset_i2c_master); |
| 127 | |
| 128 | static int i2c_adapter_init(struct i2c_adapter *i2c_adap, |
| 129 | struct i2c_algorithm *algo, const char *name, |
| 130 | struct dibx000_i2c_master *mst) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 131 | { |
David Brownell | 2096b95 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 132 | strncpy(i2c_adap->name, name, sizeof(i2c_adap->name)); |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 133 | i2c_adap->class = I2C_CLASS_TV_DIGITAL, i2c_adap->algo = algo; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 134 | i2c_adap->algo_data = NULL; |
| 135 | i2c_set_adapdata(i2c_adap, mst); |
| 136 | if (i2c_add_adapter(i2c_adap) < 0) |
| 137 | return -ENODEV; |
| 138 | return 0; |
| 139 | } |
| 140 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 141 | int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev, |
| 142 | struct i2c_adapter *i2c_adap, u8 i2c_addr) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 143 | { |
| 144 | u8 tx[4]; |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 145 | struct i2c_msg m = {.addr = i2c_addr >> 1,.buf = tx,.len = 4 }; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 146 | |
| 147 | mst->device_rev = device_rev; |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 148 | mst->i2c_adap = i2c_adap; |
| 149 | mst->i2c_addr = i2c_addr >> 1; |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 150 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 151 | if (device_rev == DIB7000P || device_rev == DIB8000) |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 152 | mst->base_reg = 1024; |
| 153 | else |
| 154 | mst->base_reg = 768; |
| 155 | |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 156 | if (i2c_adapter_init |
| 157 | (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo, |
| 158 | "DiBX000 tuner I2C bus", mst) != 0) |
| 159 | printk(KERN_ERR |
| 160 | "DiBX000: could not initialize the tuner i2c_adapter\n"); |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 161 | |
| 162 | /* initialize the i2c-master by closing the gate */ |
| 163 | dibx000_i2c_gate_ctrl(mst, tx, 0, 0); |
| 164 | |
| 165 | return i2c_transfer(i2c_adap, &m, 1) == 1; |
| 166 | } |
Patrick Boettcher | 77e2c0f | 2009-08-17 07:01:10 -0300 | [diff] [blame] | 167 | |
Patrick Boettcher | 42afd06 | 2006-07-29 17:33:44 -0300 | [diff] [blame] | 168 | EXPORT_SYMBOL(dibx000_init_i2c_master); |
| 169 | |
| 170 | void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst) |
| 171 | { |
| 172 | i2c_del_adapter(&mst->gated_tuner_i2c_adap); |
| 173 | } |
| 174 | EXPORT_SYMBOL(dibx000_exit_i2c_master); |
Patrick Boettcher | c6d74c2 | 2006-08-06 08:49:09 -0300 | [diff] [blame] | 175 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 176 | |
Randy Dunlap | 7ccf1ee | 2010-02-14 23:39:32 -0300 | [diff] [blame^] | 177 | u32 systime(void) |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 178 | { |
| 179 | struct timespec t; |
| 180 | |
| 181 | t = current_kernel_time(); |
| 182 | return (t.tv_sec * 10000) + (t.tv_nsec / 100000); |
| 183 | } |
| 184 | EXPORT_SYMBOL(systime); |
| 185 | |
Patrick Boettcher | c6d74c2 | 2006-08-06 08:49:09 -0300 | [diff] [blame] | 186 | MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>"); |
| 187 | MODULE_DESCRIPTION("Common function the DiBcom demodulator family"); |
| 188 | MODULE_LICENSE("GPL"); |