Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/drivers/i2c/algos/i2c-algo-pca.c (Version 2.6.25) and /linux/drivers/i2c/algos/i2c-algo-pca.c (Version 2.6.31.13)


  1 /*                                                  1 /*
  2  *  i2c-algo-pca.c i2c driver algorithms for P !!   2  *  i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
  3  *    Copyright (C) 2004 Arcom Control Systems      3  *    Copyright (C) 2004 Arcom Control Systems
                                                   >>   4  *    Copyright (C) 2008 Pengutronix
  4  *                                                  5  *
  5  *  This program is free software; you can red      6  *  This program is free software; you can redistribute it and/or modify
  6  *  it under the terms of the GNU General Publ      7  *  it under the terms of the GNU General Public License as published by
  7  *  the Free Software Foundation; either versi      8  *  the Free Software Foundation; either version 2 of the License, or
  8  *  (at your option) any later version.             9  *  (at your option) any later version.
  9  *                                                 10  *
 10  *  This program is distributed in the hope th     11  *  This program is distributed in the hope that it will be useful,
 11  *  but WITHOUT ANY WARRANTY; without even the     12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  *  MERCHANTABILITY or FITNESS FOR A PARTICULA     13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  *  GNU General Public License for more detail     14  *  GNU General Public License for more details.
 14  *                                                 15  *
 15  *  You should have received a copy of the GNU     16  *  You should have received a copy of the GNU General Public License
 16  *  along with this program; if not, write to      17  *  along with this program; if not, write to the Free Software
 17  *  Foundation, Inc., 675 Mass Ave, Cambridge,     18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 18  */                                                19  */
 19                                                    20 
 20 #include <linux/kernel.h>                          21 #include <linux/kernel.h>
 21 #include <linux/module.h>                          22 #include <linux/module.h>
 22 #include <linux/moduleparam.h>                     23 #include <linux/moduleparam.h>
 23 #include <linux/delay.h>                           24 #include <linux/delay.h>
 24 #include <linux/slab.h>                        !!  25 #include <linux/jiffies.h>
 25 #include <linux/init.h>                            26 #include <linux/init.h>
 26 #include <linux/errno.h>                           27 #include <linux/errno.h>
 27 #include <linux/i2c.h>                             28 #include <linux/i2c.h>
 28 #include <linux/i2c-algo-pca.h>                    29 #include <linux/i2c-algo-pca.h>
 29 #include "i2c-algo-pca.h"                      << 
 30                                                    30 
 31 #define DRIVER "i2c-algo-pca"                  !!  31 #define DEB1(fmt, args...) do { if (i2c_debug >= 1)                     \
 32                                                !!  32                                  printk(KERN_DEBUG fmt, ## args); } while (0)
 33 #define DEB1(fmt, args...) do { if (i2c_debug> !!  33 #define DEB2(fmt, args...) do { if (i2c_debug >= 2)                     \
 34 #define DEB2(fmt, args...) do { if (i2c_debug> !!  34                                  printk(KERN_DEBUG fmt, ## args); } while (0)
 35 #define DEB3(fmt, args...) do { if (i2c_debug> !!  35 #define DEB3(fmt, args...) do { if (i2c_debug >= 3)                     \
                                                   >>  36                                  printk(KERN_DEBUG fmt, ## args); } while (0)
 36                                                    37 
 37 static int i2c_debug;                              38 static int i2c_debug;
 38                                                    39 
 39 #define pca_outw(adap, reg, val) adap->write_b !!  40 #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
 40 #define pca_inw(adap, reg) adap->read_byte(ada !!  41 #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
 41                                                    42 
 42 #define pca_status(adap) pca_inw(adap, I2C_PCA     43 #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
 43 #define pca_clock(adap) adap->get_clock(adap)  !!  44 #define pca_clock(adap) adap->i2c_clock
 44 #define pca_own(adap) adap->get_own(adap)      << 
 45 #define pca_set_con(adap, val) pca_outw(adap,      45 #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
 46 #define pca_get_con(adap) pca_inw(adap, I2C_PC     46 #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
 47 #define pca_wait(adap) adap->wait_for_interrup !!  47 #define pca_wait(adap) adap->wait_for_completion(adap->data)
                                                   >>  48 #define pca_reset(adap) adap->reset_chip(adap->data)
                                                   >>  49 
                                                   >>  50 static void pca9665_reset(void *pd)
                                                   >>  51 {
                                                   >>  52         struct i2c_algo_pca_data *adap = pd;
                                                   >>  53         pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);
                                                   >>  54         pca_outw(adap, I2C_PCA_IND, 0xA5);
                                                   >>  55         pca_outw(adap, I2C_PCA_IND, 0x5A);
                                                   >>  56 }
 48                                                    57 
 49 /*                                                 58 /*
 50  * Generate a start condition on the i2c bus.      59  * Generate a start condition on the i2c bus.
 51  *                                                 60  *
 52  * returns after the start condition has occur     61  * returns after the start condition has occurred
 53  */                                                62  */
 54 static void pca_start(struct i2c_algo_pca_data !!  63 static int pca_start(struct i2c_algo_pca_data *adap)
 55 {                                                  64 {
 56         int sta = pca_get_con(adap);               65         int sta = pca_get_con(adap);
 57         DEB2("=== START\n");                       66         DEB2("=== START\n");
 58         sta |= I2C_PCA_CON_STA;                    67         sta |= I2C_PCA_CON_STA;
 59         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_S     68         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
 60         pca_set_con(adap, sta);                    69         pca_set_con(adap, sta);
 61         pca_wait(adap);                        !!  70         return pca_wait(adap);
 62 }                                                  71 }
 63                                                    72 
 64 /*                                                 73 /*
 65  * Generate a repeated start condition on the      74  * Generate a repeated start condition on the i2c bus
 66  *                                                 75  *
 67  * return after the repeated start condition h     76  * return after the repeated start condition has occurred
 68  */                                                77  */
 69 static void pca_repeated_start(struct i2c_algo !!  78 static int pca_repeated_start(struct i2c_algo_pca_data *adap)
 70 {                                                  79 {
 71         int sta = pca_get_con(adap);               80         int sta = pca_get_con(adap);
 72         DEB2("=== REPEATED START\n");              81         DEB2("=== REPEATED START\n");
 73         sta |= I2C_PCA_CON_STA;                    82         sta |= I2C_PCA_CON_STA;
 74         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_S     83         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
 75         pca_set_con(adap, sta);                    84         pca_set_con(adap, sta);
 76         pca_wait(adap);                        !!  85         return pca_wait(adap);
 77 }                                                  86 }
 78                                                    87 
 79 /*                                                 88 /*
 80  * Generate a stop condition on the i2c bus        89  * Generate a stop condition on the i2c bus
 81  *                                                 90  *
 82  * returns after the stop condition has been g     91  * returns after the stop condition has been generated
 83  *                                                 92  *
 84  * STOPs do not generate an interrupt or set t     93  * STOPs do not generate an interrupt or set the SI flag, since the
 85  * part returns the idle state (0xf8). Hence w     94  * part returns the idle state (0xf8). Hence we don't need to
 86  * pca_wait here.                                  95  * pca_wait here.
 87  */                                                96  */
 88 static void pca_stop(struct i2c_algo_pca_data      97 static void pca_stop(struct i2c_algo_pca_data *adap)
 89 {                                                  98 {
 90         int sta = pca_get_con(adap);               99         int sta = pca_get_con(adap);
 91         DEB2("=== STOP\n");                       100         DEB2("=== STOP\n");
 92         sta |= I2C_PCA_CON_STO;                   101         sta |= I2C_PCA_CON_STO;
 93         sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_S    102         sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
 94         pca_set_con(adap, sta);                   103         pca_set_con(adap, sta);
 95 }                                                 104 }
 96                                                   105 
 97 /*                                                106 /*
 98  * Send the slave address and R/W bit             107  * Send the slave address and R/W bit
 99  *                                                108  *
100  * returns after the address has been sent        109  * returns after the address has been sent
101  */                                               110  */
102 static void pca_address(struct i2c_algo_pca_da !! 111 static int pca_address(struct i2c_algo_pca_data *adap,
103                         struct i2c_msg *msg)      112                         struct i2c_msg *msg)
104 {                                                 113 {
105         int sta = pca_get_con(adap);              114         int sta = pca_get_con(adap);
106         int addr;                                 115         int addr;
107                                                   116 
108         addr = ( (0x7f & msg->addr) << 1 );       117         addr = ( (0x7f & msg->addr) << 1 );
109         if (msg->flags & I2C_M_RD )               118         if (msg->flags & I2C_M_RD )
110                 addr |= 1;                        119                 addr |= 1;
111         DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x !! 120         DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
112              msg->addr, msg->flags & I2C_M_RD     121              msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
113                                                !! 122 
114         pca_outw(adap, I2C_PCA_DAT, addr);        123         pca_outw(adap, I2C_PCA_DAT, addr);
115                                                   124 
116         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_S    125         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
117         pca_set_con(adap, sta);                   126         pca_set_con(adap, sta);
118                                                   127 
119         pca_wait(adap);                        !! 128         return pca_wait(adap);
120 }                                                 129 }
121                                                   130 
122 /*                                                131 /*
123  * Transmit a byte.                               132  * Transmit a byte.
124  *                                                133  *
125  * Returns after the byte has been transmitted    134  * Returns after the byte has been transmitted
126  */                                               135  */
127 static void pca_tx_byte(struct i2c_algo_pca_da !! 136 static int pca_tx_byte(struct i2c_algo_pca_data *adap,
128                         __u8 b)                   137                         __u8 b)
129 {                                                 138 {
130         int sta = pca_get_con(adap);              139         int sta = pca_get_con(adap);
131         DEB2("=== WRITE %#04x\n", b);             140         DEB2("=== WRITE %#04x\n", b);
132         pca_outw(adap, I2C_PCA_DAT, b);           141         pca_outw(adap, I2C_PCA_DAT, b);
133                                                   142 
134         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_S    143         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
135         pca_set_con(adap, sta);                   144         pca_set_con(adap, sta);
136                                                   145 
137         pca_wait(adap);                        !! 146         return pca_wait(adap);
138 }                                                 147 }
139                                                   148 
140 /*                                                149 /*
141  * Receive a byte                                 150  * Receive a byte
142  *                                                151  *
143  * returns immediately.                           152  * returns immediately.
144  */                                               153  */
145 static void pca_rx_byte(struct i2c_algo_pca_da !! 154 static void pca_rx_byte(struct i2c_algo_pca_data *adap,
146                         __u8 *b, int ack)         155                         __u8 *b, int ack)
147 {                                                 156 {
148         *b = pca_inw(adap, I2C_PCA_DAT);          157         *b = pca_inw(adap, I2C_PCA_DAT);
149         DEB2("=== READ %#04x %s\n", *b, ack ?     158         DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
150 }                                                 159 }
151                                                   160 
152 /*                                             !! 161 /*
153  * Setup ACK or NACK for next received byte an    162  * Setup ACK or NACK for next received byte and wait for it to arrive.
154  *                                                163  *
155  * Returns after next byte has arrived.           164  * Returns after next byte has arrived.
156  */                                               165  */
157 static void pca_rx_ack(struct i2c_algo_pca_dat !! 166 static int pca_rx_ack(struct i2c_algo_pca_data *adap,
158                        int ack)                   167                        int ack)
159 {                                                 168 {
160         int sta = pca_get_con(adap);              169         int sta = pca_get_con(adap);
161                                                   170 
162         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_S    171         sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
163                                                   172 
164         if ( ack )                                173         if ( ack )
165                 sta |= I2C_PCA_CON_AA;            174                 sta |= I2C_PCA_CON_AA;
166                                                   175 
167         pca_set_con(adap, sta);                   176         pca_set_con(adap, sta);
168         pca_wait(adap);                        !! 177         return pca_wait(adap);
169 }                                              << 
170                                                << 
171 /*                                             << 
172  * Reset the i2c bus / SIO                     << 
173  */                                            << 
174 static void pca_reset(struct i2c_algo_pca_data << 
175 {                                              << 
176         /* apparently only an external reset w << 
177         printk(KERN_ERR DRIVER ": Haven't figu << 
178 }                                                 178 }
179                                                   179 
180 static int pca_xfer(struct i2c_adapter *i2c_ad    180 static int pca_xfer(struct i2c_adapter *i2c_adap,
181                     struct i2c_msg *msgs,         181                     struct i2c_msg *msgs,
182                     int num)                      182                     int num)
183 {                                                 183 {
184         struct i2c_algo_pca_data *adap = i2c_a    184         struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
185         struct i2c_msg *msg = NULL;               185         struct i2c_msg *msg = NULL;
186         int curmsg;                               186         int curmsg;
187         int numbytes = 0;                         187         int numbytes = 0;
188         int state;                                188         int state;
189         int ret;                                  189         int ret;
190         int timeout = 100;                     !! 190         int completed = 1;
                                                   >> 191         unsigned long timeout = jiffies + i2c_adap->timeout;
191                                                   192 
192         while ((state = pca_status(adap)) != 0 !! 193         while ((state = pca_status(adap)) != 0xf8) {
193                 msleep(10);                    !! 194                 if (time_before(jiffies, timeout)) {
194         }                                      !! 195                         msleep(10);
195         if (state != 0xf8) {                   !! 196                 } else {
196                 dev_dbg(&i2c_adap->dev, "bus i !! 197                         dev_dbg(&i2c_adap->dev, "bus is not idle. status is "
197                 return -EIO;                   !! 198                                 "%#04x\n", state);
                                                   >> 199                         return -EAGAIN;
                                                   >> 200                 }
198         }                                         201         }
199                                                   202 
200         DEB1("{{{ XFER %d messages\n", num);      203         DEB1("{{{ XFER %d messages\n", num);
201                                                   204 
202         if (i2c_debug>=2) {                       205         if (i2c_debug>=2) {
203                 for (curmsg = 0; curmsg < num;    206                 for (curmsg = 0; curmsg < num; curmsg++) {
204                         int addr, i;              207                         int addr, i;
205                         msg = &msgs[curmsg];      208                         msg = &msgs[curmsg];
206                                                !! 209 
207                         addr = (0x7f & msg->ad    210                         addr = (0x7f & msg->addr) ;
208                                                !! 211 
209                         if (msg->flags & I2C_M    212                         if (msg->flags & I2C_M_RD )
210                                 printk(KERN_IN !! 213                                 printk(KERN_INFO "    [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
211                                        curmsg,    214                                        curmsg, msg->len, addr, (addr<<1) | 1);
212                         else {                    215                         else {
213                                 printk(KERN_IN !! 216                                 printk(KERN_INFO "    [%02d] WR %d bytes to %#02x [%#02x%s",
214                                        curmsg,    217                                        curmsg, msg->len, addr, addr<<1,
215                                        msg->le    218                                        msg->len == 0 ? "" : ", ");
216                                 for(i=0; i < m    219                                 for(i=0; i < msg->len; i++)
217                                         printk    220                                         printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
218                                 printk("]\n");    221                                 printk("]\n");
219                         }                         222                         }
220                 }                                 223                 }
221         }                                         224         }
222                                                   225 
223         curmsg = 0;                               226         curmsg = 0;
224         ret = -EREMOTEIO;                         227         ret = -EREMOTEIO;
225         while (curmsg < num) {                    228         while (curmsg < num) {
226                 state = pca_status(adap);         229                 state = pca_status(adap);
227                                                   230 
228                 DEB3("STATE is 0x%02x\n", stat    231                 DEB3("STATE is 0x%02x\n", state);
229                 msg = &msgs[curmsg];              232                 msg = &msgs[curmsg];
230                                                   233 
231                 switch (state) {                  234                 switch (state) {
232                 case 0xf8: /* On reset or stop    235                 case 0xf8: /* On reset or stop the bus is idle */
233                         pca_start(adap);       !! 236                         completed = pca_start(adap);
234                         break;                    237                         break;
235                                                   238 
236                 case 0x08: /* A START conditio    239                 case 0x08: /* A START condition has been transmitted */
237                 case 0x10: /* A repeated start    240                 case 0x10: /* A repeated start condition has been transmitted */
238                         pca_address(adap, msg) !! 241                         completed = pca_address(adap, msg);
239                         break;                    242                         break;
240                                                !! 243 
241                 case 0x18: /* SLA+W has been t    244                 case 0x18: /* SLA+W has been transmitted; ACK has been received */
242                 case 0x28: /* Data byte in I2C    245                 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
243                         if (numbytes < msg->le    246                         if (numbytes < msg->len) {
244                                 pca_tx_byte(ad !! 247                                 completed = pca_tx_byte(adap,
                                                   >> 248                                                         msg->buf[numbytes]);
245                                 numbytes++;       249                                 numbytes++;
246                                 break;            250                                 break;
247                         }                         251                         }
248                         curmsg++; numbytes = 0    252                         curmsg++; numbytes = 0;
249                         if (curmsg == num)        253                         if (curmsg == num)
250                                 pca_stop(adap)    254                                 pca_stop(adap);
251                         else                      255                         else
252                                 pca_repeated_s !! 256                                 completed = pca_repeated_start(adap);
253                         break;                    257                         break;
254                                                   258 
255                 case 0x20: /* SLA+W has been t    259                 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
256                         DEB2("NOT ACK received    260                         DEB2("NOT ACK received after SLA+W\n");
257                         pca_stop(adap);           261                         pca_stop(adap);
258                         goto out;                 262                         goto out;
259                                                   263 
260                 case 0x40: /* SLA+R has been t    264                 case 0x40: /* SLA+R has been transmitted; ACK has been received */
261                         pca_rx_ack(adap, msg-> !! 265                         completed = pca_rx_ack(adap, msg->len > 1);
262                         break;                    266                         break;
263                                                   267 
264                 case 0x50: /* Data bytes has b    268                 case 0x50: /* Data bytes has been received; ACK has been returned */
265                         if (numbytes < msg->le    269                         if (numbytes < msg->len) {
266                                 pca_rx_byte(ad    270                                 pca_rx_byte(adap, &msg->buf[numbytes], 1);
267                                 numbytes++;       271                                 numbytes++;
268                                 pca_rx_ack(ada !! 272                                 completed = pca_rx_ack(adap,
                                                   >> 273                                                        numbytes < msg->len - 1);
269                                 break;            274                                 break;
270                         }                         275                         }
271                         curmsg++; numbytes = 0    276                         curmsg++; numbytes = 0;
272                         if (curmsg == num)        277                         if (curmsg == num)
273                                 pca_stop(adap)    278                                 pca_stop(adap);
274                         else                      279                         else
275                                 pca_repeated_s !! 280                                 completed = pca_repeated_start(adap);
276                         break;                    281                         break;
277                                                   282 
278                 case 0x48: /* SLA+R has been t    283                 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
279                         DEB2("NOT ACK received    284                         DEB2("NOT ACK received after SLA+R\n");
280                         pca_stop(adap);           285                         pca_stop(adap);
281                         goto out;                 286                         goto out;
282                                                   287 
283                 case 0x30: /* Data byte in I2C    288                 case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
284                         DEB2("NOT ACK received    289                         DEB2("NOT ACK received after data byte\n");
                                                   >> 290                         pca_stop(adap);
285                         goto out;                 291                         goto out;
286                                                   292 
287                 case 0x38: /* Arbitration lost    293                 case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
288                         DEB2("Arbitration lost    294                         DEB2("Arbitration lost\n");
                                                   >> 295                         /*
                                                   >> 296                          * The PCA9564 data sheet (2006-09-01) says "A
                                                   >> 297                          * START condition will be transmitted when the
                                                   >> 298                          * bus becomes free (STOP or SCL and SDA high)"
                                                   >> 299                          * when the STA bit is set (p. 11).
                                                   >> 300                          *
                                                   >> 301                          * In case this won't work, try pca_reset()
                                                   >> 302                          * instead.
                                                   >> 303                          */
                                                   >> 304                         pca_start(adap);
289                         goto out;                 305                         goto out;
290                                                !! 306 
291                 case 0x58: /* Data byte has be    307                 case 0x58: /* Data byte has been received; NOT ACK has been returned */
292                         if ( numbytes == msg->    308                         if ( numbytes == msg->len - 1 ) {
293                                 pca_rx_byte(ad    309                                 pca_rx_byte(adap, &msg->buf[numbytes], 0);
294                                 curmsg++; numb    310                                 curmsg++; numbytes = 0;
295                                 if (curmsg ==     311                                 if (curmsg == num)
296                                         pca_st    312                                         pca_stop(adap);
297                                 else              313                                 else
298                                         pca_re !! 314                                         completed = pca_repeated_start(adap);
299                         } else {                  315                         } else {
300                                 DEB2("NOT ACK     316                                 DEB2("NOT ACK sent after data byte received. "
301                                      "Not fina    317                                      "Not final byte. numbytes %d. len %d\n",
302                                      numbytes,    318                                      numbytes, msg->len);
303                                 pca_stop(adap)    319                                 pca_stop(adap);
304                                 goto out;         320                                 goto out;
305                         }                         321                         }
306                         break;                    322                         break;
307                 case 0x70: /* Bus error - SDA     323                 case 0x70: /* Bus error - SDA stuck low */
308                         DEB2("BUS ERROR - SDA     324                         DEB2("BUS ERROR - SDA Stuck low\n");
309                         pca_reset(adap);          325                         pca_reset(adap);
310                         goto out;                 326                         goto out;
311                 case 0x90: /* Bus error - SCL     327                 case 0x90: /* Bus error - SCL stuck low */
312                         DEB2("BUS ERROR - SCL     328                         DEB2("BUS ERROR - SCL Stuck low\n");
313                         pca_reset(adap);          329                         pca_reset(adap);
314                         goto out;                 330                         goto out;
315                 case 0x00: /* Bus error during    331                 case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
316                         DEB2("BUS ERROR - Ille    332                         DEB2("BUS ERROR - Illegal START or STOP\n");
317                         pca_reset(adap);          333                         pca_reset(adap);
318                         goto out;                 334                         goto out;
319                 default:                          335                 default:
320                         printk(KERN_ERR DRIVER !! 336                         dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
321                         break;                    337                         break;
322                 }                                 338                 }
323                                                !! 339 
                                                   >> 340                 if (!completed)
                                                   >> 341                         goto out;
324         }                                         342         }
325                                                   343 
326         ret = curmsg;                             344         ret = curmsg;
327  out:                                             345  out:
328         DEB1(KERN_CRIT "}}} transfered %d/%d m !! 346         DEB1("}}} transfered %d/%d messages. "
329              "status is %#04x. control is %#04 !! 347              "status is %#04x. control is %#04x\n",
330              curmsg, num, pca_status(adap),       348              curmsg, num, pca_status(adap),
331              pca_get_con(adap));                  349              pca_get_con(adap));
332         return ret;                               350         return ret;
333 }                                                 351 }
334                                                   352 
335 static u32 pca_func(struct i2c_adapter *adap)     353 static u32 pca_func(struct i2c_adapter *adap)
336 {                                                 354 {
337         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_E    355         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
338 }                                                 356 }
339                                                   357 
340 static int pca_init(struct i2c_algo_pca_data * !! 358 static const struct i2c_algorithm pca_algo = {
                                                   >> 359         .master_xfer    = pca_xfer,
                                                   >> 360         .functionality  = pca_func,
                                                   >> 361 };
                                                   >> 362 
                                                   >> 363 static unsigned int pca_probe_chip(struct i2c_adapter *adap)
341 {                                                 364 {
342         static int freqs[] = {330,288,217,146, !! 365         struct i2c_algo_pca_data *pca_data = adap->algo_data;
343         int own, clock;                        !! 366         /* The trick here is to check if there is an indirect register
                                                   >> 367          * available. If there is one, we will read the value we first
                                                   >> 368          * wrote on I2C_PCA_IADR. Otherwise, we will read the last value
                                                   >> 369          * we wrote on I2C_PCA_ADR
                                                   >> 370          */
                                                   >> 371         pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
                                                   >> 372         pca_outw(pca_data, I2C_PCA_IND, 0xAA);
                                                   >> 373         pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ITO);
                                                   >> 374         pca_outw(pca_data, I2C_PCA_IND, 0x00);
                                                   >> 375         pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
                                                   >> 376         if (pca_inw(pca_data, I2C_PCA_IND) == 0xAA) {
                                                   >> 377                 printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name);
                                                   >> 378                 return I2C_PCA_CHIP_9665;
                                                   >> 379         } else {
                                                   >> 380                 printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);
                                                   >> 381                 return I2C_PCA_CHIP_9564;
                                                   >> 382         }
                                                   >> 383 }
                                                   >> 384 
                                                   >> 385 static int pca_init(struct i2c_adapter *adap)
                                                   >> 386 {
                                                   >> 387         struct i2c_algo_pca_data *pca_data = adap->algo_data;
                                                   >> 388 
                                                   >> 389         adap->algo = &pca_algo;
                                                   >> 390 
                                                   >> 391         if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {
                                                   >> 392                 static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
                                                   >> 393                 int clock;
                                                   >> 394 
                                                   >> 395                 if (pca_data->i2c_clock > 7) {
                                                   >> 396                         switch (pca_data->i2c_clock) {
                                                   >> 397                         case 330000:
                                                   >> 398                                 pca_data->i2c_clock = I2C_PCA_CON_330kHz;
                                                   >> 399                                 break;
                                                   >> 400                         case 288000:
                                                   >> 401                                 pca_data->i2c_clock = I2C_PCA_CON_288kHz;
                                                   >> 402                                 break;
                                                   >> 403                         case 217000:
                                                   >> 404                                 pca_data->i2c_clock = I2C_PCA_CON_217kHz;
                                                   >> 405                                 break;
                                                   >> 406                         case 146000:
                                                   >> 407                                 pca_data->i2c_clock = I2C_PCA_CON_146kHz;
                                                   >> 408                                 break;
                                                   >> 409                         case 88000:
                                                   >> 410                                 pca_data->i2c_clock = I2C_PCA_CON_88kHz;
                                                   >> 411                                 break;
                                                   >> 412                         case 59000:
                                                   >> 413                                 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
                                                   >> 414                                 break;
                                                   >> 415                         case 44000:
                                                   >> 416                                 pca_data->i2c_clock = I2C_PCA_CON_44kHz;
                                                   >> 417                                 break;
                                                   >> 418                         case 36000:
                                                   >> 419                                 pca_data->i2c_clock = I2C_PCA_CON_36kHz;
                                                   >> 420                                 break;
                                                   >> 421                         default:
                                                   >> 422                                 printk(KERN_WARNING
                                                   >> 423                                         "%s: Invalid I2C clock speed selected."
                                                   >> 424                                         " Using default 59kHz.\n", adap->name);
                                                   >> 425                         pca_data->i2c_clock = I2C_PCA_CON_59kHz;
                                                   >> 426                         }
                                                   >> 427                 } else {
                                                   >> 428                         printk(KERN_WARNING "%s: "
                                                   >> 429                                 "Choosing the clock frequency based on "
                                                   >> 430                                 "index is deprecated."
                                                   >> 431                                 " Use the nominal frequency.\n", adap->name);
                                                   >> 432                 }
                                                   >> 433 
                                                   >> 434                 pca_reset(pca_data);
                                                   >> 435 
                                                   >> 436                 clock = pca_clock(pca_data);
                                                   >> 437                 printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
                                                   >> 438                      adap->name, freqs[clock]);
                                                   >> 439 
                                                   >> 440                 pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
                                                   >> 441         } else {
                                                   >> 442                 int clock;
                                                   >> 443                 int mode;
                                                   >> 444                 int tlow, thi;
                                                   >> 445                 /* Values can be found on PCA9665 datasheet section 7.3.2.6 */
                                                   >> 446                 int min_tlow, min_thi;
                                                   >> 447                 /* These values are the maximum raise and fall values allowed
                                                   >> 448                  * by the I2C operation mode (Standard, Fast or Fast+)
                                                   >> 449                  * They are used (added) below to calculate the clock dividers
                                                   >> 450                  * of PCA9665. Note that they are slightly different of the
                                                   >> 451                  * real maximum, to allow the change on mode exactly on the
                                                   >> 452                  * maximum clock rate for each mode
                                                   >> 453                  */
                                                   >> 454                 int raise_fall_time;
                                                   >> 455 
                                                   >> 456                 struct i2c_algo_pca_data *pca_data = adap->algo_data;
                                                   >> 457 
                                                   >> 458                 /* Ignore the reset function from the module,
                                                   >> 459                  * we can use the parallel bus reset
                                                   >> 460                  */
                                                   >> 461                 pca_data->reset_chip = pca9665_reset;
                                                   >> 462 
                                                   >> 463                 if (pca_data->i2c_clock > 1265800) {
                                                   >> 464                         printk(KERN_WARNING "%s: I2C clock speed too high."
                                                   >> 465                                 " Using 1265.8kHz.\n", adap->name);
                                                   >> 466                         pca_data->i2c_clock = 1265800;
                                                   >> 467                 }
                                                   >> 468 
                                                   >> 469                 if (pca_data->i2c_clock < 60300) {
                                                   >> 470                         printk(KERN_WARNING "%s: I2C clock speed too low."
                                                   >> 471                                 " Using 60.3kHz.\n", adap->name);
                                                   >> 472                         pca_data->i2c_clock = 60300;
                                                   >> 473                 }
                                                   >> 474 
                                                   >> 475                 /* To avoid integer overflow, use clock/100 for calculations */
                                                   >> 476                 clock = pca_clock(pca_data) / 100;
                                                   >> 477 
                                                   >> 478                 if (pca_data->i2c_clock > 10000) {
                                                   >> 479                         mode = I2C_PCA_MODE_TURBO;
                                                   >> 480                         min_tlow = 14;
                                                   >> 481                         min_thi  = 5;
                                                   >> 482                         raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
                                                   >> 483                 } else if (pca_data->i2c_clock > 4000) {
                                                   >> 484                         mode = I2C_PCA_MODE_FASTP;
                                                   >> 485                         min_tlow = 17;
                                                   >> 486                         min_thi  = 9;
                                                   >> 487                         raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
                                                   >> 488                 } else if (pca_data->i2c_clock > 1000) {
                                                   >> 489                         mode = I2C_PCA_MODE_FAST;
                                                   >> 490                         min_tlow = 44;
                                                   >> 491                         min_thi  = 20;
                                                   >> 492                         raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */
                                                   >> 493                 } else {
                                                   >> 494                         mode = I2C_PCA_MODE_STD;
                                                   >> 495                         min_tlow = 157;
                                                   >> 496                         min_thi  = 134;
                                                   >> 497                         raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */
                                                   >> 498                 }
                                                   >> 499 
                                                   >> 500                 /* The minimum clock that respects the thi/tlow = 134/157 is
                                                   >> 501                  * 64800 Hz. Below that, we have to fix the tlow to 255 and
                                                   >> 502                  * calculate the thi factor.
                                                   >> 503                  */
                                                   >> 504                 if (clock < 648) {
                                                   >> 505                         tlow = 255;
                                                   >> 506                         thi = 1000000 - clock * raise_fall_time;
                                                   >> 507                         thi /= (I2C_PCA_OSC_PER * clock) - tlow;
                                                   >> 508                 } else {
                                                   >> 509                         tlow = (1000000 - clock * raise_fall_time) * min_tlow;
                                                   >> 510                         tlow /= I2C_PCA_OSC_PER * clock * (min_thi + min_tlow);
                                                   >> 511                         thi = tlow * min_thi / min_tlow;
                                                   >> 512                 }
344                                                   513 
345         own = pca_own(adap);                   !! 514                 pca_reset(pca_data);
346         clock = pca_clock(adap);               << 
347         DEB1(KERN_INFO DRIVER ": own address i << 
348         DEB1(KERN_INFO DRIVER ": clock freqeun << 
349                                                   515 
350         pca_outw(adap, I2C_PCA_ADR, own << 1); !! 516                 printk(KERN_INFO
                                                   >> 517                      "%s: Clock frequency is %dHz\n", adap->name, clock * 100);
351                                                   518 
352         pca_set_con(adap, I2C_PCA_CON_ENSIO |  !! 519                 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IMODE);
353         udelay(500); /* 500 µs for oscilator  !! 520                 pca_outw(pca_data, I2C_PCA_IND, mode);
                                                   >> 521                 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLL);
                                                   >> 522                 pca_outw(pca_data, I2C_PCA_IND, tlow);
                                                   >> 523                 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLH);
                                                   >> 524                 pca_outw(pca_data, I2C_PCA_IND, thi);
                                                   >> 525 
                                                   >> 526                 pca_set_con(pca_data, I2C_PCA_CON_ENSIO);
                                                   >> 527         }
                                                   >> 528         udelay(500); /* 500 us for oscilator to stabilise */
354                                                   529 
355         return 0;                                 530         return 0;
356 }                                                 531 }
357                                                   532 
358 static const struct i2c_algorithm pca_algo = { !! 533 /*
359         .master_xfer    = pca_xfer,            !! 534  * registering functions to load algorithms at runtime
360         .functionality  = pca_func,            << 
361 };                                             << 
362                                                << 
363 /*                                             << 
364  * registering functions to load algorithms at << 
365  */                                               535  */
366 int i2c_pca_add_bus(struct i2c_adapter *adap)     536 int i2c_pca_add_bus(struct i2c_adapter *adap)
367 {                                                 537 {
368         struct i2c_algo_pca_data *pca_adap = a << 
369         int rval;                                 538         int rval;
370                                                   539 
371         /* register new adapter to i2c module. !! 540         rval = pca_init(adap);
372         adap->algo = &pca_algo;                !! 541         if (rval)
                                                   >> 542                 return rval;
373                                                   543 
374         adap->timeout = 100;            /* def !! 544         return i2c_add_adapter(adap);
375         adap->retries = 3;              /* be  !! 545 }
                                                   >> 546 EXPORT_SYMBOL(i2c_pca_add_bus);
376                                                   547 
377         if ((rval = pca_init(pca_adap)))       !! 548 int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
378                 return rval;                   !! 549 {
                                                   >> 550         int rval;
379                                                   551 
380         rval = i2c_add_adapter(adap);          !! 552         rval = pca_init(adap);
                                                   >> 553         if (rval)
                                                   >> 554                 return rval;
381                                                   555 
382         return rval;                           !! 556         return i2c_add_numbered_adapter(adap);
383 }                                                 557 }
384 EXPORT_SYMBOL(i2c_pca_add_bus);                !! 558 EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
385                                                   559 
386 MODULE_AUTHOR("Ian Campbell <icampbell@arcom.c !! 560 MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
387 MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm" !! 561         "Wolfram Sang <w.sang@pengutronix.de>");
                                                   >> 562 MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");
388 MODULE_LICENSE("GPL");                            563 MODULE_LICENSE("GPL");
389                                                   564 
390 module_param(i2c_debug, int, 0);                  565 module_param(i2c_debug, int, 0);
391                                                   566 
  This page was automatically generated by the LXR engine.