blob: 88e4dd2adbfedb3ea87aa8738e811fc050a936e1 [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
Jarkko Nikula15407792018-02-16 11:24:29 +020061 * Braswell (SOC) 0x2292 32 hard yes yes yes
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -070062 * Sunrise Point-H (PCH) 0xa123 32 hard yes yes yes
Devin Ryles3eee17992014-11-05 16:30:03 -050063 * Sunrise Point-LP (PCH) 0x9d23 32 hard yes yes yes
Mika Westerberg84d7f2e2015-10-13 15:41:39 +030064 * DNV (SOC) 0x19df 32 hard yes yes yes
Jarkko Nikuladd77f422015-10-22 17:16:58 +030065 * Broxton (SOC) 0x5ad4 32 hard yes yes yes
Alexandra Yatescdc5a312015-11-05 11:40:25 -080066 * Lewisburg (PCH) 0xa1a3 32 hard yes yes yes
67 * Lewisburg Supersku (PCH) 0xa223 32 hard yes yes yes
Andy Shevchenko31158762016-09-23 11:56:01 +030068 * Kaby Lake PCH-H (PCH) 0xa2a3 32 hard yes yes yes
Mika Westerberg9827f9e2017-02-01 19:20:59 +030069 * Gemini Lake (SOC) 0x31d4 32 hard yes yes yes
Srinivas Pandruvada09a1de02017-05-18 11:23:06 +030070 * Cannon Lake-H (PCH) 0xa323 32 hard yes yes yes
71 * Cannon Lake-LP (PCH) 0x9da3 32 hard yes yes yes
Jarkko Nikulacb09d942017-09-21 16:23:16 +030072 * Cedar Fork (PCH) 0x18df 32 hard yes yes yes
Mika Westerberg0bff2a82018-06-28 16:08:24 +030073 * Ice Lake-LP (PCH) 0x34a3 32 hard yes yes yes
Jarkko Nikula5cd1c562019-03-15 12:56:49 +020074 * Comet Lake (PCH) 0x02a3 32 hard yes yes yes
Jean Delvarece316112014-07-17 15:03:24 +020075 *
76 * Features supported by this driver:
77 * Software PEC no
78 * Hardware PEC yes
79 * Block buffer yes
80 * Block process call transaction no
81 * I2C block read transaction yes (doesn't use the block buffer)
82 * Slave mode no
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +020083 * SMBus Host Notify yes
Jean Delvarece316112014-07-17 15:03:24 +020084 * Interrupt processing yes
85 *
86 * See the file Documentation/i2c/busses/i2c-i801 for details.
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Daniel Kurtz636752b2012-07-24 14:13:58 +020089#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/module.h>
91#include <linux/pci.h>
92#include <linux/kernel.h>
93#include <linux/stddef.h>
94#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/ioport.h>
96#include <linux/init.h>
97#include <linux/i2c.h>
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +020098#include <linux/i2c-smbus.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020099#include <linux/acpi.h>
Jean Delvare1561bfe2009-01-07 14:29:17 +0100100#include <linux/io.h>
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200101#include <linux/dmi.h>
Ben Hutchings665a96b2011-01-10 22:11:22 +0100102#include <linux/slab.h>
Andy Shevchenkoaf668d62019-06-21 14:36:24 +0300103#include <linux/string.h>
Daniel Kurtz636752b2012-07-24 14:13:58 +0200104#include <linux/wait.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200105#include <linux/err.h>
Mika Westerberg94246932015-08-06 13:46:25 +0100106#include <linux/platform_device.h>
107#include <linux/platform_data/itco_wdt.h>
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200108#include <linux/pm_runtime.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200109
Javier Martinez Canillas175c7082016-07-21 12:11:01 -0400110#if IS_ENABLED(CONFIG_I2C_MUX_GPIO) && defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200111#include <linux/gpio.h>
Wolfram Sang62ea22c2018-04-19 22:00:08 +0200112#include <linux/platform_data/i2c-mux-gpio.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* I801 SMBus address offsets */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100116#define SMBHSTSTS(p) (0 + (p)->smba)
117#define SMBHSTCNT(p) (2 + (p)->smba)
118#define SMBHSTCMD(p) (3 + (p)->smba)
119#define SMBHSTADD(p) (4 + (p)->smba)
120#define SMBHSTDAT0(p) (5 + (p)->smba)
121#define SMBHSTDAT1(p) (6 + (p)->smba)
122#define SMBBLKDAT(p) (7 + (p)->smba)
123#define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
124#define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
125#define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200126#define SMBSLVSTS(p) (16 + (p)->smba) /* ICH3 and later */
127#define SMBSLVCMD(p) (17 + (p)->smba) /* ICH3 and later */
128#define SMBNTFDADD(p) (20 + (p)->smba) /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* PCI Address Constants */
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200131#define SMBBAR 4
Jean Delvareaeb8a3d2014-11-12 10:25:37 +0100132#define SMBPCICTL 0x004
Daniel Kurtz636752b2012-07-24 14:13:58 +0200133#define SMBPCISTS 0x006
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define SMBHSTCFG 0x040
Mika Westerberg94246932015-08-06 13:46:25 +0100135#define TCOBASE 0x050
136#define TCOCTL 0x054
137
138#define ACPIBASE 0x040
139#define ACPIBASE_SMI_OFF 0x030
140#define ACPICTRL 0x044
141#define ACPICTRL_EN 0x080
142
143#define SBREG_BAR 0x10
144#define SBREG_SMBCTRL 0xc6000c
Felipe Balbi851a1512018-09-03 11:24:57 +0300145#define SBREG_SMBCTRL_DNV 0xcf000c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Daniel Kurtz636752b2012-07-24 14:13:58 +0200147/* Host status bits for SMBPCISTS */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200148#define SMBPCISTS_INTS BIT(3)
Daniel Kurtz636752b2012-07-24 14:13:58 +0200149
Jean Delvareaeb8a3d2014-11-12 10:25:37 +0100150/* Control bits for SMBPCICTL */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200151#define SMBPCICTL_INTDIS BIT(10)
Jean Delvareaeb8a3d2014-11-12 10:25:37 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Host configuration bits for SMBHSTCFG */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200154#define SMBHSTCFG_HST_EN BIT(0)
155#define SMBHSTCFG_SMB_SMI_EN BIT(1)
156#define SMBHSTCFG_I2C_EN BIT(2)
157#define SMBHSTCFG_SPD_WD BIT(4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Mika Westerberg94246932015-08-06 13:46:25 +0100159/* TCO configuration bits for TCOCTL */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200160#define TCOCTL_EN BIT(8)
Mika Westerberg94246932015-08-06 13:46:25 +0100161
Ellen Wang97d34ec2016-07-01 22:42:15 +0200162/* Auxiliary status register bits, ICH4+ only */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200163#define SMBAUXSTS_CRCE BIT(0)
164#define SMBAUXSTS_STCO BIT(1)
Ellen Wang97d34ec2016-07-01 22:42:15 +0200165
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166/* Auxiliary control register bits, ICH4+ only */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200167#define SMBAUXCTL_CRC BIT(0)
168#define SMBAUXCTL_E32B BIT(1)
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Other settings */
Jean Delvare84c1af42012-03-26 21:47:19 +0200171#define MAX_RETRIES 400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* I801 command constants */
174#define I801_QUICK 0x00
175#define I801_BYTE 0x04
176#define I801_BYTE_DATA 0x08
177#define I801_WORD_DATA 0x0C
Jean Delvareae7b0492008-01-27 18:14:49 +0100178#define I801_PROC_CALL 0x10 /* unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define I801_BLOCK_DATA 0x14
Jean Delvare63420642008-01-27 18:14:50 +0100180#define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200181
182/* I801 Host Control register bits */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200183#define SMBHSTCNT_INTREN BIT(0)
184#define SMBHSTCNT_KILL BIT(1)
185#define SMBHSTCNT_LAST_BYTE BIT(5)
186#define SMBHSTCNT_START BIT(6)
187#define SMBHSTCNT_PEC_EN BIT(7) /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200189/* I801 Hosts Status register bits */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200190#define SMBHSTSTS_BYTE_DONE BIT(7)
191#define SMBHSTSTS_INUSE_STS BIT(6)
192#define SMBHSTSTS_SMBALERT_STS BIT(5)
193#define SMBHSTSTS_FAILED BIT(4)
194#define SMBHSTSTS_BUS_ERR BIT(3)
195#define SMBHSTSTS_DEV_ERR BIT(2)
196#define SMBHSTSTS_INTR BIT(1)
197#define SMBHSTSTS_HOST_BUSY BIT(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Benjamin Tissoires9786b1f2016-10-13 14:10:36 +0200199/* Host Notify Status register bits */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200200#define SMBSLVSTS_HST_NTFY_STS BIT(0)
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200201
Benjamin Tissoires9786b1f2016-10-13 14:10:36 +0200202/* Host Notify Command register bits */
Benjamin Tissoiresfe9ba3e2016-10-13 14:10:37 +0200203#define SMBSLVCMD_HST_NTFY_INTREN BIT(0)
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200204
Daniel Kurtz70a1cc12012-07-24 14:13:58 +0200205#define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
206 SMBHSTSTS_DEV_ERR)
207
208#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
209 STATUS_ERROR_FLAGS)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200210
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200211/* Older devices have their ID defined in <linux/pci_ids.h> */
Jean Delvarece316112014-07-17 15:03:24 +0200212#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
Jarkko Nikulacb09d942017-09-21 16:23:16 +0300213#define PCI_DEVICE_ID_INTEL_CDF_SMBUS 0x18df
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200214#define PCI_DEVICE_ID_INTEL_DNV_SMBUS 0x19df
Jean Delvarece316112014-07-17 15:03:24 +0200215#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
216#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
David Woodhouse55fee8d2010-10-31 21:07:00 +0100217/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
Jean Delvarece316112014-07-17 15:03:24 +0200218#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
219#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
220#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
221#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
222#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200223#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
Jean Delvarece316112014-07-17 15:03:24 +0200224#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
225#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
Mika Westerberg9827f9e2017-02-01 19:20:59 +0300226#define PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS 0x31d4
Mika Westerberg0bff2a82018-06-28 16:08:24 +0300227#define PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS 0x34a3
Jean Delvarece316112014-07-17 15:03:24 +0200228#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200229#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
Jean Delvarece316112014-07-17 15:03:24 +0200230#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
Jean Delvareb299de82014-07-17 15:04:41 +0200231#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
Jean Delvarece316112014-07-17 15:03:24 +0200232#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
233#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
234#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
235#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
236#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
James Ralstonafc65922013-11-04 09:29:48 -0800237#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
Devin Ryles3eee17992014-11-05 16:30:03 -0500238#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23
Srinivas Pandruvada09a1de02017-05-18 11:23:06 +0300239#define PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS 0x9da3
Andy Shevchenko34b57f42016-03-09 14:14:17 +0200240#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
Alexandra Yatescdc5a312015-11-05 11:40:25 -0800241#define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3
242#define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223
Andy Shevchenko31158762016-09-23 11:56:01 +0300243#define PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS 0xa2a3
Srinivas Pandruvada09a1de02017-05-18 11:23:06 +0300244#define PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS 0xa323
Jarkko Nikula5cd1c562019-03-15 12:56:49 +0200245#define PCI_DEVICE_ID_INTEL_COMETLAKE_SMBUS 0x02a3
David Woodhouse55fee8d2010-10-31 21:07:00 +0100246
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200247struct i801_mux_config {
248 char *gpio_chip;
249 unsigned values[3];
250 int n_values;
251 unsigned classes[3];
252 unsigned gpios[2]; /* Relative to gpio_chip->base */
253 int n_gpios;
254};
255
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100256struct i801_priv {
257 struct i2c_adapter adapter;
258 unsigned long smba;
259 unsigned char original_hstcfg;
Benjamin Tissoires22e94bd2016-10-13 14:10:35 +0200260 unsigned char original_slvcmd;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100261 struct pci_dev *pci_dev;
262 unsigned int features;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200263
264 /* isr processing */
265 wait_queue_head_t waitq;
266 u8 status;
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200267
268 /* Command state used by isr for byte-by-byte block transactions */
269 u8 cmd;
270 bool is_read;
271 int count;
272 int len;
273 u8 *data;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200274
Javier Martinez Canillas175c7082016-07-21 12:11:01 -0400275#if IS_ENABLED(CONFIG_I2C_MUX_GPIO) && defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200276 const struct i801_mux_config *mux_drvdata;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200277 struct platform_device *mux_pdev;
278#endif
Mika Westerberg94246932015-08-06 13:46:25 +0100279 struct platform_device *tco_pdev;
Mika Westerberga7ae8192016-06-09 16:56:28 +0300280
281 /*
282 * If set to true the host controller registers are reserved for
283 * ACPI AML use. Protected by acpi_lock.
284 */
285 bool acpi_reserved;
286 struct mutex acpi_lock;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100287};
288
Benjamin Tissoiresf91fba62016-10-13 14:10:38 +0200289#define FEATURE_SMBUS_PEC BIT(0)
290#define FEATURE_BLOCK_BUFFER BIT(1)
291#define FEATURE_BLOCK_PROC BIT(2)
292#define FEATURE_I2C_BLOCK_READ BIT(3)
293#define FEATURE_IRQ BIT(4)
294#define FEATURE_HOST_NOTIFY BIT(5)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200295/* Not really a feature, but it's convenient to handle it as such */
Benjamin Tissoiresf91fba62016-10-13 14:10:38 +0200296#define FEATURE_IDF BIT(15)
297#define FEATURE_TCO BIT(16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jean Delvareadff6872010-05-21 18:40:54 +0200299static const char *i801_feature_names[] = {
300 "SMBus PEC",
301 "Block buffer",
302 "Block process call",
303 "I2C block read",
Daniel Kurtz636752b2012-07-24 14:13:58 +0200304 "Interrupt",
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200305 "SMBus Host Notify",
Jean Delvareadff6872010-05-21 18:40:54 +0200306};
307
308static unsigned int disable_features;
309module_param(disable_features, uint, S_IRUGO | S_IWUSR);
Jean Delvare53229342013-05-15 02:44:10 +0000310MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
311 "\t\t 0x01 disable SMBus PEC\n"
312 "\t\t 0x02 disable the block buffer\n"
313 "\t\t 0x08 disable the I2C block read functionality\n"
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200314 "\t\t 0x10 don't use interrupts\n"
315 "\t\t 0x20 disable SMBus Host Notify ");
Jean Delvareadff6872010-05-21 18:40:54 +0200316
Jean Delvarecf898dc2008-07-14 22:38:33 +0200317/* Make sure the SMBus host is ready to start transmitting.
318 Return 0 if it is, -EBUSY if it is not. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100319static int i801_check_pre(struct i801_priv *priv)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200320{
321 int status;
322
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100323 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200324 if (status & SMBHSTSTS_HOST_BUSY) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100325 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200326 return -EBUSY;
327 }
328
329 status &= STATUS_FLAGS;
330 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100331 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
Jean Delvarecf898dc2008-07-14 22:38:33 +0200332 status);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100333 outb_p(status, SMBHSTSTS(priv));
334 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200335 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100336 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200337 "Failed clearing status flags (%02x)\n",
338 status);
339 return -EBUSY;
340 }
341 }
342
Ellen Wang97d34ec2016-07-01 22:42:15 +0200343 /*
344 * Clear CRC status if needed.
345 * During normal operation, i801_check_post() takes care
346 * of it after every operation. We do it here only in case
347 * the hardware was already in this state when the driver
348 * started.
349 */
350 if (priv->features & FEATURE_SMBUS_PEC) {
351 status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
352 if (status) {
353 dev_dbg(&priv->pci_dev->dev,
354 "Clearing aux status flags (%02x)\n", status);
355 outb_p(status, SMBAUXSTS(priv));
356 status = inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE;
357 if (status) {
358 dev_err(&priv->pci_dev->dev,
359 "Failed clearing aux status flags (%02x)\n",
360 status);
361 return -EBUSY;
362 }
363 }
364 }
365
Jean Delvarecf898dc2008-07-14 22:38:33 +0200366 return 0;
367}
368
Jean Delvare6cad93c2012-07-24 14:13:58 +0200369/*
370 * Convert the status register to an error code, and clear it.
371 * Note that status only contains the bits we want to clear, not the
372 * actual register value.
373 */
374static int i801_check_post(struct i801_priv *priv, int status)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200375{
376 int result = 0;
377
Daniel Kurtz636752b2012-07-24 14:13:58 +0200378 /*
379 * If the SMBus is still busy, we give up
380 * Note: This timeout condition only happens when using polling
381 * transactions. For interrupt operation, NAK/timeout is indicated by
382 * DEV_ERR.
383 */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200384 if (unlikely(status < 0)) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100385 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200386 /* try to stop the current command */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100387 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
388 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
389 SMBHSTCNT(priv));
Jean Delvare84c1af42012-03-26 21:47:19 +0200390 usleep_range(1000, 2000);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100391 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
392 SMBHSTCNT(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200393
394 /* Check if it worked */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100395 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200396 if ((status & SMBHSTSTS_HOST_BUSY) ||
397 !(status & SMBHSTSTS_FAILED))
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100398 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200399 "Failed terminating the transaction\n");
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100400 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200401 return -ETIMEDOUT;
402 }
403
404 if (status & SMBHSTSTS_FAILED) {
405 result = -EIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100406 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200407 }
408 if (status & SMBHSTSTS_DEV_ERR) {
Ellen Wang97d34ec2016-07-01 22:42:15 +0200409 /*
410 * This may be a PEC error, check and clear it.
411 *
412 * AUXSTS is handled differently from HSTSTS.
413 * For HSTSTS, i801_isr() or i801_wait_intr()
414 * has already cleared the error bits in hardware,
415 * and we are passed a copy of the original value
416 * in "status".
417 * For AUXSTS, the hardware register is left
418 * for us to handle here.
419 * This is asymmetric, slightly iffy, but safe,
420 * since all this code is serialized and the CRCE
421 * bit is harmless as long as it's cleared before
422 * the next operation.
423 */
424 if ((priv->features & FEATURE_SMBUS_PEC) &&
425 (inb_p(SMBAUXSTS(priv)) & SMBAUXSTS_CRCE)) {
426 outb_p(SMBAUXSTS_CRCE, SMBAUXSTS(priv));
427 result = -EBADMSG;
428 dev_dbg(&priv->pci_dev->dev, "PEC error\n");
429 } else {
430 result = -ENXIO;
431 dev_dbg(&priv->pci_dev->dev, "No response\n");
432 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200433 }
434 if (status & SMBHSTSTS_BUS_ERR) {
435 result = -EAGAIN;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100436 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200437 }
438
Jean Delvare6cad93c2012-07-24 14:13:58 +0200439 /* Clear status flags except BYTE_DONE, to be cleared by caller */
440 outb_p(status, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200441
442 return result;
443}
444
Jean Delvare6cad93c2012-07-24 14:13:58 +0200445/* Wait for BUSY being cleared and either INTR or an error flag being set */
446static int i801_wait_intr(struct i801_priv *priv)
447{
448 int timeout = 0;
449 int status;
450
451 /* We will always wait for a fraction of a second! */
452 do {
453 usleep_range(250, 500);
454 status = inb_p(SMBHSTSTS(priv));
455 } while (((status & SMBHSTSTS_HOST_BUSY) ||
456 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
457 (timeout++ < MAX_RETRIES));
458
459 if (timeout > MAX_RETRIES) {
460 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
461 return -ETIMEDOUT;
462 }
463 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
464}
465
466/* Wait for either BYTE_DONE or an error flag being set */
467static int i801_wait_byte_done(struct i801_priv *priv)
468{
469 int timeout = 0;
470 int status;
471
472 /* We will always wait for a fraction of a second! */
473 do {
474 usleep_range(250, 500);
475 status = inb_p(SMBHSTSTS(priv));
476 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
477 (timeout++ < MAX_RETRIES));
478
479 if (timeout > MAX_RETRIES) {
480 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
481 return -ETIMEDOUT;
482 }
483 return status & STATUS_ERROR_FLAGS;
484}
485
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100486static int i801_transaction(struct i801_priv *priv, int xact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Jean Delvare2b738092008-07-14 22:38:32 +0200488 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200489 int result;
Jean Delvareb3b8df92014-11-12 10:20:40 +0100490 const struct i2c_adapter *adap = &priv->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100492 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200493 if (result < 0)
494 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Daniel Kurtz636752b2012-07-24 14:13:58 +0200496 if (priv->features & FEATURE_IRQ) {
497 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
498 SMBHSTCNT(priv));
Jean Delvareb3b8df92014-11-12 10:20:40 +0100499 result = wait_event_timeout(priv->waitq,
500 (status = priv->status),
501 adap->timeout);
502 if (!result) {
503 status = -ETIMEDOUT;
504 dev_warn(&priv->pci_dev->dev,
505 "Timeout waiting for interrupt!\n");
506 }
Daniel Kurtz636752b2012-07-24 14:13:58 +0200507 priv->status = 0;
508 return i801_check_post(priv, status);
509 }
510
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200511 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
Daniel Kurtz37af8712012-07-24 14:13:58 +0200512 * SMBSCMD are passed in xact */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200513 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jean Delvare6cad93c2012-07-24 14:13:58 +0200515 status = i801_wait_intr(priv);
516 return i801_check_post(priv, status);
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200517}
518
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100519static int i801_block_transaction_by_block(struct i801_priv *priv,
520 union i2c_smbus_data *data,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200521 char read_write, int hwpec)
522{
523 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200524 int status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200525
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100526 inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200527
528 /* Use 32-byte buffer to process this transaction */
529 if (read_write == I2C_SMBUS_WRITE) {
530 len = data->block[0];
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100531 outb_p(len, SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200532 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100533 outb_p(data->block[i+1], SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200534 }
535
Daniel Kurtz37af8712012-07-24 14:13:58 +0200536 status = i801_transaction(priv, I801_BLOCK_DATA |
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200537 (hwpec ? SMBHSTCNT_PEC_EN : 0));
David Brownell97140342008-07-14 22:38:25 +0200538 if (status)
539 return status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200540
541 if (read_write == I2C_SMBUS_READ) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100542 len = inb_p(SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200543 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
David Brownell97140342008-07-14 22:38:25 +0200544 return -EPROTO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200545
546 data->block[0] = len;
547 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100548 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200549 }
550 return 0;
551}
552
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200553static void i801_isr_byte_done(struct i801_priv *priv)
554{
555 if (priv->is_read) {
556 /* For SMBus block reads, length is received with first byte */
557 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
558 (priv->count == 0)) {
559 priv->len = inb_p(SMBHSTDAT0(priv));
560 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
561 dev_err(&priv->pci_dev->dev,
562 "Illegal SMBus block read size %d\n",
563 priv->len);
564 /* FIXME: Recover */
565 priv->len = I2C_SMBUS_BLOCK_MAX;
566 } else {
567 dev_dbg(&priv->pci_dev->dev,
568 "SMBus block read size is %d\n",
569 priv->len);
570 }
571 priv->data[-1] = priv->len;
572 }
573
574 /* Read next byte */
575 if (priv->count < priv->len)
576 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
577 else
578 dev_dbg(&priv->pci_dev->dev,
579 "Discarding extra byte on block read\n");
580
581 /* Set LAST_BYTE for last byte of read transaction */
582 if (priv->count == priv->len - 1)
583 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
584 SMBHSTCNT(priv));
585 } else if (priv->count < priv->len - 1) {
586 /* Write next byte, except for IRQ after last byte */
587 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
588 }
589
590 /* Clear BYTE_DONE to continue with next byte */
591 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
592}
593
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200594static irqreturn_t i801_host_notify_isr(struct i801_priv *priv)
595{
596 unsigned short addr;
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200597
598 addr = inb_p(SMBNTFDADD(priv)) >> 1;
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200599
Benjamin Tissoiresc912a252016-10-13 14:10:39 +0200600 /*
601 * With the tested platforms, reading SMBNTFDDAT (22 + (p)->smba)
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200602 * always returns 0. Our current implementation doesn't provide
603 * data, so we just ignore it.
Benjamin Tissoiresc912a252016-10-13 14:10:39 +0200604 */
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200605 i2c_handle_smbus_host_notify(&priv->adapter, addr);
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200606
607 /* clear Host Notify bit and return */
608 outb_p(SMBSLVSTS_HST_NTFY_STS, SMBSLVSTS(priv));
609 return IRQ_HANDLED;
610}
611
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200612/*
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200613 * There are three kinds of interrupts:
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200614 *
615 * 1) i801 signals transaction completion with one of these interrupts:
616 * INTR - Success
617 * DEV_ERR - Invalid command, NAK or communication timeout
618 * BUS_ERR - SMI# transaction collision
619 * FAILED - transaction was canceled due to a KILL request
620 * When any of these occur, update ->status and wake up the waitq.
621 * ->status must be cleared before kicking off the next transaction.
622 *
623 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
624 * occurs for each byte of a byte-by-byte to prepare the next byte.
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200625 *
626 * 3) Host Notify interrupts
Daniel Kurtz636752b2012-07-24 14:13:58 +0200627 */
628static irqreturn_t i801_isr(int irq, void *dev_id)
629{
630 struct i801_priv *priv = dev_id;
631 u16 pcists;
632 u8 status;
633
634 /* Confirm this is our interrupt */
635 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
636 if (!(pcists & SMBPCISTS_INTS))
637 return IRQ_NONE;
638
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200639 if (priv->features & FEATURE_HOST_NOTIFY) {
640 status = inb_p(SMBSLVSTS(priv));
641 if (status & SMBSLVSTS_HST_NTFY_STS)
642 return i801_host_notify_isr(priv);
643 }
644
Daniel Kurtz636752b2012-07-24 14:13:58 +0200645 status = inb_p(SMBHSTSTS(priv));
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200646 if (status & SMBHSTSTS_BYTE_DONE)
647 i801_isr_byte_done(priv);
648
Daniel Kurtz636752b2012-07-24 14:13:58 +0200649 /*
650 * Clear irq sources and report transaction result.
651 * ->status must be cleared before the next transaction is started.
652 */
653 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
654 if (status) {
655 outb_p(status, SMBHSTSTS(priv));
Jean Delvarea90bc5d2016-05-25 09:37:02 +0200656 priv->status = status;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200657 wake_up(&priv->waitq);
658 }
659
660 return IRQ_HANDLED;
661}
662
663/*
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200664 * For "byte-by-byte" block transactions:
665 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
666 * I2C read uses cmd=I801_I2C_BLOCK_DATA
667 */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100668static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
669 union i2c_smbus_data *data,
Jean Delvare63420642008-01-27 18:14:50 +0100670 char read_write, int command,
671 int hwpec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int i, len;
674 int smbcmd;
Jean Delvare2b738092008-07-14 22:38:32 +0200675 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200676 int result;
Jean Delvareb3b8df92014-11-12 10:20:40 +0100677 const struct i2c_adapter *adap = &priv->adapter;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200678
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100679 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200680 if (result < 0)
681 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200683 len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100686 outb_p(len, SMBHSTDAT0(priv));
687 outb_p(data->block[1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200690 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
691 read_write == I2C_SMBUS_READ)
692 smbcmd = I801_I2C_BLOCK_DATA;
693 else
694 smbcmd = I801_BLOCK_DATA;
695
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200696 if (priv->features & FEATURE_IRQ) {
697 priv->is_read = (read_write == I2C_SMBUS_READ);
698 if (len == 1 && priv->is_read)
699 smbcmd |= SMBHSTCNT_LAST_BYTE;
700 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
701 priv->len = len;
702 priv->count = 0;
703 priv->data = &data->block[1];
704
705 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
Jean Delvareb3b8df92014-11-12 10:20:40 +0100706 result = wait_event_timeout(priv->waitq,
707 (status = priv->status),
708 adap->timeout);
709 if (!result) {
710 status = -ETIMEDOUT;
711 dev_warn(&priv->pci_dev->dev,
712 "Timeout waiting for interrupt!\n");
713 }
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200714 priv->status = 0;
715 return i801_check_post(priv, status);
716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 for (i = 1; i <= len; i++) {
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200719 if (i == len && read_write == I2C_SMBUS_READ)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200720 smbcmd |= SMBHSTCNT_LAST_BYTE;
Daniel Kurtz37af8712012-07-24 14:13:58 +0200721 outb_p(smbcmd, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (i == 1)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200724 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100725 SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jean Delvare6cad93c2012-07-24 14:13:58 +0200727 status = i801_wait_byte_done(priv);
728 if (status)
729 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jean Delvare63420642008-01-27 18:14:50 +0100731 if (i == 1 && read_write == I2C_SMBUS_READ
732 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100733 len = inb_p(SMBHSTDAT0(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200734 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100735 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200736 "Illegal SMBus block read size %d\n",
737 len);
738 /* Recover */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100739 while (inb_p(SMBHSTSTS(priv)) &
740 SMBHSTSTS_HOST_BUSY)
741 outb_p(SMBHSTSTS_BYTE_DONE,
742 SMBHSTSTS(priv));
743 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
David Brownell97140342008-07-14 22:38:25 +0200744 return -EPROTO;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 data->block[0] = len;
747 }
748
749 /* Retrieve/store value in SMBBLKDAT */
750 if (read_write == I2C_SMBUS_READ)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100751 data->block[i] = inb_p(SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100753 outb_p(data->block[i+1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jean Delvarecf898dc2008-07-14 22:38:33 +0200755 /* signals SMBBLKDAT ready */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200756 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200757 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200758
Jean Delvare6cad93c2012-07-24 14:13:58 +0200759 status = i801_wait_intr(priv);
760exit:
761 return i801_check_post(priv, status);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200762}
763
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100764static int i801_set_block_buffer_mode(struct i801_priv *priv)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200765{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100766 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
767 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
David Brownell97140342008-07-14 22:38:25 +0200768 return -EIO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200769 return 0;
770}
771
772/* Block transaction function */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100773static int i801_block_transaction(struct i801_priv *priv,
774 union i2c_smbus_data *data, char read_write,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200775 int command, int hwpec)
776{
777 int result = 0;
778 unsigned char hostc;
779
780 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
781 if (read_write == I2C_SMBUS_WRITE) {
782 /* set I2C_EN bit in configuration register */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100783 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
784 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200785 hostc | SMBHSTCFG_I2C_EN);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100786 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
787 dev_err(&priv->pci_dev->dev,
Jean Delvare63420642008-01-27 18:14:50 +0100788 "I2C block read is unsupported!\n");
David Brownell97140342008-07-14 22:38:25 +0200789 return -EOPNOTSUPP;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Jean Delvare63420642008-01-27 18:14:50 +0100793 if (read_write == I2C_SMBUS_WRITE
794 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200795 if (data->block[0] < 1)
796 data->block[0] = 1;
797 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
798 data->block[0] = I2C_SMBUS_BLOCK_MAX;
799 } else {
Jean Delvare63420642008-01-27 18:14:50 +0100800 data->block[0] = 32; /* max for SMBus block reads */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200801 }
802
Jean Delvarec074c392010-03-13 20:56:53 +0100803 /* Experience has shown that the block buffer can only be used for
804 SMBus (not I2C) block transactions, even though the datasheet
805 doesn't mention this limitation. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100806 if ((priv->features & FEATURE_BLOCK_BUFFER)
Jean Delvarec074c392010-03-13 20:56:53 +0100807 && command != I2C_SMBUS_I2C_BLOCK_DATA
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100808 && i801_set_block_buffer_mode(priv) == 0)
809 result = i801_block_transaction_by_block(priv, data,
810 read_write, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200811 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100812 result = i801_block_transaction_byte_by_byte(priv, data,
813 read_write,
Jean Delvare63420642008-01-27 18:14:50 +0100814 command, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200815
Jean Delvare63420642008-01-27 18:14:50 +0100816 if (command == I2C_SMBUS_I2C_BLOCK_DATA
817 && read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* restore saved configuration register value */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100819 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 return result;
822}
823
David Brownell97140342008-07-14 22:38:25 +0200824/* Return negative errno on error. */
Ivo Manca3fb21c62010-05-21 18:40:55 +0200825static s32 i801_access(struct i2c_adapter *adap, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 unsigned short flags, char read_write, u8 command,
Ivo Manca3fb21c62010-05-21 18:40:55 +0200827 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200829 int hwpec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int block = 0;
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200831 int ret = 0, xact = 0;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100832 struct i801_priv *priv = i2c_get_adapdata(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Mika Westerberga7ae8192016-06-09 16:56:28 +0300834 mutex_lock(&priv->acpi_lock);
835 if (priv->acpi_reserved) {
836 mutex_unlock(&priv->acpi_lock);
837 return -EBUSY;
838 }
839
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200840 pm_runtime_get_sync(&priv->pci_dev->dev);
841
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100842 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200843 && size != I2C_SMBUS_QUICK
844 && size != I2C_SMBUS_I2C_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 switch (size) {
847 case I2C_SMBUS_QUICK:
848 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100849 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 xact = I801_QUICK;
851 break;
852 case I2C_SMBUS_BYTE:
853 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100854 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100856 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 xact = I801_BYTE;
858 break;
859 case I2C_SMBUS_BYTE_DATA:
860 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100861 SMBHSTADD(priv));
862 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100864 outb_p(data->byte, SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xact = I801_BYTE_DATA;
866 break;
867 case I2C_SMBUS_WORD_DATA:
868 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100869 SMBHSTADD(priv));
870 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100872 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
873 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 xact = I801_WORD_DATA;
876 break;
877 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100879 SMBHSTADD(priv));
880 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 block = 1;
882 break;
Jean Delvare63420642008-01-27 18:14:50 +0100883 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvareba9ad2a2016-10-11 13:13:27 +0200884 /*
885 * NB: page 240 of ICH5 datasheet shows that the R/#W
886 * bit should be cleared here, even when reading.
887 * However if SPD Write Disable is set (Lynx Point and later),
888 * the read will fail if we don't set the R/#W bit.
889 */
890 outb_p(((addr & 0x7f) << 1) |
891 ((priv->original_hstcfg & SMBHSTCFG_SPD_WD) ?
892 (read_write & 0x01) : 0),
893 SMBHSTADD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100894 if (read_write == I2C_SMBUS_READ) {
895 /* NB: page 240 of ICH5 datasheet also shows
896 * that DATA1 is the cmd field when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100897 outb_p(command, SMBHSTDAT1(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100898 } else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100899 outb_p(command, SMBHSTCMD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100900 block = 1;
901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100903 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
904 size);
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200905 ret = -EOPNOTSUPP;
906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200909 if (hwpec) /* enable/disable hardware PEC */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100910 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200911 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100912 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
913 SMBAUXCTL(priv));
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200914
Ivo Manca3fb21c62010-05-21 18:40:55 +0200915 if (block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100916 ret = i801_block_transaction(priv, data, read_write, size,
917 hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200918 else
Daniel Kurtz37af8712012-07-24 14:13:58 +0200919 ret = i801_transaction(priv, xact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Jean Delvarec79cfba2006-04-20 02:43:18 -0700921 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200922 time, so we forcibly disable it after every transaction. Turn off
923 E32B for the same reason. */
Jean Delvarea0921b62008-01-27 18:14:50 +0100924 if (hwpec || block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100925 outb_p(inb_p(SMBAUXCTL(priv)) &
926 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarec79cfba2006-04-20 02:43:18 -0700927
Ivo Manca3fb21c62010-05-21 18:40:55 +0200928 if (block)
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200929 goto out;
Ivo Manca3fb21c62010-05-21 18:40:55 +0200930 if (ret)
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 switch (xact & 0x7f) {
936 case I801_BYTE: /* Result put in SMBHSTDAT0 */
937 case I801_BYTE_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100938 data->byte = inb_p(SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940 case I801_WORD_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100941 data->word = inb_p(SMBHSTDAT0(priv)) +
942 (inb_p(SMBHSTDAT1(priv)) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 }
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200945
946out:
947 pm_runtime_mark_last_busy(&priv->pci_dev->dev);
948 pm_runtime_put_autosuspend(&priv->pci_dev->dev);
Mika Westerberga7ae8192016-06-09 16:56:28 +0300949 mutex_unlock(&priv->acpi_lock);
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +0200950 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953
954static u32 i801_func(struct i2c_adapter *adapter)
955{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100956 struct i801_priv *priv = i2c_get_adapdata(adapter);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare369f6f42008-01-27 18:14:50 +0100959 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
960 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100961 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
962 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200963 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0) |
964 ((priv->features & FEATURE_HOST_NOTIFY) ?
965 I2C_FUNC_SMBUS_HOST_NOTIFY : 0);
966}
967
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200968static void i801_enable_host_notify(struct i2c_adapter *adapter)
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200969{
970 struct i801_priv *priv = i2c_get_adapdata(adapter);
971
972 if (!(priv->features & FEATURE_HOST_NOTIFY))
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200973 return;
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200974
Benjamin Tissoires22e94bd2016-10-13 14:10:35 +0200975 if (!(SMBSLVCMD_HST_NTFY_INTREN & priv->original_slvcmd))
976 outb_p(SMBSLVCMD_HST_NTFY_INTREN | priv->original_slvcmd,
977 SMBSLVCMD(priv));
978
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +0200979 /* clear Host Notify bit to allow a new notification */
980 outb_p(SMBSLVSTS_HST_NTFY_STS, SMBSLVSTS(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Benjamin Tissoires22e94bd2016-10-13 14:10:35 +0200983static void i801_disable_host_notify(struct i801_priv *priv)
984{
985 if (!(priv->features & FEATURE_HOST_NOTIFY))
986 return;
987
988 outb_p(priv->original_slvcmd, SMBSLVCMD(priv));
989}
990
Jean Delvare8f9082c2006-09-03 22:39:46 +0200991static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 .smbus_xfer = i801_access,
993 .functionality = i801_func,
994};
995
Jingoo Han392debf2013-12-03 08:11:20 +0900996static const struct pci_device_id i801_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
998 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
999 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
1000 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
1001 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
1002 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
1003 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
1004 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
1005 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
Jason Gastonb0a70b52005-04-16 15:24:45 -07001006 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
Jason Gaston8254fc42006-01-09 10:58:08 -08001007 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
Jason Gastonadbc2a12006-11-22 15:19:12 -08001008 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
Seth Heasleycb04e952010-10-04 13:27:14 -07001009 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
Gaston, Jason Dd28dc712008-02-24 20:03:42 +01001010 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
1011 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
Seth Heasleycb04e952010-10-04 13:27:14 -07001012 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
1013 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
Seth Heasleye30d9852010-10-31 21:06:59 +01001014 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
David Woodhouse55fee8d2010-10-31 21:07:00 +01001015 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
1016 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
1017 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
Seth Heasley662cda82011-03-20 14:50:53 +01001018 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
Seth Heasley6e2a8512011-05-24 20:58:49 +02001019 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
Seth Heasley062737f2012-03-26 21:47:19 +02001020 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
James Ralston4a8f1dd2012-09-10 10:14:02 +02001021 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
Seth Heasleyc2db409c2013-01-30 15:25:32 +00001022 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
James Ralstona3fc0ff2013-02-14 09:15:33 +00001023 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
1024 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
1025 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
1026 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
Seth Heasleyf39901c2013-06-19 16:59:57 -07001027 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
Mika Westerberg9827f9e2017-02-01 19:20:59 +03001028 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS) },
Jean Delvareb299de82014-07-17 15:04:41 +02001029 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
James Ralstonafc65922013-11-04 09:29:48 -08001030 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
Chew, Kean ho1b31e9b2014-03-01 00:03:56 +08001031 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
Alan Cox39e8e302014-08-19 17:37:28 +03001032 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -07001033 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS) },
Devin Ryles3eee17992014-11-05 16:30:03 -05001034 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS) },
Jarkko Nikulacb09d942017-09-21 16:23:16 +03001035 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CDF_SMBUS) },
Mika Westerberg84d7f2e2015-10-13 15:41:39 +03001036 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMBUS) },
Jarkko Nikuladd77f422015-10-22 17:16:58 +03001037 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BROXTON_SMBUS) },
Alexandra Yatescdc5a312015-11-05 11:40:25 -08001038 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS) },
1039 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS) },
Andy Shevchenko31158762016-09-23 11:56:01 +03001040 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS) },
Srinivas Pandruvada09a1de02017-05-18 11:23:06 +03001041 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS) },
1042 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS) },
Mika Westerberg0bff2a82018-06-28 16:08:24 +03001043 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS) },
Jarkko Nikula5cd1c562019-03-15 12:56:49 +02001044 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COMETLAKE_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 { 0, }
1046};
1047
Ivo Manca3fb21c62010-05-21 18:40:55 +02001048MODULE_DEVICE_TABLE(pci, i801_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Jean Delvare8eacfce2011-05-24 20:58:49 +02001050#if defined CONFIG_X86 && defined CONFIG_DMI
Jean Delvare1561bfe2009-01-07 14:29:17 +01001051static unsigned char apanel_addr;
1052
1053/* Scan the system ROM for the signature "FJKEYINF" */
1054static __init const void __iomem *bios_signature(const void __iomem *bios)
1055{
1056 ssize_t offset;
1057 const unsigned char signature[] = "FJKEYINF";
1058
1059 for (offset = 0; offset < 0x10000; offset += 0x10) {
1060 if (check_signature(bios + offset, signature,
1061 sizeof(signature)-1))
1062 return bios + offset;
1063 }
1064 return NULL;
1065}
1066
1067static void __init input_apanel_init(void)
1068{
1069 void __iomem *bios;
1070 const void __iomem *p;
1071
1072 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
1073 p = bios_signature(bios);
1074 if (p) {
1075 /* just use the first address */
1076 apanel_addr = readb(p + 8 + 3) >> 1;
1077 }
1078 iounmap(bios);
1079}
Jean Delvare1561bfe2009-01-07 14:29:17 +01001080
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001081struct dmi_onboard_device_info {
1082 const char *name;
1083 u8 type;
1084 unsigned short i2c_addr;
1085 const char *i2c_type;
1086};
1087
Bill Pemberton0b255e92012-11-27 15:59:38 -05001088static const struct dmi_onboard_device_info dmi_devices[] = {
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001089 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
1090 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
1091 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
1092};
1093
Bill Pemberton0b255e92012-11-27 15:59:38 -05001094static void dmi_check_onboard_device(u8 type, const char *name,
1095 struct i2c_adapter *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001096{
1097 int i;
1098 struct i2c_board_info info;
1099
1100 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
1101 /* & ~0x80, ignore enabled/disabled bit */
1102 if ((type & ~0x80) != dmi_devices[i].type)
1103 continue;
Jean Delvarefaabd472010-07-09 16:22:51 +02001104 if (strcasecmp(name, dmi_devices[i].name))
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001105 continue;
1106
1107 memset(&info, 0, sizeof(struct i2c_board_info));
1108 info.addr = dmi_devices[i].i2c_addr;
1109 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
1110 i2c_new_device(adap, &info);
1111 break;
1112 }
1113}
1114
1115/* We use our own function to check for onboard devices instead of
1116 dmi_find_device() as some buggy BIOS's have the devices we are interested
1117 in marked as disabled */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001118static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001119{
1120 int i, count;
1121
1122 if (dm->type != 10)
1123 return;
1124
1125 count = (dm->length - sizeof(struct dmi_header)) / 2;
1126 for (i = 0; i < count; i++) {
1127 const u8 *d = (char *)(dm + 1) + (i * 2);
1128 const char *name = ((char *) dm) + dm->length;
1129 u8 type = d[0];
1130 u8 s = d[1];
1131
1132 if (!s)
1133 continue;
1134 s--;
1135 while (s > 0 && name[0]) {
1136 name += strlen(name) + 1;
1137 s--;
1138 }
1139 if (name[0] == 0) /* Bogus string reference */
1140 continue;
1141
1142 dmi_check_onboard_device(type, name, adap);
1143 }
1144}
Hans de Goedefa5bfab2009-03-30 21:46:44 +02001145
Pali Rohár19b07cb2019-06-06 20:18:45 +02001146/* NOTE: Keep this list in sync with drivers/platform/x86/dell-smo8800.c */
1147static const char *const acpi_smo8800_ids[] = {
1148 "SMO8800",
1149 "SMO8801",
1150 "SMO8810",
1151 "SMO8811",
1152 "SMO8820",
1153 "SMO8821",
1154 "SMO8830",
1155 "SMO8831",
1156};
1157
1158static acpi_status check_acpi_smo88xx_device(acpi_handle obj_handle,
1159 u32 nesting_level,
1160 void *context,
1161 void **return_value)
1162{
1163 struct acpi_device_info *info;
1164 acpi_status status;
1165 char *hid;
1166 int i;
1167
1168 status = acpi_get_object_info(obj_handle, &info);
1169 if (!ACPI_SUCCESS(status) || !(info->valid & ACPI_VALID_HID))
1170 return AE_OK;
1171
1172 hid = info->hardware_id.string;
1173 if (!hid)
1174 return AE_OK;
1175
Andy Shevchenkoaf668d62019-06-21 14:36:24 +03001176 i = match_string(acpi_smo8800_ids, ARRAY_SIZE(acpi_smo8800_ids), hid);
1177 if (i < 0)
1178 return AE_OK;
Pali Rohár19b07cb2019-06-06 20:18:45 +02001179
Andy Shevchenkoaf668d62019-06-21 14:36:24 +03001180 *((bool *)return_value) = true;
1181 return AE_CTRL_TERMINATE;
Pali Rohár19b07cb2019-06-06 20:18:45 +02001182}
1183
1184static bool is_dell_system_with_lis3lv02d(void)
1185{
1186 bool found;
1187 const char *vendor;
1188
1189 vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Andy Shevchenkoaa5ae062019-06-13 19:45:27 +03001190 if (!vendor || strcmp(vendor, "Dell Inc."))
Pali Rohár19b07cb2019-06-06 20:18:45 +02001191 return false;
1192
1193 /*
1194 * Check that ACPI device SMO88xx is present and is functioning.
1195 * Function acpi_get_devices() already filters all ACPI devices
1196 * which are not present or are not functioning.
1197 * ACPI device SMO88xx represents our ST microelectronics lis3lv02d
1198 * accelerometer but unfortunately ACPI does not provide any other
1199 * information (like I2C address).
1200 */
1201 found = false;
1202 acpi_get_devices(NULL, check_acpi_smo88xx_device, NULL,
1203 (void **)&found);
1204
1205 return found;
1206}
1207
1208/*
1209 * Accelerometer's I2C address is not specified in DMI nor ACPI,
1210 * so it is needed to define mapping table based on DMI product names.
1211 */
1212static const struct {
1213 const char *dmi_product_name;
1214 unsigned short i2c_addr;
1215} dell_lis3lv02d_devices[] = {
1216 /*
1217 * Dell platform team told us that these Latitude devices have
1218 * ST microelectronics accelerometer at I2C address 0x29.
1219 */
1220 { "Latitude E5250", 0x29 },
1221 { "Latitude E5450", 0x29 },
1222 { "Latitude E5550", 0x29 },
1223 { "Latitude E6440", 0x29 },
1224 { "Latitude E6440 ATG", 0x29 },
1225 { "Latitude E6540", 0x29 },
1226 /*
1227 * Additional individual entries were added after verification.
1228 */
1229 { "Vostro V131", 0x1d },
1230};
1231
1232static void register_dell_lis3lv02d_i2c_device(struct i801_priv *priv)
1233{
1234 struct i2c_board_info info;
1235 const char *dmi_product_name;
1236 int i;
1237
1238 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
1239 for (i = 0; i < ARRAY_SIZE(dell_lis3lv02d_devices); ++i) {
1240 if (strcmp(dmi_product_name,
1241 dell_lis3lv02d_devices[i].dmi_product_name) == 0)
1242 break;
1243 }
1244
1245 if (i == ARRAY_SIZE(dell_lis3lv02d_devices)) {
1246 dev_warn(&priv->pci_dev->dev,
1247 "Accelerometer lis3lv02d is present on SMBus but its"
1248 " address is unknown, skipping registration\n");
1249 return;
1250 }
1251
1252 memset(&info, 0, sizeof(struct i2c_board_info));
1253 info.addr = dell_lis3lv02d_devices[i].i2c_addr;
1254 strlcpy(info.type, "lis3lv02d", I2C_NAME_SIZE);
1255 i2c_new_device(&priv->adapter, &info);
1256}
1257
Jean Delvaree7198fb2011-05-24 20:58:49 +02001258/* Register optional slaves */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001259static void i801_probe_optional_slaves(struct i801_priv *priv)
Jean Delvaree7198fb2011-05-24 20:58:49 +02001260{
1261 /* Only register slaves on main SMBus channel */
1262 if (priv->features & FEATURE_IDF)
1263 return;
1264
Jean Delvaree7198fb2011-05-24 20:58:49 +02001265 if (apanel_addr) {
1266 struct i2c_board_info info;
1267
1268 memset(&info, 0, sizeof(struct i2c_board_info));
1269 info.addr = apanel_addr;
1270 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
1271 i2c_new_device(&priv->adapter, &info);
1272 }
Jean Delvare8eacfce2011-05-24 20:58:49 +02001273
Jean Delvaree7198fb2011-05-24 20:58:49 +02001274 if (dmi_name_in_vendors("FUJITSU"))
1275 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
Pali Rohár19b07cb2019-06-06 20:18:45 +02001276
1277 if (is_dell_system_with_lis3lv02d())
1278 register_dell_lis3lv02d_i2c_device(priv);
Jean Delvaree7198fb2011-05-24 20:58:49 +02001279}
Jean Delvare8eacfce2011-05-24 20:58:49 +02001280#else
1281static void __init input_apanel_init(void) {}
Bill Pemberton0b255e92012-11-27 15:59:38 -05001282static void i801_probe_optional_slaves(struct i801_priv *priv) {}
Jean Delvare8eacfce2011-05-24 20:58:49 +02001283#endif /* CONFIG_X86 && CONFIG_DMI */
Jean Delvaree7198fb2011-05-24 20:58:49 +02001284
Javier Martinez Canillas175c7082016-07-21 12:11:01 -04001285#if IS_ENABLED(CONFIG_I2C_MUX_GPIO) && defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001286static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
1287 .gpio_chip = "gpio_ich",
1288 .values = { 0x02, 0x03 },
1289 .n_values = 2,
1290 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
1291 .gpios = { 52, 53 },
1292 .n_gpios = 2,
1293};
1294
1295static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
1296 .gpio_chip = "gpio_ich",
1297 .values = { 0x02, 0x03, 0x01 },
1298 .n_values = 3,
1299 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
1300 .gpios = { 52, 53 },
1301 .n_gpios = 2,
1302};
1303
Bill Pemberton0b255e92012-11-27 15:59:38 -05001304static const struct dmi_system_id mux_dmi_table[] = {
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001305 {
1306 .matches = {
1307 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1308 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
1309 },
1310 .driver_data = &i801_mux_config_asus_z8_d12,
1311 },
1312 {
1313 .matches = {
1314 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1315 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
1316 },
1317 .driver_data = &i801_mux_config_asus_z8_d12,
1318 },
1319 {
1320 .matches = {
1321 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1322 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
1323 },
1324 .driver_data = &i801_mux_config_asus_z8_d12,
1325 },
1326 {
1327 .matches = {
1328 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1329 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
1330 },
1331 .driver_data = &i801_mux_config_asus_z8_d12,
1332 },
1333 {
1334 .matches = {
1335 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1336 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
1337 },
1338 .driver_data = &i801_mux_config_asus_z8_d12,
1339 },
1340 {
1341 .matches = {
1342 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1343 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
1344 },
1345 .driver_data = &i801_mux_config_asus_z8_d12,
1346 },
1347 {
1348 .matches = {
1349 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1350 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
1351 },
1352 .driver_data = &i801_mux_config_asus_z8_d18,
1353 },
1354 {
1355 .matches = {
1356 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1357 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1358 },
1359 .driver_data = &i801_mux_config_asus_z8_d18,
1360 },
1361 {
1362 .matches = {
1363 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1364 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1365 },
1366 .driver_data = &i801_mux_config_asus_z8_d12,
1367 },
1368 { }
1369};
1370
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001371/* Setup multiplexing if needed */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001372static int i801_add_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001373{
1374 struct device *dev = &priv->adapter.dev;
1375 const struct i801_mux_config *mux_config;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001376 struct i2c_mux_gpio_platform_data gpio_data;
Jean Delvaref82b8622012-10-05 22:23:54 +02001377 int err;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001378
1379 if (!priv->mux_drvdata)
1380 return 0;
1381 mux_config = priv->mux_drvdata;
1382
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001383 /* Prepare the platform data */
1384 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1385 gpio_data.parent = priv->adapter.nr;
1386 gpio_data.values = mux_config->values;
1387 gpio_data.n_values = mux_config->n_values;
1388 gpio_data.classes = mux_config->classes;
Jean Delvaref82b8622012-10-05 22:23:54 +02001389 gpio_data.gpio_chip = mux_config->gpio_chip;
1390 gpio_data.gpios = mux_config->gpios;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001391 gpio_data.n_gpios = mux_config->n_gpios;
1392 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1393
1394 /* Register the mux device */
1395 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
Jean Delvaref82b8622012-10-05 22:23:54 +02001396 PLATFORM_DEVID_AUTO, &gpio_data,
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001397 sizeof(struct i2c_mux_gpio_platform_data));
1398 if (IS_ERR(priv->mux_pdev)) {
1399 err = PTR_ERR(priv->mux_pdev);
1400 priv->mux_pdev = NULL;
1401 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1402 return err;
1403 }
1404
1405 return 0;
1406}
1407
Bill Pemberton0b255e92012-11-27 15:59:38 -05001408static void i801_del_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001409{
1410 if (priv->mux_pdev)
1411 platform_device_unregister(priv->mux_pdev);
1412}
1413
Bill Pemberton0b255e92012-11-27 15:59:38 -05001414static unsigned int i801_get_adapter_class(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001415{
1416 const struct dmi_system_id *id;
1417 const struct i801_mux_config *mux_config;
1418 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1419 int i;
1420
1421 id = dmi_first_match(mux_dmi_table);
1422 if (id) {
Jean Delvare28901f52012-10-28 21:37:01 +01001423 /* Remove branch classes from trunk */
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001424 mux_config = id->driver_data;
1425 for (i = 0; i < mux_config->n_values; i++)
1426 class &= ~mux_config->classes[i];
1427
1428 /* Remember for later */
1429 priv->mux_drvdata = mux_config;
1430 }
1431
1432 return class;
1433}
1434#else
1435static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1436static inline void i801_del_mux(struct i801_priv *priv) { }
1437
1438static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1439{
1440 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1441}
1442#endif
1443
Mika Westerberg94246932015-08-06 13:46:25 +01001444static const struct itco_wdt_platform_data tco_platform_data = {
1445 .name = "Intel PCH",
1446 .version = 4,
1447};
1448
1449static DEFINE_SPINLOCK(p2sb_spinlock);
1450
1451static void i801_add_tco(struct i801_priv *priv)
1452{
1453 struct pci_dev *pci_dev = priv->pci_dev;
1454 struct resource tco_res[3], *res;
1455 struct platform_device *pdev;
1456 unsigned int devfn;
1457 u32 tco_base, tco_ctl;
1458 u32 base_addr, ctrl_val;
1459 u64 base64_addr;
Qiuxu Zhuobfd44732017-08-15 00:04:50 +08001460 u8 hidden;
Mika Westerberg94246932015-08-06 13:46:25 +01001461
1462 if (!(priv->features & FEATURE_TCO))
1463 return;
1464
1465 pci_read_config_dword(pci_dev, TCOBASE, &tco_base);
1466 pci_read_config_dword(pci_dev, TCOCTL, &tco_ctl);
1467 if (!(tco_ctl & TCOCTL_EN))
1468 return;
1469
1470 memset(tco_res, 0, sizeof(tco_res));
1471
1472 res = &tco_res[ICH_RES_IO_TCO];
1473 res->start = tco_base & ~1;
1474 res->end = res->start + 32 - 1;
1475 res->flags = IORESOURCE_IO;
1476
1477 /*
1478 * Power Management registers.
1479 */
1480 devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 2);
1481 pci_bus_read_config_dword(pci_dev->bus, devfn, ACPIBASE, &base_addr);
1482
1483 res = &tco_res[ICH_RES_IO_SMI];
1484 res->start = (base_addr & ~1) + ACPIBASE_SMI_OFF;
1485 res->end = res->start + 3;
1486 res->flags = IORESOURCE_IO;
1487
1488 /*
1489 * Enable the ACPI I/O space.
1490 */
1491 pci_bus_read_config_dword(pci_dev->bus, devfn, ACPICTRL, &ctrl_val);
1492 ctrl_val |= ACPICTRL_EN;
1493 pci_bus_write_config_dword(pci_dev->bus, devfn, ACPICTRL, ctrl_val);
1494
1495 /*
1496 * We must access the NO_REBOOT bit over the Primary to Sideband
1497 * bridge (P2SB). The BIOS prevents the P2SB device from being
1498 * enumerated by the PCI subsystem, so we need to unhide/hide it
1499 * to lookup the P2SB BAR.
1500 */
1501 spin_lock(&p2sb_spinlock);
1502
1503 devfn = PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 1);
1504
Qiuxu Zhuobfd44732017-08-15 00:04:50 +08001505 /* Unhide the P2SB device, if it is hidden */
1506 pci_bus_read_config_byte(pci_dev->bus, devfn, 0xe1, &hidden);
1507 if (hidden)
1508 pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, 0x0);
Mika Westerberg94246932015-08-06 13:46:25 +01001509
1510 pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR, &base_addr);
1511 base64_addr = base_addr & 0xfffffff0;
1512
1513 pci_bus_read_config_dword(pci_dev->bus, devfn, SBREG_BAR + 0x4, &base_addr);
1514 base64_addr |= (u64)base_addr << 32;
1515
Qiuxu Zhuobfd44732017-08-15 00:04:50 +08001516 /* Hide the P2SB device, if it was hidden before */
1517 if (hidden)
1518 pci_bus_write_config_byte(pci_dev->bus, devfn, 0xe1, hidden);
Mika Westerberg94246932015-08-06 13:46:25 +01001519 spin_unlock(&p2sb_spinlock);
1520
1521 res = &tco_res[ICH_RES_MEM_OFF];
Felipe Balbi851a1512018-09-03 11:24:57 +03001522 if (pci_dev->device == PCI_DEVICE_ID_INTEL_DNV_SMBUS)
1523 res->start = (resource_size_t)base64_addr + SBREG_SMBCTRL_DNV;
1524 else
1525 res->start = (resource_size_t)base64_addr + SBREG_SMBCTRL;
1526
Mika Westerberg94246932015-08-06 13:46:25 +01001527 res->end = res->start + 3;
1528 res->flags = IORESOURCE_MEM;
1529
1530 pdev = platform_device_register_resndata(&pci_dev->dev, "iTCO_wdt", -1,
1531 tco_res, 3, &tco_platform_data,
1532 sizeof(tco_platform_data));
1533 if (IS_ERR(pdev)) {
1534 dev_warn(&pci_dev->dev, "failed to create iTCO device\n");
1535 return;
1536 }
1537
1538 priv->tco_pdev = pdev;
1539}
1540
Mika Westerberga7ae8192016-06-09 16:56:28 +03001541#ifdef CONFIG_ACPI
Mika Westerberg7fd6d982018-08-30 11:50:13 +03001542static bool i801_acpi_is_smbus_ioport(const struct i801_priv *priv,
1543 acpi_physical_address address)
1544{
1545 return address >= priv->smba &&
1546 address <= pci_resource_end(priv->pci_dev, SMBBAR);
1547}
1548
Mika Westerberga7ae8192016-06-09 16:56:28 +03001549static acpi_status
1550i801_acpi_io_handler(u32 function, acpi_physical_address address, u32 bits,
1551 u64 *value, void *handler_context, void *region_context)
1552{
1553 struct i801_priv *priv = handler_context;
1554 struct pci_dev *pdev = priv->pci_dev;
1555 acpi_status status;
1556
1557 /*
1558 * Once BIOS AML code touches the OpRegion we warn and inhibit any
1559 * further access from the driver itself. This device is now owned
1560 * by the system firmware.
1561 */
1562 mutex_lock(&priv->acpi_lock);
1563
Mika Westerberg7fd6d982018-08-30 11:50:13 +03001564 if (!priv->acpi_reserved && i801_acpi_is_smbus_ioport(priv, address)) {
Mika Westerberga7ae8192016-06-09 16:56:28 +03001565 priv->acpi_reserved = true;
1566
1567 dev_warn(&pdev->dev, "BIOS is accessing SMBus registers\n");
1568 dev_warn(&pdev->dev, "Driver SMBus register access inhibited\n");
1569
1570 /*
1571 * BIOS is accessing the host controller so prevent it from
1572 * suspending automatically from now on.
1573 */
1574 pm_runtime_get_sync(&pdev->dev);
1575 }
1576
1577 if ((function & ACPI_IO_MASK) == ACPI_READ)
1578 status = acpi_os_read_port(address, (u32 *)value, bits);
1579 else
1580 status = acpi_os_write_port(address, (u32)*value, bits);
1581
1582 mutex_unlock(&priv->acpi_lock);
1583
1584 return status;
1585}
1586
1587static int i801_acpi_probe(struct i801_priv *priv)
1588{
1589 struct acpi_device *adev;
1590 acpi_status status;
1591
1592 adev = ACPI_COMPANION(&priv->pci_dev->dev);
1593 if (adev) {
1594 status = acpi_install_address_space_handler(adev->handle,
1595 ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler,
1596 NULL, priv);
1597 if (ACPI_SUCCESS(status))
1598 return 0;
1599 }
1600
1601 return acpi_check_resource_conflict(&priv->pci_dev->resource[SMBBAR]);
1602}
1603
1604static void i801_acpi_remove(struct i801_priv *priv)
1605{
1606 struct acpi_device *adev;
1607
1608 adev = ACPI_COMPANION(&priv->pci_dev->dev);
1609 if (!adev)
1610 return;
1611
1612 acpi_remove_address_space_handler(adev->handle,
1613 ACPI_ADR_SPACE_SYSTEM_IO, i801_acpi_io_handler);
1614
1615 mutex_lock(&priv->acpi_lock);
1616 if (priv->acpi_reserved)
1617 pm_runtime_put(&priv->pci_dev->dev);
1618 mutex_unlock(&priv->acpi_lock);
1619}
1620#else
1621static inline int i801_acpi_probe(struct i801_priv *priv) { return 0; }
1622static inline void i801_acpi_remove(struct i801_priv *priv) { }
1623#endif
1624
Bill Pemberton0b255e92012-11-27 15:59:38 -05001625static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001627 unsigned char temp;
Jean Delvareadff6872010-05-21 18:40:54 +02001628 int err, i;
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001629 struct i801_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Jarkko Nikula1621c592015-02-13 15:52:23 +02001631 priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001632 if (!priv)
1633 return -ENOMEM;
1634
1635 i2c_set_adapdata(&priv->adapter, priv);
1636 priv->adapter.owner = THIS_MODULE;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001637 priv->adapter.class = i801_get_adapter_class(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001638 priv->adapter.algo = &smbus_algorithm;
Dustin Byford8eb5c872015-10-23 12:27:07 -07001639 priv->adapter.dev.parent = &dev->dev;
1640 ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&dev->dev));
1641 priv->adapter.retries = 3;
Mika Westerberga7ae8192016-06-09 16:56:28 +03001642 mutex_init(&priv->acpi_lock);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001643
1644 priv->pci_dev = dev;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001645 switch (dev->device) {
Mika Westerberg94246932015-08-06 13:46:25 +01001646 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS:
1647 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS:
Srinivas Pandruvada09a1de02017-05-18 11:23:06 +03001648 case PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS:
1649 case PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS:
Alexandra Yates1a1503c2016-02-17 18:21:21 -08001650 case PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS:
1651 case PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS:
Jarkko Nikulacb09d942017-09-21 16:23:16 +03001652 case PCI_DEVICE_ID_INTEL_CDF_SMBUS:
Mika Westerberg84d7f2e2015-10-13 15:41:39 +03001653 case PCI_DEVICE_ID_INTEL_DNV_SMBUS:
Andy Shevchenko31158762016-09-23 11:56:01 +03001654 case PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS:
Mika Westerberg0bff2a82018-06-28 16:08:24 +03001655 case PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS:
Jarkko Nikula5cd1c562019-03-15 12:56:49 +02001656 case PCI_DEVICE_ID_INTEL_COMETLAKE_SMBUS:
Mika Westerberg94246932015-08-06 13:46:25 +01001657 priv->features |= FEATURE_I2C_BLOCK_READ;
1658 priv->features |= FEATURE_IRQ;
1659 priv->features |= FEATURE_SMBUS_PEC;
1660 priv->features |= FEATURE_BLOCK_BUFFER;
Mika Westerberg1f6dbb02016-09-20 15:30:53 +03001661 /* If we have ACPI based watchdog use that instead */
1662 if (!acpi_has_watchdog())
1663 priv->features |= FEATURE_TCO;
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +02001664 priv->features |= FEATURE_HOST_NOTIFY;
Mika Westerberg94246932015-08-06 13:46:25 +01001665 break;
1666
Jean Delvaree7198fb2011-05-24 20:58:49 +02001667 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1668 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1669 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
James Ralstona3fc0ff2013-02-14 09:15:33 +00001670 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
1671 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
1672 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
Jean Delvaree7198fb2011-05-24 20:58:49 +02001673 priv->features |= FEATURE_IDF;
1674 /* fall through */
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001675 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001676 priv->features |= FEATURE_I2C_BLOCK_READ;
Jean Delvare6676a842012-12-16 21:11:55 +01001677 priv->features |= FEATURE_IRQ;
Jean Delvare63420642008-01-27 18:14:50 +01001678 /* fall through */
1679 case PCI_DEVICE_ID_INTEL_82801DB_3:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001680 priv->features |= FEATURE_SMBUS_PEC;
1681 priv->features |= FEATURE_BLOCK_BUFFER;
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001682 /* fall through */
1683 case PCI_DEVICE_ID_INTEL_82801CA_3:
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +02001684 priv->features |= FEATURE_HOST_NOTIFY;
1685 /* fall through */
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001686 case PCI_DEVICE_ID_INTEL_82801BA_2:
1687 case PCI_DEVICE_ID_INTEL_82801AB_3:
1688 case PCI_DEVICE_ID_INTEL_82801AA_3:
Jean Delvare250d1bd2006-12-10 21:21:33 +01001689 break;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001690 }
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001691
Jean Delvareadff6872010-05-21 18:40:54 +02001692 /* Disable features on user request */
1693 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001694 if (priv->features & disable_features & (1 << i))
Jean Delvareadff6872010-05-21 18:40:54 +02001695 dev_notice(&dev->dev, "%s disabled by user\n",
1696 i801_feature_names[i]);
1697 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001698 priv->features &= ~disable_features;
Jean Delvareadff6872010-05-21 18:40:54 +02001699
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001700 err = pcim_enable_device(dev);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001701 if (err) {
1702 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1703 err);
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001704 return err;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001705 }
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001706 pcim_pin_device(dev);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001707
1708 /* Determine the address of the SMBus area */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001709 priv->smba = pci_resource_start(dev, SMBBAR);
1710 if (!priv->smba) {
Jarkko Nikula9cbbf3d2015-02-13 15:52:21 +02001711 dev_err(&dev->dev,
1712 "SMBus base address uninitialized, upgrade BIOS\n");
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001713 return -ENODEV;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001714 }
1715
Mika Westerberga7ae8192016-06-09 16:56:28 +03001716 if (i801_acpi_probe(priv))
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001717 return -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +02001718
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001719 err = pcim_iomap_regions(dev, 1 << SMBBAR,
1720 dev_driver_string(&dev->dev));
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001721 if (err) {
Jarkko Nikula9cbbf3d2015-02-13 15:52:21 +02001722 dev_err(&dev->dev,
1723 "Failed to request SMBus region 0x%lx-0x%Lx\n",
1724 priv->smba,
Andrew Morton598736c2006-06-30 01:56:20 -07001725 (unsigned long long)pci_resource_end(dev, SMBBAR));
Mika Westerberga7ae8192016-06-09 16:56:28 +03001726 i801_acpi_remove(priv);
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001727 return err;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001728 }
1729
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001730 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1731 priv->original_hstcfg = temp;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001732 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
1733 if (!(temp & SMBHSTCFG_HST_EN)) {
1734 dev_info(&dev->dev, "Enabling SMBus device\n");
1735 temp |= SMBHSTCFG_HST_EN;
1736 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001737 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001738
Daniel Kurtz636752b2012-07-24 14:13:58 +02001739 if (temp & SMBHSTCFG_SMB_SMI_EN) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001740 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001741 /* Disable SMBus interrupt feature if SMBus using SMI# */
1742 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001743 }
Jean Delvareba9ad2a2016-10-11 13:13:27 +02001744 if (temp & SMBHSTCFG_SPD_WD)
1745 dev_info(&dev->dev, "SPD Write Disable is set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Jean Delvarea0921b62008-01-27 18:14:50 +01001747 /* Clear special mode bits */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001748 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1749 outb_p(inb_p(SMBAUXCTL(priv)) &
1750 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarea0921b62008-01-27 18:14:50 +01001751
Jean Delvarea086bb82018-04-11 18:03:31 +02001752 /* Remember original Host Notify setting */
1753 if (priv->features & FEATURE_HOST_NOTIFY)
1754 priv->original_slvcmd = inb_p(SMBSLVCMD(priv));
1755
Jean Delvareb3b8df92014-11-12 10:20:40 +01001756 /* Default timeout in interrupt mode: 200 ms */
1757 priv->adapter.timeout = HZ / 5;
1758
Hans de Goede6e0c9502017-11-22 12:28:17 +01001759 if (dev->irq == IRQ_NOTCONNECTED)
1760 priv->features &= ~FEATURE_IRQ;
1761
Daniel Kurtz636752b2012-07-24 14:13:58 +02001762 if (priv->features & FEATURE_IRQ) {
Jean Delvareaeb8a3d2014-11-12 10:25:37 +01001763 u16 pcictl, pcists;
1764
1765 /* Complain if an interrupt is already pending */
1766 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
1767 if (pcists & SMBPCISTS_INTS)
1768 dev_warn(&dev->dev, "An interrupt is pending!\n");
1769
1770 /* Check if interrupts have been disabled */
1771 pci_read_config_word(priv->pci_dev, SMBPCICTL, &pcictl);
1772 if (pcictl & SMBPCICTL_INTDIS) {
1773 dev_info(&dev->dev, "Interrupts are disabled\n");
1774 priv->features &= ~FEATURE_IRQ;
1775 }
1776 }
1777
1778 if (priv->features & FEATURE_IRQ) {
Daniel Kurtz636752b2012-07-24 14:13:58 +02001779 init_waitqueue_head(&priv->waitq);
1780
Jarkko Nikula1621c592015-02-13 15:52:23 +02001781 err = devm_request_irq(&dev->dev, dev->irq, i801_isr,
1782 IRQF_SHARED,
1783 dev_driver_string(&dev->dev), priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001784 if (err) {
1785 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1786 dev->irq, err);
Jean Delvareae944712014-11-12 10:24:07 +01001787 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001788 }
1789 }
Jean Delvareae944712014-11-12 10:24:07 +01001790 dev_info(&dev->dev, "SMBus using %s\n",
1791 priv->features & FEATURE_IRQ ? "PCI interrupt" : "polling");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001792
Mika Westerberg94246932015-08-06 13:46:25 +01001793 i801_add_tco(priv);
1794
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001795 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1796 "SMBus I801 adapter at %04lx", priv->smba);
1797 err = i2c_add_adapter(&priv->adapter);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001798 if (err) {
Mika Westerberga7ae8192016-06-09 16:56:28 +03001799 i801_acpi_remove(priv);
Jarkko Nikulafef220d2015-02-13 15:52:25 +02001800 return err;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001801 }
Jean Delvare1561bfe2009-01-07 14:29:17 +01001802
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001803 i801_enable_host_notify(&priv->adapter);
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +02001804
Jean Delvaree7198fb2011-05-24 20:58:49 +02001805 i801_probe_optional_slaves(priv);
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001806 /* We ignore errors - multiplexing is optional */
1807 i801_add_mux(priv);
Jean Delvare1561bfe2009-01-07 14:29:17 +01001808
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001809 pci_set_drvdata(dev, priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001810
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +02001811 pm_runtime_set_autosuspend_delay(&dev->dev, 1000);
1812 pm_runtime_use_autosuspend(&dev->dev);
1813 pm_runtime_put_autosuspend(&dev->dev);
1814 pm_runtime_allow(&dev->dev);
1815
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Bill Pemberton0b255e92012-11-27 15:59:38 -05001819static void i801_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001821 struct i801_priv *priv = pci_get_drvdata(dev);
1822
Jarkko Nikulaa7401ca2016-03-10 14:12:22 +02001823 pm_runtime_forbid(&dev->dev);
1824 pm_runtime_get_noresume(&dev->dev);
1825
Benjamin Tissoires22e94bd2016-10-13 14:10:35 +02001826 i801_disable_host_notify(priv);
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001827 i801_del_mux(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001828 i2c_del_adapter(&priv->adapter);
Mika Westerberga7ae8192016-06-09 16:56:28 +03001829 i801_acpi_remove(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001830 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001831
Mika Westerberg94246932015-08-06 13:46:25 +01001832 platform_device_unregister(priv->tco_pdev);
1833
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001834 /*
1835 * do not call pci_disable_device(dev) since it can cause hard hangs on
1836 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Jean Delvaref7f6d912018-04-11 18:05:34 +02001840static void i801_shutdown(struct pci_dev *dev)
1841{
1842 struct i801_priv *priv = pci_get_drvdata(dev);
1843
1844 /* Restore config registers to avoid hard hang on some systems */
1845 i801_disable_host_notify(priv);
1846 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
1847}
1848
Anders Roxell4b2f9bd52018-05-14 11:33:26 +02001849#ifdef CONFIG_PM_SLEEP
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001850static int i801_suspend(struct device *dev)
Jean Delvarea5aaea32007-03-22 19:49:01 +01001851{
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001852 struct pci_dev *pci_dev = to_pci_dev(dev);
1853 struct i801_priv *priv = pci_get_drvdata(pci_dev);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001854
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001855 pci_write_config_byte(pci_dev, SMBHSTCFG, priv->original_hstcfg);
Jean Delvarea5aaea32007-03-22 19:49:01 +01001856 return 0;
1857}
1858
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001859static int i801_resume(struct device *dev)
Jean Delvarea5aaea32007-03-22 19:49:01 +01001860{
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +02001861 struct pci_dev *pci_dev = to_pci_dev(dev);
1862 struct i801_priv *priv = pci_get_drvdata(pci_dev);
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +02001863
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001864 i801_enable_host_notify(&priv->adapter);
Benjamin Tissoires7b0ed332016-06-24 16:39:49 +02001865
Jarkko Nikulaf85da3f2015-02-13 15:52:24 +02001866 return 0;
Jean Delvarea5aaea32007-03-22 19:49:01 +01001867}
Jean Delvarea5aaea32007-03-22 19:49:01 +01001868#endif
1869
Jean Delvarea9c80882018-04-25 11:53:40 +02001870static SIMPLE_DEV_PM_OPS(i801_pm_ops, i801_suspend, i801_resume);
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872static struct pci_driver i801_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 .name = "i801_smbus",
1874 .id_table = i801_ids,
1875 .probe = i801_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001876 .remove = i801_remove,
Jean Delvaref7f6d912018-04-11 18:05:34 +02001877 .shutdown = i801_shutdown,
Jarkko Nikula2ee73c42016-03-10 14:12:21 +02001878 .driver = {
1879 .pm = &i801_pm_ops,
1880 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881};
1882
1883static int __init i2c_i801_init(void)
1884{
Jean Delvare6aa14642011-05-24 20:58:49 +02001885 if (dmi_name_in_vendors("FUJITSU"))
1886 input_apanel_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return pci_register_driver(&i801_driver);
1888}
1889
1890static void __exit i2c_i801_exit(void)
1891{
1892 pci_unregister_driver(&i801_driver);
1893}
1894
Jean Delvare7c81c60f2014-01-29 20:40:08 +01001895MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896MODULE_DESCRIPTION("I801 SMBus driver");
1897MODULE_LICENSE("GPL");
1898
1899module_init(i2c_i801_init);
1900module_exit(i2c_i801_exit);