18 #include <linux/module.h>
19 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/i2c.h>
26 #include <linux/types.h>
29 #include <linux/pci.h>
32 #include <linux/slab.h>
34 #define PCH_EVENT_SET 0
35 #define PCH_EVENT_NONE 1
36 #define PCH_MAX_CLK 100000
37 #define PCH_BUFFER_MODE_ENABLE 0x0002
38 #define PCH_EEPROM_SW_RST_MODE_ENABLE 0x0008
40 #define PCH_I2CSADR 0x00
41 #define PCH_I2CCTL 0x04
42 #define PCH_I2CSR 0x08
43 #define PCH_I2CDR 0x0C
44 #define PCH_I2CMON 0x10
45 #define PCH_I2CBC 0x14
46 #define PCH_I2CMOD 0x18
47 #define PCH_I2CBUFSLV 0x1C
48 #define PCH_I2CBUFSUB 0x20
49 #define PCH_I2CBUFFOR 0x24
50 #define PCH_I2CBUFCTL 0x28
51 #define PCH_I2CBUFMSK 0x2C
52 #define PCH_I2CBUFSTA 0x30
53 #define PCH_I2CBUFLEV 0x34
54 #define PCH_I2CESRFOR 0x38
55 #define PCH_I2CESRCTL 0x3C
56 #define PCH_I2CESRMSK 0x40
57 #define PCH_I2CESRSTA 0x44
58 #define PCH_I2CTMR 0x48
59 #define PCH_I2CSRST 0xFC
60 #define PCH_I2CNF 0xF8
62 #define BUS_IDLE_TIMEOUT 20
63 #define PCH_I2CCTL_I2CMEN 0x0080
64 #define TEN_BIT_ADDR_DEFAULT 0xF000
65 #define TEN_BIT_ADDR_MASK 0xF0
66 #define PCH_START 0x0020
67 #define PCH_RESTART 0x0004
68 #define PCH_ESR_START 0x0001
69 #define PCH_BUFF_START 0x1
70 #define PCH_REPSTART 0x0004
71 #define PCH_ACK 0x0008
72 #define PCH_GETACK 0x0001
75 #define I2CMCF_BIT 0x0080
76 #define I2CMIF_BIT 0x0002
77 #define I2CMAL_BIT 0x0010
78 #define I2CBMFI_BIT 0x0001
79 #define I2CBMAL_BIT 0x0002
80 #define I2CBMNA_BIT 0x0004
81 #define I2CBMTO_BIT 0x0008
82 #define I2CBMIS_BIT 0x0010
83 #define I2CESRFI_BIT 0X0001
84 #define I2CESRTO_BIT 0x0002
85 #define I2CESRFIIE_BIT 0x1
86 #define I2CESRTOIE_BIT 0x2
87 #define I2CBMDZ_BIT 0x0040
88 #define I2CBMAG_BIT 0x0020
89 #define I2CMBB_BIT 0x0020
90 #define BUFFER_MODE_MASK (I2CBMFI_BIT | I2CBMAL_BIT | I2CBMNA_BIT | \
91 I2CBMTO_BIT | I2CBMIS_BIT)
92 #define I2C_ADDR_MSK 0xFF
93 #define I2C_MSB_2B_MSK 0x300
94 #define FAST_MODE_CLK 400
95 #define FAST_MODE_EN 0x0001
96 #define SUB_ADDR_LEN_MAX 4
97 #define BUF_LEN_MAX 32
98 #define PCH_BUFFER_MODE 0x1
99 #define EEPROM_SW_RST_MODE 0x0002
100 #define NORMAL_INTR_ENBL 0x0300
101 #define EEPROM_RST_INTR_ENBL (I2CESRFIIE_BIT | I2CESRTOIE_BIT)
102 #define EEPROM_RST_INTR_DISBL 0x0
103 #define BUFFER_MODE_INTR_ENBL 0x001F
104 #define BUFFER_MODE_INTR_DISBL 0x0
105 #define NORMAL_MODE 0x0
106 #define BUFFER_MODE 0x1
107 #define EEPROM_SR_MODE 0x2
108 #define I2C_TX_MODE 0x0010
109 #define PCH_BUF_TX 0xFFF7
110 #define PCH_BUF_RD 0x0008
111 #define I2C_ERROR_MASK (I2CESRTO_EVENT | I2CBMIS_EVENT | I2CBMTO_EVENT | \
112 I2CBMNA_EVENT | I2CBMAL_EVENT | I2CMAL_EVENT)
113 #define I2CMAL_EVENT 0x0001
114 #define I2CMCF_EVENT 0x0002
115 #define I2CBMFI_EVENT 0x0004
116 #define I2CBMAL_EVENT 0x0008
117 #define I2CBMNA_EVENT 0x0010
118 #define I2CBMTO_EVENT 0x0020
119 #define I2CBMIS_EVENT 0x0040
120 #define I2CESRFI_EVENT 0x0080
121 #define I2CESRTO_EVENT 0x0100
122 #define PCI_DEVICE_ID_PCH_I2C 0x8817
124 #define pch_dbg(adap, fmt, arg...) \
125 dev_dbg(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
127 #define pch_err(adap, fmt, arg...) \
128 dev_err(adap->pch_adapter.dev.parent, "%s :" fmt, __func__, ##arg)
130 #define pch_pci_err(pdev, fmt, arg...) \
131 dev_err(&pdev->dev, "%s :" fmt, __func__, ##arg)
133 #define pch_pci_dbg(pdev, fmt, arg...) \
134 dev_dbg(&pdev->dev, "%s :" fmt, __func__, ##arg)
142 #define PCH_I2C_MAX_DEV 2
179 static int pch_i2c_speed = 100;
180 static int pch_clk = 50000;
185 #define PCI_VENDOR_ID_ROHM 0x10DB
186 #define PCI_DEVICE_ID_ML7213_I2C 0x802D
187 #define PCI_DEVICE_ID_ML7223_I2C 0x8010
188 #define PCI_DEVICE_ID_ML7831_I2C 0x8817
237 if (pch_i2c_speed != 400)
243 pch_dbg(adap,
"Fast mode enabled\n");
249 pch_i2cbc = (pch_clk + (pch_i2c_speed * 4)) / (pch_i2c_speed * 8);
253 pch_i2ctmr = (pch_clk) / 8;
260 "I2CCTL=%x pch_i2cbc=%x pch_i2ctmr=%x Enable interrupts\n",
281 pch_err(adap,
"%s: Timeout Error.return%d\n",
351 pch_err(adap,
"%s:wait-event timeout\n", __func__);
359 pch_err(adap,
"Lost Arbitration\n");
369 if (pch_i2c_getack(adap)) {
370 pch_dbg(adap,
"Receive NACK for slave address"
432 rtn = pch_i2c_wait_for_check_xfer(adap);
445 rtn = pch_i2c_wait_for_check_xfer(adap);
449 for (wrcount = 0; wrcount <
length; ++wrcount) {
452 pch_dbg(adap,
"writing %x to Data register\n", buf[wrcount]);
454 rtn = pch_i2c_wait_for_check_xfer(adap);
466 pch_i2c_repstart(adap);
468 pch_dbg(adap,
"return=%d\n", wrcount);
547 rtn = pch_i2c_wait_for_check_xfer(adap);
554 pch_i2c_restart(adap);
556 rtn = pch_i2c_wait_for_check_xfer(adap);
572 rtn = pch_i2c_wait_for_check_xfer(adap);
584 pch_i2c_sendack(adap);
587 for (loop = 1, read_index = 0; loop <
length; loop++) {
593 rtn = pch_i2c_wait_for_check_xfer(adap);
598 pch_i2c_sendnack(adap);
605 rtn = pch_i2c_wait_for_check_xfer(adap);
612 pch_i2c_repstart(adap);
660 for (i = 0, flag = 0; i < adap_info->
ch_num; i++) {
661 p = adap_info->
pch_data[
i].pch_base_address;
666 "I2C-%d mode(%d) is not supported\n", mode, i);
670 if (reg_val & (I2CMAL_BIT | I2CMCF_BIT |
I2CMIF_BIT)) {
671 pch_i2c_cb(&adap_info->
pch_data[i]);
704 pch_dbg(adap,
"adap->p_adapter_info->pch_i2c_suspended is %d\n",
709 for (i = 0; i < num && ret >= 0; i++) {
712 status = pmsg->
flags;
714 "After invoking I2C_MODE_SEL :flag= 0x%x\n", status);
716 if ((status & (
I2C_M_RD)) !=
false) {
717 ret = pch_i2c_readbytes(i2c_adap, pmsg, (i + 1 == num),
720 ret = pch_i2c_writebytes(i2c_adap, pmsg, (i + 1 == num),
729 return (ret < 0) ? ret : num;
742 .master_xfer = pch_i2c_xfer,
743 .functionality = pch_i2c_func
773 if (adap_info ==
NULL) {
790 base_addr = pci_iomap(pdev, 1, 0);
792 if (base_addr ==
NULL) {
799 adap_info->
ch_num =
id->driver_data;
802 KBUILD_MODNAME, adap_info);
805 goto err_request_irq;
808 for (i = 0; i < adap_info->
ch_num; i++) {
809 pch_adap = &adap_info->
pch_data[
i].pch_adapter;
812 adap_info->
pch_data[
i].p_adapter_info = adap_info;
817 pch_adap->
algo = &pch_algorithm;
821 adap_info->
pch_data[
i].pch_base_address = base_addr + 0x100 *
i;
823 pch_adap->
dev.parent = &pdev->
dev;
825 pch_i2c_init(&adap_info->
pch_data[i]);
830 pch_pci_err(pdev,
"i2c_add_adapter[ch:%d] FAILED\n", i);
831 goto err_add_adapter;
835 pci_set_drvdata(pdev, adap_info);
840 for (j = 0; j <
i; j++)
861 for (i = 0; i < adap_info->
ch_num; i++) {
862 pch_i2c_disbl_int(&adap_info->
pch_data[i]);
866 if (adap_info->
pch_data[0].pch_base_address)
869 for (i = 0; i < adap_info->
ch_num; i++)
872 pci_set_drvdata(pdev,
NULL);
890 for (i = 0; i < adap_info->
ch_num; i++) {
891 while ((adap_info->
pch_data[i].pch_i2c_xfer_in_progress)) {
898 for (i = 0; i < adap_info->
ch_num; i++)
899 pch_i2c_disbl_int(&adap_info->
pch_data[i]);
901 pch_pci_dbg(pdev,
"I2CSR = %x I2CBUFSTA = %x I2CESRSTA = %x "
902 "invoked function pch_i2c_disbl_int successfully\n",
929 pch_pci_err(pdev,
"pch_i2c_resume:pci_enable_device FAILED\n");
935 for (i = 0; i < adap_info->
ch_num; i++)
936 pch_i2c_init(&adap_info->
pch_data[i]);
943 #define pch_i2c_suspend NULL
944 #define pch_i2c_resume NULL
948 .name = KBUILD_MODNAME,
949 .id_table = pch_pcidev_id,
950 .probe = pch_i2c_probe,