blob: 5c2379522aa9fb8d9e8c0e1a6635e4429443289f [file] [log] [blame]
Roel Kluin0c168ce2009-03-28 21:34:42 +01001/*
2 * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
3 *
4 * Copyright (C) 1995-1997 Simon G. Vogl
5 * 1998-2000 Hans Berglund
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Jean Delvare5694f8a2012-03-26 21:47:19 +020019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301 USA.
Roel Kluin0c168ce2009-03-28 21:34:42 +010021 *
22 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
23 * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
24 * <mbailey@littlefeet-inc.com>
25 *
26 * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
27 * messages, proper stop/repstart signaling during receive, added detect code
28 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
34#include <linux/errno.h>
35#include <linux/i2c.h>
36#include <linux/i2c-algo-pcf.h>
37#include "i2c-algo-pcf.h"
38
39
Roel Kluin0c168ce2009-03-28 21:34:42 +010040#define DEB2(x) if (i2c_debug >= 2) x
41#define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */
42#define DEBPROTO(x) if (i2c_debug >= 9) x;
43 /* debug the protocol by showing transferred bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define DEF_TIMEOUT 16
45
Roel Kluin0c168ce2009-03-28 21:34:42 +010046/*
47 * module parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49static int i2c_debug;
50
Roel Kluin0c168ce2009-03-28 21:34:42 +010051/* setting states on the bus with the right timing: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
54#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
55#define get_own(adap) adap->getown(adap->data)
56#define get_clock(adap) adap->getclock(adap->data)
57#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
58#define i2c_inb(adap) adap->getpcf(adap->data, 0)
59
Roel Kluin0c168ce2009-03-28 21:34:42 +010060/* other auxiliary functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Roel Kluin0c168ce2009-03-28 21:34:42 +010062static void i2c_start(struct i2c_algo_pcf_data *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Frank Seidel154d22b2009-03-28 21:34:42 +010064 DEBPROTO(printk(KERN_DEBUG "S "));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 set_pcf(adap, 1, I2C_PCF_START);
66}
67
Roel Kluin0c168ce2009-03-28 21:34:42 +010068static void i2c_repstart(struct i2c_algo_pcf_data *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 DEBPROTO(printk(" Sr "));
71 set_pcf(adap, 1, I2C_PCF_REPSTART);
72}
73
Roel Kluin0c168ce2009-03-28 21:34:42 +010074static void i2c_stop(struct i2c_algo_pcf_data *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 DEBPROTO(printk("P\n"));
77 set_pcf(adap, 1, I2C_PCF_STOP);
78}
79
Eric Brower0573d112008-07-14 22:38:31 +020080static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
81{
82 DEB2(printk(KERN_INFO
83 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
Roel Kluin0c168ce2009-03-28 21:34:42 +010084 *status));
85 /*
86 * Cleanup from LAB -- reset and enable ESO.
Eric Brower0573d112008-07-14 22:38:31 +020087 * This resets the PCF8584; since we've lost the bus, no
88 * further attempts should be made by callers to clean up
89 * (no i2c_stop() etc.)
90 */
91 set_pcf(adap, 1, I2C_PCF_PIN);
92 set_pcf(adap, 1, I2C_PCF_ESO);
Roel Kluin0c168ce2009-03-28 21:34:42 +010093 /*
94 * We pause for a time period sufficient for any running
Eric Brower0573d112008-07-14 22:38:31 +020095 * I2C transaction to complete -- the arbitration logic won't
96 * work properly until the next START is seen.
97 * It is assumed the bus driver or client has set a proper value.
98 *
99 * REVISIT: should probably use msleep instead of mdelay if we
100 * know we can sleep.
101 */
102 if (adap->lab_mdelay)
103 mdelay(adap->lab_mdelay);
104
105 DEB2(printk(KERN_INFO
106 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
107 get_pcf(adap, 1)));
108}
109
Roel Kluin0c168ce2009-03-28 21:34:42 +0100110static int wait_for_bb(struct i2c_algo_pcf_data *adap)
111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 int timeout = DEF_TIMEOUT;
114 int status;
115
116 status = get_pcf(adap, 1);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100117
Roel Kluin94d78e12009-03-28 21:34:42 +0100118 while (!(status & I2C_PCF_BB) && --timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 udelay(100); /* wait for 100 us */
120 status = get_pcf(adap, 1);
121 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100122
Roel Kluin94d78e12009-03-28 21:34:42 +0100123 if (timeout == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
Roel Kluin94d78e12009-03-28 21:34:42 +0100125 return -ETIMEDOUT;
126 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100127
Roel Kluin94d78e12009-03-28 21:34:42 +0100128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Roel Kluin0c168ce2009-03-28 21:34:42 +0100131static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
132{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 int timeout = DEF_TIMEOUT;
135
136 *status = get_pcf(adap, 1);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100137
Roel Kluin94d78e12009-03-28 21:34:42 +0100138 while ((*status & I2C_PCF_PIN) && --timeout) {
David Miller08e53382008-10-22 20:21:29 +0200139 adap->waitforpin(adap->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *status = get_pcf(adap, 1);
141 }
142 if (*status & I2C_PCF_LAB) {
Eric Brower0573d112008-07-14 22:38:31 +0200143 handle_lab(adap, status);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100144 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100146
Roel Kluin94d78e12009-03-28 21:34:42 +0100147 if (timeout == 0)
148 return -ETIMEDOUT;
149
150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Roel Kluin0c168ce2009-03-28 21:34:42 +0100153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * This should perform the 'PCF8584 initialization sequence' as described
155 * in the Philips IC12 data book (1995, Aug 29).
156 * There should be a 30 clock cycle wait after reset, I assume this
157 * has been fulfilled.
158 * There should be a delay at the end equal to the longest I2C message
159 * to synchronize the BB-bit (in multimaster systems). How long is
160 * this? I assume 1 second is always long enough.
161 *
162 * vdovikin: added detect code for PCF8584
163 */
164static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
165{
166 unsigned char temp;
167
Roel Kluin0c168ce2009-03-28 21:34:42 +0100168 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
169 get_pcf(adap, 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* S1=0x80: S0 selected, serial interface off */
172 set_pcf(adap, 1, I2C_PCF_PIN);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100173 /*
174 * check to see S1 now used as R/W ctrl -
175 * PCF8584 does that when ESO is zero
176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
178 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
Uwe Kleine-König7d9b48e2010-01-28 22:09:43 +0100179 return -ENXIO; /* definitely not PCF8584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
Roel Kluin0c168ce2009-03-28 21:34:42 +0100182 /* load own address in S0, effective address is (own << 1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 i2c_outb(adap, get_own(adap));
184 /* check it's really written */
185 if ((temp = i2c_inb(adap)) != get_own(adap)) {
186 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
187 return -ENXIO;
188 }
189
Roel Kluin0c168ce2009-03-28 21:34:42 +0100190 /* S1=0xA0, next byte in S2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
192 /* check to see S2 now selected */
193 if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
194 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
195 return -ENXIO;
196 }
197
Roel Kluin0c168ce2009-03-28 21:34:42 +0100198 /* load clock register S2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 i2c_outb(adap, get_clock(adap));
200 /* check it's really written, the only 5 lowest bits does matter */
201 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
202 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
203 return -ENXIO;
204 }
205
Roel Kluin0c168ce2009-03-28 21:34:42 +0100206 /* Enable serial interface, idle, S0 selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 set_pcf(adap, 1, I2C_PCF_IDLE);
208
209 /* check to see PCF is really idled and we can access status register */
210 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
211 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
212 return -ENXIO;
213 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100214
David Millera672b4c2008-10-22 20:21:30 +0200215 printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 return 0;
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
Roel Kluin0c168ce2009-03-28 21:34:42 +0100221 int count, int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
224 int wrcount, status, timeout;
Roel Kluin0c168ce2009-03-28 21:34:42 +0100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 for (wrcount=0; wrcount<count; ++wrcount) {
227 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
Roel Kluin0c168ce2009-03-28 21:34:42 +0100228 buf[wrcount] & 0xff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 i2c_outb(adap, buf[wrcount]);
230 timeout = wait_for_pin(adap, &status);
231 if (timeout) {
Roel Kluin0c168ce2009-03-28 21:34:42 +0100232 if (timeout == -EINTR)
233 return -EINTR; /* arbitration lost */
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 i2c_stop(adap);
236 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
237 return -EREMOTEIO; /* got a better one ?? */
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (status & I2C_PCF_LRB) {
240 i2c_stop(adap);
241 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
242 return -EREMOTEIO; /* got a better one ?? */
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100245 if (last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 i2c_stop(adap);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100247 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 i2c_repstart(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Roel Kluin0c168ce2009-03-28 21:34:42 +0100250 return wrcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
Roel Kluin0c168ce2009-03-28 21:34:42 +0100254 int count, int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int i, status;
257 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
258 int wfp;
259
260 /* increment number of bytes to read by one -- read dummy byte */
261 for (i = 0; i <= count; i++) {
262
263 if ((wfp = wait_for_pin(adap, &status))) {
Roel Kluin0c168ce2009-03-28 21:34:42 +0100264 if (wfp == -EINTR)
265 return -EINTR; /* arbitration lost */
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 i2c_stop(adap);
268 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
Roel Kluin0c168ce2009-03-28 21:34:42 +0100269 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if ((status & I2C_PCF_LRB) && (i != count)) {
273 i2c_stop(adap);
274 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
Roel Kluin0c168ce2009-03-28 21:34:42 +0100275 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (i == count - 1) {
279 set_pcf(adap, 1, I2C_PCF_ESO);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100280 } else if (i == count) {
281 if (last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 i2c_stop(adap);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100283 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 i2c_repstart(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100286
287 if (i)
288 buf[i - 1] = i2c_inb(adap);
289 else
290 i2c_inb(adap); /* dummy read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Roel Kluin0c168ce2009-03-28 21:34:42 +0100293 return i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296
Jean Delvare6408a832008-01-27 18:14:46 +0100297static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
298 struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned short flags = msg->flags;
301 unsigned char addr;
Jean Delvare6408a832008-01-27 18:14:46 +0100302
303 addr = msg->addr << 1;
304 if (flags & I2C_M_RD)
305 addr |= 1;
306 if (flags & I2C_M_REV_DIR_ADDR)
307 addr ^= 1;
308 i2c_outb(adap, addr);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
312
313static int pcf_xfer(struct i2c_adapter *i2c_adap,
Roel Kluin0c168ce2009-03-28 21:34:42 +0100314 struct i2c_msg *msgs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int num)
316{
317 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
318 struct i2c_msg *pmsg;
319 int i;
320 int ret=0, timeout, status;
Roel Kluin0c168ce2009-03-28 21:34:42 +0100321
David Miller30091402008-10-22 20:21:30 +0200322 if (adap->xfer_begin)
323 adap->xfer_begin(adap->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Check for bus busy */
326 timeout = wait_for_bb(adap);
327 if (timeout) {
328 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
Roel Kluin0c168ce2009-03-28 21:34:42 +0100329 "Timeout waiting for BB in pcf_xfer\n");)
David Miller30091402008-10-22 20:21:30 +0200330 i = -EIO;
331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 for (i = 0;ret >= 0 && i < num; i++) {
335 pmsg = &msgs[i];
336
337 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
338 pmsg->flags & I2C_M_RD ? "read" : "write",
Roel Kluin0c168ce2009-03-28 21:34:42 +0100339 pmsg->len, pmsg->addr, i + 1, num);)
340
Jean Delvare6408a832008-01-27 18:14:46 +0100341 ret = pcf_doAddress(adap, pmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Send START */
Roel Kluin0c168ce2009-03-28 21:34:42 +0100344 if (i == 0)
345 i2c_start(adap);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* Wait for PIN (pending interrupt NOT) */
348 timeout = wait_for_pin(adap, &status);
349 if (timeout) {
350 if (timeout == -EINTR) {
351 /* arbitration lost */
David Miller30091402008-10-22 20:21:30 +0200352 i = -EINTR;
353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 i2c_stop(adap);
356 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
357 "for PIN(1) in pcf_xfer\n");)
David Miller30091402008-10-22 20:21:30 +0200358 i = -EREMOTEIO;
359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Check LRB (last rcvd bit - slave ack) */
363 if (status & I2C_PCF_LRB) {
364 i2c_stop(adap);
365 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
David Miller30091402008-10-22 20:21:30 +0200366 i = -EREMOTEIO;
367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
371 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
Roel Kluin0c168ce2009-03-28 21:34:42 +0100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (pmsg->flags & I2C_M_RD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
Roel Kluin0c168ce2009-03-28 21:34:42 +0100375 (i + 1 == num));
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (ret != pmsg->len) {
378 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
379 "only read %d bytes.\n",ret));
380 } else {
381 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
382 }
Roel Kluin0c168ce2009-03-28 21:34:42 +0100383 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
Roel Kluin0c168ce2009-03-28 21:34:42 +0100385 (i + 1 == num));
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (ret != pmsg->len) {
388 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
389 "only wrote %d bytes.\n",ret));
390 } else {
391 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
392 }
393 }
394 }
395
David Miller30091402008-10-22 20:21:30 +0200396out:
397 if (adap->xfer_end)
398 adap->xfer_end(adap->data);
Roel Kluin0c168ce2009-03-28 21:34:42 +0100399 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static u32 pcf_func(struct i2c_adapter *adap)
403{
Roel Kluin0c168ce2009-03-28 21:34:42 +0100404 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Jean Delvare6408a832008-01-27 18:14:46 +0100405 I2C_FUNC_PROTOCOL_MANGLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Roel Kluin0c168ce2009-03-28 21:34:42 +0100408/* exported algorithm data: */
Jean Delvare9e11a9f2006-09-03 22:38:52 +0200409static const struct i2c_algorithm pcf_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .master_xfer = pcf_xfer,
411 .functionality = pcf_func,
412};
413
Roel Kluin0c168ce2009-03-28 21:34:42 +0100414/*
415 * registering functions to load algorithms at runtime
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417int i2c_pcf_add_bus(struct i2c_adapter *adap)
418{
419 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
420 int rval;
421
422 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
423
424 /* register new adapter to i2c module... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 adap->algo = &pcf_algo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Mark M. Hoffmanb39ad0c2006-07-01 17:16:06 +0200427 if ((rval = pcf_init_8584(pcf_adap)))
428 return rval;
429
430 rval = i2c_add_adapter(adap);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return rval;
433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434EXPORT_SYMBOL(i2c_pcf_add_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
437MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
438MODULE_LICENSE("GPL");
439
440module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
441MODULE_PARM_DESC(i2c_debug,
Roel Kluin0c168ce2009-03-28 21:34:42 +0100442 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");