blob: 10467a3277492a6fa98ee597e7b1b33df687ec32 [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 Delvare7c81c602014-01-29 20:40:08 +01005 Copyright (C) 2007 - 2012 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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24/*
Jean Delvarece316112014-07-17 15:03:24 +020025 * Supports the following Intel I/O Controller Hubs (ICH):
26 *
27 * I/O Block I2C
28 * region SMBus Block proc. block
29 * Chip name PCI ID size PEC buffer call read
30 * ---------------------------------------------------------------------------
31 * 82801AA (ICH) 0x2413 16 no no no no
32 * 82801AB (ICH0) 0x2423 16 no no no no
33 * 82801BA (ICH2) 0x2443 16 no no no no
34 * 82801CA (ICH3) 0x2483 32 soft no no no
35 * 82801DB (ICH4) 0x24c3 32 hard yes no no
36 * 82801E (ICH5) 0x24d3 32 hard yes yes yes
37 * 6300ESB 0x25a4 32 hard yes yes yes
38 * 82801F (ICH6) 0x266a 32 hard yes yes yes
39 * 6310ESB/6320ESB 0x269b 32 hard yes yes yes
40 * 82801G (ICH7) 0x27da 32 hard yes yes yes
41 * 82801H (ICH8) 0x283e 32 hard yes yes yes
42 * 82801I (ICH9) 0x2930 32 hard yes yes yes
43 * EP80579 (Tolapai) 0x5032 32 hard yes yes yes
44 * ICH10 0x3a30 32 hard yes yes yes
45 * ICH10 0x3a60 32 hard yes yes yes
46 * 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
47 * 6 Series (PCH) 0x1c22 32 hard yes yes yes
48 * Patsburg (PCH) 0x1d22 32 hard yes yes yes
49 * Patsburg (PCH) IDF 0x1d70 32 hard yes yes yes
50 * Patsburg (PCH) IDF 0x1d71 32 hard yes yes yes
51 * Patsburg (PCH) IDF 0x1d72 32 hard yes yes yes
52 * DH89xxCC (PCH) 0x2330 32 hard yes yes yes
53 * Panther Point (PCH) 0x1e22 32 hard yes yes yes
54 * Lynx Point (PCH) 0x8c22 32 hard yes yes yes
55 * Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
56 * Avoton (SOC) 0x1f3c 32 hard yes yes yes
57 * Wellsburg (PCH) 0x8d22 32 hard yes yes yes
58 * Wellsburg (PCH) MS 0x8d7d 32 hard yes yes yes
59 * Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes
60 * Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes
61 * Coleto Creek (PCH) 0x23b0 32 hard yes yes yes
Jean Delvareb299de82014-07-17 15:04:41 +020062 * Wildcat Point (PCH) 0x8ca2 32 hard yes yes yes
Jean Delvarece316112014-07-17 15:03:24 +020063 * Wildcat Point-LP (PCH) 0x9ca2 32 hard yes yes yes
64 * BayTrail (SOC) 0x0f12 32 hard yes yes yes
65 *
66 * Features supported by this driver:
67 * Software PEC no
68 * Hardware PEC yes
69 * Block buffer yes
70 * Block process call transaction no
71 * I2C block read transaction yes (doesn't use the block buffer)
72 * Slave mode no
73 * Interrupt processing yes
74 *
75 * See the file Documentation/i2c/busses/i2c-i801 for details.
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Daniel Kurtz636752b2012-07-24 14:13:58 +020078#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/pci.h>
81#include <linux/kernel.h>
82#include <linux/stddef.h>
83#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/ioport.h>
85#include <linux/init.h>
86#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020087#include <linux/acpi.h>
Jean Delvare1561bfe2009-01-07 14:29:17 +010088#include <linux/io.h>
Hans de Goedefa5bfab2009-03-30 21:46:44 +020089#include <linux/dmi.h>
Ben Hutchings665a96b2011-01-10 22:11:22 +010090#include <linux/slab.h>
Daniel Kurtz636752b2012-07-24 14:13:58 +020091#include <linux/wait.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +020092#include <linux/err.h>
93
Jean Delvare79e3e5b2012-10-28 21:37:01 +010094#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
95 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +020096#include <linux/gpio.h>
97#include <linux/i2c-mux-gpio.h>
98#include <linux/platform_device.h>
99#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* I801 SMBus address offsets */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100102#define SMBHSTSTS(p) (0 + (p)->smba)
103#define SMBHSTCNT(p) (2 + (p)->smba)
104#define SMBHSTCMD(p) (3 + (p)->smba)
105#define SMBHSTADD(p) (4 + (p)->smba)
106#define SMBHSTDAT0(p) (5 + (p)->smba)
107#define SMBHSTDAT1(p) (6 + (p)->smba)
108#define SMBBLKDAT(p) (7 + (p)->smba)
109#define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
110#define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
111#define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* PCI Address Constants */
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200114#define SMBBAR 4
Daniel Kurtz636752b2012-07-24 14:13:58 +0200115#define SMBPCISTS 0x006
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define SMBHSTCFG 0x040
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Daniel Kurtz636752b2012-07-24 14:13:58 +0200118/* Host status bits for SMBPCISTS */
119#define SMBPCISTS_INTS 0x08
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* Host configuration bits for SMBHSTCFG */
122#define SMBHSTCFG_HST_EN 1
123#define SMBHSTCFG_SMB_SMI_EN 2
124#define SMBHSTCFG_I2C_EN 4
125
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300126/* Auxiliary control register bits, ICH4+ only */
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200127#define SMBAUXCTL_CRC 1
128#define SMBAUXCTL_E32B 2
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* Other settings */
Jean Delvare84c1af42012-03-26 21:47:19 +0200131#define MAX_RETRIES 400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/* I801 command constants */
134#define I801_QUICK 0x00
135#define I801_BYTE 0x04
136#define I801_BYTE_DATA 0x08
137#define I801_WORD_DATA 0x0C
Jean Delvareae7b0492008-01-27 18:14:49 +0100138#define I801_PROC_CALL 0x10 /* unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define I801_BLOCK_DATA 0x14
Jean Delvare63420642008-01-27 18:14:50 +0100140#define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200141
142/* I801 Host Control register bits */
143#define SMBHSTCNT_INTREN 0x01
144#define SMBHSTCNT_KILL 0x02
145#define SMBHSTCNT_LAST_BYTE 0x20
146#define SMBHSTCNT_START 0x40
147#define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200149/* I801 Hosts Status register bits */
150#define SMBHSTSTS_BYTE_DONE 0x80
151#define SMBHSTSTS_INUSE_STS 0x40
152#define SMBHSTSTS_SMBALERT_STS 0x20
153#define SMBHSTSTS_FAILED 0x10
154#define SMBHSTSTS_BUS_ERR 0x08
155#define SMBHSTSTS_DEV_ERR 0x04
156#define SMBHSTSTS_INTR 0x02
157#define SMBHSTSTS_HOST_BUSY 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Daniel Kurtz70a1cc12012-07-24 14:13:58 +0200159#define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
160 SMBHSTSTS_DEV_ERR)
161
162#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
163 STATUS_ERROR_FLAGS)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200164
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200165/* Older devices have their ID defined in <linux/pci_ids.h> */
Jean Delvarece316112014-07-17 15:03:24 +0200166#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
Alan Cox39e8e302014-08-19 17:37:28 +0300167#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
Jean Delvarece316112014-07-17 15:03:24 +0200168#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
169#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
David Woodhouse55fee8d2010-10-31 21:07:00 +0100170/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
Jean Delvarece316112014-07-17 15:03:24 +0200171#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
172#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
173#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
174#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
175#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
176#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
177#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
178#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
179#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
Jean Delvareb299de82014-07-17 15:04:41 +0200180#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
Jean Delvarece316112014-07-17 15:03:24 +0200181#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
182#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
183#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
184#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
185#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
James Ralstonafc65922013-11-04 09:29:48 -0800186#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
David Woodhouse55fee8d2010-10-31 21:07:00 +0100187
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200188struct i801_mux_config {
189 char *gpio_chip;
190 unsigned values[3];
191 int n_values;
192 unsigned classes[3];
193 unsigned gpios[2]; /* Relative to gpio_chip->base */
194 int n_gpios;
195};
196
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100197struct i801_priv {
198 struct i2c_adapter adapter;
199 unsigned long smba;
200 unsigned char original_hstcfg;
201 struct pci_dev *pci_dev;
202 unsigned int features;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200203
204 /* isr processing */
205 wait_queue_head_t waitq;
206 u8 status;
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200207
208 /* Command state used by isr for byte-by-byte block transactions */
209 u8 cmd;
210 bool is_read;
211 int count;
212 int len;
213 u8 *data;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200214
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100215#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
216 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200217 const struct i801_mux_config *mux_drvdata;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200218 struct platform_device *mux_pdev;
219#endif
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100220};
221
Jean Delvared6072f82005-09-25 16:37:04 +0200222static struct pci_driver i801_driver;
Jean Delvare369f6f42008-01-27 18:14:50 +0100223
224#define FEATURE_SMBUS_PEC (1 << 0)
225#define FEATURE_BLOCK_BUFFER (1 << 1)
226#define FEATURE_BLOCK_PROC (1 << 2)
227#define FEATURE_I2C_BLOCK_READ (1 << 3)
Daniel Kurtz636752b2012-07-24 14:13:58 +0200228#define FEATURE_IRQ (1 << 4)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200229/* Not really a feature, but it's convenient to handle it as such */
230#define FEATURE_IDF (1 << 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jean Delvareadff6872010-05-21 18:40:54 +0200232static const char *i801_feature_names[] = {
233 "SMBus PEC",
234 "Block buffer",
235 "Block process call",
236 "I2C block read",
Daniel Kurtz636752b2012-07-24 14:13:58 +0200237 "Interrupt",
Jean Delvareadff6872010-05-21 18:40:54 +0200238};
239
240static unsigned int disable_features;
241module_param(disable_features, uint, S_IRUGO | S_IWUSR);
Jean Delvare53229342013-05-15 02:44:10 +0000242MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
243 "\t\t 0x01 disable SMBus PEC\n"
244 "\t\t 0x02 disable the block buffer\n"
245 "\t\t 0x08 disable the I2C block read functionality\n"
246 "\t\t 0x10 don't use interrupts ");
Jean Delvareadff6872010-05-21 18:40:54 +0200247
Jean Delvarecf898dc2008-07-14 22:38:33 +0200248/* Make sure the SMBus host is ready to start transmitting.
249 Return 0 if it is, -EBUSY if it is not. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100250static int i801_check_pre(struct i801_priv *priv)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200251{
252 int status;
253
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100254 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200255 if (status & SMBHSTSTS_HOST_BUSY) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100256 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200257 return -EBUSY;
258 }
259
260 status &= STATUS_FLAGS;
261 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100262 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
Jean Delvarecf898dc2008-07-14 22:38:33 +0200263 status);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100264 outb_p(status, SMBHSTSTS(priv));
265 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200266 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100267 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200268 "Failed clearing status flags (%02x)\n",
269 status);
270 return -EBUSY;
271 }
272 }
273
274 return 0;
275}
276
Jean Delvare6cad93c2012-07-24 14:13:58 +0200277/*
278 * Convert the status register to an error code, and clear it.
279 * Note that status only contains the bits we want to clear, not the
280 * actual register value.
281 */
282static int i801_check_post(struct i801_priv *priv, int status)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200283{
284 int result = 0;
285
Daniel Kurtz636752b2012-07-24 14:13:58 +0200286 /*
287 * If the SMBus is still busy, we give up
288 * Note: This timeout condition only happens when using polling
289 * transactions. For interrupt operation, NAK/timeout is indicated by
290 * DEV_ERR.
291 */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200292 if (unlikely(status < 0)) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100293 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200294 /* try to stop the current command */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100295 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
296 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
297 SMBHSTCNT(priv));
Jean Delvare84c1af42012-03-26 21:47:19 +0200298 usleep_range(1000, 2000);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100299 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
300 SMBHSTCNT(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200301
302 /* Check if it worked */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100303 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200304 if ((status & SMBHSTSTS_HOST_BUSY) ||
305 !(status & SMBHSTSTS_FAILED))
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100306 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200307 "Failed terminating the transaction\n");
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100308 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200309 return -ETIMEDOUT;
310 }
311
312 if (status & SMBHSTSTS_FAILED) {
313 result = -EIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100314 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200315 }
316 if (status & SMBHSTSTS_DEV_ERR) {
317 result = -ENXIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100318 dev_dbg(&priv->pci_dev->dev, "No response\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200319 }
320 if (status & SMBHSTSTS_BUS_ERR) {
321 result = -EAGAIN;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100322 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200323 }
324
Jean Delvare6cad93c2012-07-24 14:13:58 +0200325 /* Clear status flags except BYTE_DONE, to be cleared by caller */
326 outb_p(status, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200327
328 return result;
329}
330
Jean Delvare6cad93c2012-07-24 14:13:58 +0200331/* Wait for BUSY being cleared and either INTR or an error flag being set */
332static int i801_wait_intr(struct i801_priv *priv)
333{
334 int timeout = 0;
335 int status;
336
337 /* We will always wait for a fraction of a second! */
338 do {
339 usleep_range(250, 500);
340 status = inb_p(SMBHSTSTS(priv));
341 } while (((status & SMBHSTSTS_HOST_BUSY) ||
342 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
343 (timeout++ < MAX_RETRIES));
344
345 if (timeout > MAX_RETRIES) {
346 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
347 return -ETIMEDOUT;
348 }
349 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
350}
351
352/* Wait for either BYTE_DONE or an error flag being set */
353static int i801_wait_byte_done(struct i801_priv *priv)
354{
355 int timeout = 0;
356 int status;
357
358 /* We will always wait for a fraction of a second! */
359 do {
360 usleep_range(250, 500);
361 status = inb_p(SMBHSTSTS(priv));
362 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
363 (timeout++ < MAX_RETRIES));
364
365 if (timeout > MAX_RETRIES) {
366 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
367 return -ETIMEDOUT;
368 }
369 return status & STATUS_ERROR_FLAGS;
370}
371
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100372static int i801_transaction(struct i801_priv *priv, int xact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jean Delvare2b738092008-07-14 22:38:32 +0200374 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200375 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100377 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200378 if (result < 0)
379 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Daniel Kurtz636752b2012-07-24 14:13:58 +0200381 if (priv->features & FEATURE_IRQ) {
382 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
383 SMBHSTCNT(priv));
384 wait_event(priv->waitq, (status = priv->status));
385 priv->status = 0;
386 return i801_check_post(priv, status);
387 }
388
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200389 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
Daniel Kurtz37af8712012-07-24 14:13:58 +0200390 * SMBSCMD are passed in xact */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200391 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jean Delvare6cad93c2012-07-24 14:13:58 +0200393 status = i801_wait_intr(priv);
394 return i801_check_post(priv, status);
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200395}
396
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100397static int i801_block_transaction_by_block(struct i801_priv *priv,
398 union i2c_smbus_data *data,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200399 char read_write, int hwpec)
400{
401 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200402 int status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200403
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100404 inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200405
406 /* Use 32-byte buffer to process this transaction */
407 if (read_write == I2C_SMBUS_WRITE) {
408 len = data->block[0];
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100409 outb_p(len, SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200410 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100411 outb_p(data->block[i+1], SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200412 }
413
Daniel Kurtz37af8712012-07-24 14:13:58 +0200414 status = i801_transaction(priv, I801_BLOCK_DATA |
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200415 (hwpec ? SMBHSTCNT_PEC_EN : 0));
David Brownell97140342008-07-14 22:38:25 +0200416 if (status)
417 return status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200418
419 if (read_write == I2C_SMBUS_READ) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100420 len = inb_p(SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200421 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
David Brownell97140342008-07-14 22:38:25 +0200422 return -EPROTO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200423
424 data->block[0] = len;
425 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100426 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200427 }
428 return 0;
429}
430
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200431static void i801_isr_byte_done(struct i801_priv *priv)
432{
433 if (priv->is_read) {
434 /* For SMBus block reads, length is received with first byte */
435 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
436 (priv->count == 0)) {
437 priv->len = inb_p(SMBHSTDAT0(priv));
438 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
439 dev_err(&priv->pci_dev->dev,
440 "Illegal SMBus block read size %d\n",
441 priv->len);
442 /* FIXME: Recover */
443 priv->len = I2C_SMBUS_BLOCK_MAX;
444 } else {
445 dev_dbg(&priv->pci_dev->dev,
446 "SMBus block read size is %d\n",
447 priv->len);
448 }
449 priv->data[-1] = priv->len;
450 }
451
452 /* Read next byte */
453 if (priv->count < priv->len)
454 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
455 else
456 dev_dbg(&priv->pci_dev->dev,
457 "Discarding extra byte on block read\n");
458
459 /* Set LAST_BYTE for last byte of read transaction */
460 if (priv->count == priv->len - 1)
461 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
462 SMBHSTCNT(priv));
463 } else if (priv->count < priv->len - 1) {
464 /* Write next byte, except for IRQ after last byte */
465 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
466 }
467
468 /* Clear BYTE_DONE to continue with next byte */
469 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
470}
471
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200472/*
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200473 * There are two kinds of interrupts:
474 *
475 * 1) i801 signals transaction completion with one of these interrupts:
476 * INTR - Success
477 * DEV_ERR - Invalid command, NAK or communication timeout
478 * BUS_ERR - SMI# transaction collision
479 * FAILED - transaction was canceled due to a KILL request
480 * When any of these occur, update ->status and wake up the waitq.
481 * ->status must be cleared before kicking off the next transaction.
482 *
483 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
484 * occurs for each byte of a byte-by-byte to prepare the next byte.
Daniel Kurtz636752b2012-07-24 14:13:58 +0200485 */
486static irqreturn_t i801_isr(int irq, void *dev_id)
487{
488 struct i801_priv *priv = dev_id;
489 u16 pcists;
490 u8 status;
491
492 /* Confirm this is our interrupt */
493 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
494 if (!(pcists & SMBPCISTS_INTS))
495 return IRQ_NONE;
496
497 status = inb_p(SMBHSTSTS(priv));
498 if (status != 0x42)
499 dev_dbg(&priv->pci_dev->dev, "irq: status = %02x\n", status);
500
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200501 if (status & SMBHSTSTS_BYTE_DONE)
502 i801_isr_byte_done(priv);
503
Daniel Kurtz636752b2012-07-24 14:13:58 +0200504 /*
505 * Clear irq sources and report transaction result.
506 * ->status must be cleared before the next transaction is started.
507 */
508 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
509 if (status) {
510 outb_p(status, SMBHSTSTS(priv));
511 priv->status |= status;
512 wake_up(&priv->waitq);
513 }
514
515 return IRQ_HANDLED;
516}
517
518/*
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200519 * For "byte-by-byte" block transactions:
520 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
521 * I2C read uses cmd=I801_I2C_BLOCK_DATA
522 */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100523static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
524 union i2c_smbus_data *data,
Jean Delvare63420642008-01-27 18:14:50 +0100525 char read_write, int command,
526 int hwpec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 int i, len;
529 int smbcmd;
Jean Delvare2b738092008-07-14 22:38:32 +0200530 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200531 int result;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200532
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100533 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200534 if (result < 0)
535 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200537 len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100540 outb_p(len, SMBHSTDAT0(priv));
541 outb_p(data->block[1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200544 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
545 read_write == I2C_SMBUS_READ)
546 smbcmd = I801_I2C_BLOCK_DATA;
547 else
548 smbcmd = I801_BLOCK_DATA;
549
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200550 if (priv->features & FEATURE_IRQ) {
551 priv->is_read = (read_write == I2C_SMBUS_READ);
552 if (len == 1 && priv->is_read)
553 smbcmd |= SMBHSTCNT_LAST_BYTE;
554 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
555 priv->len = len;
556 priv->count = 0;
557 priv->data = &data->block[1];
558
559 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
560 wait_event(priv->waitq, (status = priv->status));
561 priv->status = 0;
562 return i801_check_post(priv, status);
563 }
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 for (i = 1; i <= len; i++) {
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200566 if (i == len && read_write == I2C_SMBUS_READ)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200567 smbcmd |= SMBHSTCNT_LAST_BYTE;
Daniel Kurtz37af8712012-07-24 14:13:58 +0200568 outb_p(smbcmd, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (i == 1)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200571 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100572 SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Jean Delvare6cad93c2012-07-24 14:13:58 +0200574 status = i801_wait_byte_done(priv);
575 if (status)
576 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jean Delvare63420642008-01-27 18:14:50 +0100578 if (i == 1 && read_write == I2C_SMBUS_READ
579 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100580 len = inb_p(SMBHSTDAT0(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200581 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100582 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200583 "Illegal SMBus block read size %d\n",
584 len);
585 /* Recover */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100586 while (inb_p(SMBHSTSTS(priv)) &
587 SMBHSTSTS_HOST_BUSY)
588 outb_p(SMBHSTSTS_BYTE_DONE,
589 SMBHSTSTS(priv));
590 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
David Brownell97140342008-07-14 22:38:25 +0200591 return -EPROTO;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 data->block[0] = len;
594 }
595
596 /* Retrieve/store value in SMBBLKDAT */
597 if (read_write == I2C_SMBUS_READ)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100598 data->block[i] = inb_p(SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100600 outb_p(data->block[i+1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jean Delvarecf898dc2008-07-14 22:38:33 +0200602 /* signals SMBBLKDAT ready */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200603 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200604 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200605
Jean Delvare6cad93c2012-07-24 14:13:58 +0200606 status = i801_wait_intr(priv);
607exit:
608 return i801_check_post(priv, status);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200609}
610
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100611static int i801_set_block_buffer_mode(struct i801_priv *priv)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200612{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100613 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
614 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
David Brownell97140342008-07-14 22:38:25 +0200615 return -EIO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200616 return 0;
617}
618
619/* Block transaction function */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100620static int i801_block_transaction(struct i801_priv *priv,
621 union i2c_smbus_data *data, char read_write,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200622 int command, int hwpec)
623{
624 int result = 0;
625 unsigned char hostc;
626
627 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
628 if (read_write == I2C_SMBUS_WRITE) {
629 /* set I2C_EN bit in configuration register */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100630 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
631 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200632 hostc | SMBHSTCFG_I2C_EN);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100633 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
634 dev_err(&priv->pci_dev->dev,
Jean Delvare63420642008-01-27 18:14:50 +0100635 "I2C block read is unsupported!\n");
David Brownell97140342008-07-14 22:38:25 +0200636 return -EOPNOTSUPP;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Jean Delvare63420642008-01-27 18:14:50 +0100640 if (read_write == I2C_SMBUS_WRITE
641 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200642 if (data->block[0] < 1)
643 data->block[0] = 1;
644 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
645 data->block[0] = I2C_SMBUS_BLOCK_MAX;
646 } else {
Jean Delvare63420642008-01-27 18:14:50 +0100647 data->block[0] = 32; /* max for SMBus block reads */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200648 }
649
Jean Delvarec074c392010-03-13 20:56:53 +0100650 /* Experience has shown that the block buffer can only be used for
651 SMBus (not I2C) block transactions, even though the datasheet
652 doesn't mention this limitation. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100653 if ((priv->features & FEATURE_BLOCK_BUFFER)
Jean Delvarec074c392010-03-13 20:56:53 +0100654 && command != I2C_SMBUS_I2C_BLOCK_DATA
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100655 && i801_set_block_buffer_mode(priv) == 0)
656 result = i801_block_transaction_by_block(priv, data,
657 read_write, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200658 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100659 result = i801_block_transaction_byte_by_byte(priv, data,
660 read_write,
Jean Delvare63420642008-01-27 18:14:50 +0100661 command, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200662
Jean Delvare63420642008-01-27 18:14:50 +0100663 if (command == I2C_SMBUS_I2C_BLOCK_DATA
664 && read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* restore saved configuration register value */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100666 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 return result;
669}
670
David Brownell97140342008-07-14 22:38:25 +0200671/* Return negative errno on error. */
Ivo Manca3fb21c62010-05-21 18:40:55 +0200672static s32 i801_access(struct i2c_adapter *adap, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned short flags, char read_write, u8 command,
Ivo Manca3fb21c62010-05-21 18:40:55 +0200674 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200676 int hwpec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int block = 0;
678 int ret, xact = 0;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100679 struct i801_priv *priv = i2c_get_adapdata(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100681 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200682 && size != I2C_SMBUS_QUICK
683 && size != I2C_SMBUS_I2C_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 switch (size) {
686 case I2C_SMBUS_QUICK:
687 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100688 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 xact = I801_QUICK;
690 break;
691 case I2C_SMBUS_BYTE:
692 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100693 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100695 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xact = I801_BYTE;
697 break;
698 case I2C_SMBUS_BYTE_DATA:
699 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100700 SMBHSTADD(priv));
701 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100703 outb_p(data->byte, SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 xact = I801_BYTE_DATA;
705 break;
706 case I2C_SMBUS_WORD_DATA:
707 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100708 SMBHSTADD(priv));
709 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100711 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
712 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 xact = I801_WORD_DATA;
715 break;
716 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100718 SMBHSTADD(priv));
719 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 block = 1;
721 break;
Jean Delvare63420642008-01-27 18:14:50 +0100722 case I2C_SMBUS_I2C_BLOCK_DATA:
723 /* NB: page 240 of ICH5 datasheet shows that the R/#W
724 * bit should be cleared here, even when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100725 outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100726 if (read_write == I2C_SMBUS_READ) {
727 /* NB: page 240 of ICH5 datasheet also shows
728 * that DATA1 is the cmd field when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100729 outb_p(command, SMBHSTDAT1(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100730 } else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100731 outb_p(command, SMBHSTCMD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100732 block = 1;
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100735 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
736 size);
David Brownell97140342008-07-14 22:38:25 +0200737 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200740 if (hwpec) /* enable/disable hardware PEC */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100741 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200742 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100743 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
744 SMBAUXCTL(priv));
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200745
Ivo Manca3fb21c62010-05-21 18:40:55 +0200746 if (block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100747 ret = i801_block_transaction(priv, data, read_write, size,
748 hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200749 else
Daniel Kurtz37af8712012-07-24 14:13:58 +0200750 ret = i801_transaction(priv, xact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Jean Delvarec79cfba2006-04-20 02:43:18 -0700752 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200753 time, so we forcibly disable it after every transaction. Turn off
754 E32B for the same reason. */
Jean Delvarea0921b62008-01-27 18:14:50 +0100755 if (hwpec || block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100756 outb_p(inb_p(SMBAUXCTL(priv)) &
757 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarec79cfba2006-04-20 02:43:18 -0700758
Ivo Manca3fb21c62010-05-21 18:40:55 +0200759 if (block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
Ivo Manca3fb21c62010-05-21 18:40:55 +0200761 if (ret)
David Brownell97140342008-07-14 22:38:25 +0200762 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
764 return 0;
765
766 switch (xact & 0x7f) {
767 case I801_BYTE: /* Result put in SMBHSTDAT0 */
768 case I801_BYTE_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100769 data->byte = inb_p(SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771 case I801_WORD_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100772 data->word = inb_p(SMBHSTDAT0(priv)) +
773 (inb_p(SMBHSTDAT1(priv)) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775 }
776 return 0;
777}
778
779
780static u32 i801_func(struct i2c_adapter *adapter)
781{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100782 struct i801_priv *priv = i2c_get_adapdata(adapter);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare369f6f42008-01-27 18:14:50 +0100785 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
786 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100787 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
788 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
Jean Delvare63420642008-01-27 18:14:50 +0100789 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Jean Delvare8f9082c2006-09-03 22:39:46 +0200792static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .smbus_xfer = i801_access,
794 .functionality = i801_func,
795};
796
Jingoo Han392debf2013-12-03 08:11:20 +0900797static const struct pci_device_id i801_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
799 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
800 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
801 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
802 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
803 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
804 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
805 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
806 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
Jason Gastonb0a70b52005-04-16 15:24:45 -0700807 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
Jason Gaston8254fc42006-01-09 10:58:08 -0800808 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
Jason Gastonadbc2a12006-11-22 15:19:12 -0800809 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700810 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
Gaston, Jason Dd28dc712008-02-24 20:03:42 +0100811 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
812 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700813 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
814 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
Seth Heasleye30d9852010-10-31 21:06:59 +0100815 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
David Woodhouse55fee8d2010-10-31 21:07:00 +0100816 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
817 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
818 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
Seth Heasley662cda82011-03-20 14:50:53 +0100819 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
Seth Heasley6e2a8512011-05-24 20:58:49 +0200820 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
Seth Heasley062737f2012-03-26 21:47:19 +0200821 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
James Ralston4a8f1dd2012-09-10 10:14:02 +0200822 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
Seth Heasleyc2db409c2013-01-30 15:25:32 +0000823 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
James Ralstona3fc0ff2013-02-14 09:15:33 +0000824 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
825 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
826 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
827 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
Seth Heasleyf39901c2013-06-19 16:59:57 -0700828 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
Jean Delvareb299de82014-07-17 15:04:41 +0200829 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
James Ralstonafc65922013-11-04 09:29:48 -0800830 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
Chew, Kean ho1b31e9b2014-03-01 00:03:56 +0800831 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
Alan Cox39e8e302014-08-19 17:37:28 +0300832 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 { 0, }
834};
835
Ivo Manca3fb21c62010-05-21 18:40:55 +0200836MODULE_DEVICE_TABLE(pci, i801_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Jean Delvare8eacfce2011-05-24 20:58:49 +0200838#if defined CONFIG_X86 && defined CONFIG_DMI
Jean Delvare1561bfe2009-01-07 14:29:17 +0100839static unsigned char apanel_addr;
840
841/* Scan the system ROM for the signature "FJKEYINF" */
842static __init const void __iomem *bios_signature(const void __iomem *bios)
843{
844 ssize_t offset;
845 const unsigned char signature[] = "FJKEYINF";
846
847 for (offset = 0; offset < 0x10000; offset += 0x10) {
848 if (check_signature(bios + offset, signature,
849 sizeof(signature)-1))
850 return bios + offset;
851 }
852 return NULL;
853}
854
855static void __init input_apanel_init(void)
856{
857 void __iomem *bios;
858 const void __iomem *p;
859
860 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
861 p = bios_signature(bios);
862 if (p) {
863 /* just use the first address */
864 apanel_addr = readb(p + 8 + 3) >> 1;
865 }
866 iounmap(bios);
867}
Jean Delvare1561bfe2009-01-07 14:29:17 +0100868
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200869struct dmi_onboard_device_info {
870 const char *name;
871 u8 type;
872 unsigned short i2c_addr;
873 const char *i2c_type;
874};
875
Bill Pemberton0b255e92012-11-27 15:59:38 -0500876static const struct dmi_onboard_device_info dmi_devices[] = {
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200877 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
878 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
879 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
880};
881
Bill Pemberton0b255e92012-11-27 15:59:38 -0500882static void dmi_check_onboard_device(u8 type, const char *name,
883 struct i2c_adapter *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200884{
885 int i;
886 struct i2c_board_info info;
887
888 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
889 /* & ~0x80, ignore enabled/disabled bit */
890 if ((type & ~0x80) != dmi_devices[i].type)
891 continue;
Jean Delvarefaabd472010-07-09 16:22:51 +0200892 if (strcasecmp(name, dmi_devices[i].name))
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200893 continue;
894
895 memset(&info, 0, sizeof(struct i2c_board_info));
896 info.addr = dmi_devices[i].i2c_addr;
897 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
898 i2c_new_device(adap, &info);
899 break;
900 }
901}
902
903/* We use our own function to check for onboard devices instead of
904 dmi_find_device() as some buggy BIOS's have the devices we are interested
905 in marked as disabled */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500906static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200907{
908 int i, count;
909
910 if (dm->type != 10)
911 return;
912
913 count = (dm->length - sizeof(struct dmi_header)) / 2;
914 for (i = 0; i < count; i++) {
915 const u8 *d = (char *)(dm + 1) + (i * 2);
916 const char *name = ((char *) dm) + dm->length;
917 u8 type = d[0];
918 u8 s = d[1];
919
920 if (!s)
921 continue;
922 s--;
923 while (s > 0 && name[0]) {
924 name += strlen(name) + 1;
925 s--;
926 }
927 if (name[0] == 0) /* Bogus string reference */
928 continue;
929
930 dmi_check_onboard_device(type, name, adap);
931 }
932}
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200933
Jean Delvaree7198fb2011-05-24 20:58:49 +0200934/* Register optional slaves */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500935static void i801_probe_optional_slaves(struct i801_priv *priv)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200936{
937 /* Only register slaves on main SMBus channel */
938 if (priv->features & FEATURE_IDF)
939 return;
940
Jean Delvaree7198fb2011-05-24 20:58:49 +0200941 if (apanel_addr) {
942 struct i2c_board_info info;
943
944 memset(&info, 0, sizeof(struct i2c_board_info));
945 info.addr = apanel_addr;
946 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
947 i2c_new_device(&priv->adapter, &info);
948 }
Jean Delvare8eacfce2011-05-24 20:58:49 +0200949
Jean Delvaree7198fb2011-05-24 20:58:49 +0200950 if (dmi_name_in_vendors("FUJITSU"))
951 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
Jean Delvaree7198fb2011-05-24 20:58:49 +0200952}
Jean Delvare8eacfce2011-05-24 20:58:49 +0200953#else
954static void __init input_apanel_init(void) {}
Bill Pemberton0b255e92012-11-27 15:59:38 -0500955static void i801_probe_optional_slaves(struct i801_priv *priv) {}
Jean Delvare8eacfce2011-05-24 20:58:49 +0200956#endif /* CONFIG_X86 && CONFIG_DMI */
Jean Delvaree7198fb2011-05-24 20:58:49 +0200957
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100958#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
959 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200960static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
961 .gpio_chip = "gpio_ich",
962 .values = { 0x02, 0x03 },
963 .n_values = 2,
964 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
965 .gpios = { 52, 53 },
966 .n_gpios = 2,
967};
968
969static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
970 .gpio_chip = "gpio_ich",
971 .values = { 0x02, 0x03, 0x01 },
972 .n_values = 3,
973 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
974 .gpios = { 52, 53 },
975 .n_gpios = 2,
976};
977
Bill Pemberton0b255e92012-11-27 15:59:38 -0500978static const struct dmi_system_id mux_dmi_table[] = {
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200979 {
980 .matches = {
981 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
982 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
983 },
984 .driver_data = &i801_mux_config_asus_z8_d12,
985 },
986 {
987 .matches = {
988 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
989 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
990 },
991 .driver_data = &i801_mux_config_asus_z8_d12,
992 },
993 {
994 .matches = {
995 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
996 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
997 },
998 .driver_data = &i801_mux_config_asus_z8_d12,
999 },
1000 {
1001 .matches = {
1002 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1003 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
1004 },
1005 .driver_data = &i801_mux_config_asus_z8_d12,
1006 },
1007 {
1008 .matches = {
1009 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1010 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
1011 },
1012 .driver_data = &i801_mux_config_asus_z8_d12,
1013 },
1014 {
1015 .matches = {
1016 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1017 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
1018 },
1019 .driver_data = &i801_mux_config_asus_z8_d12,
1020 },
1021 {
1022 .matches = {
1023 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1024 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
1025 },
1026 .driver_data = &i801_mux_config_asus_z8_d18,
1027 },
1028 {
1029 .matches = {
1030 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1031 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1032 },
1033 .driver_data = &i801_mux_config_asus_z8_d18,
1034 },
1035 {
1036 .matches = {
1037 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1038 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1039 },
1040 .driver_data = &i801_mux_config_asus_z8_d12,
1041 },
1042 { }
1043};
1044
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001045/* Setup multiplexing if needed */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001046static int i801_add_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001047{
1048 struct device *dev = &priv->adapter.dev;
1049 const struct i801_mux_config *mux_config;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001050 struct i2c_mux_gpio_platform_data gpio_data;
Jean Delvaref82b8622012-10-05 22:23:54 +02001051 int err;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001052
1053 if (!priv->mux_drvdata)
1054 return 0;
1055 mux_config = priv->mux_drvdata;
1056
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001057 /* Prepare the platform data */
1058 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1059 gpio_data.parent = priv->adapter.nr;
1060 gpio_data.values = mux_config->values;
1061 gpio_data.n_values = mux_config->n_values;
1062 gpio_data.classes = mux_config->classes;
Jean Delvaref82b8622012-10-05 22:23:54 +02001063 gpio_data.gpio_chip = mux_config->gpio_chip;
1064 gpio_data.gpios = mux_config->gpios;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001065 gpio_data.n_gpios = mux_config->n_gpios;
1066 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1067
1068 /* Register the mux device */
1069 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
Jean Delvaref82b8622012-10-05 22:23:54 +02001070 PLATFORM_DEVID_AUTO, &gpio_data,
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001071 sizeof(struct i2c_mux_gpio_platform_data));
1072 if (IS_ERR(priv->mux_pdev)) {
1073 err = PTR_ERR(priv->mux_pdev);
1074 priv->mux_pdev = NULL;
1075 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1076 return err;
1077 }
1078
1079 return 0;
1080}
1081
Bill Pemberton0b255e92012-11-27 15:59:38 -05001082static void i801_del_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001083{
1084 if (priv->mux_pdev)
1085 platform_device_unregister(priv->mux_pdev);
1086}
1087
Bill Pemberton0b255e92012-11-27 15:59:38 -05001088static unsigned int i801_get_adapter_class(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001089{
1090 const struct dmi_system_id *id;
1091 const struct i801_mux_config *mux_config;
1092 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1093 int i;
1094
1095 id = dmi_first_match(mux_dmi_table);
1096 if (id) {
Jean Delvare28901f52012-10-28 21:37:01 +01001097 /* Remove branch classes from trunk */
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001098 mux_config = id->driver_data;
1099 for (i = 0; i < mux_config->n_values; i++)
1100 class &= ~mux_config->classes[i];
1101
1102 /* Remember for later */
1103 priv->mux_drvdata = mux_config;
1104 }
1105
1106 return class;
1107}
1108#else
1109static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1110static inline void i801_del_mux(struct i801_priv *priv) { }
1111
1112static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1113{
1114 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1115}
1116#endif
1117
Bill Pemberton0b255e92012-11-27 15:59:38 -05001118static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001120 unsigned char temp;
Jean Delvareadff6872010-05-21 18:40:54 +02001121 int err, i;
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001122 struct i801_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001124 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1125 if (!priv)
1126 return -ENOMEM;
1127
1128 i2c_set_adapdata(&priv->adapter, priv);
1129 priv->adapter.owner = THIS_MODULE;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001130 priv->adapter.class = i801_get_adapter_class(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001131 priv->adapter.algo = &smbus_algorithm;
1132
1133 priv->pci_dev = dev;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001134 switch (dev->device) {
Jean Delvaree7198fb2011-05-24 20:58:49 +02001135 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1136 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1137 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
James Ralstona3fc0ff2013-02-14 09:15:33 +00001138 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
1139 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
1140 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
Jean Delvaree7198fb2011-05-24 20:58:49 +02001141 priv->features |= FEATURE_IDF;
1142 /* fall through */
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001143 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001144 priv->features |= FEATURE_I2C_BLOCK_READ;
Jean Delvare6676a842012-12-16 21:11:55 +01001145 priv->features |= FEATURE_IRQ;
Jean Delvare63420642008-01-27 18:14:50 +01001146 /* fall through */
1147 case PCI_DEVICE_ID_INTEL_82801DB_3:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001148 priv->features |= FEATURE_SMBUS_PEC;
1149 priv->features |= FEATURE_BLOCK_BUFFER;
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001150 /* fall through */
1151 case PCI_DEVICE_ID_INTEL_82801CA_3:
1152 case PCI_DEVICE_ID_INTEL_82801BA_2:
1153 case PCI_DEVICE_ID_INTEL_82801AB_3:
1154 case PCI_DEVICE_ID_INTEL_82801AA_3:
Jean Delvare250d1bd2006-12-10 21:21:33 +01001155 break;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001156 }
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001157
Jean Delvareadff6872010-05-21 18:40:54 +02001158 /* Disable features on user request */
1159 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001160 if (priv->features & disable_features & (1 << i))
Jean Delvareadff6872010-05-21 18:40:54 +02001161 dev_notice(&dev->dev, "%s disabled by user\n",
1162 i801_feature_names[i]);
1163 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001164 priv->features &= ~disable_features;
Jean Delvareadff6872010-05-21 18:40:54 +02001165
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001166 err = pci_enable_device(dev);
1167 if (err) {
1168 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1169 err);
1170 goto exit;
1171 }
1172
1173 /* Determine the address of the SMBus area */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001174 priv->smba = pci_resource_start(dev, SMBBAR);
1175 if (!priv->smba) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001176 dev_err(&dev->dev, "SMBus base address uninitialized, "
1177 "upgrade BIOS\n");
1178 err = -ENODEV;
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001179 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001180 }
1181
Jean Delvare54fb4a052008-07-14 22:38:33 +02001182 err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
Jean Delvare18669ea2009-10-04 22:53:45 +02001183 if (err) {
1184 err = -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +02001185 goto exit;
Jean Delvare18669ea2009-10-04 22:53:45 +02001186 }
Jean Delvare54fb4a052008-07-14 22:38:33 +02001187
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001188 err = pci_request_region(dev, SMBBAR, i801_driver.name);
1189 if (err) {
1190 dev_err(&dev->dev, "Failed to request SMBus region "
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001191 "0x%lx-0x%Lx\n", priv->smba,
Andrew Morton598736c2006-06-30 01:56:20 -07001192 (unsigned long long)pci_resource_end(dev, SMBBAR));
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001193 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001194 }
1195
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001196 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1197 priv->original_hstcfg = temp;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001198 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
1199 if (!(temp & SMBHSTCFG_HST_EN)) {
1200 dev_info(&dev->dev, "Enabling SMBus device\n");
1201 temp |= SMBHSTCFG_HST_EN;
1202 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001203 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001204
Daniel Kurtz636752b2012-07-24 14:13:58 +02001205 if (temp & SMBHSTCFG_SMB_SMI_EN) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001206 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001207 /* Disable SMBus interrupt feature if SMBus using SMI# */
1208 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Jean Delvarea0921b62008-01-27 18:14:50 +01001211 /* Clear special mode bits */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001212 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1213 outb_p(inb_p(SMBAUXCTL(priv)) &
1214 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarea0921b62008-01-27 18:14:50 +01001215
Daniel Kurtz636752b2012-07-24 14:13:58 +02001216 if (priv->features & FEATURE_IRQ) {
1217 init_waitqueue_head(&priv->waitq);
1218
1219 err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
1220 i801_driver.name, priv);
1221 if (err) {
1222 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1223 dev->irq, err);
1224 goto exit_release;
1225 }
Jean Delvare29b60852012-07-24 14:13:59 +02001226 dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001227 }
1228
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01001229 /* set up the sysfs linkage to our parent device */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001230 priv->adapter.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jean Delvare7e2193a2009-12-06 17:06:27 +01001232 /* Retry up to 3 times on lost arbitration */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001233 priv->adapter.retries = 3;
Jean Delvare7e2193a2009-12-06 17:06:27 +01001234
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001235 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1236 "SMBus I801 adapter at %04lx", priv->smba);
1237 err = i2c_add_adapter(&priv->adapter);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001238 if (err) {
1239 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001240 goto exit_free_irq;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001241 }
Jean Delvare1561bfe2009-01-07 14:29:17 +01001242
Jean Delvaree7198fb2011-05-24 20:58:49 +02001243 i801_probe_optional_slaves(priv);
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001244 /* We ignore errors - multiplexing is optional */
1245 i801_add_mux(priv);
Jean Delvare1561bfe2009-01-07 14:29:17 +01001246
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001247 pci_set_drvdata(dev, priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001248
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001249 return 0;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001250
Daniel Kurtz636752b2012-07-24 14:13:58 +02001251exit_free_irq:
1252 if (priv->features & FEATURE_IRQ)
1253 free_irq(dev->irq, priv);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001254exit_release:
1255 pci_release_region(dev, SMBBAR);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001256exit:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001257 kfree(priv);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Bill Pemberton0b255e92012-11-27 15:59:38 -05001261static void i801_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001263 struct i801_priv *priv = pci_get_drvdata(dev);
1264
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001265 i801_del_mux(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001266 i2c_del_adapter(&priv->adapter);
1267 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001268
1269 if (priv->features & FEATURE_IRQ)
1270 free_irq(dev->irq, priv);
Jean Delvare6dcc19d2006-06-12 21:53:02 +02001271 pci_release_region(dev, SMBBAR);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001272
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001273 kfree(priv);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001274 /*
1275 * do not call pci_disable_device(dev) since it can cause hard hangs on
1276 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Jean Delvarea5aaea32007-03-22 19:49:01 +01001280#ifdef CONFIG_PM
1281static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
1282{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001283 struct i801_priv *priv = pci_get_drvdata(dev);
1284
Jean Delvarea5aaea32007-03-22 19:49:01 +01001285 pci_save_state(dev);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001286 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Jean Delvarea5aaea32007-03-22 19:49:01 +01001287 pci_set_power_state(dev, pci_choose_state(dev, mesg));
1288 return 0;
1289}
1290
1291static int i801_resume(struct pci_dev *dev)
1292{
1293 pci_set_power_state(dev, PCI_D0);
1294 pci_restore_state(dev);
1295 return pci_enable_device(dev);
1296}
1297#else
1298#define i801_suspend NULL
1299#define i801_resume NULL
1300#endif
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302static struct pci_driver i801_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .name = "i801_smbus",
1304 .id_table = i801_ids,
1305 .probe = i801_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001306 .remove = i801_remove,
Jean Delvarea5aaea32007-03-22 19:49:01 +01001307 .suspend = i801_suspend,
1308 .resume = i801_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309};
1310
1311static int __init i2c_i801_init(void)
1312{
Jean Delvare6aa14642011-05-24 20:58:49 +02001313 if (dmi_name_in_vendors("FUJITSU"))
1314 input_apanel_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return pci_register_driver(&i801_driver);
1316}
1317
1318static void __exit i2c_i801_exit(void)
1319{
1320 pci_unregister_driver(&i801_driver);
1321}
1322
Jean Delvare7c81c602014-01-29 20:40:08 +01001323MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324MODULE_DESCRIPTION("I801 SMBus driver");
1325MODULE_LICENSE("GPL");
1326
1327module_init(i2c_i801_init);
1328module_exit(i2c_i801_exit);