blob: 67661bbcbdd37d5334d22541c3df3b84fdf37a9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
3 Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
4 <mdsxyz123@yahoo.com>
Jean Delvareb3b8df92014-11-12 10:20:40 +01005 Copyright (C) 2007 - 2014 Jean Delvare <jdelvare@suse.de>
David Woodhouse0cd96eb2010-10-31 21:06:59 +01006 Copyright (C) 2010 Intel Corporation,
7 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018*/
19
20/*
Jean Delvarece316112014-07-17 15:03:24 +020021 * Supports the following Intel I/O Controller Hubs (ICH):
22 *
23 * I/O Block I2C
24 * region SMBus Block proc. block
25 * Chip name PCI ID size PEC buffer call read
26 * ---------------------------------------------------------------------------
27 * 82801AA (ICH) 0x2413 16 no no no no
28 * 82801AB (ICH0) 0x2423 16 no no no no
29 * 82801BA (ICH2) 0x2443 16 no no no no
30 * 82801CA (ICH3) 0x2483 32 soft no no no
31 * 82801DB (ICH4) 0x24c3 32 hard yes no no
32 * 82801E (ICH5) 0x24d3 32 hard yes yes yes
33 * 6300ESB 0x25a4 32 hard yes yes yes
34 * 82801F (ICH6) 0x266a 32 hard yes yes yes
35 * 6310ESB/6320ESB 0x269b 32 hard yes yes yes
36 * 82801G (ICH7) 0x27da 32 hard yes yes yes
37 * 82801H (ICH8) 0x283e 32 hard yes yes yes
38 * 82801I (ICH9) 0x2930 32 hard yes yes yes
39 * EP80579 (Tolapai) 0x5032 32 hard yes yes yes
40 * ICH10 0x3a30 32 hard yes yes yes
41 * ICH10 0x3a60 32 hard yes yes yes
42 * 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
43 * 6 Series (PCH) 0x1c22 32 hard yes yes yes
44 * Patsburg (PCH) 0x1d22 32 hard yes yes yes
45 * Patsburg (PCH) IDF 0x1d70 32 hard yes yes yes
46 * Patsburg (PCH) IDF 0x1d71 32 hard yes yes yes
47 * Patsburg (PCH) IDF 0x1d72 32 hard yes yes yes
48 * DH89xxCC (PCH) 0x2330 32 hard yes yes yes
49 * Panther Point (PCH) 0x1e22 32 hard yes yes yes
50 * Lynx Point (PCH) 0x8c22 32 hard yes yes yes
51 * Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
52 * Avoton (SOC) 0x1f3c 32 hard yes yes yes
53 * Wellsburg (PCH) 0x8d22 32 hard yes yes yes
54 * Wellsburg (PCH) MS 0x8d7d 32 hard yes yes yes
55 * Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes
56 * Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes
57 * Coleto Creek (PCH) 0x23b0 32 hard yes yes yes
Jean Delvareb299de82014-07-17 15:04:41 +020058 * Wildcat Point (PCH) 0x8ca2 32 hard yes yes yes
Jean Delvarece316112014-07-17 15:03:24 +020059 * Wildcat Point-LP (PCH) 0x9ca2 32 hard yes yes yes
60 * BayTrail (SOC) 0x0f12 32 hard yes yes yes
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -070061 * Sunrise Point-H (PCH) 0xa123 32 hard yes yes yes
Jean Delvarece316112014-07-17 15:03:24 +020062 *
63 * Features supported by this driver:
64 * Software PEC no
65 * Hardware PEC yes
66 * Block buffer yes
67 * Block process call transaction no
68 * I2C block read transaction yes (doesn't use the block buffer)
69 * Slave mode no
70 * Interrupt processing yes
71 *
72 * See the file Documentation/i2c/busses/i2c-i801 for details.
73 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Daniel Kurtz636752b2012-07-24 14:13:58 +020075#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/module.h>
77#include <linux/pci.h>
78#include <linux/kernel.h>
79#include <linux/stddef.h>
80#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/ioport.h>
82#include <linux/init.h>
83#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020084#include <linux/acpi.h>
Jean Delvare1561bfe2009-01-07 14:29:17 +010085#include <linux/io.h>
Hans de Goedefa5bfab2009-03-30 21:46:44 +020086#include <linux/dmi.h>
Ben Hutchings665a96b2011-01-10 22:11:22 +010087#include <linux/slab.h>
Daniel Kurtz636752b2012-07-24 14:13:58 +020088#include <linux/wait.h>
Jean Delvare3ad7ea12012-10-05 22:23:53 +020089#include <linux/err.h>
90
Jean Delvare79e3e5b2012-10-28 21:37:01 +010091#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
92 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +020093#include <linux/gpio.h>
94#include <linux/i2c-mux-gpio.h>
95#include <linux/platform_device.h>
96#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* I801 SMBus address offsets */
David Woodhouse0cd96eb2010-10-31 21:06:59 +010099#define SMBHSTSTS(p) (0 + (p)->smba)
100#define SMBHSTCNT(p) (2 + (p)->smba)
101#define SMBHSTCMD(p) (3 + (p)->smba)
102#define SMBHSTADD(p) (4 + (p)->smba)
103#define SMBHSTDAT0(p) (5 + (p)->smba)
104#define SMBHSTDAT1(p) (6 + (p)->smba)
105#define SMBBLKDAT(p) (7 + (p)->smba)
106#define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
107#define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
108#define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* PCI Address Constants */
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200111#define SMBBAR 4
Daniel Kurtz636752b2012-07-24 14:13:58 +0200112#define SMBPCISTS 0x006
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define SMBHSTCFG 0x040
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Daniel Kurtz636752b2012-07-24 14:13:58 +0200115/* Host status bits for SMBPCISTS */
116#define SMBPCISTS_INTS 0x08
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Host configuration bits for SMBHSTCFG */
119#define SMBHSTCFG_HST_EN 1
120#define SMBHSTCFG_SMB_SMI_EN 2
121#define SMBHSTCFG_I2C_EN 4
122
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300123/* Auxiliary control register bits, ICH4+ only */
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200124#define SMBAUXCTL_CRC 1
125#define SMBAUXCTL_E32B 2
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* Other settings */
Jean Delvare84c1af42012-03-26 21:47:19 +0200128#define MAX_RETRIES 400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* I801 command constants */
131#define I801_QUICK 0x00
132#define I801_BYTE 0x04
133#define I801_BYTE_DATA 0x08
134#define I801_WORD_DATA 0x0C
Jean Delvareae7b0492008-01-27 18:14:49 +0100135#define I801_PROC_CALL 0x10 /* unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define I801_BLOCK_DATA 0x14
Jean Delvare63420642008-01-27 18:14:50 +0100137#define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200138
139/* I801 Host Control register bits */
140#define SMBHSTCNT_INTREN 0x01
141#define SMBHSTCNT_KILL 0x02
142#define SMBHSTCNT_LAST_BYTE 0x20
143#define SMBHSTCNT_START 0x40
144#define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200146/* I801 Hosts Status register bits */
147#define SMBHSTSTS_BYTE_DONE 0x80
148#define SMBHSTSTS_INUSE_STS 0x40
149#define SMBHSTSTS_SMBALERT_STS 0x20
150#define SMBHSTSTS_FAILED 0x10
151#define SMBHSTSTS_BUS_ERR 0x08
152#define SMBHSTSTS_DEV_ERR 0x04
153#define SMBHSTSTS_INTR 0x02
154#define SMBHSTSTS_HOST_BUSY 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Daniel Kurtz70a1cc12012-07-24 14:13:58 +0200156#define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
157 SMBHSTSTS_DEV_ERR)
158
159#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
160 STATUS_ERROR_FLAGS)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200161
Jean Delvarea6e5e2b2011-05-01 18:18:49 +0200162/* Older devices have their ID defined in <linux/pci_ids.h> */
Jean Delvarece316112014-07-17 15:03:24 +0200163#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
Alan Cox39e8e302014-08-19 17:37:28 +0300164#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
Jean Delvarece316112014-07-17 15:03:24 +0200165#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
166#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
David Woodhouse55fee8d2010-10-31 21:07:00 +0100167/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
Jean Delvarece316112014-07-17 15:03:24 +0200168#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
169#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
170#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
171#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
172#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
173#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
174#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
175#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
176#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
Jean Delvareb299de82014-07-17 15:04:41 +0200177#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
Jean Delvarece316112014-07-17 15:03:24 +0200178#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
179#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0 0x8d7d
180#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
181#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
182#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
James Ralstonafc65922013-11-04 09:29:48 -0800183#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -0700184#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
David Woodhouse55fee8d2010-10-31 21:07:00 +0100185
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200186struct i801_mux_config {
187 char *gpio_chip;
188 unsigned values[3];
189 int n_values;
190 unsigned classes[3];
191 unsigned gpios[2]; /* Relative to gpio_chip->base */
192 int n_gpios;
193};
194
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100195struct i801_priv {
196 struct i2c_adapter adapter;
197 unsigned long smba;
198 unsigned char original_hstcfg;
199 struct pci_dev *pci_dev;
200 unsigned int features;
Daniel Kurtz636752b2012-07-24 14:13:58 +0200201
202 /* isr processing */
203 wait_queue_head_t waitq;
204 u8 status;
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200205
206 /* Command state used by isr for byte-by-byte block transactions */
207 u8 cmd;
208 bool is_read;
209 int count;
210 int len;
211 u8 *data;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200212
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100213#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
214 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200215 const struct i801_mux_config *mux_drvdata;
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200216 struct platform_device *mux_pdev;
217#endif
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100218};
219
Jean Delvared6072f82005-09-25 16:37:04 +0200220static struct pci_driver i801_driver;
Jean Delvare369f6f42008-01-27 18:14:50 +0100221
222#define FEATURE_SMBUS_PEC (1 << 0)
223#define FEATURE_BLOCK_BUFFER (1 << 1)
224#define FEATURE_BLOCK_PROC (1 << 2)
225#define FEATURE_I2C_BLOCK_READ (1 << 3)
Daniel Kurtz636752b2012-07-24 14:13:58 +0200226#define FEATURE_IRQ (1 << 4)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200227/* Not really a feature, but it's convenient to handle it as such */
228#define FEATURE_IDF (1 << 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jean Delvareadff6872010-05-21 18:40:54 +0200230static const char *i801_feature_names[] = {
231 "SMBus PEC",
232 "Block buffer",
233 "Block process call",
234 "I2C block read",
Daniel Kurtz636752b2012-07-24 14:13:58 +0200235 "Interrupt",
Jean Delvareadff6872010-05-21 18:40:54 +0200236};
237
238static unsigned int disable_features;
239module_param(disable_features, uint, S_IRUGO | S_IWUSR);
Jean Delvare53229342013-05-15 02:44:10 +0000240MODULE_PARM_DESC(disable_features, "Disable selected driver features:\n"
241 "\t\t 0x01 disable SMBus PEC\n"
242 "\t\t 0x02 disable the block buffer\n"
243 "\t\t 0x08 disable the I2C block read functionality\n"
244 "\t\t 0x10 don't use interrupts ");
Jean Delvareadff6872010-05-21 18:40:54 +0200245
Jean Delvarecf898dc2008-07-14 22:38:33 +0200246/* Make sure the SMBus host is ready to start transmitting.
247 Return 0 if it is, -EBUSY if it is not. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100248static int i801_check_pre(struct i801_priv *priv)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200249{
250 int status;
251
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100252 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200253 if (status & SMBHSTSTS_HOST_BUSY) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100254 dev_err(&priv->pci_dev->dev, "SMBus is busy, can't use it!\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200255 return -EBUSY;
256 }
257
258 status &= STATUS_FLAGS;
259 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100260 dev_dbg(&priv->pci_dev->dev, "Clearing status flags (%02x)\n",
Jean Delvarecf898dc2008-07-14 22:38:33 +0200261 status);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100262 outb_p(status, SMBHSTSTS(priv));
263 status = inb_p(SMBHSTSTS(priv)) & STATUS_FLAGS;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200264 if (status) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100265 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200266 "Failed clearing status flags (%02x)\n",
267 status);
268 return -EBUSY;
269 }
270 }
271
272 return 0;
273}
274
Jean Delvare6cad93c2012-07-24 14:13:58 +0200275/*
276 * Convert the status register to an error code, and clear it.
277 * Note that status only contains the bits we want to clear, not the
278 * actual register value.
279 */
280static int i801_check_post(struct i801_priv *priv, int status)
Jean Delvarecf898dc2008-07-14 22:38:33 +0200281{
282 int result = 0;
283
Daniel Kurtz636752b2012-07-24 14:13:58 +0200284 /*
285 * If the SMBus is still busy, we give up
286 * Note: This timeout condition only happens when using polling
287 * transactions. For interrupt operation, NAK/timeout is indicated by
288 * DEV_ERR.
289 */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200290 if (unlikely(status < 0)) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100291 dev_err(&priv->pci_dev->dev, "Transaction timeout\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200292 /* try to stop the current command */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100293 dev_dbg(&priv->pci_dev->dev, "Terminating the current operation\n");
294 outb_p(inb_p(SMBHSTCNT(priv)) | SMBHSTCNT_KILL,
295 SMBHSTCNT(priv));
Jean Delvare84c1af42012-03-26 21:47:19 +0200296 usleep_range(1000, 2000);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100297 outb_p(inb_p(SMBHSTCNT(priv)) & (~SMBHSTCNT_KILL),
298 SMBHSTCNT(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200299
300 /* Check if it worked */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100301 status = inb_p(SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200302 if ((status & SMBHSTSTS_HOST_BUSY) ||
303 !(status & SMBHSTSTS_FAILED))
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100304 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200305 "Failed terminating the transaction\n");
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100306 outb_p(STATUS_FLAGS, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200307 return -ETIMEDOUT;
308 }
309
310 if (status & SMBHSTSTS_FAILED) {
311 result = -EIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100312 dev_err(&priv->pci_dev->dev, "Transaction failed\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200313 }
314 if (status & SMBHSTSTS_DEV_ERR) {
315 result = -ENXIO;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100316 dev_dbg(&priv->pci_dev->dev, "No response\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200317 }
318 if (status & SMBHSTSTS_BUS_ERR) {
319 result = -EAGAIN;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100320 dev_dbg(&priv->pci_dev->dev, "Lost arbitration\n");
Jean Delvarecf898dc2008-07-14 22:38:33 +0200321 }
322
Jean Delvare6cad93c2012-07-24 14:13:58 +0200323 /* Clear status flags except BYTE_DONE, to be cleared by caller */
324 outb_p(status, SMBHSTSTS(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200325
326 return result;
327}
328
Jean Delvare6cad93c2012-07-24 14:13:58 +0200329/* Wait for BUSY being cleared and either INTR or an error flag being set */
330static int i801_wait_intr(struct i801_priv *priv)
331{
332 int timeout = 0;
333 int status;
334
335 /* We will always wait for a fraction of a second! */
336 do {
337 usleep_range(250, 500);
338 status = inb_p(SMBHSTSTS(priv));
339 } while (((status & SMBHSTSTS_HOST_BUSY) ||
340 !(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR))) &&
341 (timeout++ < MAX_RETRIES));
342
343 if (timeout > MAX_RETRIES) {
344 dev_dbg(&priv->pci_dev->dev, "INTR Timeout!\n");
345 return -ETIMEDOUT;
346 }
347 return status & (STATUS_ERROR_FLAGS | SMBHSTSTS_INTR);
348}
349
350/* Wait for either BYTE_DONE or an error flag being set */
351static int i801_wait_byte_done(struct i801_priv *priv)
352{
353 int timeout = 0;
354 int status;
355
356 /* We will always wait for a fraction of a second! */
357 do {
358 usleep_range(250, 500);
359 status = inb_p(SMBHSTSTS(priv));
360 } while (!(status & (STATUS_ERROR_FLAGS | SMBHSTSTS_BYTE_DONE)) &&
361 (timeout++ < MAX_RETRIES));
362
363 if (timeout > MAX_RETRIES) {
364 dev_dbg(&priv->pci_dev->dev, "BYTE_DONE Timeout!\n");
365 return -ETIMEDOUT;
366 }
367 return status & STATUS_ERROR_FLAGS;
368}
369
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100370static int i801_transaction(struct i801_priv *priv, int xact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Jean Delvare2b738092008-07-14 22:38:32 +0200372 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200373 int result;
Jean Delvareb3b8df92014-11-12 10:20:40 +0100374 const struct i2c_adapter *adap = &priv->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100376 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200377 if (result < 0)
378 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Daniel Kurtz636752b2012-07-24 14:13:58 +0200380 if (priv->features & FEATURE_IRQ) {
381 outb_p(xact | SMBHSTCNT_INTREN | SMBHSTCNT_START,
382 SMBHSTCNT(priv));
Jean Delvareb3b8df92014-11-12 10:20:40 +0100383 result = wait_event_timeout(priv->waitq,
384 (status = priv->status),
385 adap->timeout);
386 if (!result) {
387 status = -ETIMEDOUT;
388 dev_warn(&priv->pci_dev->dev,
389 "Timeout waiting for interrupt!\n");
390 }
Daniel Kurtz636752b2012-07-24 14:13:58 +0200391 priv->status = 0;
392 return i801_check_post(priv, status);
393 }
394
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200395 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
Daniel Kurtz37af8712012-07-24 14:13:58 +0200396 * SMBSCMD are passed in xact */
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200397 outb_p(xact | SMBHSTCNT_START, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Jean Delvare6cad93c2012-07-24 14:13:58 +0200399 status = i801_wait_intr(priv);
400 return i801_check_post(priv, status);
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200401}
402
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100403static int i801_block_transaction_by_block(struct i801_priv *priv,
404 union i2c_smbus_data *data,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200405 char read_write, int hwpec)
406{
407 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200408 int status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200409
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100410 inb_p(SMBHSTCNT(priv)); /* reset the data buffer index */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200411
412 /* Use 32-byte buffer to process this transaction */
413 if (read_write == I2C_SMBUS_WRITE) {
414 len = data->block[0];
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100415 outb_p(len, SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200416 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100417 outb_p(data->block[i+1], SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200418 }
419
Daniel Kurtz37af8712012-07-24 14:13:58 +0200420 status = i801_transaction(priv, I801_BLOCK_DATA |
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200421 (hwpec ? SMBHSTCNT_PEC_EN : 0));
David Brownell97140342008-07-14 22:38:25 +0200422 if (status)
423 return status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200424
425 if (read_write == I2C_SMBUS_READ) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100426 len = inb_p(SMBHSTDAT0(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200427 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
David Brownell97140342008-07-14 22:38:25 +0200428 return -EPROTO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200429
430 data->block[0] = len;
431 for (i = 0; i < len; i++)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100432 data->block[i + 1] = inb_p(SMBBLKDAT(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200433 }
434 return 0;
435}
436
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200437static void i801_isr_byte_done(struct i801_priv *priv)
438{
439 if (priv->is_read) {
440 /* For SMBus block reads, length is received with first byte */
441 if (((priv->cmd & 0x1c) == I801_BLOCK_DATA) &&
442 (priv->count == 0)) {
443 priv->len = inb_p(SMBHSTDAT0(priv));
444 if (priv->len < 1 || priv->len > I2C_SMBUS_BLOCK_MAX) {
445 dev_err(&priv->pci_dev->dev,
446 "Illegal SMBus block read size %d\n",
447 priv->len);
448 /* FIXME: Recover */
449 priv->len = I2C_SMBUS_BLOCK_MAX;
450 } else {
451 dev_dbg(&priv->pci_dev->dev,
452 "SMBus block read size is %d\n",
453 priv->len);
454 }
455 priv->data[-1] = priv->len;
456 }
457
458 /* Read next byte */
459 if (priv->count < priv->len)
460 priv->data[priv->count++] = inb(SMBBLKDAT(priv));
461 else
462 dev_dbg(&priv->pci_dev->dev,
463 "Discarding extra byte on block read\n");
464
465 /* Set LAST_BYTE for last byte of read transaction */
466 if (priv->count == priv->len - 1)
467 outb_p(priv->cmd | SMBHSTCNT_LAST_BYTE,
468 SMBHSTCNT(priv));
469 } else if (priv->count < priv->len - 1) {
470 /* Write next byte, except for IRQ after last byte */
471 outb_p(priv->data[++priv->count], SMBBLKDAT(priv));
472 }
473
474 /* Clear BYTE_DONE to continue with next byte */
475 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
476}
477
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200478/*
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200479 * There are two kinds of interrupts:
480 *
481 * 1) i801 signals transaction completion with one of these interrupts:
482 * INTR - Success
483 * DEV_ERR - Invalid command, NAK or communication timeout
484 * BUS_ERR - SMI# transaction collision
485 * FAILED - transaction was canceled due to a KILL request
486 * When any of these occur, update ->status and wake up the waitq.
487 * ->status must be cleared before kicking off the next transaction.
488 *
489 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
490 * occurs for each byte of a byte-by-byte to prepare the next byte.
Daniel Kurtz636752b2012-07-24 14:13:58 +0200491 */
492static irqreturn_t i801_isr(int irq, void *dev_id)
493{
494 struct i801_priv *priv = dev_id;
495 u16 pcists;
496 u8 status;
497
498 /* Confirm this is our interrupt */
499 pci_read_config_word(priv->pci_dev, SMBPCISTS, &pcists);
500 if (!(pcists & SMBPCISTS_INTS))
501 return IRQ_NONE;
502
503 status = inb_p(SMBHSTSTS(priv));
504 if (status != 0x42)
505 dev_dbg(&priv->pci_dev->dev, "irq: status = %02x\n", status);
506
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200507 if (status & SMBHSTSTS_BYTE_DONE)
508 i801_isr_byte_done(priv);
509
Daniel Kurtz636752b2012-07-24 14:13:58 +0200510 /*
511 * Clear irq sources and report transaction result.
512 * ->status must be cleared before the next transaction is started.
513 */
514 status &= SMBHSTSTS_INTR | STATUS_ERROR_FLAGS;
515 if (status) {
516 outb_p(status, SMBHSTSTS(priv));
517 priv->status |= status;
518 wake_up(&priv->waitq);
519 }
520
521 return IRQ_HANDLED;
522}
523
524/*
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200525 * For "byte-by-byte" block transactions:
526 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
527 * I2C read uses cmd=I801_I2C_BLOCK_DATA
528 */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100529static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
530 union i2c_smbus_data *data,
Jean Delvare63420642008-01-27 18:14:50 +0100531 char read_write, int command,
532 int hwpec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int i, len;
535 int smbcmd;
Jean Delvare2b738092008-07-14 22:38:32 +0200536 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200537 int result;
Jean Delvareb3b8df92014-11-12 10:20:40 +0100538 const struct i2c_adapter *adap = &priv->adapter;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200539
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100540 result = i801_check_pre(priv);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200541 if (result < 0)
542 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200544 len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100547 outb_p(len, SMBHSTDAT0(priv));
548 outb_p(data->block[1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200551 if (command == I2C_SMBUS_I2C_BLOCK_DATA &&
552 read_write == I2C_SMBUS_READ)
553 smbcmd = I801_I2C_BLOCK_DATA;
554 else
555 smbcmd = I801_BLOCK_DATA;
556
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200557 if (priv->features & FEATURE_IRQ) {
558 priv->is_read = (read_write == I2C_SMBUS_READ);
559 if (len == 1 && priv->is_read)
560 smbcmd |= SMBHSTCNT_LAST_BYTE;
561 priv->cmd = smbcmd | SMBHSTCNT_INTREN;
562 priv->len = len;
563 priv->count = 0;
564 priv->data = &data->block[1];
565
566 outb_p(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
Jean Delvareb3b8df92014-11-12 10:20:40 +0100567 result = wait_event_timeout(priv->waitq,
568 (status = priv->status),
569 adap->timeout);
570 if (!result) {
571 status = -ETIMEDOUT;
572 dev_warn(&priv->pci_dev->dev,
573 "Timeout waiting for interrupt!\n");
574 }
Daniel Kurtzd3ff6ce2012-07-24 14:13:59 +0200575 priv->status = 0;
576 return i801_check_post(priv, status);
577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 for (i = 1; i <= len; i++) {
Daniel Kurtzefa3cb12012-07-24 14:13:57 +0200580 if (i == len && read_write == I2C_SMBUS_READ)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200581 smbcmd |= SMBHSTCNT_LAST_BYTE;
Daniel Kurtz37af8712012-07-24 14:13:58 +0200582 outb_p(smbcmd, SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (i == 1)
Daniel Kurtzedbeea62012-07-24 14:13:58 +0200585 outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100586 SMBHSTCNT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Jean Delvare6cad93c2012-07-24 14:13:58 +0200588 status = i801_wait_byte_done(priv);
589 if (status)
590 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Jean Delvare63420642008-01-27 18:14:50 +0100592 if (i == 1 && read_write == I2C_SMBUS_READ
593 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100594 len = inb_p(SMBHSTDAT0(priv));
Jean Delvarecf898dc2008-07-14 22:38:33 +0200595 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100596 dev_err(&priv->pci_dev->dev,
Jean Delvarecf898dc2008-07-14 22:38:33 +0200597 "Illegal SMBus block read size %d\n",
598 len);
599 /* Recover */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100600 while (inb_p(SMBHSTSTS(priv)) &
601 SMBHSTSTS_HOST_BUSY)
602 outb_p(SMBHSTSTS_BYTE_DONE,
603 SMBHSTSTS(priv));
604 outb_p(SMBHSTSTS_INTR, SMBHSTSTS(priv));
David Brownell97140342008-07-14 22:38:25 +0200605 return -EPROTO;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 data->block[0] = len;
608 }
609
610 /* Retrieve/store value in SMBBLKDAT */
611 if (read_write == I2C_SMBUS_READ)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100612 data->block[i] = inb_p(SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100614 outb_p(data->block[i+1], SMBBLKDAT(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Jean Delvarecf898dc2008-07-14 22:38:33 +0200616 /* signals SMBBLKDAT ready */
Jean Delvare6cad93c2012-07-24 14:13:58 +0200617 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS(priv));
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200618 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200619
Jean Delvare6cad93c2012-07-24 14:13:58 +0200620 status = i801_wait_intr(priv);
621exit:
622 return i801_check_post(priv, status);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200623}
624
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100625static int i801_set_block_buffer_mode(struct i801_priv *priv)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200626{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100627 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_E32B, SMBAUXCTL(priv));
628 if ((inb_p(SMBAUXCTL(priv)) & SMBAUXCTL_E32B) == 0)
David Brownell97140342008-07-14 22:38:25 +0200629 return -EIO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200630 return 0;
631}
632
633/* Block transaction function */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100634static int i801_block_transaction(struct i801_priv *priv,
635 union i2c_smbus_data *data, char read_write,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200636 int command, int hwpec)
637{
638 int result = 0;
639 unsigned char hostc;
640
641 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
642 if (read_write == I2C_SMBUS_WRITE) {
643 /* set I2C_EN bit in configuration register */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100644 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &hostc);
645 pci_write_config_byte(priv->pci_dev, SMBHSTCFG,
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200646 hostc | SMBHSTCFG_I2C_EN);
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100647 } else if (!(priv->features & FEATURE_I2C_BLOCK_READ)) {
648 dev_err(&priv->pci_dev->dev,
Jean Delvare63420642008-01-27 18:14:50 +0100649 "I2C block read is unsupported!\n");
David Brownell97140342008-07-14 22:38:25 +0200650 return -EOPNOTSUPP;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Jean Delvare63420642008-01-27 18:14:50 +0100654 if (read_write == I2C_SMBUS_WRITE
655 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200656 if (data->block[0] < 1)
657 data->block[0] = 1;
658 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
659 data->block[0] = I2C_SMBUS_BLOCK_MAX;
660 } else {
Jean Delvare63420642008-01-27 18:14:50 +0100661 data->block[0] = 32; /* max for SMBus block reads */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200662 }
663
Jean Delvarec074c392010-03-13 20:56:53 +0100664 /* Experience has shown that the block buffer can only be used for
665 SMBus (not I2C) block transactions, even though the datasheet
666 doesn't mention this limitation. */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100667 if ((priv->features & FEATURE_BLOCK_BUFFER)
Jean Delvarec074c392010-03-13 20:56:53 +0100668 && command != I2C_SMBUS_I2C_BLOCK_DATA
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100669 && i801_set_block_buffer_mode(priv) == 0)
670 result = i801_block_transaction_by_block(priv, data,
671 read_write, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200672 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100673 result = i801_block_transaction_byte_by_byte(priv, data,
674 read_write,
Jean Delvare63420642008-01-27 18:14:50 +0100675 command, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200676
Jean Delvare63420642008-01-27 18:14:50 +0100677 if (command == I2C_SMBUS_I2C_BLOCK_DATA
678 && read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* restore saved configuration register value */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100680 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, hostc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 return result;
683}
684
David Brownell97140342008-07-14 22:38:25 +0200685/* Return negative errno on error. */
Ivo Manca3fb21c62010-05-21 18:40:55 +0200686static s32 i801_access(struct i2c_adapter *adap, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned short flags, char read_write, u8 command,
Ivo Manca3fb21c62010-05-21 18:40:55 +0200688 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200690 int hwpec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int block = 0;
692 int ret, xact = 0;
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100693 struct i801_priv *priv = i2c_get_adapdata(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100695 hwpec = (priv->features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200696 && size != I2C_SMBUS_QUICK
697 && size != I2C_SMBUS_I2C_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 switch (size) {
700 case I2C_SMBUS_QUICK:
701 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100702 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xact = I801_QUICK;
704 break;
705 case I2C_SMBUS_BYTE:
706 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100707 SMBHSTADD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100709 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 xact = I801_BYTE;
711 break;
712 case I2C_SMBUS_BYTE_DATA:
713 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100714 SMBHSTADD(priv));
715 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (read_write == I2C_SMBUS_WRITE)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100717 outb_p(data->byte, SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 xact = I801_BYTE_DATA;
719 break;
720 case I2C_SMBUS_WORD_DATA:
721 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100722 SMBHSTADD(priv));
723 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (read_write == I2C_SMBUS_WRITE) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100725 outb_p(data->word & 0xff, SMBHSTDAT0(priv));
726 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 xact = I801_WORD_DATA;
729 break;
730 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100732 SMBHSTADD(priv));
733 outb_p(command, SMBHSTCMD(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 block = 1;
735 break;
Jean Delvare63420642008-01-27 18:14:50 +0100736 case I2C_SMBUS_I2C_BLOCK_DATA:
737 /* NB: page 240 of ICH5 datasheet shows that the R/#W
738 * bit should be cleared here, even when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100739 outb_p((addr & 0x7f) << 1, SMBHSTADD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100740 if (read_write == I2C_SMBUS_READ) {
741 /* NB: page 240 of ICH5 datasheet also shows
742 * that DATA1 is the cmd field when reading */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100743 outb_p(command, SMBHSTDAT1(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100744 } else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100745 outb_p(command, SMBHSTCMD(priv));
Jean Delvare63420642008-01-27 18:14:50 +0100746 block = 1;
747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100749 dev_err(&priv->pci_dev->dev, "Unsupported transaction %d\n",
750 size);
David Brownell97140342008-07-14 22:38:25 +0200751 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200754 if (hwpec) /* enable/disable hardware PEC */
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100755 outb_p(inb_p(SMBAUXCTL(priv)) | SMBAUXCTL_CRC, SMBAUXCTL(priv));
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200756 else
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100757 outb_p(inb_p(SMBAUXCTL(priv)) & (~SMBAUXCTL_CRC),
758 SMBAUXCTL(priv));
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200759
Ivo Manca3fb21c62010-05-21 18:40:55 +0200760 if (block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100761 ret = i801_block_transaction(priv, data, read_write, size,
762 hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200763 else
Daniel Kurtz37af8712012-07-24 14:13:58 +0200764 ret = i801_transaction(priv, xact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jean Delvarec79cfba2006-04-20 02:43:18 -0700766 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200767 time, so we forcibly disable it after every transaction. Turn off
768 E32B for the same reason. */
Jean Delvarea0921b62008-01-27 18:14:50 +0100769 if (hwpec || block)
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100770 outb_p(inb_p(SMBAUXCTL(priv)) &
771 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarec79cfba2006-04-20 02:43:18 -0700772
Ivo Manca3fb21c62010-05-21 18:40:55 +0200773 if (block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return ret;
Ivo Manca3fb21c62010-05-21 18:40:55 +0200775 if (ret)
David Brownell97140342008-07-14 22:38:25 +0200776 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
778 return 0;
779
780 switch (xact & 0x7f) {
781 case I801_BYTE: /* Result put in SMBHSTDAT0 */
782 case I801_BYTE_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100783 data->byte = inb_p(SMBHSTDAT0(priv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 case I801_WORD_DATA:
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100786 data->word = inb_p(SMBHSTDAT0(priv)) +
787 (inb_p(SMBHSTDAT1(priv)) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 }
790 return 0;
791}
792
793
794static u32 i801_func(struct i2c_adapter *adapter)
795{
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100796 struct i801_priv *priv = i2c_get_adapdata(adapter);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare369f6f42008-01-27 18:14:50 +0100799 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
800 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
David Woodhouse0cd96eb2010-10-31 21:06:59 +0100801 ((priv->features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
802 ((priv->features & FEATURE_I2C_BLOCK_READ) ?
Jean Delvare63420642008-01-27 18:14:50 +0100803 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Jean Delvare8f9082c2006-09-03 22:39:46 +0200806static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .smbus_xfer = i801_access,
808 .functionality = i801_func,
809};
810
Jingoo Han392debf2013-12-03 08:11:20 +0900811static const struct pci_device_id i801_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
813 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
814 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
815 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
816 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
817 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
818 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
819 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
820 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
Jason Gastonb0a70b52005-04-16 15:24:45 -0700821 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
Jason Gaston8254fc42006-01-09 10:58:08 -0800822 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
Jason Gastonadbc2a12006-11-22 15:19:12 -0800823 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700824 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EP80579_1) },
Gaston, Jason Dd28dc712008-02-24 20:03:42 +0100825 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
826 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
Seth Heasleycb04e952010-10-04 13:27:14 -0700827 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS) },
828 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS) },
Seth Heasleye30d9852010-10-31 21:06:59 +0100829 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS) },
David Woodhouse55fee8d2010-10-31 21:07:00 +0100830 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0) },
831 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1) },
832 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2) },
Seth Heasley662cda82011-03-20 14:50:53 +0100833 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS) },
Seth Heasley6e2a8512011-05-24 20:58:49 +0200834 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS) },
Seth Heasley062737f2012-03-26 21:47:19 +0200835 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS) },
James Ralston4a8f1dd2012-09-10 10:14:02 +0200836 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS) },
Seth Heasleyc2db409c2013-01-30 15:25:32 +0000837 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMBUS) },
James Ralstona3fc0ff2013-02-14 09:15:33 +0000838 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS) },
839 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0) },
840 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
841 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
Seth Heasleyf39901c2013-06-19 16:59:57 -0700842 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
Jean Delvareb299de82014-07-17 15:04:41 +0200843 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
James Ralstonafc65922013-11-04 09:29:48 -0800844 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
Chew, Kean ho1b31e9b2014-03-01 00:03:56 +0800845 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
Alan Cox39e8e302014-08-19 17:37:28 +0300846 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
james.d.ralston@intel.com3e27a842014-10-13 15:20:24 -0700847 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 { 0, }
849};
850
Ivo Manca3fb21c62010-05-21 18:40:55 +0200851MODULE_DEVICE_TABLE(pci, i801_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Jean Delvare8eacfce2011-05-24 20:58:49 +0200853#if defined CONFIG_X86 && defined CONFIG_DMI
Jean Delvare1561bfe2009-01-07 14:29:17 +0100854static unsigned char apanel_addr;
855
856/* Scan the system ROM for the signature "FJKEYINF" */
857static __init const void __iomem *bios_signature(const void __iomem *bios)
858{
859 ssize_t offset;
860 const unsigned char signature[] = "FJKEYINF";
861
862 for (offset = 0; offset < 0x10000; offset += 0x10) {
863 if (check_signature(bios + offset, signature,
864 sizeof(signature)-1))
865 return bios + offset;
866 }
867 return NULL;
868}
869
870static void __init input_apanel_init(void)
871{
872 void __iomem *bios;
873 const void __iomem *p;
874
875 bios = ioremap(0xF0000, 0x10000); /* Can't fail */
876 p = bios_signature(bios);
877 if (p) {
878 /* just use the first address */
879 apanel_addr = readb(p + 8 + 3) >> 1;
880 }
881 iounmap(bios);
882}
Jean Delvare1561bfe2009-01-07 14:29:17 +0100883
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200884struct dmi_onboard_device_info {
885 const char *name;
886 u8 type;
887 unsigned short i2c_addr;
888 const char *i2c_type;
889};
890
Bill Pemberton0b255e92012-11-27 15:59:38 -0500891static const struct dmi_onboard_device_info dmi_devices[] = {
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200892 { "Syleus", DMI_DEV_TYPE_OTHER, 0x73, "fscsyl" },
893 { "Hermes", DMI_DEV_TYPE_OTHER, 0x73, "fscher" },
894 { "Hades", DMI_DEV_TYPE_OTHER, 0x73, "fschds" },
895};
896
Bill Pemberton0b255e92012-11-27 15:59:38 -0500897static void dmi_check_onboard_device(u8 type, const char *name,
898 struct i2c_adapter *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200899{
900 int i;
901 struct i2c_board_info info;
902
903 for (i = 0; i < ARRAY_SIZE(dmi_devices); i++) {
904 /* & ~0x80, ignore enabled/disabled bit */
905 if ((type & ~0x80) != dmi_devices[i].type)
906 continue;
Jean Delvarefaabd472010-07-09 16:22:51 +0200907 if (strcasecmp(name, dmi_devices[i].name))
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200908 continue;
909
910 memset(&info, 0, sizeof(struct i2c_board_info));
911 info.addr = dmi_devices[i].i2c_addr;
912 strlcpy(info.type, dmi_devices[i].i2c_type, I2C_NAME_SIZE);
913 i2c_new_device(adap, &info);
914 break;
915 }
916}
917
918/* We use our own function to check for onboard devices instead of
919 dmi_find_device() as some buggy BIOS's have the devices we are interested
920 in marked as disabled */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500921static void dmi_check_onboard_devices(const struct dmi_header *dm, void *adap)
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200922{
923 int i, count;
924
925 if (dm->type != 10)
926 return;
927
928 count = (dm->length - sizeof(struct dmi_header)) / 2;
929 for (i = 0; i < count; i++) {
930 const u8 *d = (char *)(dm + 1) + (i * 2);
931 const char *name = ((char *) dm) + dm->length;
932 u8 type = d[0];
933 u8 s = d[1];
934
935 if (!s)
936 continue;
937 s--;
938 while (s > 0 && name[0]) {
939 name += strlen(name) + 1;
940 s--;
941 }
942 if (name[0] == 0) /* Bogus string reference */
943 continue;
944
945 dmi_check_onboard_device(type, name, adap);
946 }
947}
Hans de Goedefa5bfab2009-03-30 21:46:44 +0200948
Jean Delvaree7198fb2011-05-24 20:58:49 +0200949/* Register optional slaves */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500950static void i801_probe_optional_slaves(struct i801_priv *priv)
Jean Delvaree7198fb2011-05-24 20:58:49 +0200951{
952 /* Only register slaves on main SMBus channel */
953 if (priv->features & FEATURE_IDF)
954 return;
955
Jean Delvaree7198fb2011-05-24 20:58:49 +0200956 if (apanel_addr) {
957 struct i2c_board_info info;
958
959 memset(&info, 0, sizeof(struct i2c_board_info));
960 info.addr = apanel_addr;
961 strlcpy(info.type, "fujitsu_apanel", I2C_NAME_SIZE);
962 i2c_new_device(&priv->adapter, &info);
963 }
Jean Delvare8eacfce2011-05-24 20:58:49 +0200964
Jean Delvaree7198fb2011-05-24 20:58:49 +0200965 if (dmi_name_in_vendors("FUJITSU"))
966 dmi_walk(dmi_check_onboard_devices, &priv->adapter);
Jean Delvaree7198fb2011-05-24 20:58:49 +0200967}
Jean Delvare8eacfce2011-05-24 20:58:49 +0200968#else
969static void __init input_apanel_init(void) {}
Bill Pemberton0b255e92012-11-27 15:59:38 -0500970static void i801_probe_optional_slaves(struct i801_priv *priv) {}
Jean Delvare8eacfce2011-05-24 20:58:49 +0200971#endif /* CONFIG_X86 && CONFIG_DMI */
Jean Delvaree7198fb2011-05-24 20:58:49 +0200972
Jean Delvare79e3e5b2012-10-28 21:37:01 +0100973#if (defined CONFIG_I2C_MUX_GPIO || defined CONFIG_I2C_MUX_GPIO_MODULE) && \
974 defined CONFIG_DMI
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200975static struct i801_mux_config i801_mux_config_asus_z8_d12 = {
976 .gpio_chip = "gpio_ich",
977 .values = { 0x02, 0x03 },
978 .n_values = 2,
979 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD },
980 .gpios = { 52, 53 },
981 .n_gpios = 2,
982};
983
984static struct i801_mux_config i801_mux_config_asus_z8_d18 = {
985 .gpio_chip = "gpio_ich",
986 .values = { 0x02, 0x03, 0x01 },
987 .n_values = 3,
988 .classes = { I2C_CLASS_SPD, I2C_CLASS_SPD, I2C_CLASS_SPD },
989 .gpios = { 52, 53 },
990 .n_gpios = 2,
991};
992
Bill Pemberton0b255e92012-11-27 15:59:38 -0500993static const struct dmi_system_id mux_dmi_table[] = {
Jean Delvare3ad7ea12012-10-05 22:23:53 +0200994 {
995 .matches = {
996 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
997 DMI_MATCH(DMI_BOARD_NAME, "Z8NA-D6(C)"),
998 },
999 .driver_data = &i801_mux_config_asus_z8_d12,
1000 },
1001 {
1002 .matches = {
1003 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1004 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)E-D12(X)"),
1005 },
1006 .driver_data = &i801_mux_config_asus_z8_d12,
1007 },
1008 {
1009 .matches = {
1010 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1011 DMI_MATCH(DMI_BOARD_NAME, "Z8NH-D12"),
1012 },
1013 .driver_data = &i801_mux_config_asus_z8_d12,
1014 },
1015 {
1016 .matches = {
1017 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1018 DMI_MATCH(DMI_BOARD_NAME, "Z8PH-D12/IFB"),
1019 },
1020 .driver_data = &i801_mux_config_asus_z8_d12,
1021 },
1022 {
1023 .matches = {
1024 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1025 DMI_MATCH(DMI_BOARD_NAME, "Z8NR-D12"),
1026 },
1027 .driver_data = &i801_mux_config_asus_z8_d12,
1028 },
1029 {
1030 .matches = {
1031 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1032 DMI_MATCH(DMI_BOARD_NAME, "Z8P(N)H-D12"),
1033 },
1034 .driver_data = &i801_mux_config_asus_z8_d12,
1035 },
1036 {
1037 .matches = {
1038 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1039 DMI_MATCH(DMI_BOARD_NAME, "Z8PG-D18"),
1040 },
1041 .driver_data = &i801_mux_config_asus_z8_d18,
1042 },
1043 {
1044 .matches = {
1045 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1046 DMI_MATCH(DMI_BOARD_NAME, "Z8PE-D18"),
1047 },
1048 .driver_data = &i801_mux_config_asus_z8_d18,
1049 },
1050 {
1051 .matches = {
1052 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
1053 DMI_MATCH(DMI_BOARD_NAME, "Z8PS-D12"),
1054 },
1055 .driver_data = &i801_mux_config_asus_z8_d12,
1056 },
1057 { }
1058};
1059
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001060/* Setup multiplexing if needed */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001061static int i801_add_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001062{
1063 struct device *dev = &priv->adapter.dev;
1064 const struct i801_mux_config *mux_config;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001065 struct i2c_mux_gpio_platform_data gpio_data;
Jean Delvaref82b8622012-10-05 22:23:54 +02001066 int err;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001067
1068 if (!priv->mux_drvdata)
1069 return 0;
1070 mux_config = priv->mux_drvdata;
1071
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001072 /* Prepare the platform data */
1073 memset(&gpio_data, 0, sizeof(struct i2c_mux_gpio_platform_data));
1074 gpio_data.parent = priv->adapter.nr;
1075 gpio_data.values = mux_config->values;
1076 gpio_data.n_values = mux_config->n_values;
1077 gpio_data.classes = mux_config->classes;
Jean Delvaref82b8622012-10-05 22:23:54 +02001078 gpio_data.gpio_chip = mux_config->gpio_chip;
1079 gpio_data.gpios = mux_config->gpios;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001080 gpio_data.n_gpios = mux_config->n_gpios;
1081 gpio_data.idle = I2C_MUX_GPIO_NO_IDLE;
1082
1083 /* Register the mux device */
1084 priv->mux_pdev = platform_device_register_data(dev, "i2c-mux-gpio",
Jean Delvaref82b8622012-10-05 22:23:54 +02001085 PLATFORM_DEVID_AUTO, &gpio_data,
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001086 sizeof(struct i2c_mux_gpio_platform_data));
1087 if (IS_ERR(priv->mux_pdev)) {
1088 err = PTR_ERR(priv->mux_pdev);
1089 priv->mux_pdev = NULL;
1090 dev_err(dev, "Failed to register i2c-mux-gpio device\n");
1091 return err;
1092 }
1093
1094 return 0;
1095}
1096
Bill Pemberton0b255e92012-11-27 15:59:38 -05001097static void i801_del_mux(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001098{
1099 if (priv->mux_pdev)
1100 platform_device_unregister(priv->mux_pdev);
1101}
1102
Bill Pemberton0b255e92012-11-27 15:59:38 -05001103static unsigned int i801_get_adapter_class(struct i801_priv *priv)
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001104{
1105 const struct dmi_system_id *id;
1106 const struct i801_mux_config *mux_config;
1107 unsigned int class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
1108 int i;
1109
1110 id = dmi_first_match(mux_dmi_table);
1111 if (id) {
Jean Delvare28901f52012-10-28 21:37:01 +01001112 /* Remove branch classes from trunk */
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001113 mux_config = id->driver_data;
1114 for (i = 0; i < mux_config->n_values; i++)
1115 class &= ~mux_config->classes[i];
1116
1117 /* Remember for later */
1118 priv->mux_drvdata = mux_config;
1119 }
1120
1121 return class;
1122}
1123#else
1124static inline int i801_add_mux(struct i801_priv *priv) { return 0; }
1125static inline void i801_del_mux(struct i801_priv *priv) { }
1126
1127static inline unsigned int i801_get_adapter_class(struct i801_priv *priv)
1128{
1129 return I2C_CLASS_HWMON | I2C_CLASS_SPD;
1130}
1131#endif
1132
Bill Pemberton0b255e92012-11-27 15:59:38 -05001133static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001135 unsigned char temp;
Jean Delvareadff6872010-05-21 18:40:54 +02001136 int err, i;
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001137 struct i801_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001139 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1140 if (!priv)
1141 return -ENOMEM;
1142
1143 i2c_set_adapdata(&priv->adapter, priv);
1144 priv->adapter.owner = THIS_MODULE;
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001145 priv->adapter.class = i801_get_adapter_class(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001146 priv->adapter.algo = &smbus_algorithm;
1147
1148 priv->pci_dev = dev;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001149 switch (dev->device) {
Jean Delvaree7198fb2011-05-24 20:58:49 +02001150 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0:
1151 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1:
1152 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2:
James Ralstona3fc0ff2013-02-14 09:15:33 +00001153 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS0:
1154 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1:
1155 case PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2:
Jean Delvaree7198fb2011-05-24 20:58:49 +02001156 priv->features |= FEATURE_IDF;
1157 /* fall through */
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001158 default:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001159 priv->features |= FEATURE_I2C_BLOCK_READ;
Jean Delvare6676a842012-12-16 21:11:55 +01001160 priv->features |= FEATURE_IRQ;
Jean Delvare63420642008-01-27 18:14:50 +01001161 /* fall through */
1162 case PCI_DEVICE_ID_INTEL_82801DB_3:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001163 priv->features |= FEATURE_SMBUS_PEC;
1164 priv->features |= FEATURE_BLOCK_BUFFER;
Jean Delvaree0e8398c2010-05-21 18:40:55 +02001165 /* fall through */
1166 case PCI_DEVICE_ID_INTEL_82801CA_3:
1167 case PCI_DEVICE_ID_INTEL_82801BA_2:
1168 case PCI_DEVICE_ID_INTEL_82801AB_3:
1169 case PCI_DEVICE_ID_INTEL_82801AA_3:
Jean Delvare250d1bd2006-12-10 21:21:33 +01001170 break;
Jean Delvare250d1bd2006-12-10 21:21:33 +01001171 }
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001172
Jean Delvareadff6872010-05-21 18:40:54 +02001173 /* Disable features on user request */
1174 for (i = 0; i < ARRAY_SIZE(i801_feature_names); i++) {
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001175 if (priv->features & disable_features & (1 << i))
Jean Delvareadff6872010-05-21 18:40:54 +02001176 dev_notice(&dev->dev, "%s disabled by user\n",
1177 i801_feature_names[i]);
1178 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001179 priv->features &= ~disable_features;
Jean Delvareadff6872010-05-21 18:40:54 +02001180
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001181 err = pci_enable_device(dev);
1182 if (err) {
1183 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
1184 err);
1185 goto exit;
1186 }
1187
1188 /* Determine the address of the SMBus area */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001189 priv->smba = pci_resource_start(dev, SMBBAR);
1190 if (!priv->smba) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001191 dev_err(&dev->dev, "SMBus base address uninitialized, "
1192 "upgrade BIOS\n");
1193 err = -ENODEV;
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001194 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001195 }
1196
Jean Delvare54fb4a052008-07-14 22:38:33 +02001197 err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
Jean Delvare18669ea2009-10-04 22:53:45 +02001198 if (err) {
1199 err = -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +02001200 goto exit;
Jean Delvare18669ea2009-10-04 22:53:45 +02001201 }
Jean Delvare54fb4a052008-07-14 22:38:33 +02001202
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001203 err = pci_request_region(dev, SMBBAR, i801_driver.name);
1204 if (err) {
1205 dev_err(&dev->dev, "Failed to request SMBus region "
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001206 "0x%lx-0x%Lx\n", priv->smba,
Andrew Morton598736c2006-06-30 01:56:20 -07001207 (unsigned long long)pci_resource_end(dev, SMBBAR));
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001208 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001209 }
1210
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001211 pci_read_config_byte(priv->pci_dev, SMBHSTCFG, &temp);
1212 priv->original_hstcfg = temp;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001213 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
1214 if (!(temp & SMBHSTCFG_HST_EN)) {
1215 dev_info(&dev->dev, "Enabling SMBus device\n");
1216 temp |= SMBHSTCFG_HST_EN;
1217 }
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001218 pci_write_config_byte(priv->pci_dev, SMBHSTCFG, temp);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001219
Daniel Kurtz636752b2012-07-24 14:13:58 +02001220 if (temp & SMBHSTCFG_SMB_SMI_EN) {
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001221 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001222 /* Disable SMBus interrupt feature if SMBus using SMI# */
1223 priv->features &= ~FEATURE_IRQ;
Daniel Kurtz636752b2012-07-24 14:13:58 +02001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Jean Delvarea0921b62008-01-27 18:14:50 +01001226 /* Clear special mode bits */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001227 if (priv->features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
1228 outb_p(inb_p(SMBAUXCTL(priv)) &
1229 ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B), SMBAUXCTL(priv));
Jean Delvarea0921b62008-01-27 18:14:50 +01001230
Jean Delvareb3b8df92014-11-12 10:20:40 +01001231 /* Default timeout in interrupt mode: 200 ms */
1232 priv->adapter.timeout = HZ / 5;
1233
Daniel Kurtz636752b2012-07-24 14:13:58 +02001234 if (priv->features & FEATURE_IRQ) {
1235 init_waitqueue_head(&priv->waitq);
1236
1237 err = request_irq(dev->irq, i801_isr, IRQF_SHARED,
1238 i801_driver.name, priv);
1239 if (err) {
1240 dev_err(&dev->dev, "Failed to allocate irq %d: %d\n",
1241 dev->irq, err);
1242 goto exit_release;
1243 }
Jean Delvare29b60852012-07-24 14:13:59 +02001244 dev_info(&dev->dev, "SMBus using PCI Interrupt\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001245 }
1246
Robert P. J. Day405ae7d2007-02-17 19:13:42 +01001247 /* set up the sysfs linkage to our parent device */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001248 priv->adapter.dev.parent = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Jean Delvare7e2193a8f2009-12-06 17:06:27 +01001250 /* Retry up to 3 times on lost arbitration */
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001251 priv->adapter.retries = 3;
Jean Delvare7e2193a8f2009-12-06 17:06:27 +01001252
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001253 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
1254 "SMBus I801 adapter at %04lx", priv->smba);
1255 err = i2c_add_adapter(&priv->adapter);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001256 if (err) {
1257 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
Daniel Kurtz636752b2012-07-24 14:13:58 +02001258 goto exit_free_irq;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001259 }
Jean Delvare1561bfe2009-01-07 14:29:17 +01001260
Jean Delvaree7198fb2011-05-24 20:58:49 +02001261 i801_probe_optional_slaves(priv);
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001262 /* We ignore errors - multiplexing is optional */
1263 i801_add_mux(priv);
Jean Delvare1561bfe2009-01-07 14:29:17 +01001264
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001265 pci_set_drvdata(dev, priv);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001266
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001267 return 0;
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001268
Daniel Kurtz636752b2012-07-24 14:13:58 +02001269exit_free_irq:
1270 if (priv->features & FEATURE_IRQ)
1271 free_irq(dev->irq, priv);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001272exit_release:
1273 pci_release_region(dev, SMBBAR);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001274exit:
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001275 kfree(priv);
Jean Delvare02dd7ae2006-06-12 21:53:41 +02001276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Bill Pemberton0b255e92012-11-27 15:59:38 -05001279static void i801_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001281 struct i801_priv *priv = pci_get_drvdata(dev);
1282
Jean Delvare3ad7ea12012-10-05 22:23:53 +02001283 i801_del_mux(priv);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001284 i2c_del_adapter(&priv->adapter);
1285 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001286
1287 if (priv->features & FEATURE_IRQ)
1288 free_irq(dev->irq, priv);
Jean Delvare6dcc19d2006-06-12 21:53:02 +02001289 pci_release_region(dev, SMBBAR);
Daniel Kurtz636752b2012-07-24 14:13:58 +02001290
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001291 kfree(priv);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +02001292 /*
1293 * do not call pci_disable_device(dev) since it can cause hard hangs on
1294 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Jean Delvarea5aaea32007-03-22 19:49:01 +01001298#ifdef CONFIG_PM
1299static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
1300{
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001301 struct i801_priv *priv = pci_get_drvdata(dev);
1302
Jean Delvarea5aaea32007-03-22 19:49:01 +01001303 pci_save_state(dev);
David Woodhouse0cd96eb2010-10-31 21:06:59 +01001304 pci_write_config_byte(dev, SMBHSTCFG, priv->original_hstcfg);
Jean Delvarea5aaea32007-03-22 19:49:01 +01001305 pci_set_power_state(dev, pci_choose_state(dev, mesg));
1306 return 0;
1307}
1308
1309static int i801_resume(struct pci_dev *dev)
1310{
1311 pci_set_power_state(dev, PCI_D0);
1312 pci_restore_state(dev);
1313 return pci_enable_device(dev);
1314}
1315#else
1316#define i801_suspend NULL
1317#define i801_resume NULL
1318#endif
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320static struct pci_driver i801_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 .name = "i801_smbus",
1322 .id_table = i801_ids,
1323 .probe = i801_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001324 .remove = i801_remove,
Jean Delvarea5aaea32007-03-22 19:49:01 +01001325 .suspend = i801_suspend,
1326 .resume = i801_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327};
1328
1329static int __init i2c_i801_init(void)
1330{
Jean Delvare6aa14642011-05-24 20:58:49 +02001331 if (dmi_name_in_vendors("FUJITSU"))
1332 input_apanel_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return pci_register_driver(&i801_driver);
1334}
1335
1336static void __exit i2c_i801_exit(void)
1337{
1338 pci_unregister_driver(&i801_driver);
1339}
1340
Jean Delvare7c81c602014-01-29 20:40:08 +01001341MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342MODULE_DESCRIPTION("I801 SMBus driver");
1343MODULE_LICENSE("GPL");
1344
1345module_init(i2c_i801_init);
1346module_exit(i2c_i801_exit);