blob: 91e349c884c5c9ec73fc42b6fecd4964afc7ff12 [file] [log] [blame]
Ben Gardner99c3adb2006-01-18 22:41:50 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
3
4 National Semiconductor SCx200 ACCESS.bus support
Ben Gardner16ffc5c2006-01-18 22:48:26 +01005 Also supports the AMD CS5535 and AMD CS5536
Ben Gardner99c3adb2006-01-18 22:41:50 +01006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 Based on i2c-keywest.c which is:
8 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
9 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
Ben Gardner99c3adb2006-01-18 22:41:50 +010010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
Ben Gardner99c3adb2006-01-18 22:41:50 +010015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
Ben Gardner99c3adb2006-01-18 22:41:50 +010020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024*/
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/kernel.h>
29#include <linux/init.h>
30#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/pci.h>
Andres Salomonde8255c2010-12-30 20:27:33 -080032#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
Jean Delvare3fb9a652006-01-18 23:17:01 +010034#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020036#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <linux/scx200.h>
39
40#define NAME "scx200_acb"
41
42MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
43MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
Andres Salomonde8255c2010-12-30 20:27:33 -080044MODULE_ALIAS("platform:cs5535-smb");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_LICENSE("GPL");
46
47#define MAX_DEVICES 4
48static int base[MAX_DEVICES] = { 0x820, 0x840 };
49module_param_array(base, int, NULL, 0);
50MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
51
Ben Gardnerf933ff52006-01-18 22:52:06 +010052#define POLL_TIMEOUT (HZ/5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54enum scx200_acb_state {
55 state_idle,
56 state_address,
57 state_command,
58 state_repeat_start,
59 state_quick,
60 state_read,
61 state_write,
62};
63
64static const char *scx200_acb_state_name[] = {
65 "idle",
66 "address",
67 "command",
68 "repeat_start",
69 "quick",
70 "read",
71 "write",
72};
73
74/* Physical interface */
Ben Gardner99c3adb2006-01-18 22:41:50 +010075struct scx200_acb_iface {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct scx200_acb_iface *next;
77 struct i2c_adapter adapter;
78 unsigned base;
Jean Delvare3fb9a652006-01-18 23:17:01 +010079 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* State machine data */
82 enum scx200_acb_state state;
83 int result;
84 u8 address_byte;
85 u8 command;
86 u8 *ptr;
87 char needs_reset;
88 unsigned len;
89};
90
91/* Register Definitions */
92#define ACBSDA (iface->base + 0)
93#define ACBST (iface->base + 1)
94#define ACBST_SDAST 0x40 /* SDA Status */
Ben Gardner99c3adb2006-01-18 22:41:50 +010095#define ACBST_BER 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define ACBST_NEGACK 0x10 /* Negative Acknowledge */
97#define ACBST_STASTR 0x08 /* Stall After Start */
98#define ACBST_MASTER 0x02
99#define ACBCST (iface->base + 2)
100#define ACBCST_BB 0x02
101#define ACBCTL1 (iface->base + 3)
102#define ACBCTL1_STASTRE 0x80
103#define ACBCTL1_NMINTE 0x40
Ben Gardner99c3adb2006-01-18 22:41:50 +0100104#define ACBCTL1_ACK 0x10
105#define ACBCTL1_STOP 0x02
106#define ACBCTL1_START 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define ACBADDR (iface->base + 4)
108#define ACBCTL2 (iface->base + 5)
109#define ACBCTL2_ENABLE 0x01
110
111/************************************************************************/
112
113static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
114{
115 const char *errmsg;
116
Ben Gardneref4d9272006-01-18 22:43:10 +0100117 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
118 scx200_acb_state_name[iface->state], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (status & ACBST_BER) {
121 errmsg = "bus error";
122 goto error;
123 }
124 if (!(status & ACBST_MASTER)) {
125 errmsg = "not master";
126 goto error;
127 }
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100128 if (status & ACBST_NEGACK) {
129 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
130 scx200_acb_state_name[iface->state]);
131
132 iface->state = state_idle;
133 iface->result = -ENXIO;
134
135 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
136 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200137
138 /* Reset the status register */
139 outb(0, ACBST);
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100140 return;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 switch (iface->state) {
144 case state_idle:
145 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
146 break;
147
148 case state_address:
149 /* Do a pointer write first */
150 outb(iface->address_byte & ~1, ACBSDA);
151
152 iface->state = state_command;
153 break;
154
155 case state_command:
156 outb(iface->command, ACBSDA);
157
158 if (iface->address_byte & 1)
159 iface->state = state_repeat_start;
160 else
161 iface->state = state_write;
162 break;
163
164 case state_repeat_start:
165 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
166 /* fallthrough */
Ben Gardner99c3adb2006-01-18 22:41:50 +0100167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 case state_quick:
169 if (iface->address_byte & 1) {
Ben Gardner99c3adb2006-01-18 22:41:50 +0100170 if (iface->len == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
172 else
173 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
174 outb(iface->address_byte, ACBSDA);
175
176 iface->state = state_read;
177 } else {
178 outb(iface->address_byte, ACBSDA);
179
180 iface->state = state_write;
181 }
182 break;
183
184 case state_read:
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200185 /* Set ACK if _next_ byte will be the last one */
186 if (iface->len == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
188 else
189 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
190
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200191 if (iface->len == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 iface->result = 0;
193 iface->state = state_idle;
194 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
195 }
196
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200197 *iface->ptr++ = inb(ACBSDA);
198 --iface->len;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 case state_write:
203 if (iface->len == 0) {
204 iface->result = 0;
205 iface->state = state_idle;
206 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
207 break;
208 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 outb(*iface->ptr++, ACBSDA);
211 --iface->len;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 }
215
216 return;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 error:
Willy Tarreaufce96f32009-09-18 22:45:47 +0200219 dev_err(&iface->adapter.dev,
220 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
221 scx200_acb_state_name[iface->state], iface->address_byte,
222 iface->len, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 iface->state = state_idle;
225 iface->result = -EIO;
226 iface->needs_reset = 1;
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void scx200_acb_poll(struct scx200_acb_iface *iface)
230{
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100231 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 unsigned long timeout;
233
234 timeout = jiffies + POLL_TIMEOUT;
David Woodhouse3e3183b2006-08-05 12:15:19 -0700235 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 status = inb(ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200237
238 /* Reset the status register to avoid the hang */
239 outb(0, ACBST);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
242 scx200_acb_machine(iface, status);
243 return;
244 }
David Woodhouse3e3183b2006-08-05 12:15:19 -0700245 if (time_after(jiffies, timeout))
246 break;
247 cpu_relax();
248 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100251 dev_err(&iface->adapter.dev, "timeout in state %s\n",
252 scx200_acb_state_name[iface->state]);
253
254 iface->state = state_idle;
255 iface->result = -EIO;
256 iface->needs_reset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259static void scx200_acb_reset(struct scx200_acb_iface *iface)
260{
261 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100262 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 outb(0x70, ACBCTL2);
264 /* Polling mode */
265 outb(0, ACBCTL1);
266 /* Disable slave address */
267 outb(0, ACBADDR);
268 /* Enable the ACCESS.bus device */
269 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
270 /* Free STALL after START */
271 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
272 /* Send a STOP */
273 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
274 /* Clear BER, NEGACK and STASTR bits */
275 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
276 /* Clear BB bit */
277 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
278}
279
280static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
Ben Gardner99c3adb2006-01-18 22:41:50 +0100281 u16 address, unsigned short flags,
282 char rw, u8 command, int size,
283 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
286 int len;
287 u8 *buffer;
288 u16 cur_word;
289 int rc;
290
291 switch (size) {
292 case I2C_SMBUS_QUICK:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100293 len = 0;
294 buffer = NULL;
295 break;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 case I2C_SMBUS_BYTE:
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100298 len = 1;
299 buffer = rw ? &data->byte : &command;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100300 break;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case I2C_SMBUS_BYTE_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100303 len = 1;
304 buffer = &data->byte;
305 break;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 case I2C_SMBUS_WORD_DATA:
308 len = 2;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100309 cur_word = cpu_to_le16(data->word);
310 buffer = (u8 *)&cur_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100312
Jean Delvarec3efaca2006-07-01 17:06:43 +0200313 case I2C_SMBUS_I2C_BLOCK_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100314 len = data->block[0];
Jean Delvarec3efaca2006-07-01 17:06:43 +0200315 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
316 return -EINVAL;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100317 buffer = &data->block[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 default:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Ben Gardneref4d9272006-01-18 22:43:10 +0100324 dev_dbg(&adapter->dev,
325 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
326 size, address, command, len, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (!len && rw == I2C_SMBUS_READ) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100329 dev_dbg(&adapter->dev, "zero length read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EINVAL;
331 }
332
Jean Delvare3fb9a652006-01-18 23:17:01 +0100333 mutex_lock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100335 iface->address_byte = (address << 1) | rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 iface->command = command;
337 iface->ptr = buffer;
338 iface->len = len;
339 iface->result = -EINVAL;
340 iface->needs_reset = 0;
341
342 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
343
344 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
345 iface->state = state_quick;
346 else
347 iface->state = state_address;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 while (iface->state != state_idle)
350 scx200_acb_poll(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (iface->needs_reset)
353 scx200_acb_reset(iface);
354
355 rc = iface->result;
356
Jean Delvare3fb9a652006-01-18 23:17:01 +0100357 mutex_unlock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
Ben Gardner99c3adb2006-01-18 22:41:50 +0100360 data->word = le16_to_cpu(cur_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362#ifdef DEBUG
Ben Gardneref4d9272006-01-18 22:43:10 +0100363 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (buffer) {
365 int i;
366 printk(" data:");
367 for (i = 0; i < len; ++i)
368 printk(" %02x", buffer[i]);
369 }
370 printk("\n");
371#endif
372
373 return rc;
374}
375
376static u32 scx200_acb_func(struct i2c_adapter *adapter)
377{
378 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
379 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
Jean Delvarec3efaca2006-07-01 17:06:43 +0200380 I2C_FUNC_SMBUS_I2C_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/* For now, we only handle combined mode (smbus) */
Jean Delvare8f9082c2006-09-03 22:39:46 +0200384static const struct i2c_algorithm scx200_acb_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 .smbus_xfer = scx200_acb_smbus_xfer,
386 .functionality = scx200_acb_func,
387};
388
389static struct scx200_acb_iface *scx200_acb_list;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200390static DEFINE_MUTEX(scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Andres Salomonde8255c2010-12-30 20:27:33 -0800392static __devinit int scx200_acb_probe(struct scx200_acb_iface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 u8 val;
395
396 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100397 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 outb(0x70, ACBCTL2);
399
400 if (inb(ACBCTL2) != 0x70) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100401 pr_debug(NAME ": ACBCTL2 readback failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -ENXIO;
403 }
404
405 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
406
407 val = inb(ACBCTL1);
408 if (val) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100409 pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
410 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -ENXIO;
412 }
413
414 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
415
416 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
417
418 val = inb(ACBCTL1);
419 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100420 pr_debug(NAME ": enabled, but NMINTE won't be set, "
421 "ACBCTL1=0x%02x\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -ENXIO;
423 }
424
425 return 0;
426}
427
Andres Salomonde8255c2010-12-30 20:27:33 -0800428static __devinit struct scx200_acb_iface *scx200_create_iface(const char *text,
Jean Delvare12a917f2007-02-13 22:09:03 +0100429 struct device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct scx200_acb_iface *iface;
432 struct i2c_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200434 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!iface) {
436 printk(KERN_ERR NAME ": can't allocate memory\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200437 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 adapter = &iface->adapter;
441 i2c_set_adapdata(adapter, iface);
David Brownell2096b952007-05-01 23:26:28 +0200442 snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 adapter->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 adapter->algo = &scx200_acb_algorithm;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200445 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare12a917f2007-02-13 22:09:03 +0100446 adapter->dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jean Delvare3fb9a652006-01-18 23:17:01 +0100448 mutex_init(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200450 return iface;
451}
452
Andres Salomonde8255c2010-12-30 20:27:33 -0800453static int __devinit scx200_acb_create(struct scx200_acb_iface *iface)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200454{
455 struct i2c_adapter *adapter;
456 int rc;
457
458 adapter = &iface->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 rc = scx200_acb_probe(iface);
461 if (rc) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100462 printk(KERN_WARNING NAME ": probe failed\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200463 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 scx200_acb_reset(iface);
467
468 if (i2c_add_adapter(adapter) < 0) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100469 printk(KERN_ERR NAME ": failed to register\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200470 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
Andres Salomonde8255c2010-12-30 20:27:33 -0800473 if (!adapter->dev.parent) {
474 /* If there's no dev, we're tracking (ISA) ifaces manually */
475 mutex_lock(&scx200_acb_list_mutex);
476 iface->next = scx200_acb_list;
477 scx200_acb_list = iface;
478 mutex_unlock(&scx200_acb_list_mutex);
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return 0;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Andres Salomonde8255c2010-12-30 20:27:33 -0800484static struct scx200_acb_iface * __devinit scx200_create_dev(const char *text,
485 unsigned long base, int index, struct device *dev)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200486{
487 struct scx200_acb_iface *iface;
488 int rc;
489
Andres Salomonde8255c2010-12-30 20:27:33 -0800490 iface = scx200_create_iface(text, dev, index);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200491
492 if (iface == NULL)
Andres Salomonde8255c2010-12-30 20:27:33 -0800493 return NULL;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200494
Adrian Bunkdec1a992008-04-22 22:16:48 +0200495 if (!request_region(base, 8, iface->adapter.name)) {
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200496 printk(KERN_ERR NAME ": can't allocate io 0x%lx-0x%lx\n",
497 base, base + 8 - 1);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200498 goto errout_free;
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100499 }
500
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200501 iface->base = base;
502 rc = scx200_acb_create(iface);
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100503
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200504 if (rc == 0)
Andres Salomonde8255c2010-12-30 20:27:33 -0800505 return iface;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200506
507 release_region(base, 8);
508 errout_free:
509 kfree(iface);
Andres Salomonde8255c2010-12-30 20:27:33 -0800510 return NULL;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200511}
512
Andres Salomonde8255c2010-12-30 20:27:33 -0800513static int __devinit scx200_probe(struct platform_device *pdev)
514{
515 struct scx200_acb_iface *iface;
516 struct resource *res;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200517
Andres Salomonde8255c2010-12-30 20:27:33 -0800518 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
519 if (!res) {
520 dev_err(&pdev->dev, "can't fetch device resource info\n");
521 return -ENODEV;
522 }
523
524 iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
525 if (!iface)
526 return -EIO;
527
528 dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
529 iface->adapter.name);
530 platform_set_drvdata(pdev, iface);
531
532 return 0;
533}
534
535static void __devexit scx200_cleanup_iface(struct scx200_acb_iface *iface)
536{
537 i2c_del_adapter(&iface->adapter);
538 release_region(iface->base, 8);
539 kfree(iface);
540}
541
542static int __devexit scx200_remove(struct platform_device *pdev)
543{
544 struct scx200_acb_iface *iface;
545
546 iface = platform_get_drvdata(pdev);
547 platform_set_drvdata(pdev, NULL);
548 scx200_cleanup_iface(iface);
549
550 return 0;
551}
552
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100553static struct platform_driver scx200_pci_driver = {
Andres Salomonde8255c2010-12-30 20:27:33 -0800554 .driver = {
555 .name = "cs5535-smb",
556 .owner = THIS_MODULE,
557 },
558 .probe = scx200_probe,
559 .remove = __devexit_p(scx200_remove),
560};
561
562static const struct pci_device_id scx200_isa[] __initconst = {
563 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
564 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
Dzianis Kahanovich87acf5a2010-10-27 20:33:05 -0600565 { 0, }
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200566};
567
Andres Salomonde8255c2010-12-30 20:27:33 -0800568static __init void scx200_scan_isa(void)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200569{
Andres Salomonde8255c2010-12-30 20:27:33 -0800570 int i;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200571
Andres Salomonde8255c2010-12-30 20:27:33 -0800572 if (!pci_dev_present(scx200_isa))
573 return;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200574
Andres Salomonde8255c2010-12-30 20:27:33 -0800575 for (i = 0; i < MAX_DEVICES; ++i) {
576 if (base[i] == 0)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200577 continue;
578
Andres Salomonde8255c2010-12-30 20:27:33 -0800579 /* XXX: should we care about failures? */
580 scx200_create_dev("SCx200", base[i], i, NULL);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200581 }
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static int __init scx200_acb_init(void)
585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
587
Andres Salomonde8255c2010-12-30 20:27:33 -0800588 /* First scan for ISA-based devices */
589 scx200_scan_isa(); /* XXX: should we care about errors? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Jean Delvare6f9c2962006-04-26 22:50:32 +0200591 /* If at least one bus was created, init must succeed */
592 if (scx200_acb_list)
593 return 0;
Andres Salomonde8255c2010-12-30 20:27:33 -0800594
595 /* No ISA devices; register the platform driver for PCI-based devices */
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100596 return platform_driver_register(&scx200_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static void __exit scx200_acb_cleanup(void)
600{
601 struct scx200_acb_iface *iface;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100602
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100603 platform_driver_unregister(&scx200_pci_driver);
Andres Salomonde8255c2010-12-30 20:27:33 -0800604
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200605 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 while ((iface = scx200_acb_list) != NULL) {
607 scx200_acb_list = iface->next;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200608 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Andres Salomonde8255c2010-12-30 20:27:33 -0800610 scx200_cleanup_iface(iface);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200611
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200612 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200614 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617module_init(scx200_acb_init);
618module_exit(scx200_acb_cleanup);