22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/stat.h>
28 #include <linux/i2c.h>
30 #include <linux/pci.h>
35 #define DRIVER_NAME "i2c-intel-mid"
36 #define VERSION "Version 0.5ac2"
37 #define PLATFORM "Moorestown/Medfield"
40 #define NUM_PLATFORMS 2
98 #define SLV_DIS (1 << 6)
99 #define RESTART (1 << 5)
100 #define ADDR_10BIT (1 << 4)
101 #define STANDARD_MODE (1 << 1)
102 #define FAST_MODE (2 << 1)
103 #define HIGH_MODE (3 << 1)
104 #define MASTER_EN (1 << 0)
108 #define IC_TAR_10BIT_ADDR (1 << 12)
109 #define IC_TAR_SPECIAL (1 << 11)
110 #define IC_TAR_GC_OR_START (1 << 10)
116 #define IC_HS_MADDR 0x0c
119 #define IC_DATA_CMD 0x10
120 #define IC_RD (1 << 8)
123 #define IC_SS_SCL_HCNT 0x14
126 #define IC_SS_SCL_LCNT 0x18
129 #define IC_FS_SCL_HCNT 0x1c
132 #define IC_FS_SCL_LCNT 0x20
135 #define IC_HS_SCL_HCNT 0x24
138 #define IC_HS_SCL_LCNT 0x28
141 #define IC_INTR_STAT 0x2c
142 #define R_GEN_CALL (1 << 11)
143 #define R_START_DET (1 << 10)
144 #define R_STOP_DET (1 << 9)
145 #define R_ACTIVITY (1 << 8)
146 #define R_RX_DONE (1 << 7)
147 #define R_TX_ABRT (1 << 6)
148 #define R_RD_REQ (1 << 5)
149 #define R_TX_EMPTY (1 << 4)
150 #define R_TX_OVER (1 << 3)
151 #define R_RX_FULL (1 << 2)
152 #define R_RX_OVER (1 << 1)
153 #define R_RX_UNDER (1 << 0)
156 #define IC_INTR_MASK 0x30
157 #define M_GEN_CALL (1 << 11)
158 #define M_START_DET (1 << 10)
159 #define M_STOP_DET (1 << 9)
160 #define M_ACTIVITY (1 << 8)
161 #define M_RX_DONE (1 << 7)
162 #define M_TX_ABRT (1 << 6)
163 #define M_RD_REQ (1 << 5)
164 #define M_TX_EMPTY (1 << 4)
165 #define M_TX_OVER (1 << 3)
166 #define M_RX_FULL (1 << 2)
167 #define M_RX_OVER (1 << 1)
168 #define M_RX_UNDER (1 << 0)
171 #define IC_RAW_INTR_STAT 0x34
172 #define GEN_CALL (1 << 11)
173 #define START_DET (1 << 10)
174 #define STOP_DET (1 << 9)
175 #define ACTIVITY (1 << 8)
176 #define RX_DONE (1 << 7)
177 #define TX_ABRT (1 << 6)
178 #define RD_REQ (1 << 5)
179 #define TX_EMPTY (1 << 4)
180 #define TX_OVER (1 << 3)
181 #define RX_FULL (1 << 2)
182 #define RX_OVER (1 << 1)
183 #define RX_UNDER (1 << 0)
186 #define IC_RX_TL 0x38
189 #define IC_TX_TL 0x3c
192 #define IC_CLR_INTR 0x40
193 #define CLR_INTR (1 << 0)
196 #define IC_CLR_RX_UNDER 0x44
197 #define CLR_RX_UNDER (1 << 0)
200 #define IC_CLR_RX_OVER 0x48
201 #define CLR_RX_OVER (1 << 0)
204 #define IC_CLR_TX_OVER 0x4c
205 #define CLR_TX_OVER (1 << 0)
207 #define IC_CLR_RD_REQ 0x50
210 #define IC_CLR_TX_ABRT 0x54
211 #define CLR_TX_ABRT (1 << 0)
212 #define IC_CLR_RX_DONE 0x58
215 #define IC_CLR_ACTIVITY 0x5c
216 #define CLR_ACTIVITY (1 << 0)
219 #define IC_CLR_STOP_DET 0x60
220 #define CLR_STOP_DET (1 << 0)
223 #define IC_CLR_START_DET 0x64
224 #define CLR_START_DET (1 << 0)
227 #define IC_CLR_GEN_CALL 0x68
228 #define CLR_GEN_CALL (1 << 0)
231 #define IC_ENABLE 0x6c
232 #define ENABLE (1 << 0)
235 #define IC_STATUS 0x70
236 #define STAT_SLV_ACTIVITY (1 << 6)
237 #define STAT_MST_ACTIVITY (1 << 5)
238 #define STAT_RFF (1 << 4)
239 #define STAT_RFNE (1 << 3)
240 #define STAT_TFE (1 << 2)
241 #define STAT_TFNF (1 << 1)
242 #define STAT_ACTIVITY (1 << 0)
245 #define IC_TXFLR 0x74
246 #define TXFLR (1 << 0)
249 #define IC_RXFLR 0x78
250 #define RXFLR (1 << 0)
253 #define IC_TX_ABRT_SOURCE 0x80
254 #define ABRT_SLVRD_INTX (1 << 15)
255 #define ABRT_SLV_ARBLOST (1 << 14)
256 #define ABRT_SLVFLUSH_TXFIFO (1 << 13)
257 #define ARB_LOST (1 << 12)
258 #define ABRT_MASTER_DIS (1 << 11)
259 #define ABRT_10B_RD_NORSTRT (1 << 10)
260 #define ABRT_SBYTE_NORSTRT (1 << 9)
261 #define ABRT_HS_NORSTRT (1 << 8)
262 #define ABRT_SBYTE_ACKDET (1 << 7)
263 #define ABRT_HS_ACKDET (1 << 6)
264 #define ABRT_GCALL_READ (1 << 5)
265 #define ABRT_GCALL_NOACK (1 << 4)
266 #define ABRT_TXDATA_NOACK (1 << 3)
267 #define ABRT_10ADDR2_NOACK (1 << 2)
268 #define ABRT_10ADDR1_NOACK (1 << 1)
269 #define ABRT_7B_ADDR_NOACK (1 << 0)
272 #define IC_ENABLE_STATUS 0x9c
273 #define IC_EN (1 << 0)
276 #define IC_COMP_PARAM_1 0xf4
277 #define APB_DATA_WIDTH (0x3 << 0)
280 #define SS_MIN_SCL_HIGH 4000
281 #define SS_MIN_SCL_LOW 4700
282 #define FS_MIN_SCL_HIGH 600
283 #define FS_MIN_SCL_LOW 1300
284 #define HS_MIN_SCL_HIGH_100PF 60
285 #define HS_MIN_SCL_LOW_100PF 120
293 static int speed_mode[6] = {
302 static int ctl_num = 6;
319 static int intel_mid_i2c_disable(
struct i2c_adapter *adap)
336 dev_dbg(&adap->
dev,
"i2c is busy, count is %d speed %d\n",
386 { 0x75, 0x15, 0x07 },
387 { 0x04c, 0x10, 0x06 }
390 { 0x7C, 0x21, 0x0E },
391 { 0x053, 0x19, 0x0F }
395 err = intel_mid_i2c_disable(&i2c->
adap);
442 static inline bool intel_mid_i2c_address_neq(
const struct i2c_msg *
p1,
476 "initiate master operation with master mode disabled.\n");
479 "RESTART disabled and master sent READ cmd in 10-bit addressing.\n");
483 "RESTART disabled and user is trying to send START byte.\n");
491 "START byte was not acknowledged.\n");
494 "No acknowledgement received from slave.\n");
497 "The 2nd address byte of the 10-bit address was not acknowledged.\n");
500 "The 1st address byte of 10-bit address was not acknowledged.\n");
503 "I2C slave device not acknowledged.\n");
536 "I2C FIFO cannot support larger than 256 bytes\n");
553 dev_err(&adap->
dev,
"Timeout for ACK from I2C slave device\n");
554 intel_mid_i2c_hwinit(i2c);
582 unsigned char *buf,
int length)
589 "I2C FIFO cannot support larger than 256 bytes\n");
599 for (i = 0; i <
length; i++)
605 dev_err(&adap->
dev,
"Timeout for ACK from I2C slave device\n");
606 intel_mid_i2c_hwinit(i2c);
625 err = intel_mid_i2c_disable(adap);
628 "Cannot disable i2c controller, timeout\n");
632 mode = (1 + i2c->
speed) << 1;
635 if ((reg & 0x06) != mode) {
644 dev_dbg(&adap->
dev,
"set i2c 10 bit address mode\n");
649 dev_dbg(&adap->
dev,
"set i2c 7 bit address mode\n");
655 if ((reg &
RESTART) != RESTART) {
656 dev_dbg(&adap->
dev,
"enable restart conditions\n");
662 dev_dbg(&adap->
dev,
"ic_con reg is 0x%x\n", reg);
664 if ((reg &
SLV_DIS) != SLV_DIS) {
667 dev_dbg(&adap->
dev,
"ic_con reg is 0x%x\n", reg);
674 if ((reg & bit_mask) != 0x0) {
676 "WR: use target address when intiating transfer, i2c_tx_target\n");
682 "set target address to the I2C slave address, addr is %x\n",
714 static int intel_mid_i2c_xfer(
struct i2c_adapter *adap,
725 pm_runtime_get(i2c->
dev);
728 dev_dbg(&adap->
dev,
"intel_mid_i2c_xfer, process %d msg(s)\n", num);
733 dev_err(&adap->
dev,
"Adapter %d in transfer/standby\n",
736 pm_runtime_put(i2c->
dev);
741 for (i = 1; i < num; i++) {
743 if (
unlikely(intel_mid_i2c_address_neq(&pmsg[0], &pmsg[i]))) {
744 dev_err(&adap->
dev,
"Invalid address in msg[%d]\n", i);
746 pm_runtime_put(i2c->
dev);
751 if (intel_mid_i2c_setup(adap, pmsg)) {
753 pm_runtime_put(i2c->
dev);
757 for (i = 0; i < num; i++) {
763 err = xfer_read(adap, pmsg->
buf, pmsg->
len);
766 err = xfer_write(adap, pmsg->
buf, pmsg->
len);
770 dev_dbg(&adap->
dev,
"msg[%d] transfer complete\n", i);
781 pm_runtime_put(i2c->
dev);
786 static int intel_mid_i2c_runtime_suspend(
struct device *
dev)
796 intel_mid_i2c_disable(adap);
800 dev_err(dev,
"pci_save_state failed\n");
806 dev_err(dev,
"pci_set_power_state failed\n");
814 static int intel_mid_i2c_runtime_resume(
struct device *dev)
827 dev_err(dev,
"pci_enable_device failed\n");
833 intel_mid_i2c_hwinit(i2c);
857 for (; len > 0 && rx_num > 0; len--, rx_num--)
870 static irqreturn_t intel_mid_i2c_isr(
int this_irq,
void *dev)
878 dev_dbg(&i2c->
adap.dev,
"%s, stat = 0x%x\n", __func__, stat);
891 if (stat & TX_ABRT) {
892 intel_mid_i2c_abort(i2c);
921 .master_xfer = intel_mid_i2c_xfer,
922 .functionality = intel_mid_i2c_func,
926 static const struct dev_pm_ops intel_mid_i2c_pm_ops = {
927 .runtime_suspend = intel_mid_i2c_runtime_suspend,
928 .runtime_resume = intel_mid_i2c_runtime_resume,
954 unsigned long start, len;
958 dev_dbg(&dev->
dev,
"Get into probe function for I2C\n");
961 dev_err(&dev->
dev,
"Failed to enable I2C PCI device (%d)\n",
969 if (!start || len == 0) {
974 dev_dbg(&dev->
dev,
"%s i2c resource start 0x%lx, len=%ld\n",
979 dev_err(&dev->
dev,
"failed to request I2C region "
980 "0x%lx-0x%lx\n", start,
987 dev_err(&dev->
dev,
"I/O memory remapping failed\n");
995 dev_err(&dev->
dev,
"can't allocate interface\n");
1002 "Intel MID I2C at %lx", start);
1004 mrst->
adap.algo = &intel_mid_i2c_algorithm;
1005 mrst->
adap.dev.parent = &dev->
dev;
1013 pci_set_drvdata(dev, mrst);
1014 i2c_set_adapdata(&mrst->
adap, mrst);
1016 mrst->
adap.nr = busnum =
id->driver_data;
1017 if (dev->
device <= 0x0804)
1024 if (ctl_num > busnum) {
1025 if (speed_mode[busnum] < 0 || speed_mode[busnum] >=
NUM_SPEEDS)
1026 dev_warn(&dev->
dev,
"invalid speed %d ignored.\n",
1027 speed_mode[busnum]);
1033 err = intel_mid_i2c_hwinit(mrst);
1035 dev_err(&dev->
dev,
"I2C interface initialization failed\n");
1047 mrst->
adap.name, mrst);
1049 dev_err(&dev->
dev,
"Failed to request IRQ for I2C controller: "
1050 "%s", mrst->
adap.name);
1057 dev_err(&dev->
dev,
"Adapter %s registration failed\n",
1062 dev_dbg(&dev->
dev,
"%s I2C bus %d driver bind success.\n",
1072 pci_set_drvdata(dev,
NULL);
1085 intel_mid_i2c_disable(&mrst->
adap);
1087 dev_err(&dev->
dev,
"Failed to delete i2c adapter");
1090 pci_set_drvdata(dev,
NULL);
1112 static struct pci_driver intel_mid_i2c_driver = {
1114 .id_table = intel_mid_i2c_ids,
1115 .probe = intel_mid_i2c_probe,