blob: 0a7e410b619529a44fa17706fe4ced3c9197c8ba [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020*/
21
Jim Cromie401e72b2012-10-05 22:23:52 +020022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/errno.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pci.h>
Andres Salomonde8255c2010-12-30 20:27:33 -080030#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
Jean Delvare3fb9a652006-01-18 23:17:01 +010032#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020034#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/scx200.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
39MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
Andres Salomonde8255c2010-12-30 20:27:33 -080040MODULE_ALIAS("platform:cs5535-smb");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041MODULE_LICENSE("GPL");
42
43#define MAX_DEVICES 4
44static int base[MAX_DEVICES] = { 0x820, 0x840 };
45module_param_array(base, int, NULL, 0);
46MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
47
Ben Gardnerf933ff52006-01-18 22:52:06 +010048#define POLL_TIMEOUT (HZ/5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50enum scx200_acb_state {
51 state_idle,
52 state_address,
53 state_command,
54 state_repeat_start,
55 state_quick,
56 state_read,
57 state_write,
58};
59
60static const char *scx200_acb_state_name[] = {
61 "idle",
62 "address",
63 "command",
64 "repeat_start",
65 "quick",
66 "read",
67 "write",
68};
69
70/* Physical interface */
Ben Gardner99c3adb2006-01-18 22:41:50 +010071struct scx200_acb_iface {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct scx200_acb_iface *next;
73 struct i2c_adapter adapter;
74 unsigned base;
Jean Delvare3fb9a652006-01-18 23:17:01 +010075 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 /* State machine data */
78 enum scx200_acb_state state;
79 int result;
80 u8 address_byte;
81 u8 command;
82 u8 *ptr;
83 char needs_reset;
84 unsigned len;
85};
86
87/* Register Definitions */
88#define ACBSDA (iface->base + 0)
89#define ACBST (iface->base + 1)
90#define ACBST_SDAST 0x40 /* SDA Status */
Ben Gardner99c3adb2006-01-18 22:41:50 +010091#define ACBST_BER 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define ACBST_NEGACK 0x10 /* Negative Acknowledge */
93#define ACBST_STASTR 0x08 /* Stall After Start */
94#define ACBST_MASTER 0x02
95#define ACBCST (iface->base + 2)
96#define ACBCST_BB 0x02
97#define ACBCTL1 (iface->base + 3)
98#define ACBCTL1_STASTRE 0x80
99#define ACBCTL1_NMINTE 0x40
Ben Gardner99c3adb2006-01-18 22:41:50 +0100100#define ACBCTL1_ACK 0x10
101#define ACBCTL1_STOP 0x02
102#define ACBCTL1_START 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define ACBADDR (iface->base + 4)
104#define ACBCTL2 (iface->base + 5)
105#define ACBCTL2_ENABLE 0x01
106
107/************************************************************************/
108
109static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
110{
111 const char *errmsg;
112
Ben Gardneref4d9272006-01-18 22:43:10 +0100113 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
114 scx200_acb_state_name[iface->state], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (status & ACBST_BER) {
117 errmsg = "bus error";
118 goto error;
119 }
120 if (!(status & ACBST_MASTER)) {
121 errmsg = "not master";
122 goto error;
123 }
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100124 if (status & ACBST_NEGACK) {
125 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
126 scx200_acb_state_name[iface->state]);
127
128 iface->state = state_idle;
129 iface->result = -ENXIO;
130
131 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
132 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200133
134 /* Reset the status register */
135 outb(0, ACBST);
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100136 return;
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 switch (iface->state) {
140 case state_idle:
141 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
142 break;
143
144 case state_address:
145 /* Do a pointer write first */
146 outb(iface->address_byte & ~1, ACBSDA);
147
148 iface->state = state_command;
149 break;
150
151 case state_command:
152 outb(iface->command, ACBSDA);
153
154 if (iface->address_byte & 1)
155 iface->state = state_repeat_start;
156 else
157 iface->state = state_write;
158 break;
159
160 case state_repeat_start:
161 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
162 /* fallthrough */
Ben Gardner99c3adb2006-01-18 22:41:50 +0100163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 case state_quick:
165 if (iface->address_byte & 1) {
Ben Gardner99c3adb2006-01-18 22:41:50 +0100166 if (iface->len == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
168 else
169 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
170 outb(iface->address_byte, ACBSDA);
171
172 iface->state = state_read;
173 } else {
174 outb(iface->address_byte, ACBSDA);
175
176 iface->state = state_write;
177 }
178 break;
179
180 case state_read:
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200181 /* Set ACK if _next_ byte will be the last one */
182 if (iface->len == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
184 else
185 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
186
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200187 if (iface->len == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 iface->result = 0;
189 iface->state = state_idle;
190 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
191 }
192
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200193 *iface->ptr++ = inb(ACBSDA);
194 --iface->len;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197
198 case state_write:
199 if (iface->len == 0) {
200 iface->result = 0;
201 iface->state = state_idle;
202 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
203 break;
204 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 outb(*iface->ptr++, ACBSDA);
207 --iface->len;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 break;
210 }
211
212 return;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 error:
Willy Tarreaufce96f32009-09-18 22:45:47 +0200215 dev_err(&iface->adapter.dev,
216 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
217 scx200_acb_state_name[iface->state], iface->address_byte,
218 iface->len, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 iface->state = state_idle;
221 iface->result = -EIO;
222 iface->needs_reset = 1;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void scx200_acb_poll(struct scx200_acb_iface *iface)
226{
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100227 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned long timeout;
229
230 timeout = jiffies + POLL_TIMEOUT;
David Woodhouse3e3183b2006-08-05 12:15:19 -0700231 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 status = inb(ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200233
234 /* Reset the status register to avoid the hang */
235 outb(0, ACBST);
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
238 scx200_acb_machine(iface, status);
239 return;
240 }
David Woodhouse3e3183b2006-08-05 12:15:19 -0700241 if (time_after(jiffies, timeout))
242 break;
243 cpu_relax();
244 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100247 dev_err(&iface->adapter.dev, "timeout in state %s\n",
248 scx200_acb_state_name[iface->state]);
249
250 iface->state = state_idle;
251 iface->result = -EIO;
252 iface->needs_reset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255static void scx200_acb_reset(struct scx200_acb_iface *iface)
256{
257 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100258 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 outb(0x70, ACBCTL2);
260 /* Polling mode */
261 outb(0, ACBCTL1);
262 /* Disable slave address */
263 outb(0, ACBADDR);
264 /* Enable the ACCESS.bus device */
265 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
266 /* Free STALL after START */
267 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
268 /* Send a STOP */
269 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
270 /* Clear BER, NEGACK and STASTR bits */
271 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
272 /* Clear BB bit */
273 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
274}
275
276static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
Ben Gardner99c3adb2006-01-18 22:41:50 +0100277 u16 address, unsigned short flags,
278 char rw, u8 command, int size,
279 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
282 int len;
283 u8 *buffer;
284 u16 cur_word;
285 int rc;
286
287 switch (size) {
288 case I2C_SMBUS_QUICK:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100289 len = 0;
290 buffer = NULL;
291 break;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 case I2C_SMBUS_BYTE:
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100294 len = 1;
295 buffer = rw ? &data->byte : &command;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100296 break;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case I2C_SMBUS_BYTE_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100299 len = 1;
300 buffer = &data->byte;
301 break;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 case I2C_SMBUS_WORD_DATA:
304 len = 2;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100305 cur_word = cpu_to_le16(data->word);
306 buffer = (u8 *)&cur_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100308
Jean Delvarec3efaca2006-07-01 17:06:43 +0200309 case I2C_SMBUS_I2C_BLOCK_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100310 len = data->block[0];
Jean Delvarec3efaca2006-07-01 17:06:43 +0200311 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
312 return -EINVAL;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100313 buffer = &data->block[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 default:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Ben Gardneref4d9272006-01-18 22:43:10 +0100320 dev_dbg(&adapter->dev,
321 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
322 size, address, command, len, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (!len && rw == I2C_SMBUS_READ) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100325 dev_dbg(&adapter->dev, "zero length read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EINVAL;
327 }
328
Jean Delvare3fb9a652006-01-18 23:17:01 +0100329 mutex_lock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100331 iface->address_byte = (address << 1) | rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 iface->command = command;
333 iface->ptr = buffer;
334 iface->len = len;
335 iface->result = -EINVAL;
336 iface->needs_reset = 0;
337
338 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
339
340 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
341 iface->state = state_quick;
342 else
343 iface->state = state_address;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 while (iface->state != state_idle)
346 scx200_acb_poll(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (iface->needs_reset)
349 scx200_acb_reset(iface);
350
351 rc = iface->result;
352
Jean Delvare3fb9a652006-01-18 23:17:01 +0100353 mutex_unlock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
Ben Gardner99c3adb2006-01-18 22:41:50 +0100356 data->word = le16_to_cpu(cur_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358#ifdef DEBUG
Ben Gardneref4d9272006-01-18 22:43:10 +0100359 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (buffer) {
361 int i;
362 printk(" data:");
363 for (i = 0; i < len; ++i)
364 printk(" %02x", buffer[i]);
365 }
366 printk("\n");
367#endif
368
369 return rc;
370}
371
372static u32 scx200_acb_func(struct i2c_adapter *adapter)
373{
374 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
375 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
Jean Delvarec3efaca2006-07-01 17:06:43 +0200376 I2C_FUNC_SMBUS_I2C_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/* For now, we only handle combined mode (smbus) */
Jean Delvare8f9082c2006-09-03 22:39:46 +0200380static const struct i2c_algorithm scx200_acb_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .smbus_xfer = scx200_acb_smbus_xfer,
382 .functionality = scx200_acb_func,
383};
384
385static struct scx200_acb_iface *scx200_acb_list;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200386static DEFINE_MUTEX(scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Bill Pemberton0b255e92012-11-27 15:59:38 -0500388static int scx200_acb_probe(struct scx200_acb_iface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 u8 val;
391
392 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100393 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 outb(0x70, ACBCTL2);
395
396 if (inb(ACBCTL2) != 0x70) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200397 pr_debug("ACBCTL2 readback failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENXIO;
399 }
400
401 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
402
403 val = inb(ACBCTL1);
404 if (val) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200405 pr_debug("disabled, but ACBCTL1=0x%02x\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -ENXIO;
407 }
408
409 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
410
411 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
412
413 val = inb(ACBCTL1);
414 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200415 pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
416 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -ENXIO;
418 }
419
420 return 0;
421}
422
Bill Pemberton0b255e92012-11-27 15:59:38 -0500423static struct scx200_acb_iface *scx200_create_iface(const char *text,
Jean Delvare12a917f2007-02-13 22:09:03 +0100424 struct device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct scx200_acb_iface *iface;
427 struct i2c_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200429 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900430 if (!iface)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200431 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 adapter = &iface->adapter;
434 i2c_set_adapdata(adapter, iface);
David Brownell2096b952007-05-01 23:26:28 +0200435 snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 adapter->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 adapter->algo = &scx200_acb_algorithm;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200438 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare12a917f2007-02-13 22:09:03 +0100439 adapter->dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Jean Delvare3fb9a652006-01-18 23:17:01 +0100441 mutex_init(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200443 return iface;
444}
445
Bill Pemberton0b255e92012-11-27 15:59:38 -0500446static int scx200_acb_create(struct scx200_acb_iface *iface)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200447{
448 struct i2c_adapter *adapter;
449 int rc;
450
451 adapter = &iface->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 rc = scx200_acb_probe(iface);
454 if (rc) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200455 pr_warn("probe failed\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200456 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 scx200_acb_reset(iface);
460
461 if (i2c_add_adapter(adapter) < 0) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200462 pr_err("failed to register\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200463 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Andres Salomonde8255c2010-12-30 20:27:33 -0800466 if (!adapter->dev.parent) {
467 /* If there's no dev, we're tracking (ISA) ifaces manually */
468 mutex_lock(&scx200_acb_list_mutex);
469 iface->next = scx200_acb_list;
470 scx200_acb_list = iface;
471 mutex_unlock(&scx200_acb_list_mutex);
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 return 0;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Bill Pemberton0b255e92012-11-27 15:59:38 -0500477static struct scx200_acb_iface *scx200_create_dev(const char *text,
Andres Salomonde8255c2010-12-30 20:27:33 -0800478 unsigned long base, int index, struct device *dev)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200479{
480 struct scx200_acb_iface *iface;
481 int rc;
482
Andres Salomonde8255c2010-12-30 20:27:33 -0800483 iface = scx200_create_iface(text, dev, index);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200484
485 if (iface == NULL)
Andres Salomonde8255c2010-12-30 20:27:33 -0800486 return NULL;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200487
Adrian Bunkdec1a992008-04-22 22:16:48 +0200488 if (!request_region(base, 8, iface->adapter.name)) {
Jim Cromie401e72b2012-10-05 22:23:52 +0200489 pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200490 goto errout_free;
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100491 }
492
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200493 iface->base = base;
494 rc = scx200_acb_create(iface);
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100495
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200496 if (rc == 0)
Andres Salomonde8255c2010-12-30 20:27:33 -0800497 return iface;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200498
499 release_region(base, 8);
500 errout_free:
501 kfree(iface);
Andres Salomonde8255c2010-12-30 20:27:33 -0800502 return NULL;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200503}
504
Bill Pemberton0b255e92012-11-27 15:59:38 -0500505static int scx200_probe(struct platform_device *pdev)
Andres Salomonde8255c2010-12-30 20:27:33 -0800506{
507 struct scx200_acb_iface *iface;
508 struct resource *res;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200509
Andres Salomonde8255c2010-12-30 20:27:33 -0800510 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
511 if (!res) {
512 dev_err(&pdev->dev, "can't fetch device resource info\n");
513 return -ENODEV;
514 }
515
516 iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
517 if (!iface)
518 return -EIO;
519
520 dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
521 iface->adapter.name);
522 platform_set_drvdata(pdev, iface);
523
524 return 0;
525}
526
Bill Pemberton0b255e92012-11-27 15:59:38 -0500527static void scx200_cleanup_iface(struct scx200_acb_iface *iface)
Andres Salomonde8255c2010-12-30 20:27:33 -0800528{
529 i2c_del_adapter(&iface->adapter);
530 release_region(iface->base, 8);
531 kfree(iface);
532}
533
Bill Pemberton0b255e92012-11-27 15:59:38 -0500534static int scx200_remove(struct platform_device *pdev)
Andres Salomonde8255c2010-12-30 20:27:33 -0800535{
536 struct scx200_acb_iface *iface;
537
538 iface = platform_get_drvdata(pdev);
Andres Salomonde8255c2010-12-30 20:27:33 -0800539 scx200_cleanup_iface(iface);
540
541 return 0;
542}
543
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100544static struct platform_driver scx200_pci_driver = {
Andres Salomonde8255c2010-12-30 20:27:33 -0800545 .driver = {
546 .name = "cs5535-smb",
Andres Salomonde8255c2010-12-30 20:27:33 -0800547 },
548 .probe = scx200_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500549 .remove = scx200_remove,
Andres Salomonde8255c2010-12-30 20:27:33 -0800550};
551
Jingoo Han392debf2013-12-03 08:11:20 +0900552static const struct pci_device_id scx200_isa[] = {
Andres Salomonde8255c2010-12-30 20:27:33 -0800553 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
554 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
Dzianis Kahanovich87acf5a2010-10-27 20:33:05 -0600555 { 0, }
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200556};
557
Andres Salomonde8255c2010-12-30 20:27:33 -0800558static __init void scx200_scan_isa(void)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200559{
Andres Salomonde8255c2010-12-30 20:27:33 -0800560 int i;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200561
Andres Salomonde8255c2010-12-30 20:27:33 -0800562 if (!pci_dev_present(scx200_isa))
563 return;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200564
Andres Salomonde8255c2010-12-30 20:27:33 -0800565 for (i = 0; i < MAX_DEVICES; ++i) {
566 if (base[i] == 0)
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200567 continue;
568
Andres Salomonde8255c2010-12-30 20:27:33 -0800569 /* XXX: should we care about failures? */
570 scx200_create_dev("SCx200", base[i], i, NULL);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200571 }
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static int __init scx200_acb_init(void)
575{
Jim Cromie401e72b2012-10-05 22:23:52 +0200576 pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Andres Salomonde8255c2010-12-30 20:27:33 -0800578 /* First scan for ISA-based devices */
579 scx200_scan_isa(); /* XXX: should we care about errors? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Jean Delvare6f9c2962006-04-26 22:50:32 +0200581 /* If at least one bus was created, init must succeed */
582 if (scx200_acb_list)
583 return 0;
Andres Salomonde8255c2010-12-30 20:27:33 -0800584
585 /* No ISA devices; register the platform driver for PCI-based devices */
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100586 return platform_driver_register(&scx200_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589static void __exit scx200_acb_cleanup(void)
590{
591 struct scx200_acb_iface *iface;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100592
Harvey Yang6fcf84a2011-10-30 13:47:25 +0100593 platform_driver_unregister(&scx200_pci_driver);
Andres Salomonde8255c2010-12-30 20:27:33 -0800594
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200595 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 while ((iface = scx200_acb_list) != NULL) {
597 scx200_acb_list = iface->next;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200598 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Andres Salomonde8255c2010-12-30 20:27:33 -0800600 scx200_cleanup_iface(iface);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200601
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200602 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200604 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607module_init(scx200_acb_init);
608module_exit(scx200_acb_cleanup);