2323#include "i2c-algo-pcf.h"
2424
2525
26- #define DEB2 (x ) if (i2c_debug >= 2) x
27- #define DEB3 (x ) if (i2c_debug >= 3) x /* print several statistical values */
28- #define DEBPROTO (x ) if (i2c_debug >= 9) x;
29- /* debug the protocol by showing transferred bits */
3026#define DEF_TIMEOUT 16
3127
32- /*
33- * module parameters:
34- */
35- static int i2c_debug ;
36-
3728/* setting states on the bus with the right timing: */
3829
3930#define set_pcf (adap , ctl , val ) adap->setpcf(adap->data, ctl, val)
@@ -47,27 +38,21 @@ static int i2c_debug;
4738
4839static void i2c_start (struct i2c_algo_pcf_data * adap )
4940{
50- DEBPROTO (printk (KERN_DEBUG "S " ));
5141 set_pcf (adap , 1 , I2C_PCF_START );
5242}
5343
5444static void i2c_repstart (struct i2c_algo_pcf_data * adap )
5545{
56- DEBPROTO (printk (" Sr " ));
5746 set_pcf (adap , 1 , I2C_PCF_REPSTART );
5847}
5948
6049static void i2c_stop (struct i2c_algo_pcf_data * adap )
6150{
62- DEBPROTO (printk ("P\n" ));
6351 set_pcf (adap , 1 , I2C_PCF_STOP );
6452}
6553
6654static void handle_lab (struct i2c_algo_pcf_data * adap , const int * status )
6755{
68- DEB2 (printk (KERN_INFO
69- "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n" ,
70- * status ));
7156 /*
7257 * Cleanup from LAB -- reset and enable ESO.
7358 * This resets the PCF8584; since we've lost the bus, no
@@ -88,9 +73,6 @@ static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
8873 if (adap -> lab_mdelay )
8974 mdelay (adap -> lab_mdelay );
9075
91- DEB2 (printk (KERN_INFO
92- "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n" ,
93- get_pcf (adap , 1 )));
9476}
9577
9678static int wait_for_bb (struct i2c_algo_pcf_data * adap )
@@ -147,56 +129,48 @@ static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
147129 *
148130 * vdovikin: added detect code for PCF8584
149131 */
150- static int pcf_init_8584 (struct i2c_algo_pcf_data * adap )
132+ static int pcf_init_8584 (struct i2c_algo_pcf_data * adap )
151133{
152134 unsigned char temp ;
153135
154- DEB3 (printk (KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n" ,
155- get_pcf (adap , 1 )));
156-
157136 /* S1=0x80: S0 selected, serial interface off */
158137 set_pcf (adap , 1 , I2C_PCF_PIN );
159138 /*
160139 * check to see S1 now used as R/W ctrl -
161140 * PCF8584 does that when ESO is zero
162141 */
163- if ((( temp = get_pcf (adap , 1 )) & 0x7f ) != ( 0 )) {
164- DEB2 ( printk ( KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n" , temp ));
142+ temp = get_pcf (adap , 1 );
143+ if (( temp & 0x7f ) != 0 )
165144 return - ENXIO ; /* definitely not PCF8584 */
166- }
167145
168146 /* load own address in S0, effective address is (own << 1) */
169147 i2c_outb (adap , get_own (adap ));
170148 /* check it's really written */
171- if (( temp = i2c_inb (adap )) != get_own ( adap )) {
172- DEB2 ( printk ( KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n" , temp ));
149+ temp = i2c_inb (adap );
150+ if ( temp != get_own ( adap ))
173151 return - ENXIO ;
174- }
175152
176153 /* S1=0xA0, next byte in S2 */
177154 set_pcf (adap , 1 , I2C_PCF_PIN | I2C_PCF_ES1 );
178155 /* check to see S2 now selected */
179- if ((( temp = get_pcf (adap , 1 )) & 0x7f ) != I2C_PCF_ES1 ) {
180- DEB2 ( printk ( KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n" , temp ));
156+ temp = get_pcf (adap , 1 );
157+ if (( temp & 0x7f ) != I2C_PCF_ES1 )
181158 return - ENXIO ;
182- }
183159
184160 /* load clock register S2 */
185161 i2c_outb (adap , get_clock (adap ));
186162 /* check it's really written, the only 5 lowest bits does matter */
187- if ((( temp = i2c_inb (adap )) & 0x1f ) != get_clock ( adap )) {
188- DEB2 ( printk ( KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n" , temp ));
163+ temp = i2c_inb (adap );
164+ if (( temp & 0x1f ) != get_clock ( adap ))
189165 return - ENXIO ;
190- }
191166
192167 /* Enable serial interface, idle, S0 selected */
193168 set_pcf (adap , 1 , I2C_PCF_IDLE );
194169
195170 /* check to see PCF is really idled and we can access status register */
196- if (( temp = get_pcf (adap , 1 )) != ( I2C_PCF_PIN | I2C_PCF_BB )) {
197- DEB2 ( printk ( KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n" , temp ));
171+ temp = get_pcf (adap , 1 );
172+ if ( temp != ( I2C_PCF_PIN | I2C_PCF_BB ))
198173 return - ENXIO ;
199- }
200174
201175 printk (KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n" );
202176
@@ -209,9 +183,7 @@ static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
209183 struct i2c_algo_pcf_data * adap = i2c_adap -> algo_data ;
210184 int wrcount , status , timeout ;
211185
212- for (wrcount = 0 ; wrcount < count ; ++ wrcount ) {
213- DEB2 (dev_dbg (& i2c_adap -> dev , "i2c_write: writing %2.2X\n" ,
214- buf [wrcount ] & 0xff ));
186+ for (wrcount = 0 ; wrcount < count ; ++ wrcount ) {
215187 i2c_outb (adap , buf [wrcount ]);
216188 timeout = wait_for_pin (adap , & status );
217189 if (timeout ) {
@@ -246,7 +218,8 @@ static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
246218 /* increment number of bytes to read by one -- read dummy byte */
247219 for (i = 0 ; i <= count ; i ++ ) {
248220
249- if ((wfp = wait_for_pin (adap , & status ))) {
221+ wfp = wait_for_pin (adap , & status );
222+ if (wfp ) {
250223 if (wfp == - EINTR )
251224 return - EINTR ; /* arbitration lost */
252225
@@ -280,16 +253,14 @@ static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
280253}
281254
282255
283- static int pcf_doAddress (struct i2c_algo_pcf_data * adap ,
256+ static void pcf_send_address (struct i2c_algo_pcf_data * adap ,
284257 struct i2c_msg * msg )
285258{
286259 unsigned char addr = i2c_8bit_addr_from_msg (msg );
287260
288261 if (msg -> flags & I2C_M_REV_DIR_ADDR )
289262 addr ^= 1 ;
290263 i2c_outb (adap , addr );
291-
292- return 0 ;
293264}
294265
295266static int pcf_xfer (struct i2c_adapter * i2c_adap ,
@@ -299,28 +270,23 @@ static int pcf_xfer(struct i2c_adapter *i2c_adap,
299270 struct i2c_algo_pcf_data * adap = i2c_adap -> algo_data ;
300271 struct i2c_msg * pmsg ;
301272 int i ;
302- int ret = 0 , timeout , status ;
273+ int timeout , status ;
303274
304275 if (adap -> xfer_begin )
305276 adap -> xfer_begin (adap -> data );
306277
307278 /* Check for bus busy */
308279 timeout = wait_for_bb (adap );
309280 if (timeout ) {
310- DEB2 (printk (KERN_ERR "i2c-algo-pcf.o: "
311- "Timeout waiting for BB in pcf_xfer\n" );)
312281 i = - EIO ;
313282 goto out ;
314283 }
315284
316- for (i = 0 ;ret >= 0 && i < num ; i ++ ) {
317- pmsg = & msgs [i ];
318-
319- DEB2 (printk (KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n" ,
320- str_read_write (pmsg -> flags & I2C_M_RD ),
321- pmsg -> len , pmsg -> addr , i + 1 , num );)
285+ for (i = 0 ; i < num ; i ++ ) {
286+ int ret ;
322287
323- ret = pcf_doAddress (adap , pmsg );
288+ pmsg = & msgs [i ];
289+ pcf_send_address (adap , pmsg );
324290
325291 /* Send START */
326292 if (i == 0 )
@@ -335,44 +301,28 @@ static int pcf_xfer(struct i2c_adapter *i2c_adap,
335301 goto out ;
336302 }
337303 i2c_stop (adap );
338- DEB2 (printk (KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
339- "for PIN(1) in pcf_xfer\n" );)
340304 i = - EREMOTEIO ;
341305 goto out ;
342306 }
343307
344308 /* Check LRB (last rcvd bit - slave ack) */
345309 if (status & I2C_PCF_LRB ) {
346310 i2c_stop (adap );
347- DEB2 (printk (KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n" );)
348311 i = - EREMOTEIO ;
349312 goto out ;
350313 }
351314
352- DEB3 (printk (KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n" ,
353- i , msgs [i ].addr , msgs [i ].flags , msgs [i ].len );)
354315
355316 if (pmsg -> flags & I2C_M_RD ) {
356317 ret = pcf_readbytes (i2c_adap , pmsg -> buf , pmsg -> len ,
357318 (i + 1 == num ));
358-
359- if (ret != pmsg -> len ) {
360- DEB2 (printk (KERN_DEBUG "i2c-algo-pcf.o: fail: "
361- "only read %d bytes.\n" ,ret ));
362- } else {
363- DEB2 (printk (KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n" ,ret ));
364- }
365319 } else {
366320 ret = pcf_sendbytes (i2c_adap , pmsg -> buf , pmsg -> len ,
367321 (i + 1 == num ));
368-
369- if (ret != pmsg -> len ) {
370- DEB2 (printk (KERN_DEBUG "i2c-algo-pcf.o: fail: "
371- "only wrote %d bytes.\n" ,ret ));
372- } else {
373- DEB2 (printk (KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n" ,ret ));
374- }
375322 }
323+
324+ if (ret < 0 )
325+ goto out ;
376326 }
377327
378328out :
@@ -401,12 +351,11 @@ int i2c_pcf_add_bus(struct i2c_adapter *adap)
401351 struct i2c_algo_pcf_data * pcf_adap = adap -> algo_data ;
402352 int rval ;
403353
404- DEB2 (dev_dbg (& adap -> dev , "hw routines registered.\n" ));
405-
406354 /* register new adapter to i2c module... */
407355 adap -> algo = & pcf_algo ;
408356
409- if ((rval = pcf_init_8584 (pcf_adap )))
357+ rval = pcf_init_8584 (pcf_adap );
358+ if (rval )
410359 return rval ;
411360
412361 rval = i2c_add_adapter (adap );
@@ -418,7 +367,3 @@ EXPORT_SYMBOL(i2c_pcf_add_bus);
418367MODULE_AUTHOR ("Hans Berglund <hb@spacetec.no>" );
419368MODULE_DESCRIPTION ("I2C-Bus PCF8584 algorithm" );
420369MODULE_LICENSE ("GPL" );
421-
422- module_param (i2c_debug , int , S_IRUGO | S_IWUSR );
423- MODULE_PARM_DESC (i2c_debug ,
424- "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol" );
0 commit comments