2 * Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/errno.h>
23 #include <linux/i2c.h>
26 #include <linux/types.h>
27 #include <linux/interrupt.h>
28 #include <linux/jiffies.h>
29 #include <linux/pci.h>
30 #include <linux/mutex.h>
31 #include <linux/ktime.h>
33 #define PCH_EVENT_SET 0 /* I2C Interrupt Event Set Status */
34 #define PCH_EVENT_NONE 1 /* I2C Interrupt Event Clear Status */
35 #define PCH_MAX_CLK 100000 /* Maximum Clock speed in MHz */
36 #define PCH_BUFFER_MODE_ENABLE 0x0002 /* flag for Buffer mode enable */
37 #define PCH_EEPROM_SW_RST_MODE_ENABLE 0x0008 /* EEPROM SW RST enable flag */
39 #define PCH_I2CSADR 0x00 /* I2C slave address register */
40 #define PCH_I2CCTL 0x04 /* I2C control register */
41 #define PCH_I2CSR 0x08 /* I2C status register */
42 #define PCH_I2CDR 0x0C /* I2C data register */
43 #define PCH_I2CMON 0x10 /* I2C bus monitor register */
44 #define PCH_I2CBC 0x14 /* I2C bus transfer rate setup counter */
45 #define PCH_I2CMOD 0x18 /* I2C mode register */
46 #define PCH_I2CBUFSLV 0x1C /* I2C buffer mode slave address register */
47 #define PCH_I2CBUFSUB 0x20 /* I2C buffer mode subaddress register */
48 #define PCH_I2CBUFFOR 0x24 /* I2C buffer mode format register */
49 #define PCH_I2CBUFCTL 0x28 /* I2C buffer mode control register */
50 #define PCH_I2CBUFMSK 0x2C /* I2C buffer mode interrupt mask register */
51 #define PCH_I2CBUFSTA 0x30 /* I2C buffer mode status register */
52 #define PCH_I2CBUFLEV 0x34 /* I2C buffer mode level register */
53 #define PCH_I2CESRFOR 0x38 /* EEPROM software reset mode format register */
54 #define PCH_I2CESRCTL 0x3C /* EEPROM software reset mode ctrl register */
55 #define PCH_I2CESRMSK 0x40 /* EEPROM software reset mode */
56 #define PCH_I2CESRSTA 0x44 /* EEPROM software reset mode status register */
57 #define PCH_I2CTMR 0x48 /* I2C timer register */
58 #define PCH_I2CSRST 0xFC /* I2C reset register */
59 #define PCH_I2CNF 0xF8 /* I2C noise filter register */
61 #define BUS_IDLE_TIMEOUT 20
62 #define PCH_I2CCTL_I2CMEN 0x0080
63 #define TEN_BIT_ADDR_DEFAULT 0xF000
64 #define TEN_BIT_ADDR_MASK 0xF0
65 #define PCH_START 0x0020
66 #define PCH_ESR_START 0x0001
67 #define PCH_BUFF_START 0x1
68 #define PCH_REPSTART 0x0004
69 #define PCH_ACK 0x0008
70 #define PCH_GETACK 0x0001
73 #define I2CMCF_BIT 0x0080
74 #define I2CMIF_BIT 0x0002
75 #define I2CMAL_BIT 0x0010
76 #define I2CBMFI_BIT 0x0001
77 #define I2CBMAL_BIT 0x0002
78 #define I2CBMNA_BIT 0x0004
79 #define I2CBMTO_BIT 0x0008
80 #define I2CBMIS_BIT 0x0010
81 #define I2CESRFI_BIT 0X0001
82 #define I2CESRTO_BIT 0x0002
83 #define I2CESRFIIE_BIT 0x1
84 #define I2CESRTOIE_BIT 0x2
85 #define I2CBMDZ_BIT 0x0040
86 #define I2CBMAG_BIT 0x0020
87 #define I2CMBB_BIT 0x0020
88 #define BUFFER_MODE_MASK (I2CBMFI_BIT | I2CBMAL_BIT | I2CBMNA_BIT | \
89 I2CBMTO_BIT | I2CBMIS_BIT)
90 #define I2C_ADDR_MSK 0xFF
91 #define I2C_MSB_2B_MSK 0x300
92 #define FAST_MODE_CLK 400
93 #define FAST_MODE_EN 0x0001
94 #define SUB_ADDR_LEN_MAX 4
95 #define BUF_LEN_MAX 32
96 #define PCH_BUFFER_MODE 0x1
97 #define EEPROM_SW_RST_MODE 0x0002
98 #define NORMAL_INTR_ENBL 0x0300
99 #define EEPROM_RST_INTR_ENBL (I2CESRFIIE_BIT | I2CESRTOIE_BIT)
100 #define EEPROM_RST_INTR_DISBL 0x0
101 #define BUFFER_MODE_INTR_ENBL 0x001F
102 #define BUFFER_MODE_INTR_DISBL 0x0
103 #define NORMAL_MODE 0x0
104 #define BUFFER_MODE 0x1
105 #define EEPROM_SR_MODE 0x2
106 #define I2C_TX_MODE 0x0010
107 #define PCH_BUF_TX 0xFFF7
108 #define PCH_BUF_RD 0x0008
109 #define I2C_ERROR_MASK (I2CESRTO_EVENT | I2CBMIS_EVENT | I2CBMTO_EVENT | \
110 I2CBMNA_EVENT | I2CBMAL_EVENT | I2CMAL_EVENT)
111 #define I2CMAL_EVENT 0x0001
112 #define I2CMCF_EVENT 0x0002
113 #define I2CBMFI_EVENT 0x0004
114 #define I2CBMAL_EVENT 0x0008
115 #define I2CBMNA_EVENT 0x0010
116 #define I2CBMTO_EVENT 0x0020
117 #define I2CBMIS_EVENT 0x0040
118 #define I2CESRFI_EVENT 0x0080
119 #define I2CESRTO_EVENT 0x0100
120 #define PCI_DEVICE_ID_PCH_I2C 0x8817
122 #define pch_dbg(adap, fmt, arg...) \
123 dev_dbg(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
125 #define pch_err(adap, fmt, arg...) \
126 dev_err(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
128 #define pch_pci_err(pdev, fmt, arg...) \
129 dev_err(&pdev->dev, "%s :" fmt, __func__, ##arg)
131 #define pch_pci_dbg(pdev, fmt, arg...) \
132 dev_dbg(&pdev->dev, "%s :" fmt, __func__, ##arg)
135 * struct i2c_algo_pch_data - for I2C driver functionalities
136 * @pch_adapter: stores the reference to i2c_adapter structure
137 * @p_adapter_info: stores the reference to adapter_info structure
138 * @pch_base_address: specifies the remapped base address
139 * @pch_buff_mode_en: specifies if buffer mode is enabled
140 * @pch_event_flag: specifies occurrence of interrupt events
141 * @pch_i2c_xfer_in_progress: specifies whether the transfer is completed
143 struct i2c_algo_pch_data {
144 struct i2c_adapter pch_adapter;
145 struct adapter_info *p_adapter_info;
146 void __iomem *pch_base_address;
147 int pch_buff_mode_en;
149 bool pch_i2c_xfer_in_progress;
153 * struct adapter_info - This structure holds the adapter information for the
155 * @pch_data: stores a list of i2c_algo_pch_data
156 * @pch_i2c_suspended: specifies whether the system is suspended or not
157 * perhaps with more lines and words.
159 * pch_data has as many elements as maximum I2C channels
161 struct adapter_info {
162 struct i2c_algo_pch_data pch_data;
163 bool pch_i2c_suspended;
167 static int pch_i2c_speed = 100; /* I2C bus speed in Kbps */
168 static int pch_clk = 50000; /* specifies I2C clock speed in KHz */
169 static wait_queue_head_t pch_event;
170 static DEFINE_MUTEX(pch_mutex);
172 static struct pci_device_id __devinitdata pch_pcidev_id[] = {
173 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PCH_I2C)},
177 static irqreturn_t pch_i2c_handler(int irq, void *pData);
179 static inline void pch_setbit(void __iomem *addr, u32 offset, u32 bitmask)
182 val = ioread32(addr + offset);
184 iowrite32(val, addr + offset);
187 static inline void pch_clrbit(void __iomem *addr, u32 offset, u32 bitmask)
190 val = ioread32(addr + offset);
192 iowrite32(val, addr + offset);
196 * pch_i2c_init() - hardware initialization of I2C module
197 * @adap: Pointer to struct i2c_algo_pch_data.
199 static void pch_i2c_init(struct i2c_algo_pch_data *adap)
201 void __iomem *p = adap->pch_base_address;
206 /* reset I2C controller */
207 iowrite32(0x01, p + PCH_I2CSRST);
209 iowrite32(0x0, p + PCH_I2CSRST);
211 /* Initialize I2C registers */
212 iowrite32(0x21, p + PCH_I2CNF);
214 pch_setbit(adap->pch_base_address, PCH_I2CCTL,
217 if (pch_i2c_speed != 400)
220 reg_value = PCH_I2CCTL_I2CMEN;
221 if (pch_i2c_speed == FAST_MODE_CLK) {
222 reg_value |= FAST_MODE_EN;
223 pch_dbg(adap, "Fast mode enabled\n");
226 if (pch_clk > PCH_MAX_CLK)
229 pch_i2cbc = (pch_clk + (pch_i2c_speed * 4)) / pch_i2c_speed * 8;
230 /* Set transfer speed in I2CBC */
231 iowrite32(pch_i2cbc, p + PCH_I2CBC);
233 pch_i2ctmr = (pch_clk) / 8;
234 iowrite32(pch_i2ctmr, p + PCH_I2CTMR);
236 reg_value |= NORMAL_INTR_ENBL; /* Enable interrupts in normal mode */
237 iowrite32(reg_value, p + PCH_I2CCTL);
240 "I2CCTL=%x pch_i2cbc=%x pch_i2ctmr=%x Enable interrupts\n",
241 ioread32(p + PCH_I2CCTL), pch_i2cbc, pch_i2ctmr);
243 init_waitqueue_head(&pch_event);
246 static inline bool ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
248 return cmp1.tv64 < cmp2.tv64;
252 * pch_i2c_wait_for_bus_idle() - check the status of bus.
253 * @adap: Pointer to struct i2c_algo_pch_data.
254 * @timeout: waiting time counter (us).
256 static s32 pch_i2c_wait_for_bus_idle(struct i2c_algo_pch_data *adap,
259 void __iomem *p = adap->pch_base_address;
261 /* MAX timeout value is timeout*1000*1000nsec */
262 ktime_t ns_val = ktime_add_ns(ktime_get(), timeout*1000*1000);
264 if ((ioread32(p + PCH_I2CSR) & I2CMBB_BIT) == 0)
267 } while (ktime_lt(ktime_get(), ns_val));
269 pch_dbg(adap, "I2CSR = %x\n", ioread32(p + PCH_I2CSR));
272 pch_err(adap, "%s: Timeout Error.return%d\n", __func__, -ETIME);
280 * pch_i2c_start() - Generate I2C start condition in normal mode.
281 * @adap: Pointer to struct i2c_algo_pch_data.
283 * Generate I2C start condition in normal mode by setting I2CCTL.I2CMSTA to 1.
285 static void pch_i2c_start(struct i2c_algo_pch_data *adap)
287 void __iomem *p = adap->pch_base_address;
288 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
289 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
293 * pch_i2c_wait_for_xfer_complete() - initiates a wait for the tx complete event
294 * @adap: Pointer to struct i2c_algo_pch_data.
296 static s32 pch_i2c_wait_for_xfer_complete(struct i2c_algo_pch_data *adap)
299 ret = wait_event_timeout(pch_event,
300 (adap->pch_event_flag != 0), msecs_to_jiffies(50));
302 pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
307 pch_err(adap, "timeout: %x\n", adap->pch_event_flag);
311 if (adap->pch_event_flag & I2C_ERROR_MASK) {
312 pch_err(adap, "error bits set: %x\n", adap->pch_event_flag);
316 adap->pch_event_flag = 0;
322 * pch_i2c_getack() - to confirm ACK/NACK
323 * @adap: Pointer to struct i2c_algo_pch_data.
325 static s32 pch_i2c_getack(struct i2c_algo_pch_data *adap)
328 void __iomem *p = adap->pch_base_address;
329 reg_val = ioread32(p + PCH_I2CSR) & PCH_GETACK;
332 pch_err(adap, "return%d\n", -EPROTO);
340 * pch_i2c_stop() - generate stop condition in normal mode.
341 * @adap: Pointer to struct i2c_algo_pch_data.
343 static void pch_i2c_stop(struct i2c_algo_pch_data *adap)
345 void __iomem *p = adap->pch_base_address;
346 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
347 /* clear the start bit */
348 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_START);
352 * pch_i2c_repstart() - generate repeated start condition in normal mode
353 * @adap: Pointer to struct i2c_algo_pch_data.
355 static void pch_i2c_repstart(struct i2c_algo_pch_data *adap)
357 void __iomem *p = adap->pch_base_address;
358 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
359 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_REPSTART);
363 * pch_i2c_writebytes() - write data to I2C bus in normal mode
364 * @i2c_adap: Pointer to the struct i2c_adapter.
365 * @last: specifies whether last message or not.
366 * In the case of compound mode it will be 1 for last message,
368 * @first: specifies whether first message or not.
369 * 1 for first message otherwise 0.
371 static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,
372 struct i2c_msg *msgs, u32 last, u32 first)
374 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
381 void __iomem *p = adap->pch_base_address;
387 /* enable master tx */
388 pch_setbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
390 pch_dbg(adap, "I2CCTL = %x msgs->len = %d\n", ioread32(p + PCH_I2CCTL),
394 if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
398 if (msgs->flags & I2C_M_TEN) {
399 addr_2_msb = ((addr & I2C_MSB_2B_MSK) >> 7);
400 iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
403 if (pch_i2c_wait_for_xfer_complete(adap) == 0 &&
404 pch_i2c_getack(adap) == 0) {
405 addr_8_lsb = (addr & I2C_ADDR_MSK);
406 iowrite32(addr_8_lsb, p + PCH_I2CDR);
412 /* set 7 bit slave address and R/W bit as 0 */
413 iowrite32(addr << 1, p + PCH_I2CDR);
418 if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
419 (pch_i2c_getack(adap) == 0)) {
420 for (wrcount = 0; wrcount < length; ++wrcount) {
421 /* write buffer value to I2C data register */
422 iowrite32(buf[wrcount], p + PCH_I2CDR);
423 pch_dbg(adap, "writing %x to Data register\n",
426 if (pch_i2c_wait_for_xfer_complete(adap) != 0)
429 if (pch_i2c_getack(adap))
433 /* check if this is the last message */
437 pch_i2c_repstart(adap);
443 pch_dbg(adap, "return=%d\n", wrcount);
449 * pch_i2c_sendack() - send ACK
450 * @adap: Pointer to struct i2c_algo_pch_data.
452 static void pch_i2c_sendack(struct i2c_algo_pch_data *adap)
454 void __iomem *p = adap->pch_base_address;
455 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
456 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
460 * pch_i2c_sendnack() - send NACK
461 * @adap: Pointer to struct i2c_algo_pch_data.
463 static void pch_i2c_sendnack(struct i2c_algo_pch_data *adap)
465 void __iomem *p = adap->pch_base_address;
466 pch_dbg(adap, "I2CCTL = %x\n", ioread32(p + PCH_I2CCTL));
467 pch_setbit(adap->pch_base_address, PCH_I2CCTL, PCH_ACK);
471 * pch_i2c_readbytes() - read data from I2C bus in normal mode.
472 * @i2c_adap: Pointer to the struct i2c_adapter.
473 * @msgs: Pointer to i2c_msg structure.
474 * @last: specifies whether last message or not.
475 * @first: specifies whether first message or not.
477 s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
480 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
487 void __iomem *p = adap->pch_base_address;
493 /* enable master reception */
494 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, I2C_TX_MODE);
497 if (pch_i2c_wait_for_bus_idle(adap, BUS_IDLE_TIMEOUT) == -ETIME)
501 if (msgs->flags & I2C_M_TEN) {
502 addr_2_msb = (((addr & I2C_MSB_2B_MSK) >> 7) | (I2C_RD));
503 iowrite32(addr_2_msb | TEN_BIT_ADDR_MASK, p + PCH_I2CDR);
506 /* 7 address bits + R/W bit */
507 addr = (((addr) << 1) | (I2C_RD));
508 iowrite32(addr, p + PCH_I2CDR);
511 /* check if it is the first message */
515 if ((pch_i2c_wait_for_xfer_complete(adap) == 0) &&
516 (pch_i2c_getack(adap) == 0)) {
517 pch_dbg(adap, "return %d\n", 0);
521 ioread32(p + PCH_I2CDR); /* Dummy read needs */
527 pch_i2c_sendack(adap);
530 for (loop = 1, read_index = 0; loop < length; loop++) {
531 buf[read_index] = ioread32(p + PCH_I2CDR);
536 if (pch_i2c_wait_for_xfer_complete(adap) != 0) {
542 pch_i2c_sendnack(adap);
544 buf[read_index] = ioread32(p + PCH_I2CDR);
549 if (pch_i2c_wait_for_xfer_complete(adap) == 0) {
553 pch_i2c_repstart(adap);
555 buf[read_index++] = ioread32(p + PCH_I2CDR);
571 * pch_i2c_cb_ch0() - Interrupt handler Call back function
572 * @adap: Pointer to struct i2c_algo_pch_data.
574 static void pch_i2c_cb_ch0(struct i2c_algo_pch_data *adap)
577 void __iomem *p = adap->pch_base_address;
579 sts = ioread32(p + PCH_I2CSR);
580 sts &= (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT);
581 if (sts & I2CMAL_BIT)
582 adap->pch_event_flag |= I2CMAL_EVENT;
584 if (sts & I2CMCF_BIT)
585 adap->pch_event_flag |= I2CMCF_EVENT;
587 /* clear the applicable bits */
588 pch_clrbit(adap->pch_base_address, PCH_I2CSR, sts);
590 pch_dbg(adap, "PCH_I2CSR = %x\n", ioread32(p + PCH_I2CSR));
596 * pch_i2c_handler() - interrupt handler for the PCH I2C controller
598 * @pData: cookie passed back to the handler function.
600 static irqreturn_t pch_i2c_handler(int irq, void *pData)
604 struct i2c_algo_pch_data *adap_data = (struct i2c_algo_pch_data *)pData;
605 void __iomem *p = adap_data->pch_base_address;
606 u32 mode = ioread32(p + PCH_I2CMOD) & (BUFFER_MODE | EEPROM_SR_MODE);
608 if (mode != NORMAL_MODE) {
609 pch_err(adap_data, "I2C mode is not supported\n");
613 reg_val = ioread32(p + PCH_I2CSR);
614 if (reg_val & (I2CMAL_BIT | I2CMCF_BIT | I2CMIF_BIT))
615 pch_i2c_cb_ch0(adap_data);
623 * pch_i2c_xfer() - Reading adnd writing data through I2C bus
624 * @i2c_adap: Pointer to the struct i2c_adapter.
625 * @msgs: Pointer to i2c_msg structure.
626 * @num: number of messages.
628 static s32 pch_i2c_xfer(struct i2c_adapter *i2c_adap,
629 struct i2c_msg *msgs, s32 num)
631 struct i2c_msg *pmsg;
638 struct i2c_algo_pch_data *adap = i2c_adap->algo_data;
640 ret = mutex_lock_interruptible(&pch_mutex);
644 if (adap->p_adapter_info->pch_i2c_suspended) {
645 mutex_unlock(&pch_mutex);
649 pch_dbg(adap, "adap->p_adapter_info->pch_i2c_suspended is %d\n",
650 adap->p_adapter_info->pch_i2c_suspended);
651 /* transfer not completed */
652 adap->pch_i2c_xfer_in_progress = true;
655 pmsg->flags |= adap->pch_buff_mode_en;
656 status = pmsg->flags;
658 "After invoking I2C_MODE_SEL :flag= 0x%x\n", status);
659 /* calculate sub address length and message length */
660 /* these are applicable only for buffer mode */
661 subaddrlen = pmsg->buf[0];
662 /* calculate actual message length excluding
663 * the sub address fields */
664 msglen = (pmsg->len) - (subaddrlen + 1);
665 if (status & (I2C_M_RD)) {
666 pch_dbg(adap, "invoking pch_i2c_readbytes\n");
667 ret = pch_i2c_readbytes(i2c_adap, pmsg, (i + 1 == num),
670 pch_dbg(adap, "invoking pch_i2c_writebytes\n");
671 ret = pch_i2c_writebytes(i2c_adap, pmsg, (i + 1 == num),
675 adap->pch_i2c_xfer_in_progress = false; /* transfer completed */
677 mutex_unlock(&pch_mutex);
683 * pch_i2c_func() - return the functionality of the I2C driver
684 * @adap: Pointer to struct i2c_algo_pch_data.
686 static u32 pch_i2c_func(struct i2c_adapter *adap)
688 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
691 static struct i2c_algorithm pch_algorithm = {
692 .master_xfer = pch_i2c_xfer,
693 .functionality = pch_i2c_func
697 * pch_i2c_disbl_int() - Disable PCH I2C interrupts
698 * @adap: Pointer to struct i2c_algo_pch_data.
700 static void pch_i2c_disbl_int(struct i2c_algo_pch_data *adap)
702 void __iomem *p = adap->pch_base_address;
704 pch_clrbit(adap->pch_base_address, PCH_I2CCTL, NORMAL_INTR_ENBL);
706 iowrite32(EEPROM_RST_INTR_DISBL, p + PCH_I2CESRMSK);
708 iowrite32(BUFFER_MODE_INTR_DISBL, p + PCH_I2CBUFMSK);
711 static int __devinit pch_i2c_probe(struct pci_dev *pdev,
712 const struct pci_device_id *id)
714 void __iomem *base_addr;
716 struct adapter_info *adap_info;
718 pch_pci_dbg(pdev, "Entered.\n");
720 adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL);
721 if (adap_info == NULL) {
722 pch_pci_err(pdev, "Memory allocation FAILED\n");
726 ret = pci_enable_device(pdev);
728 pch_pci_err(pdev, "pci_enable_device FAILED\n");
732 ret = pci_request_regions(pdev, KBUILD_MODNAME);
734 pch_pci_err(pdev, "pci_request_regions FAILED\n");
738 base_addr = pci_iomap(pdev, 1, 0);
740 if (base_addr == NULL) {
741 pch_pci_err(pdev, "pci_iomap FAILED\n");
746 adap_info->pch_i2c_suspended = false;
748 adap_info->pch_data.p_adapter_info = adap_info;
750 adap_info->pch_data.pch_adapter.owner = THIS_MODULE;
751 adap_info->pch_data.pch_adapter.class = I2C_CLASS_HWMON;
752 strcpy(adap_info->pch_data.pch_adapter.name, KBUILD_MODNAME);
753 adap_info->pch_data.pch_adapter.algo = &pch_algorithm;
754 adap_info->pch_data.pch_adapter.algo_data =
755 &adap_info->pch_data;
757 /* (i * 0x80) + base_addr; */
758 adap_info->pch_data.pch_base_address = base_addr;
760 adap_info->pch_data.pch_adapter.dev.parent = &pdev->dev;
762 ret = i2c_add_adapter(&(adap_info->pch_data.pch_adapter));
765 pch_pci_err(pdev, "i2c_add_adapter FAILED\n");
766 goto err_i2c_add_adapter;
769 pch_i2c_init(&adap_info->pch_data);
770 ret = request_irq(pdev->irq, pch_i2c_handler, IRQF_SHARED,
771 KBUILD_MODNAME, &adap_info->pch_data);
773 pch_pci_err(pdev, "request_irq FAILED\n");
774 goto err_request_irq;
777 pci_set_drvdata(pdev, adap_info);
778 pch_pci_dbg(pdev, "returns %d.\n", ret);
782 i2c_del_adapter(&(adap_info->pch_data.pch_adapter));
784 pci_iounmap(pdev, base_addr);
786 pci_release_regions(pdev);
788 pci_disable_device(pdev);
794 static void __devexit pch_i2c_remove(struct pci_dev *pdev)
796 struct adapter_info *adap_info = pci_get_drvdata(pdev);
798 pch_i2c_disbl_int(&adap_info->pch_data);
799 free_irq(pdev->irq, &adap_info->pch_data);
800 i2c_del_adapter(&(adap_info->pch_data.pch_adapter));
802 if (adap_info->pch_data.pch_base_address) {
803 pci_iounmap(pdev, adap_info->pch_data.pch_base_address);
804 adap_info->pch_data.pch_base_address = 0;
807 pci_set_drvdata(pdev, NULL);
809 pci_release_regions(pdev);
811 pci_disable_device(pdev);
816 static int pch_i2c_suspend(struct pci_dev *pdev, pm_message_t state)
819 struct adapter_info *adap_info = pci_get_drvdata(pdev);
820 void __iomem *p = adap_info->pch_data.pch_base_address;
822 adap_info->pch_i2c_suspended = true;
824 while ((adap_info->pch_data.pch_i2c_xfer_in_progress)) {
825 /* Wait until all channel transfers are completed */
828 /* Disable the i2c interrupts */
829 pch_i2c_disbl_int(&adap_info->pch_data);
831 pch_pci_dbg(pdev, "I2CSR = %x I2CBUFSTA = %x I2CESRSTA = %x "
832 "invoked function pch_i2c_disbl_int successfully\n",
833 ioread32(p + PCH_I2CSR), ioread32(p + PCH_I2CBUFSTA),
834 ioread32(p + PCH_I2CESRSTA));
836 ret = pci_save_state(pdev);
839 pch_pci_err(pdev, "pci_save_state\n");
843 pci_enable_wake(pdev, PCI_D3hot, 0);
844 pci_disable_device(pdev);
845 pci_set_power_state(pdev, pci_choose_state(pdev, state));
850 static int pch_i2c_resume(struct pci_dev *pdev)
852 struct adapter_info *adap_info = pci_get_drvdata(pdev);
854 pci_set_power_state(pdev, PCI_D0);
855 pci_restore_state(pdev);
857 if (pci_enable_device(pdev) < 0) {
858 pch_pci_err(pdev, "pch_i2c_resume:pci_enable_device FAILED\n");
862 pci_enable_wake(pdev, PCI_D3hot, 0);
864 pch_i2c_init(&adap_info->pch_data);
866 adap_info->pch_i2c_suspended = false;
871 #define pch_i2c_suspend NULL
872 #define pch_i2c_resume NULL
875 static struct pci_driver pch_pcidriver = {
876 .name = KBUILD_MODNAME,
877 .id_table = pch_pcidev_id,
878 .probe = pch_i2c_probe,
879 .remove = __devexit_p(pch_i2c_remove),
880 .suspend = pch_i2c_suspend,
881 .resume = pch_i2c_resume
884 static int __init pch_pci_init(void)
886 return pci_register_driver(&pch_pcidriver);
888 module_init(pch_pci_init);
890 static void __exit pch_pci_exit(void)
892 pci_unregister_driver(&pch_pcidriver);
894 module_exit(pch_pci_exit);
896 MODULE_DESCRIPTION("PCH I2C PCI Driver");
897 MODULE_LICENSE("GPL");
898 MODULE_AUTHOR("Tomoya MORINAGA. <tomoya-linux@dsn.okisemi.com>");
899 module_param(pch_i2c_speed, int, (S_IRUSR | S_IWUSR));
900 module_param(pch_clk, int, (S_IRUSR | S_IWUSR));