drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / i2c / busses / i2c-mchp-pci1xxxx.c
blob5ef136c3ecb1262144f36be9e54336740d9e5528
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Microchip PCI1XXXX I2C adapter driver for PCIe Switch
4 * which has I2C controller in one of its downstream functions
6 * Copyright (C) 2021 - 2022 Microchip Technology Inc.
8 * Authors: Tharun Kumar P <tharunkumar.pasumarthi@microchip.com>
9 * Kumaravel Thiagarajan <kumaravel.thiagarajan@microchip.com>
12 #include <linux/bits.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/i2c-smbus.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/types.h>
22 #define SMBUS_MAST_CORE_ADDR_BASE 0x00000
23 #define SMBUS_MAST_SYS_REG_ADDR_BASE 0x01000
25 /* SMB register space. */
26 #define SMB_CORE_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x00)
28 #define SMB_CORE_CTRL_ESO BIT(6)
29 #define SMB_CORE_CTRL_FW_ACK BIT(4)
30 #define SMB_CORE_CTRL_ACK BIT(0)
32 #define SMB_CORE_CMD_REG_OFF3 (SMBUS_MAST_CORE_ADDR_BASE + 0x0F)
33 #define SMB_CORE_CMD_REG_OFF2 (SMBUS_MAST_CORE_ADDR_BASE + 0x0E)
34 #define SMB_CORE_CMD_REG_OFF1 (SMBUS_MAST_CORE_ADDR_BASE + 0x0D)
36 #define SMB_CORE_CMD_READM BIT(4)
37 #define SMB_CORE_CMD_STOP BIT(2)
38 #define SMB_CORE_CMD_START BIT(0)
40 #define SMB_CORE_CMD_REG_OFF0 (SMBUS_MAST_CORE_ADDR_BASE + 0x0C)
42 #define SMB_CORE_CMD_M_PROCEED BIT(1)
43 #define SMB_CORE_CMD_M_RUN BIT(0)
45 #define SMB_CORE_SR_HOLD_TIME_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x18)
48 * SR_HOLD_TIME_XK_TICKS field will indicate the number of ticks of the
49 * baud clock required to program 'Hold Time' at X KHz.
51 #define SR_HOLD_TIME_100K_TICKS 150
52 #define SR_HOLD_TIME_400K_TICKS 20
53 #define SR_HOLD_TIME_1000K_TICKS 12
55 #define SMB_CORE_COMPLETION_REG_OFF3 (SMBUS_MAST_CORE_ADDR_BASE + 0x23)
57 #define COMPLETION_MDONE BIT(6)
58 #define COMPLETION_IDLE BIT(5)
59 #define COMPLETION_MNAKX BIT(0)
61 #define SMB_CORE_IDLE_SCALING_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x24)
64 * FAIR_BUS_IDLE_MIN_XK_TICKS field will indicate the number of ticks of
65 * the baud clock required to program 'fair idle delay' at X KHz. Fair idle
66 * delay establishes the MCTP T(IDLE_DELAY) period.
68 #define FAIR_BUS_IDLE_MIN_100K_TICKS 992
69 #define FAIR_BUS_IDLE_MIN_400K_TICKS 500
70 #define FAIR_BUS_IDLE_MIN_1000K_TICKS 500
73 * FAIR_IDLE_DELAY_XK_TICKS field will indicate the number of ticks of the
74 * baud clock required to satisfy the fairness protocol at X KHz.
76 #define FAIR_IDLE_DELAY_100K_TICKS 963
77 #define FAIR_IDLE_DELAY_400K_TICKS 156
78 #define FAIR_IDLE_DELAY_1000K_TICKS 156
80 #define SMB_IDLE_SCALING_100K \
81 ((FAIR_IDLE_DELAY_100K_TICKS << 16) | FAIR_BUS_IDLE_MIN_100K_TICKS)
82 #define SMB_IDLE_SCALING_400K \
83 ((FAIR_IDLE_DELAY_400K_TICKS << 16) | FAIR_BUS_IDLE_MIN_400K_TICKS)
84 #define SMB_IDLE_SCALING_1000K \
85 ((FAIR_IDLE_DELAY_1000K_TICKS << 16) | FAIR_BUS_IDLE_MIN_1000K_TICKS)
87 #define SMB_CORE_CONFIG_REG3 (SMBUS_MAST_CORE_ADDR_BASE + 0x2B)
89 #define SMB_CONFIG3_ENMI BIT(6)
90 #define SMB_CONFIG3_ENIDI BIT(5)
92 #define SMB_CORE_CONFIG_REG2 (SMBUS_MAST_CORE_ADDR_BASE + 0x2A)
93 #define SMB_CORE_CONFIG_REG1 (SMBUS_MAST_CORE_ADDR_BASE + 0x29)
95 #define SMB_CONFIG1_ASR BIT(7)
96 #define SMB_CONFIG1_ENAB BIT(2)
97 #define SMB_CONFIG1_RESET BIT(1)
98 #define SMB_CONFIG1_FEN BIT(0)
100 #define SMB_CORE_BUS_CLK_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x2C)
103 * BUS_CLK_XK_LOW_PERIOD_TICKS field defines the number of I2C Baud Clock
104 * periods that make up the low phase of the I2C/SMBus bus clock at X KHz.
106 #define BUS_CLK_100K_LOW_PERIOD_TICKS 156
107 #define BUS_CLK_400K_LOW_PERIOD_TICKS 41
108 #define BUS_CLK_1000K_LOW_PERIOD_TICKS 15
111 * BUS_CLK_XK_HIGH_PERIOD_TICKS field defines the number of I2C Baud Clock
112 * periods that make up the high phase of the I2C/SMBus bus clock at X KHz.
114 #define BUS_CLK_100K_HIGH_PERIOD_TICKS 154
115 #define BUS_CLK_400K_HIGH_PERIOD_TICKS 35
116 #define BUS_CLK_1000K_HIGH_PERIOD_TICKS 14
118 #define BUS_CLK_100K \
119 ((BUS_CLK_100K_HIGH_PERIOD_TICKS << 8) | BUS_CLK_100K_LOW_PERIOD_TICKS)
120 #define BUS_CLK_400K \
121 ((BUS_CLK_400K_HIGH_PERIOD_TICKS << 8) | BUS_CLK_400K_LOW_PERIOD_TICKS)
122 #define BUS_CLK_1000K \
123 ((BUS_CLK_1000K_HIGH_PERIOD_TICKS << 8) | BUS_CLK_1000K_LOW_PERIOD_TICKS)
125 #define SMB_CORE_CLK_SYNC_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x3C)
128 * CLK_SYNC_XK defines the number of clock cycles to sync up to the external
129 * clock before comparing the internal and external clocks for clock stretching
130 * at X KHz.
132 #define CLK_SYNC_100K 4
133 #define CLK_SYNC_400K 4
134 #define CLK_SYNC_1000K 4
136 #define SMB_CORE_DATA_TIMING_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x40)
140 * FIRST_START_HOLD_XK_TICKS will indicate the number of ticks of the baud
141 * clock required to program 'FIRST_START_HOLD' timer at X KHz. This timer
142 * determines the SCLK hold time following SDAT driven low during the first
143 * START bit in a transfer.
145 #define FIRST_START_HOLD_100K_TICKS 23
146 #define FIRST_START_HOLD_400K_TICKS 8
147 #define FIRST_START_HOLD_1000K_TICKS 12
150 * STOP_SETUP_XK_TICKS will indicate the number of ticks of the baud clock
151 * required to program 'STOP_SETUP' timer at X KHz. This timer determines the
152 * SDAT setup time from the rising edge of SCLK for a STOP condition.
154 #define STOP_SETUP_100K_TICKS 150
155 #define STOP_SETUP_400K_TICKS 20
156 #define STOP_SETUP_1000K_TICKS 12
159 * RESTART_SETUP_XK_TICKS will indicate the number of ticks of the baud clock
160 * required to program 'RESTART_SETUP' timer at X KHz. This timer determines the
161 * SDAT setup time from the rising edge of SCLK for a repeated START condition.
163 #define RESTART_SETUP_100K_TICKS 156
164 #define RESTART_SETUP_400K_TICKS 20
165 #define RESTART_SETUP_1000K_TICKS 12
168 * DATA_HOLD_XK_TICKS will indicate the number of ticks of the baud clock
169 * required to program 'DATA_HOLD' timer at X KHz. This timer determines the
170 * SDAT hold time following SCLK driven low.
172 #define DATA_HOLD_100K_TICKS 12
173 #define DATA_HOLD_400K_TICKS 2
174 #define DATA_HOLD_1000K_TICKS 2
176 #define DATA_TIMING_100K \
177 ((FIRST_START_HOLD_100K_TICKS << 24) | (STOP_SETUP_100K_TICKS << 16) | \
178 (RESTART_SETUP_100K_TICKS << 8) | DATA_HOLD_100K_TICKS)
179 #define DATA_TIMING_400K \
180 ((FIRST_START_HOLD_400K_TICKS << 24) | (STOP_SETUP_400K_TICKS << 16) | \
181 (RESTART_SETUP_400K_TICKS << 8) | DATA_HOLD_400K_TICKS)
182 #define DATA_TIMING_1000K \
183 ((FIRST_START_HOLD_1000K_TICKS << 24) | (STOP_SETUP_1000K_TICKS << 16) | \
184 (RESTART_SETUP_1000K_TICKS << 8) | DATA_HOLD_1000K_TICKS)
186 #define SMB_CORE_TO_SCALING_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x44)
189 * BUS_IDLE_MIN_XK_TICKS defines Bus Idle Minimum Time.
190 * Bus Idle Minimum time = BUS_IDLE_MIN[7:0] x Baud_Clock_Period x
191 * (BUS_IDLE_MIN_XK_TICKS[7] ? 4,1)
193 #define BUS_IDLE_MIN_100K_TICKS 36UL
194 #define BUS_IDLE_MIN_400K_TICKS 10UL
195 #define BUS_IDLE_MIN_1000K_TICKS 4UL
198 * CTRL_CUM_TIME_OUT_XK_TICKS defines SMBus Controller Cumulative Time-Out.
199 * SMBus Controller Cumulative Time-Out duration =
200 * CTRL_CUM_TIME_OUT_XK_TICKS[7:0] x Baud_Clock_Period x 2048
202 #define CTRL_CUM_TIME_OUT_100K_TICKS 76
203 #define CTRL_CUM_TIME_OUT_400K_TICKS 76
204 #define CTRL_CUM_TIME_OUT_1000K_TICKS 76
207 * TARGET_CUM_TIME_OUT_XK_TICKS defines SMBus Target Cumulative Time-Out duration.
208 * SMBus Target Cumulative Time-Out duration = TARGET_CUM_TIME_OUT_XK_TICKS[7:0] x
209 * Baud_Clock_Period x 4096
211 #define TARGET_CUM_TIME_OUT_100K_TICKS 95
212 #define TARGET_CUM_TIME_OUT_400K_TICKS 95
213 #define TARGET_CUM_TIME_OUT_1000K_TICKS 95
216 * CLOCK_HIGH_TIME_OUT_XK defines Clock High time out period.
217 * Clock High time out period = CLOCK_HIGH_TIME_OUT_XK[7:0] x Baud_Clock_Period x 8
219 #define CLOCK_HIGH_TIME_OUT_100K_TICKS 97
220 #define CLOCK_HIGH_TIME_OUT_400K_TICKS 97
221 #define CLOCK_HIGH_TIME_OUT_1000K_TICKS 97
223 #define TO_SCALING_100K \
224 ((BUS_IDLE_MIN_100K_TICKS << 24) | (CTRL_CUM_TIME_OUT_100K_TICKS << 16) | \
225 (TARGET_CUM_TIME_OUT_100K_TICKS << 8) | CLOCK_HIGH_TIME_OUT_100K_TICKS)
226 #define TO_SCALING_400K \
227 ((BUS_IDLE_MIN_400K_TICKS << 24) | (CTRL_CUM_TIME_OUT_400K_TICKS << 16) | \
228 (TARGET_CUM_TIME_OUT_400K_TICKS << 8) | CLOCK_HIGH_TIME_OUT_400K_TICKS)
229 #define TO_SCALING_1000K \
230 ((BUS_IDLE_MIN_1000K_TICKS << 24) | (CTRL_CUM_TIME_OUT_1000K_TICKS << 16) | \
231 (TARGET_CUM_TIME_OUT_1000K_TICKS << 8) | CLOCK_HIGH_TIME_OUT_1000K_TICKS)
233 #define I2C_SCL_PAD_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x100)
234 #define I2C_SDA_PAD_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x101)
236 #define I2C_FOD_EN BIT(4)
237 #define I2C_PULL_UP_EN BIT(3)
238 #define I2C_PULL_DOWN_EN BIT(2)
239 #define I2C_INPUT_EN BIT(1)
240 #define I2C_OUTPUT_EN BIT(0)
242 #define SMBUS_CONTROL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x200)
244 #define CTL_RESET_COUNTERS BIT(3)
245 #define CTL_TRANSFER_DIR BIT(2)
246 #define CTL_HOST_FIFO_ENTRY BIT(1)
247 #define CTL_RUN BIT(0)
249 #define I2C_DIRN_WRITE 0
250 #define I2C_DIRN_READ 1
252 #define SMBUS_STATUS_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x204)
254 #define STA_DMA_TERM BIT(7)
255 #define STA_DMA_REQ BIT(6)
256 #define STA_THRESHOLD BIT(2)
257 #define STA_BUF_FULL BIT(1)
258 #define STA_BUF_EMPTY BIT(0)
260 #define SMBUS_INTR_STAT_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x208)
262 #define INTR_STAT_DMA_TERM BIT(7)
263 #define INTR_STAT_THRESHOLD BIT(2)
264 #define INTR_STAT_BUF_FULL BIT(1)
265 #define INTR_STAT_BUF_EMPTY BIT(0)
267 #define SMBUS_INTR_MSK_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x20C)
269 #define INTR_MSK_DMA_TERM BIT(7)
270 #define INTR_MSK_THRESHOLD BIT(2)
271 #define INTR_MSK_BUF_FULL BIT(1)
272 #define INTR_MSK_BUF_EMPTY BIT(0)
274 #define ALL_NW_LAYER_INTERRUPTS \
275 (INTR_MSK_DMA_TERM | INTR_MSK_THRESHOLD | INTR_MSK_BUF_FULL | \
276 INTR_MSK_BUF_EMPTY)
278 #define SMBUS_MCU_COUNTER_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x214)
280 #define SMBALERT_MST_PAD_CTRL_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x230)
282 #define SMBALERT_MST_PU BIT(0)
284 #define SMBUS_GEN_INT_STAT_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x23C)
286 #define SMBUS_GEN_INT_MASK_REG_OFF (SMBUS_MAST_CORE_ADDR_BASE + 0x240)
288 #define SMBALERT_INTR_MASK BIT(10)
289 #define I2C_BUF_MSTR_INTR_MASK BIT(9)
290 #define I2C_INTR_MASK BIT(8)
291 #define SMBALERT_WAKE_INTR_MASK BIT(2)
292 #define I2C_BUF_MSTR_WAKE_INTR_MASK BIT(1)
293 #define I2C_WAKE_INTR_MASK BIT(0)
295 #define ALL_HIGH_LAYER_INTR \
296 (SMBALERT_INTR_MASK | I2C_BUF_MSTR_INTR_MASK | I2C_INTR_MASK | \
297 SMBALERT_WAKE_INTR_MASK | I2C_BUF_MSTR_WAKE_INTR_MASK | \
298 I2C_WAKE_INTR_MASK)
300 #define SMBUS_RESET_REG (SMBUS_MAST_CORE_ADDR_BASE + 0x248)
302 #define PERI_SMBUS_D3_RESET_DIS BIT(16)
304 #define SMBUS_MST_BUF (SMBUS_MAST_CORE_ADDR_BASE + 0x280)
306 #define SMBUS_BUF_MAX_SIZE 0x80
308 #define I2C_FLAGS_DIRECT_MODE BIT(7)
309 #define I2C_FLAGS_POLLING_MODE BIT(6)
310 #define I2C_FLAGS_STOP BIT(5)
311 #define I2C_FLAGS_SMB_BLK_READ BIT(4)
313 #define PCI1XXXX_I2C_TIMEOUT_MS 1000
315 /* General Purpose Register. */
316 #define SMB_GPR_REG (SMBUS_MAST_CORE_ADDR_BASE + 0x1000 + 0x0c00 + \
317 0x00)
319 /* Lock Register. */
320 #define SMB_GPR_LOCK_REG (SMBUS_MAST_CORE_ADDR_BASE + 0x1000 + 0x0000 + \
321 0x00A0)
323 #define SMBUS_PERI_LOCK BIT(3)
325 struct pci1xxxx_i2c {
326 struct completion i2c_xfer_done;
327 bool i2c_xfer_in_progress;
328 struct i2c_adapter adap;
329 void __iomem *i2c_base;
330 u32 freq;
331 u32 flags;
334 static int set_sys_lock(struct pci1xxxx_i2c *i2c)
336 void __iomem *p = i2c->i2c_base + SMB_GPR_LOCK_REG;
337 u8 data;
339 writel(SMBUS_PERI_LOCK, p);
340 data = readl(p);
341 if (data != SMBUS_PERI_LOCK)
342 return -EPERM;
344 return 0;
347 static int release_sys_lock(struct pci1xxxx_i2c *i2c)
349 void __iomem *p = i2c->i2c_base + SMB_GPR_LOCK_REG;
350 u8 data;
352 data = readl(p);
353 if (data != SMBUS_PERI_LOCK)
354 return 0;
356 writel(0, p);
357 data = readl(p);
358 if (data & SMBUS_PERI_LOCK)
359 return -EPERM;
361 return 0;
364 static void pci1xxxx_ack_high_level_intr(struct pci1xxxx_i2c *i2c, u16 intr_msk)
366 writew(intr_msk, i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF);
369 static void pci1xxxx_i2c_configure_smbalert_pin(struct pci1xxxx_i2c *i2c,
370 bool enable)
372 void __iomem *p = i2c->i2c_base + SMBALERT_MST_PAD_CTRL_REG_OFF;
373 u8 regval;
375 regval = readb(p);
377 if (enable)
378 regval |= SMBALERT_MST_PU;
379 else
380 regval &= ~SMBALERT_MST_PU;
382 writeb(regval, p);
385 static void pci1xxxx_i2c_send_start_stop(struct pci1xxxx_i2c *i2c, bool start)
387 void __iomem *p = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
388 u8 regval;
390 regval = readb(p);
392 if (start)
393 regval |= SMB_CORE_CMD_START;
394 else
395 regval |= SMB_CORE_CMD_STOP;
397 writeb(regval, p);
401 * When accessing the core control reg, we should not do a read modified write
402 * as they are write '1' to clear bits. Instead we need to write with the
403 * specific bits that needs to be set.
405 static void pci1xxxx_i2c_set_clear_FW_ACK(struct pci1xxxx_i2c *i2c, bool set)
407 u8 regval;
409 if (set)
410 regval = SMB_CORE_CTRL_FW_ACK | SMB_CORE_CTRL_ESO | SMB_CORE_CTRL_ACK;
411 else
412 regval = SMB_CORE_CTRL_ESO | SMB_CORE_CTRL_ACK;
414 writeb(regval, i2c->i2c_base + SMB_CORE_CTRL_REG_OFF);
417 static void pci1xxxx_i2c_buffer_write(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
418 u8 transferlen, unsigned char *buf)
420 void __iomem *p = i2c->i2c_base + SMBUS_MST_BUF;
422 if (slaveaddr)
423 writeb(slaveaddr, p++);
425 if (buf)
426 memcpy_toio(p, buf, transferlen);
430 * When accessing the core control reg, we should not do a read modified write
431 * as there are write '1' to clear bits. Instead we need to write with the
432 * specific bits that needs to be set.
434 static void pci1xxxx_i2c_enable_ESO(struct pci1xxxx_i2c *i2c)
436 writeb(SMB_CORE_CTRL_ESO, i2c->i2c_base + SMB_CORE_CTRL_REG_OFF);
439 static void pci1xxxx_i2c_reset_counters(struct pci1xxxx_i2c *i2c)
441 void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
442 u8 regval;
444 regval = readb(p);
445 regval |= CTL_RESET_COUNTERS;
446 writeb(regval, p);
449 static void pci1xxxx_i2c_set_transfer_dir(struct pci1xxxx_i2c *i2c, u8 direction)
451 void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
452 u8 regval;
454 regval = readb(p);
455 if (direction == I2C_DIRN_WRITE)
456 regval &= ~CTL_TRANSFER_DIR;
457 else
458 regval |= CTL_TRANSFER_DIR;
460 writeb(regval, p);
463 static void pci1xxxx_i2c_set_mcu_count(struct pci1xxxx_i2c *i2c, u8 count)
465 writeb(count, i2c->i2c_base + SMBUS_MCU_COUNTER_REG_OFF);
468 static void pci1xxxx_i2c_set_read_count(struct pci1xxxx_i2c *i2c, u8 readcount)
470 writeb(readcount, i2c->i2c_base + SMB_CORE_CMD_REG_OFF3);
473 static void pci1xxxx_i2c_set_write_count(struct pci1xxxx_i2c *i2c, u8 writecount)
475 writeb(writecount, i2c->i2c_base + SMB_CORE_CMD_REG_OFF2);
478 static void pci1xxxx_i2c_set_DMA_run(struct pci1xxxx_i2c *i2c)
480 void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
481 u8 regval;
483 regval = readb(p);
484 regval |= CTL_RUN;
485 writeb(regval, p);
488 static void pci1xxxx_i2c_set_mrun_proceed(struct pci1xxxx_i2c *i2c)
490 void __iomem *p = i2c->i2c_base + SMB_CORE_CMD_REG_OFF0;
491 u8 regval;
493 regval = readb(p);
494 regval |= SMB_CORE_CMD_M_RUN;
495 regval |= SMB_CORE_CMD_M_PROCEED;
496 writeb(regval, p);
499 static void pci1xxxx_i2c_start_DMA(struct pci1xxxx_i2c *i2c)
501 pci1xxxx_i2c_set_DMA_run(i2c);
502 pci1xxxx_i2c_set_mrun_proceed(i2c);
505 static void pci1xxxx_i2c_config_asr(struct pci1xxxx_i2c *i2c, bool enable)
507 void __iomem *p = i2c->i2c_base + SMB_CORE_CONFIG_REG1;
508 u8 regval;
510 regval = readb(p);
511 if (enable)
512 regval |= SMB_CONFIG1_ASR;
513 else
514 regval &= ~SMB_CONFIG1_ASR;
515 writeb(regval, p);
518 static irqreturn_t pci1xxxx_i2c_isr(int irq, void *dev)
520 struct pci1xxxx_i2c *i2c = dev;
521 void __iomem *p1 = i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF;
522 void __iomem *p2 = i2c->i2c_base + SMBUS_INTR_STAT_REG_OFF;
523 irqreturn_t intr_handled = IRQ_NONE;
524 u16 reg1;
525 u8 reg3;
528 * Read the SMBus interrupt status register to see if the
529 * DMA_TERM interrupt has caused this callback.
531 reg1 = readw(p1);
533 if (reg1 & I2C_BUF_MSTR_INTR_MASK) {
534 reg3 = readb(p2);
535 if (reg3 & INTR_STAT_DMA_TERM) {
536 complete(&i2c->i2c_xfer_done);
537 intr_handled = IRQ_HANDLED;
538 writeb(INTR_STAT_DMA_TERM, p2);
540 pci1xxxx_ack_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK);
543 if (reg1 & SMBALERT_INTR_MASK) {
544 intr_handled = IRQ_HANDLED;
545 pci1xxxx_ack_high_level_intr(i2c, SMBALERT_INTR_MASK);
548 return intr_handled;
551 static void pci1xxxx_i2c_set_count(struct pci1xxxx_i2c *i2c, u8 mcucount,
552 u8 writecount, u8 readcount)
554 pci1xxxx_i2c_set_mcu_count(i2c, mcucount);
555 pci1xxxx_i2c_set_write_count(i2c, writecount);
556 pci1xxxx_i2c_set_read_count(i2c, readcount);
559 static void pci1xxxx_i2c_set_readm(struct pci1xxxx_i2c *i2c, bool enable)
561 void __iomem *p = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
562 u8 regval;
564 regval = readb(p);
565 if (enable)
566 regval |= SMB_CORE_CMD_READM;
567 else
568 regval &= ~SMB_CORE_CMD_READM;
570 writeb(regval, p);
573 static void pci1xxxx_ack_nw_layer_intr(struct pci1xxxx_i2c *i2c, u8 ack_intr_msk)
575 writeb(ack_intr_msk, i2c->i2c_base + SMBUS_INTR_STAT_REG_OFF);
578 static void pci1xxxx_config_nw_layer_intr(struct pci1xxxx_i2c *i2c,
579 u8 intr_msk, bool enable)
581 void __iomem *p = i2c->i2c_base + SMBUS_INTR_MSK_REG_OFF;
582 u8 regval;
584 regval = readb(p);
585 if (enable)
586 regval &= ~intr_msk;
587 else
588 regval |= intr_msk;
590 writeb(regval, p);
593 static void pci1xxxx_i2c_config_padctrl(struct pci1xxxx_i2c *i2c, bool enable)
595 void __iomem *p1 = i2c->i2c_base + I2C_SCL_PAD_CTRL_REG_OFF;
596 void __iomem *p2 = i2c->i2c_base + I2C_SDA_PAD_CTRL_REG_OFF;
597 u8 regval;
599 regval = readb(p1);
600 if (enable)
601 regval |= I2C_INPUT_EN | I2C_OUTPUT_EN;
602 else
603 regval &= ~(I2C_INPUT_EN | I2C_OUTPUT_EN);
605 writeb(regval, p1);
607 regval = readb(p2);
608 if (enable)
609 regval |= I2C_INPUT_EN | I2C_OUTPUT_EN;
610 else
611 regval &= ~(I2C_INPUT_EN | I2C_OUTPUT_EN);
613 writeb(regval, p2);
616 static void pci1xxxx_i2c_set_mode(struct pci1xxxx_i2c *i2c)
618 void __iomem *p = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;
619 u8 regval;
621 regval = readb(p);
622 if (i2c->flags & I2C_FLAGS_DIRECT_MODE)
623 regval &= ~CTL_HOST_FIFO_ENTRY;
624 else
625 regval |= CTL_HOST_FIFO_ENTRY;
627 writeb(regval, p);
630 static void pci1xxxx_i2c_config_high_level_intr(struct pci1xxxx_i2c *i2c,
631 u16 intr_msk, bool enable)
633 void __iomem *p = i2c->i2c_base + SMBUS_GEN_INT_MASK_REG_OFF;
634 u16 regval;
636 regval = readw(p);
637 if (enable)
638 regval &= ~intr_msk;
639 else
640 regval |= intr_msk;
641 writew(regval, p);
644 static void pci1xxxx_i2c_configure_core_reg(struct pci1xxxx_i2c *i2c, bool enable)
646 void __iomem *p1 = i2c->i2c_base + SMB_CORE_CONFIG_REG1;
647 void __iomem *p3 = i2c->i2c_base + SMB_CORE_CONFIG_REG3;
648 u8 reg1;
649 u8 reg3;
651 reg1 = readb(p1);
652 reg3 = readb(p3);
653 if (enable) {
654 reg1 |= SMB_CONFIG1_ENAB | SMB_CONFIG1_FEN;
655 reg3 |= SMB_CONFIG3_ENMI | SMB_CONFIG3_ENIDI;
656 } else {
657 reg1 &= ~(SMB_CONFIG1_ENAB | SMB_CONFIG1_FEN);
658 reg3 &= ~(SMB_CONFIG3_ENMI | SMB_CONFIG3_ENIDI);
661 writeb(reg1, p1);
662 writeb(reg3, p3);
665 static void pci1xxxx_i2c_set_freq(struct pci1xxxx_i2c *i2c)
667 void __iomem *bp = i2c->i2c_base;
668 void __iomem *p_idle_scaling = bp + SMB_CORE_IDLE_SCALING_REG_OFF;
669 void __iomem *p_data_timing = bp + SMB_CORE_DATA_TIMING_REG_OFF;
670 void __iomem *p_hold_time = bp + SMB_CORE_SR_HOLD_TIME_REG_OFF;
671 void __iomem *p_to_scaling = bp + SMB_CORE_TO_SCALING_REG_OFF;
672 void __iomem *p_clk_sync = bp + SMB_CORE_CLK_SYNC_REG_OFF;
673 void __iomem *p_clk_reg = bp + SMB_CORE_BUS_CLK_REG_OFF;
675 switch (i2c->freq) {
676 case I2C_MAX_STANDARD_MODE_FREQ:
677 writeb(SR_HOLD_TIME_100K_TICKS, p_hold_time);
678 writel(SMB_IDLE_SCALING_100K, p_idle_scaling);
679 writew(BUS_CLK_100K, p_clk_reg);
680 writel(CLK_SYNC_100K, p_clk_sync);
681 writel(DATA_TIMING_100K, p_data_timing);
682 writel(TO_SCALING_100K, p_to_scaling);
683 break;
685 case I2C_MAX_FAST_MODE_PLUS_FREQ:
686 writeb(SR_HOLD_TIME_1000K_TICKS, p_hold_time);
687 writel(SMB_IDLE_SCALING_1000K, p_idle_scaling);
688 writew(BUS_CLK_1000K, p_clk_reg);
689 writel(CLK_SYNC_1000K, p_clk_sync);
690 writel(DATA_TIMING_1000K, p_data_timing);
691 writel(TO_SCALING_1000K, p_to_scaling);
692 break;
694 case I2C_MAX_FAST_MODE_FREQ:
695 default:
696 writeb(SR_HOLD_TIME_400K_TICKS, p_hold_time);
697 writel(SMB_IDLE_SCALING_400K, p_idle_scaling);
698 writew(BUS_CLK_400K, p_clk_reg);
699 writel(CLK_SYNC_400K, p_clk_sync);
700 writel(DATA_TIMING_400K, p_data_timing);
701 writel(TO_SCALING_400K, p_to_scaling);
702 break;
706 static void pci1xxxx_i2c_init(struct pci1xxxx_i2c *i2c)
708 void __iomem *p2 = i2c->i2c_base + SMBUS_STATUS_REG_OFF;
709 void __iomem *p1 = i2c->i2c_base + SMB_GPR_REG;
710 u8 regval;
711 int ret;
713 ret = set_sys_lock(i2c);
714 if (ret == -EPERM) {
716 * Configure I2C Fast Mode as default frequency if unable
717 * to acquire sys lock.
719 regval = 0;
720 } else {
721 regval = readl(p1);
722 release_sys_lock(i2c);
725 switch (regval) {
726 case 0:
727 i2c->freq = I2C_MAX_FAST_MODE_FREQ;
728 pci1xxxx_i2c_set_freq(i2c);
729 break;
730 case 1:
731 i2c->freq = I2C_MAX_STANDARD_MODE_FREQ;
732 pci1xxxx_i2c_set_freq(i2c);
733 break;
734 case 2:
735 i2c->freq = I2C_MAX_FAST_MODE_PLUS_FREQ;
736 pci1xxxx_i2c_set_freq(i2c);
737 break;
738 case 3:
739 default:
740 break;
743 pci1xxxx_i2c_config_padctrl(i2c, true);
744 i2c->flags |= I2C_FLAGS_DIRECT_MODE;
745 pci1xxxx_i2c_set_mode(i2c);
748 * Added as a precaution since BUF_EMPTY in status register
749 * also trigered an Interrupt.
751 writeb(STA_BUF_EMPTY, p2);
753 /* Configure core I2c control registers. */
754 pci1xxxx_i2c_configure_core_reg(i2c, true);
757 * Enable pull-up for the SMB alert pin which is just used for
758 * wakeup right now.
760 pci1xxxx_i2c_configure_smbalert_pin(i2c, true);
763 static void pci1xxxx_i2c_clear_flags(struct pci1xxxx_i2c *i2c)
765 u8 regval;
767 /* Reset the internal buffer counters. */
768 pci1xxxx_i2c_reset_counters(i2c);
770 /* Clear low level interrupts. */
771 regval = COMPLETION_MNAKX | COMPLETION_IDLE | COMPLETION_MDONE;
772 writeb(regval, i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3);
773 reinit_completion(&i2c->i2c_xfer_done);
774 pci1xxxx_ack_nw_layer_intr(i2c, ALL_NW_LAYER_INTERRUPTS);
775 pci1xxxx_ack_high_level_intr(i2c, ALL_HIGH_LAYER_INTR);
778 static int pci1xxxx_i2c_read(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
779 unsigned char *buf, u16 total_len)
781 void __iomem *p2 = i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3;
782 void __iomem *p1 = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
783 void __iomem *p3 = i2c->i2c_base + SMBUS_MST_BUF;
784 unsigned long time_left;
785 u16 remainingbytes;
786 u8 transferlen;
787 int retval = 0;
788 u8 read_count;
789 u32 regval;
790 u16 count;
792 /* Enable I2C host controller by setting the ESO bit in the CONTROL REG. */
793 pci1xxxx_i2c_enable_ESO(i2c);
794 pci1xxxx_i2c_clear_flags(i2c);
795 pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, true);
796 pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, true);
799 * The I2C transfer could be more than 128 bytes. Our Core is
800 * capable of only sending 128 at a time.
801 * As far as the I2C read is concerned, initailly send the
802 * read slave address along with the number of bytes to read in
803 * ReadCount. After sending the slave address the interrupt
804 * is generated. On seeing the ACK for the slave address, reverse the
805 * buffer direction and run the DMA to initiate Read from slave.
807 for (count = 0; count < total_len; count += transferlen) {
810 * Before start of any transaction clear the existing
811 * START/STOP conditions.
813 writeb(0, p1);
814 remainingbytes = total_len - count;
815 transferlen = min_t(u16, remainingbytes, SMBUS_BUF_MAX_SIZE);
818 * Send STOP bit for the last chunk in the transaction.
819 * For I2C read transaction of more than BUF_SIZE, NACK should
820 * only be sent for the last read.
821 * Hence a bit FW_ACK is set for all the read chunks except for
822 * the last chunk. For the last chunk NACK should be sent and
823 * FW_ACK is cleared Send STOP only when I2C_FLAGS_STOP bit is
824 * set in the flags and only for the last transaction.
826 if ((count + transferlen >= total_len) &&
827 (i2c->flags & I2C_FLAGS_STOP)) {
828 pci1xxxx_i2c_set_clear_FW_ACK(i2c, false);
829 pci1xxxx_i2c_send_start_stop(i2c, 0);
830 } else {
831 pci1xxxx_i2c_set_clear_FW_ACK(i2c, true);
834 /* Send START bit for the first transaction. */
835 if (count == 0) {
836 pci1xxxx_i2c_set_transfer_dir(i2c, I2C_DIRN_WRITE);
837 pci1xxxx_i2c_send_start_stop(i2c, 1);
839 /* Write I2c buffer with just the slave addr. */
840 pci1xxxx_i2c_buffer_write(i2c, slaveaddr, 0, NULL);
842 /* Set the count. Readcount is the transfer bytes. */
843 pci1xxxx_i2c_set_count(i2c, 1, 1, transferlen);
846 * Set the Auto_start_read bit so that the HW itself
847 * will take care of the read phase.
849 pci1xxxx_i2c_config_asr(i2c, true);
850 if (i2c->flags & I2C_FLAGS_SMB_BLK_READ)
851 pci1xxxx_i2c_set_readm(i2c, true);
852 } else {
853 pci1xxxx_i2c_set_count(i2c, 0, 0, transferlen);
854 pci1xxxx_i2c_config_asr(i2c, false);
855 pci1xxxx_i2c_clear_flags(i2c);
856 pci1xxxx_i2c_set_transfer_dir(i2c, I2C_DIRN_READ);
859 /* Start the DMA. */
860 pci1xxxx_i2c_start_DMA(i2c);
862 /* Wait for the DMA_TERM interrupt. */
863 time_left = wait_for_completion_timeout(&i2c->i2c_xfer_done,
864 msecs_to_jiffies(PCI1XXXX_I2C_TIMEOUT_MS));
865 if (time_left == 0) {
866 /* Reset the I2C core to release the bus lock. */
867 pci1xxxx_i2c_init(i2c);
868 retval = -ETIMEDOUT;
869 goto cleanup;
872 /* Read the completion reg to know the reason for DMA_TERM. */
873 regval = readb(p2);
875 /* Slave did not respond. */
876 if (regval & COMPLETION_MNAKX) {
877 writeb(COMPLETION_MNAKX, p2);
878 retval = -ETIMEDOUT;
879 goto cleanup;
882 if (i2c->flags & I2C_FLAGS_SMB_BLK_READ) {
883 buf[0] = readb(p3);
884 read_count = buf[0];
885 memcpy_fromio(&buf[1], p3 + 1, read_count);
886 } else {
887 memcpy_fromio(&buf[count], p3, transferlen);
891 cleanup:
892 /* Disable all the interrupts. */
893 pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, false);
894 pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, false);
895 pci1xxxx_i2c_config_asr(i2c, false);
896 return retval;
899 static int pci1xxxx_i2c_write(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
900 unsigned char *buf, u16 total_len)
902 void __iomem *p2 = i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3;
903 void __iomem *p1 = i2c->i2c_base + SMB_CORE_CMD_REG_OFF1;
904 unsigned long time_left;
905 u16 remainingbytes;
906 u8 actualwritelen;
907 u8 transferlen;
908 int retval = 0;
909 u32 regval;
910 u16 count;
912 /* Enable I2C host controller by setting the ESO bit in the CONTROL REG. */
913 pci1xxxx_i2c_enable_ESO(i2c);
915 /* Set the Buffer direction. */
916 pci1xxxx_i2c_set_transfer_dir(i2c, I2C_DIRN_WRITE);
917 pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, true);
918 pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, true);
921 * The i2c transfer could be more than 128 bytes. Our Core is
922 * capable of only sending 128 at a time.
924 for (count = 0; count < total_len; count += transferlen) {
926 * Before start of any transaction clear the existing
927 * START/STOP conditions.
929 writeb(0, p1);
930 pci1xxxx_i2c_clear_flags(i2c);
931 remainingbytes = total_len - count;
933 /* If it is the starting of the transaction send START. */
934 if (count == 0) {
935 pci1xxxx_i2c_send_start_stop(i2c, 1);
937 /* -1 for the slave address. */
938 transferlen = min_t(u16, SMBUS_BUF_MAX_SIZE - 1,
939 remainingbytes);
940 pci1xxxx_i2c_buffer_write(i2c, slaveaddr,
941 transferlen, &buf[count]);
943 * The actual number of bytes written on the I2C bus
944 * is including the slave address.
946 actualwritelen = transferlen + 1;
947 } else {
948 transferlen = min_t(u16, SMBUS_BUF_MAX_SIZE, remainingbytes);
949 pci1xxxx_i2c_buffer_write(i2c, 0, transferlen, &buf[count]);
950 actualwritelen = transferlen;
953 pci1xxxx_i2c_set_count(i2c, actualwritelen, actualwritelen, 0);
956 * Send STOP only when I2C_FLAGS_STOP bit is set in the flags and
957 * only for the last transaction.
959 if (remainingbytes <= transferlen &&
960 (i2c->flags & I2C_FLAGS_STOP))
961 pci1xxxx_i2c_send_start_stop(i2c, 0);
963 pci1xxxx_i2c_start_DMA(i2c);
966 * Wait for the DMA_TERM interrupt.
968 time_left = wait_for_completion_timeout(&i2c->i2c_xfer_done,
969 msecs_to_jiffies(PCI1XXXX_I2C_TIMEOUT_MS));
970 if (time_left == 0) {
971 /* Reset the I2C core to release the bus lock. */
972 pci1xxxx_i2c_init(i2c);
973 retval = -ETIMEDOUT;
974 goto cleanup;
977 regval = readb(p2);
978 if (regval & COMPLETION_MNAKX) {
979 writeb(COMPLETION_MNAKX, p2);
980 retval = -ETIMEDOUT;
981 goto cleanup;
984 cleanup:
985 /* Disable all the interrupts. */
986 pci1xxxx_config_nw_layer_intr(i2c, INTR_MSK_DMA_TERM, false);
987 pci1xxxx_i2c_config_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK, false);
989 return retval;
992 static int pci1xxxx_i2c_xfer(struct i2c_adapter *adap,
993 struct i2c_msg *msgs, int num)
995 struct pci1xxxx_i2c *i2c = i2c_get_adapdata(adap);
996 u8 slaveaddr;
997 int retval;
998 u32 i;
1000 i2c->i2c_xfer_in_progress = true;
1001 for (i = 0; i < num; i++) {
1002 slaveaddr = i2c_8bit_addr_from_msg(&msgs[i]);
1005 * Send the STOP bit if the transfer is the final one or
1006 * if the I2C_M_STOP flag is set.
1008 if ((i == num - 1) || (msgs[i].flags & I2C_M_STOP))
1009 i2c->flags |= I2C_FLAGS_STOP;
1010 else
1011 i2c->flags &= ~I2C_FLAGS_STOP;
1013 if (msgs[i].flags & I2C_M_RECV_LEN)
1014 i2c->flags |= I2C_FLAGS_SMB_BLK_READ;
1015 else
1016 i2c->flags &= ~I2C_FLAGS_SMB_BLK_READ;
1018 if (msgs[i].flags & I2C_M_RD)
1019 retval = pci1xxxx_i2c_read(i2c, slaveaddr,
1020 msgs[i].buf, msgs[i].len);
1021 else
1022 retval = pci1xxxx_i2c_write(i2c, slaveaddr,
1023 msgs[i].buf, msgs[i].len);
1025 if (retval < 0)
1026 break;
1028 i2c->i2c_xfer_in_progress = false;
1030 if (retval < 0)
1031 return retval;
1033 return num;
1037 * List of supported functions by the driver.
1039 static u32 pci1xxxx_i2c_get_funcs(struct i2c_adapter *adap)
1041 return I2C_FUNC_I2C | I2C_FUNC_PROTOCOL_MANGLING |
1042 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1043 I2C_FUNC_SMBUS_BYTE |
1044 I2C_FUNC_SMBUS_BYTE_DATA |
1045 I2C_FUNC_SMBUS_WORD_DATA |
1046 I2C_FUNC_SMBUS_PROC_CALL |
1047 I2C_FUNC_SMBUS_BLOCK_DATA;
1050 static const struct i2c_algorithm pci1xxxx_i2c_algo = {
1051 .master_xfer = pci1xxxx_i2c_xfer,
1052 .functionality = pci1xxxx_i2c_get_funcs,
1055 static const struct i2c_adapter_quirks pci1xxxx_i2c_quirks = {
1056 .flags = I2C_AQ_NO_ZERO_LEN,
1059 static const struct i2c_adapter pci1xxxx_i2c_ops = {
1060 .owner = THIS_MODULE,
1061 .name = "PCI1xxxx I2C Adapter",
1062 .algo = &pci1xxxx_i2c_algo,
1063 .quirks = &pci1xxxx_i2c_quirks,
1066 static int pci1xxxx_i2c_suspend(struct device *dev)
1068 struct pci1xxxx_i2c *i2c = dev_get_drvdata(dev);
1069 void __iomem *p = i2c->i2c_base + SMBUS_RESET_REG;
1070 struct pci_dev *pdev = to_pci_dev(dev);
1071 u32 regval;
1073 i2c_mark_adapter_suspended(&i2c->adap);
1076 * If the system is put into 'suspend' state when the I2C transfer is in
1077 * progress, wait until the transfer completes.
1079 while (i2c->i2c_xfer_in_progress)
1080 msleep(20);
1082 pci1xxxx_i2c_config_high_level_intr(i2c, SMBALERT_WAKE_INTR_MASK, true);
1085 * Enable the PERST_DIS bit to mask the PERST from resetting the core
1086 * registers.
1088 regval = readl(p);
1089 regval |= PERI_SMBUS_D3_RESET_DIS;
1090 writel(regval, p);
1092 /* Enable PCI wake in the PMCSR register. */
1093 device_set_wakeup_enable(dev, true);
1094 pci_wake_from_d3(pdev, true);
1096 return 0;
1099 static int pci1xxxx_i2c_resume(struct device *dev)
1101 struct pci1xxxx_i2c *i2c = dev_get_drvdata(dev);
1102 void __iomem *p1 = i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF;
1103 void __iomem *p2 = i2c->i2c_base + SMBUS_RESET_REG;
1104 struct pci_dev *pdev = to_pci_dev(dev);
1105 u32 regval;
1107 regval = readw(p1);
1108 writew(regval, p1);
1109 pci1xxxx_i2c_config_high_level_intr(i2c, SMBALERT_WAKE_INTR_MASK, false);
1110 regval = readl(p2);
1111 regval &= ~PERI_SMBUS_D3_RESET_DIS;
1112 writel(regval, p2);
1113 i2c_mark_adapter_resumed(&i2c->adap);
1114 pci_wake_from_d3(pdev, false);
1115 return 0;
1118 static DEFINE_SIMPLE_DEV_PM_OPS(pci1xxxx_i2c_pm_ops, pci1xxxx_i2c_suspend,
1119 pci1xxxx_i2c_resume);
1121 static void pci1xxxx_i2c_shutdown(void *data)
1123 struct pci1xxxx_i2c *i2c = data;
1125 pci1xxxx_i2c_config_padctrl(i2c, false);
1126 pci1xxxx_i2c_configure_core_reg(i2c, false);
1129 static int pci1xxxx_i2c_probe_pci(struct pci_dev *pdev,
1130 const struct pci_device_id *ent)
1132 struct device *dev = &pdev->dev;
1133 struct pci1xxxx_i2c *i2c;
1134 int ret;
1136 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
1137 if (!i2c)
1138 return -ENOMEM;
1140 pci_set_drvdata(pdev, i2c);
1141 i2c->i2c_xfer_in_progress = false;
1143 ret = pcim_enable_device(pdev);
1144 if (ret)
1145 return ret;
1147 pci_set_master(pdev);
1150 * We are getting the base address of the SMB core. SMB core uses
1151 * BAR0 and size is 32K.
1153 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
1154 if (ret < 0)
1155 return ret;
1157 i2c->i2c_base = pcim_iomap_table(pdev)[0];
1158 init_completion(&i2c->i2c_xfer_done);
1159 pci1xxxx_i2c_init(i2c);
1161 ret = devm_add_action(dev, pci1xxxx_i2c_shutdown, i2c);
1162 if (ret)
1163 return ret;
1165 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
1166 if (ret < 0)
1167 return ret;
1169 ret = devm_request_irq(dev, pci_irq_vector(pdev, 0), pci1xxxx_i2c_isr,
1170 0, pci_name(pdev), i2c);
1171 if (ret)
1172 return ret;
1174 i2c->adap = pci1xxxx_i2c_ops;
1175 i2c->adap.dev.parent = dev;
1177 snprintf(i2c->adap.name, sizeof(i2c->adap.name),
1178 "MCHP PCI1xxxx i2c adapter at %s", pci_name(pdev));
1180 i2c_set_adapdata(&i2c->adap, i2c);
1182 ret = devm_i2c_add_adapter(dev, &i2c->adap);
1183 if (ret)
1184 return dev_err_probe(dev, ret, "i2c add adapter failed\n");
1186 return 0;
1189 static const struct pci_device_id pci1xxxx_i2c_pci_id_table[] = {
1190 { PCI_VDEVICE(EFAR, 0xA003) },
1191 { PCI_VDEVICE(EFAR, 0xA013) },
1192 { PCI_VDEVICE(EFAR, 0xA023) },
1193 { PCI_VDEVICE(EFAR, 0xA033) },
1194 { PCI_VDEVICE(EFAR, 0xA043) },
1197 MODULE_DEVICE_TABLE(pci, pci1xxxx_i2c_pci_id_table);
1199 static struct pci_driver pci1xxxx_i2c_pci_driver = {
1200 .name = "i2c-mchp-pci1xxxx",
1201 .id_table = pci1xxxx_i2c_pci_id_table,
1202 .probe = pci1xxxx_i2c_probe_pci,
1203 .driver = {
1204 .pm = pm_sleep_ptr(&pci1xxxx_i2c_pm_ops),
1207 module_pci_driver(pci1xxxx_i2c_pci_driver);
1209 MODULE_LICENSE("GPL");
1210 MODULE_AUTHOR("Tharun Kumar P<tharunkumar.pasumarthi@microchip.com>");
1211 MODULE_AUTHOR("Kumaravel Thiagarajan <kumaravel.thiagarajan@microchip.com>");
1212 MODULE_DESCRIPTION("Microchip Technology Inc. pci1xxxx I2C bus driver");