blob: 7b42a18bd05c2690a7c67eacec72d146ed89ad26 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ben Gardner99c3adb2006-01-18 22:41:50 +01002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
4
5 National Semiconductor SCx200 ACCESS.bus support
Ben Gardner16ffc5c2006-01-18 22:48:26 +01006 Also supports the AMD CS5535 and AMD CS5536
Ben Gardner99c3adb2006-01-18 22:41:50 +01007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 Based on i2c-keywest.c which is:
9 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
10 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
Ben Gardner99c3adb2006-01-18 22:41:50 +010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012*/
13
Jim Cromie401e72b2012-10-05 22:23:52 +020014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/pci.h>
Andres Salomonde8255c2010-12-30 20:27:33 -080022#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
Jean Delvare3fb9a652006-01-18 23:17:01 +010024#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020026#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <linux/scx200.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
Andres Salomonde8255c2010-12-30 20:27:33 -080032MODULE_ALIAS("platform:cs5535-smb");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_LICENSE("GPL");
34
35#define MAX_DEVICES 4
36static int base[MAX_DEVICES] = { 0x820, 0x840 };
David Howellsc78babc2017-04-04 16:54:23 +010037module_param_hw_array(base, int, ioport, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
39
Ben Gardnerf933ff52006-01-18 22:52:06 +010040#define POLL_TIMEOUT (HZ/5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42enum scx200_acb_state {
43 state_idle,
44 state_address,
45 state_command,
46 state_repeat_start,
47 state_quick,
48 state_read,
49 state_write,
50};
51
52static const char *scx200_acb_state_name[] = {
53 "idle",
54 "address",
55 "command",
56 "repeat_start",
57 "quick",
58 "read",
59 "write",
60};
61
62/* Physical interface */
Ben Gardner99c3adb2006-01-18 22:41:50 +010063struct scx200_acb_iface {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct scx200_acb_iface *next;
65 struct i2c_adapter adapter;
66 unsigned base;
Jean Delvare3fb9a652006-01-18 23:17:01 +010067 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* State machine data */
70 enum scx200_acb_state state;
71 int result;
72 u8 address_byte;
73 u8 command;
74 u8 *ptr;
75 char needs_reset;
76 unsigned len;
77};
78
79/* Register Definitions */
80#define ACBSDA (iface->base + 0)
81#define ACBST (iface->base + 1)
82#define ACBST_SDAST 0x40 /* SDA Status */
Ben Gardner99c3adb2006-01-18 22:41:50 +010083#define ACBST_BER 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define ACBST_NEGACK 0x10 /* Negative Acknowledge */
85#define ACBST_STASTR 0x08 /* Stall After Start */
86#define ACBST_MASTER 0x02
87#define ACBCST (iface->base + 2)
88#define ACBCST_BB 0x02
89#define ACBCTL1 (iface->base + 3)
90#define ACBCTL1_STASTRE 0x80
91#define ACBCTL1_NMINTE 0x40
Ben Gardner99c3adb2006-01-18 22:41:50 +010092#define ACBCTL1_ACK 0x10
93#define ACBCTL1_STOP 0x02
94#define ACBCTL1_START 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define ACBADDR (iface->base + 4)
96#define ACBCTL2 (iface->base + 5)
97#define ACBCTL2_ENABLE 0x01
98
99/************************************************************************/
100
101static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
102{
103 const char *errmsg;
104
Ben Gardneref4d9272006-01-18 22:43:10 +0100105 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
106 scx200_acb_state_name[iface->state], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (status & ACBST_BER) {
109 errmsg = "bus error";
110 goto error;
111 }
112 if (!(status & ACBST_MASTER)) {
113 errmsg = "not master";
114 goto error;
115 }
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100116 if (status & ACBST_NEGACK) {
117 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
118 scx200_acb_state_name[iface->state]);
119
120 iface->state = state_idle;
121 iface->result = -ENXIO;
122
123 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
124 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200125
126 /* Reset the status register */
127 outb(0, ACBST);
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100128 return;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 switch (iface->state) {
132 case state_idle:
133 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
134 break;
135
136 case state_address:
137 /* Do a pointer write first */
138 outb(iface->address_byte & ~1, ACBSDA);
139
140 iface->state = state_command;
141 break;
142
143 case state_command:
144 outb(iface->command, ACBSDA);
145
146 if (iface->address_byte & 1)
147 iface->state = state_repeat_start;
148 else
149 iface->state = state_write;
150 break;
151
152 case state_repeat_start:
153 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
Gustavo A. R. Silva4db7e172020-07-21 18:05:10 -0500154 fallthrough;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 case state_quick:
157 if (iface->address_byte & 1) {
Ben Gardner99c3adb2006-01-18 22:41:50 +0100158 if (iface->len == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
160 else
161 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
162 outb(iface->address_byte, ACBSDA);
163
164 iface->state = state_read;
165 } else {
166 outb(iface->address_byte, ACBSDA);
167
168 iface->state = state_write;
169 }
170 break;
171
172 case state_read:
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200173 /* Set ACK if _next_ byte will be the last one */
174 if (iface->len == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
176 else
177 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
178
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200179 if (iface->len == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 iface->result = 0;
181 iface->state = state_idle;
182 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
183 }
184
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200185 *iface->ptr++ = inb(ACBSDA);
186 --iface->len;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
189
190 case state_write:
191 if (iface->len == 0) {
192 iface->result = 0;
193 iface->state = state_idle;
194 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
195 break;
196 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 outb(*iface->ptr++, ACBSDA);
199 --iface->len;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202 }
203
204 return;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 error:
Willy Tarreaufce96f32009-09-18 22:45:47 +0200207 dev_err(&iface->adapter.dev,
208 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
209 scx200_acb_state_name[iface->state], iface->address_byte,
210 iface->len, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 iface->state = state_idle;
213 iface->result = -EIO;
214 iface->needs_reset = 1;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void scx200_acb_poll(struct scx200_acb_iface *iface)
218{
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100219 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned long timeout;
221
222 timeout = jiffies + POLL_TIMEOUT;
David Woodhouse3e3183b2006-08-05 12:15:19 -0700223 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 status = inb(ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200225
226 /* Reset the status register to avoid the hang */
227 outb(0, ACBST);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
230 scx200_acb_machine(iface, status);
231 return;
232 }
David Woodhouse3e3183b2006-08-05 12:15:19 -0700233 if (time_after(jiffies, timeout))
234 break;
235 cpu_relax();
236 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100239 dev_err(&iface->adapter.dev, "timeout in state %s\n",
240 scx200_acb_state_name[iface->state]);
241
242 iface->state = state_idle;
243 iface->result = -EIO;
244 iface->needs_reset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247static void scx200_acb_reset(struct scx200_acb_iface *iface)
248{
249 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100250 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 outb(0x70, ACBCTL2);
252 /* Polling mode */
253 outb(0, ACBCTL1);
254 /* Disable slave address */
255 outb(0, ACBADDR);
256 /* Enable the ACCESS.bus device */
257 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
258 /* Free STALL after START */
259 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
260 /* Send a STOP */
261 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
262 /* Clear BER, NEGACK and STASTR bits */
263 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
264 /* Clear BB bit */
265 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
266}
267
268static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
Ben Gardner99c3adb2006-01-18 22:41:50 +0100269 u16 address, unsigned short flags,
270 char rw, u8 command, int size,
271 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
274 int len;
275 u8 *buffer;
276 u16 cur_word;
277 int rc;
278
279 switch (size) {
280 case I2C_SMBUS_QUICK:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100281 len = 0;
282 buffer = NULL;
283 break;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 case I2C_SMBUS_BYTE:
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100286 len = 1;
287 buffer = rw ? &data->byte : &command;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100288 break;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case I2C_SMBUS_BYTE_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100291 len = 1;
292 buffer = &data->byte;
293 break;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case I2C_SMBUS_WORD_DATA:
296 len = 2;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100297 cur_word = cpu_to_le16(data->word);
298 buffer = (u8 *)&cur_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100300
Jean Delvarec3efacaa2006-07-01 17:06:43 +0200301 case I2C_SMBUS_I2C_BLOCK_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100302 len = data->block[0];
Jean Delvarec3efacaa2006-07-01 17:06:43 +0200303 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
304 return -EINVAL;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100305 buffer = &data->block[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 default:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Ben Gardneref4d9272006-01-18 22:43:10 +0100312 dev_dbg(&adapter->dev,
313 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
314 size, address, command, len, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (!len && rw == I2C_SMBUS_READ) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100317 dev_dbg(&adapter->dev, "zero length read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
319 }
320
Jean Delvare3fb9a652006-01-18 23:17:01 +0100321 mutex_lock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100323 iface->address_byte = (address << 1) | rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 iface->command = command;
325 iface->ptr = buffer;
326 iface->len = len;
327 iface->result = -EINVAL;
328 iface->needs_reset = 0;
329
330 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
331
332 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
333 iface->state = state_quick;
334 else
335 iface->state = state_address;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 while (iface->state != state_idle)
338 scx200_acb_poll(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (iface->needs_reset)
341 scx200_acb_reset(iface);
342
343 rc = iface->result;
344
Jean Delvare3fb9a652006-01-18 23:17:01 +0100345 mutex_unlock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
Ben Gardner99c3adb2006-01-18 22:41:50 +0100348 data->word = le16_to_cpu(cur_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350#ifdef DEBUG
Ben Gardneref4d9272006-01-18 22:43:10 +0100351 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (buffer) {
353 int i;
354 printk(" data:");
355 for (i = 0; i < len; ++i)
356 printk(" %02x", buffer[i]);
357 }
358 printk("\n");
359#endif
360
361 return rc;
362}
363
364static u32 scx200_acb_func(struct i2c_adapter *adapter)
365{
366 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
367 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
Jean Delvarec3efacaa2006-07-01 17:06:43 +0200368 I2C_FUNC_SMBUS_I2C_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371/* For now, we only handle combined mode (smbus) */
Jean Delvare8f9082c2006-09-03 22:39:46 +0200372static const struct i2c_algorithm scx200_acb_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 .smbus_xfer = scx200_acb_smbus_xfer,
374 .functionality = scx200_acb_func,
375};
376
377static struct scx200_acb_iface *scx200_acb_list;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200378static DEFINE_MUTEX(scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Bill Pemberton0b255e92012-11-27 15:59:38 -0500380static int scx200_acb_probe(struct scx200_acb_iface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 u8 val;
383
384 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100385 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 outb(0x70, ACBCTL2);
387
388 if (inb(ACBCTL2) != 0x70) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200389 pr_debug("ACBCTL2 readback failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -ENXIO;
391 }
392
393 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
394
395 val = inb(ACBCTL1);
396 if (val) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200397 pr_debug("disabled, but ACBCTL1=0x%02x\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENXIO;
399 }
400
401 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
402
403 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
404
405 val = inb(ACBCTL1);
406 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200407 pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
408 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return -ENXIO;
410 }
411
412 return 0;
413}
414
Bill Pemberton0b255e92012-11-27 15:59:38 -0500415static struct scx200_acb_iface *scx200_create_iface(const char *text,
Jean Delvare12a917f2007-02-13 22:09:03 +0100416 struct device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct scx200_acb_iface *iface;
419 struct i2c_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200421 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900422 if (!iface)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200423 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 adapter = &iface->adapter;
426 i2c_set_adapdata(adapter, iface);
David Brownell2096b952007-05-01 23:26:28 +0200427 snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 adapter->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 adapter->algo = &scx200_acb_algorithm;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200430 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare12a917f2007-02-13 22:09:03 +0100431 adapter->dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jean Delvare3fb9a652006-01-18 23:17:01 +0100433 mutex_init(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200435 return iface;
436}
437
Bill Pemberton0b255e92012-11-27 15:59:38 -0500438static int scx200_acb_create(struct scx200_acb_iface *iface)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200439{
440 struct i2c_adapter *adapter;
441 int rc;
442
443 adapter = &iface->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 rc = scx200_acb_probe(iface);
446 if (rc) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200447 pr_warn("probe failed\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200448 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 scx200_acb_reset(iface);
452
453 if (i2c_add_adapter(adapter) < 0) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200454 pr_err("failed to register\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200455 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Andres Salomonde8255c2010-12-30 20:27:33 -0800458 if (!adapter->dev.parent) {
459 /* If there's no dev, we're tracking (ISA) ifaces manually */
460 mutex_lock(&scx200_acb_list_mutex);
461 iface->next = scx200_acb_list;
462 scx200_acb_list = iface;
463 mutex_unlock(&scx200_acb_list_mutex);
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return 0;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Bill Pemberton0b255e92012-11-27 15:59:38 -0500469static struct scx200_acb_iface *scx200_create_dev(const char *text,
Andres Salomonde8255c2010-12-30 20:27:33 -0800470 unsigned long base, int index, struct device *dev)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200471{
472 struct scx200_acb_iface *iface;
473 int rc;
474
Andres Salomonde8255c2010-12-30 20:27:33 -0800475 iface = scx200_create_iface(text, dev, index);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200476
477 if (iface == NULL)
Andres Salomonde8255c2010-12-30 20:27:33 -0800478 return NULL;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200479
Adrian Bunkdec1a992008-04-22 22:16:48 +0200480 if (!request_region(base, 8, iface->adapter.name)) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200481 pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200482 goto errout_free;
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100483 }
484
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200485 iface->base = base;
486 rc = scx200_acb_create(iface);
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100487
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200488 if (rc == 0)
Andres Salomonde8255c2010-12-30 20:27:33 -0800489 return iface;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200490
491 release_region(base, 8);
492 errout_free:
493 kfree(iface);
Andres Salomonde8255c2010-12-30 20:27:33 -0800494 return NULL;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200495}
496
Bill Pemberton0b255e92012-11-27 15:59:38 -0500497static int scx200_probe(struct platform_device *pdev)
Andres Salomonde8255c2010-12-30 20:27:33 -0800498{
499 struct scx200_acb_iface *iface;
500 struct resource *res;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200501
Andres Salomonde8255c2010-12-30 20:27:33 -0800502 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
503 if (!res) {
504 dev_err(&pdev->dev, "can't fetch device resource info\n");
505 return -ENODEV;
506 }
507
508 iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
509 if (!iface)
510 return -EIO;
511
512 dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
513 iface->adapter.name);
514 platform_set_drvdata(pdev, iface);
515
516 return 0;
517}
518
Bill Pemberton0b255e92012-11-27 15:59:38 -0500519static void scx200_cleanup_iface(struct scx200_acb_iface *iface)
Andres Salomonde8255c2010-12-30 20:27:33 -0800520{
521 i2c_del_adapter(&iface->adapter);
522 release_region(iface->base, 8);
523 kfree(iface);
524}
525
Bill Pemberton0b255e92012-11-27 15:59:38 -0500526static int scx200_remove(struct platform_device *pdev)
Andres Salomonde8255c2010-12-30 20:27:33 -0800527{
528 struct scx200_acb_iface *iface;
529
530 iface = platform_get_drvdata(pdev);
Andres Salomonde8255c2010-12-30 20:27:33 -0800531 scx200_cleanup_iface(iface);
532
533 return 0;
534}
535
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100536static struct platform_driver scx200_pci_driver = {
Andres Salomonde8255c2010-12-30 20:27:33 -0800537 .driver = {
538 .name = "cs5535-smb",
Andres Salomonde8255c2010-12-30 20:27:33 -0800539 },
540 .probe = scx200_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500541 .remove = scx200_remove,
Andres Salomonde8255c2010-12-30 20:27:33 -0800542};
543
Jingoo Han392debf2013-12-03 08:11:20 +0900544static const struct pci_device_id scx200_isa[] = {
Andres Salomonde8255c2010-12-30 20:27:33 -0800545 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
546 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
Dzianis Kahanovich87acf5a2010-10-27 20:33:05 -0600547 { 0, }
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200548};
549
Andres Salomonde8255c2010-12-30 20:27:33 -0800550static __init void scx200_scan_isa(void)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200551{
Andres Salomonde8255c2010-12-30 20:27:33 -0800552 int i;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200553
Andres Salomonde8255c2010-12-30 20:27:33 -0800554 if (!pci_dev_present(scx200_isa))
555 return;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200556
Andres Salomonde8255c2010-12-30 20:27:33 -0800557 for (i = 0; i < MAX_DEVICES; ++i) {
558 if (base[i] == 0)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200559 continue;
560
Andres Salomonde8255c2010-12-30 20:27:33 -0800561 /* XXX: should we care about failures? */
562 scx200_create_dev("SCx200", base[i], i, NULL);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200563 }
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static int __init scx200_acb_init(void)
567{
Jim Cromie401e72b2012-10-05 22:23:52 +0200568 pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Andres Salomonde8255c2010-12-30 20:27:33 -0800570 /* First scan for ISA-based devices */
571 scx200_scan_isa(); /* XXX: should we care about errors? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Jean Delvare6f9c2962006-04-26 22:50:32 +0200573 /* If at least one bus was created, init must succeed */
574 if (scx200_acb_list)
575 return 0;
Andres Salomonde8255c2010-12-30 20:27:33 -0800576
577 /* No ISA devices; register the platform driver for PCI-based devices */
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100578 return platform_driver_register(&scx200_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static void __exit scx200_acb_cleanup(void)
582{
583 struct scx200_acb_iface *iface;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100584
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100585 platform_driver_unregister(&scx200_pci_driver);
Andres Salomonde8255c2010-12-30 20:27:33 -0800586
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200587 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 while ((iface = scx200_acb_list) != NULL) {
589 scx200_acb_list = iface->next;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200590 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Andres Salomonde8255c2010-12-30 20:27:33 -0800592 scx200_cleanup_iface(iface);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200593
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200594 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200596 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599module_init(scx200_acb_init);
600module_exit(scx200_acb_cleanup);