gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / i2c / busses / scx200_acb.c
blobbd9afa383d12d9be8872d9fa06207eee6a46b37a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
5 National Semiconductor SCx200 ACCESS.bus support
6 Also supports the AMD CS5535 and AMD CS5536
8 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>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/io.h>
28 #include <linux/scx200.h>
30 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
32 MODULE_ALIAS("platform:cs5535-smb");
33 MODULE_LICENSE("GPL");
35 #define MAX_DEVICES 4
36 static int base[MAX_DEVICES] = { 0x820, 0x840 };
37 module_param_hw_array(base, int, ioport, NULL, 0);
38 MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
40 #define POLL_TIMEOUT (HZ/5)
42 enum 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,
52 static const char *scx200_acb_state_name[] = {
53 "idle",
54 "address",
55 "command",
56 "repeat_start",
57 "quick",
58 "read",
59 "write",
62 /* Physical interface */
63 struct scx200_acb_iface {
64 struct scx200_acb_iface *next;
65 struct i2c_adapter adapter;
66 unsigned base;
67 struct mutex mutex;
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;
79 /* Register Definitions */
80 #define ACBSDA (iface->base + 0)
81 #define ACBST (iface->base + 1)
82 #define ACBST_SDAST 0x40 /* SDA Status */
83 #define ACBST_BER 0x20
84 #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
92 #define ACBCTL1_ACK 0x10
93 #define ACBCTL1_STOP 0x02
94 #define ACBCTL1_START 0x01
95 #define ACBADDR (iface->base + 4)
96 #define ACBCTL2 (iface->base + 5)
97 #define ACBCTL2_ENABLE 0x01
99 /************************************************************************/
101 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
103 const char *errmsg;
105 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
106 scx200_acb_state_name[iface->state], status);
108 if (status & ACBST_BER) {
109 errmsg = "bus error";
110 goto error;
112 if (!(status & ACBST_MASTER)) {
113 errmsg = "not master";
114 goto error;
116 if (status & ACBST_NEGACK) {
117 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
118 scx200_acb_state_name[iface->state]);
120 iface->state = state_idle;
121 iface->result = -ENXIO;
123 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
124 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
126 /* Reset the status register */
127 outb(0, ACBST);
128 return;
131 switch (iface->state) {
132 case state_idle:
133 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
134 break;
136 case state_address:
137 /* Do a pointer write first */
138 outb(iface->address_byte & ~1, ACBSDA);
140 iface->state = state_command;
141 break;
143 case state_command:
144 outb(iface->command, ACBSDA);
146 if (iface->address_byte & 1)
147 iface->state = state_repeat_start;
148 else
149 iface->state = state_write;
150 break;
152 case state_repeat_start:
153 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
154 /* fallthrough */
156 case state_quick:
157 if (iface->address_byte & 1) {
158 if (iface->len == 1)
159 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
160 else
161 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
162 outb(iface->address_byte, ACBSDA);
164 iface->state = state_read;
165 } else {
166 outb(iface->address_byte, ACBSDA);
168 iface->state = state_write;
170 break;
172 case state_read:
173 /* Set ACK if _next_ byte will be the last one */
174 if (iface->len == 2)
175 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
176 else
177 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
179 if (iface->len == 1) {
180 iface->result = 0;
181 iface->state = state_idle;
182 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
185 *iface->ptr++ = inb(ACBSDA);
186 --iface->len;
188 break;
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;
198 outb(*iface->ptr++, ACBSDA);
199 --iface->len;
201 break;
204 return;
206 error:
207 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);
212 iface->state = state_idle;
213 iface->result = -EIO;
214 iface->needs_reset = 1;
217 static void scx200_acb_poll(struct scx200_acb_iface *iface)
219 u8 status;
220 unsigned long timeout;
222 timeout = jiffies + POLL_TIMEOUT;
223 while (1) {
224 status = inb(ACBST);
226 /* Reset the status register to avoid the hang */
227 outb(0, ACBST);
229 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
230 scx200_acb_machine(iface, status);
231 return;
233 if (time_after(jiffies, timeout))
234 break;
235 cpu_relax();
236 cond_resched();
239 dev_err(&iface->adapter.dev, "timeout in state %s\n",
240 scx200_acb_state_name[iface->state]);
242 iface->state = state_idle;
243 iface->result = -EIO;
244 iface->needs_reset = 1;
247 static void scx200_acb_reset(struct scx200_acb_iface *iface)
249 /* Disable the ACCESS.bus device and Configure the SCL
250 frequency: 16 clock cycles */
251 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);
268 static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
269 u16 address, unsigned short flags,
270 char rw, u8 command, int size,
271 union i2c_smbus_data *data)
273 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
274 int len;
275 u8 *buffer;
276 u16 cur_word;
277 int rc;
279 switch (size) {
280 case I2C_SMBUS_QUICK:
281 len = 0;
282 buffer = NULL;
283 break;
285 case I2C_SMBUS_BYTE:
286 len = 1;
287 buffer = rw ? &data->byte : &command;
288 break;
290 case I2C_SMBUS_BYTE_DATA:
291 len = 1;
292 buffer = &data->byte;
293 break;
295 case I2C_SMBUS_WORD_DATA:
296 len = 2;
297 cur_word = cpu_to_le16(data->word);
298 buffer = (u8 *)&cur_word;
299 break;
301 case I2C_SMBUS_I2C_BLOCK_DATA:
302 len = data->block[0];
303 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
304 return -EINVAL;
305 buffer = &data->block[1];
306 break;
308 default:
309 return -EINVAL;
312 dev_dbg(&adapter->dev,
313 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
314 size, address, command, len, rw);
316 if (!len && rw == I2C_SMBUS_READ) {
317 dev_dbg(&adapter->dev, "zero length read\n");
318 return -EINVAL;
321 mutex_lock(&iface->mutex);
323 iface->address_byte = (address << 1) | rw;
324 iface->command = command;
325 iface->ptr = buffer;
326 iface->len = len;
327 iface->result = -EINVAL;
328 iface->needs_reset = 0;
330 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
332 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
333 iface->state = state_quick;
334 else
335 iface->state = state_address;
337 while (iface->state != state_idle)
338 scx200_acb_poll(iface);
340 if (iface->needs_reset)
341 scx200_acb_reset(iface);
343 rc = iface->result;
345 mutex_unlock(&iface->mutex);
347 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
348 data->word = le16_to_cpu(cur_word);
350 #ifdef DEBUG
351 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
352 if (buffer) {
353 int i;
354 printk(" data:");
355 for (i = 0; i < len; ++i)
356 printk(" %02x", buffer[i]);
358 printk("\n");
359 #endif
361 return rc;
364 static u32 scx200_acb_func(struct i2c_adapter *adapter)
366 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
367 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
368 I2C_FUNC_SMBUS_I2C_BLOCK;
371 /* For now, we only handle combined mode (smbus) */
372 static const struct i2c_algorithm scx200_acb_algorithm = {
373 .smbus_xfer = scx200_acb_smbus_xfer,
374 .functionality = scx200_acb_func,
377 static struct scx200_acb_iface *scx200_acb_list;
378 static DEFINE_MUTEX(scx200_acb_list_mutex);
380 static int scx200_acb_probe(struct scx200_acb_iface *iface)
382 u8 val;
384 /* Disable the ACCESS.bus device and Configure the SCL
385 frequency: 16 clock cycles */
386 outb(0x70, ACBCTL2);
388 if (inb(ACBCTL2) != 0x70) {
389 pr_debug("ACBCTL2 readback failed\n");
390 return -ENXIO;
393 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
395 val = inb(ACBCTL1);
396 if (val) {
397 pr_debug("disabled, but ACBCTL1=0x%02x\n", val);
398 return -ENXIO;
401 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
403 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
405 val = inb(ACBCTL1);
406 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
407 pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
408 val);
409 return -ENXIO;
412 return 0;
415 static struct scx200_acb_iface *scx200_create_iface(const char *text,
416 struct device *dev, int index)
418 struct scx200_acb_iface *iface;
419 struct i2c_adapter *adapter;
421 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
422 if (!iface)
423 return NULL;
425 adapter = &iface->adapter;
426 i2c_set_adapdata(adapter, iface);
427 snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
428 adapter->owner = THIS_MODULE;
429 adapter->algo = &scx200_acb_algorithm;
430 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
431 adapter->dev.parent = dev;
433 mutex_init(&iface->mutex);
435 return iface;
438 static int scx200_acb_create(struct scx200_acb_iface *iface)
440 struct i2c_adapter *adapter;
441 int rc;
443 adapter = &iface->adapter;
445 rc = scx200_acb_probe(iface);
446 if (rc) {
447 pr_warn("probe failed\n");
448 return rc;
451 scx200_acb_reset(iface);
453 if (i2c_add_adapter(adapter) < 0) {
454 pr_err("failed to register\n");
455 return -ENODEV;
458 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);
466 return 0;
469 static struct scx200_acb_iface *scx200_create_dev(const char *text,
470 unsigned long base, int index, struct device *dev)
472 struct scx200_acb_iface *iface;
473 int rc;
475 iface = scx200_create_iface(text, dev, index);
477 if (iface == NULL)
478 return NULL;
480 if (!request_region(base, 8, iface->adapter.name)) {
481 pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1);
482 goto errout_free;
485 iface->base = base;
486 rc = scx200_acb_create(iface);
488 if (rc == 0)
489 return iface;
491 release_region(base, 8);
492 errout_free:
493 kfree(iface);
494 return NULL;
497 static int scx200_probe(struct platform_device *pdev)
499 struct scx200_acb_iface *iface;
500 struct resource *res;
502 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;
508 iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
509 if (!iface)
510 return -EIO;
512 dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
513 iface->adapter.name);
514 platform_set_drvdata(pdev, iface);
516 return 0;
519 static void scx200_cleanup_iface(struct scx200_acb_iface *iface)
521 i2c_del_adapter(&iface->adapter);
522 release_region(iface->base, 8);
523 kfree(iface);
526 static int scx200_remove(struct platform_device *pdev)
528 struct scx200_acb_iface *iface;
530 iface = platform_get_drvdata(pdev);
531 scx200_cleanup_iface(iface);
533 return 0;
536 static struct platform_driver scx200_pci_driver = {
537 .driver = {
538 .name = "cs5535-smb",
540 .probe = scx200_probe,
541 .remove = scx200_remove,
544 static const struct pci_device_id scx200_isa[] = {
545 { 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) },
547 { 0, }
550 static __init void scx200_scan_isa(void)
552 int i;
554 if (!pci_dev_present(scx200_isa))
555 return;
557 for (i = 0; i < MAX_DEVICES; ++i) {
558 if (base[i] == 0)
559 continue;
561 /* XXX: should we care about failures? */
562 scx200_create_dev("SCx200", base[i], i, NULL);
566 static int __init scx200_acb_init(void)
568 pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
570 /* First scan for ISA-based devices */
571 scx200_scan_isa(); /* XXX: should we care about errors? */
573 /* If at least one bus was created, init must succeed */
574 if (scx200_acb_list)
575 return 0;
577 /* No ISA devices; register the platform driver for PCI-based devices */
578 return platform_driver_register(&scx200_pci_driver);
581 static void __exit scx200_acb_cleanup(void)
583 struct scx200_acb_iface *iface;
585 platform_driver_unregister(&scx200_pci_driver);
587 mutex_lock(&scx200_acb_list_mutex);
588 while ((iface = scx200_acb_list) != NULL) {
589 scx200_acb_list = iface->next;
590 mutex_unlock(&scx200_acb_list_mutex);
592 scx200_cleanup_iface(iface);
594 mutex_lock(&scx200_acb_list_mutex);
596 mutex_unlock(&scx200_acb_list_mutex);
599 module_init(scx200_acb_init);
600 module_exit(scx200_acb_cleanup);