2 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
3 Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
5 Copyright (C) 2007 - 2012 Jean Delvare <khali@linux-fr.org>
6 Copyright (C) 2010 Intel Corporation,
7 David Woodhouse <dwmw2@infradead.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Supports the following Intel I/O Controller Hubs (ICH):
28 region SMBus Block proc. block
29 Chip name PCI ID size PEC buffer call read
30 ----------------------------------------------------------------------
31 82801AA (ICH) 0x2413 16 no no no no
32 82801AB (ICH0) 0x2423 16 no no no no
33 82801BA (ICH2) 0x2443 16 no no no no
34 82801CA (ICH3) 0x2483 32 soft no no no
35 82801DB (ICH4) 0x24c3 32 hard yes no no
36 82801E (ICH5) 0x24d3 32 hard yes yes yes
37 6300ESB 0x25a4 32 hard yes yes yes
38 82801F (ICH6) 0x266a 32 hard yes yes yes
39 6310ESB/6320ESB 0x269b 32 hard yes yes yes
40 82801G (ICH7) 0x27da 32 hard yes yes yes
41 82801H (ICH8) 0x283e 32 hard yes yes yes
42 82801I (ICH9) 0x2930 32 hard yes yes yes
43 EP80579 (Tolapai) 0x5032 32 hard yes yes yes
44 ICH10 0x3a30 32 hard yes yes yes
45 ICH10 0x3a60 32 hard yes yes yes
46 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
47 6 Series (PCH) 0x1c22 32 hard yes yes yes
48 Patsburg (PCH) 0x1d22 32 hard yes yes yes
49 Patsburg (PCH) IDF 0x1d70 32 hard yes yes yes
50 Patsburg (PCH) IDF 0x1d71 32 hard yes yes yes
51 Patsburg (PCH) IDF 0x1d72 32 hard yes yes yes
52 DH89xxCC (PCH) 0x2330 32 hard yes yes yes
53 Panther Point (PCH) 0x1e22 32 hard yes yes yes
54 Lynx Point (PCH) 0x8c22 32 hard yes yes yes
55 Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
56 Avoton (SOC) 0x1f3c 32 hard yes yes yes
58 Features supported by this driver:
62 Block process call transaction no
63 I2C block read transaction yes (doesn't use the block buffer)
65 Interrupt processing yes
67 See the file Documentation/i2c/busses/i2c-i801 for details.
70 #include <linux/interrupt.h>
71 #include <linux/module.h>
72 #include <linux/pci.h>
73 #include <linux/kernel.h>
74 #include <linux/stddef.h>
75 #include <linux/delay.h>
76 #include <linux/ioport.h>
77 #include <linux/init.h>
78 #include <linux/i2c.h>
79 #include <linux/acpi.h>
81 #include <linux/dmi.h>
82 #include <linux/slab.h>
83 #include <linux/wait.h>
84 #include <linux/err.h>
85 #include <linux/of_i2c.h>
87 #if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
89 #include <linux/gpio.h>
90 #include <linux/i2c-mux-gpio.h>
91 #include <linux/platform_device.h>
94 /* I801 SMBus address offsets */
95 #define SMBHSTSTS(p) (0 + (p)->smba)
96 #define SMBHSTCNT(p) (2 + (p)->smba)
97 #define SMBHSTCMD(p) (3 + (p)->smba)
98 #define SMBHSTADD(p) (4 + (p)->smba)
99 #define SMBHSTDAT0(p) (5 + (p)->smba)
100 #define SMBHSTDAT1(p) (6 + (p)->smba)
101 #define SMBBLKDAT(p) (7 + (p)->smba)
102 #define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
103 #define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
104 #define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
106 /* PCI Address Constants */
108 #define SMBPCISTS 0x006
109 #define SMBHSTCFG 0x040
111 /* Host status bits for SMBPCISTS */
112 #define SMBPCISTS_INTS 0x08
114 /* Host configuration bits for SMBHSTCFG */
115 #define SMBHSTCFG_HST_EN 1
116 #define SMBHSTCFG_SMB_SMI_EN 2
117 #define SMBHSTCFG_I2C_EN 4
119 /* Auxiliary control register bits, ICH4+ only */
120 #define SMBAUXCTL_CRC 1
121 #define SMBAUXCTL_E32B 2
124 #define MAX_RETRIES 400
126 /* I801 command constants */
127 #define I801_QUICK 0x00
128 #define I801_BYTE 0x04
129 #define I801_BYTE_DATA 0x08
130 #define I801_WORD_DATA 0x0C
131 #define I801_PROC_CALL 0x10 /* unimplemented */
132 #define I801_BLOCK_DATA 0x14
133 #define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
135 /* I801 Host Control register bits */
136 #define SMBHSTCNT_INTREN 0x01
137 #define SMBHSTCNT_KILL 0x02
138 #define SMBHSTCNT_LAST_BYTE 0x20
139 #define SMBHSTCNT_START 0x40
140 #define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
142 /* I801 Hosts Status register bits */
143 #define SMBHSTSTS_BYTE_DONE 0x80
144 #define SMBHSTSTS_INUSE_STS 0x40
145 #define SMBHSTSTS_SMBALERT_STS 0x20
146 #define SMBHSTSTS_FAILED 0x10
147 #define SMBHSTSTS_BUS_ERR 0x08
148 #define SMBHSTSTS_DEV_ERR 0x04
149 #define SMBHSTSTS_INTR 0x02
150 #define SMBHSTSTS_HOST_BUSY 0x01
152 #define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
155 #define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
158 /* Older devices have their ID defined in <linux/pci_ids.h> */
159 #define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
160 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
161 /* Patsburg also has three 'Integrated Device Function' SMBus controllers */
162 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
163 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
164 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
165 #define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
166 #define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
167 #define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
168 #define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
169 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
170 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
172 struct i801_mux_config {
177 unsigned gpios[2]; /* Relative to gpio_chip->base */
182 struct i2c_adapter adapter;
184 unsigned char original_hstcfg;
185 struct pci_dev *pci_dev;
186 unsigned int features;
189 wait_queue_head_t waitq;
192 /* Command state used by isr for byte-by-byte block transactions */
199 #if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
201 const struct i801_mux_config *mux_drvdata;
202 struct platform_device *mux_pdev;
206 static struct pci_driver i801_driver;
208 #define FEATURE_SMBUS_PEC (1 << 0)
209 #define FEATURE_BLOCK_BUFFER (1 << 1)
210 #define FEATURE_BLOCK_PROC (1 << 2)
211 #define FEATURE_I2C_BLOCK_READ (1 << 3)
212 #define FEATURE_IRQ (1 << 4)
213 /* Not really a feature, but it's convenient to handle it as such */
214 #define FEATURE_IDF (1 << 15)
216 static const char *i801_feature_names[] = {
219 "Block process call",
224 static unsigned int disable_features;
225 module_param(disable_features, uint, S_IRUGO | S_IWUSR);
226 MODULE_PARM_DESC(disable_features, "Disable selected driver features");
228 /* Make sure the SMBus host is ready to start transmitting.
229 Return 0 if it is, -EBUSY if it is not. */
230 static int i801_check_pre(struct i801_priv *priv)
234 status = inb_p(SMBHSTSTS(priv));
235 if (status & SMBHSTSTS_HOST_BUSY) {
236 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
240 status &= STATUS_FLAGS;
242 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
244 outb_p(status, SMBHSTSTS(priv));
245 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
247 dev_err(&priv->pci_dev->dev,
248 "Failed clearing status flags (%02x)\n",
258 * Convert the status register to an error code, and clear it.
259 * Note that status only contains the bits we want to clear, not the
260 * actual register value.
262 static int i801_check_post(struct i801_priv *priv, int status)
267 * If the SMBus is still busy, we give up
268 * Note: This timeout condition only happens when using polling
269 * transactions. For interrupt operation, NAK/timeout is indicated by
272 if (unlikely(status < 0)) {
273 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
274 /* try to stop the current command */
275 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
276 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
278 usleep_range(1000, 2000);
279 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
282 /* Check if it worked */
283 status = inb_p(SMBHSTSTS(priv));
284 if ((status & SMBHSTSTS_HOST_BUSY) ||
285 !(status & SMBHSTSTS_FAILED))
286 dev_err(&priv->pci_dev->dev,
287 "Failed terminating the transaction\n");
288 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
292 if (status & SMBHSTSTS_FAILED) {
294 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
296 if (status & SMBHSTSTS_DEV_ERR) {
298 dev_dbg(&priv->pci_dev->dev, "No response\n");
300 if (status & SMBHSTSTS_BUS_ERR) {
302 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
305 /* Clear status flags except BYTE_DONE, to be cleared by caller */
306 outb_p(status, SMBHSTSTS(priv));
311 /* Wait for BUSY being cleared and either INTR or an error flag being set */
312 static int i801_wait_intr(struct i801_priv *priv)
317 /* We will always wait for a fraction of a second! */
319 usleep_range(250, 500);
320 status = inb_p(SMBHSTSTS(priv));
321 } while (((status & SMBHSTSTS_HOST_BUSY) ||
322 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
323 (timeout++ < MAX_RETRIES));
325 if (timeout > MAX_RETRIES) {
326 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
329 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
332 /* Wait for either BYTE_DONE or an error flag being set */
333 static int i801_wait_byte_done(struct i801_priv *priv)
338 /* We will always wait for a fraction of a second! */
340 usleep_range(250, 500);
341 status = inb_p(SMBHSTSTS(priv));
342 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
343 (timeout++ < MAX_RETRIES));
345 if (timeout > MAX_RETRIES) {
346 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
349 return status & STATUS_ERROR_FLAGS;
352 static int i801_transaction(struct i801_priv *priv, int xact)
357 result = i801_check_pre(priv);
361 if (priv->features & FEATURE_IRQ) {
362 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
364 wait_event(priv->waitq, (status = priv->status));
366 return i801_check_post(priv, status);
369 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
370 * SMBSCMD are passed in xact */
371 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
373 status = i801_wait_intr(priv);
374 return i801_check_post(priv, status);
377 static int i801_block_transaction_by_block(struct i801_priv *priv,
378 union i2c_smbus_data *data,
379 char read_write, int hwpec)
384 inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
386 /* Use 32-byte buffer to process this transaction */
387 if (read_write == I2C_SMBUS_WRITE) {
388 len = data->block[0];
389 outb_p(len, SMBHSTDAT0(priv));
390 for (i = 0; i < len; i++)
391 outb_p(data->block[i+1], SMBBLKDAT(priv));
394 status = i801_transaction(priv, I801_BLOCK_DATA |
395 (hwpec ? SMBHSTCNT_PEC_EN : 0));
399 if (read_write == I2C_SMBUS_READ) {
400 len = inb_p(SMBHSTDAT0(priv));
401 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
404 data->block[0] = len;
405 for (i = 0; i < len; i++)
406 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
411 static void i801_isr_byte_done(struct i801_priv *priv)
414 /* For SMBus block reads, length is received with first byte */
415 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
416 (priv->count == 0)) {
417 priv->len = inb_p(SMBHSTDAT0(priv));
418 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
419 dev_err(&priv->pci_dev->dev,
420 "Illegal SMBus block read size %d\n",
423 priv->len = I2C_SMBUS_BLOCK_MAX;
425 dev_dbg(&priv->pci_dev->dev,
426 "SMBus block read size is %d\n",
429 priv->data[-1] = priv->len;
433 if (priv->count < priv->len)
434 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
436 dev_dbg(&priv->pci_dev->dev,
437 "Discarding extra byte on block read\n");
439 /* Set LAST_BYTE for last byte of read transaction */
440 if (priv->count == priv->len - 1)
441 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
443 } else if (priv->count < priv->len - 1) {
444 /* Write next byte, except for IRQ after last byte */
445 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
448 /* Clear BYTE_DONE to continue with next byte */
449 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
453 * There are two kinds of interrupts:
455 * 1) i801 signals transaction completion with one of these interrupts:
457 * DEV_ERR - Invalid command, NAK or communication timeout
458 * BUS_ERR - SMI# transaction collision
459 * FAILED - transaction was canceled due to a KILL request
460 * When any of these occur, update ->status and wake up the waitq.
461 * ->status must be cleared before kicking off the next transaction.
463 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
464 * occurs for each byte of a byte-by-byte to prepare the next byte.
466 static irqreturn_t i801_isr(int irq, void *dev_id)
468 struct i801_priv *priv = dev_id;
472 /* Confirm this is our interrupt */
473 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
474 if (!(pcists & SMBPCISTS_INTS))
477 status = inb_p(SMBHSTSTS(priv));
479 dev_dbg(&priv->pci_dev->dev, "irq: status = %02x\n", status);
481 if (status & SMBHSTSTS_BYTE_DONE)
482 i801_isr_byte_done(priv);
485 * Clear irq sources and report transaction result.
486 * ->status must be cleared before the next transaction is started.
488 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
490 outb_p(status, SMBHSTSTS(priv));
491 priv->status |= status;
492 wake_up(&priv->waitq);
499 * For "byte-by-byte" block transactions:
500 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
501 * I2C read uses cmd=I801_I2C_BLOCK_DATA
503 static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
504 union i2c_smbus_data *data,
505 char read_write, int command,
513 result = i801_check_pre(priv);
517 len = data->block[0];
519 if (read_write == I2C_SMBUS_WRITE) {
520 outb_p(len, SMBHSTDAT0(priv));
521 outb_p(data->block[1], SMBBLKDAT(priv));
524 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
525 read_write == I2C_SMBUS_READ)
526 smbcmd = I801_I2C_BLOCK_DATA;
528 smbcmd = I801_BLOCK_DATA;
530 if (priv->features & FEATURE_IRQ) {
531 priv->is_read = (read_write == I2C_SMBUS_READ);
532 if (len == 1 && priv->is_read)
533 smbcmd |= SMBHSTCNT_LAST_BYTE;
534 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
537 priv->data = &data->block[1];
539 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
540 wait_event(priv->waitq, (status = priv->status));
542 return i801_check_post(priv, status);
545 for (i = 1; i <= len; i++) {
546 if (i == len && read_write == I2C_SMBUS_READ)
547 smbcmd |= SMBHSTCNT_LAST_BYTE;
548 outb_p(smbcmd, SMBHSTCNT(priv));
551 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
554 status = i801_wait_byte_done(priv);
558 if (i == 1 && read_write == I2C_SMBUS_READ
559 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
560 len = inb_p(SMBHSTDAT0(priv));
561 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
562 dev_err(&priv->pci_dev->dev,
563 "Illegal SMBus block read size %d\n",
566 while (inb_p(SMBHSTSTS(priv)) &
568 outb_p(SMBHSTSTS_BYTE_DONE,
570 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
573 data->block[0] = len;
576 /* Retrieve/store value in SMBBLKDAT */
577 if (read_write == I2C_SMBUS_READ)
578 data->block[i] = inb_p(SMBBLKDAT(priv));
579 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
580 outb_p(data->block[i+1], SMBBLKDAT(priv));
582 /* signals SMBBLKDAT ready */
583 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
586 status = i801_wait_intr(priv);
588 return i801_check_post(priv, status);
591 static int i801_set_block_buffer_mode(struct i801_priv *priv)
593 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
594 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
599 /* Block transaction function */
600 static int i801_block_transaction(struct i801_priv *priv,
601 union i2c_smbus_data *data, char read_write,
602 int command, int hwpec)
607 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
608 if (read_write == I2C_SMBUS_WRITE) {
609 /* set I2C_EN bit in configuration register */
610 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
611 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
612 hostc | SMBHSTCFG_I2C_EN);
613 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
614 dev_err(&priv->pci_dev->dev,
615 "I2C block read is unsupported!\n");
620 if (read_write == I2C_SMBUS_WRITE
621 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
622 if (data->block[0] < 1)
624 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
625 data->block[0] = I2C_SMBUS_BLOCK_MAX;
627 data->block[0] = 32; /* max for SMBus block reads */
630 /* Experience has shown that the block buffer can only be used for
631 SMBus (not I2C) block transactions, even though the datasheet
632 doesn't mention this limitation. */
633 if ((priv->features & FEATURE_BLOCK_BUFFER)
634 && command != I2C_SMBUS_I2C_BLOCK_DATA
635 && i801_set_block_buffer_mode(priv) == 0)
636 result = i801_block_transaction_by_block(priv, data,
639 result = i801_block_transaction_byte_by_byte(priv, data,
643 if (command == I2C_SMBUS_I2C_BLOCK_DATA
644 && read_write == I2C_SMBUS_WRITE) {
645 /* restore saved configuration register value */
646 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
651 /* Return negative errno on error. */
652 static s32 i801_access(struct i2c_adapter *adap, u16 addr,
653 unsigned short flags, char read_write, u8 command,
654 int size, union i2c_smbus_data *data)
659 struct i801_priv *priv = i2c_get_adapdata(adap);
661 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
662 && size != I2C_SMBUS_QUICK
663 && size != I2C_SMBUS_I2C_BLOCK_DATA;
666 case I2C_SMBUS_QUICK:
667 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
672 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
674 if (read_write == I2C_SMBUS_WRITE)
675 outb_p(command, SMBHSTCMD(priv));
678 case I2C_SMBUS_BYTE_DATA:
679 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
681 outb_p(command, SMBHSTCMD(priv));
682 if (read_write == I2C_SMBUS_WRITE)
683 outb_p(data->byte, SMBHSTDAT0(priv));
684 xact = I801_BYTE_DATA;
686 case I2C_SMBUS_WORD_DATA:
687 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
689 outb_p(command, SMBHSTCMD(priv));
690 if (read_write == I2C_SMBUS_WRITE) {
691 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
692 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
694 xact = I801_WORD_DATA;
696 case I2C_SMBUS_BLOCK_DATA:
697 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
699 outb_p(command, SMBHSTCMD(priv));
702 case I2C_SMBUS_I2C_BLOCK_DATA:
703 /* NB: page 240 of ICH5 datasheet shows that the R/#W
704 * bit should be cleared here, even when reading */
705 outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
706 if (read_write == I2C_SMBUS_READ) {
707 /* NB: page 240 of ICH5 datasheet also shows
708 * that DATA1 is the cmd field when reading */
709 outb_p(command, SMBHSTDAT1(priv));
711 outb_p(command, SMBHSTCMD(priv));
715 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
720 if (hwpec) /* enable/disable hardware PEC */
721 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
723 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
727 ret = i801_block_transaction(priv, data, read_write, size,
730 ret = i801_transaction(priv, xact);
732 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
733 time, so we forcibly disable it after every transaction. Turn off
734 E32B for the same reason. */
736 outb_p(inb_p(SMBAUXCTL(priv)) &
737 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
743 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
746 switch (xact & 0x7f) {
747 case I801_BYTE: /* Result put in SMBHSTDAT0 */
749 data->byte = inb_p(SMBHSTDAT0(priv));
752 data->word = inb_p(SMBHSTDAT0(priv)) +
753 (inb_p(SMBHSTDAT1(priv)) << 8);
760 static u32 i801_func(struct i2c_adapter *adapter)
762 struct i801_priv *priv = i2c_get_adapdata(adapter);
764 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
765 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
766 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
767 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
768 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
769 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
772 static const struct i2c_algorithm smbus_algorithm = {
773 .smbus_xfer = i801_access,
774 .functionality = i801_func,
777 static DEFINE_PCI_DEVICE_TABLE(i801_ids) = {
778 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
779 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
780 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
781 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
782 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
783 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
784 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
785 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
786 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
787 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
788 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
789 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
790 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
791 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
792 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
793 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
794 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
795 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
796 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
797 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
798 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
799 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
800 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
801 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
802 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
803 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
807 MODULE_DEVICE_TABLE(pci, i801_ids);
809 #if defined CONFIG_X86 && defined CONFIG_DMI
810 static unsigned char apanel_addr;
812 /* Scan the system ROM for the signature "FJKEYINF" */
813 static __init const void __iomem *bios_signature(const void __iomem *bios)
816 const unsigned char signature[] = "FJKEYINF";
818 for (offset = 0; offset < 0x10000; offset += 0x10) {
819 if (check_signature(bios + offset, signature,
820 sizeof(signature)-1))
821 return bios + offset;
826 static void __init input_apanel_init(void)
829 const void __iomem *p;
831 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
832 p = bios_signature(bios);
834 /* just use the first address */
835 apanel_addr = readb(p + 8 + 3) >> 1;
840 struct dmi_onboard_device_info {
843 unsigned short i2c_addr;
844 const char *i2c_type;
847 static const struct dmi_onboard_device_info dmi_devices[] = {
848 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
849 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
850 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
853 static void dmi_check_onboard_device(u8 type, const char *name,
854 struct i2c_adapter *adap)
857 struct i2c_board_info info;
859 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
860 /* & ~0x80, ignore enabled/disabled bit */
861 if ((type & ~0x80) != dmi_devices[i].type)
863 if (strcasecmp(name, dmi_devices[i].name))
866 memset(&info, 0, sizeof(struct i2c_board_info));
867 info.addr = dmi_devices[i].i2c_addr;
868 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
869 i2c_new_device(adap, &info);
874 /* We use our own function to check for onboard devices instead of
875 dmi_find_device() as some buggy BIOS's have the devices we are interested
876 in marked as disabled */
877 static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
884 count = (dm->length - sizeof(struct dmi_header)) / 2;
885 for (i = 0; i < count; i++) {
886 const u8 *d = (char *)(dm + 1) + (i * 2);
887 const char *name = ((char *) dm) + dm->length;
894 while (s > 0 && name[0]) {
895 name += strlen(name) + 1;
898 if (name[0] == 0) /* Bogus string reference */
901 dmi_check_onboard_device(type, name, adap);
905 /* Register optional slaves */
906 static void i801_probe_optional_slaves(struct i801_priv *priv)
908 /* Only register slaves on main SMBus channel */
909 if (priv->features & FEATURE_IDF)
913 struct i2c_board_info info;
915 memset(&info, 0, sizeof(struct i2c_board_info));
916 info.addr = apanel_addr;
917 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
918 i2c_new_device(&priv->adapter, &info);
921 if (dmi_name_in_vendors("FUJITSU"))
922 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
925 static void __init input_apanel_init(void) {}
926 static void i801_probe_optional_slaves(struct i801_priv *priv) {}
927 #endif /* CONFIG_X86 && CONFIG_DMI */
929 #if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
931 static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
932 .gpio_chip = "gpio_ich",
933 .values = { 0x02, 0x03 },
935 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
940 static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
941 .gpio_chip = "gpio_ich",
942 .values = { 0x02, 0x03, 0x01 },
944 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
949 static const struct dmi_system_id mux_dmi_table[] = {
952 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
953 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
955 .driver_data = &i801_mux_config_asus_z8_d12,
959 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
960 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
962 .driver_data = &i801_mux_config_asus_z8_d12,
966 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
967 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
969 .driver_data = &i801_mux_config_asus_z8_d12,
973 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
974 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
976 .driver_data = &i801_mux_config_asus_z8_d12,
980 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
981 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
983 .driver_data = &i801_mux_config_asus_z8_d12,
987 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
988 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
990 .driver_data = &i801_mux_config_asus_z8_d12,
994 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
995 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
997 .driver_data = &i801_mux_config_asus_z8_d18,
1001 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1002 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1004 .driver_data = &i801_mux_config_asus_z8_d18,
1008 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1009 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1011 .driver_data = &i801_mux_config_asus_z8_d12,
1016 /* Setup multiplexing if needed */
1017 static int i801_add_mux(struct i801_priv *priv)
1019 struct device *dev = &priv->adapter.dev;
1020 const struct i801_mux_config *mux_config;
1021 struct i2c_mux_gpio_platform_data gpio_data;
1024 if (!priv->mux_drvdata)
1026 mux_config = priv->mux_drvdata;
1028 /* Prepare the platform data */
1029 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1030 gpio_data.parent = priv->adapter.nr;
1031 gpio_data.values = mux_config->values;
1032 gpio_data.n_values = mux_config->n_values;
1033 gpio_data.classes = mux_config->classes;
1034 gpio_data.gpio_chip = mux_config->gpio_chip;
1035 gpio_data.gpios = mux_config->gpios;
1036 gpio_data.n_gpios = mux_config->n_gpios;
1037 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1039 /* Register the mux device */
1040 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
1041 PLATFORM_DEVID_AUTO, &gpio_data,
1042 sizeof(struct i2c_mux_gpio_platform_data));
1043 if (IS_ERR(priv->mux_pdev)) {
1044 err = PTR_ERR(priv->mux_pdev);
1045 priv->mux_pdev = NULL;
1046 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1053 static void i801_del_mux(struct i801_priv *priv)
1056 platform_device_unregister(priv->mux_pdev);
1059 static unsigned int i801_get_adapter_class(struct i801_priv *priv)
1061 const struct dmi_system_id *id;
1062 const struct i801_mux_config *mux_config;
1063 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1066 id = dmi_first_match(mux_dmi_table);
1068 /* Remove branch classes from trunk */
1069 mux_config = id->driver_data;
1070 for (i = 0; i < mux_config->n_values; i++)
1071 class &= ~mux_config->classes[i];
1073 /* Remember for later */
1074 priv->mux_drvdata = mux_config;
1080 static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1081 static inline void i801_del_mux(struct i801_priv *priv) { }
1083 static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1085 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1089 static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
1093 struct i801_priv *priv;
1095 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1099 i2c_set_adapdata(&priv->adapter, priv);
1100 priv->adapter.owner = THIS_MODULE;
1101 priv->adapter.class = i801_get_adapter_class(priv);
1102 priv->adapter.algo = &smbus_algorithm;
1104 priv->pci_dev = dev;
1105 switch (dev->device) {
1106 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1107 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1108 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
1109 priv->features |= FEATURE_IDF;
1112 priv->features |= FEATURE_I2C_BLOCK_READ;
1113 priv->features |= FEATURE_IRQ;
1115 case PCI_DEVICE_ID_INTEL_82801DB_3:
1116 priv->features |= FEATURE_SMBUS_PEC;
1117 priv->features |= FEATURE_BLOCK_BUFFER;
1119 case PCI_DEVICE_ID_INTEL_82801CA_3:
1120 case PCI_DEVICE_ID_INTEL_82801BA_2:
1121 case PCI_DEVICE_ID_INTEL_82801AB_3:
1122 case PCI_DEVICE_ID_INTEL_82801AA_3:
1126 /* Disable features on user request */
1127 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
1128 if (priv->features & disable_features & (1 << i))
1129 dev_notice(&dev->dev, "%s disabled by user\n",
1130 i801_feature_names[i]);
1132 priv->features &= ~disable_features;
1134 err = pci_enable_device(dev);
1136 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1141 /* Determine the address of the SMBus area */
1142 priv->smba = pci_resource_start(dev, SMBBAR);
1144 dev_err(&dev->dev, "SMBus base address uninitialized, "
1150 err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
1156 err = pci_request_region(dev, SMBBAR, i801_driver.name);
1158 dev_err(&dev->dev, "Failed to request SMBus region "
1159 "0x%lx-0x%Lx\n", priv->smba,
1160 (unsigned long long)pci_resource_end(dev, SMBBAR));
1164 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1165 priv->original_hstcfg = temp;
1166 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
1167 if (!(temp & SMBHSTCFG_HST_EN)) {
1168 dev_info(&dev->dev, "Enabling SMBus device\n");
1169 temp |= SMBHSTCFG_HST_EN;
1171 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
1173 if (temp & SMBHSTCFG_SMB_SMI_EN) {
1174 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
1175 /* Disable SMBus interrupt feature if SMBus using SMI# */
1176 priv->features &= ~FEATURE_IRQ;
1179 /* Clear special mode bits */
1180 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1181 outb_p(inb_p(SMBAUXCTL(priv)) &
1182 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
1184 if (priv->features & FEATURE_IRQ) {
1185 init_waitqueue_head(&priv->waitq);
1187 err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
1188 i801_driver.name, priv);
1190 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1194 dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
1197 /* set up the sysfs linkage to our parent device */
1198 priv->adapter.dev.parent = &dev->dev;
1200 /* Retry up to 3 times on lost arbitration */
1201 priv->adapter.retries = 3;
1203 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1204 "SMBus I801 adapter at %04lx", priv->smba);
1205 err = i2c_add_adapter(&priv->adapter);
1207 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
1211 of_i2c_register_devices(&priv->adapter);
1212 i801_probe_optional_slaves(priv);
1213 /* We ignore errors - multiplexing is optional */
1216 pci_set_drvdata(dev, priv);
1221 if (priv->features & FEATURE_IRQ)
1222 free_irq(dev->irq, priv);
1224 pci_release_region(dev, SMBBAR);
1230 static void i801_remove(struct pci_dev *dev)
1232 struct i801_priv *priv = pci_get_drvdata(dev);
1235 i2c_del_adapter(&priv->adapter);
1236 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
1238 if (priv->features & FEATURE_IRQ)
1239 free_irq(dev->irq, priv);
1240 pci_release_region(dev, SMBBAR);
1242 pci_set_drvdata(dev, NULL);
1245 * do not call pci_disable_device(dev) since it can cause hard hangs on
1246 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1251 static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
1253 struct i801_priv *priv = pci_get_drvdata(dev);
1255 pci_save_state(dev);
1256 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
1257 pci_set_power_state(dev, pci_choose_state(dev, mesg));
1261 static int i801_resume(struct pci_dev *dev)
1263 pci_set_power_state(dev, PCI_D0);
1264 pci_restore_state(dev);
1265 return pci_enable_device(dev);
1268 #define i801_suspend NULL
1269 #define i801_resume NULL
1272 static struct pci_driver i801_driver = {
1273 .name = "i801_smbus",
1274 .id_table = i801_ids,
1275 .probe = i801_probe,
1276 .remove = i801_remove,
1277 .suspend = i801_suspend,
1278 .resume = i801_resume,
1281 static int __init i2c_i801_init(void)
1283 if (dmi_name_in_vendors("FUJITSU"))
1284 input_apanel_init();
1285 return pci_register_driver(&i801_driver);
1288 static void __exit i2c_i801_exit(void)
1290 pci_unregister_driver(&i801_driver);
1293 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, "
1294 "Jean Delvare <khali@linux-fr.org>");
1295 MODULE_DESCRIPTION("I801 SMBus driver");
1296 MODULE_LICENSE("GPL");
1298 module_init(i2c_i801_init);
1299 module_exit(i2c_i801_exit);