blob: 4ebceed6bc6612dbbc50bd62a8e91ef97615b66d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
3 Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
4 <mdsxyz123@yahoo.com>
Jean Delvare84c1af42012-03-26 21:47:19 +02005 Copyright (C) 2007 - 2012 Jean Delvare <khali@linux-fr.org>
David Woodhouse0cd96eb2010-10-31 21:06:59 +01006 Copyright (C) 2010 Intel Corporation,
7 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
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.
13
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.
18
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.
22*/
23
24/*
Jean Delvareae7b0492008-01-27 18:14:49 +010025 Supports the following Intel I/O Controller Hubs (ICH):
26
27 I/O Block I2C
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
Seth Heasleycb04e952010-10-04 13:27:14 -070043 EP80579 (Tolapai) 0x5032 32 hard yes yes yes
Gaston, Jason Dd28dc712008-02-24 20:03:42 +010044 ICH10 0x3a30 32 hard yes yes yes
45 ICH10 0x3a60 32 hard yes yes yes
Seth Heasleycb04e952010-10-04 13:27:14 -070046 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
Seth Heasley662cda82011-03-20 14:50:53 +010047 6 Series (PCH) 0x1c22 32 hard yes yes yes
Seth Heasleye30d9852010-10-31 21:06:59 +010048 Patsburg (PCH) 0x1d22 32 hard yes yes yes
David Woodhouse55fee8d2010-10-31 21:07:00 +010049 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
Seth Heasley662cda82011-03-20 14:50:53 +010052 DH89xxCC (PCH) 0x2330 32 hard yes yes yes
Seth Heasley6e2a8512011-05-24 20:58:49 +020053 Panther Point (PCH) 0x1e22 32 hard yes yes yes
Seth Heasley062737f2012-03-26 21:47:19 +020054 Lynx Point (PCH) 0x8c22 32 hard yes yes yes
James Ralston4a8f1dd2012-09-10 10:14:02 +020055 Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
Seth Heasleyc2db409c2013-01-30 15:25:32 +000056 Avoton (SOC) 0x1f3c 32 hard yes yes yes
James Ralstona3fc0ff2013-02-14 09:15:33 +000057 Wellsburg (PCH) 0x8d22 32 hard yes yes yes
58 Wellsburg (PCH) MS 0x8d7d 32 hard yes yes yes
59 Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes
60 Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes
Seth Heasleyf39901c2013-06-19 16:59:57 -070061 Coleto Creek (PCH) 0x23b0 32 hard yes yes yes
Jean Delvareae7b0492008-01-27 18:14:49 +010062
63 Features supported by this driver:
64 Software PEC no
65 Hardware PEC yes
66 Block buffer yes
67 Block process call transaction no
Jean Delvare63420642008-01-27 18:14:50 +010068 I2C block read transaction yes (doesn't use the block buffer)
David Woodhouse55fee8d2010-10-31 21:07:00 +010069 Slave mode no
Daniel Kurtz636752b2012-07-24 14:13:58 +020070 Interrupt processing yes
Jean Delvareae7b0492008-01-27 18:14:49 +010071
72 See the file Documentation/i2c/busses/i2c-i801 for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073*/
74
Daniel Kurtz636752b2012-07-24 14:13:58 +020075#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/module.h>
77#include <linux/pci.h>
78#include <linux/kernel.h>
79#include <linux/stddef.h>
80#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/ioport.h>
82#include <linux/init.h>
83#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020084#include <linux/acpi.h>
Jean Delvare1561bfe2009-01-07 14:29:17 +010085#include <linux/io.h>
Hans de Goedefa5bfab2009-03-30 21:46:44 +020086#include <linux/dmi.h>
Ben Hutchings665a96b2011-01-10 22:11:22 +010087#include <linux/slab.h>
Daniel Kurtz636752b2012-07-24 14:13:58 +020088#include <linux/wait.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +020089#include <linux/err.h>
Thierry Redingf6afc8b2012-12-16 21:11:54 +010090#include <linux/of_i2c.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +020091
Jean Delvare79e3e5b2012-10-28 21:37:01 +010092#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
93 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +020094#include <linux/gpio.h>
95#include <linux/i2c-mux-gpio.h>
96#include <linux/platform_device.h>
97#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* I801 SMBus address offsets */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100100#define SMBHSTSTS(p) (0 + (p)->smba)
101#define SMBHSTCNT(p) (2 + (p)->smba)
102#define SMBHSTCMD(p) (3 + (p)->smba)
103#define SMBHSTADD(p) (4 + (p)->smba)
104#define SMBHSTDAT0(p) (5 + (p)->smba)
105#define SMBHSTDAT1(p) (6 + (p)->smba)
106#define SMBBLKDAT(p) (7 + (p)->smba)
107#define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
108#define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
109#define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/* PCI Address Constants */
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200112#define SMBBAR 4
Daniel Kurtz636752b2012-07-24 14:13:58 +0200113#define SMBPCISTS 0x006
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define SMBHSTCFG 0x040
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Daniel Kurtz636752b2012-07-24 14:13:58 +0200116/* Host status bits for SMBPCISTS */
117#define SMBPCISTS_INTS 0x08
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Host configuration bits for SMBHSTCFG */
120#define SMBHSTCFG_HST_EN 1
121#define SMBHSTCFG_SMB_SMI_EN 2
122#define SMBHSTCFG_I2C_EN 4
123
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300124/* Auxiliary control register bits, ICH4+ only */
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200125#define SMBAUXCTL_CRC 1
126#define SMBAUXCTL_E32B 2
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* Other settings */
Jean Delvare84c1af42012-03-26 21:47:19 +0200129#define MAX_RETRIES 400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* I801 command constants */
132#define I801_QUICK 0x00
133#define I801_BYTE 0x04
134#define I801_BYTE_DATA 0x08
135#define I801_WORD_DATA 0x0C
Jean Delvareae7b0492008-01-27 18:14:49 +0100136#define I801_PROC_CALL 0x10 /* unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define I801_BLOCK_DATA 0x14
Jean Delvare63420642008-01-27 18:14:50 +0100138#define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200139
140/* I801 Host Control register bits */
141#define SMBHSTCNT_INTREN 0x01
142#define SMBHSTCNT_KILL 0x02
143#define SMBHSTCNT_LAST_BYTE 0x20
144#define SMBHSTCNT_START 0x40
145#define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200147/* I801 Hosts Status register bits */
148#define SMBHSTSTS_BYTE_DONE 0x80
149#define SMBHSTSTS_INUSE_STS 0x40
150#define SMBHSTSTS_SMBALERT_STS 0x20
151#define SMBHSTSTS_FAILED 0x10
152#define SMBHSTSTS_BUS_ERR 0x08
153#define SMBHSTSTS_DEV_ERR 0x04
154#define SMBHSTSTS_INTR 0x02
155#define SMBHSTSTS_HOST_BUSY 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Daniel Kurtz70a1cc12012-07-24 14:13:58 +0200157#define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
158 SMBHSTSTS_DEV_ERR)
159
160#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
161 STATUS_ERROR_FLAGS)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200162
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200163/* Older devices have their ID defined in <linux/pci_ids.h> */
164#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
165#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
David Woodhouse55fee8d2010-10-31 21:07:00 +0100166/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
167#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
168#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
169#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
Seth Heasley6e2a8512011-05-24 20:58:49 +0200170#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
Seth Heasleyc2db409c2013-01-30 15:25:32 +0000171#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200172#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
Seth Heasleyf39901c2013-06-19 16:59:57 -0700173#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200174#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
Seth Heasley062737f2012-03-26 21:47:19 +0200175#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
James Ralstona3fc0ff2013-02-14 09:15:33 +0000176#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
177#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
178#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
179#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
James Ralston4a8f1dd2012-09-10 10:14:02 +0200180#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
David Woodhouse55fee8d2010-10-31 21:07:00 +0100181
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200182struct i801_mux_config {
183 char *gpio_chip;
184 unsigned values[3];
185 int n_values;
186 unsigned classes[3];
187 unsigned gpios[2]; /* Relative to gpio_chip->base */
188 int n_gpios;
189};
190
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100191struct i801_priv {
192 struct i2c_adapter adapter;
193 unsigned long smba;
194 unsigned char original_hstcfg;
195 struct pci_dev *pci_dev;
196 unsigned int features;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200197
198 /* isr processing */
199 wait_queue_head_t waitq;
200 u8 status;
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200201
202 /* Command state used by isr for byte-by-byte block transactions */
203 u8 cmd;
204 bool is_read;
205 int count;
206 int len;
207 u8 *data;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200208
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100209#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
210 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200211 const struct i801_mux_config *mux_drvdata;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200212 struct platform_device *mux_pdev;
213#endif
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100214};
215
Jean Delvared6072f82005-09-25 16:37:04 +0200216static struct pci_driver i801_driver;
Jean Delvare369f6f42008-01-27 18:14:50 +0100217
218#define FEATURE_SMBUS_PEC (1 << 0)
219#define FEATURE_BLOCK_BUFFER (1 << 1)
220#define FEATURE_BLOCK_PROC (1 << 2)
221#define FEATURE_I2C_BLOCK_READ (1 << 3)
Daniel Kurtz636752b2012-07-24 14:13:58 +0200222#define FEATURE_IRQ (1 << 4)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200223/* Not really a feature, but it's convenient to handle it as such */
224#define FEATURE_IDF (1 << 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Jean Delvareadff6872010-05-21 18:40:54 +0200226static const char *i801_feature_names[] = {
227 "SMBus PEC",
228 "Block buffer",
229 "Block process call",
230 "I2C block read",
Daniel Kurtz636752b2012-07-24 14:13:58 +0200231 "Interrupt",
Jean Delvareadff6872010-05-21 18:40:54 +0200232};
233
234static unsigned int disable_features;
235module_param(disable_features, uint, S_IRUGO | S_IWUSR);
Jean Delvare53229342013-05-15 02:44:10 +0000236MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
237 "\t\t 0x01 disable SMBus PEC\n"
238 "\t\t 0x02 disable the block buffer\n"
239 "\t\t 0x08 disable the I2C block read functionality\n"
240 "\t\t 0x10 don't use interrupts ");
Jean Delvareadff6872010-05-21 18:40:54 +0200241
Jean Delvarecf898dc2008-07-14 22:38:33 +0200242/* Make sure the SMBus host is ready to start transmitting.
243 Return 0 if it is, -EBUSY if it is not. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100244static int i801_check_pre(struct i801_priv *priv)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200245{
246 int status;
247
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100248 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200249 if (status & SMBHSTSTS_HOST_BUSY) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100250 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200251 return -EBUSY;
252 }
253
254 status &= STATUS_FLAGS;
255 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100256 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
Jean Delvarecf898dc2008-07-14 22:38:33 +0200257 status);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100258 outb_p(status, SMBHSTSTS(priv));
259 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200260 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100261 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200262 "Failed clearing status flags (%02x)\n",
263 status);
264 return -EBUSY;
265 }
266 }
267
268 return 0;
269}
270
Jean Delvare6cad93c2012-07-24 14:13:58 +0200271/*
272 * Convert the status register to an error code, and clear it.
273 * Note that status only contains the bits we want to clear, not the
274 * actual register value.
275 */
276static int i801_check_post(struct i801_priv *priv, int status)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200277{
278 int result = 0;
279
Daniel Kurtz636752b2012-07-24 14:13:58 +0200280 /*
281 * If the SMBus is still busy, we give up
282 * Note: This timeout condition only happens when using polling
283 * transactions. For interrupt operation, NAK/timeout is indicated by
284 * DEV_ERR.
285 */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200286 if (unlikely(status < 0)) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100287 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200288 /* try to stop the current command */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100289 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
290 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
291 SMBHSTCNT(priv));
Jean Delvare84c1af42012-03-26 21:47:19 +0200292 usleep_range(1000, 2000);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100293 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
294 SMBHSTCNT(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200295
296 /* Check if it worked */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100297 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200298 if ((status & SMBHSTSTS_HOST_BUSY) ||
299 !(status & SMBHSTSTS_FAILED))
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100300 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200301 "Failed terminating the transaction\n");
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100302 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200303 return -ETIMEDOUT;
304 }
305
306 if (status & SMBHSTSTS_FAILED) {
307 result = -EIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100308 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200309 }
310 if (status & SMBHSTSTS_DEV_ERR) {
311 result = -ENXIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100312 dev_dbg(&priv->pci_dev->dev, "No response\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200313 }
314 if (status & SMBHSTSTS_BUS_ERR) {
315 result = -EAGAIN;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100316 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200317 }
318
Jean Delvare6cad93c2012-07-24 14:13:58 +0200319 /* Clear status flags except BYTE_DONE, to be cleared by caller */
320 outb_p(status, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200321
322 return result;
323}
324
Jean Delvare6cad93c2012-07-24 14:13:58 +0200325/* Wait for BUSY being cleared and either INTR or an error flag being set */
326static int i801_wait_intr(struct i801_priv *priv)
327{
328 int timeout = 0;
329 int status;
330
331 /* We will always wait for a fraction of a second! */
332 do {
333 usleep_range(250, 500);
334 status = inb_p(SMBHSTSTS(priv));
335 } while (((status & SMBHSTSTS_HOST_BUSY) ||
336 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
337 (timeout++ < MAX_RETRIES));
338
339 if (timeout > MAX_RETRIES) {
340 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
341 return -ETIMEDOUT;
342 }
343 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
344}
345
346/* Wait for either BYTE_DONE or an error flag being set */
347static int i801_wait_byte_done(struct i801_priv *priv)
348{
349 int timeout = 0;
350 int status;
351
352 /* We will always wait for a fraction of a second! */
353 do {
354 usleep_range(250, 500);
355 status = inb_p(SMBHSTSTS(priv));
356 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
357 (timeout++ < MAX_RETRIES));
358
359 if (timeout > MAX_RETRIES) {
360 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
361 return -ETIMEDOUT;
362 }
363 return status & STATUS_ERROR_FLAGS;
364}
365
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100366static int i801_transaction(struct i801_priv *priv, int xact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare2b738092008-07-14 22:38:32 +0200368 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200369 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100371 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200372 if (result < 0)
373 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Daniel Kurtz636752b2012-07-24 14:13:58 +0200375 if (priv->features & FEATURE_IRQ) {
376 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
377 SMBHSTCNT(priv));
378 wait_event(priv->waitq, (status = priv->status));
379 priv->status = 0;
380 return i801_check_post(priv, status);
381 }
382
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200383 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
Daniel Kurtz37af8712012-07-24 14:13:58 +0200384 * SMBSCMD are passed in xact */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200385 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jean Delvare6cad93c2012-07-24 14:13:58 +0200387 status = i801_wait_intr(priv);
388 return i801_check_post(priv, status);
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200389}
390
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100391static int i801_block_transaction_by_block(struct i801_priv *priv,
392 union i2c_smbus_data *data,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200393 char read_write, int hwpec)
394{
395 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200396 int status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200397
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100398 inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200399
400 /* Use 32-byte buffer to process this transaction */
401 if (read_write == I2C_SMBUS_WRITE) {
402 len = data->block[0];
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100403 outb_p(len, SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200404 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100405 outb_p(data->block[i+1], SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200406 }
407
Daniel Kurtz37af8712012-07-24 14:13:58 +0200408 status = i801_transaction(priv, I801_BLOCK_DATA |
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200409 (hwpec ? SMBHSTCNT_PEC_EN : 0));
David Brownell97140342008-07-14 22:38:25 +0200410 if (status)
411 return status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200412
413 if (read_write == I2C_SMBUS_READ) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100414 len = inb_p(SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200415 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
David Brownell97140342008-07-14 22:38:25 +0200416 return -EPROTO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200417
418 data->block[0] = len;
419 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100420 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200421 }
422 return 0;
423}
424
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200425static void i801_isr_byte_done(struct i801_priv *priv)
426{
427 if (priv->is_read) {
428 /* For SMBus block reads, length is received with first byte */
429 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
430 (priv->count == 0)) {
431 priv->len = inb_p(SMBHSTDAT0(priv));
432 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
433 dev_err(&priv->pci_dev->dev,
434 "Illegal SMBus block read size %d\n",
435 priv->len);
436 /* FIXME: Recover */
437 priv->len = I2C_SMBUS_BLOCK_MAX;
438 } else {
439 dev_dbg(&priv->pci_dev->dev,
440 "SMBus block read size is %d\n",
441 priv->len);
442 }
443 priv->data[-1] = priv->len;
444 }
445
446 /* Read next byte */
447 if (priv->count < priv->len)
448 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
449 else
450 dev_dbg(&priv->pci_dev->dev,
451 "Discarding extra byte on block read\n");
452
453 /* Set LAST_BYTE for last byte of read transaction */
454 if (priv->count == priv->len - 1)
455 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
456 SMBHSTCNT(priv));
457 } else if (priv->count < priv->len - 1) {
458 /* Write next byte, except for IRQ after last byte */
459 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
460 }
461
462 /* Clear BYTE_DONE to continue with next byte */
463 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
464}
465
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200466/*
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200467 * There are two kinds of interrupts:
468 *
469 * 1) i801 signals transaction completion with one of these interrupts:
470 * INTR - Success
471 * DEV_ERR - Invalid command, NAK or communication timeout
472 * BUS_ERR - SMI# transaction collision
473 * FAILED - transaction was canceled due to a KILL request
474 * When any of these occur, update ->status and wake up the waitq.
475 * ->status must be cleared before kicking off the next transaction.
476 *
477 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
478 * occurs for each byte of a byte-by-byte to prepare the next byte.
Daniel Kurtz636752b2012-07-24 14:13:58 +0200479 */
480static irqreturn_t i801_isr(int irq, void *dev_id)
481{
482 struct i801_priv *priv = dev_id;
483 u16 pcists;
484 u8 status;
485
486 /* Confirm this is our interrupt */
487 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
488 if (!(pcists & SMBPCISTS_INTS))
489 return IRQ_NONE;
490
491 status = inb_p(SMBHSTSTS(priv));
492 if (status != 0x42)
493 dev_dbg(&priv->pci_dev->dev, "irq: status = %02x\n", status);
494
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200495 if (status & SMBHSTSTS_BYTE_DONE)
496 i801_isr_byte_done(priv);
497
Daniel Kurtz636752b2012-07-24 14:13:58 +0200498 /*
499 * Clear irq sources and report transaction result.
500 * ->status must be cleared before the next transaction is started.
501 */
502 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
503 if (status) {
504 outb_p(status, SMBHSTSTS(priv));
505 priv->status |= status;
506 wake_up(&priv->waitq);
507 }
508
509 return IRQ_HANDLED;
510}
511
512/*
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200513 * For "byte-by-byte" block transactions:
514 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
515 * I2C read uses cmd=I801_I2C_BLOCK_DATA
516 */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100517static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
518 union i2c_smbus_data *data,
Jean Delvare63420642008-01-27 18:14:50 +0100519 char read_write, int command,
520 int hwpec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int i, len;
523 int smbcmd;
Jean Delvare2b738092008-07-14 22:38:32 +0200524 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200525 int result;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200526
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100527 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200528 if (result < 0)
529 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200531 len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100534 outb_p(len, SMBHSTDAT0(priv));
535 outb_p(data->block[1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200538 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
539 read_write == I2C_SMBUS_READ)
540 smbcmd = I801_I2C_BLOCK_DATA;
541 else
542 smbcmd = I801_BLOCK_DATA;
543
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200544 if (priv->features & FEATURE_IRQ) {
545 priv->is_read = (read_write == I2C_SMBUS_READ);
546 if (len == 1 && priv->is_read)
547 smbcmd |= SMBHSTCNT_LAST_BYTE;
548 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
549 priv->len = len;
550 priv->count = 0;
551 priv->data = &data->block[1];
552
553 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
554 wait_event(priv->waitq, (status = priv->status));
555 priv->status = 0;
556 return i801_check_post(priv, status);
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 for (i = 1; i <= len; i++) {
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200560 if (i == len && read_write == I2C_SMBUS_READ)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200561 smbcmd |= SMBHSTCNT_LAST_BYTE;
Daniel Kurtz37af8712012-07-24 14:13:58 +0200562 outb_p(smbcmd, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (i == 1)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200565 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100566 SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jean Delvare6cad93c2012-07-24 14:13:58 +0200568 status = i801_wait_byte_done(priv);
569 if (status)
570 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jean Delvare63420642008-01-27 18:14:50 +0100572 if (i == 1 && read_write == I2C_SMBUS_READ
573 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100574 len = inb_p(SMBHSTDAT0(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200575 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100576 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200577 "Illegal SMBus block read size %d\n",
578 len);
579 /* Recover */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100580 while (inb_p(SMBHSTSTS(priv)) &
581 SMBHSTSTS_HOST_BUSY)
582 outb_p(SMBHSTSTS_BYTE_DONE,
583 SMBHSTSTS(priv));
584 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
David Brownell97140342008-07-14 22:38:25 +0200585 return -EPROTO;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 data->block[0] = len;
588 }
589
590 /* Retrieve/store value in SMBBLKDAT */
591 if (read_write == I2C_SMBUS_READ)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100592 data->block[i] = inb_p(SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100594 outb_p(data->block[i+1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Jean Delvarecf898dc2008-07-14 22:38:33 +0200596 /* signals SMBBLKDAT ready */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200597 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200598 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200599
Jean Delvare6cad93c2012-07-24 14:13:58 +0200600 status = i801_wait_intr(priv);
601exit:
602 return i801_check_post(priv, status);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200603}
604
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100605static int i801_set_block_buffer_mode(struct i801_priv *priv)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200606{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100607 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
608 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
David Brownell97140342008-07-14 22:38:25 +0200609 return -EIO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200610 return 0;
611}
612
613/* Block transaction function */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100614static int i801_block_transaction(struct i801_priv *priv,
615 union i2c_smbus_data *data, char read_write,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200616 int command, int hwpec)
617{
618 int result = 0;
619 unsigned char hostc;
620
621 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
622 if (read_write == I2C_SMBUS_WRITE) {
623 /* set I2C_EN bit in configuration register */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100624 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
625 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200626 hostc | SMBHSTCFG_I2C_EN);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100627 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
628 dev_err(&priv->pci_dev->dev,
Jean Delvare63420642008-01-27 18:14:50 +0100629 "I2C block read is unsupported!\n");
David Brownell97140342008-07-14 22:38:25 +0200630 return -EOPNOTSUPP;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Jean Delvare63420642008-01-27 18:14:50 +0100634 if (read_write == I2C_SMBUS_WRITE
635 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200636 if (data->block[0] < 1)
637 data->block[0] = 1;
638 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
639 data->block[0] = I2C_SMBUS_BLOCK_MAX;
640 } else {
Jean Delvare63420642008-01-27 18:14:50 +0100641 data->block[0] = 32; /* max for SMBus block reads */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200642 }
643
Jean Delvarec074c392010-03-13 20:56:53 +0100644 /* Experience has shown that the block buffer can only be used for
645 SMBus (not I2C) block transactions, even though the datasheet
646 doesn't mention this limitation. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100647 if ((priv->features & FEATURE_BLOCK_BUFFER)
Jean Delvarec074c392010-03-13 20:56:53 +0100648 && command != I2C_SMBUS_I2C_BLOCK_DATA
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100649 && i801_set_block_buffer_mode(priv) == 0)
650 result = i801_block_transaction_by_block(priv, data,
651 read_write, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200652 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100653 result = i801_block_transaction_byte_by_byte(priv, data,
654 read_write,
Jean Delvare63420642008-01-27 18:14:50 +0100655 command, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200656
Jean Delvare63420642008-01-27 18:14:50 +0100657 if (command == I2C_SMBUS_I2C_BLOCK_DATA
658 && read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* restore saved configuration register value */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100660 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 return result;
663}
664
David Brownell97140342008-07-14 22:38:25 +0200665/* Return negative errno on error. */
Ivo Manca3fb21c62010-05-21 18:40:55 +0200666static s32 i801_access(struct i2c_adapter *adap, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 unsigned short flags, char read_write, u8 command,
Ivo Manca3fb21c62010-05-21 18:40:55 +0200668 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200670 int hwpec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int block = 0;
672 int ret, xact = 0;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100673 struct i801_priv *priv = i2c_get_adapdata(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100675 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200676 && size != I2C_SMBUS_QUICK
677 && size != I2C_SMBUS_I2C_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 switch (size) {
680 case I2C_SMBUS_QUICK:
681 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100682 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xact = I801_QUICK;
684 break;
685 case I2C_SMBUS_BYTE:
686 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100687 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100689 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 xact = I801_BYTE;
691 break;
692 case I2C_SMBUS_BYTE_DATA:
693 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100694 SMBHSTADD(priv));
695 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100697 outb_p(data->byte, SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 xact = I801_BYTE_DATA;
699 break;
700 case I2C_SMBUS_WORD_DATA:
701 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100702 SMBHSTADD(priv));
703 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100705 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
706 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 xact = I801_WORD_DATA;
709 break;
710 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100712 SMBHSTADD(priv));
713 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 block = 1;
715 break;
Jean Delvare63420642008-01-27 18:14:50 +0100716 case I2C_SMBUS_I2C_BLOCK_DATA:
717 /* NB: page 240 of ICH5 datasheet shows that the R/#W
718 * bit should be cleared here, even when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100719 outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100720 if (read_write == I2C_SMBUS_READ) {
721 /* NB: page 240 of ICH5 datasheet also shows
722 * that DATA1 is the cmd field when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100723 outb_p(command, SMBHSTDAT1(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100724 } else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100725 outb_p(command, SMBHSTCMD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100726 block = 1;
727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100729 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
730 size);
David Brownell97140342008-07-14 22:38:25 +0200731 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200734 if (hwpec) /* enable/disable hardware PEC */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100735 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200736 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100737 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
738 SMBAUXCTL(priv));
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200739
Ivo Manca3fb21c62010-05-21 18:40:55 +0200740 if (block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100741 ret = i801_block_transaction(priv, data, read_write, size,
742 hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200743 else
Daniel Kurtz37af8712012-07-24 14:13:58 +0200744 ret = i801_transaction(priv, xact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Jean Delvarec79cfba2006-04-20 02:43:18 -0700746 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200747 time, so we forcibly disable it after every transaction. Turn off
748 E32B for the same reason. */
Jean Delvarea0921b62008-01-27 18:14:50 +0100749 if (hwpec || block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100750 outb_p(inb_p(SMBAUXCTL(priv)) &
751 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarec79cfba2006-04-20 02:43:18 -0700752
Ivo Manca3fb21c62010-05-21 18:40:55 +0200753 if (block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return ret;
Ivo Manca3fb21c62010-05-21 18:40:55 +0200755 if (ret)
David Brownell97140342008-07-14 22:38:25 +0200756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
758 return 0;
759
760 switch (xact & 0x7f) {
761 case I801_BYTE: /* Result put in SMBHSTDAT0 */
762 case I801_BYTE_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100763 data->byte = inb_p(SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 case I801_WORD_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100766 data->word = inb_p(SMBHSTDAT0(priv)) +
767 (inb_p(SMBHSTDAT1(priv)) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
769 }
770 return 0;
771}
772
773
774static u32 i801_func(struct i2c_adapter *adapter)
775{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100776 struct i801_priv *priv = i2c_get_adapdata(adapter);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare369f6f42008-01-27 18:14:50 +0100779 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
780 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100781 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
782 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
Jean Delvare63420642008-01-27 18:14:50 +0100783 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Jean Delvare8f9082c2006-09-03 22:39:46 +0200786static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 .smbus_xfer = i801_access,
788 .functionality = i801_func,
789};
790
Axel Lin3527bd52012-01-12 20:32:04 +0100791static DEFINE_PCI_DEVICE_TABLE(i801_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
793 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
794 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
795 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
796 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
797 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
798 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
799 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
800 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
Jason Gastonb0a70b52005-04-16 15:24:45 -0700801 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
Jason Gaston8254fc42006-01-09 10:58:08 -0800802 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
Jason Gastonadbc2a12006-11-22 15:19:12 -0800803 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700804 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
Gaston, Jason Dd28dc712008-02-24 20:03:42 +0100805 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
806 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700807 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
808 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
Seth Heasleye30d9852010-10-31 21:06:59 +0100809 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
David Woodhouse55fee8d2010-10-31 21:07:00 +0100810 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
811 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
812 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
Seth Heasley662cda82011-03-20 14:50:53 +0100813 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
Seth Heasley6e2a8512011-05-24 20:58:49 +0200814 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
Seth Heasley062737f2012-03-26 21:47:19 +0200815 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
James Ralston4a8f1dd2012-09-10 10:14:02 +0200816 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
Seth Heasleyc2db409c2013-01-30 15:25:32 +0000817 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
James Ralstona3fc0ff2013-02-14 09:15:33 +0000818 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
819 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
820 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
821 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
Seth Heasleyf39901c2013-06-19 16:59:57 -0700822 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 { 0, }
824};
825
Ivo Manca3fb21c62010-05-21 18:40:55 +0200826MODULE_DEVICE_TABLE(pci, i801_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jean Delvare8eacfce2011-05-24 20:58:49 +0200828#if defined CONFIG_X86 && defined CONFIG_DMI
Jean Delvare1561bfe2009-01-07 14:29:17 +0100829static unsigned char apanel_addr;
830
831/* Scan the system ROM for the signature "FJKEYINF" */
832static __init const void __iomem *bios_signature(const void __iomem *bios)
833{
834 ssize_t offset;
835 const unsigned char signature[] = "FJKEYINF";
836
837 for (offset = 0; offset < 0x10000; offset += 0x10) {
838 if (check_signature(bios + offset, signature,
839 sizeof(signature)-1))
840 return bios + offset;
841 }
842 return NULL;
843}
844
845static void __init input_apanel_init(void)
846{
847 void __iomem *bios;
848 const void __iomem *p;
849
850 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
851 p = bios_signature(bios);
852 if (p) {
853 /* just use the first address */
854 apanel_addr = readb(p + 8 + 3) >> 1;
855 }
856 iounmap(bios);
857}
Jean Delvare1561bfe2009-01-07 14:29:17 +0100858
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200859struct dmi_onboard_device_info {
860 const char *name;
861 u8 type;
862 unsigned short i2c_addr;
863 const char *i2c_type;
864};
865
Bill Pemberton0b255e92012-11-27 15:59:38 -0500866static const struct dmi_onboard_device_info dmi_devices[] = {
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200867 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
868 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
869 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
870};
871
Bill Pemberton0b255e92012-11-27 15:59:38 -0500872static void dmi_check_onboard_device(u8 type, const char *name,
873 struct i2c_adapter *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200874{
875 int i;
876 struct i2c_board_info info;
877
878 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
879 /* & ~0x80, ignore enabled/disabled bit */
880 if ((type & ~0x80) != dmi_devices[i].type)
881 continue;
Jean Delvarefaabd472010-07-09 16:22:51 +0200882 if (strcasecmp(name, dmi_devices[i].name))
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200883 continue;
884
885 memset(&info, 0, sizeof(struct i2c_board_info));
886 info.addr = dmi_devices[i].i2c_addr;
887 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
888 i2c_new_device(adap, &info);
889 break;
890 }
891}
892
893/* We use our own function to check for onboard devices instead of
894 dmi_find_device() as some buggy BIOS's have the devices we are interested
895 in marked as disabled */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500896static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200897{
898 int i, count;
899
900 if (dm->type != 10)
901 return;
902
903 count = (dm->length - sizeof(struct dmi_header)) / 2;
904 for (i = 0; i < count; i++) {
905 const u8 *d = (char *)(dm + 1) + (i * 2);
906 const char *name = ((char *) dm) + dm->length;
907 u8 type = d[0];
908 u8 s = d[1];
909
910 if (!s)
911 continue;
912 s--;
913 while (s > 0 && name[0]) {
914 name += strlen(name) + 1;
915 s--;
916 }
917 if (name[0] == 0) /* Bogus string reference */
918 continue;
919
920 dmi_check_onboard_device(type, name, adap);
921 }
922}
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200923
Jean Delvaree7198fb2011-05-24 20:58:49 +0200924/* Register optional slaves */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500925static void i801_probe_optional_slaves(struct i801_priv *priv)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200926{
927 /* Only register slaves on main SMBus channel */
928 if (priv->features & FEATURE_IDF)
929 return;
930
Jean Delvaree7198fb2011-05-24 20:58:49 +0200931 if (apanel_addr) {
932 struct i2c_board_info info;
933
934 memset(&info, 0, sizeof(struct i2c_board_info));
935 info.addr = apanel_addr;
936 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
937 i2c_new_device(&priv->adapter, &info);
938 }
Jean Delvare8eacfce2011-05-24 20:58:49 +0200939
Jean Delvaree7198fb2011-05-24 20:58:49 +0200940 if (dmi_name_in_vendors("FUJITSU"))
941 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
Jean Delvaree7198fb2011-05-24 20:58:49 +0200942}
Jean Delvare8eacfce2011-05-24 20:58:49 +0200943#else
944static void __init input_apanel_init(void) {}
Bill Pemberton0b255e92012-11-27 15:59:38 -0500945static void i801_probe_optional_slaves(struct i801_priv *priv) {}
Jean Delvare8eacfce2011-05-24 20:58:49 +0200946#endif /* CONFIG_X86 && CONFIG_DMI */
Jean Delvaree7198fb2011-05-24 20:58:49 +0200947
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100948#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
949 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200950static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
951 .gpio_chip = "gpio_ich",
952 .values = { 0x02, 0x03 },
953 .n_values = 2,
954 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
955 .gpios = { 52, 53 },
956 .n_gpios = 2,
957};
958
959static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
960 .gpio_chip = "gpio_ich",
961 .values = { 0x02, 0x03, 0x01 },
962 .n_values = 3,
963 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
964 .gpios = { 52, 53 },
965 .n_gpios = 2,
966};
967
Bill Pemberton0b255e92012-11-27 15:59:38 -0500968static const struct dmi_system_id mux_dmi_table[] = {
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200969 {
970 .matches = {
971 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
972 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
973 },
974 .driver_data = &i801_mux_config_asus_z8_d12,
975 },
976 {
977 .matches = {
978 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
979 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
980 },
981 .driver_data = &i801_mux_config_asus_z8_d12,
982 },
983 {
984 .matches = {
985 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
986 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
987 },
988 .driver_data = &i801_mux_config_asus_z8_d12,
989 },
990 {
991 .matches = {
992 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
993 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
994 },
995 .driver_data = &i801_mux_config_asus_z8_d12,
996 },
997 {
998 .matches = {
999 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1000 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
1001 },
1002 .driver_data = &i801_mux_config_asus_z8_d12,
1003 },
1004 {
1005 .matches = {
1006 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1007 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
1008 },
1009 .driver_data = &i801_mux_config_asus_z8_d12,
1010 },
1011 {
1012 .matches = {
1013 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1014 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
1015 },
1016 .driver_data = &i801_mux_config_asus_z8_d18,
1017 },
1018 {
1019 .matches = {
1020 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1021 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1022 },
1023 .driver_data = &i801_mux_config_asus_z8_d18,
1024 },
1025 {
1026 .matches = {
1027 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1028 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1029 },
1030 .driver_data = &i801_mux_config_asus_z8_d12,
1031 },
1032 { }
1033};
1034
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001035/* Setup multiplexing if needed */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001036static int i801_add_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001037{
1038 struct device *dev = &priv->adapter.dev;
1039 const struct i801_mux_config *mux_config;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001040 struct i2c_mux_gpio_platform_data gpio_data;
Jean Delvaref82b8622012-10-05 22:23:54 +02001041 int err;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001042
1043 if (!priv->mux_drvdata)
1044 return 0;
1045 mux_config = priv->mux_drvdata;
1046
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001047 /* Prepare the platform data */
1048 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1049 gpio_data.parent = priv->adapter.nr;
1050 gpio_data.values = mux_config->values;
1051 gpio_data.n_values = mux_config->n_values;
1052 gpio_data.classes = mux_config->classes;
Jean Delvaref82b8622012-10-05 22:23:54 +02001053 gpio_data.gpio_chip = mux_config->gpio_chip;
1054 gpio_data.gpios = mux_config->gpios;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001055 gpio_data.n_gpios = mux_config->n_gpios;
1056 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1057
1058 /* Register the mux device */
1059 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
Jean Delvaref82b8622012-10-05 22:23:54 +02001060 PLATFORM_DEVID_AUTO, &gpio_data,
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001061 sizeof(struct i2c_mux_gpio_platform_data));
1062 if (IS_ERR(priv->mux_pdev)) {
1063 err = PTR_ERR(priv->mux_pdev);
1064 priv->mux_pdev = NULL;
1065 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1066 return err;
1067 }
1068
1069 return 0;
1070}
1071
Bill Pemberton0b255e92012-11-27 15:59:38 -05001072static void i801_del_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001073{
1074 if (priv->mux_pdev)
1075 platform_device_unregister(priv->mux_pdev);
1076}
1077
Bill Pemberton0b255e92012-11-27 15:59:38 -05001078static unsigned int i801_get_adapter_class(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001079{
1080 const struct dmi_system_id *id;
1081 const struct i801_mux_config *mux_config;
1082 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1083 int i;
1084
1085 id = dmi_first_match(mux_dmi_table);
1086 if (id) {
Jean Delvare28901f52012-10-28 21:37:01 +01001087 /* Remove branch classes from trunk */
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001088 mux_config = id->driver_data;
1089 for (i = 0; i < mux_config->n_values; i++)
1090 class &= ~mux_config->classes[i];
1091
1092 /* Remember for later */
1093 priv->mux_drvdata = mux_config;
1094 }
1095
1096 return class;
1097}
1098#else
1099static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1100static inline void i801_del_mux(struct i801_priv *priv) { }
1101
1102static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1103{
1104 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1105}
1106#endif
1107
Bill Pemberton0b255e92012-11-27 15:59:38 -05001108static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001110 unsigned char temp;
Jean Delvareadff6872010-05-21 18:40:54 +02001111 int err, i;
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001112 struct i801_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001114 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1115 if (!priv)
1116 return -ENOMEM;
1117
1118 i2c_set_adapdata(&priv->adapter, priv);
1119 priv->adapter.owner = THIS_MODULE;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001120 priv->adapter.class = i801_get_adapter_class(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001121 priv->adapter.algo = &smbus_algorithm;
1122
1123 priv->pci_dev = dev;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001124 switch (dev->device) {
Jean Delvaree7198fb2011-05-24 20:58:49 +02001125 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1126 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1127 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
James Ralstona3fc0ff2013-02-14 09:15:33 +00001128 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
1129 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
1130 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
Jean Delvaree7198fb2011-05-24 20:58:49 +02001131 priv->features |= FEATURE_IDF;
1132 /* fall through */
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001133 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001134 priv->features |= FEATURE_I2C_BLOCK_READ;
Jean Delvare6676a842012-12-16 21:11:55 +01001135 priv->features |= FEATURE_IRQ;
Jean Delvare63420642008-01-27 18:14:50 +01001136 /* fall through */
1137 case PCI_DEVICE_ID_INTEL_82801DB_3:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001138 priv->features |= FEATURE_SMBUS_PEC;
1139 priv->features |= FEATURE_BLOCK_BUFFER;
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001140 /* fall through */
1141 case PCI_DEVICE_ID_INTEL_82801CA_3:
1142 case PCI_DEVICE_ID_INTEL_82801BA_2:
1143 case PCI_DEVICE_ID_INTEL_82801AB_3:
1144 case PCI_DEVICE_ID_INTEL_82801AA_3:
Jean Delvare250d1bd2006-12-10 21:21:33 +01001145 break;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001146 }
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001147
Jean Delvareadff6872010-05-21 18:40:54 +02001148 /* Disable features on user request */
1149 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001150 if (priv->features & disable_features & (1 << i))
Jean Delvareadff6872010-05-21 18:40:54 +02001151 dev_notice(&dev->dev, "%s disabled by user\n",
1152 i801_feature_names[i]);
1153 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001154 priv->features &= ~disable_features;
Jean Delvareadff6872010-05-21 18:40:54 +02001155
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001156 err = pci_enable_device(dev);
1157 if (err) {
1158 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1159 err);
1160 goto exit;
1161 }
1162
1163 /* Determine the address of the SMBus area */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001164 priv->smba = pci_resource_start(dev, SMBBAR);
1165 if (!priv->smba) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001166 dev_err(&dev->dev, "SMBus base address uninitialized, "
1167 "upgrade BIOS\n");
1168 err = -ENODEV;
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001169 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001170 }
1171
Jean Delvare54fb4a052008-07-14 22:38:33 +02001172 err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
Jean Delvare18669ea2009-10-04 22:53:45 +02001173 if (err) {
1174 err = -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +02001175 goto exit;
Jean Delvare18669ea2009-10-04 22:53:45 +02001176 }
Jean Delvare54fb4a052008-07-14 22:38:33 +02001177
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001178 err = pci_request_region(dev, SMBBAR, i801_driver.name);
1179 if (err) {
1180 dev_err(&dev->dev, "Failed to request SMBus region "
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001181 "0x%lx-0x%Lx\n", priv->smba,
Andrew Morton598736c2006-06-30 01:56:20 -07001182 (unsigned long long)pci_resource_end(dev, SMBBAR));
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001183 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001184 }
1185
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001186 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1187 priv->original_hstcfg = temp;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001188 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
1189 if (!(temp & SMBHSTCFG_HST_EN)) {
1190 dev_info(&dev->dev, "Enabling SMBus device\n");
1191 temp |= SMBHSTCFG_HST_EN;
1192 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001193 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001194
Daniel Kurtz636752b2012-07-24 14:13:58 +02001195 if (temp & SMBHSTCFG_SMB_SMI_EN) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001196 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001197 /* Disable SMBus interrupt feature if SMBus using SMI# */
1198 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Jean Delvarea0921b62008-01-27 18:14:50 +01001201 /* Clear special mode bits */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001202 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1203 outb_p(inb_p(SMBAUXCTL(priv)) &
1204 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarea0921b62008-01-27 18:14:50 +01001205
Daniel Kurtz636752b2012-07-24 14:13:58 +02001206 if (priv->features & FEATURE_IRQ) {
1207 init_waitqueue_head(&priv->waitq);
1208
1209 err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
1210 i801_driver.name, priv);
1211 if (err) {
1212 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1213 dev->irq, err);
1214 goto exit_release;
1215 }
Jean Delvare29b60852012-07-24 14:13:59 +02001216 dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001217 }
1218
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01001219 /* set up the sysfs linkage to our parent device */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001220 priv->adapter.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jean Delvare7e2193a2009-12-06 17:06:27 +01001222 /* Retry up to 3 times on lost arbitration */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001223 priv->adapter.retries = 3;
Jean Delvare7e2193a2009-12-06 17:06:27 +01001224
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001225 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1226 "SMBus I801 adapter at %04lx", priv->smba);
1227 err = i2c_add_adapter(&priv->adapter);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001228 if (err) {
1229 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001230 goto exit_free_irq;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001231 }
Jean Delvare1561bfe2009-01-07 14:29:17 +01001232
Thierry Redingf6afc8b2012-12-16 21:11:54 +01001233 of_i2c_register_devices(&priv->adapter);
Jean Delvaree7198fb2011-05-24 20:58:49 +02001234 i801_probe_optional_slaves(priv);
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001235 /* We ignore errors - multiplexing is optional */
1236 i801_add_mux(priv);
Jean Delvare1561bfe2009-01-07 14:29:17 +01001237
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001238 pci_set_drvdata(dev, priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001239
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001240 return 0;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001241
Daniel Kurtz636752b2012-07-24 14:13:58 +02001242exit_free_irq:
1243 if (priv->features & FEATURE_IRQ)
1244 free_irq(dev->irq, priv);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001245exit_release:
1246 pci_release_region(dev, SMBBAR);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001247exit:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001248 kfree(priv);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Bill Pemberton0b255e92012-11-27 15:59:38 -05001252static void i801_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001254 struct i801_priv *priv = pci_get_drvdata(dev);
1255
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001256 i801_del_mux(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001257 i2c_del_adapter(&priv->adapter);
1258 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001259
1260 if (priv->features & FEATURE_IRQ)
1261 free_irq(dev->irq, priv);
Jean Delvare6dcc19d2006-06-12 21:53:02 +02001262 pci_release_region(dev, SMBBAR);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001263
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001264 kfree(priv);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001265 /*
1266 * do not call pci_disable_device(dev) since it can cause hard hangs on
1267 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Jean Delvarea5aaea32007-03-22 19:49:01 +01001271#ifdef CONFIG_PM
1272static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
1273{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001274 struct i801_priv *priv = pci_get_drvdata(dev);
1275
Jean Delvarea5aaea32007-03-22 19:49:01 +01001276 pci_save_state(dev);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001277 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Jean Delvarea5aaea32007-03-22 19:49:01 +01001278 pci_set_power_state(dev, pci_choose_state(dev, mesg));
1279 return 0;
1280}
1281
1282static int i801_resume(struct pci_dev *dev)
1283{
1284 pci_set_power_state(dev, PCI_D0);
1285 pci_restore_state(dev);
1286 return pci_enable_device(dev);
1287}
1288#else
1289#define i801_suspend NULL
1290#define i801_resume NULL
1291#endif
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static struct pci_driver i801_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .name = "i801_smbus",
1295 .id_table = i801_ids,
1296 .probe = i801_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001297 .remove = i801_remove,
Jean Delvarea5aaea32007-03-22 19:49:01 +01001298 .suspend = i801_suspend,
1299 .resume = i801_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300};
1301
1302static int __init i2c_i801_init(void)
1303{
Jean Delvare6aa14642011-05-24 20:58:49 +02001304 if (dmi_name_in_vendors("FUJITSU"))
1305 input_apanel_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return pci_register_driver(&i801_driver);
1307}
1308
1309static void __exit i2c_i801_exit(void)
1310{
1311 pci_unregister_driver(&i801_driver);
1312}
1313
Jean Delvare63420642008-01-27 18:14:50 +01001314MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, "
1315 "Jean Delvare <khali@linux-fr.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316MODULE_DESCRIPTION("I801 SMBus driver");
1317MODULE_LICENSE("GPL");
1318
1319module_init(i2c_i801_init);
1320module_exit(i2c_i801_exit);