blob: 36a9556d7cfa8e287b4e8a2f819dd7b2768842c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 2002,2003 Alexander Malysh <amalysh@web.de>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 Status: beta
21
22 Supports:
23 SIS 630
24 SIS 730
Amaury Decrême974d6a32013-01-28 22:21:05 +010025 SIS 964
26
27 Notable differences between chips:
28 +------------------------+--------------------+-------------------+
29 | | SIS630/730 | SIS964 |
30 +------------------------+--------------------+-------------------+
31 | Clock | 14kHz/56kHz | 55.56kHz/27.78kHz |
32 | SMBus registers offset | 0x80 | 0xE0 |
33 | SMB_CNT | Bit 1 = Slave Busy | Bit 1 = Bus probe |
34 | (not used yet) | Bit 3 is reserved | Bit 3 = Last byte |
35 | SMB_PCOUNT | Offset + 0x06 | Offset + 0x14 |
36 | SMB_COUNT | 4:0 bits | 5:0 bits |
37 +------------------------+--------------------+-------------------+
38 (Other differences don't affect the functions provided by the driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 Note: we assume there can only be one device, with one SMBus interface.
41*/
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kernel.h>
44#include <linux/module.h>
45#include <linux/delay.h>
46#include <linux/pci.h>
47#include <linux/ioport.h>
48#include <linux/init.h>
49#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020050#include <linux/acpi.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Amaury Decrême974d6a32013-01-28 22:21:05 +010053/* SIS964 id is defined here as we are the only file using it */
54#define PCI_DEVICE_ID_SI_964 0x0964
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Amaury Decrême974d6a32013-01-28 22:21:05 +010056/* SIS630/730/964 SMBus registers */
57#define SMB_STS 0x00 /* status */
58#define SMB_CNT 0x02 /* control */
59#define SMBHOST_CNT 0x03 /* host control */
60#define SMB_ADDR 0x04 /* address */
61#define SMB_CMD 0x05 /* command */
62#define SMB_COUNT 0x07 /* byte count */
63#define SMB_BYTE 0x08 /* ~0x8F data byte field */
64
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +010065/* SMB_STS register */
66#define BYTE_DONE_STS 0x10 /* Byte Done Status / Block Array */
67#define SMBCOL_STS 0x04 /* Collision */
68#define SMBERR_STS 0x02 /* Device error */
69
70/* SMB_CNT register */
71#define MSTO_EN 0x40 /* Host Master Timeout Enable */
72#define SMBCLK_SEL 0x20 /* Host master clock selection */
73#define SMB_PROBE 0x02 /* Bus Probe/Slave busy */
74#define SMB_HOSTBUSY 0x01 /* Host Busy */
75
76/* SMBHOST_CNT register */
77#define SMB_KILL 0x20 /* Kill */
78#define SMB_START 0x10 /* Start */
79
Amaury Decrême974d6a32013-01-28 22:21:05 +010080/* register count for request_region
81 * As we don't use SMB_PCOUNT, 20 is ok for SiS630 and SiS964
82 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define SIS630_SMB_IOREGION 20
84
85/* PCI address constants */
86/* acpi base address register */
87#define SIS630_ACPI_BASE_REG 0x74
88/* bios control register */
89#define SIS630_BIOS_CTL_REG 0x40
90
91/* Other settings */
92#define MAX_TIMEOUT 500
93
94/* SIS630 constants */
95#define SIS630_QUICK 0x00
96#define SIS630_BYTE 0x01
97#define SIS630_BYTE_DATA 0x02
98#define SIS630_WORD_DATA 0x03
99#define SIS630_PCALL 0x04
100#define SIS630_BLOCK_DATA 0x05
101
Jean Delvared6072f82005-09-25 16:37:04 +0200102static struct pci_driver sis630_driver;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* insmod parameters */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030105static bool high_clock;
106static bool force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107module_param(high_clock, bool, 0);
Amaury Decrême974d6a32013-01-28 22:21:05 +0100108MODULE_PARM_DESC(high_clock,
109 "Set Host Master Clock to 56KHz (default 14KHz) (SIS630/730 only).");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110module_param(force, bool, 0);
111MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!");
112
Amaury Decrême974d6a32013-01-28 22:21:05 +0100113/* SMBus base adress */
114static unsigned short smbus_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* supported chips */
117static int supported[] = {
118 PCI_DEVICE_ID_SI_630,
119 PCI_DEVICE_ID_SI_730,
Amaury Decrême974d6a32013-01-28 22:21:05 +0100120 PCI_DEVICE_ID_SI_760,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 0 /* terminates the list */
122};
123
124static inline u8 sis630_read(u8 reg)
125{
Amaury Decrême974d6a32013-01-28 22:21:05 +0100126 return inb(smbus_base + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static inline void sis630_write(u8 reg, u8 data)
130{
Amaury Decrême974d6a32013-01-28 22:21:05 +0100131 outb(data, smbus_base + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Amaury Decrême91991f32013-01-29 21:22:26 +0100134static int sis630_transaction_start(struct i2c_adapter *adap, int size,
135 u8 *oldclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Amaury Decrême91991f32013-01-29 21:22:26 +0100137 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Make sure the SMBus host is ready to start transmitting. */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100140 temp = sis630_read(SMB_CNT);
141 if ((temp & (SMB_PROBE | SMB_HOSTBUSY)) != 0x00) {
142 dev_dbg(&adap->dev, "SMBus busy (%02x). Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* kill smbus transaction */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100144 sis630_write(SMBHOST_CNT, SMB_KILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100146 temp = sis630_read(SMB_CNT);
147 if (temp & (SMB_PROBE | SMB_HOSTBUSY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200149 return -EBUSY;
Amaury Decrême91991f32013-01-29 21:22:26 +0100150 } else {
Jean Delvarec5d21b72008-04-29 23:11:37 +0200151 dev_dbg(&adap->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Amaury Decrême91991f32013-01-29 21:22:26 +0100153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* save old clock, so we can prevent machine for hung */
156 *oldclock = sis630_read(SMB_CNT);
157
158 dev_dbg(&adap->dev, "saved clock 0x%02x\n", *oldclock);
159
Amaury Decrême91991f32013-01-29 21:22:26 +0100160 /* disable timeout interrupt,
161 * set Host Master Clock to 56KHz if requested */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (high_clock)
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100163 sis630_write(SMB_CNT, SMBCLK_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 else
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100165 sis630_write(SMB_CNT, (*oldclock & ~MSTO_EN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* clear all sticky bits */
168 temp = sis630_read(SMB_STS);
169 sis630_write(SMB_STS, temp & 0x1e);
170
171 /* start the transaction by setting bit 4 and size */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100172 sis630_write(SMBHOST_CNT, SMB_START | (size & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return 0;
175}
176
177static int sis630_transaction_wait(struct i2c_adapter *adap, int size)
178{
179 int temp, result = 0, timeout = 0;
180
181 /* We will always wait for a fraction of a second! */
182 do {
183 msleep(1);
184 temp = sis630_read(SMB_STS);
185 /* check if block transmitted */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100186 if (size == SIS630_BLOCK_DATA && (temp & BYTE_DONE_STS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188 } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
189
190 /* If the SMBus is still busy, we give up */
Roel Kluin4ccc28f2009-05-05 08:39:24 +0200191 if (timeout > MAX_TIMEOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dev_dbg(&adap->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200193 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100196 if (temp & SMBERR_STS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
David Brownell97140342008-07-14 22:38:25 +0200198 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100201 if (temp & SMBCOL_STS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dev_err(&adap->dev, "Bus collision!\n");
Amaury Decrême499b9192013-01-28 22:21:07 +0100203 result = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 return result;
207}
208
209static void sis630_transaction_end(struct i2c_adapter *adap, u8 oldclock)
210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* clear all status "sticky" bits */
Amaury Decrêmeaa9e7a32013-01-28 22:21:06 +0100212 sis630_write(SMB_STS, 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Amaury Decrême91991f32013-01-29 21:22:26 +0100214 dev_dbg(&adap->dev,
215 "SMB_CNT before clock restore 0x%02x\n", sis630_read(SMB_CNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /*
218 * restore old Host Master Clock if high_clock is set
219 * and oldclock was not 56KHz
220 */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100221 if (high_clock && !(oldclock & SMBCLK_SEL))
222 sis630_write(SMB_CNT, sis630_read(SMB_CNT) & ~SMBCLK_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Amaury Decrême91991f32013-01-29 21:22:26 +0100224 dev_dbg(&adap->dev,
225 "SMB_CNT after clock restore 0x%02x\n", sis630_read(SMB_CNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static int sis630_transaction(struct i2c_adapter *adap, int size)
229{
230 int result = 0;
231 u8 oldclock = 0;
232
233 result = sis630_transaction_start(adap, size, &oldclock);
234 if (!result) {
235 result = sis630_transaction_wait(adap, size);
236 sis630_transaction_end(adap, oldclock);
237 }
238
239 return result;
240}
241
Amaury Decrême91991f32013-01-29 21:22:26 +0100242static int sis630_block_data(struct i2c_adapter *adap,
243 union i2c_smbus_data *data, int read_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int i, len = 0, rc = 0;
246 u8 oldclock = 0;
247
248 if (read_write == I2C_SMBUS_WRITE) {
249 len = data->block[0];
250 if (len < 0)
251 len = 0;
252 else if (len > 32)
253 len = 32;
254 sis630_write(SMB_COUNT, len);
Amaury Decrême91991f32013-01-29 21:22:26 +0100255 for (i = 1; i <= len; i++) {
256 dev_dbg(&adap->dev,
257 "set data 0x%02x\n", data->block[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* set data */
Amaury Decrême91991f32013-01-29 21:22:26 +0100259 sis630_write(SMB_BYTE + (i - 1) % 8, data->block[i]);
260 if (i == 8 || (len < 8 && i == len)) {
261 dev_dbg(&adap->dev,
262 "start trans len=%d i=%d\n", len, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* first transaction */
David Brownell97140342008-07-14 22:38:25 +0200264 rc = sis630_transaction_start(adap,
265 SIS630_BLOCK_DATA, &oldclock);
266 if (rc)
267 return rc;
Amaury Decrême91991f32013-01-29 21:22:26 +0100268 } else if ((i - 1) % 8 == 7 || i == len) {
269 dev_dbg(&adap->dev,
270 "trans_wait len=%d i=%d\n", len, i);
271 if (i > 8) {
272 dev_dbg(&adap->dev,
273 "clear smbary_sts"
274 " len=%d i=%d\n", len, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /*
276 If this is not first transaction,
277 we must clear sticky bit.
278 clear SMBARY_STS
279 */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100280 sis630_write(SMB_STS, BYTE_DONE_STS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
David Brownell97140342008-07-14 22:38:25 +0200282 rc = sis630_transaction_wait(adap,
283 SIS630_BLOCK_DATA);
284 if (rc) {
Amaury Decrême91991f32013-01-29 21:22:26 +0100285 dev_dbg(&adap->dev,
286 "trans_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 }
289 }
290 }
Amaury Decrême91991f32013-01-29 21:22:26 +0100291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* read request */
293 data->block[0] = len = 0;
David Brownell97140342008-07-14 22:38:25 +0200294 rc = sis630_transaction_start(adap,
295 SIS630_BLOCK_DATA, &oldclock);
296 if (rc)
297 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 do {
David Brownell97140342008-07-14 22:38:25 +0200299 rc = sis630_transaction_wait(adap, SIS630_BLOCK_DATA);
300 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 dev_dbg(&adap->dev, "trans_wait failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 }
304 /* if this first transaction then read byte count */
305 if (len == 0)
306 data->block[0] = sis630_read(SMB_COUNT);
307
308 /* just to be sure */
309 if (data->block[0] > 32)
310 data->block[0] = 32;
311
Amaury Decrême91991f32013-01-29 21:22:26 +0100312 dev_dbg(&adap->dev,
313 "block data read len=0x%x\n", data->block[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Amaury Decrême91991f32013-01-29 21:22:26 +0100315 for (i = 0; i < 8 && len < data->block[0]; i++, len++) {
316 dev_dbg(&adap->dev,
317 "read i=%d len=%d\n", i, len);
318 data->block[len + 1] = sis630_read(SMB_BYTE +
319 i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Amaury Decrême91991f32013-01-29 21:22:26 +0100322 dev_dbg(&adap->dev,
323 "clear smbary_sts len=%d i=%d\n", len, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* clear SMBARY_STS */
Amaury Decrêmea6e7d0ef2013-01-28 22:21:08 +0100326 sis630_write(SMB_STS, BYTE_DONE_STS);
Amaury Decrême91991f32013-01-29 21:22:26 +0100327 } while (len < data->block[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 sis630_transaction_end(adap, oldclock);
331
332 return rc;
333}
334
David Brownell97140342008-07-14 22:38:25 +0200335/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static s32 sis630_access(struct i2c_adapter *adap, u16 addr,
337 unsigned short flags, char read_write,
338 u8 command, int size, union i2c_smbus_data *data)
339{
David Brownell97140342008-07-14 22:38:25 +0200340 int status;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 switch (size) {
Amaury Decrême91991f32013-01-29 21:22:26 +0100343 case I2C_SMBUS_QUICK:
344 sis630_write(SMB_ADDR,
345 ((addr & 0x7f) << 1) | (read_write & 0x01));
346 size = SIS630_QUICK;
347 break;
348 case I2C_SMBUS_BYTE:
349 sis630_write(SMB_ADDR,
350 ((addr & 0x7f) << 1) | (read_write & 0x01));
351 if (read_write == I2C_SMBUS_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 sis630_write(SMB_CMD, command);
Amaury Decrême91991f32013-01-29 21:22:26 +0100353 size = SIS630_BYTE;
354 break;
355 case I2C_SMBUS_BYTE_DATA:
356 sis630_write(SMB_ADDR,
357 ((addr & 0x7f) << 1) | (read_write & 0x01));
358 sis630_write(SMB_CMD, command);
359 if (read_write == I2C_SMBUS_WRITE)
360 sis630_write(SMB_BYTE, data->byte);
361 size = SIS630_BYTE_DATA;
362 break;
363 case I2C_SMBUS_PROC_CALL:
364 case I2C_SMBUS_WORD_DATA:
365 sis630_write(SMB_ADDR,
366 ((addr & 0x7f) << 1) | (read_write & 0x01));
367 sis630_write(SMB_CMD, command);
368 if (read_write == I2C_SMBUS_WRITE) {
369 sis630_write(SMB_BYTE, data->word & 0xff);
370 sis630_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
371 }
372 size = (size == I2C_SMBUS_PROC_CALL ?
373 SIS630_PCALL : SIS630_WORD_DATA);
374 break;
375 case I2C_SMBUS_BLOCK_DATA:
376 sis630_write(SMB_ADDR,
377 ((addr & 0x7f) << 1) | (read_write & 0x01));
378 sis630_write(SMB_CMD, command);
379 size = SIS630_BLOCK_DATA;
380 return sis630_block_data(adap, data, read_write);
381 default:
382 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
383 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
David Brownell97140342008-07-14 22:38:25 +0200386 status = sis630_transaction(adap, size);
387 if (status)
388 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if ((size != SIS630_PCALL) &&
391 ((read_write == I2C_SMBUS_WRITE) || (size == SIS630_QUICK))) {
392 return 0;
393 }
394
Amaury Decrême91991f32013-01-29 21:22:26 +0100395 switch (size) {
396 case SIS630_BYTE:
397 case SIS630_BYTE_DATA:
398 data->byte = sis630_read(SMB_BYTE);
399 break;
400 case SIS630_PCALL:
401 case SIS630_WORD_DATA:
402 data->word = sis630_read(SMB_BYTE) +
403 (sis630_read(SMB_BYTE + 1) << 8);
404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 return 0;
408}
409
410static u32 sis630_func(struct i2c_adapter *adapter)
411{
Amaury Decrême91991f32013-01-29 21:22:26 +0100412 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
413 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
414 I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Bill Pemberton0b255e92012-11-27 15:59:38 -0500417static int sis630_setup(struct pci_dev *sis630_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 unsigned char b;
420 struct pci_dev *dummy = NULL;
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100421 int retval, i;
Amaury Decrême974d6a32013-01-28 22:21:05 +0100422 /* acpi base address */
423 unsigned short acpi_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* check for supported SiS devices */
Amaury Decrême91991f32013-01-29 21:22:26 +0100426 for (i = 0; supported[i] > 0; i++) {
427 dummy = pci_get_device(PCI_VENDOR_ID_SI, supported[i], dummy);
428 if (dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break; /* found */
430 }
431
432 if (dummy) {
433 pci_dev_put(dummy);
Amaury Decrême91991f32013-01-29 21:22:26 +0100434 } else if (force) {
435 dev_err(&sis630_dev->dev,
436 "WARNING: Can't detect SIS630 compatible device, but "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 "loading because of force option enabled\n");
Amaury Decrême91991f32013-01-29 21:22:26 +0100438 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -ENODEV;
440 }
441
442 /*
443 Enable ACPI first , so we can accsess reg 74-75
444 in acpi io space and read acpi base addr
445 */
Amaury Decrême91991f32013-01-29 21:22:26 +0100446 if (pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, &b)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 dev_err(&sis630_dev->dev, "Error: Can't read bios ctl reg\n");
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100448 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto exit;
450 }
451 /* if ACPI already enabled , do nothing */
452 if (!(b & 0x80) &&
453 pci_write_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, b | 0x80)) {
454 dev_err(&sis630_dev->dev, "Error: Can't enable ACPI\n");
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100455 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto exit;
457 }
458
459 /* Determine the ACPI base address */
Amaury Decrême91991f32013-01-29 21:22:26 +0100460 if (pci_read_config_word(sis630_dev,
461 SIS630_ACPI_BASE_REG, &acpi_base)) {
462 dev_err(&sis630_dev->dev,
463 "Error: Can't determine ACPI base address\n");
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100464 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto exit;
466 }
467
Amaury Decrême97da42d2013-01-28 22:21:09 +0100468 dev_dbg(&sis630_dev->dev, "ACPI base at 0x%04hx\n", acpi_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Amaury Decrême974d6a32013-01-28 22:21:05 +0100470 if (supported[i] == PCI_DEVICE_ID_SI_760)
471 smbus_base = acpi_base + 0xE0;
472 else
473 smbus_base = acpi_base + 0x80;
474
475 dev_dbg(&sis630_dev->dev, "SMBus base at 0x%04hx\n", smbus_base);
476
477 retval = acpi_check_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
Jean Delvare54fb4a052008-07-14 22:38:33 +0200478 sis630_driver.name);
479 if (retval)
480 goto exit;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Everything is happy, let's grab the memory and set things up. */
Amaury Decrême974d6a32013-01-28 22:21:05 +0100483 if (!request_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
Jean Delvared6072f82005-09-25 16:37:04 +0200484 sis630_driver.name)) {
Amaury Decrême974d6a32013-01-28 22:21:05 +0100485 dev_err(&sis630_dev->dev,
486 "I/O Region 0x%04hx-0x%04hx for SMBus already in use.\n",
487 smbus_base + SMB_STS,
488 smbus_base + SMB_STS + SIS630_SMB_IOREGION - 1);
Jean Delvare7c1f59c2012-01-12 20:32:03 +0100489 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto exit;
491 }
492
493 retval = 0;
494
495exit:
496 if (retval)
Amaury Decrême974d6a32013-01-28 22:21:05 +0100497 smbus_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return retval;
499}
500
501
Jean Delvare8f9082c2006-09-03 22:39:46 +0200502static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .smbus_xfer = sis630_access,
504 .functionality = sis630_func,
505};
506
507static struct i2c_adapter sis630_adapter = {
508 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200509 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .algo = &smbus_algorithm,
Amaury Decrême974d6a32013-01-28 22:21:05 +0100511 .retries = 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512};
513
Axel Lin3527bd52012-01-12 20:32:04 +0100514static DEFINE_PCI_DEVICE_TABLE(sis630_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
516 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC) },
Amaury Decrême974d6a32013-01-28 22:21:05 +0100517 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_964) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 { 0, }
519};
520
Amaury Decrême91991f32013-01-29 21:22:26 +0100521MODULE_DEVICE_TABLE(pci, sis630_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Bill Pemberton0b255e92012-11-27 15:59:38 -0500523static int sis630_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 if (sis630_setup(dev)) {
Amaury Decrême91991f32013-01-29 21:22:26 +0100526 dev_err(&dev->dev,
527 "SIS630 compatible bus not detected, "
528 "module not inserted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -ENODEV;
530 }
531
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100532 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sis630_adapter.dev.parent = &dev->dev;
534
Jean Delvare66c7acf2009-01-07 14:29:18 +0100535 snprintf(sis630_adapter.name, sizeof(sis630_adapter.name),
Amaury Decrême974d6a32013-01-28 22:21:05 +0100536 "SMBus SIS630 adapter at %04hx", smbus_base + SMB_STS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return i2c_add_adapter(&sis630_adapter);
539}
540
Bill Pemberton0b255e92012-11-27 15:59:38 -0500541static void sis630_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Amaury Decrême974d6a32013-01-28 22:21:05 +0100543 if (smbus_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 i2c_del_adapter(&sis630_adapter);
Amaury Decrême974d6a32013-01-28 22:21:05 +0100545 release_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION);
546 smbus_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548}
549
550
551static struct pci_driver sis630_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .name = "sis630_smbus",
553 .id_table = sis630_ids,
554 .probe = sis630_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500555 .remove = sis630_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556};
557
Axel Lin56f21782012-07-24 14:13:56 +0200558module_pci_driver(sis630_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560MODULE_LICENSE("GPL");
561MODULE_AUTHOR("Alexander Malysh <amalysh@web.de>");
562MODULE_DESCRIPTION("SIS630 SMBus driver");