blob: ae5df147327b538dffe4c0943295fc23214d26ac [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 Delvareb3b8df92014-11-12 10:20:40 +01005 Copyright (C) 2007 - 2014 Jean Delvare <jdelvare@suse.de>
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018*/
19
20/*
Jean Delvarece316112014-07-17 15:03:24 +020021 * Supports the following Intel I/O Controller Hubs (ICH):
22 *
23 * I/O Block I2C
24 * region SMBus Block proc. block
25 * Chip name PCI ID size PEC buffer call read
26 * ---------------------------------------------------------------------------
27 * 82801AA (ICH) 0x2413 16 no no no no
28 * 82801AB (ICH0) 0x2423 16 no no no no
29 * 82801BA (ICH2) 0x2443 16 no no no no
30 * 82801CA (ICH3) 0x2483 32 soft no no no
31 * 82801DB (ICH4) 0x24c3 32 hard yes no no
32 * 82801E (ICH5) 0x24d3 32 hard yes yes yes
33 * 6300ESB 0x25a4 32 hard yes yes yes
34 * 82801F (ICH6) 0x266a 32 hard yes yes yes
35 * 6310ESB/6320ESB 0x269b 32 hard yes yes yes
36 * 82801G (ICH7) 0x27da 32 hard yes yes yes
37 * 82801H (ICH8) 0x283e 32 hard yes yes yes
38 * 82801I (ICH9) 0x2930 32 hard yes yes yes
39 * EP80579 (Tolapai) 0x5032 32 hard yes yes yes
40 * ICH10 0x3a30 32 hard yes yes yes
41 * ICH10 0x3a60 32 hard yes yes yes
42 * 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
43 * 6 Series (PCH) 0x1c22 32 hard yes yes yes
44 * Patsburg (PCH) 0x1d22 32 hard yes yes yes
45 * Patsburg (PCH) IDF 0x1d70 32 hard yes yes yes
46 * Patsburg (PCH) IDF 0x1d71 32 hard yes yes yes
47 * Patsburg (PCH) IDF 0x1d72 32 hard yes yes yes
48 * DH89xxCC (PCH) 0x2330 32 hard yes yes yes
49 * Panther Point (PCH) 0x1e22 32 hard yes yes yes
50 * Lynx Point (PCH) 0x8c22 32 hard yes yes yes
51 * Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
52 * Avoton (SOC) 0x1f3c 32 hard yes yes yes
53 * Wellsburg (PCH) 0x8d22 32 hard yes yes yes
54 * Wellsburg (PCH) MS 0x8d7d 32 hard yes yes yes
55 * Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes
56 * Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes
57 * Coleto Creek (PCH) 0x23b0 32 hard yes yes yes
Jean Delvareb299de82014-07-17 15:04:41 +020058 * Wildcat Point (PCH) 0x8ca2 32 hard yes yes yes
Jean Delvarece316112014-07-17 15:03:24 +020059 * Wildcat Point-LP (PCH) 0x9ca2 32 hard yes yes yes
60 * BayTrail (SOC) 0x0f12 32 hard yes yes yes
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -070061 * Sunrise Point-H (PCH) 0xa123 32 hard yes yes yes
Devin Ryles3eee17992014-11-05 16:30:03 -050062 * Sunrise Point-LP (PCH) 0x9d23 32 hard yes yes yes
Mika Westerberg84d7f2e2015-10-13 15:41:39 +030063 * DNV (SOC) 0x19df 32 hard yes yes yes
Jarkko Nikuladd77f422015-10-22 17:16:58 +030064 * Broxton (SOC) 0x5ad4 32 hard yes yes yes
Alexandra Yatescdc5a312015-11-05 11:40:25 -080065 * Lewisburg (PCH) 0xa1a3 32 hard yes yes yes
66 * Lewisburg Supersku (PCH) 0xa223 32 hard yes yes yes
Jean Delvarece316112014-07-17 15:03:24 +020067 *
68 * Features supported by this driver:
69 * Software PEC no
70 * Hardware PEC yes
71 * Block buffer yes
72 * Block process call transaction no
73 * I2C block read transaction yes (doesn't use the block buffer)
74 * Slave mode no
75 * Interrupt processing yes
76 *
77 * See the file Documentation/i2c/busses/i2c-i801 for details.
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Daniel Kurtz636752b2012-07-24 14:13:58 +020080#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/module.h>
82#include <linux/pci.h>
83#include <linux/kernel.h>
84#include <linux/stddef.h>
85#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/ioport.h>
87#include <linux/init.h>
88#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020089#include <linux/acpi.h>
Jean Delvare1561bfe2009-01-07 14:29:17 +010090#include <linux/io.h>
Hans de Goedefa5bfab2009-03-30 21:46:44 +020091#include <linux/dmi.h>
Ben Hutchings665a96b2011-01-10 22:11:22 +010092#include <linux/slab.h>
Daniel Kurtz636752b2012-07-24 14:13:58 +020093#include <linux/wait.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +020094#include <linux/err.h>
Mika Westerberg94246932015-08-06 13:46:25 +010095#include <linux/platform_device.h>
96#include <linux/platform_data/itco_wdt.h>
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +020097#include <linux/pm_runtime.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +020098
Jean Delvare79e3e5b2012-10-28 21:37:01 +010099#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
100 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200101#include <linux/gpio.h>
102#include <linux/i2c-mux-gpio.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* I801 SMBus address offsets */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100106#define SMBHSTSTS(p) (0 + (p)->smba)
107#define SMBHSTCNT(p) (2 + (p)->smba)
108#define SMBHSTCMD(p) (3 + (p)->smba)
109#define SMBHSTADD(p) (4 + (p)->smba)
110#define SMBHSTDAT0(p) (5 + (p)->smba)
111#define SMBHSTDAT1(p) (6 + (p)->smba)
112#define SMBBLKDAT(p) (7 + (p)->smba)
113#define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
114#define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
115#define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/* PCI Address Constants */
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200118#define SMBBAR 4
Jean Delvareaeb8a3d2014-11-12 10:25:37 +0100119#define SMBPCICTL 0x004
Daniel Kurtz636752b2012-07-24 14:13:58 +0200120#define SMBPCISTS 0x006
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define SMBHSTCFG 0x040
Mika Westerberg94246932015-08-06 13:46:25 +0100122#define TCOBASE 0x050
123#define TCOCTL 0x054
124
125#define ACPIBASE 0x040
126#define ACPIBASE_SMI_OFF 0x030
127#define ACPICTRL 0x044
128#define ACPICTRL_EN 0x080
129
130#define SBREG_BAR 0x10
131#define SBREG_SMBCTRL 0xc6000c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Daniel Kurtz636752b2012-07-24 14:13:58 +0200133/* Host status bits for SMBPCISTS */
134#define SMBPCISTS_INTS 0x08
135
Jean Delvareaeb8a3d2014-11-12 10:25:37 +0100136/* Control bits for SMBPCICTL */
137#define SMBPCICTL_INTDIS 0x0400
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* Host configuration bits for SMBHSTCFG */
140#define SMBHSTCFG_HST_EN 1
141#define SMBHSTCFG_SMB_SMI_EN 2
142#define SMBHSTCFG_I2C_EN 4
143
Mika Westerberg94246932015-08-06 13:46:25 +0100144/* TCO configuration bits for TCOCTL */
145#define TCOCTL_EN 0x0100
146
Ellen Wang97d34ec2016-07-01 22:42:15 +0200147/* Auxiliary status register bits, ICH4+ only */
148#define SMBAUXSTS_CRCE 1
149#define SMBAUXSTS_STCO 2
150
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300151/* Auxiliary control register bits, ICH4+ only */
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200152#define SMBAUXCTL_CRC 1
153#define SMBAUXCTL_E32B 2
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Other settings */
Jean Delvare84c1af42012-03-26 21:47:19 +0200156#define MAX_RETRIES 400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* I801 command constants */
159#define I801_QUICK 0x00
160#define I801_BYTE 0x04
161#define I801_BYTE_DATA 0x08
162#define I801_WORD_DATA 0x0C
Jean Delvareae7b0492008-01-27 18:14:49 +0100163#define I801_PROC_CALL 0x10 /* unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define I801_BLOCK_DATA 0x14
Jean Delvare63420642008-01-27 18:14:50 +0100165#define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200166
167/* I801 Host Control register bits */
168#define SMBHSTCNT_INTREN 0x01
169#define SMBHSTCNT_KILL 0x02
170#define SMBHSTCNT_LAST_BYTE 0x20
171#define SMBHSTCNT_START 0x40
172#define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200174/* I801 Hosts Status register bits */
175#define SMBHSTSTS_BYTE_DONE 0x80
176#define SMBHSTSTS_INUSE_STS 0x40
177#define SMBHSTSTS_SMBALERT_STS 0x20
178#define SMBHSTSTS_FAILED 0x10
179#define SMBHSTSTS_BUS_ERR 0x08
180#define SMBHSTSTS_DEV_ERR 0x04
181#define SMBHSTSTS_INTR 0x02
182#define SMBHSTSTS_HOST_BUSY 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Daniel Kurtz70a1cc12012-07-24 14:13:58 +0200184#define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
185 SMBHSTSTS_DEV_ERR)
186
187#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
188 STATUS_ERROR_FLAGS)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200189
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200190/* Older devices have their ID defined in <linux/pci_ids.h> */
Jean Delvarece316112014-07-17 15:03:24 +0200191#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200192#define PCI_DEVICE_ID_INTEL_DNV_SMBUS 0x19df
Jean Delvarece316112014-07-17 15:03:24 +0200193#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
194#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
David Woodhouse55fee8d2010-10-31 21:07:00 +0100195/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
Jean Delvarece316112014-07-17 15:03:24 +0200196#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
197#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
198#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
199#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
200#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200201#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
Jean Delvarece316112014-07-17 15:03:24 +0200202#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
203#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
204#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200205#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
Jean Delvarece316112014-07-17 15:03:24 +0200206#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
Jean Delvareb299de82014-07-17 15:04:41 +0200207#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
Jean Delvarece316112014-07-17 15:03:24 +0200208#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
209#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
210#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
211#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
212#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
James Ralstonafc65922013-11-04 09:29:48 -0800213#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
Devin Ryles3eee17992014-11-05 16:30:03 -0500214#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200215#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
Alexandra Yatescdc5a312015-11-05 11:40:25 -0800216#define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3
217#define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223
David Woodhouse55fee8d2010-10-31 21:07:00 +0100218
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200219struct i801_mux_config {
220 char *gpio_chip;
221 unsigned values[3];
222 int n_values;
223 unsigned classes[3];
224 unsigned gpios[2]; /* Relative to gpio_chip->base */
225 int n_gpios;
226};
227
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100228struct i801_priv {
229 struct i2c_adapter adapter;
230 unsigned long smba;
231 unsigned char original_hstcfg;
232 struct pci_dev *pci_dev;
233 unsigned int features;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200234
235 /* isr processing */
236 wait_queue_head_t waitq;
237 u8 status;
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200238
239 /* Command state used by isr for byte-by-byte block transactions */
240 u8 cmd;
241 bool is_read;
242 int count;
243 int len;
244 u8 *data;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200245
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100246#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
247 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200248 const struct i801_mux_config *mux_drvdata;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200249 struct platform_device *mux_pdev;
250#endif
Mika Westerberg94246932015-08-06 13:46:25 +0100251 struct platform_device *tco_pdev;
Mika Westerberga7ae8192016-06-09 16:56:28 +0300252
253 /*
254 * If set to true the host controller registers are reserved for
255 * ACPI AML use. Protected by acpi_lock.
256 */
257 bool acpi_reserved;
258 struct mutex acpi_lock;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100259};
260
Jean Delvare369f6f42008-01-27 18:14:50 +0100261#define FEATURE_SMBUS_PEC (1 << 0)
262#define FEATURE_BLOCK_BUFFER (1 << 1)
263#define FEATURE_BLOCK_PROC (1 << 2)
264#define FEATURE_I2C_BLOCK_READ (1 << 3)
Daniel Kurtz636752b2012-07-24 14:13:58 +0200265#define FEATURE_IRQ (1 << 4)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200266/* Not really a feature, but it's convenient to handle it as such */
267#define FEATURE_IDF (1 << 15)
Mika Westerberg94246932015-08-06 13:46:25 +0100268#define FEATURE_TCO (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jean Delvareadff6872010-05-21 18:40:54 +0200270static const char *i801_feature_names[] = {
271 "SMBus PEC",
272 "Block buffer",
273 "Block process call",
274 "I2C block read",
Daniel Kurtz636752b2012-07-24 14:13:58 +0200275 "Interrupt",
Jean Delvareadff6872010-05-21 18:40:54 +0200276};
277
278static unsigned int disable_features;
279module_param(disable_features, uint, S_IRUGO | S_IWUSR);
Jean Delvare53229342013-05-15 02:44:10 +0000280MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
281 "\t\t 0x01 disable SMBus PEC\n"
282 "\t\t 0x02 disable the block buffer\n"
283 "\t\t 0x08 disable the I2C block read functionality\n"
284 "\t\t 0x10 don't use interrupts ");
Jean Delvareadff6872010-05-21 18:40:54 +0200285
Jean Delvarecf898dc2008-07-14 22:38:33 +0200286/* Make sure the SMBus host is ready to start transmitting.
287 Return 0 if it is, -EBUSY if it is not. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100288static int i801_check_pre(struct i801_priv *priv)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200289{
290 int status;
291
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100292 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200293 if (status & SMBHSTSTS_HOST_BUSY) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100294 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200295 return -EBUSY;
296 }
297
298 status &= STATUS_FLAGS;
299 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100300 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
Jean Delvarecf898dc2008-07-14 22:38:33 +0200301 status);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100302 outb_p(status, SMBHSTSTS(priv));
303 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200304 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100305 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200306 "Failed clearing status flags (%02x)\n",
307 status);
308 return -EBUSY;
309 }
310 }
311
Ellen Wang97d34ec2016-07-01 22:42:15 +0200312 /*
313 * Clear CRC status if needed.
314 * During normal operation, i801_check_post() takes care
315 * of it after every operation. We do it here only in case
316 * the hardware was already in this state when the driver
317 * started.
318 */
319 if (priv->features & FEATURE_SMBUS_PEC) {
320 status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
321 if (status) {
322 dev_dbg(&priv->pci_dev->dev,
323 "Clearing aux status flags (%02x)\n", status);
324 outb_p(status, SMBAUXSTS(priv));
325 status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
326 if (status) {
327 dev_err(&priv->pci_dev->dev,
328 "Failed clearing aux status flags (%02x)\n",
329 status);
330 return -EBUSY;
331 }
332 }
333 }
334
Jean Delvarecf898dc2008-07-14 22:38:33 +0200335 return 0;
336}
337
Jean Delvare6cad93c2012-07-24 14:13:58 +0200338/*
339 * Convert the status register to an error code, and clear it.
340 * Note that status only contains the bits we want to clear, not the
341 * actual register value.
342 */
343static int i801_check_post(struct i801_priv *priv, int status)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200344{
345 int result = 0;
346
Daniel Kurtz636752b2012-07-24 14:13:58 +0200347 /*
348 * If the SMBus is still busy, we give up
349 * Note: This timeout condition only happens when using polling
350 * transactions. For interrupt operation, NAK/timeout is indicated by
351 * DEV_ERR.
352 */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200353 if (unlikely(status < 0)) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100354 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200355 /* try to stop the current command */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100356 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
357 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
358 SMBHSTCNT(priv));
Jean Delvare84c1af42012-03-26 21:47:19 +0200359 usleep_range(1000, 2000);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100360 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
361 SMBHSTCNT(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200362
363 /* Check if it worked */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100364 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200365 if ((status & SMBHSTSTS_HOST_BUSY) ||
366 !(status & SMBHSTSTS_FAILED))
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100367 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200368 "Failed terminating the transaction\n");
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100369 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200370 return -ETIMEDOUT;
371 }
372
373 if (status & SMBHSTSTS_FAILED) {
374 result = -EIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100375 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200376 }
377 if (status & SMBHSTSTS_DEV_ERR) {
Ellen Wang97d34ec2016-07-01 22:42:15 +0200378 /*
379 * This may be a PEC error, check and clear it.
380 *
381 * AUXSTS is handled differently from HSTSTS.
382 * For HSTSTS, i801_isr() or i801_wait_intr()
383 * has already cleared the error bits in hardware,
384 * and we are passed a copy of the original value
385 * in "status".
386 * For AUXSTS, the hardware register is left
387 * for us to handle here.
388 * This is asymmetric, slightly iffy, but safe,
389 * since all this code is serialized and the CRCE
390 * bit is harmless as long as it's cleared before
391 * the next operation.
392 */
393 if ((priv->features & FEATURE_SMBUS_PEC) &&
394 (inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE)) {
395 outb_p(SMBAUXSTS_CRCE, SMBAUXSTS(priv));
396 result = -EBADMSG;
397 dev_dbg(&priv->pci_dev->dev, "PEC error\n");
398 } else {
399 result = -ENXIO;
400 dev_dbg(&priv->pci_dev->dev, "No response\n");
401 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200402 }
403 if (status & SMBHSTSTS_BUS_ERR) {
404 result = -EAGAIN;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100405 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200406 }
407
Jean Delvare6cad93c2012-07-24 14:13:58 +0200408 /* Clear status flags except BYTE_DONE, to be cleared by caller */
409 outb_p(status, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200410
411 return result;
412}
413
Jean Delvare6cad93c2012-07-24 14:13:58 +0200414/* Wait for BUSY being cleared and either INTR or an error flag being set */
415static int i801_wait_intr(struct i801_priv *priv)
416{
417 int timeout = 0;
418 int status;
419
420 /* We will always wait for a fraction of a second! */
421 do {
422 usleep_range(250, 500);
423 status = inb_p(SMBHSTSTS(priv));
424 } while (((status & SMBHSTSTS_HOST_BUSY) ||
425 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
426 (timeout++ < MAX_RETRIES));
427
428 if (timeout > MAX_RETRIES) {
429 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
430 return -ETIMEDOUT;
431 }
432 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
433}
434
435/* Wait for either BYTE_DONE or an error flag being set */
436static int i801_wait_byte_done(struct i801_priv *priv)
437{
438 int timeout = 0;
439 int status;
440
441 /* We will always wait for a fraction of a second! */
442 do {
443 usleep_range(250, 500);
444 status = inb_p(SMBHSTSTS(priv));
445 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
446 (timeout++ < MAX_RETRIES));
447
448 if (timeout > MAX_RETRIES) {
449 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
450 return -ETIMEDOUT;
451 }
452 return status & STATUS_ERROR_FLAGS;
453}
454
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100455static int i801_transaction(struct i801_priv *priv, int xact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Jean Delvare2b738092008-07-14 22:38:32 +0200457 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200458 int result;
Jean Delvareb3b8df92014-11-12 10:20:40 +0100459 const struct i2c_adapter *adap = &priv->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100461 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200462 if (result < 0)
463 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Daniel Kurtz636752b2012-07-24 14:13:58 +0200465 if (priv->features & FEATURE_IRQ) {
466 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
467 SMBHSTCNT(priv));
Jean Delvareb3b8df92014-11-12 10:20:40 +0100468 result = wait_event_timeout(priv->waitq,
469 (status = priv->status),
470 adap->timeout);
471 if (!result) {
472 status = -ETIMEDOUT;
473 dev_warn(&priv->pci_dev->dev,
474 "Timeout waiting for interrupt!\n");
475 }
Daniel Kurtz636752b2012-07-24 14:13:58 +0200476 priv->status = 0;
477 return i801_check_post(priv, status);
478 }
479
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200480 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
Daniel Kurtz37af8712012-07-24 14:13:58 +0200481 * SMBSCMD are passed in xact */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200482 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jean Delvare6cad93c2012-07-24 14:13:58 +0200484 status = i801_wait_intr(priv);
485 return i801_check_post(priv, status);
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200486}
487
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100488static int i801_block_transaction_by_block(struct i801_priv *priv,
489 union i2c_smbus_data *data,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200490 char read_write, int hwpec)
491{
492 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200493 int status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200494
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100495 inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200496
497 /* Use 32-byte buffer to process this transaction */
498 if (read_write == I2C_SMBUS_WRITE) {
499 len = data->block[0];
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100500 outb_p(len, SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200501 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100502 outb_p(data->block[i+1], SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200503 }
504
Daniel Kurtz37af8712012-07-24 14:13:58 +0200505 status = i801_transaction(priv, I801_BLOCK_DATA |
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200506 (hwpec ? SMBHSTCNT_PEC_EN : 0));
David Brownell97140342008-07-14 22:38:25 +0200507 if (status)
508 return status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200509
510 if (read_write == I2C_SMBUS_READ) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100511 len = inb_p(SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200512 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
David Brownell97140342008-07-14 22:38:25 +0200513 return -EPROTO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200514
515 data->block[0] = len;
516 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100517 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200518 }
519 return 0;
520}
521
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200522static void i801_isr_byte_done(struct i801_priv *priv)
523{
524 if (priv->is_read) {
525 /* For SMBus block reads, length is received with first byte */
526 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
527 (priv->count == 0)) {
528 priv->len = inb_p(SMBHSTDAT0(priv));
529 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
530 dev_err(&priv->pci_dev->dev,
531 "Illegal SMBus block read size %d\n",
532 priv->len);
533 /* FIXME: Recover */
534 priv->len = I2C_SMBUS_BLOCK_MAX;
535 } else {
536 dev_dbg(&priv->pci_dev->dev,
537 "SMBus block read size is %d\n",
538 priv->len);
539 }
540 priv->data[-1] = priv->len;
541 }
542
543 /* Read next byte */
544 if (priv->count < priv->len)
545 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
546 else
547 dev_dbg(&priv->pci_dev->dev,
548 "Discarding extra byte on block read\n");
549
550 /* Set LAST_BYTE for last byte of read transaction */
551 if (priv->count == priv->len - 1)
552 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
553 SMBHSTCNT(priv));
554 } else if (priv->count < priv->len - 1) {
555 /* Write next byte, except for IRQ after last byte */
556 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
557 }
558
559 /* Clear BYTE_DONE to continue with next byte */
560 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
561}
562
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200563/*
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200564 * There are two kinds of interrupts:
565 *
566 * 1) i801 signals transaction completion with one of these interrupts:
567 * INTR - Success
568 * DEV_ERR - Invalid command, NAK or communication timeout
569 * BUS_ERR - SMI# transaction collision
570 * FAILED - transaction was canceled due to a KILL request
571 * When any of these occur, update ->status and wake up the waitq.
572 * ->status must be cleared before kicking off the next transaction.
573 *
574 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
575 * occurs for each byte of a byte-by-byte to prepare the next byte.
Daniel Kurtz636752b2012-07-24 14:13:58 +0200576 */
577static irqreturn_t i801_isr(int irq, void *dev_id)
578{
579 struct i801_priv *priv = dev_id;
580 u16 pcists;
581 u8 status;
582
583 /* Confirm this is our interrupt */
584 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
585 if (!(pcists & SMBPCISTS_INTS))
586 return IRQ_NONE;
587
588 status = inb_p(SMBHSTSTS(priv));
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200589 if (status & SMBHSTSTS_BYTE_DONE)
590 i801_isr_byte_done(priv);
591
Daniel Kurtz636752b2012-07-24 14:13:58 +0200592 /*
593 * Clear irq sources and report transaction result.
594 * ->status must be cleared before the next transaction is started.
595 */
596 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
597 if (status) {
598 outb_p(status, SMBHSTSTS(priv));
Jean Delvarea90bc5d2016-05-25 09:37:02 +0200599 priv->status = status;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200600 wake_up(&priv->waitq);
601 }
602
603 return IRQ_HANDLED;
604}
605
606/*
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200607 * For "byte-by-byte" block transactions:
608 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
609 * I2C read uses cmd=I801_I2C_BLOCK_DATA
610 */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100611static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
612 union i2c_smbus_data *data,
Jean Delvare63420642008-01-27 18:14:50 +0100613 char read_write, int command,
614 int hwpec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 int i, len;
617 int smbcmd;
Jean Delvare2b738092008-07-14 22:38:32 +0200618 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200619 int result;
Jean Delvareb3b8df92014-11-12 10:20:40 +0100620 const struct i2c_adapter *adap = &priv->adapter;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200621
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100622 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200623 if (result < 0)
624 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200626 len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100629 outb_p(len, SMBHSTDAT0(priv));
630 outb_p(data->block[1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200633 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
634 read_write == I2C_SMBUS_READ)
635 smbcmd = I801_I2C_BLOCK_DATA;
636 else
637 smbcmd = I801_BLOCK_DATA;
638
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200639 if (priv->features & FEATURE_IRQ) {
640 priv->is_read = (read_write == I2C_SMBUS_READ);
641 if (len == 1 && priv->is_read)
642 smbcmd |= SMBHSTCNT_LAST_BYTE;
643 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
644 priv->len = len;
645 priv->count = 0;
646 priv->data = &data->block[1];
647
648 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
Jean Delvareb3b8df92014-11-12 10:20:40 +0100649 result = wait_event_timeout(priv->waitq,
650 (status = priv->status),
651 adap->timeout);
652 if (!result) {
653 status = -ETIMEDOUT;
654 dev_warn(&priv->pci_dev->dev,
655 "Timeout waiting for interrupt!\n");
656 }
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200657 priv->status = 0;
658 return i801_check_post(priv, status);
659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 for (i = 1; i <= len; i++) {
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200662 if (i == len && read_write == I2C_SMBUS_READ)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200663 smbcmd |= SMBHSTCNT_LAST_BYTE;
Daniel Kurtz37af8712012-07-24 14:13:58 +0200664 outb_p(smbcmd, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (i == 1)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200667 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100668 SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jean Delvare6cad93c2012-07-24 14:13:58 +0200670 status = i801_wait_byte_done(priv);
671 if (status)
672 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Jean Delvare63420642008-01-27 18:14:50 +0100674 if (i == 1 && read_write == I2C_SMBUS_READ
675 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100676 len = inb_p(SMBHSTDAT0(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200677 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100678 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200679 "Illegal SMBus block read size %d\n",
680 len);
681 /* Recover */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100682 while (inb_p(SMBHSTSTS(priv)) &
683 SMBHSTSTS_HOST_BUSY)
684 outb_p(SMBHSTSTS_BYTE_DONE,
685 SMBHSTSTS(priv));
686 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
David Brownell97140342008-07-14 22:38:25 +0200687 return -EPROTO;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 data->block[0] = len;
690 }
691
692 /* Retrieve/store value in SMBBLKDAT */
693 if (read_write == I2C_SMBUS_READ)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100694 data->block[i] = inb_p(SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100696 outb_p(data->block[i+1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Jean Delvarecf898dc2008-07-14 22:38:33 +0200698 /* signals SMBBLKDAT ready */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200699 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200700 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200701
Jean Delvare6cad93c2012-07-24 14:13:58 +0200702 status = i801_wait_intr(priv);
703exit:
704 return i801_check_post(priv, status);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200705}
706
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100707static int i801_set_block_buffer_mode(struct i801_priv *priv)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200708{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100709 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
710 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
David Brownell97140342008-07-14 22:38:25 +0200711 return -EIO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200712 return 0;
713}
714
715/* Block transaction function */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100716static int i801_block_transaction(struct i801_priv *priv,
717 union i2c_smbus_data *data, char read_write,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200718 int command, int hwpec)
719{
720 int result = 0;
721 unsigned char hostc;
722
723 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
724 if (read_write == I2C_SMBUS_WRITE) {
725 /* set I2C_EN bit in configuration register */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100726 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
727 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200728 hostc | SMBHSTCFG_I2C_EN);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100729 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
730 dev_err(&priv->pci_dev->dev,
Jean Delvare63420642008-01-27 18:14:50 +0100731 "I2C block read is unsupported!\n");
David Brownell97140342008-07-14 22:38:25 +0200732 return -EOPNOTSUPP;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Jean Delvare63420642008-01-27 18:14:50 +0100736 if (read_write == I2C_SMBUS_WRITE
737 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200738 if (data->block[0] < 1)
739 data->block[0] = 1;
740 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
741 data->block[0] = I2C_SMBUS_BLOCK_MAX;
742 } else {
Jean Delvare63420642008-01-27 18:14:50 +0100743 data->block[0] = 32; /* max for SMBus block reads */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200744 }
745
Jean Delvarec074c392010-03-13 20:56:53 +0100746 /* Experience has shown that the block buffer can only be used for
747 SMBus (not I2C) block transactions, even though the datasheet
748 doesn't mention this limitation. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100749 if ((priv->features & FEATURE_BLOCK_BUFFER)
Jean Delvarec074c392010-03-13 20:56:53 +0100750 && command != I2C_SMBUS_I2C_BLOCK_DATA
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100751 && i801_set_block_buffer_mode(priv) == 0)
752 result = i801_block_transaction_by_block(priv, data,
753 read_write, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200754 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100755 result = i801_block_transaction_byte_by_byte(priv, data,
756 read_write,
Jean Delvare63420642008-01-27 18:14:50 +0100757 command, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200758
Jean Delvare63420642008-01-27 18:14:50 +0100759 if (command == I2C_SMBUS_I2C_BLOCK_DATA
760 && read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* restore saved configuration register value */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100762 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 return result;
765}
766
David Brownell97140342008-07-14 22:38:25 +0200767/* Return negative errno on error. */
Ivo Manca3fb21c62010-05-21 18:40:55 +0200768static s32 i801_access(struct i2c_adapter *adap, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned short flags, char read_write, u8 command,
Ivo Manca3fb21c62010-05-21 18:40:55 +0200770 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200772 int hwpec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int block = 0;
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200774 int ret = 0, xact = 0;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100775 struct i801_priv *priv = i2c_get_adapdata(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Mika Westerberga7ae8192016-06-09 16:56:28 +0300777 mutex_lock(&priv->acpi_lock);
778 if (priv->acpi_reserved) {
779 mutex_unlock(&priv->acpi_lock);
780 return -EBUSY;
781 }
782
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200783 pm_runtime_get_sync(&priv->pci_dev->dev);
784
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100785 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200786 && size != I2C_SMBUS_QUICK
787 && size != I2C_SMBUS_I2C_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 switch (size) {
790 case I2C_SMBUS_QUICK:
791 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100792 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 xact = I801_QUICK;
794 break;
795 case I2C_SMBUS_BYTE:
796 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100797 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100799 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 xact = I801_BYTE;
801 break;
802 case I2C_SMBUS_BYTE_DATA:
803 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100804 SMBHSTADD(priv));
805 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100807 outb_p(data->byte, SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xact = I801_BYTE_DATA;
809 break;
810 case I2C_SMBUS_WORD_DATA:
811 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100812 SMBHSTADD(priv));
813 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100815 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
816 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 xact = I801_WORD_DATA;
819 break;
820 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100822 SMBHSTADD(priv));
823 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 block = 1;
825 break;
Jean Delvare63420642008-01-27 18:14:50 +0100826 case I2C_SMBUS_I2C_BLOCK_DATA:
827 /* NB: page 240 of ICH5 datasheet shows that the R/#W
828 * bit should be cleared here, even when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100829 outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100830 if (read_write == I2C_SMBUS_READ) {
831 /* NB: page 240 of ICH5 datasheet also shows
832 * that DATA1 is the cmd field when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100833 outb_p(command, SMBHSTDAT1(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100834 } else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100835 outb_p(command, SMBHSTCMD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100836 block = 1;
837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100839 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
840 size);
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200841 ret = -EOPNOTSUPP;
842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200845 if (hwpec) /* enable/disable hardware PEC */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100846 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200847 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100848 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
849 SMBAUXCTL(priv));
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200850
Ivo Manca3fb21c62010-05-21 18:40:55 +0200851 if (block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100852 ret = i801_block_transaction(priv, data, read_write, size,
853 hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200854 else
Daniel Kurtz37af8712012-07-24 14:13:58 +0200855 ret = i801_transaction(priv, xact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jean Delvarec79cfba2006-04-20 02:43:18 -0700857 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200858 time, so we forcibly disable it after every transaction. Turn off
859 E32B for the same reason. */
Jean Delvarea0921b62008-01-27 18:14:50 +0100860 if (hwpec || block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100861 outb_p(inb_p(SMBAUXCTL(priv)) &
862 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarec79cfba2006-04-20 02:43:18 -0700863
Ivo Manca3fb21c62010-05-21 18:40:55 +0200864 if (block)
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200865 goto out;
Ivo Manca3fb21c62010-05-21 18:40:55 +0200866 if (ret)
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 switch (xact & 0x7f) {
872 case I801_BYTE: /* Result put in SMBHSTDAT0 */
873 case I801_BYTE_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100874 data->byte = inb_p(SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876 case I801_WORD_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100877 data->word = inb_p(SMBHSTDAT0(priv)) +
878 (inb_p(SMBHSTDAT1(priv)) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
880 }
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200881
882out:
883 pm_runtime_mark_last_busy(&priv->pci_dev->dev);
884 pm_runtime_put_autosuspend(&priv->pci_dev->dev);
Mika Westerberga7ae8192016-06-09 16:56:28 +0300885 mutex_unlock(&priv->acpi_lock);
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889
890static u32 i801_func(struct i2c_adapter *adapter)
891{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100892 struct i801_priv *priv = i2c_get_adapdata(adapter);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare369f6f42008-01-27 18:14:50 +0100895 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
896 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100897 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
898 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
Jean Delvare63420642008-01-27 18:14:50 +0100899 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Jean Delvare8f9082c2006-09-03 22:39:46 +0200902static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .smbus_xfer = i801_access,
904 .functionality = i801_func,
905};
906
Jingoo Han392debf2013-12-03 08:11:20 +0900907static const struct pci_device_id i801_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
909 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
910 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
911 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
912 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
913 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
914 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
915 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
916 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
Jason Gastonb0a70b52005-04-16 15:24:45 -0700917 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
Jason Gaston8254fc42006-01-09 10:58:08 -0800918 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
Jason Gastonadbc2a12006-11-22 15:19:12 -0800919 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700920 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
Gaston, Jason Dd28dc712008-02-24 20:03:42 +0100921 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
922 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700923 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
924 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
Seth Heasleye30d9852010-10-31 21:06:59 +0100925 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
David Woodhouse55fee8d2010-10-31 21:07:00 +0100926 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
927 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
928 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
Seth Heasley662cda82011-03-20 14:50:53 +0100929 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
Seth Heasley6e2a8512011-05-24 20:58:49 +0200930 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
Seth Heasley062737f2012-03-26 21:47:19 +0200931 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
James Ralston4a8f1dd2012-09-10 10:14:02 +0200932 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
Seth Heasleyc2db409c2013-01-30 15:25:32 +0000933 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
James Ralstona3fc0ff2013-02-14 09:15:33 +0000934 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
935 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
936 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
937 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
Seth Heasleyf39901c2013-06-19 16:59:57 -0700938 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
Jean Delvareb299de82014-07-17 15:04:41 +0200939 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
James Ralstonafc65922013-11-04 09:29:48 -0800940 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
Chew, Kean ho1b31e9b2014-03-01 00:03:56 +0800941 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
Alan Cox39e8e302014-08-19 17:37:28 +0300942 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -0700943 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS) },
Devin Ryles3eee17992014-11-05 16:30:03 -0500944 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS) },
Mika Westerberg84d7f2e2015-10-13 15:41:39 +0300945 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMBUS) },
Jarkko Nikuladd77f422015-10-22 17:16:58 +0300946 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROXTON_SMBUS) },
Alexandra Yatescdc5a312015-11-05 11:40:25 -0800947 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS) },
948 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 { 0, }
950};
951
Ivo Manca3fb21c62010-05-21 18:40:55 +0200952MODULE_DEVICE_TABLE(pci, i801_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Jean Delvare8eacfce2011-05-24 20:58:49 +0200954#if defined CONFIG_X86 && defined CONFIG_DMI
Jean Delvare1561bfe2009-01-07 14:29:17 +0100955static unsigned char apanel_addr;
956
957/* Scan the system ROM for the signature "FJKEYINF" */
958static __init const void __iomem *bios_signature(const void __iomem *bios)
959{
960 ssize_t offset;
961 const unsigned char signature[] = "FJKEYINF";
962
963 for (offset = 0; offset < 0x10000; offset += 0x10) {
964 if (check_signature(bios + offset, signature,
965 sizeof(signature)-1))
966 return bios + offset;
967 }
968 return NULL;
969}
970
971static void __init input_apanel_init(void)
972{
973 void __iomem *bios;
974 const void __iomem *p;
975
976 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
977 p = bios_signature(bios);
978 if (p) {
979 /* just use the first address */
980 apanel_addr = readb(p + 8 + 3) >> 1;
981 }
982 iounmap(bios);
983}
Jean Delvare1561bfe2009-01-07 14:29:17 +0100984
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200985struct dmi_onboard_device_info {
986 const char *name;
987 u8 type;
988 unsigned short i2c_addr;
989 const char *i2c_type;
990};
991
Bill Pemberton0b255e92012-11-27 15:59:38 -0500992static const struct dmi_onboard_device_info dmi_devices[] = {
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200993 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
994 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
995 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
996};
997
Bill Pemberton0b255e92012-11-27 15:59:38 -0500998static void dmi_check_onboard_device(u8 type, const char *name,
999 struct i2c_adapter *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001000{
1001 int i;
1002 struct i2c_board_info info;
1003
1004 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
1005 /* & ~0x80, ignore enabled/disabled bit */
1006 if ((type & ~0x80) != dmi_devices[i].type)
1007 continue;
Jean Delvarefaabd472010-07-09 16:22:51 +02001008 if (strcasecmp(name, dmi_devices[i].name))
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001009 continue;
1010
1011 memset(&info, 0, sizeof(struct i2c_board_info));
1012 info.addr = dmi_devices[i].i2c_addr;
1013 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
1014 i2c_new_device(adap, &info);
1015 break;
1016 }
1017}
1018
1019/* We use our own function to check for onboard devices instead of
1020 dmi_find_device() as some buggy BIOS's have the devices we are interested
1021 in marked as disabled */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001022static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001023{
1024 int i, count;
1025
1026 if (dm->type != 10)
1027 return;
1028
1029 count = (dm->length - sizeof(struct dmi_header)) / 2;
1030 for (i = 0; i < count; i++) {
1031 const u8 *d = (char *)(dm + 1) + (i * 2);
1032 const char *name = ((char *) dm) + dm->length;
1033 u8 type = d[0];
1034 u8 s = d[1];
1035
1036 if (!s)
1037 continue;
1038 s--;
1039 while (s > 0 && name[0]) {
1040 name += strlen(name) + 1;
1041 s--;
1042 }
1043 if (name[0] == 0) /* Bogus string reference */
1044 continue;
1045
1046 dmi_check_onboard_device(type, name, adap);
1047 }
1048}
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001049
Jean Delvaree7198fb2011-05-24 20:58:49 +02001050/* Register optional slaves */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001051static void i801_probe_optional_slaves(struct i801_priv *priv)
Jean Delvaree7198fb2011-05-24 20:58:49 +02001052{
1053 /* Only register slaves on main SMBus channel */
1054 if (priv->features & FEATURE_IDF)
1055 return;
1056
Jean Delvaree7198fb2011-05-24 20:58:49 +02001057 if (apanel_addr) {
1058 struct i2c_board_info info;
1059
1060 memset(&info, 0, sizeof(struct i2c_board_info));
1061 info.addr = apanel_addr;
1062 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
1063 i2c_new_device(&priv->adapter, &info);
1064 }
Jean Delvare8eacfce2011-05-24 20:58:49 +02001065
Jean Delvaree7198fb2011-05-24 20:58:49 +02001066 if (dmi_name_in_vendors("FUJITSU"))
1067 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
Jean Delvaree7198fb2011-05-24 20:58:49 +02001068}
Jean Delvare8eacfce2011-05-24 20:58:49 +02001069#else
1070static void __init input_apanel_init(void) {}
Bill Pemberton0b255e92012-11-27 15:59:38 -05001071static void i801_probe_optional_slaves(struct i801_priv *priv) {}
Jean Delvare8eacfce2011-05-24 20:58:49 +02001072#endif /* CONFIG_X86 && CONFIG_DMI */
Jean Delvaree7198fb2011-05-24 20:58:49 +02001073
Jean Delvare79e3e5b2012-10-28 21:37:01 +01001074#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
1075 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001076static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
1077 .gpio_chip = "gpio_ich",
1078 .values = { 0x02, 0x03 },
1079 .n_values = 2,
1080 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
1081 .gpios = { 52, 53 },
1082 .n_gpios = 2,
1083};
1084
1085static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
1086 .gpio_chip = "gpio_ich",
1087 .values = { 0x02, 0x03, 0x01 },
1088 .n_values = 3,
1089 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
1090 .gpios = { 52, 53 },
1091 .n_gpios = 2,
1092};
1093
Bill Pemberton0b255e92012-11-27 15:59:38 -05001094static const struct dmi_system_id mux_dmi_table[] = {
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001095 {
1096 .matches = {
1097 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1098 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
1099 },
1100 .driver_data = &i801_mux_config_asus_z8_d12,
1101 },
1102 {
1103 .matches = {
1104 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1105 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
1106 },
1107 .driver_data = &i801_mux_config_asus_z8_d12,
1108 },
1109 {
1110 .matches = {
1111 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1112 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
1113 },
1114 .driver_data = &i801_mux_config_asus_z8_d12,
1115 },
1116 {
1117 .matches = {
1118 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1119 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
1120 },
1121 .driver_data = &i801_mux_config_asus_z8_d12,
1122 },
1123 {
1124 .matches = {
1125 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1126 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
1127 },
1128 .driver_data = &i801_mux_config_asus_z8_d12,
1129 },
1130 {
1131 .matches = {
1132 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1133 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
1134 },
1135 .driver_data = &i801_mux_config_asus_z8_d12,
1136 },
1137 {
1138 .matches = {
1139 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1140 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
1141 },
1142 .driver_data = &i801_mux_config_asus_z8_d18,
1143 },
1144 {
1145 .matches = {
1146 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1147 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1148 },
1149 .driver_data = &i801_mux_config_asus_z8_d18,
1150 },
1151 {
1152 .matches = {
1153 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1154 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1155 },
1156 .driver_data = &i801_mux_config_asus_z8_d12,
1157 },
1158 { }
1159};
1160
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001161/* Setup multiplexing if needed */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001162static int i801_add_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001163{
1164 struct device *dev = &priv->adapter.dev;
1165 const struct i801_mux_config *mux_config;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001166 struct i2c_mux_gpio_platform_data gpio_data;
Jean Delvaref82b8622012-10-05 22:23:54 +02001167 int err;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001168
1169 if (!priv->mux_drvdata)
1170 return 0;
1171 mux_config = priv->mux_drvdata;
1172
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001173 /* Prepare the platform data */
1174 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1175 gpio_data.parent = priv->adapter.nr;
1176 gpio_data.values = mux_config->values;
1177 gpio_data.n_values = mux_config->n_values;
1178 gpio_data.classes = mux_config->classes;
Jean Delvaref82b8622012-10-05 22:23:54 +02001179 gpio_data.gpio_chip = mux_config->gpio_chip;
1180 gpio_data.gpios = mux_config->gpios;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001181 gpio_data.n_gpios = mux_config->n_gpios;
1182 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1183
1184 /* Register the mux device */
1185 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
Jean Delvaref82b8622012-10-05 22:23:54 +02001186 PLATFORM_DEVID_AUTO, &gpio_data,
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001187 sizeof(struct i2c_mux_gpio_platform_data));
1188 if (IS_ERR(priv->mux_pdev)) {
1189 err = PTR_ERR(priv->mux_pdev);
1190 priv->mux_pdev = NULL;
1191 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1192 return err;
1193 }
1194
1195 return 0;
1196}
1197
Bill Pemberton0b255e92012-11-27 15:59:38 -05001198static void i801_del_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001199{
1200 if (priv->mux_pdev)
1201 platform_device_unregister(priv->mux_pdev);
1202}
1203
Bill Pemberton0b255e92012-11-27 15:59:38 -05001204static unsigned int i801_get_adapter_class(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001205{
1206 const struct dmi_system_id *id;
1207 const struct i801_mux_config *mux_config;
1208 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1209 int i;
1210
1211 id = dmi_first_match(mux_dmi_table);
1212 if (id) {
Jean Delvare28901f52012-10-28 21:37:01 +01001213 /* Remove branch classes from trunk */
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001214 mux_config = id->driver_data;
1215 for (i = 0; i < mux_config->n_values; i++)
1216 class &= ~mux_config->classes[i];
1217
1218 /* Remember for later */
1219 priv->mux_drvdata = mux_config;
1220 }
1221
1222 return class;
1223}
1224#else
1225static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1226static inline void i801_del_mux(struct i801_priv *priv) { }
1227
1228static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1229{
1230 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1231}
1232#endif
1233
Mika Westerberg94246932015-08-06 13:46:25 +01001234static const struct itco_wdt_platform_data tco_platform_data = {
1235 .name = "Intel PCH",
1236 .version = 4,
1237};
1238
1239static DEFINE_SPINLOCK(p2sb_spinlock);
1240
1241static void i801_add_tco(struct i801_priv *priv)
1242{
1243 struct pci_dev *pci_dev = priv->pci_dev;
1244 struct resource tco_res[3], *res;
1245 struct platform_device *pdev;
1246 unsigned int devfn;
1247 u32 tco_base, tco_ctl;
1248 u32 base_addr, ctrl_val;
1249 u64 base64_addr;
1250
1251 if (!(priv->features & FEATURE_TCO))
1252 return;
1253
1254 pci_read_config_dword(pci_dev, TCOBASE, &tco_base);
1255 pci_read_config_dword(pci_dev, TCOCTL, &tco_ctl);
1256 if (!(tco_ctl & TCOCTL_EN))
1257 return;
1258
1259 memset(tco_res, 0, sizeof(tco_res));
1260
1261 res = &tco_res[ICH_RES_IO_TCO];
1262 res->start = tco_base & ~1;
1263 res->end = res->start + 32 - 1;
1264 res->flags = IORESOURCE_IO;
1265
1266 /*
1267 * Power Management registers.
1268 */
1269 devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 2);
1270 pci_bus_read_config_dword(pci_dev->bus, devfn, ACPIBASE, &base_addr);
1271
1272 res = &tco_res[ICH_RES_IO_SMI];
1273 res->start = (base_addr & ~1) + ACPIBASE_SMI_OFF;
1274 res->end = res->start + 3;
1275 res->flags = IORESOURCE_IO;
1276
1277 /*
1278 * Enable the ACPI I/O space.
1279 */
1280 pci_bus_read_config_dword(pci_dev->bus, devfn, ACPICTRL, &ctrl_val);
1281 ctrl_val |= ACPICTRL_EN;
1282 pci_bus_write_config_dword(pci_dev->bus, devfn, ACPICTRL, ctrl_val);
1283
1284 /*
1285 * We must access the NO_REBOOT bit over the Primary to Sideband
1286 * bridge (P2SB). The BIOS prevents the P2SB device from being
1287 * enumerated by the PCI subsystem, so we need to unhide/hide it
1288 * to lookup the P2SB BAR.
1289 */
1290 spin_lock(&p2sb_spinlock);
1291
1292 devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 1);
1293
1294 /* Unhide the P2SB device */
1295 pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, 0x0);
1296
1297 pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR, &base_addr);
1298 base64_addr = base_addr & 0xfffffff0;
1299
1300 pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR + 0x4, &base_addr);
1301 base64_addr |= (u64)base_addr << 32;
1302
1303 /* Hide the P2SB device */
1304 pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, 0x1);
1305 spin_unlock(&p2sb_spinlock);
1306
1307 res = &tco_res[ICH_RES_MEM_OFF];
1308 res->start = (resource_size_t)base64_addr + SBREG_SMBCTRL;
1309 res->end = res->start + 3;
1310 res->flags = IORESOURCE_MEM;
1311
1312 pdev = platform_device_register_resndata(&pci_dev->dev, "iTCO_wdt", -1,
1313 tco_res, 3, &tco_platform_data,
1314 sizeof(tco_platform_data));
1315 if (IS_ERR(pdev)) {
1316 dev_warn(&pci_dev->dev, "failed to create iTCO device\n");
1317 return;
1318 }
1319
1320 priv->tco_pdev = pdev;
1321}
1322
Mika Westerberga7ae8192016-06-09 16:56:28 +03001323#ifdef CONFIG_ACPI
1324static acpi_status
1325i801_acpi_io_handler(u32 function, acpi_physical_address address, u32 bits,
1326 u64 *value, void *handler_context, void *region_context)
1327{
1328 struct i801_priv *priv = handler_context;
1329 struct pci_dev *pdev = priv->pci_dev;
1330 acpi_status status;
1331
1332 /*
1333 * Once BIOS AML code touches the OpRegion we warn and inhibit any
1334 * further access from the driver itself. This device is now owned
1335 * by the system firmware.
1336 */
1337 mutex_lock(&priv->acpi_lock);
1338
1339 if (!priv->acpi_reserved) {
1340 priv->acpi_reserved = true;
1341
1342 dev_warn(&pdev->dev, "BIOS is accessing SMBus registers\n");
1343 dev_warn(&pdev->dev, "Driver SMBus register access inhibited\n");
1344
1345 /*
1346 * BIOS is accessing the host controller so prevent it from
1347 * suspending automatically from now on.
1348 */
1349 pm_runtime_get_sync(&pdev->dev);
1350 }
1351
1352 if ((function & ACPI_IO_MASK) == ACPI_READ)
1353 status = acpi_os_read_port(address, (u32 *)value, bits);
1354 else
1355 status = acpi_os_write_port(address, (u32)*value, bits);
1356
1357 mutex_unlock(&priv->acpi_lock);
1358
1359 return status;
1360}
1361
1362static int i801_acpi_probe(struct i801_priv *priv)
1363{
1364 struct acpi_device *adev;
1365 acpi_status status;
1366
1367 adev = ACPI_COMPANION(&priv->pci_dev->dev);
1368 if (adev) {
1369 status = acpi_install_address_space_handler(adev->handle,
1370 ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler,
1371 NULL, priv);
1372 if (ACPI_SUCCESS(status))
1373 return 0;
1374 }
1375
1376 return acpi_check_resource_conflict(&priv->pci_dev->resource[SMBBAR]);
1377}
1378
1379static void i801_acpi_remove(struct i801_priv *priv)
1380{
1381 struct acpi_device *adev;
1382
1383 adev = ACPI_COMPANION(&priv->pci_dev->dev);
1384 if (!adev)
1385 return;
1386
1387 acpi_remove_address_space_handler(adev->handle,
1388 ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler);
1389
1390 mutex_lock(&priv->acpi_lock);
1391 if (priv->acpi_reserved)
1392 pm_runtime_put(&priv->pci_dev->dev);
1393 mutex_unlock(&priv->acpi_lock);
1394}
1395#else
1396static inline int i801_acpi_probe(struct i801_priv *priv) { return 0; }
1397static inline void i801_acpi_remove(struct i801_priv *priv) { }
1398#endif
1399
Bill Pemberton0b255e92012-11-27 15:59:38 -05001400static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001402 unsigned char temp;
Jean Delvareadff6872010-05-21 18:40:54 +02001403 int err, i;
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001404 struct i801_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Jarkko Nikula1621c592015-02-13 15:52:23 +02001406 priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001407 if (!priv)
1408 return -ENOMEM;
1409
1410 i2c_set_adapdata(&priv->adapter, priv);
1411 priv->adapter.owner = THIS_MODULE;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001412 priv->adapter.class = i801_get_adapter_class(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001413 priv->adapter.algo = &smbus_algorithm;
Dustin Byford8eb5c872015-10-23 12:27:07 -07001414 priv->adapter.dev.parent = &dev->dev;
1415 ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&dev->dev));
1416 priv->adapter.retries = 3;
Mika Westerberga7ae8192016-06-09 16:56:28 +03001417 mutex_init(&priv->acpi_lock);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001418
1419 priv->pci_dev = dev;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001420 switch (dev->device) {
Mika Westerberg94246932015-08-06 13:46:25 +01001421 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS:
1422 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS:
Alexandra Yates1a1503c2016-02-17 18:21:21 -08001423 case PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS:
1424 case PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS:
Mika Westerberg84d7f2e2015-10-13 15:41:39 +03001425 case PCI_DEVICE_ID_INTEL_DNV_SMBUS:
Mika Westerberg94246932015-08-06 13:46:25 +01001426 priv->features |= FEATURE_I2C_BLOCK_READ;
1427 priv->features |= FEATURE_IRQ;
1428 priv->features |= FEATURE_SMBUS_PEC;
1429 priv->features |= FEATURE_BLOCK_BUFFER;
1430 priv->features |= FEATURE_TCO;
1431 break;
1432
Jean Delvaree7198fb2011-05-24 20:58:49 +02001433 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1434 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1435 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
James Ralstona3fc0ff2013-02-14 09:15:33 +00001436 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
1437 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
1438 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
Jean Delvaree7198fb2011-05-24 20:58:49 +02001439 priv->features |= FEATURE_IDF;
1440 /* fall through */
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001441 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001442 priv->features |= FEATURE_I2C_BLOCK_READ;
Jean Delvare6676a842012-12-16 21:11:55 +01001443 priv->features |= FEATURE_IRQ;
Jean Delvare63420642008-01-27 18:14:50 +01001444 /* fall through */
1445 case PCI_DEVICE_ID_INTEL_82801DB_3:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001446 priv->features |= FEATURE_SMBUS_PEC;
1447 priv->features |= FEATURE_BLOCK_BUFFER;
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001448 /* fall through */
1449 case PCI_DEVICE_ID_INTEL_82801CA_3:
1450 case PCI_DEVICE_ID_INTEL_82801BA_2:
1451 case PCI_DEVICE_ID_INTEL_82801AB_3:
1452 case PCI_DEVICE_ID_INTEL_82801AA_3:
Jean Delvare250d1bd2006-12-10 21:21:33 +01001453 break;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001454 }
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001455
Jean Delvareadff6872010-05-21 18:40:54 +02001456 /* Disable features on user request */
1457 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001458 if (priv->features & disable_features & (1 << i))
Jean Delvareadff6872010-05-21 18:40:54 +02001459 dev_notice(&dev->dev, "%s disabled by user\n",
1460 i801_feature_names[i]);
1461 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001462 priv->features &= ~disable_features;
Jean Delvareadff6872010-05-21 18:40:54 +02001463
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001464 err = pcim_enable_device(dev);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001465 if (err) {
1466 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1467 err);
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001468 return err;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001469 }
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001470 pcim_pin_device(dev);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001471
1472 /* Determine the address of the SMBus area */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001473 priv->smba = pci_resource_start(dev, SMBBAR);
1474 if (!priv->smba) {
Jarkko Nikula9cbbf3d2015-02-13 15:52:21 +02001475 dev_err(&dev->dev,
1476 "SMBus base address uninitialized, upgrade BIOS\n");
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001477 return -ENODEV;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001478 }
1479
Mika Westerberga7ae8192016-06-09 16:56:28 +03001480 if (i801_acpi_probe(priv))
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001481 return -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +02001482
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001483 err = pcim_iomap_regions(dev, 1 << SMBBAR,
1484 dev_driver_string(&dev->dev));
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001485 if (err) {
Jarkko Nikula9cbbf3d2015-02-13 15:52:21 +02001486 dev_err(&dev->dev,
1487 "Failed to request SMBus region 0x%lx-0x%Lx\n",
1488 priv->smba,
Andrew Morton598736c2006-06-30 01:56:20 -07001489 (unsigned long long)pci_resource_end(dev, SMBBAR));
Mika Westerberga7ae8192016-06-09 16:56:28 +03001490 i801_acpi_remove(priv);
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001491 return err;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001492 }
1493
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001494 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1495 priv->original_hstcfg = temp;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001496 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
1497 if (!(temp & SMBHSTCFG_HST_EN)) {
1498 dev_info(&dev->dev, "Enabling SMBus device\n");
1499 temp |= SMBHSTCFG_HST_EN;
1500 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001501 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001502
Daniel Kurtz636752b2012-07-24 14:13:58 +02001503 if (temp & SMBHSTCFG_SMB_SMI_EN) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001504 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001505 /* Disable SMBus interrupt feature if SMBus using SMI# */
1506 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Jean Delvarea0921b62008-01-27 18:14:50 +01001509 /* Clear special mode bits */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001510 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1511 outb_p(inb_p(SMBAUXCTL(priv)) &
1512 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarea0921b62008-01-27 18:14:50 +01001513
Jean Delvareb3b8df92014-11-12 10:20:40 +01001514 /* Default timeout in interrupt mode: 200 ms */
1515 priv->adapter.timeout = HZ / 5;
1516
Daniel Kurtz636752b2012-07-24 14:13:58 +02001517 if (priv->features & FEATURE_IRQ) {
Jean Delvareaeb8a3d2014-11-12 10:25:37 +01001518 u16 pcictl, pcists;
1519
1520 /* Complain if an interrupt is already pending */
1521 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
1522 if (pcists & SMBPCISTS_INTS)
1523 dev_warn(&dev->dev, "An interrupt is pending!\n");
1524
1525 /* Check if interrupts have been disabled */
1526 pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
1527 if (pcictl & SMBPCICTL_INTDIS) {
1528 dev_info(&dev->dev, "Interrupts are disabled\n");
1529 priv->features &= ~FEATURE_IRQ;
1530 }
1531 }
1532
1533 if (priv->features & FEATURE_IRQ) {
Daniel Kurtz636752b2012-07-24 14:13:58 +02001534 init_waitqueue_head(&priv->waitq);
1535
Jarkko Nikula1621c592015-02-13 15:52:23 +02001536 err = devm_request_irq(&dev->dev, dev->irq, i801_isr,
1537 IRQF_SHARED,
1538 dev_driver_string(&dev->dev), priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001539 if (err) {
1540 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1541 dev->irq, err);
Jean Delvareae944712014-11-12 10:24:07 +01001542 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001543 }
1544 }
Jean Delvareae944712014-11-12 10:24:07 +01001545 dev_info(&dev->dev, "SMBus using %s\n",
1546 priv->features & FEATURE_IRQ ? "PCI interrupt" : "polling");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001547
Mika Westerberg94246932015-08-06 13:46:25 +01001548 i801_add_tco(priv);
1549
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001550 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1551 "SMBus I801 adapter at %04lx", priv->smba);
1552 err = i2c_add_adapter(&priv->adapter);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001553 if (err) {
1554 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
Mika Westerberga7ae8192016-06-09 16:56:28 +03001555 i801_acpi_remove(priv);
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001556 return err;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001557 }
Jean Delvare1561bfe2009-01-07 14:29:17 +01001558
Jean Delvaree7198fb2011-05-24 20:58:49 +02001559 i801_probe_optional_slaves(priv);
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001560 /* We ignore errors - multiplexing is optional */
1561 i801_add_mux(priv);
Jean Delvare1561bfe2009-01-07 14:29:17 +01001562
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001563 pci_set_drvdata(dev, priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001564
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +02001565 pm_runtime_set_autosuspend_delay(&dev->dev, 1000);
1566 pm_runtime_use_autosuspend(&dev->dev);
1567 pm_runtime_put_autosuspend(&dev->dev);
1568 pm_runtime_allow(&dev->dev);
1569
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Bill Pemberton0b255e92012-11-27 15:59:38 -05001573static void i801_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001575 struct i801_priv *priv = pci_get_drvdata(dev);
1576
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +02001577 pm_runtime_forbid(&dev->dev);
1578 pm_runtime_get_noresume(&dev->dev);
1579
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001580 i801_del_mux(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001581 i2c_del_adapter(&priv->adapter);
Mika Westerberga7ae8192016-06-09 16:56:28 +03001582 i801_acpi_remove(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001583 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001584
Mika Westerberg94246932015-08-06 13:46:25 +01001585 platform_device_unregister(priv->tco_pdev);
1586
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001587 /*
1588 * do not call pci_disable_device(dev) since it can cause hard hangs on
1589 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Jean Delvarea5aaea32007-03-22 19:49:01 +01001593#ifdef CONFIG_PM
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001594static int i801_suspend(struct device *dev)
Jean Delvarea5aaea32007-03-22 19:49:01 +01001595{
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001596 struct pci_dev *pci_dev = to_pci_dev(dev);
1597 struct i801_priv *priv = pci_get_drvdata(pci_dev);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001598
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001599 pci_write_config_byte(pci_dev, SMBHSTCFG, priv->original_hstcfg);
Jean Delvarea5aaea32007-03-22 19:49:01 +01001600 return 0;
1601}
1602
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001603static int i801_resume(struct device *dev)
Jean Delvarea5aaea32007-03-22 19:49:01 +01001604{
Jarkko Nikulaf85da3f2015-02-13 15:52:24 +02001605 return 0;
Jean Delvarea5aaea32007-03-22 19:49:01 +01001606}
Jean Delvarea5aaea32007-03-22 19:49:01 +01001607#endif
1608
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001609static UNIVERSAL_DEV_PM_OPS(i801_pm_ops, i801_suspend,
1610 i801_resume, NULL);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static struct pci_driver i801_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 .name = "i801_smbus",
1614 .id_table = i801_ids,
1615 .probe = i801_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001616 .remove = i801_remove,
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001617 .driver = {
1618 .pm = &i801_pm_ops,
1619 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620};
1621
1622static int __init i2c_i801_init(void)
1623{
Jean Delvare6aa14642011-05-24 20:58:49 +02001624 if (dmi_name_in_vendors("FUJITSU"))
1625 input_apanel_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return pci_register_driver(&i801_driver);
1627}
1628
1629static void __exit i2c_i801_exit(void)
1630{
1631 pci_unregister_driver(&i801_driver);
1632}
1633
Jean Delvare7c81c602014-01-29 20:40:08 +01001634MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635MODULE_DESCRIPTION("I801 SMBus driver");
1636MODULE_LICENSE("GPL");
1637
1638module_init(i2c_i801_init);
1639module_exit(i2c_i801_exit);